LLVM API Documentation
00001 //===- X86ISelDAGToDAG.cpp - A DAG pattern matching inst selector for X86 -===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file defines a DAG pattern matching instruction selector for X86, 00011 // converting from a legalized dag to a X86 dag. 00012 // 00013 //===----------------------------------------------------------------------===// 00014 00015 #define DEBUG_TYPE "x86-isel" 00016 #include "X86.h" 00017 #include "X86InstrBuilder.h" 00018 #include "X86ISelLowering.h" 00019 #include "X86MachineFunctionInfo.h" 00020 #include "X86RegisterInfo.h" 00021 #include "X86Subtarget.h" 00022 #include "X86TargetMachine.h" 00023 #include "llvm/GlobalValue.h" 00024 #include "llvm/Instructions.h" 00025 #include "llvm/Intrinsics.h" 00026 #include "llvm/Support/CFG.h" 00027 #include "llvm/Type.h" 00028 #include "llvm/CodeGen/MachineConstantPool.h" 00029 #include "llvm/CodeGen/MachineFunction.h" 00030 #include "llvm/CodeGen/MachineFrameInfo.h" 00031 #include "llvm/CodeGen/MachineInstrBuilder.h" 00032 #include "llvm/CodeGen/MachineRegisterInfo.h" 00033 #include "llvm/CodeGen/SelectionDAGISel.h" 00034 #include "llvm/Target/TargetMachine.h" 00035 #include "llvm/Target/TargetOptions.h" 00036 #include "llvm/Support/Compiler.h" 00037 #include "llvm/Support/Debug.h" 00038 #include "llvm/Support/MathExtras.h" 00039 #include "llvm/Support/Streams.h" 00040 #include "llvm/ADT/SmallPtrSet.h" 00041 #include "llvm/ADT/Statistic.h" 00042 using namespace llvm; 00043 00044 STATISTIC(NumLoadMoved, "Number of loads moved below TokenFactor"); 00045 00046 //===----------------------------------------------------------------------===// 00047 // Pattern Matcher Implementation 00048 //===----------------------------------------------------------------------===// 00049 00050 namespace { 00051 /// X86ISelAddressMode - This corresponds to X86AddressMode, but uses 00052 /// SDValue's instead of register numbers for the leaves of the matched 00053 /// tree. 00054 struct X86ISelAddressMode { 00055 enum { 00056 RegBase, 00057 FrameIndexBase 00058 } BaseType; 00059 00060 struct { // This is really a union, discriminated by BaseType! 00061 SDValue Reg; 00062 int FrameIndex; 00063 } Base; 00064 00065 bool isRIPRel; // RIP as base? 00066 unsigned Scale; 00067 SDValue IndexReg; 00068 int32_t Disp; 00069 GlobalValue *GV; 00070 Constant *CP; 00071 const char *ES; 00072 int JT; 00073 unsigned Align; // CP alignment. 00074 00075 X86ISelAddressMode() 00076 : BaseType(RegBase), isRIPRel(false), Scale(1), IndexReg(), Disp(0), 00077 GV(0), CP(0), ES(0), JT(-1), Align(0) { 00078 } 00079 void dump() { 00080 cerr << "X86ISelAddressMode " << this << "\n"; 00081 cerr << "Base.Reg "; 00082 if (Base.Reg.getNode() != 0) Base.Reg.getNode()->dump(); 00083 else cerr << "nul"; 00084 cerr << " Base.FrameIndex " << Base.FrameIndex << "\n"; 00085 cerr << "isRIPRel " << isRIPRel << " Scale" << Scale << "\n"; 00086 cerr << "IndexReg "; 00087 if (IndexReg.getNode() != 0) IndexReg.getNode()->dump(); 00088 else cerr << "nul"; 00089 cerr << " Disp " << Disp << "\n"; 00090 cerr << "GV "; if (GV) GV->dump(); 00091 else cerr << "nul"; 00092 cerr << " CP "; if (CP) CP->dump(); 00093 else cerr << "nul"; 00094 cerr << "\n"; 00095 cerr << "ES "; if (ES) cerr << ES; else cerr << "nul"; 00096 cerr << " JT" << JT << " Align" << Align << "\n"; 00097 } 00098 }; 00099 } 00100 00101 namespace { 00102 //===--------------------------------------------------------------------===// 00103 /// ISel - X86 specific code to select X86 machine instructions for 00104 /// SelectionDAG operations. 00105 /// 00106 class VISIBILITY_HIDDEN X86DAGToDAGISel : public SelectionDAGISel { 00107 /// TM - Keep a reference to X86TargetMachine. 00108 /// 00109 X86TargetMachine &TM; 00110 00111 /// X86Lowering - This object fully describes how to lower LLVM code to an 00112 /// X86-specific SelectionDAG. 00113 X86TargetLowering &X86Lowering; 00114 00115 /// Subtarget - Keep a pointer to the X86Subtarget around so that we can 00116 /// make the right decision when generating code for different targets. 00117 const X86Subtarget *Subtarget; 00118 00119 /// CurBB - Current BB being isel'd. 00120 /// 00121 MachineBasicBlock *CurBB; 00122 00123 /// OptForSize - If true, selector should try to optimize for code size 00124 /// instead of performance. 00125 bool OptForSize; 00126 00127 public: 00128 X86DAGToDAGISel(X86TargetMachine &tm, bool fast) 00129 : SelectionDAGISel(tm, fast), 00130 TM(tm), X86Lowering(*TM.getTargetLowering()), 00131 Subtarget(&TM.getSubtarget<X86Subtarget>()), 00132 OptForSize(false) {} 00133 00134 virtual const char *getPassName() const { 00135 return "X86 DAG->DAG Instruction Selection"; 00136 } 00137 00138 /// InstructionSelect - This callback is invoked by 00139 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen. 00140 virtual void InstructionSelect(); 00141 00142 virtual void EmitFunctionEntryCode(Function &Fn, MachineFunction &MF); 00143 00144 virtual 00145 bool IsLegalAndProfitableToFold(SDNode *N, SDNode *U, SDNode *Root) const; 00146 00147 // Include the pieces autogenerated from the target description. 00148 #include "X86GenDAGISel.inc" 00149 00150 private: 00151 SDNode *Select(SDValue N); 00152 SDNode *SelectAtomic64(SDNode *Node, unsigned Opc); 00153 00154 bool MatchAddress(SDValue N, X86ISelAddressMode &AM, 00155 bool isRoot = true, unsigned Depth = 0); 00156 bool MatchAddressBase(SDValue N, X86ISelAddressMode &AM, 00157 bool isRoot, unsigned Depth); 00158 bool SelectAddr(SDValue Op, SDValue N, SDValue &Base, 00159 SDValue &Scale, SDValue &Index, SDValue &Disp); 00160 bool SelectLEAAddr(SDValue Op, SDValue N, SDValue &Base, 00161 SDValue &Scale, SDValue &Index, SDValue &Disp); 00162 bool SelectScalarSSELoad(SDValue Op, SDValue Pred, 00163 SDValue N, SDValue &Base, SDValue &Scale, 00164 SDValue &Index, SDValue &Disp, 00165 SDValue &InChain, SDValue &OutChain); 00166 bool TryFoldLoad(SDValue P, SDValue N, 00167 SDValue &Base, SDValue &Scale, 00168 SDValue &Index, SDValue &Disp); 00169 void PreprocessForRMW(); 00170 void PreprocessForFPConvert(); 00171 00172 /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for 00173 /// inline asm expressions. 00174 virtual bool SelectInlineAsmMemoryOperand(const SDValue &Op, 00175 char ConstraintCode, 00176 std::vector<SDValue> &OutOps); 00177 00178 void EmitSpecialCodeForMain(MachineBasicBlock *BB, MachineFrameInfo *MFI); 00179 00180 inline void getAddressOperands(X86ISelAddressMode &AM, SDValue &Base, 00181 SDValue &Scale, SDValue &Index, 00182 SDValue &Disp) { 00183 Base = (AM.BaseType == X86ISelAddressMode::FrameIndexBase) ? 00184 CurDAG->getTargetFrameIndex(AM.Base.FrameIndex, TLI.getPointerTy()) : 00185 AM.Base.Reg; 00186 Scale = getI8Imm(AM.Scale); 00187 Index = AM.IndexReg; 00188 // These are 32-bit even in 64-bit mode since RIP relative offset 00189 // is 32-bit. 00190 if (AM.GV) 00191 Disp = CurDAG->getTargetGlobalAddress(AM.GV, MVT::i32, AM.Disp); 00192 else if (AM.CP) 00193 Disp = CurDAG->getTargetConstantPool(AM.CP, MVT::i32, 00194 AM.Align, AM.Disp); 00195 else if (AM.ES) 00196 Disp = CurDAG->getTargetExternalSymbol(AM.ES, MVT::i32); 00197 else if (AM.JT != -1) 00198 Disp = CurDAG->getTargetJumpTable(AM.JT, MVT::i32); 00199 else 00200 Disp = CurDAG->getTargetConstant(AM.Disp, MVT::i32); 00201 } 00202 00203 /// getI8Imm - Return a target constant with the specified value, of type 00204 /// i8. 00205 inline SDValue getI8Imm(unsigned Imm) { 00206 return CurDAG->getTargetConstant(Imm, MVT::i8); 00207 } 00208 00209 /// getI16Imm - Return a target constant with the specified value, of type 00210 /// i16. 00211 inline SDValue getI16Imm(unsigned Imm) { 00212 return CurDAG->getTargetConstant(Imm, MVT::i16); 00213 } 00214 00215 /// getI32Imm - Return a target constant with the specified value, of type 00216 /// i32. 00217 inline SDValue getI32Imm(unsigned Imm) { 00218 return CurDAG->getTargetConstant(Imm, MVT::i32); 00219 } 00220 00221 /// getGlobalBaseReg - Return an SDNode that returns the value of 00222 /// the global base register. Output instructions required to 00223 /// initialize the global base register, if necessary. 00224 /// 00225 SDNode *getGlobalBaseReg(); 00226 00227 /// getTruncateTo8Bit - return an SDNode that implements a subreg based 00228 /// truncate of the specified operand to i8. This can be done with tablegen, 00229 /// except that this code uses MVT::Flag in a tricky way that happens to 00230 /// improve scheduling in some cases. 00231 SDNode *getTruncateTo8Bit(SDValue N0); 00232 00233 #ifndef NDEBUG 00234 unsigned Indent; 00235 #endif 00236 }; 00237 } 00238 00239 /// findFlagUse - Return use of MVT::Flag value produced by the specified 00240 /// SDNode. 00241 /// 00242 static SDNode *findFlagUse(SDNode *N) { 00243 unsigned FlagResNo = N->getNumValues()-1; 00244 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) { 00245 SDUse &Use = I.getUse(); 00246 if (Use.getResNo() == FlagResNo) 00247 return Use.getUser(); 00248 } 00249 return NULL; 00250 } 00251 00252 /// findNonImmUse - Return true if "Use" is a non-immediate use of "Def". 00253 /// This function recursively traverses up the operand chain, ignoring 00254 /// certain nodes. 00255 static bool findNonImmUse(SDNode *Use, SDNode* Def, SDNode *ImmedUse, 00256 SDNode *Root, 00257 SmallPtrSet<SDNode*, 16> &Visited) { 00258 if (Use->getNodeId() < Def->getNodeId() || 00259 !Visited.insert(Use)) 00260 return false; 00261 00262 for (unsigned i = 0, e = Use->getNumOperands(); i != e; ++i) { 00263 SDNode *N = Use->getOperand(i).getNode(); 00264 if (N == Def) { 00265 if (Use == ImmedUse || Use == Root) 00266 continue; // We are not looking for immediate use. 00267 assert(N != Root); 00268 return true; 00269 } 00270 00271 // Traverse up the operand chain. 00272 if (findNonImmUse(N, Def, ImmedUse, Root, Visited)) 00273 return true; 00274 } 00275 return false; 00276 } 00277 00278 /// isNonImmUse - Start searching from Root up the DAG to check is Def can 00279 /// be reached. Return true if that's the case. However, ignore direct uses 00280 /// by ImmedUse (which would be U in the example illustrated in 00281 /// IsLegalAndProfitableToFold) and by Root (which can happen in the store 00282 /// case). 00283 /// FIXME: to be really generic, we should allow direct use by any node 00284 /// that is being folded. But realisticly since we only fold loads which 00285 /// have one non-chain use, we only need to watch out for load/op/store 00286 /// and load/op/cmp case where the root (store / cmp) may reach the load via 00287 /// its chain operand. 00288 static inline bool isNonImmUse(SDNode *Root, SDNode *Def, SDNode *ImmedUse) { 00289 SmallPtrSet<SDNode*, 16> Visited; 00290 return findNonImmUse(Root, Def, ImmedUse, Root, Visited); 00291 } 00292 00293 00294 bool X86DAGToDAGISel::IsLegalAndProfitableToFold(SDNode *N, SDNode *U, 00295 SDNode *Root) const { 00296 if (Fast) return false; 00297 00298 if (U == Root) 00299 switch (U->getOpcode()) { 00300 default: break; 00301 case ISD::ADD: 00302 case ISD::ADDC: 00303 case ISD::ADDE: 00304 case ISD::AND: 00305 case ISD::OR: 00306 case ISD::XOR: { 00307 // If the other operand is a 8-bit immediate we should fold the immediate 00308 // instead. This reduces code size. 00309 // e.g. 00310 // movl 4(%esp), %eax 00311 // addl $4, %eax 00312 // vs. 00313 // movl $4, %eax 00314 // addl 4(%esp), %eax 00315 // The former is 2 bytes shorter. In case where the increment is 1, then 00316 // the saving can be 4 bytes (by using incl %eax). 00317 ConstantSDNode *Imm = dyn_cast<ConstantSDNode>(U->getOperand(1)); 00318 if (Imm) { 00319 if (U->getValueType(0) == MVT::i64) { 00320 if ((int32_t)Imm->getZExtValue() == (int64_t)Imm->getZExtValue()) 00321 return false; 00322 } else { 00323 if ((int8_t)Imm->getZExtValue() == (int64_t)Imm->getZExtValue()) 00324 return false; 00325 } 00326 } 00327 } 00328 } 00329 00330 // If Root use can somehow reach N through a path that that doesn't contain 00331 // U then folding N would create a cycle. e.g. In the following 00332 // diagram, Root can reach N through X. If N is folded into into Root, then 00333 // X is both a predecessor and a successor of U. 00334 // 00335 // [N*] // 00336 // ^ ^ // 00337 // / \ // 00338 // [U*] [X]? // 00339 // ^ ^ // 00340 // \ / // 00341 // \ / // 00342 // [Root*] // 00343 // 00344 // * indicates nodes to be folded together. 00345 // 00346 // If Root produces a flag, then it gets (even more) interesting. Since it 00347 // will be "glued" together with its flag use in the scheduler, we need to 00348 // check if it might reach N. 00349 // 00350 // [N*] // 00351 // ^ ^ // 00352 // / \ // 00353 // [U*] [X]? // 00354 // ^ ^ // 00355 // \ \ // 00356 // \ | // 00357 // [Root*] | // 00358 // ^ | // 00359 // f | // 00360 // | / // 00361 // [Y] / // 00362 // ^ / // 00363 // f / // 00364 // | / // 00365 // [FU] // 00366 // 00367 // If FU (flag use) indirectly reaches N (the load), and Root folds N 00368 // (call it Fold), then X is a predecessor of FU and a successor of 00369 // Fold. But since Fold and FU are flagged together, this will create 00370 // a cycle in the scheduling graph. 00371 00372 MVT VT = Root->getValueType(Root->getNumValues()-1); 00373 while (VT == MVT::Flag) { 00374 SDNode *FU = findFlagUse(Root); 00375 if (FU == NULL) 00376 break; 00377 Root = FU; 00378 VT = Root->getValueType(Root->getNumValues()-1); 00379 } 00380 00381 return !isNonImmUse(Root, N, U); 00382 } 00383 00384 /// MoveBelowTokenFactor - Replace TokenFactor operand with load's chain operand 00385 /// and move load below the TokenFactor. Replace store's chain operand with 00386 /// load's chain result. 00387 static void MoveBelowTokenFactor(SelectionDAG *CurDAG, SDValue Load, 00388 SDValue Store, SDValue TF) { 00389 SmallVector<SDValue, 4> Ops; 00390 for (unsigned i = 0, e = TF.getNode()->getNumOperands(); i != e; ++i) 00391 if (Load.getNode() == TF.getOperand(i).getNode()) 00392 Ops.push_back(Load.getOperand(0)); 00393 else 00394 Ops.push_back(TF.getOperand(i)); 00395 CurDAG->UpdateNodeOperands(TF, &Ops[0], Ops.size()); 00396 CurDAG->UpdateNodeOperands(Load, TF, Load.getOperand(1), Load.getOperand(2)); 00397 CurDAG->UpdateNodeOperands(Store, Load.getValue(1), Store.getOperand(1), 00398 Store.getOperand(2), Store.getOperand(3)); 00399 } 00400 00401 /// isRMWLoad - Return true if N is a load that's part of RMW sub-DAG. 00402 /// 00403 static bool isRMWLoad(SDValue N, SDValue Chain, SDValue Address, 00404 SDValue &Load) { 00405 if (N.getOpcode() == ISD::BIT_CONVERT) 00406 N = N.getOperand(0); 00407 00408 LoadSDNode *LD = dyn_cast<LoadSDNode>(N); 00409 if (!LD || LD->isVolatile()) 00410 return false; 00411 if (LD->getAddressingMode() != ISD::UNINDEXED) 00412 return false; 00413 00414 ISD::LoadExtType ExtType = LD->getExtensionType(); 00415 if (ExtType != ISD::NON_EXTLOAD && ExtType != ISD::EXTLOAD) 00416 return false; 00417 00418 if (N.hasOneUse() && 00419 N.getOperand(1) == Address && 00420 N.getNode()->isOperandOf(Chain.getNode())) { 00421 Load = N; 00422 return true; 00423 } 00424 return false; 00425 } 00426 00427 /// MoveBelowCallSeqStart - Replace CALLSEQ_START operand with load's chain 00428 /// operand and move load below the call's chain operand. 00429 static void MoveBelowCallSeqStart(SelectionDAG *CurDAG, SDValue Load, 00430 SDValue Call, SDValue CallSeqStart) { 00431 SmallVector<SDValue, 8> Ops; 00432 SDValue Chain = CallSeqStart.getOperand(0); 00433 if (Chain.getNode() == Load.getNode()) 00434 Ops.push_back(Load.getOperand(0)); 00435 else { 00436 assert(Chain.getOpcode() == ISD::TokenFactor && 00437 "Unexpected CallSeqStart chain operand"); 00438 for (unsigned i = 0, e = Chain.getNumOperands(); i != e; ++i) 00439 if (Chain.getOperand(i).getNode() == Load.getNode()) 00440 Ops.push_back(Load.getOperand(0)); 00441 else 00442 Ops.push_back(Chain.getOperand(i)); 00443 SDValue NewChain = 00444 CurDAG->getNode(ISD::TokenFactor, MVT::Other, &Ops[0], Ops.size()); 00445 Ops.clear(); 00446 Ops.push_back(NewChain); 00447 } 00448 for (unsigned i = 1, e = CallSeqStart.getNumOperands(); i != e; ++i) 00449 Ops.push_back(CallSeqStart.getOperand(i)); 00450 CurDAG->UpdateNodeOperands(CallSeqStart, &Ops[0], Ops.size()); 00451 CurDAG->UpdateNodeOperands(Load, Call.getOperand(0), 00452 Load.getOperand(1), Load.getOperand(2)); 00453 Ops.clear(); 00454 Ops.push_back(SDValue(Load.getNode(), 1)); 00455 for (unsigned i = 1, e = Call.getNode()->getNumOperands(); i != e; ++i) 00456 Ops.push_back(Call.getOperand(i)); 00457 CurDAG->UpdateNodeOperands(Call, &Ops[0], Ops.size()); 00458 } 00459 00460 /// isCalleeLoad - Return true if call address is a load and it can be 00461 /// moved below CALLSEQ_START and the chains leading up to the call. 00462 /// Return the CALLSEQ_START by reference as a second output. 00463 static bool isCalleeLoad(SDValue Callee, SDValue &Chain) { 00464 if (Callee.getNode() == Chain.getNode() || !Callee.hasOneUse()) 00465 return false; 00466 LoadSDNode *LD = dyn_cast<LoadSDNode>(Callee.getNode()); 00467 if (!LD || 00468 LD->isVolatile() || 00469 LD->getAddressingMode() != ISD::UNINDEXED || 00470 LD->getExtensionType() != ISD::NON_EXTLOAD) 00471 return false; 00472 00473 // Now let's find the callseq_start. 00474 while (Chain.getOpcode() != ISD::CALLSEQ_START) { 00475 if (!Chain.hasOneUse()) 00476 return false; 00477 Chain = Chain.getOperand(0); 00478 } 00479 00480 if (Chain.getOperand(0).getNode() == Callee.getNode()) 00481 return true; 00482 if (Chain.getOperand(0).getOpcode() == ISD::TokenFactor && 00483 Callee.getValue(1).isOperandOf(Chain.getOperand(0).getNode())) 00484 return true; 00485 return false; 00486 } 00487 00488 00489 /// PreprocessForRMW - Preprocess the DAG to make instruction selection better. 00490 /// This is only run if not in -fast mode (aka -O0). 00491 /// This allows the instruction selector to pick more read-modify-write 00492 /// instructions. This is a common case: 00493 /// 00494 /// [Load chain] 00495 /// ^ 00496 /// | 00497 /// [Load] 00498 /// ^ ^ 00499 /// | | 00500 /// / \- 00501 /// / | 00502 /// [TokenFactor] [Op] 00503 /// ^ ^ 00504 /// | | 00505 /// \ / 00506 /// \ / 00507 /// [Store] 00508 /// 00509 /// The fact the store's chain operand != load's chain will prevent the 00510 /// (store (op (load))) instruction from being selected. We can transform it to: 00511 /// 00512 /// [Load chain] 00513 /// ^ 00514 /// | 00515 /// [TokenFactor] 00516 /// ^ 00517 /// | 00518 /// [Load] 00519 /// ^ ^ 00520 /// | | 00521 /// | \- 00522 /// | | 00523 /// | [Op] 00524 /// | ^ 00525 /// | | 00526 /// \ / 00527 /// \ / 00528 /// [Store] 00529 void X86DAGToDAGISel::PreprocessForRMW() { 00530 for (SelectionDAG::allnodes_iterator I = CurDAG->allnodes_begin(), 00531 E = CurDAG->allnodes_end(); I != E; ++I) { 00532 if (I->getOpcode() == X86ISD::CALL) { 00533 /// Also try moving call address load from outside callseq_start to just 00534 /// before the call to allow it to be folded. 00535 /// 00536 /// [Load chain] 00537 /// ^ 00538 /// | 00539 /// [Load] 00540 /// ^ ^ 00541 /// | | 00542 /// / \-- 00543 /// / | 00544 ///[CALLSEQ_START] | 00545 /// ^ | 00546 /// | | 00547 /// [LOAD/C2Reg] | 00548 /// | | 00549 /// \ / 00550 /// \ / 00551 /// [CALL] 00552 SDValue Chain = I->getOperand(0); 00553 SDValue Load = I->getOperand(1); 00554 if (!isCalleeLoad(Load, Chain)) 00555 continue; 00556 MoveBelowCallSeqStart(CurDAG, Load, SDValue(I, 0), Chain); 00557 ++NumLoadMoved; 00558 continue; 00559 } 00560 00561 if (!ISD::isNON_TRUNCStore(I)) 00562 continue; 00563 SDValue Chain = I->getOperand(0); 00564 00565 if (Chain.getNode()->getOpcode() != ISD::TokenFactor) 00566 continue; 00567 00568 SDValue N1 = I->getOperand(1); 00569 SDValue N2 = I->getOperand(2); 00570 if ((N1.getValueType().isFloatingPoint() && 00571 !N1.getValueType().isVector()) || 00572 !N1.hasOneUse()) 00573 continue; 00574 00575 bool RModW = false; 00576 SDValue Load; 00577 unsigned Opcode = N1.getNode()->getOpcode(); 00578 switch (Opcode) { 00579 case ISD::ADD: 00580 case ISD::MUL: 00581 case ISD::AND: 00582 case ISD::OR: 00583 case ISD::XOR: 00584 case ISD::ADDC: 00585 case ISD::ADDE: 00586 case ISD::VECTOR_SHUFFLE: { 00587 SDValue N10 = N1.getOperand(0); 00588 SDValue N11 = N1.getOperand(1); 00589 RModW = isRMWLoad(N10, Chain, N2, Load); 00590 if (!RModW) 00591 RModW = isRMWLoad(N11, Chain, N2, Load); 00592 break; 00593 } 00594 case ISD::SUB: 00595 case ISD::SHL: 00596 case ISD::SRA: 00597 case ISD::SRL: 00598 case ISD::ROTL: 00599 case ISD::ROTR: 00600 case ISD::SUBC: 00601 case ISD::SUBE: 00602 case X86ISD::SHLD: 00603 case X86ISD::SHRD: { 00604 SDValue N10 = N1.getOperand(0); 00605 RModW = isRMWLoad(N10, Chain, N2, Load); 00606 break; 00607 } 00608 } 00609 00610 if (RModW) { 00611 MoveBelowTokenFactor(CurDAG, Load, SDValue(I, 0), Chain); 00612 ++NumLoadMoved; 00613 } 00614 } 00615 } 00616 00617 00618 /// PreprocessForFPConvert - Walk over the dag lowering fpround and fpextend 00619 /// nodes that target the FP stack to be store and load to the stack. This is a 00620 /// gross hack. We would like to simply mark these as being illegal, but when 00621 /// we do that, legalize produces these when it expands calls, then expands 00622 /// these in the same legalize pass. We would like dag combine to be able to 00623 /// hack on these between the call expansion and the node legalization. As such 00624 /// this pass basically does "really late" legalization of these inline with the 00625 /// X86 isel pass. 00626 void X86DAGToDAGISel::PreprocessForFPConvert() { 00627 for (SelectionDAG::allnodes_iterator I = CurDAG->allnodes_begin(), 00628 E = CurDAG->allnodes_end(); I != E; ) { 00629 SDNode *N = I++; // Preincrement iterator to avoid invalidation issues. 00630 if (N->getOpcode() != ISD::FP_ROUND && N->getOpcode() != ISD::FP_EXTEND) 00631 continue; 00632 00633 // If the source and destination are SSE registers, then this is a legal 00634 // conversion that should not be lowered. 00635 MVT SrcVT = N->getOperand(0).getValueType(); 00636 MVT DstVT = N->getValueType(0); 00637 bool SrcIsSSE = X86Lowering.isScalarFPTypeInSSEReg(SrcVT); 00638 bool DstIsSSE = X86Lowering.isScalarFPTypeInSSEReg(DstVT); 00639 if (SrcIsSSE && DstIsSSE) 00640 continue; 00641 00642 if (!SrcIsSSE && !DstIsSSE) { 00643 // If this is an FPStack extension, it is a noop. 00644 if (N->getOpcode() == ISD::FP_EXTEND) 00645 continue; 00646 // If this is a value-preserving FPStack truncation, it is a noop. 00647 if (N->getConstantOperandVal(1)) 00648 continue; 00649 } 00650 00651 // Here we could have an FP stack truncation or an FPStack <-> SSE convert. 00652 // FPStack has extload and truncstore. SSE can fold direct loads into other 00653 // operations. Based on this, decide what we want to do. 00654 MVT MemVT; 00655 if (N->getOpcode() == ISD::FP_ROUND) 00656 MemVT = DstVT; // FP_ROUND must use DstVT, we can't do a 'trunc load'. 00657 else 00658 MemVT = SrcIsSSE ? SrcVT : DstVT; 00659 00660 SDValue MemTmp = CurDAG->CreateStackTemporary(MemVT); 00661 00662 // FIXME: optimize the case where the src/dest is a load or store? 00663 SDValue Store = CurDAG->getTruncStore(CurDAG->getEntryNode(), 00664 N->getOperand(0), 00665 MemTmp, NULL, 0, MemVT); 00666 SDValue Result = CurDAG->getExtLoad(ISD::EXTLOAD, DstVT, Store, MemTmp, 00667 NULL, 0, MemVT); 00668 00669 // We're about to replace all uses of the FP_ROUND/FP_EXTEND with the 00670 // extload we created. This will cause general havok on the dag because 00671 // anything below the conversion could be folded into other existing nodes. 00672 // To avoid invalidating 'I', back it up to the convert node. 00673 --I; 00674 CurDAG->ReplaceAllUsesOfValueWith(SDValue(N, 0), Result); 00675 00676 // Now that we did that, the node is dead. Increment the iterator to the 00677 // next node to process, then delete N. 00678 ++I; 00679 CurDAG->DeleteNode(N); 00680 } 00681 } 00682 00683 /// InstructionSelectBasicBlock - This callback is invoked by SelectionDAGISel 00684 /// when it has created a SelectionDAG for us to codegen. 00685 void X86DAGToDAGISel::InstructionSelect() { 00686 CurBB = BB; // BB can change as result of isel. 00687 const Function *F = CurDAG->getMachineFunction().getFunction(); 00688 OptForSize = F->hasFnAttr(Attribute::OptimizeForSize); 00689 00690 DEBUG(BB->dump()); 00691 if (!Fast) 00692 PreprocessForRMW(); 00693 00694 // FIXME: This should only happen when not -fast. 00695 PreprocessForFPConvert(); 00696 00697 // Codegen the basic block. 00698 #ifndef NDEBUG 00699 DOUT << "===== Instruction selection begins:\n"; 00700 Indent = 0; 00701 #endif 00702 SelectRoot(*CurDAG); 00703 #ifndef NDEBUG 00704 DOUT << "===== Instruction selection ends:\n"; 00705 #endif 00706 00707 CurDAG->RemoveDeadNodes(); 00708 } 00709 00710 /// EmitSpecialCodeForMain - Emit any code that needs to be executed only in 00711 /// the main function. 00712 void X86DAGToDAGISel::EmitSpecialCodeForMain(MachineBasicBlock *BB, 00713 MachineFrameInfo *MFI) { 00714 const TargetInstrInfo *TII = TM.getInstrInfo(); 00715 if (Subtarget->isTargetCygMing()) 00716 BuildMI(BB, TII->get(X86::CALLpcrel32)).addExternalSymbol("__main"); 00717 } 00718 00719 void X86DAGToDAGISel::EmitFunctionEntryCode(Function &Fn, MachineFunction &MF) { 00720 // If this is main, emit special code for main. 00721 MachineBasicBlock *BB = MF.begin(); 00722 if (Fn.hasExternalLinkage() && Fn.getName() == "main") 00723 EmitSpecialCodeForMain(BB, MF.getFrameInfo()); 00724 } 00725 00726 /// MatchAddress - Add the specified node to the specified addressing mode, 00727 /// returning true if it cannot be done. This just pattern matches for the 00728 /// addressing mode. 00729 bool X86DAGToDAGISel::MatchAddress(SDValue N, X86ISelAddressMode &AM, 00730 bool isRoot, unsigned Depth) { 00731 bool is64Bit = Subtarget->is64Bit(); 00732 DOUT << "MatchAddress: "; DEBUG(AM.dump()); 00733 // Limit recursion. 00734 if (Depth > 5) 00735 return MatchAddressBase(N, AM, isRoot, Depth); 00736 00737 // RIP relative addressing: %rip + 32-bit displacement! 00738 if (AM.isRIPRel) { 00739 if (!AM.ES && AM.JT != -1 && N.getOpcode() == ISD::Constant) { 00740 uint64_t Val = cast<ConstantSDNode>(N)->getSExtValue(); 00741 if (!is64Bit || isInt32(AM.Disp + Val)) { 00742 AM.Disp += Val; 00743 return false; 00744 } 00745 } 00746 return true; 00747 } 00748 00749 switch (N.getOpcode()) { 00750 default: break; 00751 case ISD::Constant: { 00752 uint64_t Val = cast<ConstantSDNode>(N)->getSExtValue(); 00753 if (!is64Bit || isInt32(AM.Disp + Val)) { 00754 AM.Disp += Val; 00755 return false; 00756 } 00757 break; 00758 } 00759 00760 case X86ISD::Wrapper: { 00761 DOUT << "Wrapper: 64bit " << is64Bit; 00762 DOUT << " AM "; DEBUG(AM.dump()); DOUT << "\n"; 00763 // Under X86-64 non-small code model, GV (and friends) are 64-bits. 00764 // Also, base and index reg must be 0 in order to use rip as base. 00765 if (is64Bit && (TM.getCodeModel() != CodeModel::Small || 00766 AM.Base.Reg.getNode() || AM.IndexReg.getNode())) 00767 break; 00768 if (AM.GV != 0 || AM.CP != 0 || AM.ES != 0 || AM.JT != -1) 00769 break; 00770 // If value is available in a register both base and index components have 00771 // been picked, we can't fit the result available in the register in the 00772 // addressing mode. Duplicate GlobalAddress or ConstantPool as displacement. 00773 { 00774 SDValue N0 = N.getOperand(0); 00775 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(N0)) { 00776 uint64_t Offset = G->getOffset(); 00777 if (!is64Bit || isInt32(AM.Disp + Offset)) { 00778 GlobalValue *GV = G->getGlobal(); 00779 AM.GV = GV; 00780 AM.Disp += Offset; 00781 AM.isRIPRel = TM.symbolicAddressesAreRIPRel(); 00782 return false; 00783 } 00784 } else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N0)) { 00785 uint64_t Offset = CP->getOffset(); 00786 if (!is64Bit || isInt32(AM.Disp + Offset)) { 00787 AM.CP = CP->getConstVal(); 00788 AM.Align = CP->getAlignment(); 00789 AM.Disp += Offset; 00790 AM.isRIPRel = TM.symbolicAddressesAreRIPRel(); 00791 return false; 00792 } 00793 } else if (ExternalSymbolSDNode *S =dyn_cast<ExternalSymbolSDNode>(N0)) { 00794 AM.ES = S->getSymbol(); 00795 AM.isRIPRel = TM.symbolicAddressesAreRIPRel(); 00796 return false; 00797 } else if (JumpTableSDNode *J = dyn_cast<JumpTableSDNode>(N0)) { 00798 AM.JT = J->getIndex(); 00799 AM.isRIPRel = TM.symbolicAddressesAreRIPRel(); 00800 return false; 00801 } 00802 } 00803 break; 00804 } 00805 00806 case ISD::FrameIndex: 00807 if (AM.BaseType == X86ISelAddressMode::RegBase 00808 && AM.Base.Reg.getNode() == 0) { 00809 AM.BaseType = X86ISelAddressMode::FrameIndexBase; 00810 AM.Base.FrameIndex = cast<FrameIndexSDNode>(N)->getIndex(); 00811 return false; 00812 } 00813 break; 00814 00815 case ISD::SHL: 00816 if (AM.IndexReg.getNode() != 0 || AM.Scale != 1 || AM.isRIPRel) 00817 break; 00818 00819 if (ConstantSDNode 00820 *CN = dyn_cast<ConstantSDNode>(N.getNode()->getOperand(1))) { 00821 unsigned Val = CN->getZExtValue(); 00822 if (Val == 1 || Val == 2 || Val == 3) { 00823 AM.Scale = 1 << Val; 00824 SDValue ShVal = N.getNode()->getOperand(0); 00825 00826 // Okay, we know that we have a scale by now. However, if the scaled 00827 // value is an add of something and a constant, we can fold the 00828 // constant into the disp field here. 00829 if (ShVal.getNode()->getOpcode() == ISD::ADD && ShVal.hasOneUse() && 00830 isa<ConstantSDNode>(ShVal.getNode()->getOperand(1))) { 00831 AM.IndexReg = ShVal.getNode()->getOperand(0); 00832 ConstantSDNode *AddVal = 00833 cast<ConstantSDNode>(ShVal.getNode()->getOperand(1)); 00834 uint64_t Disp = AM.Disp + (AddVal->getSExtValue() << Val); 00835 if (!is64Bit || isInt32(Disp)) 00836 AM.Disp = Disp; 00837 else 00838 AM.IndexReg = ShVal; 00839 } else { 00840 AM.IndexReg = ShVal; 00841 } 00842 return false; 00843 } 00844 break; 00845 } 00846 00847 case ISD::SMUL_LOHI: 00848 case ISD::UMUL_LOHI: 00849 // A mul_lohi where we need the low part can be folded as a plain multiply. 00850 if (N.getResNo() != 0) break; 00851 // FALL THROUGH 00852 case ISD::MUL: 00853 // X*[3,5,9] -> X+X*[2,4,8] 00854 if (AM.BaseType == X86ISelAddressMode::RegBase && 00855 AM.Base.Reg.getNode() == 0 && 00856 AM.IndexReg.getNode() == 0 && 00857 !AM.isRIPRel) { 00858 if (ConstantSDNode 00859 *CN = dyn_cast<ConstantSDNode>(N.getNode()->getOperand(1))) 00860 if (CN->getZExtValue() == 3 || CN->getZExtValue() == 5 || 00861 CN->getZExtValue() == 9) { 00862 AM.Scale = unsigned(CN->getZExtValue())-1; 00863 00864 SDValue MulVal = N.getNode()->getOperand(0); 00865 SDValue Reg; 00866 00867 // Okay, we know that we have a scale by now. However, if the scaled 00868 // value is an add of something and a constant, we can fold the 00869 // constant into the disp field here. 00870 if (MulVal.getNode()->getOpcode() == ISD::ADD && MulVal.hasOneUse() && 00871 isa<ConstantSDNode>(MulVal.getNode()->getOperand(1))) { 00872 Reg = MulVal.getNode()->getOperand(0); 00873 ConstantSDNode *AddVal = 00874 cast<ConstantSDNode>(MulVal.getNode()->getOperand(1)); 00875 uint64_t Disp = AM.Disp + AddVal->getSExtValue() * 00876 CN->getZExtValue(); 00877 if (!is64Bit || isInt32(Disp)) 00878 AM.Disp = Disp; 00879 else 00880 Reg = N.getNode()->getOperand(0); 00881 } else { 00882 Reg = N.getNode()->getOperand(0); 00883 } 00884 00885 AM.IndexReg = AM.Base.Reg = Reg; 00886 return false; 00887 } 00888 } 00889 break; 00890 00891 case ISD::ADD: { 00892 X86ISelAddressMode Backup = AM; 00893 if (!MatchAddress(N.getNode()->getOperand(0), AM, false, Depth+1) && 00894 !MatchAddress(N.getNode()->getOperand(1), AM, false, Depth+1)) 00895 return false; 00896 AM = Backup; 00897 if (!MatchAddress(N.getNode()->getOperand(1), AM, false, Depth+1) && 00898 !MatchAddress(N.getNode()->getOperand(0), AM, false, Depth+1)) 00899 return false; 00900 AM = Backup; 00901 break; 00902 } 00903 00904 case ISD::OR: 00905 // Handle "X | C" as "X + C" iff X is known to have C bits clear. 00906 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) { 00907 X86ISelAddressMode Backup = AM; 00908 uint64_t Offset = CN->getSExtValue(); 00909 // Start with the LHS as an addr mode. 00910 if (!MatchAddress(N.getOperand(0), AM, false) && 00911 // Address could not have picked a GV address for the displacement. 00912 AM.GV == NULL && 00913 // On x86-64, the resultant disp must fit in 32-bits. 00914 (!is64Bit || isInt32(AM.Disp + Offset)) && 00915 // Check to see if the LHS & C is zero. 00916 CurDAG->MaskedValueIsZero(N.getOperand(0), CN->getAPIntValue())) { 00917 AM.Disp += Offset; 00918 return false; 00919 } 00920 AM = Backup; 00921 } 00922 break; 00923 00924 case ISD::AND: { 00925 // Handle "(x << C1) & C2" as "(X & (C2>>C1)) << C1" if safe and if this 00926 // allows us to fold the shift into this addressing mode. 00927 SDValue Shift = N.getOperand(0); 00928 if (Shift.getOpcode() != ISD::SHL) break; 00929 00930 // Scale must not be used already. 00931 if (AM.IndexReg.getNode() != 0 || AM.Scale != 1) break; 00932 00933 // Not when RIP is used as the base. 00934 if (AM.isRIPRel) break; 00935 00936 ConstantSDNode *C2 = dyn_cast<ConstantSDNode>(N.getOperand(1)); 00937 ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(Shift.getOperand(1)); 00938 if (!C1 || !C2) break; 00939 00940 // Not likely to be profitable if either the AND or SHIFT node has more 00941 // than one use (unless all uses are for address computation). Besides, 00942 // isel mechanism requires their node ids to be reused. 00943 if (!N.hasOneUse() || !Shift.hasOneUse()) 00944 break; 00945 00946 // Verify that the shift amount is something we can fold. 00947 unsigned ShiftCst = C1->getZExtValue(); 00948 if (ShiftCst != 1 && ShiftCst != 2 && ShiftCst != 3) 00949 break; 00950 00951 // Get the new AND mask, this folds to a constant. 00952 SDValue X = Shift.getOperand(0); 00953 SDValue NewANDMask = CurDAG->getNode(ISD::SRL, N.getValueType(), 00954 SDValue(C2, 0), SDValue(C1, 0)); 00955 SDValue NewAND = CurDAG->getNode(ISD::AND, N.getValueType(), X, NewANDMask); 00956 SDValue NewSHIFT = CurDAG->getNode(ISD::SHL, N.getValueType(), 00957 NewAND, SDValue(C1, 0)); 00958 00959 // Insert the new nodes into the topological ordering. 00960 if (C1->getNodeId() > X.getNode()->getNodeId()) { 00961 CurDAG->RepositionNode(X.getNode(), C1); 00962 C1->setNodeId(X.getNode()->getNodeId()); 00963 } 00964 if (NewANDMask.getNode()->getNodeId() == -1 || 00965 NewANDMask.getNode()->getNodeId() > X.getNode()->getNodeId()) { 00966 CurDAG->RepositionNode(X.getNode(), NewANDMask.getNode()); 00967 NewANDMask.getNode()->setNodeId(X.getNode()->getNodeId()); 00968 } 00969 if (NewAND.getNode()->getNodeId() == -1 || 00970 NewAND.getNode()->getNodeId() > Shift.getNode()->getNodeId()) { 00971 CurDAG->RepositionNode(Shift.getNode(), NewAND.getNode()); 00972 NewAND.getNode()->setNodeId(Shift.getNode()->getNodeId()); 00973 } 00974 if (NewSHIFT.getNode()->getNodeId() == -1 || 00975 NewSHIFT.getNode()->getNodeId() > N.getNode()->getNodeId()) { 00976 CurDAG->RepositionNode(N.getNode(), NewSHIFT.getNode()); 00977 NewSHIFT.getNode()->setNodeId(N.getNode()->getNodeId()); 00978 } 00979 00980 CurDAG->ReplaceAllUsesWith(N, NewSHIFT); 00981 00982 AM.Scale = 1 << ShiftCst; 00983 AM.IndexReg = NewAND; 00984 return false; 00985 } 00986 } 00987 00988 return MatchAddressBase(N, AM, isRoot, Depth); 00989 } 00990 00991 /// MatchAddressBase - Helper for MatchAddress. Add the specified node to the 00992 /// specified addressing mode without any further recursion. 00993 bool X86DAGToDAGISel::MatchAddressBase(SDValue N, X86ISelAddressMode &AM, 00994 bool isRoot, unsigned Depth) { 00995 // Is the base register already occupied? 00996 if (AM.BaseType != X86ISelAddressMode::RegBase || AM.Base.Reg.getNode()) { 00997 // If so, check to see if the scale index register is set. 00998 if (AM.IndexReg.getNode() == 0 && !AM.isRIPRel) { 00999 AM.IndexReg = N; 01000 AM.Scale = 1; 01001 return false; 01002 } 01003 01004 // Otherwise, we cannot select it. 01005 return true; 01006 } 01007 01008 // Default, generate it as a register. 01009 AM.BaseType = X86ISelAddressMode::RegBase; 01010 AM.Base.Reg = N; 01011 return false; 01012 } 01013 01014 /// SelectAddr - returns true if it is able pattern match an addressing mode. 01015 /// It returns the operands which make up the maximal addressing mode it can 01016 /// match by reference. 01017 bool X86DAGToDAGISel::SelectAddr(SDValue Op, SDValue N, SDValue &Base, 01018 SDValue &Scale, SDValue &Index, 01019 SDValue &Disp) { 01020 X86ISelAddressMode AM; 01021 if (MatchAddress(N, AM)) 01022 return false; 01023 01024 MVT VT = N.getValueType(); 01025 if (AM.BaseType == X86ISelAddressMode::RegBase) { 01026 if (!AM.Base.Reg.getNode()) 01027 AM.Base.Reg = CurDAG->getRegister(0, VT); 01028 } 01029 01030 if (!AM.IndexReg.getNode()) 01031 AM.IndexReg = CurDAG->getRegister(0, VT); 01032 01033 getAddressOperands(AM, Base, Scale, Index, Disp); 01034 return true; 01035 } 01036 01037 /// SelectScalarSSELoad - Match a scalar SSE load. In particular, we want to 01038 /// match a load whose top elements are either undef or zeros. The load flavor 01039 /// is derived from the type of N, which is either v4f32 or v2f64. 01040 bool X86DAGToDAGISel::SelectScalarSSELoad(SDValue Op, SDValue Pred, 01041 SDValue N, SDValue &Base, 01042 SDValue &Scale, SDValue &Index, 01043 SDValue &Disp, SDValue &InChain, 01044 SDValue &OutChain) { 01045 if (N.getOpcode() == ISD::SCALAR_TO_VECTOR) { 01046 InChain = N.getOperand(0).getValue(1); 01047 if (ISD::isNON_EXTLoad(InChain.getNode()) && 01048 InChain.getValue(0).hasOneUse() && 01049 N.hasOneUse() && 01050 IsLegalAndProfitableToFold(N.getNode(), Pred.getNode(), Op.getNode())) { 01051 LoadSDNode *LD = cast<LoadSDNode>(InChain); 01052 if (!SelectAddr(Op, LD->getBasePtr(), Base, Scale, Index, Disp)) 01053 return false; 01054 OutChain = LD->getChain(); 01055 return true; 01056 } 01057 } 01058 01059 // Also handle the case where we explicitly require zeros in the top 01060 // elements. This is a vector shuffle from the zero vector. 01061 if (N.getOpcode() == X86ISD::VZEXT_MOVL && N.getNode()->hasOneUse() && 01062 // Check to see if the top elements are all zeros (or bitcast of zeros). 01063 N.getOperand(0).getOpcode() == ISD::SCALAR_TO_VECTOR && 01064 N.getOperand(0).getNode()->hasOneUse() && 01065 ISD::isNON_EXTLoad(N.getOperand(0).getOperand(0).getNode()) && 01066 N.getOperand(0).getOperand(0).hasOneUse()) { 01067 // Okay, this is a zero extending load. Fold it. 01068 LoadSDNode *LD = cast<LoadSDNode>(N.getOperand(0).getOperand(0)); 01069 if (!SelectAddr(Op, LD->getBasePtr(), Base, Scale, Index, Disp)) 01070 return false; 01071 OutChain = LD->getChain(); 01072 InChain = SDValue(LD, 1); 01073 return true; 01074 } 01075 return false; 01076 } 01077 01078 01079 /// SelectLEAAddr - it calls SelectAddr and determines if the maximal addressing 01080 /// mode it matches can be cost effectively emitted as an LEA instruction. 01081 bool X86DAGToDAGISel::SelectLEAAddr(SDValue Op, SDValue N, 01082 SDValue &Base, SDValue &Scale, 01083 SDValue &Index, SDValue &Disp) { 01084 X86ISelAddressMode AM; 01085 if (MatchAddress(N, AM)) 01086 return false; 01087 01088 MVT VT = N.getValueType(); 01089 unsigned Complexity = 0; 01090 if (AM.BaseType == X86ISelAddressMode::RegBase) 01091 if (AM.Base.Reg.getNode()) 01092 Complexity = 1; 01093 else 01094 AM.Base.Reg = CurDAG->getRegister(0, VT); 01095 else if (AM.BaseType == X86ISelAddressMode::FrameIndexBase) 01096 Complexity = 4; 01097 01098 if (AM.IndexReg.getNode()) 01099 Complexity++; 01100 else 01101 AM.IndexReg = CurDAG->getRegister(0, VT); 01102 01103 // Don't match just leal(,%reg,2). It's cheaper to do addl %reg, %reg, or with 01104 // a simple shift. 01105 if (AM.Scale > 1) 01106 Complexity++; 01107 01108 // FIXME: We are artificially lowering the criteria to turn ADD %reg, $GA 01109 // to a LEA. This is determined with some expermentation but is by no means 01110 // optimal (especially for code size consideration). LEA is nice because of 01111 // its three-address nature. Tweak the cost function again when we can run 01112 // convertToThreeAddress() at register allocation time. 01113 if (AM.GV || AM.CP || AM.ES || AM.JT != -1) { 01114 // For X86-64, we should always use lea to materialize RIP relative 01115 // addresses. 01116 if (Subtarget->is64Bit()) 01117 Complexity = 4; 01118 else 01119 Complexity += 2; 01120 } 01121 01122 if (AM.Disp && (AM.Base.Reg.getNode() || AM.IndexReg.getNode())) 01123 Complexity++; 01124 01125 if (Complexity > 2) { 01126 getAddressOperands(AM, Base, Scale, Index, Disp); 01127 return true; 01128 } 01129 return false; 01130 } 01131 01132 bool X86DAGToDAGISel::TryFoldLoad(SDValue P, SDValue N, 01133 SDValue &Base, SDValue &Scale, 01134 SDValue &Index, SDValue &Disp) { 01135 if (ISD::isNON_EXTLoad(N.getNode()) && 01136 N.hasOneUse() && 01137 IsLegalAndProfitableToFold(N.getNode(), P.getNode(), P.getNode())) 01138 return SelectAddr(P, N.getOperand(1), Base, Scale, Index, Disp); 01139 return false; 01140 } 01141 01142 /// getGlobalBaseReg - Return an SDNode that returns the value of 01143 /// the global base register. Output instructions required to 01144 /// initialize the global base register, if necessary. 01145 /// 01146 SDNode *X86DAGToDAGISel::getGlobalBaseReg() { 01147 MachineFunction *MF = CurBB->getParent(); 01148 unsigned GlobalBaseReg = TM.getInstrInfo()->getGlobalBaseReg(MF); 01149 return CurDAG->getRegister(GlobalBaseReg, TLI.getPointerTy()).getNode(); 01150 } 01151 01152 static SDNode *FindCallStartFromCall(SDNode *Node) { 01153 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node; 01154 assert(Node->getOperand(0).getValueType() == MVT::Other && 01155 "Node doesn't have a token chain argument!"); 01156 return FindCallStartFromCall(Node->getOperand(0).getNode()); 01157 } 01158 01159 /// getTruncateTo8Bit - return an SDNode that implements a subreg based 01160 /// truncate of the specified operand to i8. This can be done with tablegen, 01161 /// except that this code uses MVT::Flag in a tricky way that happens to 01162 /// improve scheduling in some cases. 01163 SDNode *X86DAGToDAGISel::getTruncateTo8Bit(SDValue N0) { 01164 assert(!Subtarget->is64Bit() && 01165 "getTruncateTo8Bit is only needed on x86-32!"); 01166 SDValue SRIdx = CurDAG->getTargetConstant(1, MVT::i32); // SubRegSet 1 01167 01168 // Ensure that the source register has an 8-bit subreg on 32-bit targets 01169 unsigned Opc; 01170 MVT N0VT = N0.getValueType(); 01171 switch (N0VT.getSimpleVT()) { 01172 default: assert(0 && "Unknown truncate!"); 01173 case MVT::i16: 01174 Opc = X86::MOV16to16_; 01175 break; 01176 case MVT::i32: 01177 Opc = X86::MOV32to32_; 01178 break; 01179 } 01180 01181 // The use of MVT::Flag here is not strictly accurate, but it helps 01182 // scheduling in some cases. 01183 N0 = SDValue(CurDAG->getTargetNode(Opc, N0VT, MVT::Flag, N0), 0); 01184 return CurDAG->getTargetNode(X86::EXTRACT_SUBREG, 01185 MVT::i8, N0, SRIdx, N0.getValue(1)); 01186 } 01187 01188 SDNode *X86DAGToDAGISel::SelectAtomic64(SDNode *Node, unsigned Opc) { 01189 SDValue Chain = Node->getOperand(0); 01190 SDValue In1 = Node->getOperand(1); 01191 SDValue In2L = Node->getOperand(2); 01192 SDValue In2H = Node->getOperand(3); 01193 SDValue Tmp0, Tmp1, Tmp2, Tmp3; 01194 if (!SelectAddr(In1, In1, Tmp0, Tmp1, Tmp2, Tmp3)) 01195 return NULL; 01196 SDValue LSI = Node->getOperand(4); // MemOperand 01197 const SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, In2L, In2H, LSI, Chain }; 01198 return CurDAG->getTargetNode(Opc, MVT::i32, MVT::i32, MVT::Other, Ops, 8); 01199 } 01200 01201 SDNode *X86DAGToDAGISel::Select(SDValue N) { 01202 SDNode *Node = N.getNode(); 01203 MVT NVT = Node->getValueType(0); 01204 unsigned Opc, MOpc; 01205 unsigned Opcode = Node->getOpcode(); 01206 01207 #ifndef NDEBUG 01208 DOUT << std::string(Indent, ' ') << "Selecting: "; 01209 DEBUG(Node->dump(CurDAG)); 01210 DOUT << "\n"; 01211 Indent += 2; 01212 #endif 01213 01214 if (Node->isMachineOpcode()) { 01215 #ifndef NDEBUG 01216 DOUT << std::string(Indent-2, ' ') << "== "; 01217 DEBUG(Node->dump(CurDAG)); 01218 DOUT << "\n"; 01219 Indent -= 2; 01220 #endif 01221 return NULL; // Already selected. 01222 } 01223 01224 switch (Opcode) { 01225 default: break; 01226 case X86ISD::GlobalBaseReg: 01227 return getGlobalBaseReg(); 01228 01229 case X86ISD::ATOMOR64_DAG: 01230 return SelectAtomic64(Node, X86::ATOMOR6432); 01231 case X86ISD::ATOMXOR64_DAG: 01232 return SelectAtomic64(Node, X86::ATOMXOR6432); 01233 case X86ISD::ATOMADD64_DAG: 01234 return SelectAtomic64(Node, X86::ATOMADD6432); 01235 case X86ISD::ATOMSUB64_DAG: 01236 return SelectAtomic64(Node, X86::ATOMSUB6432); 01237 case X86ISD::ATOMNAND64_DAG: 01238 return SelectAtomic64(Node, X86::ATOMNAND6432); 01239 case X86ISD::ATOMAND64_DAG: 01240 return SelectAtomic64(Node, X86::ATOMAND6432); 01241 case X86ISD::ATOMSWAP64_DAG: 01242 return SelectAtomic64(Node, X86::ATOMSWAP6432); 01243 01244 case ISD::SMUL_LOHI: 01245 case ISD::UMUL_LOHI: { 01246 SDValue N0 = Node->getOperand(0); 01247 SDValue N1 = Node->getOperand(1); 01248 01249 bool isSigned = Opcode == ISD::SMUL_LOHI; 01250 if (!isSigned) 01251 switch (NVT.getSimpleVT()) { 01252 default: assert(0 && "Unsupported VT!"); 01253 case MVT::i8: Opc = X86::MUL8r; MOpc = X86::MUL8m; break; 01254 case MVT::i16: Opc = X86::MUL16r; MOpc = X86::MUL16m; break; 01255 case MVT::i32: Opc = X86::MUL32r; MOpc = X86::MUL32m; break; 01256 case MVT::i64: Opc = X86::MUL64r; MOpc = X86::MUL64m; break; 01257 } 01258 else 01259 switch (NVT.getSimpleVT()) { 01260 default: assert(0 && "Unsupported VT!"); 01261 case MVT::i8: Opc = X86::IMUL8r; MOpc = X86::IMUL8m; break; 01262 case MVT::i16: Opc = X86::IMUL16r; MOpc = X86::IMUL16m; break; 01263 case MVT::i32: Opc = X86::IMUL32r; MOpc = X86::IMUL32m; break; 01264 case MVT::i64: Opc = X86::IMUL64r; MOpc = X86::IMUL64m; break; 01265 } 01266 01267 unsigned LoReg, HiReg; 01268 switch (NVT.getSimpleVT()) { 01269 default: assert(0 && "Unsupported VT!"); 01270 case MVT::i8: LoReg = X86::AL; HiReg = X86::AH; break; 01271 case MVT::i16: LoReg = X86::AX; HiReg = X86::DX; break; 01272 case MVT::i32: LoReg = X86::EAX; HiReg = X86::EDX; break; 01273 case MVT::i64: LoReg = X86::RAX; HiReg = X86::RDX; break; 01274 } 01275 01276 SDValue Tmp0, Tmp1, Tmp2, Tmp3; 01277 bool foldedLoad = TryFoldLoad(N, N1, Tmp0, Tmp1, Tmp2, Tmp3); 01278 // multiplty is commmutative 01279 if (!foldedLoad) { 01280 foldedLoad = TryFoldLoad(N, N0, Tmp0, Tmp1, Tmp2, Tmp3); 01281 if (foldedLoad) 01282 std::swap(N0, N1); 01283 } 01284 01285 SDValue InFlag = CurDAG->getCopyToReg(CurDAG->getEntryNode(), LoReg, 01286 N0, SDValue()).getValue(1); 01287 01288 if (foldedLoad) { 01289 SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, N1.getOperand(0), InFlag }; 01290 SDNode *CNode = 01291 CurDAG->getTargetNode(MOpc, MVT::Other, MVT::Flag, Ops, 6); 01292 InFlag = SDValue(CNode, 1); 01293 // Update the chain. 01294 ReplaceUses(N1.getValue(1), SDValue(CNode, 0)); 01295 } else { 01296 InFlag = 01297 SDValue(CurDAG->getTargetNode(Opc, MVT::Flag, N1, InFlag), 0); 01298 } 01299 01300 // Copy the low half of the result, if it is needed. 01301 if (!N.getValue(0).use_empty()) { 01302 SDValue Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), 01303 LoReg, NVT, InFlag); 01304 InFlag = Result.getValue(2); 01305 ReplaceUses(N.getValue(0), Result); 01306 #ifndef NDEBUG 01307 DOUT << std::string(Indent-2, ' ') << "=> "; 01308 DEBUG(Result.getNode()->dump(CurDAG)); 01309 DOUT << "\n"; 01310 #endif 01311 } 01312 // Copy the high half of the result, if it is needed. 01313 if (!N.getValue(1).use_empty()) { 01314 SDValue Result; 01315 if (HiReg == X86::AH && Subtarget->is64Bit()) { 01316 // Prevent use of AH in a REX instruction by referencing AX instead. 01317 // Shift it down 8 bits. 01318 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), 01319 X86::AX, MVT::i16, InFlag); 01320 InFlag = Result.getValue(2); 01321 Result = SDValue(CurDAG->getTargetNode(X86::SHR16ri, MVT::i16, Result, 01322 CurDAG->getTargetConstant(8, MVT::i8)), 0); 01323 // Then truncate it down to i8. 01324 SDValue SRIdx = CurDAG->getTargetConstant(1, MVT::i32); // SubRegSet 1 01325 Result = SDValue(CurDAG->getTargetNode(X86::EXTRACT_SUBREG, 01326 MVT::i8, Result, SRIdx), 0); 01327 } else { 01328 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), 01329 HiReg, NVT, InFlag); 01330 InFlag = Result.getValue(2); 01331 } 01332 ReplaceUses(N.getValue(1), Result); 01333 #ifndef NDEBUG 01334 DOUT << std::string(Indent-2, ' ') << "=> "; 01335 DEBUG(Result.getNode()->dump(CurDAG)); 01336 DOUT << "\n"; 01337 #endif 01338 } 01339 01340 #ifndef NDEBUG 01341 Indent -= 2; 01342 #endif 01343 01344 return NULL; 01345 } 01346 01347 case ISD::SDIVREM: 01348 case ISD::UDIVREM: { 01349 SDValue N0 = Node->getOperand(0); 01350 SDValue N1 = Node->getOperand(1); 01351 01352 bool isSigned = Opcode == ISD::SDIVREM; 01353 if (!isSigned) 01354 switch (NVT.getSimpleVT()) { 01355 default: assert(0 && "Unsupported VT!"); 01356 case MVT::i8: Opc = X86::DIV8r; MOpc = X86::DIV8m; break; 01357 case MVT::i16: Opc = X86::DIV16r; MOpc = X86::DIV16m; break; 01358 case MVT::i32: Opc = X86::DIV32r; MOpc = X86::DIV32m; break; 01359 case MVT::i64: Opc = X86::DIV64r; MOpc = X86::DIV64m; break; 01360 } 01361 else 01362 switch (NVT.getSimpleVT()) { 01363 default: assert(0 && "Unsupported VT!"); 01364 case MVT::i8: Opc = X86::IDIV8r; MOpc = X86::IDIV8m; break; 01365 case MVT::i16: Opc = X86::IDIV16r; MOpc = X86::IDIV16m; break; 01366 case MVT::i32: Opc = X86::IDIV32r; MOpc = X86::IDIV32m; break; 01367 case MVT::i64: Opc = X86::IDIV64r; MOpc = X86::IDIV64m; break; 01368 } 01369 01370 unsigned LoReg, HiReg; 01371 unsigned ClrOpcode, SExtOpcode; 01372 switch (NVT.getSimpleVT()) { 01373 default: assert(0 && "Unsupported VT!"); 01374 case MVT::i8: 01375 LoReg = X86::AL; HiReg = X86::AH; 01376 ClrOpcode = 0; 01377 SExtOpcode = X86::CBW; 01378 break; 01379 case MVT::i16: 01380 LoReg = X86::AX; HiReg = X86::DX; 01381 ClrOpcode = X86::MOV16r0; 01382 SExtOpcode = X86::CWD; 01383 break; 01384 case MVT::i32: 01385 LoReg = X86::EAX; HiReg = X86::EDX; 01386 ClrOpcode = X86::MOV32r0; 01387 SExtOpcode = X86::CDQ; 01388 break; 01389 case MVT::i64: 01390 LoReg = X86::RAX; HiReg = X86::RDX; 01391 ClrOpcode = X86::MOV64r0; 01392 SExtOpcode = X86::CQO; 01393 break; 01394 } 01395 01396 SDValue Tmp0, Tmp1, Tmp2, Tmp3; 01397 bool foldedLoad = TryFoldLoad(N, N1, Tmp0, Tmp1, Tmp2, Tmp3); 01398 bool signBitIsZero = CurDAG->SignBitIsZero(N0); 01399 01400 SDValue InFlag; 01401 if (NVT == MVT::i8 && (!isSigned || signBitIsZero)) { 01402 // Special case for div8, just use a move with zero extension to AX to 01403 // clear the upper 8 bits (AH). 01404 SDValue Tmp0, Tmp1, Tmp2, Tmp3, Move, Chain; 01405 if (TryFoldLoad(N, N0, Tmp0, Tmp1, Tmp2, Tmp3)) { 01406 SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, N0.getOperand(0) }; 01407 Move = 01408 SDValue(CurDAG->getTargetNode(X86::MOVZX16rm8, MVT::i16, MVT::Other, 01409 Ops, 5), 0); 01410 Chain = Move.getValue(1); 01411 ReplaceUses(N0.getValue(1), Chain); 01412 } else { 01413 Move = 01414 SDValue(CurDAG->getTargetNode(X86::MOVZX16rr8, MVT::i16, N0), 0); 01415 Chain = CurDAG->getEntryNode(); 01416 } 01417 Chain = CurDAG->getCopyToReg(Chain, X86::AX, Move, SDValue()); 01418 InFlag = Chain.getValue(1); 01419 } else { 01420 InFlag = 01421 CurDAG->getCopyToReg(CurDAG->getEntryNode(), 01422 LoReg, N0, SDValue()).getValue(1); 01423 if (isSigned && !signBitIsZero) { 01424 // Sign extend the low part into the high part. 01425 InFlag = 01426 SDValue(CurDAG->getTargetNode(SExtOpcode, MVT::Flag, InFlag), 0); 01427 } else { 01428 // Zero out the high part, effectively zero extending the input. 01429 SDValue ClrNode = SDValue(CurDAG->getTargetNode(ClrOpcode, NVT), 0); 01430 InFlag = CurDAG->getCopyToReg(CurDAG->getEntryNode(), HiReg, 01431 ClrNode, InFlag).getValue(1); 01432 } 01433 } 01434 01435 if (foldedLoad) { 01436 SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, N1.getOperand(0), InFlag }; 01437 SDNode *CNode = 01438 CurDAG->getTargetNode(MOpc, MVT::Other, MVT::Flag, Ops, 6); 01439 InFlag = SDValue(CNode, 1); 01440 // Update the chain. 01441 ReplaceUses(N1.getValue(1), SDValue(CNode, 0)); 01442 } else { 01443 InFlag = 01444 SDValue(CurDAG->getTargetNode(Opc, MVT::Flag, N1, InFlag), 0); 01445 } 01446 01447 // Copy the division (low) result, if it is needed. 01448 if (!N.getValue(0).use_empty()) { 01449 SDValue Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), 01450 LoReg, NVT, InFlag); 01451 InFlag = Result.getValue(2); 01452 ReplaceUses(N.getValue(0), Result); 01453 #ifndef NDEBUG 01454 DOUT << std::string(Indent-2, ' ') << "=> "; 01455 DEBUG(Result.getNode()->dump(CurDAG)); 01456 DOUT << "\n"; 01457 #endif 01458 } 01459 // Copy the remainder (high) result, if it is needed. 01460 if (!N.getValue(1).use_empty()) { 01461 SDValue Result; 01462 if (HiReg == X86::AH && Subtarget->is64Bit()) { 01463 // Prevent use of AH in a REX instruction by referencing AX instead. 01464 // Shift it down 8 bits. 01465 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), 01466 X86::AX, MVT::i16, InFlag); 01467 InFlag = Result.getValue(2); 01468 Result = SDValue(CurDAG->getTargetNode(X86::SHR16ri, MVT::i16, Result, 01469 CurDAG->getTargetConstant(8, MVT::i8)), 0); 01470 // Then truncate it down to i8. 01471 SDValue SRIdx = CurDAG->getTargetConstant(1, MVT::i32); // SubRegSet 1 01472 Result = SDValue(CurDAG->getTargetNode(X86::EXTRACT_SUBREG, 01473 MVT::i8, Result, SRIdx), 0); 01474 } else { 01475 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), 01476 HiReg, NVT, InFlag); 01477 InFlag = Result.getValue(2); 01478 } 01479 ReplaceUses(N.getValue(1), Result); 01480 #ifndef NDEBUG 01481 DOUT << std::string(Indent-2, ' ') << "=> "; 01482 DEBUG(Result.getNode()->dump(CurDAG)); 01483 DOUT << "\n"; 01484 #endif 01485 } 01486 01487 #ifndef NDEBUG 01488 Indent -= 2; 01489 #endif 01490 01491 return NULL; 01492 } 01493 01494 case ISD::SIGN_EXTEND_INREG: { 01495 MVT SVT = cast<VTSDNode>(Node->getOperand(1))->getVT(); 01496 if (SVT == MVT::i8 && !Subtarget->is64Bit()) { 01497 SDValue N0 = Node->getOperand(0); 01498 01499 SDValue TruncOp = SDValue(getTruncateTo8Bit(N0), 0); 01500 unsigned Opc = 0; 01501 switch (NVT.getSimpleVT()) { 01502 default: assert(0 && "Unknown sign_extend_inreg!"); 01503 case MVT::i16: 01504 Opc = X86::MOVSX16rr8; 01505 break; 01506 case MVT::i32: 01507 Opc = X86::MOVSX32rr8; 01508 break; 01509 } 01510 01511 SDNode *ResNode = CurDAG->getTargetNode(Opc, NVT, TruncOp); 01512 01513 #ifndef NDEBUG 01514 DOUT << std::string(Indent-2, ' ') << "=> "; 01515 DEBUG(TruncOp.getNode()->dump(CurDAG)); 01516 DOUT << "\n"; 01517 DOUT << std::string(Indent-2, ' ') << "=> "; 01518 DEBUG(ResNode->dump(CurDAG)); 01519 DOUT << "\n"; 01520 Indent -= 2; 01521 #endif 01522 return ResNode; 01523 } 01524 break; 01525 } 01526 01527 case ISD::TRUNCATE: { 01528 if (NVT == MVT::i8 && !Subtarget->is64Bit()) { 01529 SDValue Input = Node->getOperand(0); 01530 SDNode *ResNode = getTruncateTo8Bit(Input); 01531 01532 #ifndef NDEBUG 01533 DOUT << std::string(Indent-2, ' ') << "=> "; 01534 DEBUG(ResNode->dump(CurDAG)); 01535 DOUT << "\n"; 01536 Indent -= 2; 01537 #endif 01538 return ResNode; 01539 } 01540 break; 01541 } 01542 01543 case ISD::DECLARE: { 01544 // Handle DECLARE nodes here because the second operand may have been 01545 // wrapped in X86ISD::Wrapper. 01546 SDValue Chain = Node->getOperand(0); 01547 SDValue N1 = Node->getOperand(1); 01548 SDValue N2 = Node->getOperand(2); 01549 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(N1); 01550 if (!FINode) 01551 break; 01552 if (N2.getOpcode() == ISD::ADD && 01553 N2.getOperand(0).getOpcode() == X86ISD::GlobalBaseReg) 01554 N2 = N2.getOperand(1); 01555 if (N2.getOpcode() != X86ISD::Wrapper) 01556 break; 01557 GlobalAddressSDNode *GVNode = 01558 dyn_cast<GlobalAddressSDNode>(N2.getOperand(0)); 01559 if (!GVNode) 01560 break; 01561 SDValue Tmp1 = CurDAG->getTargetFrameIndex(FINode->getIndex(), 01562 TLI.getPointerTy()); 01563 SDValue Tmp2 = CurDAG->getTargetGlobalAddress(GVNode->getGlobal(), 01564 TLI.getPointerTy()); 01565 SDValue Ops[] = { Tmp1, Tmp2, Chain }; 01566 return CurDAG->getTargetNode(TargetInstrInfo::DECLARE, 01567 MVT::Other, Ops, 3); 01568 break; 01569 } 01570 } 01571 01572 SDNode *ResNode = SelectCode(N); 01573 01574 #ifndef NDEBUG 01575 DOUT << std::string(Indent-2, ' ') << "=> "; 01576 if (ResNode == NULL || ResNode == N.getNode()) 01577 DEBUG(N.getNode()->dump(CurDAG)); 01578 else 01579 DEBUG(ResNode->dump(CurDAG)); 01580 DOUT << "\n"; 01581 Indent -= 2; 01582 #endif 01583 01584 return ResNode; 01585 } 01586 01587 bool X86DAGToDAGISel:: 01588 SelectInlineAsmMemoryOperand(const SDValue &Op, char ConstraintCode, 01589 std::vector<SDValue> &OutOps) { 01590 SDValue Op0, Op1, Op2, Op3; 01591 switch (ConstraintCode) { 01592 case 'o': // offsetable ?? 01593 case 'v': // not offsetable ?? 01594 default: return true; 01595 case 'm': // memory 01596 if (!SelectAddr(Op, Op, Op0, Op1, Op2, Op3)) 01597 return true; 01598 break; 01599 } 01600 01601 OutOps.push_back(Op0); 01602 OutOps.push_back(Op1); 01603 OutOps.push_back(Op2); 01604 OutOps.push_back(Op3); 01605 return false; 01606 } 01607 01608 /// createX86ISelDag - This pass converts a legalized DAG into a 01609 /// X86-specific DAG, ready for instruction scheduling. 01610 /// 01611 FunctionPass *llvm::createX86ISelDag(X86TargetMachine &TM, bool Fast) { 01612 return new X86DAGToDAGISel(TM, Fast); 01613 }