Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 1 | //===-- SelectionDAGBuilder.h - Selection-DAG building --------------------===// |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 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. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 14 | #ifndef SELECTIONDAGBUILDER_H |
| 15 | #define SELECTIONDAGBUILDER_H |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 16 | |
| 17 | #include "llvm/Constants.h" |
Owen Anderson | 0a5372e | 2009-07-13 04:09:18 +0000 | [diff] [blame] | 18 | #include "llvm/CodeGen/SelectionDAG.h" |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/APInt.h" |
| 20 | #include "llvm/ADT/DenseMap.h" |
| 21 | #ifndef NDEBUG |
| 22 | #include "llvm/ADT/SmallSet.h" |
| 23 | #endif |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 24 | #include "llvm/CodeGen/SelectionDAGNodes.h" |
Bill Wendling | 0eb96fd | 2009-02-03 01:32:22 +0000 | [diff] [blame] | 25 | #include "llvm/CodeGen/ValueTypes.h" |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 26 | #include "llvm/Support/CallSite.h" |
Torok Edwin | c25e758 | 2009-07-11 20:10:48 +0000 | [diff] [blame] | 27 | #include "llvm/Support/ErrorHandling.h" |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 28 | #include <vector> |
| 29 | #include <set> |
| 30 | |
| 31 | namespace llvm { |
| 32 | |
| 33 | class AliasAnalysis; |
| 34 | class AllocaInst; |
| 35 | class BasicBlock; |
| 36 | class BitCastInst; |
| 37 | class BranchInst; |
| 38 | class CallInst; |
| 39 | class ExtractElementInst; |
| 40 | class ExtractValueInst; |
| 41 | class FCmpInst; |
| 42 | class FPExtInst; |
| 43 | class FPToSIInst; |
| 44 | class FPToUIInst; |
| 45 | class FPTruncInst; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 46 | class Function; |
Dan Gohman | 6277eb2 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 47 | class FunctionLoweringInfo; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 48 | class GetElementPtrInst; |
| 49 | class GCFunctionInfo; |
| 50 | class ICmpInst; |
| 51 | class IntToPtrInst; |
Chris Lattner | ab21db7 | 2009-10-28 00:19:10 +0000 | [diff] [blame] | 52 | class IndirectBrInst; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 53 | class InvokeInst; |
| 54 | class InsertElementInst; |
| 55 | class InsertValueInst; |
| 56 | class Instruction; |
| 57 | class LoadInst; |
| 58 | class MachineBasicBlock; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 59 | class MachineInstr; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 60 | class MachineRegisterInfo; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 61 | class PHINode; |
| 62 | class PtrToIntInst; |
| 63 | class ReturnInst; |
| 64 | class SDISelAsmOperandInfo; |
| 65 | class SExtInst; |
| 66 | class SelectInst; |
| 67 | class ShuffleVectorInst; |
| 68 | class SIToFPInst; |
| 69 | class StoreInst; |
| 70 | class SwitchInst; |
| 71 | class TargetData; |
| 72 | class TargetLowering; |
| 73 | class TruncInst; |
| 74 | class UIToFPInst; |
| 75 | class UnreachableInst; |
| 76 | class UnwindInst; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 77 | class VAArgInst; |
| 78 | class ZExtInst; |
| 79 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 80 | //===----------------------------------------------------------------------===// |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 81 | /// SelectionDAGBuilder - This is the common target-independent lowering |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 82 | /// implementation that is parameterized by a TargetLowering object. |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 83 | /// |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 84 | class SelectionDAGBuilder { |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 85 | /// CurDebugLoc - current file + line number. Changes as we build the DAG. |
| 86 | DebugLoc CurDebugLoc; |
| 87 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 88 | DenseMap<const Value*, SDValue> NodeMap; |
| 89 | |
Chris Lattner | 8047d9a | 2009-12-24 00:37:38 +0000 | [diff] [blame] | 90 | public: |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 91 | /// PendingLoads - Loads are not emitted to the program immediately. We bunch |
| 92 | /// them up and then emit token factor nodes when possible. This allows us to |
| 93 | /// get simple disambiguation between loads without worrying about alias |
| 94 | /// analysis. |
| 95 | SmallVector<SDValue, 8> PendingLoads; |
Chris Lattner | 8047d9a | 2009-12-24 00:37:38 +0000 | [diff] [blame] | 96 | private: |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 97 | |
| 98 | /// PendingExports - CopyToReg nodes that copy values to virtual registers |
| 99 | /// for export to other blocks need to be emitted before any terminator |
| 100 | /// instruction, but they have no other ordering requirements. We bunch them |
| 101 | /// up and the emit a single tokenfactor for them just before terminator |
| 102 | /// instructions. |
| 103 | SmallVector<SDValue, 8> PendingExports; |
| 104 | |
Bill Wendling | b4e6a5d | 2009-12-18 23:32:53 +0000 | [diff] [blame] | 105 | /// SDNodeOrder - A unique monotonically increasing number used to order the |
| 106 | /// SDNodes we create. |
| 107 | unsigned SDNodeOrder; |
| 108 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 109 | /// Case - A struct to record the Value for a switch case, and the |
| 110 | /// case's target basic block. |
| 111 | struct Case { |
| 112 | Constant* Low; |
| 113 | Constant* High; |
| 114 | MachineBasicBlock* BB; |
| 115 | |
| 116 | Case() : Low(0), High(0), BB(0) { } |
| 117 | Case(Constant* low, Constant* high, MachineBasicBlock* bb) : |
| 118 | Low(low), High(high), BB(bb) { } |
Chris Lattner | e880efe | 2009-11-07 07:50:34 +0000 | [diff] [blame] | 119 | APInt size() const { |
| 120 | const APInt &rHigh = cast<ConstantInt>(High)->getValue(); |
| 121 | const APInt &rLow = cast<ConstantInt>(Low)->getValue(); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 122 | return (rHigh - rLow + 1ULL); |
| 123 | } |
| 124 | }; |
| 125 | |
| 126 | struct CaseBits { |
| 127 | uint64_t Mask; |
| 128 | MachineBasicBlock* BB; |
| 129 | unsigned Bits; |
| 130 | |
| 131 | CaseBits(uint64_t mask, MachineBasicBlock* bb, unsigned bits): |
| 132 | Mask(mask), BB(bb), Bits(bits) { } |
| 133 | }; |
| 134 | |
| 135 | typedef std::vector<Case> CaseVector; |
| 136 | typedef std::vector<CaseBits> CaseBitsVector; |
| 137 | typedef CaseVector::iterator CaseItr; |
| 138 | typedef std::pair<CaseItr, CaseItr> CaseRange; |
| 139 | |
| 140 | /// CaseRec - A struct with ctor used in lowering switches to a binary tree |
| 141 | /// of conditional branches. |
| 142 | struct CaseRec { |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 143 | CaseRec(MachineBasicBlock *bb, const Constant *lt, const Constant *ge, |
| 144 | CaseRange r) : |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 145 | CaseBB(bb), LT(lt), GE(ge), Range(r) {} |
| 146 | |
| 147 | /// CaseBB - The MBB in which to emit the compare and branch |
| 148 | MachineBasicBlock *CaseBB; |
| 149 | /// LT, GE - If nonzero, we know the current case value must be less-than or |
| 150 | /// greater-than-or-equal-to these Constants. |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 151 | const Constant *LT; |
| 152 | const Constant *GE; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 153 | /// Range - A pair of iterators representing the range of case values to be |
| 154 | /// processed at this point in the binary search tree. |
| 155 | CaseRange Range; |
| 156 | }; |
| 157 | |
| 158 | typedef std::vector<CaseRec> CaseRecVector; |
| 159 | |
| 160 | /// The comparison function for sorting the switch case values in the vector. |
| 161 | /// WARNING: Case ranges should be disjoint! |
| 162 | struct CaseCmp { |
Chris Lattner | 53334ca | 2010-01-01 23:37:34 +0000 | [diff] [blame] | 163 | bool operator()(const Case &C1, const Case &C2) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 164 | assert(isa<ConstantInt>(C1.Low) && isa<ConstantInt>(C2.High)); |
| 165 | const ConstantInt* CI1 = cast<const ConstantInt>(C1.Low); |
| 166 | const ConstantInt* CI2 = cast<const ConstantInt>(C2.High); |
| 167 | return CI1->getValue().slt(CI2->getValue()); |
| 168 | } |
| 169 | }; |
| 170 | |
| 171 | struct CaseBitsCmp { |
Chris Lattner | 53334ca | 2010-01-01 23:37:34 +0000 | [diff] [blame] | 172 | bool operator()(const CaseBits &C1, const CaseBits &C2) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 173 | return C1.Bits > C2.Bits; |
| 174 | } |
| 175 | }; |
| 176 | |
Chris Lattner | 53334ca | 2010-01-01 23:37:34 +0000 | [diff] [blame] | 177 | size_t Clusterify(CaseVector &Cases, const SwitchInst &SI); |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 178 | |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 179 | /// CaseBlock - This structure is used to communicate between |
| 180 | /// SelectionDAGBuilder and SDISel for the code generation of additional basic |
| 181 | /// blocks needed by multi-case switch statements. |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 182 | struct CaseBlock { |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 183 | CaseBlock(ISD::CondCode cc, const Value *cmplhs, const Value *cmprhs, |
| 184 | const Value *cmpmiddle, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 185 | MachineBasicBlock *truebb, MachineBasicBlock *falsebb, |
| 186 | MachineBasicBlock *me) |
| 187 | : CC(cc), CmpLHS(cmplhs), CmpMHS(cmpmiddle), CmpRHS(cmprhs), |
| 188 | TrueBB(truebb), FalseBB(falsebb), ThisBB(me) {} |
| 189 | // CC - the condition code to use for the case block's setcc node |
| 190 | ISD::CondCode CC; |
| 191 | // CmpLHS/CmpRHS/CmpMHS - The LHS/MHS/RHS of the comparison to emit. |
| 192 | // Emit by default LHS op RHS. MHS is used for range comparisons: |
| 193 | // If MHS is not null: (LHS <= MHS) and (MHS <= RHS). |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 194 | const Value *CmpLHS, *CmpMHS, *CmpRHS; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 195 | // TrueBB/FalseBB - the block to branch to if the setcc is true/false. |
| 196 | MachineBasicBlock *TrueBB, *FalseBB; |
| 197 | // ThisBB - the block into which to emit the code for the setcc and branches |
| 198 | MachineBasicBlock *ThisBB; |
| 199 | }; |
| 200 | struct JumpTable { |
| 201 | JumpTable(unsigned R, unsigned J, MachineBasicBlock *M, |
| 202 | MachineBasicBlock *D): Reg(R), JTI(J), MBB(M), Default(D) {} |
| 203 | |
| 204 | /// Reg - the virtual register containing the index of the jump table entry |
| 205 | //. to jump to. |
| 206 | unsigned Reg; |
| 207 | /// JTI - the JumpTableIndex for this jump table in the function. |
| 208 | unsigned JTI; |
| 209 | /// MBB - the MBB into which to emit the code for the indirect jump. |
| 210 | MachineBasicBlock *MBB; |
| 211 | /// Default - the MBB of the default bb, which is a successor of the range |
| 212 | /// check MBB. This is when updating PHI nodes in successors. |
| 213 | MachineBasicBlock *Default; |
| 214 | }; |
| 215 | struct JumpTableHeader { |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 216 | JumpTableHeader(APInt F, APInt L, const Value *SV, MachineBasicBlock *H, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 217 | bool E = false): |
| 218 | First(F), Last(L), SValue(SV), HeaderBB(H), Emitted(E) {} |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 219 | APInt First; |
| 220 | APInt Last; |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 221 | const Value *SValue; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 222 | MachineBasicBlock *HeaderBB; |
| 223 | bool Emitted; |
| 224 | }; |
| 225 | typedef std::pair<JumpTableHeader, JumpTable> JumpTableBlock; |
| 226 | |
| 227 | struct BitTestCase { |
| 228 | BitTestCase(uint64_t M, MachineBasicBlock* T, MachineBasicBlock* Tr): |
| 229 | Mask(M), ThisBB(T), TargetBB(Tr) { } |
| 230 | uint64_t Mask; |
Chris Lattner | 53334ca | 2010-01-01 23:37:34 +0000 | [diff] [blame] | 231 | MachineBasicBlock *ThisBB; |
| 232 | MachineBasicBlock *TargetBB; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 233 | }; |
| 234 | |
| 235 | typedef SmallVector<BitTestCase, 3> BitTestInfo; |
| 236 | |
| 237 | struct BitTestBlock { |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 238 | BitTestBlock(APInt F, APInt R, const Value* SV, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 239 | unsigned Rg, bool E, |
| 240 | MachineBasicBlock* P, MachineBasicBlock* D, |
| 241 | const BitTestInfo& C): |
| 242 | First(F), Range(R), SValue(SV), Reg(Rg), Emitted(E), |
| 243 | Parent(P), Default(D), Cases(C) { } |
Anton Korobeynikov | 2321858 | 2008-12-23 22:25:27 +0000 | [diff] [blame] | 244 | APInt First; |
| 245 | APInt Range; |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 246 | const Value *SValue; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 247 | unsigned Reg; |
| 248 | bool Emitted; |
| 249 | MachineBasicBlock *Parent; |
| 250 | MachineBasicBlock *Default; |
| 251 | BitTestInfo Cases; |
| 252 | }; |
| 253 | |
| 254 | public: |
| 255 | // TLI - This is information that describes the available target features we |
| 256 | // need for lowering. This indicates when operations are unavailable, |
| 257 | // implemented with a libcall, etc. |
Dan Gohman | 55e59c1 | 2010-04-19 19:05:59 +0000 | [diff] [blame] | 258 | const TargetMachine &TM; |
Dan Gohman | d858e90 | 2010-04-17 15:26:15 +0000 | [diff] [blame] | 259 | const TargetLowering &TLI; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 260 | SelectionDAG &DAG; |
| 261 | const TargetData *TD; |
| 262 | AliasAnalysis *AA; |
| 263 | |
| 264 | /// SwitchCases - Vector of CaseBlock structures used to communicate |
| 265 | /// SwitchInst code generation information. |
| 266 | std::vector<CaseBlock> SwitchCases; |
| 267 | /// JTCases - Vector of JumpTable structures used to communicate |
| 268 | /// SwitchInst code generation information. |
| 269 | std::vector<JumpTableBlock> JTCases; |
| 270 | /// BitTestCases - Vector of BitTestBlock structures used to communicate |
| 271 | /// SwitchInst code generation information. |
| 272 | std::vector<BitTestBlock> BitTestCases; |
Evan Cheng | fb2e752 | 2009-09-18 21:02:19 +0000 | [diff] [blame] | 273 | |
| 274 | /// PHINodesToUpdate - A list of phi instructions whose operand list will |
| 275 | /// be updated after processing the current basic block. |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 276 | std::vector<std::pair<MachineInstr*, unsigned> > PHINodesToUpdate; |
| 277 | |
Evan Cheng | fb2e752 | 2009-09-18 21:02:19 +0000 | [diff] [blame] | 278 | /// EdgeMapping - If an edge from CurMBB to any MBB is changed (e.g. due to |
| 279 | /// scheduler custom lowering), track the change here. |
| 280 | DenseMap<MachineBasicBlock*, MachineBasicBlock*> EdgeMapping; |
| 281 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 282 | // Emit PHI-node-operand constants only once even if used by multiple |
| 283 | // PHI nodes. |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 284 | DenseMap<const Constant *, unsigned> ConstantsOut; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 285 | |
| 286 | /// FuncInfo - Information about the function as a whole. |
| 287 | /// |
| 288 | FunctionLoweringInfo &FuncInfo; |
Bill Wendling | dfdacee | 2009-02-19 21:12:54 +0000 | [diff] [blame] | 289 | |
Bill Wendling | be8cc2a | 2009-04-29 00:15:41 +0000 | [diff] [blame] | 290 | /// OptLevel - What optimization level we're generating code for. |
Bill Wendling | dfdacee | 2009-02-19 21:12:54 +0000 | [diff] [blame] | 291 | /// |
Bill Wendling | 98a366d | 2009-04-29 23:29:43 +0000 | [diff] [blame] | 292 | CodeGenOpt::Level OptLevel; |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 293 | |
| 294 | /// GFI - Garbage collection metadata for the function. |
| 295 | GCFunctionInfo *GFI; |
| 296 | |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 297 | /// HasTailCall - This is set to true if a call in the current |
| 298 | /// block has been translated as a tail call. In this case, |
| 299 | /// no subsequent DAG nodes should be created. |
| 300 | /// |
| 301 | bool HasTailCall; |
| 302 | |
Owen Anderson | 0a5372e | 2009-07-13 04:09:18 +0000 | [diff] [blame] | 303 | LLVMContext *Context; |
| 304 | |
Dan Gohman | 55e59c1 | 2010-04-19 19:05:59 +0000 | [diff] [blame] | 305 | SelectionDAGBuilder(SelectionDAG &dag, FunctionLoweringInfo &funcinfo, |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 306 | CodeGenOpt::Level ol) |
Dan Gohman | 55e59c1 | 2010-04-19 19:05:59 +0000 | [diff] [blame] | 307 | : SDNodeOrder(0), TM(dag.getTarget()), TLI(dag.getTargetLoweringInfo()), |
| 308 | DAG(dag), FuncInfo(funcinfo), OptLevel(ol), |
Chris Lattner | a4f2bb0 | 2010-04-02 20:17:23 +0000 | [diff] [blame] | 309 | HasTailCall(false), Context(dag.getContext()) { |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | void init(GCFunctionInfo *gfi, AliasAnalysis &aa); |
| 313 | |
Dan Gohman | b02b62a | 2010-04-14 18:24:06 +0000 | [diff] [blame] | 314 | /// clear - Clear out the current SelectionDAG and the associated |
Dan Gohman | 2048b85 | 2009-11-23 18:04:58 +0000 | [diff] [blame] | 315 | /// state and prepare this SelectionDAGBuilder object to be used |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 316 | /// for a new block. This doesn't clear out information about |
| 317 | /// additional blocks that are needed to complete switch lowering |
| 318 | /// or PHI node updating; that information is cleared out as it is |
| 319 | /// consumed. |
| 320 | void clear(); |
| 321 | |
| 322 | /// getRoot - Return the current virtual root of the Selection DAG, |
| 323 | /// flushing any PendingLoad items. This must be done before emitting |
| 324 | /// a store or any other node that may need to be ordered after any |
| 325 | /// prior load instructions. |
| 326 | /// |
| 327 | SDValue getRoot(); |
| 328 | |
| 329 | /// getControlRoot - Similar to getRoot, but instead of flushing all the |
| 330 | /// PendingLoad items, flush all the PendingExports items. It is necessary |
| 331 | /// to do this before emitting a terminator instruction. |
| 332 | /// |
| 333 | SDValue getControlRoot(); |
| 334 | |
Dale Johannesen | 66978ee | 2009-01-31 02:22:37 +0000 | [diff] [blame] | 335 | DebugLoc getCurDebugLoc() const { return CurDebugLoc; } |
| 336 | |
Bill Wendling | 3ea3c24 | 2009-12-22 02:10:19 +0000 | [diff] [blame] | 337 | unsigned getSDNodeOrder() const { return SDNodeOrder; } |
| 338 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 339 | void CopyValueToVirtualRegister(const Value *V, unsigned Reg); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 340 | |
Bill Wendling | 4533cac | 2010-01-28 21:51:40 +0000 | [diff] [blame] | 341 | /// AssignOrderingToNode - Assign an ordering to the node. The order is gotten |
| 342 | /// from how the code appeared in the source. The ordering is used by the |
| 343 | /// scheduler to effectively turn off scheduling. |
| 344 | void AssignOrderingToNode(const SDNode *Node); |
| 345 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 346 | void visit(const Instruction &I); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 347 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 348 | void visit(unsigned Opcode, const User &I); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 349 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 350 | SDValue getValue(const Value *V); |
| 351 | |
| 352 | void setValue(const Value *V, SDValue NewN) { |
| 353 | SDValue &N = NodeMap[V]; |
| 354 | assert(N.getNode() == 0 && "Already set a value for this node!"); |
| 355 | N = NewN; |
| 356 | } |
| 357 | |
Dale Johannesen | 8e3455b | 2008-09-24 23:13:09 +0000 | [diff] [blame] | 358 | void GetRegistersForValue(SDISelAsmOperandInfo &OpInfo, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 359 | std::set<unsigned> &OutputRegs, |
| 360 | std::set<unsigned> &InputRegs); |
| 361 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 362 | void FindMergedConditions(const Value *Cond, MachineBasicBlock *TBB, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 363 | MachineBasicBlock *FBB, MachineBasicBlock *CurBB, |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 364 | MachineBasicBlock *SwitchBB, unsigned Opc); |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 365 | void EmitBranchForMergedCondition(const Value *Cond, MachineBasicBlock *TBB, |
Dan Gohman | c227734 | 2008-10-17 21:16:08 +0000 | [diff] [blame] | 366 | MachineBasicBlock *FBB, |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 367 | MachineBasicBlock *CurBB, |
| 368 | MachineBasicBlock *SwitchBB); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 369 | bool ShouldEmitAsBranches(const std::vector<CaseBlock> &Cases); |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 370 | bool isExportableFromCurrentBlock(const Value *V, const BasicBlock *FromBB); |
| 371 | void CopyToExportRegsIfNeeded(const Value *V); |
| 372 | void ExportFromCurrentBlock(const Value *V); |
| 373 | void LowerCallTo(ImmutableCallSite CS, SDValue Callee, bool IsTailCall, |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 374 | MachineBasicBlock *LandingPad = NULL); |
| 375 | |
| 376 | private: |
| 377 | // Terminator instructions. |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 378 | void visitRet(const ReturnInst &I); |
| 379 | void visitBr(const BranchInst &I); |
| 380 | void visitSwitch(const SwitchInst &I); |
| 381 | void visitIndirectBr(const IndirectBrInst &I); |
| 382 | void visitUnreachable(const UnreachableInst &I) { /* noop */ } |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 383 | |
| 384 | // Helpers for visitSwitch |
| 385 | bool handleSmallSwitchRange(CaseRec& CR, |
| 386 | CaseRecVector& WorkList, |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 387 | const Value* SV, |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 388 | MachineBasicBlock* Default, |
| 389 | MachineBasicBlock *SwitchBB); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 390 | bool handleJTSwitchCase(CaseRec& CR, |
| 391 | CaseRecVector& WorkList, |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 392 | const Value* SV, |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 393 | MachineBasicBlock* Default, |
| 394 | MachineBasicBlock *SwitchBB); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 395 | bool handleBTSplitSwitchCase(CaseRec& CR, |
| 396 | CaseRecVector& WorkList, |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 397 | const Value* SV, |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 398 | MachineBasicBlock* Default, |
| 399 | MachineBasicBlock *SwitchBB); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 400 | bool handleBitTestsSwitchCase(CaseRec& CR, |
| 401 | CaseRecVector& WorkList, |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 402 | const Value* SV, |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 403 | MachineBasicBlock* Default, |
| 404 | MachineBasicBlock *SwitchBB); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 405 | public: |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 406 | void visitSwitchCase(CaseBlock &CB, |
| 407 | MachineBasicBlock *SwitchBB); |
| 408 | void visitBitTestHeader(BitTestBlock &B, MachineBasicBlock *SwitchBB); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 409 | void visitBitTestCase(MachineBasicBlock* NextMBB, |
| 410 | unsigned Reg, |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 411 | BitTestCase &B, |
| 412 | MachineBasicBlock *SwitchBB); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 413 | void visitJumpTable(JumpTable &JT); |
Dan Gohman | 99be8ae | 2010-04-19 22:41:47 +0000 | [diff] [blame] | 414 | void visitJumpTableHeader(JumpTable &JT, JumpTableHeader &JTH, |
| 415 | MachineBasicBlock *SwitchBB); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 416 | |
| 417 | private: |
| 418 | // These all get lowered before this pass. |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 419 | void visitInvoke(const InvokeInst &I); |
| 420 | void visitUnwind(const UnwindInst &I); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 421 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 422 | void visitBinary(const User &I, unsigned OpCode); |
| 423 | void visitShift(const User &I, unsigned Opcode); |
| 424 | void visitAdd(const User &I) { visitBinary(I, ISD::ADD); } |
| 425 | void visitFAdd(const User &I) { visitBinary(I, ISD::FADD); } |
| 426 | void visitSub(const User &I) { visitBinary(I, ISD::SUB); } |
| 427 | void visitFSub(const User &I); |
| 428 | void visitMul(const User &I) { visitBinary(I, ISD::MUL); } |
| 429 | void visitFMul(const User &I) { visitBinary(I, ISD::FMUL); } |
| 430 | void visitURem(const User &I) { visitBinary(I, ISD::UREM); } |
| 431 | void visitSRem(const User &I) { visitBinary(I, ISD::SREM); } |
| 432 | void visitFRem(const User &I) { visitBinary(I, ISD::FREM); } |
| 433 | void visitUDiv(const User &I) { visitBinary(I, ISD::UDIV); } |
| 434 | void visitSDiv(const User &I) { visitBinary(I, ISD::SDIV); } |
| 435 | void visitFDiv(const User &I) { visitBinary(I, ISD::FDIV); } |
| 436 | void visitAnd (const User &I) { visitBinary(I, ISD::AND); } |
| 437 | void visitOr (const User &I) { visitBinary(I, ISD::OR); } |
| 438 | void visitXor (const User &I) { visitBinary(I, ISD::XOR); } |
| 439 | void visitShl (const User &I) { visitShift(I, ISD::SHL); } |
| 440 | void visitLShr(const User &I) { visitShift(I, ISD::SRL); } |
| 441 | void visitAShr(const User &I) { visitShift(I, ISD::SRA); } |
| 442 | void visitICmp(const User &I); |
| 443 | void visitFCmp(const User &I); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 444 | // Visit the conversion instructions |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 445 | void visitTrunc(const User &I); |
| 446 | void visitZExt(const User &I); |
| 447 | void visitSExt(const User &I); |
| 448 | void visitFPTrunc(const User &I); |
| 449 | void visitFPExt(const User &I); |
| 450 | void visitFPToUI(const User &I); |
| 451 | void visitFPToSI(const User &I); |
| 452 | void visitUIToFP(const User &I); |
| 453 | void visitSIToFP(const User &I); |
| 454 | void visitPtrToInt(const User &I); |
| 455 | void visitIntToPtr(const User &I); |
| 456 | void visitBitCast(const User &I); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 457 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 458 | void visitExtractElement(const User &I); |
| 459 | void visitInsertElement(const User &I); |
| 460 | void visitShuffleVector(const User &I); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 461 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 462 | void visitExtractValue(const ExtractValueInst &I); |
| 463 | void visitInsertValue(const InsertValueInst &I); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 464 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 465 | void visitGetElementPtr(const User &I); |
| 466 | void visitSelect(const User &I); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 467 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 468 | void visitAlloca(const AllocaInst &I); |
| 469 | void visitLoad(const LoadInst &I); |
| 470 | void visitStore(const StoreInst &I); |
Dan Gohman | ba5be5c | 2010-04-20 15:00:41 +0000 | [diff] [blame] | 471 | void visitPHI(const PHINode &I); |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 472 | void visitCall(const CallInst &I); |
| 473 | bool visitMemCmpCall(const CallInst &I); |
Chris Lattner | 8047d9a | 2009-12-24 00:37:38 +0000 | [diff] [blame] | 474 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 475 | void visitInlineAsm(ImmutableCallSite CS); |
| 476 | const char *visitIntrinsicCall(const CallInst &I, unsigned Intrinsic); |
| 477 | void visitTargetIntrinsic(const CallInst &I, unsigned Intrinsic); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 478 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 479 | void visitPow(const CallInst &I); |
| 480 | void visitExp2(const CallInst &I); |
| 481 | void visitExp(const CallInst &I); |
| 482 | void visitLog(const CallInst &I); |
| 483 | void visitLog2(const CallInst &I); |
| 484 | void visitLog10(const CallInst &I); |
Dale Johannesen | 601d3c0 | 2008-09-05 01:48:15 +0000 | [diff] [blame] | 485 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 486 | void visitVAStart(const CallInst &I); |
| 487 | void visitVAArg(const VAArgInst &I); |
| 488 | void visitVAEnd(const CallInst &I); |
| 489 | void visitVACopy(const CallInst &I); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 490 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 491 | void visitUserOp1(const Instruction &I) { |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 492 | llvm_unreachable("UserOp1 should not exist at instruction selection time!"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 493 | } |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 494 | void visitUserOp2(const Instruction &I) { |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 495 | llvm_unreachable("UserOp2 should not exist at instruction selection time!"); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 496 | } |
| 497 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 498 | const char *implVisitBinaryAtomic(const CallInst& I, ISD::NodeType Op); |
| 499 | const char *implVisitAluOverflow(const CallInst &I, ISD::NodeType Op); |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 500 | }; |
| 501 | |
Dan Gohman | f0cbcd4 | 2008-09-03 16:12:24 +0000 | [diff] [blame] | 502 | } // end namespace llvm |
| 503 | |
| 504 | #endif |