Chris Lattner | 5930d3d | 2005-11-16 22:59:19 +0000 | [diff] [blame] | 1 | //===- X86ISelDAGToDAG.cpp - A DAG pattern matching inst selector for X86 -===// |
Chris Lattner | 655e7df | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by the Evan Cheng and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file defines a DAG pattern matching instruction selector for X86, |
| 11 | // converting from a legalized dag to a X86 dag. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Evan Cheng | d49cc36 | 2006-02-10 22:24:32 +0000 | [diff] [blame] | 15 | #define DEBUG_TYPE "isel" |
Chris Lattner | 655e7df | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 16 | #include "X86.h" |
Evan Cheng | bc7a0f44 | 2006-01-11 06:09:51 +0000 | [diff] [blame] | 17 | #include "X86InstrBuilder.h" |
Evan Cheng | 2dd2c65 | 2006-03-13 23:20:37 +0000 | [diff] [blame] | 18 | #include "X86ISelLowering.h" |
Chris Lattner | 7c55126 | 2006-01-11 01:15:34 +0000 | [diff] [blame] | 19 | #include "X86RegisterInfo.h" |
Chris Lattner | 655e7df | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 20 | #include "X86Subtarget.h" |
Evan Cheng | 2dd2c65 | 2006-03-13 23:20:37 +0000 | [diff] [blame] | 21 | #include "X86TargetMachine.h" |
Chris Lattner | 3f0f71b | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 22 | #include "llvm/GlobalValue.h" |
Chris Lattner | 7c55126 | 2006-01-11 01:15:34 +0000 | [diff] [blame] | 23 | #include "llvm/Instructions.h" |
Chris Lattner | 5d70a7c | 2006-03-25 06:47:10 +0000 | [diff] [blame] | 24 | #include "llvm/Intrinsics.h" |
Chris Lattner | 7c55126 | 2006-01-11 01:15:34 +0000 | [diff] [blame] | 25 | #include "llvm/Support/CFG.h" |
Chris Lattner | 3f0f71b | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 26 | #include "llvm/CodeGen/MachineConstantPool.h" |
Chris Lattner | 655e7df | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 27 | #include "llvm/CodeGen/MachineFunction.h" |
Evan Cheng | 73a1ad9 | 2006-01-10 20:26:56 +0000 | [diff] [blame] | 28 | #include "llvm/CodeGen/MachineFrameInfo.h" |
Chris Lattner | 7c55126 | 2006-01-11 01:15:34 +0000 | [diff] [blame] | 29 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
| 30 | #include "llvm/CodeGen/SSARegMap.h" |
Chris Lattner | 655e7df | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 31 | #include "llvm/CodeGen/SelectionDAGISel.h" |
| 32 | #include "llvm/Target/TargetMachine.h" |
| 33 | #include "llvm/Support/Debug.h" |
Chris Lattner | 0cc5907 | 2006-06-28 23:27:49 +0000 | [diff] [blame] | 34 | #include "llvm/Support/Visibility.h" |
Chris Lattner | 655e7df | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 35 | #include "llvm/ADT/Statistic.h" |
Evan Cheng | 2e94538 | 2006-07-28 06:05:06 +0000 | [diff] [blame] | 36 | #include <deque> |
Chris Lattner | de02d77 | 2006-01-22 23:41:00 +0000 | [diff] [blame] | 37 | #include <iostream> |
Evan Cheng | 54cb183 | 2006-02-05 06:46:41 +0000 | [diff] [blame] | 38 | #include <set> |
Chris Lattner | 655e7df | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 39 | using namespace llvm; |
| 40 | |
| 41 | //===----------------------------------------------------------------------===// |
| 42 | // Pattern Matcher Implementation |
| 43 | //===----------------------------------------------------------------------===// |
| 44 | |
| 45 | namespace { |
Chris Lattner | 3f0f71b | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 46 | /// X86ISelAddressMode - This corresponds to X86AddressMode, but uses |
| 47 | /// SDOperand's instead of register numbers for the leaves of the matched |
| 48 | /// tree. |
| 49 | struct X86ISelAddressMode { |
| 50 | enum { |
| 51 | RegBase, |
Chris Lattner | aa237256 | 2006-05-24 17:04:05 +0000 | [diff] [blame] | 52 | FrameIndexBase |
Chris Lattner | 3f0f71b | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 53 | } BaseType; |
| 54 | |
| 55 | struct { // This is really a union, discriminated by BaseType! |
| 56 | SDOperand Reg; |
| 57 | int FrameIndex; |
| 58 | } Base; |
| 59 | |
| 60 | unsigned Scale; |
| 61 | SDOperand IndexReg; |
| 62 | unsigned Disp; |
| 63 | GlobalValue *GV; |
Evan Cheng | 77d86ff | 2006-02-25 10:09:08 +0000 | [diff] [blame] | 64 | Constant *CP; |
| 65 | unsigned Align; // CP alignment. |
Chris Lattner | 3f0f71b | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 66 | |
| 67 | X86ISelAddressMode() |
Evan Cheng | 77d86ff | 2006-02-25 10:09:08 +0000 | [diff] [blame] | 68 | : BaseType(RegBase), Scale(1), IndexReg(), Disp(0), GV(0), |
| 69 | CP(0), Align(0) { |
Chris Lattner | 3f0f71b | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 70 | } |
| 71 | }; |
| 72 | } |
| 73 | |
| 74 | namespace { |
Chris Lattner | 655e7df | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 75 | Statistic<> |
| 76 | NumFPKill("x86-codegen", "Number of FP_REG_KILL instructions added"); |
| 77 | |
| 78 | //===--------------------------------------------------------------------===// |
| 79 | /// ISel - X86 specific code to select X86 machine instructions for |
| 80 | /// SelectionDAG operations. |
| 81 | /// |
Chris Lattner | 0cc5907 | 2006-06-28 23:27:49 +0000 | [diff] [blame] | 82 | class VISIBILITY_HIDDEN X86DAGToDAGISel : public SelectionDAGISel { |
Chris Lattner | 655e7df | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 83 | /// ContainsFPCode - Every instruction we select that uses or defines a FP |
| 84 | /// register should set this to true. |
| 85 | bool ContainsFPCode; |
| 86 | |
| 87 | /// X86Lowering - This object fully describes how to lower LLVM code to an |
| 88 | /// X86-specific SelectionDAG. |
| 89 | X86TargetLowering X86Lowering; |
| 90 | |
| 91 | /// Subtarget - Keep a pointer to the X86Subtarget around so that we can |
| 92 | /// make the right decision when generating code for different targets. |
| 93 | const X86Subtarget *Subtarget; |
Evan Cheng | 5588de9 | 2006-02-18 00:15:05 +0000 | [diff] [blame] | 94 | |
| 95 | unsigned GlobalBaseReg; |
Evan Cheng | 691a63d | 2006-07-27 16:44:36 +0000 | [diff] [blame] | 96 | |
Chris Lattner | 655e7df | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 97 | public: |
Evan Cheng | 2dd2c65 | 2006-03-13 23:20:37 +0000 | [diff] [blame] | 98 | X86DAGToDAGISel(X86TargetMachine &TM) |
| 99 | : SelectionDAGISel(X86Lowering), |
Evan Cheng | 691a63d | 2006-07-27 16:44:36 +0000 | [diff] [blame] | 100 | X86Lowering(*TM.getTargetLowering()), |
| 101 | Subtarget(&TM.getSubtarget<X86Subtarget>()), |
Evan Cheng | 8101dd6 | 2006-08-02 09:18:33 +0000 | [diff] [blame] | 102 | DAGSize(0), ReachabilityMatrix(NULL), ReachMatrixRange(NULL) {} |
Chris Lattner | 655e7df | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 103 | |
Evan Cheng | 5588de9 | 2006-02-18 00:15:05 +0000 | [diff] [blame] | 104 | virtual bool runOnFunction(Function &Fn) { |
| 105 | // Make sure we re-emit a set of the global base reg if necessary |
| 106 | GlobalBaseReg = 0; |
| 107 | return SelectionDAGISel::runOnFunction(Fn); |
| 108 | } |
| 109 | |
Chris Lattner | 655e7df | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 110 | virtual const char *getPassName() const { |
| 111 | return "X86 DAG->DAG Instruction Selection"; |
| 112 | } |
| 113 | |
| 114 | /// InstructionSelectBasicBlock - This callback is invoked by |
| 115 | /// SelectionDAGISel when it has created a SelectionDAG for us to codegen. |
| 116 | virtual void InstructionSelectBasicBlock(SelectionDAG &DAG); |
| 117 | |
Evan Cheng | bc7a0f44 | 2006-01-11 06:09:51 +0000 | [diff] [blame] | 118 | virtual void EmitFunctionEntryCode(Function &Fn, MachineFunction &MF); |
| 119 | |
Evan Cheng | e2a3f70 | 2006-07-28 01:03:48 +0000 | [diff] [blame] | 120 | virtual bool CanBeFoldedBy(SDNode *N, SDNode *U); |
Evan Cheng | 691a63d | 2006-07-27 16:44:36 +0000 | [diff] [blame] | 121 | |
Chris Lattner | 655e7df | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 122 | // Include the pieces autogenerated from the target description. |
| 123 | #include "X86GenDAGISel.inc" |
| 124 | |
| 125 | private: |
Evan Cheng | e8071ec | 2006-07-28 06:33:41 +0000 | [diff] [blame] | 126 | void DetermineReachability(SDNode *f, SDNode *t); |
Evan Cheng | 691a63d | 2006-07-27 16:44:36 +0000 | [diff] [blame] | 127 | |
Evan Cheng | 6dc90ca | 2006-02-09 00:37:58 +0000 | [diff] [blame] | 128 | void Select(SDOperand &Result, SDOperand N); |
Chris Lattner | 655e7df | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 129 | |
Evan Cheng | a86ba85 | 2006-02-11 02:05:36 +0000 | [diff] [blame] | 130 | bool MatchAddress(SDOperand N, X86ISelAddressMode &AM, bool isRoot = true); |
Evan Cheng | c9fab31 | 2005-12-08 02:01:35 +0000 | [diff] [blame] | 131 | bool SelectAddr(SDOperand N, SDOperand &Base, SDOperand &Scale, |
| 132 | SDOperand &Index, SDOperand &Disp); |
| 133 | bool SelectLEAAddr(SDOperand N, SDOperand &Base, SDOperand &Scale, |
| 134 | SDOperand &Index, SDOperand &Disp); |
Evan Cheng | d5f2ba0 | 2006-02-06 06:02:33 +0000 | [diff] [blame] | 135 | bool TryFoldLoad(SDOperand P, SDOperand N, |
| 136 | SDOperand &Base, SDOperand &Scale, |
Evan Cheng | 10d2790 | 2006-01-06 20:36:21 +0000 | [diff] [blame] | 137 | SDOperand &Index, SDOperand &Disp); |
Chris Lattner | ba1ed58 | 2006-06-08 18:03:49 +0000 | [diff] [blame] | 138 | /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for |
| 139 | /// inline asm expressions. |
| 140 | virtual bool SelectInlineAsmMemoryOperand(const SDOperand &Op, |
| 141 | char ConstraintCode, |
| 142 | std::vector<SDOperand> &OutOps, |
| 143 | SelectionDAG &DAG); |
| 144 | |
Evan Cheng | e8a4236 | 2006-06-02 22:38:37 +0000 | [diff] [blame] | 145 | void EmitSpecialCodeForMain(MachineBasicBlock *BB, MachineFrameInfo *MFI); |
| 146 | |
Evan Cheng | 67ed58e | 2005-12-12 21:49:40 +0000 | [diff] [blame] | 147 | inline void getAddressOperands(X86ISelAddressMode &AM, SDOperand &Base, |
| 148 | SDOperand &Scale, SDOperand &Index, |
| 149 | SDOperand &Disp) { |
| 150 | Base = (AM.BaseType == X86ISelAddressMode::FrameIndexBase) ? |
| 151 | CurDAG->getTargetFrameIndex(AM.Base.FrameIndex, MVT::i32) : AM.Base.Reg; |
Evan Cheng | 1d71248 | 2005-12-17 09:13:43 +0000 | [diff] [blame] | 152 | Scale = getI8Imm(AM.Scale); |
Evan Cheng | 67ed58e | 2005-12-12 21:49:40 +0000 | [diff] [blame] | 153 | Index = AM.IndexReg; |
| 154 | Disp = AM.GV ? CurDAG->getTargetGlobalAddress(AM.GV, MVT::i32, AM.Disp) |
Evan Cheng | 77d86ff | 2006-02-25 10:09:08 +0000 | [diff] [blame] | 155 | : (AM.CP ? |
| 156 | CurDAG->getTargetConstantPool(AM.CP, MVT::i32, AM.Align, AM.Disp) |
| 157 | : getI32Imm(AM.Disp)); |
Evan Cheng | 67ed58e | 2005-12-12 21:49:40 +0000 | [diff] [blame] | 158 | } |
| 159 | |
Chris Lattner | 3f0f71b | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 160 | /// getI8Imm - Return a target constant with the specified value, of type |
| 161 | /// i8. |
| 162 | inline SDOperand getI8Imm(unsigned Imm) { |
| 163 | return CurDAG->getTargetConstant(Imm, MVT::i8); |
| 164 | } |
| 165 | |
Chris Lattner | 655e7df | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 166 | /// getI16Imm - Return a target constant with the specified value, of type |
| 167 | /// i16. |
| 168 | inline SDOperand getI16Imm(unsigned Imm) { |
| 169 | return CurDAG->getTargetConstant(Imm, MVT::i16); |
| 170 | } |
| 171 | |
| 172 | /// getI32Imm - Return a target constant with the specified value, of type |
| 173 | /// i32. |
| 174 | inline SDOperand getI32Imm(unsigned Imm) { |
| 175 | return CurDAG->getTargetConstant(Imm, MVT::i32); |
| 176 | } |
Evan Cheng | d49cc36 | 2006-02-10 22:24:32 +0000 | [diff] [blame] | 177 | |
Evan Cheng | 5588de9 | 2006-02-18 00:15:05 +0000 | [diff] [blame] | 178 | /// getGlobalBaseReg - insert code into the entry mbb to materialize the PIC |
| 179 | /// base register. Return the virtual register that holds this value. |
| 180 | SDOperand getGlobalBaseReg(); |
| 181 | |
Evan Cheng | 691a63d | 2006-07-27 16:44:36 +0000 | [diff] [blame] | 182 | /// DAGSize - Number of nodes in the DAG. |
| 183 | /// |
| 184 | unsigned DAGSize; |
| 185 | |
| 186 | /// TopOrder - Topological ordering of all nodes in the DAG. |
| 187 | /// |
Evan Cheng | 45af287 | 2006-08-01 08:17:22 +0000 | [diff] [blame] | 188 | std::vector<SDNode*> TopOrder; |
Evan Cheng | 8758576 | 2006-07-27 22:10:00 +0000 | [diff] [blame] | 189 | |
Evan Cheng | 45af287 | 2006-08-01 08:17:22 +0000 | [diff] [blame] | 190 | /// ReachabilityMatrix - A N x N matrix representing all pairs reachability |
Evan Cheng | 691a63d | 2006-07-27 16:44:36 +0000 | [diff] [blame] | 191 | /// information. One bit per potential edge. |
Evan Cheng | 8101dd6 | 2006-08-02 09:18:33 +0000 | [diff] [blame] | 192 | unsigned char *ReachabilityMatrix; |
Evan Cheng | 45af287 | 2006-08-01 08:17:22 +0000 | [diff] [blame] | 193 | |
Evan Cheng | 8101dd6 | 2006-08-02 09:18:33 +0000 | [diff] [blame] | 194 | /// ReachMatrixRange - The range of reachability information available for |
| 195 | /// the particular source node. |
| 196 | unsigned *ReachMatrixRange; |
Evan Cheng | 691a63d | 2006-07-27 16:44:36 +0000 | [diff] [blame] | 197 | |
| 198 | inline void setReachable(SDNode *f, SDNode *t) { |
| 199 | unsigned Idx = f->getNodeId() * DAGSize + t->getNodeId(); |
Evan Cheng | 8101dd6 | 2006-08-02 09:18:33 +0000 | [diff] [blame] | 200 | ReachabilityMatrix[Idx / 8] |= 1 << (Idx % 8); |
Evan Cheng | 691a63d | 2006-07-27 16:44:36 +0000 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | inline bool isReachable(SDNode *f, SDNode *t) { |
| 204 | unsigned Idx = f->getNodeId() * DAGSize + t->getNodeId(); |
Evan Cheng | 8101dd6 | 2006-08-02 09:18:33 +0000 | [diff] [blame] | 205 | return ReachabilityMatrix[Idx / 8] & (1 << (Idx % 8)); |
Evan Cheng | 691a63d | 2006-07-27 16:44:36 +0000 | [diff] [blame] | 206 | } |
| 207 | |
Evan Cheng | 11a4d8c | 2006-07-28 00:49:31 +0000 | [diff] [blame] | 208 | /// UnfoldableSet - An boolean array representing nodes which have been |
| 209 | /// folded into addressing modes and therefore should not be folded in |
| 210 | /// another operation. |
Evan Cheng | 8101dd6 | 2006-08-02 09:18:33 +0000 | [diff] [blame] | 211 | unsigned char *UnfoldableSet; |
Evan Cheng | 11a4d8c | 2006-07-28 00:49:31 +0000 | [diff] [blame] | 212 | |
| 213 | inline void setUnfoldable(SDNode *N) { |
Evan Cheng | 8101dd6 | 2006-08-02 09:18:33 +0000 | [diff] [blame] | 214 | unsigned Id = N->getNodeId(); |
| 215 | UnfoldableSet[Id / 8] |= 1 << (Id % 8); |
Evan Cheng | 11a4d8c | 2006-07-28 00:49:31 +0000 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | inline bool isUnfoldable(SDNode *N) { |
Evan Cheng | 8101dd6 | 2006-08-02 09:18:33 +0000 | [diff] [blame] | 219 | unsigned Id = N->getNodeId(); |
| 220 | return UnfoldableSet[Id / 8] & (1 << (Id % 8)); |
Evan Cheng | 11a4d8c | 2006-07-28 00:49:31 +0000 | [diff] [blame] | 221 | } |
| 222 | |
Evan Cheng | 2b6f78b | 2006-02-10 22:46:26 +0000 | [diff] [blame] | 223 | #ifndef NDEBUG |
| 224 | unsigned Indent; |
| 225 | #endif |
Chris Lattner | 655e7df | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 226 | }; |
| 227 | } |
| 228 | |
Evan Cheng | e2a3f70 | 2006-07-28 01:03:48 +0000 | [diff] [blame] | 229 | bool X86DAGToDAGISel::CanBeFoldedBy(SDNode *N, SDNode *U) { |
Evan Cheng | 11a4d8c | 2006-07-28 00:49:31 +0000 | [diff] [blame] | 230 | // Is it already folded by SelectAddr / SelectLEAAddr? |
| 231 | if (isUnfoldable(N)) |
| 232 | return false; |
| 233 | |
Evan Cheng | 691a63d | 2006-07-27 16:44:36 +0000 | [diff] [blame] | 234 | // If U use can somehow reach N through another path then U can't fold N or |
| 235 | // it will create a cycle. e.g. In the following diagram, U can reach N |
Evan Cheng | e8071ec | 2006-07-28 06:33:41 +0000 | [diff] [blame] | 236 | // through X. If N is folded into into U, then X is both a predecessor and |
Evan Cheng | 691a63d | 2006-07-27 16:44:36 +0000 | [diff] [blame] | 237 | // a successor of U. |
| 238 | // |
| 239 | // [ N ] |
| 240 | // ^ ^ |
| 241 | // | | |
| 242 | // / \--- |
| 243 | // / [X] |
| 244 | // | ^ |
| 245 | // [U]--------| |
Evan Cheng | e8071ec | 2006-07-28 06:33:41 +0000 | [diff] [blame] | 246 | DetermineReachability(U, N); |
Evan Cheng | 691a63d | 2006-07-27 16:44:36 +0000 | [diff] [blame] | 247 | assert(isReachable(U, N) && "Attempting to fold a non-operand node?"); |
| 248 | for (SDNode::op_iterator I = U->op_begin(), E = U->op_end(); I != E; ++I) { |
| 249 | SDNode *P = I->Val; |
| 250 | if (P != N && isReachable(P, N)) |
| 251 | return false; |
| 252 | } |
| 253 | return true; |
| 254 | } |
| 255 | |
Evan Cheng | 45af287 | 2006-08-01 08:17:22 +0000 | [diff] [blame] | 256 | /// DetermineReachability - Determine reachability between all pairs of nodes |
Evan Cheng | 2e94538 | 2006-07-28 06:05:06 +0000 | [diff] [blame] | 257 | /// between f and t in topological order. |
Evan Cheng | e8071ec | 2006-07-28 06:33:41 +0000 | [diff] [blame] | 258 | void X86DAGToDAGISel::DetermineReachability(SDNode *f, SDNode *t) { |
Evan Cheng | 45af287 | 2006-08-01 08:17:22 +0000 | [diff] [blame] | 259 | unsigned Orderf = f->getNodeId(); |
| 260 | unsigned Ordert = t->getNodeId(); |
| 261 | unsigned Range = ReachMatrixRange[Orderf]; |
Evan Cheng | 8758576 | 2006-07-27 22:10:00 +0000 | [diff] [blame] | 262 | if (Range >= Ordert) |
| 263 | return; |
| 264 | if (Range < Orderf) |
| 265 | Range = Orderf; |
| 266 | |
| 267 | for (unsigned i = Range; i < Ordert; ++i) { |
Evan Cheng | 691a63d | 2006-07-27 16:44:36 +0000 | [diff] [blame] | 268 | SDNode *N = TopOrder[i]; |
| 269 | setReachable(N, N); |
| 270 | // If N is a leaf node, there is nothing more to do. |
| 271 | if (N->getNumOperands() == 0) |
| 272 | continue; |
| 273 | |
Evan Cheng | 8f58519 | 2006-08-02 22:01:32 +0000 | [diff] [blame] | 274 | for (unsigned i2 = Range; ; ++i2) { |
Evan Cheng | 691a63d | 2006-07-27 16:44:36 +0000 | [diff] [blame] | 275 | SDNode *M = TopOrder[i2]; |
| 276 | if (isReachable(M, N)) { |
Evan Cheng | 45af287 | 2006-08-01 08:17:22 +0000 | [diff] [blame] | 277 | // Update reachability from M to N's operands. |
Evan Cheng | 8f58519 | 2006-08-02 22:01:32 +0000 | [diff] [blame] | 278 | for (SDNode::op_iterator I = N->op_begin(),E = N->op_end(); I != E;++I){ |
| 279 | SDNode *P = I->Val; |
| 280 | if (P->getNodeId() >= 0) |
| 281 | setReachable(M, P); |
| 282 | } |
Evan Cheng | 691a63d | 2006-07-27 16:44:36 +0000 | [diff] [blame] | 283 | } |
| 284 | if (M == N) break; |
| 285 | } |
| 286 | } |
Evan Cheng | 8758576 | 2006-07-27 22:10:00 +0000 | [diff] [blame] | 287 | |
Evan Cheng | 45af287 | 2006-08-01 08:17:22 +0000 | [diff] [blame] | 288 | ReachMatrixRange[Orderf] = Ordert; |
Evan Cheng | 691a63d | 2006-07-27 16:44:36 +0000 | [diff] [blame] | 289 | } |
| 290 | |
Chris Lattner | 655e7df | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 291 | /// InstructionSelectBasicBlock - This callback is invoked by SelectionDAGISel |
| 292 | /// when it has created a SelectionDAG for us to codegen. |
| 293 | void X86DAGToDAGISel::InstructionSelectBasicBlock(SelectionDAG &DAG) { |
| 294 | DEBUG(BB->dump()); |
Chris Lattner | 7c55126 | 2006-01-11 01:15:34 +0000 | [diff] [blame] | 295 | MachineFunction::iterator FirstMBB = BB; |
Chris Lattner | 655e7df | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 296 | |
Evan Cheng | 8f58519 | 2006-08-02 22:01:32 +0000 | [diff] [blame] | 297 | DAGSize = DAG.AssignTopologicalOrder(TopOrder); |
Evan Cheng | 8101dd6 | 2006-08-02 09:18:33 +0000 | [diff] [blame] | 298 | unsigned RMSize = (DAGSize * DAGSize + 7) / 8; |
| 299 | ReachabilityMatrix = new unsigned char[RMSize]; |
| 300 | memset(ReachabilityMatrix, 0, RMSize); |
| 301 | ReachMatrixRange = new unsigned[DAGSize]; |
| 302 | memset(ReachMatrixRange, 0, DAGSize * sizeof(unsigned)); |
| 303 | unsigned NumBytes = (DAGSize + 7) / 8; |
| 304 | UnfoldableSet = new unsigned char[NumBytes]; |
| 305 | memset(UnfoldableSet, 0, NumBytes); |
Evan Cheng | 3b5e0ca | 2006-07-28 00:10:59 +0000 | [diff] [blame] | 306 | |
Chris Lattner | 655e7df | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 307 | // Codegen the basic block. |
Evan Cheng | d49cc36 | 2006-02-10 22:24:32 +0000 | [diff] [blame] | 308 | #ifndef NDEBUG |
| 309 | DEBUG(std::cerr << "===== Instruction selection begins:\n"); |
Evan Cheng | 2b6f78b | 2006-02-10 22:46:26 +0000 | [diff] [blame] | 310 | Indent = 0; |
Evan Cheng | d49cc36 | 2006-02-10 22:24:32 +0000 | [diff] [blame] | 311 | #endif |
Evan Cheng | 54cb183 | 2006-02-05 06:46:41 +0000 | [diff] [blame] | 312 | DAG.setRoot(SelectRoot(DAG.getRoot())); |
Evan Cheng | d49cc36 | 2006-02-10 22:24:32 +0000 | [diff] [blame] | 313 | #ifndef NDEBUG |
| 314 | DEBUG(std::cerr << "===== Instruction selection ends:\n"); |
| 315 | #endif |
Evan Cheng | 3b5e0ca | 2006-07-28 00:10:59 +0000 | [diff] [blame] | 316 | |
Evan Cheng | 8101dd6 | 2006-08-02 09:18:33 +0000 | [diff] [blame] | 317 | delete[] ReachabilityMatrix; |
| 318 | delete[] ReachMatrixRange; |
| 319 | delete[] UnfoldableSet; |
| 320 | ReachabilityMatrix = NULL; |
| 321 | ReachMatrixRange = NULL; |
| 322 | UnfoldableSet = NULL; |
Evan Cheng | 8f58519 | 2006-08-02 22:01:32 +0000 | [diff] [blame] | 323 | TopOrder.clear(); |
Evan Cheng | 1d9b671 | 2005-12-19 22:36:02 +0000 | [diff] [blame] | 324 | CodeGenMap.clear(); |
Evan Cheng | 1a8e74d | 2006-05-24 20:46:25 +0000 | [diff] [blame] | 325 | HandleMap.clear(); |
| 326 | ReplaceMap.clear(); |
Chris Lattner | 655e7df | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 327 | DAG.RemoveDeadNodes(); |
| 328 | |
| 329 | // Emit machine code to BB. |
| 330 | ScheduleAndEmitDAG(DAG); |
Chris Lattner | 7c55126 | 2006-01-11 01:15:34 +0000 | [diff] [blame] | 331 | |
| 332 | // If we are emitting FP stack code, scan the basic block to determine if this |
| 333 | // block defines any FP values. If so, put an FP_REG_KILL instruction before |
| 334 | // the terminator of the block. |
Evan Cheng | cde9e30 | 2006-01-27 08:10:46 +0000 | [diff] [blame] | 335 | if (!Subtarget->hasSSE2()) { |
Chris Lattner | 7c55126 | 2006-01-11 01:15:34 +0000 | [diff] [blame] | 336 | // Note that FP stack instructions *are* used in SSE code when returning |
| 337 | // values, but these are not live out of the basic block, so we don't need |
| 338 | // an FP_REG_KILL in this case either. |
| 339 | bool ContainsFPCode = false; |
| 340 | |
| 341 | // Scan all of the machine instructions in these MBBs, checking for FP |
| 342 | // stores. |
| 343 | MachineFunction::iterator MBBI = FirstMBB; |
| 344 | do { |
| 345 | for (MachineBasicBlock::iterator I = MBBI->begin(), E = MBBI->end(); |
| 346 | !ContainsFPCode && I != E; ++I) { |
| 347 | for (unsigned op = 0, e = I->getNumOperands(); op != e; ++op) { |
| 348 | if (I->getOperand(op).isRegister() && I->getOperand(op).isDef() && |
| 349 | MRegisterInfo::isVirtualRegister(I->getOperand(op).getReg()) && |
| 350 | RegMap->getRegClass(I->getOperand(0).getReg()) == |
| 351 | X86::RFPRegisterClass) { |
| 352 | ContainsFPCode = true; |
| 353 | break; |
| 354 | } |
| 355 | } |
| 356 | } |
| 357 | } while (!ContainsFPCode && &*(MBBI++) != BB); |
| 358 | |
| 359 | // Check PHI nodes in successor blocks. These PHI's will be lowered to have |
| 360 | // a copy of the input value in this block. |
| 361 | if (!ContainsFPCode) { |
| 362 | // Final check, check LLVM BB's that are successors to the LLVM BB |
| 363 | // corresponding to BB for FP PHI nodes. |
| 364 | const BasicBlock *LLVMBB = BB->getBasicBlock(); |
| 365 | const PHINode *PN; |
| 366 | for (succ_const_iterator SI = succ_begin(LLVMBB), E = succ_end(LLVMBB); |
| 367 | !ContainsFPCode && SI != E; ++SI) { |
| 368 | for (BasicBlock::const_iterator II = SI->begin(); |
| 369 | (PN = dyn_cast<PHINode>(II)); ++II) { |
| 370 | if (PN->getType()->isFloatingPoint()) { |
| 371 | ContainsFPCode = true; |
| 372 | break; |
| 373 | } |
| 374 | } |
| 375 | } |
| 376 | } |
| 377 | |
| 378 | // Finally, if we found any FP code, emit the FP_REG_KILL instruction. |
| 379 | if (ContainsFPCode) { |
| 380 | BuildMI(*BB, BB->getFirstTerminator(), X86::FP_REG_KILL, 0); |
| 381 | ++NumFPKill; |
| 382 | } |
| 383 | } |
Chris Lattner | 655e7df | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 384 | } |
| 385 | |
Evan Cheng | bc7a0f44 | 2006-01-11 06:09:51 +0000 | [diff] [blame] | 386 | /// EmitSpecialCodeForMain - Emit any code that needs to be executed only in |
| 387 | /// the main function. |
Evan Cheng | e8a4236 | 2006-06-02 22:38:37 +0000 | [diff] [blame] | 388 | void X86DAGToDAGISel::EmitSpecialCodeForMain(MachineBasicBlock *BB, |
| 389 | MachineFrameInfo *MFI) { |
| 390 | if (Subtarget->TargetType == X86Subtarget::isCygwin) |
| 391 | BuildMI(BB, X86::CALLpcrel32, 1).addExternalSymbol("__main"); |
| 392 | |
Evan Cheng | bc7a0f44 | 2006-01-11 06:09:51 +0000 | [diff] [blame] | 393 | // Switch the FPU to 64-bit precision mode for better compatibility and speed. |
| 394 | int CWFrameIdx = MFI->CreateStackObject(2, 2); |
| 395 | addFrameReference(BuildMI(BB, X86::FNSTCW16m, 4), CWFrameIdx); |
| 396 | |
| 397 | // Set the high part to be 64-bit precision. |
| 398 | addFrameReference(BuildMI(BB, X86::MOV8mi, 5), |
| 399 | CWFrameIdx, 1).addImm(2); |
| 400 | |
| 401 | // Reload the modified control word now. |
| 402 | addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx); |
| 403 | } |
| 404 | |
| 405 | void X86DAGToDAGISel::EmitFunctionEntryCode(Function &Fn, MachineFunction &MF) { |
| 406 | // If this is main, emit special code for main. |
| 407 | MachineBasicBlock *BB = MF.begin(); |
| 408 | if (Fn.hasExternalLinkage() && Fn.getName() == "main") |
| 409 | EmitSpecialCodeForMain(BB, MF.getFrameInfo()); |
| 410 | } |
| 411 | |
Chris Lattner | 3f0f71b | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 412 | /// MatchAddress - Add the specified node to the specified addressing mode, |
| 413 | /// returning true if it cannot be done. This just pattern matches for the |
| 414 | /// addressing mode |
Evan Cheng | a86ba85 | 2006-02-11 02:05:36 +0000 | [diff] [blame] | 415 | bool X86DAGToDAGISel::MatchAddress(SDOperand N, X86ISelAddressMode &AM, |
| 416 | bool isRoot) { |
Evan Cheng | 77d86ff | 2006-02-25 10:09:08 +0000 | [diff] [blame] | 417 | bool Available = false; |
| 418 | // If N has already been selected, reuse the result unless in some very |
| 419 | // specific cases. |
Evan Cheng | a86ba85 | 2006-02-11 02:05:36 +0000 | [diff] [blame] | 420 | std::map<SDOperand, SDOperand>::iterator CGMI= CodeGenMap.find(N.getValue(0)); |
| 421 | if (CGMI != CodeGenMap.end()) { |
Evan Cheng | 77d86ff | 2006-02-25 10:09:08 +0000 | [diff] [blame] | 422 | Available = true; |
Evan Cheng | a86ba85 | 2006-02-11 02:05:36 +0000 | [diff] [blame] | 423 | } |
| 424 | |
Chris Lattner | 3f0f71b | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 425 | switch (N.getOpcode()) { |
| 426 | default: break; |
Evan Cheng | 77d86ff | 2006-02-25 10:09:08 +0000 | [diff] [blame] | 427 | case ISD::Constant: |
| 428 | AM.Disp += cast<ConstantSDNode>(N)->getValue(); |
| 429 | return false; |
| 430 | |
| 431 | case X86ISD::Wrapper: |
| 432 | // If both base and index components have been picked, we can't fit |
| 433 | // the result available in the register in the addressing mode. Duplicate |
| 434 | // GlobalAddress or ConstantPool as displacement. |
| 435 | if (!Available || (AM.Base.Reg.Val && AM.IndexReg.Val)) { |
| 436 | if (ConstantPoolSDNode *CP = |
| 437 | dyn_cast<ConstantPoolSDNode>(N.getOperand(0))) { |
| 438 | if (AM.CP == 0) { |
| 439 | AM.CP = CP->get(); |
| 440 | AM.Align = CP->getAlignment(); |
| 441 | AM.Disp += CP->getOffset(); |
| 442 | return false; |
| 443 | } |
| 444 | } else if (GlobalAddressSDNode *G = |
| 445 | dyn_cast<GlobalAddressSDNode>(N.getOperand(0))) { |
| 446 | if (AM.GV == 0) { |
| 447 | AM.GV = G->getGlobal(); |
| 448 | AM.Disp += G->getOffset(); |
| 449 | return false; |
| 450 | } |
| 451 | } |
| 452 | } |
| 453 | break; |
| 454 | |
Chris Lattner | 3f0f71b | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 455 | case ISD::FrameIndex: |
| 456 | if (AM.BaseType == X86ISelAddressMode::RegBase && AM.Base.Reg.Val == 0) { |
| 457 | AM.BaseType = X86ISelAddressMode::FrameIndexBase; |
| 458 | AM.Base.FrameIndex = cast<FrameIndexSDNode>(N)->getIndex(); |
| 459 | return false; |
| 460 | } |
| 461 | break; |
Evan Cheng | c9fab31 | 2005-12-08 02:01:35 +0000 | [diff] [blame] | 462 | |
Chris Lattner | 3f0f71b | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 463 | case ISD::SHL: |
Evan Cheng | 77d86ff | 2006-02-25 10:09:08 +0000 | [diff] [blame] | 464 | if (!Available && AM.IndexReg.Val == 0 && AM.Scale == 1) |
Chris Lattner | 3f0f71b | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 465 | if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1))) { |
| 466 | unsigned Val = CN->getValue(); |
| 467 | if (Val == 1 || Val == 2 || Val == 3) { |
| 468 | AM.Scale = 1 << Val; |
| 469 | SDOperand ShVal = N.Val->getOperand(0); |
| 470 | |
| 471 | // Okay, we know that we have a scale by now. However, if the scaled |
| 472 | // value is an add of something and a constant, we can fold the |
| 473 | // constant into the disp field here. |
| 474 | if (ShVal.Val->getOpcode() == ISD::ADD && ShVal.hasOneUse() && |
| 475 | isa<ConstantSDNode>(ShVal.Val->getOperand(1))) { |
| 476 | AM.IndexReg = ShVal.Val->getOperand(0); |
| 477 | ConstantSDNode *AddVal = |
| 478 | cast<ConstantSDNode>(ShVal.Val->getOperand(1)); |
| 479 | AM.Disp += AddVal->getValue() << Val; |
| 480 | } else { |
| 481 | AM.IndexReg = ShVal; |
| 482 | } |
| 483 | return false; |
| 484 | } |
| 485 | } |
| 486 | break; |
Evan Cheng | c9fab31 | 2005-12-08 02:01:35 +0000 | [diff] [blame] | 487 | |
Chris Lattner | 3f0f71b | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 488 | case ISD::MUL: |
| 489 | // X*[3,5,9] -> X+X*[2,4,8] |
Evan Cheng | 77d86ff | 2006-02-25 10:09:08 +0000 | [diff] [blame] | 490 | if (!Available && |
| 491 | AM.BaseType == X86ISelAddressMode::RegBase && |
| 492 | AM.Base.Reg.Val == 0 && |
| 493 | AM.IndexReg.Val == 0) |
Chris Lattner | 3f0f71b | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 494 | if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1))) |
| 495 | if (CN->getValue() == 3 || CN->getValue() == 5 || CN->getValue() == 9) { |
| 496 | AM.Scale = unsigned(CN->getValue())-1; |
| 497 | |
| 498 | SDOperand MulVal = N.Val->getOperand(0); |
| 499 | SDOperand Reg; |
| 500 | |
| 501 | // Okay, we know that we have a scale by now. However, if the scaled |
| 502 | // value is an add of something and a constant, we can fold the |
| 503 | // constant into the disp field here. |
| 504 | if (MulVal.Val->getOpcode() == ISD::ADD && MulVal.hasOneUse() && |
| 505 | isa<ConstantSDNode>(MulVal.Val->getOperand(1))) { |
| 506 | Reg = MulVal.Val->getOperand(0); |
| 507 | ConstantSDNode *AddVal = |
| 508 | cast<ConstantSDNode>(MulVal.Val->getOperand(1)); |
| 509 | AM.Disp += AddVal->getValue() * CN->getValue(); |
| 510 | } else { |
| 511 | Reg = N.Val->getOperand(0); |
| 512 | } |
| 513 | |
| 514 | AM.IndexReg = AM.Base.Reg = Reg; |
| 515 | return false; |
| 516 | } |
| 517 | break; |
| 518 | |
| 519 | case ISD::ADD: { |
Evan Cheng | 77d86ff | 2006-02-25 10:09:08 +0000 | [diff] [blame] | 520 | if (!Available) { |
Evan Cheng | a86ba85 | 2006-02-11 02:05:36 +0000 | [diff] [blame] | 521 | X86ISelAddressMode Backup = AM; |
| 522 | if (!MatchAddress(N.Val->getOperand(0), AM, false) && |
| 523 | !MatchAddress(N.Val->getOperand(1), AM, false)) |
| 524 | return false; |
| 525 | AM = Backup; |
| 526 | if (!MatchAddress(N.Val->getOperand(1), AM, false) && |
| 527 | !MatchAddress(N.Val->getOperand(0), AM, false)) |
| 528 | return false; |
| 529 | AM = Backup; |
| 530 | } |
Chris Lattner | 3f0f71b | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 531 | break; |
| 532 | } |
Evan Cheng | 734e1e2 | 2006-05-30 06:59:36 +0000 | [diff] [blame] | 533 | |
| 534 | case ISD::OR: { |
| 535 | if (!Available) { |
| 536 | X86ISelAddressMode Backup = AM; |
| 537 | // Look for (x << c1) | c2 where (c2 < c1) |
| 538 | ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(0)); |
| 539 | if (CN && !MatchAddress(N.Val->getOperand(1), AM, false)) { |
| 540 | if (AM.GV == NULL && AM.Disp == 0 && CN->getValue() < AM.Scale) { |
| 541 | AM.Disp = CN->getValue(); |
| 542 | return false; |
| 543 | } |
| 544 | } |
| 545 | AM = Backup; |
| 546 | CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1)); |
| 547 | if (CN && !MatchAddress(N.Val->getOperand(0), AM, false)) { |
| 548 | if (AM.GV == NULL && AM.Disp == 0 && CN->getValue() < AM.Scale) { |
| 549 | AM.Disp = CN->getValue(); |
| 550 | return false; |
| 551 | } |
| 552 | } |
| 553 | AM = Backup; |
| 554 | } |
| 555 | break; |
| 556 | } |
Chris Lattner | 3f0f71b | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 557 | } |
| 558 | |
| 559 | // Is the base register already occupied? |
| 560 | if (AM.BaseType != X86ISelAddressMode::RegBase || AM.Base.Reg.Val) { |
| 561 | // If so, check to see if the scale index register is set. |
| 562 | if (AM.IndexReg.Val == 0) { |
| 563 | AM.IndexReg = N; |
| 564 | AM.Scale = 1; |
| 565 | return false; |
| 566 | } |
| 567 | |
| 568 | // Otherwise, we cannot select it. |
| 569 | return true; |
| 570 | } |
| 571 | |
| 572 | // Default, generate it as a register. |
| 573 | AM.BaseType = X86ISelAddressMode::RegBase; |
| 574 | AM.Base.Reg = N; |
| 575 | return false; |
| 576 | } |
| 577 | |
Evan Cheng | c9fab31 | 2005-12-08 02:01:35 +0000 | [diff] [blame] | 578 | /// SelectAddr - returns true if it is able pattern match an addressing mode. |
| 579 | /// It returns the operands which make up the maximal addressing mode it can |
| 580 | /// match by reference. |
| 581 | bool X86DAGToDAGISel::SelectAddr(SDOperand N, SDOperand &Base, SDOperand &Scale, |
| 582 | SDOperand &Index, SDOperand &Disp) { |
| 583 | X86ISelAddressMode AM; |
Evan Cheng | bc7a0f44 | 2006-01-11 06:09:51 +0000 | [diff] [blame] | 584 | if (MatchAddress(N, AM)) |
| 585 | return false; |
Evan Cheng | c9fab31 | 2005-12-08 02:01:35 +0000 | [diff] [blame] | 586 | |
Evan Cheng | bc7a0f44 | 2006-01-11 06:09:51 +0000 | [diff] [blame] | 587 | if (AM.BaseType == X86ISelAddressMode::RegBase) { |
Evan Cheng | d19d51f | 2006-02-05 05:25:07 +0000 | [diff] [blame] | 588 | if (!AM.Base.Reg.Val) |
Evan Cheng | bc7a0f44 | 2006-01-11 06:09:51 +0000 | [diff] [blame] | 589 | AM.Base.Reg = CurDAG->getRegister(0, MVT::i32); |
Evan Cheng | c9fab31 | 2005-12-08 02:01:35 +0000 | [diff] [blame] | 590 | } |
Evan Cheng | bc7a0f44 | 2006-01-11 06:09:51 +0000 | [diff] [blame] | 591 | |
Evan Cheng | d19d51f | 2006-02-05 05:25:07 +0000 | [diff] [blame] | 592 | if (!AM.IndexReg.Val) |
Evan Cheng | bc7a0f44 | 2006-01-11 06:09:51 +0000 | [diff] [blame] | 593 | AM.IndexReg = CurDAG->getRegister(0, MVT::i32); |
| 594 | |
| 595 | getAddressOperands(AM, Base, Scale, Index, Disp); |
Evan Cheng | 77d86ff | 2006-02-25 10:09:08 +0000 | [diff] [blame] | 596 | |
Evan Cheng | 11a4d8c | 2006-07-28 00:49:31 +0000 | [diff] [blame] | 597 | int Id = Base.Val ? Base.Val->getNodeId() : -1; |
| 598 | if (Id != -1) |
| 599 | setUnfoldable(Base.Val); |
| 600 | Id = Index.Val ? Index.Val->getNodeId() : -1; |
| 601 | if (Id != -1) |
| 602 | setUnfoldable(Index.Val); |
| 603 | |
Evan Cheng | bc7a0f44 | 2006-01-11 06:09:51 +0000 | [diff] [blame] | 604 | return true; |
Evan Cheng | c9fab31 | 2005-12-08 02:01:35 +0000 | [diff] [blame] | 605 | } |
| 606 | |
Evan Cheng | 77d86ff | 2006-02-25 10:09:08 +0000 | [diff] [blame] | 607 | /// SelectLEAAddr - it calls SelectAddr and determines if the maximal addressing |
| 608 | /// mode it matches can be cost effectively emitted as an LEA instruction. |
Evan Cheng | 77d86ff | 2006-02-25 10:09:08 +0000 | [diff] [blame] | 609 | bool X86DAGToDAGISel::SelectLEAAddr(SDOperand N, SDOperand &Base, |
| 610 | SDOperand &Scale, |
| 611 | SDOperand &Index, SDOperand &Disp) { |
| 612 | X86ISelAddressMode AM; |
| 613 | if (MatchAddress(N, AM)) |
| 614 | return false; |
| 615 | |
| 616 | unsigned Complexity = 0; |
| 617 | if (AM.BaseType == X86ISelAddressMode::RegBase) |
| 618 | if (AM.Base.Reg.Val) |
| 619 | Complexity = 1; |
| 620 | else |
| 621 | AM.Base.Reg = CurDAG->getRegister(0, MVT::i32); |
| 622 | else if (AM.BaseType == X86ISelAddressMode::FrameIndexBase) |
| 623 | Complexity = 4; |
| 624 | |
| 625 | if (AM.IndexReg.Val) |
| 626 | Complexity++; |
| 627 | else |
| 628 | AM.IndexReg = CurDAG->getRegister(0, MVT::i32); |
| 629 | |
Evan Cheng | 990c360 | 2006-02-28 21:13:57 +0000 | [diff] [blame] | 630 | if (AM.Scale > 2) |
Evan Cheng | 77d86ff | 2006-02-25 10:09:08 +0000 | [diff] [blame] | 631 | Complexity += 2; |
Evan Cheng | 990c360 | 2006-02-28 21:13:57 +0000 | [diff] [blame] | 632 | // Don't match just leal(,%reg,2). It's cheaper to do addl %reg, %reg |
| 633 | else if (AM.Scale > 1) |
| 634 | Complexity++; |
Evan Cheng | 77d86ff | 2006-02-25 10:09:08 +0000 | [diff] [blame] | 635 | |
| 636 | // FIXME: We are artificially lowering the criteria to turn ADD %reg, $GA |
| 637 | // to a LEA. This is determined with some expermentation but is by no means |
| 638 | // optimal (especially for code size consideration). LEA is nice because of |
| 639 | // its three-address nature. Tweak the cost function again when we can run |
| 640 | // convertToThreeAddress() at register allocation time. |
| 641 | if (AM.GV || AM.CP) |
| 642 | Complexity += 2; |
| 643 | |
| 644 | if (AM.Disp && (AM.Base.Reg.Val || AM.IndexReg.Val)) |
| 645 | Complexity++; |
| 646 | |
| 647 | if (Complexity > 2) { |
| 648 | getAddressOperands(AM, Base, Scale, Index, Disp); |
| 649 | return true; |
| 650 | } |
| 651 | |
Evan Cheng | 11a4d8c | 2006-07-28 00:49:31 +0000 | [diff] [blame] | 652 | int Id = Base.Val ? Base.Val->getNodeId() : -1; |
| 653 | if (Id != -1) |
| 654 | setUnfoldable(Base.Val); |
| 655 | Id = Index.Val ? Index.Val->getNodeId() : -1; |
| 656 | if (Id != -1) |
| 657 | setUnfoldable(Index.Val); |
| 658 | |
Evan Cheng | 77d86ff | 2006-02-25 10:09:08 +0000 | [diff] [blame] | 659 | return false; |
| 660 | } |
| 661 | |
Evan Cheng | d5f2ba0 | 2006-02-06 06:02:33 +0000 | [diff] [blame] | 662 | bool X86DAGToDAGISel::TryFoldLoad(SDOperand P, SDOperand N, |
| 663 | SDOperand &Base, SDOperand &Scale, |
| 664 | SDOperand &Index, SDOperand &Disp) { |
| 665 | if (N.getOpcode() == ISD::LOAD && |
| 666 | N.hasOneUse() && |
| 667 | !CodeGenMap.count(N.getValue(0)) && |
Evan Cheng | e2a3f70 | 2006-07-28 01:03:48 +0000 | [diff] [blame] | 668 | !CanBeFoldedBy(N.Val, P.Val)) |
Evan Cheng | 10d2790 | 2006-01-06 20:36:21 +0000 | [diff] [blame] | 669 | return SelectAddr(N.getOperand(1), Base, Scale, Index, Disp); |
| 670 | return false; |
| 671 | } |
| 672 | |
| 673 | static bool isRegister0(SDOperand Op) { |
Evan Cheng | c9fab31 | 2005-12-08 02:01:35 +0000 | [diff] [blame] | 674 | if (RegisterSDNode *R = dyn_cast<RegisterSDNode>(Op)) |
| 675 | return (R->getReg() == 0); |
| 676 | return false; |
| 677 | } |
| 678 | |
Evan Cheng | 5588de9 | 2006-02-18 00:15:05 +0000 | [diff] [blame] | 679 | /// getGlobalBaseReg - Output the instructions required to put the |
| 680 | /// base address to use for accessing globals into a register. |
| 681 | /// |
| 682 | SDOperand X86DAGToDAGISel::getGlobalBaseReg() { |
| 683 | if (!GlobalBaseReg) { |
| 684 | // Insert the set of GlobalBaseReg into the first MBB of the function |
| 685 | MachineBasicBlock &FirstMBB = BB->getParent()->front(); |
| 686 | MachineBasicBlock::iterator MBBI = FirstMBB.begin(); |
| 687 | SSARegMap *RegMap = BB->getParent()->getSSARegMap(); |
| 688 | // FIXME: when we get to LP64, we will need to create the appropriate |
| 689 | // type of register here. |
Evan Cheng | 9fee442 | 2006-05-16 07:21:53 +0000 | [diff] [blame] | 690 | GlobalBaseReg = RegMap->createVirtualRegister(X86::GR32RegisterClass); |
Evan Cheng | 5588de9 | 2006-02-18 00:15:05 +0000 | [diff] [blame] | 691 | BuildMI(FirstMBB, MBBI, X86::MovePCtoStack, 0); |
| 692 | BuildMI(FirstMBB, MBBI, X86::POP32r, 1, GlobalBaseReg); |
| 693 | } |
| 694 | return CurDAG->getRegister(GlobalBaseReg, MVT::i32); |
| 695 | } |
| 696 | |
Evan Cheng | f838cfc | 2006-05-20 01:36:52 +0000 | [diff] [blame] | 697 | static SDNode *FindCallStartFromCall(SDNode *Node) { |
| 698 | if (Node->getOpcode() == ISD::CALLSEQ_START) return Node; |
| 699 | assert(Node->getOperand(0).getValueType() == MVT::Other && |
| 700 | "Node doesn't have a token chain argument!"); |
| 701 | return FindCallStartFromCall(Node->getOperand(0).Val); |
| 702 | } |
| 703 | |
Evan Cheng | 6dc90ca | 2006-02-09 00:37:58 +0000 | [diff] [blame] | 704 | void X86DAGToDAGISel::Select(SDOperand &Result, SDOperand N) { |
Evan Cheng | 00fcb00 | 2005-12-15 01:02:48 +0000 | [diff] [blame] | 705 | SDNode *Node = N.Val; |
| 706 | MVT::ValueType NVT = Node->getValueType(0); |
Evan Cheng | 10d2790 | 2006-01-06 20:36:21 +0000 | [diff] [blame] | 707 | unsigned Opc, MOpc; |
| 708 | unsigned Opcode = Node->getOpcode(); |
Chris Lattner | 655e7df | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 709 | |
Evan Cheng | d49cc36 | 2006-02-10 22:24:32 +0000 | [diff] [blame] | 710 | #ifndef NDEBUG |
Evan Cheng | 2b6f78b | 2006-02-10 22:46:26 +0000 | [diff] [blame] | 711 | DEBUG(std::cerr << std::string(Indent, ' ')); |
Evan Cheng | d49cc36 | 2006-02-10 22:24:32 +0000 | [diff] [blame] | 712 | DEBUG(std::cerr << "Selecting: "); |
| 713 | DEBUG(Node->dump(CurDAG)); |
| 714 | DEBUG(std::cerr << "\n"); |
Evan Cheng | 2b6f78b | 2006-02-10 22:46:26 +0000 | [diff] [blame] | 715 | Indent += 2; |
Evan Cheng | d49cc36 | 2006-02-10 22:24:32 +0000 | [diff] [blame] | 716 | #endif |
| 717 | |
Evan Cheng | 6dc90ca | 2006-02-09 00:37:58 +0000 | [diff] [blame] | 718 | if (Opcode >= ISD::BUILTIN_OP_END && Opcode < X86ISD::FIRST_NUMBER) { |
| 719 | Result = N; |
Evan Cheng | d49cc36 | 2006-02-10 22:24:32 +0000 | [diff] [blame] | 720 | #ifndef NDEBUG |
Evan Cheng | a86ba85 | 2006-02-11 02:05:36 +0000 | [diff] [blame] | 721 | DEBUG(std::cerr << std::string(Indent-2, ' ')); |
Evan Cheng | d49cc36 | 2006-02-10 22:24:32 +0000 | [diff] [blame] | 722 | DEBUG(std::cerr << "== "); |
| 723 | DEBUG(Node->dump(CurDAG)); |
| 724 | DEBUG(std::cerr << "\n"); |
Evan Cheng | 2b6f78b | 2006-02-10 22:46:26 +0000 | [diff] [blame] | 725 | Indent -= 2; |
Evan Cheng | d49cc36 | 2006-02-10 22:24:32 +0000 | [diff] [blame] | 726 | #endif |
Evan Cheng | 6dc90ca | 2006-02-09 00:37:58 +0000 | [diff] [blame] | 727 | return; // Already selected. |
| 728 | } |
Evan Cheng | 2ae799a | 2006-01-11 22:15:18 +0000 | [diff] [blame] | 729 | |
| 730 | std::map<SDOperand, SDOperand>::iterator CGMI = CodeGenMap.find(N); |
Evan Cheng | 6dc90ca | 2006-02-09 00:37:58 +0000 | [diff] [blame] | 731 | if (CGMI != CodeGenMap.end()) { |
| 732 | Result = CGMI->second; |
Evan Cheng | d49cc36 | 2006-02-10 22:24:32 +0000 | [diff] [blame] | 733 | #ifndef NDEBUG |
Evan Cheng | a86ba85 | 2006-02-11 02:05:36 +0000 | [diff] [blame] | 734 | DEBUG(std::cerr << std::string(Indent-2, ' ')); |
Evan Cheng | d49cc36 | 2006-02-10 22:24:32 +0000 | [diff] [blame] | 735 | DEBUG(std::cerr << "== "); |
| 736 | DEBUG(Result.Val->dump(CurDAG)); |
| 737 | DEBUG(std::cerr << "\n"); |
Evan Cheng | 2b6f78b | 2006-02-10 22:46:26 +0000 | [diff] [blame] | 738 | Indent -= 2; |
Evan Cheng | d49cc36 | 2006-02-10 22:24:32 +0000 | [diff] [blame] | 739 | #endif |
Evan Cheng | 6dc90ca | 2006-02-09 00:37:58 +0000 | [diff] [blame] | 740 | return; |
| 741 | } |
Chris Lattner | 655e7df | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 742 | |
Evan Cheng | 10d2790 | 2006-01-06 20:36:21 +0000 | [diff] [blame] | 743 | switch (Opcode) { |
Chris Lattner | 655e7df | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 744 | default: break; |
Evan Cheng | e0ed6ec | 2006-02-23 20:41:18 +0000 | [diff] [blame] | 745 | case X86ISD::GlobalBaseReg: |
| 746 | Result = getGlobalBaseReg(); |
| 747 | return; |
| 748 | |
Evan Cheng | 77d86ff | 2006-02-25 10:09:08 +0000 | [diff] [blame] | 749 | case ISD::ADD: { |
| 750 | // Turn ADD X, c to MOV32ri X+c. This cannot be done with tblgen'd |
| 751 | // code and is matched first so to prevent it from being turned into |
| 752 | // LEA32r X+c. |
| 753 | SDOperand N0 = N.getOperand(0); |
| 754 | SDOperand N1 = N.getOperand(1); |
| 755 | if (N.Val->getValueType(0) == MVT::i32 && |
| 756 | N0.getOpcode() == X86ISD::Wrapper && |
| 757 | N1.getOpcode() == ISD::Constant) { |
| 758 | unsigned Offset = (unsigned)cast<ConstantSDNode>(N1)->getValue(); |
| 759 | SDOperand C(0, 0); |
| 760 | // TODO: handle ExternalSymbolSDNode. |
| 761 | if (GlobalAddressSDNode *G = |
| 762 | dyn_cast<GlobalAddressSDNode>(N0.getOperand(0))) { |
| 763 | C = CurDAG->getTargetGlobalAddress(G->getGlobal(), MVT::i32, |
| 764 | G->getOffset() + Offset); |
| 765 | } else if (ConstantPoolSDNode *CP = |
| 766 | dyn_cast<ConstantPoolSDNode>(N0.getOperand(0))) { |
| 767 | C = CurDAG->getTargetConstantPool(CP->get(), MVT::i32, |
| 768 | CP->getAlignment(), |
| 769 | CP->getOffset()+Offset); |
| 770 | } |
| 771 | |
| 772 | if (C.Val) { |
| 773 | if (N.Val->hasOneUse()) { |
| 774 | Result = CurDAG->SelectNodeTo(N.Val, X86::MOV32ri, MVT::i32, C); |
| 775 | } else { |
| 776 | SDNode *ResNode = CurDAG->getTargetNode(X86::MOV32ri, MVT::i32, C); |
| 777 | Result = CodeGenMap[N] = SDOperand(ResNode, 0); |
| 778 | } |
| 779 | return; |
| 780 | } |
| 781 | } |
| 782 | |
| 783 | // Other cases are handled by auto-generated code. |
| 784 | break; |
Evan Cheng | 1f342c2 | 2006-02-23 02:43:52 +0000 | [diff] [blame] | 785 | } |
Evan Cheng | e0ed6ec | 2006-02-23 20:41:18 +0000 | [diff] [blame] | 786 | |
Evan Cheng | 10d2790 | 2006-01-06 20:36:21 +0000 | [diff] [blame] | 787 | case ISD::MULHU: |
| 788 | case ISD::MULHS: { |
| 789 | if (Opcode == ISD::MULHU) |
| 790 | switch (NVT) { |
| 791 | default: assert(0 && "Unsupported VT!"); |
| 792 | case MVT::i8: Opc = X86::MUL8r; MOpc = X86::MUL8m; break; |
| 793 | case MVT::i16: Opc = X86::MUL16r; MOpc = X86::MUL16m; break; |
| 794 | case MVT::i32: Opc = X86::MUL32r; MOpc = X86::MUL32m; break; |
| 795 | } |
| 796 | else |
| 797 | switch (NVT) { |
| 798 | default: assert(0 && "Unsupported VT!"); |
| 799 | case MVT::i8: Opc = X86::IMUL8r; MOpc = X86::IMUL8m; break; |
| 800 | case MVT::i16: Opc = X86::IMUL16r; MOpc = X86::IMUL16m; break; |
| 801 | case MVT::i32: Opc = X86::IMUL32r; MOpc = X86::IMUL32m; break; |
| 802 | } |
| 803 | |
| 804 | unsigned LoReg, HiReg; |
| 805 | switch (NVT) { |
| 806 | default: assert(0 && "Unsupported VT!"); |
| 807 | case MVT::i8: LoReg = X86::AL; HiReg = X86::AH; break; |
| 808 | case MVT::i16: LoReg = X86::AX; HiReg = X86::DX; break; |
| 809 | case MVT::i32: LoReg = X86::EAX; HiReg = X86::EDX; break; |
| 810 | } |
| 811 | |
| 812 | SDOperand N0 = Node->getOperand(0); |
| 813 | SDOperand N1 = Node->getOperand(1); |
| 814 | |
| 815 | bool foldedLoad = false; |
| 816 | SDOperand Tmp0, Tmp1, Tmp2, Tmp3; |
Evan Cheng | d5f2ba0 | 2006-02-06 06:02:33 +0000 | [diff] [blame] | 817 | foldedLoad = TryFoldLoad(N, N1, Tmp0, Tmp1, Tmp2, Tmp3); |
Evan Cheng | 92e2797 | 2006-01-06 23:19:29 +0000 | [diff] [blame] | 818 | // MULHU and MULHS are commmutative |
| 819 | if (!foldedLoad) { |
Evan Cheng | d5f2ba0 | 2006-02-06 06:02:33 +0000 | [diff] [blame] | 820 | foldedLoad = TryFoldLoad(N, N0, Tmp0, Tmp1, Tmp2, Tmp3); |
Evan Cheng | 92e2797 | 2006-01-06 23:19:29 +0000 | [diff] [blame] | 821 | if (foldedLoad) { |
| 822 | N0 = Node->getOperand(1); |
| 823 | N1 = Node->getOperand(0); |
| 824 | } |
| 825 | } |
| 826 | |
Evan Cheng | 6dc90ca | 2006-02-09 00:37:58 +0000 | [diff] [blame] | 827 | SDOperand Chain; |
| 828 | if (foldedLoad) |
| 829 | Select(Chain, N1.getOperand(0)); |
| 830 | else |
| 831 | Chain = CurDAG->getEntryNode(); |
Evan Cheng | 10d2790 | 2006-01-06 20:36:21 +0000 | [diff] [blame] | 832 | |
Evan Cheng | 6dc90ca | 2006-02-09 00:37:58 +0000 | [diff] [blame] | 833 | SDOperand InFlag(0, 0); |
| 834 | Select(N0, N0); |
Evan Cheng | 10d2790 | 2006-01-06 20:36:21 +0000 | [diff] [blame] | 835 | Chain = CurDAG->getCopyToReg(Chain, CurDAG->getRegister(LoReg, NVT), |
Evan Cheng | 6dc90ca | 2006-02-09 00:37:58 +0000 | [diff] [blame] | 836 | N0, InFlag); |
Evan Cheng | 10d2790 | 2006-01-06 20:36:21 +0000 | [diff] [blame] | 837 | InFlag = Chain.getValue(1); |
| 838 | |
| 839 | if (foldedLoad) { |
Evan Cheng | 6dc90ca | 2006-02-09 00:37:58 +0000 | [diff] [blame] | 840 | Select(Tmp0, Tmp0); |
| 841 | Select(Tmp1, Tmp1); |
| 842 | Select(Tmp2, Tmp2); |
| 843 | Select(Tmp3, Tmp3); |
Evan Cheng | d1b82d8 | 2006-02-09 07:17:49 +0000 | [diff] [blame] | 844 | SDNode *CNode = |
| 845 | CurDAG->getTargetNode(MOpc, MVT::Other, MVT::Flag, Tmp0, Tmp1, |
| 846 | Tmp2, Tmp3, Chain, InFlag); |
| 847 | Chain = SDOperand(CNode, 0); |
| 848 | InFlag = SDOperand(CNode, 1); |
Evan Cheng | 10d2790 | 2006-01-06 20:36:21 +0000 | [diff] [blame] | 849 | } else { |
Evan Cheng | 6dc90ca | 2006-02-09 00:37:58 +0000 | [diff] [blame] | 850 | Select(N1, N1); |
Evan Cheng | d1b82d8 | 2006-02-09 07:17:49 +0000 | [diff] [blame] | 851 | InFlag = |
| 852 | SDOperand(CurDAG->getTargetNode(Opc, MVT::Flag, N1, InFlag), 0); |
Evan Cheng | 10d2790 | 2006-01-06 20:36:21 +0000 | [diff] [blame] | 853 | } |
| 854 | |
Evan Cheng | 6dc90ca | 2006-02-09 00:37:58 +0000 | [diff] [blame] | 855 | Result = CurDAG->getCopyFromReg(Chain, HiReg, NVT, InFlag); |
Evan Cheng | 10d2790 | 2006-01-06 20:36:21 +0000 | [diff] [blame] | 856 | CodeGenMap[N.getValue(0)] = Result; |
Evan Cheng | d5f2ba0 | 2006-02-06 06:02:33 +0000 | [diff] [blame] | 857 | if (foldedLoad) { |
Evan Cheng | 92e2797 | 2006-01-06 23:19:29 +0000 | [diff] [blame] | 858 | CodeGenMap[N1.getValue(1)] = Result.getValue(1); |
Evan Cheng | 101e4b9 | 2006-02-09 22:12:53 +0000 | [diff] [blame] | 859 | AddHandleReplacement(N1.Val, 1, Result.Val, 1); |
Evan Cheng | d5f2ba0 | 2006-02-06 06:02:33 +0000 | [diff] [blame] | 860 | } |
Evan Cheng | 6dc90ca | 2006-02-09 00:37:58 +0000 | [diff] [blame] | 861 | |
Evan Cheng | d49cc36 | 2006-02-10 22:24:32 +0000 | [diff] [blame] | 862 | #ifndef NDEBUG |
Evan Cheng | a86ba85 | 2006-02-11 02:05:36 +0000 | [diff] [blame] | 863 | DEBUG(std::cerr << std::string(Indent-2, ' ')); |
Evan Cheng | d49cc36 | 2006-02-10 22:24:32 +0000 | [diff] [blame] | 864 | DEBUG(std::cerr << "== "); |
| 865 | DEBUG(Result.Val->dump(CurDAG)); |
| 866 | DEBUG(std::cerr << "\n"); |
Evan Cheng | 2b6f78b | 2006-02-10 22:46:26 +0000 | [diff] [blame] | 867 | Indent -= 2; |
Evan Cheng | d49cc36 | 2006-02-10 22:24:32 +0000 | [diff] [blame] | 868 | #endif |
Evan Cheng | 6dc90ca | 2006-02-09 00:37:58 +0000 | [diff] [blame] | 869 | return; |
Evan Cheng | 92e2797 | 2006-01-06 23:19:29 +0000 | [diff] [blame] | 870 | } |
Evan Cheng | 5588de9 | 2006-02-18 00:15:05 +0000 | [diff] [blame] | 871 | |
Evan Cheng | 92e2797 | 2006-01-06 23:19:29 +0000 | [diff] [blame] | 872 | case ISD::SDIV: |
| 873 | case ISD::UDIV: |
| 874 | case ISD::SREM: |
| 875 | case ISD::UREM: { |
| 876 | bool isSigned = Opcode == ISD::SDIV || Opcode == ISD::SREM; |
| 877 | bool isDiv = Opcode == ISD::SDIV || Opcode == ISD::UDIV; |
| 878 | if (!isSigned) |
| 879 | switch (NVT) { |
| 880 | default: assert(0 && "Unsupported VT!"); |
| 881 | case MVT::i8: Opc = X86::DIV8r; MOpc = X86::DIV8m; break; |
| 882 | case MVT::i16: Opc = X86::DIV16r; MOpc = X86::DIV16m; break; |
| 883 | case MVT::i32: Opc = X86::DIV32r; MOpc = X86::DIV32m; break; |
| 884 | } |
| 885 | else |
| 886 | switch (NVT) { |
| 887 | default: assert(0 && "Unsupported VT!"); |
| 888 | case MVT::i8: Opc = X86::IDIV8r; MOpc = X86::IDIV8m; break; |
| 889 | case MVT::i16: Opc = X86::IDIV16r; MOpc = X86::IDIV16m; break; |
| 890 | case MVT::i32: Opc = X86::IDIV32r; MOpc = X86::IDIV32m; break; |
| 891 | } |
| 892 | |
| 893 | unsigned LoReg, HiReg; |
| 894 | unsigned ClrOpcode, SExtOpcode; |
| 895 | switch (NVT) { |
| 896 | default: assert(0 && "Unsupported VT!"); |
| 897 | case MVT::i8: |
| 898 | LoReg = X86::AL; HiReg = X86::AH; |
Evan Cheng | a2efb9f | 2006-06-02 21:20:34 +0000 | [diff] [blame] | 899 | ClrOpcode = X86::MOV8r0; |
Evan Cheng | 92e2797 | 2006-01-06 23:19:29 +0000 | [diff] [blame] | 900 | SExtOpcode = X86::CBW; |
| 901 | break; |
| 902 | case MVT::i16: |
| 903 | LoReg = X86::AX; HiReg = X86::DX; |
Evan Cheng | a2efb9f | 2006-06-02 21:20:34 +0000 | [diff] [blame] | 904 | ClrOpcode = X86::MOV16r0; |
Evan Cheng | 92e2797 | 2006-01-06 23:19:29 +0000 | [diff] [blame] | 905 | SExtOpcode = X86::CWD; |
| 906 | break; |
| 907 | case MVT::i32: |
| 908 | LoReg = X86::EAX; HiReg = X86::EDX; |
Evan Cheng | a2efb9f | 2006-06-02 21:20:34 +0000 | [diff] [blame] | 909 | ClrOpcode = X86::MOV32r0; |
Evan Cheng | 92e2797 | 2006-01-06 23:19:29 +0000 | [diff] [blame] | 910 | SExtOpcode = X86::CDQ; |
| 911 | break; |
| 912 | } |
| 913 | |
| 914 | SDOperand N0 = Node->getOperand(0); |
| 915 | SDOperand N1 = Node->getOperand(1); |
| 916 | |
| 917 | bool foldedLoad = false; |
| 918 | SDOperand Tmp0, Tmp1, Tmp2, Tmp3; |
Evan Cheng | d5f2ba0 | 2006-02-06 06:02:33 +0000 | [diff] [blame] | 919 | foldedLoad = TryFoldLoad(N, N1, Tmp0, Tmp1, Tmp2, Tmp3); |
Evan Cheng | 6dc90ca | 2006-02-09 00:37:58 +0000 | [diff] [blame] | 920 | SDOperand Chain; |
| 921 | if (foldedLoad) |
| 922 | Select(Chain, N1.getOperand(0)); |
| 923 | else |
| 924 | Chain = CurDAG->getEntryNode(); |
Evan Cheng | 92e2797 | 2006-01-06 23:19:29 +0000 | [diff] [blame] | 925 | |
Evan Cheng | 6dc90ca | 2006-02-09 00:37:58 +0000 | [diff] [blame] | 926 | SDOperand InFlag(0, 0); |
| 927 | Select(N0, N0); |
Evan Cheng | 92e2797 | 2006-01-06 23:19:29 +0000 | [diff] [blame] | 928 | Chain = CurDAG->getCopyToReg(Chain, CurDAG->getRegister(LoReg, NVT), |
Evan Cheng | 6dc90ca | 2006-02-09 00:37:58 +0000 | [diff] [blame] | 929 | N0, InFlag); |
Evan Cheng | 92e2797 | 2006-01-06 23:19:29 +0000 | [diff] [blame] | 930 | InFlag = Chain.getValue(1); |
| 931 | |
| 932 | if (isSigned) { |
| 933 | // Sign extend the low part into the high part. |
Evan Cheng | d1b82d8 | 2006-02-09 07:17:49 +0000 | [diff] [blame] | 934 | InFlag = |
| 935 | SDOperand(CurDAG->getTargetNode(SExtOpcode, MVT::Flag, InFlag), 0); |
Evan Cheng | 92e2797 | 2006-01-06 23:19:29 +0000 | [diff] [blame] | 936 | } else { |
| 937 | // Zero out the high part, effectively zero extending the input. |
Evan Cheng | a2efb9f | 2006-06-02 21:20:34 +0000 | [diff] [blame] | 938 | SDOperand ClrNode = SDOperand(CurDAG->getTargetNode(ClrOpcode, NVT), 0); |
Evan Cheng | 92e2797 | 2006-01-06 23:19:29 +0000 | [diff] [blame] | 939 | Chain = CurDAG->getCopyToReg(Chain, CurDAG->getRegister(HiReg, NVT), |
| 940 | ClrNode, InFlag); |
| 941 | InFlag = Chain.getValue(1); |
| 942 | } |
| 943 | |
| 944 | if (foldedLoad) { |
Evan Cheng | 6dc90ca | 2006-02-09 00:37:58 +0000 | [diff] [blame] | 945 | Select(Tmp0, Tmp0); |
| 946 | Select(Tmp1, Tmp1); |
| 947 | Select(Tmp2, Tmp2); |
| 948 | Select(Tmp3, Tmp3); |
Evan Cheng | d1b82d8 | 2006-02-09 07:17:49 +0000 | [diff] [blame] | 949 | SDNode *CNode = |
| 950 | CurDAG->getTargetNode(MOpc, MVT::Other, MVT::Flag, Tmp0, Tmp1, |
| 951 | Tmp2, Tmp3, Chain, InFlag); |
| 952 | Chain = SDOperand(CNode, 0); |
| 953 | InFlag = SDOperand(CNode, 1); |
Evan Cheng | 92e2797 | 2006-01-06 23:19:29 +0000 | [diff] [blame] | 954 | } else { |
Evan Cheng | 6dc90ca | 2006-02-09 00:37:58 +0000 | [diff] [blame] | 955 | Select(N1, N1); |
Evan Cheng | d1b82d8 | 2006-02-09 07:17:49 +0000 | [diff] [blame] | 956 | InFlag = |
| 957 | SDOperand(CurDAG->getTargetNode(Opc, MVT::Flag, N1, InFlag), 0); |
Evan Cheng | 92e2797 | 2006-01-06 23:19:29 +0000 | [diff] [blame] | 958 | } |
| 959 | |
Evan Cheng | 6dc90ca | 2006-02-09 00:37:58 +0000 | [diff] [blame] | 960 | Result = CurDAG->getCopyFromReg(Chain, isDiv ? LoReg : HiReg, |
| 961 | NVT, InFlag); |
Evan Cheng | 92e2797 | 2006-01-06 23:19:29 +0000 | [diff] [blame] | 962 | CodeGenMap[N.getValue(0)] = Result; |
Evan Cheng | d5f2ba0 | 2006-02-06 06:02:33 +0000 | [diff] [blame] | 963 | if (foldedLoad) { |
Evan Cheng | 92e2797 | 2006-01-06 23:19:29 +0000 | [diff] [blame] | 964 | CodeGenMap[N1.getValue(1)] = Result.getValue(1); |
Evan Cheng | 101e4b9 | 2006-02-09 22:12:53 +0000 | [diff] [blame] | 965 | AddHandleReplacement(N1.Val, 1, Result.Val, 1); |
Evan Cheng | d5f2ba0 | 2006-02-06 06:02:33 +0000 | [diff] [blame] | 966 | } |
Evan Cheng | d49cc36 | 2006-02-10 22:24:32 +0000 | [diff] [blame] | 967 | |
| 968 | #ifndef NDEBUG |
Evan Cheng | a86ba85 | 2006-02-11 02:05:36 +0000 | [diff] [blame] | 969 | DEBUG(std::cerr << std::string(Indent-2, ' ')); |
Evan Cheng | d49cc36 | 2006-02-10 22:24:32 +0000 | [diff] [blame] | 970 | DEBUG(std::cerr << "== "); |
| 971 | DEBUG(Result.Val->dump(CurDAG)); |
| 972 | DEBUG(std::cerr << "\n"); |
Evan Cheng | 2b6f78b | 2006-02-10 22:46:26 +0000 | [diff] [blame] | 973 | Indent -= 2; |
Evan Cheng | d49cc36 | 2006-02-10 22:24:32 +0000 | [diff] [blame] | 974 | #endif |
Evan Cheng | 6dc90ca | 2006-02-09 00:37:58 +0000 | [diff] [blame] | 975 | return; |
Evan Cheng | 10d2790 | 2006-01-06 20:36:21 +0000 | [diff] [blame] | 976 | } |
Evan Cheng | 9733bde | 2006-05-08 08:01:26 +0000 | [diff] [blame] | 977 | |
| 978 | case ISD::TRUNCATE: { |
| 979 | if (NVT == MVT::i8) { |
| 980 | unsigned Opc2; |
| 981 | MVT::ValueType VT; |
| 982 | switch (Node->getOperand(0).getValueType()) { |
| 983 | default: assert(0 && "Unknown truncate!"); |
| 984 | case MVT::i16: |
| 985 | Opc = X86::MOV16to16_; |
| 986 | VT = MVT::i16; |
Evan Cheng | 9fee442 | 2006-05-16 07:21:53 +0000 | [diff] [blame] | 987 | Opc2 = X86::TRUNC_GR16_GR8; |
Evan Cheng | 9733bde | 2006-05-08 08:01:26 +0000 | [diff] [blame] | 988 | break; |
| 989 | case MVT::i32: |
| 990 | Opc = X86::MOV32to32_; |
| 991 | VT = MVT::i32; |
Evan Cheng | 9fee442 | 2006-05-16 07:21:53 +0000 | [diff] [blame] | 992 | Opc2 = X86::TRUNC_GR32_GR8; |
Evan Cheng | 9733bde | 2006-05-08 08:01:26 +0000 | [diff] [blame] | 993 | break; |
| 994 | } |
| 995 | |
| 996 | SDOperand Tmp0, Tmp1; |
| 997 | Select(Tmp0, Node->getOperand(0)); |
| 998 | Tmp1 = SDOperand(CurDAG->getTargetNode(Opc, VT, Tmp0), 0); |
| 999 | Result = CodeGenMap[N] = |
| 1000 | SDOperand(CurDAG->getTargetNode(Opc2, NVT, Tmp1), 0); |
| 1001 | |
| 1002 | #ifndef NDEBUG |
| 1003 | DEBUG(std::cerr << std::string(Indent-2, ' ')); |
| 1004 | DEBUG(std::cerr << "== "); |
| 1005 | DEBUG(Result.Val->dump(CurDAG)); |
| 1006 | DEBUG(std::cerr << "\n"); |
| 1007 | Indent -= 2; |
| 1008 | #endif |
| 1009 | return; |
| 1010 | } |
Evan Cheng | a26c451 | 2006-05-20 07:44:28 +0000 | [diff] [blame] | 1011 | |
| 1012 | break; |
Evan Cheng | 9733bde | 2006-05-08 08:01:26 +0000 | [diff] [blame] | 1013 | } |
Chris Lattner | 655e7df | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 1014 | } |
| 1015 | |
Evan Cheng | 6dc90ca | 2006-02-09 00:37:58 +0000 | [diff] [blame] | 1016 | SelectCode(Result, N); |
Evan Cheng | d49cc36 | 2006-02-10 22:24:32 +0000 | [diff] [blame] | 1017 | #ifndef NDEBUG |
Evan Cheng | a86ba85 | 2006-02-11 02:05:36 +0000 | [diff] [blame] | 1018 | DEBUG(std::cerr << std::string(Indent-2, ' ')); |
Evan Cheng | d49cc36 | 2006-02-10 22:24:32 +0000 | [diff] [blame] | 1019 | DEBUG(std::cerr << "=> "); |
| 1020 | DEBUG(Result.Val->dump(CurDAG)); |
| 1021 | DEBUG(std::cerr << "\n"); |
Evan Cheng | 2b6f78b | 2006-02-10 22:46:26 +0000 | [diff] [blame] | 1022 | Indent -= 2; |
Evan Cheng | d49cc36 | 2006-02-10 22:24:32 +0000 | [diff] [blame] | 1023 | #endif |
Chris Lattner | 655e7df | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 1024 | } |
| 1025 | |
Chris Lattner | ba1ed58 | 2006-06-08 18:03:49 +0000 | [diff] [blame] | 1026 | bool X86DAGToDAGISel:: |
| 1027 | SelectInlineAsmMemoryOperand(const SDOperand &Op, char ConstraintCode, |
| 1028 | std::vector<SDOperand> &OutOps, SelectionDAG &DAG){ |
| 1029 | SDOperand Op0, Op1, Op2, Op3; |
| 1030 | switch (ConstraintCode) { |
| 1031 | case 'o': // offsetable ?? |
| 1032 | case 'v': // not offsetable ?? |
| 1033 | default: return true; |
| 1034 | case 'm': // memory |
| 1035 | if (!SelectAddr(Op, Op0, Op1, Op2, Op3)) |
| 1036 | return true; |
| 1037 | break; |
| 1038 | } |
| 1039 | |
| 1040 | OutOps.resize(4); |
| 1041 | Select(OutOps[0], Op0); |
| 1042 | Select(OutOps[1], Op1); |
| 1043 | Select(OutOps[2], Op2); |
| 1044 | Select(OutOps[3], Op3); |
| 1045 | return false; |
| 1046 | } |
| 1047 | |
Chris Lattner | 655e7df | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 1048 | /// createX86ISelDag - This pass converts a legalized DAG into a |
| 1049 | /// X86-specific DAG, ready for instruction scheduling. |
| 1050 | /// |
Evan Cheng | 2dd2c65 | 2006-03-13 23:20:37 +0000 | [diff] [blame] | 1051 | FunctionPass *llvm::createX86ISelDag(X86TargetMachine &TM) { |
Chris Lattner | 655e7df | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 1052 | return new X86DAGToDAGISel(TM); |
| 1053 | } |