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 | // |
Chris Lattner | 4ee451d | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Chris Lattner | c961eea | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 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" |
Evan Cheng | 0475ab5 | 2008-01-05 00:41:47 +0000 | [diff] [blame] | 19 | #include "X86MachineFunctionInfo.h" |
Chris Lattner | 92cb0af | 2006-01-11 01:15:34 +0000 | [diff] [blame] | 20 | #include "X86RegisterInfo.h" |
Chris Lattner | c961eea | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 21 | #include "X86Subtarget.h" |
Evan Cheng | c4c6257 | 2006-03-13 23:20:37 +0000 | [diff] [blame] | 22 | #include "X86TargetMachine.h" |
Chris Lattner | f9ce9fb | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 23 | #include "llvm/GlobalValue.h" |
Chris Lattner | 92cb0af | 2006-01-11 01:15:34 +0000 | [diff] [blame] | 24 | #include "llvm/Instructions.h" |
Chris Lattner | 420736d | 2006-03-25 06:47:10 +0000 | [diff] [blame] | 25 | #include "llvm/Intrinsics.h" |
Chris Lattner | 92cb0af | 2006-01-11 01:15:34 +0000 | [diff] [blame] | 26 | #include "llvm/Support/CFG.h" |
Reid Spencer | 7aa8a45 | 2007-01-12 23:22:14 +0000 | [diff] [blame] | 27 | #include "llvm/Type.h" |
Chris Lattner | f9ce9fb | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 28 | #include "llvm/CodeGen/MachineConstantPool.h" |
Chris Lattner | c961eea | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 29 | #include "llvm/CodeGen/MachineFunction.h" |
Evan Cheng | aaca22c | 2006-01-10 20:26:56 +0000 | [diff] [blame] | 30 | #include "llvm/CodeGen/MachineFrameInfo.h" |
Chris Lattner | 92cb0af | 2006-01-11 01:15:34 +0000 | [diff] [blame] | 31 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
Chris Lattner | 84bc542 | 2007-12-31 04:13:23 +0000 | [diff] [blame] | 32 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Chris Lattner | c961eea | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 33 | #include "llvm/CodeGen/SelectionDAGISel.h" |
| 34 | #include "llvm/Target/TargetMachine.h" |
Evan Cheng | e9c608d | 2008-02-19 23:36:51 +0000 | [diff] [blame] | 35 | #include "llvm/Support/CommandLine.h" |
Chris Lattner | a4f0b3a | 2006-08-27 12:54:02 +0000 | [diff] [blame] | 36 | #include "llvm/Support/Compiler.h" |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 37 | #include "llvm/Support/Debug.h" |
| 38 | #include "llvm/Support/MathExtras.h" |
Chris Lattner | c961eea | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 39 | #include "llvm/ADT/Statistic.h" |
Evan Cheng | 2ef88a0 | 2006-08-07 22:28:20 +0000 | [diff] [blame] | 40 | #include <queue> |
Evan Cheng | ba2f0a9 | 2006-02-05 06:46:41 +0000 | [diff] [blame] | 41 | #include <set> |
Chris Lattner | c961eea | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 42 | using namespace llvm; |
| 43 | |
Chris Lattner | 95b2c7d | 2006-12-19 22:59:26 +0000 | [diff] [blame] | 44 | STATISTIC(NumFPKill , "Number of FP_REG_KILL instructions added"); |
| 45 | STATISTIC(NumLoadMoved, "Number of loads moved below TokenFactor"); |
| 46 | |
Chris Lattner | c961eea | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 47 | //===----------------------------------------------------------------------===// |
| 48 | // Pattern Matcher Implementation |
| 49 | //===----------------------------------------------------------------------===// |
| 50 | |
| 51 | namespace { |
Chris Lattner | f9ce9fb | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 52 | /// X86ISelAddressMode - This corresponds to X86AddressMode, but uses |
| 53 | /// SDOperand's instead of register numbers for the leaves of the matched |
| 54 | /// tree. |
| 55 | struct X86ISelAddressMode { |
| 56 | enum { |
| 57 | RegBase, |
Chris Lattner | d74ea2b | 2006-05-24 17:04:05 +0000 | [diff] [blame] | 58 | FrameIndexBase |
Chris Lattner | f9ce9fb | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 59 | } BaseType; |
| 60 | |
| 61 | struct { // This is really a union, discriminated by BaseType! |
| 62 | SDOperand Reg; |
| 63 | int FrameIndex; |
| 64 | } Base; |
| 65 | |
Evan Cheng | be3bf42 | 2008-02-07 08:53:49 +0000 | [diff] [blame] | 66 | bool isRIPRel; // RIP as base? |
Chris Lattner | f9ce9fb | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 67 | unsigned Scale; |
| 68 | SDOperand IndexReg; |
| 69 | unsigned Disp; |
| 70 | GlobalValue *GV; |
Evan Cheng | 51a9ed9 | 2006-02-25 10:09:08 +0000 | [diff] [blame] | 71 | Constant *CP; |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 72 | const char *ES; |
| 73 | int JT; |
Evan Cheng | 51a9ed9 | 2006-02-25 10:09:08 +0000 | [diff] [blame] | 74 | unsigned Align; // CP alignment. |
Chris Lattner | f9ce9fb | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 75 | |
| 76 | X86ISelAddressMode() |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 77 | : BaseType(RegBase), isRIPRel(false), Scale(1), IndexReg(), Disp(0), |
| 78 | GV(0), CP(0), ES(0), JT(-1), Align(0) { |
Chris Lattner | f9ce9fb | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 79 | } |
| 80 | }; |
| 81 | } |
| 82 | |
| 83 | namespace { |
Chris Lattner | c961eea | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 84 | //===--------------------------------------------------------------------===// |
| 85 | /// ISel - X86 specific code to select X86 machine instructions for |
| 86 | /// SelectionDAG operations. |
| 87 | /// |
Chris Lattner | 2c79de8 | 2006-06-28 23:27:49 +0000 | [diff] [blame] | 88 | class VISIBILITY_HIDDEN X86DAGToDAGISel : public SelectionDAGISel { |
Chris Lattner | c961eea | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 89 | /// ContainsFPCode - Every instruction we select that uses or defines a FP |
| 90 | /// register should set this to true. |
| 91 | bool ContainsFPCode; |
| 92 | |
Evan Cheng | e50794a | 2006-08-29 18:28:33 +0000 | [diff] [blame] | 93 | /// FastISel - Enable fast(er) instruction selection. |
| 94 | /// |
| 95 | bool FastISel; |
| 96 | |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 97 | /// TM - Keep a reference to X86TargetMachine. |
| 98 | /// |
| 99 | X86TargetMachine &TM; |
| 100 | |
Chris Lattner | c961eea | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 101 | /// X86Lowering - This object fully describes how to lower LLVM code to an |
| 102 | /// X86-specific SelectionDAG. |
| 103 | X86TargetLowering X86Lowering; |
| 104 | |
| 105 | /// Subtarget - Keep a pointer to the X86Subtarget around so that we can |
| 106 | /// make the right decision when generating code for different targets. |
| 107 | const X86Subtarget *Subtarget; |
Evan Cheng | 7ccced6 | 2006-02-18 00:15:05 +0000 | [diff] [blame] | 108 | |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 109 | /// GlobalBaseReg - keeps track of the virtual register mapped onto global |
| 110 | /// base register. |
Evan Cheng | 7ccced6 | 2006-02-18 00:15:05 +0000 | [diff] [blame] | 111 | unsigned GlobalBaseReg; |
Evan Cheng | a8df1b4 | 2006-07-27 16:44:36 +0000 | [diff] [blame] | 112 | |
Chris Lattner | c961eea | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 113 | public: |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 114 | X86DAGToDAGISel(X86TargetMachine &tm, bool fast) |
Evan Cheng | c4c6257 | 2006-03-13 23:20:37 +0000 | [diff] [blame] | 115 | : SelectionDAGISel(X86Lowering), |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 116 | ContainsFPCode(false), FastISel(fast), TM(tm), |
Evan Cheng | a8df1b4 | 2006-07-27 16:44:36 +0000 | [diff] [blame] | 117 | X86Lowering(*TM.getTargetLowering()), |
Evan Cheng | f4b4c41 | 2006-08-08 00:31:00 +0000 | [diff] [blame] | 118 | Subtarget(&TM.getSubtarget<X86Subtarget>()) {} |
Chris Lattner | c961eea | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 119 | |
Evan Cheng | 7ccced6 | 2006-02-18 00:15:05 +0000 | [diff] [blame] | 120 | virtual bool runOnFunction(Function &Fn) { |
| 121 | // Make sure we re-emit a set of the global base reg if necessary |
| 122 | GlobalBaseReg = 0; |
| 123 | return SelectionDAGISel::runOnFunction(Fn); |
| 124 | } |
| 125 | |
Chris Lattner | c961eea | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 126 | virtual const char *getPassName() const { |
| 127 | return "X86 DAG->DAG Instruction Selection"; |
| 128 | } |
| 129 | |
| 130 | /// InstructionSelectBasicBlock - This callback is invoked by |
| 131 | /// SelectionDAGISel when it has created a SelectionDAG for us to codegen. |
| 132 | virtual void InstructionSelectBasicBlock(SelectionDAG &DAG); |
| 133 | |
Anton Korobeynikov | 2fe1259 | 2007-09-25 21:52:30 +0000 | [diff] [blame] | 134 | virtual void EmitFunctionEntryCode(Function &Fn, MachineFunction &MF); |
| 135 | |
Dan Gohman | dc9b3d0 | 2007-07-24 23:00:27 +0000 | [diff] [blame] | 136 | virtual bool CanBeFoldedBy(SDNode *N, SDNode *U, SDNode *Root) const; |
Evan Cheng | a8df1b4 | 2006-07-27 16:44:36 +0000 | [diff] [blame] | 137 | |
Chris Lattner | c961eea | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 138 | // Include the pieces autogenerated from the target description. |
| 139 | #include "X86GenDAGISel.inc" |
| 140 | |
| 141 | private: |
Evan Cheng | 9ade218 | 2006-08-26 05:34:46 +0000 | [diff] [blame] | 142 | SDNode *Select(SDOperand N); |
Chris Lattner | c961eea | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 143 | |
Anton Korobeynikov | 33bf8c4 | 2007-03-28 18:36:33 +0000 | [diff] [blame] | 144 | bool MatchAddress(SDOperand N, X86ISelAddressMode &AM, |
Anton Korobeynikov | f6e9353 | 2007-03-28 18:38:33 +0000 | [diff] [blame] | 145 | bool isRoot = true, unsigned Depth = 0); |
Dan Gohman | badb2d2 | 2007-08-13 20:03:06 +0000 | [diff] [blame] | 146 | bool MatchAddressBase(SDOperand N, X86ISelAddressMode &AM, |
| 147 | bool isRoot, unsigned Depth); |
Evan Cheng | 0d53826 | 2006-11-08 20:34:28 +0000 | [diff] [blame] | 148 | bool SelectAddr(SDOperand Op, SDOperand N, SDOperand &Base, |
| 149 | SDOperand &Scale, SDOperand &Index, SDOperand &Disp); |
| 150 | bool SelectLEAAddr(SDOperand Op, SDOperand N, SDOperand &Base, |
| 151 | SDOperand &Scale, SDOperand &Index, SDOperand &Disp); |
| 152 | bool SelectScalarSSELoad(SDOperand Op, SDOperand Pred, |
Evan Cheng | 07e4b00 | 2006-10-16 06:34:55 +0000 | [diff] [blame] | 153 | SDOperand N, SDOperand &Base, SDOperand &Scale, |
Evan Cheng | 82a9164 | 2006-10-11 21:06:01 +0000 | [diff] [blame] | 154 | SDOperand &Index, SDOperand &Disp, |
| 155 | SDOperand &InChain, SDOperand &OutChain); |
Evan Cheng | 5e35168 | 2006-02-06 06:02:33 +0000 | [diff] [blame] | 156 | bool TryFoldLoad(SDOperand P, SDOperand N, |
| 157 | SDOperand &Base, SDOperand &Scale, |
Evan Cheng | 0114e94 | 2006-01-06 20:36:21 +0000 | [diff] [blame] | 158 | SDOperand &Index, SDOperand &Disp); |
Chris Lattner | d43d00c | 2008-01-24 08:07:48 +0000 | [diff] [blame] | 159 | void PreprocessForRMW(SelectionDAG &DAG); |
| 160 | void PreprocessForFPConvert(SelectionDAG &DAG); |
Evan Cheng | 2ef88a0 | 2006-08-07 22:28:20 +0000 | [diff] [blame] | 161 | |
Chris Lattner | c0bad57 | 2006-06-08 18:03:49 +0000 | [diff] [blame] | 162 | /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for |
| 163 | /// inline asm expressions. |
| 164 | virtual bool SelectInlineAsmMemoryOperand(const SDOperand &Op, |
| 165 | char ConstraintCode, |
| 166 | std::vector<SDOperand> &OutOps, |
| 167 | SelectionDAG &DAG); |
| 168 | |
Anton Korobeynikov | 2fe1259 | 2007-09-25 21:52:30 +0000 | [diff] [blame] | 169 | void EmitSpecialCodeForMain(MachineBasicBlock *BB, MachineFrameInfo *MFI); |
| 170 | |
Evan Cheng | e528053 | 2005-12-12 21:49:40 +0000 | [diff] [blame] | 171 | inline void getAddressOperands(X86ISelAddressMode &AM, SDOperand &Base, |
| 172 | SDOperand &Scale, SDOperand &Index, |
| 173 | SDOperand &Disp) { |
| 174 | Base = (AM.BaseType == X86ISelAddressMode::FrameIndexBase) ? |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 175 | CurDAG->getTargetFrameIndex(AM.Base.FrameIndex, TLI.getPointerTy()) : |
| 176 | AM.Base.Reg; |
Evan Cheng | bdce7b4 | 2005-12-17 09:13:43 +0000 | [diff] [blame] | 177 | Scale = getI8Imm(AM.Scale); |
Evan Cheng | e528053 | 2005-12-12 21:49:40 +0000 | [diff] [blame] | 178 | Index = AM.IndexReg; |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 179 | // These are 32-bit even in 64-bit mode since RIP relative offset |
| 180 | // is 32-bit. |
| 181 | if (AM.GV) |
| 182 | Disp = CurDAG->getTargetGlobalAddress(AM.GV, MVT::i32, AM.Disp); |
| 183 | else if (AM.CP) |
| 184 | Disp = CurDAG->getTargetConstantPool(AM.CP, MVT::i32, AM.Align, AM.Disp); |
| 185 | else if (AM.ES) |
| 186 | Disp = CurDAG->getTargetExternalSymbol(AM.ES, MVT::i32); |
| 187 | else if (AM.JT != -1) |
| 188 | Disp = CurDAG->getTargetJumpTable(AM.JT, MVT::i32); |
| 189 | else |
| 190 | Disp = getI32Imm(AM.Disp); |
Evan Cheng | e528053 | 2005-12-12 21:49:40 +0000 | [diff] [blame] | 191 | } |
| 192 | |
Chris Lattner | f9ce9fb | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 193 | /// getI8Imm - Return a target constant with the specified value, of type |
| 194 | /// i8. |
| 195 | inline SDOperand getI8Imm(unsigned Imm) { |
| 196 | return CurDAG->getTargetConstant(Imm, MVT::i8); |
| 197 | } |
| 198 | |
Chris Lattner | c961eea | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 199 | /// getI16Imm - Return a target constant with the specified value, of type |
| 200 | /// i16. |
| 201 | inline SDOperand getI16Imm(unsigned Imm) { |
| 202 | return CurDAG->getTargetConstant(Imm, MVT::i16); |
| 203 | } |
| 204 | |
| 205 | /// getI32Imm - Return a target constant with the specified value, of type |
| 206 | /// i32. |
| 207 | inline SDOperand getI32Imm(unsigned Imm) { |
| 208 | return CurDAG->getTargetConstant(Imm, MVT::i32); |
| 209 | } |
Evan Cheng | f597dc7 | 2006-02-10 22:24:32 +0000 | [diff] [blame] | 210 | |
Evan Cheng | 7ccced6 | 2006-02-18 00:15:05 +0000 | [diff] [blame] | 211 | /// getGlobalBaseReg - insert code into the entry mbb to materialize the PIC |
| 212 | /// base register. Return the virtual register that holds this value. |
Evan Cheng | 9ade218 | 2006-08-26 05:34:46 +0000 | [diff] [blame] | 213 | SDNode *getGlobalBaseReg(); |
Evan Cheng | 7ccced6 | 2006-02-18 00:15:05 +0000 | [diff] [blame] | 214 | |
Christopher Lamb | c59e521 | 2007-08-10 21:48:46 +0000 | [diff] [blame] | 215 | /// getTruncate - return an SDNode that implements a subreg based truncate |
| 216 | /// of the specified operand to the the specified value type. |
| 217 | SDNode *getTruncate(SDOperand N0, MVT::ValueType VT); |
| 218 | |
Evan Cheng | 23addc0 | 2006-02-10 22:46:26 +0000 | [diff] [blame] | 219 | #ifndef NDEBUG |
| 220 | unsigned Indent; |
| 221 | #endif |
Chris Lattner | c961eea | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 222 | }; |
| 223 | } |
| 224 | |
Evan Cheng | a275ecb | 2006-10-10 01:46:56 +0000 | [diff] [blame] | 225 | static SDNode *findFlagUse(SDNode *N) { |
| 226 | unsigned FlagResNo = N->getNumValues()-1; |
| 227 | for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) { |
| 228 | SDNode *User = *I; |
| 229 | for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i) { |
| 230 | SDOperand Op = User->getOperand(i); |
Evan Cheng | 494cec6 | 2006-10-12 19:13:59 +0000 | [diff] [blame] | 231 | if (Op.Val == N && Op.ResNo == FlagResNo) |
Evan Cheng | a275ecb | 2006-10-10 01:46:56 +0000 | [diff] [blame] | 232 | return User; |
| 233 | } |
| 234 | } |
| 235 | return NULL; |
| 236 | } |
| 237 | |
Evan Cheng | 27e1fe9 | 2006-10-14 08:33:25 +0000 | [diff] [blame] | 238 | static void findNonImmUse(SDNode *Use, SDNode* Def, SDNode *ImmedUse, |
| 239 | SDNode *Root, SDNode *Skip, bool &found, |
Evan Cheng | f4b4c41 | 2006-08-08 00:31:00 +0000 | [diff] [blame] | 240 | std::set<SDNode *> &Visited) { |
| 241 | if (found || |
| 242 | Use->getNodeId() > Def->getNodeId() || |
| 243 | !Visited.insert(Use).second) |
| 244 | return; |
| 245 | |
Evan Cheng | 27e1fe9 | 2006-10-14 08:33:25 +0000 | [diff] [blame] | 246 | for (unsigned i = 0, e = Use->getNumOperands(); !found && i != e; ++i) { |
Evan Cheng | f4b4c41 | 2006-08-08 00:31:00 +0000 | [diff] [blame] | 247 | SDNode *N = Use->getOperand(i).Val; |
Evan Cheng | 27e1fe9 | 2006-10-14 08:33:25 +0000 | [diff] [blame] | 248 | if (N == Skip) |
Evan Cheng | a275ecb | 2006-10-10 01:46:56 +0000 | [diff] [blame] | 249 | continue; |
Evan Cheng | 27e1fe9 | 2006-10-14 08:33:25 +0000 | [diff] [blame] | 250 | if (N == Def) { |
| 251 | if (Use == ImmedUse) |
| 252 | continue; // Immediate use is ok. |
| 253 | if (Use == Root) { |
| 254 | assert(Use->getOpcode() == ISD::STORE || |
| 255 | Use->getOpcode() == X86ISD::CMP); |
| 256 | continue; |
| 257 | } |
Evan Cheng | f4b4c41 | 2006-08-08 00:31:00 +0000 | [diff] [blame] | 258 | found = true; |
| 259 | break; |
| 260 | } |
Evan Cheng | 27e1fe9 | 2006-10-14 08:33:25 +0000 | [diff] [blame] | 261 | findNonImmUse(N, Def, ImmedUse, Root, Skip, found, Visited); |
Evan Cheng | f4b4c41 | 2006-08-08 00:31:00 +0000 | [diff] [blame] | 262 | } |
| 263 | } |
| 264 | |
Evan Cheng | 27e1fe9 | 2006-10-14 08:33:25 +0000 | [diff] [blame] | 265 | /// isNonImmUse - Start searching from Root up the DAG to check is Def can |
| 266 | /// be reached. Return true if that's the case. However, ignore direct uses |
| 267 | /// by ImmedUse (which would be U in the example illustrated in |
| 268 | /// CanBeFoldedBy) and by Root (which can happen in the store case). |
| 269 | /// FIXME: to be really generic, we should allow direct use by any node |
| 270 | /// that is being folded. But realisticly since we only fold loads which |
| 271 | /// have one non-chain use, we only need to watch out for load/op/store |
| 272 | /// and load/op/cmp case where the root (store / cmp) may reach the load via |
| 273 | /// its chain operand. |
| 274 | static inline bool isNonImmUse(SDNode *Root, SDNode *Def, SDNode *ImmedUse, |
| 275 | SDNode *Skip = NULL) { |
Evan Cheng | f4b4c41 | 2006-08-08 00:31:00 +0000 | [diff] [blame] | 276 | std::set<SDNode *> Visited; |
| 277 | bool found = false; |
Evan Cheng | 27e1fe9 | 2006-10-14 08:33:25 +0000 | [diff] [blame] | 278 | findNonImmUse(Root, Def, ImmedUse, Root, Skip, found, Visited); |
Evan Cheng | f4b4c41 | 2006-08-08 00:31:00 +0000 | [diff] [blame] | 279 | return found; |
| 280 | } |
| 281 | |
| 282 | |
Dan Gohman | dc9b3d0 | 2007-07-24 23:00:27 +0000 | [diff] [blame] | 283 | bool X86DAGToDAGISel::CanBeFoldedBy(SDNode *N, SDNode *U, SDNode *Root) const { |
Evan Cheng | 27e1fe9 | 2006-10-14 08:33:25 +0000 | [diff] [blame] | 284 | if (FastISel) return false; |
| 285 | |
Evan Cheng | a8df1b4 | 2006-07-27 16:44:36 +0000 | [diff] [blame] | 286 | // If U use can somehow reach N through another path then U can't fold N or |
| 287 | // 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] | 288 | // 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] | 289 | // a successor of U. |
| 290 | // |
| 291 | // [ N ] |
| 292 | // ^ ^ |
| 293 | // | | |
| 294 | // / \--- |
| 295 | // / [X] |
| 296 | // | ^ |
| 297 | // [U]--------| |
Evan Cheng | 27e1fe9 | 2006-10-14 08:33:25 +0000 | [diff] [blame] | 298 | |
| 299 | if (isNonImmUse(Root, N, U)) |
| 300 | return false; |
| 301 | |
| 302 | // If U produces a flag, then it gets (even more) interesting. Since it |
| 303 | // would have been "glued" together with its flag use, we need to check if |
| 304 | // it might reach N: |
| 305 | // |
| 306 | // [ N ] |
| 307 | // ^ ^ |
| 308 | // | | |
| 309 | // [U] \-- |
| 310 | // ^ [TF] |
| 311 | // | ^ |
| 312 | // | | |
| 313 | // \ / |
| 314 | // [FU] |
| 315 | // |
| 316 | // If FU (flag use) indirectly reach N (the load), and U fold N (call it |
| 317 | // NU), then TF is a predecessor of FU and a successor of NU. But since |
| 318 | // NU and FU are flagged together, this effectively creates a cycle. |
| 319 | bool HasFlagUse = false; |
| 320 | MVT::ValueType VT = Root->getValueType(Root->getNumValues()-1); |
| 321 | while ((VT == MVT::Flag && !Root->use_empty())) { |
| 322 | SDNode *FU = findFlagUse(Root); |
| 323 | if (FU == NULL) |
| 324 | break; |
| 325 | else { |
| 326 | Root = FU; |
| 327 | HasFlagUse = true; |
Evan Cheng | a275ecb | 2006-10-10 01:46:56 +0000 | [diff] [blame] | 328 | } |
Evan Cheng | 27e1fe9 | 2006-10-14 08:33:25 +0000 | [diff] [blame] | 329 | VT = Root->getValueType(Root->getNumValues()-1); |
Evan Cheng | a275ecb | 2006-10-10 01:46:56 +0000 | [diff] [blame] | 330 | } |
Evan Cheng | 27e1fe9 | 2006-10-14 08:33:25 +0000 | [diff] [blame] | 331 | |
| 332 | if (HasFlagUse) |
| 333 | return !isNonImmUse(Root, N, Root, U); |
| 334 | return true; |
Evan Cheng | a8df1b4 | 2006-07-27 16:44:36 +0000 | [diff] [blame] | 335 | } |
| 336 | |
Evan Cheng | 70e674e | 2006-08-28 20:10:17 +0000 | [diff] [blame] | 337 | /// MoveBelowTokenFactor - Replace TokenFactor operand with load's chain operand |
| 338 | /// and move load below the TokenFactor. Replace store's chain operand with |
| 339 | /// load's chain result. |
| 340 | static void MoveBelowTokenFactor(SelectionDAG &DAG, SDOperand Load, |
| 341 | SDOperand Store, SDOperand TF) { |
| 342 | std::vector<SDOperand> Ops; |
| 343 | for (unsigned i = 0, e = TF.Val->getNumOperands(); i != e; ++i) |
| 344 | if (Load.Val == TF.Val->getOperand(i).Val) |
| 345 | Ops.push_back(Load.Val->getOperand(0)); |
| 346 | else |
| 347 | Ops.push_back(TF.Val->getOperand(i)); |
| 348 | DAG.UpdateNodeOperands(TF, &Ops[0], Ops.size()); |
| 349 | DAG.UpdateNodeOperands(Load, TF, Load.getOperand(1), Load.getOperand(2)); |
| 350 | DAG.UpdateNodeOperands(Store, Load.getValue(1), Store.getOperand(1), |
| 351 | Store.getOperand(2), Store.getOperand(3)); |
| 352 | } |
| 353 | |
Chris Lattner | d43d00c | 2008-01-24 08:07:48 +0000 | [diff] [blame] | 354 | /// PreprocessForRMW - Preprocess the DAG to make instruction selection better. |
| 355 | /// This is only run if not in -fast mode (aka -O0). |
| 356 | /// This allows the instruction selector to pick more read-modify-write |
| 357 | /// instructions. This is a common case: |
Evan Cheng | 70e674e | 2006-08-28 20:10:17 +0000 | [diff] [blame] | 358 | /// |
| 359 | /// [Load chain] |
| 360 | /// ^ |
| 361 | /// | |
| 362 | /// [Load] |
| 363 | /// ^ ^ |
| 364 | /// | | |
| 365 | /// / \- |
| 366 | /// / | |
| 367 | /// [TokenFactor] [Op] |
| 368 | /// ^ ^ |
| 369 | /// | | |
| 370 | /// \ / |
| 371 | /// \ / |
| 372 | /// [Store] |
| 373 | /// |
| 374 | /// The fact the store's chain operand != load's chain will prevent the |
| 375 | /// (store (op (load))) instruction from being selected. We can transform it to: |
| 376 | /// |
| 377 | /// [Load chain] |
| 378 | /// ^ |
| 379 | /// | |
| 380 | /// [TokenFactor] |
| 381 | /// ^ |
| 382 | /// | |
| 383 | /// [Load] |
| 384 | /// ^ ^ |
| 385 | /// | | |
| 386 | /// | \- |
| 387 | /// | | |
| 388 | /// | [Op] |
| 389 | /// | ^ |
| 390 | /// | | |
| 391 | /// \ / |
| 392 | /// \ / |
| 393 | /// [Store] |
Chris Lattner | d43d00c | 2008-01-24 08:07:48 +0000 | [diff] [blame] | 394 | void X86DAGToDAGISel::PreprocessForRMW(SelectionDAG &DAG) { |
Evan Cheng | 70e674e | 2006-08-28 20:10:17 +0000 | [diff] [blame] | 395 | for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(), |
| 396 | E = DAG.allnodes_end(); I != E; ++I) { |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 397 | if (!ISD::isNON_TRUNCStore(I)) |
Evan Cheng | 70e674e | 2006-08-28 20:10:17 +0000 | [diff] [blame] | 398 | continue; |
| 399 | SDOperand Chain = I->getOperand(0); |
| 400 | if (Chain.Val->getOpcode() != ISD::TokenFactor) |
| 401 | continue; |
| 402 | |
| 403 | SDOperand N1 = I->getOperand(1); |
| 404 | SDOperand N2 = I->getOperand(2); |
Evan Cheng | 1453de5 | 2006-09-01 22:52:28 +0000 | [diff] [blame] | 405 | if (MVT::isFloatingPoint(N1.getValueType()) || |
| 406 | MVT::isVector(N1.getValueType()) || |
Evan Cheng | 780413d | 2006-08-29 18:37:37 +0000 | [diff] [blame] | 407 | !N1.hasOneUse()) |
Evan Cheng | 70e674e | 2006-08-28 20:10:17 +0000 | [diff] [blame] | 408 | continue; |
| 409 | |
| 410 | bool RModW = false; |
| 411 | SDOperand Load; |
| 412 | unsigned Opcode = N1.Val->getOpcode(); |
| 413 | switch (Opcode) { |
| 414 | case ISD::ADD: |
| 415 | case ISD::MUL: |
Evan Cheng | 70e674e | 2006-08-28 20:10:17 +0000 | [diff] [blame] | 416 | case ISD::AND: |
| 417 | case ISD::OR: |
| 418 | case ISD::XOR: |
| 419 | case ISD::ADDC: |
| 420 | case ISD::ADDE: { |
| 421 | SDOperand N10 = N1.getOperand(0); |
| 422 | SDOperand N11 = N1.getOperand(1); |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 423 | if (ISD::isNON_EXTLoad(N10.Val)) |
Evan Cheng | 70e674e | 2006-08-28 20:10:17 +0000 | [diff] [blame] | 424 | RModW = true; |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 425 | else if (ISD::isNON_EXTLoad(N11.Val)) { |
Evan Cheng | 70e674e | 2006-08-28 20:10:17 +0000 | [diff] [blame] | 426 | RModW = true; |
| 427 | std::swap(N10, N11); |
| 428 | } |
Evan Cheng | 07b7ea1 | 2008-03-04 00:40:35 +0000 | [diff] [blame] | 429 | RModW = RModW && N10.Val->isOperandOf(Chain.Val) && N10.hasOneUse() && |
Evan Cheng | 82a35b3 | 2006-08-29 06:44:17 +0000 | [diff] [blame] | 430 | (N10.getOperand(1) == N2) && |
| 431 | (N10.Val->getValueType(0) == N1.getValueType()); |
Evan Cheng | 70e674e | 2006-08-28 20:10:17 +0000 | [diff] [blame] | 432 | if (RModW) |
| 433 | Load = N10; |
| 434 | break; |
| 435 | } |
| 436 | case ISD::SUB: |
| 437 | case ISD::SHL: |
| 438 | case ISD::SRA: |
| 439 | case ISD::SRL: |
| 440 | case ISD::ROTL: |
| 441 | case ISD::ROTR: |
| 442 | case ISD::SUBC: |
| 443 | case ISD::SUBE: |
| 444 | case X86ISD::SHLD: |
| 445 | case X86ISD::SHRD: { |
| 446 | SDOperand N10 = N1.getOperand(0); |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 447 | if (ISD::isNON_EXTLoad(N10.Val)) |
Evan Cheng | 07b7ea1 | 2008-03-04 00:40:35 +0000 | [diff] [blame] | 448 | RModW = N10.Val->isOperandOf(Chain.Val) && N10.hasOneUse() && |
Evan Cheng | 82a35b3 | 2006-08-29 06:44:17 +0000 | [diff] [blame] | 449 | (N10.getOperand(1) == N2) && |
| 450 | (N10.Val->getValueType(0) == N1.getValueType()); |
Evan Cheng | 70e674e | 2006-08-28 20:10:17 +0000 | [diff] [blame] | 451 | if (RModW) |
| 452 | Load = N10; |
| 453 | break; |
| 454 | } |
| 455 | } |
| 456 | |
Evan Cheng | 82a35b3 | 2006-08-29 06:44:17 +0000 | [diff] [blame] | 457 | if (RModW) { |
Evan Cheng | 70e674e | 2006-08-28 20:10:17 +0000 | [diff] [blame] | 458 | MoveBelowTokenFactor(DAG, Load, SDOperand(I, 0), Chain); |
Evan Cheng | 82a35b3 | 2006-08-29 06:44:17 +0000 | [diff] [blame] | 459 | ++NumLoadMoved; |
| 460 | } |
Evan Cheng | 70e674e | 2006-08-28 20:10:17 +0000 | [diff] [blame] | 461 | } |
| 462 | } |
| 463 | |
Chris Lattner | d43d00c | 2008-01-24 08:07:48 +0000 | [diff] [blame] | 464 | |
| 465 | /// PreprocessForFPConvert - Walk over the dag lowering fpround and fpextend |
| 466 | /// nodes that target the FP stack to be store and load to the stack. This is a |
| 467 | /// gross hack. We would like to simply mark these as being illegal, but when |
| 468 | /// we do that, legalize produces these when it expands calls, then expands |
| 469 | /// these in the same legalize pass. We would like dag combine to be able to |
| 470 | /// hack on these between the call expansion and the node legalization. As such |
| 471 | /// this pass basically does "really late" legalization of these inline with the |
| 472 | /// X86 isel pass. |
| 473 | void X86DAGToDAGISel::PreprocessForFPConvert(SelectionDAG &DAG) { |
| 474 | for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(), |
| 475 | E = DAG.allnodes_end(); I != E; ) { |
| 476 | SDNode *N = I++; // Preincrement iterator to avoid invalidation issues. |
| 477 | if (N->getOpcode() != ISD::FP_ROUND && N->getOpcode() != ISD::FP_EXTEND) |
| 478 | continue; |
| 479 | |
| 480 | // If the source and destination are SSE registers, then this is a legal |
| 481 | // conversion that should not be lowered. |
| 482 | MVT::ValueType SrcVT = N->getOperand(0).getValueType(); |
| 483 | MVT::ValueType DstVT = N->getValueType(0); |
| 484 | bool SrcIsSSE = X86Lowering.isScalarFPTypeInSSEReg(SrcVT); |
| 485 | bool DstIsSSE = X86Lowering.isScalarFPTypeInSSEReg(DstVT); |
| 486 | if (SrcIsSSE && DstIsSSE) |
| 487 | continue; |
| 488 | |
Chris Lattner | 6fa2f9c | 2008-03-09 07:05:32 +0000 | [diff] [blame] | 489 | if (!SrcIsSSE && !DstIsSSE) { |
| 490 | // If this is an FPStack extension, it is a noop. |
| 491 | if (N->getOpcode() == ISD::FP_EXTEND) |
| 492 | continue; |
| 493 | // If this is a value-preserving FPStack truncation, it is a noop. |
| 494 | if (N->getConstantOperandVal(1)) |
| 495 | continue; |
| 496 | } |
| 497 | |
Chris Lattner | d43d00c | 2008-01-24 08:07:48 +0000 | [diff] [blame] | 498 | // Here we could have an FP stack truncation or an FPStack <-> SSE convert. |
| 499 | // FPStack has extload and truncstore. SSE can fold direct loads into other |
| 500 | // operations. Based on this, decide what we want to do. |
| 501 | MVT::ValueType MemVT; |
| 502 | if (N->getOpcode() == ISD::FP_ROUND) |
| 503 | MemVT = DstVT; // FP_ROUND must use DstVT, we can't do a 'trunc load'. |
| 504 | else |
| 505 | MemVT = SrcIsSSE ? SrcVT : DstVT; |
| 506 | |
| 507 | SDOperand MemTmp = DAG.CreateStackTemporary(MemVT); |
| 508 | |
| 509 | // FIXME: optimize the case where the src/dest is a load or store? |
| 510 | SDOperand Store = DAG.getTruncStore(DAG.getEntryNode(), N->getOperand(0), |
| 511 | MemTmp, NULL, 0, MemVT); |
| 512 | SDOperand Result = DAG.getExtLoad(ISD::EXTLOAD, DstVT, Store, MemTmp, |
| 513 | NULL, 0, MemVT); |
| 514 | |
| 515 | // We're about to replace all uses of the FP_ROUND/FP_EXTEND with the |
| 516 | // extload we created. This will cause general havok on the dag because |
| 517 | // anything below the conversion could be folded into other existing nodes. |
| 518 | // To avoid invalidating 'I', back it up to the convert node. |
| 519 | --I; |
| 520 | DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 0), Result); |
| 521 | |
| 522 | // Now that we did that, the node is dead. Increment the iterator to the |
| 523 | // next node to process, then delete N. |
| 524 | ++I; |
| 525 | DAG.DeleteNode(N); |
| 526 | } |
| 527 | } |
| 528 | |
Chris Lattner | c961eea | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 529 | /// InstructionSelectBasicBlock - This callback is invoked by SelectionDAGISel |
| 530 | /// when it has created a SelectionDAG for us to codegen. |
| 531 | void X86DAGToDAGISel::InstructionSelectBasicBlock(SelectionDAG &DAG) { |
| 532 | DEBUG(BB->dump()); |
Chris Lattner | 92cb0af | 2006-01-11 01:15:34 +0000 | [diff] [blame] | 533 | MachineFunction::iterator FirstMBB = BB; |
Chris Lattner | c961eea | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 534 | |
Evan Cheng | e50794a | 2006-08-29 18:28:33 +0000 | [diff] [blame] | 535 | if (!FastISel) |
Chris Lattner | d43d00c | 2008-01-24 08:07:48 +0000 | [diff] [blame] | 536 | PreprocessForRMW(DAG); |
| 537 | |
| 538 | // FIXME: This should only happen when not -fast. |
| 539 | PreprocessForFPConvert(DAG); |
Evan Cheng | 70e674e | 2006-08-28 20:10:17 +0000 | [diff] [blame] | 540 | |
Chris Lattner | c961eea | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 541 | // Codegen the basic block. |
Evan Cheng | f597dc7 | 2006-02-10 22:24:32 +0000 | [diff] [blame] | 542 | #ifndef NDEBUG |
Bill Wendling | 6345d75 | 2006-11-17 07:52:03 +0000 | [diff] [blame] | 543 | DOUT << "===== Instruction selection begins:\n"; |
Evan Cheng | 23addc0 | 2006-02-10 22:46:26 +0000 | [diff] [blame] | 544 | Indent = 0; |
Evan Cheng | f597dc7 | 2006-02-10 22:24:32 +0000 | [diff] [blame] | 545 | #endif |
Evan Cheng | ba2f0a9 | 2006-02-05 06:46:41 +0000 | [diff] [blame] | 546 | DAG.setRoot(SelectRoot(DAG.getRoot())); |
Evan Cheng | f597dc7 | 2006-02-10 22:24:32 +0000 | [diff] [blame] | 547 | #ifndef NDEBUG |
Bill Wendling | 6345d75 | 2006-11-17 07:52:03 +0000 | [diff] [blame] | 548 | DOUT << "===== Instruction selection ends:\n"; |
Evan Cheng | f597dc7 | 2006-02-10 22:24:32 +0000 | [diff] [blame] | 549 | #endif |
Evan Cheng | 63ce568 | 2006-07-28 00:10:59 +0000 | [diff] [blame] | 550 | |
Chris Lattner | c961eea | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 551 | DAG.RemoveDeadNodes(); |
| 552 | |
Chris Lattner | 03fdec0 | 2008-03-10 23:34:12 +0000 | [diff] [blame^] | 553 | // Emit machine code to BB. This can change 'BB' to the last block being |
| 554 | // inserted into. |
Chris Lattner | c961eea | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 555 | ScheduleAndEmitDAG(DAG); |
Chris Lattner | 92cb0af | 2006-01-11 01:15:34 +0000 | [diff] [blame] | 556 | |
| 557 | // If we are emitting FP stack code, scan the basic block to determine if this |
| 558 | // block defines any FP values. If so, put an FP_REG_KILL instruction before |
| 559 | // the terminator of the block. |
Dale Johannesen | cdbe4d3 | 2007-08-07 20:29:26 +0000 | [diff] [blame] | 560 | |
Dale Johannesen | 48d1e45 | 2007-09-24 22:52:39 +0000 | [diff] [blame] | 561 | // Note that FP stack instructions are used in all modes for long double, |
| 562 | // so we always need to do this check. |
| 563 | // Also note that it's possible for an FP stack register to be live across |
| 564 | // an instruction that produces multiple basic blocks (SSE CMOV) so we |
| 565 | // must check all the generated basic blocks. |
Dale Johannesen | cdbe4d3 | 2007-08-07 20:29:26 +0000 | [diff] [blame] | 566 | |
| 567 | // Scan all of the machine instructions in these MBBs, checking for FP |
| 568 | // stores. (RFP32 and RFP64 will not exist in SSE mode, but RFP80 might.) |
| 569 | MachineFunction::iterator MBBI = FirstMBB; |
Chris Lattner | 03fdec0 | 2008-03-10 23:34:12 +0000 | [diff] [blame^] | 570 | MachineFunction::iterator EndMBB = BB; ++EndMBB; |
| 571 | for (; MBBI != EndMBB; ++MBBI) { |
| 572 | MachineBasicBlock *MBB = MBBI; |
| 573 | |
| 574 | // If this block returns, ignore it. We don't want to insert an FP_REG_KILL |
| 575 | // before the return. |
| 576 | if (!MBB->empty()) { |
| 577 | MachineBasicBlock::iterator EndI = MBB->end(); |
| 578 | --EndI; |
| 579 | if (EndI->getDesc().isReturn()) |
| 580 | continue; |
| 581 | } |
| 582 | |
Dale Johannesen | 48d1e45 | 2007-09-24 22:52:39 +0000 | [diff] [blame] | 583 | bool ContainsFPCode = false; |
Chris Lattner | 03fdec0 | 2008-03-10 23:34:12 +0000 | [diff] [blame^] | 584 | for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end(); |
Dale Johannesen | cdbe4d3 | 2007-08-07 20:29:26 +0000 | [diff] [blame] | 585 | !ContainsFPCode && I != E; ++I) { |
| 586 | if (I->getNumOperands() != 0 && I->getOperand(0).isRegister()) { |
| 587 | const TargetRegisterClass *clas; |
| 588 | for (unsigned op = 0, e = I->getNumOperands(); op != e; ++op) { |
| 589 | if (I->getOperand(op).isRegister() && I->getOperand(op).isDef() && |
Chris Lattner | 03fdec0 | 2008-03-10 23:34:12 +0000 | [diff] [blame^] | 590 | TargetRegisterInfo::isVirtualRegister(I->getOperand(op).getReg()) && |
Chris Lattner | 84bc542 | 2007-12-31 04:13:23 +0000 | [diff] [blame] | 591 | ((clas = RegInfo->getRegClass(I->getOperand(0).getReg())) == |
Dale Johannesen | cdbe4d3 | 2007-08-07 20:29:26 +0000 | [diff] [blame] | 592 | X86::RFP32RegisterClass || |
| 593 | clas == X86::RFP64RegisterClass || |
| 594 | clas == X86::RFP80RegisterClass)) { |
Chris Lattner | 92cb0af | 2006-01-11 01:15:34 +0000 | [diff] [blame] | 595 | ContainsFPCode = true; |
| 596 | break; |
| 597 | } |
| 598 | } |
| 599 | } |
| 600 | } |
Dale Johannesen | 48d1e45 | 2007-09-24 22:52:39 +0000 | [diff] [blame] | 601 | // Check PHI nodes in successor blocks. These PHI's will be lowered to have |
| 602 | // a copy of the input value in this block. In SSE mode, we only care about |
| 603 | // 80-bit values. |
| 604 | if (!ContainsFPCode) { |
| 605 | // Final check, check LLVM BB's that are successors to the LLVM BB |
| 606 | // corresponding to BB for FP PHI nodes. |
| 607 | const BasicBlock *LLVMBB = BB->getBasicBlock(); |
| 608 | const PHINode *PN; |
| 609 | for (succ_const_iterator SI = succ_begin(LLVMBB), E = succ_end(LLVMBB); |
| 610 | !ContainsFPCode && SI != E; ++SI) { |
| 611 | for (BasicBlock::const_iterator II = SI->begin(); |
| 612 | (PN = dyn_cast<PHINode>(II)); ++II) { |
| 613 | if (PN->getType()==Type::X86_FP80Ty || |
| 614 | (!Subtarget->hasSSE1() && PN->getType()->isFloatingPoint()) || |
| 615 | (!Subtarget->hasSSE2() && PN->getType()==Type::DoubleTy)) { |
| 616 | ContainsFPCode = true; |
| 617 | break; |
| 618 | } |
Dale Johannesen | cdbe4d3 | 2007-08-07 20:29:26 +0000 | [diff] [blame] | 619 | } |
| 620 | } |
Chris Lattner | 92cb0af | 2006-01-11 01:15:34 +0000 | [diff] [blame] | 621 | } |
Dale Johannesen | 48d1e45 | 2007-09-24 22:52:39 +0000 | [diff] [blame] | 622 | // Finally, if we found any FP code, emit the FP_REG_KILL instruction. |
| 623 | if (ContainsFPCode) { |
Chris Lattner | 03fdec0 | 2008-03-10 23:34:12 +0000 | [diff] [blame^] | 624 | BuildMI(*MBB, MBBI->getFirstTerminator(), |
Dale Johannesen | 48d1e45 | 2007-09-24 22:52:39 +0000 | [diff] [blame] | 625 | TM.getInstrInfo()->get(X86::FP_REG_KILL)); |
| 626 | ++NumFPKill; |
| 627 | } |
Chris Lattner | 03fdec0 | 2008-03-10 23:34:12 +0000 | [diff] [blame^] | 628 | } |
Chris Lattner | c961eea | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 629 | } |
| 630 | |
Anton Korobeynikov | 2fe1259 | 2007-09-25 21:52:30 +0000 | [diff] [blame] | 631 | /// EmitSpecialCodeForMain - Emit any code that needs to be executed only in |
| 632 | /// the main function. |
| 633 | void X86DAGToDAGISel::EmitSpecialCodeForMain(MachineBasicBlock *BB, |
| 634 | MachineFrameInfo *MFI) { |
| 635 | const TargetInstrInfo *TII = TM.getInstrInfo(); |
| 636 | if (Subtarget->isTargetCygMing()) |
| 637 | BuildMI(BB, TII->get(X86::CALLpcrel32)).addExternalSymbol("__main"); |
| 638 | } |
| 639 | |
| 640 | void X86DAGToDAGISel::EmitFunctionEntryCode(Function &Fn, MachineFunction &MF) { |
| 641 | // If this is main, emit special code for main. |
| 642 | MachineBasicBlock *BB = MF.begin(); |
| 643 | if (Fn.hasExternalLinkage() && Fn.getName() == "main") |
| 644 | EmitSpecialCodeForMain(BB, MF.getFrameInfo()); |
| 645 | } |
| 646 | |
Chris Lattner | f9ce9fb | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 647 | /// MatchAddress - Add the specified node to the specified addressing mode, |
| 648 | /// returning true if it cannot be done. This just pattern matches for the |
Chris Lattner | 5aaddaa | 2007-12-08 07:22:58 +0000 | [diff] [blame] | 649 | /// addressing mode. |
Evan Cheng | 2486af1 | 2006-02-11 02:05:36 +0000 | [diff] [blame] | 650 | bool X86DAGToDAGISel::MatchAddress(SDOperand N, X86ISelAddressMode &AM, |
Anton Korobeynikov | 33bf8c4 | 2007-03-28 18:36:33 +0000 | [diff] [blame] | 651 | bool isRoot, unsigned Depth) { |
Dan Gohman | badb2d2 | 2007-08-13 20:03:06 +0000 | [diff] [blame] | 652 | // Limit recursion. |
| 653 | if (Depth > 5) |
| 654 | return MatchAddressBase(N, AM, isRoot, Depth); |
Anton Korobeynikov | 33bf8c4 | 2007-03-28 18:36:33 +0000 | [diff] [blame] | 655 | |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 656 | // RIP relative addressing: %rip + 32-bit displacement! |
| 657 | if (AM.isRIPRel) { |
| 658 | if (!AM.ES && AM.JT != -1 && N.getOpcode() == ISD::Constant) { |
Chris Lattner | 0f27fc3 | 2006-09-13 04:45:25 +0000 | [diff] [blame] | 659 | int64_t Val = cast<ConstantSDNode>(N)->getSignExtended(); |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 660 | if (isInt32(AM.Disp + Val)) { |
| 661 | AM.Disp += Val; |
| 662 | return false; |
| 663 | } |
| 664 | } |
| 665 | return true; |
| 666 | } |
| 667 | |
Evan Cheng | 2ef88a0 | 2006-08-07 22:28:20 +0000 | [diff] [blame] | 668 | int id = N.Val->getNodeId(); |
Evan Cheng | 1314b00 | 2007-12-13 00:43:27 +0000 | [diff] [blame] | 669 | bool AlreadySelected = isSelected(id); // Already selected, not yet replaced. |
Evan Cheng | 2486af1 | 2006-02-11 02:05:36 +0000 | [diff] [blame] | 670 | |
Chris Lattner | f9ce9fb | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 671 | switch (N.getOpcode()) { |
| 672 | default: break; |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 673 | case ISD::Constant: { |
Chris Lattner | 0f27fc3 | 2006-09-13 04:45:25 +0000 | [diff] [blame] | 674 | int64_t Val = cast<ConstantSDNode>(N)->getSignExtended(); |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 675 | if (isInt32(AM.Disp + Val)) { |
| 676 | AM.Disp += Val; |
| 677 | return false; |
| 678 | } |
| 679 | break; |
| 680 | } |
Evan Cheng | 51a9ed9 | 2006-02-25 10:09:08 +0000 | [diff] [blame] | 681 | |
Evan Cheng | 19f2ffc | 2006-12-05 04:01:03 +0000 | [diff] [blame] | 682 | case X86ISD::Wrapper: { |
| 683 | bool is64Bit = Subtarget->is64Bit(); |
Evan Cheng | 0085a28 | 2006-11-30 21:55:46 +0000 | [diff] [blame] | 684 | // Under X86-64 non-small code model, GV (and friends) are 64-bits. |
Evan Cheng | be3bf42 | 2008-02-07 08:53:49 +0000 | [diff] [blame] | 685 | // Also, base and index reg must be 0 in order to use rip as base. |
| 686 | if (is64Bit && (TM.getCodeModel() != CodeModel::Small || |
| 687 | AM.Base.Reg.Val || AM.IndexReg.Val)) |
Evan Cheng | 0085a28 | 2006-11-30 21:55:46 +0000 | [diff] [blame] | 688 | break; |
Evan Cheng | 28b51439 | 2006-12-05 19:50:18 +0000 | [diff] [blame] | 689 | if (AM.GV != 0 || AM.CP != 0 || AM.ES != 0 || AM.JT != -1) |
| 690 | break; |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 691 | // If value is available in a register both base and index components have |
| 692 | // been picked, we can't fit the result available in the register in the |
| 693 | // addressing mode. Duplicate GlobalAddress or ConstantPool as displacement. |
Evan Cheng | 1314b00 | 2007-12-13 00:43:27 +0000 | [diff] [blame] | 694 | if (!AlreadySelected || (AM.Base.Reg.Val && AM.IndexReg.Val)) { |
Evan Cheng | 28b51439 | 2006-12-05 19:50:18 +0000 | [diff] [blame] | 695 | SDOperand N0 = N.getOperand(0); |
| 696 | if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(N0)) { |
| 697 | GlobalValue *GV = G->getGlobal(); |
Evan Cheng | be3bf42 | 2008-02-07 08:53:49 +0000 | [diff] [blame] | 698 | AM.GV = GV; |
| 699 | AM.Disp += G->getOffset(); |
Evan Cheng | 9f143ce | 2008-02-12 19:20:46 +0000 | [diff] [blame] | 700 | AM.isRIPRel = TM.getRelocationModel() != Reloc::Static && |
| 701 | Subtarget->isPICStyleRIPRel(); |
Evan Cheng | be3bf42 | 2008-02-07 08:53:49 +0000 | [diff] [blame] | 702 | return false; |
Evan Cheng | 28b51439 | 2006-12-05 19:50:18 +0000 | [diff] [blame] | 703 | } else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N0)) { |
Evan Cheng | be3bf42 | 2008-02-07 08:53:49 +0000 | [diff] [blame] | 704 | AM.CP = CP->getConstVal(); |
| 705 | AM.Align = CP->getAlignment(); |
| 706 | AM.Disp += CP->getOffset(); |
Evan Cheng | 9f143ce | 2008-02-12 19:20:46 +0000 | [diff] [blame] | 707 | AM.isRIPRel = TM.getRelocationModel() != Reloc::Static && |
| 708 | Subtarget->isPICStyleRIPRel(); |
Evan Cheng | be3bf42 | 2008-02-07 08:53:49 +0000 | [diff] [blame] | 709 | return false; |
Evan Cheng | 28b51439 | 2006-12-05 19:50:18 +0000 | [diff] [blame] | 710 | } else if (ExternalSymbolSDNode *S =dyn_cast<ExternalSymbolSDNode>(N0)) { |
Evan Cheng | be3bf42 | 2008-02-07 08:53:49 +0000 | [diff] [blame] | 711 | AM.ES = S->getSymbol(); |
Evan Cheng | 9f143ce | 2008-02-12 19:20:46 +0000 | [diff] [blame] | 712 | AM.isRIPRel = TM.getRelocationModel() != Reloc::Static && |
| 713 | Subtarget->isPICStyleRIPRel(); |
Evan Cheng | be3bf42 | 2008-02-07 08:53:49 +0000 | [diff] [blame] | 714 | return false; |
Evan Cheng | 28b51439 | 2006-12-05 19:50:18 +0000 | [diff] [blame] | 715 | } else if (JumpTableSDNode *J = dyn_cast<JumpTableSDNode>(N0)) { |
Evan Cheng | be3bf42 | 2008-02-07 08:53:49 +0000 | [diff] [blame] | 716 | AM.JT = J->getIndex(); |
Evan Cheng | 9f143ce | 2008-02-12 19:20:46 +0000 | [diff] [blame] | 717 | AM.isRIPRel = TM.getRelocationModel() != Reloc::Static && |
| 718 | Subtarget->isPICStyleRIPRel(); |
Evan Cheng | be3bf42 | 2008-02-07 08:53:49 +0000 | [diff] [blame] | 719 | return false; |
Evan Cheng | 51a9ed9 | 2006-02-25 10:09:08 +0000 | [diff] [blame] | 720 | } |
| 721 | } |
| 722 | break; |
Evan Cheng | 0085a28 | 2006-11-30 21:55:46 +0000 | [diff] [blame] | 723 | } |
Evan Cheng | 51a9ed9 | 2006-02-25 10:09:08 +0000 | [diff] [blame] | 724 | |
Chris Lattner | f9ce9fb | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 725 | case ISD::FrameIndex: |
| 726 | if (AM.BaseType == X86ISelAddressMode::RegBase && AM.Base.Reg.Val == 0) { |
| 727 | AM.BaseType = X86ISelAddressMode::FrameIndexBase; |
| 728 | AM.Base.FrameIndex = cast<FrameIndexSDNode>(N)->getIndex(); |
| 729 | return false; |
| 730 | } |
| 731 | break; |
Evan Cheng | ec693f7 | 2005-12-08 02:01:35 +0000 | [diff] [blame] | 732 | |
Chris Lattner | f9ce9fb | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 733 | case ISD::SHL: |
Evan Cheng | be3bf42 | 2008-02-07 08:53:49 +0000 | [diff] [blame] | 734 | if (AlreadySelected || AM.IndexReg.Val != 0 || AM.Scale != 1 || AM.isRIPRel) |
Chris Lattner | 5aaddaa | 2007-12-08 07:22:58 +0000 | [diff] [blame] | 735 | break; |
| 736 | |
| 737 | if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1))) { |
| 738 | unsigned Val = CN->getValue(); |
| 739 | if (Val == 1 || Val == 2 || Val == 3) { |
| 740 | AM.Scale = 1 << Val; |
| 741 | SDOperand ShVal = N.Val->getOperand(0); |
Chris Lattner | f9ce9fb | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 742 | |
Chris Lattner | 5aaddaa | 2007-12-08 07:22:58 +0000 | [diff] [blame] | 743 | // Okay, we know that we have a scale by now. However, if the scaled |
| 744 | // value is an add of something and a constant, we can fold the |
| 745 | // constant into the disp field here. |
| 746 | if (ShVal.Val->getOpcode() == ISD::ADD && ShVal.hasOneUse() && |
| 747 | isa<ConstantSDNode>(ShVal.Val->getOperand(1))) { |
| 748 | AM.IndexReg = ShVal.Val->getOperand(0); |
| 749 | ConstantSDNode *AddVal = |
| 750 | cast<ConstantSDNode>(ShVal.Val->getOperand(1)); |
| 751 | uint64_t Disp = AM.Disp + (AddVal->getValue() << Val); |
| 752 | if (isInt32(Disp)) |
| 753 | AM.Disp = Disp; |
| 754 | else |
Chris Lattner | f9ce9fb | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 755 | AM.IndexReg = ShVal; |
Chris Lattner | 5aaddaa | 2007-12-08 07:22:58 +0000 | [diff] [blame] | 756 | } else { |
| 757 | AM.IndexReg = ShVal; |
Chris Lattner | f9ce9fb | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 758 | } |
Chris Lattner | 5aaddaa | 2007-12-08 07:22:58 +0000 | [diff] [blame] | 759 | return false; |
Chris Lattner | f9ce9fb | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 760 | } |
| 761 | break; |
Chris Lattner | 5aaddaa | 2007-12-08 07:22:58 +0000 | [diff] [blame] | 762 | } |
Evan Cheng | ec693f7 | 2005-12-08 02:01:35 +0000 | [diff] [blame] | 763 | |
Dan Gohman | 8368805 | 2007-10-22 20:22:24 +0000 | [diff] [blame] | 764 | case ISD::SMUL_LOHI: |
| 765 | case ISD::UMUL_LOHI: |
| 766 | // A mul_lohi where we need the low part can be folded as a plain multiply. |
| 767 | if (N.ResNo != 0) break; |
| 768 | // FALL THROUGH |
Chris Lattner | f9ce9fb | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 769 | case ISD::MUL: |
| 770 | // X*[3,5,9] -> X+X*[2,4,8] |
Evan Cheng | 1314b00 | 2007-12-13 00:43:27 +0000 | [diff] [blame] | 771 | if (!AlreadySelected && |
Evan Cheng | 51a9ed9 | 2006-02-25 10:09:08 +0000 | [diff] [blame] | 772 | AM.BaseType == X86ISelAddressMode::RegBase && |
| 773 | AM.Base.Reg.Val == 0 && |
Evan Cheng | be3bf42 | 2008-02-07 08:53:49 +0000 | [diff] [blame] | 774 | AM.IndexReg.Val == 0 && |
| 775 | !AM.isRIPRel) { |
Chris Lattner | f9ce9fb | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 776 | if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1))) |
| 777 | if (CN->getValue() == 3 || CN->getValue() == 5 || CN->getValue() == 9) { |
| 778 | AM.Scale = unsigned(CN->getValue())-1; |
| 779 | |
| 780 | SDOperand MulVal = N.Val->getOperand(0); |
| 781 | SDOperand Reg; |
| 782 | |
| 783 | // Okay, we know that we have a scale by now. However, if the scaled |
| 784 | // value is an add of something and a constant, we can fold the |
| 785 | // constant into the disp field here. |
| 786 | if (MulVal.Val->getOpcode() == ISD::ADD && MulVal.hasOneUse() && |
| 787 | isa<ConstantSDNode>(MulVal.Val->getOperand(1))) { |
| 788 | Reg = MulVal.Val->getOperand(0); |
| 789 | ConstantSDNode *AddVal = |
| 790 | cast<ConstantSDNode>(MulVal.Val->getOperand(1)); |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 791 | uint64_t Disp = AM.Disp + AddVal->getValue() * CN->getValue(); |
| 792 | if (isInt32(Disp)) |
| 793 | AM.Disp = Disp; |
| 794 | else |
| 795 | Reg = N.Val->getOperand(0); |
Chris Lattner | f9ce9fb | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 796 | } else { |
| 797 | Reg = N.Val->getOperand(0); |
| 798 | } |
| 799 | |
| 800 | AM.IndexReg = AM.Base.Reg = Reg; |
| 801 | return false; |
| 802 | } |
Chris Lattner | 6241226 | 2007-02-04 20:18:17 +0000 | [diff] [blame] | 803 | } |
Chris Lattner | f9ce9fb | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 804 | break; |
| 805 | |
Chris Lattner | 6241226 | 2007-02-04 20:18:17 +0000 | [diff] [blame] | 806 | case ISD::ADD: |
Evan Cheng | 1314b00 | 2007-12-13 00:43:27 +0000 | [diff] [blame] | 807 | if (!AlreadySelected) { |
Evan Cheng | 2486af1 | 2006-02-11 02:05:36 +0000 | [diff] [blame] | 808 | X86ISelAddressMode Backup = AM; |
Anton Korobeynikov | 33bf8c4 | 2007-03-28 18:36:33 +0000 | [diff] [blame] | 809 | if (!MatchAddress(N.Val->getOperand(0), AM, false, Depth+1) && |
| 810 | !MatchAddress(N.Val->getOperand(1), AM, false, Depth+1)) |
Evan Cheng | 2486af1 | 2006-02-11 02:05:36 +0000 | [diff] [blame] | 811 | return false; |
| 812 | AM = Backup; |
Anton Korobeynikov | 33bf8c4 | 2007-03-28 18:36:33 +0000 | [diff] [blame] | 813 | if (!MatchAddress(N.Val->getOperand(1), AM, false, Depth+1) && |
| 814 | !MatchAddress(N.Val->getOperand(0), AM, false, Depth+1)) |
Evan Cheng | 2486af1 | 2006-02-11 02:05:36 +0000 | [diff] [blame] | 815 | return false; |
| 816 | AM = Backup; |
| 817 | } |
Chris Lattner | f9ce9fb | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 818 | break; |
Evan Cheng | e6ad27e | 2006-05-30 06:59:36 +0000 | [diff] [blame] | 819 | |
Chris Lattner | 6241226 | 2007-02-04 20:18:17 +0000 | [diff] [blame] | 820 | case ISD::OR: |
| 821 | // Handle "X | C" as "X + C" iff X is known to have C bits clear. |
Evan Cheng | 1314b00 | 2007-12-13 00:43:27 +0000 | [diff] [blame] | 822 | if (AlreadySelected) break; |
Chris Lattner | 5aaddaa | 2007-12-08 07:22:58 +0000 | [diff] [blame] | 823 | |
| 824 | if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) { |
| 825 | X86ISelAddressMode Backup = AM; |
| 826 | // Start with the LHS as an addr mode. |
| 827 | if (!MatchAddress(N.getOperand(0), AM, false) && |
| 828 | // Address could not have picked a GV address for the displacement. |
| 829 | AM.GV == NULL && |
| 830 | // On x86-64, the resultant disp must fit in 32-bits. |
| 831 | isInt32(AM.Disp + CN->getSignExtended()) && |
| 832 | // Check to see if the LHS & C is zero. |
Dan Gohman | 2e68b6f | 2008-02-25 21:11:39 +0000 | [diff] [blame] | 833 | CurDAG->MaskedValueIsZero(N.getOperand(0), CN->getAPIntValue())) { |
Chris Lattner | 5aaddaa | 2007-12-08 07:22:58 +0000 | [diff] [blame] | 834 | AM.Disp += CN->getValue(); |
| 835 | return false; |
Evan Cheng | e6ad27e | 2006-05-30 06:59:36 +0000 | [diff] [blame] | 836 | } |
Chris Lattner | 5aaddaa | 2007-12-08 07:22:58 +0000 | [diff] [blame] | 837 | AM = Backup; |
Evan Cheng | e6ad27e | 2006-05-30 06:59:36 +0000 | [diff] [blame] | 838 | } |
| 839 | break; |
Evan Cheng | 1314b00 | 2007-12-13 00:43:27 +0000 | [diff] [blame] | 840 | |
| 841 | case ISD::AND: { |
| 842 | // Handle "(x << C1) & C2" as "(X & (C2>>C1)) << C1" if safe and if this |
| 843 | // allows us to fold the shift into this addressing mode. |
| 844 | if (AlreadySelected) break; |
| 845 | SDOperand Shift = N.getOperand(0); |
| 846 | if (Shift.getOpcode() != ISD::SHL) break; |
| 847 | |
| 848 | // Scale must not be used already. |
| 849 | if (AM.IndexReg.Val != 0 || AM.Scale != 1) break; |
Evan Cheng | be3bf42 | 2008-02-07 08:53:49 +0000 | [diff] [blame] | 850 | |
| 851 | // Not when RIP is used as the base. |
| 852 | if (AM.isRIPRel) break; |
Evan Cheng | 1314b00 | 2007-12-13 00:43:27 +0000 | [diff] [blame] | 853 | |
| 854 | ConstantSDNode *C2 = dyn_cast<ConstantSDNode>(N.getOperand(1)); |
| 855 | ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(Shift.getOperand(1)); |
| 856 | if (!C1 || !C2) break; |
| 857 | |
| 858 | // Not likely to be profitable if either the AND or SHIFT node has more |
| 859 | // than one use (unless all uses are for address computation). Besides, |
| 860 | // isel mechanism requires their node ids to be reused. |
| 861 | if (!N.hasOneUse() || !Shift.hasOneUse()) |
| 862 | break; |
| 863 | |
| 864 | // Verify that the shift amount is something we can fold. |
| 865 | unsigned ShiftCst = C1->getValue(); |
| 866 | if (ShiftCst != 1 && ShiftCst != 2 && ShiftCst != 3) |
| 867 | break; |
| 868 | |
| 869 | // Get the new AND mask, this folds to a constant. |
| 870 | SDOperand NewANDMask = CurDAG->getNode(ISD::SRL, N.getValueType(), |
| 871 | SDOperand(C2, 0), SDOperand(C1, 0)); |
| 872 | SDOperand NewAND = CurDAG->getNode(ISD::AND, N.getValueType(), |
| 873 | Shift.getOperand(0), NewANDMask); |
| 874 | NewANDMask.Val->setNodeId(Shift.Val->getNodeId()); |
| 875 | NewAND.Val->setNodeId(N.Val->getNodeId()); |
| 876 | |
| 877 | AM.Scale = 1 << ShiftCst; |
| 878 | AM.IndexReg = NewAND; |
| 879 | return false; |
| 880 | } |
Evan Cheng | e6ad27e | 2006-05-30 06:59:36 +0000 | [diff] [blame] | 881 | } |
Chris Lattner | f9ce9fb | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 882 | |
Dan Gohman | badb2d2 | 2007-08-13 20:03:06 +0000 | [diff] [blame] | 883 | return MatchAddressBase(N, AM, isRoot, Depth); |
| 884 | } |
| 885 | |
| 886 | /// MatchAddressBase - Helper for MatchAddress. Add the specified node to the |
| 887 | /// specified addressing mode without any further recursion. |
| 888 | bool X86DAGToDAGISel::MatchAddressBase(SDOperand N, X86ISelAddressMode &AM, |
| 889 | bool isRoot, unsigned Depth) { |
Chris Lattner | f9ce9fb | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 890 | // Is the base register already occupied? |
| 891 | if (AM.BaseType != X86ISelAddressMode::RegBase || AM.Base.Reg.Val) { |
| 892 | // If so, check to see if the scale index register is set. |
Evan Cheng | be3bf42 | 2008-02-07 08:53:49 +0000 | [diff] [blame] | 893 | if (AM.IndexReg.Val == 0 && !AM.isRIPRel) { |
Chris Lattner | f9ce9fb | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 894 | AM.IndexReg = N; |
| 895 | AM.Scale = 1; |
| 896 | return false; |
| 897 | } |
| 898 | |
| 899 | // Otherwise, we cannot select it. |
| 900 | return true; |
| 901 | } |
| 902 | |
| 903 | // Default, generate it as a register. |
| 904 | AM.BaseType = X86ISelAddressMode::RegBase; |
| 905 | AM.Base.Reg = N; |
| 906 | return false; |
| 907 | } |
| 908 | |
Evan Cheng | ec693f7 | 2005-12-08 02:01:35 +0000 | [diff] [blame] | 909 | /// SelectAddr - returns true if it is able pattern match an addressing mode. |
| 910 | /// It returns the operands which make up the maximal addressing mode it can |
| 911 | /// match by reference. |
Evan Cheng | 0d53826 | 2006-11-08 20:34:28 +0000 | [diff] [blame] | 912 | bool X86DAGToDAGISel::SelectAddr(SDOperand Op, SDOperand N, SDOperand &Base, |
| 913 | SDOperand &Scale, SDOperand &Index, |
| 914 | SDOperand &Disp) { |
Evan Cheng | ec693f7 | 2005-12-08 02:01:35 +0000 | [diff] [blame] | 915 | X86ISelAddressMode AM; |
Evan Cheng | 8700e14 | 2006-01-11 06:09:51 +0000 | [diff] [blame] | 916 | if (MatchAddress(N, AM)) |
| 917 | return false; |
Evan Cheng | ec693f7 | 2005-12-08 02:01:35 +0000 | [diff] [blame] | 918 | |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 919 | MVT::ValueType VT = N.getValueType(); |
Evan Cheng | 8700e14 | 2006-01-11 06:09:51 +0000 | [diff] [blame] | 920 | if (AM.BaseType == X86ISelAddressMode::RegBase) { |
Evan Cheng | 7dd281b | 2006-02-05 05:25:07 +0000 | [diff] [blame] | 921 | if (!AM.Base.Reg.Val) |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 922 | AM.Base.Reg = CurDAG->getRegister(0, VT); |
Evan Cheng | ec693f7 | 2005-12-08 02:01:35 +0000 | [diff] [blame] | 923 | } |
Evan Cheng | 8700e14 | 2006-01-11 06:09:51 +0000 | [diff] [blame] | 924 | |
Evan Cheng | 7dd281b | 2006-02-05 05:25:07 +0000 | [diff] [blame] | 925 | if (!AM.IndexReg.Val) |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 926 | AM.IndexReg = CurDAG->getRegister(0, VT); |
Evan Cheng | 8700e14 | 2006-01-11 06:09:51 +0000 | [diff] [blame] | 927 | |
| 928 | getAddressOperands(AM, Base, Scale, Index, Disp); |
| 929 | return true; |
Evan Cheng | ec693f7 | 2005-12-08 02:01:35 +0000 | [diff] [blame] | 930 | } |
| 931 | |
Chris Lattner | 4fe4f25 | 2006-10-11 22:09:58 +0000 | [diff] [blame] | 932 | /// isZeroNode - Returns true if Elt is a constant zero or a floating point |
| 933 | /// constant +0.0. |
| 934 | static inline bool isZeroNode(SDOperand Elt) { |
| 935 | return ((isa<ConstantSDNode>(Elt) && |
| 936 | cast<ConstantSDNode>(Elt)->getValue() == 0) || |
| 937 | (isa<ConstantFPSDNode>(Elt) && |
Dale Johannesen | eaf0894 | 2007-08-31 04:03:46 +0000 | [diff] [blame] | 938 | cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero())); |
Chris Lattner | 4fe4f25 | 2006-10-11 22:09:58 +0000 | [diff] [blame] | 939 | } |
| 940 | |
| 941 | |
Chris Lattner | 3a7cd95 | 2006-10-07 21:55:32 +0000 | [diff] [blame] | 942 | /// SelectScalarSSELoad - Match a scalar SSE load. In particular, we want to |
| 943 | /// match a load whose top elements are either undef or zeros. The load flavor |
| 944 | /// 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] | 945 | bool X86DAGToDAGISel::SelectScalarSSELoad(SDOperand Op, SDOperand Pred, |
Evan Cheng | 07e4b00 | 2006-10-16 06:34:55 +0000 | [diff] [blame] | 946 | SDOperand N, SDOperand &Base, |
Evan Cheng | 82a9164 | 2006-10-11 21:06:01 +0000 | [diff] [blame] | 947 | SDOperand &Scale, SDOperand &Index, |
| 948 | SDOperand &Disp, SDOperand &InChain, |
| 949 | SDOperand &OutChain) { |
Chris Lattner | 3a7cd95 | 2006-10-07 21:55:32 +0000 | [diff] [blame] | 950 | if (N.getOpcode() == ISD::SCALAR_TO_VECTOR) { |
Chris Lattner | 4fe4f25 | 2006-10-11 22:09:58 +0000 | [diff] [blame] | 951 | InChain = N.getOperand(0).getValue(1); |
Evan Cheng | 07e4b00 | 2006-10-16 06:34:55 +0000 | [diff] [blame] | 952 | if (ISD::isNON_EXTLoad(InChain.Val) && |
| 953 | InChain.getValue(0).hasOneUse() && |
Evan Cheng | d6373bc | 2006-11-10 21:23:04 +0000 | [diff] [blame] | 954 | N.hasOneUse() && |
Evan Cheng | 0d53826 | 2006-11-08 20:34:28 +0000 | [diff] [blame] | 955 | CanBeFoldedBy(N.Val, Pred.Val, Op.Val)) { |
Evan Cheng | 82a9164 | 2006-10-11 21:06:01 +0000 | [diff] [blame] | 956 | LoadSDNode *LD = cast<LoadSDNode>(InChain); |
Evan Cheng | 0d53826 | 2006-11-08 20:34:28 +0000 | [diff] [blame] | 957 | if (!SelectAddr(Op, LD->getBasePtr(), Base, Scale, Index, Disp)) |
Chris Lattner | 3a7cd95 | 2006-10-07 21:55:32 +0000 | [diff] [blame] | 958 | return false; |
Evan Cheng | 82a9164 | 2006-10-11 21:06:01 +0000 | [diff] [blame] | 959 | OutChain = LD->getChain(); |
Chris Lattner | 3a7cd95 | 2006-10-07 21:55:32 +0000 | [diff] [blame] | 960 | return true; |
| 961 | } |
| 962 | } |
Chris Lattner | 4fe4f25 | 2006-10-11 22:09:58 +0000 | [diff] [blame] | 963 | |
| 964 | // Also handle the case where we explicitly require zeros in the top |
Chris Lattner | 3a7cd95 | 2006-10-07 21:55:32 +0000 | [diff] [blame] | 965 | // elements. This is a vector shuffle from the zero vector. |
Chris Lattner | 4fe4f25 | 2006-10-11 22:09:58 +0000 | [diff] [blame] | 966 | if (N.getOpcode() == ISD::VECTOR_SHUFFLE && N.Val->hasOneUse() && |
Chris Lattner | 8a59448 | 2007-11-25 00:24:49 +0000 | [diff] [blame] | 967 | // Check to see if the top elements are all zeros (or bitcast of zeros). |
| 968 | ISD::isBuildVectorAllZeros(N.getOperand(0).Val) && |
Chris Lattner | 4fe4f25 | 2006-10-11 22:09:58 +0000 | [diff] [blame] | 969 | N.getOperand(1).getOpcode() == ISD::SCALAR_TO_VECTOR && |
| 970 | N.getOperand(1).Val->hasOneUse() && |
| 971 | ISD::isNON_EXTLoad(N.getOperand(1).getOperand(0).Val) && |
| 972 | N.getOperand(1).getOperand(0).hasOneUse()) { |
Chris Lattner | 4fe4f25 | 2006-10-11 22:09:58 +0000 | [diff] [blame] | 973 | // Check to see if the shuffle mask is 4/L/L/L or 2/L, where L is something |
| 974 | // from the LHS. |
Chris Lattner | 8a59448 | 2007-11-25 00:24:49 +0000 | [diff] [blame] | 975 | unsigned VecWidth=MVT::getVectorNumElements(N.getOperand(0).getValueType()); |
Chris Lattner | 4fe4f25 | 2006-10-11 22:09:58 +0000 | [diff] [blame] | 976 | SDOperand ShufMask = N.getOperand(2); |
| 977 | assert(ShufMask.getOpcode() == ISD::BUILD_VECTOR && "Invalid shuf mask!"); |
| 978 | if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(ShufMask.getOperand(0))) { |
| 979 | if (C->getValue() == VecWidth) { |
| 980 | for (unsigned i = 1; i != VecWidth; ++i) { |
| 981 | if (ShufMask.getOperand(i).getOpcode() == ISD::UNDEF) { |
| 982 | // ok. |
| 983 | } else { |
| 984 | ConstantSDNode *C = cast<ConstantSDNode>(ShufMask.getOperand(i)); |
| 985 | if (C->getValue() >= VecWidth) return false; |
| 986 | } |
| 987 | } |
| 988 | } |
| 989 | |
| 990 | // Okay, this is a zero extending load. Fold it. |
| 991 | LoadSDNode *LD = cast<LoadSDNode>(N.getOperand(1).getOperand(0)); |
Evan Cheng | 0d53826 | 2006-11-08 20:34:28 +0000 | [diff] [blame] | 992 | if (!SelectAddr(Op, LD->getBasePtr(), Base, Scale, Index, Disp)) |
Chris Lattner | 4fe4f25 | 2006-10-11 22:09:58 +0000 | [diff] [blame] | 993 | return false; |
| 994 | OutChain = LD->getChain(); |
| 995 | InChain = SDOperand(LD, 1); |
| 996 | return true; |
| 997 | } |
| 998 | } |
Chris Lattner | 3a7cd95 | 2006-10-07 21:55:32 +0000 | [diff] [blame] | 999 | return false; |
| 1000 | } |
| 1001 | |
| 1002 | |
Evan Cheng | 51a9ed9 | 2006-02-25 10:09:08 +0000 | [diff] [blame] | 1003 | /// SelectLEAAddr - it calls SelectAddr and determines if the maximal addressing |
| 1004 | /// mode it matches can be cost effectively emitted as an LEA instruction. |
Evan Cheng | 0d53826 | 2006-11-08 20:34:28 +0000 | [diff] [blame] | 1005 | bool X86DAGToDAGISel::SelectLEAAddr(SDOperand Op, SDOperand N, |
| 1006 | SDOperand &Base, SDOperand &Scale, |
Evan Cheng | 51a9ed9 | 2006-02-25 10:09:08 +0000 | [diff] [blame] | 1007 | SDOperand &Index, SDOperand &Disp) { |
| 1008 | X86ISelAddressMode AM; |
| 1009 | if (MatchAddress(N, AM)) |
| 1010 | return false; |
| 1011 | |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 1012 | MVT::ValueType VT = N.getValueType(); |
Evan Cheng | 51a9ed9 | 2006-02-25 10:09:08 +0000 | [diff] [blame] | 1013 | unsigned Complexity = 0; |
| 1014 | if (AM.BaseType == X86ISelAddressMode::RegBase) |
| 1015 | if (AM.Base.Reg.Val) |
| 1016 | Complexity = 1; |
| 1017 | else |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 1018 | AM.Base.Reg = CurDAG->getRegister(0, VT); |
Evan Cheng | 51a9ed9 | 2006-02-25 10:09:08 +0000 | [diff] [blame] | 1019 | else if (AM.BaseType == X86ISelAddressMode::FrameIndexBase) |
| 1020 | Complexity = 4; |
| 1021 | |
| 1022 | if (AM.IndexReg.Val) |
| 1023 | Complexity++; |
| 1024 | else |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 1025 | AM.IndexReg = CurDAG->getRegister(0, VT); |
Evan Cheng | 51a9ed9 | 2006-02-25 10:09:08 +0000 | [diff] [blame] | 1026 | |
Chris Lattner | a16b7cb | 2007-03-20 06:08:29 +0000 | [diff] [blame] | 1027 | // Don't match just leal(,%reg,2). It's cheaper to do addl %reg, %reg, or with |
| 1028 | // a simple shift. |
| 1029 | if (AM.Scale > 1) |
Evan Cheng | 8c03fe4 | 2006-02-28 21:13:57 +0000 | [diff] [blame] | 1030 | Complexity++; |
Evan Cheng | 51a9ed9 | 2006-02-25 10:09:08 +0000 | [diff] [blame] | 1031 | |
| 1032 | // FIXME: We are artificially lowering the criteria to turn ADD %reg, $GA |
| 1033 | // to a LEA. This is determined with some expermentation but is by no means |
| 1034 | // optimal (especially for code size consideration). LEA is nice because of |
| 1035 | // its three-address nature. Tweak the cost function again when we can run |
| 1036 | // convertToThreeAddress() at register allocation time. |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 1037 | if (AM.GV || AM.CP || AM.ES || AM.JT != -1) { |
| 1038 | // For X86-64, we should always use lea to materialize RIP relative |
| 1039 | // addresses. |
Evan Cheng | 953fa04 | 2006-12-05 22:03:40 +0000 | [diff] [blame] | 1040 | if (Subtarget->is64Bit()) |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 1041 | Complexity = 4; |
| 1042 | else |
| 1043 | Complexity += 2; |
| 1044 | } |
Evan Cheng | 51a9ed9 | 2006-02-25 10:09:08 +0000 | [diff] [blame] | 1045 | |
| 1046 | if (AM.Disp && (AM.Base.Reg.Val || AM.IndexReg.Val)) |
| 1047 | Complexity++; |
| 1048 | |
| 1049 | if (Complexity > 2) { |
| 1050 | getAddressOperands(AM, Base, Scale, Index, Disp); |
| 1051 | return true; |
| 1052 | } |
Evan Cheng | 51a9ed9 | 2006-02-25 10:09:08 +0000 | [diff] [blame] | 1053 | return false; |
| 1054 | } |
| 1055 | |
Evan Cheng | 5e35168 | 2006-02-06 06:02:33 +0000 | [diff] [blame] | 1056 | bool X86DAGToDAGISel::TryFoldLoad(SDOperand P, SDOperand N, |
| 1057 | SDOperand &Base, SDOperand &Scale, |
| 1058 | SDOperand &Index, SDOperand &Disp) { |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 1059 | if (ISD::isNON_EXTLoad(N.Val) && |
Evan Cheng | 5e35168 | 2006-02-06 06:02:33 +0000 | [diff] [blame] | 1060 | N.hasOneUse() && |
Evan Cheng | 27e1fe9 | 2006-10-14 08:33:25 +0000 | [diff] [blame] | 1061 | CanBeFoldedBy(N.Val, P.Val, P.Val)) |
Evan Cheng | 0d53826 | 2006-11-08 20:34:28 +0000 | [diff] [blame] | 1062 | return SelectAddr(P, N.getOperand(1), Base, Scale, Index, Disp); |
Evan Cheng | 0114e94 | 2006-01-06 20:36:21 +0000 | [diff] [blame] | 1063 | return false; |
| 1064 | } |
| 1065 | |
Evan Cheng | 7ccced6 | 2006-02-18 00:15:05 +0000 | [diff] [blame] | 1066 | /// getGlobalBaseReg - Output the instructions required to put the |
| 1067 | /// base address to use for accessing globals into a register. |
| 1068 | /// |
Evan Cheng | 9ade218 | 2006-08-26 05:34:46 +0000 | [diff] [blame] | 1069 | SDNode *X86DAGToDAGISel::getGlobalBaseReg() { |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 1070 | assert(!Subtarget->is64Bit() && "X86-64 PIC uses RIP relative addressing"); |
Evan Cheng | 7ccced6 | 2006-02-18 00:15:05 +0000 | [diff] [blame] | 1071 | if (!GlobalBaseReg) { |
| 1072 | // Insert the set of GlobalBaseReg into the first MBB of the function |
Evan Cheng | 0475ab5 | 2008-01-05 00:41:47 +0000 | [diff] [blame] | 1073 | MachineFunction *MF = BB->getParent(); |
| 1074 | MachineBasicBlock &FirstMBB = MF->front(); |
Evan Cheng | 7ccced6 | 2006-02-18 00:15:05 +0000 | [diff] [blame] | 1075 | MachineBasicBlock::iterator MBBI = FirstMBB.begin(); |
Evan Cheng | 0475ab5 | 2008-01-05 00:41:47 +0000 | [diff] [blame] | 1076 | MachineRegisterInfo &RegInfo = MF->getRegInfo(); |
Chris Lattner | 84bc542 | 2007-12-31 04:13:23 +0000 | [diff] [blame] | 1077 | unsigned PC = RegInfo.createVirtualRegister(X86::GR32RegisterClass); |
Anton Korobeynikov | 7f70559 | 2007-01-12 19:20:47 +0000 | [diff] [blame] | 1078 | |
Evan Cheng | c0f64ff | 2006-11-27 23:37:22 +0000 | [diff] [blame] | 1079 | const TargetInstrInfo *TII = TM.getInstrInfo(); |
Evan Cheng | f02ca69 | 2007-12-22 02:26:46 +0000 | [diff] [blame] | 1080 | // Operand of MovePCtoStack is completely ignored by asm printer. It's |
| 1081 | // only used in JIT code emission as displacement to pc. |
Evan Cheng | 0475ab5 | 2008-01-05 00:41:47 +0000 | [diff] [blame] | 1082 | BuildMI(FirstMBB, MBBI, TII->get(X86::MOVPC32r), PC).addImm(0); |
Anton Korobeynikov | 7f70559 | 2007-01-12 19:20:47 +0000 | [diff] [blame] | 1083 | |
| 1084 | // If we're using vanilla 'GOT' PIC style, we should use relative addressing |
| 1085 | // not to pc, but to _GLOBAL_ADDRESS_TABLE_ external |
Evan Cheng | 706535d | 2007-01-22 21:34:25 +0000 | [diff] [blame] | 1086 | if (TM.getRelocationModel() == Reloc::PIC_ && |
| 1087 | Subtarget->isPICStyleGOT()) { |
Chris Lattner | 84bc542 | 2007-12-31 04:13:23 +0000 | [diff] [blame] | 1088 | GlobalBaseReg = RegInfo.createVirtualRegister(X86::GR32RegisterClass); |
Evan Cheng | 0475ab5 | 2008-01-05 00:41:47 +0000 | [diff] [blame] | 1089 | BuildMI(FirstMBB, MBBI, TII->get(X86::ADD32ri), GlobalBaseReg) |
| 1090 | .addReg(PC).addExternalSymbol("_GLOBAL_OFFSET_TABLE_"); |
Anton Korobeynikov | 7f70559 | 2007-01-12 19:20:47 +0000 | [diff] [blame] | 1091 | } else { |
| 1092 | GlobalBaseReg = PC; |
| 1093 | } |
| 1094 | |
Evan Cheng | 7ccced6 | 2006-02-18 00:15:05 +0000 | [diff] [blame] | 1095 | } |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 1096 | return CurDAG->getRegister(GlobalBaseReg, TLI.getPointerTy()).Val; |
Evan Cheng | 7ccced6 | 2006-02-18 00:15:05 +0000 | [diff] [blame] | 1097 | } |
| 1098 | |
Evan Cheng | b245d92 | 2006-05-20 01:36:52 +0000 | [diff] [blame] | 1099 | static SDNode *FindCallStartFromCall(SDNode *Node) { |
| 1100 | if (Node->getOpcode() == ISD::CALLSEQ_START) return Node; |
| 1101 | assert(Node->getOperand(0).getValueType() == MVT::Other && |
| 1102 | "Node doesn't have a token chain argument!"); |
| 1103 | return FindCallStartFromCall(Node->getOperand(0).Val); |
| 1104 | } |
| 1105 | |
Christopher Lamb | c59e521 | 2007-08-10 21:48:46 +0000 | [diff] [blame] | 1106 | SDNode *X86DAGToDAGISel::getTruncate(SDOperand N0, MVT::ValueType VT) { |
| 1107 | SDOperand SRIdx; |
| 1108 | switch (VT) { |
| 1109 | case MVT::i8: |
| 1110 | SRIdx = CurDAG->getTargetConstant(1, MVT::i32); // SubRegSet 1 |
| 1111 | // Ensure that the source register has an 8-bit subreg on 32-bit targets |
| 1112 | if (!Subtarget->is64Bit()) { |
| 1113 | unsigned Opc; |
| 1114 | MVT::ValueType VT; |
| 1115 | switch (N0.getValueType()) { |
| 1116 | default: assert(0 && "Unknown truncate!"); |
| 1117 | case MVT::i16: |
| 1118 | Opc = X86::MOV16to16_; |
| 1119 | VT = MVT::i16; |
| 1120 | break; |
| 1121 | case MVT::i32: |
| 1122 | Opc = X86::MOV32to32_; |
| 1123 | VT = MVT::i32; |
| 1124 | break; |
| 1125 | } |
Evan Cheng | 96aaa54 | 2007-10-12 07:55:53 +0000 | [diff] [blame] | 1126 | N0 = SDOperand(CurDAG->getTargetNode(Opc, VT, MVT::Flag, N0), 0); |
| 1127 | return CurDAG->getTargetNode(X86::EXTRACT_SUBREG, |
| 1128 | VT, N0, SRIdx, N0.getValue(1)); |
Christopher Lamb | c59e521 | 2007-08-10 21:48:46 +0000 | [diff] [blame] | 1129 | } |
| 1130 | break; |
| 1131 | case MVT::i16: |
| 1132 | SRIdx = CurDAG->getTargetConstant(2, MVT::i32); // SubRegSet 2 |
| 1133 | break; |
| 1134 | case MVT::i32: |
| 1135 | SRIdx = CurDAG->getTargetConstant(3, MVT::i32); // SubRegSet 3 |
| 1136 | break; |
Evan Cheng | 96aaa54 | 2007-10-12 07:55:53 +0000 | [diff] [blame] | 1137 | default: assert(0 && "Unknown truncate!"); break; |
Christopher Lamb | c59e521 | 2007-08-10 21:48:46 +0000 | [diff] [blame] | 1138 | } |
Evan Cheng | 96aaa54 | 2007-10-12 07:55:53 +0000 | [diff] [blame] | 1139 | return CurDAG->getTargetNode(X86::EXTRACT_SUBREG, VT, N0, SRIdx); |
Christopher Lamb | c59e521 | 2007-08-10 21:48:46 +0000 | [diff] [blame] | 1140 | } |
| 1141 | |
| 1142 | |
Evan Cheng | 9ade218 | 2006-08-26 05:34:46 +0000 | [diff] [blame] | 1143 | SDNode *X86DAGToDAGISel::Select(SDOperand N) { |
Evan Cheng | def941b | 2005-12-15 01:02:48 +0000 | [diff] [blame] | 1144 | SDNode *Node = N.Val; |
| 1145 | MVT::ValueType NVT = Node->getValueType(0); |
Evan Cheng | 0114e94 | 2006-01-06 20:36:21 +0000 | [diff] [blame] | 1146 | unsigned Opc, MOpc; |
| 1147 | unsigned Opcode = Node->getOpcode(); |
Chris Lattner | c961eea | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 1148 | |
Evan Cheng | f597dc7 | 2006-02-10 22:24:32 +0000 | [diff] [blame] | 1149 | #ifndef NDEBUG |
Bill Wendling | 6345d75 | 2006-11-17 07:52:03 +0000 | [diff] [blame] | 1150 | DOUT << std::string(Indent, ' ') << "Selecting: "; |
Evan Cheng | f597dc7 | 2006-02-10 22:24:32 +0000 | [diff] [blame] | 1151 | DEBUG(Node->dump(CurDAG)); |
Bill Wendling | 6345d75 | 2006-11-17 07:52:03 +0000 | [diff] [blame] | 1152 | DOUT << "\n"; |
Evan Cheng | 23addc0 | 2006-02-10 22:46:26 +0000 | [diff] [blame] | 1153 | Indent += 2; |
Evan Cheng | f597dc7 | 2006-02-10 22:24:32 +0000 | [diff] [blame] | 1154 | #endif |
| 1155 | |
Evan Cheng | 3416721 | 2006-02-09 00:37:58 +0000 | [diff] [blame] | 1156 | if (Opcode >= ISD::BUILTIN_OP_END && Opcode < X86ISD::FIRST_NUMBER) { |
Evan Cheng | f597dc7 | 2006-02-10 22:24:32 +0000 | [diff] [blame] | 1157 | #ifndef NDEBUG |
Bill Wendling | 6345d75 | 2006-11-17 07:52:03 +0000 | [diff] [blame] | 1158 | DOUT << std::string(Indent-2, ' ') << "== "; |
Evan Cheng | f597dc7 | 2006-02-10 22:24:32 +0000 | [diff] [blame] | 1159 | DEBUG(Node->dump(CurDAG)); |
Bill Wendling | 6345d75 | 2006-11-17 07:52:03 +0000 | [diff] [blame] | 1160 | DOUT << "\n"; |
Evan Cheng | 23addc0 | 2006-02-10 22:46:26 +0000 | [diff] [blame] | 1161 | Indent -= 2; |
Evan Cheng | f597dc7 | 2006-02-10 22:24:32 +0000 | [diff] [blame] | 1162 | #endif |
Evan Cheng | 64a752f | 2006-08-11 09:08:15 +0000 | [diff] [blame] | 1163 | return NULL; // Already selected. |
Evan Cheng | 3416721 | 2006-02-09 00:37:58 +0000 | [diff] [blame] | 1164 | } |
Evan Cheng | 38262ca | 2006-01-11 22:15:18 +0000 | [diff] [blame] | 1165 | |
Evan Cheng | 0114e94 | 2006-01-06 20:36:21 +0000 | [diff] [blame] | 1166 | switch (Opcode) { |
Chris Lattner | c961eea | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 1167 | default: break; |
Evan Cheng | 020d2e8 | 2006-02-23 20:41:18 +0000 | [diff] [blame] | 1168 | case X86ISD::GlobalBaseReg: |
Evan Cheng | 9ade218 | 2006-08-26 05:34:46 +0000 | [diff] [blame] | 1169 | return getGlobalBaseReg(); |
Evan Cheng | 020d2e8 | 2006-02-23 20:41:18 +0000 | [diff] [blame] | 1170 | |
Chris Lattner | 6fa2f9c | 2008-03-09 07:05:32 +0000 | [diff] [blame] | 1171 | case X86ISD::FP_GET_ST0_ST1: { |
Evan Cheng | 0d9e976 | 2008-01-29 19:34:22 +0000 | [diff] [blame] | 1172 | SDOperand Chain = N.getOperand(0); |
| 1173 | SDOperand InFlag = N.getOperand(1); |
| 1174 | AddToISelQueue(Chain); |
| 1175 | AddToISelQueue(InFlag); |
| 1176 | std::vector<MVT::ValueType> Tys; |
| 1177 | Tys.push_back(MVT::f80); |
| 1178 | Tys.push_back(MVT::f80); |
| 1179 | Tys.push_back(MVT::Other); |
| 1180 | Tys.push_back(MVT::Flag); |
| 1181 | SDOperand Ops[] = { Chain, InFlag }; |
Chris Lattner | 6fa2f9c | 2008-03-09 07:05:32 +0000 | [diff] [blame] | 1182 | SDNode *ResNode = CurDAG->getTargetNode(X86::FpGET_ST0_ST1, Tys, |
Evan Cheng | 0d9e976 | 2008-01-29 19:34:22 +0000 | [diff] [blame] | 1183 | Ops, 2); |
| 1184 | Chain = SDOperand(ResNode, 2); |
| 1185 | InFlag = SDOperand(ResNode, 3); |
| 1186 | ReplaceUses(SDOperand(N.Val, 2), Chain); |
| 1187 | ReplaceUses(SDOperand(N.Val, 3), InFlag); |
| 1188 | return ResNode; |
| 1189 | } |
| 1190 | |
Evan Cheng | 51a9ed9 | 2006-02-25 10:09:08 +0000 | [diff] [blame] | 1191 | case ISD::ADD: { |
| 1192 | // Turn ADD X, c to MOV32ri X+c. This cannot be done with tblgen'd |
| 1193 | // code and is matched first so to prevent it from being turned into |
| 1194 | // LEA32r X+c. |
Evan Cheng | b1a9aec | 2008-01-08 02:06:11 +0000 | [diff] [blame] | 1195 | // In 64-bit small code size mode, use LEA to take advantage of |
| 1196 | // RIP-relative addressing. |
| 1197 | if (TM.getCodeModel() != CodeModel::Small) |
| 1198 | break; |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 1199 | MVT::ValueType PtrVT = TLI.getPointerTy(); |
Evan Cheng | 51a9ed9 | 2006-02-25 10:09:08 +0000 | [diff] [blame] | 1200 | SDOperand N0 = N.getOperand(0); |
| 1201 | SDOperand N1 = N.getOperand(1); |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 1202 | if (N.Val->getValueType(0) == PtrVT && |
Evan Cheng | 19f2ffc | 2006-12-05 04:01:03 +0000 | [diff] [blame] | 1203 | N0.getOpcode() == X86ISD::Wrapper && |
Evan Cheng | 51a9ed9 | 2006-02-25 10:09:08 +0000 | [diff] [blame] | 1204 | N1.getOpcode() == ISD::Constant) { |
| 1205 | unsigned Offset = (unsigned)cast<ConstantSDNode>(N1)->getValue(); |
| 1206 | SDOperand C(0, 0); |
| 1207 | // TODO: handle ExternalSymbolSDNode. |
| 1208 | if (GlobalAddressSDNode *G = |
| 1209 | dyn_cast<GlobalAddressSDNode>(N0.getOperand(0))) { |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 1210 | C = CurDAG->getTargetGlobalAddress(G->getGlobal(), PtrVT, |
Evan Cheng | 51a9ed9 | 2006-02-25 10:09:08 +0000 | [diff] [blame] | 1211 | G->getOffset() + Offset); |
| 1212 | } else if (ConstantPoolSDNode *CP = |
| 1213 | dyn_cast<ConstantPoolSDNode>(N0.getOperand(0))) { |
Evan Cheng | c356a57 | 2006-09-12 21:04:05 +0000 | [diff] [blame] | 1214 | C = CurDAG->getTargetConstantPool(CP->getConstVal(), PtrVT, |
Evan Cheng | 51a9ed9 | 2006-02-25 10:09:08 +0000 | [diff] [blame] | 1215 | CP->getAlignment(), |
| 1216 | CP->getOffset()+Offset); |
| 1217 | } |
| 1218 | |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 1219 | if (C.Val) { |
| 1220 | if (Subtarget->is64Bit()) { |
| 1221 | SDOperand Ops[] = { CurDAG->getRegister(0, PtrVT), getI8Imm(1), |
| 1222 | CurDAG->getRegister(0, PtrVT), C }; |
| 1223 | return CurDAG->SelectNodeTo(N.Val, X86::LEA64r, MVT::i64, Ops, 4); |
| 1224 | } else |
| 1225 | return CurDAG->SelectNodeTo(N.Val, X86::MOV32ri, PtrVT, C); |
| 1226 | } |
Evan Cheng | 51a9ed9 | 2006-02-25 10:09:08 +0000 | [diff] [blame] | 1227 | } |
| 1228 | |
| 1229 | // Other cases are handled by auto-generated code. |
| 1230 | break; |
Evan Cheng | a0ea053 | 2006-02-23 02:43:52 +0000 | [diff] [blame] | 1231 | } |
Evan Cheng | 020d2e8 | 2006-02-23 20:41:18 +0000 | [diff] [blame] | 1232 | |
Dan Gohman | 525178c | 2007-10-08 18:33:35 +0000 | [diff] [blame] | 1233 | case ISD::SMUL_LOHI: |
| 1234 | case ISD::UMUL_LOHI: { |
| 1235 | SDOperand N0 = Node->getOperand(0); |
| 1236 | SDOperand N1 = Node->getOperand(1); |
| 1237 | |
Dan Gohman | 525178c | 2007-10-08 18:33:35 +0000 | [diff] [blame] | 1238 | bool isSigned = Opcode == ISD::SMUL_LOHI; |
| 1239 | if (!isSigned) |
Evan Cheng | 0114e94 | 2006-01-06 20:36:21 +0000 | [diff] [blame] | 1240 | switch (NVT) { |
| 1241 | default: assert(0 && "Unsupported VT!"); |
| 1242 | case MVT::i8: Opc = X86::MUL8r; MOpc = X86::MUL8m; break; |
| 1243 | case MVT::i16: Opc = X86::MUL16r; MOpc = X86::MUL16m; break; |
| 1244 | case MVT::i32: Opc = X86::MUL32r; MOpc = X86::MUL32m; break; |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 1245 | case MVT::i64: Opc = X86::MUL64r; MOpc = X86::MUL64m; break; |
Evan Cheng | 0114e94 | 2006-01-06 20:36:21 +0000 | [diff] [blame] | 1246 | } |
| 1247 | else |
| 1248 | switch (NVT) { |
| 1249 | default: assert(0 && "Unsupported VT!"); |
| 1250 | case MVT::i8: Opc = X86::IMUL8r; MOpc = X86::IMUL8m; break; |
| 1251 | case MVT::i16: Opc = X86::IMUL16r; MOpc = X86::IMUL16m; break; |
| 1252 | case MVT::i32: Opc = X86::IMUL32r; MOpc = X86::IMUL32m; break; |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 1253 | case MVT::i64: Opc = X86::IMUL64r; MOpc = X86::IMUL64m; break; |
Evan Cheng | 0114e94 | 2006-01-06 20:36:21 +0000 | [diff] [blame] | 1254 | } |
| 1255 | |
| 1256 | unsigned LoReg, HiReg; |
| 1257 | switch (NVT) { |
| 1258 | default: assert(0 && "Unsupported VT!"); |
| 1259 | case MVT::i8: LoReg = X86::AL; HiReg = X86::AH; break; |
| 1260 | case MVT::i16: LoReg = X86::AX; HiReg = X86::DX; break; |
| 1261 | case MVT::i32: LoReg = X86::EAX; HiReg = X86::EDX; break; |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 1262 | case MVT::i64: LoReg = X86::RAX; HiReg = X86::RDX; break; |
Evan Cheng | 0114e94 | 2006-01-06 20:36:21 +0000 | [diff] [blame] | 1263 | } |
| 1264 | |
Evan Cheng | 0114e94 | 2006-01-06 20:36:21 +0000 | [diff] [blame] | 1265 | SDOperand Tmp0, Tmp1, Tmp2, Tmp3; |
Evan Cheng | 7afa166 | 2007-08-02 05:48:35 +0000 | [diff] [blame] | 1266 | bool foldedLoad = TryFoldLoad(N, N1, Tmp0, Tmp1, Tmp2, Tmp3); |
Dan Gohman | 525178c | 2007-10-08 18:33:35 +0000 | [diff] [blame] | 1267 | // multiplty is commmutative |
Evan Cheng | 948f343 | 2006-01-06 23:19:29 +0000 | [diff] [blame] | 1268 | if (!foldedLoad) { |
Evan Cheng | 5e35168 | 2006-02-06 06:02:33 +0000 | [diff] [blame] | 1269 | foldedLoad = TryFoldLoad(N, N0, Tmp0, Tmp1, Tmp2, Tmp3); |
Evan Cheng | 7afa166 | 2007-08-02 05:48:35 +0000 | [diff] [blame] | 1270 | if (foldedLoad) |
| 1271 | std::swap(N0, N1); |
Evan Cheng | 948f343 | 2006-01-06 23:19:29 +0000 | [diff] [blame] | 1272 | } |
| 1273 | |
Evan Cheng | 0469990 | 2006-08-26 01:05:16 +0000 | [diff] [blame] | 1274 | AddToISelQueue(N0); |
Dan Gohman | 525178c | 2007-10-08 18:33:35 +0000 | [diff] [blame] | 1275 | SDOperand InFlag = CurDAG->getCopyToReg(CurDAG->getEntryNode(), LoReg, |
| 1276 | N0, SDOperand()).getValue(1); |
Evan Cheng | 0114e94 | 2006-01-06 20:36:21 +0000 | [diff] [blame] | 1277 | |
| 1278 | if (foldedLoad) { |
Dan Gohman | 525178c | 2007-10-08 18:33:35 +0000 | [diff] [blame] | 1279 | AddToISelQueue(N1.getOperand(0)); |
Evan Cheng | 0469990 | 2006-08-26 01:05:16 +0000 | [diff] [blame] | 1280 | AddToISelQueue(Tmp0); |
| 1281 | AddToISelQueue(Tmp1); |
| 1282 | AddToISelQueue(Tmp2); |
| 1283 | AddToISelQueue(Tmp3); |
Dan Gohman | 525178c | 2007-10-08 18:33:35 +0000 | [diff] [blame] | 1284 | SDOperand Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, N1.getOperand(0), InFlag }; |
Evan Cheng | 7e9b26f | 2006-02-09 07:17:49 +0000 | [diff] [blame] | 1285 | SDNode *CNode = |
Evan Cheng | 0b828e0 | 2006-08-27 08:14:06 +0000 | [diff] [blame] | 1286 | CurDAG->getTargetNode(MOpc, MVT::Other, MVT::Flag, Ops, 6); |
Evan Cheng | 7e9b26f | 2006-02-09 07:17:49 +0000 | [diff] [blame] | 1287 | InFlag = SDOperand(CNode, 1); |
Dan Gohman | 525178c | 2007-10-08 18:33:35 +0000 | [diff] [blame] | 1288 | // Update the chain. |
| 1289 | ReplaceUses(N1.getValue(1), SDOperand(CNode, 0)); |
Evan Cheng | 0114e94 | 2006-01-06 20:36:21 +0000 | [diff] [blame] | 1290 | } else { |
Evan Cheng | 0469990 | 2006-08-26 01:05:16 +0000 | [diff] [blame] | 1291 | AddToISelQueue(N1); |
Evan Cheng | 7e9b26f | 2006-02-09 07:17:49 +0000 | [diff] [blame] | 1292 | InFlag = |
| 1293 | SDOperand(CurDAG->getTargetNode(Opc, MVT::Flag, N1, InFlag), 0); |
Evan Cheng | 0114e94 | 2006-01-06 20:36:21 +0000 | [diff] [blame] | 1294 | } |
| 1295 | |
Dan Gohman | 525178c | 2007-10-08 18:33:35 +0000 | [diff] [blame] | 1296 | // Copy the low half of the result, if it is needed. |
| 1297 | if (!N.getValue(0).use_empty()) { |
| 1298 | SDOperand Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), |
| 1299 | LoReg, NVT, InFlag); |
| 1300 | InFlag = Result.getValue(2); |
| 1301 | ReplaceUses(N.getValue(0), Result); |
| 1302 | #ifndef NDEBUG |
| 1303 | DOUT << std::string(Indent-2, ' ') << "=> "; |
| 1304 | DEBUG(Result.Val->dump(CurDAG)); |
| 1305 | DOUT << "\n"; |
| 1306 | #endif |
Evan Cheng | f7ef26e | 2007-08-09 21:59:35 +0000 | [diff] [blame] | 1307 | } |
Dan Gohman | 525178c | 2007-10-08 18:33:35 +0000 | [diff] [blame] | 1308 | // Copy the high half of the result, if it is needed. |
| 1309 | if (!N.getValue(1).use_empty()) { |
| 1310 | SDOperand Result; |
| 1311 | if (HiReg == X86::AH && Subtarget->is64Bit()) { |
| 1312 | // Prevent use of AH in a REX instruction by referencing AX instead. |
| 1313 | // Shift it down 8 bits. |
| 1314 | Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), |
| 1315 | X86::AX, MVT::i16, InFlag); |
| 1316 | InFlag = Result.getValue(2); |
| 1317 | Result = SDOperand(CurDAG->getTargetNode(X86::SHR16ri, MVT::i16, Result, |
| 1318 | CurDAG->getTargetConstant(8, MVT::i8)), 0); |
| 1319 | // Then truncate it down to i8. |
| 1320 | SDOperand SRIdx = CurDAG->getTargetConstant(1, MVT::i32); // SubRegSet 1 |
| 1321 | Result = SDOperand(CurDAG->getTargetNode(X86::EXTRACT_SUBREG, |
| 1322 | MVT::i8, Result, SRIdx), 0); |
| 1323 | } else { |
| 1324 | Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), |
| 1325 | HiReg, NVT, InFlag); |
| 1326 | InFlag = Result.getValue(2); |
| 1327 | } |
| 1328 | ReplaceUses(N.getValue(1), Result); |
| 1329 | #ifndef NDEBUG |
| 1330 | DOUT << std::string(Indent-2, ' ') << "=> "; |
| 1331 | DEBUG(Result.Val->dump(CurDAG)); |
| 1332 | DOUT << "\n"; |
| 1333 | #endif |
| 1334 | } |
Evan Cheng | 3416721 | 2006-02-09 00:37:58 +0000 | [diff] [blame] | 1335 | |
Evan Cheng | f597dc7 | 2006-02-10 22:24:32 +0000 | [diff] [blame] | 1336 | #ifndef NDEBUG |
Evan Cheng | 23addc0 | 2006-02-10 22:46:26 +0000 | [diff] [blame] | 1337 | Indent -= 2; |
Evan Cheng | f597dc7 | 2006-02-10 22:24:32 +0000 | [diff] [blame] | 1338 | #endif |
Dan Gohman | 525178c | 2007-10-08 18:33:35 +0000 | [diff] [blame] | 1339 | |
Evan Cheng | 64a752f | 2006-08-11 09:08:15 +0000 | [diff] [blame] | 1340 | return NULL; |
Evan Cheng | 948f343 | 2006-01-06 23:19:29 +0000 | [diff] [blame] | 1341 | } |
Evan Cheng | 7ccced6 | 2006-02-18 00:15:05 +0000 | [diff] [blame] | 1342 | |
Dan Gohman | 525178c | 2007-10-08 18:33:35 +0000 | [diff] [blame] | 1343 | case ISD::SDIVREM: |
| 1344 | case ISD::UDIVREM: { |
| 1345 | SDOperand N0 = Node->getOperand(0); |
| 1346 | SDOperand N1 = Node->getOperand(1); |
| 1347 | |
| 1348 | bool isSigned = Opcode == ISD::SDIVREM; |
Evan Cheng | 948f343 | 2006-01-06 23:19:29 +0000 | [diff] [blame] | 1349 | if (!isSigned) |
| 1350 | switch (NVT) { |
| 1351 | default: assert(0 && "Unsupported VT!"); |
| 1352 | case MVT::i8: Opc = X86::DIV8r; MOpc = X86::DIV8m; break; |
| 1353 | case MVT::i16: Opc = X86::DIV16r; MOpc = X86::DIV16m; break; |
| 1354 | case MVT::i32: Opc = X86::DIV32r; MOpc = X86::DIV32m; break; |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 1355 | case MVT::i64: Opc = X86::DIV64r; MOpc = X86::DIV64m; break; |
Evan Cheng | 948f343 | 2006-01-06 23:19:29 +0000 | [diff] [blame] | 1356 | } |
| 1357 | else |
| 1358 | switch (NVT) { |
| 1359 | default: assert(0 && "Unsupported VT!"); |
| 1360 | case MVT::i8: Opc = X86::IDIV8r; MOpc = X86::IDIV8m; break; |
| 1361 | case MVT::i16: Opc = X86::IDIV16r; MOpc = X86::IDIV16m; break; |
| 1362 | case MVT::i32: Opc = X86::IDIV32r; MOpc = X86::IDIV32m; break; |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 1363 | case MVT::i64: Opc = X86::IDIV64r; MOpc = X86::IDIV64m; break; |
Evan Cheng | 948f343 | 2006-01-06 23:19:29 +0000 | [diff] [blame] | 1364 | } |
| 1365 | |
| 1366 | unsigned LoReg, HiReg; |
| 1367 | unsigned ClrOpcode, SExtOpcode; |
| 1368 | switch (NVT) { |
| 1369 | default: assert(0 && "Unsupported VT!"); |
| 1370 | case MVT::i8: |
| 1371 | LoReg = X86::AL; HiReg = X86::AH; |
Evan Cheng | b1409ce | 2006-11-17 22:10:14 +0000 | [diff] [blame] | 1372 | ClrOpcode = 0; |
Evan Cheng | 948f343 | 2006-01-06 23:19:29 +0000 | [diff] [blame] | 1373 | SExtOpcode = X86::CBW; |
| 1374 | break; |
| 1375 | case MVT::i16: |
| 1376 | LoReg = X86::AX; HiReg = X86::DX; |
Evan Cheng | aede9b9 | 2006-06-02 21:20:34 +0000 | [diff] [blame] | 1377 | ClrOpcode = X86::MOV16r0; |
Evan Cheng | 948f343 | 2006-01-06 23:19:29 +0000 | [diff] [blame] | 1378 | SExtOpcode = X86::CWD; |
| 1379 | break; |
| 1380 | case MVT::i32: |
| 1381 | LoReg = X86::EAX; HiReg = X86::EDX; |
Evan Cheng | aede9b9 | 2006-06-02 21:20:34 +0000 | [diff] [blame] | 1382 | ClrOpcode = X86::MOV32r0; |
Evan Cheng | 948f343 | 2006-01-06 23:19:29 +0000 | [diff] [blame] | 1383 | SExtOpcode = X86::CDQ; |
| 1384 | break; |
Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 1385 | case MVT::i64: |
| 1386 | LoReg = X86::RAX; HiReg = X86::RDX; |
| 1387 | ClrOpcode = X86::MOV64r0; |
| 1388 | SExtOpcode = X86::CQO; |
| 1389 | break; |
Evan Cheng | 948f343 | 2006-01-06 23:19:29 +0000 | [diff] [blame] | 1390 | } |
| 1391 | |
Dan Gohman | 525178c | 2007-10-08 18:33:35 +0000 | [diff] [blame] | 1392 | SDOperand Tmp0, Tmp1, Tmp2, Tmp3; |
| 1393 | bool foldedLoad = TryFoldLoad(N, N1, Tmp0, Tmp1, Tmp2, Tmp3); |
| 1394 | |
| 1395 | SDOperand InFlag; |
Evan Cheng | b1409ce | 2006-11-17 22:10:14 +0000 | [diff] [blame] | 1396 | if (NVT == MVT::i8 && !isSigned) { |
| 1397 | // Special case for div8, just use a move with zero extension to AX to |
| 1398 | // clear the upper 8 bits (AH). |
| 1399 | SDOperand Tmp0, Tmp1, Tmp2, Tmp3, Move, Chain; |
| 1400 | if (TryFoldLoad(N, N0, Tmp0, Tmp1, Tmp2, Tmp3)) { |
| 1401 | SDOperand Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, N0.getOperand(0) }; |
| 1402 | AddToISelQueue(N0.getOperand(0)); |
| 1403 | AddToISelQueue(Tmp0); |
| 1404 | AddToISelQueue(Tmp1); |
| 1405 | AddToISelQueue(Tmp2); |
| 1406 | AddToISelQueue(Tmp3); |
| 1407 | Move = |
| 1408 | SDOperand(CurDAG->getTargetNode(X86::MOVZX16rm8, MVT::i16, MVT::Other, |
| 1409 | Ops, 5), 0); |
| 1410 | Chain = Move.getValue(1); |
| 1411 | ReplaceUses(N0.getValue(1), Chain); |
| 1412 | } else { |
| 1413 | AddToISelQueue(N0); |
| 1414 | Move = |
| 1415 | SDOperand(CurDAG->getTargetNode(X86::MOVZX16rr8, MVT::i16, N0), 0); |
| 1416 | Chain = CurDAG->getEntryNode(); |
| 1417 | } |
Dan Gohman | 525178c | 2007-10-08 18:33:35 +0000 | [diff] [blame] | 1418 | Chain = CurDAG->getCopyToReg(Chain, X86::AX, Move, SDOperand()); |
Evan Cheng | 948f343 | 2006-01-06 23:19:29 +0000 | [diff] [blame] | 1419 | InFlag = Chain.getValue(1); |
Evan Cheng | b1409ce | 2006-11-17 22:10:14 +0000 | [diff] [blame] | 1420 | } else { |
| 1421 | AddToISelQueue(N0); |
| 1422 | InFlag = |
Dan Gohman | 525178c | 2007-10-08 18:33:35 +0000 | [diff] [blame] | 1423 | CurDAG->getCopyToReg(CurDAG->getEntryNode(), |
| 1424 | LoReg, N0, SDOperand()).getValue(1); |
Evan Cheng | b1409ce | 2006-11-17 22:10:14 +0000 | [diff] [blame] | 1425 | if (isSigned) { |
| 1426 | // Sign extend the low part into the high part. |
| 1427 | InFlag = |
| 1428 | SDOperand(CurDAG->getTargetNode(SExtOpcode, MVT::Flag, InFlag), 0); |
| 1429 | } else { |
| 1430 | // Zero out the high part, effectively zero extending the input. |
| 1431 | SDOperand ClrNode = SDOperand(CurDAG->getTargetNode(ClrOpcode, NVT), 0); |
Dan Gohman | 525178c | 2007-10-08 18:33:35 +0000 | [diff] [blame] | 1432 | InFlag = CurDAG->getCopyToReg(CurDAG->getEntryNode(), HiReg, |
| 1433 | ClrNode, InFlag).getValue(1); |
Evan Cheng | b1409ce | 2006-11-17 22:10:14 +0000 | [diff] [blame] | 1434 | } |
Evan Cheng | 948f343 | 2006-01-06 23:19:29 +0000 | [diff] [blame] | 1435 | } |
| 1436 | |
| 1437 | if (foldedLoad) { |
Evan Cheng | b1409ce | 2006-11-17 22:10:14 +0000 | [diff] [blame] | 1438 | AddToISelQueue(N1.getOperand(0)); |
Evan Cheng | 0469990 | 2006-08-26 01:05:16 +0000 | [diff] [blame] | 1439 | AddToISelQueue(Tmp0); |
| 1440 | AddToISelQueue(Tmp1); |
| 1441 | AddToISelQueue(Tmp2); |
| 1442 | AddToISelQueue(Tmp3); |
Evan Cheng | b1409ce | 2006-11-17 22:10:14 +0000 | [diff] [blame] | 1443 | SDOperand Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, N1.getOperand(0), InFlag }; |
Evan Cheng | 7e9b26f | 2006-02-09 07:17:49 +0000 | [diff] [blame] | 1444 | SDNode *CNode = |
Evan Cheng | 0b828e0 | 2006-08-27 08:14:06 +0000 | [diff] [blame] | 1445 | CurDAG->getTargetNode(MOpc, MVT::Other, MVT::Flag, Ops, 6); |
Evan Cheng | 7e9b26f | 2006-02-09 07:17:49 +0000 | [diff] [blame] | 1446 | InFlag = SDOperand(CNode, 1); |
Dan Gohman | 525178c | 2007-10-08 18:33:35 +0000 | [diff] [blame] | 1447 | // Update the chain. |
| 1448 | ReplaceUses(N1.getValue(1), SDOperand(CNode, 0)); |
Evan Cheng | 948f343 | 2006-01-06 23:19:29 +0000 | [diff] [blame] | 1449 | } else { |
Evan Cheng | 0469990 | 2006-08-26 01:05:16 +0000 | [diff] [blame] | 1450 | AddToISelQueue(N1); |
Evan Cheng | 7e9b26f | 2006-02-09 07:17:49 +0000 | [diff] [blame] | 1451 | InFlag = |
| 1452 | SDOperand(CurDAG->getTargetNode(Opc, MVT::Flag, N1, InFlag), 0); |
Evan Cheng | 948f343 | 2006-01-06 23:19:29 +0000 | [diff] [blame] | 1453 | } |
| 1454 | |
Dan Gohman | a37c9f7 | 2007-09-25 18:23:27 +0000 | [diff] [blame] | 1455 | // Copy the division (low) result, if it is needed. |
| 1456 | if (!N.getValue(0).use_empty()) { |
Dan Gohman | 525178c | 2007-10-08 18:33:35 +0000 | [diff] [blame] | 1457 | SDOperand Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), |
| 1458 | LoReg, NVT, InFlag); |
Dan Gohman | a37c9f7 | 2007-09-25 18:23:27 +0000 | [diff] [blame] | 1459 | InFlag = Result.getValue(2); |
| 1460 | ReplaceUses(N.getValue(0), Result); |
| 1461 | #ifndef NDEBUG |
| 1462 | DOUT << std::string(Indent-2, ' ') << "=> "; |
| 1463 | DEBUG(Result.Val->dump(CurDAG)); |
| 1464 | DOUT << "\n"; |
| 1465 | #endif |
Evan Cheng | f7ef26e | 2007-08-09 21:59:35 +0000 | [diff] [blame] | 1466 | } |
Dan Gohman | a37c9f7 | 2007-09-25 18:23:27 +0000 | [diff] [blame] | 1467 | // Copy the remainder (high) result, if it is needed. |
| 1468 | if (!N.getValue(1).use_empty()) { |
| 1469 | SDOperand Result; |
| 1470 | if (HiReg == X86::AH && Subtarget->is64Bit()) { |
| 1471 | // Prevent use of AH in a REX instruction by referencing AX instead. |
| 1472 | // Shift it down 8 bits. |
Dan Gohman | 525178c | 2007-10-08 18:33:35 +0000 | [diff] [blame] | 1473 | Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), |
| 1474 | X86::AX, MVT::i16, InFlag); |
Dan Gohman | a37c9f7 | 2007-09-25 18:23:27 +0000 | [diff] [blame] | 1475 | InFlag = Result.getValue(2); |
| 1476 | Result = SDOperand(CurDAG->getTargetNode(X86::SHR16ri, MVT::i16, Result, |
| 1477 | CurDAG->getTargetConstant(8, MVT::i8)), 0); |
| 1478 | // Then truncate it down to i8. |
| 1479 | SDOperand SRIdx = CurDAG->getTargetConstant(1, MVT::i32); // SubRegSet 1 |
| 1480 | Result = SDOperand(CurDAG->getTargetNode(X86::EXTRACT_SUBREG, |
| 1481 | MVT::i8, Result, SRIdx), 0); |
| 1482 | } else { |
Dan Gohman | 525178c | 2007-10-08 18:33:35 +0000 | [diff] [blame] | 1483 | Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), |
| 1484 | HiReg, NVT, InFlag); |
Dan Gohman | a37c9f7 | 2007-09-25 18:23:27 +0000 | [diff] [blame] | 1485 | InFlag = Result.getValue(2); |
| 1486 | } |
| 1487 | ReplaceUses(N.getValue(1), Result); |
| 1488 | #ifndef NDEBUG |
| 1489 | DOUT << std::string(Indent-2, ' ') << "=> "; |
| 1490 | DEBUG(Result.Val->dump(CurDAG)); |
| 1491 | DOUT << "\n"; |
| 1492 | #endif |
| 1493 | } |
Evan Cheng | f597dc7 | 2006-02-10 22:24:32 +0000 | [diff] [blame] | 1494 | |
| 1495 | #ifndef NDEBUG |
Evan Cheng | 23addc0 | 2006-02-10 22:46:26 +0000 | [diff] [blame] | 1496 | Indent -= 2; |
Evan Cheng | f597dc7 | 2006-02-10 22:24:32 +0000 | [diff] [blame] | 1497 | #endif |
Evan Cheng | 64a752f | 2006-08-11 09:08:15 +0000 | [diff] [blame] | 1498 | |
| 1499 | return NULL; |
Evan Cheng | 0114e94 | 2006-01-06 20:36:21 +0000 | [diff] [blame] | 1500 | } |
Christopher Lamb | a1eb155 | 2007-08-10 22:22:41 +0000 | [diff] [blame] | 1501 | |
| 1502 | case ISD::ANY_EXTEND: { |
| 1503 | SDOperand N0 = Node->getOperand(0); |
| 1504 | AddToISelQueue(N0); |
| 1505 | if (NVT == MVT::i64 || NVT == MVT::i32 || NVT == MVT::i16) { |
| 1506 | SDOperand SRIdx; |
| 1507 | switch(N0.getValueType()) { |
| 1508 | case MVT::i32: |
Evan Cheng | 4499e49 | 2008-03-10 19:31:26 +0000 | [diff] [blame] | 1509 | SRIdx = CurDAG->getTargetConstant(3, MVT::i32); // SubRegSet 3 |
Christopher Lamb | a1eb155 | 2007-08-10 22:22:41 +0000 | [diff] [blame] | 1510 | break; |
| 1511 | case MVT::i16: |
Evan Cheng | 4499e49 | 2008-03-10 19:31:26 +0000 | [diff] [blame] | 1512 | SRIdx = CurDAG->getTargetConstant(2, MVT::i32); // SubRegSet 2 |
Christopher Lamb | a1eb155 | 2007-08-10 22:22:41 +0000 | [diff] [blame] | 1513 | break; |
| 1514 | case MVT::i8: |
| 1515 | if (Subtarget->is64Bit()) |
Evan Cheng | 4499e49 | 2008-03-10 19:31:26 +0000 | [diff] [blame] | 1516 | SRIdx = CurDAG->getTargetConstant(1, MVT::i32); // SubRegSet 1 |
Christopher Lamb | a1eb155 | 2007-08-10 22:22:41 +0000 | [diff] [blame] | 1517 | break; |
| 1518 | default: assert(0 && "Unknown any_extend!"); |
| 1519 | } |
| 1520 | if (SRIdx.Val) { |
Evan Cheng | 96aaa54 | 2007-10-12 07:55:53 +0000 | [diff] [blame] | 1521 | SDNode *ResNode = CurDAG->getTargetNode(X86::INSERT_SUBREG, |
Evan Cheng | 4499e49 | 2008-03-10 19:31:26 +0000 | [diff] [blame] | 1522 | NVT, N0, SRIdx); |
Christopher Lamb | a1eb155 | 2007-08-10 22:22:41 +0000 | [diff] [blame] | 1523 | |
| 1524 | #ifndef NDEBUG |
| 1525 | DOUT << std::string(Indent-2, ' ') << "=> "; |
| 1526 | DEBUG(ResNode->dump(CurDAG)); |
| 1527 | DOUT << "\n"; |
| 1528 | Indent -= 2; |
| 1529 | #endif |
| 1530 | return ResNode; |
| 1531 | } // Otherwise let generated ISel handle it. |
| 1532 | } |
| 1533 | break; |
| 1534 | } |
Christopher Lamb | c59e521 | 2007-08-10 21:48:46 +0000 | [diff] [blame] | 1535 | |
| 1536 | case ISD::SIGN_EXTEND_INREG: { |
| 1537 | SDOperand N0 = Node->getOperand(0); |
| 1538 | AddToISelQueue(N0); |
Evan Cheng | 403be7e | 2006-05-08 08:01:26 +0000 | [diff] [blame] | 1539 | |
Christopher Lamb | c59e521 | 2007-08-10 21:48:46 +0000 | [diff] [blame] | 1540 | MVT::ValueType SVT = cast<VTSDNode>(Node->getOperand(1))->getVT(); |
| 1541 | SDOperand TruncOp = SDOperand(getTruncate(N0, SVT), 0); |
Bill Wendling | 0d64287 | 2007-11-01 08:51:44 +0000 | [diff] [blame] | 1542 | unsigned Opc = 0; |
Christopher Lamb | 2dc6dc6 | 2007-07-29 01:24:57 +0000 | [diff] [blame] | 1543 | switch (NVT) { |
Christopher Lamb | 2dc6dc6 | 2007-07-29 01:24:57 +0000 | [diff] [blame] | 1544 | case MVT::i16: |
Christopher Lamb | c59e521 | 2007-08-10 21:48:46 +0000 | [diff] [blame] | 1545 | if (SVT == MVT::i8) Opc = X86::MOVSX16rr8; |
| 1546 | else assert(0 && "Unknown sign_extend_inreg!"); |
Christopher Lamb | 2dc6dc6 | 2007-07-29 01:24:57 +0000 | [diff] [blame] | 1547 | break; |
| 1548 | case MVT::i32: |
Christopher Lamb | c59e521 | 2007-08-10 21:48:46 +0000 | [diff] [blame] | 1549 | switch (SVT) { |
| 1550 | case MVT::i8: Opc = X86::MOVSX32rr8; break; |
| 1551 | case MVT::i16: Opc = X86::MOVSX32rr16; break; |
| 1552 | default: assert(0 && "Unknown sign_extend_inreg!"); |
| 1553 | } |
Christopher Lamb | 2dc6dc6 | 2007-07-29 01:24:57 +0000 | [diff] [blame] | 1554 | break; |
Christopher Lamb | c59e521 | 2007-08-10 21:48:46 +0000 | [diff] [blame] | 1555 | case MVT::i64: |
| 1556 | switch (SVT) { |
| 1557 | case MVT::i8: Opc = X86::MOVSX64rr8; break; |
| 1558 | case MVT::i16: Opc = X86::MOVSX64rr16; break; |
| 1559 | case MVT::i32: Opc = X86::MOVSX64rr32; break; |
| 1560 | default: assert(0 && "Unknown sign_extend_inreg!"); |
| 1561 | } |
| 1562 | break; |
| 1563 | default: assert(0 && "Unknown sign_extend_inreg!"); |
Christopher Lamb | 2dc6dc6 | 2007-07-29 01:24:57 +0000 | [diff] [blame] | 1564 | } |
Christopher Lamb | c59e521 | 2007-08-10 21:48:46 +0000 | [diff] [blame] | 1565 | |
| 1566 | SDNode *ResNode = CurDAG->getTargetNode(Opc, NVT, TruncOp); |
| 1567 | |
| 1568 | #ifndef NDEBUG |
| 1569 | DOUT << std::string(Indent-2, ' ') << "=> "; |
| 1570 | DEBUG(TruncOp.Val->dump(CurDAG)); |
| 1571 | DOUT << "\n"; |
| 1572 | DOUT << std::string(Indent-2, ' ') << "=> "; |
| 1573 | DEBUG(ResNode->dump(CurDAG)); |
| 1574 | DOUT << "\n"; |
| 1575 | Indent -= 2; |
| 1576 | #endif |
| 1577 | return ResNode; |
| 1578 | break; |
| 1579 | } |
| 1580 | |
| 1581 | case ISD::TRUNCATE: { |
| 1582 | SDOperand Input = Node->getOperand(0); |
| 1583 | AddToISelQueue(Node->getOperand(0)); |
| 1584 | SDNode *ResNode = getTruncate(Input, NVT); |
| 1585 | |
Evan Cheng | 403be7e | 2006-05-08 08:01:26 +0000 | [diff] [blame] | 1586 | #ifndef NDEBUG |
Bill Wendling | 6345d75 | 2006-11-17 07:52:03 +0000 | [diff] [blame] | 1587 | DOUT << std::string(Indent-2, ' ') << "=> "; |
Evan Cheng | 9ade218 | 2006-08-26 05:34:46 +0000 | [diff] [blame] | 1588 | DEBUG(ResNode->dump(CurDAG)); |
Bill Wendling | 6345d75 | 2006-11-17 07:52:03 +0000 | [diff] [blame] | 1589 | DOUT << "\n"; |
Evan Cheng | 403be7e | 2006-05-08 08:01:26 +0000 | [diff] [blame] | 1590 | Indent -= 2; |
| 1591 | #endif |
Christopher Lamb | 2dc6dc6 | 2007-07-29 01:24:57 +0000 | [diff] [blame] | 1592 | return ResNode; |
Evan Cheng | 6b2e254 | 2006-05-20 07:44:28 +0000 | [diff] [blame] | 1593 | break; |
Evan Cheng | 403be7e | 2006-05-08 08:01:26 +0000 | [diff] [blame] | 1594 | } |
Chris Lattner | c961eea | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 1595 | } |
| 1596 | |
Evan Cheng | 9ade218 | 2006-08-26 05:34:46 +0000 | [diff] [blame] | 1597 | SDNode *ResNode = SelectCode(N); |
Evan Cheng | 64a752f | 2006-08-11 09:08:15 +0000 | [diff] [blame] | 1598 | |
Evan Cheng | f597dc7 | 2006-02-10 22:24:32 +0000 | [diff] [blame] | 1599 | #ifndef NDEBUG |
Bill Wendling | 6345d75 | 2006-11-17 07:52:03 +0000 | [diff] [blame] | 1600 | DOUT << std::string(Indent-2, ' ') << "=> "; |
Evan Cheng | 9ade218 | 2006-08-26 05:34:46 +0000 | [diff] [blame] | 1601 | if (ResNode == NULL || ResNode == N.Val) |
| 1602 | DEBUG(N.Val->dump(CurDAG)); |
| 1603 | else |
| 1604 | DEBUG(ResNode->dump(CurDAG)); |
Bill Wendling | 6345d75 | 2006-11-17 07:52:03 +0000 | [diff] [blame] | 1605 | DOUT << "\n"; |
Evan Cheng | 23addc0 | 2006-02-10 22:46:26 +0000 | [diff] [blame] | 1606 | Indent -= 2; |
Evan Cheng | f597dc7 | 2006-02-10 22:24:32 +0000 | [diff] [blame] | 1607 | #endif |
Evan Cheng | 64a752f | 2006-08-11 09:08:15 +0000 | [diff] [blame] | 1608 | |
| 1609 | return ResNode; |
Chris Lattner | c961eea | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 1610 | } |
| 1611 | |
Chris Lattner | c0bad57 | 2006-06-08 18:03:49 +0000 | [diff] [blame] | 1612 | bool X86DAGToDAGISel:: |
| 1613 | SelectInlineAsmMemoryOperand(const SDOperand &Op, char ConstraintCode, |
| 1614 | std::vector<SDOperand> &OutOps, SelectionDAG &DAG){ |
| 1615 | SDOperand Op0, Op1, Op2, Op3; |
| 1616 | switch (ConstraintCode) { |
| 1617 | case 'o': // offsetable ?? |
| 1618 | case 'v': // not offsetable ?? |
| 1619 | default: return true; |
| 1620 | case 'm': // memory |
Evan Cheng | 0d53826 | 2006-11-08 20:34:28 +0000 | [diff] [blame] | 1621 | if (!SelectAddr(Op, Op, Op0, Op1, Op2, Op3)) |
Chris Lattner | c0bad57 | 2006-06-08 18:03:49 +0000 | [diff] [blame] | 1622 | return true; |
| 1623 | break; |
| 1624 | } |
| 1625 | |
Evan Cheng | 0469990 | 2006-08-26 01:05:16 +0000 | [diff] [blame] | 1626 | OutOps.push_back(Op0); |
| 1627 | OutOps.push_back(Op1); |
| 1628 | OutOps.push_back(Op2); |
| 1629 | OutOps.push_back(Op3); |
| 1630 | AddToISelQueue(Op0); |
| 1631 | AddToISelQueue(Op1); |
| 1632 | AddToISelQueue(Op2); |
| 1633 | AddToISelQueue(Op3); |
Chris Lattner | c0bad57 | 2006-06-08 18:03:49 +0000 | [diff] [blame] | 1634 | return false; |
| 1635 | } |
| 1636 | |
Chris Lattner | c961eea | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 1637 | /// createX86ISelDag - This pass converts a legalized DAG into a |
| 1638 | /// X86-specific DAG, ready for instruction scheduling. |
| 1639 | /// |
Evan Cheng | e50794a | 2006-08-29 18:28:33 +0000 | [diff] [blame] | 1640 | FunctionPass *llvm::createX86ISelDag(X86TargetMachine &TM, bool Fast) { |
| 1641 | return new X86DAGToDAGISel(TM, Fast); |
Chris Lattner | c961eea | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 1642 | } |