Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1 | //===-- SelectionDAGISel.cpp - Implement the SelectionDAGISel class -------===// |
| 2 | // |
| 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. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This implements the SelectionDAGISel class. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #define DEBUG_TYPE "isel" |
| 15 | #include "llvm/CodeGen/SelectionDAGISel.h" |
| 16 | #include "llvm/Constants.h" |
| 17 | #include "llvm/DerivedTypes.h" |
| 18 | #include "llvm/Function.h" |
| 19 | #include "llvm/Instructions.h" |
| 20 | #include "llvm/Intrinsics.h" |
| 21 | #include "llvm/CodeGen/MachineFunction.h" |
| 22 | #include "llvm/CodeGen/MachineFrameInfo.h" |
| 23 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
| 24 | #include "llvm/CodeGen/SelectionDAG.h" |
| 25 | #include "llvm/CodeGen/SSARegMap.h" |
| 26 | #include "llvm/Target/TargetData.h" |
| 27 | #include "llvm/Target/TargetFrameInfo.h" |
| 28 | #include "llvm/Target/TargetInstrInfo.h" |
| 29 | #include "llvm/Target/TargetLowering.h" |
| 30 | #include "llvm/Target/TargetMachine.h" |
Chris Lattner | 7944d9d | 2005-01-12 03:41:21 +0000 | [diff] [blame] | 31 | #include "llvm/Support/CommandLine.h" |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 32 | #include "llvm/Support/Debug.h" |
| 33 | #include <map> |
| 34 | #include <iostream> |
| 35 | using namespace llvm; |
| 36 | |
Chris Lattner | 7944d9d | 2005-01-12 03:41:21 +0000 | [diff] [blame] | 37 | #ifndef _NDEBUG |
| 38 | static cl::opt<bool> |
| 39 | ViewDAGs("view-isel-dags", cl::Hidden, |
| 40 | cl::desc("Pop up a window to show isel dags as they are selected")); |
| 41 | #else |
| 42 | static const bool ViewDAGS = 0; |
| 43 | #endif |
| 44 | |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 45 | namespace llvm { |
| 46 | //===--------------------------------------------------------------------===// |
| 47 | /// FunctionLoweringInfo - This contains information that is global to a |
| 48 | /// function that is used when lowering a region of the function. |
Chris Lattner | f26bc8e | 2005-01-08 19:52:31 +0000 | [diff] [blame] | 49 | class FunctionLoweringInfo { |
| 50 | public: |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 51 | TargetLowering &TLI; |
| 52 | Function &Fn; |
| 53 | MachineFunction &MF; |
| 54 | SSARegMap *RegMap; |
| 55 | |
| 56 | FunctionLoweringInfo(TargetLowering &TLI, Function &Fn,MachineFunction &MF); |
| 57 | |
| 58 | /// MBBMap - A mapping from LLVM basic blocks to their machine code entry. |
| 59 | std::map<const BasicBlock*, MachineBasicBlock *> MBBMap; |
| 60 | |
| 61 | /// ValueMap - Since we emit code for the function a basic block at a time, |
| 62 | /// we must remember which virtual registers hold the values for |
| 63 | /// cross-basic-block values. |
| 64 | std::map<const Value*, unsigned> ValueMap; |
| 65 | |
| 66 | /// StaticAllocaMap - Keep track of frame indices for fixed sized allocas in |
| 67 | /// the entry block. This allows the allocas to be efficiently referenced |
| 68 | /// anywhere in the function. |
| 69 | std::map<const AllocaInst*, int> StaticAllocaMap; |
| 70 | |
Chris Lattner | 0afa8e3 | 2005-01-17 17:55:19 +0000 | [diff] [blame] | 71 | /// BlockLocalArguments - If any arguments are only used in a single basic |
| 72 | /// block, and if the target can access the arguments without side-effects, |
| 73 | /// avoid emitting CopyToReg nodes for those arguments. This map keeps |
| 74 | /// track of which arguments are local to each BB. |
| 75 | std::multimap<BasicBlock*, std::pair<Argument*, |
| 76 | unsigned> > BlockLocalArguments; |
| 77 | |
| 78 | |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 79 | unsigned MakeReg(MVT::ValueType VT) { |
| 80 | return RegMap->createVirtualRegister(TLI.getRegClassFor(VT)); |
| 81 | } |
| 82 | |
| 83 | unsigned CreateRegForValue(const Value *V) { |
| 84 | MVT::ValueType VT = TLI.getValueType(V->getType()); |
| 85 | // The common case is that we will only create one register for this |
| 86 | // value. If we have that case, create and return the virtual register. |
| 87 | unsigned NV = TLI.getNumElements(VT); |
Chris Lattner | fb84980 | 2005-01-16 00:37:38 +0000 | [diff] [blame] | 88 | if (NV == 1) { |
| 89 | // If we are promoting this value, pick the next largest supported type. |
Chris Lattner | 98e5c0e | 2005-01-16 01:11:19 +0000 | [diff] [blame] | 90 | return MakeReg(TLI.getTypeToTransformTo(VT)); |
Chris Lattner | fb84980 | 2005-01-16 00:37:38 +0000 | [diff] [blame] | 91 | } |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 92 | |
| 93 | // If this value is represented with multiple target registers, make sure |
| 94 | // to create enough consequtive registers of the right (smaller) type. |
| 95 | unsigned NT = VT-1; // Find the type to use. |
| 96 | while (TLI.getNumElements((MVT::ValueType)NT) != 1) |
| 97 | --NT; |
| 98 | |
| 99 | unsigned R = MakeReg((MVT::ValueType)NT); |
| 100 | for (unsigned i = 1; i != NV; ++i) |
| 101 | MakeReg((MVT::ValueType)NT); |
| 102 | return R; |
| 103 | } |
| 104 | |
| 105 | unsigned InitializeRegForValue(const Value *V) { |
| 106 | unsigned &R = ValueMap[V]; |
| 107 | assert(R == 0 && "Already initialized this value register!"); |
| 108 | return R = CreateRegForValue(V); |
| 109 | } |
| 110 | }; |
| 111 | } |
| 112 | |
| 113 | /// isUsedOutsideOfDefiningBlock - Return true if this instruction is used by |
| 114 | /// PHI nodes or outside of the basic block that defines it. |
| 115 | static bool isUsedOutsideOfDefiningBlock(Instruction *I) { |
| 116 | if (isa<PHINode>(I)) return true; |
| 117 | BasicBlock *BB = I->getParent(); |
| 118 | for (Value::use_iterator UI = I->use_begin(), E = I->use_end(); UI != E; ++UI) |
| 119 | if (cast<Instruction>(*UI)->getParent() != BB || isa<PHINode>(*UI)) |
| 120 | return true; |
| 121 | return false; |
| 122 | } |
| 123 | |
| 124 | FunctionLoweringInfo::FunctionLoweringInfo(TargetLowering &tli, |
| 125 | Function &fn, MachineFunction &mf) |
| 126 | : TLI(tli), Fn(fn), MF(mf), RegMap(MF.getSSARegMap()) { |
| 127 | |
| 128 | // Initialize the mapping of values to registers. This is only set up for |
| 129 | // instruction values that are used outside of the block that defines |
| 130 | // them. |
Chris Lattner | e4d5c44 | 2005-03-15 04:54:21 +0000 | [diff] [blame] | 131 | for (Function::arg_iterator AI = Fn.arg_begin(), E = Fn.arg_end(); AI != E; ++AI) |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 132 | InitializeRegForValue(AI); |
| 133 | |
| 134 | Function::iterator BB = Fn.begin(), E = Fn.end(); |
| 135 | for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) |
| 136 | if (AllocaInst *AI = dyn_cast<AllocaInst>(I)) |
| 137 | if (ConstantUInt *CUI = dyn_cast<ConstantUInt>(AI->getArraySize())) { |
| 138 | const Type *Ty = AI->getAllocatedType(); |
| 139 | uint64_t TySize = TLI.getTargetData().getTypeSize(Ty); |
| 140 | unsigned Align = TLI.getTargetData().getTypeAlignment(Ty); |
| 141 | TySize *= CUI->getValue(); // Get total allocated size. |
| 142 | StaticAllocaMap[AI] = |
Chris Lattner | f26bc8e | 2005-01-08 19:52:31 +0000 | [diff] [blame] | 143 | MF.getFrameInfo()->CreateStackObject((unsigned)TySize, Align); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | for (; BB != E; ++BB) |
Chris Lattner | f26bc8e | 2005-01-08 19:52:31 +0000 | [diff] [blame] | 147 | for (BasicBlock::iterator I = BB->begin(), e = BB->end(); I != e; ++I) |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 148 | if (!I->use_empty() && isUsedOutsideOfDefiningBlock(I)) |
| 149 | if (!isa<AllocaInst>(I) || |
| 150 | !StaticAllocaMap.count(cast<AllocaInst>(I))) |
| 151 | InitializeRegForValue(I); |
| 152 | |
| 153 | // Create an initial MachineBasicBlock for each LLVM BasicBlock in F. This |
| 154 | // also creates the initial PHI MachineInstrs, though none of the input |
| 155 | // operands are populated. |
| 156 | for (Function::iterator BB = Fn.begin(), E = Fn.end(); BB != E; ++BB) { |
| 157 | MachineBasicBlock *MBB = new MachineBasicBlock(BB); |
| 158 | MBBMap[BB] = MBB; |
| 159 | MF.getBasicBlockList().push_back(MBB); |
| 160 | |
| 161 | // Create Machine PHI nodes for LLVM PHI nodes, lowering them as |
| 162 | // appropriate. |
| 163 | PHINode *PN; |
| 164 | for (BasicBlock::iterator I = BB->begin(); |
Chris Lattner | f44fd88 | 2005-01-07 21:34:19 +0000 | [diff] [blame] | 165 | (PN = dyn_cast<PHINode>(I)); ++I) |
| 166 | if (!PN->use_empty()) { |
| 167 | unsigned NumElements = |
| 168 | TLI.getNumElements(TLI.getValueType(PN->getType())); |
| 169 | unsigned PHIReg = ValueMap[PN]; |
| 170 | assert(PHIReg &&"PHI node does not have an assigned virtual register!"); |
| 171 | for (unsigned i = 0; i != NumElements; ++i) |
| 172 | BuildMI(MBB, TargetInstrInfo::PHI, PN->getNumOperands(), PHIReg+i); |
| 173 | } |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 174 | } |
| 175 | } |
| 176 | |
| 177 | |
| 178 | |
| 179 | //===----------------------------------------------------------------------===// |
| 180 | /// SelectionDAGLowering - This is the common target-independent lowering |
| 181 | /// implementation that is parameterized by a TargetLowering object. |
| 182 | /// Also, targets can overload any lowering method. |
| 183 | /// |
| 184 | namespace llvm { |
| 185 | class SelectionDAGLowering { |
| 186 | MachineBasicBlock *CurMBB; |
| 187 | |
| 188 | std::map<const Value*, SDOperand> NodeMap; |
| 189 | |
Chris Lattner | d394811 | 2005-01-17 22:19:26 +0000 | [diff] [blame] | 190 | /// PendingLoads - Loads are not emitted to the program immediately. We bunch |
| 191 | /// them up and then emit token factor nodes when possible. This allows us to |
| 192 | /// get simple disambiguation between loads without worrying about alias |
| 193 | /// analysis. |
| 194 | std::vector<SDOperand> PendingLoads; |
| 195 | |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 196 | public: |
| 197 | // TLI - This is information that describes the available target features we |
| 198 | // need for lowering. This indicates when operations are unavailable, |
| 199 | // implemented with a libcall, etc. |
| 200 | TargetLowering &TLI; |
| 201 | SelectionDAG &DAG; |
| 202 | const TargetData &TD; |
| 203 | |
| 204 | /// FuncInfo - Information about the function as a whole. |
| 205 | /// |
| 206 | FunctionLoweringInfo &FuncInfo; |
| 207 | |
| 208 | SelectionDAGLowering(SelectionDAG &dag, TargetLowering &tli, |
| 209 | FunctionLoweringInfo &funcinfo) |
| 210 | : TLI(tli), DAG(dag), TD(DAG.getTarget().getTargetData()), |
| 211 | FuncInfo(funcinfo) { |
| 212 | } |
| 213 | |
Chris Lattner | a651cf6 | 2005-01-17 19:43:36 +0000 | [diff] [blame] | 214 | /// getRoot - Return the current virtual root of the Selection DAG. |
| 215 | /// |
| 216 | SDOperand getRoot() { |
Chris Lattner | d394811 | 2005-01-17 22:19:26 +0000 | [diff] [blame] | 217 | if (PendingLoads.empty()) |
| 218 | return DAG.getRoot(); |
| 219 | |
| 220 | if (PendingLoads.size() == 1) { |
| 221 | SDOperand Root = PendingLoads[0]; |
| 222 | DAG.setRoot(Root); |
| 223 | PendingLoads.clear(); |
| 224 | return Root; |
| 225 | } |
| 226 | |
| 227 | // Otherwise, we have to make a token factor node. |
| 228 | SDOperand Root = DAG.getNode(ISD::TokenFactor, MVT::Other, PendingLoads); |
| 229 | PendingLoads.clear(); |
| 230 | DAG.setRoot(Root); |
| 231 | return Root; |
Chris Lattner | a651cf6 | 2005-01-17 19:43:36 +0000 | [diff] [blame] | 232 | } |
| 233 | |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 234 | void visit(Instruction &I) { visit(I.getOpcode(), I); } |
| 235 | |
| 236 | void visit(unsigned Opcode, User &I) { |
| 237 | switch (Opcode) { |
| 238 | default: assert(0 && "Unknown instruction type encountered!"); |
| 239 | abort(); |
| 240 | // Build the switch statement using the Instruction.def file. |
| 241 | #define HANDLE_INST(NUM, OPCODE, CLASS) \ |
| 242 | case Instruction::OPCODE:return visit##OPCODE((CLASS&)I); |
| 243 | #include "llvm/Instruction.def" |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | void setCurrentBasicBlock(MachineBasicBlock *MBB) { CurMBB = MBB; } |
| 248 | |
| 249 | |
| 250 | SDOperand getIntPtrConstant(uint64_t Val) { |
| 251 | return DAG.getConstant(Val, TLI.getPointerTy()); |
| 252 | } |
| 253 | |
| 254 | SDOperand getValue(const Value *V) { |
| 255 | SDOperand &N = NodeMap[V]; |
| 256 | if (N.Val) return N; |
| 257 | |
| 258 | MVT::ValueType VT = TLI.getValueType(V->getType()); |
| 259 | if (Constant *C = const_cast<Constant*>(dyn_cast<Constant>(V))) |
| 260 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) { |
| 261 | visit(CE->getOpcode(), *CE); |
| 262 | assert(N.Val && "visit didn't populate the ValueMap!"); |
| 263 | return N; |
| 264 | } else if (GlobalValue *GV = dyn_cast<GlobalValue>(C)) { |
| 265 | return N = DAG.getGlobalAddress(GV, VT); |
| 266 | } else if (isa<ConstantPointerNull>(C)) { |
| 267 | return N = DAG.getConstant(0, TLI.getPointerTy()); |
| 268 | } else if (isa<UndefValue>(C)) { |
| 269 | /// FIXME: Implement UNDEFVALUE better. |
| 270 | if (MVT::isInteger(VT)) |
| 271 | return N = DAG.getConstant(0, VT); |
| 272 | else if (MVT::isFloatingPoint(VT)) |
| 273 | return N = DAG.getConstantFP(0, VT); |
| 274 | else |
| 275 | assert(0 && "Unknown value type!"); |
| 276 | |
| 277 | } else if (ConstantFP *CFP = dyn_cast<ConstantFP>(C)) { |
| 278 | return N = DAG.getConstantFP(CFP->getValue(), VT); |
| 279 | } else { |
| 280 | // Canonicalize all constant ints to be unsigned. |
| 281 | return N = DAG.getConstant(cast<ConstantIntegral>(C)->getRawValue(),VT); |
| 282 | } |
| 283 | |
| 284 | if (const AllocaInst *AI = dyn_cast<AllocaInst>(V)) { |
| 285 | std::map<const AllocaInst*, int>::iterator SI = |
| 286 | FuncInfo.StaticAllocaMap.find(AI); |
| 287 | if (SI != FuncInfo.StaticAllocaMap.end()) |
| 288 | return DAG.getFrameIndex(SI->second, TLI.getPointerTy()); |
| 289 | } |
| 290 | |
| 291 | std::map<const Value*, unsigned>::const_iterator VMI = |
| 292 | FuncInfo.ValueMap.find(V); |
| 293 | assert(VMI != FuncInfo.ValueMap.end() && "Value not in map!"); |
Chris Lattner | c8ea3c4 | 2005-01-16 02:23:07 +0000 | [diff] [blame] | 294 | |
Chris Lattner | ef5cd1d | 2005-01-18 17:54:55 +0000 | [diff] [blame] | 295 | return N = DAG.getCopyFromReg(VMI->second, VT, DAG.getEntryNode()); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | const SDOperand &setValue(const Value *V, SDOperand NewN) { |
| 299 | SDOperand &N = NodeMap[V]; |
| 300 | assert(N.Val == 0 && "Already set a value for this node!"); |
| 301 | return N = NewN; |
| 302 | } |
| 303 | |
| 304 | // Terminator instructions. |
| 305 | void visitRet(ReturnInst &I); |
| 306 | void visitBr(BranchInst &I); |
| 307 | void visitUnreachable(UnreachableInst &I) { /* noop */ } |
| 308 | |
| 309 | // These all get lowered before this pass. |
| 310 | void visitSwitch(SwitchInst &I) { assert(0 && "TODO"); } |
| 311 | void visitInvoke(InvokeInst &I) { assert(0 && "TODO"); } |
| 312 | void visitUnwind(UnwindInst &I) { assert(0 && "TODO"); } |
| 313 | |
| 314 | // |
| 315 | void visitBinary(User &I, unsigned Opcode); |
| 316 | void visitAdd(User &I) { visitBinary(I, ISD::ADD); } |
| 317 | void visitSub(User &I) { visitBinary(I, ISD::SUB); } |
| 318 | void visitMul(User &I) { visitBinary(I, ISD::MUL); } |
| 319 | void visitDiv(User &I) { |
| 320 | visitBinary(I, I.getType()->isUnsigned() ? ISD::UDIV : ISD::SDIV); |
| 321 | } |
| 322 | void visitRem(User &I) { |
| 323 | visitBinary(I, I.getType()->isUnsigned() ? ISD::UREM : ISD::SREM); |
| 324 | } |
| 325 | void visitAnd(User &I) { visitBinary(I, ISD::AND); } |
| 326 | void visitOr (User &I) { visitBinary(I, ISD::OR); } |
| 327 | void visitXor(User &I) { visitBinary(I, ISD::XOR); } |
| 328 | void visitShl(User &I) { visitBinary(I, ISD::SHL); } |
| 329 | void visitShr(User &I) { |
| 330 | visitBinary(I, I.getType()->isUnsigned() ? ISD::SRL : ISD::SRA); |
| 331 | } |
| 332 | |
| 333 | void visitSetCC(User &I, ISD::CondCode SignedOpc, ISD::CondCode UnsignedOpc); |
| 334 | void visitSetEQ(User &I) { visitSetCC(I, ISD::SETEQ, ISD::SETEQ); } |
| 335 | void visitSetNE(User &I) { visitSetCC(I, ISD::SETNE, ISD::SETNE); } |
| 336 | void visitSetLE(User &I) { visitSetCC(I, ISD::SETLE, ISD::SETULE); } |
| 337 | void visitSetGE(User &I) { visitSetCC(I, ISD::SETGE, ISD::SETUGE); } |
| 338 | void visitSetLT(User &I) { visitSetCC(I, ISD::SETLT, ISD::SETULT); } |
| 339 | void visitSetGT(User &I) { visitSetCC(I, ISD::SETGT, ISD::SETUGT); } |
| 340 | |
| 341 | void visitGetElementPtr(User &I); |
| 342 | void visitCast(User &I); |
| 343 | void visitSelect(User &I); |
| 344 | // |
| 345 | |
| 346 | void visitMalloc(MallocInst &I); |
| 347 | void visitFree(FreeInst &I); |
| 348 | void visitAlloca(AllocaInst &I); |
| 349 | void visitLoad(LoadInst &I); |
| 350 | void visitStore(StoreInst &I); |
| 351 | void visitPHI(PHINode &I) { } // PHI nodes are handled specially. |
| 352 | void visitCall(CallInst &I); |
| 353 | |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 354 | void visitVAStart(CallInst &I); |
| 355 | void visitVANext(VANextInst &I); |
| 356 | void visitVAArg(VAArgInst &I); |
| 357 | void visitVAEnd(CallInst &I); |
| 358 | void visitVACopy(CallInst &I); |
Chris Lattner | 39ae362 | 2005-01-09 00:00:49 +0000 | [diff] [blame] | 359 | void visitFrameReturnAddress(CallInst &I, bool isFrameAddress); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 360 | |
Chris Lattner | 7041ee3 | 2005-01-11 05:56:49 +0000 | [diff] [blame] | 361 | void visitMemIntrinsic(CallInst &I, unsigned Op); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 362 | |
| 363 | void visitUserOp1(Instruction &I) { |
| 364 | assert(0 && "UserOp1 should not exist at instruction selection time!"); |
| 365 | abort(); |
| 366 | } |
| 367 | void visitUserOp2(Instruction &I) { |
| 368 | assert(0 && "UserOp2 should not exist at instruction selection time!"); |
| 369 | abort(); |
| 370 | } |
| 371 | }; |
| 372 | } // end namespace llvm |
| 373 | |
| 374 | void SelectionDAGLowering::visitRet(ReturnInst &I) { |
| 375 | if (I.getNumOperands() == 0) { |
Chris Lattner | a651cf6 | 2005-01-17 19:43:36 +0000 | [diff] [blame] | 376 | DAG.setRoot(DAG.getNode(ISD::RET, MVT::Other, getRoot())); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 377 | return; |
| 378 | } |
| 379 | |
| 380 | SDOperand Op1 = getValue(I.getOperand(0)); |
Chris Lattner | f51d3bd | 2005-03-29 19:09:56 +0000 | [diff] [blame^] | 381 | MVT::ValueType TmpVT; |
| 382 | |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 383 | switch (Op1.getValueType()) { |
| 384 | default: assert(0 && "Unknown value type!"); |
| 385 | case MVT::i1: |
| 386 | case MVT::i8: |
| 387 | case MVT::i16: |
Chris Lattner | f51d3bd | 2005-03-29 19:09:56 +0000 | [diff] [blame^] | 388 | case MVT::i32: |
| 389 | // If this is a machine where 32-bits is legal or expanded, promote to |
| 390 | // 32-bits, otherwise, promote to 64-bits. |
| 391 | if (TLI.getTypeAction(MVT::i32) == TargetLowering::Promote) |
| 392 | TmpVT = TLI.getTypeToTransformTo(MVT::i32); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 393 | else |
Chris Lattner | f51d3bd | 2005-03-29 19:09:56 +0000 | [diff] [blame^] | 394 | TmpVT = MVT::i32; |
| 395 | |
| 396 | // Extend integer types to result type. |
| 397 | if (I.getOperand(0)->getType()->isSigned()) |
| 398 | Op1 = DAG.getNode(ISD::SIGN_EXTEND, TmpVT, Op1); |
| 399 | else |
| 400 | Op1 = DAG.getNode(ISD::ZERO_EXTEND, TmpVT, Op1); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 401 | break; |
| 402 | case MVT::f32: |
| 403 | // Extend float to double. |
| 404 | Op1 = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Op1); |
| 405 | break; |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 406 | case MVT::i64: |
| 407 | case MVT::f64: |
| 408 | break; // No extension needed! |
| 409 | } |
| 410 | |
Chris Lattner | a651cf6 | 2005-01-17 19:43:36 +0000 | [diff] [blame] | 411 | DAG.setRoot(DAG.getNode(ISD::RET, MVT::Other, getRoot(), Op1)); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 412 | } |
| 413 | |
| 414 | void SelectionDAGLowering::visitBr(BranchInst &I) { |
| 415 | // Update machine-CFG edges. |
| 416 | MachineBasicBlock *Succ0MBB = FuncInfo.MBBMap[I.getSuccessor(0)]; |
| 417 | CurMBB->addSuccessor(Succ0MBB); |
| 418 | |
| 419 | // Figure out which block is immediately after the current one. |
| 420 | MachineBasicBlock *NextBlock = 0; |
| 421 | MachineFunction::iterator BBI = CurMBB; |
| 422 | if (++BBI != CurMBB->getParent()->end()) |
| 423 | NextBlock = BBI; |
| 424 | |
| 425 | if (I.isUnconditional()) { |
| 426 | // If this is not a fall-through branch, emit the branch. |
| 427 | if (Succ0MBB != NextBlock) |
Chris Lattner | a651cf6 | 2005-01-17 19:43:36 +0000 | [diff] [blame] | 428 | DAG.setRoot(DAG.getNode(ISD::BR, MVT::Other, getRoot(), |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 429 | DAG.getBasicBlock(Succ0MBB))); |
| 430 | } else { |
| 431 | MachineBasicBlock *Succ1MBB = FuncInfo.MBBMap[I.getSuccessor(1)]; |
| 432 | CurMBB->addSuccessor(Succ1MBB); |
| 433 | |
| 434 | SDOperand Cond = getValue(I.getCondition()); |
| 435 | |
| 436 | if (Succ1MBB == NextBlock) { |
| 437 | // If the condition is false, fall through. This means we should branch |
| 438 | // if the condition is true to Succ #0. |
Chris Lattner | a651cf6 | 2005-01-17 19:43:36 +0000 | [diff] [blame] | 439 | DAG.setRoot(DAG.getNode(ISD::BRCOND, MVT::Other, getRoot(), |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 440 | Cond, DAG.getBasicBlock(Succ0MBB))); |
| 441 | } else if (Succ0MBB == NextBlock) { |
| 442 | // If the condition is true, fall through. This means we should branch if |
| 443 | // the condition is false to Succ #1. Invert the condition first. |
| 444 | SDOperand True = DAG.getConstant(1, Cond.getValueType()); |
| 445 | Cond = DAG.getNode(ISD::XOR, Cond.getValueType(), Cond, True); |
Chris Lattner | a651cf6 | 2005-01-17 19:43:36 +0000 | [diff] [blame] | 446 | DAG.setRoot(DAG.getNode(ISD::BRCOND, MVT::Other, getRoot(), |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 447 | Cond, DAG.getBasicBlock(Succ1MBB))); |
| 448 | } else { |
| 449 | // Neither edge is a fall through. If the comparison is true, jump to |
| 450 | // Succ#0, otherwise branch unconditionally to succ #1. |
Chris Lattner | a651cf6 | 2005-01-17 19:43:36 +0000 | [diff] [blame] | 451 | DAG.setRoot(DAG.getNode(ISD::BRCOND, MVT::Other, getRoot(), |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 452 | Cond, DAG.getBasicBlock(Succ0MBB))); |
Chris Lattner | a651cf6 | 2005-01-17 19:43:36 +0000 | [diff] [blame] | 453 | DAG.setRoot(DAG.getNode(ISD::BR, MVT::Other, getRoot(), |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 454 | DAG.getBasicBlock(Succ1MBB))); |
| 455 | } |
| 456 | } |
| 457 | } |
| 458 | |
| 459 | void SelectionDAGLowering::visitBinary(User &I, unsigned Opcode) { |
| 460 | SDOperand Op1 = getValue(I.getOperand(0)); |
| 461 | SDOperand Op2 = getValue(I.getOperand(1)); |
Chris Lattner | 2c49f27 | 2005-01-19 22:31:21 +0000 | [diff] [blame] | 462 | |
| 463 | if (isa<ShiftInst>(I)) |
| 464 | Op2 = DAG.getNode(ISD::ZERO_EXTEND, TLI.getShiftAmountTy(), Op2); |
| 465 | |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 466 | setValue(&I, DAG.getNode(Opcode, Op1.getValueType(), Op1, Op2)); |
| 467 | } |
| 468 | |
| 469 | void SelectionDAGLowering::visitSetCC(User &I,ISD::CondCode SignedOpcode, |
| 470 | ISD::CondCode UnsignedOpcode) { |
| 471 | SDOperand Op1 = getValue(I.getOperand(0)); |
| 472 | SDOperand Op2 = getValue(I.getOperand(1)); |
| 473 | ISD::CondCode Opcode = SignedOpcode; |
| 474 | if (I.getOperand(0)->getType()->isUnsigned()) |
| 475 | Opcode = UnsignedOpcode; |
Chris Lattner | f30b73b | 2005-01-18 02:52:03 +0000 | [diff] [blame] | 476 | setValue(&I, DAG.getSetCC(Opcode, MVT::i1, Op1, Op2)); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 477 | } |
| 478 | |
| 479 | void SelectionDAGLowering::visitSelect(User &I) { |
| 480 | SDOperand Cond = getValue(I.getOperand(0)); |
| 481 | SDOperand TrueVal = getValue(I.getOperand(1)); |
| 482 | SDOperand FalseVal = getValue(I.getOperand(2)); |
| 483 | setValue(&I, DAG.getNode(ISD::SELECT, TrueVal.getValueType(), Cond, |
| 484 | TrueVal, FalseVal)); |
| 485 | } |
| 486 | |
| 487 | void SelectionDAGLowering::visitCast(User &I) { |
| 488 | SDOperand N = getValue(I.getOperand(0)); |
| 489 | MVT::ValueType SrcTy = TLI.getValueType(I.getOperand(0)->getType()); |
| 490 | MVT::ValueType DestTy = TLI.getValueType(I.getType()); |
| 491 | |
| 492 | if (N.getValueType() == DestTy) { |
| 493 | setValue(&I, N); // noop cast. |
Chris Lattner | ae0aacb | 2005-01-08 08:08:56 +0000 | [diff] [blame] | 494 | } else if (isInteger(SrcTy)) { |
| 495 | if (isInteger(DestTy)) { // Int -> Int cast |
| 496 | if (DestTy < SrcTy) // Truncating cast? |
| 497 | setValue(&I, DAG.getNode(ISD::TRUNCATE, DestTy, N)); |
| 498 | else if (I.getOperand(0)->getType()->isSigned()) |
| 499 | setValue(&I, DAG.getNode(ISD::SIGN_EXTEND, DestTy, N)); |
| 500 | else |
| 501 | setValue(&I, DAG.getNode(ISD::ZERO_EXTEND, DestTy, N)); |
| 502 | } else { // Int -> FP cast |
| 503 | if (I.getOperand(0)->getType()->isSigned()) |
| 504 | setValue(&I, DAG.getNode(ISD::SINT_TO_FP, DestTy, N)); |
| 505 | else |
| 506 | setValue(&I, DAG.getNode(ISD::UINT_TO_FP, DestTy, N)); |
| 507 | } |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 508 | } else { |
Chris Lattner | ae0aacb | 2005-01-08 08:08:56 +0000 | [diff] [blame] | 509 | assert(isFloatingPoint(SrcTy) && "Unknown value type!"); |
| 510 | if (isFloatingPoint(DestTy)) { // FP -> FP cast |
| 511 | if (DestTy < SrcTy) // Rounding cast? |
| 512 | setValue(&I, DAG.getNode(ISD::FP_ROUND, DestTy, N)); |
| 513 | else |
| 514 | setValue(&I, DAG.getNode(ISD::FP_EXTEND, DestTy, N)); |
| 515 | } else { // FP -> Int cast. |
| 516 | if (I.getType()->isSigned()) |
| 517 | setValue(&I, DAG.getNode(ISD::FP_TO_SINT, DestTy, N)); |
| 518 | else |
| 519 | setValue(&I, DAG.getNode(ISD::FP_TO_UINT, DestTy, N)); |
| 520 | } |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 521 | } |
| 522 | } |
| 523 | |
| 524 | void SelectionDAGLowering::visitGetElementPtr(User &I) { |
| 525 | SDOperand N = getValue(I.getOperand(0)); |
| 526 | const Type *Ty = I.getOperand(0)->getType(); |
| 527 | const Type *UIntPtrTy = TD.getIntPtrType(); |
| 528 | |
| 529 | for (GetElementPtrInst::op_iterator OI = I.op_begin()+1, E = I.op_end(); |
| 530 | OI != E; ++OI) { |
| 531 | Value *Idx = *OI; |
| 532 | if (const StructType *StTy = dyn_cast<StructType> (Ty)) { |
| 533 | unsigned Field = cast<ConstantUInt>(Idx)->getValue(); |
| 534 | if (Field) { |
| 535 | // N = N + Offset |
| 536 | uint64_t Offset = TD.getStructLayout(StTy)->MemberOffsets[Field]; |
| 537 | N = DAG.getNode(ISD::ADD, N.getValueType(), N, |
| 538 | getIntPtrConstant(Offset)); |
| 539 | } |
| 540 | Ty = StTy->getElementType(Field); |
| 541 | } else { |
| 542 | Ty = cast<SequentialType>(Ty)->getElementType(); |
| 543 | if (!isa<Constant>(Idx) || !cast<Constant>(Idx)->isNullValue()) { |
| 544 | // N = N + Idx * ElementSize; |
| 545 | uint64_t ElementSize = TD.getTypeSize(Ty); |
Chris Lattner | 7cc4777 | 2005-01-07 21:56:57 +0000 | [diff] [blame] | 546 | SDOperand IdxN = getValue(Idx), Scale = getIntPtrConstant(ElementSize); |
| 547 | |
| 548 | // If the index is smaller or larger than intptr_t, truncate or extend |
| 549 | // it. |
| 550 | if (IdxN.getValueType() < Scale.getValueType()) { |
| 551 | if (Idx->getType()->isSigned()) |
| 552 | IdxN = DAG.getNode(ISD::SIGN_EXTEND, Scale.getValueType(), IdxN); |
| 553 | else |
| 554 | IdxN = DAG.getNode(ISD::ZERO_EXTEND, Scale.getValueType(), IdxN); |
| 555 | } else if (IdxN.getValueType() > Scale.getValueType()) |
| 556 | IdxN = DAG.getNode(ISD::TRUNCATE, Scale.getValueType(), IdxN); |
| 557 | |
| 558 | IdxN = DAG.getNode(ISD::MUL, N.getValueType(), IdxN, Scale); |
| 559 | |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 560 | N = DAG.getNode(ISD::ADD, N.getValueType(), N, IdxN); |
| 561 | } |
| 562 | } |
| 563 | } |
| 564 | setValue(&I, N); |
| 565 | } |
| 566 | |
| 567 | void SelectionDAGLowering::visitAlloca(AllocaInst &I) { |
| 568 | // If this is a fixed sized alloca in the entry block of the function, |
| 569 | // allocate it statically on the stack. |
| 570 | if (FuncInfo.StaticAllocaMap.count(&I)) |
| 571 | return; // getValue will auto-populate this. |
| 572 | |
| 573 | const Type *Ty = I.getAllocatedType(); |
| 574 | uint64_t TySize = TLI.getTargetData().getTypeSize(Ty); |
| 575 | unsigned Align = TLI.getTargetData().getTypeAlignment(Ty); |
| 576 | |
| 577 | SDOperand AllocSize = getValue(I.getArraySize()); |
Chris Lattner | 68cd65e | 2005-01-22 23:04:37 +0000 | [diff] [blame] | 578 | MVT::ValueType IntPtr = TLI.getPointerTy(); |
| 579 | if (IntPtr < AllocSize.getValueType()) |
| 580 | AllocSize = DAG.getNode(ISD::TRUNCATE, IntPtr, AllocSize); |
| 581 | else if (IntPtr > AllocSize.getValueType()) |
| 582 | AllocSize = DAG.getNode(ISD::ZERO_EXTEND, IntPtr, AllocSize); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 583 | |
Chris Lattner | 68cd65e | 2005-01-22 23:04:37 +0000 | [diff] [blame] | 584 | AllocSize = DAG.getNode(ISD::MUL, IntPtr, AllocSize, |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 585 | getIntPtrConstant(TySize)); |
| 586 | |
| 587 | // Handle alignment. If the requested alignment is less than or equal to the |
| 588 | // stack alignment, ignore it and round the size of the allocation up to the |
| 589 | // stack alignment size. If the size is greater than the stack alignment, we |
| 590 | // note this in the DYNAMIC_STACKALLOC node. |
| 591 | unsigned StackAlign = |
| 592 | TLI.getTargetMachine().getFrameInfo()->getStackAlignment(); |
| 593 | if (Align <= StackAlign) { |
| 594 | Align = 0; |
| 595 | // Add SA-1 to the size. |
| 596 | AllocSize = DAG.getNode(ISD::ADD, AllocSize.getValueType(), AllocSize, |
| 597 | getIntPtrConstant(StackAlign-1)); |
| 598 | // Mask out the low bits for alignment purposes. |
| 599 | AllocSize = DAG.getNode(ISD::AND, AllocSize.getValueType(), AllocSize, |
| 600 | getIntPtrConstant(~(uint64_t)(StackAlign-1))); |
| 601 | } |
| 602 | |
| 603 | SDOperand DSA = DAG.getNode(ISD::DYNAMIC_STACKALLOC, AllocSize.getValueType(), |
Chris Lattner | a651cf6 | 2005-01-17 19:43:36 +0000 | [diff] [blame] | 604 | getRoot(), AllocSize, |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 605 | getIntPtrConstant(Align)); |
| 606 | DAG.setRoot(setValue(&I, DSA).getValue(1)); |
| 607 | |
| 608 | // Inform the Frame Information that we have just allocated a variable-sized |
| 609 | // object. |
| 610 | CurMBB->getParent()->getFrameInfo()->CreateVariableSizedObject(); |
| 611 | } |
| 612 | |
| 613 | |
| 614 | void SelectionDAGLowering::visitLoad(LoadInst &I) { |
| 615 | SDOperand Ptr = getValue(I.getOperand(0)); |
Chris Lattner | d394811 | 2005-01-17 22:19:26 +0000 | [diff] [blame] | 616 | |
| 617 | SDOperand Root; |
| 618 | if (I.isVolatile()) |
| 619 | Root = getRoot(); |
| 620 | else { |
| 621 | // Do not serialize non-volatile loads against each other. |
| 622 | Root = DAG.getRoot(); |
| 623 | } |
| 624 | |
| 625 | SDOperand L = DAG.getLoad(TLI.getValueType(I.getType()), Root, Ptr); |
| 626 | setValue(&I, L); |
| 627 | |
| 628 | if (I.isVolatile()) |
| 629 | DAG.setRoot(L.getValue(1)); |
| 630 | else |
| 631 | PendingLoads.push_back(L.getValue(1)); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 632 | } |
| 633 | |
| 634 | |
| 635 | void SelectionDAGLowering::visitStore(StoreInst &I) { |
| 636 | Value *SrcV = I.getOperand(0); |
| 637 | SDOperand Src = getValue(SrcV); |
| 638 | SDOperand Ptr = getValue(I.getOperand(1)); |
Chris Lattner | a651cf6 | 2005-01-17 19:43:36 +0000 | [diff] [blame] | 639 | DAG.setRoot(DAG.getNode(ISD::STORE, MVT::Other, getRoot(), Src, Ptr)); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 640 | } |
| 641 | |
| 642 | void SelectionDAGLowering::visitCall(CallInst &I) { |
Chris Lattner | 64e14b1 | 2005-01-08 22:48:57 +0000 | [diff] [blame] | 643 | const char *RenameFn = 0; |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 644 | if (Function *F = I.getCalledFunction()) |
| 645 | switch (F->getIntrinsicID()) { |
| 646 | case 0: break; // Not an intrinsic. |
| 647 | case Intrinsic::vastart: visitVAStart(I); return; |
| 648 | case Intrinsic::vaend: visitVAEnd(I); return; |
| 649 | case Intrinsic::vacopy: visitVACopy(I); return; |
Chris Lattner | 39ae362 | 2005-01-09 00:00:49 +0000 | [diff] [blame] | 650 | case Intrinsic::returnaddress: visitFrameReturnAddress(I, false); return; |
| 651 | case Intrinsic::frameaddress: visitFrameReturnAddress(I, true); return; |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 652 | default: |
| 653 | // FIXME: IMPLEMENT THESE. |
| 654 | // readport, writeport, readio, writeio |
| 655 | assert(0 && "This intrinsic is not implemented yet!"); |
| 656 | return; |
Chris Lattner | 64e14b1 | 2005-01-08 22:48:57 +0000 | [diff] [blame] | 657 | case Intrinsic::setjmp: RenameFn = "setjmp"; break; |
| 658 | case Intrinsic::longjmp: RenameFn = "longjmp"; break; |
Chris Lattner | 7041ee3 | 2005-01-11 05:56:49 +0000 | [diff] [blame] | 659 | case Intrinsic::memcpy: visitMemIntrinsic(I, ISD::MEMCPY); return; |
| 660 | case Intrinsic::memset: visitMemIntrinsic(I, ISD::MEMSET); return; |
| 661 | case Intrinsic::memmove: visitMemIntrinsic(I, ISD::MEMMOVE); return; |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 662 | |
| 663 | case Intrinsic::isunordered: |
Chris Lattner | f30b73b | 2005-01-18 02:52:03 +0000 | [diff] [blame] | 664 | setValue(&I, DAG.getSetCC(ISD::SETUO, MVT::i1, getValue(I.getOperand(1)), |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 665 | getValue(I.getOperand(2)))); |
| 666 | return; |
| 667 | } |
| 668 | |
Chris Lattner | 64e14b1 | 2005-01-08 22:48:57 +0000 | [diff] [blame] | 669 | SDOperand Callee; |
| 670 | if (!RenameFn) |
| 671 | Callee = getValue(I.getOperand(0)); |
| 672 | else |
| 673 | Callee = DAG.getExternalSymbol(RenameFn, TLI.getPointerTy()); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 674 | std::vector<std::pair<SDOperand, const Type*> > Args; |
| 675 | |
| 676 | for (unsigned i = 1, e = I.getNumOperands(); i != e; ++i) { |
| 677 | Value *Arg = I.getOperand(i); |
| 678 | SDOperand ArgNode = getValue(Arg); |
| 679 | Args.push_back(std::make_pair(ArgNode, Arg->getType())); |
| 680 | } |
| 681 | |
Nate Begeman | 8e21e71 | 2005-03-26 01:29:23 +0000 | [diff] [blame] | 682 | const PointerType *PT = cast<PointerType>(I.getCalledValue()->getType()); |
| 683 | const FunctionType *FTy = cast<FunctionType>(PT->getElementType()); |
| 684 | |
Chris Lattner | cf5734d | 2005-01-08 19:26:18 +0000 | [diff] [blame] | 685 | std::pair<SDOperand,SDOperand> Result = |
Nate Begeman | 8e21e71 | 2005-03-26 01:29:23 +0000 | [diff] [blame] | 686 | TLI.LowerCallTo(getRoot(), I.getType(), FTy->isVarArg(), Callee, Args, DAG); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 687 | if (I.getType() != Type::VoidTy) |
Chris Lattner | cf5734d | 2005-01-08 19:26:18 +0000 | [diff] [blame] | 688 | setValue(&I, Result.first); |
| 689 | DAG.setRoot(Result.second); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 690 | } |
| 691 | |
| 692 | void SelectionDAGLowering::visitMalloc(MallocInst &I) { |
| 693 | SDOperand Src = getValue(I.getOperand(0)); |
| 694 | |
| 695 | MVT::ValueType IntPtr = TLI.getPointerTy(); |
Chris Lattner | 68cd65e | 2005-01-22 23:04:37 +0000 | [diff] [blame] | 696 | |
| 697 | if (IntPtr < Src.getValueType()) |
| 698 | Src = DAG.getNode(ISD::TRUNCATE, IntPtr, Src); |
| 699 | else if (IntPtr > Src.getValueType()) |
| 700 | Src = DAG.getNode(ISD::ZERO_EXTEND, IntPtr, Src); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 701 | |
| 702 | // Scale the source by the type size. |
| 703 | uint64_t ElementSize = TD.getTypeSize(I.getType()->getElementType()); |
| 704 | Src = DAG.getNode(ISD::MUL, Src.getValueType(), |
| 705 | Src, getIntPtrConstant(ElementSize)); |
| 706 | |
| 707 | std::vector<std::pair<SDOperand, const Type*> > Args; |
| 708 | Args.push_back(std::make_pair(Src, TLI.getTargetData().getIntPtrType())); |
Chris Lattner | cf5734d | 2005-01-08 19:26:18 +0000 | [diff] [blame] | 709 | |
| 710 | std::pair<SDOperand,SDOperand> Result = |
Nate Begeman | 8e21e71 | 2005-03-26 01:29:23 +0000 | [diff] [blame] | 711 | TLI.LowerCallTo(getRoot(), I.getType(), false, |
Chris Lattner | cf5734d | 2005-01-08 19:26:18 +0000 | [diff] [blame] | 712 | DAG.getExternalSymbol("malloc", IntPtr), |
| 713 | Args, DAG); |
| 714 | setValue(&I, Result.first); // Pointers always fit in registers |
| 715 | DAG.setRoot(Result.second); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 716 | } |
| 717 | |
| 718 | void SelectionDAGLowering::visitFree(FreeInst &I) { |
| 719 | std::vector<std::pair<SDOperand, const Type*> > Args; |
| 720 | Args.push_back(std::make_pair(getValue(I.getOperand(0)), |
| 721 | TLI.getTargetData().getIntPtrType())); |
| 722 | MVT::ValueType IntPtr = TLI.getPointerTy(); |
Chris Lattner | cf5734d | 2005-01-08 19:26:18 +0000 | [diff] [blame] | 723 | std::pair<SDOperand,SDOperand> Result = |
Nate Begeman | 8e21e71 | 2005-03-26 01:29:23 +0000 | [diff] [blame] | 724 | TLI.LowerCallTo(getRoot(), Type::VoidTy, false, |
Chris Lattner | cf5734d | 2005-01-08 19:26:18 +0000 | [diff] [blame] | 725 | DAG.getExternalSymbol("free", IntPtr), Args, DAG); |
| 726 | DAG.setRoot(Result.second); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 727 | } |
| 728 | |
Chris Lattner | 39ae362 | 2005-01-09 00:00:49 +0000 | [diff] [blame] | 729 | std::pair<SDOperand, SDOperand> |
| 730 | TargetLowering::LowerVAStart(SDOperand Chain, SelectionDAG &DAG) { |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 731 | // We have no sane default behavior, just emit a useful error message and bail |
| 732 | // out. |
Chris Lattner | 39ae362 | 2005-01-09 00:00:49 +0000 | [diff] [blame] | 733 | std::cerr << "Variable arguments handling not implemented on this target!\n"; |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 734 | abort(); |
Misha Brukman | d3f03e4 | 2005-02-17 21:39:27 +0000 | [diff] [blame] | 735 | return std::make_pair(SDOperand(), SDOperand()); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 736 | } |
| 737 | |
Chris Lattner | 39ae362 | 2005-01-09 00:00:49 +0000 | [diff] [blame] | 738 | SDOperand TargetLowering::LowerVAEnd(SDOperand Chain, SDOperand L, |
| 739 | SelectionDAG &DAG) { |
| 740 | // Default to a noop. |
| 741 | return Chain; |
| 742 | } |
| 743 | |
| 744 | std::pair<SDOperand,SDOperand> |
| 745 | TargetLowering::LowerVACopy(SDOperand Chain, SDOperand L, SelectionDAG &DAG) { |
| 746 | // Default to returning the input list. |
| 747 | return std::make_pair(L, Chain); |
| 748 | } |
| 749 | |
| 750 | std::pair<SDOperand,SDOperand> |
| 751 | TargetLowering::LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList, |
| 752 | const Type *ArgTy, SelectionDAG &DAG) { |
| 753 | // We have no sane default behavior, just emit a useful error message and bail |
| 754 | // out. |
| 755 | std::cerr << "Variable arguments handling not implemented on this target!\n"; |
| 756 | abort(); |
Misha Brukman | d3f03e4 | 2005-02-17 21:39:27 +0000 | [diff] [blame] | 757 | return std::make_pair(SDOperand(), SDOperand()); |
Chris Lattner | 39ae362 | 2005-01-09 00:00:49 +0000 | [diff] [blame] | 758 | } |
| 759 | |
| 760 | |
| 761 | void SelectionDAGLowering::visitVAStart(CallInst &I) { |
Chris Lattner | a651cf6 | 2005-01-17 19:43:36 +0000 | [diff] [blame] | 762 | std::pair<SDOperand,SDOperand> Result = TLI.LowerVAStart(getRoot(), DAG); |
Chris Lattner | 39ae362 | 2005-01-09 00:00:49 +0000 | [diff] [blame] | 763 | setValue(&I, Result.first); |
| 764 | DAG.setRoot(Result.second); |
| 765 | } |
| 766 | |
| 767 | void SelectionDAGLowering::visitVAArg(VAArgInst &I) { |
| 768 | std::pair<SDOperand,SDOperand> Result = |
Chris Lattner | a651cf6 | 2005-01-17 19:43:36 +0000 | [diff] [blame] | 769 | TLI.LowerVAArgNext(false, getRoot(), getValue(I.getOperand(0)), |
Chris Lattner | 39ae362 | 2005-01-09 00:00:49 +0000 | [diff] [blame] | 770 | I.getType(), DAG); |
| 771 | setValue(&I, Result.first); |
| 772 | DAG.setRoot(Result.second); |
| 773 | } |
| 774 | |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 775 | void SelectionDAGLowering::visitVANext(VANextInst &I) { |
Chris Lattner | 39ae362 | 2005-01-09 00:00:49 +0000 | [diff] [blame] | 776 | std::pair<SDOperand,SDOperand> Result = |
Chris Lattner | a651cf6 | 2005-01-17 19:43:36 +0000 | [diff] [blame] | 777 | TLI.LowerVAArgNext(true, getRoot(), getValue(I.getOperand(0)), |
Chris Lattner | 39ae362 | 2005-01-09 00:00:49 +0000 | [diff] [blame] | 778 | I.getArgType(), DAG); |
| 779 | setValue(&I, Result.first); |
| 780 | DAG.setRoot(Result.second); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 781 | } |
| 782 | |
| 783 | void SelectionDAGLowering::visitVAEnd(CallInst &I) { |
Chris Lattner | a651cf6 | 2005-01-17 19:43:36 +0000 | [diff] [blame] | 784 | DAG.setRoot(TLI.LowerVAEnd(getRoot(), getValue(I.getOperand(1)), DAG)); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 785 | } |
| 786 | |
| 787 | void SelectionDAGLowering::visitVACopy(CallInst &I) { |
Chris Lattner | 39ae362 | 2005-01-09 00:00:49 +0000 | [diff] [blame] | 788 | std::pair<SDOperand,SDOperand> Result = |
Chris Lattner | a651cf6 | 2005-01-17 19:43:36 +0000 | [diff] [blame] | 789 | TLI.LowerVACopy(getRoot(), getValue(I.getOperand(1)), DAG); |
Chris Lattner | 39ae362 | 2005-01-09 00:00:49 +0000 | [diff] [blame] | 790 | setValue(&I, Result.first); |
| 791 | DAG.setRoot(Result.second); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 792 | } |
| 793 | |
Chris Lattner | 39ae362 | 2005-01-09 00:00:49 +0000 | [diff] [blame] | 794 | |
| 795 | // It is always conservatively correct for llvm.returnaddress and |
| 796 | // llvm.frameaddress to return 0. |
| 797 | std::pair<SDOperand, SDOperand> |
| 798 | TargetLowering::LowerFrameReturnAddress(bool isFrameAddr, SDOperand Chain, |
| 799 | unsigned Depth, SelectionDAG &DAG) { |
| 800 | return std::make_pair(DAG.getConstant(0, getPointerTy()), Chain); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 801 | } |
| 802 | |
Chris Lattner | 171453a | 2005-01-16 07:28:41 +0000 | [diff] [blame] | 803 | SDOperand TargetLowering::LowerOperation(SDOperand Op) { |
| 804 | assert(0 && "LowerOperation not implemented for this target!"); |
| 805 | abort(); |
Misha Brukman | d3f03e4 | 2005-02-17 21:39:27 +0000 | [diff] [blame] | 806 | return SDOperand(); |
Chris Lattner | 171453a | 2005-01-16 07:28:41 +0000 | [diff] [blame] | 807 | } |
| 808 | |
Chris Lattner | 39ae362 | 2005-01-09 00:00:49 +0000 | [diff] [blame] | 809 | void SelectionDAGLowering::visitFrameReturnAddress(CallInst &I, bool isFrame) { |
| 810 | unsigned Depth = (unsigned)cast<ConstantUInt>(I.getOperand(1))->getValue(); |
| 811 | std::pair<SDOperand,SDOperand> Result = |
Chris Lattner | a651cf6 | 2005-01-17 19:43:36 +0000 | [diff] [blame] | 812 | TLI.LowerFrameReturnAddress(isFrame, getRoot(), Depth, DAG); |
Chris Lattner | 39ae362 | 2005-01-09 00:00:49 +0000 | [diff] [blame] | 813 | setValue(&I, Result.first); |
| 814 | DAG.setRoot(Result.second); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 815 | } |
| 816 | |
Chris Lattner | 7041ee3 | 2005-01-11 05:56:49 +0000 | [diff] [blame] | 817 | void SelectionDAGLowering::visitMemIntrinsic(CallInst &I, unsigned Op) { |
| 818 | std::vector<SDOperand> Ops; |
Chris Lattner | a651cf6 | 2005-01-17 19:43:36 +0000 | [diff] [blame] | 819 | Ops.push_back(getRoot()); |
Chris Lattner | 7041ee3 | 2005-01-11 05:56:49 +0000 | [diff] [blame] | 820 | Ops.push_back(getValue(I.getOperand(1))); |
| 821 | Ops.push_back(getValue(I.getOperand(2))); |
| 822 | Ops.push_back(getValue(I.getOperand(3))); |
| 823 | Ops.push_back(getValue(I.getOperand(4))); |
| 824 | DAG.setRoot(DAG.getNode(Op, MVT::Other, Ops)); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 825 | } |
| 826 | |
Chris Lattner | 7041ee3 | 2005-01-11 05:56:49 +0000 | [diff] [blame] | 827 | //===----------------------------------------------------------------------===// |
| 828 | // SelectionDAGISel code |
| 829 | //===----------------------------------------------------------------------===// |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 830 | |
| 831 | unsigned SelectionDAGISel::MakeReg(MVT::ValueType VT) { |
| 832 | return RegMap->createVirtualRegister(TLI.getRegClassFor(VT)); |
| 833 | } |
| 834 | |
| 835 | |
| 836 | |
| 837 | bool SelectionDAGISel::runOnFunction(Function &Fn) { |
| 838 | MachineFunction &MF = MachineFunction::construct(&Fn, TLI.getTargetMachine()); |
| 839 | RegMap = MF.getSSARegMap(); |
| 840 | DEBUG(std::cerr << "\n\n\n=== " << Fn.getName() << "\n"); |
| 841 | |
| 842 | FunctionLoweringInfo FuncInfo(TLI, Fn, MF); |
| 843 | |
| 844 | for (Function::iterator I = Fn.begin(), E = Fn.end(); I != E; ++I) |
| 845 | SelectBasicBlock(I, MF, FuncInfo); |
| 846 | |
| 847 | return true; |
| 848 | } |
| 849 | |
| 850 | |
Chris Lattner | ddb870b | 2005-01-13 17:59:43 +0000 | [diff] [blame] | 851 | SDOperand SelectionDAGISel:: |
| 852 | CopyValueToVirtualRegister(SelectionDAGLowering &SDL, Value *V, unsigned Reg) { |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 853 | SelectionDAG &DAG = SDL.DAG; |
Chris Lattner | f1fdaca | 2005-01-11 22:03:46 +0000 | [diff] [blame] | 854 | SDOperand Op = SDL.getValue(V); |
Chris Lattner | 18c2f13 | 2005-01-13 20:50:02 +0000 | [diff] [blame] | 855 | assert((Op.getOpcode() != ISD::CopyFromReg || |
| 856 | cast<RegSDNode>(Op)->getReg() != Reg) && |
| 857 | "Copy from a reg to the same reg!"); |
Chris Lattner | a651cf6 | 2005-01-17 19:43:36 +0000 | [diff] [blame] | 858 | return DAG.getCopyToReg(SDL.getRoot(), Op, Reg); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 859 | } |
| 860 | |
Chris Lattner | 0afa8e3 | 2005-01-17 17:55:19 +0000 | [diff] [blame] | 861 | /// IsOnlyUsedInOneBasicBlock - If the specified argument is only used in a |
| 862 | /// single basic block, return that block. Otherwise, return a null pointer. |
| 863 | static BasicBlock *IsOnlyUsedInOneBasicBlock(Argument *A) { |
| 864 | if (A->use_empty()) return 0; |
| 865 | BasicBlock *BB = cast<Instruction>(A->use_back())->getParent(); |
| 866 | for (Argument::use_iterator UI = A->use_begin(), E = A->use_end(); UI != E; |
| 867 | ++UI) |
| 868 | if (isa<PHINode>(*UI) || cast<Instruction>(*UI)->getParent() != BB) |
| 869 | return 0; // Disagreement among the users? |
Chris Lattner | aa781b3 | 2005-02-17 19:40:32 +0000 | [diff] [blame] | 870 | |
| 871 | // Okay, there is a single BB user. Only permit this optimization if this is |
| 872 | // the entry block, otherwise, we might sink argument loads into loops and |
| 873 | // stuff. Later, when we have global instruction selection, this won't be an |
| 874 | // issue clearly. |
| 875 | if (BB == BB->getParent()->begin()) |
| 876 | return BB; |
| 877 | return 0; |
Chris Lattner | 0afa8e3 | 2005-01-17 17:55:19 +0000 | [diff] [blame] | 878 | } |
| 879 | |
Chris Lattner | 068a81e | 2005-01-17 17:15:02 +0000 | [diff] [blame] | 880 | void SelectionDAGISel:: |
| 881 | LowerArguments(BasicBlock *BB, SelectionDAGLowering &SDL, |
| 882 | std::vector<SDOperand> &UnorderedChains) { |
| 883 | // If this is the entry block, emit arguments. |
| 884 | Function &F = *BB->getParent(); |
Chris Lattner | 0afa8e3 | 2005-01-17 17:55:19 +0000 | [diff] [blame] | 885 | FunctionLoweringInfo &FuncInfo = SDL.FuncInfo; |
Chris Lattner | 068a81e | 2005-01-17 17:15:02 +0000 | [diff] [blame] | 886 | |
| 887 | if (BB == &F.front()) { |
Chris Lattner | 0afa8e3 | 2005-01-17 17:55:19 +0000 | [diff] [blame] | 888 | SDOperand OldRoot = SDL.DAG.getRoot(); |
| 889 | |
Chris Lattner | 068a81e | 2005-01-17 17:15:02 +0000 | [diff] [blame] | 890 | std::vector<SDOperand> Args = TLI.LowerArguments(F, SDL.DAG); |
| 891 | |
Chris Lattner | 0afa8e3 | 2005-01-17 17:55:19 +0000 | [diff] [blame] | 892 | // If there were side effects accessing the argument list, do not do |
| 893 | // anything special. |
| 894 | if (OldRoot != SDL.DAG.getRoot()) { |
| 895 | unsigned a = 0; |
Chris Lattner | e4d5c44 | 2005-03-15 04:54:21 +0000 | [diff] [blame] | 896 | for (Function::arg_iterator AI = F.arg_begin(), E = F.arg_end(); AI != E; ++AI,++a) |
Chris Lattner | 0afa8e3 | 2005-01-17 17:55:19 +0000 | [diff] [blame] | 897 | if (!AI->use_empty()) { |
| 898 | SDL.setValue(AI, Args[a]); |
| 899 | SDOperand Copy = |
| 900 | CopyValueToVirtualRegister(SDL, AI, FuncInfo.ValueMap[AI]); |
| 901 | UnorderedChains.push_back(Copy); |
| 902 | } |
| 903 | } else { |
| 904 | // Otherwise, if any argument is only accessed in a single basic block, |
| 905 | // emit that argument only to that basic block. |
| 906 | unsigned a = 0; |
Chris Lattner | e4d5c44 | 2005-03-15 04:54:21 +0000 | [diff] [blame] | 907 | for (Function::arg_iterator AI = F.arg_begin(), E = F.arg_end(); AI != E; ++AI,++a) |
Chris Lattner | 0afa8e3 | 2005-01-17 17:55:19 +0000 | [diff] [blame] | 908 | if (!AI->use_empty()) { |
| 909 | if (BasicBlock *BBU = IsOnlyUsedInOneBasicBlock(AI)) { |
| 910 | FuncInfo.BlockLocalArguments.insert(std::make_pair(BBU, |
| 911 | std::make_pair(AI, a))); |
| 912 | } else { |
| 913 | SDL.setValue(AI, Args[a]); |
| 914 | SDOperand Copy = |
| 915 | CopyValueToVirtualRegister(SDL, AI, FuncInfo.ValueMap[AI]); |
| 916 | UnorderedChains.push_back(Copy); |
| 917 | } |
| 918 | } |
| 919 | } |
| 920 | } |
Chris Lattner | 068a81e | 2005-01-17 17:15:02 +0000 | [diff] [blame] | 921 | |
Chris Lattner | 0afa8e3 | 2005-01-17 17:55:19 +0000 | [diff] [blame] | 922 | // See if there are any block-local arguments that need to be emitted in this |
| 923 | // block. |
| 924 | |
| 925 | if (!FuncInfo.BlockLocalArguments.empty()) { |
| 926 | std::multimap<BasicBlock*, std::pair<Argument*, unsigned> >::iterator BLAI = |
| 927 | FuncInfo.BlockLocalArguments.lower_bound(BB); |
| 928 | if (BLAI != FuncInfo.BlockLocalArguments.end() && BLAI->first == BB) { |
| 929 | // Lower the arguments into this block. |
| 930 | std::vector<SDOperand> Args = TLI.LowerArguments(F, SDL.DAG); |
| 931 | |
| 932 | // Set up the value mapping for the local arguments. |
| 933 | for (; BLAI != FuncInfo.BlockLocalArguments.end() && BLAI->first == BB; |
| 934 | ++BLAI) |
| 935 | SDL.setValue(BLAI->second.first, Args[BLAI->second.second]); |
| 936 | |
| 937 | // Any dead arguments will just be ignored here. |
| 938 | } |
Chris Lattner | 068a81e | 2005-01-17 17:15:02 +0000 | [diff] [blame] | 939 | } |
| 940 | } |
| 941 | |
| 942 | |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 943 | void SelectionDAGISel::BuildSelectionDAG(SelectionDAG &DAG, BasicBlock *LLVMBB, |
| 944 | std::vector<std::pair<MachineInstr*, unsigned> > &PHINodesToUpdate, |
| 945 | FunctionLoweringInfo &FuncInfo) { |
| 946 | SelectionDAGLowering SDL(DAG, TLI, FuncInfo); |
Chris Lattner | ddb870b | 2005-01-13 17:59:43 +0000 | [diff] [blame] | 947 | |
| 948 | std::vector<SDOperand> UnorderedChains; |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 949 | |
Chris Lattner | 068a81e | 2005-01-17 17:15:02 +0000 | [diff] [blame] | 950 | // Lower any arguments needed in this block. |
| 951 | LowerArguments(LLVMBB, SDL, UnorderedChains); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 952 | |
| 953 | BB = FuncInfo.MBBMap[LLVMBB]; |
| 954 | SDL.setCurrentBasicBlock(BB); |
| 955 | |
| 956 | // Lower all of the non-terminator instructions. |
| 957 | for (BasicBlock::iterator I = LLVMBB->begin(), E = --LLVMBB->end(); |
| 958 | I != E; ++I) |
| 959 | SDL.visit(*I); |
| 960 | |
| 961 | // Ensure that all instructions which are used outside of their defining |
| 962 | // blocks are available as virtual registers. |
| 963 | for (BasicBlock::iterator I = LLVMBB->begin(), E = LLVMBB->end(); I != E;++I) |
Chris Lattner | f1fdaca | 2005-01-11 22:03:46 +0000 | [diff] [blame] | 964 | if (!I->use_empty() && !isa<PHINode>(I)) { |
Chris Lattner | ee749d7 | 2005-01-09 01:16:24 +0000 | [diff] [blame] | 965 | std::map<const Value*, unsigned>::iterator VMI =FuncInfo.ValueMap.find(I); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 966 | if (VMI != FuncInfo.ValueMap.end()) |
Chris Lattner | ddb870b | 2005-01-13 17:59:43 +0000 | [diff] [blame] | 967 | UnorderedChains.push_back( |
| 968 | CopyValueToVirtualRegister(SDL, I, VMI->second)); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 969 | } |
| 970 | |
| 971 | // Handle PHI nodes in successor blocks. Emit code into the SelectionDAG to |
| 972 | // ensure constants are generated when needed. Remember the virtual registers |
| 973 | // that need to be added to the Machine PHI nodes as input. We cannot just |
| 974 | // directly add them, because expansion might result in multiple MBB's for one |
| 975 | // BB. As such, the start of the BB might correspond to a different MBB than |
| 976 | // the end. |
| 977 | // |
| 978 | |
| 979 | // Emit constants only once even if used by multiple PHI nodes. |
| 980 | std::map<Constant*, unsigned> ConstantsOut; |
| 981 | |
| 982 | // Check successor nodes PHI nodes that expect a constant to be available from |
| 983 | // this block. |
| 984 | TerminatorInst *TI = LLVMBB->getTerminator(); |
| 985 | for (unsigned succ = 0, e = TI->getNumSuccessors(); succ != e; ++succ) { |
| 986 | BasicBlock *SuccBB = TI->getSuccessor(succ); |
| 987 | MachineBasicBlock::iterator MBBI = FuncInfo.MBBMap[SuccBB]->begin(); |
| 988 | PHINode *PN; |
| 989 | |
| 990 | // At this point we know that there is a 1-1 correspondence between LLVM PHI |
| 991 | // nodes and Machine PHI nodes, but the incoming operands have not been |
| 992 | // emitted yet. |
| 993 | for (BasicBlock::iterator I = SuccBB->begin(); |
Chris Lattner | f44fd88 | 2005-01-07 21:34:19 +0000 | [diff] [blame] | 994 | (PN = dyn_cast<PHINode>(I)); ++I) |
| 995 | if (!PN->use_empty()) { |
| 996 | unsigned Reg; |
| 997 | Value *PHIOp = PN->getIncomingValueForBlock(LLVMBB); |
| 998 | if (Constant *C = dyn_cast<Constant>(PHIOp)) { |
| 999 | unsigned &RegOut = ConstantsOut[C]; |
| 1000 | if (RegOut == 0) { |
| 1001 | RegOut = FuncInfo.CreateRegForValue(C); |
Chris Lattner | ddb870b | 2005-01-13 17:59:43 +0000 | [diff] [blame] | 1002 | UnorderedChains.push_back( |
| 1003 | CopyValueToVirtualRegister(SDL, C, RegOut)); |
Chris Lattner | f44fd88 | 2005-01-07 21:34:19 +0000 | [diff] [blame] | 1004 | } |
| 1005 | Reg = RegOut; |
| 1006 | } else { |
| 1007 | Reg = FuncInfo.ValueMap[PHIOp]; |
Chris Lattner | ee749d7 | 2005-01-09 01:16:24 +0000 | [diff] [blame] | 1008 | if (Reg == 0) { |
| 1009 | assert(isa<AllocaInst>(PHIOp) && |
| 1010 | FuncInfo.StaticAllocaMap.count(cast<AllocaInst>(PHIOp)) && |
| 1011 | "Didn't codegen value into a register!??"); |
| 1012 | Reg = FuncInfo.CreateRegForValue(PHIOp); |
Chris Lattner | ddb870b | 2005-01-13 17:59:43 +0000 | [diff] [blame] | 1013 | UnorderedChains.push_back( |
| 1014 | CopyValueToVirtualRegister(SDL, PHIOp, Reg)); |
Chris Lattner | ee749d7 | 2005-01-09 01:16:24 +0000 | [diff] [blame] | 1015 | } |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1016 | } |
Chris Lattner | f44fd88 | 2005-01-07 21:34:19 +0000 | [diff] [blame] | 1017 | |
| 1018 | // Remember that this register needs to added to the machine PHI node as |
| 1019 | // the input for this MBB. |
| 1020 | unsigned NumElements = |
| 1021 | TLI.getNumElements(TLI.getValueType(PN->getType())); |
| 1022 | for (unsigned i = 0, e = NumElements; i != e; ++i) |
| 1023 | PHINodesToUpdate.push_back(std::make_pair(MBBI++, Reg+i)); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1024 | } |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1025 | } |
| 1026 | ConstantsOut.clear(); |
| 1027 | |
Chris Lattner | ddb870b | 2005-01-13 17:59:43 +0000 | [diff] [blame] | 1028 | // Turn all of the unordered chains into one factored node. |
Chris Lattner | 5a6c6d9 | 2005-01-13 19:53:14 +0000 | [diff] [blame] | 1029 | if (!UnorderedChains.empty()) { |
Chris Lattner | d394811 | 2005-01-17 22:19:26 +0000 | [diff] [blame] | 1030 | UnorderedChains.push_back(SDL.getRoot()); |
Chris Lattner | ddb870b | 2005-01-13 17:59:43 +0000 | [diff] [blame] | 1031 | DAG.setRoot(DAG.getNode(ISD::TokenFactor, MVT::Other, UnorderedChains)); |
| 1032 | } |
| 1033 | |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1034 | // Lower the terminator after the copies are emitted. |
| 1035 | SDL.visit(*LLVMBB->getTerminator()); |
Chris Lattner | a651cf6 | 2005-01-17 19:43:36 +0000 | [diff] [blame] | 1036 | |
| 1037 | // Make sure the root of the DAG is up-to-date. |
| 1038 | DAG.setRoot(SDL.getRoot()); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1039 | } |
| 1040 | |
| 1041 | void SelectionDAGISel::SelectBasicBlock(BasicBlock *LLVMBB, MachineFunction &MF, |
| 1042 | FunctionLoweringInfo &FuncInfo) { |
Chris Lattner | ac9dc08 | 2005-01-23 04:36:26 +0000 | [diff] [blame] | 1043 | SelectionDAG DAG(TLI, MF); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1044 | CurDAG = &DAG; |
| 1045 | std::vector<std::pair<MachineInstr*, unsigned> > PHINodesToUpdate; |
| 1046 | |
| 1047 | // First step, lower LLVM code to some DAG. This DAG may use operations and |
| 1048 | // types that are not supported by the target. |
| 1049 | BuildSelectionDAG(DAG, LLVMBB, PHINodesToUpdate, FuncInfo); |
| 1050 | |
| 1051 | DEBUG(std::cerr << "Lowered selection DAG:\n"); |
| 1052 | DEBUG(DAG.dump()); |
| 1053 | |
| 1054 | // Second step, hack on the DAG until it only uses operations and types that |
| 1055 | // the target supports. |
Chris Lattner | ac9dc08 | 2005-01-23 04:36:26 +0000 | [diff] [blame] | 1056 | DAG.Legalize(); |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1057 | |
| 1058 | DEBUG(std::cerr << "Legalized selection DAG:\n"); |
| 1059 | DEBUG(DAG.dump()); |
| 1060 | |
| 1061 | // Finally, instruction select all of the operations to machine code, adding |
| 1062 | // the code to the MachineBasicBlock. |
| 1063 | InstructionSelectBasicBlock(DAG); |
| 1064 | |
Chris Lattner | 7944d9d | 2005-01-12 03:41:21 +0000 | [diff] [blame] | 1065 | if (ViewDAGs) DAG.viewGraph(); |
| 1066 | |
Chris Lattner | 1c08c71 | 2005-01-07 07:47:53 +0000 | [diff] [blame] | 1067 | DEBUG(std::cerr << "Selected machine code:\n"); |
| 1068 | DEBUG(BB->dump()); |
| 1069 | |
| 1070 | // Finally, now that we know what the last MBB the LLVM BB expanded is, update |
| 1071 | // PHI nodes in successors. |
| 1072 | for (unsigned i = 0, e = PHINodesToUpdate.size(); i != e; ++i) { |
| 1073 | MachineInstr *PHI = PHINodesToUpdate[i].first; |
| 1074 | assert(PHI->getOpcode() == TargetInstrInfo::PHI && |
| 1075 | "This is not a machine PHI node that we are updating!"); |
| 1076 | PHI->addRegOperand(PHINodesToUpdate[i].second); |
| 1077 | PHI->addMachineBasicBlockOperand(BB); |
| 1078 | } |
| 1079 | } |