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" |
Evan Cheng | 8700e14 | 2006-01-11 06:09:51 +0000 | [diff] [blame] | 16 | #include "X86InstrBuilder.h" |
Chris Lattner | 92cb0af | 2006-01-11 01:15:34 +0000 | [diff] [blame] | 17 | #include "X86RegisterInfo.h" |
Chris Lattner | c961eea | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 18 | #include "X86Subtarget.h" |
| 19 | #include "X86ISelLowering.h" |
Chris Lattner | f9ce9fb | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 20 | #include "llvm/GlobalValue.h" |
Chris Lattner | 92cb0af | 2006-01-11 01:15:34 +0000 | [diff] [blame] | 21 | #include "llvm/Instructions.h" |
| 22 | #include "llvm/Support/CFG.h" |
Chris Lattner | f9ce9fb | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 23 | #include "llvm/CodeGen/MachineConstantPool.h" |
Chris Lattner | c961eea | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 24 | #include "llvm/CodeGen/MachineFunction.h" |
Evan Cheng | aaca22c | 2006-01-10 20:26:56 +0000 | [diff] [blame] | 25 | #include "llvm/CodeGen/MachineFrameInfo.h" |
Chris Lattner | 92cb0af | 2006-01-11 01:15:34 +0000 | [diff] [blame] | 26 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
| 27 | #include "llvm/CodeGen/SSARegMap.h" |
Chris Lattner | c961eea | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 28 | #include "llvm/CodeGen/SelectionDAGISel.h" |
| 29 | #include "llvm/Target/TargetMachine.h" |
| 30 | #include "llvm/Support/Debug.h" |
| 31 | #include "llvm/ADT/Statistic.h" |
Chris Lattner | 2c2c6c6 | 2006-01-22 23:41:00 +0000 | [diff] [blame] | 32 | #include <iostream> |
Evan Cheng | ba2f0a9 | 2006-02-05 06:46:41 +0000 | [diff] [blame] | 33 | #include <set> |
Chris Lattner | c961eea | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 34 | using namespace llvm; |
| 35 | |
| 36 | //===----------------------------------------------------------------------===// |
| 37 | // Pattern Matcher Implementation |
| 38 | //===----------------------------------------------------------------------===// |
| 39 | |
| 40 | namespace { |
Chris Lattner | f9ce9fb | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 41 | /// X86ISelAddressMode - This corresponds to X86AddressMode, but uses |
| 42 | /// SDOperand's instead of register numbers for the leaves of the matched |
| 43 | /// tree. |
| 44 | struct X86ISelAddressMode { |
| 45 | enum { |
| 46 | RegBase, |
| 47 | FrameIndexBase, |
Evan Cheng | ec693f7 | 2005-12-08 02:01:35 +0000 | [diff] [blame] | 48 | ConstantPoolBase |
Chris Lattner | f9ce9fb | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 49 | } BaseType; |
| 50 | |
| 51 | struct { // This is really a union, discriminated by BaseType! |
| 52 | SDOperand Reg; |
| 53 | int FrameIndex; |
| 54 | } Base; |
| 55 | |
| 56 | unsigned Scale; |
| 57 | SDOperand IndexReg; |
| 58 | unsigned Disp; |
| 59 | GlobalValue *GV; |
| 60 | |
| 61 | X86ISelAddressMode() |
Evan Cheng | bd3d25c | 2005-11-30 02:51:20 +0000 | [diff] [blame] | 62 | : BaseType(RegBase), Scale(1), IndexReg(), Disp(0), GV(0) { |
Chris Lattner | f9ce9fb | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 63 | } |
| 64 | }; |
| 65 | } |
| 66 | |
| 67 | namespace { |
Chris Lattner | c961eea | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 68 | Statistic<> |
| 69 | NumFPKill("x86-codegen", "Number of FP_REG_KILL instructions added"); |
| 70 | |
| 71 | //===--------------------------------------------------------------------===// |
| 72 | /// ISel - X86 specific code to select X86 machine instructions for |
| 73 | /// SelectionDAG operations. |
| 74 | /// |
| 75 | class X86DAGToDAGISel : public SelectionDAGISel { |
| 76 | /// ContainsFPCode - Every instruction we select that uses or defines a FP |
| 77 | /// register should set this to true. |
| 78 | bool ContainsFPCode; |
| 79 | |
| 80 | /// X86Lowering - This object fully describes how to lower LLVM code to an |
| 81 | /// X86-specific SelectionDAG. |
| 82 | X86TargetLowering X86Lowering; |
| 83 | |
| 84 | /// Subtarget - Keep a pointer to the X86Subtarget around so that we can |
| 85 | /// make the right decision when generating code for different targets. |
| 86 | const X86Subtarget *Subtarget; |
| 87 | public: |
| 88 | X86DAGToDAGISel(TargetMachine &TM) |
| 89 | : SelectionDAGISel(X86Lowering), X86Lowering(TM) { |
| 90 | Subtarget = &TM.getSubtarget<X86Subtarget>(); |
| 91 | } |
| 92 | |
| 93 | virtual const char *getPassName() const { |
| 94 | return "X86 DAG->DAG Instruction Selection"; |
| 95 | } |
| 96 | |
| 97 | /// InstructionSelectBasicBlock - This callback is invoked by |
| 98 | /// SelectionDAGISel when it has created a SelectionDAG for us to codegen. |
| 99 | virtual void InstructionSelectBasicBlock(SelectionDAG &DAG); |
| 100 | |
Evan Cheng | 8700e14 | 2006-01-11 06:09:51 +0000 | [diff] [blame] | 101 | virtual void EmitFunctionEntryCode(Function &Fn, MachineFunction &MF); |
| 102 | |
Chris Lattner | c961eea | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 103 | // Include the pieces autogenerated from the target description. |
| 104 | #include "X86GenDAGISel.inc" |
| 105 | |
| 106 | private: |
Evan Cheng | 3416721 | 2006-02-09 00:37:58 +0000 | [diff] [blame] | 107 | void Select(SDOperand &Result, SDOperand N); |
Chris Lattner | c961eea | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 108 | |
Chris Lattner | f9ce9fb | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 109 | bool MatchAddress(SDOperand N, X86ISelAddressMode &AM); |
Evan Cheng | ec693f7 | 2005-12-08 02:01:35 +0000 | [diff] [blame] | 110 | bool SelectAddr(SDOperand N, SDOperand &Base, SDOperand &Scale, |
| 111 | SDOperand &Index, SDOperand &Disp); |
| 112 | bool SelectLEAAddr(SDOperand N, SDOperand &Base, SDOperand &Scale, |
| 113 | SDOperand &Index, SDOperand &Disp); |
Evan Cheng | 5e35168 | 2006-02-06 06:02:33 +0000 | [diff] [blame] | 114 | bool TryFoldLoad(SDOperand P, SDOperand N, |
| 115 | SDOperand &Base, SDOperand &Scale, |
Evan Cheng | 0114e94 | 2006-01-06 20:36:21 +0000 | [diff] [blame] | 116 | SDOperand &Index, SDOperand &Disp); |
Chris Lattner | f9ce9fb | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 117 | |
Evan Cheng | e528053 | 2005-12-12 21:49:40 +0000 | [diff] [blame] | 118 | inline void getAddressOperands(X86ISelAddressMode &AM, SDOperand &Base, |
| 119 | SDOperand &Scale, SDOperand &Index, |
| 120 | SDOperand &Disp) { |
| 121 | Base = (AM.BaseType == X86ISelAddressMode::FrameIndexBase) ? |
| 122 | CurDAG->getTargetFrameIndex(AM.Base.FrameIndex, MVT::i32) : AM.Base.Reg; |
Evan Cheng | bdce7b4 | 2005-12-17 09:13:43 +0000 | [diff] [blame] | 123 | Scale = getI8Imm(AM.Scale); |
Evan Cheng | e528053 | 2005-12-12 21:49:40 +0000 | [diff] [blame] | 124 | Index = AM.IndexReg; |
| 125 | Disp = AM.GV ? CurDAG->getTargetGlobalAddress(AM.GV, MVT::i32, AM.Disp) |
| 126 | : getI32Imm(AM.Disp); |
| 127 | } |
| 128 | |
Chris Lattner | f9ce9fb | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 129 | /// getI8Imm - Return a target constant with the specified value, of type |
| 130 | /// i8. |
| 131 | inline SDOperand getI8Imm(unsigned Imm) { |
| 132 | return CurDAG->getTargetConstant(Imm, MVT::i8); |
| 133 | } |
| 134 | |
Chris Lattner | c961eea | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 135 | /// getI16Imm - Return a target constant with the specified value, of type |
| 136 | /// i16. |
| 137 | inline SDOperand getI16Imm(unsigned Imm) { |
| 138 | return CurDAG->getTargetConstant(Imm, MVT::i16); |
| 139 | } |
| 140 | |
| 141 | /// getI32Imm - Return a target constant with the specified value, of type |
| 142 | /// i32. |
| 143 | inline SDOperand getI32Imm(unsigned Imm) { |
| 144 | return CurDAG->getTargetConstant(Imm, MVT::i32); |
| 145 | } |
| 146 | }; |
| 147 | } |
| 148 | |
| 149 | /// InstructionSelectBasicBlock - This callback is invoked by SelectionDAGISel |
| 150 | /// when it has created a SelectionDAG for us to codegen. |
| 151 | void X86DAGToDAGISel::InstructionSelectBasicBlock(SelectionDAG &DAG) { |
| 152 | DEBUG(BB->dump()); |
Chris Lattner | 92cb0af | 2006-01-11 01:15:34 +0000 | [diff] [blame] | 153 | MachineFunction::iterator FirstMBB = BB; |
Chris Lattner | c961eea | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 154 | |
| 155 | // Codegen the basic block. |
Evan Cheng | ba2f0a9 | 2006-02-05 06:46:41 +0000 | [diff] [blame] | 156 | DAG.setRoot(SelectRoot(DAG.getRoot())); |
Evan Cheng | fcaa995 | 2005-12-19 22:36:02 +0000 | [diff] [blame] | 157 | CodeGenMap.clear(); |
Chris Lattner | c961eea | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 158 | DAG.RemoveDeadNodes(); |
| 159 | |
| 160 | // Emit machine code to BB. |
| 161 | ScheduleAndEmitDAG(DAG); |
Chris Lattner | 92cb0af | 2006-01-11 01:15:34 +0000 | [diff] [blame] | 162 | |
| 163 | // If we are emitting FP stack code, scan the basic block to determine if this |
| 164 | // block defines any FP values. If so, put an FP_REG_KILL instruction before |
| 165 | // the terminator of the block. |
Evan Cheng | 559806f | 2006-01-27 08:10:46 +0000 | [diff] [blame] | 166 | if (!Subtarget->hasSSE2()) { |
Chris Lattner | 92cb0af | 2006-01-11 01:15:34 +0000 | [diff] [blame] | 167 | // Note that FP stack instructions *are* used in SSE code when returning |
| 168 | // values, but these are not live out of the basic block, so we don't need |
| 169 | // an FP_REG_KILL in this case either. |
| 170 | bool ContainsFPCode = false; |
| 171 | |
| 172 | // Scan all of the machine instructions in these MBBs, checking for FP |
| 173 | // stores. |
| 174 | MachineFunction::iterator MBBI = FirstMBB; |
| 175 | do { |
| 176 | for (MachineBasicBlock::iterator I = MBBI->begin(), E = MBBI->end(); |
| 177 | !ContainsFPCode && I != E; ++I) { |
| 178 | for (unsigned op = 0, e = I->getNumOperands(); op != e; ++op) { |
| 179 | if (I->getOperand(op).isRegister() && I->getOperand(op).isDef() && |
| 180 | MRegisterInfo::isVirtualRegister(I->getOperand(op).getReg()) && |
| 181 | RegMap->getRegClass(I->getOperand(0).getReg()) == |
| 182 | X86::RFPRegisterClass) { |
| 183 | ContainsFPCode = true; |
| 184 | break; |
| 185 | } |
| 186 | } |
| 187 | } |
| 188 | } while (!ContainsFPCode && &*(MBBI++) != BB); |
| 189 | |
| 190 | // Check PHI nodes in successor blocks. These PHI's will be lowered to have |
| 191 | // a copy of the input value in this block. |
| 192 | if (!ContainsFPCode) { |
| 193 | // Final check, check LLVM BB's that are successors to the LLVM BB |
| 194 | // corresponding to BB for FP PHI nodes. |
| 195 | const BasicBlock *LLVMBB = BB->getBasicBlock(); |
| 196 | const PHINode *PN; |
| 197 | for (succ_const_iterator SI = succ_begin(LLVMBB), E = succ_end(LLVMBB); |
| 198 | !ContainsFPCode && SI != E; ++SI) { |
| 199 | for (BasicBlock::const_iterator II = SI->begin(); |
| 200 | (PN = dyn_cast<PHINode>(II)); ++II) { |
| 201 | if (PN->getType()->isFloatingPoint()) { |
| 202 | ContainsFPCode = true; |
| 203 | break; |
| 204 | } |
| 205 | } |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | // Finally, if we found any FP code, emit the FP_REG_KILL instruction. |
| 210 | if (ContainsFPCode) { |
| 211 | BuildMI(*BB, BB->getFirstTerminator(), X86::FP_REG_KILL, 0); |
| 212 | ++NumFPKill; |
| 213 | } |
| 214 | } |
Chris Lattner | c961eea | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 215 | } |
| 216 | |
Evan Cheng | 8700e14 | 2006-01-11 06:09:51 +0000 | [diff] [blame] | 217 | /// EmitSpecialCodeForMain - Emit any code that needs to be executed only in |
| 218 | /// the main function. |
| 219 | static void EmitSpecialCodeForMain(MachineBasicBlock *BB, |
| 220 | MachineFrameInfo *MFI) { |
| 221 | // Switch the FPU to 64-bit precision mode for better compatibility and speed. |
| 222 | int CWFrameIdx = MFI->CreateStackObject(2, 2); |
| 223 | addFrameReference(BuildMI(BB, X86::FNSTCW16m, 4), CWFrameIdx); |
| 224 | |
| 225 | // Set the high part to be 64-bit precision. |
| 226 | addFrameReference(BuildMI(BB, X86::MOV8mi, 5), |
| 227 | CWFrameIdx, 1).addImm(2); |
| 228 | |
| 229 | // Reload the modified control word now. |
| 230 | addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx); |
| 231 | } |
| 232 | |
| 233 | void X86DAGToDAGISel::EmitFunctionEntryCode(Function &Fn, MachineFunction &MF) { |
| 234 | // If this is main, emit special code for main. |
| 235 | MachineBasicBlock *BB = MF.begin(); |
| 236 | if (Fn.hasExternalLinkage() && Fn.getName() == "main") |
| 237 | EmitSpecialCodeForMain(BB, MF.getFrameInfo()); |
| 238 | } |
| 239 | |
Chris Lattner | f9ce9fb | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 240 | /// MatchAddress - Add the specified node to the specified addressing mode, |
| 241 | /// returning true if it cannot be done. This just pattern matches for the |
| 242 | /// addressing mode |
| 243 | bool X86DAGToDAGISel::MatchAddress(SDOperand N, X86ISelAddressMode &AM) { |
| 244 | switch (N.getOpcode()) { |
| 245 | default: break; |
| 246 | case ISD::FrameIndex: |
| 247 | if (AM.BaseType == X86ISelAddressMode::RegBase && AM.Base.Reg.Val == 0) { |
| 248 | AM.BaseType = X86ISelAddressMode::FrameIndexBase; |
| 249 | AM.Base.FrameIndex = cast<FrameIndexSDNode>(N)->getIndex(); |
| 250 | return false; |
| 251 | } |
| 252 | break; |
Evan Cheng | ec693f7 | 2005-12-08 02:01:35 +0000 | [diff] [blame] | 253 | |
| 254 | case ISD::ConstantPool: |
| 255 | if (AM.BaseType == X86ISelAddressMode::RegBase && AM.Base.Reg.Val == 0) { |
| 256 | if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N)) { |
| 257 | AM.BaseType = X86ISelAddressMode::ConstantPoolBase; |
Evan Cheng | 223547a | 2006-01-31 22:28:30 +0000 | [diff] [blame] | 258 | AM.Base.Reg = CurDAG->getTargetConstantPool(CP->get(), MVT::i32, |
| 259 | CP->getAlignment()); |
Evan Cheng | ec693f7 | 2005-12-08 02:01:35 +0000 | [diff] [blame] | 260 | return false; |
| 261 | } |
| 262 | } |
| 263 | break; |
| 264 | |
Chris Lattner | f9ce9fb | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 265 | case ISD::GlobalAddress: |
Evan Cheng | 3a03ebb | 2005-12-21 23:05:39 +0000 | [diff] [blame] | 266 | case ISD::TargetGlobalAddress: |
Chris Lattner | f9ce9fb | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 267 | if (AM.GV == 0) { |
Evan Cheng | b077b84 | 2005-12-21 02:39:21 +0000 | [diff] [blame] | 268 | AM.GV = cast<GlobalAddressSDNode>(N)->getGlobal(); |
Evan Cheng | bdce7b4 | 2005-12-17 09:13:43 +0000 | [diff] [blame] | 269 | return false; |
Chris Lattner | f9ce9fb | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 270 | } |
| 271 | break; |
Evan Cheng | ec693f7 | 2005-12-08 02:01:35 +0000 | [diff] [blame] | 272 | |
Chris Lattner | f9ce9fb | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 273 | case ISD::Constant: |
| 274 | AM.Disp += cast<ConstantSDNode>(N)->getValue(); |
| 275 | return false; |
Evan Cheng | ec693f7 | 2005-12-08 02:01:35 +0000 | [diff] [blame] | 276 | |
Chris Lattner | f9ce9fb | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 277 | case ISD::SHL: |
| 278 | if (AM.IndexReg.Val == 0 && AM.Scale == 1) |
| 279 | if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1))) { |
| 280 | unsigned Val = CN->getValue(); |
| 281 | if (Val == 1 || Val == 2 || Val == 3) { |
| 282 | AM.Scale = 1 << Val; |
| 283 | SDOperand ShVal = N.Val->getOperand(0); |
| 284 | |
| 285 | // Okay, we know that we have a scale by now. However, if the scaled |
| 286 | // value is an add of something and a constant, we can fold the |
| 287 | // constant into the disp field here. |
| 288 | if (ShVal.Val->getOpcode() == ISD::ADD && ShVal.hasOneUse() && |
| 289 | isa<ConstantSDNode>(ShVal.Val->getOperand(1))) { |
| 290 | AM.IndexReg = ShVal.Val->getOperand(0); |
| 291 | ConstantSDNode *AddVal = |
| 292 | cast<ConstantSDNode>(ShVal.Val->getOperand(1)); |
| 293 | AM.Disp += AddVal->getValue() << Val; |
| 294 | } else { |
| 295 | AM.IndexReg = ShVal; |
| 296 | } |
| 297 | return false; |
| 298 | } |
| 299 | } |
| 300 | break; |
Evan Cheng | ec693f7 | 2005-12-08 02:01:35 +0000 | [diff] [blame] | 301 | |
Chris Lattner | f9ce9fb | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 302 | case ISD::MUL: |
| 303 | // X*[3,5,9] -> X+X*[2,4,8] |
| 304 | if (AM.IndexReg.Val == 0 && AM.BaseType == X86ISelAddressMode::RegBase && |
| 305 | AM.Base.Reg.Val == 0) |
| 306 | if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1))) |
| 307 | if (CN->getValue() == 3 || CN->getValue() == 5 || CN->getValue() == 9) { |
| 308 | AM.Scale = unsigned(CN->getValue())-1; |
| 309 | |
| 310 | SDOperand MulVal = N.Val->getOperand(0); |
| 311 | SDOperand Reg; |
| 312 | |
| 313 | // Okay, we know that we have a scale by now. However, if the scaled |
| 314 | // value is an add of something and a constant, we can fold the |
| 315 | // constant into the disp field here. |
| 316 | if (MulVal.Val->getOpcode() == ISD::ADD && MulVal.hasOneUse() && |
| 317 | isa<ConstantSDNode>(MulVal.Val->getOperand(1))) { |
| 318 | Reg = MulVal.Val->getOperand(0); |
| 319 | ConstantSDNode *AddVal = |
| 320 | cast<ConstantSDNode>(MulVal.Val->getOperand(1)); |
| 321 | AM.Disp += AddVal->getValue() * CN->getValue(); |
| 322 | } else { |
| 323 | Reg = N.Val->getOperand(0); |
| 324 | } |
| 325 | |
| 326 | AM.IndexReg = AM.Base.Reg = Reg; |
| 327 | return false; |
| 328 | } |
| 329 | break; |
| 330 | |
| 331 | case ISD::ADD: { |
| 332 | X86ISelAddressMode Backup = AM; |
| 333 | if (!MatchAddress(N.Val->getOperand(0), AM) && |
| 334 | !MatchAddress(N.Val->getOperand(1), AM)) |
| 335 | return false; |
| 336 | AM = Backup; |
| 337 | if (!MatchAddress(N.Val->getOperand(1), AM) && |
| 338 | !MatchAddress(N.Val->getOperand(0), AM)) |
| 339 | return false; |
| 340 | AM = Backup; |
| 341 | break; |
| 342 | } |
| 343 | } |
| 344 | |
| 345 | // Is the base register already occupied? |
| 346 | if (AM.BaseType != X86ISelAddressMode::RegBase || AM.Base.Reg.Val) { |
| 347 | // If so, check to see if the scale index register is set. |
| 348 | if (AM.IndexReg.Val == 0) { |
| 349 | AM.IndexReg = N; |
| 350 | AM.Scale = 1; |
| 351 | return false; |
| 352 | } |
| 353 | |
| 354 | // Otherwise, we cannot select it. |
| 355 | return true; |
| 356 | } |
| 357 | |
| 358 | // Default, generate it as a register. |
| 359 | AM.BaseType = X86ISelAddressMode::RegBase; |
| 360 | AM.Base.Reg = N; |
| 361 | return false; |
| 362 | } |
| 363 | |
Evan Cheng | ec693f7 | 2005-12-08 02:01:35 +0000 | [diff] [blame] | 364 | /// SelectAddr - returns true if it is able pattern match an addressing mode. |
| 365 | /// It returns the operands which make up the maximal addressing mode it can |
| 366 | /// match by reference. |
| 367 | bool X86DAGToDAGISel::SelectAddr(SDOperand N, SDOperand &Base, SDOperand &Scale, |
| 368 | SDOperand &Index, SDOperand &Disp) { |
| 369 | X86ISelAddressMode AM; |
Evan Cheng | 8700e14 | 2006-01-11 06:09:51 +0000 | [diff] [blame] | 370 | if (MatchAddress(N, AM)) |
| 371 | return false; |
Evan Cheng | ec693f7 | 2005-12-08 02:01:35 +0000 | [diff] [blame] | 372 | |
Evan Cheng | 8700e14 | 2006-01-11 06:09:51 +0000 | [diff] [blame] | 373 | if (AM.BaseType == X86ISelAddressMode::RegBase) { |
Evan Cheng | 7dd281b | 2006-02-05 05:25:07 +0000 | [diff] [blame] | 374 | if (!AM.Base.Reg.Val) |
Evan Cheng | 8700e14 | 2006-01-11 06:09:51 +0000 | [diff] [blame] | 375 | AM.Base.Reg = CurDAG->getRegister(0, MVT::i32); |
Evan Cheng | ec693f7 | 2005-12-08 02:01:35 +0000 | [diff] [blame] | 376 | } |
Evan Cheng | 8700e14 | 2006-01-11 06:09:51 +0000 | [diff] [blame] | 377 | |
Evan Cheng | 7dd281b | 2006-02-05 05:25:07 +0000 | [diff] [blame] | 378 | if (!AM.IndexReg.Val) |
Evan Cheng | 8700e14 | 2006-01-11 06:09:51 +0000 | [diff] [blame] | 379 | AM.IndexReg = CurDAG->getRegister(0, MVT::i32); |
| 380 | |
| 381 | getAddressOperands(AM, Base, Scale, Index, Disp); |
| 382 | return true; |
Evan Cheng | ec693f7 | 2005-12-08 02:01:35 +0000 | [diff] [blame] | 383 | } |
| 384 | |
Evan Cheng | 5e35168 | 2006-02-06 06:02:33 +0000 | [diff] [blame] | 385 | bool X86DAGToDAGISel::TryFoldLoad(SDOperand P, SDOperand N, |
| 386 | SDOperand &Base, SDOperand &Scale, |
| 387 | SDOperand &Index, SDOperand &Disp) { |
| 388 | if (N.getOpcode() == ISD::LOAD && |
| 389 | N.hasOneUse() && |
| 390 | !CodeGenMap.count(N.getValue(0)) && |
| 391 | (P.getNumOperands() == 1 || !isNonImmUse(P.Val, N.Val))) |
Evan Cheng | 0114e94 | 2006-01-06 20:36:21 +0000 | [diff] [blame] | 392 | return SelectAddr(N.getOperand(1), Base, Scale, Index, Disp); |
| 393 | return false; |
| 394 | } |
| 395 | |
| 396 | static bool isRegister0(SDOperand Op) { |
Evan Cheng | ec693f7 | 2005-12-08 02:01:35 +0000 | [diff] [blame] | 397 | if (RegisterSDNode *R = dyn_cast<RegisterSDNode>(Op)) |
| 398 | return (R->getReg() == 0); |
| 399 | return false; |
| 400 | } |
| 401 | |
| 402 | /// SelectLEAAddr - it calls SelectAddr and determines if the maximal addressing |
| 403 | /// mode it matches can be cost effectively emitted as an LEA instruction. |
| 404 | /// For X86, it always is unless it's just a (Reg + const). |
Chris Lattner | a2b694c | 2006-01-11 00:46:55 +0000 | [diff] [blame] | 405 | bool X86DAGToDAGISel::SelectLEAAddr(SDOperand N, SDOperand &Base, |
| 406 | SDOperand &Scale, |
Evan Cheng | ec693f7 | 2005-12-08 02:01:35 +0000 | [diff] [blame] | 407 | SDOperand &Index, SDOperand &Disp) { |
Evan Cheng | e528053 | 2005-12-12 21:49:40 +0000 | [diff] [blame] | 408 | X86ISelAddressMode AM; |
| 409 | if (!MatchAddress(N, AM)) { |
| 410 | bool SelectBase = false; |
| 411 | bool SelectIndex = false; |
| 412 | bool Check = false; |
| 413 | if (AM.BaseType == X86ISelAddressMode::RegBase) { |
| 414 | if (AM.Base.Reg.Val) { |
| 415 | Check = true; |
| 416 | SelectBase = true; |
Evan Cheng | ec693f7 | 2005-12-08 02:01:35 +0000 | [diff] [blame] | 417 | } else { |
Evan Cheng | e528053 | 2005-12-12 21:49:40 +0000 | [diff] [blame] | 418 | AM.Base.Reg = CurDAG->getRegister(0, MVT::i32); |
Evan Cheng | ec693f7 | 2005-12-08 02:01:35 +0000 | [diff] [blame] | 419 | } |
Evan Cheng | ec693f7 | 2005-12-08 02:01:35 +0000 | [diff] [blame] | 420 | } |
Evan Cheng | e528053 | 2005-12-12 21:49:40 +0000 | [diff] [blame] | 421 | |
| 422 | if (AM.IndexReg.Val) { |
| 423 | SelectIndex = true; |
| 424 | } else { |
| 425 | AM.IndexReg = CurDAG->getRegister(0, MVT::i32); |
| 426 | } |
| 427 | |
| 428 | if (Check) { |
| 429 | unsigned Complexity = 0; |
| 430 | if (AM.Scale > 1) |
| 431 | Complexity++; |
| 432 | if (SelectIndex) |
| 433 | Complexity++; |
| 434 | if (AM.GV) |
| 435 | Complexity++; |
| 436 | else if (AM.Disp > 1) |
| 437 | Complexity++; |
| 438 | if (Complexity <= 1) |
| 439 | return false; |
| 440 | } |
| 441 | |
Evan Cheng | e528053 | 2005-12-12 21:49:40 +0000 | [diff] [blame] | 442 | getAddressOperands(AM, Base, Scale, Index, Disp); |
Evan Cheng | ec693f7 | 2005-12-08 02:01:35 +0000 | [diff] [blame] | 443 | return true; |
Evan Cheng | ec693f7 | 2005-12-08 02:01:35 +0000 | [diff] [blame] | 444 | } |
Evan Cheng | e528053 | 2005-12-12 21:49:40 +0000 | [diff] [blame] | 445 | return false; |
Evan Cheng | ec693f7 | 2005-12-08 02:01:35 +0000 | [diff] [blame] | 446 | } |
| 447 | |
Evan Cheng | 3416721 | 2006-02-09 00:37:58 +0000 | [diff] [blame] | 448 | void X86DAGToDAGISel::Select(SDOperand &Result, SDOperand N) { |
Evan Cheng | def941b | 2005-12-15 01:02:48 +0000 | [diff] [blame] | 449 | SDNode *Node = N.Val; |
| 450 | MVT::ValueType NVT = Node->getValueType(0); |
Evan Cheng | 0114e94 | 2006-01-06 20:36:21 +0000 | [diff] [blame] | 451 | unsigned Opc, MOpc; |
| 452 | unsigned Opcode = Node->getOpcode(); |
Chris Lattner | c961eea | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 453 | |
Evan Cheng | 3416721 | 2006-02-09 00:37:58 +0000 | [diff] [blame] | 454 | if (Opcode >= ISD::BUILTIN_OP_END && Opcode < X86ISD::FIRST_NUMBER) { |
| 455 | Result = N; |
| 456 | return; // Already selected. |
| 457 | } |
Evan Cheng | 38262ca | 2006-01-11 22:15:18 +0000 | [diff] [blame] | 458 | |
| 459 | std::map<SDOperand, SDOperand>::iterator CGMI = CodeGenMap.find(N); |
Evan Cheng | 3416721 | 2006-02-09 00:37:58 +0000 | [diff] [blame] | 460 | if (CGMI != CodeGenMap.end()) { |
| 461 | Result = CGMI->second; |
| 462 | return; |
| 463 | } |
Chris Lattner | c961eea | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 464 | |
Evan Cheng | 0114e94 | 2006-01-06 20:36:21 +0000 | [diff] [blame] | 465 | switch (Opcode) { |
Chris Lattner | c961eea | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 466 | default: break; |
Evan Cheng | 0114e94 | 2006-01-06 20:36:21 +0000 | [diff] [blame] | 467 | case ISD::MULHU: |
| 468 | case ISD::MULHS: { |
| 469 | if (Opcode == ISD::MULHU) |
| 470 | switch (NVT) { |
| 471 | default: assert(0 && "Unsupported VT!"); |
| 472 | case MVT::i8: Opc = X86::MUL8r; MOpc = X86::MUL8m; break; |
| 473 | case MVT::i16: Opc = X86::MUL16r; MOpc = X86::MUL16m; break; |
| 474 | case MVT::i32: Opc = X86::MUL32r; MOpc = X86::MUL32m; break; |
| 475 | } |
| 476 | else |
| 477 | switch (NVT) { |
| 478 | default: assert(0 && "Unsupported VT!"); |
| 479 | case MVT::i8: Opc = X86::IMUL8r; MOpc = X86::IMUL8m; break; |
| 480 | case MVT::i16: Opc = X86::IMUL16r; MOpc = X86::IMUL16m; break; |
| 481 | case MVT::i32: Opc = X86::IMUL32r; MOpc = X86::IMUL32m; break; |
| 482 | } |
| 483 | |
| 484 | unsigned LoReg, HiReg; |
| 485 | switch (NVT) { |
| 486 | default: assert(0 && "Unsupported VT!"); |
| 487 | case MVT::i8: LoReg = X86::AL; HiReg = X86::AH; break; |
| 488 | case MVT::i16: LoReg = X86::AX; HiReg = X86::DX; break; |
| 489 | case MVT::i32: LoReg = X86::EAX; HiReg = X86::EDX; break; |
| 490 | } |
| 491 | |
| 492 | SDOperand N0 = Node->getOperand(0); |
| 493 | SDOperand N1 = Node->getOperand(1); |
| 494 | |
| 495 | bool foldedLoad = false; |
| 496 | SDOperand Tmp0, Tmp1, Tmp2, Tmp3; |
Evan Cheng | 5e35168 | 2006-02-06 06:02:33 +0000 | [diff] [blame] | 497 | foldedLoad = TryFoldLoad(N, N1, Tmp0, Tmp1, Tmp2, Tmp3); |
Evan Cheng | 948f343 | 2006-01-06 23:19:29 +0000 | [diff] [blame] | 498 | // MULHU and MULHS are commmutative |
| 499 | if (!foldedLoad) { |
Evan Cheng | 5e35168 | 2006-02-06 06:02:33 +0000 | [diff] [blame] | 500 | foldedLoad = TryFoldLoad(N, N0, Tmp0, Tmp1, Tmp2, Tmp3); |
Evan Cheng | 948f343 | 2006-01-06 23:19:29 +0000 | [diff] [blame] | 501 | if (foldedLoad) { |
| 502 | N0 = Node->getOperand(1); |
| 503 | N1 = Node->getOperand(0); |
| 504 | } |
| 505 | } |
| 506 | |
Evan Cheng | 3416721 | 2006-02-09 00:37:58 +0000 | [diff] [blame] | 507 | SDOperand Chain; |
| 508 | if (foldedLoad) |
| 509 | Select(Chain, N1.getOperand(0)); |
| 510 | else |
| 511 | Chain = CurDAG->getEntryNode(); |
Evan Cheng | 0114e94 | 2006-01-06 20:36:21 +0000 | [diff] [blame] | 512 | |
Evan Cheng | 3416721 | 2006-02-09 00:37:58 +0000 | [diff] [blame] | 513 | SDOperand InFlag(0, 0); |
| 514 | Select(N0, N0); |
Evan Cheng | 0114e94 | 2006-01-06 20:36:21 +0000 | [diff] [blame] | 515 | Chain = CurDAG->getCopyToReg(Chain, CurDAG->getRegister(LoReg, NVT), |
Evan Cheng | 3416721 | 2006-02-09 00:37:58 +0000 | [diff] [blame] | 516 | N0, InFlag); |
Evan Cheng | 0114e94 | 2006-01-06 20:36:21 +0000 | [diff] [blame] | 517 | InFlag = Chain.getValue(1); |
| 518 | |
| 519 | if (foldedLoad) { |
Evan Cheng | 3416721 | 2006-02-09 00:37:58 +0000 | [diff] [blame] | 520 | Select(Tmp0, Tmp0); |
| 521 | Select(Tmp1, Tmp1); |
| 522 | Select(Tmp2, Tmp2); |
| 523 | Select(Tmp3, Tmp3); |
Evan Cheng | 7e9b26f | 2006-02-09 07:17:49 +0000 | [diff] [blame] | 524 | SDNode *CNode = |
| 525 | CurDAG->getTargetNode(MOpc, MVT::Other, MVT::Flag, Tmp0, Tmp1, |
| 526 | Tmp2, Tmp3, Chain, InFlag); |
| 527 | Chain = SDOperand(CNode, 0); |
| 528 | InFlag = SDOperand(CNode, 1); |
Evan Cheng | 0114e94 | 2006-01-06 20:36:21 +0000 | [diff] [blame] | 529 | } else { |
Evan Cheng | 3416721 | 2006-02-09 00:37:58 +0000 | [diff] [blame] | 530 | Select(N1, N1); |
Evan Cheng | 7e9b26f | 2006-02-09 07:17:49 +0000 | [diff] [blame] | 531 | InFlag = |
| 532 | SDOperand(CurDAG->getTargetNode(Opc, MVT::Flag, N1, InFlag), 0); |
Evan Cheng | 0114e94 | 2006-01-06 20:36:21 +0000 | [diff] [blame] | 533 | } |
| 534 | |
Evan Cheng | 3416721 | 2006-02-09 00:37:58 +0000 | [diff] [blame] | 535 | Result = CurDAG->getCopyFromReg(Chain, HiReg, NVT, InFlag); |
Evan Cheng | 0114e94 | 2006-01-06 20:36:21 +0000 | [diff] [blame] | 536 | CodeGenMap[N.getValue(0)] = Result; |
Evan Cheng | 5e35168 | 2006-02-06 06:02:33 +0000 | [diff] [blame] | 537 | if (foldedLoad) { |
Evan Cheng | 948f343 | 2006-01-06 23:19:29 +0000 | [diff] [blame] | 538 | CodeGenMap[N1.getValue(1)] = Result.getValue(1); |
Evan Cheng | 7d82d60 | 2006-02-09 22:12:53 +0000 | [diff] [blame^] | 539 | AddHandleReplacement(N1.Val, 1, Result.Val, 1); |
Evan Cheng | 5e35168 | 2006-02-06 06:02:33 +0000 | [diff] [blame] | 540 | } |
Evan Cheng | 3416721 | 2006-02-09 00:37:58 +0000 | [diff] [blame] | 541 | |
| 542 | return; |
Evan Cheng | 948f343 | 2006-01-06 23:19:29 +0000 | [diff] [blame] | 543 | } |
| 544 | |
| 545 | case ISD::SDIV: |
| 546 | case ISD::UDIV: |
| 547 | case ISD::SREM: |
| 548 | case ISD::UREM: { |
| 549 | bool isSigned = Opcode == ISD::SDIV || Opcode == ISD::SREM; |
| 550 | bool isDiv = Opcode == ISD::SDIV || Opcode == ISD::UDIV; |
| 551 | if (!isSigned) |
| 552 | switch (NVT) { |
| 553 | default: assert(0 && "Unsupported VT!"); |
| 554 | case MVT::i8: Opc = X86::DIV8r; MOpc = X86::DIV8m; break; |
| 555 | case MVT::i16: Opc = X86::DIV16r; MOpc = X86::DIV16m; break; |
| 556 | case MVT::i32: Opc = X86::DIV32r; MOpc = X86::DIV32m; break; |
| 557 | } |
| 558 | else |
| 559 | switch (NVT) { |
| 560 | default: assert(0 && "Unsupported VT!"); |
| 561 | case MVT::i8: Opc = X86::IDIV8r; MOpc = X86::IDIV8m; break; |
| 562 | case MVT::i16: Opc = X86::IDIV16r; MOpc = X86::IDIV16m; break; |
| 563 | case MVT::i32: Opc = X86::IDIV32r; MOpc = X86::IDIV32m; break; |
| 564 | } |
| 565 | |
| 566 | unsigned LoReg, HiReg; |
| 567 | unsigned ClrOpcode, SExtOpcode; |
| 568 | switch (NVT) { |
| 569 | default: assert(0 && "Unsupported VT!"); |
| 570 | case MVT::i8: |
| 571 | LoReg = X86::AL; HiReg = X86::AH; |
| 572 | ClrOpcode = X86::MOV8ri; |
| 573 | SExtOpcode = X86::CBW; |
| 574 | break; |
| 575 | case MVT::i16: |
| 576 | LoReg = X86::AX; HiReg = X86::DX; |
| 577 | ClrOpcode = X86::MOV16ri; |
| 578 | SExtOpcode = X86::CWD; |
| 579 | break; |
| 580 | case MVT::i32: |
| 581 | LoReg = X86::EAX; HiReg = X86::EDX; |
| 582 | ClrOpcode = X86::MOV32ri; |
| 583 | SExtOpcode = X86::CDQ; |
| 584 | break; |
| 585 | } |
| 586 | |
| 587 | SDOperand N0 = Node->getOperand(0); |
| 588 | SDOperand N1 = Node->getOperand(1); |
| 589 | |
| 590 | bool foldedLoad = false; |
| 591 | SDOperand Tmp0, Tmp1, Tmp2, Tmp3; |
Evan Cheng | 5e35168 | 2006-02-06 06:02:33 +0000 | [diff] [blame] | 592 | foldedLoad = TryFoldLoad(N, N1, Tmp0, Tmp1, Tmp2, Tmp3); |
Evan Cheng | 3416721 | 2006-02-09 00:37:58 +0000 | [diff] [blame] | 593 | SDOperand Chain; |
| 594 | if (foldedLoad) |
| 595 | Select(Chain, N1.getOperand(0)); |
| 596 | else |
| 597 | Chain = CurDAG->getEntryNode(); |
Evan Cheng | 948f343 | 2006-01-06 23:19:29 +0000 | [diff] [blame] | 598 | |
Evan Cheng | 3416721 | 2006-02-09 00:37:58 +0000 | [diff] [blame] | 599 | SDOperand InFlag(0, 0); |
| 600 | Select(N0, N0); |
Evan Cheng | 948f343 | 2006-01-06 23:19:29 +0000 | [diff] [blame] | 601 | Chain = CurDAG->getCopyToReg(Chain, CurDAG->getRegister(LoReg, NVT), |
Evan Cheng | 3416721 | 2006-02-09 00:37:58 +0000 | [diff] [blame] | 602 | N0, InFlag); |
Evan Cheng | 948f343 | 2006-01-06 23:19:29 +0000 | [diff] [blame] | 603 | InFlag = Chain.getValue(1); |
| 604 | |
| 605 | if (isSigned) { |
| 606 | // Sign extend the low part into the high part. |
Evan Cheng | 7e9b26f | 2006-02-09 07:17:49 +0000 | [diff] [blame] | 607 | InFlag = |
| 608 | SDOperand(CurDAG->getTargetNode(SExtOpcode, MVT::Flag, InFlag), 0); |
Evan Cheng | 948f343 | 2006-01-06 23:19:29 +0000 | [diff] [blame] | 609 | } else { |
| 610 | // Zero out the high part, effectively zero extending the input. |
| 611 | SDOperand ClrNode = |
Evan Cheng | 7e9b26f | 2006-02-09 07:17:49 +0000 | [diff] [blame] | 612 | SDOperand(CurDAG->getTargetNode(ClrOpcode, NVT, |
| 613 | CurDAG->getTargetConstant(0, NVT)), 0); |
Evan Cheng | 948f343 | 2006-01-06 23:19:29 +0000 | [diff] [blame] | 614 | Chain = CurDAG->getCopyToReg(Chain, CurDAG->getRegister(HiReg, NVT), |
| 615 | ClrNode, InFlag); |
| 616 | InFlag = Chain.getValue(1); |
| 617 | } |
| 618 | |
| 619 | if (foldedLoad) { |
Evan Cheng | 3416721 | 2006-02-09 00:37:58 +0000 | [diff] [blame] | 620 | Select(Tmp0, Tmp0); |
| 621 | Select(Tmp1, Tmp1); |
| 622 | Select(Tmp2, Tmp2); |
| 623 | Select(Tmp3, Tmp3); |
Evan Cheng | 7e9b26f | 2006-02-09 07:17:49 +0000 | [diff] [blame] | 624 | SDNode *CNode = |
| 625 | CurDAG->getTargetNode(MOpc, MVT::Other, MVT::Flag, Tmp0, Tmp1, |
| 626 | Tmp2, Tmp3, Chain, InFlag); |
| 627 | Chain = SDOperand(CNode, 0); |
| 628 | InFlag = SDOperand(CNode, 1); |
Evan Cheng | 948f343 | 2006-01-06 23:19:29 +0000 | [diff] [blame] | 629 | } else { |
Evan Cheng | 3416721 | 2006-02-09 00:37:58 +0000 | [diff] [blame] | 630 | Select(N1, N1); |
Evan Cheng | 7e9b26f | 2006-02-09 07:17:49 +0000 | [diff] [blame] | 631 | InFlag = |
| 632 | SDOperand(CurDAG->getTargetNode(Opc, MVT::Flag, N1, InFlag), 0); |
Evan Cheng | 948f343 | 2006-01-06 23:19:29 +0000 | [diff] [blame] | 633 | } |
| 634 | |
Evan Cheng | 3416721 | 2006-02-09 00:37:58 +0000 | [diff] [blame] | 635 | Result = CurDAG->getCopyFromReg(Chain, isDiv ? LoReg : HiReg, |
| 636 | NVT, InFlag); |
Evan Cheng | 948f343 | 2006-01-06 23:19:29 +0000 | [diff] [blame] | 637 | CodeGenMap[N.getValue(0)] = Result; |
Evan Cheng | 5e35168 | 2006-02-06 06:02:33 +0000 | [diff] [blame] | 638 | if (foldedLoad) { |
Evan Cheng | 948f343 | 2006-01-06 23:19:29 +0000 | [diff] [blame] | 639 | CodeGenMap[N1.getValue(1)] = Result.getValue(1); |
Evan Cheng | 7d82d60 | 2006-02-09 22:12:53 +0000 | [diff] [blame^] | 640 | AddHandleReplacement(N1.Val, 1, Result.Val, 1); |
Evan Cheng | 5e35168 | 2006-02-06 06:02:33 +0000 | [diff] [blame] | 641 | } |
Evan Cheng | 3416721 | 2006-02-09 00:37:58 +0000 | [diff] [blame] | 642 | return; |
Evan Cheng | 0114e94 | 2006-01-06 20:36:21 +0000 | [diff] [blame] | 643 | } |
Evan Cheng | bd3d25c | 2005-11-30 02:51:20 +0000 | [diff] [blame] | 644 | |
Evan Cheng | 45f37bc | 2005-12-17 02:02:50 +0000 | [diff] [blame] | 645 | case ISD::TRUNCATE: { |
| 646 | unsigned Reg; |
| 647 | MVT::ValueType VT; |
| 648 | switch (Node->getOperand(0).getValueType()) { |
| 649 | default: assert(0 && "Unknown truncate!"); |
| 650 | case MVT::i16: Reg = X86::AX; Opc = X86::MOV16rr; VT = MVT::i16; break; |
| 651 | case MVT::i32: Reg = X86::EAX; Opc = X86::MOV32rr; VT = MVT::i32; break; |
| 652 | } |
Evan Cheng | 3416721 | 2006-02-09 00:37:58 +0000 | [diff] [blame] | 653 | SDOperand Tmp0, Tmp1; |
| 654 | Select(Tmp0, Node->getOperand(0)); |
Evan Cheng | 7e9b26f | 2006-02-09 07:17:49 +0000 | [diff] [blame] | 655 | Select(Tmp1, SDOperand(CurDAG->getTargetNode(Opc, VT, Tmp0), 0)); |
Evan Cheng | 45f37bc | 2005-12-17 02:02:50 +0000 | [diff] [blame] | 656 | SDOperand InFlag = SDOperand(0,0); |
Evan Cheng | 3416721 | 2006-02-09 00:37:58 +0000 | [diff] [blame] | 657 | Result = CurDAG->getCopyToReg(CurDAG->getEntryNode(), Reg, Tmp1, InFlag); |
Evan Cheng | 45f37bc | 2005-12-17 02:02:50 +0000 | [diff] [blame] | 658 | SDOperand Chain = Result.getValue(0); |
| 659 | InFlag = Result.getValue(1); |
| 660 | |
| 661 | switch (NVT) { |
| 662 | default: assert(0 && "Unknown truncate!"); |
| 663 | case MVT::i8: Reg = X86::AL; Opc = X86::MOV8rr; VT = MVT::i8; break; |
| 664 | case MVT::i16: Reg = X86::AX; Opc = X86::MOV16rr; VT = MVT::i16; break; |
| 665 | } |
| 666 | |
Evan Cheng | 3416721 | 2006-02-09 00:37:58 +0000 | [diff] [blame] | 667 | Result = CurDAG->getCopyFromReg(Chain, Reg, VT, InFlag); |
Evan Cheng | 0114e94 | 2006-01-06 20:36:21 +0000 | [diff] [blame] | 668 | if (N.Val->hasOneUse()) |
Evan Cheng | 7e9b26f | 2006-02-09 07:17:49 +0000 | [diff] [blame] | 669 | Result = CurDAG->SelectNodeTo(N.Val, Opc, VT, Result); |
Evan Cheng | 0114e94 | 2006-01-06 20:36:21 +0000 | [diff] [blame] | 670 | else |
Evan Cheng | 7e9b26f | 2006-02-09 07:17:49 +0000 | [diff] [blame] | 671 | Result = CodeGenMap[N] = |
| 672 | SDOperand(CurDAG->getTargetNode(Opc, VT, Result), 0); |
Evan Cheng | 3416721 | 2006-02-09 00:37:58 +0000 | [diff] [blame] | 673 | return; |
Evan Cheng | 45f37bc | 2005-12-17 02:02:50 +0000 | [diff] [blame] | 674 | } |
Chris Lattner | c961eea | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 675 | } |
| 676 | |
Evan Cheng | 3416721 | 2006-02-09 00:37:58 +0000 | [diff] [blame] | 677 | SelectCode(Result, N); |
Chris Lattner | c961eea | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 678 | } |
| 679 | |
| 680 | /// createX86ISelDag - This pass converts a legalized DAG into a |
| 681 | /// X86-specific DAG, ready for instruction scheduling. |
| 682 | /// |
| 683 | FunctionPass *llvm::createX86ISelDag(TargetMachine &TM) { |
| 684 | return new X86DAGToDAGISel(TM); |
| 685 | } |