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 | |
| 15 | #include "X86.h" |
| 16 | #include "X86Subtarget.h" |
| 17 | #include "X86ISelLowering.h" |
Chris Lattner | 3f0f71b | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 18 | #include "llvm/GlobalValue.h" |
| 19 | #include "llvm/CodeGen/MachineConstantPool.h" |
Chris Lattner | 655e7df | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 20 | #include "llvm/CodeGen/MachineFunction.h" |
| 21 | #include "llvm/CodeGen/SelectionDAGISel.h" |
| 22 | #include "llvm/Target/TargetMachine.h" |
| 23 | #include "llvm/Support/Debug.h" |
| 24 | #include "llvm/ADT/Statistic.h" |
Evan Cheng | 00fcb00 | 2005-12-15 01:02:48 +0000 | [diff] [blame^] | 25 | #include <set> |
Chris Lattner | 655e7df | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 26 | using namespace llvm; |
| 27 | |
| 28 | //===----------------------------------------------------------------------===// |
| 29 | // Pattern Matcher Implementation |
| 30 | //===----------------------------------------------------------------------===// |
| 31 | |
| 32 | namespace { |
Chris Lattner | 3f0f71b | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 33 | /// X86ISelAddressMode - This corresponds to X86AddressMode, but uses |
| 34 | /// SDOperand's instead of register numbers for the leaves of the matched |
| 35 | /// tree. |
| 36 | struct X86ISelAddressMode { |
| 37 | enum { |
| 38 | RegBase, |
| 39 | FrameIndexBase, |
Evan Cheng | c9fab31 | 2005-12-08 02:01:35 +0000 | [diff] [blame] | 40 | ConstantPoolBase |
Chris Lattner | 3f0f71b | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 41 | } BaseType; |
| 42 | |
| 43 | struct { // This is really a union, discriminated by BaseType! |
| 44 | SDOperand Reg; |
| 45 | int FrameIndex; |
| 46 | } Base; |
| 47 | |
| 48 | unsigned Scale; |
| 49 | SDOperand IndexReg; |
| 50 | unsigned Disp; |
| 51 | GlobalValue *GV; |
| 52 | |
| 53 | X86ISelAddressMode() |
Evan Cheng | 4eb7af9 | 2005-11-30 02:51:20 +0000 | [diff] [blame] | 54 | : BaseType(RegBase), Scale(1), IndexReg(), Disp(0), GV(0) { |
Chris Lattner | 3f0f71b | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 55 | } |
| 56 | }; |
| 57 | } |
| 58 | |
| 59 | namespace { |
Chris Lattner | 655e7df | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 60 | Statistic<> |
| 61 | NumFPKill("x86-codegen", "Number of FP_REG_KILL instructions added"); |
| 62 | |
| 63 | //===--------------------------------------------------------------------===// |
| 64 | /// ISel - X86 specific code to select X86 machine instructions for |
| 65 | /// SelectionDAG operations. |
| 66 | /// |
| 67 | class X86DAGToDAGISel : public SelectionDAGISel { |
| 68 | /// ContainsFPCode - Every instruction we select that uses or defines a FP |
| 69 | /// register should set this to true. |
| 70 | bool ContainsFPCode; |
| 71 | |
| 72 | /// X86Lowering - This object fully describes how to lower LLVM code to an |
| 73 | /// X86-specific SelectionDAG. |
| 74 | X86TargetLowering X86Lowering; |
| 75 | |
| 76 | /// Subtarget - Keep a pointer to the X86Subtarget around so that we can |
| 77 | /// make the right decision when generating code for different targets. |
| 78 | const X86Subtarget *Subtarget; |
| 79 | public: |
| 80 | X86DAGToDAGISel(TargetMachine &TM) |
| 81 | : SelectionDAGISel(X86Lowering), X86Lowering(TM) { |
| 82 | Subtarget = &TM.getSubtarget<X86Subtarget>(); |
| 83 | } |
| 84 | |
| 85 | virtual const char *getPassName() const { |
| 86 | return "X86 DAG->DAG Instruction Selection"; |
| 87 | } |
| 88 | |
| 89 | /// InstructionSelectBasicBlock - This callback is invoked by |
| 90 | /// SelectionDAGISel when it has created a SelectionDAG for us to codegen. |
| 91 | virtual void InstructionSelectBasicBlock(SelectionDAG &DAG); |
| 92 | |
| 93 | // Include the pieces autogenerated from the target description. |
| 94 | #include "X86GenDAGISel.inc" |
| 95 | |
| 96 | private: |
| 97 | SDOperand Select(SDOperand N); |
| 98 | |
Evan Cheng | 00fcb00 | 2005-12-15 01:02:48 +0000 | [diff] [blame^] | 99 | bool isFoldableLoad(SDOperand Op, SDOperand OtherOp, |
| 100 | bool FloatPromoteOk = false); |
Chris Lattner | 3f0f71b | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 101 | bool MatchAddress(SDOperand N, X86ISelAddressMode &AM); |
Evan Cheng | c9fab31 | 2005-12-08 02:01:35 +0000 | [diff] [blame] | 102 | bool SelectAddr(SDOperand N, SDOperand &Base, SDOperand &Scale, |
| 103 | SDOperand &Index, SDOperand &Disp); |
| 104 | bool SelectLEAAddr(SDOperand N, SDOperand &Base, SDOperand &Scale, |
| 105 | SDOperand &Index, SDOperand &Disp); |
Chris Lattner | 3f0f71b | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 106 | |
Evan Cheng | 67ed58e | 2005-12-12 21:49:40 +0000 | [diff] [blame] | 107 | inline void getAddressOperands(X86ISelAddressMode &AM, SDOperand &Base, |
| 108 | SDOperand &Scale, SDOperand &Index, |
| 109 | SDOperand &Disp) { |
| 110 | Base = (AM.BaseType == X86ISelAddressMode::FrameIndexBase) ? |
| 111 | CurDAG->getTargetFrameIndex(AM.Base.FrameIndex, MVT::i32) : AM.Base.Reg; |
| 112 | Scale = getI8Imm (AM.Scale); |
| 113 | Index = AM.IndexReg; |
| 114 | Disp = AM.GV ? CurDAG->getTargetGlobalAddress(AM.GV, MVT::i32, AM.Disp) |
| 115 | : getI32Imm(AM.Disp); |
| 116 | } |
| 117 | |
Chris Lattner | 3f0f71b | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 118 | /// getI8Imm - Return a target constant with the specified value, of type |
| 119 | /// i8. |
| 120 | inline SDOperand getI8Imm(unsigned Imm) { |
| 121 | return CurDAG->getTargetConstant(Imm, MVT::i8); |
| 122 | } |
| 123 | |
Chris Lattner | 655e7df | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 124 | /// getI16Imm - Return a target constant with the specified value, of type |
| 125 | /// i16. |
| 126 | inline SDOperand getI16Imm(unsigned Imm) { |
| 127 | return CurDAG->getTargetConstant(Imm, MVT::i16); |
| 128 | } |
| 129 | |
| 130 | /// getI32Imm - Return a target constant with the specified value, of type |
| 131 | /// i32. |
| 132 | inline SDOperand getI32Imm(unsigned Imm) { |
| 133 | return CurDAG->getTargetConstant(Imm, MVT::i32); |
| 134 | } |
| 135 | }; |
| 136 | } |
| 137 | |
| 138 | /// InstructionSelectBasicBlock - This callback is invoked by SelectionDAGISel |
| 139 | /// when it has created a SelectionDAG for us to codegen. |
| 140 | void X86DAGToDAGISel::InstructionSelectBasicBlock(SelectionDAG &DAG) { |
| 141 | DEBUG(BB->dump()); |
| 142 | |
| 143 | // Codegen the basic block. |
| 144 | DAG.setRoot(Select(DAG.getRoot())); |
| 145 | DAG.RemoveDeadNodes(); |
| 146 | |
| 147 | // Emit machine code to BB. |
| 148 | ScheduleAndEmitDAG(DAG); |
| 149 | } |
| 150 | |
Chris Lattner | 3f0f71b | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 151 | /// FIXME: copied from X86ISelPattern.cpp |
| 152 | /// MatchAddress - Add the specified node to the specified addressing mode, |
| 153 | /// returning true if it cannot be done. This just pattern matches for the |
| 154 | /// addressing mode |
| 155 | bool X86DAGToDAGISel::MatchAddress(SDOperand N, X86ISelAddressMode &AM) { |
| 156 | switch (N.getOpcode()) { |
| 157 | default: break; |
| 158 | case ISD::FrameIndex: |
| 159 | if (AM.BaseType == X86ISelAddressMode::RegBase && AM.Base.Reg.Val == 0) { |
| 160 | AM.BaseType = X86ISelAddressMode::FrameIndexBase; |
| 161 | AM.Base.FrameIndex = cast<FrameIndexSDNode>(N)->getIndex(); |
| 162 | return false; |
| 163 | } |
| 164 | break; |
Evan Cheng | c9fab31 | 2005-12-08 02:01:35 +0000 | [diff] [blame] | 165 | |
| 166 | case ISD::ConstantPool: |
| 167 | if (AM.BaseType == X86ISelAddressMode::RegBase && AM.Base.Reg.Val == 0) { |
| 168 | if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N)) { |
| 169 | AM.BaseType = X86ISelAddressMode::ConstantPoolBase; |
| 170 | AM.Base.Reg = CurDAG->getTargetConstantPool(CP->get(), MVT::i32); |
| 171 | return false; |
| 172 | } |
| 173 | } |
| 174 | break; |
| 175 | |
Chris Lattner | 3f0f71b | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 176 | case ISD::GlobalAddress: |
| 177 | if (AM.GV == 0) { |
| 178 | GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal(); |
| 179 | // For Darwin, external and weak symbols are indirect, so we want to load |
| 180 | // the value at address GV, not the value of GV itself. This means that |
| 181 | // the GlobalAddress must be in the base or index register of the address, |
| 182 | // not the GV offset field. |
| 183 | if (Subtarget->getIndirectExternAndWeakGlobals() && |
| 184 | (GV->hasWeakLinkage() || GV->isExternal())) { |
| 185 | break; |
| 186 | } else { |
| 187 | AM.GV = GV; |
| 188 | return false; |
| 189 | } |
| 190 | } |
| 191 | break; |
Evan Cheng | c9fab31 | 2005-12-08 02:01:35 +0000 | [diff] [blame] | 192 | |
Chris Lattner | 3f0f71b | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 193 | case ISD::Constant: |
| 194 | AM.Disp += cast<ConstantSDNode>(N)->getValue(); |
| 195 | return false; |
Evan Cheng | c9fab31 | 2005-12-08 02:01:35 +0000 | [diff] [blame] | 196 | |
Chris Lattner | 3f0f71b | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 197 | case ISD::SHL: |
| 198 | if (AM.IndexReg.Val == 0 && AM.Scale == 1) |
| 199 | if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1))) { |
| 200 | unsigned Val = CN->getValue(); |
| 201 | if (Val == 1 || Val == 2 || Val == 3) { |
| 202 | AM.Scale = 1 << Val; |
| 203 | SDOperand ShVal = N.Val->getOperand(0); |
| 204 | |
| 205 | // Okay, we know that we have a scale by now. However, if the scaled |
| 206 | // value is an add of something and a constant, we can fold the |
| 207 | // constant into the disp field here. |
| 208 | if (ShVal.Val->getOpcode() == ISD::ADD && ShVal.hasOneUse() && |
| 209 | isa<ConstantSDNode>(ShVal.Val->getOperand(1))) { |
| 210 | AM.IndexReg = ShVal.Val->getOperand(0); |
| 211 | ConstantSDNode *AddVal = |
| 212 | cast<ConstantSDNode>(ShVal.Val->getOperand(1)); |
| 213 | AM.Disp += AddVal->getValue() << Val; |
| 214 | } else { |
| 215 | AM.IndexReg = ShVal; |
| 216 | } |
| 217 | return false; |
| 218 | } |
| 219 | } |
| 220 | break; |
Evan Cheng | c9fab31 | 2005-12-08 02:01:35 +0000 | [diff] [blame] | 221 | |
Chris Lattner | 3f0f71b | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 222 | case ISD::MUL: |
| 223 | // X*[3,5,9] -> X+X*[2,4,8] |
| 224 | if (AM.IndexReg.Val == 0 && AM.BaseType == X86ISelAddressMode::RegBase && |
| 225 | AM.Base.Reg.Val == 0) |
| 226 | if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1))) |
| 227 | if (CN->getValue() == 3 || CN->getValue() == 5 || CN->getValue() == 9) { |
| 228 | AM.Scale = unsigned(CN->getValue())-1; |
| 229 | |
| 230 | SDOperand MulVal = N.Val->getOperand(0); |
| 231 | SDOperand Reg; |
| 232 | |
| 233 | // Okay, we know that we have a scale by now. However, if the scaled |
| 234 | // value is an add of something and a constant, we can fold the |
| 235 | // constant into the disp field here. |
| 236 | if (MulVal.Val->getOpcode() == ISD::ADD && MulVal.hasOneUse() && |
| 237 | isa<ConstantSDNode>(MulVal.Val->getOperand(1))) { |
| 238 | Reg = MulVal.Val->getOperand(0); |
| 239 | ConstantSDNode *AddVal = |
| 240 | cast<ConstantSDNode>(MulVal.Val->getOperand(1)); |
| 241 | AM.Disp += AddVal->getValue() * CN->getValue(); |
| 242 | } else { |
| 243 | Reg = N.Val->getOperand(0); |
| 244 | } |
| 245 | |
| 246 | AM.IndexReg = AM.Base.Reg = Reg; |
| 247 | return false; |
| 248 | } |
| 249 | break; |
| 250 | |
| 251 | case ISD::ADD: { |
| 252 | X86ISelAddressMode Backup = AM; |
| 253 | if (!MatchAddress(N.Val->getOperand(0), AM) && |
| 254 | !MatchAddress(N.Val->getOperand(1), AM)) |
| 255 | return false; |
| 256 | AM = Backup; |
| 257 | if (!MatchAddress(N.Val->getOperand(1), AM) && |
| 258 | !MatchAddress(N.Val->getOperand(0), AM)) |
| 259 | return false; |
| 260 | AM = Backup; |
| 261 | break; |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | // Is the base register already occupied? |
| 266 | if (AM.BaseType != X86ISelAddressMode::RegBase || AM.Base.Reg.Val) { |
| 267 | // If so, check to see if the scale index register is set. |
| 268 | if (AM.IndexReg.Val == 0) { |
| 269 | AM.IndexReg = N; |
| 270 | AM.Scale = 1; |
| 271 | return false; |
| 272 | } |
| 273 | |
| 274 | // Otherwise, we cannot select it. |
| 275 | return true; |
| 276 | } |
| 277 | |
| 278 | // Default, generate it as a register. |
| 279 | AM.BaseType = X86ISelAddressMode::RegBase; |
| 280 | AM.Base.Reg = N; |
| 281 | return false; |
| 282 | } |
| 283 | |
Evan Cheng | c9fab31 | 2005-12-08 02:01:35 +0000 | [diff] [blame] | 284 | /// SelectAddr - returns true if it is able pattern match an addressing mode. |
| 285 | /// It returns the operands which make up the maximal addressing mode it can |
| 286 | /// match by reference. |
| 287 | bool X86DAGToDAGISel::SelectAddr(SDOperand N, SDOperand &Base, SDOperand &Scale, |
| 288 | SDOperand &Index, SDOperand &Disp) { |
| 289 | X86ISelAddressMode AM; |
| 290 | if (!MatchAddress(N, AM)) { |
| 291 | if (AM.BaseType == X86ISelAddressMode::RegBase) { |
| 292 | if (AM.Base.Reg.Val) |
| 293 | AM.Base.Reg = Select(AM.Base.Reg); |
| 294 | else |
| 295 | AM.Base.Reg = CurDAG->getRegister(0, MVT::i32); |
| 296 | } |
| 297 | if (AM.IndexReg.Val) |
| 298 | AM.IndexReg = Select(AM.IndexReg); |
| 299 | else |
| 300 | AM.IndexReg = CurDAG->getRegister(0, MVT::i32); |
| 301 | |
Evan Cheng | 67ed58e | 2005-12-12 21:49:40 +0000 | [diff] [blame] | 302 | getAddressOperands(AM, Base, Scale, Index, Disp); |
Evan Cheng | c9fab31 | 2005-12-08 02:01:35 +0000 | [diff] [blame] | 303 | return true; |
| 304 | } |
| 305 | return false; |
| 306 | } |
| 307 | |
| 308 | static bool isRegister0(SDOperand Op) |
| 309 | { |
| 310 | if (RegisterSDNode *R = dyn_cast<RegisterSDNode>(Op)) |
| 311 | return (R->getReg() == 0); |
| 312 | return false; |
| 313 | } |
| 314 | |
| 315 | /// SelectLEAAddr - it calls SelectAddr and determines if the maximal addressing |
| 316 | /// mode it matches can be cost effectively emitted as an LEA instruction. |
| 317 | /// For X86, it always is unless it's just a (Reg + const). |
| 318 | bool X86DAGToDAGISel::SelectLEAAddr(SDOperand N, SDOperand &Base, SDOperand &Scale, |
| 319 | SDOperand &Index, SDOperand &Disp) { |
Evan Cheng | 67ed58e | 2005-12-12 21:49:40 +0000 | [diff] [blame] | 320 | X86ISelAddressMode AM; |
| 321 | if (!MatchAddress(N, AM)) { |
| 322 | bool SelectBase = false; |
| 323 | bool SelectIndex = false; |
| 324 | bool Check = false; |
| 325 | if (AM.BaseType == X86ISelAddressMode::RegBase) { |
| 326 | if (AM.Base.Reg.Val) { |
| 327 | Check = true; |
| 328 | SelectBase = true; |
Evan Cheng | c9fab31 | 2005-12-08 02:01:35 +0000 | [diff] [blame] | 329 | } else { |
Evan Cheng | 67ed58e | 2005-12-12 21:49:40 +0000 | [diff] [blame] | 330 | AM.Base.Reg = CurDAG->getRegister(0, MVT::i32); |
Evan Cheng | c9fab31 | 2005-12-08 02:01:35 +0000 | [diff] [blame] | 331 | } |
Evan Cheng | c9fab31 | 2005-12-08 02:01:35 +0000 | [diff] [blame] | 332 | } |
Evan Cheng | 67ed58e | 2005-12-12 21:49:40 +0000 | [diff] [blame] | 333 | |
| 334 | if (AM.IndexReg.Val) { |
| 335 | SelectIndex = true; |
| 336 | } else { |
| 337 | AM.IndexReg = CurDAG->getRegister(0, MVT::i32); |
| 338 | } |
| 339 | |
| 340 | if (Check) { |
| 341 | unsigned Complexity = 0; |
| 342 | if (AM.Scale > 1) |
| 343 | Complexity++; |
| 344 | if (SelectIndex) |
| 345 | Complexity++; |
| 346 | if (AM.GV) |
| 347 | Complexity++; |
| 348 | else if (AM.Disp > 1) |
| 349 | Complexity++; |
| 350 | if (Complexity <= 1) |
| 351 | return false; |
| 352 | } |
| 353 | |
| 354 | if (SelectBase) |
| 355 | AM.Base.Reg = Select(AM.Base.Reg); |
| 356 | if (SelectIndex) |
| 357 | AM.IndexReg = Select(AM.IndexReg); |
| 358 | |
| 359 | getAddressOperands(AM, Base, Scale, Index, Disp); |
Evan Cheng | c9fab31 | 2005-12-08 02:01:35 +0000 | [diff] [blame] | 360 | return true; |
Evan Cheng | c9fab31 | 2005-12-08 02:01:35 +0000 | [diff] [blame] | 361 | } |
Evan Cheng | 67ed58e | 2005-12-12 21:49:40 +0000 | [diff] [blame] | 362 | return false; |
Evan Cheng | c9fab31 | 2005-12-08 02:01:35 +0000 | [diff] [blame] | 363 | } |
| 364 | |
Evan Cheng | 00fcb00 | 2005-12-15 01:02:48 +0000 | [diff] [blame^] | 365 | /// NodeTransitivelyUsesValue - Return true if N or any of its uses uses Op. |
| 366 | /// The DAG cannot have cycles in it, by definition, so the visited set is not |
| 367 | /// needed to prevent infinite loops. The DAG CAN, however, have unbounded |
| 368 | /// reuse, so it prevents exponential cases. |
| 369 | /// |
| 370 | static bool NodeTransitivelyUsesValue(SDOperand N, SDOperand Op, |
| 371 | std::set<SDNode*> &Visited) { |
| 372 | if (N == Op) return true; // Found it. |
| 373 | SDNode *Node = N.Val; |
| 374 | if (Node->getNumOperands() == 0 || // Leaf? |
| 375 | Node->getNodeDepth() <= Op.getNodeDepth()) return false; // Can't find it? |
| 376 | if (!Visited.insert(Node).second) return false; // Already visited? |
| 377 | |
| 378 | // Recurse for the first N-1 operands. |
| 379 | for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i) |
| 380 | if (NodeTransitivelyUsesValue(Node->getOperand(i), Op, Visited)) |
| 381 | return true; |
| 382 | |
| 383 | // Tail recurse for the last operand. |
| 384 | return NodeTransitivelyUsesValue(Node->getOperand(0), Op, Visited); |
| 385 | } |
| 386 | |
| 387 | /// isFoldableLoad - Return true if this is a load instruction that can safely |
| 388 | /// be folded into an operation that uses it. |
| 389 | bool X86DAGToDAGISel::isFoldableLoad(SDOperand Op, SDOperand OtherOp, |
| 390 | bool FloatPromoteOk) { |
| 391 | if (Op.getOpcode() == ISD::LOAD) { |
| 392 | // FIXME: currently can't fold constant pool indexes. |
| 393 | if (isa<ConstantPoolSDNode>(Op.getOperand(1))) |
| 394 | return false; |
| 395 | } else if (FloatPromoteOk && Op.getOpcode() == ISD::EXTLOAD && |
| 396 | cast<VTSDNode>(Op.getOperand(3))->getVT() == MVT::f32) { |
| 397 | // FIXME: currently can't fold constant pool indexes. |
| 398 | if (isa<ConstantPoolSDNode>(Op.getOperand(1))) |
| 399 | return false; |
| 400 | } else { |
| 401 | return false; |
| 402 | } |
| 403 | |
| 404 | // If this load has already been emitted, we clearly can't fold it. |
| 405 | assert(Op.ResNo == 0 && "Not a use of the value of the load?"); |
| 406 | if (CodeGenMap.count(Op.getValue(1))) return false; |
| 407 | assert(!CodeGenMap.count(Op.getValue(0)) && |
| 408 | "Value in map but not token chain?"); |
| 409 | assert(!CodeGenMap.count(Op.getValue(1)) && |
| 410 | "Token lowered but value not in map?"); |
| 411 | |
| 412 | // If there is not just one use of its value, we cannot fold. |
| 413 | if (!Op.Val->hasNUsesOfValue(1, 0)) return false; |
| 414 | |
| 415 | // Finally, we cannot fold the load into the operation if this would induce a |
| 416 | // cycle into the resultant dag. To check for this, see if OtherOp (the other |
| 417 | // operand of the operation we are folding the load into) can possible use the |
| 418 | // chain node defined by the load. |
| 419 | if (OtherOp.Val && !Op.Val->hasNUsesOfValue(0, 1)) { // Has uses of chain? |
| 420 | std::set<SDNode*> Visited; |
| 421 | if (NodeTransitivelyUsesValue(OtherOp, Op.getValue(1), Visited)) |
| 422 | return false; |
| 423 | } |
| 424 | return true; |
| 425 | } |
| 426 | |
| 427 | SDOperand X86DAGToDAGISel::Select(SDOperand N) { |
| 428 | SDNode *Node = N.Val; |
| 429 | MVT::ValueType NVT = Node->getValueType(0); |
Chris Lattner | 655e7df | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 430 | unsigned Opc; |
| 431 | |
Evan Cheng | 00fcb00 | 2005-12-15 01:02:48 +0000 | [diff] [blame^] | 432 | if (Node->getOpcode() >= ISD::BUILTIN_OP_END) |
| 433 | return N; // Already selected. |
Chris Lattner | 655e7df | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 434 | |
Evan Cheng | 00fcb00 | 2005-12-15 01:02:48 +0000 | [diff] [blame^] | 435 | switch (Node->getOpcode()) { |
Chris Lattner | 655e7df | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 436 | default: break; |
Evan Cheng | 4eb7af9 | 2005-11-30 02:51:20 +0000 | [diff] [blame] | 437 | |
Chris Lattner | 3f0f71b | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 438 | case ISD::SHL: |
Evan Cheng | 00fcb00 | 2005-12-15 01:02:48 +0000 | [diff] [blame^] | 439 | if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Node->getOperand(1))) { |
Evan Cheng | 4b02426 | 2005-12-01 00:43:55 +0000 | [diff] [blame] | 440 | if (CN->getValue() == 1) { |
Evan Cheng | 4eb7af9 | 2005-11-30 02:51:20 +0000 | [diff] [blame] | 441 | // X = SHL Y, 1 -> X = ADD Y, Y |
Evan Cheng | 00fcb00 | 2005-12-15 01:02:48 +0000 | [diff] [blame^] | 442 | switch (NVT) { |
Chris Lattner | 3f0f71b | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 443 | default: assert(0 && "Cannot shift this type!"); |
| 444 | case MVT::i8: Opc = X86::ADD8rr; break; |
| 445 | case MVT::i16: Opc = X86::ADD16rr; break; |
| 446 | case MVT::i32: Opc = X86::ADD32rr; break; |
| 447 | } |
Evan Cheng | 00fcb00 | 2005-12-15 01:02:48 +0000 | [diff] [blame^] | 448 | SDOperand Tmp0 = Select(Node->getOperand(0)); |
| 449 | if (Node->hasOneUse()) |
| 450 | return CurDAG->SelectNodeTo(Node, Opc, NVT, Tmp0, Tmp0); |
| 451 | else |
| 452 | return CodeGenMap[N] = |
| 453 | CurDAG->getTargetNode(Opc, NVT, Tmp0, Tmp0); |
Chris Lattner | 3f0f71b | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 454 | } |
| 455 | } |
Evan Cheng | 4eb7af9 | 2005-11-30 02:51:20 +0000 | [diff] [blame] | 456 | break; |
Chris Lattner | 3f0f71b | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 457 | |
Evan Cheng | 00fcb00 | 2005-12-15 01:02:48 +0000 | [diff] [blame^] | 458 | case ISD::ANY_EXTEND: // treat any extend like zext |
| 459 | case ISD::ZERO_EXTEND: { |
| 460 | SDOperand N0 = N.getOperand(0); |
| 461 | if (N0.getValueType() == MVT::i1) { |
| 462 | // FIXME: This hack is here for zero extension casts from bool to i8. |
| 463 | // This would not be needed if bools were promoted by Legalize. |
| 464 | if (NVT == MVT::i8) { |
| 465 | Opc = X86::MOV8rr; |
| 466 | } else if (!isFoldableLoad(N0, SDOperand())) { |
| 467 | switch (NVT) { |
| 468 | default: assert(0 && "Cannot zero extend to this type!"); |
| 469 | case MVT::i16: Opc = X86::MOVZX16rr8; break; |
| 470 | case MVT::i32: Opc = X86::MOVZX32rr8; break; |
| 471 | } |
| 472 | } else { |
| 473 | switch (NVT) { |
| 474 | default: assert(0 && "Cannot zero extend to this type!"); |
| 475 | case MVT::i16: Opc = X86::MOVZX16rm8; break; |
| 476 | case MVT::i32: Opc = X86::MOVZX32rm8; break; |
| 477 | } |
| 478 | |
| 479 | SDOperand Chain = Select(N0.getOperand(0)); |
| 480 | SDOperand Base, Scale, Index, Disp; |
| 481 | (void) SelectAddr(N0.getOperand(1), Base, Scale, Index, Disp); |
| 482 | SDOperand Result = CurDAG->getTargetNode(Opc, NVT, |
| 483 | MVT::Other, Base, Scale, |
| 484 | Index, Disp, Chain); |
| 485 | CodeGenMap[N.getValue(0)] = Result; |
| 486 | Chain = CodeGenMap[N.getValue(1)] = Result.getValue(1); |
| 487 | return (N.ResNo) ? Chain : Result.getValue(0); |
| 488 | } |
| 489 | |
| 490 | SDOperand Tmp0 = Select(Node->getOperand(0)); |
| 491 | if (Node->hasOneUse()) |
| 492 | return CurDAG->SelectNodeTo(Node, Opc, NVT, Tmp0); |
| 493 | else |
| 494 | return CodeGenMap[N] = CurDAG->getTargetNode(Opc, NVT, Tmp0); |
| 495 | } |
| 496 | // Other cases are autogenerated. |
| 497 | break; |
| 498 | } |
| 499 | |
Chris Lattner | 655e7df | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 500 | case ISD::RET: { |
Evan Cheng | 00fcb00 | 2005-12-15 01:02:48 +0000 | [diff] [blame^] | 501 | SDOperand Chain = Node->getOperand(0); // Token chain. |
| 502 | unsigned NumOps = Node->getNumOperands(); |
Evan Cheng | bfd259a | 2005-12-12 20:32:18 +0000 | [diff] [blame] | 503 | |
| 504 | // Note: A bit of a hack / optimization... Try to delay chain selection |
| 505 | // as much as possible. So it's more likely it has already been selected |
| 506 | // for a real use. |
| 507 | switch (NumOps) { |
Chris Lattner | 655e7df | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 508 | default: |
| 509 | assert(0 && "Unknown return instruction!"); |
| 510 | case 3: |
Evan Cheng | bfd259a | 2005-12-12 20:32:18 +0000 | [diff] [blame] | 511 | Chain = Select(Chain); |
Chris Lattner | 655e7df | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 512 | assert(0 && "Not yet handled return instruction!"); |
| 513 | break; |
| 514 | case 2: { |
Evan Cheng | 00fcb00 | 2005-12-15 01:02:48 +0000 | [diff] [blame^] | 515 | SDOperand Val = Select(Node->getOperand(1)); |
Evan Cheng | bfd259a | 2005-12-12 20:32:18 +0000 | [diff] [blame] | 516 | Chain = Select(Chain); |
Evan Cheng | 00fcb00 | 2005-12-15 01:02:48 +0000 | [diff] [blame^] | 517 | switch (Node->getOperand(1).getValueType()) { |
Chris Lattner | 655e7df | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 518 | default: |
| 519 | assert(0 && "All other types should have been promoted!!"); |
| 520 | case MVT::i32: |
| 521 | Chain = CurDAG->getCopyToReg(Chain, X86::EAX, Val); |
| 522 | break; |
| 523 | case MVT::f32: |
| 524 | case MVT::f64: |
| 525 | assert(0 && "Not yet handled return instruction!"); |
| 526 | break; |
| 527 | } |
| 528 | } |
| 529 | case 1: |
Evan Cheng | bfd259a | 2005-12-12 20:32:18 +0000 | [diff] [blame] | 530 | Chain = Select(Chain); |
Chris Lattner | 655e7df | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 531 | break; |
| 532 | } |
| 533 | if (X86Lowering.getBytesToPopOnReturn() == 0) |
Evan Cheng | 00fcb00 | 2005-12-15 01:02:48 +0000 | [diff] [blame^] | 534 | return CurDAG->SelectNodeTo(Node, X86::RET, MVT::Other, Chain); |
Chris Lattner | 655e7df | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 535 | else |
Evan Cheng | 00fcb00 | 2005-12-15 01:02:48 +0000 | [diff] [blame^] | 536 | return CurDAG->SelectNodeTo(Node, X86::RET, MVT::Other, |
Chris Lattner | af2e037 | 2005-11-30 22:59:19 +0000 | [diff] [blame] | 537 | getI16Imm(X86Lowering.getBytesToPopOnReturn()), |
| 538 | Chain); |
Chris Lattner | 655e7df | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 539 | } |
Chris Lattner | 655e7df | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 540 | } |
| 541 | |
Evan Cheng | 00fcb00 | 2005-12-15 01:02:48 +0000 | [diff] [blame^] | 542 | return SelectCode(N); |
Chris Lattner | 655e7df | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 543 | } |
| 544 | |
| 545 | /// createX86ISelDag - This pass converts a legalized DAG into a |
| 546 | /// X86-specific DAG, ready for instruction scheduling. |
| 547 | /// |
| 548 | FunctionPass *llvm::createX86ISelDag(TargetMachine &TM) { |
| 549 | return new X86DAGToDAGISel(TM); |
| 550 | } |