Dan Gohman | 1adf1b0 | 2008-08-19 21:45:35 +0000 | [diff] [blame] | 1 | //===-- X86FastISel.cpp - X86 FastISel implementation ---------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file defines the X86-specific support for the FastISel class. Much |
| 11 | // of the target-specific code is generated by tablegen in the file |
| 12 | // X86GenFastISel.inc, which is #included here. |
| 13 | // |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
| 16 | #include "X86.h" |
Evan Cheng | 8b19e56 | 2008-09-03 06:44:39 +0000 | [diff] [blame] | 17 | #include "X86InstrBuilder.h" |
Dan Gohman | 1adf1b0 | 2008-08-19 21:45:35 +0000 | [diff] [blame] | 18 | #include "X86ISelLowering.h" |
Evan Cheng | 88e3041 | 2008-09-03 01:04:47 +0000 | [diff] [blame] | 19 | #include "X86RegisterInfo.h" |
| 20 | #include "X86Subtarget.h" |
Dan Gohman | 22bb311 | 2008-08-22 00:20:26 +0000 | [diff] [blame] | 21 | #include "X86TargetMachine.h" |
Evan Cheng | f3d4efe | 2008-09-07 09:09:33 +0000 | [diff] [blame] | 22 | #include "llvm/CallingConv.h" |
Dan Gohman | 6e3f05f | 2008-09-04 23:26:51 +0000 | [diff] [blame] | 23 | #include "llvm/DerivedTypes.h" |
Evan Cheng | f3d4efe | 2008-09-07 09:09:33 +0000 | [diff] [blame] | 24 | #include "llvm/Instructions.h" |
Evan Cheng | c3f44b0 | 2008-09-03 00:03:49 +0000 | [diff] [blame] | 25 | #include "llvm/CodeGen/FastISel.h" |
Owen Anderson | 95267a1 | 2008-09-05 00:06:23 +0000 | [diff] [blame] | 26 | #include "llvm/CodeGen/MachineConstantPool.h" |
Evan Cheng | f3d4efe | 2008-09-07 09:09:33 +0000 | [diff] [blame] | 27 | #include "llvm/CodeGen/MachineFrameInfo.h" |
Owen Anderson | 667d8f7 | 2008-08-29 17:45:56 +0000 | [diff] [blame] | 28 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Evan Cheng | f3d4efe | 2008-09-07 09:09:33 +0000 | [diff] [blame] | 29 | #include "llvm/Support/CallSite.h" |
Dan Gohman | 3589308 | 2008-09-18 23:23:44 +0000 | [diff] [blame] | 30 | #include "llvm/Support/GetElementPtrTypeIterator.h" |
Evan Cheng | c3f44b0 | 2008-09-03 00:03:49 +0000 | [diff] [blame] | 31 | |
| 32 | using namespace llvm; |
| 33 | |
| 34 | class X86FastISel : public FastISel { |
| 35 | /// Subtarget - Keep a pointer to the X86Subtarget around so that we can |
| 36 | /// make the right decision when generating code for different targets. |
| 37 | const X86Subtarget *Subtarget; |
Evan Cheng | f3d4efe | 2008-09-07 09:09:33 +0000 | [diff] [blame] | 38 | |
| 39 | /// StackPtr - Register used as the stack pointer. |
| 40 | /// |
| 41 | unsigned StackPtr; |
| 42 | |
| 43 | /// X86ScalarSSEf32, X86ScalarSSEf64 - Select between SSE or x87 |
| 44 | /// floating point ops. |
| 45 | /// When SSE is available, use it for f32 operations. |
| 46 | /// When SSE2 is available, use it for f64 operations. |
| 47 | bool X86ScalarSSEf64; |
| 48 | bool X86ScalarSSEf32; |
| 49 | |
Evan Cheng | 8b19e56 | 2008-09-03 06:44:39 +0000 | [diff] [blame] | 50 | public: |
Dan Gohman | 3df24e6 | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 51 | explicit X86FastISel(MachineFunction &mf, |
Dan Gohman | d57dd5f | 2008-09-23 21:53:34 +0000 | [diff] [blame] | 52 | MachineModuleInfo *mmi, |
Dan Gohman | 3df24e6 | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 53 | DenseMap<const Value *, unsigned> &vm, |
Dan Gohman | 0586d91 | 2008-09-10 20:11:02 +0000 | [diff] [blame] | 54 | DenseMap<const BasicBlock *, MachineBasicBlock *> &bm, |
Dan Gohman | dd5b58a | 2008-10-14 23:54:11 +0000 | [diff] [blame] | 55 | DenseMap<const AllocaInst *, int> &am |
| 56 | #ifndef NDEBUG |
| 57 | , SmallSet<Instruction*, 8> &cil |
| 58 | #endif |
| 59 | ) |
| 60 | : FastISel(mf, mmi, vm, bm, am |
| 61 | #ifndef NDEBUG |
| 62 | , cil |
| 63 | #endif |
| 64 | ) { |
Evan Cheng | 88e3041 | 2008-09-03 01:04:47 +0000 | [diff] [blame] | 65 | Subtarget = &TM.getSubtarget<X86Subtarget>(); |
Evan Cheng | f3d4efe | 2008-09-07 09:09:33 +0000 | [diff] [blame] | 66 | StackPtr = Subtarget->is64Bit() ? X86::RSP : X86::ESP; |
| 67 | X86ScalarSSEf64 = Subtarget->hasSSE2(); |
| 68 | X86ScalarSSEf32 = Subtarget->hasSSE1(); |
Evan Cheng | 88e3041 | 2008-09-03 01:04:47 +0000 | [diff] [blame] | 69 | } |
Evan Cheng | c3f44b0 | 2008-09-03 00:03:49 +0000 | [diff] [blame] | 70 | |
Dan Gohman | 3df24e6 | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 71 | virtual bool TargetSelectInstruction(Instruction *I); |
Evan Cheng | c3f44b0 | 2008-09-03 00:03:49 +0000 | [diff] [blame] | 72 | |
Dan Gohman | 1adf1b0 | 2008-08-19 21:45:35 +0000 | [diff] [blame] | 73 | #include "X86GenFastISel.inc" |
Evan Cheng | 8b19e56 | 2008-09-03 06:44:39 +0000 | [diff] [blame] | 74 | |
| 75 | private: |
Chris Lattner | 9a08a61 | 2008-10-15 04:26:38 +0000 | [diff] [blame] | 76 | bool X86FastEmitCompare(Value *LHS, Value *RHS, MVT VT); |
| 77 | |
Dan Gohman | 0586d91 | 2008-09-10 20:11:02 +0000 | [diff] [blame] | 78 | bool X86FastEmitLoad(MVT VT, const X86AddressMode &AM, unsigned &RR); |
Evan Cheng | 0de588f | 2008-09-05 21:00:03 +0000 | [diff] [blame] | 79 | |
Evan Cheng | f3d4efe | 2008-09-07 09:09:33 +0000 | [diff] [blame] | 80 | bool X86FastEmitStore(MVT VT, unsigned Val, |
Dan Gohman | 0586d91 | 2008-09-10 20:11:02 +0000 | [diff] [blame] | 81 | const X86AddressMode &AM); |
Evan Cheng | 24e3a90 | 2008-09-08 06:35:17 +0000 | [diff] [blame] | 82 | |
| 83 | bool X86FastEmitExtend(ISD::NodeType Opc, MVT DstVT, unsigned Src, MVT SrcVT, |
| 84 | unsigned &ResultReg); |
Evan Cheng | 0de588f | 2008-09-05 21:00:03 +0000 | [diff] [blame] | 85 | |
Dan Gohman | 2ff7fd1 | 2008-09-19 22:16:54 +0000 | [diff] [blame] | 86 | bool X86SelectAddress(Value *V, X86AddressMode &AM, bool isCall); |
Dan Gohman | 0586d91 | 2008-09-10 20:11:02 +0000 | [diff] [blame] | 87 | |
Dan Gohman | 3df24e6 | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 88 | bool X86SelectLoad(Instruction *I); |
Owen Anderson | a3971df | 2008-09-04 07:08:58 +0000 | [diff] [blame] | 89 | |
| 90 | bool X86SelectStore(Instruction *I); |
Dan Gohman | 6e3f05f | 2008-09-04 23:26:51 +0000 | [diff] [blame] | 91 | |
| 92 | bool X86SelectCmp(Instruction *I); |
Dan Gohman | d89ae99 | 2008-09-05 01:06:14 +0000 | [diff] [blame] | 93 | |
| 94 | bool X86SelectZExt(Instruction *I); |
| 95 | |
| 96 | bool X86SelectBranch(Instruction *I); |
Dan Gohman | c39f4db | 2008-09-05 18:30:08 +0000 | [diff] [blame] | 97 | |
| 98 | bool X86SelectShift(Instruction *I); |
| 99 | |
| 100 | bool X86SelectSelect(Instruction *I); |
Evan Cheng | 0de588f | 2008-09-05 21:00:03 +0000 | [diff] [blame] | 101 | |
Evan Cheng | 10a8d9c | 2008-09-07 08:47:42 +0000 | [diff] [blame] | 102 | bool X86SelectTrunc(Instruction *I); |
Dan Gohman | d98d620 | 2008-10-02 22:15:21 +0000 | [diff] [blame] | 103 | |
Dan Gohman | 78efce6 | 2008-09-10 21:02:08 +0000 | [diff] [blame] | 104 | bool X86SelectFPExt(Instruction *I); |
| 105 | bool X86SelectFPTrunc(Instruction *I); |
| 106 | |
Evan Cheng | f3d4efe | 2008-09-07 09:09:33 +0000 | [diff] [blame] | 107 | bool X86SelectCall(Instruction *I); |
| 108 | |
| 109 | CCAssignFn *CCAssignFnForCall(unsigned CC, bool isTailCall = false); |
| 110 | |
Dan Gohman | 2cc3aa4 | 2008-09-25 15:24:26 +0000 | [diff] [blame] | 111 | const X86InstrInfo *getInstrInfo() const { |
Dan Gohman | 97135e1 | 2008-09-26 19:15:30 +0000 | [diff] [blame] | 112 | return getTargetMachine()->getInstrInfo(); |
| 113 | } |
| 114 | const X86TargetMachine *getTargetMachine() const { |
| 115 | return static_cast<const X86TargetMachine *>(&TM); |
Dan Gohman | 2cc3aa4 | 2008-09-25 15:24:26 +0000 | [diff] [blame] | 116 | } |
| 117 | |
Dan Gohman | 0586d91 | 2008-09-10 20:11:02 +0000 | [diff] [blame] | 118 | unsigned TargetMaterializeConstant(Constant *C); |
| 119 | |
| 120 | unsigned TargetMaterializeAlloca(AllocaInst *C); |
Evan Cheng | f3d4efe | 2008-09-07 09:09:33 +0000 | [diff] [blame] | 121 | |
| 122 | /// isScalarFPTypeInSSEReg - Return true if the specified scalar FP type is |
| 123 | /// computed in an SSE register, not on the X87 floating point stack. |
| 124 | bool isScalarFPTypeInSSEReg(MVT VT) const { |
| 125 | return (VT == MVT::f64 && X86ScalarSSEf64) || // f64 is when SSE2 |
| 126 | (VT == MVT::f32 && X86ScalarSSEf32); // f32 is when SSE1 |
| 127 | } |
| 128 | |
Dan Gohman | 9b66d73 | 2008-09-30 00:48:39 +0000 | [diff] [blame] | 129 | bool isTypeLegal(const Type *Ty, const TargetLowering &TLI, MVT &VT, |
| 130 | bool AllowI1 = false); |
Evan Cheng | c3f44b0 | 2008-09-03 00:03:49 +0000 | [diff] [blame] | 131 | }; |
Dan Gohman | 99b2182 | 2008-08-28 23:21:34 +0000 | [diff] [blame] | 132 | |
Dan Gohman | 9b66d73 | 2008-09-30 00:48:39 +0000 | [diff] [blame] | 133 | bool X86FastISel::isTypeLegal(const Type *Ty, const TargetLowering &TLI, |
| 134 | MVT &VT, bool AllowI1) { |
Evan Cheng | f3d4efe | 2008-09-07 09:09:33 +0000 | [diff] [blame] | 135 | VT = MVT::getMVT(Ty, /*HandleUnknown=*/true); |
| 136 | if (VT == MVT::Other || !VT.isSimple()) |
| 137 | // Unhandled type. Halt "fast" selection and bail. |
| 138 | return false; |
| 139 | if (VT == MVT::iPTR) |
| 140 | // Use pointer type. |
| 141 | VT = TLI.getPointerTy(); |
Dan Gohman | 9b66d73 | 2008-09-30 00:48:39 +0000 | [diff] [blame] | 142 | // For now, require SSE/SSE2 for performing floating-point operations, |
| 143 | // since x87 requires additional work. |
| 144 | if (VT == MVT::f64 && !X86ScalarSSEf64) |
| 145 | return false; |
| 146 | if (VT == MVT::f32 && !X86ScalarSSEf32) |
| 147 | return false; |
| 148 | // Similarly, no f80 support yet. |
| 149 | if (VT == MVT::f80) |
| 150 | return false; |
Evan Cheng | f3d4efe | 2008-09-07 09:09:33 +0000 | [diff] [blame] | 151 | // We only handle legal types. For example, on x86-32 the instruction |
| 152 | // selector contains all of the 64-bit instructions from x86-64, |
| 153 | // under the assumption that i64 won't be used if the target doesn't |
| 154 | // support it. |
Evan Cheng | debdea0 | 2008-09-08 17:15:42 +0000 | [diff] [blame] | 155 | return (AllowI1 && VT == MVT::i1) || TLI.isTypeLegal(VT); |
Evan Cheng | f3d4efe | 2008-09-07 09:09:33 +0000 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | #include "X86GenCallingConv.inc" |
| 159 | |
| 160 | /// CCAssignFnForCall - Selects the correct CCAssignFn for a given calling |
| 161 | /// convention. |
| 162 | CCAssignFn *X86FastISel::CCAssignFnForCall(unsigned CC, bool isTaillCall) { |
| 163 | if (Subtarget->is64Bit()) { |
| 164 | if (Subtarget->isTargetWin64()) |
| 165 | return CC_X86_Win64_C; |
| 166 | else if (CC == CallingConv::Fast && isTaillCall) |
| 167 | return CC_X86_64_TailCall; |
| 168 | else |
| 169 | return CC_X86_64_C; |
| 170 | } |
| 171 | |
| 172 | if (CC == CallingConv::X86_FastCall) |
| 173 | return CC_X86_32_FastCall; |
Evan Cheng | f3d4efe | 2008-09-07 09:09:33 +0000 | [diff] [blame] | 174 | else if (CC == CallingConv::Fast) |
| 175 | return CC_X86_32_FastCC; |
| 176 | else |
| 177 | return CC_X86_32_C; |
| 178 | } |
| 179 | |
Evan Cheng | 0de588f | 2008-09-05 21:00:03 +0000 | [diff] [blame] | 180 | /// X86FastEmitLoad - Emit a machine instruction to load a value of type VT. |
Evan Cheng | f3d4efe | 2008-09-07 09:09:33 +0000 | [diff] [blame] | 181 | /// The address is either pre-computed, i.e. Ptr, or a GlobalAddress, i.e. GV. |
Evan Cheng | 0de588f | 2008-09-05 21:00:03 +0000 | [diff] [blame] | 182 | /// Return true and the result register by reference if it is possible. |
Dan Gohman | 0586d91 | 2008-09-10 20:11:02 +0000 | [diff] [blame] | 183 | bool X86FastISel::X86FastEmitLoad(MVT VT, const X86AddressMode &AM, |
Evan Cheng | 0de588f | 2008-09-05 21:00:03 +0000 | [diff] [blame] | 184 | unsigned &ResultReg) { |
| 185 | // Get opcode and regclass of the output for the given load instruction. |
| 186 | unsigned Opc = 0; |
| 187 | const TargetRegisterClass *RC = NULL; |
| 188 | switch (VT.getSimpleVT()) { |
| 189 | default: return false; |
| 190 | case MVT::i8: |
| 191 | Opc = X86::MOV8rm; |
| 192 | RC = X86::GR8RegisterClass; |
| 193 | break; |
| 194 | case MVT::i16: |
| 195 | Opc = X86::MOV16rm; |
| 196 | RC = X86::GR16RegisterClass; |
| 197 | break; |
| 198 | case MVT::i32: |
| 199 | Opc = X86::MOV32rm; |
| 200 | RC = X86::GR32RegisterClass; |
| 201 | break; |
| 202 | case MVT::i64: |
| 203 | // Must be in x86-64 mode. |
| 204 | Opc = X86::MOV64rm; |
| 205 | RC = X86::GR64RegisterClass; |
| 206 | break; |
| 207 | case MVT::f32: |
| 208 | if (Subtarget->hasSSE1()) { |
| 209 | Opc = X86::MOVSSrm; |
| 210 | RC = X86::FR32RegisterClass; |
| 211 | } else { |
| 212 | Opc = X86::LD_Fp32m; |
| 213 | RC = X86::RFP32RegisterClass; |
| 214 | } |
| 215 | break; |
| 216 | case MVT::f64: |
| 217 | if (Subtarget->hasSSE2()) { |
| 218 | Opc = X86::MOVSDrm; |
| 219 | RC = X86::FR64RegisterClass; |
| 220 | } else { |
| 221 | Opc = X86::LD_Fp64m; |
| 222 | RC = X86::RFP64RegisterClass; |
| 223 | } |
| 224 | break; |
| 225 | case MVT::f80: |
Dan Gohman | 5af29c2 | 2008-09-26 01:39:32 +0000 | [diff] [blame] | 226 | // No f80 support yet. |
| 227 | return false; |
Evan Cheng | 0de588f | 2008-09-05 21:00:03 +0000 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | ResultReg = createResultReg(RC); |
Evan Cheng | 0de588f | 2008-09-05 21:00:03 +0000 | [diff] [blame] | 231 | addFullAddress(BuildMI(MBB, TII.get(Opc), ResultReg), AM); |
| 232 | return true; |
| 233 | } |
| 234 | |
Evan Cheng | f3d4efe | 2008-09-07 09:09:33 +0000 | [diff] [blame] | 235 | /// X86FastEmitStore - Emit a machine instruction to store a value Val of |
| 236 | /// type VT. The address is either pre-computed, consisted of a base ptr, Ptr |
| 237 | /// and a displacement offset, or a GlobalAddress, |
Evan Cheng | 0de588f | 2008-09-05 21:00:03 +0000 | [diff] [blame] | 238 | /// i.e. V. Return true if it is possible. |
| 239 | bool |
Evan Cheng | f3d4efe | 2008-09-07 09:09:33 +0000 | [diff] [blame] | 240 | X86FastISel::X86FastEmitStore(MVT VT, unsigned Val, |
Dan Gohman | 0586d91 | 2008-09-10 20:11:02 +0000 | [diff] [blame] | 241 | const X86AddressMode &AM) { |
Dan Gohman | 863890e | 2008-09-08 16:31:35 +0000 | [diff] [blame] | 242 | // Get opcode and regclass of the output for the given store instruction. |
Evan Cheng | 0de588f | 2008-09-05 21:00:03 +0000 | [diff] [blame] | 243 | unsigned Opc = 0; |
| 244 | const TargetRegisterClass *RC = NULL; |
| 245 | switch (VT.getSimpleVT()) { |
| 246 | default: return false; |
| 247 | case MVT::i8: |
| 248 | Opc = X86::MOV8mr; |
| 249 | RC = X86::GR8RegisterClass; |
| 250 | break; |
| 251 | case MVT::i16: |
| 252 | Opc = X86::MOV16mr; |
| 253 | RC = X86::GR16RegisterClass; |
| 254 | break; |
| 255 | case MVT::i32: |
| 256 | Opc = X86::MOV32mr; |
| 257 | RC = X86::GR32RegisterClass; |
| 258 | break; |
| 259 | case MVT::i64: |
| 260 | // Must be in x86-64 mode. |
| 261 | Opc = X86::MOV64mr; |
| 262 | RC = X86::GR64RegisterClass; |
| 263 | break; |
| 264 | case MVT::f32: |
| 265 | if (Subtarget->hasSSE1()) { |
| 266 | Opc = X86::MOVSSmr; |
| 267 | RC = X86::FR32RegisterClass; |
| 268 | } else { |
| 269 | Opc = X86::ST_Fp32m; |
| 270 | RC = X86::RFP32RegisterClass; |
| 271 | } |
| 272 | break; |
| 273 | case MVT::f64: |
| 274 | if (Subtarget->hasSSE2()) { |
| 275 | Opc = X86::MOVSDmr; |
| 276 | RC = X86::FR64RegisterClass; |
| 277 | } else { |
| 278 | Opc = X86::ST_Fp64m; |
| 279 | RC = X86::RFP64RegisterClass; |
| 280 | } |
| 281 | break; |
| 282 | case MVT::f80: |
Dan Gohman | 5af29c2 | 2008-09-26 01:39:32 +0000 | [diff] [blame] | 283 | // No f80 support yet. |
| 284 | return false; |
Evan Cheng | 0de588f | 2008-09-05 21:00:03 +0000 | [diff] [blame] | 285 | } |
| 286 | |
Evan Cheng | f3d4efe | 2008-09-07 09:09:33 +0000 | [diff] [blame] | 287 | addFullAddress(BuildMI(MBB, TII.get(Opc)), AM).addReg(Val); |
Evan Cheng | 0de588f | 2008-09-05 21:00:03 +0000 | [diff] [blame] | 288 | return true; |
| 289 | } |
| 290 | |
Evan Cheng | 24e3a90 | 2008-09-08 06:35:17 +0000 | [diff] [blame] | 291 | /// X86FastEmitExtend - Emit a machine instruction to extend a value Src of |
| 292 | /// type SrcVT to type DstVT using the specified extension opcode Opc (e.g. |
| 293 | /// ISD::SIGN_EXTEND). |
| 294 | bool X86FastISel::X86FastEmitExtend(ISD::NodeType Opc, MVT DstVT, |
| 295 | unsigned Src, MVT SrcVT, |
| 296 | unsigned &ResultReg) { |
Owen Anderson | ac34a00 | 2008-09-11 19:44:55 +0000 | [diff] [blame] | 297 | unsigned RR = FastEmit_r(SrcVT.getSimpleVT(), DstVT.getSimpleVT(), Opc, Src); |
| 298 | |
| 299 | if (RR != 0) { |
| 300 | ResultReg = RR; |
| 301 | return true; |
| 302 | } else |
| 303 | return false; |
Evan Cheng | 24e3a90 | 2008-09-08 06:35:17 +0000 | [diff] [blame] | 304 | } |
| 305 | |
Dan Gohman | 0586d91 | 2008-09-10 20:11:02 +0000 | [diff] [blame] | 306 | /// X86SelectAddress - Attempt to fill in an address from the given value. |
| 307 | /// |
Dan Gohman | 2ff7fd1 | 2008-09-19 22:16:54 +0000 | [diff] [blame] | 308 | bool X86FastISel::X86SelectAddress(Value *V, X86AddressMode &AM, bool isCall) { |
Dan Gohman | 3589308 | 2008-09-18 23:23:44 +0000 | [diff] [blame] | 309 | User *U; |
| 310 | unsigned Opcode = Instruction::UserOp1; |
| 311 | if (Instruction *I = dyn_cast<Instruction>(V)) { |
| 312 | Opcode = I->getOpcode(); |
| 313 | U = I; |
| 314 | } else if (ConstantExpr *C = dyn_cast<ConstantExpr>(V)) { |
| 315 | Opcode = C->getOpcode(); |
| 316 | U = C; |
| 317 | } |
Dan Gohman | 0586d91 | 2008-09-10 20:11:02 +0000 | [diff] [blame] | 318 | |
Dan Gohman | 3589308 | 2008-09-18 23:23:44 +0000 | [diff] [blame] | 319 | switch (Opcode) { |
| 320 | default: break; |
| 321 | case Instruction::BitCast: |
| 322 | // Look past bitcasts. |
Dan Gohman | 2ff7fd1 | 2008-09-19 22:16:54 +0000 | [diff] [blame] | 323 | return X86SelectAddress(U->getOperand(0), AM, isCall); |
Dan Gohman | 3589308 | 2008-09-18 23:23:44 +0000 | [diff] [blame] | 324 | |
| 325 | case Instruction::IntToPtr: |
| 326 | // Look past no-op inttoptrs. |
| 327 | if (TLI.getValueType(U->getOperand(0)->getType()) == TLI.getPointerTy()) |
Dan Gohman | 2ff7fd1 | 2008-09-19 22:16:54 +0000 | [diff] [blame] | 328 | return X86SelectAddress(U->getOperand(0), AM, isCall); |
Dan Gohman | 3589308 | 2008-09-18 23:23:44 +0000 | [diff] [blame] | 329 | |
| 330 | case Instruction::PtrToInt: |
| 331 | // Look past no-op ptrtoints. |
| 332 | if (TLI.getValueType(U->getType()) == TLI.getPointerTy()) |
Dan Gohman | 2ff7fd1 | 2008-09-19 22:16:54 +0000 | [diff] [blame] | 333 | return X86SelectAddress(U->getOperand(0), AM, isCall); |
Dan Gohman | 3589308 | 2008-09-18 23:23:44 +0000 | [diff] [blame] | 334 | |
| 335 | case Instruction::Alloca: { |
Dan Gohman | 2ff7fd1 | 2008-09-19 22:16:54 +0000 | [diff] [blame] | 336 | if (isCall) break; |
Dan Gohman | 3589308 | 2008-09-18 23:23:44 +0000 | [diff] [blame] | 337 | // Do static allocas. |
| 338 | const AllocaInst *A = cast<AllocaInst>(V); |
Dan Gohman | 0586d91 | 2008-09-10 20:11:02 +0000 | [diff] [blame] | 339 | DenseMap<const AllocaInst*, int>::iterator SI = StaticAllocaMap.find(A); |
Dan Gohman | 97135e1 | 2008-09-26 19:15:30 +0000 | [diff] [blame] | 340 | if (SI != StaticAllocaMap.end()) { |
| 341 | AM.BaseType = X86AddressMode::FrameIndexBase; |
| 342 | AM.Base.FrameIndex = SI->second; |
| 343 | return true; |
| 344 | } |
| 345 | break; |
Dan Gohman | 3589308 | 2008-09-18 23:23:44 +0000 | [diff] [blame] | 346 | } |
| 347 | |
| 348 | case Instruction::Add: { |
Dan Gohman | 2ff7fd1 | 2008-09-19 22:16:54 +0000 | [diff] [blame] | 349 | if (isCall) break; |
Dan Gohman | 3589308 | 2008-09-18 23:23:44 +0000 | [diff] [blame] | 350 | // Adds of constants are common and easy enough. |
| 351 | if (ConstantInt *CI = dyn_cast<ConstantInt>(U->getOperand(1))) { |
Dan Gohman | 09aae46 | 2008-09-26 20:04:15 +0000 | [diff] [blame] | 352 | uint64_t Disp = (int32_t)AM.Disp + (uint64_t)CI->getSExtValue(); |
| 353 | // They have to fit in the 32-bit signed displacement field though. |
| 354 | if (isInt32(Disp)) { |
| 355 | AM.Disp = (uint32_t)Disp; |
| 356 | return X86SelectAddress(U->getOperand(0), AM, isCall); |
| 357 | } |
Dan Gohman | 0586d91 | 2008-09-10 20:11:02 +0000 | [diff] [blame] | 358 | } |
Dan Gohman | 3589308 | 2008-09-18 23:23:44 +0000 | [diff] [blame] | 359 | break; |
| 360 | } |
| 361 | |
| 362 | case Instruction::GetElementPtr: { |
Dan Gohman | 2ff7fd1 | 2008-09-19 22:16:54 +0000 | [diff] [blame] | 363 | if (isCall) break; |
Dan Gohman | 3589308 | 2008-09-18 23:23:44 +0000 | [diff] [blame] | 364 | // Pattern-match simple GEPs. |
Dan Gohman | 09aae46 | 2008-09-26 20:04:15 +0000 | [diff] [blame] | 365 | uint64_t Disp = (int32_t)AM.Disp; |
Dan Gohman | 3589308 | 2008-09-18 23:23:44 +0000 | [diff] [blame] | 366 | unsigned IndexReg = AM.IndexReg; |
| 367 | unsigned Scale = AM.Scale; |
| 368 | gep_type_iterator GTI = gep_type_begin(U); |
| 369 | // Look at all but the last index. Constants can be folded, |
| 370 | // and one dynamic index can be handled, if the scale is supported. |
| 371 | for (User::op_iterator i = U->op_begin() + 1, e = U->op_end(); |
| 372 | i != e; ++i, ++GTI) { |
| 373 | Value *Op = *i; |
| 374 | if (const StructType *STy = dyn_cast<StructType>(*GTI)) { |
| 375 | const StructLayout *SL = TD.getStructLayout(STy); |
| 376 | unsigned Idx = cast<ConstantInt>(Op)->getZExtValue(); |
| 377 | Disp += SL->getElementOffset(Idx); |
| 378 | } else { |
| 379 | uint64_t S = TD.getABITypeSize(GTI.getIndexedType()); |
| 380 | if (ConstantInt *CI = dyn_cast<ConstantInt>(Op)) { |
| 381 | // Constant-offset addressing. |
Dan Gohman | 09aae46 | 2008-09-26 20:04:15 +0000 | [diff] [blame] | 382 | Disp += CI->getSExtValue() * S; |
Dan Gohman | 3589308 | 2008-09-18 23:23:44 +0000 | [diff] [blame] | 383 | } else if (IndexReg == 0 && |
Dan Gohman | 97135e1 | 2008-09-26 19:15:30 +0000 | [diff] [blame] | 384 | (!AM.GV || |
| 385 | !getTargetMachine()->symbolicAddressesAreRIPRel()) && |
Dan Gohman | 3589308 | 2008-09-18 23:23:44 +0000 | [diff] [blame] | 386 | (S == 1 || S == 2 || S == 4 || S == 8)) { |
| 387 | // Scaled-index addressing. |
| 388 | Scale = S; |
| 389 | IndexReg = getRegForValue(Op); |
| 390 | if (IndexReg == 0) |
| 391 | return false; |
| 392 | } else |
| 393 | // Unsupported. |
| 394 | goto unsupported_gep; |
| 395 | } |
| 396 | } |
Dan Gohman | 09aae46 | 2008-09-26 20:04:15 +0000 | [diff] [blame] | 397 | // Check for displacement overflow. |
| 398 | if (!isInt32(Disp)) |
| 399 | break; |
Dan Gohman | 3589308 | 2008-09-18 23:23:44 +0000 | [diff] [blame] | 400 | // Ok, the GEP indices were covered by constant-offset and scaled-index |
| 401 | // addressing. Update the address state and move on to examining the base. |
| 402 | AM.IndexReg = IndexReg; |
| 403 | AM.Scale = Scale; |
Dan Gohman | 09aae46 | 2008-09-26 20:04:15 +0000 | [diff] [blame] | 404 | AM.Disp = (uint32_t)Disp; |
Dan Gohman | 2ff7fd1 | 2008-09-19 22:16:54 +0000 | [diff] [blame] | 405 | return X86SelectAddress(U->getOperand(0), AM, isCall); |
Dan Gohman | 3589308 | 2008-09-18 23:23:44 +0000 | [diff] [blame] | 406 | unsupported_gep: |
| 407 | // Ok, the GEP indices weren't all covered. |
| 408 | break; |
| 409 | } |
| 410 | } |
| 411 | |
| 412 | // Handle constant address. |
Dan Gohman | 2ff7fd1 | 2008-09-19 22:16:54 +0000 | [diff] [blame] | 413 | if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) { |
Dan Gohman | 2cc3aa4 | 2008-09-25 15:24:26 +0000 | [diff] [blame] | 414 | // Can't handle alternate code models yet. |
| 415 | if (TM.getCodeModel() != CodeModel::Default && |
| 416 | TM.getCodeModel() != CodeModel::Small) |
| 417 | return false; |
| 418 | |
Dan Gohman | 97135e1 | 2008-09-26 19:15:30 +0000 | [diff] [blame] | 419 | // RIP-relative addresses can't have additional register operands. |
| 420 | if (getTargetMachine()->symbolicAddressesAreRIPRel() && |
| 421 | (AM.Base.Reg != 0 || AM.IndexReg != 0)) |
| 422 | return false; |
| 423 | |
Dan Gohman | 2cc3aa4 | 2008-09-25 15:24:26 +0000 | [diff] [blame] | 424 | // Set up the basic address. |
| 425 | AM.GV = GV; |
| 426 | if (!isCall && |
| 427 | TM.getRelocationModel() == Reloc::PIC_ && |
| 428 | !Subtarget->is64Bit()) |
Dan Gohman | 57c3dac | 2008-09-30 00:58:23 +0000 | [diff] [blame] | 429 | AM.Base.Reg = getInstrInfo()->getGlobalBaseReg(&MF); |
Dan Gohman | 2cc3aa4 | 2008-09-25 15:24:26 +0000 | [diff] [blame] | 430 | |
| 431 | // Emit an extra load if the ABI requires it. |
Dan Gohman | 2ff7fd1 | 2008-09-19 22:16:54 +0000 | [diff] [blame] | 432 | if (Subtarget->GVRequiresExtraLoad(GV, TM, isCall)) { |
| 433 | // Check to see if we've already materialized this |
| 434 | // value in a register in this block. |
Dan Gohman | 7e8ef60 | 2008-09-19 23:42:04 +0000 | [diff] [blame] | 435 | if (unsigned Reg = LocalValueMap[V]) { |
| 436 | AM.Base.Reg = Reg; |
Dan Gohman | 2cc3aa4 | 2008-09-25 15:24:26 +0000 | [diff] [blame] | 437 | AM.GV = 0; |
Dan Gohman | 7e8ef60 | 2008-09-19 23:42:04 +0000 | [diff] [blame] | 438 | return true; |
| 439 | } |
Dan Gohman | 2ff7fd1 | 2008-09-19 22:16:54 +0000 | [diff] [blame] | 440 | // Issue load from stub if necessary. |
| 441 | unsigned Opc = 0; |
| 442 | const TargetRegisterClass *RC = NULL; |
| 443 | if (TLI.getPointerTy() == MVT::i32) { |
| 444 | Opc = X86::MOV32rm; |
| 445 | RC = X86::GR32RegisterClass; |
| 446 | } else { |
| 447 | Opc = X86::MOV64rm; |
| 448 | RC = X86::GR64RegisterClass; |
| 449 | } |
Dan Gohman | 789ce77 | 2008-09-25 23:34:02 +0000 | [diff] [blame] | 450 | |
| 451 | X86AddressMode StubAM; |
| 452 | StubAM.Base.Reg = AM.Base.Reg; |
| 453 | StubAM.GV = AM.GV; |
Dan Gohman | 2cc3aa4 | 2008-09-25 15:24:26 +0000 | [diff] [blame] | 454 | unsigned ResultReg = createResultReg(RC); |
Dan Gohman | 789ce77 | 2008-09-25 23:34:02 +0000 | [diff] [blame] | 455 | addFullAddress(BuildMI(MBB, TII.get(Opc), ResultReg), StubAM); |
| 456 | |
| 457 | // Now construct the final address. Note that the Disp, Scale, |
| 458 | // and Index values may already be set here. |
Dan Gohman | 2cc3aa4 | 2008-09-25 15:24:26 +0000 | [diff] [blame] | 459 | AM.Base.Reg = ResultReg; |
| 460 | AM.GV = 0; |
Dan Gohman | 789ce77 | 2008-09-25 23:34:02 +0000 | [diff] [blame] | 461 | |
Dan Gohman | 2ff7fd1 | 2008-09-19 22:16:54 +0000 | [diff] [blame] | 462 | // Prevent loading GV stub multiple times in same MBB. |
| 463 | LocalValueMap[V] = AM.Base.Reg; |
Dan Gohman | 2ff7fd1 | 2008-09-19 22:16:54 +0000 | [diff] [blame] | 464 | } |
| 465 | return true; |
Dan Gohman | 0586d91 | 2008-09-10 20:11:02 +0000 | [diff] [blame] | 466 | } |
| 467 | |
Dan Gohman | 97135e1 | 2008-09-26 19:15:30 +0000 | [diff] [blame] | 468 | // If all else fails, try to materialize the value in a register. |
Dan Gohman | 7962e85 | 2008-09-29 21:13:15 +0000 | [diff] [blame] | 469 | if (!AM.GV || !getTargetMachine()->symbolicAddressesAreRIPRel()) { |
Dan Gohman | 97135e1 | 2008-09-26 19:15:30 +0000 | [diff] [blame] | 470 | if (AM.Base.Reg == 0) { |
| 471 | AM.Base.Reg = getRegForValue(V); |
| 472 | return AM.Base.Reg != 0; |
| 473 | } |
| 474 | if (AM.IndexReg == 0) { |
| 475 | assert(AM.Scale == 1 && "Scale with no index!"); |
| 476 | AM.IndexReg = getRegForValue(V); |
| 477 | return AM.IndexReg != 0; |
| 478 | } |
| 479 | } |
| 480 | |
| 481 | return false; |
Dan Gohman | 0586d91 | 2008-09-10 20:11:02 +0000 | [diff] [blame] | 482 | } |
| 483 | |
Owen Anderson | a3971df | 2008-09-04 07:08:58 +0000 | [diff] [blame] | 484 | /// X86SelectStore - Select and emit code to implement store instructions. |
| 485 | bool X86FastISel::X86SelectStore(Instruction* I) { |
Evan Cheng | 24e3a90 | 2008-09-08 06:35:17 +0000 | [diff] [blame] | 486 | MVT VT; |
| 487 | if (!isTypeLegal(I->getOperand(0)->getType(), TLI, VT)) |
Owen Anderson | a3971df | 2008-09-04 07:08:58 +0000 | [diff] [blame] | 488 | return false; |
Evan Cheng | f3d4efe | 2008-09-07 09:09:33 +0000 | [diff] [blame] | 489 | unsigned Val = getRegForValue(I->getOperand(0)); |
| 490 | if (Val == 0) |
Owen Anderson | a3971df | 2008-09-04 07:08:58 +0000 | [diff] [blame] | 491 | // Unhandled operand. Halt "fast" selection and bail. |
| 492 | return false; |
| 493 | |
Dan Gohman | 0586d91 | 2008-09-10 20:11:02 +0000 | [diff] [blame] | 494 | X86AddressMode AM; |
Dan Gohman | 2ff7fd1 | 2008-09-19 22:16:54 +0000 | [diff] [blame] | 495 | if (!X86SelectAddress(I->getOperand(1), AM, false)) |
Dan Gohman | 0586d91 | 2008-09-10 20:11:02 +0000 | [diff] [blame] | 496 | return false; |
Owen Anderson | a3971df | 2008-09-04 07:08:58 +0000 | [diff] [blame] | 497 | |
Dan Gohman | 0586d91 | 2008-09-10 20:11:02 +0000 | [diff] [blame] | 498 | return X86FastEmitStore(VT, Val, AM); |
Owen Anderson | a3971df | 2008-09-04 07:08:58 +0000 | [diff] [blame] | 499 | } |
| 500 | |
Evan Cheng | 8b19e56 | 2008-09-03 06:44:39 +0000 | [diff] [blame] | 501 | /// X86SelectLoad - Select and emit code to implement load instructions. |
| 502 | /// |
Dan Gohman | 3df24e6 | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 503 | bool X86FastISel::X86SelectLoad(Instruction *I) { |
Evan Cheng | f3d4efe | 2008-09-07 09:09:33 +0000 | [diff] [blame] | 504 | MVT VT; |
| 505 | if (!isTypeLegal(I->getType(), TLI, VT)) |
Evan Cheng | 8b19e56 | 2008-09-03 06:44:39 +0000 | [diff] [blame] | 506 | return false; |
| 507 | |
Dan Gohman | 0586d91 | 2008-09-10 20:11:02 +0000 | [diff] [blame] | 508 | X86AddressMode AM; |
Dan Gohman | 2ff7fd1 | 2008-09-19 22:16:54 +0000 | [diff] [blame] | 509 | if (!X86SelectAddress(I->getOperand(0), AM, false)) |
Dan Gohman | 0586d91 | 2008-09-10 20:11:02 +0000 | [diff] [blame] | 510 | return false; |
Evan Cheng | 8b19e56 | 2008-09-03 06:44:39 +0000 | [diff] [blame] | 511 | |
Evan Cheng | 0de588f | 2008-09-05 21:00:03 +0000 | [diff] [blame] | 512 | unsigned ResultReg = 0; |
Dan Gohman | 0586d91 | 2008-09-10 20:11:02 +0000 | [diff] [blame] | 513 | if (X86FastEmitLoad(VT, AM, ResultReg)) { |
Evan Cheng | 0de588f | 2008-09-05 21:00:03 +0000 | [diff] [blame] | 514 | UpdateValueMap(I, ResultReg); |
| 515 | return true; |
Evan Cheng | 8b19e56 | 2008-09-03 06:44:39 +0000 | [diff] [blame] | 516 | } |
Evan Cheng | 0de588f | 2008-09-05 21:00:03 +0000 | [diff] [blame] | 517 | return false; |
Evan Cheng | 8b19e56 | 2008-09-03 06:44:39 +0000 | [diff] [blame] | 518 | } |
| 519 | |
Chris Lattner | 51ccb3d | 2008-10-15 04:29:23 +0000 | [diff] [blame] | 520 | static unsigned X86ChooseCmpOpcode(MVT VT) { |
Dan Gohman | d98d620 | 2008-10-02 22:15:21 +0000 | [diff] [blame] | 521 | switch (VT.getSimpleVT()) { |
Chris Lattner | 45ac17f | 2008-10-15 04:32:45 +0000 | [diff] [blame^] | 522 | default: return 0; |
| 523 | case MVT::i8: return X86::CMP8rr; |
Dan Gohman | d98d620 | 2008-10-02 22:15:21 +0000 | [diff] [blame] | 524 | case MVT::i16: return X86::CMP16rr; |
| 525 | case MVT::i32: return X86::CMP32rr; |
| 526 | case MVT::i64: return X86::CMP64rr; |
| 527 | case MVT::f32: return X86::UCOMISSrr; |
| 528 | case MVT::f64: return X86::UCOMISDrr; |
Dan Gohman | d98d620 | 2008-10-02 22:15:21 +0000 | [diff] [blame] | 529 | } |
Chris Lattner | 45ac17f | 2008-10-15 04:32:45 +0000 | [diff] [blame^] | 530 | |
Dan Gohman | d98d620 | 2008-10-02 22:15:21 +0000 | [diff] [blame] | 531 | } |
| 532 | |
Chris Lattner | 0e13c78 | 2008-10-15 04:13:29 +0000 | [diff] [blame] | 533 | /// X86ChooseCmpImmediateOpcode - If we have a comparison with RHS as the RHS |
| 534 | /// of the comparison, return an opcode that works for the compare (e.g. |
| 535 | /// CMP32ri) otherwise return 0. |
Chris Lattner | 45ac17f | 2008-10-15 04:32:45 +0000 | [diff] [blame^] | 536 | static unsigned X86ChooseCmpImmediateOpcode(MVT VT, ConstantInt *RHSC) { |
| 537 | switch (VT.getSimpleVT()) { |
Chris Lattner | 0e13c78 | 2008-10-15 04:13:29 +0000 | [diff] [blame] | 538 | // Otherwise, we can't fold the immediate into this comparison. |
Chris Lattner | 45ac17f | 2008-10-15 04:32:45 +0000 | [diff] [blame^] | 539 | default: return 0; |
| 540 | case MVT::i8: return X86::CMP8ri; |
| 541 | case MVT::i16: return X86::CMP16ri; |
| 542 | case MVT::i32: return X86::CMP32ri; |
| 543 | case MVT::i64: |
| 544 | // 64-bit comparisons are only valid if the immediate fits in a 32-bit sext |
| 545 | // field. |
| 546 | if (RHSC->getType() == Type::Int64Ty && |
| 547 | (int)RHSC->getSExtValue() == RHSC->getSExtValue()) |
| 548 | return X86::CMP64ri32; |
| 549 | return 0; |
| 550 | } |
Chris Lattner | 0e13c78 | 2008-10-15 04:13:29 +0000 | [diff] [blame] | 551 | } |
| 552 | |
Chris Lattner | 9a08a61 | 2008-10-15 04:26:38 +0000 | [diff] [blame] | 553 | bool X86FastISel::X86FastEmitCompare(Value *Op0, Value *Op1, MVT VT) { |
| 554 | unsigned Op0Reg = getRegForValue(Op0); |
| 555 | if (Op0Reg == 0) return false; |
| 556 | |
| 557 | // We have two options: compare with register or immediate. If the RHS of |
| 558 | // the compare is an immediate that we can fold into this compare, use |
| 559 | // CMPri, otherwise use CMPrr. |
| 560 | if (ConstantInt *Op1C = dyn_cast<ConstantInt>(Op1)) { |
Chris Lattner | 45ac17f | 2008-10-15 04:32:45 +0000 | [diff] [blame^] | 561 | if (unsigned CompareImmOpc = X86ChooseCmpImmediateOpcode(VT, Op1C)) { |
Chris Lattner | 9a08a61 | 2008-10-15 04:26:38 +0000 | [diff] [blame] | 562 | BuildMI(MBB, TII.get(CompareImmOpc)).addReg(Op0Reg) |
| 563 | .addImm(Op1C->getSExtValue()); |
| 564 | return true; |
| 565 | } |
| 566 | } |
| 567 | |
| 568 | unsigned CompareOpc = X86ChooseCmpOpcode(VT); |
| 569 | if (CompareOpc == 0) return false; |
| 570 | |
| 571 | unsigned Op1Reg = getRegForValue(Op1); |
| 572 | if (Op1Reg == 0) return false; |
| 573 | BuildMI(MBB, TII.get(CompareOpc)).addReg(Op0Reg).addReg(Op1Reg); |
| 574 | |
| 575 | return true; |
| 576 | } |
| 577 | |
Dan Gohman | 6e3f05f | 2008-09-04 23:26:51 +0000 | [diff] [blame] | 578 | bool X86FastISel::X86SelectCmp(Instruction *I) { |
| 579 | CmpInst *CI = cast<CmpInst>(I); |
| 580 | |
Dan Gohman | 9b66d73 | 2008-09-30 00:48:39 +0000 | [diff] [blame] | 581 | MVT VT; |
| 582 | if (!isTypeLegal(I->getOperand(0)->getType(), TLI, VT)) |
Dan Gohman | 4f22bb0 | 2008-09-05 01:33:56 +0000 | [diff] [blame] | 583 | return false; |
| 584 | |
Dan Gohman | 6e3f05f | 2008-09-04 23:26:51 +0000 | [diff] [blame] | 585 | unsigned ResultReg = createResultReg(&X86::GR8RegClass); |
Chris Lattner | 54aebde | 2008-10-15 03:47:17 +0000 | [diff] [blame] | 586 | unsigned SetCCOpc; |
Chris Lattner | 8aeeeb9 | 2008-10-15 03:52:54 +0000 | [diff] [blame] | 587 | bool SwapArgs; // false -> compare Op0, Op1. true -> compare Op1, Op0. |
Dan Gohman | 6e3f05f | 2008-09-04 23:26:51 +0000 | [diff] [blame] | 588 | switch (CI->getPredicate()) { |
| 589 | case CmpInst::FCMP_OEQ: { |
Chris Lattner | 51ccb3d | 2008-10-15 04:29:23 +0000 | [diff] [blame] | 590 | if (!X86FastEmitCompare(CI->getOperand(0), CI->getOperand(1), VT)) |
| 591 | return false; |
Chris Lattner | 9a08a61 | 2008-10-15 04:26:38 +0000 | [diff] [blame] | 592 | |
Dan Gohman | 6e3f05f | 2008-09-04 23:26:51 +0000 | [diff] [blame] | 593 | unsigned EReg = createResultReg(&X86::GR8RegClass); |
| 594 | unsigned NPReg = createResultReg(&X86::GR8RegClass); |
Dan Gohman | 6e3f05f | 2008-09-04 23:26:51 +0000 | [diff] [blame] | 595 | BuildMI(MBB, TII.get(X86::SETEr), EReg); |
| 596 | BuildMI(MBB, TII.get(X86::SETNPr), NPReg); |
| 597 | BuildMI(MBB, TII.get(X86::AND8rr), ResultReg).addReg(NPReg).addReg(EReg); |
Chris Lattner | 54aebde | 2008-10-15 03:47:17 +0000 | [diff] [blame] | 598 | UpdateValueMap(I, ResultReg); |
| 599 | return true; |
Dan Gohman | 6e3f05f | 2008-09-04 23:26:51 +0000 | [diff] [blame] | 600 | } |
| 601 | case CmpInst::FCMP_UNE: { |
Chris Lattner | 51ccb3d | 2008-10-15 04:29:23 +0000 | [diff] [blame] | 602 | if (!X86FastEmitCompare(CI->getOperand(0), CI->getOperand(1), VT)) |
| 603 | return false; |
| 604 | |
Dan Gohman | 6e3f05f | 2008-09-04 23:26:51 +0000 | [diff] [blame] | 605 | unsigned NEReg = createResultReg(&X86::GR8RegClass); |
| 606 | unsigned PReg = createResultReg(&X86::GR8RegClass); |
Dan Gohman | 6e3f05f | 2008-09-04 23:26:51 +0000 | [diff] [blame] | 607 | BuildMI(MBB, TII.get(X86::SETNEr), NEReg); |
| 608 | BuildMI(MBB, TII.get(X86::SETPr), PReg); |
| 609 | BuildMI(MBB, TII.get(X86::OR8rr), ResultReg).addReg(PReg).addReg(NEReg); |
Chris Lattner | 54aebde | 2008-10-15 03:47:17 +0000 | [diff] [blame] | 610 | UpdateValueMap(I, ResultReg); |
| 611 | return true; |
Dan Gohman | 6e3f05f | 2008-09-04 23:26:51 +0000 | [diff] [blame] | 612 | } |
Chris Lattner | 8aeeeb9 | 2008-10-15 03:52:54 +0000 | [diff] [blame] | 613 | case CmpInst::FCMP_OGT: SwapArgs = false; SetCCOpc = X86::SETAr; break; |
| 614 | case CmpInst::FCMP_OGE: SwapArgs = false; SetCCOpc = X86::SETAEr; break; |
| 615 | case CmpInst::FCMP_OLT: SwapArgs = true; SetCCOpc = X86::SETAr; break; |
| 616 | case CmpInst::FCMP_OLE: SwapArgs = true; SetCCOpc = X86::SETAEr; break; |
| 617 | case CmpInst::FCMP_ONE: SwapArgs = false; SetCCOpc = X86::SETNEr; break; |
| 618 | case CmpInst::FCMP_ORD: SwapArgs = false; SetCCOpc = X86::SETNPr; break; |
| 619 | case CmpInst::FCMP_UNO: SwapArgs = false; SetCCOpc = X86::SETPr; break; |
| 620 | case CmpInst::FCMP_UEQ: SwapArgs = false; SetCCOpc = X86::SETEr; break; |
| 621 | case CmpInst::FCMP_UGT: SwapArgs = true; SetCCOpc = X86::SETBr; break; |
| 622 | case CmpInst::FCMP_UGE: SwapArgs = true; SetCCOpc = X86::SETBEr; break; |
| 623 | case CmpInst::FCMP_ULT: SwapArgs = false; SetCCOpc = X86::SETBr; break; |
| 624 | case CmpInst::FCMP_ULE: SwapArgs = false; SetCCOpc = X86::SETBEr; break; |
| 625 | |
| 626 | case CmpInst::ICMP_EQ: SwapArgs = false; SetCCOpc = X86::SETEr; break; |
| 627 | case CmpInst::ICMP_NE: SwapArgs = false; SetCCOpc = X86::SETNEr; break; |
| 628 | case CmpInst::ICMP_UGT: SwapArgs = false; SetCCOpc = X86::SETAr; break; |
| 629 | case CmpInst::ICMP_UGE: SwapArgs = false; SetCCOpc = X86::SETAEr; break; |
| 630 | case CmpInst::ICMP_ULT: SwapArgs = false; SetCCOpc = X86::SETBr; break; |
| 631 | case CmpInst::ICMP_ULE: SwapArgs = false; SetCCOpc = X86::SETBEr; break; |
| 632 | case CmpInst::ICMP_SGT: SwapArgs = false; SetCCOpc = X86::SETGr; break; |
| 633 | case CmpInst::ICMP_SGE: SwapArgs = false; SetCCOpc = X86::SETGEr; break; |
| 634 | case CmpInst::ICMP_SLT: SwapArgs = false; SetCCOpc = X86::SETLr; break; |
| 635 | case CmpInst::ICMP_SLE: SwapArgs = false; SetCCOpc = X86::SETLEr; break; |
Dan Gohman | 6e3f05f | 2008-09-04 23:26:51 +0000 | [diff] [blame] | 636 | default: |
| 637 | return false; |
| 638 | } |
| 639 | |
Chris Lattner | 9a08a61 | 2008-10-15 04:26:38 +0000 | [diff] [blame] | 640 | Value *Op0 = CI->getOperand(0), *Op1 = CI->getOperand(1); |
Chris Lattner | 8aeeeb9 | 2008-10-15 03:52:54 +0000 | [diff] [blame] | 641 | if (SwapArgs) |
Chris Lattner | 9a08a61 | 2008-10-15 04:26:38 +0000 | [diff] [blame] | 642 | std::swap(Op0, Op1); |
Chris Lattner | 8aeeeb9 | 2008-10-15 03:52:54 +0000 | [diff] [blame] | 643 | |
Chris Lattner | 9a08a61 | 2008-10-15 04:26:38 +0000 | [diff] [blame] | 644 | // Emit a compare of Op0/Op1. |
Chris Lattner | 51ccb3d | 2008-10-15 04:29:23 +0000 | [diff] [blame] | 645 | if (!X86FastEmitCompare(Op0, Op1, VT)) |
| 646 | return false; |
Chris Lattner | 9a08a61 | 2008-10-15 04:26:38 +0000 | [diff] [blame] | 647 | |
Chris Lattner | 8aeeeb9 | 2008-10-15 03:52:54 +0000 | [diff] [blame] | 648 | BuildMI(MBB, TII.get(SetCCOpc), ResultReg); |
Dan Gohman | 6e3f05f | 2008-09-04 23:26:51 +0000 | [diff] [blame] | 649 | UpdateValueMap(I, ResultReg); |
| 650 | return true; |
| 651 | } |
Evan Cheng | 8b19e56 | 2008-09-03 06:44:39 +0000 | [diff] [blame] | 652 | |
Dan Gohman | d89ae99 | 2008-09-05 01:06:14 +0000 | [diff] [blame] | 653 | bool X86FastISel::X86SelectZExt(Instruction *I) { |
| 654 | // Special-case hack: The only i1 values we know how to produce currently |
| 655 | // set the upper bits of an i8 value to zero. |
| 656 | if (I->getType() == Type::Int8Ty && |
| 657 | I->getOperand(0)->getType() == Type::Int1Ty) { |
| 658 | unsigned ResultReg = getRegForValue(I->getOperand(0)); |
Dan Gohman | f52550b | 2008-09-05 01:15:35 +0000 | [diff] [blame] | 659 | if (ResultReg == 0) return false; |
Dan Gohman | d89ae99 | 2008-09-05 01:06:14 +0000 | [diff] [blame] | 660 | UpdateValueMap(I, ResultReg); |
| 661 | return true; |
| 662 | } |
| 663 | |
| 664 | return false; |
| 665 | } |
| 666 | |
Chris Lattner | 9a08a61 | 2008-10-15 04:26:38 +0000 | [diff] [blame] | 667 | |
Dan Gohman | d89ae99 | 2008-09-05 01:06:14 +0000 | [diff] [blame] | 668 | bool X86FastISel::X86SelectBranch(Instruction *I) { |
Dan Gohman | d89ae99 | 2008-09-05 01:06:14 +0000 | [diff] [blame] | 669 | // Unconditional branches are selected by tablegen-generated code. |
Dan Gohman | d98d620 | 2008-10-02 22:15:21 +0000 | [diff] [blame] | 670 | // Handle a conditional branch. |
| 671 | BranchInst *BI = cast<BranchInst>(I); |
Dan Gohman | d89ae99 | 2008-09-05 01:06:14 +0000 | [diff] [blame] | 672 | MachineBasicBlock *TrueMBB = MBBMap[BI->getSuccessor(0)]; |
| 673 | MachineBasicBlock *FalseMBB = MBBMap[BI->getSuccessor(1)]; |
| 674 | |
Dan Gohman | d98d620 | 2008-10-02 22:15:21 +0000 | [diff] [blame] | 675 | // Fold the common case of a conditional branch with a comparison. |
| 676 | if (CmpInst *CI = dyn_cast<CmpInst>(BI->getCondition())) { |
| 677 | if (CI->hasOneUse()) { |
| 678 | MVT VT = TLI.getValueType(CI->getOperand(0)->getType()); |
Dan Gohman | d89ae99 | 2008-09-05 01:06:14 +0000 | [diff] [blame] | 679 | |
Dan Gohman | d98d620 | 2008-10-02 22:15:21 +0000 | [diff] [blame] | 680 | // Try to take advantage of fallthrough opportunities. |
| 681 | CmpInst::Predicate Predicate = CI->getPredicate(); |
| 682 | if (MBB->isLayoutSuccessor(TrueMBB)) { |
| 683 | std::swap(TrueMBB, FalseMBB); |
| 684 | Predicate = CmpInst::getInversePredicate(Predicate); |
| 685 | } |
| 686 | |
Chris Lattner | 871d246 | 2008-10-15 03:58:05 +0000 | [diff] [blame] | 687 | bool SwapArgs; // false -> compare Op0, Op1. true -> compare Op1, Op0. |
| 688 | unsigned BranchOpc; // Opcode to jump on, e.g. "X86::JA" |
| 689 | |
Dan Gohman | d98d620 | 2008-10-02 22:15:21 +0000 | [diff] [blame] | 690 | switch (Predicate) { |
Chris Lattner | 871d246 | 2008-10-15 03:58:05 +0000 | [diff] [blame] | 691 | case CmpInst::FCMP_OGT: SwapArgs = false; BranchOpc = X86::JA; break; |
| 692 | case CmpInst::FCMP_OGE: SwapArgs = false; BranchOpc = X86::JAE; break; |
| 693 | case CmpInst::FCMP_OLT: SwapArgs = true; BranchOpc = X86::JA; break; |
| 694 | case CmpInst::FCMP_OLE: SwapArgs = true; BranchOpc = X86::JAE; break; |
| 695 | case CmpInst::FCMP_ONE: SwapArgs = false; BranchOpc = X86::JNE; break; |
| 696 | case CmpInst::FCMP_ORD: SwapArgs = false; BranchOpc = X86::JNP; break; |
| 697 | case CmpInst::FCMP_UNO: SwapArgs = false; BranchOpc = X86::JP; break; |
| 698 | case CmpInst::FCMP_UEQ: SwapArgs = false; BranchOpc = X86::JE; break; |
| 699 | case CmpInst::FCMP_UGT: SwapArgs = true; BranchOpc = X86::JB; break; |
| 700 | case CmpInst::FCMP_UGE: SwapArgs = true; BranchOpc = X86::JBE; break; |
| 701 | case CmpInst::FCMP_ULT: SwapArgs = false; BranchOpc = X86::JB; break; |
| 702 | case CmpInst::FCMP_ULE: SwapArgs = false; BranchOpc = X86::JBE; break; |
Chris Lattner | 9a08a61 | 2008-10-15 04:26:38 +0000 | [diff] [blame] | 703 | |
Chris Lattner | 871d246 | 2008-10-15 03:58:05 +0000 | [diff] [blame] | 704 | case CmpInst::ICMP_EQ: SwapArgs = false; BranchOpc = X86::JE; break; |
| 705 | case CmpInst::ICMP_NE: SwapArgs = false; BranchOpc = X86::JNE; break; |
| 706 | case CmpInst::ICMP_UGT: SwapArgs = false; BranchOpc = X86::JA; break; |
| 707 | case CmpInst::ICMP_UGE: SwapArgs = false; BranchOpc = X86::JAE; break; |
| 708 | case CmpInst::ICMP_ULT: SwapArgs = false; BranchOpc = X86::JB; break; |
| 709 | case CmpInst::ICMP_ULE: SwapArgs = false; BranchOpc = X86::JBE; break; |
| 710 | case CmpInst::ICMP_SGT: SwapArgs = false; BranchOpc = X86::JG; break; |
| 711 | case CmpInst::ICMP_SGE: SwapArgs = false; BranchOpc = X86::JGE; break; |
| 712 | case CmpInst::ICMP_SLT: SwapArgs = false; BranchOpc = X86::JL; break; |
| 713 | case CmpInst::ICMP_SLE: SwapArgs = false; BranchOpc = X86::JLE; break; |
Dan Gohman | d98d620 | 2008-10-02 22:15:21 +0000 | [diff] [blame] | 714 | default: |
| 715 | return false; |
| 716 | } |
Chris Lattner | 54aebde | 2008-10-15 03:47:17 +0000 | [diff] [blame] | 717 | |
Chris Lattner | 709d829 | 2008-10-15 04:02:26 +0000 | [diff] [blame] | 718 | Value *Op0 = CI->getOperand(0), *Op1 = CI->getOperand(1); |
| 719 | if (SwapArgs) |
| 720 | std::swap(Op0, Op1); |
| 721 | |
Chris Lattner | 9a08a61 | 2008-10-15 04:26:38 +0000 | [diff] [blame] | 722 | // Emit a compare of the LHS and RHS, setting the flags. |
| 723 | if (!X86FastEmitCompare(Op0, Op1, VT)) |
| 724 | return false; |
Chris Lattner | 0e13c78 | 2008-10-15 04:13:29 +0000 | [diff] [blame] | 725 | |
Chris Lattner | 54aebde | 2008-10-15 03:47:17 +0000 | [diff] [blame] | 726 | BuildMI(MBB, TII.get(BranchOpc)).addMBB(TrueMBB); |
Dan Gohman | d98d620 | 2008-10-02 22:15:21 +0000 | [diff] [blame] | 727 | FastEmitBranch(FalseMBB); |
Dan Gohman | 8c3f8b6 | 2008-10-07 22:10:33 +0000 | [diff] [blame] | 728 | MBB->addSuccessor(TrueMBB); |
Dan Gohman | d98d620 | 2008-10-02 22:15:21 +0000 | [diff] [blame] | 729 | return true; |
| 730 | } |
| 731 | } |
| 732 | |
| 733 | // Otherwise do a clumsy setcc and re-test it. |
| 734 | unsigned OpReg = getRegForValue(BI->getCondition()); |
| 735 | if (OpReg == 0) return false; |
| 736 | |
| 737 | BuildMI(MBB, TII.get(X86::TEST8rr)).addReg(OpReg).addReg(OpReg); |
Dan Gohman | d98d620 | 2008-10-02 22:15:21 +0000 | [diff] [blame] | 738 | BuildMI(MBB, TII.get(X86::JNE)).addMBB(TrueMBB); |
Dan Gohman | d98d620 | 2008-10-02 22:15:21 +0000 | [diff] [blame] | 739 | FastEmitBranch(FalseMBB); |
Dan Gohman | 8c3f8b6 | 2008-10-07 22:10:33 +0000 | [diff] [blame] | 740 | MBB->addSuccessor(TrueMBB); |
Dan Gohman | d89ae99 | 2008-09-05 01:06:14 +0000 | [diff] [blame] | 741 | return true; |
| 742 | } |
| 743 | |
Dan Gohman | c39f4db | 2008-09-05 18:30:08 +0000 | [diff] [blame] | 744 | bool X86FastISel::X86SelectShift(Instruction *I) { |
Chris Lattner | 743922e | 2008-09-21 21:44:29 +0000 | [diff] [blame] | 745 | unsigned CReg = 0, OpReg = 0, OpImm = 0; |
Dan Gohman | c39f4db | 2008-09-05 18:30:08 +0000 | [diff] [blame] | 746 | const TargetRegisterClass *RC = NULL; |
| 747 | if (I->getType() == Type::Int8Ty) { |
| 748 | CReg = X86::CL; |
| 749 | RC = &X86::GR8RegClass; |
| 750 | switch (I->getOpcode()) { |
Chris Lattner | 743922e | 2008-09-21 21:44:29 +0000 | [diff] [blame] | 751 | case Instruction::LShr: OpReg = X86::SHR8rCL; OpImm = X86::SHR8ri; break; |
| 752 | case Instruction::AShr: OpReg = X86::SAR8rCL; OpImm = X86::SAR8ri; break; |
| 753 | case Instruction::Shl: OpReg = X86::SHL8rCL; OpImm = X86::SHL8ri; break; |
Dan Gohman | c39f4db | 2008-09-05 18:30:08 +0000 | [diff] [blame] | 754 | default: return false; |
| 755 | } |
| 756 | } else if (I->getType() == Type::Int16Ty) { |
| 757 | CReg = X86::CX; |
| 758 | RC = &X86::GR16RegClass; |
| 759 | switch (I->getOpcode()) { |
Chris Lattner | 743922e | 2008-09-21 21:44:29 +0000 | [diff] [blame] | 760 | case Instruction::LShr: OpReg = X86::SHR16rCL; OpImm = X86::SHR16ri; break; |
| 761 | case Instruction::AShr: OpReg = X86::SAR16rCL; OpImm = X86::SAR16ri; break; |
| 762 | case Instruction::Shl: OpReg = X86::SHL16rCL; OpImm = X86::SHL16ri; break; |
Dan Gohman | c39f4db | 2008-09-05 18:30:08 +0000 | [diff] [blame] | 763 | default: return false; |
| 764 | } |
| 765 | } else if (I->getType() == Type::Int32Ty) { |
| 766 | CReg = X86::ECX; |
| 767 | RC = &X86::GR32RegClass; |
| 768 | switch (I->getOpcode()) { |
Chris Lattner | 743922e | 2008-09-21 21:44:29 +0000 | [diff] [blame] | 769 | case Instruction::LShr: OpReg = X86::SHR32rCL; OpImm = X86::SHR32ri; break; |
| 770 | case Instruction::AShr: OpReg = X86::SAR32rCL; OpImm = X86::SAR32ri; break; |
| 771 | case Instruction::Shl: OpReg = X86::SHL32rCL; OpImm = X86::SHL32ri; break; |
Dan Gohman | c39f4db | 2008-09-05 18:30:08 +0000 | [diff] [blame] | 772 | default: return false; |
| 773 | } |
| 774 | } else if (I->getType() == Type::Int64Ty) { |
| 775 | CReg = X86::RCX; |
| 776 | RC = &X86::GR64RegClass; |
| 777 | switch (I->getOpcode()) { |
Chris Lattner | 743922e | 2008-09-21 21:44:29 +0000 | [diff] [blame] | 778 | case Instruction::LShr: OpReg = X86::SHR64rCL; OpImm = X86::SHR64ri; break; |
| 779 | case Instruction::AShr: OpReg = X86::SAR64rCL; OpImm = X86::SAR64ri; break; |
| 780 | case Instruction::Shl: OpReg = X86::SHL64rCL; OpImm = X86::SHL64ri; break; |
Dan Gohman | c39f4db | 2008-09-05 18:30:08 +0000 | [diff] [blame] | 781 | default: return false; |
| 782 | } |
| 783 | } else { |
| 784 | return false; |
| 785 | } |
| 786 | |
Dan Gohman | f58cb6d | 2008-09-05 21:27:34 +0000 | [diff] [blame] | 787 | MVT VT = MVT::getMVT(I->getType(), /*HandleUnknown=*/true); |
Dan Gohman | 9b66d73 | 2008-09-30 00:48:39 +0000 | [diff] [blame] | 788 | if (VT == MVT::Other || !isTypeLegal(I->getType(), TLI, VT)) |
Dan Gohman | f58cb6d | 2008-09-05 21:27:34 +0000 | [diff] [blame] | 789 | return false; |
| 790 | |
Dan Gohman | c39f4db | 2008-09-05 18:30:08 +0000 | [diff] [blame] | 791 | unsigned Op0Reg = getRegForValue(I->getOperand(0)); |
| 792 | if (Op0Reg == 0) return false; |
Chris Lattner | 743922e | 2008-09-21 21:44:29 +0000 | [diff] [blame] | 793 | |
| 794 | // Fold immediate in shl(x,3). |
| 795 | if (ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1))) { |
| 796 | unsigned ResultReg = createResultReg(RC); |
| 797 | BuildMI(MBB, TII.get(OpImm), |
| 798 | ResultReg).addReg(Op0Reg).addImm(CI->getZExtValue()); |
| 799 | UpdateValueMap(I, ResultReg); |
| 800 | return true; |
| 801 | } |
| 802 | |
Dan Gohman | c39f4db | 2008-09-05 18:30:08 +0000 | [diff] [blame] | 803 | unsigned Op1Reg = getRegForValue(I->getOperand(1)); |
| 804 | if (Op1Reg == 0) return false; |
| 805 | TII.copyRegToReg(*MBB, MBB->end(), CReg, Op1Reg, RC, RC); |
Dan Gohman | 145b828 | 2008-10-07 21:50:36 +0000 | [diff] [blame] | 806 | |
| 807 | // The shift instruction uses X86::CL. If we defined a super-register |
| 808 | // of X86::CL, emit an EXTRACT_SUBREG to precisely describe what |
| 809 | // we're doing here. |
| 810 | if (CReg != X86::CL) |
| 811 | BuildMI(MBB, TII.get(TargetInstrInfo::EXTRACT_SUBREG), X86::CL) |
| 812 | .addReg(CReg).addImm(X86::SUBREG_8BIT); |
| 813 | |
Dan Gohman | c39f4db | 2008-09-05 18:30:08 +0000 | [diff] [blame] | 814 | unsigned ResultReg = createResultReg(RC); |
Dan Gohman | 145b828 | 2008-10-07 21:50:36 +0000 | [diff] [blame] | 815 | BuildMI(MBB, TII.get(OpReg), ResultReg).addReg(Op0Reg); |
Dan Gohman | c39f4db | 2008-09-05 18:30:08 +0000 | [diff] [blame] | 816 | UpdateValueMap(I, ResultReg); |
| 817 | return true; |
| 818 | } |
| 819 | |
| 820 | bool X86FastISel::X86SelectSelect(Instruction *I) { |
Dan Gohman | f58cb6d | 2008-09-05 21:27:34 +0000 | [diff] [blame] | 821 | const Type *Ty = I->getType(); |
Dan Gohman | c39f4db | 2008-09-05 18:30:08 +0000 | [diff] [blame] | 822 | if (isa<PointerType>(Ty)) |
Dan Gohman | 1fbc3cd | 2008-09-18 18:26:43 +0000 | [diff] [blame] | 823 | Ty = TD.getIntPtrType(); |
Dan Gohman | c39f4db | 2008-09-05 18:30:08 +0000 | [diff] [blame] | 824 | |
| 825 | unsigned Opc = 0; |
| 826 | const TargetRegisterClass *RC = NULL; |
| 827 | if (Ty == Type::Int16Ty) { |
Dan Gohman | 31d2691 | 2008-09-05 21:13:04 +0000 | [diff] [blame] | 828 | Opc = X86::CMOVE16rr; |
Dan Gohman | c39f4db | 2008-09-05 18:30:08 +0000 | [diff] [blame] | 829 | RC = &X86::GR16RegClass; |
| 830 | } else if (Ty == Type::Int32Ty) { |
Dan Gohman | 31d2691 | 2008-09-05 21:13:04 +0000 | [diff] [blame] | 831 | Opc = X86::CMOVE32rr; |
Dan Gohman | c39f4db | 2008-09-05 18:30:08 +0000 | [diff] [blame] | 832 | RC = &X86::GR32RegClass; |
| 833 | } else if (Ty == Type::Int64Ty) { |
Dan Gohman | 31d2691 | 2008-09-05 21:13:04 +0000 | [diff] [blame] | 834 | Opc = X86::CMOVE64rr; |
Dan Gohman | c39f4db | 2008-09-05 18:30:08 +0000 | [diff] [blame] | 835 | RC = &X86::GR64RegClass; |
| 836 | } else { |
| 837 | return false; |
| 838 | } |
| 839 | |
Dan Gohman | f58cb6d | 2008-09-05 21:27:34 +0000 | [diff] [blame] | 840 | MVT VT = MVT::getMVT(Ty, /*HandleUnknown=*/true); |
Dan Gohman | 9b66d73 | 2008-09-30 00:48:39 +0000 | [diff] [blame] | 841 | if (VT == MVT::Other || !isTypeLegal(Ty, TLI, VT)) |
Dan Gohman | f58cb6d | 2008-09-05 21:27:34 +0000 | [diff] [blame] | 842 | return false; |
| 843 | |
Dan Gohman | c39f4db | 2008-09-05 18:30:08 +0000 | [diff] [blame] | 844 | unsigned Op0Reg = getRegForValue(I->getOperand(0)); |
| 845 | if (Op0Reg == 0) return false; |
| 846 | unsigned Op1Reg = getRegForValue(I->getOperand(1)); |
| 847 | if (Op1Reg == 0) return false; |
| 848 | unsigned Op2Reg = getRegForValue(I->getOperand(2)); |
| 849 | if (Op2Reg == 0) return false; |
| 850 | |
| 851 | BuildMI(MBB, TII.get(X86::TEST8rr)).addReg(Op0Reg).addReg(Op0Reg); |
| 852 | unsigned ResultReg = createResultReg(RC); |
| 853 | BuildMI(MBB, TII.get(Opc), ResultReg).addReg(Op1Reg).addReg(Op2Reg); |
| 854 | UpdateValueMap(I, ResultReg); |
| 855 | return true; |
| 856 | } |
| 857 | |
Dan Gohman | 78efce6 | 2008-09-10 21:02:08 +0000 | [diff] [blame] | 858 | bool X86FastISel::X86SelectFPExt(Instruction *I) { |
| 859 | if (Subtarget->hasSSE2()) { |
| 860 | if (I->getType() == Type::DoubleTy) { |
| 861 | Value *V = I->getOperand(0); |
| 862 | if (V->getType() == Type::FloatTy) { |
| 863 | unsigned OpReg = getRegForValue(V); |
| 864 | if (OpReg == 0) return false; |
| 865 | unsigned ResultReg = createResultReg(X86::FR64RegisterClass); |
| 866 | BuildMI(MBB, TII.get(X86::CVTSS2SDrr), ResultReg).addReg(OpReg); |
| 867 | UpdateValueMap(I, ResultReg); |
| 868 | return true; |
| 869 | } |
| 870 | } |
| 871 | } |
| 872 | |
| 873 | return false; |
| 874 | } |
| 875 | |
| 876 | bool X86FastISel::X86SelectFPTrunc(Instruction *I) { |
| 877 | if (Subtarget->hasSSE2()) { |
| 878 | if (I->getType() == Type::FloatTy) { |
| 879 | Value *V = I->getOperand(0); |
| 880 | if (V->getType() == Type::DoubleTy) { |
| 881 | unsigned OpReg = getRegForValue(V); |
| 882 | if (OpReg == 0) return false; |
| 883 | unsigned ResultReg = createResultReg(X86::FR32RegisterClass); |
| 884 | BuildMI(MBB, TII.get(X86::CVTSD2SSrr), ResultReg).addReg(OpReg); |
| 885 | UpdateValueMap(I, ResultReg); |
| 886 | return true; |
| 887 | } |
| 888 | } |
| 889 | } |
| 890 | |
| 891 | return false; |
| 892 | } |
| 893 | |
Evan Cheng | 10a8d9c | 2008-09-07 08:47:42 +0000 | [diff] [blame] | 894 | bool X86FastISel::X86SelectTrunc(Instruction *I) { |
| 895 | if (Subtarget->is64Bit()) |
| 896 | // All other cases should be handled by the tblgen generated code. |
| 897 | return false; |
| 898 | MVT SrcVT = TLI.getValueType(I->getOperand(0)->getType()); |
| 899 | MVT DstVT = TLI.getValueType(I->getType()); |
| 900 | if (DstVT != MVT::i8) |
| 901 | // All other cases should be handled by the tblgen generated code. |
| 902 | return false; |
| 903 | if (SrcVT != MVT::i16 && SrcVT != MVT::i32) |
| 904 | // All other cases should be handled by the tblgen generated code. |
| 905 | return false; |
| 906 | |
| 907 | unsigned InputReg = getRegForValue(I->getOperand(0)); |
| 908 | if (!InputReg) |
| 909 | // Unhandled operand. Halt "fast" selection and bail. |
| 910 | return false; |
| 911 | |
| 912 | // First issue a copy to GR16_ or GR32_. |
| 913 | unsigned CopyOpc = (SrcVT == MVT::i16) ? X86::MOV16to16_ : X86::MOV32to32_; |
| 914 | const TargetRegisterClass *CopyRC = (SrcVT == MVT::i16) |
| 915 | ? X86::GR16_RegisterClass : X86::GR32_RegisterClass; |
| 916 | unsigned CopyReg = createResultReg(CopyRC); |
| 917 | BuildMI(MBB, TII.get(CopyOpc), CopyReg).addReg(InputReg); |
| 918 | |
| 919 | // Then issue an extract_subreg. |
Dan Gohman | 145b828 | 2008-10-07 21:50:36 +0000 | [diff] [blame] | 920 | unsigned ResultReg = FastEmitInst_extractsubreg(CopyReg, X86::SUBREG_8BIT); |
Evan Cheng | 10a8d9c | 2008-09-07 08:47:42 +0000 | [diff] [blame] | 921 | if (!ResultReg) |
| 922 | return false; |
| 923 | |
| 924 | UpdateValueMap(I, ResultReg); |
| 925 | return true; |
| 926 | } |
| 927 | |
Evan Cheng | f3d4efe | 2008-09-07 09:09:33 +0000 | [diff] [blame] | 928 | bool X86FastISel::X86SelectCall(Instruction *I) { |
| 929 | CallInst *CI = cast<CallInst>(I); |
| 930 | Value *Callee = I->getOperand(0); |
| 931 | |
| 932 | // Can't handle inline asm yet. |
| 933 | if (isa<InlineAsm>(Callee)) |
| 934 | return false; |
| 935 | |
| 936 | // FIXME: Handle some intrinsics. |
| 937 | if (Function *F = CI->getCalledFunction()) { |
| 938 | if (F->isDeclaration() &&F->getIntrinsicID()) |
| 939 | return false; |
| 940 | } |
| 941 | |
Evan Cheng | f3d4efe | 2008-09-07 09:09:33 +0000 | [diff] [blame] | 942 | // Handle only C and fastcc calling conventions for now. |
| 943 | CallSite CS(CI); |
| 944 | unsigned CC = CS.getCallingConv(); |
| 945 | if (CC != CallingConv::C && |
| 946 | CC != CallingConv::Fast && |
| 947 | CC != CallingConv::X86_FastCall) |
| 948 | return false; |
| 949 | |
| 950 | // Let SDISel handle vararg functions. |
| 951 | const PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType()); |
| 952 | const FunctionType *FTy = cast<FunctionType>(PT->getElementType()); |
| 953 | if (FTy->isVarArg()) |
| 954 | return false; |
| 955 | |
| 956 | // Handle *simple* calls for now. |
| 957 | const Type *RetTy = CS.getType(); |
| 958 | MVT RetVT; |
Dan Gohman | b5b6ec6 | 2008-09-17 21:18:49 +0000 | [diff] [blame] | 959 | if (RetTy == Type::VoidTy) |
| 960 | RetVT = MVT::isVoid; |
| 961 | else if (!isTypeLegal(RetTy, TLI, RetVT, true)) |
Evan Cheng | f3d4efe | 2008-09-07 09:09:33 +0000 | [diff] [blame] | 962 | return false; |
| 963 | |
Dan Gohman | b5b6ec6 | 2008-09-17 21:18:49 +0000 | [diff] [blame] | 964 | // Materialize callee address in a register. FIXME: GV address can be |
| 965 | // handled with a CALLpcrel32 instead. |
Dan Gohman | 2ff7fd1 | 2008-09-19 22:16:54 +0000 | [diff] [blame] | 966 | X86AddressMode CalleeAM; |
| 967 | if (!X86SelectAddress(Callee, CalleeAM, true)) |
| 968 | return false; |
Dan Gohman | b5b6ec6 | 2008-09-17 21:18:49 +0000 | [diff] [blame] | 969 | unsigned CalleeOp = 0; |
Dan Gohman | 2ff7fd1 | 2008-09-19 22:16:54 +0000 | [diff] [blame] | 970 | GlobalValue *GV = 0; |
| 971 | if (CalleeAM.Base.Reg != 0) { |
| 972 | assert(CalleeAM.GV == 0); |
| 973 | CalleeOp = CalleeAM.Base.Reg; |
| 974 | } else if (CalleeAM.GV != 0) { |
| 975 | assert(CalleeAM.GV != 0); |
| 976 | GV = CalleeAM.GV; |
| 977 | } else |
| 978 | return false; |
Dan Gohman | b5b6ec6 | 2008-09-17 21:18:49 +0000 | [diff] [blame] | 979 | |
Evan Cheng | debdea0 | 2008-09-08 17:15:42 +0000 | [diff] [blame] | 980 | // Allow calls which produce i1 results. |
| 981 | bool AndToI1 = false; |
| 982 | if (RetVT == MVT::i1) { |
| 983 | RetVT = MVT::i8; |
| 984 | AndToI1 = true; |
| 985 | } |
| 986 | |
Evan Cheng | f3d4efe | 2008-09-07 09:09:33 +0000 | [diff] [blame] | 987 | // Deal with call operands first. |
| 988 | SmallVector<unsigned, 4> Args; |
| 989 | SmallVector<MVT, 4> ArgVTs; |
| 990 | SmallVector<ISD::ArgFlagsTy, 4> ArgFlags; |
| 991 | Args.reserve(CS.arg_size()); |
| 992 | ArgVTs.reserve(CS.arg_size()); |
| 993 | ArgFlags.reserve(CS.arg_size()); |
| 994 | for (CallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end(); |
| 995 | i != e; ++i) { |
| 996 | unsigned Arg = getRegForValue(*i); |
| 997 | if (Arg == 0) |
| 998 | return false; |
| 999 | ISD::ArgFlagsTy Flags; |
| 1000 | unsigned AttrInd = i - CS.arg_begin() + 1; |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 1001 | if (CS.paramHasAttr(AttrInd, Attribute::SExt)) |
Evan Cheng | f3d4efe | 2008-09-07 09:09:33 +0000 | [diff] [blame] | 1002 | Flags.setSExt(); |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 1003 | if (CS.paramHasAttr(AttrInd, Attribute::ZExt)) |
Evan Cheng | f3d4efe | 2008-09-07 09:09:33 +0000 | [diff] [blame] | 1004 | Flags.setZExt(); |
| 1005 | |
| 1006 | // FIXME: Only handle *easy* calls for now. |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 1007 | if (CS.paramHasAttr(AttrInd, Attribute::InReg) || |
| 1008 | CS.paramHasAttr(AttrInd, Attribute::StructRet) || |
| 1009 | CS.paramHasAttr(AttrInd, Attribute::Nest) || |
| 1010 | CS.paramHasAttr(AttrInd, Attribute::ByVal)) |
Evan Cheng | f3d4efe | 2008-09-07 09:09:33 +0000 | [diff] [blame] | 1011 | return false; |
| 1012 | |
| 1013 | const Type *ArgTy = (*i)->getType(); |
| 1014 | MVT ArgVT; |
| 1015 | if (!isTypeLegal(ArgTy, TLI, ArgVT)) |
| 1016 | return false; |
| 1017 | unsigned OriginalAlignment = TD.getABITypeAlignment(ArgTy); |
| 1018 | Flags.setOrigAlign(OriginalAlignment); |
| 1019 | |
| 1020 | Args.push_back(Arg); |
| 1021 | ArgVTs.push_back(ArgVT); |
| 1022 | ArgFlags.push_back(Flags); |
| 1023 | } |
| 1024 | |
| 1025 | // Analyze operands of the call, assigning locations to each operand. |
| 1026 | SmallVector<CCValAssign, 16> ArgLocs; |
| 1027 | CCState CCInfo(CC, false, TM, ArgLocs); |
| 1028 | CCInfo.AnalyzeCallOperands(ArgVTs, ArgFlags, CCAssignFnForCall(CC)); |
| 1029 | |
| 1030 | // Get a count of how many bytes are to be pushed on the stack. |
| 1031 | unsigned NumBytes = CCInfo.getNextStackOffset(); |
| 1032 | |
| 1033 | // Issue CALLSEQ_START |
Dan Gohman | 6d4b052 | 2008-10-01 18:28:06 +0000 | [diff] [blame] | 1034 | unsigned AdjStackDown = TM.getRegisterInfo()->getCallFrameSetupOpcode(); |
| 1035 | BuildMI(MBB, TII.get(AdjStackDown)).addImm(NumBytes); |
Evan Cheng | f3d4efe | 2008-09-07 09:09:33 +0000 | [diff] [blame] | 1036 | |
| 1037 | // Process argumenet: walk the register/memloc assignments, inserting |
| 1038 | // copies / loads. |
| 1039 | SmallVector<unsigned, 4> RegArgs; |
| 1040 | for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) { |
| 1041 | CCValAssign &VA = ArgLocs[i]; |
| 1042 | unsigned Arg = Args[VA.getValNo()]; |
| 1043 | MVT ArgVT = ArgVTs[VA.getValNo()]; |
| 1044 | |
| 1045 | // Promote the value if needed. |
| 1046 | switch (VA.getLocInfo()) { |
| 1047 | default: assert(0 && "Unknown loc info!"); |
| 1048 | case CCValAssign::Full: break; |
Evan Cheng | 24e3a90 | 2008-09-08 06:35:17 +0000 | [diff] [blame] | 1049 | case CCValAssign::SExt: { |
| 1050 | bool Emitted = X86FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(), |
| 1051 | Arg, ArgVT, Arg); |
| 1052 | assert(Emitted && "Failed to emit a sext!"); |
| 1053 | ArgVT = VA.getLocVT(); |
Evan Cheng | f3d4efe | 2008-09-07 09:09:33 +0000 | [diff] [blame] | 1054 | break; |
Evan Cheng | 24e3a90 | 2008-09-08 06:35:17 +0000 | [diff] [blame] | 1055 | } |
| 1056 | case CCValAssign::ZExt: { |
| 1057 | bool Emitted = X86FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(), |
| 1058 | Arg, ArgVT, Arg); |
| 1059 | assert(Emitted && "Failed to emit a zext!"); |
| 1060 | ArgVT = VA.getLocVT(); |
Evan Cheng | f3d4efe | 2008-09-07 09:09:33 +0000 | [diff] [blame] | 1061 | break; |
Evan Cheng | 24e3a90 | 2008-09-08 06:35:17 +0000 | [diff] [blame] | 1062 | } |
| 1063 | case CCValAssign::AExt: { |
| 1064 | bool Emitted = X86FastEmitExtend(ISD::ANY_EXTEND, VA.getLocVT(), |
| 1065 | Arg, ArgVT, Arg); |
Owen Anderson | b636913 | 2008-09-11 02:41:37 +0000 | [diff] [blame] | 1066 | if (!Emitted) |
| 1067 | Emitted = X86FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(), |
| 1068 | Arg, ArgVT, Arg); |
| 1069 | if (!Emitted) |
| 1070 | Emitted = X86FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(), |
| 1071 | Arg, ArgVT, Arg); |
| 1072 | |
Evan Cheng | 24e3a90 | 2008-09-08 06:35:17 +0000 | [diff] [blame] | 1073 | assert(Emitted && "Failed to emit a aext!"); |
| 1074 | ArgVT = VA.getLocVT(); |
Evan Cheng | f3d4efe | 2008-09-07 09:09:33 +0000 | [diff] [blame] | 1075 | break; |
| 1076 | } |
Evan Cheng | 24e3a90 | 2008-09-08 06:35:17 +0000 | [diff] [blame] | 1077 | } |
Evan Cheng | f3d4efe | 2008-09-07 09:09:33 +0000 | [diff] [blame] | 1078 | |
| 1079 | if (VA.isRegLoc()) { |
| 1080 | TargetRegisterClass* RC = TLI.getRegClassFor(ArgVT); |
| 1081 | bool Emitted = TII.copyRegToReg(*MBB, MBB->end(), VA.getLocReg(), |
| 1082 | Arg, RC, RC); |
| 1083 | assert(Emitted && "Failed to emit a copy instruction!"); |
| 1084 | RegArgs.push_back(VA.getLocReg()); |
| 1085 | } else { |
| 1086 | unsigned LocMemOffset = VA.getLocMemOffset(); |
Dan Gohman | 0586d91 | 2008-09-10 20:11:02 +0000 | [diff] [blame] | 1087 | X86AddressMode AM; |
| 1088 | AM.Base.Reg = StackPtr; |
| 1089 | AM.Disp = LocMemOffset; |
| 1090 | X86FastEmitStore(ArgVT, Arg, AM); |
Evan Cheng | f3d4efe | 2008-09-07 09:09:33 +0000 | [diff] [blame] | 1091 | } |
| 1092 | } |
| 1093 | |
Dan Gohman | 2cc3aa4 | 2008-09-25 15:24:26 +0000 | [diff] [blame] | 1094 | // ELF / PIC requires GOT in the EBX register before function calls via PLT |
| 1095 | // GOT pointer. |
| 1096 | if (!Subtarget->is64Bit() && |
| 1097 | TM.getRelocationModel() == Reloc::PIC_ && |
| 1098 | Subtarget->isPICStyleGOT()) { |
| 1099 | TargetRegisterClass *RC = X86::GR32RegisterClass; |
Dan Gohman | 57c3dac | 2008-09-30 00:58:23 +0000 | [diff] [blame] | 1100 | unsigned Base = getInstrInfo()->getGlobalBaseReg(&MF); |
Dan Gohman | 2cc3aa4 | 2008-09-25 15:24:26 +0000 | [diff] [blame] | 1101 | bool Emitted = TII.copyRegToReg(*MBB, MBB->end(), X86::EBX, Base, RC, RC); |
| 1102 | assert(Emitted && "Failed to emit a copy instruction!"); |
| 1103 | } |
| 1104 | |
Evan Cheng | f3d4efe | 2008-09-07 09:09:33 +0000 | [diff] [blame] | 1105 | // Issue the call. |
| 1106 | unsigned CallOpc = CalleeOp |
| 1107 | ? (Subtarget->is64Bit() ? X86::CALL64r : X86::CALL32r) |
| 1108 | : (Subtarget->is64Bit() ? X86::CALL64pcrel32 : X86::CALLpcrel32); |
| 1109 | MachineInstrBuilder MIB = CalleeOp |
| 1110 | ? BuildMI(MBB, TII.get(CallOpc)).addReg(CalleeOp) |
Dan Gohman | 2ff7fd1 | 2008-09-19 22:16:54 +0000 | [diff] [blame] | 1111 | : BuildMI(MBB, TII.get(CallOpc)).addGlobalAddress(GV); |
Dan Gohman | 2cc3aa4 | 2008-09-25 15:24:26 +0000 | [diff] [blame] | 1112 | |
| 1113 | // Add an implicit use GOT pointer in EBX. |
| 1114 | if (!Subtarget->is64Bit() && |
| 1115 | TM.getRelocationModel() == Reloc::PIC_ && |
| 1116 | Subtarget->isPICStyleGOT()) |
| 1117 | MIB.addReg(X86::EBX); |
| 1118 | |
Evan Cheng | f3d4efe | 2008-09-07 09:09:33 +0000 | [diff] [blame] | 1119 | // Add implicit physical register uses to the call. |
Dan Gohman | 8c3f8b6 | 2008-10-07 22:10:33 +0000 | [diff] [blame] | 1120 | for (unsigned i = 0, e = RegArgs.size(); i != e; ++i) |
| 1121 | MIB.addReg(RegArgs[i]); |
Evan Cheng | f3d4efe | 2008-09-07 09:09:33 +0000 | [diff] [blame] | 1122 | |
| 1123 | // Issue CALLSEQ_END |
Dan Gohman | 6d4b052 | 2008-10-01 18:28:06 +0000 | [diff] [blame] | 1124 | unsigned AdjStackUp = TM.getRegisterInfo()->getCallFrameDestroyOpcode(); |
| 1125 | BuildMI(MBB, TII.get(AdjStackUp)).addImm(NumBytes).addImm(0); |
Evan Cheng | f3d4efe | 2008-09-07 09:09:33 +0000 | [diff] [blame] | 1126 | |
| 1127 | // Now handle call return value (if any). |
Evan Cheng | f3d4efe | 2008-09-07 09:09:33 +0000 | [diff] [blame] | 1128 | if (RetVT.getSimpleVT() != MVT::isVoid) { |
| 1129 | SmallVector<CCValAssign, 16> RVLocs; |
| 1130 | CCState CCInfo(CC, false, TM, RVLocs); |
| 1131 | CCInfo.AnalyzeCallResult(RetVT, RetCC_X86); |
| 1132 | |
| 1133 | // Copy all of the result registers out of their specified physreg. |
| 1134 | assert(RVLocs.size() == 1 && "Can't handle multi-value calls!"); |
| 1135 | MVT CopyVT = RVLocs[0].getValVT(); |
| 1136 | TargetRegisterClass* DstRC = TLI.getRegClassFor(CopyVT); |
| 1137 | TargetRegisterClass *SrcRC = DstRC; |
| 1138 | |
| 1139 | // If this is a call to a function that returns an fp value on the x87 fp |
| 1140 | // stack, but where we prefer to use the value in xmm registers, copy it |
| 1141 | // out as F80 and use a truncate to move it from fp stack reg to xmm reg. |
| 1142 | if ((RVLocs[0].getLocReg() == X86::ST0 || |
| 1143 | RVLocs[0].getLocReg() == X86::ST1) && |
| 1144 | isScalarFPTypeInSSEReg(RVLocs[0].getValVT())) { |
| 1145 | CopyVT = MVT::f80; |
| 1146 | SrcRC = X86::RSTRegisterClass; |
| 1147 | DstRC = X86::RFP80RegisterClass; |
| 1148 | } |
| 1149 | |
| 1150 | unsigned ResultReg = createResultReg(DstRC); |
| 1151 | bool Emitted = TII.copyRegToReg(*MBB, MBB->end(), ResultReg, |
| 1152 | RVLocs[0].getLocReg(), DstRC, SrcRC); |
| 1153 | assert(Emitted && "Failed to emit a copy instruction!"); |
| 1154 | if (CopyVT != RVLocs[0].getValVT()) { |
| 1155 | // Round the F80 the right size, which also moves to the appropriate xmm |
| 1156 | // register. This is accomplished by storing the F80 value in memory and |
| 1157 | // then loading it back. Ewww... |
| 1158 | MVT ResVT = RVLocs[0].getValVT(); |
| 1159 | unsigned Opc = ResVT == MVT::f32 ? X86::ST_Fp80m32 : X86::ST_Fp80m64; |
| 1160 | unsigned MemSize = ResVT.getSizeInBits()/8; |
Dan Gohman | 0586d91 | 2008-09-10 20:11:02 +0000 | [diff] [blame] | 1161 | int FI = MFI.CreateStackObject(MemSize, MemSize); |
Evan Cheng | f3d4efe | 2008-09-07 09:09:33 +0000 | [diff] [blame] | 1162 | addFrameReference(BuildMI(MBB, TII.get(Opc)), FI).addReg(ResultReg); |
| 1163 | DstRC = ResVT == MVT::f32 |
| 1164 | ? X86::FR32RegisterClass : X86::FR64RegisterClass; |
| 1165 | Opc = ResVT == MVT::f32 ? X86::MOVSSrm : X86::MOVSDrm; |
| 1166 | ResultReg = createResultReg(DstRC); |
| 1167 | addFrameReference(BuildMI(MBB, TII.get(Opc), ResultReg), FI); |
| 1168 | } |
| 1169 | |
Evan Cheng | debdea0 | 2008-09-08 17:15:42 +0000 | [diff] [blame] | 1170 | if (AndToI1) { |
| 1171 | // Mask out all but lowest bit for some call which produces an i1. |
| 1172 | unsigned AndResult = createResultReg(X86::GR8RegisterClass); |
| 1173 | BuildMI(MBB, TII.get(X86::AND8ri), AndResult).addReg(ResultReg).addImm(1); |
| 1174 | ResultReg = AndResult; |
| 1175 | } |
| 1176 | |
Evan Cheng | f3d4efe | 2008-09-07 09:09:33 +0000 | [diff] [blame] | 1177 | UpdateValueMap(I, ResultReg); |
| 1178 | } |
| 1179 | |
| 1180 | return true; |
| 1181 | } |
| 1182 | |
| 1183 | |
Dan Gohman | 99b2182 | 2008-08-28 23:21:34 +0000 | [diff] [blame] | 1184 | bool |
Dan Gohman | 3df24e6 | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 1185 | X86FastISel::TargetSelectInstruction(Instruction *I) { |
Dan Gohman | 99b2182 | 2008-08-28 23:21:34 +0000 | [diff] [blame] | 1186 | switch (I->getOpcode()) { |
| 1187 | default: break; |
Evan Cheng | 8b19e56 | 2008-09-03 06:44:39 +0000 | [diff] [blame] | 1188 | case Instruction::Load: |
Dan Gohman | 3df24e6 | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 1189 | return X86SelectLoad(I); |
Owen Anderson | 79924eb | 2008-09-04 16:48:33 +0000 | [diff] [blame] | 1190 | case Instruction::Store: |
| 1191 | return X86SelectStore(I); |
Dan Gohman | 6e3f05f | 2008-09-04 23:26:51 +0000 | [diff] [blame] | 1192 | case Instruction::ICmp: |
| 1193 | case Instruction::FCmp: |
| 1194 | return X86SelectCmp(I); |
Dan Gohman | d89ae99 | 2008-09-05 01:06:14 +0000 | [diff] [blame] | 1195 | case Instruction::ZExt: |
| 1196 | return X86SelectZExt(I); |
| 1197 | case Instruction::Br: |
| 1198 | return X86SelectBranch(I); |
Evan Cheng | f3d4efe | 2008-09-07 09:09:33 +0000 | [diff] [blame] | 1199 | case Instruction::Call: |
| 1200 | return X86SelectCall(I); |
Dan Gohman | c39f4db | 2008-09-05 18:30:08 +0000 | [diff] [blame] | 1201 | case Instruction::LShr: |
| 1202 | case Instruction::AShr: |
| 1203 | case Instruction::Shl: |
| 1204 | return X86SelectShift(I); |
| 1205 | case Instruction::Select: |
| 1206 | return X86SelectSelect(I); |
Evan Cheng | 10a8d9c | 2008-09-07 08:47:42 +0000 | [diff] [blame] | 1207 | case Instruction::Trunc: |
| 1208 | return X86SelectTrunc(I); |
Dan Gohman | 78efce6 | 2008-09-10 21:02:08 +0000 | [diff] [blame] | 1209 | case Instruction::FPExt: |
| 1210 | return X86SelectFPExt(I); |
| 1211 | case Instruction::FPTrunc: |
| 1212 | return X86SelectFPTrunc(I); |
Dan Gohman | 99b2182 | 2008-08-28 23:21:34 +0000 | [diff] [blame] | 1213 | } |
| 1214 | |
| 1215 | return false; |
| 1216 | } |
| 1217 | |
Dan Gohman | 0586d91 | 2008-09-10 20:11:02 +0000 | [diff] [blame] | 1218 | unsigned X86FastISel::TargetMaterializeConstant(Constant *C) { |
Evan Cheng | 59fbc80 | 2008-09-09 01:26:59 +0000 | [diff] [blame] | 1219 | MVT VT; |
| 1220 | if (!isTypeLegal(C->getType(), TLI, VT)) |
Owen Anderson | 95267a1 | 2008-09-05 00:06:23 +0000 | [diff] [blame] | 1221 | return false; |
| 1222 | |
| 1223 | // Get opcode and regclass of the output for the given load instruction. |
| 1224 | unsigned Opc = 0; |
| 1225 | const TargetRegisterClass *RC = NULL; |
| 1226 | switch (VT.getSimpleVT()) { |
| 1227 | default: return false; |
| 1228 | case MVT::i8: |
| 1229 | Opc = X86::MOV8rm; |
| 1230 | RC = X86::GR8RegisterClass; |
| 1231 | break; |
| 1232 | case MVT::i16: |
| 1233 | Opc = X86::MOV16rm; |
| 1234 | RC = X86::GR16RegisterClass; |
| 1235 | break; |
| 1236 | case MVT::i32: |
| 1237 | Opc = X86::MOV32rm; |
| 1238 | RC = X86::GR32RegisterClass; |
| 1239 | break; |
| 1240 | case MVT::i64: |
| 1241 | // Must be in x86-64 mode. |
| 1242 | Opc = X86::MOV64rm; |
| 1243 | RC = X86::GR64RegisterClass; |
| 1244 | break; |
| 1245 | case MVT::f32: |
| 1246 | if (Subtarget->hasSSE1()) { |
| 1247 | Opc = X86::MOVSSrm; |
| 1248 | RC = X86::FR32RegisterClass; |
| 1249 | } else { |
| 1250 | Opc = X86::LD_Fp32m; |
| 1251 | RC = X86::RFP32RegisterClass; |
| 1252 | } |
| 1253 | break; |
| 1254 | case MVT::f64: |
| 1255 | if (Subtarget->hasSSE2()) { |
| 1256 | Opc = X86::MOVSDrm; |
| 1257 | RC = X86::FR64RegisterClass; |
| 1258 | } else { |
| 1259 | Opc = X86::LD_Fp64m; |
| 1260 | RC = X86::RFP64RegisterClass; |
| 1261 | } |
| 1262 | break; |
| 1263 | case MVT::f80: |
Dan Gohman | 5af29c2 | 2008-09-26 01:39:32 +0000 | [diff] [blame] | 1264 | // No f80 support yet. |
| 1265 | return false; |
Owen Anderson | 95267a1 | 2008-09-05 00:06:23 +0000 | [diff] [blame] | 1266 | } |
| 1267 | |
Dan Gohman | 2ff7fd1 | 2008-09-19 22:16:54 +0000 | [diff] [blame] | 1268 | // Materialize addresses with LEA instructions. |
Owen Anderson | 95267a1 | 2008-09-05 00:06:23 +0000 | [diff] [blame] | 1269 | if (isa<GlobalValue>(C)) { |
Dan Gohman | 2ff7fd1 | 2008-09-19 22:16:54 +0000 | [diff] [blame] | 1270 | X86AddressMode AM; |
| 1271 | if (X86SelectAddress(C, AM, false)) { |
| 1272 | if (TLI.getPointerTy() == MVT::i32) |
| 1273 | Opc = X86::LEA32r; |
| 1274 | else |
| 1275 | Opc = X86::LEA64r; |
| 1276 | unsigned ResultReg = createResultReg(RC); |
| 1277 | addFullAddress(BuildMI(MBB, TII.get(Opc), ResultReg), AM); |
Owen Anderson | 95267a1 | 2008-09-05 00:06:23 +0000 | [diff] [blame] | 1278 | return ResultReg; |
Dan Gohman | 2ff7fd1 | 2008-09-19 22:16:54 +0000 | [diff] [blame] | 1279 | } |
Evan Cheng | 0de588f | 2008-09-05 21:00:03 +0000 | [diff] [blame] | 1280 | return 0; |
Owen Anderson | 95267a1 | 2008-09-05 00:06:23 +0000 | [diff] [blame] | 1281 | } |
| 1282 | |
Owen Anderson | 3b217c6 | 2008-09-06 01:11:01 +0000 | [diff] [blame] | 1283 | // MachineConstantPool wants an explicit alignment. |
Dan Gohman | 1fbc3cd | 2008-09-18 18:26:43 +0000 | [diff] [blame] | 1284 | unsigned Align = TD.getPreferredTypeAlignmentShift(C->getType()); |
Owen Anderson | 3b217c6 | 2008-09-06 01:11:01 +0000 | [diff] [blame] | 1285 | if (Align == 0) { |
| 1286 | // Alignment of vector types. FIXME! |
Dan Gohman | 1fbc3cd | 2008-09-18 18:26:43 +0000 | [diff] [blame] | 1287 | Align = TD.getABITypeSize(C->getType()); |
Owen Anderson | 3b217c6 | 2008-09-06 01:11:01 +0000 | [diff] [blame] | 1288 | Align = Log2_64(Align); |
| 1289 | } |
Owen Anderson | 95267a1 | 2008-09-05 00:06:23 +0000 | [diff] [blame] | 1290 | |
Dan Gohman | 5396c99 | 2008-09-30 01:21:32 +0000 | [diff] [blame] | 1291 | // x86-32 PIC requires a PIC base register for constant pools. |
| 1292 | unsigned PICBase = 0; |
| 1293 | if (TM.getRelocationModel() == Reloc::PIC_ && |
| 1294 | !Subtarget->is64Bit()) |
| 1295 | PICBase = getInstrInfo()->getGlobalBaseReg(&MF); |
| 1296 | |
| 1297 | // Create the load from the constant pool. |
Dan Gohman | 0586d91 | 2008-09-10 20:11:02 +0000 | [diff] [blame] | 1298 | unsigned MCPOffset = MCP.getConstantPoolIndex(C, Align); |
Dan Gohman | 2ff7fd1 | 2008-09-19 22:16:54 +0000 | [diff] [blame] | 1299 | unsigned ResultReg = createResultReg(RC); |
Dan Gohman | 5396c99 | 2008-09-30 01:21:32 +0000 | [diff] [blame] | 1300 | addConstantPoolReference(BuildMI(MBB, TII.get(Opc), ResultReg), MCPOffset, |
| 1301 | PICBase); |
| 1302 | |
Owen Anderson | 95267a1 | 2008-09-05 00:06:23 +0000 | [diff] [blame] | 1303 | return ResultReg; |
| 1304 | } |
| 1305 | |
Dan Gohman | 0586d91 | 2008-09-10 20:11:02 +0000 | [diff] [blame] | 1306 | unsigned X86FastISel::TargetMaterializeAlloca(AllocaInst *C) { |
Dan Gohman | 4e6ed5e | 2008-10-03 01:27:49 +0000 | [diff] [blame] | 1307 | // Fail on dynamic allocas. At this point, getRegForValue has already |
| 1308 | // checked its CSE maps, so if we're here trying to handle a dynamic |
| 1309 | // alloca, we're not going to succeed. X86SelectAddress has a |
| 1310 | // check for dynamic allocas, because it's called directly from |
| 1311 | // various places, but TargetMaterializeAlloca also needs a check |
| 1312 | // in order to avoid recursion between getRegForValue, |
| 1313 | // X86SelectAddrss, and TargetMaterializeAlloca. |
| 1314 | if (!StaticAllocaMap.count(C)) |
| 1315 | return 0; |
| 1316 | |
Dan Gohman | 0586d91 | 2008-09-10 20:11:02 +0000 | [diff] [blame] | 1317 | X86AddressMode AM; |
Dan Gohman | 2ff7fd1 | 2008-09-19 22:16:54 +0000 | [diff] [blame] | 1318 | if (!X86SelectAddress(C, AM, false)) |
Dan Gohman | 0586d91 | 2008-09-10 20:11:02 +0000 | [diff] [blame] | 1319 | return 0; |
| 1320 | unsigned Opc = Subtarget->is64Bit() ? X86::LEA64r : X86::LEA32r; |
| 1321 | TargetRegisterClass* RC = TLI.getRegClassFor(TLI.getPointerTy()); |
| 1322 | unsigned ResultReg = createResultReg(RC); |
| 1323 | addFullAddress(BuildMI(MBB, TII.get(Opc), ResultReg), AM); |
| 1324 | return ResultReg; |
| 1325 | } |
| 1326 | |
Evan Cheng | c3f44b0 | 2008-09-03 00:03:49 +0000 | [diff] [blame] | 1327 | namespace llvm { |
Dan Gohman | 3df24e6 | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 1328 | llvm::FastISel *X86::createFastISel(MachineFunction &mf, |
Dan Gohman | d57dd5f | 2008-09-23 21:53:34 +0000 | [diff] [blame] | 1329 | MachineModuleInfo *mmi, |
Dan Gohman | 3df24e6 | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 1330 | DenseMap<const Value *, unsigned> &vm, |
Dan Gohman | 0586d91 | 2008-09-10 20:11:02 +0000 | [diff] [blame] | 1331 | DenseMap<const BasicBlock *, MachineBasicBlock *> &bm, |
Dan Gohman | dd5b58a | 2008-10-14 23:54:11 +0000 | [diff] [blame] | 1332 | DenseMap<const AllocaInst *, int> &am |
| 1333 | #ifndef NDEBUG |
| 1334 | , SmallSet<Instruction*, 8> &cil |
| 1335 | #endif |
| 1336 | ) { |
| 1337 | return new X86FastISel(mf, mmi, vm, bm, am |
| 1338 | #ifndef NDEBUG |
| 1339 | , cil |
| 1340 | #endif |
| 1341 | ); |
Evan Cheng | c3f44b0 | 2008-09-03 00:03:49 +0000 | [diff] [blame] | 1342 | } |
Dan Gohman | 99b2182 | 2008-08-28 23:21:34 +0000 | [diff] [blame] | 1343 | } |