Chris Lattner | 7a12537 | 2005-11-16 22:59:19 +0000 | [diff] [blame] | 1 | //===- X86ISelDAGToDAG.cpp - A DAG pattern matching inst selector for X86 -===// |
Chris Lattner | c961eea | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by the Evan Cheng and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file defines a DAG pattern matching instruction selector for X86, |
| 11 | // converting from a legalized dag to a X86 dag. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Evan Cheng | 2ef88a0 | 2006-08-07 22:28:20 +0000 | [diff] [blame] | 15 | #define DEBUG_TYPE "x86-isel" |
Chris Lattner | c961eea | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 16 | #include "X86.h" |
Evan Cheng | 8700e14 | 2006-01-11 06:09:51 +0000 | [diff] [blame] | 17 | #include "X86InstrBuilder.h" |
Evan Cheng | c4c6257 | 2006-03-13 23:20:37 +0000 | [diff] [blame] | 18 | #include "X86ISelLowering.h" |
Chris Lattner | 92cb0af | 2006-01-11 01:15:34 +0000 | [diff] [blame] | 19 | #include "X86RegisterInfo.h" |
Chris Lattner | c961eea | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 20 | #include "X86Subtarget.h" |
Evan Cheng | c4c6257 | 2006-03-13 23:20:37 +0000 | [diff] [blame] | 21 | #include "X86TargetMachine.h" |
Chris Lattner | f9ce9fb | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 22 | #include "llvm/GlobalValue.h" |
Chris Lattner | 92cb0af | 2006-01-11 01:15:34 +0000 | [diff] [blame] | 23 | #include "llvm/Instructions.h" |
Chris Lattner | 420736d | 2006-03-25 06:47:10 +0000 | [diff] [blame] | 24 | #include "llvm/Intrinsics.h" |
Chris Lattner | 92cb0af | 2006-01-11 01:15:34 +0000 | [diff] [blame] | 25 | #include "llvm/Support/CFG.h" |
Chris Lattner | f9ce9fb | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 26 | #include "llvm/CodeGen/MachineConstantPool.h" |
Chris Lattner | c961eea | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 27 | #include "llvm/CodeGen/MachineFunction.h" |
Evan Cheng | aaca22c | 2006-01-10 20:26:56 +0000 | [diff] [blame] | 28 | #include "llvm/CodeGen/MachineFrameInfo.h" |
Chris Lattner | 92cb0af | 2006-01-11 01:15:34 +0000 | [diff] [blame] | 29 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
| 30 | #include "llvm/CodeGen/SSARegMap.h" |
Chris Lattner | c961eea | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 31 | #include "llvm/CodeGen/SelectionDAGISel.h" |
| 32 | #include "llvm/Target/TargetMachine.h" |
Chris Lattner | a4f0b3a | 2006-08-27 12:54:02 +0000 | [diff] [blame] | 33 | #include "llvm/Support/Compiler.h" |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 34 | #include "llvm/Support/Debug.h" |
| 35 | #include "llvm/Support/MathExtras.h" |
Chris Lattner | c961eea | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 36 | #include "llvm/ADT/Statistic.h" |
Chris Lattner | 2c2c6c6 | 2006-01-22 23:41:00 +0000 | [diff] [blame] | 37 | #include <iostream> |
Evan Cheng | 2ef88a0 | 2006-08-07 22:28:20 +0000 | [diff] [blame] | 38 | #include <queue> |
Evan Cheng | ba2f0a9 | 2006-02-05 06:46:41 +0000 | [diff] [blame] | 39 | #include <set> |
Chris Lattner | c961eea | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 40 | using namespace llvm; |
| 41 | |
| 42 | //===----------------------------------------------------------------------===// |
| 43 | // Pattern Matcher Implementation |
| 44 | //===----------------------------------------------------------------------===// |
| 45 | |
| 46 | namespace { |
Chris Lattner | f9ce9fb | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 47 | /// X86ISelAddressMode - This corresponds to X86AddressMode, but uses |
| 48 | /// SDOperand's instead of register numbers for the leaves of the matched |
| 49 | /// tree. |
| 50 | struct X86ISelAddressMode { |
| 51 | enum { |
| 52 | RegBase, |
Chris Lattner | d74ea2b | 2006-05-24 17:04:05 +0000 | [diff] [blame] | 53 | FrameIndexBase |
Chris Lattner | f9ce9fb | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 54 | } BaseType; |
| 55 | |
| 56 | struct { // This is really a union, discriminated by BaseType! |
| 57 | SDOperand Reg; |
| 58 | int FrameIndex; |
| 59 | } Base; |
| 60 | |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 61 | bool isRIPRel; // RIP relative? |
Chris Lattner | f9ce9fb | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 62 | unsigned Scale; |
| 63 | SDOperand IndexReg; |
| 64 | unsigned Disp; |
| 65 | GlobalValue *GV; |
Evan Cheng | 51a9ed9 | 2006-02-25 10:09:08 +0000 | [diff] [blame] | 66 | Constant *CP; |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 67 | const char *ES; |
| 68 | int JT; |
Evan Cheng | 51a9ed9 | 2006-02-25 10:09:08 +0000 | [diff] [blame] | 69 | unsigned Align; // CP alignment. |
Chris Lattner | f9ce9fb | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 70 | |
| 71 | X86ISelAddressMode() |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 72 | : BaseType(RegBase), isRIPRel(false), Scale(1), IndexReg(), Disp(0), |
| 73 | GV(0), CP(0), ES(0), JT(-1), Align(0) { |
Chris Lattner | f9ce9fb | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 74 | } |
| 75 | }; |
| 76 | } |
| 77 | |
| 78 | namespace { |
Chris Lattner | c961eea | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 79 | Statistic<> |
| 80 | NumFPKill("x86-codegen", "Number of FP_REG_KILL instructions added"); |
| 81 | |
Evan Cheng | 82a35b3 | 2006-08-29 06:44:17 +0000 | [diff] [blame] | 82 | Statistic<> |
| 83 | NumLoadMoved("x86-codegen", "Number of loads moved below TokenFactor"); |
| 84 | |
Chris Lattner | c961eea | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 85 | //===--------------------------------------------------------------------===// |
| 86 | /// ISel - X86 specific code to select X86 machine instructions for |
| 87 | /// SelectionDAG operations. |
| 88 | /// |
Chris Lattner | 2c79de8 | 2006-06-28 23:27:49 +0000 | [diff] [blame] | 89 | class VISIBILITY_HIDDEN X86DAGToDAGISel : public SelectionDAGISel { |
Chris Lattner | c961eea | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 90 | /// ContainsFPCode - Every instruction we select that uses or defines a FP |
| 91 | /// register should set this to true. |
| 92 | bool ContainsFPCode; |
| 93 | |
Evan Cheng | e50794a | 2006-08-29 18:28:33 +0000 | [diff] [blame] | 94 | /// FastISel - Enable fast(er) instruction selection. |
| 95 | /// |
| 96 | bool FastISel; |
| 97 | |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 98 | /// TM - Keep a reference to X86TargetMachine. |
| 99 | /// |
| 100 | X86TargetMachine &TM; |
| 101 | |
Chris Lattner | c961eea | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 102 | /// X86Lowering - This object fully describes how to lower LLVM code to an |
| 103 | /// X86-specific SelectionDAG. |
| 104 | X86TargetLowering X86Lowering; |
| 105 | |
| 106 | /// Subtarget - Keep a pointer to the X86Subtarget around so that we can |
| 107 | /// make the right decision when generating code for different targets. |
| 108 | const X86Subtarget *Subtarget; |
Evan Cheng | 7ccced6 | 2006-02-18 00:15:05 +0000 | [diff] [blame] | 109 | |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 110 | /// GlobalBaseReg - keeps track of the virtual register mapped onto global |
| 111 | /// base register. |
Evan Cheng | 7ccced6 | 2006-02-18 00:15:05 +0000 | [diff] [blame] | 112 | unsigned GlobalBaseReg; |
Evan Cheng | a8df1b4 | 2006-07-27 16:44:36 +0000 | [diff] [blame] | 113 | |
Chris Lattner | c961eea | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 114 | public: |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 115 | X86DAGToDAGISel(X86TargetMachine &tm, bool fast) |
Evan Cheng | c4c6257 | 2006-03-13 23:20:37 +0000 | [diff] [blame] | 116 | : SelectionDAGISel(X86Lowering), |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 117 | ContainsFPCode(false), FastISel(fast), TM(tm), |
Evan Cheng | a8df1b4 | 2006-07-27 16:44:36 +0000 | [diff] [blame] | 118 | X86Lowering(*TM.getTargetLowering()), |
Evan Cheng | f4b4c41 | 2006-08-08 00:31:00 +0000 | [diff] [blame] | 119 | Subtarget(&TM.getSubtarget<X86Subtarget>()) {} |
Chris Lattner | c961eea | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 120 | |
Evan Cheng | 7ccced6 | 2006-02-18 00:15:05 +0000 | [diff] [blame] | 121 | virtual bool runOnFunction(Function &Fn) { |
| 122 | // Make sure we re-emit a set of the global base reg if necessary |
| 123 | GlobalBaseReg = 0; |
| 124 | return SelectionDAGISel::runOnFunction(Fn); |
| 125 | } |
| 126 | |
Chris Lattner | c961eea | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 127 | virtual const char *getPassName() const { |
| 128 | return "X86 DAG->DAG Instruction Selection"; |
| 129 | } |
| 130 | |
| 131 | /// InstructionSelectBasicBlock - This callback is invoked by |
| 132 | /// SelectionDAGISel when it has created a SelectionDAG for us to codegen. |
| 133 | virtual void InstructionSelectBasicBlock(SelectionDAG &DAG); |
| 134 | |
Evan Cheng | 8700e14 | 2006-01-11 06:09:51 +0000 | [diff] [blame] | 135 | virtual void EmitFunctionEntryCode(Function &Fn, MachineFunction &MF); |
| 136 | |
Evan Cheng | 27e1fe9 | 2006-10-14 08:33:25 +0000 | [diff] [blame] | 137 | virtual bool CanBeFoldedBy(SDNode *N, SDNode *U, SDNode *Root); |
Evan Cheng | a8df1b4 | 2006-07-27 16:44:36 +0000 | [diff] [blame] | 138 | |
Chris Lattner | c961eea | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 139 | // Include the pieces autogenerated from the target description. |
| 140 | #include "X86GenDAGISel.inc" |
| 141 | |
| 142 | private: |
Evan Cheng | 9ade218 | 2006-08-26 05:34:46 +0000 | [diff] [blame] | 143 | SDNode *Select(SDOperand N); |
Chris Lattner | c961eea | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 144 | |
Evan Cheng | 2486af1 | 2006-02-11 02:05:36 +0000 | [diff] [blame] | 145 | bool MatchAddress(SDOperand N, X86ISelAddressMode &AM, bool isRoot = true); |
Evan Cheng | 0d53826 | 2006-11-08 20:34:28 +0000 | [diff] [blame] | 146 | bool SelectAddr(SDOperand Op, SDOperand N, SDOperand &Base, |
| 147 | SDOperand &Scale, SDOperand &Index, SDOperand &Disp); |
| 148 | bool SelectLEAAddr(SDOperand Op, SDOperand N, SDOperand &Base, |
| 149 | SDOperand &Scale, SDOperand &Index, SDOperand &Disp); |
| 150 | bool SelectScalarSSELoad(SDOperand Op, SDOperand Pred, |
Evan Cheng | 07e4b00 | 2006-10-16 06:34:55 +0000 | [diff] [blame] | 151 | SDOperand N, SDOperand &Base, SDOperand &Scale, |
Evan Cheng | 82a9164 | 2006-10-11 21:06:01 +0000 | [diff] [blame] | 152 | SDOperand &Index, SDOperand &Disp, |
| 153 | SDOperand &InChain, SDOperand &OutChain); |
Evan Cheng | 5e35168 | 2006-02-06 06:02:33 +0000 | [diff] [blame] | 154 | bool TryFoldLoad(SDOperand P, SDOperand N, |
| 155 | SDOperand &Base, SDOperand &Scale, |
Evan Cheng | 0114e94 | 2006-01-06 20:36:21 +0000 | [diff] [blame] | 156 | SDOperand &Index, SDOperand &Disp); |
Evan Cheng | 70e674e | 2006-08-28 20:10:17 +0000 | [diff] [blame] | 157 | void InstructionSelectPreprocess(SelectionDAG &DAG); |
Evan Cheng | 2ef88a0 | 2006-08-07 22:28:20 +0000 | [diff] [blame] | 158 | |
Chris Lattner | c0bad57 | 2006-06-08 18:03:49 +0000 | [diff] [blame] | 159 | /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for |
| 160 | /// inline asm expressions. |
| 161 | virtual bool SelectInlineAsmMemoryOperand(const SDOperand &Op, |
| 162 | char ConstraintCode, |
| 163 | std::vector<SDOperand> &OutOps, |
| 164 | SelectionDAG &DAG); |
| 165 | |
Evan Cheng | 3649b0e | 2006-06-02 22:38:37 +0000 | [diff] [blame] | 166 | void EmitSpecialCodeForMain(MachineBasicBlock *BB, MachineFrameInfo *MFI); |
| 167 | |
Evan Cheng | e528053 | 2005-12-12 21:49:40 +0000 | [diff] [blame] | 168 | inline void getAddressOperands(X86ISelAddressMode &AM, SDOperand &Base, |
| 169 | SDOperand &Scale, SDOperand &Index, |
| 170 | SDOperand &Disp) { |
| 171 | Base = (AM.BaseType == X86ISelAddressMode::FrameIndexBase) ? |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 172 | CurDAG->getTargetFrameIndex(AM.Base.FrameIndex, TLI.getPointerTy()) : |
| 173 | AM.Base.Reg; |
Evan Cheng | bdce7b4 | 2005-12-17 09:13:43 +0000 | [diff] [blame] | 174 | Scale = getI8Imm(AM.Scale); |
Evan Cheng | e528053 | 2005-12-12 21:49:40 +0000 | [diff] [blame] | 175 | Index = AM.IndexReg; |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 176 | // These are 32-bit even in 64-bit mode since RIP relative offset |
| 177 | // is 32-bit. |
| 178 | if (AM.GV) |
| 179 | Disp = CurDAG->getTargetGlobalAddress(AM.GV, MVT::i32, AM.Disp); |
| 180 | else if (AM.CP) |
| 181 | Disp = CurDAG->getTargetConstantPool(AM.CP, MVT::i32, AM.Align, AM.Disp); |
| 182 | else if (AM.ES) |
| 183 | Disp = CurDAG->getTargetExternalSymbol(AM.ES, MVT::i32); |
| 184 | else if (AM.JT != -1) |
| 185 | Disp = CurDAG->getTargetJumpTable(AM.JT, MVT::i32); |
| 186 | else |
| 187 | Disp = getI32Imm(AM.Disp); |
Evan Cheng | e528053 | 2005-12-12 21:49:40 +0000 | [diff] [blame] | 188 | } |
| 189 | |
Chris Lattner | f9ce9fb | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 190 | /// getI8Imm - Return a target constant with the specified value, of type |
| 191 | /// i8. |
| 192 | inline SDOperand getI8Imm(unsigned Imm) { |
| 193 | return CurDAG->getTargetConstant(Imm, MVT::i8); |
| 194 | } |
| 195 | |
Chris Lattner | c961eea | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 196 | /// getI16Imm - Return a target constant with the specified value, of type |
| 197 | /// i16. |
| 198 | inline SDOperand getI16Imm(unsigned Imm) { |
| 199 | return CurDAG->getTargetConstant(Imm, MVT::i16); |
| 200 | } |
| 201 | |
| 202 | /// getI32Imm - Return a target constant with the specified value, of type |
| 203 | /// i32. |
| 204 | inline SDOperand getI32Imm(unsigned Imm) { |
| 205 | return CurDAG->getTargetConstant(Imm, MVT::i32); |
| 206 | } |
Evan Cheng | f597dc7 | 2006-02-10 22:24:32 +0000 | [diff] [blame] | 207 | |
Evan Cheng | 7ccced6 | 2006-02-18 00:15:05 +0000 | [diff] [blame] | 208 | /// getGlobalBaseReg - insert code into the entry mbb to materialize the PIC |
| 209 | /// base register. Return the virtual register that holds this value. |
Evan Cheng | 9ade218 | 2006-08-26 05:34:46 +0000 | [diff] [blame] | 210 | SDNode *getGlobalBaseReg(); |
Evan Cheng | 7ccced6 | 2006-02-18 00:15:05 +0000 | [diff] [blame] | 211 | |
Evan Cheng | 23addc0 | 2006-02-10 22:46:26 +0000 | [diff] [blame] | 212 | #ifndef NDEBUG |
| 213 | unsigned Indent; |
| 214 | #endif |
Chris Lattner | c961eea | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 215 | }; |
| 216 | } |
| 217 | |
Evan Cheng | a275ecb | 2006-10-10 01:46:56 +0000 | [diff] [blame] | 218 | static SDNode *findFlagUse(SDNode *N) { |
| 219 | unsigned FlagResNo = N->getNumValues()-1; |
| 220 | for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) { |
| 221 | SDNode *User = *I; |
| 222 | for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i) { |
| 223 | SDOperand Op = User->getOperand(i); |
Evan Cheng | 494cec6 | 2006-10-12 19:13:59 +0000 | [diff] [blame] | 224 | if (Op.Val == N && Op.ResNo == FlagResNo) |
Evan Cheng | a275ecb | 2006-10-10 01:46:56 +0000 | [diff] [blame] | 225 | return User; |
| 226 | } |
| 227 | } |
| 228 | return NULL; |
| 229 | } |
| 230 | |
Evan Cheng | 27e1fe9 | 2006-10-14 08:33:25 +0000 | [diff] [blame] | 231 | static void findNonImmUse(SDNode *Use, SDNode* Def, SDNode *ImmedUse, |
| 232 | SDNode *Root, SDNode *Skip, bool &found, |
Evan Cheng | f4b4c41 | 2006-08-08 00:31:00 +0000 | [diff] [blame] | 233 | std::set<SDNode *> &Visited) { |
| 234 | if (found || |
| 235 | Use->getNodeId() > Def->getNodeId() || |
| 236 | !Visited.insert(Use).second) |
| 237 | return; |
| 238 | |
Evan Cheng | 27e1fe9 | 2006-10-14 08:33:25 +0000 | [diff] [blame] | 239 | for (unsigned i = 0, e = Use->getNumOperands(); !found && i != e; ++i) { |
Evan Cheng | f4b4c41 | 2006-08-08 00:31:00 +0000 | [diff] [blame] | 240 | SDNode *N = Use->getOperand(i).Val; |
Evan Cheng | 27e1fe9 | 2006-10-14 08:33:25 +0000 | [diff] [blame] | 241 | if (N == Skip) |
Evan Cheng | a275ecb | 2006-10-10 01:46:56 +0000 | [diff] [blame] | 242 | continue; |
Evan Cheng | 27e1fe9 | 2006-10-14 08:33:25 +0000 | [diff] [blame] | 243 | if (N == Def) { |
| 244 | if (Use == ImmedUse) |
| 245 | continue; // Immediate use is ok. |
| 246 | if (Use == Root) { |
| 247 | assert(Use->getOpcode() == ISD::STORE || |
| 248 | Use->getOpcode() == X86ISD::CMP); |
| 249 | continue; |
| 250 | } |
Evan Cheng | f4b4c41 | 2006-08-08 00:31:00 +0000 | [diff] [blame] | 251 | found = true; |
| 252 | break; |
| 253 | } |
Evan Cheng | 27e1fe9 | 2006-10-14 08:33:25 +0000 | [diff] [blame] | 254 | findNonImmUse(N, Def, ImmedUse, Root, Skip, found, Visited); |
Evan Cheng | f4b4c41 | 2006-08-08 00:31:00 +0000 | [diff] [blame] | 255 | } |
| 256 | } |
| 257 | |
Evan Cheng | 27e1fe9 | 2006-10-14 08:33:25 +0000 | [diff] [blame] | 258 | /// isNonImmUse - Start searching from Root up the DAG to check is Def can |
| 259 | /// be reached. Return true if that's the case. However, ignore direct uses |
| 260 | /// by ImmedUse (which would be U in the example illustrated in |
| 261 | /// CanBeFoldedBy) and by Root (which can happen in the store case). |
| 262 | /// FIXME: to be really generic, we should allow direct use by any node |
| 263 | /// that is being folded. But realisticly since we only fold loads which |
| 264 | /// have one non-chain use, we only need to watch out for load/op/store |
| 265 | /// and load/op/cmp case where the root (store / cmp) may reach the load via |
| 266 | /// its chain operand. |
| 267 | static inline bool isNonImmUse(SDNode *Root, SDNode *Def, SDNode *ImmedUse, |
| 268 | SDNode *Skip = NULL) { |
Evan Cheng | f4b4c41 | 2006-08-08 00:31:00 +0000 | [diff] [blame] | 269 | std::set<SDNode *> Visited; |
| 270 | bool found = false; |
Evan Cheng | 27e1fe9 | 2006-10-14 08:33:25 +0000 | [diff] [blame] | 271 | findNonImmUse(Root, Def, ImmedUse, Root, Skip, found, Visited); |
Evan Cheng | f4b4c41 | 2006-08-08 00:31:00 +0000 | [diff] [blame] | 272 | return found; |
| 273 | } |
| 274 | |
| 275 | |
Evan Cheng | 27e1fe9 | 2006-10-14 08:33:25 +0000 | [diff] [blame] | 276 | bool X86DAGToDAGISel::CanBeFoldedBy(SDNode *N, SDNode *U, SDNode *Root) { |
| 277 | if (FastISel) return false; |
| 278 | |
Evan Cheng | a8df1b4 | 2006-07-27 16:44:36 +0000 | [diff] [blame] | 279 | // If U use can somehow reach N through another path then U can't fold N or |
| 280 | // it will create a cycle. e.g. In the following diagram, U can reach N |
Evan Cheng | 37e1803 | 2006-07-28 06:33:41 +0000 | [diff] [blame] | 281 | // through X. If N is folded into into U, then X is both a predecessor and |
Evan Cheng | a8df1b4 | 2006-07-27 16:44:36 +0000 | [diff] [blame] | 282 | // a successor of U. |
| 283 | // |
| 284 | // [ N ] |
| 285 | // ^ ^ |
| 286 | // | | |
| 287 | // / \--- |
| 288 | // / [X] |
| 289 | // | ^ |
| 290 | // [U]--------| |
Evan Cheng | 27e1fe9 | 2006-10-14 08:33:25 +0000 | [diff] [blame] | 291 | |
| 292 | if (isNonImmUse(Root, N, U)) |
| 293 | return false; |
| 294 | |
| 295 | // If U produces a flag, then it gets (even more) interesting. Since it |
| 296 | // would have been "glued" together with its flag use, we need to check if |
| 297 | // it might reach N: |
| 298 | // |
| 299 | // [ N ] |
| 300 | // ^ ^ |
| 301 | // | | |
| 302 | // [U] \-- |
| 303 | // ^ [TF] |
| 304 | // | ^ |
| 305 | // | | |
| 306 | // \ / |
| 307 | // [FU] |
| 308 | // |
| 309 | // If FU (flag use) indirectly reach N (the load), and U fold N (call it |
| 310 | // NU), then TF is a predecessor of FU and a successor of NU. But since |
| 311 | // NU and FU are flagged together, this effectively creates a cycle. |
| 312 | bool HasFlagUse = false; |
| 313 | MVT::ValueType VT = Root->getValueType(Root->getNumValues()-1); |
| 314 | while ((VT == MVT::Flag && !Root->use_empty())) { |
| 315 | SDNode *FU = findFlagUse(Root); |
| 316 | if (FU == NULL) |
| 317 | break; |
| 318 | else { |
| 319 | Root = FU; |
| 320 | HasFlagUse = true; |
Evan Cheng | a275ecb | 2006-10-10 01:46:56 +0000 | [diff] [blame] | 321 | } |
Evan Cheng | 27e1fe9 | 2006-10-14 08:33:25 +0000 | [diff] [blame] | 322 | VT = Root->getValueType(Root->getNumValues()-1); |
Evan Cheng | a275ecb | 2006-10-10 01:46:56 +0000 | [diff] [blame] | 323 | } |
Evan Cheng | 27e1fe9 | 2006-10-14 08:33:25 +0000 | [diff] [blame] | 324 | |
| 325 | if (HasFlagUse) |
| 326 | return !isNonImmUse(Root, N, Root, U); |
| 327 | return true; |
Evan Cheng | a8df1b4 | 2006-07-27 16:44:36 +0000 | [diff] [blame] | 328 | } |
| 329 | |
Evan Cheng | 70e674e | 2006-08-28 20:10:17 +0000 | [diff] [blame] | 330 | /// MoveBelowTokenFactor - Replace TokenFactor operand with load's chain operand |
| 331 | /// and move load below the TokenFactor. Replace store's chain operand with |
| 332 | /// load's chain result. |
| 333 | static void MoveBelowTokenFactor(SelectionDAG &DAG, SDOperand Load, |
| 334 | SDOperand Store, SDOperand TF) { |
| 335 | std::vector<SDOperand> Ops; |
| 336 | for (unsigned i = 0, e = TF.Val->getNumOperands(); i != e; ++i) |
| 337 | if (Load.Val == TF.Val->getOperand(i).Val) |
| 338 | Ops.push_back(Load.Val->getOperand(0)); |
| 339 | else |
| 340 | Ops.push_back(TF.Val->getOperand(i)); |
| 341 | DAG.UpdateNodeOperands(TF, &Ops[0], Ops.size()); |
| 342 | DAG.UpdateNodeOperands(Load, TF, Load.getOperand(1), Load.getOperand(2)); |
| 343 | DAG.UpdateNodeOperands(Store, Load.getValue(1), Store.getOperand(1), |
| 344 | Store.getOperand(2), Store.getOperand(3)); |
| 345 | } |
| 346 | |
| 347 | /// InstructionSelectPreprocess - Preprocess the DAG to allow the instruction |
| 348 | /// selector to pick more load-modify-store instructions. This is a common |
| 349 | /// case: |
| 350 | /// |
| 351 | /// [Load chain] |
| 352 | /// ^ |
| 353 | /// | |
| 354 | /// [Load] |
| 355 | /// ^ ^ |
| 356 | /// | | |
| 357 | /// / \- |
| 358 | /// / | |
| 359 | /// [TokenFactor] [Op] |
| 360 | /// ^ ^ |
| 361 | /// | | |
| 362 | /// \ / |
| 363 | /// \ / |
| 364 | /// [Store] |
| 365 | /// |
| 366 | /// The fact the store's chain operand != load's chain will prevent the |
| 367 | /// (store (op (load))) instruction from being selected. We can transform it to: |
| 368 | /// |
| 369 | /// [Load chain] |
| 370 | /// ^ |
| 371 | /// | |
| 372 | /// [TokenFactor] |
| 373 | /// ^ |
| 374 | /// | |
| 375 | /// [Load] |
| 376 | /// ^ ^ |
| 377 | /// | | |
| 378 | /// | \- |
| 379 | /// | | |
| 380 | /// | [Op] |
| 381 | /// | ^ |
| 382 | /// | | |
| 383 | /// \ / |
| 384 | /// \ / |
| 385 | /// [Store] |
| 386 | void X86DAGToDAGISel::InstructionSelectPreprocess(SelectionDAG &DAG) { |
| 387 | for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(), |
| 388 | E = DAG.allnodes_end(); I != E; ++I) { |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 389 | if (!ISD::isNON_TRUNCStore(I)) |
Evan Cheng | 70e674e | 2006-08-28 20:10:17 +0000 | [diff] [blame] | 390 | continue; |
| 391 | SDOperand Chain = I->getOperand(0); |
| 392 | if (Chain.Val->getOpcode() != ISD::TokenFactor) |
| 393 | continue; |
| 394 | |
| 395 | SDOperand N1 = I->getOperand(1); |
| 396 | SDOperand N2 = I->getOperand(2); |
Evan Cheng | 1453de5 | 2006-09-01 22:52:28 +0000 | [diff] [blame] | 397 | if (MVT::isFloatingPoint(N1.getValueType()) || |
| 398 | MVT::isVector(N1.getValueType()) || |
Evan Cheng | 780413d | 2006-08-29 18:37:37 +0000 | [diff] [blame] | 399 | !N1.hasOneUse()) |
Evan Cheng | 70e674e | 2006-08-28 20:10:17 +0000 | [diff] [blame] | 400 | continue; |
| 401 | |
| 402 | bool RModW = false; |
| 403 | SDOperand Load; |
| 404 | unsigned Opcode = N1.Val->getOpcode(); |
| 405 | switch (Opcode) { |
| 406 | case ISD::ADD: |
| 407 | case ISD::MUL: |
Evan Cheng | 70e674e | 2006-08-28 20:10:17 +0000 | [diff] [blame] | 408 | case ISD::AND: |
| 409 | case ISD::OR: |
| 410 | case ISD::XOR: |
| 411 | case ISD::ADDC: |
| 412 | case ISD::ADDE: { |
| 413 | SDOperand N10 = N1.getOperand(0); |
| 414 | SDOperand N11 = N1.getOperand(1); |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 415 | if (ISD::isNON_EXTLoad(N10.Val)) |
Evan Cheng | 70e674e | 2006-08-28 20:10:17 +0000 | [diff] [blame] | 416 | RModW = true; |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 417 | else if (ISD::isNON_EXTLoad(N11.Val)) { |
Evan Cheng | 70e674e | 2006-08-28 20:10:17 +0000 | [diff] [blame] | 418 | RModW = true; |
| 419 | std::swap(N10, N11); |
| 420 | } |
| 421 | RModW = RModW && N10.Val->isOperand(Chain.Val) && N10.hasOneUse() && |
Evan Cheng | 82a35b3 | 2006-08-29 06:44:17 +0000 | [diff] [blame] | 422 | (N10.getOperand(1) == N2) && |
| 423 | (N10.Val->getValueType(0) == N1.getValueType()); |
Evan Cheng | 70e674e | 2006-08-28 20:10:17 +0000 | [diff] [blame] | 424 | if (RModW) |
| 425 | Load = N10; |
| 426 | break; |
| 427 | } |
| 428 | case ISD::SUB: |
| 429 | case ISD::SHL: |
| 430 | case ISD::SRA: |
| 431 | case ISD::SRL: |
| 432 | case ISD::ROTL: |
| 433 | case ISD::ROTR: |
| 434 | case ISD::SUBC: |
| 435 | case ISD::SUBE: |
| 436 | case X86ISD::SHLD: |
| 437 | case X86ISD::SHRD: { |
| 438 | SDOperand N10 = N1.getOperand(0); |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 439 | if (ISD::isNON_EXTLoad(N10.Val)) |
Evan Cheng | 70e674e | 2006-08-28 20:10:17 +0000 | [diff] [blame] | 440 | RModW = N10.Val->isOperand(Chain.Val) && N10.hasOneUse() && |
Evan Cheng | 82a35b3 | 2006-08-29 06:44:17 +0000 | [diff] [blame] | 441 | (N10.getOperand(1) == N2) && |
| 442 | (N10.Val->getValueType(0) == N1.getValueType()); |
Evan Cheng | 70e674e | 2006-08-28 20:10:17 +0000 | [diff] [blame] | 443 | if (RModW) |
| 444 | Load = N10; |
| 445 | break; |
| 446 | } |
| 447 | } |
| 448 | |
Evan Cheng | 82a35b3 | 2006-08-29 06:44:17 +0000 | [diff] [blame] | 449 | if (RModW) { |
Evan Cheng | 70e674e | 2006-08-28 20:10:17 +0000 | [diff] [blame] | 450 | MoveBelowTokenFactor(DAG, Load, SDOperand(I, 0), Chain); |
Evan Cheng | 82a35b3 | 2006-08-29 06:44:17 +0000 | [diff] [blame] | 451 | ++NumLoadMoved; |
| 452 | } |
Evan Cheng | 70e674e | 2006-08-28 20:10:17 +0000 | [diff] [blame] | 453 | } |
| 454 | } |
| 455 | |
Chris Lattner | c961eea | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 456 | /// InstructionSelectBasicBlock - This callback is invoked by SelectionDAGISel |
| 457 | /// when it has created a SelectionDAG for us to codegen. |
| 458 | void X86DAGToDAGISel::InstructionSelectBasicBlock(SelectionDAG &DAG) { |
| 459 | DEBUG(BB->dump()); |
Chris Lattner | 92cb0af | 2006-01-11 01:15:34 +0000 | [diff] [blame] | 460 | MachineFunction::iterator FirstMBB = BB; |
Chris Lattner | c961eea | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 461 | |
Evan Cheng | e50794a | 2006-08-29 18:28:33 +0000 | [diff] [blame] | 462 | if (!FastISel) |
Evan Cheng | 70e674e | 2006-08-28 20:10:17 +0000 | [diff] [blame] | 463 | InstructionSelectPreprocess(DAG); |
| 464 | |
Chris Lattner | c961eea | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 465 | // Codegen the basic block. |
Evan Cheng | f597dc7 | 2006-02-10 22:24:32 +0000 | [diff] [blame] | 466 | #ifndef NDEBUG |
Bill Wendling | 6345d75 | 2006-11-17 07:52:03 +0000 | [diff] [blame] | 467 | DOUT << "===== Instruction selection begins:\n"; |
Evan Cheng | 23addc0 | 2006-02-10 22:46:26 +0000 | [diff] [blame] | 468 | Indent = 0; |
Evan Cheng | f597dc7 | 2006-02-10 22:24:32 +0000 | [diff] [blame] | 469 | #endif |
Evan Cheng | ba2f0a9 | 2006-02-05 06:46:41 +0000 | [diff] [blame] | 470 | DAG.setRoot(SelectRoot(DAG.getRoot())); |
Evan Cheng | f597dc7 | 2006-02-10 22:24:32 +0000 | [diff] [blame] | 471 | #ifndef NDEBUG |
Bill Wendling | 6345d75 | 2006-11-17 07:52:03 +0000 | [diff] [blame] | 472 | DOUT << "===== Instruction selection ends:\n"; |
Evan Cheng | f597dc7 | 2006-02-10 22:24:32 +0000 | [diff] [blame] | 473 | #endif |
Evan Cheng | 63ce568 | 2006-07-28 00:10:59 +0000 | [diff] [blame] | 474 | |
Chris Lattner | c961eea | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 475 | DAG.RemoveDeadNodes(); |
| 476 | |
| 477 | // Emit machine code to BB. |
| 478 | ScheduleAndEmitDAG(DAG); |
Chris Lattner | 92cb0af | 2006-01-11 01:15:34 +0000 | [diff] [blame] | 479 | |
| 480 | // If we are emitting FP stack code, scan the basic block to determine if this |
| 481 | // block defines any FP values. If so, put an FP_REG_KILL instruction before |
| 482 | // the terminator of the block. |
Evan Cheng | 559806f | 2006-01-27 08:10:46 +0000 | [diff] [blame] | 483 | if (!Subtarget->hasSSE2()) { |
Chris Lattner | 92cb0af | 2006-01-11 01:15:34 +0000 | [diff] [blame] | 484 | // Note that FP stack instructions *are* used in SSE code when returning |
| 485 | // values, but these are not live out of the basic block, so we don't need |
| 486 | // an FP_REG_KILL in this case either. |
| 487 | bool ContainsFPCode = false; |
| 488 | |
| 489 | // Scan all of the machine instructions in these MBBs, checking for FP |
| 490 | // stores. |
| 491 | MachineFunction::iterator MBBI = FirstMBB; |
| 492 | do { |
| 493 | for (MachineBasicBlock::iterator I = MBBI->begin(), E = MBBI->end(); |
| 494 | !ContainsFPCode && I != E; ++I) { |
| 495 | for (unsigned op = 0, e = I->getNumOperands(); op != e; ++op) { |
| 496 | if (I->getOperand(op).isRegister() && I->getOperand(op).isDef() && |
| 497 | MRegisterInfo::isVirtualRegister(I->getOperand(op).getReg()) && |
| 498 | RegMap->getRegClass(I->getOperand(0).getReg()) == |
| 499 | X86::RFPRegisterClass) { |
| 500 | ContainsFPCode = true; |
| 501 | break; |
| 502 | } |
| 503 | } |
| 504 | } |
| 505 | } while (!ContainsFPCode && &*(MBBI++) != BB); |
| 506 | |
| 507 | // Check PHI nodes in successor blocks. These PHI's will be lowered to have |
| 508 | // a copy of the input value in this block. |
| 509 | if (!ContainsFPCode) { |
| 510 | // Final check, check LLVM BB's that are successors to the LLVM BB |
| 511 | // corresponding to BB for FP PHI nodes. |
| 512 | const BasicBlock *LLVMBB = BB->getBasicBlock(); |
| 513 | const PHINode *PN; |
| 514 | for (succ_const_iterator SI = succ_begin(LLVMBB), E = succ_end(LLVMBB); |
| 515 | !ContainsFPCode && SI != E; ++SI) { |
| 516 | for (BasicBlock::const_iterator II = SI->begin(); |
| 517 | (PN = dyn_cast<PHINode>(II)); ++II) { |
| 518 | if (PN->getType()->isFloatingPoint()) { |
| 519 | ContainsFPCode = true; |
| 520 | break; |
| 521 | } |
| 522 | } |
| 523 | } |
| 524 | } |
| 525 | |
| 526 | // Finally, if we found any FP code, emit the FP_REG_KILL instruction. |
| 527 | if (ContainsFPCode) { |
Evan Cheng | c0f64ff | 2006-11-27 23:37:22 +0000 | [diff] [blame] | 528 | BuildMI(*BB, BB->getFirstTerminator(), |
| 529 | TM.getInstrInfo()->get(X86::FP_REG_KILL)); |
Chris Lattner | 92cb0af | 2006-01-11 01:15:34 +0000 | [diff] [blame] | 530 | ++NumFPKill; |
| 531 | } |
| 532 | } |
Chris Lattner | c961eea | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 533 | } |
| 534 | |
Evan Cheng | 8700e14 | 2006-01-11 06:09:51 +0000 | [diff] [blame] | 535 | /// EmitSpecialCodeForMain - Emit any code that needs to be executed only in |
| 536 | /// the main function. |
Evan Cheng | 3649b0e | 2006-06-02 22:38:37 +0000 | [diff] [blame] | 537 | void X86DAGToDAGISel::EmitSpecialCodeForMain(MachineBasicBlock *BB, |
| 538 | MachineFrameInfo *MFI) { |
Evan Cheng | c0f64ff | 2006-11-27 23:37:22 +0000 | [diff] [blame] | 539 | const TargetInstrInfo *TII = TM.getInstrInfo(); |
Anton Korobeynikov | bcb9770 | 2006-09-17 20:25:45 +0000 | [diff] [blame] | 540 | if (Subtarget->isTargetCygwin()) |
Evan Cheng | c0f64ff | 2006-11-27 23:37:22 +0000 | [diff] [blame] | 541 | BuildMI(BB, TII->get(X86::CALLpcrel32)).addExternalSymbol("__main"); |
Evan Cheng | 3649b0e | 2006-06-02 22:38:37 +0000 | [diff] [blame] | 542 | |
Evan Cheng | 8700e14 | 2006-01-11 06:09:51 +0000 | [diff] [blame] | 543 | // Switch the FPU to 64-bit precision mode for better compatibility and speed. |
| 544 | int CWFrameIdx = MFI->CreateStackObject(2, 2); |
Evan Cheng | c0f64ff | 2006-11-27 23:37:22 +0000 | [diff] [blame] | 545 | addFrameReference(BuildMI(BB, TII->get(X86::FNSTCW16m)), CWFrameIdx); |
Evan Cheng | 8700e14 | 2006-01-11 06:09:51 +0000 | [diff] [blame] | 546 | |
| 547 | // Set the high part to be 64-bit precision. |
Evan Cheng | c0f64ff | 2006-11-27 23:37:22 +0000 | [diff] [blame] | 548 | addFrameReference(BuildMI(BB, TII->get(X86::MOV8mi)), |
Evan Cheng | 8700e14 | 2006-01-11 06:09:51 +0000 | [diff] [blame] | 549 | CWFrameIdx, 1).addImm(2); |
| 550 | |
| 551 | // Reload the modified control word now. |
Evan Cheng | c0f64ff | 2006-11-27 23:37:22 +0000 | [diff] [blame] | 552 | addFrameReference(BuildMI(BB, TII->get(X86::FLDCW16m)), CWFrameIdx); |
Evan Cheng | 8700e14 | 2006-01-11 06:09:51 +0000 | [diff] [blame] | 553 | } |
| 554 | |
| 555 | void X86DAGToDAGISel::EmitFunctionEntryCode(Function &Fn, MachineFunction &MF) { |
| 556 | // If this is main, emit special code for main. |
| 557 | MachineBasicBlock *BB = MF.begin(); |
| 558 | if (Fn.hasExternalLinkage() && Fn.getName() == "main") |
| 559 | EmitSpecialCodeForMain(BB, MF.getFrameInfo()); |
| 560 | } |
| 561 | |
Chris Lattner | f9ce9fb | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 562 | /// MatchAddress - Add the specified node to the specified addressing mode, |
| 563 | /// returning true if it cannot be done. This just pattern matches for the |
| 564 | /// addressing mode |
Evan Cheng | 2486af1 | 2006-02-11 02:05:36 +0000 | [diff] [blame] | 565 | bool X86DAGToDAGISel::MatchAddress(SDOperand N, X86ISelAddressMode &AM, |
| 566 | bool isRoot) { |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 567 | // RIP relative addressing: %rip + 32-bit displacement! |
| 568 | if (AM.isRIPRel) { |
| 569 | if (!AM.ES && AM.JT != -1 && N.getOpcode() == ISD::Constant) { |
Chris Lattner | 0f27fc3 | 2006-09-13 04:45:25 +0000 | [diff] [blame] | 570 | int64_t Val = cast<ConstantSDNode>(N)->getSignExtended(); |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 571 | if (isInt32(AM.Disp + Val)) { |
| 572 | AM.Disp += Val; |
| 573 | return false; |
| 574 | } |
| 575 | } |
| 576 | return true; |
| 577 | } |
| 578 | |
Evan Cheng | 2ef88a0 | 2006-08-07 22:28:20 +0000 | [diff] [blame] | 579 | int id = N.Val->getNodeId(); |
| 580 | bool Available = isSelected(id); |
Evan Cheng | 2486af1 | 2006-02-11 02:05:36 +0000 | [diff] [blame] | 581 | |
Chris Lattner | f9ce9fb | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 582 | switch (N.getOpcode()) { |
| 583 | default: break; |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 584 | case ISD::Constant: { |
Chris Lattner | 0f27fc3 | 2006-09-13 04:45:25 +0000 | [diff] [blame] | 585 | int64_t Val = cast<ConstantSDNode>(N)->getSignExtended(); |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 586 | if (isInt32(AM.Disp + Val)) { |
| 587 | AM.Disp += Val; |
| 588 | return false; |
| 589 | } |
| 590 | break; |
| 591 | } |
Evan Cheng | 51a9ed9 | 2006-02-25 10:09:08 +0000 | [diff] [blame] | 592 | |
Evan Cheng | d0ff02c | 2006-11-29 23:19:46 +0000 | [diff] [blame^] | 593 | case ISD::TargetConstantPool: |
| 594 | if (AM.BaseType == X86ISelAddressMode::RegBase && |
| 595 | AM.Base.Reg.Val == 0 && |
| 596 | AM.CP == 0) { |
| 597 | ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(N); |
| 598 | AM.CP = CP->getConstVal(); |
| 599 | AM.Align = CP->getAlignment(); |
| 600 | AM.Disp += CP->getOffset(); |
| 601 | return false; |
| 602 | } |
| 603 | break; |
| 604 | |
| 605 | case ISD::TargetGlobalAddress: |
| 606 | if (AM.BaseType == X86ISelAddressMode::RegBase && |
| 607 | AM.Base.Reg.Val == 0 && |
| 608 | AM.GV == 0) { |
| 609 | GlobalAddressSDNode *G = cast<GlobalAddressSDNode>(N); |
| 610 | AM.GV = G->getGlobal(); |
| 611 | AM.Disp += G->getOffset(); |
| 612 | return false; |
| 613 | } |
| 614 | break; |
| 615 | |
| 616 | case ISD::TargetExternalSymbol: |
| 617 | if (isRoot && |
| 618 | AM.BaseType == X86ISelAddressMode::RegBase && |
| 619 | AM.Base.Reg.Val == 0) { |
| 620 | ExternalSymbolSDNode *S = cast<ExternalSymbolSDNode>(N.getOperand(0)); |
| 621 | AM.ES = S->getSymbol(); |
| 622 | return false; |
| 623 | } |
| 624 | break; |
| 625 | |
| 626 | case ISD::TargetJumpTable: |
| 627 | if (isRoot && |
| 628 | AM.BaseType == X86ISelAddressMode::RegBase && |
| 629 | AM.Base.Reg.Val == 0) { |
| 630 | JumpTableSDNode *J = cast<JumpTableSDNode>(N.getOperand(0)); |
| 631 | AM.JT = J->getIndex(); |
| 632 | return false; |
| 633 | } |
| 634 | break; |
| 635 | |
Evan Cheng | 51a9ed9 | 2006-02-25 10:09:08 +0000 | [diff] [blame] | 636 | case X86ISD::Wrapper: |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 637 | // If value is available in a register both base and index components have |
| 638 | // been picked, we can't fit the result available in the register in the |
| 639 | // addressing mode. Duplicate GlobalAddress or ConstantPool as displacement. |
| 640 | |
| 641 | // Can't fit GV or CP in addressing mode for X86-64 medium or large code |
| 642 | // model since the displacement field is 32-bit. Ok for small code model. |
| 643 | |
| 644 | // For X86-64 PIC code, only allow GV / CP + displacement so we can use RIP |
| 645 | // relative addressing mode. |
| 646 | if ((!Subtarget->is64Bit() || TM.getCodeModel() == CodeModel::Small) && |
| 647 | (!Available || (AM.Base.Reg.Val && AM.IndexReg.Val))) { |
| 648 | bool isRIP = Subtarget->is64Bit(); |
| 649 | if (isRIP && (AM.Base.Reg.Val || AM.Scale > 1 || AM.IndexReg.Val || |
| 650 | AM.BaseType == X86ISelAddressMode::FrameIndexBase)) |
| 651 | break; |
Evan Cheng | 51a9ed9 | 2006-02-25 10:09:08 +0000 | [diff] [blame] | 652 | if (ConstantPoolSDNode *CP = |
| 653 | dyn_cast<ConstantPoolSDNode>(N.getOperand(0))) { |
| 654 | if (AM.CP == 0) { |
Evan Cheng | c356a57 | 2006-09-12 21:04:05 +0000 | [diff] [blame] | 655 | AM.CP = CP->getConstVal(); |
Evan Cheng | 51a9ed9 | 2006-02-25 10:09:08 +0000 | [diff] [blame] | 656 | AM.Align = CP->getAlignment(); |
| 657 | AM.Disp += CP->getOffset(); |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 658 | if (isRIP) |
| 659 | AM.isRIPRel = true; |
Evan Cheng | 51a9ed9 | 2006-02-25 10:09:08 +0000 | [diff] [blame] | 660 | return false; |
| 661 | } |
| 662 | } else if (GlobalAddressSDNode *G = |
| 663 | dyn_cast<GlobalAddressSDNode>(N.getOperand(0))) { |
| 664 | if (AM.GV == 0) { |
| 665 | AM.GV = G->getGlobal(); |
| 666 | AM.Disp += G->getOffset(); |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 667 | if (isRIP) |
| 668 | AM.isRIPRel = true; |
| 669 | return false; |
| 670 | } |
| 671 | } else if (isRoot && isRIP) { |
| 672 | if (ExternalSymbolSDNode *S = |
| 673 | dyn_cast<ExternalSymbolSDNode>(N.getOperand(0))) { |
| 674 | AM.ES = S->getSymbol(); |
| 675 | AM.isRIPRel = true; |
| 676 | return false; |
| 677 | } else if (JumpTableSDNode *J = |
| 678 | dyn_cast<JumpTableSDNode>(N.getOperand(0))) { |
| 679 | AM.JT = J->getIndex(); |
| 680 | AM.isRIPRel = true; |
Evan Cheng | 51a9ed9 | 2006-02-25 10:09:08 +0000 | [diff] [blame] | 681 | return false; |
| 682 | } |
| 683 | } |
| 684 | } |
| 685 | break; |
| 686 | |
Chris Lattner | f9ce9fb | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 687 | case ISD::FrameIndex: |
| 688 | if (AM.BaseType == X86ISelAddressMode::RegBase && AM.Base.Reg.Val == 0) { |
| 689 | AM.BaseType = X86ISelAddressMode::FrameIndexBase; |
| 690 | AM.Base.FrameIndex = cast<FrameIndexSDNode>(N)->getIndex(); |
| 691 | return false; |
| 692 | } |
| 693 | break; |
Evan Cheng | ec693f7 | 2005-12-08 02:01:35 +0000 | [diff] [blame] | 694 | |
Chris Lattner | f9ce9fb | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 695 | case ISD::SHL: |
Evan Cheng | 51a9ed9 | 2006-02-25 10:09:08 +0000 | [diff] [blame] | 696 | if (!Available && AM.IndexReg.Val == 0 && AM.Scale == 1) |
Chris Lattner | f9ce9fb | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 697 | if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1))) { |
| 698 | unsigned Val = CN->getValue(); |
| 699 | if (Val == 1 || Val == 2 || Val == 3) { |
| 700 | AM.Scale = 1 << Val; |
| 701 | SDOperand ShVal = N.Val->getOperand(0); |
| 702 | |
| 703 | // Okay, we know that we have a scale by now. However, if the scaled |
| 704 | // value is an add of something and a constant, we can fold the |
| 705 | // constant into the disp field here. |
| 706 | if (ShVal.Val->getOpcode() == ISD::ADD && ShVal.hasOneUse() && |
| 707 | isa<ConstantSDNode>(ShVal.Val->getOperand(1))) { |
| 708 | AM.IndexReg = ShVal.Val->getOperand(0); |
| 709 | ConstantSDNode *AddVal = |
| 710 | cast<ConstantSDNode>(ShVal.Val->getOperand(1)); |
Jeff Cohen | d41b30d | 2006-11-05 19:31:28 +0000 | [diff] [blame] | 711 | uint64_t Disp = AM.Disp + (AddVal->getValue() << Val); |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 712 | if (isInt32(Disp)) |
| 713 | AM.Disp = Disp; |
| 714 | else |
| 715 | AM.IndexReg = ShVal; |
Chris Lattner | f9ce9fb | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 716 | } else { |
| 717 | AM.IndexReg = ShVal; |
| 718 | } |
| 719 | return false; |
| 720 | } |
| 721 | } |
| 722 | break; |
Evan Cheng | ec693f7 | 2005-12-08 02:01:35 +0000 | [diff] [blame] | 723 | |
Chris Lattner | f9ce9fb | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 724 | case ISD::MUL: |
| 725 | // X*[3,5,9] -> X+X*[2,4,8] |
Evan Cheng | 51a9ed9 | 2006-02-25 10:09:08 +0000 | [diff] [blame] | 726 | if (!Available && |
| 727 | AM.BaseType == X86ISelAddressMode::RegBase && |
| 728 | AM.Base.Reg.Val == 0 && |
| 729 | AM.IndexReg.Val == 0) |
Chris Lattner | f9ce9fb | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 730 | if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1))) |
| 731 | if (CN->getValue() == 3 || CN->getValue() == 5 || CN->getValue() == 9) { |
| 732 | AM.Scale = unsigned(CN->getValue())-1; |
| 733 | |
| 734 | SDOperand MulVal = N.Val->getOperand(0); |
| 735 | SDOperand Reg; |
| 736 | |
| 737 | // Okay, we know that we have a scale by now. However, if the scaled |
| 738 | // value is an add of something and a constant, we can fold the |
| 739 | // constant into the disp field here. |
| 740 | if (MulVal.Val->getOpcode() == ISD::ADD && MulVal.hasOneUse() && |
| 741 | isa<ConstantSDNode>(MulVal.Val->getOperand(1))) { |
| 742 | Reg = MulVal.Val->getOperand(0); |
| 743 | ConstantSDNode *AddVal = |
| 744 | cast<ConstantSDNode>(MulVal.Val->getOperand(1)); |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 745 | uint64_t Disp = AM.Disp + AddVal->getValue() * CN->getValue(); |
| 746 | if (isInt32(Disp)) |
| 747 | AM.Disp = Disp; |
| 748 | else |
| 749 | Reg = N.Val->getOperand(0); |
Chris Lattner | f9ce9fb | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 750 | } else { |
| 751 | Reg = N.Val->getOperand(0); |
| 752 | } |
| 753 | |
| 754 | AM.IndexReg = AM.Base.Reg = Reg; |
| 755 | return false; |
| 756 | } |
| 757 | break; |
| 758 | |
| 759 | case ISD::ADD: { |
Evan Cheng | 51a9ed9 | 2006-02-25 10:09:08 +0000 | [diff] [blame] | 760 | if (!Available) { |
Evan Cheng | 2486af1 | 2006-02-11 02:05:36 +0000 | [diff] [blame] | 761 | X86ISelAddressMode Backup = AM; |
| 762 | if (!MatchAddress(N.Val->getOperand(0), AM, false) && |
| 763 | !MatchAddress(N.Val->getOperand(1), AM, false)) |
| 764 | return false; |
| 765 | AM = Backup; |
| 766 | if (!MatchAddress(N.Val->getOperand(1), AM, false) && |
| 767 | !MatchAddress(N.Val->getOperand(0), AM, false)) |
| 768 | return false; |
| 769 | AM = Backup; |
| 770 | } |
Chris Lattner | f9ce9fb | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 771 | break; |
| 772 | } |
Evan Cheng | e6ad27e | 2006-05-30 06:59:36 +0000 | [diff] [blame] | 773 | |
| 774 | case ISD::OR: { |
| 775 | if (!Available) { |
| 776 | X86ISelAddressMode Backup = AM; |
| 777 | // Look for (x << c1) | c2 where (c2 < c1) |
| 778 | ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(0)); |
| 779 | if (CN && !MatchAddress(N.Val->getOperand(1), AM, false)) { |
| 780 | if (AM.GV == NULL && AM.Disp == 0 && CN->getValue() < AM.Scale) { |
| 781 | AM.Disp = CN->getValue(); |
| 782 | return false; |
| 783 | } |
| 784 | } |
| 785 | AM = Backup; |
| 786 | CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1)); |
| 787 | if (CN && !MatchAddress(N.Val->getOperand(0), AM, false)) { |
| 788 | if (AM.GV == NULL && AM.Disp == 0 && CN->getValue() < AM.Scale) { |
| 789 | AM.Disp = CN->getValue(); |
| 790 | return false; |
| 791 | } |
| 792 | } |
| 793 | AM = Backup; |
| 794 | } |
| 795 | break; |
| 796 | } |
Chris Lattner | f9ce9fb | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 797 | } |
| 798 | |
| 799 | // Is the base register already occupied? |
| 800 | if (AM.BaseType != X86ISelAddressMode::RegBase || AM.Base.Reg.Val) { |
| 801 | // If so, check to see if the scale index register is set. |
| 802 | if (AM.IndexReg.Val == 0) { |
| 803 | AM.IndexReg = N; |
| 804 | AM.Scale = 1; |
| 805 | return false; |
| 806 | } |
| 807 | |
| 808 | // Otherwise, we cannot select it. |
| 809 | return true; |
| 810 | } |
| 811 | |
| 812 | // Default, generate it as a register. |
| 813 | AM.BaseType = X86ISelAddressMode::RegBase; |
| 814 | AM.Base.Reg = N; |
| 815 | return false; |
| 816 | } |
| 817 | |
Evan Cheng | ec693f7 | 2005-12-08 02:01:35 +0000 | [diff] [blame] | 818 | /// SelectAddr - returns true if it is able pattern match an addressing mode. |
| 819 | /// It returns the operands which make up the maximal addressing mode it can |
| 820 | /// match by reference. |
Evan Cheng | 0d53826 | 2006-11-08 20:34:28 +0000 | [diff] [blame] | 821 | bool X86DAGToDAGISel::SelectAddr(SDOperand Op, SDOperand N, SDOperand &Base, |
| 822 | SDOperand &Scale, SDOperand &Index, |
| 823 | SDOperand &Disp) { |
Evan Cheng | ec693f7 | 2005-12-08 02:01:35 +0000 | [diff] [blame] | 824 | X86ISelAddressMode AM; |
Evan Cheng | 8700e14 | 2006-01-11 06:09:51 +0000 | [diff] [blame] | 825 | if (MatchAddress(N, AM)) |
| 826 | return false; |
Evan Cheng | ec693f7 | 2005-12-08 02:01:35 +0000 | [diff] [blame] | 827 | |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 828 | MVT::ValueType VT = N.getValueType(); |
Evan Cheng | 8700e14 | 2006-01-11 06:09:51 +0000 | [diff] [blame] | 829 | if (AM.BaseType == X86ISelAddressMode::RegBase) { |
Evan Cheng | 7dd281b | 2006-02-05 05:25:07 +0000 | [diff] [blame] | 830 | if (!AM.Base.Reg.Val) |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 831 | AM.Base.Reg = CurDAG->getRegister(0, VT); |
Evan Cheng | ec693f7 | 2005-12-08 02:01:35 +0000 | [diff] [blame] | 832 | } |
Evan Cheng | 8700e14 | 2006-01-11 06:09:51 +0000 | [diff] [blame] | 833 | |
Evan Cheng | 7dd281b | 2006-02-05 05:25:07 +0000 | [diff] [blame] | 834 | if (!AM.IndexReg.Val) |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 835 | AM.IndexReg = CurDAG->getRegister(0, VT); |
Evan Cheng | 8700e14 | 2006-01-11 06:09:51 +0000 | [diff] [blame] | 836 | |
| 837 | getAddressOperands(AM, Base, Scale, Index, Disp); |
| 838 | return true; |
Evan Cheng | ec693f7 | 2005-12-08 02:01:35 +0000 | [diff] [blame] | 839 | } |
| 840 | |
Chris Lattner | 4fe4f25 | 2006-10-11 22:09:58 +0000 | [diff] [blame] | 841 | /// isZeroNode - Returns true if Elt is a constant zero or a floating point |
| 842 | /// constant +0.0. |
| 843 | static inline bool isZeroNode(SDOperand Elt) { |
| 844 | return ((isa<ConstantSDNode>(Elt) && |
| 845 | cast<ConstantSDNode>(Elt)->getValue() == 0) || |
| 846 | (isa<ConstantFPSDNode>(Elt) && |
| 847 | cast<ConstantFPSDNode>(Elt)->isExactlyValue(0.0))); |
| 848 | } |
| 849 | |
| 850 | |
Chris Lattner | 3a7cd95 | 2006-10-07 21:55:32 +0000 | [diff] [blame] | 851 | /// SelectScalarSSELoad - Match a scalar SSE load. In particular, we want to |
| 852 | /// match a load whose top elements are either undef or zeros. The load flavor |
| 853 | /// is derived from the type of N, which is either v4f32 or v2f64. |
Evan Cheng | 0d53826 | 2006-11-08 20:34:28 +0000 | [diff] [blame] | 854 | bool X86DAGToDAGISel::SelectScalarSSELoad(SDOperand Op, SDOperand Pred, |
Evan Cheng | 07e4b00 | 2006-10-16 06:34:55 +0000 | [diff] [blame] | 855 | SDOperand N, SDOperand &Base, |
Evan Cheng | 82a9164 | 2006-10-11 21:06:01 +0000 | [diff] [blame] | 856 | SDOperand &Scale, SDOperand &Index, |
| 857 | SDOperand &Disp, SDOperand &InChain, |
| 858 | SDOperand &OutChain) { |
Chris Lattner | 3a7cd95 | 2006-10-07 21:55:32 +0000 | [diff] [blame] | 859 | if (N.getOpcode() == ISD::SCALAR_TO_VECTOR) { |
Chris Lattner | 4fe4f25 | 2006-10-11 22:09:58 +0000 | [diff] [blame] | 860 | InChain = N.getOperand(0).getValue(1); |
Evan Cheng | 07e4b00 | 2006-10-16 06:34:55 +0000 | [diff] [blame] | 861 | if (ISD::isNON_EXTLoad(InChain.Val) && |
| 862 | InChain.getValue(0).hasOneUse() && |
Evan Cheng | d6373bc | 2006-11-10 21:23:04 +0000 | [diff] [blame] | 863 | N.hasOneUse() && |
Evan Cheng | 0d53826 | 2006-11-08 20:34:28 +0000 | [diff] [blame] | 864 | CanBeFoldedBy(N.Val, Pred.Val, Op.Val)) { |
Evan Cheng | 82a9164 | 2006-10-11 21:06:01 +0000 | [diff] [blame] | 865 | LoadSDNode *LD = cast<LoadSDNode>(InChain); |
Evan Cheng | 0d53826 | 2006-11-08 20:34:28 +0000 | [diff] [blame] | 866 | if (!SelectAddr(Op, LD->getBasePtr(), Base, Scale, Index, Disp)) |
Chris Lattner | 3a7cd95 | 2006-10-07 21:55:32 +0000 | [diff] [blame] | 867 | return false; |
Evan Cheng | 82a9164 | 2006-10-11 21:06:01 +0000 | [diff] [blame] | 868 | OutChain = LD->getChain(); |
Chris Lattner | 3a7cd95 | 2006-10-07 21:55:32 +0000 | [diff] [blame] | 869 | return true; |
| 870 | } |
| 871 | } |
Chris Lattner | 4fe4f25 | 2006-10-11 22:09:58 +0000 | [diff] [blame] | 872 | |
| 873 | // Also handle the case where we explicitly require zeros in the top |
Chris Lattner | 3a7cd95 | 2006-10-07 21:55:32 +0000 | [diff] [blame] | 874 | // elements. This is a vector shuffle from the zero vector. |
Chris Lattner | 4fe4f25 | 2006-10-11 22:09:58 +0000 | [diff] [blame] | 875 | if (N.getOpcode() == ISD::VECTOR_SHUFFLE && N.Val->hasOneUse() && |
| 876 | N.getOperand(0).getOpcode() == ISD::BUILD_VECTOR && |
| 877 | N.getOperand(1).getOpcode() == ISD::SCALAR_TO_VECTOR && |
| 878 | N.getOperand(1).Val->hasOneUse() && |
| 879 | ISD::isNON_EXTLoad(N.getOperand(1).getOperand(0).Val) && |
| 880 | N.getOperand(1).getOperand(0).hasOneUse()) { |
| 881 | // Check to see if the BUILD_VECTOR is building a zero vector. |
| 882 | SDOperand BV = N.getOperand(0); |
| 883 | for (unsigned i = 0, e = BV.getNumOperands(); i != e; ++i) |
| 884 | if (!isZeroNode(BV.getOperand(i)) && |
| 885 | BV.getOperand(i).getOpcode() != ISD::UNDEF) |
| 886 | return false; // Not a zero/undef vector. |
| 887 | // Check to see if the shuffle mask is 4/L/L/L or 2/L, where L is something |
| 888 | // from the LHS. |
| 889 | unsigned VecWidth = BV.getNumOperands(); |
| 890 | SDOperand ShufMask = N.getOperand(2); |
| 891 | assert(ShufMask.getOpcode() == ISD::BUILD_VECTOR && "Invalid shuf mask!"); |
| 892 | if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(ShufMask.getOperand(0))) { |
| 893 | if (C->getValue() == VecWidth) { |
| 894 | for (unsigned i = 1; i != VecWidth; ++i) { |
| 895 | if (ShufMask.getOperand(i).getOpcode() == ISD::UNDEF) { |
| 896 | // ok. |
| 897 | } else { |
| 898 | ConstantSDNode *C = cast<ConstantSDNode>(ShufMask.getOperand(i)); |
| 899 | if (C->getValue() >= VecWidth) return false; |
| 900 | } |
| 901 | } |
| 902 | } |
| 903 | |
| 904 | // Okay, this is a zero extending load. Fold it. |
| 905 | LoadSDNode *LD = cast<LoadSDNode>(N.getOperand(1).getOperand(0)); |
Evan Cheng | 0d53826 | 2006-11-08 20:34:28 +0000 | [diff] [blame] | 906 | if (!SelectAddr(Op, LD->getBasePtr(), Base, Scale, Index, Disp)) |
Chris Lattner | 4fe4f25 | 2006-10-11 22:09:58 +0000 | [diff] [blame] | 907 | return false; |
| 908 | OutChain = LD->getChain(); |
| 909 | InChain = SDOperand(LD, 1); |
| 910 | return true; |
| 911 | } |
| 912 | } |
Chris Lattner | 3a7cd95 | 2006-10-07 21:55:32 +0000 | [diff] [blame] | 913 | return false; |
| 914 | } |
| 915 | |
| 916 | |
Evan Cheng | 51a9ed9 | 2006-02-25 10:09:08 +0000 | [diff] [blame] | 917 | /// SelectLEAAddr - it calls SelectAddr and determines if the maximal addressing |
| 918 | /// mode it matches can be cost effectively emitted as an LEA instruction. |
Evan Cheng | 0d53826 | 2006-11-08 20:34:28 +0000 | [diff] [blame] | 919 | bool X86DAGToDAGISel::SelectLEAAddr(SDOperand Op, SDOperand N, |
| 920 | SDOperand &Base, SDOperand &Scale, |
Evan Cheng | 51a9ed9 | 2006-02-25 10:09:08 +0000 | [diff] [blame] | 921 | SDOperand &Index, SDOperand &Disp) { |
| 922 | X86ISelAddressMode AM; |
| 923 | if (MatchAddress(N, AM)) |
| 924 | return false; |
| 925 | |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 926 | MVT::ValueType VT = N.getValueType(); |
Evan Cheng | 51a9ed9 | 2006-02-25 10:09:08 +0000 | [diff] [blame] | 927 | unsigned Complexity = 0; |
| 928 | if (AM.BaseType == X86ISelAddressMode::RegBase) |
| 929 | if (AM.Base.Reg.Val) |
| 930 | Complexity = 1; |
| 931 | else |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 932 | AM.Base.Reg = CurDAG->getRegister(0, VT); |
Evan Cheng | 51a9ed9 | 2006-02-25 10:09:08 +0000 | [diff] [blame] | 933 | else if (AM.BaseType == X86ISelAddressMode::FrameIndexBase) |
| 934 | Complexity = 4; |
| 935 | |
| 936 | if (AM.IndexReg.Val) |
| 937 | Complexity++; |
| 938 | else |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 939 | AM.IndexReg = CurDAG->getRegister(0, VT); |
Evan Cheng | 51a9ed9 | 2006-02-25 10:09:08 +0000 | [diff] [blame] | 940 | |
Evan Cheng | 8c03fe4 | 2006-02-28 21:13:57 +0000 | [diff] [blame] | 941 | if (AM.Scale > 2) |
Evan Cheng | 51a9ed9 | 2006-02-25 10:09:08 +0000 | [diff] [blame] | 942 | Complexity += 2; |
Evan Cheng | 8c03fe4 | 2006-02-28 21:13:57 +0000 | [diff] [blame] | 943 | // Don't match just leal(,%reg,2). It's cheaper to do addl %reg, %reg |
| 944 | else if (AM.Scale > 1) |
| 945 | Complexity++; |
Evan Cheng | 51a9ed9 | 2006-02-25 10:09:08 +0000 | [diff] [blame] | 946 | |
| 947 | // FIXME: We are artificially lowering the criteria to turn ADD %reg, $GA |
| 948 | // to a LEA. This is determined with some expermentation but is by no means |
| 949 | // optimal (especially for code size consideration). LEA is nice because of |
| 950 | // its three-address nature. Tweak the cost function again when we can run |
| 951 | // convertToThreeAddress() at register allocation time. |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 952 | if (AM.GV || AM.CP || AM.ES || AM.JT != -1) { |
| 953 | // For X86-64, we should always use lea to materialize RIP relative |
| 954 | // addresses. |
| 955 | if (Subtarget->is64Bit()) |
| 956 | Complexity = 4; |
| 957 | else |
| 958 | Complexity += 2; |
| 959 | } |
Evan Cheng | 51a9ed9 | 2006-02-25 10:09:08 +0000 | [diff] [blame] | 960 | |
| 961 | if (AM.Disp && (AM.Base.Reg.Val || AM.IndexReg.Val)) |
| 962 | Complexity++; |
| 963 | |
| 964 | if (Complexity > 2) { |
| 965 | getAddressOperands(AM, Base, Scale, Index, Disp); |
| 966 | return true; |
| 967 | } |
Evan Cheng | 51a9ed9 | 2006-02-25 10:09:08 +0000 | [diff] [blame] | 968 | return false; |
| 969 | } |
| 970 | |
Evan Cheng | 5e35168 | 2006-02-06 06:02:33 +0000 | [diff] [blame] | 971 | bool X86DAGToDAGISel::TryFoldLoad(SDOperand P, SDOperand N, |
| 972 | SDOperand &Base, SDOperand &Scale, |
| 973 | SDOperand &Index, SDOperand &Disp) { |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 974 | if (ISD::isNON_EXTLoad(N.Val) && |
Evan Cheng | 5e35168 | 2006-02-06 06:02:33 +0000 | [diff] [blame] | 975 | N.hasOneUse() && |
Evan Cheng | 27e1fe9 | 2006-10-14 08:33:25 +0000 | [diff] [blame] | 976 | CanBeFoldedBy(N.Val, P.Val, P.Val)) |
Evan Cheng | 0d53826 | 2006-11-08 20:34:28 +0000 | [diff] [blame] | 977 | return SelectAddr(P, N.getOperand(1), Base, Scale, Index, Disp); |
Evan Cheng | 0114e94 | 2006-01-06 20:36:21 +0000 | [diff] [blame] | 978 | return false; |
| 979 | } |
| 980 | |
Evan Cheng | 7ccced6 | 2006-02-18 00:15:05 +0000 | [diff] [blame] | 981 | /// getGlobalBaseReg - Output the instructions required to put the |
| 982 | /// base address to use for accessing globals into a register. |
| 983 | /// |
Evan Cheng | 9ade218 | 2006-08-26 05:34:46 +0000 | [diff] [blame] | 984 | SDNode *X86DAGToDAGISel::getGlobalBaseReg() { |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 985 | assert(!Subtarget->is64Bit() && "X86-64 PIC uses RIP relative addressing"); |
Evan Cheng | 7ccced6 | 2006-02-18 00:15:05 +0000 | [diff] [blame] | 986 | if (!GlobalBaseReg) { |
| 987 | // Insert the set of GlobalBaseReg into the first MBB of the function |
| 988 | MachineBasicBlock &FirstMBB = BB->getParent()->front(); |
| 989 | MachineBasicBlock::iterator MBBI = FirstMBB.begin(); |
| 990 | SSARegMap *RegMap = BB->getParent()->getSSARegMap(); |
Evan Cheng | 069287d | 2006-05-16 07:21:53 +0000 | [diff] [blame] | 991 | GlobalBaseReg = RegMap->createVirtualRegister(X86::GR32RegisterClass); |
Evan Cheng | c0f64ff | 2006-11-27 23:37:22 +0000 | [diff] [blame] | 992 | const TargetInstrInfo *TII = TM.getInstrInfo(); |
| 993 | BuildMI(FirstMBB, MBBI, TII->get(X86::MovePCtoStack)); |
| 994 | BuildMI(FirstMBB, MBBI, TII->get(X86::POP32r), GlobalBaseReg); |
Evan Cheng | 7ccced6 | 2006-02-18 00:15:05 +0000 | [diff] [blame] | 995 | } |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 996 | return CurDAG->getRegister(GlobalBaseReg, TLI.getPointerTy()).Val; |
Evan Cheng | 7ccced6 | 2006-02-18 00:15:05 +0000 | [diff] [blame] | 997 | } |
| 998 | |
Evan Cheng | b245d92 | 2006-05-20 01:36:52 +0000 | [diff] [blame] | 999 | static SDNode *FindCallStartFromCall(SDNode *Node) { |
| 1000 | if (Node->getOpcode() == ISD::CALLSEQ_START) return Node; |
| 1001 | assert(Node->getOperand(0).getValueType() == MVT::Other && |
| 1002 | "Node doesn't have a token chain argument!"); |
| 1003 | return FindCallStartFromCall(Node->getOperand(0).Val); |
| 1004 | } |
| 1005 | |
Evan Cheng | 9ade218 | 2006-08-26 05:34:46 +0000 | [diff] [blame] | 1006 | SDNode *X86DAGToDAGISel::Select(SDOperand N) { |
Evan Cheng | def941b | 2005-12-15 01:02:48 +0000 | [diff] [blame] | 1007 | SDNode *Node = N.Val; |
| 1008 | MVT::ValueType NVT = Node->getValueType(0); |
Evan Cheng | 0114e94 | 2006-01-06 20:36:21 +0000 | [diff] [blame] | 1009 | unsigned Opc, MOpc; |
| 1010 | unsigned Opcode = Node->getOpcode(); |
Chris Lattner | c961eea | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 1011 | |
Evan Cheng | f597dc7 | 2006-02-10 22:24:32 +0000 | [diff] [blame] | 1012 | #ifndef NDEBUG |
Bill Wendling | 6345d75 | 2006-11-17 07:52:03 +0000 | [diff] [blame] | 1013 | DOUT << std::string(Indent, ' ') << "Selecting: "; |
Evan Cheng | f597dc7 | 2006-02-10 22:24:32 +0000 | [diff] [blame] | 1014 | DEBUG(Node->dump(CurDAG)); |
Bill Wendling | 6345d75 | 2006-11-17 07:52:03 +0000 | [diff] [blame] | 1015 | DOUT << "\n"; |
Evan Cheng | 23addc0 | 2006-02-10 22:46:26 +0000 | [diff] [blame] | 1016 | Indent += 2; |
Evan Cheng | f597dc7 | 2006-02-10 22:24:32 +0000 | [diff] [blame] | 1017 | #endif |
| 1018 | |
Evan Cheng | 3416721 | 2006-02-09 00:37:58 +0000 | [diff] [blame] | 1019 | if (Opcode >= ISD::BUILTIN_OP_END && Opcode < X86ISD::FIRST_NUMBER) { |
Evan Cheng | f597dc7 | 2006-02-10 22:24:32 +0000 | [diff] [blame] | 1020 | #ifndef NDEBUG |
Bill Wendling | 6345d75 | 2006-11-17 07:52:03 +0000 | [diff] [blame] | 1021 | DOUT << std::string(Indent-2, ' ') << "== "; |
Evan Cheng | f597dc7 | 2006-02-10 22:24:32 +0000 | [diff] [blame] | 1022 | DEBUG(Node->dump(CurDAG)); |
Bill Wendling | 6345d75 | 2006-11-17 07:52:03 +0000 | [diff] [blame] | 1023 | DOUT << "\n"; |
Evan Cheng | 23addc0 | 2006-02-10 22:46:26 +0000 | [diff] [blame] | 1024 | Indent -= 2; |
Evan Cheng | f597dc7 | 2006-02-10 22:24:32 +0000 | [diff] [blame] | 1025 | #endif |
Evan Cheng | 64a752f | 2006-08-11 09:08:15 +0000 | [diff] [blame] | 1026 | return NULL; // Already selected. |
Evan Cheng | 3416721 | 2006-02-09 00:37:58 +0000 | [diff] [blame] | 1027 | } |
Evan Cheng | 38262ca | 2006-01-11 22:15:18 +0000 | [diff] [blame] | 1028 | |
Evan Cheng | 0114e94 | 2006-01-06 20:36:21 +0000 | [diff] [blame] | 1029 | switch (Opcode) { |
Chris Lattner | c961eea | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 1030 | default: break; |
Evan Cheng | 020d2e8 | 2006-02-23 20:41:18 +0000 | [diff] [blame] | 1031 | case X86ISD::GlobalBaseReg: |
Evan Cheng | 9ade218 | 2006-08-26 05:34:46 +0000 | [diff] [blame] | 1032 | return getGlobalBaseReg(); |
Evan Cheng | 020d2e8 | 2006-02-23 20:41:18 +0000 | [diff] [blame] | 1033 | |
Evan Cheng | 51a9ed9 | 2006-02-25 10:09:08 +0000 | [diff] [blame] | 1034 | case ISD::ADD: { |
| 1035 | // Turn ADD X, c to MOV32ri X+c. This cannot be done with tblgen'd |
| 1036 | // code and is matched first so to prevent it from being turned into |
| 1037 | // LEA32r X+c. |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 1038 | // In 64-bit mode, use LEA to take advantage of RIP-relative addressing. |
| 1039 | MVT::ValueType PtrVT = TLI.getPointerTy(); |
Evan Cheng | 51a9ed9 | 2006-02-25 10:09:08 +0000 | [diff] [blame] | 1040 | SDOperand N0 = N.getOperand(0); |
| 1041 | SDOperand N1 = N.getOperand(1); |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 1042 | if (N.Val->getValueType(0) == PtrVT && |
Evan Cheng | 51a9ed9 | 2006-02-25 10:09:08 +0000 | [diff] [blame] | 1043 | N0.getOpcode() == X86ISD::Wrapper && |
| 1044 | N1.getOpcode() == ISD::Constant) { |
| 1045 | unsigned Offset = (unsigned)cast<ConstantSDNode>(N1)->getValue(); |
| 1046 | SDOperand C(0, 0); |
| 1047 | // TODO: handle ExternalSymbolSDNode. |
| 1048 | if (GlobalAddressSDNode *G = |
| 1049 | dyn_cast<GlobalAddressSDNode>(N0.getOperand(0))) { |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 1050 | C = CurDAG->getTargetGlobalAddress(G->getGlobal(), PtrVT, |
Evan Cheng | 51a9ed9 | 2006-02-25 10:09:08 +0000 | [diff] [blame] | 1051 | G->getOffset() + Offset); |
| 1052 | } else if (ConstantPoolSDNode *CP = |
| 1053 | dyn_cast<ConstantPoolSDNode>(N0.getOperand(0))) { |
Evan Cheng | c356a57 | 2006-09-12 21:04:05 +0000 | [diff] [blame] | 1054 | C = CurDAG->getTargetConstantPool(CP->getConstVal(), PtrVT, |
Evan Cheng | 51a9ed9 | 2006-02-25 10:09:08 +0000 | [diff] [blame] | 1055 | CP->getAlignment(), |
| 1056 | CP->getOffset()+Offset); |
| 1057 | } |
| 1058 | |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 1059 | if (C.Val) { |
| 1060 | if (Subtarget->is64Bit()) { |
| 1061 | SDOperand Ops[] = { CurDAG->getRegister(0, PtrVT), getI8Imm(1), |
| 1062 | CurDAG->getRegister(0, PtrVT), C }; |
| 1063 | return CurDAG->SelectNodeTo(N.Val, X86::LEA64r, MVT::i64, Ops, 4); |
| 1064 | } else |
| 1065 | return CurDAG->SelectNodeTo(N.Val, X86::MOV32ri, PtrVT, C); |
| 1066 | } |
Evan Cheng | 51a9ed9 | 2006-02-25 10:09:08 +0000 | [diff] [blame] | 1067 | } |
| 1068 | |
| 1069 | // Other cases are handled by auto-generated code. |
| 1070 | break; |
Evan Cheng | a0ea053 | 2006-02-23 02:43:52 +0000 | [diff] [blame] | 1071 | } |
Evan Cheng | 020d2e8 | 2006-02-23 20:41:18 +0000 | [diff] [blame] | 1072 | |
Evan Cheng | 0114e94 | 2006-01-06 20:36:21 +0000 | [diff] [blame] | 1073 | case ISD::MULHU: |
| 1074 | case ISD::MULHS: { |
| 1075 | if (Opcode == ISD::MULHU) |
| 1076 | switch (NVT) { |
| 1077 | default: assert(0 && "Unsupported VT!"); |
| 1078 | case MVT::i8: Opc = X86::MUL8r; MOpc = X86::MUL8m; break; |
| 1079 | case MVT::i16: Opc = X86::MUL16r; MOpc = X86::MUL16m; break; |
| 1080 | case MVT::i32: Opc = X86::MUL32r; MOpc = X86::MUL32m; break; |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 1081 | case MVT::i64: Opc = X86::MUL64r; MOpc = X86::MUL64m; break; |
Evan Cheng | 0114e94 | 2006-01-06 20:36:21 +0000 | [diff] [blame] | 1082 | } |
| 1083 | else |
| 1084 | switch (NVT) { |
| 1085 | default: assert(0 && "Unsupported VT!"); |
| 1086 | case MVT::i8: Opc = X86::IMUL8r; MOpc = X86::IMUL8m; break; |
| 1087 | case MVT::i16: Opc = X86::IMUL16r; MOpc = X86::IMUL16m; break; |
| 1088 | case MVT::i32: Opc = X86::IMUL32r; MOpc = X86::IMUL32m; break; |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 1089 | case MVT::i64: Opc = X86::IMUL64r; MOpc = X86::IMUL64m; break; |
Evan Cheng | 0114e94 | 2006-01-06 20:36:21 +0000 | [diff] [blame] | 1090 | } |
| 1091 | |
| 1092 | unsigned LoReg, HiReg; |
| 1093 | switch (NVT) { |
| 1094 | default: assert(0 && "Unsupported VT!"); |
| 1095 | case MVT::i8: LoReg = X86::AL; HiReg = X86::AH; break; |
| 1096 | case MVT::i16: LoReg = X86::AX; HiReg = X86::DX; break; |
| 1097 | case MVT::i32: LoReg = X86::EAX; HiReg = X86::EDX; break; |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 1098 | case MVT::i64: LoReg = X86::RAX; HiReg = X86::RDX; break; |
Evan Cheng | 0114e94 | 2006-01-06 20:36:21 +0000 | [diff] [blame] | 1099 | } |
| 1100 | |
| 1101 | SDOperand N0 = Node->getOperand(0); |
| 1102 | SDOperand N1 = Node->getOperand(1); |
| 1103 | |
| 1104 | bool foldedLoad = false; |
| 1105 | SDOperand Tmp0, Tmp1, Tmp2, Tmp3; |
Evan Cheng | 5e35168 | 2006-02-06 06:02:33 +0000 | [diff] [blame] | 1106 | foldedLoad = TryFoldLoad(N, N1, Tmp0, Tmp1, Tmp2, Tmp3); |
Evan Cheng | 948f343 | 2006-01-06 23:19:29 +0000 | [diff] [blame] | 1107 | // MULHU and MULHS are commmutative |
| 1108 | if (!foldedLoad) { |
Evan Cheng | 5e35168 | 2006-02-06 06:02:33 +0000 | [diff] [blame] | 1109 | foldedLoad = TryFoldLoad(N, N0, Tmp0, Tmp1, Tmp2, Tmp3); |
Evan Cheng | 948f343 | 2006-01-06 23:19:29 +0000 | [diff] [blame] | 1110 | if (foldedLoad) { |
| 1111 | N0 = Node->getOperand(1); |
| 1112 | N1 = Node->getOperand(0); |
| 1113 | } |
| 1114 | } |
| 1115 | |
Evan Cheng | 3416721 | 2006-02-09 00:37:58 +0000 | [diff] [blame] | 1116 | SDOperand Chain; |
Evan Cheng | 0469990 | 2006-08-26 01:05:16 +0000 | [diff] [blame] | 1117 | if (foldedLoad) { |
| 1118 | Chain = N1.getOperand(0); |
| 1119 | AddToISelQueue(Chain); |
| 1120 | } else |
Evan Cheng | 3416721 | 2006-02-09 00:37:58 +0000 | [diff] [blame] | 1121 | Chain = CurDAG->getEntryNode(); |
Evan Cheng | 0114e94 | 2006-01-06 20:36:21 +0000 | [diff] [blame] | 1122 | |
Evan Cheng | 3416721 | 2006-02-09 00:37:58 +0000 | [diff] [blame] | 1123 | SDOperand InFlag(0, 0); |
Evan Cheng | 0469990 | 2006-08-26 01:05:16 +0000 | [diff] [blame] | 1124 | AddToISelQueue(N0); |
Evan Cheng | 0114e94 | 2006-01-06 20:36:21 +0000 | [diff] [blame] | 1125 | Chain = CurDAG->getCopyToReg(Chain, CurDAG->getRegister(LoReg, NVT), |
Evan Cheng | 3416721 | 2006-02-09 00:37:58 +0000 | [diff] [blame] | 1126 | N0, InFlag); |
Evan Cheng | 0114e94 | 2006-01-06 20:36:21 +0000 | [diff] [blame] | 1127 | InFlag = Chain.getValue(1); |
| 1128 | |
| 1129 | if (foldedLoad) { |
Evan Cheng | 0469990 | 2006-08-26 01:05:16 +0000 | [diff] [blame] | 1130 | AddToISelQueue(Tmp0); |
| 1131 | AddToISelQueue(Tmp1); |
| 1132 | AddToISelQueue(Tmp2); |
| 1133 | AddToISelQueue(Tmp3); |
Evan Cheng | 0b828e0 | 2006-08-27 08:14:06 +0000 | [diff] [blame] | 1134 | SDOperand Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Chain, InFlag }; |
Evan Cheng | 7e9b26f | 2006-02-09 07:17:49 +0000 | [diff] [blame] | 1135 | SDNode *CNode = |
Evan Cheng | 0b828e0 | 2006-08-27 08:14:06 +0000 | [diff] [blame] | 1136 | CurDAG->getTargetNode(MOpc, MVT::Other, MVT::Flag, Ops, 6); |
Evan Cheng | 7e9b26f | 2006-02-09 07:17:49 +0000 | [diff] [blame] | 1137 | Chain = SDOperand(CNode, 0); |
| 1138 | InFlag = SDOperand(CNode, 1); |
Evan Cheng | 0114e94 | 2006-01-06 20:36:21 +0000 | [diff] [blame] | 1139 | } else { |
Evan Cheng | 0469990 | 2006-08-26 01:05:16 +0000 | [diff] [blame] | 1140 | AddToISelQueue(N1); |
Evan Cheng | 7e9b26f | 2006-02-09 07:17:49 +0000 | [diff] [blame] | 1141 | InFlag = |
| 1142 | SDOperand(CurDAG->getTargetNode(Opc, MVT::Flag, N1, InFlag), 0); |
Evan Cheng | 0114e94 | 2006-01-06 20:36:21 +0000 | [diff] [blame] | 1143 | } |
| 1144 | |
Evan Cheng | 9ade218 | 2006-08-26 05:34:46 +0000 | [diff] [blame] | 1145 | SDOperand Result = CurDAG->getCopyFromReg(Chain, HiReg, NVT, InFlag); |
Evan Cheng | 2ef88a0 | 2006-08-07 22:28:20 +0000 | [diff] [blame] | 1146 | ReplaceUses(N.getValue(0), Result); |
| 1147 | if (foldedLoad) |
| 1148 | ReplaceUses(N1.getValue(1), Result.getValue(1)); |
Evan Cheng | 3416721 | 2006-02-09 00:37:58 +0000 | [diff] [blame] | 1149 | |
Evan Cheng | f597dc7 | 2006-02-10 22:24:32 +0000 | [diff] [blame] | 1150 | #ifndef NDEBUG |
Bill Wendling | 6345d75 | 2006-11-17 07:52:03 +0000 | [diff] [blame] | 1151 | DOUT << std::string(Indent-2, ' ') << "=> "; |
Evan Cheng | f597dc7 | 2006-02-10 22:24:32 +0000 | [diff] [blame] | 1152 | DEBUG(Result.Val->dump(CurDAG)); |
Bill Wendling | 6345d75 | 2006-11-17 07:52:03 +0000 | [diff] [blame] | 1153 | DOUT << "\n"; |
Evan Cheng | 23addc0 | 2006-02-10 22:46:26 +0000 | [diff] [blame] | 1154 | Indent -= 2; |
Evan Cheng | f597dc7 | 2006-02-10 22:24:32 +0000 | [diff] [blame] | 1155 | #endif |
Evan Cheng | 64a752f | 2006-08-11 09:08:15 +0000 | [diff] [blame] | 1156 | return NULL; |
Evan Cheng | 948f343 | 2006-01-06 23:19:29 +0000 | [diff] [blame] | 1157 | } |
Evan Cheng | 7ccced6 | 2006-02-18 00:15:05 +0000 | [diff] [blame] | 1158 | |
Evan Cheng | 948f343 | 2006-01-06 23:19:29 +0000 | [diff] [blame] | 1159 | case ISD::SDIV: |
| 1160 | case ISD::UDIV: |
| 1161 | case ISD::SREM: |
| 1162 | case ISD::UREM: { |
| 1163 | bool isSigned = Opcode == ISD::SDIV || Opcode == ISD::SREM; |
| 1164 | bool isDiv = Opcode == ISD::SDIV || Opcode == ISD::UDIV; |
| 1165 | if (!isSigned) |
| 1166 | switch (NVT) { |
| 1167 | default: assert(0 && "Unsupported VT!"); |
| 1168 | case MVT::i8: Opc = X86::DIV8r; MOpc = X86::DIV8m; break; |
| 1169 | case MVT::i16: Opc = X86::DIV16r; MOpc = X86::DIV16m; break; |
| 1170 | case MVT::i32: Opc = X86::DIV32r; MOpc = X86::DIV32m; break; |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 1171 | case MVT::i64: Opc = X86::DIV64r; MOpc = X86::DIV64m; break; |
Evan Cheng | 948f343 | 2006-01-06 23:19:29 +0000 | [diff] [blame] | 1172 | } |
| 1173 | else |
| 1174 | switch (NVT) { |
| 1175 | default: assert(0 && "Unsupported VT!"); |
| 1176 | case MVT::i8: Opc = X86::IDIV8r; MOpc = X86::IDIV8m; break; |
| 1177 | case MVT::i16: Opc = X86::IDIV16r; MOpc = X86::IDIV16m; break; |
| 1178 | case MVT::i32: Opc = X86::IDIV32r; MOpc = X86::IDIV32m; break; |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 1179 | case MVT::i64: Opc = X86::IDIV64r; MOpc = X86::IDIV64m; break; |
Evan Cheng | 948f343 | 2006-01-06 23:19:29 +0000 | [diff] [blame] | 1180 | } |
| 1181 | |
| 1182 | unsigned LoReg, HiReg; |
| 1183 | unsigned ClrOpcode, SExtOpcode; |
| 1184 | switch (NVT) { |
| 1185 | default: assert(0 && "Unsupported VT!"); |
| 1186 | case MVT::i8: |
| 1187 | LoReg = X86::AL; HiReg = X86::AH; |
Evan Cheng | b1409ce | 2006-11-17 22:10:14 +0000 | [diff] [blame] | 1188 | ClrOpcode = 0; |
Evan Cheng | 948f343 | 2006-01-06 23:19:29 +0000 | [diff] [blame] | 1189 | SExtOpcode = X86::CBW; |
| 1190 | break; |
| 1191 | case MVT::i16: |
| 1192 | LoReg = X86::AX; HiReg = X86::DX; |
Evan Cheng | aede9b9 | 2006-06-02 21:20:34 +0000 | [diff] [blame] | 1193 | ClrOpcode = X86::MOV16r0; |
Evan Cheng | 948f343 | 2006-01-06 23:19:29 +0000 | [diff] [blame] | 1194 | SExtOpcode = X86::CWD; |
| 1195 | break; |
| 1196 | case MVT::i32: |
| 1197 | LoReg = X86::EAX; HiReg = X86::EDX; |
Evan Cheng | aede9b9 | 2006-06-02 21:20:34 +0000 | [diff] [blame] | 1198 | ClrOpcode = X86::MOV32r0; |
Evan Cheng | 948f343 | 2006-01-06 23:19:29 +0000 | [diff] [blame] | 1199 | SExtOpcode = X86::CDQ; |
| 1200 | break; |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 1201 | case MVT::i64: |
| 1202 | LoReg = X86::RAX; HiReg = X86::RDX; |
| 1203 | ClrOpcode = X86::MOV64r0; |
| 1204 | SExtOpcode = X86::CQO; |
| 1205 | break; |
Evan Cheng | 948f343 | 2006-01-06 23:19:29 +0000 | [diff] [blame] | 1206 | } |
| 1207 | |
| 1208 | SDOperand N0 = Node->getOperand(0); |
| 1209 | SDOperand N1 = Node->getOperand(1); |
Evan Cheng | 3416721 | 2006-02-09 00:37:58 +0000 | [diff] [blame] | 1210 | SDOperand InFlag(0, 0); |
Evan Cheng | b1409ce | 2006-11-17 22:10:14 +0000 | [diff] [blame] | 1211 | if (NVT == MVT::i8 && !isSigned) { |
| 1212 | // Special case for div8, just use a move with zero extension to AX to |
| 1213 | // clear the upper 8 bits (AH). |
| 1214 | SDOperand Tmp0, Tmp1, Tmp2, Tmp3, Move, Chain; |
| 1215 | if (TryFoldLoad(N, N0, Tmp0, Tmp1, Tmp2, Tmp3)) { |
| 1216 | SDOperand Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, N0.getOperand(0) }; |
| 1217 | AddToISelQueue(N0.getOperand(0)); |
| 1218 | AddToISelQueue(Tmp0); |
| 1219 | AddToISelQueue(Tmp1); |
| 1220 | AddToISelQueue(Tmp2); |
| 1221 | AddToISelQueue(Tmp3); |
| 1222 | Move = |
| 1223 | SDOperand(CurDAG->getTargetNode(X86::MOVZX16rm8, MVT::i16, MVT::Other, |
| 1224 | Ops, 5), 0); |
| 1225 | Chain = Move.getValue(1); |
| 1226 | ReplaceUses(N0.getValue(1), Chain); |
| 1227 | } else { |
| 1228 | AddToISelQueue(N0); |
| 1229 | Move = |
| 1230 | SDOperand(CurDAG->getTargetNode(X86::MOVZX16rr8, MVT::i16, N0), 0); |
| 1231 | Chain = CurDAG->getEntryNode(); |
| 1232 | } |
| 1233 | Chain = CurDAG->getCopyToReg(Chain, X86::AX, Move, InFlag); |
Evan Cheng | 948f343 | 2006-01-06 23:19:29 +0000 | [diff] [blame] | 1234 | InFlag = Chain.getValue(1); |
Evan Cheng | b1409ce | 2006-11-17 22:10:14 +0000 | [diff] [blame] | 1235 | } else { |
| 1236 | AddToISelQueue(N0); |
| 1237 | InFlag = |
| 1238 | CurDAG->getCopyToReg(CurDAG->getEntryNode(), LoReg, N0, |
| 1239 | InFlag).getValue(1); |
| 1240 | if (isSigned) { |
| 1241 | // Sign extend the low part into the high part. |
| 1242 | InFlag = |
| 1243 | SDOperand(CurDAG->getTargetNode(SExtOpcode, MVT::Flag, InFlag), 0); |
| 1244 | } else { |
| 1245 | // Zero out the high part, effectively zero extending the input. |
| 1246 | SDOperand ClrNode = SDOperand(CurDAG->getTargetNode(ClrOpcode, NVT), 0); |
| 1247 | InFlag = CurDAG->getCopyToReg(CurDAG->getEntryNode(), HiReg, ClrNode, |
| 1248 | InFlag).getValue(1); |
| 1249 | } |
Evan Cheng | 948f343 | 2006-01-06 23:19:29 +0000 | [diff] [blame] | 1250 | } |
| 1251 | |
Evan Cheng | b1409ce | 2006-11-17 22:10:14 +0000 | [diff] [blame] | 1252 | SDOperand Tmp0, Tmp1, Tmp2, Tmp3, Chain; |
| 1253 | bool foldedLoad = TryFoldLoad(N, N1, Tmp0, Tmp1, Tmp2, Tmp3); |
Evan Cheng | 948f343 | 2006-01-06 23:19:29 +0000 | [diff] [blame] | 1254 | if (foldedLoad) { |
Evan Cheng | b1409ce | 2006-11-17 22:10:14 +0000 | [diff] [blame] | 1255 | AddToISelQueue(N1.getOperand(0)); |
Evan Cheng | 0469990 | 2006-08-26 01:05:16 +0000 | [diff] [blame] | 1256 | AddToISelQueue(Tmp0); |
| 1257 | AddToISelQueue(Tmp1); |
| 1258 | AddToISelQueue(Tmp2); |
| 1259 | AddToISelQueue(Tmp3); |
Evan Cheng | b1409ce | 2006-11-17 22:10:14 +0000 | [diff] [blame] | 1260 | SDOperand Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, N1.getOperand(0), InFlag }; |
Evan Cheng | 7e9b26f | 2006-02-09 07:17:49 +0000 | [diff] [blame] | 1261 | SDNode *CNode = |
Evan Cheng | 0b828e0 | 2006-08-27 08:14:06 +0000 | [diff] [blame] | 1262 | CurDAG->getTargetNode(MOpc, MVT::Other, MVT::Flag, Ops, 6); |
Evan Cheng | 7e9b26f | 2006-02-09 07:17:49 +0000 | [diff] [blame] | 1263 | Chain = SDOperand(CNode, 0); |
| 1264 | InFlag = SDOperand(CNode, 1); |
Evan Cheng | 948f343 | 2006-01-06 23:19:29 +0000 | [diff] [blame] | 1265 | } else { |
Evan Cheng | 0469990 | 2006-08-26 01:05:16 +0000 | [diff] [blame] | 1266 | AddToISelQueue(N1); |
Evan Cheng | b1409ce | 2006-11-17 22:10:14 +0000 | [diff] [blame] | 1267 | Chain = CurDAG->getEntryNode(); |
Evan Cheng | 7e9b26f | 2006-02-09 07:17:49 +0000 | [diff] [blame] | 1268 | InFlag = |
| 1269 | SDOperand(CurDAG->getTargetNode(Opc, MVT::Flag, N1, InFlag), 0); |
Evan Cheng | 948f343 | 2006-01-06 23:19:29 +0000 | [diff] [blame] | 1270 | } |
| 1271 | |
Evan Cheng | b1409ce | 2006-11-17 22:10:14 +0000 | [diff] [blame] | 1272 | SDOperand Result = |
| 1273 | CurDAG->getCopyFromReg(Chain, isDiv ? LoReg : HiReg, NVT, InFlag); |
Evan Cheng | 2ef88a0 | 2006-08-07 22:28:20 +0000 | [diff] [blame] | 1274 | ReplaceUses(N.getValue(0), Result); |
| 1275 | if (foldedLoad) |
| 1276 | ReplaceUses(N1.getValue(1), Result.getValue(1)); |
Evan Cheng | f597dc7 | 2006-02-10 22:24:32 +0000 | [diff] [blame] | 1277 | |
| 1278 | #ifndef NDEBUG |
Bill Wendling | 6345d75 | 2006-11-17 07:52:03 +0000 | [diff] [blame] | 1279 | DOUT << std::string(Indent-2, ' ') << "=> "; |
Evan Cheng | f597dc7 | 2006-02-10 22:24:32 +0000 | [diff] [blame] | 1280 | DEBUG(Result.Val->dump(CurDAG)); |
Bill Wendling | 6345d75 | 2006-11-17 07:52:03 +0000 | [diff] [blame] | 1281 | DOUT << "\n"; |
Evan Cheng | 23addc0 | 2006-02-10 22:46:26 +0000 | [diff] [blame] | 1282 | Indent -= 2; |
Evan Cheng | f597dc7 | 2006-02-10 22:24:32 +0000 | [diff] [blame] | 1283 | #endif |
Evan Cheng | 64a752f | 2006-08-11 09:08:15 +0000 | [diff] [blame] | 1284 | |
| 1285 | return NULL; |
Evan Cheng | 0114e94 | 2006-01-06 20:36:21 +0000 | [diff] [blame] | 1286 | } |
Evan Cheng | 403be7e | 2006-05-08 08:01:26 +0000 | [diff] [blame] | 1287 | |
| 1288 | case ISD::TRUNCATE: { |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 1289 | if (!Subtarget->is64Bit() && NVT == MVT::i8) { |
Evan Cheng | 403be7e | 2006-05-08 08:01:26 +0000 | [diff] [blame] | 1290 | unsigned Opc2; |
| 1291 | MVT::ValueType VT; |
| 1292 | switch (Node->getOperand(0).getValueType()) { |
| 1293 | default: assert(0 && "Unknown truncate!"); |
| 1294 | case MVT::i16: |
| 1295 | Opc = X86::MOV16to16_; |
| 1296 | VT = MVT::i16; |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 1297 | Opc2 = X86::TRUNC_16_to8; |
Evan Cheng | 403be7e | 2006-05-08 08:01:26 +0000 | [diff] [blame] | 1298 | break; |
| 1299 | case MVT::i32: |
| 1300 | Opc = X86::MOV32to32_; |
| 1301 | VT = MVT::i32; |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 1302 | Opc2 = X86::TRUNC_32_to8; |
Evan Cheng | 403be7e | 2006-05-08 08:01:26 +0000 | [diff] [blame] | 1303 | break; |
| 1304 | } |
| 1305 | |
Evan Cheng | 0469990 | 2006-08-26 01:05:16 +0000 | [diff] [blame] | 1306 | AddToISelQueue(Node->getOperand(0)); |
| 1307 | SDOperand Tmp = |
| 1308 | SDOperand(CurDAG->getTargetNode(Opc, VT, Node->getOperand(0)), 0); |
Evan Cheng | 9ade218 | 2006-08-26 05:34:46 +0000 | [diff] [blame] | 1309 | SDNode *ResNode = CurDAG->getTargetNode(Opc2, NVT, Tmp); |
Evan Cheng | 403be7e | 2006-05-08 08:01:26 +0000 | [diff] [blame] | 1310 | |
| 1311 | #ifndef NDEBUG |
Bill Wendling | 6345d75 | 2006-11-17 07:52:03 +0000 | [diff] [blame] | 1312 | DOUT << std::string(Indent-2, ' ') << "=> "; |
Evan Cheng | 9ade218 | 2006-08-26 05:34:46 +0000 | [diff] [blame] | 1313 | DEBUG(ResNode->dump(CurDAG)); |
Bill Wendling | 6345d75 | 2006-11-17 07:52:03 +0000 | [diff] [blame] | 1314 | DOUT << "\n"; |
Evan Cheng | 403be7e | 2006-05-08 08:01:26 +0000 | [diff] [blame] | 1315 | Indent -= 2; |
| 1316 | #endif |
Evan Cheng | 9ade218 | 2006-08-26 05:34:46 +0000 | [diff] [blame] | 1317 | return ResNode; |
Evan Cheng | 403be7e | 2006-05-08 08:01:26 +0000 | [diff] [blame] | 1318 | } |
Evan Cheng | 6b2e254 | 2006-05-20 07:44:28 +0000 | [diff] [blame] | 1319 | |
| 1320 | break; |
Evan Cheng | 403be7e | 2006-05-08 08:01:26 +0000 | [diff] [blame] | 1321 | } |
Chris Lattner | c961eea | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 1322 | } |
| 1323 | |
Evan Cheng | 9ade218 | 2006-08-26 05:34:46 +0000 | [diff] [blame] | 1324 | SDNode *ResNode = SelectCode(N); |
Evan Cheng | 64a752f | 2006-08-11 09:08:15 +0000 | [diff] [blame] | 1325 | |
Evan Cheng | f597dc7 | 2006-02-10 22:24:32 +0000 | [diff] [blame] | 1326 | #ifndef NDEBUG |
Bill Wendling | 6345d75 | 2006-11-17 07:52:03 +0000 | [diff] [blame] | 1327 | DOUT << std::string(Indent-2, ' ') << "=> "; |
Evan Cheng | 9ade218 | 2006-08-26 05:34:46 +0000 | [diff] [blame] | 1328 | if (ResNode == NULL || ResNode == N.Val) |
| 1329 | DEBUG(N.Val->dump(CurDAG)); |
| 1330 | else |
| 1331 | DEBUG(ResNode->dump(CurDAG)); |
Bill Wendling | 6345d75 | 2006-11-17 07:52:03 +0000 | [diff] [blame] | 1332 | DOUT << "\n"; |
Evan Cheng | 23addc0 | 2006-02-10 22:46:26 +0000 | [diff] [blame] | 1333 | Indent -= 2; |
Evan Cheng | f597dc7 | 2006-02-10 22:24:32 +0000 | [diff] [blame] | 1334 | #endif |
Evan Cheng | 64a752f | 2006-08-11 09:08:15 +0000 | [diff] [blame] | 1335 | |
| 1336 | return ResNode; |
Chris Lattner | c961eea | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 1337 | } |
| 1338 | |
Chris Lattner | c0bad57 | 2006-06-08 18:03:49 +0000 | [diff] [blame] | 1339 | bool X86DAGToDAGISel:: |
| 1340 | SelectInlineAsmMemoryOperand(const SDOperand &Op, char ConstraintCode, |
| 1341 | std::vector<SDOperand> &OutOps, SelectionDAG &DAG){ |
| 1342 | SDOperand Op0, Op1, Op2, Op3; |
| 1343 | switch (ConstraintCode) { |
| 1344 | case 'o': // offsetable ?? |
| 1345 | case 'v': // not offsetable ?? |
| 1346 | default: return true; |
| 1347 | case 'm': // memory |
Evan Cheng | 0d53826 | 2006-11-08 20:34:28 +0000 | [diff] [blame] | 1348 | if (!SelectAddr(Op, Op, Op0, Op1, Op2, Op3)) |
Chris Lattner | c0bad57 | 2006-06-08 18:03:49 +0000 | [diff] [blame] | 1349 | return true; |
| 1350 | break; |
| 1351 | } |
| 1352 | |
Evan Cheng | 0469990 | 2006-08-26 01:05:16 +0000 | [diff] [blame] | 1353 | OutOps.push_back(Op0); |
| 1354 | OutOps.push_back(Op1); |
| 1355 | OutOps.push_back(Op2); |
| 1356 | OutOps.push_back(Op3); |
| 1357 | AddToISelQueue(Op0); |
| 1358 | AddToISelQueue(Op1); |
| 1359 | AddToISelQueue(Op2); |
| 1360 | AddToISelQueue(Op3); |
Chris Lattner | c0bad57 | 2006-06-08 18:03:49 +0000 | [diff] [blame] | 1361 | return false; |
| 1362 | } |
| 1363 | |
Chris Lattner | c961eea | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 1364 | /// createX86ISelDag - This pass converts a legalized DAG into a |
| 1365 | /// X86-specific DAG, ready for instruction scheduling. |
| 1366 | /// |
Evan Cheng | e50794a | 2006-08-29 18:28:33 +0000 | [diff] [blame] | 1367 | FunctionPass *llvm::createX86ISelDag(X86TargetMachine &TM, bool Fast) { |
| 1368 | return new X86DAGToDAGISel(TM, Fast); |
Chris Lattner | c961eea | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 1369 | } |