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