Chris Lattner | 7a12537 | 2005-11-16 22:59:19 +0000 | [diff] [blame] | 1 | //===- X86ISelDAGToDAG.cpp - A DAG pattern matching inst selector for X86 -===// |
Chris Lattner | c961eea | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by the Evan Cheng and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file defines a DAG pattern matching instruction selector for X86, |
| 11 | // converting from a legalized dag to a X86 dag. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "X86.h" |
| 16 | #include "X86Subtarget.h" |
| 17 | #include "X86ISelLowering.h" |
Chris Lattner | f9ce9fb | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 18 | #include "llvm/GlobalValue.h" |
| 19 | #include "llvm/CodeGen/MachineConstantPool.h" |
Chris Lattner | c961eea | 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" |
| 25 | using namespace llvm; |
| 26 | |
| 27 | //===----------------------------------------------------------------------===// |
| 28 | // Pattern Matcher Implementation |
| 29 | //===----------------------------------------------------------------------===// |
| 30 | |
| 31 | namespace { |
Chris Lattner | f9ce9fb | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 32 | /// X86ISelAddressMode - This corresponds to X86AddressMode, but uses |
| 33 | /// SDOperand's instead of register numbers for the leaves of the matched |
| 34 | /// tree. |
| 35 | struct X86ISelAddressMode { |
| 36 | enum { |
| 37 | RegBase, |
| 38 | FrameIndexBase, |
Evan Cheng | ec693f7 | 2005-12-08 02:01:35 +0000 | [diff] [blame] | 39 | ConstantPoolBase |
Chris Lattner | f9ce9fb | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 40 | } BaseType; |
| 41 | |
| 42 | struct { // This is really a union, discriminated by BaseType! |
| 43 | SDOperand Reg; |
| 44 | int FrameIndex; |
| 45 | } Base; |
| 46 | |
| 47 | unsigned Scale; |
| 48 | SDOperand IndexReg; |
| 49 | unsigned Disp; |
| 50 | GlobalValue *GV; |
| 51 | |
| 52 | X86ISelAddressMode() |
Evan Cheng | bd3d25c | 2005-11-30 02:51:20 +0000 | [diff] [blame] | 53 | : BaseType(RegBase), Scale(1), IndexReg(), Disp(0), GV(0) { |
Chris Lattner | f9ce9fb | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 54 | } |
| 55 | }; |
| 56 | } |
| 57 | |
| 58 | namespace { |
Chris Lattner | c961eea | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 59 | Statistic<> |
| 60 | NumFPKill("x86-codegen", "Number of FP_REG_KILL instructions added"); |
| 61 | |
| 62 | //===--------------------------------------------------------------------===// |
| 63 | /// ISel - X86 specific code to select X86 machine instructions for |
| 64 | /// SelectionDAG operations. |
| 65 | /// |
| 66 | class X86DAGToDAGISel : public SelectionDAGISel { |
| 67 | /// ContainsFPCode - Every instruction we select that uses or defines a FP |
| 68 | /// register should set this to true. |
| 69 | bool ContainsFPCode; |
| 70 | |
| 71 | /// X86Lowering - This object fully describes how to lower LLVM code to an |
| 72 | /// X86-specific SelectionDAG. |
| 73 | X86TargetLowering X86Lowering; |
| 74 | |
| 75 | /// Subtarget - Keep a pointer to the X86Subtarget around so that we can |
| 76 | /// make the right decision when generating code for different targets. |
| 77 | const X86Subtarget *Subtarget; |
| 78 | public: |
| 79 | X86DAGToDAGISel(TargetMachine &TM) |
| 80 | : SelectionDAGISel(X86Lowering), X86Lowering(TM) { |
| 81 | Subtarget = &TM.getSubtarget<X86Subtarget>(); |
| 82 | } |
| 83 | |
| 84 | virtual const char *getPassName() const { |
| 85 | return "X86 DAG->DAG Instruction Selection"; |
| 86 | } |
| 87 | |
| 88 | /// InstructionSelectBasicBlock - This callback is invoked by |
| 89 | /// SelectionDAGISel when it has created a SelectionDAG for us to codegen. |
| 90 | virtual void InstructionSelectBasicBlock(SelectionDAG &DAG); |
| 91 | |
| 92 | // Include the pieces autogenerated from the target description. |
| 93 | #include "X86GenDAGISel.inc" |
| 94 | |
| 95 | private: |
| 96 | SDOperand Select(SDOperand N); |
| 97 | |
Chris Lattner | f9ce9fb | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 98 | bool MatchAddress(SDOperand N, X86ISelAddressMode &AM); |
Evan Cheng | ec693f7 | 2005-12-08 02:01:35 +0000 | [diff] [blame] | 99 | bool SelectAddr(SDOperand N, SDOperand &Base, SDOperand &Scale, |
| 100 | SDOperand &Index, SDOperand &Disp); |
| 101 | bool SelectLEAAddr(SDOperand N, SDOperand &Base, SDOperand &Scale, |
| 102 | SDOperand &Index, SDOperand &Disp); |
Chris Lattner | f9ce9fb | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 103 | |
Evan Cheng | e528053 | 2005-12-12 21:49:40 +0000 | [diff] [blame] | 104 | inline void getAddressOperands(X86ISelAddressMode &AM, SDOperand &Base, |
| 105 | SDOperand &Scale, SDOperand &Index, |
| 106 | SDOperand &Disp) { |
| 107 | Base = (AM.BaseType == X86ISelAddressMode::FrameIndexBase) ? |
| 108 | CurDAG->getTargetFrameIndex(AM.Base.FrameIndex, MVT::i32) : AM.Base.Reg; |
Evan Cheng | bdce7b4 | 2005-12-17 09:13:43 +0000 | [diff] [blame] | 109 | Scale = getI8Imm(AM.Scale); |
Evan Cheng | e528053 | 2005-12-12 21:49:40 +0000 | [diff] [blame] | 110 | Index = AM.IndexReg; |
| 111 | Disp = AM.GV ? CurDAG->getTargetGlobalAddress(AM.GV, MVT::i32, AM.Disp) |
| 112 | : getI32Imm(AM.Disp); |
| 113 | } |
| 114 | |
Chris Lattner | f9ce9fb | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 115 | /// getI8Imm - Return a target constant with the specified value, of type |
| 116 | /// i8. |
| 117 | inline SDOperand getI8Imm(unsigned Imm) { |
| 118 | return CurDAG->getTargetConstant(Imm, MVT::i8); |
| 119 | } |
| 120 | |
Chris Lattner | c961eea | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 121 | /// getI16Imm - Return a target constant with the specified value, of type |
| 122 | /// i16. |
| 123 | inline SDOperand getI16Imm(unsigned Imm) { |
| 124 | return CurDAG->getTargetConstant(Imm, MVT::i16); |
| 125 | } |
| 126 | |
| 127 | /// getI32Imm - Return a target constant with the specified value, of type |
| 128 | /// i32. |
| 129 | inline SDOperand getI32Imm(unsigned Imm) { |
| 130 | return CurDAG->getTargetConstant(Imm, MVT::i32); |
| 131 | } |
| 132 | }; |
| 133 | } |
| 134 | |
| 135 | /// InstructionSelectBasicBlock - This callback is invoked by SelectionDAGISel |
| 136 | /// when it has created a SelectionDAG for us to codegen. |
| 137 | void X86DAGToDAGISel::InstructionSelectBasicBlock(SelectionDAG &DAG) { |
| 138 | DEBUG(BB->dump()); |
| 139 | |
| 140 | // Codegen the basic block. |
| 141 | DAG.setRoot(Select(DAG.getRoot())); |
Evan Cheng | fcaa995 | 2005-12-19 22:36:02 +0000 | [diff] [blame] | 142 | CodeGenMap.clear(); |
Chris Lattner | c961eea | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 143 | DAG.RemoveDeadNodes(); |
| 144 | |
| 145 | // Emit machine code to BB. |
| 146 | ScheduleAndEmitDAG(DAG); |
| 147 | } |
| 148 | |
Chris Lattner | f9ce9fb | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 149 | /// FIXME: copied from X86ISelPattern.cpp |
| 150 | /// MatchAddress - Add the specified node to the specified addressing mode, |
| 151 | /// returning true if it cannot be done. This just pattern matches for the |
| 152 | /// addressing mode |
| 153 | bool X86DAGToDAGISel::MatchAddress(SDOperand N, X86ISelAddressMode &AM) { |
| 154 | switch (N.getOpcode()) { |
| 155 | default: break; |
| 156 | case ISD::FrameIndex: |
| 157 | if (AM.BaseType == X86ISelAddressMode::RegBase && AM.Base.Reg.Val == 0) { |
| 158 | AM.BaseType = X86ISelAddressMode::FrameIndexBase; |
| 159 | AM.Base.FrameIndex = cast<FrameIndexSDNode>(N)->getIndex(); |
| 160 | return false; |
| 161 | } |
| 162 | break; |
Evan Cheng | ec693f7 | 2005-12-08 02:01:35 +0000 | [diff] [blame] | 163 | |
| 164 | case ISD::ConstantPool: |
| 165 | if (AM.BaseType == X86ISelAddressMode::RegBase && AM.Base.Reg.Val == 0) { |
| 166 | if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N)) { |
| 167 | AM.BaseType = X86ISelAddressMode::ConstantPoolBase; |
| 168 | AM.Base.Reg = CurDAG->getTargetConstantPool(CP->get(), MVT::i32); |
| 169 | return false; |
| 170 | } |
| 171 | } |
| 172 | break; |
| 173 | |
Chris Lattner | f9ce9fb | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 174 | case ISD::GlobalAddress: |
| 175 | if (AM.GV == 0) { |
Evan Cheng | b077b84 | 2005-12-21 02:39:21 +0000 | [diff] [blame^] | 176 | AM.GV = cast<GlobalAddressSDNode>(N)->getGlobal(); |
Evan Cheng | bdce7b4 | 2005-12-17 09:13:43 +0000 | [diff] [blame] | 177 | return false; |
Chris Lattner | f9ce9fb | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 178 | } |
| 179 | break; |
Evan Cheng | ec693f7 | 2005-12-08 02:01:35 +0000 | [diff] [blame] | 180 | |
Chris Lattner | f9ce9fb | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 181 | case ISD::Constant: |
| 182 | AM.Disp += cast<ConstantSDNode>(N)->getValue(); |
| 183 | return false; |
Evan Cheng | ec693f7 | 2005-12-08 02:01:35 +0000 | [diff] [blame] | 184 | |
Chris Lattner | f9ce9fb | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 185 | case ISD::SHL: |
| 186 | if (AM.IndexReg.Val == 0 && AM.Scale == 1) |
| 187 | if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1))) { |
| 188 | unsigned Val = CN->getValue(); |
| 189 | if (Val == 1 || Val == 2 || Val == 3) { |
| 190 | AM.Scale = 1 << Val; |
| 191 | SDOperand ShVal = N.Val->getOperand(0); |
| 192 | |
| 193 | // Okay, we know that we have a scale by now. However, if the scaled |
| 194 | // value is an add of something and a constant, we can fold the |
| 195 | // constant into the disp field here. |
| 196 | if (ShVal.Val->getOpcode() == ISD::ADD && ShVal.hasOneUse() && |
| 197 | isa<ConstantSDNode>(ShVal.Val->getOperand(1))) { |
| 198 | AM.IndexReg = ShVal.Val->getOperand(0); |
| 199 | ConstantSDNode *AddVal = |
| 200 | cast<ConstantSDNode>(ShVal.Val->getOperand(1)); |
| 201 | AM.Disp += AddVal->getValue() << Val; |
| 202 | } else { |
| 203 | AM.IndexReg = ShVal; |
| 204 | } |
| 205 | return false; |
| 206 | } |
| 207 | } |
| 208 | break; |
Evan Cheng | ec693f7 | 2005-12-08 02:01:35 +0000 | [diff] [blame] | 209 | |
Chris Lattner | f9ce9fb | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 210 | case ISD::MUL: |
| 211 | // X*[3,5,9] -> X+X*[2,4,8] |
| 212 | if (AM.IndexReg.Val == 0 && AM.BaseType == X86ISelAddressMode::RegBase && |
| 213 | AM.Base.Reg.Val == 0) |
| 214 | if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1))) |
| 215 | if (CN->getValue() == 3 || CN->getValue() == 5 || CN->getValue() == 9) { |
| 216 | AM.Scale = unsigned(CN->getValue())-1; |
| 217 | |
| 218 | SDOperand MulVal = N.Val->getOperand(0); |
| 219 | SDOperand Reg; |
| 220 | |
| 221 | // Okay, we know that we have a scale by now. However, if the scaled |
| 222 | // value is an add of something and a constant, we can fold the |
| 223 | // constant into the disp field here. |
| 224 | if (MulVal.Val->getOpcode() == ISD::ADD && MulVal.hasOneUse() && |
| 225 | isa<ConstantSDNode>(MulVal.Val->getOperand(1))) { |
| 226 | Reg = MulVal.Val->getOperand(0); |
| 227 | ConstantSDNode *AddVal = |
| 228 | cast<ConstantSDNode>(MulVal.Val->getOperand(1)); |
| 229 | AM.Disp += AddVal->getValue() * CN->getValue(); |
| 230 | } else { |
| 231 | Reg = N.Val->getOperand(0); |
| 232 | } |
| 233 | |
| 234 | AM.IndexReg = AM.Base.Reg = Reg; |
| 235 | return false; |
| 236 | } |
| 237 | break; |
| 238 | |
| 239 | case ISD::ADD: { |
| 240 | X86ISelAddressMode Backup = AM; |
| 241 | if (!MatchAddress(N.Val->getOperand(0), AM) && |
| 242 | !MatchAddress(N.Val->getOperand(1), AM)) |
| 243 | return false; |
| 244 | AM = Backup; |
| 245 | if (!MatchAddress(N.Val->getOperand(1), AM) && |
| 246 | !MatchAddress(N.Val->getOperand(0), AM)) |
| 247 | return false; |
| 248 | AM = Backup; |
| 249 | break; |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | // Is the base register already occupied? |
| 254 | if (AM.BaseType != X86ISelAddressMode::RegBase || AM.Base.Reg.Val) { |
| 255 | // If so, check to see if the scale index register is set. |
| 256 | if (AM.IndexReg.Val == 0) { |
| 257 | AM.IndexReg = N; |
| 258 | AM.Scale = 1; |
| 259 | return false; |
| 260 | } |
| 261 | |
| 262 | // Otherwise, we cannot select it. |
| 263 | return true; |
| 264 | } |
| 265 | |
| 266 | // Default, generate it as a register. |
| 267 | AM.BaseType = X86ISelAddressMode::RegBase; |
| 268 | AM.Base.Reg = N; |
| 269 | return false; |
| 270 | } |
| 271 | |
Evan Cheng | ec693f7 | 2005-12-08 02:01:35 +0000 | [diff] [blame] | 272 | /// SelectAddr - returns true if it is able pattern match an addressing mode. |
| 273 | /// It returns the operands which make up the maximal addressing mode it can |
| 274 | /// match by reference. |
| 275 | bool X86DAGToDAGISel::SelectAddr(SDOperand N, SDOperand &Base, SDOperand &Scale, |
| 276 | SDOperand &Index, SDOperand &Disp) { |
| 277 | X86ISelAddressMode AM; |
| 278 | if (!MatchAddress(N, AM)) { |
| 279 | if (AM.BaseType == X86ISelAddressMode::RegBase) { |
| 280 | if (AM.Base.Reg.Val) |
| 281 | AM.Base.Reg = Select(AM.Base.Reg); |
| 282 | else |
| 283 | AM.Base.Reg = CurDAG->getRegister(0, MVT::i32); |
| 284 | } |
| 285 | if (AM.IndexReg.Val) |
| 286 | AM.IndexReg = Select(AM.IndexReg); |
| 287 | else |
| 288 | AM.IndexReg = CurDAG->getRegister(0, MVT::i32); |
| 289 | |
Evan Cheng | e528053 | 2005-12-12 21:49:40 +0000 | [diff] [blame] | 290 | getAddressOperands(AM, Base, Scale, Index, Disp); |
Evan Cheng | ec693f7 | 2005-12-08 02:01:35 +0000 | [diff] [blame] | 291 | return true; |
| 292 | } |
| 293 | return false; |
| 294 | } |
| 295 | |
| 296 | static bool isRegister0(SDOperand Op) |
| 297 | { |
| 298 | if (RegisterSDNode *R = dyn_cast<RegisterSDNode>(Op)) |
| 299 | return (R->getReg() == 0); |
| 300 | return false; |
| 301 | } |
| 302 | |
| 303 | /// SelectLEAAddr - it calls SelectAddr and determines if the maximal addressing |
| 304 | /// mode it matches can be cost effectively emitted as an LEA instruction. |
| 305 | /// For X86, it always is unless it's just a (Reg + const). |
| 306 | bool X86DAGToDAGISel::SelectLEAAddr(SDOperand N, SDOperand &Base, SDOperand &Scale, |
| 307 | SDOperand &Index, SDOperand &Disp) { |
Evan Cheng | e528053 | 2005-12-12 21:49:40 +0000 | [diff] [blame] | 308 | X86ISelAddressMode AM; |
| 309 | if (!MatchAddress(N, AM)) { |
| 310 | bool SelectBase = false; |
| 311 | bool SelectIndex = false; |
| 312 | bool Check = false; |
| 313 | if (AM.BaseType == X86ISelAddressMode::RegBase) { |
| 314 | if (AM.Base.Reg.Val) { |
| 315 | Check = true; |
| 316 | SelectBase = true; |
Evan Cheng | ec693f7 | 2005-12-08 02:01:35 +0000 | [diff] [blame] | 317 | } else { |
Evan Cheng | e528053 | 2005-12-12 21:49:40 +0000 | [diff] [blame] | 318 | AM.Base.Reg = CurDAG->getRegister(0, MVT::i32); |
Evan Cheng | ec693f7 | 2005-12-08 02:01:35 +0000 | [diff] [blame] | 319 | } |
Evan Cheng | ec693f7 | 2005-12-08 02:01:35 +0000 | [diff] [blame] | 320 | } |
Evan Cheng | e528053 | 2005-12-12 21:49:40 +0000 | [diff] [blame] | 321 | |
| 322 | if (AM.IndexReg.Val) { |
| 323 | SelectIndex = true; |
| 324 | } else { |
| 325 | AM.IndexReg = CurDAG->getRegister(0, MVT::i32); |
| 326 | } |
| 327 | |
| 328 | if (Check) { |
| 329 | unsigned Complexity = 0; |
| 330 | if (AM.Scale > 1) |
| 331 | Complexity++; |
| 332 | if (SelectIndex) |
| 333 | Complexity++; |
| 334 | if (AM.GV) |
| 335 | Complexity++; |
| 336 | else if (AM.Disp > 1) |
| 337 | Complexity++; |
| 338 | if (Complexity <= 1) |
| 339 | return false; |
| 340 | } |
| 341 | |
| 342 | if (SelectBase) |
| 343 | AM.Base.Reg = Select(AM.Base.Reg); |
| 344 | if (SelectIndex) |
| 345 | AM.IndexReg = Select(AM.IndexReg); |
| 346 | |
| 347 | getAddressOperands(AM, Base, Scale, Index, Disp); |
Evan Cheng | ec693f7 | 2005-12-08 02:01:35 +0000 | [diff] [blame] | 348 | return true; |
Evan Cheng | ec693f7 | 2005-12-08 02:01:35 +0000 | [diff] [blame] | 349 | } |
Evan Cheng | e528053 | 2005-12-12 21:49:40 +0000 | [diff] [blame] | 350 | return false; |
Evan Cheng | ec693f7 | 2005-12-08 02:01:35 +0000 | [diff] [blame] | 351 | } |
| 352 | |
Evan Cheng | def941b | 2005-12-15 01:02:48 +0000 | [diff] [blame] | 353 | SDOperand X86DAGToDAGISel::Select(SDOperand N) { |
| 354 | SDNode *Node = N.Val; |
| 355 | MVT::ValueType NVT = Node->getValueType(0); |
Chris Lattner | c961eea | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 356 | unsigned Opc; |
| 357 | |
Evan Cheng | aed7c72 | 2005-12-17 01:24:02 +0000 | [diff] [blame] | 358 | if (Node->getOpcode() >= ISD::BUILTIN_OP_END && |
| 359 | Node->getOpcode() < X86ISD::FIRST_NUMBER) |
Evan Cheng | def941b | 2005-12-15 01:02:48 +0000 | [diff] [blame] | 360 | return N; // Already selected. |
Chris Lattner | c961eea | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 361 | |
Evan Cheng | def941b | 2005-12-15 01:02:48 +0000 | [diff] [blame] | 362 | switch (Node->getOpcode()) { |
Chris Lattner | c961eea | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 363 | default: break; |
Evan Cheng | bd3d25c | 2005-11-30 02:51:20 +0000 | [diff] [blame] | 364 | |
Chris Lattner | f9ce9fb | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 365 | case ISD::SHL: |
Evan Cheng | def941b | 2005-12-15 01:02:48 +0000 | [diff] [blame] | 366 | if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Node->getOperand(1))) { |
Evan Cheng | 640f299 | 2005-12-01 00:43:55 +0000 | [diff] [blame] | 367 | if (CN->getValue() == 1) { |
Evan Cheng | bd3d25c | 2005-11-30 02:51:20 +0000 | [diff] [blame] | 368 | // X = SHL Y, 1 -> X = ADD Y, Y |
Evan Cheng | def941b | 2005-12-15 01:02:48 +0000 | [diff] [blame] | 369 | switch (NVT) { |
Chris Lattner | f9ce9fb | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 370 | default: assert(0 && "Cannot shift this type!"); |
| 371 | case MVT::i8: Opc = X86::ADD8rr; break; |
| 372 | case MVT::i16: Opc = X86::ADD16rr; break; |
| 373 | case MVT::i32: Opc = X86::ADD32rr; break; |
| 374 | } |
Evan Cheng | def941b | 2005-12-15 01:02:48 +0000 | [diff] [blame] | 375 | SDOperand Tmp0 = Select(Node->getOperand(0)); |
| 376 | if (Node->hasOneUse()) |
| 377 | return CurDAG->SelectNodeTo(Node, Opc, NVT, Tmp0, Tmp0); |
| 378 | else |
| 379 | return CodeGenMap[N] = |
| 380 | CurDAG->getTargetNode(Opc, NVT, Tmp0, Tmp0); |
Chris Lattner | f9ce9fb | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 381 | } |
| 382 | } |
Evan Cheng | bd3d25c | 2005-11-30 02:51:20 +0000 | [diff] [blame] | 383 | break; |
Chris Lattner | f9ce9fb | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 384 | |
Evan Cheng | 45f37bc | 2005-12-17 02:02:50 +0000 | [diff] [blame] | 385 | case ISD::TRUNCATE: { |
| 386 | unsigned Reg; |
| 387 | MVT::ValueType VT; |
| 388 | switch (Node->getOperand(0).getValueType()) { |
| 389 | default: assert(0 && "Unknown truncate!"); |
| 390 | case MVT::i16: Reg = X86::AX; Opc = X86::MOV16rr; VT = MVT::i16; break; |
| 391 | case MVT::i32: Reg = X86::EAX; Opc = X86::MOV32rr; VT = MVT::i32; break; |
| 392 | } |
| 393 | SDOperand Tmp0 = Select(Node->getOperand(0)); |
| 394 | SDOperand Tmp1 = CurDAG->getTargetNode(Opc, VT, Tmp0); |
| 395 | SDOperand InFlag = SDOperand(0,0); |
| 396 | SDOperand Result = CurDAG->getCopyToReg(CurDAG->getEntryNode(), |
| 397 | Reg, Tmp1, InFlag).getValue(1); |
| 398 | SDOperand Chain = Result.getValue(0); |
| 399 | InFlag = Result.getValue(1); |
| 400 | |
| 401 | switch (NVT) { |
| 402 | default: assert(0 && "Unknown truncate!"); |
| 403 | case MVT::i8: Reg = X86::AL; Opc = X86::MOV8rr; VT = MVT::i8; break; |
| 404 | case MVT::i16: Reg = X86::AX; Opc = X86::MOV16rr; VT = MVT::i16; break; |
| 405 | } |
| 406 | |
| 407 | Result = CurDAG->getCopyFromReg(Chain, |
| 408 | Reg, VT, InFlag); |
| 409 | return CodeGenMap[N] = CurDAG->getTargetNode(Opc, VT, Result); |
| 410 | break; |
| 411 | } |
| 412 | |
Chris Lattner | c961eea | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 413 | case ISD::RET: { |
Evan Cheng | def941b | 2005-12-15 01:02:48 +0000 | [diff] [blame] | 414 | SDOperand Chain = Node->getOperand(0); // Token chain. |
| 415 | unsigned NumOps = Node->getNumOperands(); |
Evan Cheng | cbd6ed4 | 2005-12-12 20:32:18 +0000 | [diff] [blame] | 416 | |
| 417 | // Note: A bit of a hack / optimization... Try to delay chain selection |
| 418 | // as much as possible. So it's more likely it has already been selected |
| 419 | // for a real use. |
| 420 | switch (NumOps) { |
Chris Lattner | c961eea | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 421 | default: |
| 422 | assert(0 && "Unknown return instruction!"); |
| 423 | case 3: |
Evan Cheng | cbd6ed4 | 2005-12-12 20:32:18 +0000 | [diff] [blame] | 424 | Chain = Select(Chain); |
Chris Lattner | c961eea | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 425 | assert(0 && "Not yet handled return instruction!"); |
| 426 | break; |
| 427 | case 2: { |
Evan Cheng | def941b | 2005-12-15 01:02:48 +0000 | [diff] [blame] | 428 | SDOperand Val = Select(Node->getOperand(1)); |
Evan Cheng | cbd6ed4 | 2005-12-12 20:32:18 +0000 | [diff] [blame] | 429 | Chain = Select(Chain); |
Evan Cheng | def941b | 2005-12-15 01:02:48 +0000 | [diff] [blame] | 430 | switch (Node->getOperand(1).getValueType()) { |
Chris Lattner | c961eea | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 431 | default: |
| 432 | assert(0 && "All other types should have been promoted!!"); |
| 433 | case MVT::i32: |
| 434 | Chain = CurDAG->getCopyToReg(Chain, X86::EAX, Val); |
| 435 | break; |
| 436 | case MVT::f32: |
| 437 | case MVT::f64: |
| 438 | assert(0 && "Not yet handled return instruction!"); |
| 439 | break; |
| 440 | } |
| 441 | } |
| 442 | case 1: |
Evan Cheng | cbd6ed4 | 2005-12-12 20:32:18 +0000 | [diff] [blame] | 443 | Chain = Select(Chain); |
Chris Lattner | c961eea | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 444 | break; |
| 445 | } |
| 446 | if (X86Lowering.getBytesToPopOnReturn() == 0) |
Evan Cheng | def941b | 2005-12-15 01:02:48 +0000 | [diff] [blame] | 447 | return CurDAG->SelectNodeTo(Node, X86::RET, MVT::Other, Chain); |
Chris Lattner | c961eea | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 448 | else |
Evan Cheng | def941b | 2005-12-15 01:02:48 +0000 | [diff] [blame] | 449 | return CurDAG->SelectNodeTo(Node, X86::RET, MVT::Other, |
Chris Lattner | 350d22e | 2005-11-30 22:59:19 +0000 | [diff] [blame] | 450 | getI16Imm(X86Lowering.getBytesToPopOnReturn()), |
| 451 | Chain); |
Chris Lattner | c961eea | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 452 | } |
Chris Lattner | c961eea | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 453 | } |
| 454 | |
Evan Cheng | def941b | 2005-12-15 01:02:48 +0000 | [diff] [blame] | 455 | return SelectCode(N); |
Chris Lattner | c961eea | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 456 | } |
| 457 | |
| 458 | /// createX86ISelDag - This pass converts a legalized DAG into a |
| 459 | /// X86-specific DAG, ready for instruction scheduling. |
| 460 | /// |
| 461 | FunctionPass *llvm::createX86ISelDag(TargetMachine &TM) { |
| 462 | return new X86DAGToDAGISel(TM); |
| 463 | } |