Chris Lattner | 7a60d91 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1 | //===-- SelectionDAGISel.cpp - Implement the SelectionDAGISel class -------===// |
Misha Brukman | 835702a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 2 | // |
Chris Lattner | 7a60d91 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by the LLVM research group and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
Misha Brukman | 835702a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 7 | // |
Chris Lattner | 7a60d91 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This implements the SelectionDAGISel class. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #define DEBUG_TYPE "isel" |
| 15 | #include "llvm/CodeGen/SelectionDAGISel.h" |
Chris Lattner | 2e77db6 | 2005-05-13 18:50:42 +0000 | [diff] [blame] | 16 | #include "llvm/CallingConv.h" |
Chris Lattner | 7a60d91 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 17 | #include "llvm/Constants.h" |
| 18 | #include "llvm/DerivedTypes.h" |
| 19 | #include "llvm/Function.h" |
| 20 | #include "llvm/Instructions.h" |
| 21 | #include "llvm/Intrinsics.h" |
| 22 | #include "llvm/CodeGen/MachineFunction.h" |
| 23 | #include "llvm/CodeGen/MachineFrameInfo.h" |
| 24 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
| 25 | #include "llvm/CodeGen/SelectionDAG.h" |
| 26 | #include "llvm/CodeGen/SSARegMap.h" |
Chris Lattner | d4382f0 | 2005-09-13 19:30:54 +0000 | [diff] [blame] | 27 | #include "llvm/Target/MRegisterInfo.h" |
Chris Lattner | 7a60d91 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 28 | #include "llvm/Target/TargetData.h" |
| 29 | #include "llvm/Target/TargetFrameInfo.h" |
| 30 | #include "llvm/Target/TargetInstrInfo.h" |
| 31 | #include "llvm/Target/TargetLowering.h" |
| 32 | #include "llvm/Target/TargetMachine.h" |
Chris Lattner | c9950c1 | 2005-08-17 06:37:43 +0000 | [diff] [blame] | 33 | #include "llvm/Transforms/Utils/BasicBlockUtils.h" |
Chris Lattner | e05a461 | 2005-01-12 03:41:21 +0000 | [diff] [blame] | 34 | #include "llvm/Support/CommandLine.h" |
Chris Lattner | 43535a1 | 2005-11-09 04:45:33 +0000 | [diff] [blame] | 35 | #include "llvm/Support/MathExtras.h" |
Chris Lattner | 7a60d91 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 36 | #include "llvm/Support/Debug.h" |
| 37 | #include <map> |
| 38 | #include <iostream> |
| 39 | using namespace llvm; |
| 40 | |
Chris Lattner | 975f5c9 | 2005-09-01 18:44:10 +0000 | [diff] [blame] | 41 | #ifndef NDEBUG |
Chris Lattner | e05a461 | 2005-01-12 03:41:21 +0000 | [diff] [blame] | 42 | static cl::opt<bool> |
| 43 | ViewDAGs("view-isel-dags", cl::Hidden, |
| 44 | cl::desc("Pop up a window to show isel dags as they are selected")); |
| 45 | #else |
Chris Lattner | b6cde17 | 2005-09-02 07:09:28 +0000 | [diff] [blame] | 46 | static const bool ViewDAGs = 0; |
Chris Lattner | e05a461 | 2005-01-12 03:41:21 +0000 | [diff] [blame] | 47 | #endif |
| 48 | |
Nate Begeman | 007c650 | 2005-09-07 00:15:36 +0000 | [diff] [blame] | 49 | |
Chris Lattner | 7a60d91 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 50 | namespace llvm { |
| 51 | //===--------------------------------------------------------------------===// |
| 52 | /// FunctionLoweringInfo - This contains information that is global to a |
| 53 | /// function that is used when lowering a region of the function. |
Chris Lattner | d006195 | 2005-01-08 19:52:31 +0000 | [diff] [blame] | 54 | class FunctionLoweringInfo { |
| 55 | public: |
Chris Lattner | 7a60d91 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 56 | TargetLowering &TLI; |
| 57 | Function &Fn; |
| 58 | MachineFunction &MF; |
| 59 | SSARegMap *RegMap; |
| 60 | |
| 61 | FunctionLoweringInfo(TargetLowering &TLI, Function &Fn,MachineFunction &MF); |
| 62 | |
| 63 | /// MBBMap - A mapping from LLVM basic blocks to their machine code entry. |
| 64 | std::map<const BasicBlock*, MachineBasicBlock *> MBBMap; |
| 65 | |
| 66 | /// ValueMap - Since we emit code for the function a basic block at a time, |
| 67 | /// we must remember which virtual registers hold the values for |
| 68 | /// cross-basic-block values. |
| 69 | std::map<const Value*, unsigned> ValueMap; |
| 70 | |
| 71 | /// StaticAllocaMap - Keep track of frame indices for fixed sized allocas in |
| 72 | /// the entry block. This allows the allocas to be efficiently referenced |
| 73 | /// anywhere in the function. |
| 74 | std::map<const AllocaInst*, int> StaticAllocaMap; |
| 75 | |
| 76 | unsigned MakeReg(MVT::ValueType VT) { |
| 77 | return RegMap->createVirtualRegister(TLI.getRegClassFor(VT)); |
| 78 | } |
Misha Brukman | 835702a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 79 | |
Chris Lattner | 7a60d91 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 80 | unsigned CreateRegForValue(const Value *V) { |
| 81 | MVT::ValueType VT = TLI.getValueType(V->getType()); |
| 82 | // The common case is that we will only create one register for this |
| 83 | // value. If we have that case, create and return the virtual register. |
| 84 | unsigned NV = TLI.getNumElements(VT); |
Chris Lattner | a8d34fb | 2005-01-16 00:37:38 +0000 | [diff] [blame] | 85 | if (NV == 1) { |
| 86 | // If we are promoting this value, pick the next largest supported type. |
Chris Lattner | d58384f | 2005-01-16 01:11:19 +0000 | [diff] [blame] | 87 | return MakeReg(TLI.getTypeToTransformTo(VT)); |
Chris Lattner | a8d34fb | 2005-01-16 00:37:38 +0000 | [diff] [blame] | 88 | } |
Misha Brukman | 835702a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 89 | |
Chris Lattner | 7a60d91 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 90 | // If this value is represented with multiple target registers, make sure |
| 91 | // to create enough consequtive registers of the right (smaller) type. |
| 92 | unsigned NT = VT-1; // Find the type to use. |
| 93 | while (TLI.getNumElements((MVT::ValueType)NT) != 1) |
| 94 | --NT; |
Misha Brukman | 835702a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 95 | |
Chris Lattner | 7a60d91 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 96 | unsigned R = MakeReg((MVT::ValueType)NT); |
| 97 | for (unsigned i = 1; i != NV; ++i) |
| 98 | MakeReg((MVT::ValueType)NT); |
| 99 | return R; |
| 100 | } |
Misha Brukman | 835702a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 101 | |
Chris Lattner | 7a60d91 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 102 | unsigned InitializeRegForValue(const Value *V) { |
| 103 | unsigned &R = ValueMap[V]; |
| 104 | assert(R == 0 && "Already initialized this value register!"); |
| 105 | return R = CreateRegForValue(V); |
| 106 | } |
| 107 | }; |
| 108 | } |
| 109 | |
| 110 | /// isUsedOutsideOfDefiningBlock - Return true if this instruction is used by |
| 111 | /// PHI nodes or outside of the basic block that defines it. |
| 112 | static bool isUsedOutsideOfDefiningBlock(Instruction *I) { |
| 113 | if (isa<PHINode>(I)) return true; |
| 114 | BasicBlock *BB = I->getParent(); |
| 115 | for (Value::use_iterator UI = I->use_begin(), E = I->use_end(); UI != E; ++UI) |
| 116 | if (cast<Instruction>(*UI)->getParent() != BB || isa<PHINode>(*UI)) |
| 117 | return true; |
| 118 | return false; |
| 119 | } |
| 120 | |
Chris Lattner | 6871b23 | 2005-10-30 19:42:35 +0000 | [diff] [blame] | 121 | /// isOnlyUsedInEntryBlock - If the specified argument is only used in the |
| 122 | /// entry block, return true. |
| 123 | static bool isOnlyUsedInEntryBlock(Argument *A) { |
| 124 | BasicBlock *Entry = A->getParent()->begin(); |
| 125 | for (Value::use_iterator UI = A->use_begin(), E = A->use_end(); UI != E; ++UI) |
| 126 | if (cast<Instruction>(*UI)->getParent() != Entry) |
| 127 | return false; // Use not in entry block. |
| 128 | return true; |
| 129 | } |
| 130 | |
Chris Lattner | 7a60d91 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 131 | FunctionLoweringInfo::FunctionLoweringInfo(TargetLowering &tli, |
Misha Brukman | 835702a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 132 | Function &fn, MachineFunction &mf) |
Chris Lattner | 7a60d91 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 133 | : TLI(tli), Fn(fn), MF(mf), RegMap(MF.getSSARegMap()) { |
| 134 | |
Chris Lattner | 6871b23 | 2005-10-30 19:42:35 +0000 | [diff] [blame] | 135 | // Create a vreg for each argument register that is not dead and is used |
| 136 | // outside of the entry block for the function. |
| 137 | for (Function::arg_iterator AI = Fn.arg_begin(), E = Fn.arg_end(); |
| 138 | AI != E; ++AI) |
| 139 | if (!isOnlyUsedInEntryBlock(AI)) |
| 140 | InitializeRegForValue(AI); |
| 141 | |
Chris Lattner | 7a60d91 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 142 | // Initialize the mapping of values to registers. This is only set up for |
| 143 | // instruction values that are used outside of the block that defines |
| 144 | // them. |
Jeff Cohen | f8a5e5ae | 2005-10-01 03:57:14 +0000 | [diff] [blame] | 145 | Function::iterator BB = Fn.begin(), EB = Fn.end(); |
Chris Lattner | 7a60d91 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 146 | for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) |
| 147 | if (AllocaInst *AI = dyn_cast<AllocaInst>(I)) |
| 148 | if (ConstantUInt *CUI = dyn_cast<ConstantUInt>(AI->getArraySize())) { |
| 149 | const Type *Ty = AI->getAllocatedType(); |
| 150 | uint64_t TySize = TLI.getTargetData().getTypeSize(Ty); |
Nate Begeman | 3ee3e69 | 2005-11-06 09:00:38 +0000 | [diff] [blame] | 151 | unsigned Align = |
| 152 | std::max((unsigned)TLI.getTargetData().getTypeAlignment(Ty), |
| 153 | AI->getAlignment()); |
Chris Lattner | cbefe72 | 2005-05-13 23:14:17 +0000 | [diff] [blame] | 154 | |
| 155 | // If the alignment of the value is smaller than the size of the value, |
| 156 | // and if the size of the value is particularly small (<= 8 bytes), |
| 157 | // round up to the size of the value for potentially better performance. |
| 158 | // |
| 159 | // FIXME: This could be made better with a preferred alignment hook in |
| 160 | // TargetData. It serves primarily to 8-byte align doubles for X86. |
| 161 | if (Align < TySize && TySize <= 8) Align = TySize; |
Chris Lattner | 8396a30 | 2005-10-18 22:11:42 +0000 | [diff] [blame] | 162 | TySize *= CUI->getValue(); // Get total allocated size. |
Chris Lattner | 0a71a9a | 2005-10-18 22:14:06 +0000 | [diff] [blame] | 163 | if (TySize == 0) TySize = 1; // Don't create zero-sized stack objects. |
Chris Lattner | 7a60d91 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 164 | StaticAllocaMap[AI] = |
Chris Lattner | d006195 | 2005-01-08 19:52:31 +0000 | [diff] [blame] | 165 | MF.getFrameInfo()->CreateStackObject((unsigned)TySize, Align); |
Chris Lattner | 7a60d91 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 166 | } |
| 167 | |
Jeff Cohen | f8a5e5ae | 2005-10-01 03:57:14 +0000 | [diff] [blame] | 168 | for (; BB != EB; ++BB) |
| 169 | for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) |
Chris Lattner | 7a60d91 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 170 | if (!I->use_empty() && isUsedOutsideOfDefiningBlock(I)) |
| 171 | if (!isa<AllocaInst>(I) || |
| 172 | !StaticAllocaMap.count(cast<AllocaInst>(I))) |
| 173 | InitializeRegForValue(I); |
| 174 | |
| 175 | // Create an initial MachineBasicBlock for each LLVM BasicBlock in F. This |
| 176 | // also creates the initial PHI MachineInstrs, though none of the input |
| 177 | // operands are populated. |
Jeff Cohen | f8a5e5ae | 2005-10-01 03:57:14 +0000 | [diff] [blame] | 178 | for (BB = Fn.begin(), EB = Fn.end(); BB != EB; ++BB) { |
Chris Lattner | 7a60d91 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 179 | MachineBasicBlock *MBB = new MachineBasicBlock(BB); |
| 180 | MBBMap[BB] = MBB; |
| 181 | MF.getBasicBlockList().push_back(MBB); |
| 182 | |
| 183 | // Create Machine PHI nodes for LLVM PHI nodes, lowering them as |
| 184 | // appropriate. |
| 185 | PHINode *PN; |
| 186 | for (BasicBlock::iterator I = BB->begin(); |
Chris Lattner | 8ea875f | 2005-01-07 21:34:19 +0000 | [diff] [blame] | 187 | (PN = dyn_cast<PHINode>(I)); ++I) |
| 188 | if (!PN->use_empty()) { |
| 189 | unsigned NumElements = |
| 190 | TLI.getNumElements(TLI.getValueType(PN->getType())); |
| 191 | unsigned PHIReg = ValueMap[PN]; |
| 192 | assert(PHIReg &&"PHI node does not have an assigned virtual register!"); |
| 193 | for (unsigned i = 0; i != NumElements; ++i) |
| 194 | BuildMI(MBB, TargetInstrInfo::PHI, PN->getNumOperands(), PHIReg+i); |
| 195 | } |
Chris Lattner | 7a60d91 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 196 | } |
| 197 | } |
| 198 | |
| 199 | |
| 200 | |
| 201 | //===----------------------------------------------------------------------===// |
| 202 | /// SelectionDAGLowering - This is the common target-independent lowering |
| 203 | /// implementation that is parameterized by a TargetLowering object. |
| 204 | /// Also, targets can overload any lowering method. |
| 205 | /// |
| 206 | namespace llvm { |
| 207 | class SelectionDAGLowering { |
| 208 | MachineBasicBlock *CurMBB; |
| 209 | |
| 210 | std::map<const Value*, SDOperand> NodeMap; |
| 211 | |
Chris Lattner | 4d9651c | 2005-01-17 22:19:26 +0000 | [diff] [blame] | 212 | /// PendingLoads - Loads are not emitted to the program immediately. We bunch |
| 213 | /// them up and then emit token factor nodes when possible. This allows us to |
| 214 | /// get simple disambiguation between loads without worrying about alias |
| 215 | /// analysis. |
| 216 | std::vector<SDOperand> PendingLoads; |
| 217 | |
Chris Lattner | 7a60d91 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 218 | public: |
| 219 | // TLI - This is information that describes the available target features we |
| 220 | // need for lowering. This indicates when operations are unavailable, |
| 221 | // implemented with a libcall, etc. |
| 222 | TargetLowering &TLI; |
| 223 | SelectionDAG &DAG; |
| 224 | const TargetData &TD; |
| 225 | |
| 226 | /// FuncInfo - Information about the function as a whole. |
| 227 | /// |
| 228 | FunctionLoweringInfo &FuncInfo; |
| 229 | |
| 230 | SelectionDAGLowering(SelectionDAG &dag, TargetLowering &tli, |
Misha Brukman | 835702a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 231 | FunctionLoweringInfo &funcinfo) |
Chris Lattner | 7a60d91 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 232 | : TLI(tli), DAG(dag), TD(DAG.getTarget().getTargetData()), |
| 233 | FuncInfo(funcinfo) { |
| 234 | } |
| 235 | |
Chris Lattner | 4108bb0 | 2005-01-17 19:43:36 +0000 | [diff] [blame] | 236 | /// getRoot - Return the current virtual root of the Selection DAG. |
| 237 | /// |
| 238 | SDOperand getRoot() { |
Chris Lattner | 4d9651c | 2005-01-17 22:19:26 +0000 | [diff] [blame] | 239 | if (PendingLoads.empty()) |
| 240 | return DAG.getRoot(); |
Misha Brukman | 835702a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 241 | |
Chris Lattner | 4d9651c | 2005-01-17 22:19:26 +0000 | [diff] [blame] | 242 | if (PendingLoads.size() == 1) { |
| 243 | SDOperand Root = PendingLoads[0]; |
| 244 | DAG.setRoot(Root); |
| 245 | PendingLoads.clear(); |
| 246 | return Root; |
| 247 | } |
| 248 | |
| 249 | // Otherwise, we have to make a token factor node. |
| 250 | SDOperand Root = DAG.getNode(ISD::TokenFactor, MVT::Other, PendingLoads); |
| 251 | PendingLoads.clear(); |
| 252 | DAG.setRoot(Root); |
| 253 | return Root; |
Chris Lattner | 4108bb0 | 2005-01-17 19:43:36 +0000 | [diff] [blame] | 254 | } |
| 255 | |
Chris Lattner | 7a60d91 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 256 | void visit(Instruction &I) { visit(I.getOpcode(), I); } |
| 257 | |
| 258 | void visit(unsigned Opcode, User &I) { |
| 259 | switch (Opcode) { |
| 260 | default: assert(0 && "Unknown instruction type encountered!"); |
| 261 | abort(); |
| 262 | // Build the switch statement using the Instruction.def file. |
| 263 | #define HANDLE_INST(NUM, OPCODE, CLASS) \ |
| 264 | case Instruction::OPCODE:return visit##OPCODE((CLASS&)I); |
| 265 | #include "llvm/Instruction.def" |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | void setCurrentBasicBlock(MachineBasicBlock *MBB) { CurMBB = MBB; } |
| 270 | |
| 271 | |
| 272 | SDOperand getIntPtrConstant(uint64_t Val) { |
| 273 | return DAG.getConstant(Val, TLI.getPointerTy()); |
| 274 | } |
| 275 | |
| 276 | SDOperand getValue(const Value *V) { |
| 277 | SDOperand &N = NodeMap[V]; |
| 278 | if (N.Val) return N; |
| 279 | |
| 280 | MVT::ValueType VT = TLI.getValueType(V->getType()); |
| 281 | if (Constant *C = const_cast<Constant*>(dyn_cast<Constant>(V))) |
| 282 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) { |
| 283 | visit(CE->getOpcode(), *CE); |
| 284 | assert(N.Val && "visit didn't populate the ValueMap!"); |
| 285 | return N; |
| 286 | } else if (GlobalValue *GV = dyn_cast<GlobalValue>(C)) { |
| 287 | return N = DAG.getGlobalAddress(GV, VT); |
| 288 | } else if (isa<ConstantPointerNull>(C)) { |
| 289 | return N = DAG.getConstant(0, TLI.getPointerTy()); |
| 290 | } else if (isa<UndefValue>(C)) { |
Nate Begeman | af1c0f7 | 2005-04-12 23:12:17 +0000 | [diff] [blame] | 291 | return N = DAG.getNode(ISD::UNDEF, VT); |
Chris Lattner | 7a60d91 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 292 | } else if (ConstantFP *CFP = dyn_cast<ConstantFP>(C)) { |
| 293 | return N = DAG.getConstantFP(CFP->getValue(), VT); |
| 294 | } else { |
| 295 | // Canonicalize all constant ints to be unsigned. |
| 296 | return N = DAG.getConstant(cast<ConstantIntegral>(C)->getRawValue(),VT); |
| 297 | } |
| 298 | |
| 299 | if (const AllocaInst *AI = dyn_cast<AllocaInst>(V)) { |
| 300 | std::map<const AllocaInst*, int>::iterator SI = |
| 301 | FuncInfo.StaticAllocaMap.find(AI); |
| 302 | if (SI != FuncInfo.StaticAllocaMap.end()) |
| 303 | return DAG.getFrameIndex(SI->second, TLI.getPointerTy()); |
| 304 | } |
| 305 | |
| 306 | std::map<const Value*, unsigned>::const_iterator VMI = |
| 307 | FuncInfo.ValueMap.find(V); |
| 308 | assert(VMI != FuncInfo.ValueMap.end() && "Value not in map!"); |
Chris Lattner | 209f585 | 2005-01-16 02:23:07 +0000 | [diff] [blame] | 309 | |
Chris Lattner | 3318232 | 2005-08-16 21:55:35 +0000 | [diff] [blame] | 310 | unsigned InReg = VMI->second; |
| 311 | |
| 312 | // If this type is not legal, make it so now. |
| 313 | MVT::ValueType DestVT = TLI.getTypeToTransformTo(VT); |
| 314 | |
| 315 | N = DAG.getCopyFromReg(DAG.getEntryNode(), InReg, DestVT); |
| 316 | if (DestVT < VT) { |
| 317 | // Source must be expanded. This input value is actually coming from the |
| 318 | // register pair VMI->second and VMI->second+1. |
| 319 | N = DAG.getNode(ISD::BUILD_PAIR, VT, N, |
| 320 | DAG.getCopyFromReg(DAG.getEntryNode(), InReg+1, DestVT)); |
| 321 | } else { |
| 322 | if (DestVT > VT) { // Promotion case |
| 323 | if (MVT::isFloatingPoint(VT)) |
| 324 | N = DAG.getNode(ISD::FP_ROUND, VT, N); |
| 325 | else |
| 326 | N = DAG.getNode(ISD::TRUNCATE, VT, N); |
| 327 | } |
| 328 | } |
| 329 | |
| 330 | return N; |
Chris Lattner | 7a60d91 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | const SDOperand &setValue(const Value *V, SDOperand NewN) { |
| 334 | SDOperand &N = NodeMap[V]; |
| 335 | assert(N.Val == 0 && "Already set a value for this node!"); |
| 336 | return N = NewN; |
| 337 | } |
| 338 | |
| 339 | // Terminator instructions. |
| 340 | void visitRet(ReturnInst &I); |
| 341 | void visitBr(BranchInst &I); |
| 342 | void visitUnreachable(UnreachableInst &I) { /* noop */ } |
| 343 | |
| 344 | // These all get lowered before this pass. |
| 345 | void visitSwitch(SwitchInst &I) { assert(0 && "TODO"); } |
| 346 | void visitInvoke(InvokeInst &I) { assert(0 && "TODO"); } |
| 347 | void visitUnwind(UnwindInst &I) { assert(0 && "TODO"); } |
| 348 | |
| 349 | // |
Chris Lattner | 7f9e078 | 2005-08-22 17:28:31 +0000 | [diff] [blame] | 350 | void visitBinary(User &I, unsigned Opcode, bool isShift = false); |
Chris Lattner | 6f3b577 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 351 | void visitAdd(User &I) { |
| 352 | visitBinary(I, I.getType()->isFloatingPoint() ? ISD::FADD : ISD::ADD); |
| 353 | } |
Chris Lattner | f68fd0b | 2005-04-02 05:04:50 +0000 | [diff] [blame] | 354 | void visitSub(User &I); |
Chris Lattner | 6f3b577 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 355 | void visitMul(User &I) { |
| 356 | visitBinary(I, I.getType()->isFloatingPoint() ? ISD::FMUL : ISD::MUL); |
| 357 | } |
Chris Lattner | 7a60d91 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 358 | void visitDiv(User &I) { |
Chris Lattner | 6f3b577 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 359 | unsigned Opc; |
| 360 | const Type *Ty = I.getType(); |
| 361 | if (Ty->isFloatingPoint()) |
| 362 | Opc = ISD::FDIV; |
| 363 | else if (Ty->isUnsigned()) |
| 364 | Opc = ISD::UDIV; |
| 365 | else |
| 366 | Opc = ISD::SDIV; |
| 367 | visitBinary(I, Opc); |
Chris Lattner | 7a60d91 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 368 | } |
| 369 | void visitRem(User &I) { |
Chris Lattner | 6f3b577 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 370 | unsigned Opc; |
| 371 | const Type *Ty = I.getType(); |
| 372 | if (Ty->isFloatingPoint()) |
| 373 | Opc = ISD::FREM; |
| 374 | else if (Ty->isUnsigned()) |
| 375 | Opc = ISD::UREM; |
| 376 | else |
| 377 | Opc = ISD::SREM; |
| 378 | visitBinary(I, Opc); |
Chris Lattner | 7a60d91 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 379 | } |
| 380 | void visitAnd(User &I) { visitBinary(I, ISD::AND); } |
| 381 | void visitOr (User &I) { visitBinary(I, ISD::OR); } |
| 382 | void visitXor(User &I) { visitBinary(I, ISD::XOR); } |
Chris Lattner | 7f9e078 | 2005-08-22 17:28:31 +0000 | [diff] [blame] | 383 | void visitShl(User &I) { visitBinary(I, ISD::SHL, true); } |
Chris Lattner | 7a60d91 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 384 | void visitShr(User &I) { |
Chris Lattner | 7f9e078 | 2005-08-22 17:28:31 +0000 | [diff] [blame] | 385 | visitBinary(I, I.getType()->isUnsigned() ? ISD::SRL : ISD::SRA, true); |
Chris Lattner | 7a60d91 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 386 | } |
| 387 | |
| 388 | void visitSetCC(User &I, ISD::CondCode SignedOpc, ISD::CondCode UnsignedOpc); |
| 389 | void visitSetEQ(User &I) { visitSetCC(I, ISD::SETEQ, ISD::SETEQ); } |
| 390 | void visitSetNE(User &I) { visitSetCC(I, ISD::SETNE, ISD::SETNE); } |
| 391 | void visitSetLE(User &I) { visitSetCC(I, ISD::SETLE, ISD::SETULE); } |
| 392 | void visitSetGE(User &I) { visitSetCC(I, ISD::SETGE, ISD::SETUGE); } |
| 393 | void visitSetLT(User &I) { visitSetCC(I, ISD::SETLT, ISD::SETULT); } |
| 394 | void visitSetGT(User &I) { visitSetCC(I, ISD::SETGT, ISD::SETUGT); } |
| 395 | |
| 396 | void visitGetElementPtr(User &I); |
| 397 | void visitCast(User &I); |
| 398 | void visitSelect(User &I); |
| 399 | // |
| 400 | |
| 401 | void visitMalloc(MallocInst &I); |
| 402 | void visitFree(FreeInst &I); |
| 403 | void visitAlloca(AllocaInst &I); |
| 404 | void visitLoad(LoadInst &I); |
| 405 | void visitStore(StoreInst &I); |
| 406 | void visitPHI(PHINode &I) { } // PHI nodes are handled specially. |
| 407 | void visitCall(CallInst &I); |
| 408 | |
Chris Lattner | 7a60d91 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 409 | void visitVAStart(CallInst &I); |
Chris Lattner | 7a60d91 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 410 | void visitVAArg(VAArgInst &I); |
| 411 | void visitVAEnd(CallInst &I); |
| 412 | void visitVACopy(CallInst &I); |
Chris Lattner | 58cfd79 | 2005-01-09 00:00:49 +0000 | [diff] [blame] | 413 | void visitFrameReturnAddress(CallInst &I, bool isFrameAddress); |
Chris Lattner | 7a60d91 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 414 | |
Chris Lattner | 875def9 | 2005-01-11 05:56:49 +0000 | [diff] [blame] | 415 | void visitMemIntrinsic(CallInst &I, unsigned Op); |
Chris Lattner | 7a60d91 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 416 | |
| 417 | void visitUserOp1(Instruction &I) { |
| 418 | assert(0 && "UserOp1 should not exist at instruction selection time!"); |
| 419 | abort(); |
| 420 | } |
| 421 | void visitUserOp2(Instruction &I) { |
| 422 | assert(0 && "UserOp2 should not exist at instruction selection time!"); |
| 423 | abort(); |
| 424 | } |
| 425 | }; |
| 426 | } // end namespace llvm |
| 427 | |
| 428 | void SelectionDAGLowering::visitRet(ReturnInst &I) { |
| 429 | if (I.getNumOperands() == 0) { |
Chris Lattner | 4108bb0 | 2005-01-17 19:43:36 +0000 | [diff] [blame] | 430 | DAG.setRoot(DAG.getNode(ISD::RET, MVT::Other, getRoot())); |
Chris Lattner | 7a60d91 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 431 | return; |
| 432 | } |
| 433 | |
| 434 | SDOperand Op1 = getValue(I.getOperand(0)); |
Chris Lattner | db45f7d | 2005-03-29 19:09:56 +0000 | [diff] [blame] | 435 | MVT::ValueType TmpVT; |
| 436 | |
Chris Lattner | 7a60d91 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 437 | switch (Op1.getValueType()) { |
| 438 | default: assert(0 && "Unknown value type!"); |
| 439 | case MVT::i1: |
| 440 | case MVT::i8: |
| 441 | case MVT::i16: |
Chris Lattner | db45f7d | 2005-03-29 19:09:56 +0000 | [diff] [blame] | 442 | case MVT::i32: |
| 443 | // If this is a machine where 32-bits is legal or expanded, promote to |
| 444 | // 32-bits, otherwise, promote to 64-bits. |
| 445 | if (TLI.getTypeAction(MVT::i32) == TargetLowering::Promote) |
| 446 | TmpVT = TLI.getTypeToTransformTo(MVT::i32); |
Chris Lattner | 7a60d91 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 447 | else |
Chris Lattner | db45f7d | 2005-03-29 19:09:56 +0000 | [diff] [blame] | 448 | TmpVT = MVT::i32; |
| 449 | |
| 450 | // Extend integer types to result type. |
| 451 | if (I.getOperand(0)->getType()->isSigned()) |
| 452 | Op1 = DAG.getNode(ISD::SIGN_EXTEND, TmpVT, Op1); |
| 453 | else |
| 454 | Op1 = DAG.getNode(ISD::ZERO_EXTEND, TmpVT, Op1); |
Chris Lattner | 7a60d91 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 455 | break; |
| 456 | case MVT::f32: |
Chris Lattner | 7a60d91 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 457 | case MVT::i64: |
| 458 | case MVT::f64: |
| 459 | break; // No extension needed! |
| 460 | } |
Nate Begeman | 78afac2 | 2005-10-18 23:23:37 +0000 | [diff] [blame] | 461 | // Allow targets to lower this further to meet ABI requirements |
| 462 | DAG.setRoot(TLI.LowerReturnTo(getRoot(), Op1, DAG)); |
Chris Lattner | 7a60d91 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 463 | } |
| 464 | |
| 465 | void SelectionDAGLowering::visitBr(BranchInst &I) { |
| 466 | // Update machine-CFG edges. |
| 467 | MachineBasicBlock *Succ0MBB = FuncInfo.MBBMap[I.getSuccessor(0)]; |
Chris Lattner | 7a60d91 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 468 | |
| 469 | // Figure out which block is immediately after the current one. |
| 470 | MachineBasicBlock *NextBlock = 0; |
| 471 | MachineFunction::iterator BBI = CurMBB; |
| 472 | if (++BBI != CurMBB->getParent()->end()) |
| 473 | NextBlock = BBI; |
| 474 | |
| 475 | if (I.isUnconditional()) { |
| 476 | // If this is not a fall-through branch, emit the branch. |
| 477 | if (Succ0MBB != NextBlock) |
Chris Lattner | 4108bb0 | 2005-01-17 19:43:36 +0000 | [diff] [blame] | 478 | DAG.setRoot(DAG.getNode(ISD::BR, MVT::Other, getRoot(), |
Misha Brukman | 7745116 | 2005-04-22 04:01:18 +0000 | [diff] [blame] | 479 | DAG.getBasicBlock(Succ0MBB))); |
Chris Lattner | 7a60d91 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 480 | } else { |
| 481 | MachineBasicBlock *Succ1MBB = FuncInfo.MBBMap[I.getSuccessor(1)]; |
Chris Lattner | 7a60d91 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 482 | |
| 483 | SDOperand Cond = getValue(I.getCondition()); |
Chris Lattner | 7a60d91 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 484 | if (Succ1MBB == NextBlock) { |
| 485 | // If the condition is false, fall through. This means we should branch |
| 486 | // if the condition is true to Succ #0. |
Chris Lattner | 4108bb0 | 2005-01-17 19:43:36 +0000 | [diff] [blame] | 487 | DAG.setRoot(DAG.getNode(ISD::BRCOND, MVT::Other, getRoot(), |
Misha Brukman | 7745116 | 2005-04-22 04:01:18 +0000 | [diff] [blame] | 488 | Cond, DAG.getBasicBlock(Succ0MBB))); |
Chris Lattner | 7a60d91 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 489 | } else if (Succ0MBB == NextBlock) { |
| 490 | // If the condition is true, fall through. This means we should branch if |
| 491 | // the condition is false to Succ #1. Invert the condition first. |
| 492 | SDOperand True = DAG.getConstant(1, Cond.getValueType()); |
| 493 | Cond = DAG.getNode(ISD::XOR, Cond.getValueType(), Cond, True); |
Chris Lattner | 4108bb0 | 2005-01-17 19:43:36 +0000 | [diff] [blame] | 494 | DAG.setRoot(DAG.getNode(ISD::BRCOND, MVT::Other, getRoot(), |
Misha Brukman | 7745116 | 2005-04-22 04:01:18 +0000 | [diff] [blame] | 495 | Cond, DAG.getBasicBlock(Succ1MBB))); |
Chris Lattner | 7a60d91 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 496 | } else { |
Chris Lattner | 8a98c7f | 2005-04-09 03:30:29 +0000 | [diff] [blame] | 497 | std::vector<SDOperand> Ops; |
| 498 | Ops.push_back(getRoot()); |
| 499 | Ops.push_back(Cond); |
| 500 | Ops.push_back(DAG.getBasicBlock(Succ0MBB)); |
| 501 | Ops.push_back(DAG.getBasicBlock(Succ1MBB)); |
| 502 | DAG.setRoot(DAG.getNode(ISD::BRCONDTWOWAY, MVT::Other, Ops)); |
Chris Lattner | 7a60d91 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 503 | } |
| 504 | } |
| 505 | } |
| 506 | |
Chris Lattner | f68fd0b | 2005-04-02 05:04:50 +0000 | [diff] [blame] | 507 | void SelectionDAGLowering::visitSub(User &I) { |
| 508 | // -0.0 - X --> fneg |
Chris Lattner | 6f3b577 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 509 | if (I.getType()->isFloatingPoint()) { |
| 510 | if (ConstantFP *CFP = dyn_cast<ConstantFP>(I.getOperand(0))) |
| 511 | if (CFP->isExactlyValue(-0.0)) { |
| 512 | SDOperand Op2 = getValue(I.getOperand(1)); |
| 513 | setValue(&I, DAG.getNode(ISD::FNEG, Op2.getValueType(), Op2)); |
| 514 | return; |
| 515 | } |
| 516 | visitBinary(I, ISD::FSUB); |
| 517 | } else { |
| 518 | visitBinary(I, ISD::SUB); |
| 519 | } |
Chris Lattner | f68fd0b | 2005-04-02 05:04:50 +0000 | [diff] [blame] | 520 | } |
| 521 | |
Chris Lattner | 7f9e078 | 2005-08-22 17:28:31 +0000 | [diff] [blame] | 522 | void SelectionDAGLowering::visitBinary(User &I, unsigned Opcode, bool isShift) { |
Chris Lattner | 7a60d91 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 523 | SDOperand Op1 = getValue(I.getOperand(0)); |
| 524 | SDOperand Op2 = getValue(I.getOperand(1)); |
Chris Lattner | 96c2675 | 2005-01-19 22:31:21 +0000 | [diff] [blame] | 525 | |
Chris Lattner | 7f9e078 | 2005-08-22 17:28:31 +0000 | [diff] [blame] | 526 | if (isShift) |
Chris Lattner | a66403d | 2005-09-02 00:19:37 +0000 | [diff] [blame] | 527 | Op2 = DAG.getNode(ISD::ANY_EXTEND, TLI.getShiftAmountTy(), Op2); |
Chris Lattner | 96c2675 | 2005-01-19 22:31:21 +0000 | [diff] [blame] | 528 | |
Chris Lattner | 7a60d91 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 529 | setValue(&I, DAG.getNode(Opcode, Op1.getValueType(), Op1, Op2)); |
| 530 | } |
| 531 | |
| 532 | void SelectionDAGLowering::visitSetCC(User &I,ISD::CondCode SignedOpcode, |
| 533 | ISD::CondCode UnsignedOpcode) { |
| 534 | SDOperand Op1 = getValue(I.getOperand(0)); |
| 535 | SDOperand Op2 = getValue(I.getOperand(1)); |
| 536 | ISD::CondCode Opcode = SignedOpcode; |
| 537 | if (I.getOperand(0)->getType()->isUnsigned()) |
| 538 | Opcode = UnsignedOpcode; |
Chris Lattner | d47675e | 2005-08-09 20:20:18 +0000 | [diff] [blame] | 539 | setValue(&I, DAG.getSetCC(MVT::i1, Op1, Op2, Opcode)); |
Chris Lattner | 7a60d91 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 540 | } |
| 541 | |
| 542 | void SelectionDAGLowering::visitSelect(User &I) { |
| 543 | SDOperand Cond = getValue(I.getOperand(0)); |
| 544 | SDOperand TrueVal = getValue(I.getOperand(1)); |
| 545 | SDOperand FalseVal = getValue(I.getOperand(2)); |
| 546 | setValue(&I, DAG.getNode(ISD::SELECT, TrueVal.getValueType(), Cond, |
| 547 | TrueVal, FalseVal)); |
| 548 | } |
| 549 | |
| 550 | void SelectionDAGLowering::visitCast(User &I) { |
| 551 | SDOperand N = getValue(I.getOperand(0)); |
| 552 | MVT::ValueType SrcTy = TLI.getValueType(I.getOperand(0)->getType()); |
| 553 | MVT::ValueType DestTy = TLI.getValueType(I.getType()); |
| 554 | |
| 555 | if (N.getValueType() == DestTy) { |
| 556 | setValue(&I, N); // noop cast. |
Chris Lattner | 2d8b55c | 2005-05-09 22:17:13 +0000 | [diff] [blame] | 557 | } else if (DestTy == MVT::i1) { |
| 558 | // Cast to bool is a comparison against zero, not truncation to zero. |
| 559 | SDOperand Zero = isInteger(SrcTy) ? DAG.getConstant(0, N.getValueType()) : |
| 560 | DAG.getConstantFP(0.0, N.getValueType()); |
Chris Lattner | d47675e | 2005-08-09 20:20:18 +0000 | [diff] [blame] | 561 | setValue(&I, DAG.getSetCC(MVT::i1, N, Zero, ISD::SETNE)); |
Chris Lattner | 2a6db3c | 2005-01-08 08:08:56 +0000 | [diff] [blame] | 562 | } else if (isInteger(SrcTy)) { |
| 563 | if (isInteger(DestTy)) { // Int -> Int cast |
| 564 | if (DestTy < SrcTy) // Truncating cast? |
| 565 | setValue(&I, DAG.getNode(ISD::TRUNCATE, DestTy, N)); |
| 566 | else if (I.getOperand(0)->getType()->isSigned()) |
| 567 | setValue(&I, DAG.getNode(ISD::SIGN_EXTEND, DestTy, N)); |
| 568 | else |
| 569 | setValue(&I, DAG.getNode(ISD::ZERO_EXTEND, DestTy, N)); |
| 570 | } else { // Int -> FP cast |
| 571 | if (I.getOperand(0)->getType()->isSigned()) |
| 572 | setValue(&I, DAG.getNode(ISD::SINT_TO_FP, DestTy, N)); |
| 573 | else |
| 574 | setValue(&I, DAG.getNode(ISD::UINT_TO_FP, DestTy, N)); |
| 575 | } |
Chris Lattner | 7a60d91 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 576 | } else { |
Chris Lattner | 2a6db3c | 2005-01-08 08:08:56 +0000 | [diff] [blame] | 577 | assert(isFloatingPoint(SrcTy) && "Unknown value type!"); |
| 578 | if (isFloatingPoint(DestTy)) { // FP -> FP cast |
| 579 | if (DestTy < SrcTy) // Rounding cast? |
| 580 | setValue(&I, DAG.getNode(ISD::FP_ROUND, DestTy, N)); |
| 581 | else |
| 582 | setValue(&I, DAG.getNode(ISD::FP_EXTEND, DestTy, N)); |
| 583 | } else { // FP -> Int cast. |
| 584 | if (I.getType()->isSigned()) |
| 585 | setValue(&I, DAG.getNode(ISD::FP_TO_SINT, DestTy, N)); |
| 586 | else |
| 587 | setValue(&I, DAG.getNode(ISD::FP_TO_UINT, DestTy, N)); |
| 588 | } |
Chris Lattner | 7a60d91 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 589 | } |
| 590 | } |
| 591 | |
| 592 | void SelectionDAGLowering::visitGetElementPtr(User &I) { |
| 593 | SDOperand N = getValue(I.getOperand(0)); |
| 594 | const Type *Ty = I.getOperand(0)->getType(); |
| 595 | const Type *UIntPtrTy = TD.getIntPtrType(); |
| 596 | |
| 597 | for (GetElementPtrInst::op_iterator OI = I.op_begin()+1, E = I.op_end(); |
| 598 | OI != E; ++OI) { |
| 599 | Value *Idx = *OI; |
| 600 | if (const StructType *StTy = dyn_cast<StructType> (Ty)) { |
| 601 | unsigned Field = cast<ConstantUInt>(Idx)->getValue(); |
| 602 | if (Field) { |
| 603 | // N = N + Offset |
| 604 | uint64_t Offset = TD.getStructLayout(StTy)->MemberOffsets[Field]; |
| 605 | N = DAG.getNode(ISD::ADD, N.getValueType(), N, |
Misha Brukman | 7745116 | 2005-04-22 04:01:18 +0000 | [diff] [blame] | 606 | getIntPtrConstant(Offset)); |
Chris Lattner | 7a60d91 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 607 | } |
| 608 | Ty = StTy->getElementType(Field); |
| 609 | } else { |
| 610 | Ty = cast<SequentialType>(Ty)->getElementType(); |
Chris Lattner | 19a8399 | 2005-01-07 21:56:57 +0000 | [diff] [blame] | 611 | |
Chris Lattner | 43535a1 | 2005-11-09 04:45:33 +0000 | [diff] [blame] | 612 | // If this is a constant subscript, handle it quickly. |
| 613 | if (ConstantInt *CI = dyn_cast<ConstantInt>(Idx)) { |
| 614 | if (CI->getRawValue() == 0) continue; |
Chris Lattner | 19a8399 | 2005-01-07 21:56:57 +0000 | [diff] [blame] | 615 | |
Chris Lattner | 43535a1 | 2005-11-09 04:45:33 +0000 | [diff] [blame] | 616 | uint64_t Offs; |
| 617 | if (ConstantSInt *CSI = dyn_cast<ConstantSInt>(CI)) |
| 618 | Offs = (int64_t)TD.getTypeSize(Ty)*CSI->getValue(); |
| 619 | else |
| 620 | Offs = TD.getTypeSize(Ty)*cast<ConstantUInt>(CI)->getValue(); |
| 621 | N = DAG.getNode(ISD::ADD, N.getValueType(), N, getIntPtrConstant(Offs)); |
| 622 | continue; |
Chris Lattner | 7a60d91 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 623 | } |
Chris Lattner | 43535a1 | 2005-11-09 04:45:33 +0000 | [diff] [blame] | 624 | |
| 625 | // N = N + Idx * ElementSize; |
| 626 | uint64_t ElementSize = TD.getTypeSize(Ty); |
| 627 | SDOperand IdxN = getValue(Idx); |
| 628 | |
| 629 | // If the index is smaller or larger than intptr_t, truncate or extend |
| 630 | // it. |
| 631 | if (IdxN.getValueType() < N.getValueType()) { |
| 632 | if (Idx->getType()->isSigned()) |
| 633 | IdxN = DAG.getNode(ISD::SIGN_EXTEND, N.getValueType(), IdxN); |
| 634 | else |
| 635 | IdxN = DAG.getNode(ISD::ZERO_EXTEND, N.getValueType(), IdxN); |
| 636 | } else if (IdxN.getValueType() > N.getValueType()) |
| 637 | IdxN = DAG.getNode(ISD::TRUNCATE, N.getValueType(), IdxN); |
| 638 | |
| 639 | // If this is a multiply by a power of two, turn it into a shl |
| 640 | // immediately. This is a very common case. |
| 641 | if (isPowerOf2_64(ElementSize)) { |
| 642 | unsigned Amt = Log2_64(ElementSize); |
| 643 | IdxN = DAG.getNode(ISD::SHL, N.getValueType(), IdxN, |
Chris Lattner | 41fd6d5 | 2005-11-09 16:50:40 +0000 | [diff] [blame^] | 644 | DAG.getConstant(Amt, TLI.getShiftAmountTy())); |
Chris Lattner | 43535a1 | 2005-11-09 04:45:33 +0000 | [diff] [blame] | 645 | N = DAG.getNode(ISD::ADD, N.getValueType(), N, IdxN); |
| 646 | continue; |
| 647 | } |
| 648 | |
| 649 | SDOperand Scale = getIntPtrConstant(ElementSize); |
| 650 | IdxN = DAG.getNode(ISD::MUL, N.getValueType(), IdxN, Scale); |
| 651 | N = DAG.getNode(ISD::ADD, N.getValueType(), N, IdxN); |
Chris Lattner | 7a60d91 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 652 | } |
| 653 | } |
| 654 | setValue(&I, N); |
| 655 | } |
| 656 | |
| 657 | void SelectionDAGLowering::visitAlloca(AllocaInst &I) { |
| 658 | // If this is a fixed sized alloca in the entry block of the function, |
| 659 | // allocate it statically on the stack. |
| 660 | if (FuncInfo.StaticAllocaMap.count(&I)) |
| 661 | return; // getValue will auto-populate this. |
| 662 | |
| 663 | const Type *Ty = I.getAllocatedType(); |
| 664 | uint64_t TySize = TLI.getTargetData().getTypeSize(Ty); |
Nate Begeman | 3ee3e69 | 2005-11-06 09:00:38 +0000 | [diff] [blame] | 665 | unsigned Align = std::max((unsigned)TLI.getTargetData().getTypeAlignment(Ty), |
| 666 | I.getAlignment()); |
Chris Lattner | 7a60d91 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 667 | |
| 668 | SDOperand AllocSize = getValue(I.getArraySize()); |
Chris Lattner | eccb73d | 2005-01-22 23:04:37 +0000 | [diff] [blame] | 669 | MVT::ValueType IntPtr = TLI.getPointerTy(); |
| 670 | if (IntPtr < AllocSize.getValueType()) |
| 671 | AllocSize = DAG.getNode(ISD::TRUNCATE, IntPtr, AllocSize); |
| 672 | else if (IntPtr > AllocSize.getValueType()) |
| 673 | AllocSize = DAG.getNode(ISD::ZERO_EXTEND, IntPtr, AllocSize); |
Chris Lattner | 7a60d91 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 674 | |
Chris Lattner | eccb73d | 2005-01-22 23:04:37 +0000 | [diff] [blame] | 675 | AllocSize = DAG.getNode(ISD::MUL, IntPtr, AllocSize, |
Chris Lattner | 7a60d91 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 676 | getIntPtrConstant(TySize)); |
| 677 | |
| 678 | // Handle alignment. If the requested alignment is less than or equal to the |
| 679 | // stack alignment, ignore it and round the size of the allocation up to the |
| 680 | // stack alignment size. If the size is greater than the stack alignment, we |
| 681 | // note this in the DYNAMIC_STACKALLOC node. |
| 682 | unsigned StackAlign = |
| 683 | TLI.getTargetMachine().getFrameInfo()->getStackAlignment(); |
| 684 | if (Align <= StackAlign) { |
| 685 | Align = 0; |
| 686 | // Add SA-1 to the size. |
| 687 | AllocSize = DAG.getNode(ISD::ADD, AllocSize.getValueType(), AllocSize, |
| 688 | getIntPtrConstant(StackAlign-1)); |
| 689 | // Mask out the low bits for alignment purposes. |
| 690 | AllocSize = DAG.getNode(ISD::AND, AllocSize.getValueType(), AllocSize, |
| 691 | getIntPtrConstant(~(uint64_t)(StackAlign-1))); |
| 692 | } |
| 693 | |
Chris Lattner | 96c262e | 2005-05-14 07:29:57 +0000 | [diff] [blame] | 694 | std::vector<MVT::ValueType> VTs; |
| 695 | VTs.push_back(AllocSize.getValueType()); |
| 696 | VTs.push_back(MVT::Other); |
| 697 | std::vector<SDOperand> Ops; |
| 698 | Ops.push_back(getRoot()); |
| 699 | Ops.push_back(AllocSize); |
| 700 | Ops.push_back(getIntPtrConstant(Align)); |
| 701 | SDOperand DSA = DAG.getNode(ISD::DYNAMIC_STACKALLOC, VTs, Ops); |
Chris Lattner | 7a60d91 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 702 | DAG.setRoot(setValue(&I, DSA).getValue(1)); |
| 703 | |
| 704 | // Inform the Frame Information that we have just allocated a variable-sized |
| 705 | // object. |
| 706 | CurMBB->getParent()->getFrameInfo()->CreateVariableSizedObject(); |
| 707 | } |
| 708 | |
| 709 | |
| 710 | void SelectionDAGLowering::visitLoad(LoadInst &I) { |
| 711 | SDOperand Ptr = getValue(I.getOperand(0)); |
Misha Brukman | 835702a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 712 | |
Chris Lattner | 4d9651c | 2005-01-17 22:19:26 +0000 | [diff] [blame] | 713 | SDOperand Root; |
| 714 | if (I.isVolatile()) |
| 715 | Root = getRoot(); |
| 716 | else { |
| 717 | // Do not serialize non-volatile loads against each other. |
| 718 | Root = DAG.getRoot(); |
| 719 | } |
| 720 | |
Chris Lattner | f5675a0 | 2005-05-09 04:08:33 +0000 | [diff] [blame] | 721 | SDOperand L = DAG.getLoad(TLI.getValueType(I.getType()), Root, Ptr, |
Andrew Lenharth | 2edc188 | 2005-06-29 18:54:02 +0000 | [diff] [blame] | 722 | DAG.getSrcValue(I.getOperand(0))); |
Chris Lattner | 4d9651c | 2005-01-17 22:19:26 +0000 | [diff] [blame] | 723 | setValue(&I, L); |
| 724 | |
| 725 | if (I.isVolatile()) |
| 726 | DAG.setRoot(L.getValue(1)); |
| 727 | else |
| 728 | PendingLoads.push_back(L.getValue(1)); |
Chris Lattner | 7a60d91 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 729 | } |
| 730 | |
| 731 | |
| 732 | void SelectionDAGLowering::visitStore(StoreInst &I) { |
| 733 | Value *SrcV = I.getOperand(0); |
| 734 | SDOperand Src = getValue(SrcV); |
| 735 | SDOperand Ptr = getValue(I.getOperand(1)); |
Chris Lattner | f5675a0 | 2005-05-09 04:08:33 +0000 | [diff] [blame] | 736 | DAG.setRoot(DAG.getNode(ISD::STORE, MVT::Other, getRoot(), Src, Ptr, |
Andrew Lenharth | 2edc188 | 2005-06-29 18:54:02 +0000 | [diff] [blame] | 737 | DAG.getSrcValue(I.getOperand(1)))); |
Chris Lattner | 7a60d91 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 738 | } |
| 739 | |
| 740 | void SelectionDAGLowering::visitCall(CallInst &I) { |
Chris Lattner | 18d2b34 | 2005-01-08 22:48:57 +0000 | [diff] [blame] | 741 | const char *RenameFn = 0; |
Chris Lattner | 20eaeae | 2005-05-09 20:22:36 +0000 | [diff] [blame] | 742 | SDOperand Tmp; |
Chris Lattner | 7a60d91 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 743 | if (Function *F = I.getCalledFunction()) |
Chris Lattner | 0c14000 | 2005-04-02 05:26:53 +0000 | [diff] [blame] | 744 | if (F->isExternal()) |
| 745 | switch (F->getIntrinsicID()) { |
| 746 | case 0: // Not an LLVM intrinsic. |
| 747 | if (F->getName() == "fabs" || F->getName() == "fabsf") { |
| 748 | if (I.getNumOperands() == 2 && // Basic sanity checks. |
| 749 | I.getOperand(1)->getType()->isFloatingPoint() && |
| 750 | I.getType() == I.getOperand(1)->getType()) { |
Chris Lattner | 20eaeae | 2005-05-09 20:22:36 +0000 | [diff] [blame] | 751 | Tmp = getValue(I.getOperand(1)); |
Chris Lattner | 0c14000 | 2005-04-02 05:26:53 +0000 | [diff] [blame] | 752 | setValue(&I, DAG.getNode(ISD::FABS, Tmp.getValueType(), Tmp)); |
| 753 | return; |
| 754 | } |
| 755 | } |
Chris Lattner | 8002640 | 2005-04-30 04:43:14 +0000 | [diff] [blame] | 756 | else if (F->getName() == "sin" || F->getName() == "sinf") { |
| 757 | if (I.getNumOperands() == 2 && // Basic sanity checks. |
| 758 | I.getOperand(1)->getType()->isFloatingPoint() && |
| 759 | I.getType() == I.getOperand(1)->getType()) { |
Chris Lattner | 20eaeae | 2005-05-09 20:22:36 +0000 | [diff] [blame] | 760 | Tmp = getValue(I.getOperand(1)); |
Chris Lattner | 8002640 | 2005-04-30 04:43:14 +0000 | [diff] [blame] | 761 | setValue(&I, DAG.getNode(ISD::FSIN, Tmp.getValueType(), Tmp)); |
| 762 | return; |
| 763 | } |
| 764 | } |
| 765 | else if (F->getName() == "cos" || F->getName() == "cosf") { |
| 766 | if (I.getNumOperands() == 2 && // Basic sanity checks. |
| 767 | I.getOperand(1)->getType()->isFloatingPoint() && |
| 768 | I.getType() == I.getOperand(1)->getType()) { |
Chris Lattner | 20eaeae | 2005-05-09 20:22:36 +0000 | [diff] [blame] | 769 | Tmp = getValue(I.getOperand(1)); |
Chris Lattner | 8002640 | 2005-04-30 04:43:14 +0000 | [diff] [blame] | 770 | setValue(&I, DAG.getNode(ISD::FCOS, Tmp.getValueType(), Tmp)); |
| 771 | return; |
| 772 | } |
| 773 | } |
Chris Lattner | 0c14000 | 2005-04-02 05:26:53 +0000 | [diff] [blame] | 774 | break; |
| 775 | case Intrinsic::vastart: visitVAStart(I); return; |
| 776 | case Intrinsic::vaend: visitVAEnd(I); return; |
| 777 | case Intrinsic::vacopy: visitVACopy(I); return; |
| 778 | case Intrinsic::returnaddress: visitFrameReturnAddress(I, false); return; |
| 779 | case Intrinsic::frameaddress: visitFrameReturnAddress(I, true); return; |
Chris Lattner | 20eaeae | 2005-05-09 20:22:36 +0000 | [diff] [blame] | 780 | |
Chris Lattner | 0fd8f9f | 2005-09-27 22:15:53 +0000 | [diff] [blame] | 781 | case Intrinsic::setjmp: |
| 782 | RenameFn = "_setjmp"+!TLI.usesUnderscoreSetJmpLongJmp(); |
| 783 | break; |
| 784 | case Intrinsic::longjmp: |
| 785 | RenameFn = "_longjmp"+!TLI.usesUnderscoreSetJmpLongJmp(); |
| 786 | break; |
Chris Lattner | 0c14000 | 2005-04-02 05:26:53 +0000 | [diff] [blame] | 787 | case Intrinsic::memcpy: visitMemIntrinsic(I, ISD::MEMCPY); return; |
| 788 | case Intrinsic::memset: visitMemIntrinsic(I, ISD::MEMSET); return; |
| 789 | case Intrinsic::memmove: visitMemIntrinsic(I, ISD::MEMMOVE); return; |
Misha Brukman | 835702a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 790 | |
Chris Lattner | 20eaeae | 2005-05-09 20:22:36 +0000 | [diff] [blame] | 791 | case Intrinsic::readport: |
Chris Lattner | e4f71d0 | 2005-05-14 13:56:55 +0000 | [diff] [blame] | 792 | case Intrinsic::readio: { |
| 793 | std::vector<MVT::ValueType> VTs; |
| 794 | VTs.push_back(TLI.getValueType(I.getType())); |
| 795 | VTs.push_back(MVT::Other); |
| 796 | std::vector<SDOperand> Ops; |
| 797 | Ops.push_back(getRoot()); |
| 798 | Ops.push_back(getValue(I.getOperand(1))); |
Chris Lattner | 20eaeae | 2005-05-09 20:22:36 +0000 | [diff] [blame] | 799 | Tmp = DAG.getNode(F->getIntrinsicID() == Intrinsic::readport ? |
Chris Lattner | e4f71d0 | 2005-05-14 13:56:55 +0000 | [diff] [blame] | 800 | ISD::READPORT : ISD::READIO, VTs, Ops); |
Jeff Cohen | 5f4ef3c | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 801 | |
Chris Lattner | 20eaeae | 2005-05-09 20:22:36 +0000 | [diff] [blame] | 802 | setValue(&I, Tmp); |
| 803 | DAG.setRoot(Tmp.getValue(1)); |
| 804 | return; |
Chris Lattner | e4f71d0 | 2005-05-14 13:56:55 +0000 | [diff] [blame] | 805 | } |
Chris Lattner | 20eaeae | 2005-05-09 20:22:36 +0000 | [diff] [blame] | 806 | case Intrinsic::writeport: |
| 807 | case Intrinsic::writeio: |
| 808 | DAG.setRoot(DAG.getNode(F->getIntrinsicID() == Intrinsic::writeport ? |
| 809 | ISD::WRITEPORT : ISD::WRITEIO, MVT::Other, |
| 810 | getRoot(), getValue(I.getOperand(1)), |
| 811 | getValue(I.getOperand(2)))); |
| 812 | return; |
Chris Lattner | 7876156 | 2005-05-05 17:55:17 +0000 | [diff] [blame] | 813 | case Intrinsic::dbg_stoppoint: |
| 814 | case Intrinsic::dbg_region_start: |
| 815 | case Intrinsic::dbg_region_end: |
| 816 | case Intrinsic::dbg_func_start: |
| 817 | case Intrinsic::dbg_declare: |
| 818 | if (I.getType() != Type::VoidTy) |
| 819 | setValue(&I, DAG.getNode(ISD::UNDEF, TLI.getValueType(I.getType()))); |
| 820 | return; |
| 821 | |
Chris Lattner | 0c14000 | 2005-04-02 05:26:53 +0000 | [diff] [blame] | 822 | case Intrinsic::isunordered: |
Chris Lattner | d47675e | 2005-08-09 20:20:18 +0000 | [diff] [blame] | 823 | setValue(&I, DAG.getSetCC(MVT::i1,getValue(I.getOperand(1)), |
| 824 | getValue(I.getOperand(2)), ISD::SETUO)); |
Chris Lattner | 0c14000 | 2005-04-02 05:26:53 +0000 | [diff] [blame] | 825 | return; |
Chris Lattner | 8002640 | 2005-04-30 04:43:14 +0000 | [diff] [blame] | 826 | |
| 827 | case Intrinsic::sqrt: |
| 828 | setValue(&I, DAG.getNode(ISD::FSQRT, |
| 829 | getValue(I.getOperand(1)).getValueType(), |
| 830 | getValue(I.getOperand(1)))); |
| 831 | return; |
| 832 | |
Chris Lattner | 20eaeae | 2005-05-09 20:22:36 +0000 | [diff] [blame] | 833 | case Intrinsic::pcmarker: |
| 834 | Tmp = getValue(I.getOperand(1)); |
| 835 | DAG.setRoot(DAG.getNode(ISD::PCMARKER, MVT::Other, getRoot(), Tmp)); |
Chris Lattner | 0c14000 | 2005-04-02 05:26:53 +0000 | [diff] [blame] | 836 | return; |
Andrew Lenharth | 5e17782 | 2005-05-03 17:19:30 +0000 | [diff] [blame] | 837 | case Intrinsic::cttz: |
| 838 | setValue(&I, DAG.getNode(ISD::CTTZ, |
| 839 | getValue(I.getOperand(1)).getValueType(), |
| 840 | getValue(I.getOperand(1)))); |
| 841 | return; |
| 842 | case Intrinsic::ctlz: |
| 843 | setValue(&I, DAG.getNode(ISD::CTLZ, |
| 844 | getValue(I.getOperand(1)).getValueType(), |
| 845 | getValue(I.getOperand(1)))); |
| 846 | return; |
| 847 | case Intrinsic::ctpop: |
| 848 | setValue(&I, DAG.getNode(ISD::CTPOP, |
| 849 | getValue(I.getOperand(1)).getValueType(), |
| 850 | getValue(I.getOperand(1)))); |
| 851 | return; |
Chris Lattner | 20eaeae | 2005-05-09 20:22:36 +0000 | [diff] [blame] | 852 | default: |
| 853 | std::cerr << I; |
| 854 | assert(0 && "This intrinsic is not implemented yet!"); |
| 855 | return; |
Chris Lattner | 0c14000 | 2005-04-02 05:26:53 +0000 | [diff] [blame] | 856 | } |
Misha Brukman | 835702a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 857 | |
Chris Lattner | 18d2b34 | 2005-01-08 22:48:57 +0000 | [diff] [blame] | 858 | SDOperand Callee; |
| 859 | if (!RenameFn) |
| 860 | Callee = getValue(I.getOperand(0)); |
| 861 | else |
| 862 | Callee = DAG.getExternalSymbol(RenameFn, TLI.getPointerTy()); |
Chris Lattner | 7a60d91 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 863 | std::vector<std::pair<SDOperand, const Type*> > Args; |
Misha Brukman | 835702a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 864 | |
Chris Lattner | 7a60d91 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 865 | for (unsigned i = 1, e = I.getNumOperands(); i != e; ++i) { |
| 866 | Value *Arg = I.getOperand(i); |
| 867 | SDOperand ArgNode = getValue(Arg); |
| 868 | Args.push_back(std::make_pair(ArgNode, Arg->getType())); |
| 869 | } |
Misha Brukman | 835702a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 870 | |
Nate Begeman | f656525 | 2005-03-26 01:29:23 +0000 | [diff] [blame] | 871 | const PointerType *PT = cast<PointerType>(I.getCalledValue()->getType()); |
| 872 | const FunctionType *FTy = cast<FunctionType>(PT->getElementType()); |
Misha Brukman | 835702a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 873 | |
Chris Lattner | 1f45cd7 | 2005-01-08 19:26:18 +0000 | [diff] [blame] | 874 | std::pair<SDOperand,SDOperand> Result = |
Chris Lattner | 111778e | 2005-05-12 19:56:57 +0000 | [diff] [blame] | 875 | TLI.LowerCallTo(getRoot(), I.getType(), FTy->isVarArg(), I.getCallingConv(), |
Chris Lattner | 2e77db6 | 2005-05-13 18:50:42 +0000 | [diff] [blame] | 876 | I.isTailCall(), Callee, Args, DAG); |
Chris Lattner | 7a60d91 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 877 | if (I.getType() != Type::VoidTy) |
Chris Lattner | 1f45cd7 | 2005-01-08 19:26:18 +0000 | [diff] [blame] | 878 | setValue(&I, Result.first); |
| 879 | DAG.setRoot(Result.second); |
Chris Lattner | 7a60d91 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 880 | } |
| 881 | |
| 882 | void SelectionDAGLowering::visitMalloc(MallocInst &I) { |
| 883 | SDOperand Src = getValue(I.getOperand(0)); |
| 884 | |
| 885 | MVT::ValueType IntPtr = TLI.getPointerTy(); |
Chris Lattner | eccb73d | 2005-01-22 23:04:37 +0000 | [diff] [blame] | 886 | |
| 887 | if (IntPtr < Src.getValueType()) |
| 888 | Src = DAG.getNode(ISD::TRUNCATE, IntPtr, Src); |
| 889 | else if (IntPtr > Src.getValueType()) |
| 890 | Src = DAG.getNode(ISD::ZERO_EXTEND, IntPtr, Src); |
Chris Lattner | 7a60d91 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 891 | |
| 892 | // Scale the source by the type size. |
| 893 | uint64_t ElementSize = TD.getTypeSize(I.getType()->getElementType()); |
| 894 | Src = DAG.getNode(ISD::MUL, Src.getValueType(), |
| 895 | Src, getIntPtrConstant(ElementSize)); |
| 896 | |
| 897 | std::vector<std::pair<SDOperand, const Type*> > Args; |
| 898 | Args.push_back(std::make_pair(Src, TLI.getTargetData().getIntPtrType())); |
Chris Lattner | 1f45cd7 | 2005-01-08 19:26:18 +0000 | [diff] [blame] | 899 | |
| 900 | std::pair<SDOperand,SDOperand> Result = |
Chris Lattner | 2e77db6 | 2005-05-13 18:50:42 +0000 | [diff] [blame] | 901 | TLI.LowerCallTo(getRoot(), I.getType(), false, CallingConv::C, true, |
Chris Lattner | 1f45cd7 | 2005-01-08 19:26:18 +0000 | [diff] [blame] | 902 | DAG.getExternalSymbol("malloc", IntPtr), |
| 903 | Args, DAG); |
| 904 | setValue(&I, Result.first); // Pointers always fit in registers |
| 905 | DAG.setRoot(Result.second); |
Chris Lattner | 7a60d91 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 906 | } |
| 907 | |
| 908 | void SelectionDAGLowering::visitFree(FreeInst &I) { |
| 909 | std::vector<std::pair<SDOperand, const Type*> > Args; |
| 910 | Args.push_back(std::make_pair(getValue(I.getOperand(0)), |
| 911 | TLI.getTargetData().getIntPtrType())); |
| 912 | MVT::ValueType IntPtr = TLI.getPointerTy(); |
Chris Lattner | 1f45cd7 | 2005-01-08 19:26:18 +0000 | [diff] [blame] | 913 | std::pair<SDOperand,SDOperand> Result = |
Chris Lattner | 2e77db6 | 2005-05-13 18:50:42 +0000 | [diff] [blame] | 914 | TLI.LowerCallTo(getRoot(), Type::VoidTy, false, CallingConv::C, true, |
Chris Lattner | 1f45cd7 | 2005-01-08 19:26:18 +0000 | [diff] [blame] | 915 | DAG.getExternalSymbol("free", IntPtr), Args, DAG); |
| 916 | DAG.setRoot(Result.second); |
Chris Lattner | 7a60d91 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 917 | } |
| 918 | |
Chris Lattner | 13d7c25 | 2005-08-26 20:54:47 +0000 | [diff] [blame] | 919 | // InsertAtEndOfBasicBlock - This method should be implemented by targets that |
| 920 | // mark instructions with the 'usesCustomDAGSchedInserter' flag. These |
| 921 | // instructions are special in various ways, which require special support to |
| 922 | // insert. The specified MachineInstr is created but not inserted into any |
| 923 | // basic blocks, and the scheduler passes ownership of it to this method. |
| 924 | MachineBasicBlock *TargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI, |
| 925 | MachineBasicBlock *MBB) { |
| 926 | std::cerr << "If a target marks an instruction with " |
| 927 | "'usesCustomDAGSchedInserter', it must implement " |
| 928 | "TargetLowering::InsertAtEndOfBasicBlock!\n"; |
| 929 | abort(); |
| 930 | return 0; |
| 931 | } |
| 932 | |
Nate Begeman | 78afac2 | 2005-10-18 23:23:37 +0000 | [diff] [blame] | 933 | SDOperand TargetLowering::LowerReturnTo(SDOperand Chain, SDOperand Op, |
| 934 | SelectionDAG &DAG) { |
| 935 | return DAG.getNode(ISD::RET, MVT::Other, Chain, Op); |
| 936 | } |
| 937 | |
Chris Lattner | f5473e4 | 2005-07-05 19:57:53 +0000 | [diff] [blame] | 938 | SDOperand TargetLowering::LowerVAStart(SDOperand Chain, |
| 939 | SDOperand VAListP, Value *VAListV, |
| 940 | SelectionDAG &DAG) { |
Chris Lattner | 7a60d91 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 941 | // We have no sane default behavior, just emit a useful error message and bail |
| 942 | // out. |
Chris Lattner | 58cfd79 | 2005-01-09 00:00:49 +0000 | [diff] [blame] | 943 | std::cerr << "Variable arguments handling not implemented on this target!\n"; |
Chris Lattner | 7a60d91 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 944 | abort(); |
Chris Lattner | f5473e4 | 2005-07-05 19:57:53 +0000 | [diff] [blame] | 945 | return SDOperand(); |
Chris Lattner | 7a60d91 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 946 | } |
| 947 | |
Chris Lattner | f5473e4 | 2005-07-05 19:57:53 +0000 | [diff] [blame] | 948 | SDOperand TargetLowering::LowerVAEnd(SDOperand Chain, SDOperand LP, Value *LV, |
Chris Lattner | 58cfd79 | 2005-01-09 00:00:49 +0000 | [diff] [blame] | 949 | SelectionDAG &DAG) { |
| 950 | // Default to a noop. |
| 951 | return Chain; |
| 952 | } |
| 953 | |
Chris Lattner | f5473e4 | 2005-07-05 19:57:53 +0000 | [diff] [blame] | 954 | SDOperand TargetLowering::LowerVACopy(SDOperand Chain, |
| 955 | SDOperand SrcP, Value *SrcV, |
| 956 | SDOperand DestP, Value *DestV, |
| 957 | SelectionDAG &DAG) { |
| 958 | // Default to copying the input list. |
| 959 | SDOperand Val = DAG.getLoad(getPointerTy(), Chain, |
| 960 | SrcP, DAG.getSrcValue(SrcV)); |
Andrew Lenharth | 2531452 | 2005-06-22 21:04:42 +0000 | [diff] [blame] | 961 | SDOperand Result = DAG.getNode(ISD::STORE, MVT::Other, Val.getValue(1), |
Chris Lattner | f5473e4 | 2005-07-05 19:57:53 +0000 | [diff] [blame] | 962 | Val, DestP, DAG.getSrcValue(DestV)); |
| 963 | return Result; |
Chris Lattner | 58cfd79 | 2005-01-09 00:00:49 +0000 | [diff] [blame] | 964 | } |
| 965 | |
| 966 | std::pair<SDOperand,SDOperand> |
Chris Lattner | f5473e4 | 2005-07-05 19:57:53 +0000 | [diff] [blame] | 967 | TargetLowering::LowerVAArg(SDOperand Chain, SDOperand VAListP, Value *VAListV, |
| 968 | const Type *ArgTy, SelectionDAG &DAG) { |
Chris Lattner | 58cfd79 | 2005-01-09 00:00:49 +0000 | [diff] [blame] | 969 | // We have no sane default behavior, just emit a useful error message and bail |
| 970 | // out. |
| 971 | std::cerr << "Variable arguments handling not implemented on this target!\n"; |
| 972 | abort(); |
Misha Brukman | 73e929f | 2005-02-17 21:39:27 +0000 | [diff] [blame] | 973 | return std::make_pair(SDOperand(), SDOperand()); |
Chris Lattner | 58cfd79 | 2005-01-09 00:00:49 +0000 | [diff] [blame] | 974 | } |
| 975 | |
| 976 | |
| 977 | void SelectionDAGLowering::visitVAStart(CallInst &I) { |
Chris Lattner | f5473e4 | 2005-07-05 19:57:53 +0000 | [diff] [blame] | 978 | DAG.setRoot(TLI.LowerVAStart(getRoot(), getValue(I.getOperand(1)), |
| 979 | I.getOperand(1), DAG)); |
Chris Lattner | 58cfd79 | 2005-01-09 00:00:49 +0000 | [diff] [blame] | 980 | } |
| 981 | |
| 982 | void SelectionDAGLowering::visitVAArg(VAArgInst &I) { |
| 983 | std::pair<SDOperand,SDOperand> Result = |
Chris Lattner | f5473e4 | 2005-07-05 19:57:53 +0000 | [diff] [blame] | 984 | TLI.LowerVAArg(getRoot(), getValue(I.getOperand(0)), I.getOperand(0), |
Andrew Lenharth | 9144ec4 | 2005-06-18 18:34:52 +0000 | [diff] [blame] | 985 | I.getType(), DAG); |
Chris Lattner | 58cfd79 | 2005-01-09 00:00:49 +0000 | [diff] [blame] | 986 | setValue(&I, Result.first); |
| 987 | DAG.setRoot(Result.second); |
Chris Lattner | 7a60d91 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 988 | } |
| 989 | |
| 990 | void SelectionDAGLowering::visitVAEnd(CallInst &I) { |
Jeff Cohen | 5f4ef3c | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 991 | DAG.setRoot(TLI.LowerVAEnd(getRoot(), getValue(I.getOperand(1)), |
Chris Lattner | f5473e4 | 2005-07-05 19:57:53 +0000 | [diff] [blame] | 992 | I.getOperand(1), DAG)); |
Chris Lattner | 7a60d91 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 993 | } |
| 994 | |
| 995 | void SelectionDAGLowering::visitVACopy(CallInst &I) { |
Chris Lattner | f5473e4 | 2005-07-05 19:57:53 +0000 | [diff] [blame] | 996 | SDOperand Result = |
| 997 | TLI.LowerVACopy(getRoot(), getValue(I.getOperand(2)), I.getOperand(2), |
| 998 | getValue(I.getOperand(1)), I.getOperand(1), DAG); |
| 999 | DAG.setRoot(Result); |
Chris Lattner | 7a60d91 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1000 | } |
| 1001 | |
Chris Lattner | 58cfd79 | 2005-01-09 00:00:49 +0000 | [diff] [blame] | 1002 | |
| 1003 | // It is always conservatively correct for llvm.returnaddress and |
| 1004 | // llvm.frameaddress to return 0. |
| 1005 | std::pair<SDOperand, SDOperand> |
| 1006 | TargetLowering::LowerFrameReturnAddress(bool isFrameAddr, SDOperand Chain, |
| 1007 | unsigned Depth, SelectionDAG &DAG) { |
| 1008 | return std::make_pair(DAG.getConstant(0, getPointerTy()), Chain); |
Chris Lattner | 7a60d91 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1009 | } |
| 1010 | |
Chris Lattner | 29dcc71 | 2005-05-14 05:50:48 +0000 | [diff] [blame] | 1011 | SDOperand TargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) { |
Chris Lattner | 897cd7d | 2005-01-16 07:28:41 +0000 | [diff] [blame] | 1012 | assert(0 && "LowerOperation not implemented for this target!"); |
| 1013 | abort(); |
Misha Brukman | 73e929f | 2005-02-17 21:39:27 +0000 | [diff] [blame] | 1014 | return SDOperand(); |
Chris Lattner | 897cd7d | 2005-01-16 07:28:41 +0000 | [diff] [blame] | 1015 | } |
| 1016 | |
Chris Lattner | 58cfd79 | 2005-01-09 00:00:49 +0000 | [diff] [blame] | 1017 | void SelectionDAGLowering::visitFrameReturnAddress(CallInst &I, bool isFrame) { |
| 1018 | unsigned Depth = (unsigned)cast<ConstantUInt>(I.getOperand(1))->getValue(); |
| 1019 | std::pair<SDOperand,SDOperand> Result = |
Chris Lattner | 4108bb0 | 2005-01-17 19:43:36 +0000 | [diff] [blame] | 1020 | TLI.LowerFrameReturnAddress(isFrame, getRoot(), Depth, DAG); |
Chris Lattner | 58cfd79 | 2005-01-09 00:00:49 +0000 | [diff] [blame] | 1021 | setValue(&I, Result.first); |
| 1022 | DAG.setRoot(Result.second); |
Chris Lattner | 7a60d91 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1023 | } |
| 1024 | |
Chris Lattner | 875def9 | 2005-01-11 05:56:49 +0000 | [diff] [blame] | 1025 | void SelectionDAGLowering::visitMemIntrinsic(CallInst &I, unsigned Op) { |
| 1026 | std::vector<SDOperand> Ops; |
Chris Lattner | 4108bb0 | 2005-01-17 19:43:36 +0000 | [diff] [blame] | 1027 | Ops.push_back(getRoot()); |
Chris Lattner | 875def9 | 2005-01-11 05:56:49 +0000 | [diff] [blame] | 1028 | Ops.push_back(getValue(I.getOperand(1))); |
| 1029 | Ops.push_back(getValue(I.getOperand(2))); |
| 1030 | Ops.push_back(getValue(I.getOperand(3))); |
| 1031 | Ops.push_back(getValue(I.getOperand(4))); |
| 1032 | DAG.setRoot(DAG.getNode(Op, MVT::Other, Ops)); |
Chris Lattner | 7a60d91 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1033 | } |
| 1034 | |
Chris Lattner | 875def9 | 2005-01-11 05:56:49 +0000 | [diff] [blame] | 1035 | //===----------------------------------------------------------------------===// |
| 1036 | // SelectionDAGISel code |
| 1037 | //===----------------------------------------------------------------------===// |
Chris Lattner | 7a60d91 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1038 | |
| 1039 | unsigned SelectionDAGISel::MakeReg(MVT::ValueType VT) { |
| 1040 | return RegMap->createVirtualRegister(TLI.getRegClassFor(VT)); |
| 1041 | } |
| 1042 | |
Chris Lattner | c9950c1 | 2005-08-17 06:37:43 +0000 | [diff] [blame] | 1043 | void SelectionDAGISel::getAnalysisUsage(AnalysisUsage &AU) const { |
Chris Lattner | 1a908c8 | 2005-08-18 17:35:14 +0000 | [diff] [blame] | 1044 | // FIXME: we only modify the CFG to split critical edges. This |
| 1045 | // updates dom and loop info. |
Chris Lattner | c9950c1 | 2005-08-17 06:37:43 +0000 | [diff] [blame] | 1046 | } |
Chris Lattner | 7a60d91 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1047 | |
| 1048 | |
| 1049 | bool SelectionDAGISel::runOnFunction(Function &Fn) { |
| 1050 | MachineFunction &MF = MachineFunction::construct(&Fn, TLI.getTargetMachine()); |
| 1051 | RegMap = MF.getSSARegMap(); |
| 1052 | DEBUG(std::cerr << "\n\n\n=== " << Fn.getName() << "\n"); |
| 1053 | |
Chris Lattner | c9950c1 | 2005-08-17 06:37:43 +0000 | [diff] [blame] | 1054 | // First pass, split all critical edges for PHI nodes with incoming values |
| 1055 | // that are constants, this way the load of the constant into a vreg will not |
| 1056 | // be placed into MBBs that are used some other way. |
Chris Lattner | 1a908c8 | 2005-08-18 17:35:14 +0000 | [diff] [blame] | 1057 | for (Function::iterator BB = Fn.begin(), E = Fn.end(); BB != E; ++BB) { |
| 1058 | PHINode *PN; |
| 1059 | for (BasicBlock::iterator BBI = BB->begin(); |
| 1060 | (PN = dyn_cast<PHINode>(BBI)); ++BBI) |
| 1061 | for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) |
| 1062 | if (isa<Constant>(PN->getIncomingValue(i))) |
| 1063 | SplitCriticalEdge(PN->getIncomingBlock(i), BB); |
| 1064 | } |
Chris Lattner | c9950c1 | 2005-08-17 06:37:43 +0000 | [diff] [blame] | 1065 | |
Chris Lattner | 7a60d91 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1066 | FunctionLoweringInfo FuncInfo(TLI, Fn, MF); |
| 1067 | |
| 1068 | for (Function::iterator I = Fn.begin(), E = Fn.end(); I != E; ++I) |
| 1069 | SelectBasicBlock(I, MF, FuncInfo); |
Misha Brukman | 835702a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 1070 | |
Chris Lattner | 7a60d91 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1071 | return true; |
| 1072 | } |
| 1073 | |
| 1074 | |
Chris Lattner | 718b5c2 | 2005-01-13 17:59:43 +0000 | [diff] [blame] | 1075 | SDOperand SelectionDAGISel:: |
| 1076 | CopyValueToVirtualRegister(SelectionDAGLowering &SDL, Value *V, unsigned Reg) { |
Chris Lattner | 613f79f | 2005-01-11 22:03:46 +0000 | [diff] [blame] | 1077 | SDOperand Op = SDL.getValue(V); |
Chris Lattner | e727af0 | 2005-01-13 20:50:02 +0000 | [diff] [blame] | 1078 | assert((Op.getOpcode() != ISD::CopyFromReg || |
Chris Lattner | 3318232 | 2005-08-16 21:55:35 +0000 | [diff] [blame] | 1079 | cast<RegisterSDNode>(Op.getOperand(1))->getReg() != Reg) && |
Chris Lattner | e727af0 | 2005-01-13 20:50:02 +0000 | [diff] [blame] | 1080 | "Copy from a reg to the same reg!"); |
Chris Lattner | 3318232 | 2005-08-16 21:55:35 +0000 | [diff] [blame] | 1081 | |
| 1082 | // If this type is not legal, we must make sure to not create an invalid |
| 1083 | // register use. |
| 1084 | MVT::ValueType SrcVT = Op.getValueType(); |
| 1085 | MVT::ValueType DestVT = TLI.getTypeToTransformTo(SrcVT); |
| 1086 | SelectionDAG &DAG = SDL.DAG; |
| 1087 | if (SrcVT == DestVT) { |
| 1088 | return DAG.getCopyToReg(SDL.getRoot(), Reg, Op); |
| 1089 | } else if (SrcVT < DestVT) { |
| 1090 | // The src value is promoted to the register. |
Chris Lattner | ba28c27 | 2005-08-17 06:06:25 +0000 | [diff] [blame] | 1091 | if (MVT::isFloatingPoint(SrcVT)) |
| 1092 | Op = DAG.getNode(ISD::FP_EXTEND, DestVT, Op); |
| 1093 | else |
Chris Lattner | a66403d | 2005-09-02 00:19:37 +0000 | [diff] [blame] | 1094 | Op = DAG.getNode(ISD::ANY_EXTEND, DestVT, Op); |
Chris Lattner | 3318232 | 2005-08-16 21:55:35 +0000 | [diff] [blame] | 1095 | return DAG.getCopyToReg(SDL.getRoot(), Reg, Op); |
| 1096 | } else { |
| 1097 | // The src value is expanded into multiple registers. |
| 1098 | SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, DestVT, |
| 1099 | Op, DAG.getConstant(0, MVT::i32)); |
| 1100 | SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, DestVT, |
| 1101 | Op, DAG.getConstant(1, MVT::i32)); |
| 1102 | Op = DAG.getCopyToReg(SDL.getRoot(), Reg, Lo); |
| 1103 | return DAG.getCopyToReg(Op, Reg+1, Hi); |
| 1104 | } |
Chris Lattner | 7a60d91 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1105 | } |
| 1106 | |
Chris Lattner | 16f64df | 2005-01-17 17:15:02 +0000 | [diff] [blame] | 1107 | void SelectionDAGISel:: |
| 1108 | LowerArguments(BasicBlock *BB, SelectionDAGLowering &SDL, |
| 1109 | std::vector<SDOperand> &UnorderedChains) { |
| 1110 | // If this is the entry block, emit arguments. |
| 1111 | Function &F = *BB->getParent(); |
Chris Lattner | e3c2cf4 | 2005-01-17 17:55:19 +0000 | [diff] [blame] | 1112 | FunctionLoweringInfo &FuncInfo = SDL.FuncInfo; |
Chris Lattner | 6871b23 | 2005-10-30 19:42:35 +0000 | [diff] [blame] | 1113 | SDOperand OldRoot = SDL.DAG.getRoot(); |
| 1114 | std::vector<SDOperand> Args = TLI.LowerArguments(F, SDL.DAG); |
Chris Lattner | 16f64df | 2005-01-17 17:15:02 +0000 | [diff] [blame] | 1115 | |
Chris Lattner | 6871b23 | 2005-10-30 19:42:35 +0000 | [diff] [blame] | 1116 | unsigned a = 0; |
| 1117 | for (Function::arg_iterator AI = F.arg_begin(), E = F.arg_end(); |
| 1118 | AI != E; ++AI, ++a) |
| 1119 | if (!AI->use_empty()) { |
| 1120 | SDL.setValue(AI, Args[a]); |
Chris Lattner | d4382f0 | 2005-09-13 19:30:54 +0000 | [diff] [blame] | 1121 | |
Chris Lattner | 6871b23 | 2005-10-30 19:42:35 +0000 | [diff] [blame] | 1122 | // If this argument is live outside of the entry block, insert a copy from |
| 1123 | // whereever we got it to the vreg that other BB's will reference it as. |
| 1124 | if (FuncInfo.ValueMap.count(AI)) { |
| 1125 | SDOperand Copy = |
| 1126 | CopyValueToVirtualRegister(SDL, AI, FuncInfo.ValueMap[AI]); |
| 1127 | UnorderedChains.push_back(Copy); |
| 1128 | } |
Chris Lattner | e3c2cf4 | 2005-01-17 17:55:19 +0000 | [diff] [blame] | 1129 | } |
Chris Lattner | 6871b23 | 2005-10-30 19:42:35 +0000 | [diff] [blame] | 1130 | |
| 1131 | // Next, if the function has live ins that need to be copied into vregs, |
| 1132 | // emit the copies now, into the top of the block. |
| 1133 | MachineFunction &MF = SDL.DAG.getMachineFunction(); |
| 1134 | if (MF.livein_begin() != MF.livein_end()) { |
| 1135 | SSARegMap *RegMap = MF.getSSARegMap(); |
| 1136 | const MRegisterInfo &MRI = *MF.getTarget().getRegisterInfo(); |
| 1137 | for (MachineFunction::livein_iterator LI = MF.livein_begin(), |
| 1138 | E = MF.livein_end(); LI != E; ++LI) |
| 1139 | if (LI->second) |
| 1140 | MRI.copyRegToReg(*MF.begin(), MF.begin()->end(), LI->second, |
| 1141 | LI->first, RegMap->getRegClass(LI->second)); |
Chris Lattner | 16f64df | 2005-01-17 17:15:02 +0000 | [diff] [blame] | 1142 | } |
Chris Lattner | 6871b23 | 2005-10-30 19:42:35 +0000 | [diff] [blame] | 1143 | |
| 1144 | // Finally, if the target has anything special to do, allow it to do so. |
| 1145 | EmitFunctionEntryCode(F, SDL.DAG.getMachineFunction()); |
Chris Lattner | 16f64df | 2005-01-17 17:15:02 +0000 | [diff] [blame] | 1146 | } |
| 1147 | |
| 1148 | |
Chris Lattner | 7a60d91 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1149 | void SelectionDAGISel::BuildSelectionDAG(SelectionDAG &DAG, BasicBlock *LLVMBB, |
| 1150 | std::vector<std::pair<MachineInstr*, unsigned> > &PHINodesToUpdate, |
| 1151 | FunctionLoweringInfo &FuncInfo) { |
| 1152 | SelectionDAGLowering SDL(DAG, TLI, FuncInfo); |
Chris Lattner | 718b5c2 | 2005-01-13 17:59:43 +0000 | [diff] [blame] | 1153 | |
| 1154 | std::vector<SDOperand> UnorderedChains; |
Misha Brukman | 835702a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 1155 | |
Chris Lattner | 6871b23 | 2005-10-30 19:42:35 +0000 | [diff] [blame] | 1156 | // Lower any arguments needed in this block if this is the entry block. |
| 1157 | if (LLVMBB == &LLVMBB->getParent()->front()) |
| 1158 | LowerArguments(LLVMBB, SDL, UnorderedChains); |
Chris Lattner | 7a60d91 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1159 | |
| 1160 | BB = FuncInfo.MBBMap[LLVMBB]; |
| 1161 | SDL.setCurrentBasicBlock(BB); |
| 1162 | |
| 1163 | // Lower all of the non-terminator instructions. |
| 1164 | for (BasicBlock::iterator I = LLVMBB->begin(), E = --LLVMBB->end(); |
| 1165 | I != E; ++I) |
| 1166 | SDL.visit(*I); |
| 1167 | |
| 1168 | // Ensure that all instructions which are used outside of their defining |
| 1169 | // blocks are available as virtual registers. |
| 1170 | for (BasicBlock::iterator I = LLVMBB->begin(), E = LLVMBB->end(); I != E;++I) |
Chris Lattner | 613f79f | 2005-01-11 22:03:46 +0000 | [diff] [blame] | 1171 | if (!I->use_empty() && !isa<PHINode>(I)) { |
Chris Lattner | a2c5d91 | 2005-01-09 01:16:24 +0000 | [diff] [blame] | 1172 | std::map<const Value*, unsigned>::iterator VMI =FuncInfo.ValueMap.find(I); |
Chris Lattner | 7a60d91 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1173 | if (VMI != FuncInfo.ValueMap.end()) |
Chris Lattner | 718b5c2 | 2005-01-13 17:59:43 +0000 | [diff] [blame] | 1174 | UnorderedChains.push_back( |
| 1175 | CopyValueToVirtualRegister(SDL, I, VMI->second)); |
Chris Lattner | 7a60d91 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1176 | } |
| 1177 | |
| 1178 | // Handle PHI nodes in successor blocks. Emit code into the SelectionDAG to |
| 1179 | // ensure constants are generated when needed. Remember the virtual registers |
| 1180 | // that need to be added to the Machine PHI nodes as input. We cannot just |
| 1181 | // directly add them, because expansion might result in multiple MBB's for one |
| 1182 | // BB. As such, the start of the BB might correspond to a different MBB than |
| 1183 | // the end. |
Misha Brukman | 835702a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 1184 | // |
Chris Lattner | 7a60d91 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1185 | |
| 1186 | // Emit constants only once even if used by multiple PHI nodes. |
| 1187 | std::map<Constant*, unsigned> ConstantsOut; |
| 1188 | |
| 1189 | // Check successor nodes PHI nodes that expect a constant to be available from |
| 1190 | // this block. |
| 1191 | TerminatorInst *TI = LLVMBB->getTerminator(); |
| 1192 | for (unsigned succ = 0, e = TI->getNumSuccessors(); succ != e; ++succ) { |
| 1193 | BasicBlock *SuccBB = TI->getSuccessor(succ); |
| 1194 | MachineBasicBlock::iterator MBBI = FuncInfo.MBBMap[SuccBB]->begin(); |
| 1195 | PHINode *PN; |
| 1196 | |
| 1197 | // At this point we know that there is a 1-1 correspondence between LLVM PHI |
| 1198 | // nodes and Machine PHI nodes, but the incoming operands have not been |
| 1199 | // emitted yet. |
| 1200 | for (BasicBlock::iterator I = SuccBB->begin(); |
Chris Lattner | 8ea875f | 2005-01-07 21:34:19 +0000 | [diff] [blame] | 1201 | (PN = dyn_cast<PHINode>(I)); ++I) |
| 1202 | if (!PN->use_empty()) { |
| 1203 | unsigned Reg; |
| 1204 | Value *PHIOp = PN->getIncomingValueForBlock(LLVMBB); |
| 1205 | if (Constant *C = dyn_cast<Constant>(PHIOp)) { |
| 1206 | unsigned &RegOut = ConstantsOut[C]; |
| 1207 | if (RegOut == 0) { |
| 1208 | RegOut = FuncInfo.CreateRegForValue(C); |
Chris Lattner | 718b5c2 | 2005-01-13 17:59:43 +0000 | [diff] [blame] | 1209 | UnorderedChains.push_back( |
| 1210 | CopyValueToVirtualRegister(SDL, C, RegOut)); |
Chris Lattner | 8ea875f | 2005-01-07 21:34:19 +0000 | [diff] [blame] | 1211 | } |
| 1212 | Reg = RegOut; |
| 1213 | } else { |
| 1214 | Reg = FuncInfo.ValueMap[PHIOp]; |
Chris Lattner | a2c5d91 | 2005-01-09 01:16:24 +0000 | [diff] [blame] | 1215 | if (Reg == 0) { |
Misha Brukman | 835702a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 1216 | assert(isa<AllocaInst>(PHIOp) && |
Chris Lattner | a2c5d91 | 2005-01-09 01:16:24 +0000 | [diff] [blame] | 1217 | FuncInfo.StaticAllocaMap.count(cast<AllocaInst>(PHIOp)) && |
| 1218 | "Didn't codegen value into a register!??"); |
| 1219 | Reg = FuncInfo.CreateRegForValue(PHIOp); |
Chris Lattner | 718b5c2 | 2005-01-13 17:59:43 +0000 | [diff] [blame] | 1220 | UnorderedChains.push_back( |
| 1221 | CopyValueToVirtualRegister(SDL, PHIOp, Reg)); |
Chris Lattner | a2c5d91 | 2005-01-09 01:16:24 +0000 | [diff] [blame] | 1222 | } |
Chris Lattner | 7a60d91 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1223 | } |
Misha Brukman | 835702a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 1224 | |
Chris Lattner | 8ea875f | 2005-01-07 21:34:19 +0000 | [diff] [blame] | 1225 | // Remember that this register needs to added to the machine PHI node as |
| 1226 | // the input for this MBB. |
| 1227 | unsigned NumElements = |
| 1228 | TLI.getNumElements(TLI.getValueType(PN->getType())); |
| 1229 | for (unsigned i = 0, e = NumElements; i != e; ++i) |
| 1230 | PHINodesToUpdate.push_back(std::make_pair(MBBI++, Reg+i)); |
Chris Lattner | 7a60d91 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1231 | } |
Chris Lattner | 7a60d91 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1232 | } |
| 1233 | ConstantsOut.clear(); |
| 1234 | |
Chris Lattner | 718b5c2 | 2005-01-13 17:59:43 +0000 | [diff] [blame] | 1235 | // Turn all of the unordered chains into one factored node. |
Chris Lattner | 2451684 | 2005-01-13 19:53:14 +0000 | [diff] [blame] | 1236 | if (!UnorderedChains.empty()) { |
Chris Lattner | b7cad90 | 2005-11-09 05:03:03 +0000 | [diff] [blame] | 1237 | SDOperand Root = SDL.getRoot(); |
| 1238 | if (Root.getOpcode() != ISD::EntryToken) { |
| 1239 | unsigned i = 0, e = UnorderedChains.size(); |
| 1240 | for (; i != e; ++i) { |
| 1241 | assert(UnorderedChains[i].Val->getNumOperands() > 1); |
| 1242 | if (UnorderedChains[i].Val->getOperand(0) == Root) |
| 1243 | break; // Don't add the root if we already indirectly depend on it. |
| 1244 | } |
| 1245 | |
| 1246 | if (i == e) |
| 1247 | UnorderedChains.push_back(Root); |
| 1248 | } |
Chris Lattner | 718b5c2 | 2005-01-13 17:59:43 +0000 | [diff] [blame] | 1249 | DAG.setRoot(DAG.getNode(ISD::TokenFactor, MVT::Other, UnorderedChains)); |
| 1250 | } |
| 1251 | |
Chris Lattner | 7a60d91 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1252 | // Lower the terminator after the copies are emitted. |
| 1253 | SDL.visit(*LLVMBB->getTerminator()); |
Chris Lattner | 4108bb0 | 2005-01-17 19:43:36 +0000 | [diff] [blame] | 1254 | |
| 1255 | // Make sure the root of the DAG is up-to-date. |
| 1256 | DAG.setRoot(SDL.getRoot()); |
Chris Lattner | 7a60d91 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1257 | } |
| 1258 | |
| 1259 | void SelectionDAGISel::SelectBasicBlock(BasicBlock *LLVMBB, MachineFunction &MF, |
| 1260 | FunctionLoweringInfo &FuncInfo) { |
Chris Lattner | ffcb0ae | 2005-01-23 04:36:26 +0000 | [diff] [blame] | 1261 | SelectionDAG DAG(TLI, MF); |
Chris Lattner | 7a60d91 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1262 | CurDAG = &DAG; |
| 1263 | std::vector<std::pair<MachineInstr*, unsigned> > PHINodesToUpdate; |
| 1264 | |
| 1265 | // First step, lower LLVM code to some DAG. This DAG may use operations and |
| 1266 | // types that are not supported by the target. |
| 1267 | BuildSelectionDAG(DAG, LLVMBB, PHINodesToUpdate, FuncInfo); |
| 1268 | |
Chris Lattner | bcfebeb | 2005-10-10 16:47:10 +0000 | [diff] [blame] | 1269 | // Run the DAG combiner in pre-legalize mode. |
| 1270 | DAG.Combine(false); |
Nate Begeman | 007c650 | 2005-09-07 00:15:36 +0000 | [diff] [blame] | 1271 | |
Chris Lattner | 7a60d91 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1272 | DEBUG(std::cerr << "Lowered selection DAG:\n"); |
| 1273 | DEBUG(DAG.dump()); |
| 1274 | |
| 1275 | // Second step, hack on the DAG until it only uses operations and types that |
| 1276 | // the target supports. |
Chris Lattner | ffcb0ae | 2005-01-23 04:36:26 +0000 | [diff] [blame] | 1277 | DAG.Legalize(); |
Chris Lattner | 7a60d91 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1278 | |
| 1279 | DEBUG(std::cerr << "Legalized selection DAG:\n"); |
| 1280 | DEBUG(DAG.dump()); |
| 1281 | |
Chris Lattner | bcfebeb | 2005-10-10 16:47:10 +0000 | [diff] [blame] | 1282 | // Run the DAG combiner in post-legalize mode. |
| 1283 | DAG.Combine(true); |
Nate Begeman | 007c650 | 2005-09-07 00:15:36 +0000 | [diff] [blame] | 1284 | |
Chris Lattner | 6bd8fd0 | 2005-10-05 06:09:10 +0000 | [diff] [blame] | 1285 | if (ViewDAGs) DAG.viewGraph(); |
| 1286 | |
Chris Lattner | 5ca31d9 | 2005-03-30 01:10:47 +0000 | [diff] [blame] | 1287 | // Third, instruction select all of the operations to machine code, adding the |
| 1288 | // code to the MachineBasicBlock. |
Chris Lattner | 7a60d91 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1289 | InstructionSelectBasicBlock(DAG); |
| 1290 | |
Chris Lattner | 7a60d91 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1291 | DEBUG(std::cerr << "Selected machine code:\n"); |
| 1292 | DEBUG(BB->dump()); |
| 1293 | |
Chris Lattner | 5ca31d9 | 2005-03-30 01:10:47 +0000 | [diff] [blame] | 1294 | // Next, now that we know what the last MBB the LLVM BB expanded is, update |
Chris Lattner | 7a60d91 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1295 | // PHI nodes in successors. |
| 1296 | for (unsigned i = 0, e = PHINodesToUpdate.size(); i != e; ++i) { |
| 1297 | MachineInstr *PHI = PHINodesToUpdate[i].first; |
| 1298 | assert(PHI->getOpcode() == TargetInstrInfo::PHI && |
| 1299 | "This is not a machine PHI node that we are updating!"); |
| 1300 | PHI->addRegOperand(PHINodesToUpdate[i].second); |
| 1301 | PHI->addMachineBasicBlockOperand(BB); |
| 1302 | } |
Chris Lattner | 5ca31d9 | 2005-03-30 01:10:47 +0000 | [diff] [blame] | 1303 | |
| 1304 | // Finally, add the CFG edges from the last selected MBB to the successor |
| 1305 | // MBBs. |
| 1306 | TerminatorInst *TI = LLVMBB->getTerminator(); |
| 1307 | for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i) { |
| 1308 | MachineBasicBlock *Succ0MBB = FuncInfo.MBBMap[TI->getSuccessor(i)]; |
| 1309 | BB->addSuccessor(Succ0MBB); |
| 1310 | } |
Chris Lattner | 7a60d91 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1311 | } |