Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 1 | //===-- X86ISelPattern.cpp - A pattern matching inst selector for X86 -----===// |
Chris Lattner | 24aad1b | 2005-01-10 22:10:13 +0000 | [diff] [blame] | 2 | // |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by the LLVM research group and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
Misha Brukman | 0e0a7a45 | 2005-04-21 23:38:14 +0000 | [diff] [blame] | 7 | // |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file defines a pattern matching instruction selector for X86. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "X86.h" |
| 15 | #include "X86InstrBuilder.h" |
| 16 | #include "X86RegisterInfo.h" |
Chris Lattner | c6f4181 | 2005-05-12 23:06:28 +0000 | [diff] [blame] | 17 | #include "llvm/CallingConv.h" |
Chris Lattner | e3e0f27 | 2005-05-09 03:36:39 +0000 | [diff] [blame] | 18 | #include "llvm/Constants.h" |
| 19 | #include "llvm/Instructions.h" |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 20 | #include "llvm/Function.h" |
Chris Lattner | e3e0f27 | 2005-05-09 03:36:39 +0000 | [diff] [blame] | 21 | #include "llvm/CodeGen/MachineConstantPool.h" |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 22 | #include "llvm/CodeGen/MachineFunction.h" |
| 23 | #include "llvm/CodeGen/MachineFrameInfo.h" |
| 24 | #include "llvm/CodeGen/SelectionDAG.h" |
| 25 | #include "llvm/CodeGen/SelectionDAGISel.h" |
| 26 | #include "llvm/CodeGen/SSARegMap.h" |
| 27 | #include "llvm/Target/TargetData.h" |
| 28 | #include "llvm/Target/TargetLowering.h" |
Chris Lattner | c5dcb53 | 2005-04-30 04:25:35 +0000 | [diff] [blame] | 29 | #include "llvm/Target/TargetOptions.h" |
Chris Lattner | e3e0f27 | 2005-05-09 03:36:39 +0000 | [diff] [blame] | 30 | #include "llvm/Support/CFG.h" |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 31 | #include "llvm/Support/MathExtras.h" |
| 32 | #include "llvm/ADT/Statistic.h" |
| 33 | #include <set> |
Jeff Cohen | 603fea9 | 2005-01-12 04:29:05 +0000 | [diff] [blame] | 34 | #include <algorithm> |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 35 | using namespace llvm; |
| 36 | |
Chris Lattner | c6f4181 | 2005-05-12 23:06:28 +0000 | [diff] [blame] | 37 | // FIXME: temporary. |
| 38 | #include "llvm/Support/CommandLine.h" |
| 39 | static cl::opt<bool> EnableFastCC("enable-x86-fastcc", cl::Hidden, |
| 40 | cl::desc("Enable fastcc on X86")); |
| 41 | |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 42 | //===----------------------------------------------------------------------===// |
| 43 | // X86TargetLowering - X86 Implementation of the TargetLowering interface |
| 44 | namespace { |
| 45 | class X86TargetLowering : public TargetLowering { |
| 46 | int VarArgsFrameIndex; // FrameIndex for start of varargs area. |
Chris Lattner | 1482458 | 2005-01-09 00:01:27 +0000 | [diff] [blame] | 47 | int ReturnAddrIndex; // FrameIndex for return slot. |
Chris Lattner | adf6a96 | 2005-05-13 18:50:42 +0000 | [diff] [blame] | 48 | int BytesToPopOnReturn; // Number of bytes ret should pop. |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 49 | public: |
| 50 | X86TargetLowering(TargetMachine &TM) : TargetLowering(TM) { |
| 51 | // Set up the TargetLowering object. |
Chris Lattner | 4df0de9 | 2005-01-17 00:00:33 +0000 | [diff] [blame] | 52 | |
| 53 | // X86 is wierd, it always uses i8 for shift amounts and setcc results. |
| 54 | setShiftAmountType(MVT::i8); |
| 55 | setSetCCResultType(MVT::i8); |
Chris Lattner | 6659bd7 | 2005-04-07 19:41:46 +0000 | [diff] [blame] | 56 | setSetCCResultContents(ZeroOrOneSetCCResult); |
Chris Lattner | 009b55b | 2005-01-19 03:36:30 +0000 | [diff] [blame] | 57 | setShiftAmountFlavor(Mask); // shl X, 32 == shl X, 0 |
Chris Lattner | 4df0de9 | 2005-01-17 00:00:33 +0000 | [diff] [blame] | 58 | |
| 59 | // Set up the register classes. |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 60 | addRegisterClass(MVT::i8, X86::R8RegisterClass); |
| 61 | addRegisterClass(MVT::i16, X86::R16RegisterClass); |
| 62 | addRegisterClass(MVT::i32, X86::R32RegisterClass); |
| 63 | addRegisterClass(MVT::f64, X86::RFPRegisterClass); |
Misha Brukman | 0e0a7a45 | 2005-04-21 23:38:14 +0000 | [diff] [blame] | 64 | |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 65 | // FIXME: Eliminate these two classes when legalize can handle promotions |
| 66 | // well. |
Chris Lattner | da2ce11 | 2005-01-16 07:34:08 +0000 | [diff] [blame] | 67 | /**/ addRegisterClass(MVT::i1, X86::R8RegisterClass); |
Chris Lattner | da2ce11 | 2005-01-16 07:34:08 +0000 | [diff] [blame] | 68 | |
Chris Lattner | da4d469 | 2005-04-09 03:22:37 +0000 | [diff] [blame] | 69 | setOperationAction(ISD::BRCONDTWOWAY , MVT::Other, Expand); |
Chris Lattner | da2ce11 | 2005-01-16 07:34:08 +0000 | [diff] [blame] | 70 | setOperationAction(ISD::MEMMOVE , MVT::Other, Expand); |
| 71 | setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Expand); |
Chris Lattner | da2ce11 | 2005-01-16 07:34:08 +0000 | [diff] [blame] | 72 | setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand); |
Chris Lattner | da2ce11 | 2005-01-16 07:34:08 +0000 | [diff] [blame] | 73 | setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand); |
| 74 | setOperationAction(ISD::SEXTLOAD , MVT::i1 , Expand); |
| 75 | setOperationAction(ISD::SREM , MVT::f64 , Expand); |
Chris Lattner | c610d42 | 2005-05-11 05:00:34 +0000 | [diff] [blame] | 76 | setOperationAction(ISD::CTPOP , MVT::i8 , Expand); |
| 77 | setOperationAction(ISD::CTTZ , MVT::i8 , Expand); |
| 78 | setOperationAction(ISD::CTLZ , MVT::i8 , Expand); |
| 79 | setOperationAction(ISD::CTPOP , MVT::i16 , Expand); |
| 80 | setOperationAction(ISD::CTTZ , MVT::i16 , Expand); |
| 81 | setOperationAction(ISD::CTLZ , MVT::i16 , Expand); |
Andrew Lenharth | 691ef2b | 2005-05-03 17:19:30 +0000 | [diff] [blame] | 82 | setOperationAction(ISD::CTPOP , MVT::i32 , Expand); |
| 83 | setOperationAction(ISD::CTTZ , MVT::i32 , Expand); |
Andrew Lenharth | b5884d3 | 2005-05-04 19:25:37 +0000 | [diff] [blame] | 84 | setOperationAction(ISD::CTLZ , MVT::i32 , Expand); |
Chris Lattner | 43fdea0 | 2005-04-02 05:03:24 +0000 | [diff] [blame] | 85 | |
Chris Lattner | 4e6ce5f | 2005-05-09 20:37:29 +0000 | [diff] [blame] | 86 | setOperationAction(ISD::READIO , MVT::i1 , Expand); |
| 87 | setOperationAction(ISD::READIO , MVT::i8 , Expand); |
| 88 | setOperationAction(ISD::READIO , MVT::i16 , Expand); |
| 89 | setOperationAction(ISD::READIO , MVT::i32 , Expand); |
| 90 | setOperationAction(ISD::WRITEIO , MVT::i1 , Expand); |
| 91 | setOperationAction(ISD::WRITEIO , MVT::i8 , Expand); |
| 92 | setOperationAction(ISD::WRITEIO , MVT::i16 , Expand); |
| 93 | setOperationAction(ISD::WRITEIO , MVT::i32 , Expand); |
| 94 | |
Chris Lattner | c5dcb53 | 2005-04-30 04:25:35 +0000 | [diff] [blame] | 95 | if (!UnsafeFPMath) { |
| 96 | setOperationAction(ISD::FSIN , MVT::f64 , Expand); |
| 97 | setOperationAction(ISD::FCOS , MVT::f64 , Expand); |
| 98 | } |
| 99 | |
Chris Lattner | da2ce11 | 2005-01-16 07:34:08 +0000 | [diff] [blame] | 100 | // These should be promoted to a larger select which is supported. |
| 101 | /**/ setOperationAction(ISD::SELECT , MVT::i1 , Promote); |
| 102 | setOperationAction(ISD::SELECT , MVT::i8 , Promote); |
Misha Brukman | 0e0a7a45 | 2005-04-21 23:38:14 +0000 | [diff] [blame] | 103 | |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 104 | computeRegisterProperties(); |
Misha Brukman | 0e0a7a45 | 2005-04-21 23:38:14 +0000 | [diff] [blame] | 105 | |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 106 | addLegalFPImmediate(+0.0); // FLD0 |
| 107 | addLegalFPImmediate(+1.0); // FLD1 |
| 108 | addLegalFPImmediate(-0.0); // FLD0/FCHS |
| 109 | addLegalFPImmediate(-1.0); // FLD1/FCHS |
| 110 | } |
| 111 | |
Chris Lattner | 3648c67 | 2005-05-13 21:44:04 +0000 | [diff] [blame^] | 112 | // Return the number of bytes that a function should pop when it returns (in |
| 113 | // addition to the space used by the return address). |
| 114 | // |
| 115 | unsigned getBytesToPopOnReturn() const { return BytesToPopOnReturn; } |
| 116 | |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 117 | /// LowerArguments - This hook must be implemented to indicate how we should |
| 118 | /// lower the arguments for the specified function, into the specified DAG. |
| 119 | virtual std::vector<SDOperand> |
| 120 | LowerArguments(Function &F, SelectionDAG &DAG); |
| 121 | |
| 122 | /// LowerCallTo - This hook lowers an abstract call to a function into an |
| 123 | /// actual call. |
Chris Lattner | 5188ad7 | 2005-01-08 19:28:19 +0000 | [diff] [blame] | 124 | virtual std::pair<SDOperand, SDOperand> |
Chris Lattner | c57f682 | 2005-05-12 19:56:45 +0000 | [diff] [blame] | 125 | LowerCallTo(SDOperand Chain, const Type *RetTy, bool isVarArg, unsigned CC, |
Chris Lattner | adf6a96 | 2005-05-13 18:50:42 +0000 | [diff] [blame] | 126 | bool isTailCall, SDOperand Callee, ArgListTy &Args, |
| 127 | SelectionDAG &DAG); |
Chris Lattner | 1482458 | 2005-01-09 00:01:27 +0000 | [diff] [blame] | 128 | |
| 129 | virtual std::pair<SDOperand, SDOperand> |
| 130 | LowerVAStart(SDOperand Chain, SelectionDAG &DAG); |
| 131 | |
| 132 | virtual std::pair<SDOperand,SDOperand> |
| 133 | LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList, |
| 134 | const Type *ArgTy, SelectionDAG &DAG); |
| 135 | |
| 136 | virtual std::pair<SDOperand, SDOperand> |
| 137 | LowerFrameReturnAddress(bool isFrameAddr, SDOperand Chain, unsigned Depth, |
| 138 | SelectionDAG &DAG); |
Chris Lattner | c6f4181 | 2005-05-12 23:06:28 +0000 | [diff] [blame] | 139 | private: |
| 140 | // C Calling Convention implementation. |
| 141 | std::vector<SDOperand> LowerCCCArguments(Function &F, SelectionDAG &DAG); |
| 142 | std::pair<SDOperand, SDOperand> |
| 143 | LowerCCCCallTo(SDOperand Chain, const Type *RetTy, bool isVarArg, |
Chris Lattner | 2e7714a | 2005-05-13 20:29:13 +0000 | [diff] [blame] | 144 | bool isTailCall, |
Chris Lattner | c6f4181 | 2005-05-12 23:06:28 +0000 | [diff] [blame] | 145 | SDOperand Callee, ArgListTy &Args, SelectionDAG &DAG); |
| 146 | |
| 147 | // Fast Calling Convention implementation. |
| 148 | std::vector<SDOperand> LowerFastCCArguments(Function &F, SelectionDAG &DAG); |
| 149 | std::pair<SDOperand, SDOperand> |
Chris Lattner | 2e7714a | 2005-05-13 20:29:13 +0000 | [diff] [blame] | 150 | LowerFastCCCallTo(SDOperand Chain, const Type *RetTy, bool isTailCall, |
Chris Lattner | c6f4181 | 2005-05-12 23:06:28 +0000 | [diff] [blame] | 151 | SDOperand Callee, ArgListTy &Args, SelectionDAG &DAG); |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 152 | }; |
| 153 | } |
| 154 | |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 155 | std::vector<SDOperand> |
| 156 | X86TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) { |
Chris Lattner | c6f4181 | 2005-05-12 23:06:28 +0000 | [diff] [blame] | 157 | if (F.getCallingConv() == CallingConv::Fast && EnableFastCC) |
| 158 | return LowerFastCCArguments(F, DAG); |
| 159 | return LowerCCCArguments(F, DAG); |
| 160 | } |
| 161 | |
| 162 | std::pair<SDOperand, SDOperand> |
| 163 | X86TargetLowering::LowerCallTo(SDOperand Chain, const Type *RetTy, |
| 164 | bool isVarArg, unsigned CallingConv, |
Chris Lattner | adf6a96 | 2005-05-13 18:50:42 +0000 | [diff] [blame] | 165 | bool isTailCall, |
Chris Lattner | c6f4181 | 2005-05-12 23:06:28 +0000 | [diff] [blame] | 166 | SDOperand Callee, ArgListTy &Args, |
| 167 | SelectionDAG &DAG) { |
| 168 | assert((!isVarArg || CallingConv == CallingConv::C) && |
| 169 | "Only C takes varargs!"); |
| 170 | if (CallingConv == CallingConv::Fast && EnableFastCC) |
Chris Lattner | 2e7714a | 2005-05-13 20:29:13 +0000 | [diff] [blame] | 171 | return LowerFastCCCallTo(Chain, RetTy, isTailCall, Callee, Args, DAG); |
| 172 | return LowerCCCCallTo(Chain, RetTy, isVarArg, isTailCall, Callee, Args, DAG); |
Chris Lattner | c6f4181 | 2005-05-12 23:06:28 +0000 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | //===----------------------------------------------------------------------===// |
| 176 | // C Calling Convention implementation |
| 177 | //===----------------------------------------------------------------------===// |
| 178 | |
| 179 | std::vector<SDOperand> |
| 180 | X86TargetLowering::LowerCCCArguments(Function &F, SelectionDAG &DAG) { |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 181 | std::vector<SDOperand> ArgValues; |
| 182 | |
Chris Lattner | 6415bb4 | 2005-05-10 03:53:18 +0000 | [diff] [blame] | 183 | MachineFunction &MF = DAG.getMachineFunction(); |
| 184 | MachineFrameInfo *MFI = MF.getFrameInfo(); |
| 185 | |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 186 | // Add DAG nodes to load the arguments... On entry to a function on the X86, |
| 187 | // the stack frame looks like this: |
| 188 | // |
| 189 | // [ESP] -- return address |
| 190 | // [ESP + 4] -- first argument (leftmost lexically) |
| 191 | // [ESP + 8] -- second argument, if first argument is four bytes in size |
Misha Brukman | 0e0a7a45 | 2005-04-21 23:38:14 +0000 | [diff] [blame] | 192 | // ... |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 193 | // |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 194 | unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot |
Chris Lattner | e4d5c44 | 2005-03-15 04:54:21 +0000 | [diff] [blame] | 195 | for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) { |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 196 | MVT::ValueType ObjectVT = getValueType(I->getType()); |
| 197 | unsigned ArgIncrement = 4; |
| 198 | unsigned ObjSize; |
| 199 | switch (ObjectVT) { |
| 200 | default: assert(0 && "Unhandled argument type!"); |
| 201 | case MVT::i1: |
| 202 | case MVT::i8: ObjSize = 1; break; |
| 203 | case MVT::i16: ObjSize = 2; break; |
| 204 | case MVT::i32: ObjSize = 4; break; |
| 205 | case MVT::i64: ObjSize = ArgIncrement = 8; break; |
| 206 | case MVT::f32: ObjSize = 4; break; |
| 207 | case MVT::f64: ObjSize = ArgIncrement = 8; break; |
| 208 | } |
| 209 | // Create the frame index object for this incoming parameter... |
| 210 | int FI = MFI->CreateFixedObject(ObjSize, ArgOffset); |
Misha Brukman | 0e0a7a45 | 2005-04-21 23:38:14 +0000 | [diff] [blame] | 211 | |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 212 | // Create the SelectionDAG nodes corresponding to a load from this parameter |
| 213 | SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32); |
| 214 | |
| 215 | // Don't codegen dead arguments. FIXME: remove this check when we can nuke |
| 216 | // dead loads. |
| 217 | SDOperand ArgValue; |
| 218 | if (!I->use_empty()) |
Chris Lattner | a80d2bd | 2005-05-09 05:40:26 +0000 | [diff] [blame] | 219 | ArgValue = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN, |
| 220 | DAG.getSrcValue(NULL)); |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 221 | else { |
| 222 | if (MVT::isInteger(ObjectVT)) |
| 223 | ArgValue = DAG.getConstant(0, ObjectVT); |
| 224 | else |
| 225 | ArgValue = DAG.getConstantFP(0, ObjectVT); |
| 226 | } |
| 227 | ArgValues.push_back(ArgValue); |
| 228 | |
| 229 | ArgOffset += ArgIncrement; // Move on to the next argument... |
| 230 | } |
| 231 | |
| 232 | // If the function takes variable number of arguments, make a frame index for |
| 233 | // the start of the first vararg value... for expansion of llvm.va_start. |
| 234 | if (F.isVarArg()) |
| 235 | VarArgsFrameIndex = MFI->CreateFixedObject(1, ArgOffset); |
Chris Lattner | 3648c67 | 2005-05-13 21:44:04 +0000 | [diff] [blame^] | 236 | ReturnAddrIndex = 0; // No return address slot generated yet. |
| 237 | BytesToPopOnReturn = 0; // Callee pops nothing. |
Chris Lattner | 4c52f0e | 2005-04-09 15:23:56 +0000 | [diff] [blame] | 238 | |
| 239 | // Finally, inform the code generator which regs we return values in. |
| 240 | switch (getValueType(F.getReturnType())) { |
| 241 | default: assert(0 && "Unknown type!"); |
| 242 | case MVT::isVoid: break; |
| 243 | case MVT::i1: |
| 244 | case MVT::i8: |
| 245 | case MVT::i16: |
| 246 | case MVT::i32: |
| 247 | MF.addLiveOut(X86::EAX); |
| 248 | break; |
| 249 | case MVT::i64: |
| 250 | MF.addLiveOut(X86::EAX); |
| 251 | MF.addLiveOut(X86::EDX); |
| 252 | break; |
| 253 | case MVT::f32: |
| 254 | case MVT::f64: |
| 255 | MF.addLiveOut(X86::ST0); |
| 256 | break; |
| 257 | } |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 258 | return ArgValues; |
| 259 | } |
| 260 | |
Chris Lattner | 5188ad7 | 2005-01-08 19:28:19 +0000 | [diff] [blame] | 261 | std::pair<SDOperand, SDOperand> |
Chris Lattner | c6f4181 | 2005-05-12 23:06:28 +0000 | [diff] [blame] | 262 | X86TargetLowering::LowerCCCCallTo(SDOperand Chain, const Type *RetTy, |
Chris Lattner | 2e7714a | 2005-05-13 20:29:13 +0000 | [diff] [blame] | 263 | bool isVarArg, bool isTailCall, |
| 264 | SDOperand Callee, ArgListTy &Args, |
| 265 | SelectionDAG &DAG) { |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 266 | // Count how many bytes are to be pushed on the stack. |
| 267 | unsigned NumBytes = 0; |
| 268 | |
| 269 | if (Args.empty()) { |
| 270 | // Save zero bytes. |
Chris Lattner | 16cd04d | 2005-05-12 23:24:06 +0000 | [diff] [blame] | 271 | Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain, |
Chris Lattner | 5188ad7 | 2005-01-08 19:28:19 +0000 | [diff] [blame] | 272 | DAG.getConstant(0, getPointerTy())); |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 273 | } else { |
| 274 | for (unsigned i = 0, e = Args.size(); i != e; ++i) |
| 275 | switch (getValueType(Args[i].second)) { |
| 276 | default: assert(0 && "Unknown value type!"); |
| 277 | case MVT::i1: |
| 278 | case MVT::i8: |
| 279 | case MVT::i16: |
| 280 | case MVT::i32: |
| 281 | case MVT::f32: |
| 282 | NumBytes += 4; |
| 283 | break; |
| 284 | case MVT::i64: |
| 285 | case MVT::f64: |
| 286 | NumBytes += 8; |
| 287 | break; |
| 288 | } |
| 289 | |
Chris Lattner | 16cd04d | 2005-05-12 23:24:06 +0000 | [diff] [blame] | 290 | Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain, |
Chris Lattner | 5188ad7 | 2005-01-08 19:28:19 +0000 | [diff] [blame] | 291 | DAG.getConstant(NumBytes, getPointerTy())); |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 292 | |
| 293 | // Arguments go on the stack in reverse order, as specified by the ABI. |
| 294 | unsigned ArgOffset = 0; |
Chris Lattner | 7f2afac | 2005-01-14 22:37:41 +0000 | [diff] [blame] | 295 | SDOperand StackPtr = DAG.getCopyFromReg(X86::ESP, MVT::i32, |
| 296 | DAG.getEntryNode()); |
Chris Lattner | b62e1e2 | 2005-01-21 19:46:38 +0000 | [diff] [blame] | 297 | std::vector<SDOperand> Stores; |
| 298 | |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 299 | for (unsigned i = 0, e = Args.size(); i != e; ++i) { |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 300 | SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy()); |
| 301 | PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff); |
| 302 | |
| 303 | switch (getValueType(Args[i].second)) { |
| 304 | default: assert(0 && "Unexpected ValueType for argument!"); |
| 305 | case MVT::i1: |
| 306 | case MVT::i8: |
| 307 | case MVT::i16: |
| 308 | // Promote the integer to 32 bits. If the input type is signed use a |
| 309 | // sign extend, otherwise use a zero extend. |
| 310 | if (Args[i].second->isSigned()) |
| 311 | Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first); |
| 312 | else |
| 313 | Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first); |
| 314 | |
| 315 | // FALL THROUGH |
| 316 | case MVT::i32: |
| 317 | case MVT::f32: |
Chris Lattner | b62e1e2 | 2005-01-21 19:46:38 +0000 | [diff] [blame] | 318 | Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain, |
Chris Lattner | a80d2bd | 2005-05-09 05:40:26 +0000 | [diff] [blame] | 319 | Args[i].first, PtrOff, |
| 320 | DAG.getSrcValue(NULL))); |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 321 | ArgOffset += 4; |
| 322 | break; |
| 323 | case MVT::i64: |
| 324 | case MVT::f64: |
Chris Lattner | b62e1e2 | 2005-01-21 19:46:38 +0000 | [diff] [blame] | 325 | Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain, |
Chris Lattner | a80d2bd | 2005-05-09 05:40:26 +0000 | [diff] [blame] | 326 | Args[i].first, PtrOff, |
| 327 | DAG.getSrcValue(NULL))); |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 328 | ArgOffset += 8; |
| 329 | break; |
| 330 | } |
| 331 | } |
Chris Lattner | b62e1e2 | 2005-01-21 19:46:38 +0000 | [diff] [blame] | 332 | Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, Stores); |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 333 | } |
| 334 | |
| 335 | std::vector<MVT::ValueType> RetVals; |
| 336 | MVT::ValueType RetTyVT = getValueType(RetTy); |
| 337 | if (RetTyVT != MVT::isVoid) |
| 338 | RetVals.push_back(RetTyVT); |
| 339 | RetVals.push_back(MVT::Other); |
| 340 | |
Chris Lattner | 2e7714a | 2005-05-13 20:29:13 +0000 | [diff] [blame] | 341 | SDOperand TheCall = SDOperand(DAG.getCall(RetVals, Chain, Callee, |
| 342 | isTailCall), 0); |
Chris Lattner | b080265 | 2005-01-08 20:51:36 +0000 | [diff] [blame] | 343 | Chain = TheCall.getValue(RetTyVT != MVT::isVoid); |
Chris Lattner | 16cd04d | 2005-05-12 23:24:06 +0000 | [diff] [blame] | 344 | Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain, |
Chris Lattner | adf6a96 | 2005-05-13 18:50:42 +0000 | [diff] [blame] | 345 | DAG.getConstant(NumBytes, getPointerTy()), |
| 346 | DAG.getConstant(0, getPointerTy())); |
Chris Lattner | 5188ad7 | 2005-01-08 19:28:19 +0000 | [diff] [blame] | 347 | return std::make_pair(TheCall, Chain); |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 348 | } |
| 349 | |
Chris Lattner | 1482458 | 2005-01-09 00:01:27 +0000 | [diff] [blame] | 350 | std::pair<SDOperand, SDOperand> |
| 351 | X86TargetLowering::LowerVAStart(SDOperand Chain, SelectionDAG &DAG) { |
| 352 | // vastart just returns the address of the VarArgsFrameIndex slot. |
| 353 | return std::make_pair(DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32), Chain); |
| 354 | } |
| 355 | |
| 356 | std::pair<SDOperand,SDOperand> X86TargetLowering:: |
| 357 | LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList, |
| 358 | const Type *ArgTy, SelectionDAG &DAG) { |
| 359 | MVT::ValueType ArgVT = getValueType(ArgTy); |
| 360 | SDOperand Result; |
| 361 | if (!isVANext) { |
Chris Lattner | a80d2bd | 2005-05-09 05:40:26 +0000 | [diff] [blame] | 362 | Result = DAG.getLoad(ArgVT, DAG.getEntryNode(), VAList, |
| 363 | DAG.getSrcValue(NULL)); |
Chris Lattner | 1482458 | 2005-01-09 00:01:27 +0000 | [diff] [blame] | 364 | } else { |
| 365 | unsigned Amt; |
| 366 | if (ArgVT == MVT::i32) |
| 367 | Amt = 4; |
| 368 | else { |
| 369 | assert((ArgVT == MVT::i64 || ArgVT == MVT::f64) && |
| 370 | "Other types should have been promoted for varargs!"); |
| 371 | Amt = 8; |
| 372 | } |
| 373 | Result = DAG.getNode(ISD::ADD, VAList.getValueType(), VAList, |
| 374 | DAG.getConstant(Amt, VAList.getValueType())); |
| 375 | } |
| 376 | return std::make_pair(Result, Chain); |
| 377 | } |
Misha Brukman | 0e0a7a45 | 2005-04-21 23:38:14 +0000 | [diff] [blame] | 378 | |
Chris Lattner | c6f4181 | 2005-05-12 23:06:28 +0000 | [diff] [blame] | 379 | //===----------------------------------------------------------------------===// |
| 380 | // Fast Calling Convention implementation |
| 381 | //===----------------------------------------------------------------------===// |
| 382 | // |
| 383 | // The X86 'fast' calling convention passes up to two integer arguments in |
| 384 | // registers (an appropriate portion of EAX/EDX), passes arguments in C order, |
| 385 | // and requires that the callee pop its arguments off the stack (allowing proper |
| 386 | // tail calls), and has the same return value conventions as C calling convs. |
| 387 | // |
| 388 | // Note that this can be enhanced in the future to pass fp vals in registers |
| 389 | // (when we have a global fp allocator) and do other tricks. |
| 390 | // |
Chris Lattner | 63602fb | 2005-05-13 07:38:09 +0000 | [diff] [blame] | 391 | |
| 392 | /// AddLiveIn - This helper function adds the specified physical register to the |
| 393 | /// MachineFunction as a live in value. It also creates a corresponding virtual |
| 394 | /// register for it. |
| 395 | static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg, |
| 396 | TargetRegisterClass *RC) { |
| 397 | assert(RC->contains(PReg) && "Not the correct regclass!"); |
| 398 | unsigned VReg = MF.getSSARegMap()->createVirtualRegister(RC); |
| 399 | MF.addLiveIn(PReg, VReg); |
| 400 | return VReg; |
| 401 | } |
| 402 | |
| 403 | |
Chris Lattner | c6f4181 | 2005-05-12 23:06:28 +0000 | [diff] [blame] | 404 | std::vector<SDOperand> |
| 405 | X86TargetLowering::LowerFastCCArguments(Function &F, SelectionDAG &DAG) { |
| 406 | std::vector<SDOperand> ArgValues; |
| 407 | |
| 408 | MachineFunction &MF = DAG.getMachineFunction(); |
| 409 | MachineFrameInfo *MFI = MF.getFrameInfo(); |
| 410 | |
| 411 | // Add DAG nodes to load the arguments... On entry to a function the stack |
| 412 | // frame looks like this: |
| 413 | // |
| 414 | // [ESP] -- return address |
| 415 | // [ESP + 4] -- first nonreg argument (leftmost lexically) |
| 416 | // [ESP + 8] -- second nonreg argument, if first argument is 4 bytes in size |
| 417 | // ... |
| 418 | unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot |
| 419 | |
| 420 | // Keep track of the number of integer regs passed so far. This can be either |
| 421 | // 0 (neither EAX or EDX used), 1 (EAX is used) or 2 (EAX and EDX are both |
| 422 | // used). |
| 423 | unsigned NumIntRegs = 0; |
| 424 | |
| 425 | for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) { |
| 426 | MVT::ValueType ObjectVT = getValueType(I->getType()); |
| 427 | unsigned ArgIncrement = 4; |
| 428 | unsigned ObjSize = 0; |
| 429 | SDOperand ArgValue; |
| 430 | |
| 431 | switch (ObjectVT) { |
| 432 | default: assert(0 && "Unhandled argument type!"); |
| 433 | case MVT::i1: |
| 434 | case MVT::i8: |
| 435 | if (NumIntRegs < 2) { |
| 436 | if (!I->use_empty()) { |
Chris Lattner | 63602fb | 2005-05-13 07:38:09 +0000 | [diff] [blame] | 437 | unsigned VReg = AddLiveIn(MF, NumIntRegs ? X86::DL : X86::AL, |
| 438 | X86::R8RegisterClass); |
| 439 | ArgValue = DAG.getCopyFromReg(VReg, MVT::i8, DAG.getRoot()); |
Chris Lattner | c6f4181 | 2005-05-12 23:06:28 +0000 | [diff] [blame] | 440 | DAG.setRoot(ArgValue.getValue(1)); |
| 441 | } |
| 442 | ++NumIntRegs; |
| 443 | break; |
| 444 | } |
| 445 | |
| 446 | ObjSize = 1; |
| 447 | break; |
| 448 | case MVT::i16: |
| 449 | if (NumIntRegs < 2) { |
| 450 | if (!I->use_empty()) { |
Chris Lattner | 63602fb | 2005-05-13 07:38:09 +0000 | [diff] [blame] | 451 | unsigned VReg = AddLiveIn(MF, NumIntRegs ? X86::DX : X86::AX, |
| 452 | X86::R16RegisterClass); |
| 453 | ArgValue = DAG.getCopyFromReg(VReg, MVT::i16, DAG.getRoot()); |
Chris Lattner | c6f4181 | 2005-05-12 23:06:28 +0000 | [diff] [blame] | 454 | DAG.setRoot(ArgValue.getValue(1)); |
| 455 | } |
| 456 | ++NumIntRegs; |
| 457 | break; |
| 458 | } |
| 459 | ObjSize = 2; |
| 460 | break; |
| 461 | case MVT::i32: |
| 462 | if (NumIntRegs < 2) { |
| 463 | if (!I->use_empty()) { |
Chris Lattner | 63602fb | 2005-05-13 07:38:09 +0000 | [diff] [blame] | 464 | unsigned VReg = AddLiveIn(MF,NumIntRegs ? X86::EDX : X86::EAX, |
| 465 | X86::R32RegisterClass); |
| 466 | ArgValue = DAG.getCopyFromReg(VReg, MVT::i32, DAG.getRoot()); |
Chris Lattner | c6f4181 | 2005-05-12 23:06:28 +0000 | [diff] [blame] | 467 | DAG.setRoot(ArgValue.getValue(1)); |
| 468 | } |
| 469 | ++NumIntRegs; |
| 470 | break; |
| 471 | } |
| 472 | ObjSize = 4; |
| 473 | break; |
| 474 | case MVT::i64: |
| 475 | if (NumIntRegs == 0) { |
| 476 | if (!I->use_empty()) { |
Chris Lattner | 63602fb | 2005-05-13 07:38:09 +0000 | [diff] [blame] | 477 | unsigned BotReg = AddLiveIn(MF, X86::EAX, X86::R32RegisterClass); |
| 478 | unsigned TopReg = AddLiveIn(MF, X86::EDX, X86::R32RegisterClass); |
Chris Lattner | c6f4181 | 2005-05-12 23:06:28 +0000 | [diff] [blame] | 479 | |
Chris Lattner | 63602fb | 2005-05-13 07:38:09 +0000 | [diff] [blame] | 480 | SDOperand Low=DAG.getCopyFromReg(BotReg, MVT::i32, DAG.getRoot()); |
| 481 | SDOperand Hi =DAG.getCopyFromReg(TopReg, MVT::i32, Low.getValue(1)); |
Chris Lattner | c6f4181 | 2005-05-12 23:06:28 +0000 | [diff] [blame] | 482 | DAG.setRoot(Hi.getValue(1)); |
| 483 | |
| 484 | ArgValue = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Low, Hi); |
| 485 | } |
| 486 | NumIntRegs = 2; |
| 487 | break; |
| 488 | } else if (NumIntRegs == 1) { |
| 489 | if (!I->use_empty()) { |
Chris Lattner | 63602fb | 2005-05-13 07:38:09 +0000 | [diff] [blame] | 490 | unsigned BotReg = AddLiveIn(MF, X86::EDX, X86::R32RegisterClass); |
| 491 | SDOperand Low = DAG.getCopyFromReg(BotReg, MVT::i32, DAG.getRoot()); |
Chris Lattner | c6f4181 | 2005-05-12 23:06:28 +0000 | [diff] [blame] | 492 | DAG.setRoot(Low.getValue(1)); |
| 493 | |
| 494 | // Load the high part from memory. |
| 495 | // Create the frame index object for this incoming parameter... |
| 496 | int FI = MFI->CreateFixedObject(4, ArgOffset); |
| 497 | SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32); |
| 498 | SDOperand Hi = DAG.getLoad(MVT::i32, DAG.getEntryNode(), FIN, |
| 499 | DAG.getSrcValue(NULL)); |
| 500 | ArgValue = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Low, Hi); |
| 501 | } |
| 502 | ArgOffset += 4; |
| 503 | NumIntRegs = 2; |
| 504 | break; |
| 505 | } |
| 506 | ObjSize = ArgIncrement = 8; |
| 507 | break; |
| 508 | case MVT::f32: ObjSize = 4; break; |
| 509 | case MVT::f64: ObjSize = ArgIncrement = 8; break; |
| 510 | } |
| 511 | |
| 512 | // Don't codegen dead arguments. FIXME: remove this check when we can nuke |
| 513 | // dead loads. |
| 514 | if (ObjSize && !I->use_empty()) { |
| 515 | // Create the frame index object for this incoming parameter... |
| 516 | int FI = MFI->CreateFixedObject(ObjSize, ArgOffset); |
| 517 | |
| 518 | // Create the SelectionDAG nodes corresponding to a load from this |
| 519 | // parameter. |
| 520 | SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32); |
| 521 | |
| 522 | ArgValue = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN, |
| 523 | DAG.getSrcValue(NULL)); |
| 524 | } else if (ArgValue.Val == 0) { |
| 525 | if (MVT::isInteger(ObjectVT)) |
| 526 | ArgValue = DAG.getConstant(0, ObjectVT); |
| 527 | else |
| 528 | ArgValue = DAG.getConstantFP(0, ObjectVT); |
| 529 | } |
| 530 | ArgValues.push_back(ArgValue); |
| 531 | |
| 532 | if (ObjSize) |
| 533 | ArgOffset += ArgIncrement; // Move on to the next argument. |
| 534 | } |
| 535 | |
Chris Lattner | 3648c67 | 2005-05-13 21:44:04 +0000 | [diff] [blame^] | 536 | VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs. |
| 537 | ReturnAddrIndex = 0; // No return address slot generated yet. |
| 538 | BytesToPopOnReturn = ArgOffset; // Callee pops all stack arguments. |
Chris Lattner | c6f4181 | 2005-05-12 23:06:28 +0000 | [diff] [blame] | 539 | |
| 540 | // Finally, inform the code generator which regs we return values in. |
| 541 | switch (getValueType(F.getReturnType())) { |
| 542 | default: assert(0 && "Unknown type!"); |
| 543 | case MVT::isVoid: break; |
| 544 | case MVT::i1: |
| 545 | case MVT::i8: |
| 546 | case MVT::i16: |
| 547 | case MVT::i32: |
| 548 | MF.addLiveOut(X86::EAX); |
| 549 | break; |
| 550 | case MVT::i64: |
| 551 | MF.addLiveOut(X86::EAX); |
| 552 | MF.addLiveOut(X86::EDX); |
| 553 | break; |
| 554 | case MVT::f32: |
| 555 | case MVT::f64: |
| 556 | MF.addLiveOut(X86::ST0); |
| 557 | break; |
| 558 | } |
| 559 | return ArgValues; |
| 560 | } |
| 561 | |
| 562 | std::pair<SDOperand, SDOperand> |
| 563 | X86TargetLowering::LowerFastCCCallTo(SDOperand Chain, const Type *RetTy, |
Chris Lattner | 2e7714a | 2005-05-13 20:29:13 +0000 | [diff] [blame] | 564 | bool isTailCall, SDOperand Callee, |
Chris Lattner | c6f4181 | 2005-05-12 23:06:28 +0000 | [diff] [blame] | 565 | ArgListTy &Args, SelectionDAG &DAG) { |
| 566 | // Count how many bytes are to be pushed on the stack. |
| 567 | unsigned NumBytes = 0; |
| 568 | |
| 569 | // Keep track of the number of integer regs passed so far. This can be either |
| 570 | // 0 (neither EAX or EDX used), 1 (EAX is used) or 2 (EAX and EDX are both |
| 571 | // used). |
| 572 | unsigned NumIntRegs = 0; |
| 573 | |
| 574 | for (unsigned i = 0, e = Args.size(); i != e; ++i) |
| 575 | switch (getValueType(Args[i].second)) { |
| 576 | default: assert(0 && "Unknown value type!"); |
| 577 | case MVT::i1: |
| 578 | case MVT::i8: |
| 579 | case MVT::i16: |
| 580 | case MVT::i32: |
| 581 | if (NumIntRegs < 2) { |
| 582 | ++NumIntRegs; |
| 583 | break; |
| 584 | } |
| 585 | // fall through |
| 586 | case MVT::f32: |
| 587 | NumBytes += 4; |
| 588 | break; |
| 589 | case MVT::i64: |
| 590 | if (NumIntRegs == 0) { |
| 591 | NumIntRegs = 2; |
| 592 | break; |
| 593 | } else if (NumIntRegs == 1) { |
| 594 | NumIntRegs = 2; |
| 595 | NumBytes += 4; |
| 596 | break; |
| 597 | } |
| 598 | |
| 599 | // fall through |
| 600 | case MVT::f64: |
| 601 | NumBytes += 8; |
| 602 | break; |
| 603 | } |
| 604 | |
Chris Lattner | 16cd04d | 2005-05-12 23:24:06 +0000 | [diff] [blame] | 605 | Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain, |
Chris Lattner | c6f4181 | 2005-05-12 23:06:28 +0000 | [diff] [blame] | 606 | DAG.getConstant(NumBytes, getPointerTy())); |
| 607 | |
| 608 | // Arguments go on the stack in reverse order, as specified by the ABI. |
| 609 | unsigned ArgOffset = 0; |
| 610 | SDOperand StackPtr = DAG.getCopyFromReg(X86::ESP, MVT::i32, |
| 611 | DAG.getEntryNode()); |
| 612 | NumIntRegs = 0; |
| 613 | std::vector<SDOperand> Stores; |
| 614 | std::vector<SDOperand> RegValuesToPass; |
| 615 | for (unsigned i = 0, e = Args.size(); i != e; ++i) { |
| 616 | switch (getValueType(Args[i].second)) { |
| 617 | default: assert(0 && "Unexpected ValueType for argument!"); |
| 618 | case MVT::i1: |
| 619 | case MVT::i8: |
| 620 | case MVT::i16: |
| 621 | case MVT::i32: |
| 622 | if (NumIntRegs < 2) { |
| 623 | RegValuesToPass.push_back(Args[i].first); |
| 624 | ++NumIntRegs; |
| 625 | break; |
| 626 | } |
| 627 | // Fall through |
| 628 | case MVT::f32: { |
| 629 | SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy()); |
| 630 | PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff); |
| 631 | Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain, |
| 632 | Args[i].first, PtrOff, |
| 633 | DAG.getSrcValue(NULL))); |
| 634 | ArgOffset += 4; |
| 635 | break; |
| 636 | } |
| 637 | case MVT::i64: |
| 638 | if (NumIntRegs < 2) { // Can pass part of it in regs? |
| 639 | SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, |
| 640 | Args[i].first, DAG.getConstant(1, MVT::i32)); |
| 641 | SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, |
| 642 | Args[i].first, DAG.getConstant(0, MVT::i32)); |
| 643 | RegValuesToPass.push_back(Lo); |
| 644 | ++NumIntRegs; |
| 645 | if (NumIntRegs < 2) { // Pass both parts in regs? |
| 646 | RegValuesToPass.push_back(Hi); |
| 647 | ++NumIntRegs; |
| 648 | } else { |
| 649 | // Pass the high part in memory. |
| 650 | SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy()); |
| 651 | PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff); |
| 652 | Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain, |
| 653 | Args[i].first, PtrOff, |
| 654 | DAG.getSrcValue(NULL))); |
| 655 | ArgOffset += 4; |
| 656 | } |
| 657 | break; |
| 658 | } |
| 659 | // Fall through |
| 660 | case MVT::f64: |
| 661 | SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy()); |
| 662 | PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff); |
| 663 | Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain, |
| 664 | Args[i].first, PtrOff, |
| 665 | DAG.getSrcValue(NULL))); |
| 666 | ArgOffset += 8; |
| 667 | break; |
| 668 | } |
| 669 | } |
| 670 | if (!Stores.empty()) |
| 671 | Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, Stores); |
| 672 | |
| 673 | std::vector<MVT::ValueType> RetVals; |
| 674 | MVT::ValueType RetTyVT = getValueType(RetTy); |
| 675 | if (RetTyVT != MVT::isVoid) |
| 676 | RetVals.push_back(RetTyVT); |
| 677 | RetVals.push_back(MVT::Other); |
| 678 | |
| 679 | SDOperand TheCall = SDOperand(DAG.getCall(RetVals, Chain, Callee, |
Chris Lattner | 2e7714a | 2005-05-13 20:29:13 +0000 | [diff] [blame] | 680 | RegValuesToPass, isTailCall), 0); |
Chris Lattner | c6f4181 | 2005-05-12 23:06:28 +0000 | [diff] [blame] | 681 | Chain = TheCall.getValue(RetTyVT != MVT::isVoid); |
Chris Lattner | 16cd04d | 2005-05-12 23:24:06 +0000 | [diff] [blame] | 682 | Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain, |
Chris Lattner | adf6a96 | 2005-05-13 18:50:42 +0000 | [diff] [blame] | 683 | DAG.getConstant(NumBytes, getPointerTy()), |
| 684 | // The callee pops the arguments off the stack. |
| 685 | DAG.getConstant(ArgOffset, getPointerTy())); |
Chris Lattner | c6f4181 | 2005-05-12 23:06:28 +0000 | [diff] [blame] | 686 | return std::make_pair(TheCall, Chain); |
| 687 | } |
| 688 | |
| 689 | |
| 690 | |
Chris Lattner | 1482458 | 2005-01-09 00:01:27 +0000 | [diff] [blame] | 691 | |
| 692 | std::pair<SDOperand, SDOperand> X86TargetLowering:: |
| 693 | LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth, |
| 694 | SelectionDAG &DAG) { |
| 695 | SDOperand Result; |
| 696 | if (Depth) // Depths > 0 not supported yet! |
| 697 | Result = DAG.getConstant(0, getPointerTy()); |
| 698 | else { |
| 699 | if (ReturnAddrIndex == 0) { |
| 700 | // Set up a frame object for the return address. |
| 701 | MachineFunction &MF = DAG.getMachineFunction(); |
| 702 | ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(4, -4); |
| 703 | } |
Misha Brukman | 0e0a7a45 | 2005-04-21 23:38:14 +0000 | [diff] [blame] | 704 | |
Chris Lattner | 1482458 | 2005-01-09 00:01:27 +0000 | [diff] [blame] | 705 | SDOperand RetAddrFI = DAG.getFrameIndex(ReturnAddrIndex, MVT::i32); |
| 706 | |
| 707 | if (!isFrameAddress) |
| 708 | // Just load the return address |
Chris Lattner | c6f4181 | 2005-05-12 23:06:28 +0000 | [diff] [blame] | 709 | Result = DAG.getLoad(MVT::i32, DAG.getEntryNode(), RetAddrFI, |
| 710 | DAG.getSrcValue(NULL)); |
Chris Lattner | 1482458 | 2005-01-09 00:01:27 +0000 | [diff] [blame] | 711 | else |
| 712 | Result = DAG.getNode(ISD::SUB, MVT::i32, RetAddrFI, |
| 713 | DAG.getConstant(4, MVT::i32)); |
| 714 | } |
| 715 | return std::make_pair(Result, Chain); |
| 716 | } |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 717 | |
| 718 | |
Chris Lattner | 98a8ba0 | 2005-01-18 01:06:26 +0000 | [diff] [blame] | 719 | namespace { |
| 720 | /// X86ISelAddressMode - This corresponds to X86AddressMode, but uses |
| 721 | /// SDOperand's instead of register numbers for the leaves of the matched |
| 722 | /// tree. |
| 723 | struct X86ISelAddressMode { |
| 724 | enum { |
| 725 | RegBase, |
| 726 | FrameIndexBase, |
| 727 | } BaseType; |
Misha Brukman | 0e0a7a45 | 2005-04-21 23:38:14 +0000 | [diff] [blame] | 728 | |
Chris Lattner | 98a8ba0 | 2005-01-18 01:06:26 +0000 | [diff] [blame] | 729 | struct { // This is really a union, discriminated by BaseType! |
| 730 | SDOperand Reg; |
| 731 | int FrameIndex; |
| 732 | } Base; |
Misha Brukman | 0e0a7a45 | 2005-04-21 23:38:14 +0000 | [diff] [blame] | 733 | |
Chris Lattner | 98a8ba0 | 2005-01-18 01:06:26 +0000 | [diff] [blame] | 734 | unsigned Scale; |
| 735 | SDOperand IndexReg; |
| 736 | unsigned Disp; |
| 737 | GlobalValue *GV; |
Misha Brukman | 0e0a7a45 | 2005-04-21 23:38:14 +0000 | [diff] [blame] | 738 | |
Chris Lattner | 98a8ba0 | 2005-01-18 01:06:26 +0000 | [diff] [blame] | 739 | X86ISelAddressMode() |
| 740 | : BaseType(RegBase), Scale(1), IndexReg(), Disp(), GV(0) { |
| 741 | } |
| 742 | }; |
| 743 | } |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 744 | |
| 745 | |
| 746 | namespace { |
| 747 | Statistic<> |
| 748 | NumFPKill("x86-codegen", "Number of FP_REG_KILL instructions added"); |
| 749 | |
| 750 | //===--------------------------------------------------------------------===// |
| 751 | /// ISel - X86 specific code to select X86 machine instructions for |
| 752 | /// SelectionDAG operations. |
| 753 | /// |
| 754 | class ISel : public SelectionDAGISel { |
| 755 | /// ContainsFPCode - Every instruction we select that uses or defines a FP |
| 756 | /// register should set this to true. |
| 757 | bool ContainsFPCode; |
| 758 | |
| 759 | /// X86Lowering - This object fully describes how to lower LLVM code to an |
| 760 | /// X86-specific SelectionDAG. |
| 761 | X86TargetLowering X86Lowering; |
| 762 | |
Chris Lattner | 1133309 | 2005-01-11 03:11:44 +0000 | [diff] [blame] | 763 | /// RegPressureMap - This keeps an approximate count of the number of |
| 764 | /// registers required to evaluate each node in the graph. |
| 765 | std::map<SDNode*, unsigned> RegPressureMap; |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 766 | |
| 767 | /// ExprMap - As shared expressions are codegen'd, we keep track of which |
| 768 | /// vreg the value is produced in, so we only emit one copy of each compiled |
| 769 | /// tree. |
| 770 | std::map<SDOperand, unsigned> ExprMap; |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 771 | |
| 772 | public: |
| 773 | ISel(TargetMachine &TM) : SelectionDAGISel(X86Lowering), X86Lowering(TM) { |
| 774 | } |
| 775 | |
Chris Lattner | 67b1c3c | 2005-01-21 21:35:14 +0000 | [diff] [blame] | 776 | virtual const char *getPassName() const { |
| 777 | return "X86 Pattern Instruction Selection"; |
| 778 | } |
| 779 | |
Chris Lattner | 1133309 | 2005-01-11 03:11:44 +0000 | [diff] [blame] | 780 | unsigned getRegPressure(SDOperand O) { |
| 781 | return RegPressureMap[O.Val]; |
| 782 | } |
| 783 | unsigned ComputeRegPressure(SDOperand O); |
| 784 | |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 785 | /// InstructionSelectBasicBlock - This callback is invoked by |
| 786 | /// SelectionDAGISel when it has created a SelectionDAG for us to codegen. |
Chris Lattner | 7dbcb75 | 2005-01-12 04:21:28 +0000 | [diff] [blame] | 787 | virtual void InstructionSelectBasicBlock(SelectionDAG &DAG); |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 788 | |
Chris Lattner | 63602fb | 2005-05-13 07:38:09 +0000 | [diff] [blame] | 789 | virtual void EmitFunctionEntryCode(Function &Fn, MachineFunction &MF); |
| 790 | |
Chris Lattner | 44129b5 | 2005-01-25 20:03:11 +0000 | [diff] [blame] | 791 | bool isFoldableLoad(SDOperand Op, SDOperand OtherOp, |
| 792 | bool FloatPromoteOk = false); |
Chris Lattner | a5ade06 | 2005-01-11 21:19:59 +0000 | [diff] [blame] | 793 | void EmitFoldedLoad(SDOperand Op, X86AddressMode &AM); |
Chris Lattner | e10269b | 2005-01-17 19:25:26 +0000 | [diff] [blame] | 794 | bool TryToFoldLoadOpStore(SDNode *Node); |
Chris Lattner | a5ade06 | 2005-01-11 21:19:59 +0000 | [diff] [blame] | 795 | |
Chris Lattner | 30ea1e9 | 2005-01-19 07:37:26 +0000 | [diff] [blame] | 796 | bool EmitOrOpOp(SDOperand Op1, SDOperand Op2, unsigned DestReg); |
Chris Lattner | cb1aa8d | 2005-01-17 01:34:14 +0000 | [diff] [blame] | 797 | void EmitCMP(SDOperand LHS, SDOperand RHS, bool isOnlyUse); |
Chris Lattner | 6c07aee | 2005-01-11 04:06:27 +0000 | [diff] [blame] | 798 | bool EmitBranchCC(MachineBasicBlock *Dest, SDOperand Chain, SDOperand Cond); |
Chris Lattner | 24aad1b | 2005-01-10 22:10:13 +0000 | [diff] [blame] | 799 | void EmitSelectCC(SDOperand Cond, MVT::ValueType SVT, |
| 800 | unsigned RTrue, unsigned RFalse, unsigned RDest); |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 801 | unsigned SelectExpr(SDOperand N); |
Chris Lattner | 98a8ba0 | 2005-01-18 01:06:26 +0000 | [diff] [blame] | 802 | |
| 803 | X86AddressMode SelectAddrExprs(const X86ISelAddressMode &IAM); |
| 804 | bool MatchAddress(SDOperand N, X86ISelAddressMode &AM); |
| 805 | void SelectAddress(SDOperand N, X86AddressMode &AM); |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 806 | void Select(SDOperand N); |
| 807 | }; |
| 808 | } |
| 809 | |
Chris Lattner | 6415bb4 | 2005-05-10 03:53:18 +0000 | [diff] [blame] | 810 | /// EmitSpecialCodeForMain - Emit any code that needs to be executed only in |
| 811 | /// the main function. |
| 812 | static void EmitSpecialCodeForMain(MachineBasicBlock *BB, |
| 813 | MachineFrameInfo *MFI) { |
| 814 | // Switch the FPU to 64-bit precision mode for better compatibility and speed. |
| 815 | int CWFrameIdx = MFI->CreateStackObject(2, 2); |
| 816 | addFrameReference(BuildMI(BB, X86::FNSTCW16m, 4), CWFrameIdx); |
| 817 | |
| 818 | // Set the high part to be 64-bit precision. |
| 819 | addFrameReference(BuildMI(BB, X86::MOV8mi, 5), |
| 820 | CWFrameIdx, 1).addImm(2); |
| 821 | |
| 822 | // Reload the modified control word now. |
| 823 | addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx); |
| 824 | } |
| 825 | |
Chris Lattner | 63602fb | 2005-05-13 07:38:09 +0000 | [diff] [blame] | 826 | void ISel::EmitFunctionEntryCode(Function &Fn, MachineFunction &MF) { |
| 827 | // If this function has live-in values, emit the copies from pregs to vregs at |
| 828 | // the top of the function, before anything else. |
| 829 | MachineBasicBlock *BB = MF.begin(); |
| 830 | if (MF.livein_begin() != MF.livein_end()) { |
| 831 | SSARegMap *RegMap = MF.getSSARegMap(); |
| 832 | for (MachineFunction::livein_iterator LI = MF.livein_begin(), |
| 833 | E = MF.livein_end(); LI != E; ++LI) { |
| 834 | const TargetRegisterClass *RC = RegMap->getRegClass(LI->second); |
| 835 | if (RC == X86::R8RegisterClass) { |
| 836 | BuildMI(BB, X86::MOV8rr, 1, LI->second).addReg(LI->first); |
| 837 | } else if (RC == X86::R16RegisterClass) { |
| 838 | BuildMI(BB, X86::MOV16rr, 1, LI->second).addReg(LI->first); |
| 839 | } else if (RC == X86::R32RegisterClass) { |
| 840 | BuildMI(BB, X86::MOV32rr, 1, LI->second).addReg(LI->first); |
| 841 | } else if (RC == X86::RFPRegisterClass) { |
| 842 | BuildMI(BB, X86::FpMOV, 1, LI->second).addReg(LI->first); |
| 843 | } else { |
| 844 | assert(0 && "Unknown regclass!"); |
| 845 | } |
| 846 | } |
| 847 | } |
| 848 | |
| 849 | |
| 850 | // If this is main, emit special code for main. |
| 851 | if (Fn.hasExternalLinkage() && Fn.getName() == "main") |
| 852 | EmitSpecialCodeForMain(BB, MF.getFrameInfo()); |
| 853 | } |
| 854 | |
| 855 | |
Chris Lattner | 7dbcb75 | 2005-01-12 04:21:28 +0000 | [diff] [blame] | 856 | /// InstructionSelectBasicBlock - This callback is invoked by SelectionDAGISel |
| 857 | /// when it has created a SelectionDAG for us to codegen. |
| 858 | void ISel::InstructionSelectBasicBlock(SelectionDAG &DAG) { |
| 859 | // While we're doing this, keep track of whether we see any FP code for |
| 860 | // FP_REG_KILL insertion. |
| 861 | ContainsFPCode = false; |
Chris Lattner | 6415bb4 | 2005-05-10 03:53:18 +0000 | [diff] [blame] | 862 | MachineFunction *MF = BB->getParent(); |
Chris Lattner | 7dbcb75 | 2005-01-12 04:21:28 +0000 | [diff] [blame] | 863 | |
| 864 | // Scan the PHI nodes that already are inserted into this basic block. If any |
| 865 | // of them is a PHI of a floating point value, we need to insert an |
| 866 | // FP_REG_KILL. |
Chris Lattner | 6415bb4 | 2005-05-10 03:53:18 +0000 | [diff] [blame] | 867 | SSARegMap *RegMap = MF->getSSARegMap(); |
Chris Lattner | 63602fb | 2005-05-13 07:38:09 +0000 | [diff] [blame] | 868 | if (BB != MF->begin()) |
| 869 | for (MachineBasicBlock::iterator I = BB->begin(), E = BB->end(); |
| 870 | I != E; ++I) { |
| 871 | assert(I->getOpcode() == X86::PHI && |
| 872 | "Isn't just PHI nodes?"); |
| 873 | if (RegMap->getRegClass(I->getOperand(0).getReg()) == |
| 874 | X86::RFPRegisterClass) { |
| 875 | ContainsFPCode = true; |
| 876 | break; |
| 877 | } |
Chris Lattner | 7dbcb75 | 2005-01-12 04:21:28 +0000 | [diff] [blame] | 878 | } |
Chris Lattner | 6415bb4 | 2005-05-10 03:53:18 +0000 | [diff] [blame] | 879 | |
Chris Lattner | 7dbcb75 | 2005-01-12 04:21:28 +0000 | [diff] [blame] | 880 | // Compute the RegPressureMap, which is an approximation for the number of |
| 881 | // registers required to compute each node. |
| 882 | ComputeRegPressure(DAG.getRoot()); |
| 883 | |
| 884 | // Codegen the basic block. |
| 885 | Select(DAG.getRoot()); |
| 886 | |
| 887 | // Finally, look at all of the successors of this block. If any contain a PHI |
| 888 | // node of FP type, we need to insert an FP_REG_KILL in this block. |
| 889 | for (MachineBasicBlock::succ_iterator SI = BB->succ_begin(), |
| 890 | E = BB->succ_end(); SI != E && !ContainsFPCode; ++SI) |
| 891 | for (MachineBasicBlock::iterator I = (*SI)->begin(), E = (*SI)->end(); |
| 892 | I != E && I->getOpcode() == X86::PHI; ++I) { |
| 893 | if (RegMap->getRegClass(I->getOperand(0).getReg()) == |
| 894 | X86::RFPRegisterClass) { |
| 895 | ContainsFPCode = true; |
| 896 | break; |
| 897 | } |
| 898 | } |
Misha Brukman | 0e0a7a45 | 2005-04-21 23:38:14 +0000 | [diff] [blame] | 899 | |
Chris Lattner | e3e0f27 | 2005-05-09 03:36:39 +0000 | [diff] [blame] | 900 | // Final check, check LLVM BB's that are successors to the LLVM BB |
| 901 | // corresponding to BB for FP PHI nodes. |
| 902 | const BasicBlock *LLVMBB = BB->getBasicBlock(); |
| 903 | const PHINode *PN; |
| 904 | if (!ContainsFPCode) |
| 905 | for (succ_const_iterator SI = succ_begin(LLVMBB), E = succ_end(LLVMBB); |
| 906 | SI != E && !ContainsFPCode; ++SI) |
| 907 | for (BasicBlock::const_iterator II = SI->begin(); |
| 908 | (PN = dyn_cast<PHINode>(II)); ++II) |
| 909 | if (PN->getType()->isFloatingPoint()) { |
| 910 | ContainsFPCode = true; |
| 911 | break; |
| 912 | } |
| 913 | |
| 914 | |
Chris Lattner | 7dbcb75 | 2005-01-12 04:21:28 +0000 | [diff] [blame] | 915 | // Insert FP_REG_KILL instructions into basic blocks that need them. This |
| 916 | // only occurs due to the floating point stackifier not being aggressive |
| 917 | // enough to handle arbitrary global stackification. |
| 918 | // |
| 919 | // Currently we insert an FP_REG_KILL instruction into each block that uses or |
| 920 | // defines a floating point virtual register. |
| 921 | // |
| 922 | // When the global register allocators (like linear scan) finally update live |
| 923 | // variable analysis, we can keep floating point values in registers across |
| 924 | // basic blocks. This will be a huge win, but we are waiting on the global |
| 925 | // allocators before we can do this. |
| 926 | // |
Chris Lattner | 71df3f8 | 2005-03-30 01:10:00 +0000 | [diff] [blame] | 927 | if (ContainsFPCode) { |
Chris Lattner | 7dbcb75 | 2005-01-12 04:21:28 +0000 | [diff] [blame] | 928 | BuildMI(*BB, BB->getFirstTerminator(), X86::FP_REG_KILL, 0); |
| 929 | ++NumFPKill; |
| 930 | } |
Misha Brukman | 0e0a7a45 | 2005-04-21 23:38:14 +0000 | [diff] [blame] | 931 | |
Chris Lattner | 7dbcb75 | 2005-01-12 04:21:28 +0000 | [diff] [blame] | 932 | // Clear state used for selection. |
| 933 | ExprMap.clear(); |
Chris Lattner | 7dbcb75 | 2005-01-12 04:21:28 +0000 | [diff] [blame] | 934 | RegPressureMap.clear(); |
| 935 | } |
| 936 | |
| 937 | |
Chris Lattner | 1133309 | 2005-01-11 03:11:44 +0000 | [diff] [blame] | 938 | // ComputeRegPressure - Compute the RegPressureMap, which is an approximation |
| 939 | // for the number of registers required to compute each node. This is basically |
| 940 | // computing a generalized form of the Sethi-Ullman number for each node. |
| 941 | unsigned ISel::ComputeRegPressure(SDOperand O) { |
| 942 | SDNode *N = O.Val; |
| 943 | unsigned &Result = RegPressureMap[N]; |
| 944 | if (Result) return Result; |
| 945 | |
Chris Lattner | a3aa2e2 | 2005-01-11 03:37:59 +0000 | [diff] [blame] | 946 | // FIXME: Should operations like CALL (which clobber lots o regs) have a |
| 947 | // higher fixed cost?? |
| 948 | |
Chris Lattner | c4b6a78 | 2005-01-11 22:29:12 +0000 | [diff] [blame] | 949 | if (N->getNumOperands() == 0) { |
| 950 | Result = 1; |
| 951 | } else { |
| 952 | unsigned MaxRegUse = 0; |
| 953 | unsigned NumExtraMaxRegUsers = 0; |
| 954 | for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) { |
| 955 | unsigned Regs; |
| 956 | if (N->getOperand(i).getOpcode() == ISD::Constant) |
| 957 | Regs = 0; |
| 958 | else |
| 959 | Regs = ComputeRegPressure(N->getOperand(i)); |
| 960 | if (Regs > MaxRegUse) { |
| 961 | MaxRegUse = Regs; |
| 962 | NumExtraMaxRegUsers = 0; |
| 963 | } else if (Regs == MaxRegUse && |
| 964 | N->getOperand(i).getValueType() != MVT::Other) { |
| 965 | ++NumExtraMaxRegUsers; |
| 966 | } |
Chris Lattner | 1133309 | 2005-01-11 03:11:44 +0000 | [diff] [blame] | 967 | } |
Chris Lattner | 90d1be7 | 2005-01-17 22:56:09 +0000 | [diff] [blame] | 968 | |
| 969 | if (O.getOpcode() != ISD::TokenFactor) |
| 970 | Result = MaxRegUse+NumExtraMaxRegUsers; |
| 971 | else |
Chris Lattner | 869e043 | 2005-01-17 23:02:13 +0000 | [diff] [blame] | 972 | Result = MaxRegUse == 1 ? 0 : MaxRegUse-1; |
Chris Lattner | c4b6a78 | 2005-01-11 22:29:12 +0000 | [diff] [blame] | 973 | } |
Chris Lattner | afce430 | 2005-01-12 02:19:06 +0000 | [diff] [blame] | 974 | |
Chris Lattner | 837caa7 | 2005-01-11 23:21:30 +0000 | [diff] [blame] | 975 | //std::cerr << " WEIGHT: " << Result << " "; N->dump(); std::cerr << "\n"; |
Chris Lattner | c4b6a78 | 2005-01-11 22:29:12 +0000 | [diff] [blame] | 976 | return Result; |
Chris Lattner | 1133309 | 2005-01-11 03:11:44 +0000 | [diff] [blame] | 977 | } |
| 978 | |
Chris Lattner | bf52d49 | 2005-01-20 16:50:16 +0000 | [diff] [blame] | 979 | /// NodeTransitivelyUsesValue - Return true if N or any of its uses uses Op. |
| 980 | /// The DAG cannot have cycles in it, by definition, so the visited set is not |
| 981 | /// needed to prevent infinite loops. The DAG CAN, however, have unbounded |
| 982 | /// reuse, so it prevents exponential cases. |
| 983 | /// |
| 984 | static bool NodeTransitivelyUsesValue(SDOperand N, SDOperand Op, |
| 985 | std::set<SDNode*> &Visited) { |
| 986 | if (N == Op) return true; // Found it. |
| 987 | SDNode *Node = N.Val; |
Chris Lattner | fb0f53f | 2005-01-21 21:43:02 +0000 | [diff] [blame] | 988 | if (Node->getNumOperands() == 0 || // Leaf? |
| 989 | Node->getNodeDepth() <= Op.getNodeDepth()) return false; // Can't find it? |
Chris Lattner | bf52d49 | 2005-01-20 16:50:16 +0000 | [diff] [blame] | 990 | if (!Visited.insert(Node).second) return false; // Already visited? |
| 991 | |
| 992 | // Recurse for the first N-1 operands. |
| 993 | for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i) |
| 994 | if (NodeTransitivelyUsesValue(Node->getOperand(i), Op, Visited)) |
| 995 | return true; |
| 996 | |
| 997 | // Tail recurse for the last operand. |
| 998 | return NodeTransitivelyUsesValue(Node->getOperand(0), Op, Visited); |
| 999 | } |
| 1000 | |
Chris Lattner | 98a8ba0 | 2005-01-18 01:06:26 +0000 | [diff] [blame] | 1001 | X86AddressMode ISel::SelectAddrExprs(const X86ISelAddressMode &IAM) { |
| 1002 | X86AddressMode Result; |
| 1003 | |
| 1004 | // If we need to emit two register operands, emit the one with the highest |
| 1005 | // register pressure first. |
| 1006 | if (IAM.BaseType == X86ISelAddressMode::RegBase && |
| 1007 | IAM.Base.Reg.Val && IAM.IndexReg.Val) { |
Chris Lattner | bf52d49 | 2005-01-20 16:50:16 +0000 | [diff] [blame] | 1008 | bool EmitBaseThenIndex; |
Chris Lattner | 98a8ba0 | 2005-01-18 01:06:26 +0000 | [diff] [blame] | 1009 | if (getRegPressure(IAM.Base.Reg) > getRegPressure(IAM.IndexReg)) { |
Chris Lattner | bf52d49 | 2005-01-20 16:50:16 +0000 | [diff] [blame] | 1010 | std::set<SDNode*> Visited; |
| 1011 | EmitBaseThenIndex = true; |
| 1012 | // If Base ends up pointing to Index, we must emit index first. This is |
| 1013 | // because of the way we fold loads, we may end up doing bad things with |
| 1014 | // the folded add. |
| 1015 | if (NodeTransitivelyUsesValue(IAM.Base.Reg, IAM.IndexReg, Visited)) |
| 1016 | EmitBaseThenIndex = false; |
| 1017 | } else { |
| 1018 | std::set<SDNode*> Visited; |
| 1019 | EmitBaseThenIndex = false; |
| 1020 | // If Base ends up pointing to Index, we must emit index first. This is |
| 1021 | // because of the way we fold loads, we may end up doing bad things with |
| 1022 | // the folded add. |
| 1023 | if (NodeTransitivelyUsesValue(IAM.IndexReg, IAM.Base.Reg, Visited)) |
| 1024 | EmitBaseThenIndex = true; |
| 1025 | } |
| 1026 | |
| 1027 | if (EmitBaseThenIndex) { |
Chris Lattner | 98a8ba0 | 2005-01-18 01:06:26 +0000 | [diff] [blame] | 1028 | Result.Base.Reg = SelectExpr(IAM.Base.Reg); |
| 1029 | Result.IndexReg = SelectExpr(IAM.IndexReg); |
| 1030 | } else { |
| 1031 | Result.IndexReg = SelectExpr(IAM.IndexReg); |
| 1032 | Result.Base.Reg = SelectExpr(IAM.Base.Reg); |
| 1033 | } |
Chris Lattner | bf52d49 | 2005-01-20 16:50:16 +0000 | [diff] [blame] | 1034 | |
Chris Lattner | 98a8ba0 | 2005-01-18 01:06:26 +0000 | [diff] [blame] | 1035 | } else if (IAM.BaseType == X86ISelAddressMode::RegBase && IAM.Base.Reg.Val) { |
| 1036 | Result.Base.Reg = SelectExpr(IAM.Base.Reg); |
| 1037 | } else if (IAM.IndexReg.Val) { |
| 1038 | Result.IndexReg = SelectExpr(IAM.IndexReg); |
| 1039 | } |
Misha Brukman | 0e0a7a45 | 2005-04-21 23:38:14 +0000 | [diff] [blame] | 1040 | |
Chris Lattner | 98a8ba0 | 2005-01-18 01:06:26 +0000 | [diff] [blame] | 1041 | switch (IAM.BaseType) { |
| 1042 | case X86ISelAddressMode::RegBase: |
| 1043 | Result.BaseType = X86AddressMode::RegBase; |
| 1044 | break; |
| 1045 | case X86ISelAddressMode::FrameIndexBase: |
| 1046 | Result.BaseType = X86AddressMode::FrameIndexBase; |
| 1047 | Result.Base.FrameIndex = IAM.Base.FrameIndex; |
| 1048 | break; |
| 1049 | default: |
| 1050 | assert(0 && "Unknown base type!"); |
| 1051 | break; |
| 1052 | } |
| 1053 | Result.Scale = IAM.Scale; |
| 1054 | Result.Disp = IAM.Disp; |
| 1055 | Result.GV = IAM.GV; |
| 1056 | return Result; |
| 1057 | } |
| 1058 | |
| 1059 | /// SelectAddress - Pattern match the maximal addressing mode for this node and |
| 1060 | /// emit all of the leaf registers. |
| 1061 | void ISel::SelectAddress(SDOperand N, X86AddressMode &AM) { |
| 1062 | X86ISelAddressMode IAM; |
| 1063 | MatchAddress(N, IAM); |
| 1064 | AM = SelectAddrExprs(IAM); |
| 1065 | } |
| 1066 | |
| 1067 | /// MatchAddress - Add the specified node to the specified addressing mode, |
| 1068 | /// returning true if it cannot be done. This just pattern matches for the |
| 1069 | /// addressing mode, it does not cause any code to be emitted. For that, use |
| 1070 | /// SelectAddress. |
| 1071 | bool ISel::MatchAddress(SDOperand N, X86ISelAddressMode &AM) { |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 1072 | switch (N.getOpcode()) { |
| 1073 | default: break; |
| 1074 | case ISD::FrameIndex: |
Chris Lattner | 98a8ba0 | 2005-01-18 01:06:26 +0000 | [diff] [blame] | 1075 | if (AM.BaseType == X86ISelAddressMode::RegBase && AM.Base.Reg.Val == 0) { |
| 1076 | AM.BaseType = X86ISelAddressMode::FrameIndexBase; |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 1077 | AM.Base.FrameIndex = cast<FrameIndexSDNode>(N)->getIndex(); |
| 1078 | return false; |
| 1079 | } |
| 1080 | break; |
| 1081 | case ISD::GlobalAddress: |
| 1082 | if (AM.GV == 0) { |
| 1083 | AM.GV = cast<GlobalAddressSDNode>(N)->getGlobal(); |
| 1084 | return false; |
| 1085 | } |
| 1086 | break; |
| 1087 | case ISD::Constant: |
| 1088 | AM.Disp += cast<ConstantSDNode>(N)->getValue(); |
| 1089 | return false; |
| 1090 | case ISD::SHL: |
Chris Lattner | 636e79a | 2005-01-13 05:53:16 +0000 | [diff] [blame] | 1091 | // We might have folded the load into this shift, so don't regen the value |
| 1092 | // if so. |
| 1093 | if (ExprMap.count(N)) break; |
| 1094 | |
Chris Lattner | 98a8ba0 | 2005-01-18 01:06:26 +0000 | [diff] [blame] | 1095 | if (AM.IndexReg.Val == 0 && AM.Scale == 1) |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 1096 | if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1))) { |
| 1097 | unsigned Val = CN->getValue(); |
| 1098 | if (Val == 1 || Val == 2 || Val == 3) { |
| 1099 | AM.Scale = 1 << Val; |
Chris Lattner | 51a2634 | 2005-01-11 06:36:20 +0000 | [diff] [blame] | 1100 | SDOperand ShVal = N.Val->getOperand(0); |
| 1101 | |
| 1102 | // Okay, we know that we have a scale by now. However, if the scaled |
| 1103 | // value is an add of something and a constant, we can fold the |
| 1104 | // constant into the disp field here. |
Chris Lattner | 811482a | 2005-01-18 04:18:32 +0000 | [diff] [blame] | 1105 | if (ShVal.Val->getOpcode() == ISD::ADD && ShVal.hasOneUse() && |
Chris Lattner | 51a2634 | 2005-01-11 06:36:20 +0000 | [diff] [blame] | 1106 | isa<ConstantSDNode>(ShVal.Val->getOperand(1))) { |
Chris Lattner | 98a8ba0 | 2005-01-18 01:06:26 +0000 | [diff] [blame] | 1107 | AM.IndexReg = ShVal.Val->getOperand(0); |
Chris Lattner | 51a2634 | 2005-01-11 06:36:20 +0000 | [diff] [blame] | 1108 | ConstantSDNode *AddVal = |
| 1109 | cast<ConstantSDNode>(ShVal.Val->getOperand(1)); |
| 1110 | AM.Disp += AddVal->getValue() << Val; |
Chris Lattner | 636e79a | 2005-01-13 05:53:16 +0000 | [diff] [blame] | 1111 | } else { |
Chris Lattner | 98a8ba0 | 2005-01-18 01:06:26 +0000 | [diff] [blame] | 1112 | AM.IndexReg = ShVal; |
Chris Lattner | 51a2634 | 2005-01-11 06:36:20 +0000 | [diff] [blame] | 1113 | } |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 1114 | return false; |
| 1115 | } |
| 1116 | } |
| 1117 | break; |
Chris Lattner | 947d544 | 2005-01-11 19:37:02 +0000 | [diff] [blame] | 1118 | case ISD::MUL: |
Chris Lattner | 636e79a | 2005-01-13 05:53:16 +0000 | [diff] [blame] | 1119 | // We might have folded the load into this mul, so don't regen the value if |
| 1120 | // so. |
| 1121 | if (ExprMap.count(N)) break; |
| 1122 | |
Chris Lattner | 947d544 | 2005-01-11 19:37:02 +0000 | [diff] [blame] | 1123 | // X*[3,5,9] -> X+X*[2,4,8] |
Chris Lattner | 98a8ba0 | 2005-01-18 01:06:26 +0000 | [diff] [blame] | 1124 | if (AM.IndexReg.Val == 0 && AM.BaseType == X86ISelAddressMode::RegBase && |
| 1125 | AM.Base.Reg.Val == 0) |
Chris Lattner | 947d544 | 2005-01-11 19:37:02 +0000 | [diff] [blame] | 1126 | if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1))) |
| 1127 | if (CN->getValue() == 3 || CN->getValue() == 5 || CN->getValue() == 9) { |
| 1128 | AM.Scale = unsigned(CN->getValue())-1; |
| 1129 | |
| 1130 | SDOperand MulVal = N.Val->getOperand(0); |
Chris Lattner | 98a8ba0 | 2005-01-18 01:06:26 +0000 | [diff] [blame] | 1131 | SDOperand Reg; |
Chris Lattner | 947d544 | 2005-01-11 19:37:02 +0000 | [diff] [blame] | 1132 | |
| 1133 | // Okay, we know that we have a scale by now. However, if the scaled |
| 1134 | // value is an add of something and a constant, we can fold the |
| 1135 | // constant into the disp field here. |
Chris Lattner | 811482a | 2005-01-18 04:18:32 +0000 | [diff] [blame] | 1136 | if (MulVal.Val->getOpcode() == ISD::ADD && MulVal.hasOneUse() && |
Chris Lattner | 947d544 | 2005-01-11 19:37:02 +0000 | [diff] [blame] | 1137 | isa<ConstantSDNode>(MulVal.Val->getOperand(1))) { |
Chris Lattner | 98a8ba0 | 2005-01-18 01:06:26 +0000 | [diff] [blame] | 1138 | Reg = MulVal.Val->getOperand(0); |
Chris Lattner | 947d544 | 2005-01-11 19:37:02 +0000 | [diff] [blame] | 1139 | ConstantSDNode *AddVal = |
| 1140 | cast<ConstantSDNode>(MulVal.Val->getOperand(1)); |
| 1141 | AM.Disp += AddVal->getValue() * CN->getValue(); |
Misha Brukman | 0e0a7a45 | 2005-04-21 23:38:14 +0000 | [diff] [blame] | 1142 | } else { |
Chris Lattner | 98a8ba0 | 2005-01-18 01:06:26 +0000 | [diff] [blame] | 1143 | Reg = N.Val->getOperand(0); |
Chris Lattner | 947d544 | 2005-01-11 19:37:02 +0000 | [diff] [blame] | 1144 | } |
| 1145 | |
| 1146 | AM.IndexReg = AM.Base.Reg = Reg; |
| 1147 | return false; |
| 1148 | } |
| 1149 | break; |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 1150 | |
| 1151 | case ISD::ADD: { |
Chris Lattner | 636e79a | 2005-01-13 05:53:16 +0000 | [diff] [blame] | 1152 | // We might have folded the load into this mul, so don't regen the value if |
| 1153 | // so. |
| 1154 | if (ExprMap.count(N)) break; |
| 1155 | |
Chris Lattner | 98a8ba0 | 2005-01-18 01:06:26 +0000 | [diff] [blame] | 1156 | X86ISelAddressMode Backup = AM; |
| 1157 | if (!MatchAddress(N.Val->getOperand(0), AM) && |
| 1158 | !MatchAddress(N.Val->getOperand(1), AM)) |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 1159 | return false; |
| 1160 | AM = Backup; |
Chris Lattner | 98a8ba0 | 2005-01-18 01:06:26 +0000 | [diff] [blame] | 1161 | if (!MatchAddress(N.Val->getOperand(1), AM) && |
| 1162 | !MatchAddress(N.Val->getOperand(0), AM)) |
Chris Lattner | 9bbd992 | 2005-01-12 18:08:53 +0000 | [diff] [blame] | 1163 | return false; |
| 1164 | AM = Backup; |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 1165 | break; |
| 1166 | } |
| 1167 | } |
| 1168 | |
Chris Lattner | a95589b | 2005-01-11 04:40:19 +0000 | [diff] [blame] | 1169 | // Is the base register already occupied? |
Chris Lattner | 98a8ba0 | 2005-01-18 01:06:26 +0000 | [diff] [blame] | 1170 | if (AM.BaseType != X86ISelAddressMode::RegBase || AM.Base.Reg.Val) { |
Chris Lattner | a95589b | 2005-01-11 04:40:19 +0000 | [diff] [blame] | 1171 | // If so, check to see if the scale index register is set. |
Chris Lattner | 98a8ba0 | 2005-01-18 01:06:26 +0000 | [diff] [blame] | 1172 | if (AM.IndexReg.Val == 0) { |
| 1173 | AM.IndexReg = N; |
Chris Lattner | a95589b | 2005-01-11 04:40:19 +0000 | [diff] [blame] | 1174 | AM.Scale = 1; |
| 1175 | return false; |
| 1176 | } |
| 1177 | |
| 1178 | // Otherwise, we cannot select it. |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 1179 | return true; |
Chris Lattner | a95589b | 2005-01-11 04:40:19 +0000 | [diff] [blame] | 1180 | } |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 1181 | |
| 1182 | // Default, generate it as a register. |
Chris Lattner | 98a8ba0 | 2005-01-18 01:06:26 +0000 | [diff] [blame] | 1183 | AM.BaseType = X86ISelAddressMode::RegBase; |
| 1184 | AM.Base.Reg = N; |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 1185 | return false; |
| 1186 | } |
| 1187 | |
| 1188 | /// Emit2SetCCsAndLogical - Emit the following sequence of instructions, |
| 1189 | /// assuming that the temporary registers are in the 8-bit register class. |
| 1190 | /// |
| 1191 | /// Tmp1 = setcc1 |
| 1192 | /// Tmp2 = setcc2 |
| 1193 | /// DestReg = logicalop Tmp1, Tmp2 |
| 1194 | /// |
| 1195 | static void Emit2SetCCsAndLogical(MachineBasicBlock *BB, unsigned SetCC1, |
| 1196 | unsigned SetCC2, unsigned LogicalOp, |
| 1197 | unsigned DestReg) { |
| 1198 | SSARegMap *RegMap = BB->getParent()->getSSARegMap(); |
| 1199 | unsigned Tmp1 = RegMap->createVirtualRegister(X86::R8RegisterClass); |
| 1200 | unsigned Tmp2 = RegMap->createVirtualRegister(X86::R8RegisterClass); |
| 1201 | BuildMI(BB, SetCC1, 0, Tmp1); |
| 1202 | BuildMI(BB, SetCC2, 0, Tmp2); |
| 1203 | BuildMI(BB, LogicalOp, 2, DestReg).addReg(Tmp1).addReg(Tmp2); |
| 1204 | } |
| 1205 | |
| 1206 | /// EmitSetCC - Emit the code to set the specified 8-bit register to 1 if the |
| 1207 | /// condition codes match the specified SetCCOpcode. Note that some conditions |
| 1208 | /// require multiple instructions to generate the correct value. |
| 1209 | static void EmitSetCC(MachineBasicBlock *BB, unsigned DestReg, |
| 1210 | ISD::CondCode SetCCOpcode, bool isFP) { |
| 1211 | unsigned Opc; |
| 1212 | if (!isFP) { |
| 1213 | switch (SetCCOpcode) { |
| 1214 | default: assert(0 && "Illegal integer SetCC!"); |
| 1215 | case ISD::SETEQ: Opc = X86::SETEr; break; |
| 1216 | case ISD::SETGT: Opc = X86::SETGr; break; |
| 1217 | case ISD::SETGE: Opc = X86::SETGEr; break; |
| 1218 | case ISD::SETLT: Opc = X86::SETLr; break; |
| 1219 | case ISD::SETLE: Opc = X86::SETLEr; break; |
| 1220 | case ISD::SETNE: Opc = X86::SETNEr; break; |
| 1221 | case ISD::SETULT: Opc = X86::SETBr; break; |
| 1222 | case ISD::SETUGT: Opc = X86::SETAr; break; |
| 1223 | case ISD::SETULE: Opc = X86::SETBEr; break; |
| 1224 | case ISD::SETUGE: Opc = X86::SETAEr; break; |
| 1225 | } |
| 1226 | } else { |
| 1227 | // On a floating point condition, the flags are set as follows: |
| 1228 | // ZF PF CF op |
| 1229 | // 0 | 0 | 0 | X > Y |
| 1230 | // 0 | 0 | 1 | X < Y |
| 1231 | // 1 | 0 | 0 | X == Y |
| 1232 | // 1 | 1 | 1 | unordered |
| 1233 | // |
| 1234 | switch (SetCCOpcode) { |
| 1235 | default: assert(0 && "Invalid FP setcc!"); |
| 1236 | case ISD::SETUEQ: |
| 1237 | case ISD::SETEQ: |
| 1238 | Opc = X86::SETEr; // True if ZF = 1 |
| 1239 | break; |
| 1240 | case ISD::SETOGT: |
| 1241 | case ISD::SETGT: |
| 1242 | Opc = X86::SETAr; // True if CF = 0 and ZF = 0 |
| 1243 | break; |
| 1244 | case ISD::SETOGE: |
| 1245 | case ISD::SETGE: |
| 1246 | Opc = X86::SETAEr; // True if CF = 0 |
| 1247 | break; |
| 1248 | case ISD::SETULT: |
| 1249 | case ISD::SETLT: |
| 1250 | Opc = X86::SETBr; // True if CF = 1 |
| 1251 | break; |
| 1252 | case ISD::SETULE: |
| 1253 | case ISD::SETLE: |
| 1254 | Opc = X86::SETBEr; // True if CF = 1 or ZF = 1 |
| 1255 | break; |
| 1256 | case ISD::SETONE: |
| 1257 | case ISD::SETNE: |
| 1258 | Opc = X86::SETNEr; // True if ZF = 0 |
| 1259 | break; |
| 1260 | case ISD::SETUO: |
| 1261 | Opc = X86::SETPr; // True if PF = 1 |
| 1262 | break; |
| 1263 | case ISD::SETO: |
| 1264 | Opc = X86::SETNPr; // True if PF = 0 |
| 1265 | break; |
| 1266 | case ISD::SETOEQ: // !PF & ZF |
| 1267 | Emit2SetCCsAndLogical(BB, X86::SETNPr, X86::SETEr, X86::AND8rr, DestReg); |
| 1268 | return; |
| 1269 | case ISD::SETOLT: // !PF & CF |
| 1270 | Emit2SetCCsAndLogical(BB, X86::SETNPr, X86::SETBr, X86::AND8rr, DestReg); |
| 1271 | return; |
| 1272 | case ISD::SETOLE: // !PF & (CF || ZF) |
| 1273 | Emit2SetCCsAndLogical(BB, X86::SETNPr, X86::SETBEr, X86::AND8rr, DestReg); |
| 1274 | return; |
| 1275 | case ISD::SETUGT: // PF | (!ZF & !CF) |
| 1276 | Emit2SetCCsAndLogical(BB, X86::SETPr, X86::SETAr, X86::OR8rr, DestReg); |
| 1277 | return; |
| 1278 | case ISD::SETUGE: // PF | !CF |
| 1279 | Emit2SetCCsAndLogical(BB, X86::SETPr, X86::SETAEr, X86::OR8rr, DestReg); |
| 1280 | return; |
| 1281 | case ISD::SETUNE: // PF | !ZF |
| 1282 | Emit2SetCCsAndLogical(BB, X86::SETPr, X86::SETNEr, X86::OR8rr, DestReg); |
| 1283 | return; |
| 1284 | } |
| 1285 | } |
| 1286 | BuildMI(BB, Opc, 0, DestReg); |
| 1287 | } |
| 1288 | |
| 1289 | |
| 1290 | /// EmitBranchCC - Emit code into BB that arranges for control to transfer to |
| 1291 | /// the Dest block if the Cond condition is true. If we cannot fold this |
| 1292 | /// condition into the branch, return true. |
| 1293 | /// |
Chris Lattner | 6c07aee | 2005-01-11 04:06:27 +0000 | [diff] [blame] | 1294 | bool ISel::EmitBranchCC(MachineBasicBlock *Dest, SDOperand Chain, |
| 1295 | SDOperand Cond) { |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 1296 | // FIXME: Evaluate whether it would be good to emit code like (X < Y) | (A > |
| 1297 | // B) using two conditional branches instead of one condbr, two setcc's, and |
| 1298 | // an or. |
| 1299 | if ((Cond.getOpcode() == ISD::OR || |
| 1300 | Cond.getOpcode() == ISD::AND) && Cond.Val->hasOneUse()) { |
| 1301 | // And and or set the flags for us, so there is no need to emit a TST of the |
| 1302 | // result. It is only safe to do this if there is only a single use of the |
| 1303 | // AND/OR though, otherwise we don't know it will be emitted here. |
Chris Lattner | 6c07aee | 2005-01-11 04:06:27 +0000 | [diff] [blame] | 1304 | Select(Chain); |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 1305 | SelectExpr(Cond); |
| 1306 | BuildMI(BB, X86::JNE, 1).addMBB(Dest); |
| 1307 | return false; |
| 1308 | } |
| 1309 | |
| 1310 | // Codegen br not C -> JE. |
| 1311 | if (Cond.getOpcode() == ISD::XOR) |
| 1312 | if (ConstantSDNode *NC = dyn_cast<ConstantSDNode>(Cond.Val->getOperand(1))) |
| 1313 | if (NC->isAllOnesValue()) { |
Chris Lattner | 6c07aee | 2005-01-11 04:06:27 +0000 | [diff] [blame] | 1314 | unsigned CondR; |
| 1315 | if (getRegPressure(Chain) > getRegPressure(Cond)) { |
| 1316 | Select(Chain); |
| 1317 | CondR = SelectExpr(Cond.Val->getOperand(0)); |
| 1318 | } else { |
| 1319 | CondR = SelectExpr(Cond.Val->getOperand(0)); |
| 1320 | Select(Chain); |
| 1321 | } |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 1322 | BuildMI(BB, X86::TEST8rr, 2).addReg(CondR).addReg(CondR); |
| 1323 | BuildMI(BB, X86::JE, 1).addMBB(Dest); |
| 1324 | return false; |
| 1325 | } |
| 1326 | |
| 1327 | SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Cond); |
| 1328 | if (SetCC == 0) |
| 1329 | return true; // Can only handle simple setcc's so far. |
| 1330 | |
| 1331 | unsigned Opc; |
| 1332 | |
| 1333 | // Handle integer conditions first. |
| 1334 | if (MVT::isInteger(SetCC->getOperand(0).getValueType())) { |
| 1335 | switch (SetCC->getCondition()) { |
| 1336 | default: assert(0 && "Illegal integer SetCC!"); |
| 1337 | case ISD::SETEQ: Opc = X86::JE; break; |
| 1338 | case ISD::SETGT: Opc = X86::JG; break; |
| 1339 | case ISD::SETGE: Opc = X86::JGE; break; |
| 1340 | case ISD::SETLT: Opc = X86::JL; break; |
| 1341 | case ISD::SETLE: Opc = X86::JLE; break; |
| 1342 | case ISD::SETNE: Opc = X86::JNE; break; |
| 1343 | case ISD::SETULT: Opc = X86::JB; break; |
| 1344 | case ISD::SETUGT: Opc = X86::JA; break; |
| 1345 | case ISD::SETULE: Opc = X86::JBE; break; |
| 1346 | case ISD::SETUGE: Opc = X86::JAE; break; |
| 1347 | } |
Chris Lattner | 6c07aee | 2005-01-11 04:06:27 +0000 | [diff] [blame] | 1348 | Select(Chain); |
Chris Lattner | cb1aa8d | 2005-01-17 01:34:14 +0000 | [diff] [blame] | 1349 | EmitCMP(SetCC->getOperand(0), SetCC->getOperand(1), SetCC->hasOneUse()); |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 1350 | BuildMI(BB, Opc, 1).addMBB(Dest); |
| 1351 | return false; |
| 1352 | } |
| 1353 | |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 1354 | unsigned Opc2 = 0; // Second branch if needed. |
| 1355 | |
| 1356 | // On a floating point condition, the flags are set as follows: |
| 1357 | // ZF PF CF op |
| 1358 | // 0 | 0 | 0 | X > Y |
| 1359 | // 0 | 0 | 1 | X < Y |
| 1360 | // 1 | 0 | 0 | X == Y |
| 1361 | // 1 | 1 | 1 | unordered |
| 1362 | // |
| 1363 | switch (SetCC->getCondition()) { |
| 1364 | default: assert(0 && "Invalid FP setcc!"); |
| 1365 | case ISD::SETUEQ: |
| 1366 | case ISD::SETEQ: Opc = X86::JE; break; // True if ZF = 1 |
| 1367 | case ISD::SETOGT: |
| 1368 | case ISD::SETGT: Opc = X86::JA; break; // True if CF = 0 and ZF = 0 |
| 1369 | case ISD::SETOGE: |
| 1370 | case ISD::SETGE: Opc = X86::JAE; break; // True if CF = 0 |
| 1371 | case ISD::SETULT: |
| 1372 | case ISD::SETLT: Opc = X86::JB; break; // True if CF = 1 |
| 1373 | case ISD::SETULE: |
| 1374 | case ISD::SETLE: Opc = X86::JBE; break; // True if CF = 1 or ZF = 1 |
| 1375 | case ISD::SETONE: |
| 1376 | case ISD::SETNE: Opc = X86::JNE; break; // True if ZF = 0 |
| 1377 | case ISD::SETUO: Opc = X86::JP; break; // True if PF = 1 |
| 1378 | case ISD::SETO: Opc = X86::JNP; break; // True if PF = 0 |
| 1379 | case ISD::SETUGT: // PF = 1 | (ZF = 0 & CF = 0) |
| 1380 | Opc = X86::JA; // ZF = 0 & CF = 0 |
| 1381 | Opc2 = X86::JP; // PF = 1 |
| 1382 | break; |
| 1383 | case ISD::SETUGE: // PF = 1 | CF = 0 |
| 1384 | Opc = X86::JAE; // CF = 0 |
| 1385 | Opc2 = X86::JP; // PF = 1 |
| 1386 | break; |
| 1387 | case ISD::SETUNE: // PF = 1 | ZF = 0 |
| 1388 | Opc = X86::JNE; // ZF = 0 |
| 1389 | Opc2 = X86::JP; // PF = 1 |
| 1390 | break; |
| 1391 | case ISD::SETOEQ: // PF = 0 & ZF = 1 |
| 1392 | //X86::JNP, X86::JE |
| 1393 | //X86::AND8rr |
| 1394 | return true; // FIXME: Emit more efficient code for this branch. |
| 1395 | case ISD::SETOLT: // PF = 0 & CF = 1 |
| 1396 | //X86::JNP, X86::JB |
| 1397 | //X86::AND8rr |
| 1398 | return true; // FIXME: Emit more efficient code for this branch. |
| 1399 | case ISD::SETOLE: // PF = 0 & (CF = 1 || ZF = 1) |
| 1400 | //X86::JNP, X86::JBE |
| 1401 | //X86::AND8rr |
| 1402 | return true; // FIXME: Emit more efficient code for this branch. |
| 1403 | } |
| 1404 | |
Chris Lattner | 6c07aee | 2005-01-11 04:06:27 +0000 | [diff] [blame] | 1405 | Select(Chain); |
Chris Lattner | cb1aa8d | 2005-01-17 01:34:14 +0000 | [diff] [blame] | 1406 | EmitCMP(SetCC->getOperand(0), SetCC->getOperand(1), SetCC->hasOneUse()); |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 1407 | BuildMI(BB, Opc, 1).addMBB(Dest); |
| 1408 | if (Opc2) |
| 1409 | BuildMI(BB, Opc2, 1).addMBB(Dest); |
| 1410 | return false; |
| 1411 | } |
| 1412 | |
Chris Lattner | 24aad1b | 2005-01-10 22:10:13 +0000 | [diff] [blame] | 1413 | /// EmitSelectCC - Emit code into BB that performs a select operation between |
| 1414 | /// the two registers RTrue and RFalse, generating a result into RDest. Return |
| 1415 | /// true if the fold cannot be performed. |
| 1416 | /// |
| 1417 | void ISel::EmitSelectCC(SDOperand Cond, MVT::ValueType SVT, |
| 1418 | unsigned RTrue, unsigned RFalse, unsigned RDest) { |
| 1419 | enum Condition { |
| 1420 | EQ, NE, LT, LE, GT, GE, B, BE, A, AE, P, NP, |
| 1421 | NOT_SET |
| 1422 | } CondCode = NOT_SET; |
| 1423 | |
| 1424 | static const unsigned CMOVTAB16[] = { |
| 1425 | X86::CMOVE16rr, X86::CMOVNE16rr, X86::CMOVL16rr, X86::CMOVLE16rr, |
| 1426 | X86::CMOVG16rr, X86::CMOVGE16rr, X86::CMOVB16rr, X86::CMOVBE16rr, |
Misha Brukman | 0e0a7a45 | 2005-04-21 23:38:14 +0000 | [diff] [blame] | 1427 | X86::CMOVA16rr, X86::CMOVAE16rr, X86::CMOVP16rr, X86::CMOVNP16rr, |
Chris Lattner | 24aad1b | 2005-01-10 22:10:13 +0000 | [diff] [blame] | 1428 | }; |
| 1429 | static const unsigned CMOVTAB32[] = { |
| 1430 | X86::CMOVE32rr, X86::CMOVNE32rr, X86::CMOVL32rr, X86::CMOVLE32rr, |
| 1431 | X86::CMOVG32rr, X86::CMOVGE32rr, X86::CMOVB32rr, X86::CMOVBE32rr, |
Misha Brukman | 0e0a7a45 | 2005-04-21 23:38:14 +0000 | [diff] [blame] | 1432 | X86::CMOVA32rr, X86::CMOVAE32rr, X86::CMOVP32rr, X86::CMOVNP32rr, |
Chris Lattner | 24aad1b | 2005-01-10 22:10:13 +0000 | [diff] [blame] | 1433 | }; |
| 1434 | static const unsigned CMOVTABFP[] = { |
| 1435 | X86::FCMOVE , X86::FCMOVNE, /*missing*/0, /*missing*/0, |
| 1436 | /*missing*/0, /*missing*/0, X86::FCMOVB , X86::FCMOVBE, |
| 1437 | X86::FCMOVA , X86::FCMOVAE, X86::FCMOVP , X86::FCMOVNP |
| 1438 | }; |
| 1439 | |
| 1440 | if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Cond)) { |
| 1441 | if (MVT::isInteger(SetCC->getOperand(0).getValueType())) { |
| 1442 | switch (SetCC->getCondition()) { |
| 1443 | default: assert(0 && "Unknown integer comparison!"); |
| 1444 | case ISD::SETEQ: CondCode = EQ; break; |
| 1445 | case ISD::SETGT: CondCode = GT; break; |
| 1446 | case ISD::SETGE: CondCode = GE; break; |
| 1447 | case ISD::SETLT: CondCode = LT; break; |
| 1448 | case ISD::SETLE: CondCode = LE; break; |
| 1449 | case ISD::SETNE: CondCode = NE; break; |
| 1450 | case ISD::SETULT: CondCode = B; break; |
| 1451 | case ISD::SETUGT: CondCode = A; break; |
| 1452 | case ISD::SETULE: CondCode = BE; break; |
| 1453 | case ISD::SETUGE: CondCode = AE; break; |
| 1454 | } |
| 1455 | } else { |
| 1456 | // On a floating point condition, the flags are set as follows: |
| 1457 | // ZF PF CF op |
| 1458 | // 0 | 0 | 0 | X > Y |
| 1459 | // 0 | 0 | 1 | X < Y |
| 1460 | // 1 | 0 | 0 | X == Y |
| 1461 | // 1 | 1 | 1 | unordered |
| 1462 | // |
| 1463 | switch (SetCC->getCondition()) { |
| 1464 | default: assert(0 && "Unknown FP comparison!"); |
| 1465 | case ISD::SETUEQ: |
| 1466 | case ISD::SETEQ: CondCode = EQ; break; // True if ZF = 1 |
| 1467 | case ISD::SETOGT: |
| 1468 | case ISD::SETGT: CondCode = A; break; // True if CF = 0 and ZF = 0 |
| 1469 | case ISD::SETOGE: |
| 1470 | case ISD::SETGE: CondCode = AE; break; // True if CF = 0 |
| 1471 | case ISD::SETULT: |
| 1472 | case ISD::SETLT: CondCode = B; break; // True if CF = 1 |
| 1473 | case ISD::SETULE: |
| 1474 | case ISD::SETLE: CondCode = BE; break; // True if CF = 1 or ZF = 1 |
| 1475 | case ISD::SETONE: |
| 1476 | case ISD::SETNE: CondCode = NE; break; // True if ZF = 0 |
| 1477 | case ISD::SETUO: CondCode = P; break; // True if PF = 1 |
| 1478 | case ISD::SETO: CondCode = NP; break; // True if PF = 0 |
| 1479 | case ISD::SETUGT: // PF = 1 | (ZF = 0 & CF = 0) |
| 1480 | case ISD::SETUGE: // PF = 1 | CF = 0 |
| 1481 | case ISD::SETUNE: // PF = 1 | ZF = 0 |
| 1482 | case ISD::SETOEQ: // PF = 0 & ZF = 1 |
| 1483 | case ISD::SETOLT: // PF = 0 & CF = 1 |
| 1484 | case ISD::SETOLE: // PF = 0 & (CF = 1 || ZF = 1) |
| 1485 | // We cannot emit this comparison as a single cmov. |
| 1486 | break; |
| 1487 | } |
| 1488 | } |
| 1489 | } |
| 1490 | |
| 1491 | unsigned Opc = 0; |
| 1492 | if (CondCode != NOT_SET) { |
| 1493 | switch (SVT) { |
| 1494 | default: assert(0 && "Cannot select this type!"); |
| 1495 | case MVT::i16: Opc = CMOVTAB16[CondCode]; break; |
| 1496 | case MVT::i32: Opc = CMOVTAB32[CondCode]; break; |
Chris Lattner | ef7ba07 | 2005-01-11 03:50:45 +0000 | [diff] [blame] | 1497 | case MVT::f64: Opc = CMOVTABFP[CondCode]; break; |
Chris Lattner | 24aad1b | 2005-01-10 22:10:13 +0000 | [diff] [blame] | 1498 | } |
| 1499 | } |
| 1500 | |
| 1501 | // Finally, if we weren't able to fold this, just emit the condition and test |
| 1502 | // it. |
| 1503 | if (CondCode == NOT_SET || Opc == 0) { |
| 1504 | // Get the condition into the zero flag. |
| 1505 | unsigned CondReg = SelectExpr(Cond); |
| 1506 | BuildMI(BB, X86::TEST8rr, 2).addReg(CondReg).addReg(CondReg); |
| 1507 | |
| 1508 | switch (SVT) { |
| 1509 | default: assert(0 && "Cannot select this type!"); |
| 1510 | case MVT::i16: Opc = X86::CMOVE16rr; break; |
| 1511 | case MVT::i32: Opc = X86::CMOVE32rr; break; |
Chris Lattner | ef7ba07 | 2005-01-11 03:50:45 +0000 | [diff] [blame] | 1512 | case MVT::f64: Opc = X86::FCMOVE; break; |
Chris Lattner | 24aad1b | 2005-01-10 22:10:13 +0000 | [diff] [blame] | 1513 | } |
| 1514 | } else { |
| 1515 | // FIXME: CMP R, 0 -> TEST R, R |
Chris Lattner | cb1aa8d | 2005-01-17 01:34:14 +0000 | [diff] [blame] | 1516 | EmitCMP(Cond.getOperand(0), Cond.getOperand(1), Cond.Val->hasOneUse()); |
Chris Lattner | a3aa2e2 | 2005-01-11 03:37:59 +0000 | [diff] [blame] | 1517 | std::swap(RTrue, RFalse); |
Chris Lattner | 24aad1b | 2005-01-10 22:10:13 +0000 | [diff] [blame] | 1518 | } |
| 1519 | BuildMI(BB, Opc, 2, RDest).addReg(RTrue).addReg(RFalse); |
| 1520 | } |
| 1521 | |
Chris Lattner | cb1aa8d | 2005-01-17 01:34:14 +0000 | [diff] [blame] | 1522 | void ISel::EmitCMP(SDOperand LHS, SDOperand RHS, bool HasOneUse) { |
Chris Lattner | 1133309 | 2005-01-11 03:11:44 +0000 | [diff] [blame] | 1523 | unsigned Opc; |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 1524 | if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(RHS)) { |
| 1525 | Opc = 0; |
Chris Lattner | 4ff348b | 2005-01-17 06:26:58 +0000 | [diff] [blame] | 1526 | if (HasOneUse && isFoldableLoad(LHS, RHS)) { |
Chris Lattner | ef6806c | 2005-01-12 02:02:48 +0000 | [diff] [blame] | 1527 | switch (RHS.getValueType()) { |
| 1528 | default: break; |
| 1529 | case MVT::i1: |
| 1530 | case MVT::i8: Opc = X86::CMP8mi; break; |
| 1531 | case MVT::i16: Opc = X86::CMP16mi; break; |
| 1532 | case MVT::i32: Opc = X86::CMP32mi; break; |
| 1533 | } |
| 1534 | if (Opc) { |
| 1535 | X86AddressMode AM; |
| 1536 | EmitFoldedLoad(LHS, AM); |
| 1537 | addFullAddress(BuildMI(BB, Opc, 5), AM).addImm(CN->getValue()); |
| 1538 | return; |
| 1539 | } |
| 1540 | } |
| 1541 | |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 1542 | switch (RHS.getValueType()) { |
| 1543 | default: break; |
| 1544 | case MVT::i1: |
| 1545 | case MVT::i8: Opc = X86::CMP8ri; break; |
| 1546 | case MVT::i16: Opc = X86::CMP16ri; break; |
| 1547 | case MVT::i32: Opc = X86::CMP32ri; break; |
| 1548 | } |
| 1549 | if (Opc) { |
Chris Lattner | 1133309 | 2005-01-11 03:11:44 +0000 | [diff] [blame] | 1550 | unsigned Tmp1 = SelectExpr(LHS); |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 1551 | BuildMI(BB, Opc, 2).addReg(Tmp1).addImm(CN->getValue()); |
| 1552 | return; |
| 1553 | } |
Chris Lattner | 7f2afac | 2005-01-14 22:37:41 +0000 | [diff] [blame] | 1554 | } else if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(RHS)) { |
| 1555 | if (CN->isExactlyValue(+0.0) || |
| 1556 | CN->isExactlyValue(-0.0)) { |
| 1557 | unsigned Reg = SelectExpr(LHS); |
| 1558 | BuildMI(BB, X86::FTST, 1).addReg(Reg); |
| 1559 | BuildMI(BB, X86::FNSTSW8r, 0); |
| 1560 | BuildMI(BB, X86::SAHF, 1); |
Chris Lattner | 7805fa4 | 2005-03-17 16:29:26 +0000 | [diff] [blame] | 1561 | return; |
Chris Lattner | 7f2afac | 2005-01-14 22:37:41 +0000 | [diff] [blame] | 1562 | } |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 1563 | } |
| 1564 | |
Chris Lattner | ef6806c | 2005-01-12 02:02:48 +0000 | [diff] [blame] | 1565 | Opc = 0; |
Chris Lattner | 4ff348b | 2005-01-17 06:26:58 +0000 | [diff] [blame] | 1566 | if (HasOneUse && isFoldableLoad(LHS, RHS)) { |
Chris Lattner | ef6806c | 2005-01-12 02:02:48 +0000 | [diff] [blame] | 1567 | switch (RHS.getValueType()) { |
| 1568 | default: break; |
| 1569 | case MVT::i1: |
| 1570 | case MVT::i8: Opc = X86::CMP8mr; break; |
| 1571 | case MVT::i16: Opc = X86::CMP16mr; break; |
| 1572 | case MVT::i32: Opc = X86::CMP32mr; break; |
| 1573 | } |
| 1574 | if (Opc) { |
| 1575 | X86AddressMode AM; |
Chris Lattner | 636e79a | 2005-01-13 05:53:16 +0000 | [diff] [blame] | 1576 | EmitFoldedLoad(LHS, AM); |
| 1577 | unsigned Reg = SelectExpr(RHS); |
Chris Lattner | ef6806c | 2005-01-12 02:02:48 +0000 | [diff] [blame] | 1578 | addFullAddress(BuildMI(BB, Opc, 5), AM).addReg(Reg); |
| 1579 | return; |
| 1580 | } |
| 1581 | } |
| 1582 | |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 1583 | switch (LHS.getValueType()) { |
| 1584 | default: assert(0 && "Cannot compare this value!"); |
| 1585 | case MVT::i1: |
| 1586 | case MVT::i8: Opc = X86::CMP8rr; break; |
| 1587 | case MVT::i16: Opc = X86::CMP16rr; break; |
| 1588 | case MVT::i32: Opc = X86::CMP32rr; break; |
Chris Lattner | ef7ba07 | 2005-01-11 03:50:45 +0000 | [diff] [blame] | 1589 | case MVT::f64: Opc = X86::FUCOMIr; break; |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 1590 | } |
Chris Lattner | 1133309 | 2005-01-11 03:11:44 +0000 | [diff] [blame] | 1591 | unsigned Tmp1, Tmp2; |
| 1592 | if (getRegPressure(LHS) > getRegPressure(RHS)) { |
| 1593 | Tmp1 = SelectExpr(LHS); |
| 1594 | Tmp2 = SelectExpr(RHS); |
| 1595 | } else { |
| 1596 | Tmp2 = SelectExpr(RHS); |
| 1597 | Tmp1 = SelectExpr(LHS); |
| 1598 | } |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 1599 | BuildMI(BB, Opc, 2).addReg(Tmp1).addReg(Tmp2); |
| 1600 | } |
| 1601 | |
Chris Lattner | a5ade06 | 2005-01-11 21:19:59 +0000 | [diff] [blame] | 1602 | /// isFoldableLoad - Return true if this is a load instruction that can safely |
| 1603 | /// be folded into an operation that uses it. |
Chris Lattner | 44129b5 | 2005-01-25 20:03:11 +0000 | [diff] [blame] | 1604 | bool ISel::isFoldableLoad(SDOperand Op, SDOperand OtherOp, bool FloatPromoteOk){ |
| 1605 | if (Op.getOpcode() == ISD::LOAD) { |
| 1606 | // FIXME: currently can't fold constant pool indexes. |
| 1607 | if (isa<ConstantPoolSDNode>(Op.getOperand(1))) |
| 1608 | return false; |
| 1609 | } else if (FloatPromoteOk && Op.getOpcode() == ISD::EXTLOAD && |
| 1610 | cast<MVTSDNode>(Op)->getExtraValueType() == MVT::f32) { |
| 1611 | // FIXME: currently can't fold constant pool indexes. |
| 1612 | if (isa<ConstantPoolSDNode>(Op.getOperand(1))) |
| 1613 | return false; |
| 1614 | } else { |
Chris Lattner | a5ade06 | 2005-01-11 21:19:59 +0000 | [diff] [blame] | 1615 | return false; |
Chris Lattner | 44129b5 | 2005-01-25 20:03:11 +0000 | [diff] [blame] | 1616 | } |
Chris Lattner | a5ade06 | 2005-01-11 21:19:59 +0000 | [diff] [blame] | 1617 | |
| 1618 | // If this load has already been emitted, we clearly can't fold it. |
Chris Lattner | 636e79a | 2005-01-13 05:53:16 +0000 | [diff] [blame] | 1619 | assert(Op.ResNo == 0 && "Not a use of the value of the load?"); |
| 1620 | if (ExprMap.count(Op.getValue(1))) return false; |
| 1621 | assert(!ExprMap.count(Op.getValue(0)) && "Value in map but not token chain?"); |
Chris Lattner | 4a10866 | 2005-01-18 03:51:59 +0000 | [diff] [blame] | 1622 | assert(!ExprMap.count(Op.getValue(1))&&"Token lowered but value not in map?"); |
Chris Lattner | a5ade06 | 2005-01-11 21:19:59 +0000 | [diff] [blame] | 1623 | |
Chris Lattner | 4ff348b | 2005-01-17 06:26:58 +0000 | [diff] [blame] | 1624 | // If there is not just one use of its value, we cannot fold. |
| 1625 | if (!Op.Val->hasNUsesOfValue(1, 0)) return false; |
| 1626 | |
| 1627 | // Finally, we cannot fold the load into the operation if this would induce a |
| 1628 | // cycle into the resultant dag. To check for this, see if OtherOp (the other |
| 1629 | // operand of the operation we are folding the load into) can possible use the |
| 1630 | // chain node defined by the load. |
| 1631 | if (OtherOp.Val && !Op.Val->hasNUsesOfValue(0, 1)) { // Has uses of chain? |
| 1632 | std::set<SDNode*> Visited; |
| 1633 | if (NodeTransitivelyUsesValue(OtherOp, Op.getValue(1), Visited)) |
| 1634 | return false; |
| 1635 | } |
| 1636 | return true; |
Chris Lattner | a5ade06 | 2005-01-11 21:19:59 +0000 | [diff] [blame] | 1637 | } |
| 1638 | |
Chris Lattner | 4ff348b | 2005-01-17 06:26:58 +0000 | [diff] [blame] | 1639 | |
Chris Lattner | a5ade06 | 2005-01-11 21:19:59 +0000 | [diff] [blame] | 1640 | /// EmitFoldedLoad - Ensure that the arguments of the load are code generated, |
| 1641 | /// and compute the address being loaded into AM. |
| 1642 | void ISel::EmitFoldedLoad(SDOperand Op, X86AddressMode &AM) { |
| 1643 | SDOperand Chain = Op.getOperand(0); |
| 1644 | SDOperand Address = Op.getOperand(1); |
Chris Lattner | 98a8ba0 | 2005-01-18 01:06:26 +0000 | [diff] [blame] | 1645 | |
Chris Lattner | a5ade06 | 2005-01-11 21:19:59 +0000 | [diff] [blame] | 1646 | if (getRegPressure(Chain) > getRegPressure(Address)) { |
| 1647 | Select(Chain); |
| 1648 | SelectAddress(Address, AM); |
| 1649 | } else { |
| 1650 | SelectAddress(Address, AM); |
| 1651 | Select(Chain); |
| 1652 | } |
| 1653 | |
| 1654 | // The chain for this load is now lowered. |
Chris Lattner | 636e79a | 2005-01-13 05:53:16 +0000 | [diff] [blame] | 1655 | assert(ExprMap.count(SDOperand(Op.Val, 1)) == 0 && |
| 1656 | "Load emitted more than once?"); |
Chris Lattner | 4a10866 | 2005-01-18 03:51:59 +0000 | [diff] [blame] | 1657 | if (!ExprMap.insert(std::make_pair(Op.getValue(1), 1)).second) |
Chris Lattner | 636e79a | 2005-01-13 05:53:16 +0000 | [diff] [blame] | 1658 | assert(0 && "Load emitted more than once!"); |
Chris Lattner | a5ade06 | 2005-01-11 21:19:59 +0000 | [diff] [blame] | 1659 | } |
| 1660 | |
Chris Lattner | 30ea1e9 | 2005-01-19 07:37:26 +0000 | [diff] [blame] | 1661 | // EmitOrOpOp - Pattern match the expression (Op1|Op2), where we know that op1 |
| 1662 | // and op2 are i8/i16/i32 values with one use each (the or). If we can form a |
| 1663 | // SHLD or SHRD, emit the instruction (generating the value into DestReg) and |
| 1664 | // return true. |
| 1665 | bool ISel::EmitOrOpOp(SDOperand Op1, SDOperand Op2, unsigned DestReg) { |
Chris Lattner | 8571637 | 2005-01-19 06:18:43 +0000 | [diff] [blame] | 1666 | if (Op1.getOpcode() == ISD::SHL && Op2.getOpcode() == ISD::SRL) { |
| 1667 | // good! |
| 1668 | } else if (Op2.getOpcode() == ISD::SHL && Op1.getOpcode() == ISD::SRL) { |
| 1669 | std::swap(Op1, Op2); // Op1 is the SHL now. |
| 1670 | } else { |
| 1671 | return false; // No match |
| 1672 | } |
| 1673 | |
| 1674 | SDOperand ShlVal = Op1.getOperand(0); |
| 1675 | SDOperand ShlAmt = Op1.getOperand(1); |
| 1676 | SDOperand ShrVal = Op2.getOperand(0); |
| 1677 | SDOperand ShrAmt = Op2.getOperand(1); |
| 1678 | |
Chris Lattner | 30ea1e9 | 2005-01-19 07:37:26 +0000 | [diff] [blame] | 1679 | unsigned RegSize = MVT::getSizeInBits(Op1.getValueType()); |
| 1680 | |
Chris Lattner | 8571637 | 2005-01-19 06:18:43 +0000 | [diff] [blame] | 1681 | // Find out if ShrAmt = 32-ShlAmt or ShlAmt = 32-ShrAmt. |
| 1682 | if (ShlAmt.getOpcode() == ISD::SUB && ShlAmt.getOperand(1) == ShrAmt) |
| 1683 | if (ConstantSDNode *SubCST = dyn_cast<ConstantSDNode>(ShlAmt.getOperand(0))) |
Chris Lattner | 4053b1e | 2005-01-19 08:07:05 +0000 | [diff] [blame] | 1684 | if (SubCST->getValue() == RegSize) { |
| 1685 | // (A >> ShrAmt) | (A << (32-ShrAmt)) ==> ROR A, ShrAmt |
Chris Lattner | 8571637 | 2005-01-19 06:18:43 +0000 | [diff] [blame] | 1686 | // (A >> ShrAmt) | (B << (32-ShrAmt)) ==> SHRD A, B, ShrAmt |
Chris Lattner | 4053b1e | 2005-01-19 08:07:05 +0000 | [diff] [blame] | 1687 | if (ShrVal == ShlVal) { |
| 1688 | unsigned Reg, ShAmt; |
| 1689 | if (getRegPressure(ShrVal) > getRegPressure(ShrAmt)) { |
| 1690 | Reg = SelectExpr(ShrVal); |
| 1691 | ShAmt = SelectExpr(ShrAmt); |
| 1692 | } else { |
| 1693 | ShAmt = SelectExpr(ShrAmt); |
| 1694 | Reg = SelectExpr(ShrVal); |
| 1695 | } |
| 1696 | BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShAmt); |
| 1697 | unsigned Opc = RegSize == 8 ? X86::ROR8rCL : |
| 1698 | (RegSize == 16 ? X86::ROR16rCL : X86::ROR32rCL); |
| 1699 | BuildMI(BB, Opc, 1, DestReg).addReg(Reg); |
| 1700 | return true; |
| 1701 | } else if (RegSize != 8) { |
Chris Lattner | 8571637 | 2005-01-19 06:18:43 +0000 | [diff] [blame] | 1702 | unsigned AReg, BReg; |
| 1703 | if (getRegPressure(ShlVal) > getRegPressure(ShrVal)) { |
Chris Lattner | 8571637 | 2005-01-19 06:18:43 +0000 | [diff] [blame] | 1704 | BReg = SelectExpr(ShlVal); |
Chris Lattner | c3c021b | 2005-01-19 17:24:34 +0000 | [diff] [blame] | 1705 | AReg = SelectExpr(ShrVal); |
Chris Lattner | 8571637 | 2005-01-19 06:18:43 +0000 | [diff] [blame] | 1706 | } else { |
Chris Lattner | 8571637 | 2005-01-19 06:18:43 +0000 | [diff] [blame] | 1707 | AReg = SelectExpr(ShrVal); |
Chris Lattner | c3c021b | 2005-01-19 17:24:34 +0000 | [diff] [blame] | 1708 | BReg = SelectExpr(ShlVal); |
Chris Lattner | 8571637 | 2005-01-19 06:18:43 +0000 | [diff] [blame] | 1709 | } |
Chris Lattner | 4053b1e | 2005-01-19 08:07:05 +0000 | [diff] [blame] | 1710 | unsigned ShAmt = SelectExpr(ShrAmt); |
| 1711 | BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShAmt); |
| 1712 | unsigned Opc = RegSize == 16 ? X86::SHRD16rrCL : X86::SHRD32rrCL; |
| 1713 | BuildMI(BB, Opc, 2, DestReg).addReg(AReg).addReg(BReg); |
Chris Lattner | 8571637 | 2005-01-19 06:18:43 +0000 | [diff] [blame] | 1714 | return true; |
| 1715 | } |
| 1716 | } |
| 1717 | |
Chris Lattner | 4053b1e | 2005-01-19 08:07:05 +0000 | [diff] [blame] | 1718 | if (ShrAmt.getOpcode() == ISD::SUB && ShrAmt.getOperand(1) == ShlAmt) |
| 1719 | if (ConstantSDNode *SubCST = dyn_cast<ConstantSDNode>(ShrAmt.getOperand(0))) |
| 1720 | if (SubCST->getValue() == RegSize) { |
| 1721 | // (A << ShlAmt) | (A >> (32-ShlAmt)) ==> ROL A, ShrAmt |
| 1722 | // (A << ShlAmt) | (B >> (32-ShlAmt)) ==> SHLD A, B, ShrAmt |
| 1723 | if (ShrVal == ShlVal) { |
| 1724 | unsigned Reg, ShAmt; |
| 1725 | if (getRegPressure(ShrVal) > getRegPressure(ShlAmt)) { |
| 1726 | Reg = SelectExpr(ShrVal); |
| 1727 | ShAmt = SelectExpr(ShlAmt); |
| 1728 | } else { |
| 1729 | ShAmt = SelectExpr(ShlAmt); |
| 1730 | Reg = SelectExpr(ShrVal); |
| 1731 | } |
| 1732 | BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShAmt); |
| 1733 | unsigned Opc = RegSize == 8 ? X86::ROL8rCL : |
| 1734 | (RegSize == 16 ? X86::ROL16rCL : X86::ROL32rCL); |
| 1735 | BuildMI(BB, Opc, 1, DestReg).addReg(Reg); |
| 1736 | return true; |
| 1737 | } else if (RegSize != 8) { |
| 1738 | unsigned AReg, BReg; |
| 1739 | if (getRegPressure(ShlVal) > getRegPressure(ShrVal)) { |
Chris Lattner | c3c021b | 2005-01-19 17:24:34 +0000 | [diff] [blame] | 1740 | AReg = SelectExpr(ShlVal); |
| 1741 | BReg = SelectExpr(ShrVal); |
Chris Lattner | 4053b1e | 2005-01-19 08:07:05 +0000 | [diff] [blame] | 1742 | } else { |
Chris Lattner | c3c021b | 2005-01-19 17:24:34 +0000 | [diff] [blame] | 1743 | BReg = SelectExpr(ShrVal); |
| 1744 | AReg = SelectExpr(ShlVal); |
Chris Lattner | 4053b1e | 2005-01-19 08:07:05 +0000 | [diff] [blame] | 1745 | } |
| 1746 | unsigned ShAmt = SelectExpr(ShlAmt); |
| 1747 | BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShAmt); |
| 1748 | unsigned Opc = RegSize == 16 ? X86::SHLD16rrCL : X86::SHLD32rrCL; |
| 1749 | BuildMI(BB, Opc, 2, DestReg).addReg(AReg).addReg(BReg); |
| 1750 | return true; |
| 1751 | } |
| 1752 | } |
Chris Lattner | 8571637 | 2005-01-19 06:18:43 +0000 | [diff] [blame] | 1753 | |
Chris Lattner | 4053b1e | 2005-01-19 08:07:05 +0000 | [diff] [blame] | 1754 | if (ConstantSDNode *ShrCst = dyn_cast<ConstantSDNode>(ShrAmt)) |
| 1755 | if (ConstantSDNode *ShlCst = dyn_cast<ConstantSDNode>(ShlAmt)) |
| 1756 | if (ShrCst->getValue() < RegSize && ShlCst->getValue() < RegSize) |
| 1757 | if (ShrCst->getValue() == RegSize-ShlCst->getValue()) { |
| 1758 | // (A >> 5) | (A << 27) --> ROR A, 5 |
| 1759 | // (A >> 5) | (B << 27) --> SHRD A, B, 5 |
| 1760 | if (ShrVal == ShlVal) { |
| 1761 | unsigned Reg = SelectExpr(ShrVal); |
| 1762 | unsigned Opc = RegSize == 8 ? X86::ROR8ri : |
| 1763 | (RegSize == 16 ? X86::ROR16ri : X86::ROR32ri); |
| 1764 | BuildMI(BB, Opc, 2, DestReg).addReg(Reg).addImm(ShrCst->getValue()); |
| 1765 | return true; |
| 1766 | } else if (RegSize != 8) { |
| 1767 | unsigned AReg, BReg; |
| 1768 | if (getRegPressure(ShlVal) > getRegPressure(ShrVal)) { |
Chris Lattner | 4053b1e | 2005-01-19 08:07:05 +0000 | [diff] [blame] | 1769 | BReg = SelectExpr(ShlVal); |
Chris Lattner | c3c021b | 2005-01-19 17:24:34 +0000 | [diff] [blame] | 1770 | AReg = SelectExpr(ShrVal); |
Chris Lattner | 4053b1e | 2005-01-19 08:07:05 +0000 | [diff] [blame] | 1771 | } else { |
Chris Lattner | 4053b1e | 2005-01-19 08:07:05 +0000 | [diff] [blame] | 1772 | AReg = SelectExpr(ShrVal); |
Chris Lattner | c3c021b | 2005-01-19 17:24:34 +0000 | [diff] [blame] | 1773 | BReg = SelectExpr(ShlVal); |
Chris Lattner | 4053b1e | 2005-01-19 08:07:05 +0000 | [diff] [blame] | 1774 | } |
| 1775 | unsigned Opc = RegSize == 16 ? X86::SHRD16rri8 : X86::SHRD32rri8; |
| 1776 | BuildMI(BB, Opc, 3, DestReg).addReg(AReg).addReg(BReg) |
| 1777 | .addImm(ShrCst->getValue()); |
| 1778 | return true; |
| 1779 | } |
| 1780 | } |
Misha Brukman | 0e0a7a45 | 2005-04-21 23:38:14 +0000 | [diff] [blame] | 1781 | |
Chris Lattner | 8571637 | 2005-01-19 06:18:43 +0000 | [diff] [blame] | 1782 | return false; |
| 1783 | } |
| 1784 | |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 1785 | unsigned ISel::SelectExpr(SDOperand N) { |
| 1786 | unsigned Result; |
| 1787 | unsigned Tmp1, Tmp2, Tmp3; |
| 1788 | unsigned Opc = 0; |
Chris Lattner | 5188ad7 | 2005-01-08 19:28:19 +0000 | [diff] [blame] | 1789 | SDNode *Node = N.Val; |
Chris Lattner | a5ade06 | 2005-01-11 21:19:59 +0000 | [diff] [blame] | 1790 | SDOperand Op0, Op1; |
Chris Lattner | 5188ad7 | 2005-01-08 19:28:19 +0000 | [diff] [blame] | 1791 | |
Chris Lattner | 7f2afac | 2005-01-14 22:37:41 +0000 | [diff] [blame] | 1792 | if (Node->getOpcode() == ISD::CopyFromReg) { |
Chris Lattner | c6f4181 | 2005-05-12 23:06:28 +0000 | [diff] [blame] | 1793 | if (MRegisterInfo::isVirtualRegister(cast<RegSDNode>(Node)->getReg()) || |
| 1794 | cast<RegSDNode>(Node)->getReg() == X86::ESP) { |
| 1795 | // Just use the specified register as our input. |
| 1796 | return cast<RegSDNode>(Node)->getReg(); |
| 1797 | } |
Chris Lattner | 7f2afac | 2005-01-14 22:37:41 +0000 | [diff] [blame] | 1798 | } |
Misha Brukman | 0e0a7a45 | 2005-04-21 23:38:14 +0000 | [diff] [blame] | 1799 | |
Chris Lattner | a5ade06 | 2005-01-11 21:19:59 +0000 | [diff] [blame] | 1800 | unsigned &Reg = ExprMap[N]; |
| 1801 | if (Reg) return Reg; |
Misha Brukman | 0e0a7a45 | 2005-04-21 23:38:14 +0000 | [diff] [blame] | 1802 | |
Chris Lattner | b38a749 | 2005-04-02 04:01:14 +0000 | [diff] [blame] | 1803 | switch (N.getOpcode()) { |
| 1804 | default: |
Chris Lattner | a5ade06 | 2005-01-11 21:19:59 +0000 | [diff] [blame] | 1805 | Reg = Result = (N.getValueType() != MVT::Other) ? |
Chris Lattner | b38a749 | 2005-04-02 04:01:14 +0000 | [diff] [blame] | 1806 | MakeReg(N.getValueType()) : 1; |
| 1807 | break; |
Chris Lattner | 2e7714a | 2005-05-13 20:29:13 +0000 | [diff] [blame] | 1808 | case ISD::TAILCALL: |
Chris Lattner | b38a749 | 2005-04-02 04:01:14 +0000 | [diff] [blame] | 1809 | case ISD::CALL: |
Chris Lattner | a5ade06 | 2005-01-11 21:19:59 +0000 | [diff] [blame] | 1810 | // If this is a call instruction, make sure to prepare ALL of the result |
| 1811 | // values as well as the chain. |
Chris Lattner | b38a749 | 2005-04-02 04:01:14 +0000 | [diff] [blame] | 1812 | if (Node->getNumValues() == 1) |
| 1813 | Reg = Result = 1; // Void call, just a chain. |
| 1814 | else { |
Chris Lattner | a5ade06 | 2005-01-11 21:19:59 +0000 | [diff] [blame] | 1815 | Result = MakeReg(Node->getValueType(0)); |
| 1816 | ExprMap[N.getValue(0)] = Result; |
Chris Lattner | b38a749 | 2005-04-02 04:01:14 +0000 | [diff] [blame] | 1817 | for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i) |
Chris Lattner | a5ade06 | 2005-01-11 21:19:59 +0000 | [diff] [blame] | 1818 | ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i)); |
Chris Lattner | b38a749 | 2005-04-02 04:01:14 +0000 | [diff] [blame] | 1819 | ExprMap[SDOperand(Node, Node->getNumValues()-1)] = 1; |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 1820 | } |
Chris Lattner | b38a749 | 2005-04-02 04:01:14 +0000 | [diff] [blame] | 1821 | break; |
| 1822 | case ISD::ADD_PARTS: |
| 1823 | case ISD::SUB_PARTS: |
| 1824 | case ISD::SHL_PARTS: |
| 1825 | case ISD::SRL_PARTS: |
| 1826 | case ISD::SRA_PARTS: |
| 1827 | Result = MakeReg(Node->getValueType(0)); |
| 1828 | ExprMap[N.getValue(0)] = Result; |
| 1829 | for (unsigned i = 1, e = N.Val->getNumValues(); i != e; ++i) |
| 1830 | ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i)); |
| 1831 | break; |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 1832 | } |
Misha Brukman | 0e0a7a45 | 2005-04-21 23:38:14 +0000 | [diff] [blame] | 1833 | |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 1834 | switch (N.getOpcode()) { |
| 1835 | default: |
Chris Lattner | 5188ad7 | 2005-01-08 19:28:19 +0000 | [diff] [blame] | 1836 | Node->dump(); |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 1837 | assert(0 && "Node not handled!\n"); |
Chris Lattner | c6f4181 | 2005-05-12 23:06:28 +0000 | [diff] [blame] | 1838 | case ISD::CopyFromReg: |
| 1839 | Select(N.getOperand(0)); |
| 1840 | if (Result == 1) { |
| 1841 | Reg = Result = ExprMap[N.getValue(0)] = |
| 1842 | MakeReg(N.getValue(0).getValueType()); |
| 1843 | } |
| 1844 | switch (Node->getValueType(0)) { |
| 1845 | default: assert(0 && "Cannot CopyFromReg this!"); |
| 1846 | case MVT::i1: |
| 1847 | case MVT::i8: |
| 1848 | BuildMI(BB, X86::MOV8rr, 1, |
| 1849 | Result).addReg(cast<RegSDNode>(Node)->getReg()); |
| 1850 | return Result; |
| 1851 | case MVT::i16: |
| 1852 | BuildMI(BB, X86::MOV16rr, 1, |
| 1853 | Result).addReg(cast<RegSDNode>(Node)->getReg()); |
| 1854 | return Result; |
| 1855 | case MVT::i32: |
| 1856 | BuildMI(BB, X86::MOV32rr, 1, |
| 1857 | Result).addReg(cast<RegSDNode>(Node)->getReg()); |
| 1858 | return Result; |
| 1859 | } |
| 1860 | |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 1861 | case ISD::FrameIndex: |
| 1862 | Tmp1 = cast<FrameIndexSDNode>(N)->getIndex(); |
| 1863 | addFrameReference(BuildMI(BB, X86::LEA32r, 4, Result), (int)Tmp1); |
| 1864 | return Result; |
| 1865 | case ISD::ConstantPool: |
| 1866 | Tmp1 = cast<ConstantPoolSDNode>(N)->getIndex(); |
| 1867 | addConstantPoolReference(BuildMI(BB, X86::LEA32r, 4, Result), Tmp1); |
| 1868 | return Result; |
| 1869 | case ISD::ConstantFP: |
| 1870 | ContainsFPCode = true; |
| 1871 | Tmp1 = Result; // Intermediate Register |
| 1872 | if (cast<ConstantFPSDNode>(N)->getValue() < 0.0 || |
| 1873 | cast<ConstantFPSDNode>(N)->isExactlyValue(-0.0)) |
| 1874 | Tmp1 = MakeReg(MVT::f64); |
| 1875 | |
| 1876 | if (cast<ConstantFPSDNode>(N)->isExactlyValue(+0.0) || |
| 1877 | cast<ConstantFPSDNode>(N)->isExactlyValue(-0.0)) |
| 1878 | BuildMI(BB, X86::FLD0, 0, Tmp1); |
| 1879 | else if (cast<ConstantFPSDNode>(N)->isExactlyValue(+1.0) || |
| 1880 | cast<ConstantFPSDNode>(N)->isExactlyValue(-1.0)) |
| 1881 | BuildMI(BB, X86::FLD1, 0, Tmp1); |
| 1882 | else |
| 1883 | assert(0 && "Unexpected constant!"); |
| 1884 | if (Tmp1 != Result) |
| 1885 | BuildMI(BB, X86::FCHS, 1, Result).addReg(Tmp1); |
| 1886 | return Result; |
| 1887 | case ISD::Constant: |
| 1888 | switch (N.getValueType()) { |
| 1889 | default: assert(0 && "Cannot use constants of this type!"); |
| 1890 | case MVT::i1: |
| 1891 | case MVT::i8: Opc = X86::MOV8ri; break; |
| 1892 | case MVT::i16: Opc = X86::MOV16ri; break; |
| 1893 | case MVT::i32: Opc = X86::MOV32ri; break; |
| 1894 | } |
| 1895 | BuildMI(BB, Opc, 1,Result).addImm(cast<ConstantSDNode>(N)->getValue()); |
| 1896 | return Result; |
Chris Lattner | 7ce7eff | 2005-04-01 22:46:45 +0000 | [diff] [blame] | 1897 | case ISD::UNDEF: |
| 1898 | if (Node->getValueType(0) == MVT::f64) { |
| 1899 | // FIXME: SHOULD TEACH STACKIFIER ABOUT UNDEF VALUES! |
| 1900 | BuildMI(BB, X86::FLD0, 0, Result); |
| 1901 | } else { |
| 1902 | BuildMI(BB, X86::IMPLICIT_DEF, 0, Result); |
| 1903 | } |
| 1904 | return Result; |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 1905 | case ISD::GlobalAddress: { |
| 1906 | GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal(); |
| 1907 | BuildMI(BB, X86::MOV32ri, 1, Result).addGlobalAddress(GV); |
| 1908 | return Result; |
| 1909 | } |
| 1910 | case ISD::ExternalSymbol: { |
| 1911 | const char *Sym = cast<ExternalSymbolSDNode>(N)->getSymbol(); |
| 1912 | BuildMI(BB, X86::MOV32ri, 1, Result).addExternalSymbol(Sym); |
| 1913 | return Result; |
| 1914 | } |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 1915 | case ISD::ZERO_EXTEND: { |
| 1916 | int DestIs16 = N.getValueType() == MVT::i16; |
| 1917 | int SrcIs16 = N.getOperand(0).getValueType() == MVT::i16; |
Chris Lattner | 590d800 | 2005-01-09 18:52:44 +0000 | [diff] [blame] | 1918 | |
| 1919 | // FIXME: This hack is here for zero extension casts from bool to i8. This |
| 1920 | // would not be needed if bools were promoted by Legalize. |
| 1921 | if (N.getValueType() == MVT::i8) { |
Chris Lattner | dbba22f | 2005-01-11 23:33:00 +0000 | [diff] [blame] | 1922 | Tmp1 = SelectExpr(N.getOperand(0)); |
Chris Lattner | 590d800 | 2005-01-09 18:52:44 +0000 | [diff] [blame] | 1923 | BuildMI(BB, X86::MOV8rr, 1, Result).addReg(Tmp1); |
| 1924 | return Result; |
| 1925 | } |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 1926 | |
Chris Lattner | 4ff348b | 2005-01-17 06:26:58 +0000 | [diff] [blame] | 1927 | if (isFoldableLoad(N.getOperand(0), SDOperand())) { |
Chris Lattner | dbba22f | 2005-01-11 23:33:00 +0000 | [diff] [blame] | 1928 | static const unsigned Opc[3] = { |
| 1929 | X86::MOVZX32rm8, X86::MOVZX32rm16, X86::MOVZX16rm8 |
| 1930 | }; |
| 1931 | |
| 1932 | X86AddressMode AM; |
| 1933 | EmitFoldedLoad(N.getOperand(0), AM); |
| 1934 | addFullAddress(BuildMI(BB, Opc[SrcIs16+DestIs16*2], 4, Result), AM); |
Misha Brukman | 0e0a7a45 | 2005-04-21 23:38:14 +0000 | [diff] [blame] | 1935 | |
Chris Lattner | dbba22f | 2005-01-11 23:33:00 +0000 | [diff] [blame] | 1936 | return Result; |
| 1937 | } |
| 1938 | |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 1939 | static const unsigned Opc[3] = { |
| 1940 | X86::MOVZX32rr8, X86::MOVZX32rr16, X86::MOVZX16rr8 |
| 1941 | }; |
Chris Lattner | dbba22f | 2005-01-11 23:33:00 +0000 | [diff] [blame] | 1942 | Tmp1 = SelectExpr(N.getOperand(0)); |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 1943 | BuildMI(BB, Opc[SrcIs16+DestIs16*2], 1, Result).addReg(Tmp1); |
| 1944 | return Result; |
Misha Brukman | 0e0a7a45 | 2005-04-21 23:38:14 +0000 | [diff] [blame] | 1945 | } |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 1946 | case ISD::SIGN_EXTEND: { |
| 1947 | int DestIs16 = N.getValueType() == MVT::i16; |
| 1948 | int SrcIs16 = N.getOperand(0).getValueType() == MVT::i16; |
| 1949 | |
Chris Lattner | 590d800 | 2005-01-09 18:52:44 +0000 | [diff] [blame] | 1950 | // FIXME: Legalize should promote bools to i8! |
| 1951 | assert(N.getOperand(0).getValueType() != MVT::i1 && |
| 1952 | "Sign extend from bool not implemented!"); |
| 1953 | |
Chris Lattner | 4ff348b | 2005-01-17 06:26:58 +0000 | [diff] [blame] | 1954 | if (isFoldableLoad(N.getOperand(0), SDOperand())) { |
Chris Lattner | dbba22f | 2005-01-11 23:33:00 +0000 | [diff] [blame] | 1955 | static const unsigned Opc[3] = { |
| 1956 | X86::MOVSX32rm8, X86::MOVSX32rm16, X86::MOVSX16rm8 |
| 1957 | }; |
| 1958 | |
| 1959 | X86AddressMode AM; |
| 1960 | EmitFoldedLoad(N.getOperand(0), AM); |
| 1961 | addFullAddress(BuildMI(BB, Opc[SrcIs16+DestIs16*2], 4, Result), AM); |
| 1962 | return Result; |
| 1963 | } |
| 1964 | |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 1965 | static const unsigned Opc[3] = { |
| 1966 | X86::MOVSX32rr8, X86::MOVSX32rr16, X86::MOVSX16rr8 |
| 1967 | }; |
| 1968 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 1969 | BuildMI(BB, Opc[SrcIs16+DestIs16*2], 1, Result).addReg(Tmp1); |
| 1970 | return Result; |
| 1971 | } |
| 1972 | case ISD::TRUNCATE: |
Chris Lattner | afce430 | 2005-01-12 02:19:06 +0000 | [diff] [blame] | 1973 | // Fold TRUNCATE (LOAD P) into a smaller load from P. |
Chris Lattner | 477c931 | 2005-01-18 20:05:56 +0000 | [diff] [blame] | 1974 | // FIXME: This should be performed by the DAGCombiner. |
Chris Lattner | 4ff348b | 2005-01-17 06:26:58 +0000 | [diff] [blame] | 1975 | if (isFoldableLoad(N.getOperand(0), SDOperand())) { |
Chris Lattner | afce430 | 2005-01-12 02:19:06 +0000 | [diff] [blame] | 1976 | switch (N.getValueType()) { |
| 1977 | default: assert(0 && "Unknown truncate!"); |
| 1978 | case MVT::i1: |
| 1979 | case MVT::i8: Opc = X86::MOV8rm; break; |
| 1980 | case MVT::i16: Opc = X86::MOV16rm; break; |
| 1981 | } |
| 1982 | X86AddressMode AM; |
| 1983 | EmitFoldedLoad(N.getOperand(0), AM); |
| 1984 | addFullAddress(BuildMI(BB, Opc, 4, Result), AM); |
| 1985 | return Result; |
| 1986 | } |
| 1987 | |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 1988 | // Handle cast of LARGER int to SMALLER int using a move to EAX followed by |
| 1989 | // a move out of AX or AL. |
| 1990 | switch (N.getOperand(0).getValueType()) { |
| 1991 | default: assert(0 && "Unknown truncate!"); |
| 1992 | case MVT::i8: Tmp2 = X86::AL; Opc = X86::MOV8rr; break; |
| 1993 | case MVT::i16: Tmp2 = X86::AX; Opc = X86::MOV16rr; break; |
| 1994 | case MVT::i32: Tmp2 = X86::EAX; Opc = X86::MOV32rr; break; |
| 1995 | } |
| 1996 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 1997 | BuildMI(BB, Opc, 1, Tmp2).addReg(Tmp1); |
| 1998 | |
| 1999 | switch (N.getValueType()) { |
| 2000 | default: assert(0 && "Unknown truncate!"); |
| 2001 | case MVT::i1: |
| 2002 | case MVT::i8: Tmp2 = X86::AL; Opc = X86::MOV8rr; break; |
| 2003 | case MVT::i16: Tmp2 = X86::AX; Opc = X86::MOV16rr; break; |
| 2004 | } |
| 2005 | BuildMI(BB, Opc, 1, Result).addReg(Tmp2); |
| 2006 | return Result; |
| 2007 | |
Chris Lattner | 590d800 | 2005-01-09 18:52:44 +0000 | [diff] [blame] | 2008 | case ISD::SINT_TO_FP: |
| 2009 | case ISD::UINT_TO_FP: { |
| 2010 | // FIXME: Most of this grunt work should be done by legalize! |
Chris Lattner | ef7ba07 | 2005-01-11 03:50:45 +0000 | [diff] [blame] | 2011 | ContainsFPCode = true; |
Chris Lattner | 590d800 | 2005-01-09 18:52:44 +0000 | [diff] [blame] | 2012 | |
| 2013 | // Promote the integer to a type supported by FLD. We do this because there |
| 2014 | // are no unsigned FLD instructions, so we must promote an unsigned value to |
| 2015 | // a larger signed value, then use FLD on the larger value. |
| 2016 | // |
| 2017 | MVT::ValueType PromoteType = MVT::Other; |
| 2018 | MVT::ValueType SrcTy = N.getOperand(0).getValueType(); |
| 2019 | unsigned PromoteOpcode = 0; |
| 2020 | unsigned RealDestReg = Result; |
| 2021 | switch (SrcTy) { |
| 2022 | case MVT::i1: |
| 2023 | case MVT::i8: |
| 2024 | // We don't have the facilities for directly loading byte sized data from |
| 2025 | // memory (even signed). Promote it to 16 bits. |
| 2026 | PromoteType = MVT::i16; |
| 2027 | PromoteOpcode = Node->getOpcode() == ISD::SINT_TO_FP ? |
| 2028 | X86::MOVSX16rr8 : X86::MOVZX16rr8; |
| 2029 | break; |
| 2030 | case MVT::i16: |
| 2031 | if (Node->getOpcode() == ISD::UINT_TO_FP) { |
| 2032 | PromoteType = MVT::i32; |
| 2033 | PromoteOpcode = X86::MOVZX32rr16; |
| 2034 | } |
| 2035 | break; |
| 2036 | default: |
| 2037 | // Don't fild into the real destination. |
| 2038 | if (Node->getOpcode() == ISD::UINT_TO_FP) |
| 2039 | Result = MakeReg(Node->getValueType(0)); |
| 2040 | break; |
| 2041 | } |
| 2042 | |
| 2043 | Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register |
Misha Brukman | 0e0a7a45 | 2005-04-21 23:38:14 +0000 | [diff] [blame] | 2044 | |
Chris Lattner | 590d800 | 2005-01-09 18:52:44 +0000 | [diff] [blame] | 2045 | if (PromoteType != MVT::Other) { |
| 2046 | Tmp2 = MakeReg(PromoteType); |
| 2047 | BuildMI(BB, PromoteOpcode, 1, Tmp2).addReg(Tmp1); |
| 2048 | SrcTy = PromoteType; |
| 2049 | Tmp1 = Tmp2; |
| 2050 | } |
| 2051 | |
| 2052 | // Spill the integer to memory and reload it from there. |
| 2053 | unsigned Size = MVT::getSizeInBits(SrcTy)/8; |
| 2054 | MachineFunction *F = BB->getParent(); |
| 2055 | int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, Size); |
| 2056 | |
| 2057 | switch (SrcTy) { |
Chris Lattner | 590d800 | 2005-01-09 18:52:44 +0000 | [diff] [blame] | 2058 | case MVT::i32: |
| 2059 | addFrameReference(BuildMI(BB, X86::MOV32mr, 5), |
| 2060 | FrameIdx).addReg(Tmp1); |
| 2061 | addFrameReference(BuildMI(BB, X86::FILD32m, 5, Result), FrameIdx); |
| 2062 | break; |
| 2063 | case MVT::i16: |
| 2064 | addFrameReference(BuildMI(BB, X86::MOV16mr, 5), |
| 2065 | FrameIdx).addReg(Tmp1); |
| 2066 | addFrameReference(BuildMI(BB, X86::FILD16m, 5, Result), FrameIdx); |
| 2067 | break; |
| 2068 | default: break; // No promotion required. |
| 2069 | } |
| 2070 | |
Chris Lattner | 085c995 | 2005-01-12 04:00:00 +0000 | [diff] [blame] | 2071 | if (Node->getOpcode() == ISD::UINT_TO_FP && Result != RealDestReg) { |
Chris Lattner | 590d800 | 2005-01-09 18:52:44 +0000 | [diff] [blame] | 2072 | // If this is a cast from uint -> double, we need to be careful when if |
| 2073 | // the "sign" bit is set. If so, we don't want to make a negative number, |
| 2074 | // we want to make a positive number. Emit code to add an offset if the |
| 2075 | // sign bit is set. |
| 2076 | |
| 2077 | // Compute whether the sign bit is set by shifting the reg right 31 bits. |
| 2078 | unsigned IsNeg = MakeReg(MVT::i32); |
| 2079 | BuildMI(BB, X86::SHR32ri, 2, IsNeg).addReg(Tmp1).addImm(31); |
| 2080 | |
| 2081 | // Create a CP value that has the offset in one word and 0 in the other. |
| 2082 | static ConstantInt *TheOffset = ConstantUInt::get(Type::ULongTy, |
| 2083 | 0x4f80000000000000ULL); |
| 2084 | unsigned CPI = F->getConstantPool()->getConstantPoolIndex(TheOffset); |
| 2085 | BuildMI(BB, X86::FADD32m, 5, RealDestReg).addReg(Result) |
| 2086 | .addConstantPoolIndex(CPI).addZImm(4).addReg(IsNeg).addSImm(0); |
Chris Lattner | 590d800 | 2005-01-09 18:52:44 +0000 | [diff] [blame] | 2087 | } |
| 2088 | return RealDestReg; |
| 2089 | } |
| 2090 | case ISD::FP_TO_SINT: |
| 2091 | case ISD::FP_TO_UINT: { |
| 2092 | // FIXME: Most of this grunt work should be done by legalize! |
| 2093 | Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register |
| 2094 | |
| 2095 | // Change the floating point control register to use "round towards zero" |
| 2096 | // mode when truncating to an integer value. |
| 2097 | // |
| 2098 | MachineFunction *F = BB->getParent(); |
| 2099 | int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2); |
| 2100 | addFrameReference(BuildMI(BB, X86::FNSTCW16m, 4), CWFrameIdx); |
| 2101 | |
| 2102 | // Load the old value of the high byte of the control word... |
| 2103 | unsigned HighPartOfCW = MakeReg(MVT::i8); |
| 2104 | addFrameReference(BuildMI(BB, X86::MOV8rm, 4, HighPartOfCW), |
| 2105 | CWFrameIdx, 1); |
| 2106 | |
| 2107 | // Set the high part to be round to zero... |
| 2108 | addFrameReference(BuildMI(BB, X86::MOV8mi, 5), |
| 2109 | CWFrameIdx, 1).addImm(12); |
| 2110 | |
| 2111 | // Reload the modified control word now... |
| 2112 | addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx); |
Misha Brukman | 0e0a7a45 | 2005-04-21 23:38:14 +0000 | [diff] [blame] | 2113 | |
Chris Lattner | 590d800 | 2005-01-09 18:52:44 +0000 | [diff] [blame] | 2114 | // Restore the memory image of control word to original value |
| 2115 | addFrameReference(BuildMI(BB, X86::MOV8mr, 5), |
| 2116 | CWFrameIdx, 1).addReg(HighPartOfCW); |
| 2117 | |
| 2118 | // We don't have the facilities for directly storing byte sized data to |
| 2119 | // memory. Promote it to 16 bits. We also must promote unsigned values to |
| 2120 | // larger classes because we only have signed FP stores. |
| 2121 | MVT::ValueType StoreClass = Node->getValueType(0); |
| 2122 | if (StoreClass == MVT::i8 || Node->getOpcode() == ISD::FP_TO_UINT) |
| 2123 | switch (StoreClass) { |
Chris Lattner | 2afa191 | 2005-05-09 05:33:18 +0000 | [diff] [blame] | 2124 | case MVT::i1: |
Chris Lattner | 590d800 | 2005-01-09 18:52:44 +0000 | [diff] [blame] | 2125 | case MVT::i8: StoreClass = MVT::i16; break; |
| 2126 | case MVT::i16: StoreClass = MVT::i32; break; |
| 2127 | case MVT::i32: StoreClass = MVT::i64; break; |
Chris Lattner | 590d800 | 2005-01-09 18:52:44 +0000 | [diff] [blame] | 2128 | default: assert(0 && "Unknown store class!"); |
| 2129 | } |
| 2130 | |
| 2131 | // Spill the integer to memory and reload it from there. |
| 2132 | unsigned Size = MVT::getSizeInBits(StoreClass)/8; |
| 2133 | int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, Size); |
| 2134 | |
| 2135 | switch (StoreClass) { |
| 2136 | default: assert(0 && "Unknown store class!"); |
| 2137 | case MVT::i16: |
| 2138 | addFrameReference(BuildMI(BB, X86::FIST16m, 5), FrameIdx).addReg(Tmp1); |
| 2139 | break; |
| 2140 | case MVT::i32: |
Chris Lattner | 2502085 | 2005-01-09 19:49:59 +0000 | [diff] [blame] | 2141 | addFrameReference(BuildMI(BB, X86::FIST32m, 5), FrameIdx).addReg(Tmp1); |
Chris Lattner | 590d800 | 2005-01-09 18:52:44 +0000 | [diff] [blame] | 2142 | break; |
Chris Lattner | a0dbf18 | 2005-05-09 18:37:02 +0000 | [diff] [blame] | 2143 | case MVT::i64: |
| 2144 | addFrameReference(BuildMI(BB, X86::FISTP64m, 5), FrameIdx).addReg(Tmp1); |
| 2145 | break; } |
Chris Lattner | 590d800 | 2005-01-09 18:52:44 +0000 | [diff] [blame] | 2146 | |
| 2147 | switch (Node->getValueType(0)) { |
| 2148 | default: |
| 2149 | assert(0 && "Unknown integer type!"); |
Chris Lattner | 590d800 | 2005-01-09 18:52:44 +0000 | [diff] [blame] | 2150 | case MVT::i32: |
| 2151 | addFrameReference(BuildMI(BB, X86::MOV32rm, 4, Result), FrameIdx); |
| 2152 | break; |
| 2153 | case MVT::i16: |
| 2154 | addFrameReference(BuildMI(BB, X86::MOV16rm, 4, Result), FrameIdx); |
| 2155 | break; |
| 2156 | case MVT::i8: |
Chris Lattner | 2afa191 | 2005-05-09 05:33:18 +0000 | [diff] [blame] | 2157 | case MVT::i1: |
Chris Lattner | 590d800 | 2005-01-09 18:52:44 +0000 | [diff] [blame] | 2158 | addFrameReference(BuildMI(BB, X86::MOV8rm, 4, Result), FrameIdx); |
| 2159 | break; |
| 2160 | } |
| 2161 | |
| 2162 | // Reload the original control word now. |
| 2163 | addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx); |
| 2164 | return Result; |
| 2165 | } |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 2166 | case ISD::ADD: |
Chris Lattner | a5ade06 | 2005-01-11 21:19:59 +0000 | [diff] [blame] | 2167 | Op0 = N.getOperand(0); |
| 2168 | Op1 = N.getOperand(1); |
| 2169 | |
Chris Lattner | 44129b5 | 2005-01-25 20:03:11 +0000 | [diff] [blame] | 2170 | if (isFoldableLoad(Op0, Op1, true)) { |
Chris Lattner | a5ade06 | 2005-01-11 21:19:59 +0000 | [diff] [blame] | 2171 | std::swap(Op0, Op1); |
Chris Lattner | 4ff348b | 2005-01-17 06:26:58 +0000 | [diff] [blame] | 2172 | goto FoldAdd; |
| 2173 | } |
Chris Lattner | a5ade06 | 2005-01-11 21:19:59 +0000 | [diff] [blame] | 2174 | |
Chris Lattner | 44129b5 | 2005-01-25 20:03:11 +0000 | [diff] [blame] | 2175 | if (isFoldableLoad(Op1, Op0, true)) { |
Chris Lattner | 4ff348b | 2005-01-17 06:26:58 +0000 | [diff] [blame] | 2176 | FoldAdd: |
Chris Lattner | a5ade06 | 2005-01-11 21:19:59 +0000 | [diff] [blame] | 2177 | switch (N.getValueType()) { |
| 2178 | default: assert(0 && "Cannot add this type!"); |
| 2179 | case MVT::i1: |
| 2180 | case MVT::i8: Opc = X86::ADD8rm; break; |
| 2181 | case MVT::i16: Opc = X86::ADD16rm; break; |
| 2182 | case MVT::i32: Opc = X86::ADD32rm; break; |
Chris Lattner | 44129b5 | 2005-01-25 20:03:11 +0000 | [diff] [blame] | 2183 | case MVT::f64: |
| 2184 | // For F64, handle promoted load operations (from F32) as well! |
| 2185 | Opc = Op1.getOpcode() == ISD::LOAD ? X86::FADD64m : X86::FADD32m; |
| 2186 | break; |
Chris Lattner | a5ade06 | 2005-01-11 21:19:59 +0000 | [diff] [blame] | 2187 | } |
| 2188 | X86AddressMode AM; |
Chris Lattner | 636e79a | 2005-01-13 05:53:16 +0000 | [diff] [blame] | 2189 | EmitFoldedLoad(Op1, AM); |
| 2190 | Tmp1 = SelectExpr(Op0); |
Chris Lattner | a5ade06 | 2005-01-11 21:19:59 +0000 | [diff] [blame] | 2191 | addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM); |
| 2192 | return Result; |
| 2193 | } |
| 2194 | |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 2195 | // See if we can codegen this as an LEA to fold operations together. |
| 2196 | if (N.getValueType() == MVT::i32) { |
Chris Lattner | 883c86f | 2005-01-18 02:25:52 +0000 | [diff] [blame] | 2197 | ExprMap.erase(N); |
Chris Lattner | 98a8ba0 | 2005-01-18 01:06:26 +0000 | [diff] [blame] | 2198 | X86ISelAddressMode AM; |
Chris Lattner | 883c86f | 2005-01-18 02:25:52 +0000 | [diff] [blame] | 2199 | MatchAddress(N, AM); |
| 2200 | ExprMap[N] = Result; |
| 2201 | |
| 2202 | // If this is not just an add, emit the LEA. For a simple add (like |
| 2203 | // reg+reg or reg+imm), we just emit an add. It might be a good idea to |
| 2204 | // leave this as LEA, then peephole it to 'ADD' after two address elim |
| 2205 | // happens. |
| 2206 | if (AM.Scale != 1 || AM.BaseType == X86ISelAddressMode::FrameIndexBase|| |
| 2207 | AM.GV || (AM.Base.Reg.Val && AM.IndexReg.Val && AM.Disp)) { |
| 2208 | X86AddressMode XAM = SelectAddrExprs(AM); |
| 2209 | addFullAddress(BuildMI(BB, X86::LEA32r, 4, Result), XAM); |
| 2210 | return Result; |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 2211 | } |
| 2212 | } |
Chris Lattner | 1133309 | 2005-01-11 03:11:44 +0000 | [diff] [blame] | 2213 | |
Chris Lattner | a5ade06 | 2005-01-11 21:19:59 +0000 | [diff] [blame] | 2214 | if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1)) { |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 2215 | Opc = 0; |
| 2216 | if (CN->getValue() == 1) { // add X, 1 -> inc X |
| 2217 | switch (N.getValueType()) { |
| 2218 | default: assert(0 && "Cannot integer add this type!"); |
| 2219 | case MVT::i8: Opc = X86::INC8r; break; |
| 2220 | case MVT::i16: Opc = X86::INC16r; break; |
| 2221 | case MVT::i32: Opc = X86::INC32r; break; |
| 2222 | } |
| 2223 | } else if (CN->isAllOnesValue()) { // add X, -1 -> dec X |
| 2224 | switch (N.getValueType()) { |
| 2225 | default: assert(0 && "Cannot integer add this type!"); |
| 2226 | case MVT::i8: Opc = X86::DEC8r; break; |
| 2227 | case MVT::i16: Opc = X86::DEC16r; break; |
| 2228 | case MVT::i32: Opc = X86::DEC32r; break; |
| 2229 | } |
| 2230 | } |
| 2231 | |
| 2232 | if (Opc) { |
Chris Lattner | a5ade06 | 2005-01-11 21:19:59 +0000 | [diff] [blame] | 2233 | Tmp1 = SelectExpr(Op0); |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 2234 | BuildMI(BB, Opc, 1, Result).addReg(Tmp1); |
| 2235 | return Result; |
| 2236 | } |
| 2237 | |
| 2238 | switch (N.getValueType()) { |
| 2239 | default: assert(0 && "Cannot add this type!"); |
| 2240 | case MVT::i8: Opc = X86::ADD8ri; break; |
| 2241 | case MVT::i16: Opc = X86::ADD16ri; break; |
| 2242 | case MVT::i32: Opc = X86::ADD32ri; break; |
| 2243 | } |
| 2244 | if (Opc) { |
Chris Lattner | a5ade06 | 2005-01-11 21:19:59 +0000 | [diff] [blame] | 2245 | Tmp1 = SelectExpr(Op0); |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 2246 | BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue()); |
| 2247 | return Result; |
| 2248 | } |
| 2249 | } |
| 2250 | |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 2251 | switch (N.getValueType()) { |
| 2252 | default: assert(0 && "Cannot add this type!"); |
| 2253 | case MVT::i8: Opc = X86::ADD8rr; break; |
| 2254 | case MVT::i16: Opc = X86::ADD16rr; break; |
| 2255 | case MVT::i32: Opc = X86::ADD32rr; break; |
Chris Lattner | ef7ba07 | 2005-01-11 03:50:45 +0000 | [diff] [blame] | 2256 | case MVT::f64: Opc = X86::FpADD; break; |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 2257 | } |
Chris Lattner | 1133309 | 2005-01-11 03:11:44 +0000 | [diff] [blame] | 2258 | |
Chris Lattner | a5ade06 | 2005-01-11 21:19:59 +0000 | [diff] [blame] | 2259 | if (getRegPressure(Op0) > getRegPressure(Op1)) { |
| 2260 | Tmp1 = SelectExpr(Op0); |
| 2261 | Tmp2 = SelectExpr(Op1); |
Chris Lattner | 1133309 | 2005-01-11 03:11:44 +0000 | [diff] [blame] | 2262 | } else { |
Chris Lattner | a5ade06 | 2005-01-11 21:19:59 +0000 | [diff] [blame] | 2263 | Tmp2 = SelectExpr(Op1); |
| 2264 | Tmp1 = SelectExpr(Op0); |
Chris Lattner | 1133309 | 2005-01-11 03:11:44 +0000 | [diff] [blame] | 2265 | } |
| 2266 | |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 2267 | BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2); |
| 2268 | return Result; |
Chris Lattner | b7edaa1 | 2005-04-02 05:30:17 +0000 | [diff] [blame] | 2269 | |
| 2270 | case ISD::FABS: |
Chris Lattner | b7edaa1 | 2005-04-02 05:30:17 +0000 | [diff] [blame] | 2271 | case ISD::FNEG: |
Chris Lattner | c5dcb53 | 2005-04-30 04:25:35 +0000 | [diff] [blame] | 2272 | case ISD::FSIN: |
| 2273 | case ISD::FCOS: |
Chris Lattner | 2c56e8a | 2005-04-28 22:07:18 +0000 | [diff] [blame] | 2274 | case ISD::FSQRT: |
| 2275 | assert(N.getValueType()==MVT::f64 && "Illegal type for this operation"); |
Chris Lattner | b7edaa1 | 2005-04-02 05:30:17 +0000 | [diff] [blame] | 2276 | Tmp1 = SelectExpr(Node->getOperand(0)); |
Chris Lattner | 2c56e8a | 2005-04-28 22:07:18 +0000 | [diff] [blame] | 2277 | switch (N.getOpcode()) { |
| 2278 | default: assert(0 && "Unreachable!"); |
| 2279 | case ISD::FABS: BuildMI(BB, X86::FABS, 1, Result).addReg(Tmp1); break; |
| 2280 | case ISD::FNEG: BuildMI(BB, X86::FCHS, 1, Result).addReg(Tmp1); break; |
| 2281 | case ISD::FSQRT: BuildMI(BB, X86::FSQRT, 1, Result).addReg(Tmp1); break; |
Chris Lattner | c5dcb53 | 2005-04-30 04:25:35 +0000 | [diff] [blame] | 2282 | case ISD::FSIN: BuildMI(BB, X86::FSIN, 1, Result).addReg(Tmp1); break; |
| 2283 | case ISD::FCOS: BuildMI(BB, X86::FCOS, 1, Result).addReg(Tmp1); break; |
Chris Lattner | 2c56e8a | 2005-04-28 22:07:18 +0000 | [diff] [blame] | 2284 | } |
Chris Lattner | b7edaa1 | 2005-04-02 05:30:17 +0000 | [diff] [blame] | 2285 | return Result; |
| 2286 | |
Chris Lattner | 8db0af1 | 2005-04-06 04:21:07 +0000 | [diff] [blame] | 2287 | case ISD::MULHU: |
| 2288 | switch (N.getValueType()) { |
| 2289 | default: assert(0 && "Unsupported VT!"); |
| 2290 | case MVT::i8: Tmp2 = X86::MUL8r; break; |
| 2291 | case MVT::i16: Tmp2 = X86::MUL16r; break; |
| 2292 | case MVT::i32: Tmp2 = X86::MUL32r; break; |
| 2293 | } |
| 2294 | // FALL THROUGH |
| 2295 | case ISD::MULHS: { |
| 2296 | unsigned MovOpc, LowReg, HiReg; |
| 2297 | switch (N.getValueType()) { |
| 2298 | default: assert(0 && "Unsupported VT!"); |
Misha Brukman | 0e0a7a45 | 2005-04-21 23:38:14 +0000 | [diff] [blame] | 2299 | case MVT::i8: |
Chris Lattner | 8db0af1 | 2005-04-06 04:21:07 +0000 | [diff] [blame] | 2300 | MovOpc = X86::MOV8rr; |
| 2301 | LowReg = X86::AL; |
| 2302 | HiReg = X86::AH; |
| 2303 | Opc = X86::IMUL8r; |
| 2304 | break; |
| 2305 | case MVT::i16: |
| 2306 | MovOpc = X86::MOV16rr; |
| 2307 | LowReg = X86::AX; |
| 2308 | HiReg = X86::DX; |
| 2309 | Opc = X86::IMUL16r; |
| 2310 | break; |
| 2311 | case MVT::i32: |
| 2312 | MovOpc = X86::MOV32rr; |
| 2313 | LowReg = X86::EAX; |
| 2314 | HiReg = X86::EDX; |
| 2315 | Opc = X86::IMUL32r; |
| 2316 | break; |
| 2317 | } |
| 2318 | if (Node->getOpcode() != ISD::MULHS) |
| 2319 | Opc = Tmp2; // Get the MULHU opcode. |
| 2320 | |
| 2321 | Op0 = Node->getOperand(0); |
| 2322 | Op1 = Node->getOperand(1); |
| 2323 | if (getRegPressure(Op0) > getRegPressure(Op1)) { |
| 2324 | Tmp1 = SelectExpr(Op0); |
| 2325 | Tmp2 = SelectExpr(Op1); |
| 2326 | } else { |
| 2327 | Tmp2 = SelectExpr(Op1); |
| 2328 | Tmp1 = SelectExpr(Op0); |
| 2329 | } |
| 2330 | |
| 2331 | // FIXME: Implement folding of loads into the memory operands here! |
| 2332 | BuildMI(BB, MovOpc, 1, LowReg).addReg(Tmp1); |
| 2333 | BuildMI(BB, Opc, 1).addReg(Tmp2); |
| 2334 | BuildMI(BB, MovOpc, 1, Result).addReg(HiReg); |
| 2335 | return Result; |
Misha Brukman | 0e0a7a45 | 2005-04-21 23:38:14 +0000 | [diff] [blame] | 2336 | } |
Chris Lattner | 8db0af1 | 2005-04-06 04:21:07 +0000 | [diff] [blame] | 2337 | |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 2338 | case ISD::SUB: |
Chris Lattner | a5ade06 | 2005-01-11 21:19:59 +0000 | [diff] [blame] | 2339 | case ISD::MUL: |
| 2340 | case ISD::AND: |
| 2341 | case ISD::OR: |
Chris Lattner | a56cea4 | 2005-01-12 04:23:22 +0000 | [diff] [blame] | 2342 | case ISD::XOR: { |
Chris Lattner | a5ade06 | 2005-01-11 21:19:59 +0000 | [diff] [blame] | 2343 | static const unsigned SUBTab[] = { |
| 2344 | X86::SUB8ri, X86::SUB16ri, X86::SUB32ri, 0, 0, |
| 2345 | X86::SUB8rm, X86::SUB16rm, X86::SUB32rm, X86::FSUB32m, X86::FSUB64m, |
| 2346 | X86::SUB8rr, X86::SUB16rr, X86::SUB32rr, X86::FpSUB , X86::FpSUB, |
| 2347 | }; |
| 2348 | static const unsigned MULTab[] = { |
| 2349 | 0, X86::IMUL16rri, X86::IMUL32rri, 0, 0, |
| 2350 | 0, X86::IMUL16rm , X86::IMUL32rm, X86::FMUL32m, X86::FMUL64m, |
| 2351 | 0, X86::IMUL16rr , X86::IMUL32rr, X86::FpMUL , X86::FpMUL, |
| 2352 | }; |
| 2353 | static const unsigned ANDTab[] = { |
| 2354 | X86::AND8ri, X86::AND16ri, X86::AND32ri, 0, 0, |
| 2355 | X86::AND8rm, X86::AND16rm, X86::AND32rm, 0, 0, |
Misha Brukman | 0e0a7a45 | 2005-04-21 23:38:14 +0000 | [diff] [blame] | 2356 | X86::AND8rr, X86::AND16rr, X86::AND32rr, 0, 0, |
Chris Lattner | a5ade06 | 2005-01-11 21:19:59 +0000 | [diff] [blame] | 2357 | }; |
| 2358 | static const unsigned ORTab[] = { |
| 2359 | X86::OR8ri, X86::OR16ri, X86::OR32ri, 0, 0, |
| 2360 | X86::OR8rm, X86::OR16rm, X86::OR32rm, 0, 0, |
| 2361 | X86::OR8rr, X86::OR16rr, X86::OR32rr, 0, 0, |
| 2362 | }; |
| 2363 | static const unsigned XORTab[] = { |
| 2364 | X86::XOR8ri, X86::XOR16ri, X86::XOR32ri, 0, 0, |
| 2365 | X86::XOR8rm, X86::XOR16rm, X86::XOR32rm, 0, 0, |
| 2366 | X86::XOR8rr, X86::XOR16rr, X86::XOR32rr, 0, 0, |
| 2367 | }; |
| 2368 | |
| 2369 | Op0 = Node->getOperand(0); |
| 2370 | Op1 = Node->getOperand(1); |
| 2371 | |
Chris Lattner | 30ea1e9 | 2005-01-19 07:37:26 +0000 | [diff] [blame] | 2372 | if (Node->getOpcode() == ISD::OR && Op0.hasOneUse() && Op1.hasOneUse()) |
| 2373 | if (EmitOrOpOp(Op0, Op1, Result)) // Match SHLD, SHRD, and rotates. |
Chris Lattner | 8571637 | 2005-01-19 06:18:43 +0000 | [diff] [blame] | 2374 | return Result; |
| 2375 | |
| 2376 | if (Node->getOpcode() == ISD::SUB) |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 2377 | if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(0))) |
| 2378 | if (CN->isNullValue()) { // 0 - N -> neg N |
| 2379 | switch (N.getValueType()) { |
| 2380 | default: assert(0 && "Cannot sub this type!"); |
| 2381 | case MVT::i1: |
| 2382 | case MVT::i8: Opc = X86::NEG8r; break; |
| 2383 | case MVT::i16: Opc = X86::NEG16r; break; |
| 2384 | case MVT::i32: Opc = X86::NEG32r; break; |
| 2385 | } |
| 2386 | Tmp1 = SelectExpr(N.getOperand(1)); |
| 2387 | BuildMI(BB, Opc, 1, Result).addReg(Tmp1); |
| 2388 | return Result; |
| 2389 | } |
| 2390 | |
Chris Lattner | a5ade06 | 2005-01-11 21:19:59 +0000 | [diff] [blame] | 2391 | if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1)) { |
| 2392 | if (CN->isAllOnesValue() && Node->getOpcode() == ISD::XOR) { |
Chris Lattner | c98279d | 2005-01-17 00:23:16 +0000 | [diff] [blame] | 2393 | Opc = 0; |
Chris Lattner | d4dab92 | 2005-01-11 04:31:30 +0000 | [diff] [blame] | 2394 | switch (N.getValueType()) { |
| 2395 | default: assert(0 && "Cannot add this type!"); |
Chris Lattner | c98279d | 2005-01-17 00:23:16 +0000 | [diff] [blame] | 2396 | case MVT::i1: break; // Not supported, don't invert upper bits! |
Chris Lattner | d4dab92 | 2005-01-11 04:31:30 +0000 | [diff] [blame] | 2397 | case MVT::i8: Opc = X86::NOT8r; break; |
| 2398 | case MVT::i16: Opc = X86::NOT16r; break; |
| 2399 | case MVT::i32: Opc = X86::NOT32r; break; |
| 2400 | } |
Chris Lattner | c98279d | 2005-01-17 00:23:16 +0000 | [diff] [blame] | 2401 | if (Opc) { |
| 2402 | Tmp1 = SelectExpr(Op0); |
| 2403 | BuildMI(BB, Opc, 1, Result).addReg(Tmp1); |
| 2404 | return Result; |
| 2405 | } |
Chris Lattner | d4dab92 | 2005-01-11 04:31:30 +0000 | [diff] [blame] | 2406 | } |
| 2407 | |
Chris Lattner | 2a4e508 | 2005-01-17 06:48:02 +0000 | [diff] [blame] | 2408 | // Fold common multiplies into LEA instructions. |
| 2409 | if (Node->getOpcode() == ISD::MUL && N.getValueType() == MVT::i32) { |
| 2410 | switch ((int)CN->getValue()) { |
| 2411 | default: break; |
| 2412 | case 3: |
| 2413 | case 5: |
| 2414 | case 9: |
Chris Lattner | 2a4e508 | 2005-01-17 06:48:02 +0000 | [diff] [blame] | 2415 | // Remove N from exprmap so SelectAddress doesn't get confused. |
| 2416 | ExprMap.erase(N); |
Chris Lattner | 98a8ba0 | 2005-01-18 01:06:26 +0000 | [diff] [blame] | 2417 | X86AddressMode AM; |
Chris Lattner | 2a4e508 | 2005-01-17 06:48:02 +0000 | [diff] [blame] | 2418 | SelectAddress(N, AM); |
| 2419 | // Restore it to the map. |
| 2420 | ExprMap[N] = Result; |
| 2421 | addFullAddress(BuildMI(BB, X86::LEA32r, 4, Result), AM); |
| 2422 | return Result; |
| 2423 | } |
| 2424 | } |
| 2425 | |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 2426 | switch (N.getValueType()) { |
Chris Lattner | d4dab92 | 2005-01-11 04:31:30 +0000 | [diff] [blame] | 2427 | default: assert(0 && "Cannot xor this type!"); |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 2428 | case MVT::i1: |
Chris Lattner | a5ade06 | 2005-01-11 21:19:59 +0000 | [diff] [blame] | 2429 | case MVT::i8: Opc = 0; break; |
| 2430 | case MVT::i16: Opc = 1; break; |
| 2431 | case MVT::i32: Opc = 2; break; |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 2432 | } |
Chris Lattner | a5ade06 | 2005-01-11 21:19:59 +0000 | [diff] [blame] | 2433 | switch (Node->getOpcode()) { |
| 2434 | default: assert(0 && "Unreachable!"); |
| 2435 | case ISD::SUB: Opc = SUBTab[Opc]; break; |
| 2436 | case ISD::MUL: Opc = MULTab[Opc]; break; |
| 2437 | case ISD::AND: Opc = ANDTab[Opc]; break; |
| 2438 | case ISD::OR: Opc = ORTab[Opc]; break; |
| 2439 | case ISD::XOR: Opc = XORTab[Opc]; break; |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 2440 | } |
Chris Lattner | a5ade06 | 2005-01-11 21:19:59 +0000 | [diff] [blame] | 2441 | if (Opc) { // Can't fold MUL:i8 R, imm |
| 2442 | Tmp1 = SelectExpr(Op0); |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 2443 | BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue()); |
| 2444 | return Result; |
| 2445 | } |
| 2446 | } |
Chris Lattner | 1133309 | 2005-01-11 03:11:44 +0000 | [diff] [blame] | 2447 | |
Chris Lattner | 44129b5 | 2005-01-25 20:03:11 +0000 | [diff] [blame] | 2448 | if (isFoldableLoad(Op0, Op1, true)) |
Chris Lattner | a5ade06 | 2005-01-11 21:19:59 +0000 | [diff] [blame] | 2449 | if (Node->getOpcode() != ISD::SUB) { |
| 2450 | std::swap(Op0, Op1); |
Chris Lattner | 4ff348b | 2005-01-17 06:26:58 +0000 | [diff] [blame] | 2451 | goto FoldOps; |
Chris Lattner | a5ade06 | 2005-01-11 21:19:59 +0000 | [diff] [blame] | 2452 | } else { |
Chris Lattner | 44129b5 | 2005-01-25 20:03:11 +0000 | [diff] [blame] | 2453 | // For FP, emit 'reverse' subract, with a memory operand. |
| 2454 | if (N.getValueType() == MVT::f64) { |
| 2455 | if (Op0.getOpcode() == ISD::EXTLOAD) |
| 2456 | Opc = X86::FSUBR32m; |
| 2457 | else |
| 2458 | Opc = X86::FSUBR64m; |
| 2459 | |
Chris Lattner | a5ade06 | 2005-01-11 21:19:59 +0000 | [diff] [blame] | 2460 | X86AddressMode AM; |
Chris Lattner | 636e79a | 2005-01-13 05:53:16 +0000 | [diff] [blame] | 2461 | EmitFoldedLoad(Op0, AM); |
| 2462 | Tmp1 = SelectExpr(Op1); |
Chris Lattner | a5ade06 | 2005-01-11 21:19:59 +0000 | [diff] [blame] | 2463 | addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM); |
| 2464 | return Result; |
| 2465 | } |
| 2466 | } |
| 2467 | |
Chris Lattner | 44129b5 | 2005-01-25 20:03:11 +0000 | [diff] [blame] | 2468 | if (isFoldableLoad(Op1, Op0, true)) { |
Chris Lattner | 4ff348b | 2005-01-17 06:26:58 +0000 | [diff] [blame] | 2469 | FoldOps: |
Chris Lattner | a5ade06 | 2005-01-11 21:19:59 +0000 | [diff] [blame] | 2470 | switch (N.getValueType()) { |
| 2471 | default: assert(0 && "Cannot operate on this type!"); |
| 2472 | case MVT::i1: |
| 2473 | case MVT::i8: Opc = 5; break; |
| 2474 | case MVT::i16: Opc = 6; break; |
| 2475 | case MVT::i32: Opc = 7; break; |
Chris Lattner | 44129b5 | 2005-01-25 20:03:11 +0000 | [diff] [blame] | 2476 | // For F64, handle promoted load operations (from F32) as well! |
| 2477 | case MVT::f64: Opc = Op1.getOpcode() == ISD::LOAD ? 9 : 8; break; |
Chris Lattner | a5ade06 | 2005-01-11 21:19:59 +0000 | [diff] [blame] | 2478 | } |
| 2479 | switch (Node->getOpcode()) { |
| 2480 | default: assert(0 && "Unreachable!"); |
| 2481 | case ISD::SUB: Opc = SUBTab[Opc]; break; |
| 2482 | case ISD::MUL: Opc = MULTab[Opc]; break; |
| 2483 | case ISD::AND: Opc = ANDTab[Opc]; break; |
| 2484 | case ISD::OR: Opc = ORTab[Opc]; break; |
| 2485 | case ISD::XOR: Opc = XORTab[Opc]; break; |
| 2486 | } |
| 2487 | |
| 2488 | X86AddressMode AM; |
Chris Lattner | 636e79a | 2005-01-13 05:53:16 +0000 | [diff] [blame] | 2489 | EmitFoldedLoad(Op1, AM); |
| 2490 | Tmp1 = SelectExpr(Op0); |
Chris Lattner | a5ade06 | 2005-01-11 21:19:59 +0000 | [diff] [blame] | 2491 | if (Opc) { |
| 2492 | addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM); |
| 2493 | } else { |
| 2494 | assert(Node->getOpcode() == ISD::MUL && |
| 2495 | N.getValueType() == MVT::i8 && "Unexpected situation!"); |
| 2496 | // Must use the MUL instruction, which forces use of AL. |
| 2497 | BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(Tmp1); |
| 2498 | addFullAddress(BuildMI(BB, X86::MUL8m, 1), AM); |
| 2499 | BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL); |
| 2500 | } |
| 2501 | return Result; |
Chris Lattner | 1133309 | 2005-01-11 03:11:44 +0000 | [diff] [blame] | 2502 | } |
Chris Lattner | a5ade06 | 2005-01-11 21:19:59 +0000 | [diff] [blame] | 2503 | |
| 2504 | if (getRegPressure(Op0) > getRegPressure(Op1)) { |
| 2505 | Tmp1 = SelectExpr(Op0); |
| 2506 | Tmp2 = SelectExpr(Op1); |
| 2507 | } else { |
| 2508 | Tmp2 = SelectExpr(Op1); |
| 2509 | Tmp1 = SelectExpr(Op0); |
| 2510 | } |
| 2511 | |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 2512 | switch (N.getValueType()) { |
| 2513 | default: assert(0 && "Cannot add this type!"); |
Chris Lattner | a5ade06 | 2005-01-11 21:19:59 +0000 | [diff] [blame] | 2514 | case MVT::i1: |
| 2515 | case MVT::i8: Opc = 10; break; |
| 2516 | case MVT::i16: Opc = 11; break; |
| 2517 | case MVT::i32: Opc = 12; break; |
| 2518 | case MVT::f32: Opc = 13; break; |
| 2519 | case MVT::f64: Opc = 14; break; |
| 2520 | } |
| 2521 | switch (Node->getOpcode()) { |
| 2522 | default: assert(0 && "Unreachable!"); |
| 2523 | case ISD::SUB: Opc = SUBTab[Opc]; break; |
| 2524 | case ISD::MUL: Opc = MULTab[Opc]; break; |
| 2525 | case ISD::AND: Opc = ANDTab[Opc]; break; |
| 2526 | case ISD::OR: Opc = ORTab[Opc]; break; |
| 2527 | case ISD::XOR: Opc = XORTab[Opc]; break; |
| 2528 | } |
| 2529 | if (Opc) { |
| 2530 | BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2); |
| 2531 | } else { |
| 2532 | assert(Node->getOpcode() == ISD::MUL && |
| 2533 | N.getValueType() == MVT::i8 && "Unexpected situation!"); |
Chris Lattner | a13d323 | 2005-01-10 20:55:48 +0000 | [diff] [blame] | 2534 | // Must use the MUL instruction, which forces use of AL. |
| 2535 | BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(Tmp1); |
| 2536 | BuildMI(BB, X86::MUL8r, 1).addReg(Tmp2); |
| 2537 | BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL); |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 2538 | } |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 2539 | return Result; |
Chris Lattner | a56cea4 | 2005-01-12 04:23:22 +0000 | [diff] [blame] | 2540 | } |
Chris Lattner | 19ad062 | 2005-01-20 18:53:00 +0000 | [diff] [blame] | 2541 | case ISD::ADD_PARTS: |
| 2542 | case ISD::SUB_PARTS: { |
| 2543 | assert(N.getNumOperands() == 4 && N.getValueType() == MVT::i32 && |
| 2544 | "Not an i64 add/sub!"); |
| 2545 | // Emit all of the operands. |
| 2546 | std::vector<unsigned> InVals; |
| 2547 | for (unsigned i = 0, e = N.getNumOperands(); i != e; ++i) |
| 2548 | InVals.push_back(SelectExpr(N.getOperand(i))); |
| 2549 | if (N.getOpcode() == ISD::ADD_PARTS) { |
| 2550 | BuildMI(BB, X86::ADD32rr, 2, Result).addReg(InVals[0]).addReg(InVals[2]); |
| 2551 | BuildMI(BB, X86::ADC32rr,2,Result+1).addReg(InVals[1]).addReg(InVals[3]); |
| 2552 | } else { |
| 2553 | BuildMI(BB, X86::SUB32rr, 2, Result).addReg(InVals[0]).addReg(InVals[2]); |
| 2554 | BuildMI(BB, X86::SBB32rr, 2,Result+1).addReg(InVals[1]).addReg(InVals[3]); |
| 2555 | } |
| 2556 | return Result+N.ResNo; |
| 2557 | } |
| 2558 | |
Chris Lattner | b38a749 | 2005-04-02 04:01:14 +0000 | [diff] [blame] | 2559 | case ISD::SHL_PARTS: |
| 2560 | case ISD::SRA_PARTS: |
| 2561 | case ISD::SRL_PARTS: { |
| 2562 | assert(N.getNumOperands() == 3 && N.getValueType() == MVT::i32 && |
| 2563 | "Not an i64 shift!"); |
| 2564 | unsigned ShiftOpLo = SelectExpr(N.getOperand(0)); |
| 2565 | unsigned ShiftOpHi = SelectExpr(N.getOperand(1)); |
| 2566 | unsigned TmpReg = MakeReg(MVT::i32); |
| 2567 | if (N.getOpcode() == ISD::SRA_PARTS) { |
| 2568 | // If this is a SHR of a Long, then we need to do funny sign extension |
| 2569 | // stuff. TmpReg gets the value to use as the high-part if we are |
| 2570 | // shifting more than 32 bits. |
| 2571 | BuildMI(BB, X86::SAR32ri, 2, TmpReg).addReg(ShiftOpHi).addImm(31); |
| 2572 | } else { |
| 2573 | // Other shifts use a fixed zero value if the shift is more than 32 bits. |
| 2574 | BuildMI(BB, X86::MOV32ri, 1, TmpReg).addImm(0); |
| 2575 | } |
| 2576 | |
| 2577 | // Initialize CL with the shift amount. |
| 2578 | unsigned ShiftAmountReg = SelectExpr(N.getOperand(2)); |
| 2579 | BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShiftAmountReg); |
| 2580 | |
| 2581 | unsigned TmpReg2 = MakeReg(MVT::i32); |
| 2582 | unsigned TmpReg3 = MakeReg(MVT::i32); |
| 2583 | if (N.getOpcode() == ISD::SHL_PARTS) { |
| 2584 | // TmpReg2 = shld inHi, inLo |
| 2585 | BuildMI(BB, X86::SHLD32rrCL, 2,TmpReg2).addReg(ShiftOpHi) |
| 2586 | .addReg(ShiftOpLo); |
| 2587 | // TmpReg3 = shl inLo, CL |
| 2588 | BuildMI(BB, X86::SHL32rCL, 1, TmpReg3).addReg(ShiftOpLo); |
Misha Brukman | 0e0a7a45 | 2005-04-21 23:38:14 +0000 | [diff] [blame] | 2589 | |
Chris Lattner | b38a749 | 2005-04-02 04:01:14 +0000 | [diff] [blame] | 2590 | // Set the flags to indicate whether the shift was by more than 32 bits. |
| 2591 | BuildMI(BB, X86::TEST8ri, 2).addReg(X86::CL).addImm(32); |
Misha Brukman | 0e0a7a45 | 2005-04-21 23:38:14 +0000 | [diff] [blame] | 2592 | |
Chris Lattner | b38a749 | 2005-04-02 04:01:14 +0000 | [diff] [blame] | 2593 | // DestHi = (>32) ? TmpReg3 : TmpReg2; |
Misha Brukman | 0e0a7a45 | 2005-04-21 23:38:14 +0000 | [diff] [blame] | 2594 | BuildMI(BB, X86::CMOVNE32rr, 2, |
Chris Lattner | b38a749 | 2005-04-02 04:01:14 +0000 | [diff] [blame] | 2595 | Result+1).addReg(TmpReg2).addReg(TmpReg3); |
| 2596 | // DestLo = (>32) ? TmpReg : TmpReg3; |
| 2597 | BuildMI(BB, X86::CMOVNE32rr, 2, |
| 2598 | Result).addReg(TmpReg3).addReg(TmpReg); |
| 2599 | } else { |
| 2600 | // TmpReg2 = shrd inLo, inHi |
| 2601 | BuildMI(BB, X86::SHRD32rrCL,2,TmpReg2).addReg(ShiftOpLo) |
| 2602 | .addReg(ShiftOpHi); |
| 2603 | // TmpReg3 = s[ah]r inHi, CL |
Misha Brukman | 0e0a7a45 | 2005-04-21 23:38:14 +0000 | [diff] [blame] | 2604 | BuildMI(BB, N.getOpcode() == ISD::SRA_PARTS ? X86::SAR32rCL |
Chris Lattner | b38a749 | 2005-04-02 04:01:14 +0000 | [diff] [blame] | 2605 | : X86::SHR32rCL, 1, TmpReg3) |
| 2606 | .addReg(ShiftOpHi); |
Misha Brukman | 0e0a7a45 | 2005-04-21 23:38:14 +0000 | [diff] [blame] | 2607 | |
Chris Lattner | b38a749 | 2005-04-02 04:01:14 +0000 | [diff] [blame] | 2608 | // Set the flags to indicate whether the shift was by more than 32 bits. |
| 2609 | BuildMI(BB, X86::TEST8ri, 2).addReg(X86::CL).addImm(32); |
Misha Brukman | 0e0a7a45 | 2005-04-21 23:38:14 +0000 | [diff] [blame] | 2610 | |
Chris Lattner | b38a749 | 2005-04-02 04:01:14 +0000 | [diff] [blame] | 2611 | // DestLo = (>32) ? TmpReg3 : TmpReg2; |
Misha Brukman | 0e0a7a45 | 2005-04-21 23:38:14 +0000 | [diff] [blame] | 2612 | BuildMI(BB, X86::CMOVNE32rr, 2, |
Chris Lattner | b38a749 | 2005-04-02 04:01:14 +0000 | [diff] [blame] | 2613 | Result).addReg(TmpReg2).addReg(TmpReg3); |
Misha Brukman | 0e0a7a45 | 2005-04-21 23:38:14 +0000 | [diff] [blame] | 2614 | |
Chris Lattner | b38a749 | 2005-04-02 04:01:14 +0000 | [diff] [blame] | 2615 | // DestHi = (>32) ? TmpReg : TmpReg3; |
Misha Brukman | 0e0a7a45 | 2005-04-21 23:38:14 +0000 | [diff] [blame] | 2616 | BuildMI(BB, X86::CMOVNE32rr, 2, |
Chris Lattner | b38a749 | 2005-04-02 04:01:14 +0000 | [diff] [blame] | 2617 | Result+1).addReg(TmpReg3).addReg(TmpReg); |
| 2618 | } |
| 2619 | return Result+N.ResNo; |
| 2620 | } |
| 2621 | |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 2622 | case ISD::SELECT: |
Chris Lattner | da2ce11 | 2005-01-16 07:34:08 +0000 | [diff] [blame] | 2623 | if (getRegPressure(N.getOperand(1)) > getRegPressure(N.getOperand(2))) { |
| 2624 | Tmp2 = SelectExpr(N.getOperand(1)); |
| 2625 | Tmp3 = SelectExpr(N.getOperand(2)); |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 2626 | } else { |
Chris Lattner | da2ce11 | 2005-01-16 07:34:08 +0000 | [diff] [blame] | 2627 | Tmp3 = SelectExpr(N.getOperand(2)); |
| 2628 | Tmp2 = SelectExpr(N.getOperand(1)); |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 2629 | } |
Chris Lattner | da2ce11 | 2005-01-16 07:34:08 +0000 | [diff] [blame] | 2630 | EmitSelectCC(N.getOperand(0), N.getValueType(), Tmp2, Tmp3, Result); |
| 2631 | return Result; |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 2632 | |
| 2633 | case ISD::SDIV: |
| 2634 | case ISD::UDIV: |
| 2635 | case ISD::SREM: |
| 2636 | case ISD::UREM: { |
Chris Lattner | da2ce11 | 2005-01-16 07:34:08 +0000 | [diff] [blame] | 2637 | assert((N.getOpcode() != ISD::SREM || MVT::isInteger(N.getValueType())) && |
| 2638 | "We don't support this operator!"); |
| 2639 | |
Chris Lattner | 5bf2686 | 2005-04-13 03:29:53 +0000 | [diff] [blame] | 2640 | if (N.getOpcode() == ISD::SDIV) { |
Chris Lattner | 3576c84 | 2005-01-25 20:35:10 +0000 | [diff] [blame] | 2641 | // We can fold loads into FpDIVs, but not really into any others. |
| 2642 | if (N.getValueType() == MVT::f64) { |
| 2643 | // Check for reversed and unreversed DIV. |
| 2644 | if (isFoldableLoad(N.getOperand(0), N.getOperand(1), true)) { |
| 2645 | if (N.getOperand(0).getOpcode() == ISD::EXTLOAD) |
| 2646 | Opc = X86::FDIVR32m; |
| 2647 | else |
| 2648 | Opc = X86::FDIVR64m; |
| 2649 | X86AddressMode AM; |
| 2650 | EmitFoldedLoad(N.getOperand(0), AM); |
| 2651 | Tmp1 = SelectExpr(N.getOperand(1)); |
| 2652 | addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM); |
| 2653 | return Result; |
| 2654 | } else if (isFoldableLoad(N.getOperand(1), N.getOperand(0), true) && |
| 2655 | N.getOperand(1).getOpcode() == ISD::LOAD) { |
| 2656 | if (N.getOperand(1).getOpcode() == ISD::EXTLOAD) |
| 2657 | Opc = X86::FDIV32m; |
| 2658 | else |
| 2659 | Opc = X86::FDIV64m; |
| 2660 | X86AddressMode AM; |
| 2661 | EmitFoldedLoad(N.getOperand(1), AM); |
| 2662 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 2663 | addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM); |
| 2664 | return Result; |
| 2665 | } |
| 2666 | } |
| 2667 | |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 2668 | if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) { |
| 2669 | // FIXME: These special cases should be handled by the lowering impl! |
| 2670 | unsigned RHS = CN->getValue(); |
| 2671 | bool isNeg = false; |
| 2672 | if ((int)RHS < 0) { |
| 2673 | isNeg = true; |
| 2674 | RHS = -RHS; |
| 2675 | } |
| 2676 | if (RHS && (RHS & (RHS-1)) == 0) { // Signed division by power of 2? |
| 2677 | unsigned Log = log2(RHS); |
| 2678 | unsigned TmpReg = MakeReg(N.getValueType()); |
| 2679 | unsigned SAROpc, SHROpc, ADDOpc, NEGOpc; |
| 2680 | switch (N.getValueType()) { |
| 2681 | default: assert("Unknown type to signed divide!"); |
| 2682 | case MVT::i8: |
| 2683 | SAROpc = X86::SAR8ri; |
| 2684 | SHROpc = X86::SHR8ri; |
| 2685 | ADDOpc = X86::ADD8rr; |
| 2686 | NEGOpc = X86::NEG8r; |
| 2687 | break; |
| 2688 | case MVT::i16: |
| 2689 | SAROpc = X86::SAR16ri; |
| 2690 | SHROpc = X86::SHR16ri; |
| 2691 | ADDOpc = X86::ADD16rr; |
| 2692 | NEGOpc = X86::NEG16r; |
| 2693 | break; |
| 2694 | case MVT::i32: |
| 2695 | SAROpc = X86::SAR32ri; |
| 2696 | SHROpc = X86::SHR32ri; |
| 2697 | ADDOpc = X86::ADD32rr; |
| 2698 | NEGOpc = X86::NEG32r; |
| 2699 | break; |
| 2700 | } |
Chris Lattner | 1133309 | 2005-01-11 03:11:44 +0000 | [diff] [blame] | 2701 | Tmp1 = SelectExpr(N.getOperand(0)); |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 2702 | BuildMI(BB, SAROpc, 2, TmpReg).addReg(Tmp1).addImm(Log-1); |
| 2703 | unsigned TmpReg2 = MakeReg(N.getValueType()); |
| 2704 | BuildMI(BB, SHROpc, 2, TmpReg2).addReg(TmpReg).addImm(32-Log); |
| 2705 | unsigned TmpReg3 = MakeReg(N.getValueType()); |
| 2706 | BuildMI(BB, ADDOpc, 2, TmpReg3).addReg(Tmp1).addReg(TmpReg2); |
Misha Brukman | 0e0a7a45 | 2005-04-21 23:38:14 +0000 | [diff] [blame] | 2707 | |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 2708 | unsigned TmpReg4 = isNeg ? MakeReg(N.getValueType()) : Result; |
| 2709 | BuildMI(BB, SAROpc, 2, TmpReg4).addReg(TmpReg3).addImm(Log); |
| 2710 | if (isNeg) |
| 2711 | BuildMI(BB, NEGOpc, 1, Result).addReg(TmpReg4); |
| 2712 | return Result; |
| 2713 | } |
| 2714 | } |
Chris Lattner | 5bf2686 | 2005-04-13 03:29:53 +0000 | [diff] [blame] | 2715 | } |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 2716 | |
Chris Lattner | 1133309 | 2005-01-11 03:11:44 +0000 | [diff] [blame] | 2717 | if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) { |
| 2718 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 2719 | Tmp2 = SelectExpr(N.getOperand(1)); |
| 2720 | } else { |
| 2721 | Tmp2 = SelectExpr(N.getOperand(1)); |
| 2722 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 2723 | } |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 2724 | |
| 2725 | bool isSigned = N.getOpcode() == ISD::SDIV || N.getOpcode() == ISD::SREM; |
| 2726 | bool isDiv = N.getOpcode() == ISD::SDIV || N.getOpcode() == ISD::UDIV; |
| 2727 | unsigned LoReg, HiReg, DivOpcode, MovOpcode, ClrOpcode, SExtOpcode; |
| 2728 | switch (N.getValueType()) { |
| 2729 | default: assert(0 && "Cannot sdiv this type!"); |
| 2730 | case MVT::i8: |
| 2731 | DivOpcode = isSigned ? X86::IDIV8r : X86::DIV8r; |
| 2732 | LoReg = X86::AL; |
| 2733 | HiReg = X86::AH; |
| 2734 | MovOpcode = X86::MOV8rr; |
| 2735 | ClrOpcode = X86::MOV8ri; |
| 2736 | SExtOpcode = X86::CBW; |
| 2737 | break; |
| 2738 | case MVT::i16: |
| 2739 | DivOpcode = isSigned ? X86::IDIV16r : X86::DIV16r; |
| 2740 | LoReg = X86::AX; |
| 2741 | HiReg = X86::DX; |
| 2742 | MovOpcode = X86::MOV16rr; |
| 2743 | ClrOpcode = X86::MOV16ri; |
| 2744 | SExtOpcode = X86::CWD; |
| 2745 | break; |
| 2746 | case MVT::i32: |
| 2747 | DivOpcode = isSigned ? X86::IDIV32r : X86::DIV32r; |
Chris Lattner | 4292830 | 2005-01-12 03:16:09 +0000 | [diff] [blame] | 2748 | LoReg = X86::EAX; |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 2749 | HiReg = X86::EDX; |
| 2750 | MovOpcode = X86::MOV32rr; |
| 2751 | ClrOpcode = X86::MOV32ri; |
| 2752 | SExtOpcode = X86::CDQ; |
| 2753 | break; |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 2754 | case MVT::f64: |
Chris Lattner | da2ce11 | 2005-01-16 07:34:08 +0000 | [diff] [blame] | 2755 | BuildMI(BB, X86::FpDIV, 2, Result).addReg(Tmp1).addReg(Tmp2); |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 2756 | return Result; |
| 2757 | } |
| 2758 | |
| 2759 | // Set up the low part. |
| 2760 | BuildMI(BB, MovOpcode, 1, LoReg).addReg(Tmp1); |
| 2761 | |
| 2762 | if (isSigned) { |
| 2763 | // Sign extend the low part into the high part. |
| 2764 | BuildMI(BB, SExtOpcode, 0); |
| 2765 | } else { |
| 2766 | // Zero out the high part, effectively zero extending the input. |
| 2767 | BuildMI(BB, ClrOpcode, 1, HiReg).addImm(0); |
| 2768 | } |
| 2769 | |
| 2770 | // Emit the DIV/IDIV instruction. |
Misha Brukman | 0e0a7a45 | 2005-04-21 23:38:14 +0000 | [diff] [blame] | 2771 | BuildMI(BB, DivOpcode, 1).addReg(Tmp2); |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 2772 | |
| 2773 | // Get the result of the divide or rem. |
| 2774 | BuildMI(BB, MovOpcode, 1, Result).addReg(isDiv ? LoReg : HiReg); |
| 2775 | return Result; |
| 2776 | } |
| 2777 | |
| 2778 | case ISD::SHL: |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 2779 | if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) { |
Chris Lattner | a5ade06 | 2005-01-11 21:19:59 +0000 | [diff] [blame] | 2780 | if (CN->getValue() == 1) { // X = SHL Y, 1 -> X = ADD Y, Y |
| 2781 | switch (N.getValueType()) { |
| 2782 | default: assert(0 && "Cannot shift this type!"); |
| 2783 | case MVT::i8: Opc = X86::ADD8rr; break; |
| 2784 | case MVT::i16: Opc = X86::ADD16rr; break; |
| 2785 | case MVT::i32: Opc = X86::ADD32rr; break; |
| 2786 | } |
| 2787 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 2788 | BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp1); |
| 2789 | return Result; |
| 2790 | } |
Misha Brukman | 0e0a7a45 | 2005-04-21 23:38:14 +0000 | [diff] [blame] | 2791 | |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 2792 | switch (N.getValueType()) { |
| 2793 | default: assert(0 && "Cannot shift this type!"); |
| 2794 | case MVT::i8: Opc = X86::SHL8ri; break; |
| 2795 | case MVT::i16: Opc = X86::SHL16ri; break; |
| 2796 | case MVT::i32: Opc = X86::SHL32ri; break; |
| 2797 | } |
Chris Lattner | 1133309 | 2005-01-11 03:11:44 +0000 | [diff] [blame] | 2798 | Tmp1 = SelectExpr(N.getOperand(0)); |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 2799 | BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue()); |
| 2800 | return Result; |
| 2801 | } |
Chris Lattner | 1133309 | 2005-01-11 03:11:44 +0000 | [diff] [blame] | 2802 | |
| 2803 | if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) { |
| 2804 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 2805 | Tmp2 = SelectExpr(N.getOperand(1)); |
| 2806 | } else { |
| 2807 | Tmp2 = SelectExpr(N.getOperand(1)); |
| 2808 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 2809 | } |
| 2810 | |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 2811 | switch (N.getValueType()) { |
| 2812 | default: assert(0 && "Cannot shift this type!"); |
| 2813 | case MVT::i8 : Opc = X86::SHL8rCL; break; |
| 2814 | case MVT::i16: Opc = X86::SHL16rCL; break; |
| 2815 | case MVT::i32: Opc = X86::SHL32rCL; break; |
| 2816 | } |
| 2817 | BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2); |
| 2818 | BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2); |
| 2819 | return Result; |
| 2820 | case ISD::SRL: |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 2821 | if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) { |
| 2822 | switch (N.getValueType()) { |
| 2823 | default: assert(0 && "Cannot shift this type!"); |
| 2824 | case MVT::i8: Opc = X86::SHR8ri; break; |
| 2825 | case MVT::i16: Opc = X86::SHR16ri; break; |
| 2826 | case MVT::i32: Opc = X86::SHR32ri; break; |
| 2827 | } |
Chris Lattner | 1133309 | 2005-01-11 03:11:44 +0000 | [diff] [blame] | 2828 | Tmp1 = SelectExpr(N.getOperand(0)); |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 2829 | BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue()); |
| 2830 | return Result; |
| 2831 | } |
Chris Lattner | 1133309 | 2005-01-11 03:11:44 +0000 | [diff] [blame] | 2832 | |
| 2833 | if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) { |
| 2834 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 2835 | Tmp2 = SelectExpr(N.getOperand(1)); |
| 2836 | } else { |
| 2837 | Tmp2 = SelectExpr(N.getOperand(1)); |
| 2838 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 2839 | } |
| 2840 | |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 2841 | switch (N.getValueType()) { |
| 2842 | default: assert(0 && "Cannot shift this type!"); |
| 2843 | case MVT::i8 : Opc = X86::SHR8rCL; break; |
| 2844 | case MVT::i16: Opc = X86::SHR16rCL; break; |
| 2845 | case MVT::i32: Opc = X86::SHR32rCL; break; |
| 2846 | } |
| 2847 | BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2); |
| 2848 | BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2); |
| 2849 | return Result; |
| 2850 | case ISD::SRA: |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 2851 | if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) { |
| 2852 | switch (N.getValueType()) { |
| 2853 | default: assert(0 && "Cannot shift this type!"); |
| 2854 | case MVT::i8: Opc = X86::SAR8ri; break; |
| 2855 | case MVT::i16: Opc = X86::SAR16ri; break; |
| 2856 | case MVT::i32: Opc = X86::SAR32ri; break; |
| 2857 | } |
Chris Lattner | 1133309 | 2005-01-11 03:11:44 +0000 | [diff] [blame] | 2858 | Tmp1 = SelectExpr(N.getOperand(0)); |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 2859 | BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue()); |
| 2860 | return Result; |
| 2861 | } |
Chris Lattner | 1133309 | 2005-01-11 03:11:44 +0000 | [diff] [blame] | 2862 | |
| 2863 | if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) { |
| 2864 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 2865 | Tmp2 = SelectExpr(N.getOperand(1)); |
| 2866 | } else { |
| 2867 | Tmp2 = SelectExpr(N.getOperand(1)); |
| 2868 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 2869 | } |
| 2870 | |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 2871 | switch (N.getValueType()) { |
| 2872 | default: assert(0 && "Cannot shift this type!"); |
| 2873 | case MVT::i8 : Opc = X86::SAR8rCL; break; |
| 2874 | case MVT::i16: Opc = X86::SAR16rCL; break; |
| 2875 | case MVT::i32: Opc = X86::SAR32rCL; break; |
| 2876 | } |
| 2877 | BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2); |
| 2878 | BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2); |
| 2879 | return Result; |
| 2880 | |
| 2881 | case ISD::SETCC: |
Chris Lattner | cb1aa8d | 2005-01-17 01:34:14 +0000 | [diff] [blame] | 2882 | EmitCMP(N.getOperand(0), N.getOperand(1), Node->hasOneUse()); |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 2883 | EmitSetCC(BB, Result, cast<SetCCSDNode>(N)->getCondition(), |
| 2884 | MVT::isFloatingPoint(N.getOperand(1).getValueType())); |
| 2885 | return Result; |
Chris Lattner | e9ef81d | 2005-01-15 05:22:24 +0000 | [diff] [blame] | 2886 | case ISD::LOAD: |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 2887 | // Make sure we generate both values. |
Chris Lattner | 4a10866 | 2005-01-18 03:51:59 +0000 | [diff] [blame] | 2888 | if (Result != 1) { // Generate the token |
| 2889 | if (!ExprMap.insert(std::make_pair(N.getValue(1), 1)).second) |
| 2890 | assert(0 && "Load already emitted!?"); |
| 2891 | } else |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 2892 | Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType()); |
| 2893 | |
Chris Lattner | 5188ad7 | 2005-01-08 19:28:19 +0000 | [diff] [blame] | 2894 | switch (Node->getValueType(0)) { |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 2895 | default: assert(0 && "Cannot load this type!"); |
| 2896 | case MVT::i1: |
| 2897 | case MVT::i8: Opc = X86::MOV8rm; break; |
| 2898 | case MVT::i16: Opc = X86::MOV16rm; break; |
| 2899 | case MVT::i32: Opc = X86::MOV32rm; break; |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 2900 | case MVT::f64: Opc = X86::FLD64m; ContainsFPCode = true; break; |
| 2901 | } |
Chris Lattner | 1133309 | 2005-01-11 03:11:44 +0000 | [diff] [blame] | 2902 | |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 2903 | if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N.getOperand(1))){ |
Chris Lattner | 1133309 | 2005-01-11 03:11:44 +0000 | [diff] [blame] | 2904 | Select(N.getOperand(0)); |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 2905 | addConstantPoolReference(BuildMI(BB, Opc, 4, Result), CP->getIndex()); |
| 2906 | } else { |
| 2907 | X86AddressMode AM; |
Chris Lattner | 636e79a | 2005-01-13 05:53:16 +0000 | [diff] [blame] | 2908 | |
| 2909 | SDOperand Chain = N.getOperand(0); |
| 2910 | SDOperand Address = N.getOperand(1); |
| 2911 | if (getRegPressure(Chain) > getRegPressure(Address)) { |
| 2912 | Select(Chain); |
| 2913 | SelectAddress(Address, AM); |
| 2914 | } else { |
| 2915 | SelectAddress(Address, AM); |
| 2916 | Select(Chain); |
| 2917 | } |
| 2918 | |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 2919 | addFullAddress(BuildMI(BB, Opc, 4, Result), AM); |
| 2920 | } |
| 2921 | return Result; |
Chris Lattner | e9ef81d | 2005-01-15 05:22:24 +0000 | [diff] [blame] | 2922 | |
| 2923 | case ISD::EXTLOAD: // Arbitrarily codegen extloads as MOVZX* |
| 2924 | case ISD::ZEXTLOAD: { |
| 2925 | // Make sure we generate both values. |
| 2926 | if (Result != 1) |
| 2927 | ExprMap[N.getValue(1)] = 1; // Generate the token |
| 2928 | else |
| 2929 | Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType()); |
| 2930 | |
Chris Lattner | da2ce11 | 2005-01-16 07:34:08 +0000 | [diff] [blame] | 2931 | if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N.getOperand(1))) |
| 2932 | if (Node->getValueType(0) == MVT::f64) { |
| 2933 | assert(cast<MVTSDNode>(Node)->getExtraValueType() == MVT::f32 && |
| 2934 | "Bad EXTLOAD!"); |
| 2935 | addConstantPoolReference(BuildMI(BB, X86::FLD32m, 4, Result), |
| 2936 | CP->getIndex()); |
| 2937 | return Result; |
| 2938 | } |
| 2939 | |
Chris Lattner | e9ef81d | 2005-01-15 05:22:24 +0000 | [diff] [blame] | 2940 | X86AddressMode AM; |
| 2941 | if (getRegPressure(Node->getOperand(0)) > |
| 2942 | getRegPressure(Node->getOperand(1))) { |
| 2943 | Select(Node->getOperand(0)); // chain |
| 2944 | SelectAddress(Node->getOperand(1), AM); |
| 2945 | } else { |
| 2946 | SelectAddress(Node->getOperand(1), AM); |
| 2947 | Select(Node->getOperand(0)); // chain |
| 2948 | } |
| 2949 | |
| 2950 | switch (Node->getValueType(0)) { |
| 2951 | default: assert(0 && "Unknown type to sign extend to."); |
| 2952 | case MVT::f64: |
| 2953 | assert(cast<MVTSDNode>(Node)->getExtraValueType() == MVT::f32 && |
| 2954 | "Bad EXTLOAD!"); |
| 2955 | addFullAddress(BuildMI(BB, X86::FLD32m, 5, Result), AM); |
| 2956 | break; |
| 2957 | case MVT::i32: |
| 2958 | switch (cast<MVTSDNode>(Node)->getExtraValueType()) { |
| 2959 | default: |
| 2960 | assert(0 && "Bad zero extend!"); |
| 2961 | case MVT::i1: |
| 2962 | case MVT::i8: |
| 2963 | addFullAddress(BuildMI(BB, X86::MOVZX32rm8, 5, Result), AM); |
| 2964 | break; |
| 2965 | case MVT::i16: |
| 2966 | addFullAddress(BuildMI(BB, X86::MOVZX32rm16, 5, Result), AM); |
| 2967 | break; |
| 2968 | } |
| 2969 | break; |
| 2970 | case MVT::i16: |
| 2971 | assert(cast<MVTSDNode>(Node)->getExtraValueType() <= MVT::i8 && |
| 2972 | "Bad zero extend!"); |
| 2973 | addFullAddress(BuildMI(BB, X86::MOVSX16rm8, 5, Result), AM); |
| 2974 | break; |
| 2975 | case MVT::i8: |
| 2976 | assert(cast<MVTSDNode>(Node)->getExtraValueType() == MVT::i1 && |
| 2977 | "Bad zero extend!"); |
| 2978 | addFullAddress(BuildMI(BB, X86::MOV8rm, 5, Result), AM); |
| 2979 | break; |
| 2980 | } |
| 2981 | return Result; |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 2982 | } |
Chris Lattner | e9ef81d | 2005-01-15 05:22:24 +0000 | [diff] [blame] | 2983 | case ISD::SEXTLOAD: { |
| 2984 | // Make sure we generate both values. |
| 2985 | if (Result != 1) |
| 2986 | ExprMap[N.getValue(1)] = 1; // Generate the token |
| 2987 | else |
| 2988 | Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType()); |
| 2989 | |
| 2990 | X86AddressMode AM; |
| 2991 | if (getRegPressure(Node->getOperand(0)) > |
| 2992 | getRegPressure(Node->getOperand(1))) { |
| 2993 | Select(Node->getOperand(0)); // chain |
| 2994 | SelectAddress(Node->getOperand(1), AM); |
| 2995 | } else { |
| 2996 | SelectAddress(Node->getOperand(1), AM); |
| 2997 | Select(Node->getOperand(0)); // chain |
| 2998 | } |
| 2999 | |
| 3000 | switch (Node->getValueType(0)) { |
| 3001 | case MVT::i8: assert(0 && "Cannot sign extend from bool!"); |
| 3002 | default: assert(0 && "Unknown type to sign extend to."); |
| 3003 | case MVT::i32: |
| 3004 | switch (cast<MVTSDNode>(Node)->getExtraValueType()) { |
| 3005 | default: |
| 3006 | case MVT::i1: assert(0 && "Cannot sign extend from bool!"); |
| 3007 | case MVT::i8: |
| 3008 | addFullAddress(BuildMI(BB, X86::MOVSX32rm8, 5, Result), AM); |
| 3009 | break; |
| 3010 | case MVT::i16: |
| 3011 | addFullAddress(BuildMI(BB, X86::MOVSX32rm16, 5, Result), AM); |
| 3012 | break; |
| 3013 | } |
| 3014 | break; |
| 3015 | case MVT::i16: |
| 3016 | assert(cast<MVTSDNode>(Node)->getExtraValueType() == MVT::i8 && |
| 3017 | "Cannot sign extend from bool!"); |
| 3018 | addFullAddress(BuildMI(BB, X86::MOVSX16rm8, 5, Result), AM); |
| 3019 | break; |
| 3020 | } |
| 3021 | return Result; |
| 3022 | } |
| 3023 | |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 3024 | case ISD::DYNAMIC_STACKALLOC: |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 3025 | // Generate both result values. |
| 3026 | if (Result != 1) |
| 3027 | ExprMap[N.getValue(1)] = 1; // Generate the token |
| 3028 | else |
| 3029 | Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType()); |
| 3030 | |
| 3031 | // FIXME: We are currently ignoring the requested alignment for handling |
| 3032 | // greater than the stack alignment. This will need to be revisited at some |
| 3033 | // point. Align = N.getOperand(2); |
| 3034 | |
| 3035 | if (!isa<ConstantSDNode>(N.getOperand(2)) || |
| 3036 | cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) { |
| 3037 | std::cerr << "Cannot allocate stack object with greater alignment than" |
| 3038 | << " the stack alignment yet!"; |
| 3039 | abort(); |
| 3040 | } |
Misha Brukman | 0e0a7a45 | 2005-04-21 23:38:14 +0000 | [diff] [blame] | 3041 | |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 3042 | if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) { |
Chris Lattner | 1133309 | 2005-01-11 03:11:44 +0000 | [diff] [blame] | 3043 | Select(N.getOperand(0)); |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 3044 | BuildMI(BB, X86::SUB32ri, 2, X86::ESP).addReg(X86::ESP) |
| 3045 | .addImm(CN->getValue()); |
| 3046 | } else { |
Chris Lattner | 1133309 | 2005-01-11 03:11:44 +0000 | [diff] [blame] | 3047 | if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) { |
| 3048 | Select(N.getOperand(0)); |
| 3049 | Tmp1 = SelectExpr(N.getOperand(1)); |
| 3050 | } else { |
| 3051 | Tmp1 = SelectExpr(N.getOperand(1)); |
| 3052 | Select(N.getOperand(0)); |
| 3053 | } |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 3054 | |
| 3055 | // Subtract size from stack pointer, thereby allocating some space. |
| 3056 | BuildMI(BB, X86::SUB32rr, 2, X86::ESP).addReg(X86::ESP).addReg(Tmp1); |
| 3057 | } |
| 3058 | |
| 3059 | // Put a pointer to the space into the result register, by copying the stack |
| 3060 | // pointer. |
| 3061 | BuildMI(BB, X86::MOV32rr, 1, Result).addReg(X86::ESP); |
| 3062 | return Result; |
| 3063 | |
Chris Lattner | 2e7714a | 2005-05-13 20:29:13 +0000 | [diff] [blame] | 3064 | case ISD::TAILCALL: |
Chris Lattner | c6f4181 | 2005-05-12 23:06:28 +0000 | [diff] [blame] | 3065 | case ISD::CALL: { |
Chris Lattner | 5188ad7 | 2005-01-08 19:28:19 +0000 | [diff] [blame] | 3066 | // The chain for this call is now lowered. |
Chris Lattner | 4a10866 | 2005-01-18 03:51:59 +0000 | [diff] [blame] | 3067 | ExprMap.insert(std::make_pair(N.getValue(Node->getNumValues()-1), 1)); |
Chris Lattner | 5188ad7 | 2005-01-08 19:28:19 +0000 | [diff] [blame] | 3068 | |
Chris Lattner | c6f4181 | 2005-05-12 23:06:28 +0000 | [diff] [blame] | 3069 | bool isDirect = isa<GlobalAddressSDNode>(N.getOperand(1)) || |
| 3070 | isa<ExternalSymbolSDNode>(N.getOperand(1)); |
| 3071 | unsigned Callee = 0; |
| 3072 | if (isDirect) { |
| 3073 | Select(N.getOperand(0)); |
| 3074 | } else { |
| 3075 | if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) { |
| 3076 | Select(N.getOperand(0)); |
| 3077 | Callee = SelectExpr(N.getOperand(1)); |
| 3078 | } else { |
| 3079 | Callee = SelectExpr(N.getOperand(1)); |
| 3080 | Select(N.getOperand(0)); |
| 3081 | } |
| 3082 | } |
| 3083 | |
| 3084 | // If this call has values to pass in registers, do so now. |
| 3085 | if (Node->getNumOperands() > 2) { |
| 3086 | // The first value is passed in (a part of) EAX, the second in EDX. |
| 3087 | unsigned RegOp1 = SelectExpr(N.getOperand(2)); |
| 3088 | unsigned RegOp2 = |
| 3089 | Node->getNumOperands() > 3 ? SelectExpr(N.getOperand(3)) : 0; |
| 3090 | |
| 3091 | switch (N.getOperand(2).getValueType()) { |
| 3092 | default: assert(0 && "Bad thing to pass in regs"); |
| 3093 | case MVT::i1: |
| 3094 | case MVT::i8: BuildMI(BB, X86::MOV8rr , 1,X86::AL).addReg(RegOp1); break; |
| 3095 | case MVT::i16: BuildMI(BB, X86::MOV16rr, 1,X86::AX).addReg(RegOp1); break; |
| 3096 | case MVT::i32: BuildMI(BB, X86::MOV32rr, 1,X86::EAX).addReg(RegOp1);break; |
| 3097 | } |
| 3098 | if (RegOp2) |
| 3099 | switch (N.getOperand(3).getValueType()) { |
| 3100 | default: assert(0 && "Bad thing to pass in regs"); |
| 3101 | case MVT::i1: |
| 3102 | case MVT::i8: |
| 3103 | BuildMI(BB, X86::MOV8rr , 1, X86::DL).addReg(RegOp2); |
| 3104 | break; |
| 3105 | case MVT::i16: |
| 3106 | BuildMI(BB, X86::MOV16rr, 1, X86::DX).addReg(RegOp2); |
| 3107 | break; |
| 3108 | case MVT::i32: |
| 3109 | BuildMI(BB, X86::MOV32rr, 1, X86::EDX).addReg(RegOp2); |
| 3110 | break; |
| 3111 | } |
| 3112 | } |
| 3113 | |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 3114 | if (GlobalAddressSDNode *GASD = |
| 3115 | dyn_cast<GlobalAddressSDNode>(N.getOperand(1))) { |
| 3116 | BuildMI(BB, X86::CALLpcrel32, 1).addGlobalAddress(GASD->getGlobal(),true); |
| 3117 | } else if (ExternalSymbolSDNode *ESSDN = |
| 3118 | dyn_cast<ExternalSymbolSDNode>(N.getOperand(1))) { |
| 3119 | BuildMI(BB, X86::CALLpcrel32, |
| 3120 | 1).addExternalSymbol(ESSDN->getSymbol(), true); |
| 3121 | } else { |
Chris Lattner | 1133309 | 2005-01-11 03:11:44 +0000 | [diff] [blame] | 3122 | if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) { |
| 3123 | Select(N.getOperand(0)); |
| 3124 | Tmp1 = SelectExpr(N.getOperand(1)); |
| 3125 | } else { |
| 3126 | Tmp1 = SelectExpr(N.getOperand(1)); |
| 3127 | Select(N.getOperand(0)); |
| 3128 | } |
| 3129 | |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 3130 | BuildMI(BB, X86::CALL32r, 1).addReg(Tmp1); |
| 3131 | } |
Chris Lattner | 5188ad7 | 2005-01-08 19:28:19 +0000 | [diff] [blame] | 3132 | switch (Node->getValueType(0)) { |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 3133 | default: assert(0 && "Unknown value type for call result!"); |
| 3134 | case MVT::Other: return 1; |
| 3135 | case MVT::i1: |
| 3136 | case MVT::i8: |
| 3137 | BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL); |
| 3138 | break; |
| 3139 | case MVT::i16: |
| 3140 | BuildMI(BB, X86::MOV16rr, 1, Result).addReg(X86::AX); |
| 3141 | break; |
| 3142 | case MVT::i32: |
| 3143 | BuildMI(BB, X86::MOV32rr, 1, Result).addReg(X86::EAX); |
Chris Lattner | 5188ad7 | 2005-01-08 19:28:19 +0000 | [diff] [blame] | 3144 | if (Node->getValueType(1) == MVT::i32) |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 3145 | BuildMI(BB, X86::MOV32rr, 1, Result+1).addReg(X86::EDX); |
| 3146 | break; |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 3147 | case MVT::f64: // Floating-point return values live in %ST(0) |
| 3148 | ContainsFPCode = true; |
| 3149 | BuildMI(BB, X86::FpGETRESULT, 1, Result); |
| 3150 | break; |
| 3151 | } |
| 3152 | return Result+N.ResNo; |
Chris Lattner | c6f4181 | 2005-05-12 23:06:28 +0000 | [diff] [blame] | 3153 | } |
Chris Lattner | 966cdfb | 2005-05-09 21:17:38 +0000 | [diff] [blame] | 3154 | case ISD::READPORT: |
| 3155 | // First, determine that the size of the operand falls within the acceptable |
| 3156 | // range for this architecture. |
| 3157 | // |
| 3158 | if (Node->getOperand(1).getValueType() != MVT::i16) { |
| 3159 | std::cerr << "llvm.readport: Address size is not 16 bits\n"; |
| 3160 | exit(1); |
| 3161 | } |
| 3162 | |
| 3163 | // Make sure we generate both values. |
| 3164 | if (Result != 1) { // Generate the token |
| 3165 | if (!ExprMap.insert(std::make_pair(N.getValue(1), 1)).second) |
| 3166 | assert(0 && "readport already emitted!?"); |
| 3167 | } else |
| 3168 | Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType()); |
| 3169 | |
| 3170 | Select(Node->getOperand(0)); // Select the chain. |
| 3171 | |
| 3172 | // If the port is a single-byte constant, use the immediate form. |
| 3173 | if (ConstantSDNode *Port = dyn_cast<ConstantSDNode>(Node->getOperand(1))) |
| 3174 | if ((Port->getValue() & 255) == Port->getValue()) { |
| 3175 | switch (Node->getValueType(0)) { |
| 3176 | case MVT::i8: |
| 3177 | BuildMI(BB, X86::IN8ri, 1).addImm(Port->getValue()); |
| 3178 | BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL); |
| 3179 | return Result; |
| 3180 | case MVT::i16: |
| 3181 | BuildMI(BB, X86::IN16ri, 1).addImm(Port->getValue()); |
| 3182 | BuildMI(BB, X86::MOV16rr, 1, Result).addReg(X86::AX); |
| 3183 | return Result; |
| 3184 | case MVT::i32: |
| 3185 | BuildMI(BB, X86::IN32ri, 1).addImm(Port->getValue()); |
| 3186 | BuildMI(BB, X86::MOV32rr, 1, Result).addReg(X86::EAX); |
| 3187 | return Result; |
| 3188 | default: break; |
| 3189 | } |
| 3190 | } |
| 3191 | |
| 3192 | // Now, move the I/O port address into the DX register and use the IN |
| 3193 | // instruction to get the input data. |
| 3194 | // |
| 3195 | Tmp1 = SelectExpr(Node->getOperand(1)); |
| 3196 | BuildMI(BB, X86::MOV16rr, 1, X86::DX).addReg(Tmp1); |
| 3197 | switch (Node->getValueType(0)) { |
| 3198 | case MVT::i8: |
| 3199 | BuildMI(BB, X86::IN8rr, 0); |
| 3200 | BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL); |
| 3201 | return Result; |
| 3202 | case MVT::i16: |
| 3203 | BuildMI(BB, X86::IN16rr, 0); |
| 3204 | BuildMI(BB, X86::MOV16rr, 1, Result).addReg(X86::AX); |
| 3205 | return Result; |
| 3206 | case MVT::i32: |
| 3207 | BuildMI(BB, X86::IN32rr, 0); |
| 3208 | BuildMI(BB, X86::MOV32rr, 1, Result).addReg(X86::EAX); |
| 3209 | return Result; |
| 3210 | default: |
| 3211 | std::cerr << "Cannot do input on this data type"; |
| 3212 | exit(1); |
| 3213 | } |
| 3214 | |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 3215 | } |
| 3216 | |
| 3217 | return 0; |
| 3218 | } |
| 3219 | |
Chris Lattner | e10269b | 2005-01-17 19:25:26 +0000 | [diff] [blame] | 3220 | /// TryToFoldLoadOpStore - Given a store node, try to fold together a |
| 3221 | /// load/op/store instruction. If successful return true. |
| 3222 | bool ISel::TryToFoldLoadOpStore(SDNode *Node) { |
| 3223 | assert(Node->getOpcode() == ISD::STORE && "Can only do this for stores!"); |
| 3224 | SDOperand Chain = Node->getOperand(0); |
| 3225 | SDOperand StVal = Node->getOperand(1); |
Chris Lattner | 5c65981 | 2005-01-17 22:10:42 +0000 | [diff] [blame] | 3226 | SDOperand StPtr = Node->getOperand(2); |
Chris Lattner | e10269b | 2005-01-17 19:25:26 +0000 | [diff] [blame] | 3227 | |
| 3228 | // The chain has to be a load, the stored value must be an integer binary |
| 3229 | // operation with one use. |
Chris Lattner | 5c65981 | 2005-01-17 22:10:42 +0000 | [diff] [blame] | 3230 | if (!StVal.Val->hasOneUse() || StVal.Val->getNumOperands() != 2 || |
Chris Lattner | e10269b | 2005-01-17 19:25:26 +0000 | [diff] [blame] | 3231 | MVT::isFloatingPoint(StVal.getValueType())) |
| 3232 | return false; |
| 3233 | |
Chris Lattner | 5c65981 | 2005-01-17 22:10:42 +0000 | [diff] [blame] | 3234 | // Token chain must either be a factor node or the load to fold. |
| 3235 | if (Chain.getOpcode() != ISD::LOAD && Chain.getOpcode() != ISD::TokenFactor) |
| 3236 | return false; |
Chris Lattner | e10269b | 2005-01-17 19:25:26 +0000 | [diff] [blame] | 3237 | |
Chris Lattner | 5c65981 | 2005-01-17 22:10:42 +0000 | [diff] [blame] | 3238 | SDOperand TheLoad; |
| 3239 | |
| 3240 | // Check to see if there is a load from the same pointer that we're storing |
| 3241 | // to in either operand of the binop. |
| 3242 | if (StVal.getOperand(0).getOpcode() == ISD::LOAD && |
| 3243 | StVal.getOperand(0).getOperand(1) == StPtr) |
| 3244 | TheLoad = StVal.getOperand(0); |
| 3245 | else if (StVal.getOperand(1).getOpcode() == ISD::LOAD && |
| 3246 | StVal.getOperand(1).getOperand(1) == StPtr) |
| 3247 | TheLoad = StVal.getOperand(1); |
| 3248 | else |
| 3249 | return false; // No matching load operand. |
| 3250 | |
| 3251 | // We can only fold the load if there are no intervening side-effecting |
| 3252 | // operations. This means that the store uses the load as its token chain, or |
| 3253 | // there are only token factor nodes in between the store and load. |
| 3254 | if (Chain != TheLoad.getValue(1)) { |
| 3255 | // Okay, the other option is that we have a store referring to (possibly |
| 3256 | // nested) token factor nodes. For now, just try peeking through one level |
| 3257 | // of token factors to see if this is the case. |
| 3258 | bool ChainOk = false; |
| 3259 | if (Chain.getOpcode() == ISD::TokenFactor) { |
| 3260 | for (unsigned i = 0, e = Chain.getNumOperands(); i != e; ++i) |
| 3261 | if (Chain.getOperand(i) == TheLoad.getValue(1)) { |
| 3262 | ChainOk = true; |
| 3263 | break; |
| 3264 | } |
| 3265 | } |
| 3266 | |
| 3267 | if (!ChainOk) return false; |
| 3268 | } |
| 3269 | |
| 3270 | if (TheLoad.getOperand(1) != StPtr) |
Chris Lattner | e10269b | 2005-01-17 19:25:26 +0000 | [diff] [blame] | 3271 | return false; |
| 3272 | |
| 3273 | // Make sure that one of the operands of the binop is the load, and that the |
| 3274 | // load folds into the binop. |
| 3275 | if (((StVal.getOperand(0) != TheLoad || |
| 3276 | !isFoldableLoad(TheLoad, StVal.getOperand(1))) && |
| 3277 | (StVal.getOperand(1) != TheLoad || |
| 3278 | !isFoldableLoad(TheLoad, StVal.getOperand(0))))) |
| 3279 | return false; |
| 3280 | |
| 3281 | // Finally, check to see if this is one of the ops we can handle! |
| 3282 | static const unsigned ADDTAB[] = { |
| 3283 | X86::ADD8mi, X86::ADD16mi, X86::ADD32mi, |
| 3284 | X86::ADD8mr, X86::ADD16mr, X86::ADD32mr, |
| 3285 | }; |
| 3286 | static const unsigned SUBTAB[] = { |
| 3287 | X86::SUB8mi, X86::SUB16mi, X86::SUB32mi, |
| 3288 | X86::SUB8mr, X86::SUB16mr, X86::SUB32mr, |
| 3289 | }; |
| 3290 | static const unsigned ANDTAB[] = { |
| 3291 | X86::AND8mi, X86::AND16mi, X86::AND32mi, |
| 3292 | X86::AND8mr, X86::AND16mr, X86::AND32mr, |
| 3293 | }; |
| 3294 | static const unsigned ORTAB[] = { |
| 3295 | X86::OR8mi, X86::OR16mi, X86::OR32mi, |
| 3296 | X86::OR8mr, X86::OR16mr, X86::OR32mr, |
| 3297 | }; |
| 3298 | static const unsigned XORTAB[] = { |
| 3299 | X86::XOR8mi, X86::XOR16mi, X86::XOR32mi, |
| 3300 | X86::XOR8mr, X86::XOR16mr, X86::XOR32mr, |
| 3301 | }; |
| 3302 | static const unsigned SHLTAB[] = { |
| 3303 | X86::SHL8mi, X86::SHL16mi, X86::SHL32mi, |
| 3304 | /*Have to put the reg in CL*/0, 0, 0, |
| 3305 | }; |
| 3306 | static const unsigned SARTAB[] = { |
| 3307 | X86::SAR8mi, X86::SAR16mi, X86::SAR32mi, |
| 3308 | /*Have to put the reg in CL*/0, 0, 0, |
| 3309 | }; |
| 3310 | static const unsigned SHRTAB[] = { |
| 3311 | X86::SHR8mi, X86::SHR16mi, X86::SHR32mi, |
| 3312 | /*Have to put the reg in CL*/0, 0, 0, |
| 3313 | }; |
Misha Brukman | 0e0a7a45 | 2005-04-21 23:38:14 +0000 | [diff] [blame] | 3314 | |
Chris Lattner | e10269b | 2005-01-17 19:25:26 +0000 | [diff] [blame] | 3315 | const unsigned *TabPtr = 0; |
| 3316 | switch (StVal.getOpcode()) { |
| 3317 | default: |
| 3318 | std::cerr << "CANNOT [mem] op= val: "; |
| 3319 | StVal.Val->dump(); std::cerr << "\n"; |
| 3320 | case ISD::MUL: |
| 3321 | case ISD::SDIV: |
| 3322 | case ISD::UDIV: |
| 3323 | case ISD::SREM: |
| 3324 | case ISD::UREM: return false; |
Misha Brukman | 0e0a7a45 | 2005-04-21 23:38:14 +0000 | [diff] [blame] | 3325 | |
Chris Lattner | e10269b | 2005-01-17 19:25:26 +0000 | [diff] [blame] | 3326 | case ISD::ADD: TabPtr = ADDTAB; break; |
| 3327 | case ISD::SUB: TabPtr = SUBTAB; break; |
| 3328 | case ISD::AND: TabPtr = ANDTAB; break; |
| 3329 | case ISD:: OR: TabPtr = ORTAB; break; |
| 3330 | case ISD::XOR: TabPtr = XORTAB; break; |
| 3331 | case ISD::SHL: TabPtr = SHLTAB; break; |
| 3332 | case ISD::SRA: TabPtr = SARTAB; break; |
| 3333 | case ISD::SRL: TabPtr = SHRTAB; break; |
| 3334 | } |
Misha Brukman | 0e0a7a45 | 2005-04-21 23:38:14 +0000 | [diff] [blame] | 3335 | |
Chris Lattner | e10269b | 2005-01-17 19:25:26 +0000 | [diff] [blame] | 3336 | // Handle: [mem] op= CST |
| 3337 | SDOperand Op0 = StVal.getOperand(0); |
| 3338 | SDOperand Op1 = StVal.getOperand(1); |
Chris Lattner | 0a07883 | 2005-01-23 23:20:06 +0000 | [diff] [blame] | 3339 | unsigned Opc = 0; |
Chris Lattner | e10269b | 2005-01-17 19:25:26 +0000 | [diff] [blame] | 3340 | if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1)) { |
| 3341 | switch (Op0.getValueType()) { // Use Op0's type because of shifts. |
| 3342 | default: break; |
| 3343 | case MVT::i1: |
| 3344 | case MVT::i8: Opc = TabPtr[0]; break; |
| 3345 | case MVT::i16: Opc = TabPtr[1]; break; |
| 3346 | case MVT::i32: Opc = TabPtr[2]; break; |
| 3347 | } |
Misha Brukman | 0e0a7a45 | 2005-04-21 23:38:14 +0000 | [diff] [blame] | 3348 | |
Chris Lattner | e10269b | 2005-01-17 19:25:26 +0000 | [diff] [blame] | 3349 | if (Opc) { |
Chris Lattner | 4a10866 | 2005-01-18 03:51:59 +0000 | [diff] [blame] | 3350 | if (!ExprMap.insert(std::make_pair(TheLoad.getValue(1), 1)).second) |
| 3351 | assert(0 && "Already emitted?"); |
Chris Lattner | 5c65981 | 2005-01-17 22:10:42 +0000 | [diff] [blame] | 3352 | Select(Chain); |
| 3353 | |
Chris Lattner | e10269b | 2005-01-17 19:25:26 +0000 | [diff] [blame] | 3354 | X86AddressMode AM; |
| 3355 | if (getRegPressure(TheLoad.getOperand(0)) > |
| 3356 | getRegPressure(TheLoad.getOperand(1))) { |
| 3357 | Select(TheLoad.getOperand(0)); |
| 3358 | SelectAddress(TheLoad.getOperand(1), AM); |
| 3359 | } else { |
| 3360 | SelectAddress(TheLoad.getOperand(1), AM); |
| 3361 | Select(TheLoad.getOperand(0)); |
Misha Brukman | 0e0a7a45 | 2005-04-21 23:38:14 +0000 | [diff] [blame] | 3362 | } |
Chris Lattner | 5c65981 | 2005-01-17 22:10:42 +0000 | [diff] [blame] | 3363 | |
| 3364 | if (StVal.getOpcode() == ISD::ADD) { |
| 3365 | if (CN->getValue() == 1) { |
| 3366 | switch (Op0.getValueType()) { |
| 3367 | default: break; |
| 3368 | case MVT::i8: |
| 3369 | addFullAddress(BuildMI(BB, X86::INC8m, 4), AM); |
| 3370 | return true; |
| 3371 | case MVT::i16: Opc = TabPtr[1]; |
| 3372 | addFullAddress(BuildMI(BB, X86::INC16m, 4), AM); |
| 3373 | return true; |
| 3374 | case MVT::i32: Opc = TabPtr[2]; |
| 3375 | addFullAddress(BuildMI(BB, X86::INC32m, 4), AM); |
| 3376 | return true; |
| 3377 | } |
| 3378 | } else if (CN->getValue()+1 == 0) { // [X] += -1 -> DEC [X] |
| 3379 | switch (Op0.getValueType()) { |
| 3380 | default: break; |
| 3381 | case MVT::i8: |
| 3382 | addFullAddress(BuildMI(BB, X86::DEC8m, 4), AM); |
| 3383 | return true; |
| 3384 | case MVT::i16: Opc = TabPtr[1]; |
| 3385 | addFullAddress(BuildMI(BB, X86::DEC16m, 4), AM); |
| 3386 | return true; |
| 3387 | case MVT::i32: Opc = TabPtr[2]; |
| 3388 | addFullAddress(BuildMI(BB, X86::DEC32m, 4), AM); |
| 3389 | return true; |
| 3390 | } |
| 3391 | } |
| 3392 | } |
Misha Brukman | 0e0a7a45 | 2005-04-21 23:38:14 +0000 | [diff] [blame] | 3393 | |
Chris Lattner | e10269b | 2005-01-17 19:25:26 +0000 | [diff] [blame] | 3394 | addFullAddress(BuildMI(BB, Opc, 4+1),AM).addImm(CN->getValue()); |
| 3395 | return true; |
| 3396 | } |
| 3397 | } |
Misha Brukman | 0e0a7a45 | 2005-04-21 23:38:14 +0000 | [diff] [blame] | 3398 | |
Chris Lattner | e10269b | 2005-01-17 19:25:26 +0000 | [diff] [blame] | 3399 | // If we have [mem] = V op [mem], try to turn it into: |
| 3400 | // [mem] = [mem] op V. |
| 3401 | if (Op1 == TheLoad && StVal.getOpcode() != ISD::SUB && |
| 3402 | StVal.getOpcode() != ISD::SHL && StVal.getOpcode() != ISD::SRA && |
| 3403 | StVal.getOpcode() != ISD::SRL) |
| 3404 | std::swap(Op0, Op1); |
Misha Brukman | 0e0a7a45 | 2005-04-21 23:38:14 +0000 | [diff] [blame] | 3405 | |
Chris Lattner | e10269b | 2005-01-17 19:25:26 +0000 | [diff] [blame] | 3406 | if (Op0 != TheLoad) return false; |
| 3407 | |
| 3408 | switch (Op0.getValueType()) { |
| 3409 | default: return false; |
| 3410 | case MVT::i1: |
| 3411 | case MVT::i8: Opc = TabPtr[3]; break; |
| 3412 | case MVT::i16: Opc = TabPtr[4]; break; |
| 3413 | case MVT::i32: Opc = TabPtr[5]; break; |
| 3414 | } |
Chris Lattner | 5c65981 | 2005-01-17 22:10:42 +0000 | [diff] [blame] | 3415 | |
Chris Lattner | b422aea | 2005-01-18 17:35:28 +0000 | [diff] [blame] | 3416 | // Table entry doesn't exist? |
| 3417 | if (Opc == 0) return false; |
| 3418 | |
Chris Lattner | 4a10866 | 2005-01-18 03:51:59 +0000 | [diff] [blame] | 3419 | if (!ExprMap.insert(std::make_pair(TheLoad.getValue(1), 1)).second) |
| 3420 | assert(0 && "Already emitted?"); |
Chris Lattner | 5c65981 | 2005-01-17 22:10:42 +0000 | [diff] [blame] | 3421 | Select(Chain); |
Chris Lattner | e10269b | 2005-01-17 19:25:26 +0000 | [diff] [blame] | 3422 | Select(TheLoad.getOperand(0)); |
Chris Lattner | 98a8ba0 | 2005-01-18 01:06:26 +0000 | [diff] [blame] | 3423 | |
Chris Lattner | e10269b | 2005-01-17 19:25:26 +0000 | [diff] [blame] | 3424 | X86AddressMode AM; |
| 3425 | SelectAddress(TheLoad.getOperand(1), AM); |
| 3426 | unsigned Reg = SelectExpr(Op1); |
Chris Lattner | 98a8ba0 | 2005-01-18 01:06:26 +0000 | [diff] [blame] | 3427 | addFullAddress(BuildMI(BB, Opc, 4+1), AM).addReg(Reg); |
Chris Lattner | e10269b | 2005-01-17 19:25:26 +0000 | [diff] [blame] | 3428 | return true; |
| 3429 | } |
| 3430 | |
| 3431 | |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 3432 | void ISel::Select(SDOperand N) { |
| 3433 | unsigned Tmp1, Tmp2, Opc; |
| 3434 | |
Nate Begeman | 85fdeb2 | 2005-03-24 04:39:54 +0000 | [diff] [blame] | 3435 | if (!ExprMap.insert(std::make_pair(N, 1)).second) |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 3436 | return; // Already selected. |
| 3437 | |
Chris Lattner | 989de03 | 2005-01-11 06:14:36 +0000 | [diff] [blame] | 3438 | SDNode *Node = N.Val; |
| 3439 | |
| 3440 | switch (Node->getOpcode()) { |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 3441 | default: |
Chris Lattner | 989de03 | 2005-01-11 06:14:36 +0000 | [diff] [blame] | 3442 | Node->dump(); std::cerr << "\n"; |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 3443 | assert(0 && "Node not handled yet!"); |
| 3444 | case ISD::EntryToken: return; // Noop |
Chris Lattner | c358071 | 2005-01-13 18:01:36 +0000 | [diff] [blame] | 3445 | case ISD::TokenFactor: |
Chris Lattner | 1d50b7f | 2005-01-13 19:56:00 +0000 | [diff] [blame] | 3446 | if (Node->getNumOperands() == 2) { |
Misha Brukman | 0e0a7a45 | 2005-04-21 23:38:14 +0000 | [diff] [blame] | 3447 | bool OneFirst = |
Chris Lattner | 1d50b7f | 2005-01-13 19:56:00 +0000 | [diff] [blame] | 3448 | getRegPressure(Node->getOperand(1))>getRegPressure(Node->getOperand(0)); |
| 3449 | Select(Node->getOperand(OneFirst)); |
| 3450 | Select(Node->getOperand(!OneFirst)); |
| 3451 | } else { |
| 3452 | std::vector<std::pair<unsigned, unsigned> > OpsP; |
| 3453 | for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) |
| 3454 | OpsP.push_back(std::make_pair(getRegPressure(Node->getOperand(i)), i)); |
| 3455 | std::sort(OpsP.begin(), OpsP.end()); |
| 3456 | std::reverse(OpsP.begin(), OpsP.end()); |
| 3457 | for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) |
| 3458 | Select(Node->getOperand(OpsP[i].second)); |
| 3459 | } |
Chris Lattner | c358071 | 2005-01-13 18:01:36 +0000 | [diff] [blame] | 3460 | return; |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 3461 | case ISD::CopyToReg: |
Chris Lattner | ef6806c | 2005-01-12 02:02:48 +0000 | [diff] [blame] | 3462 | if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) { |
| 3463 | Select(N.getOperand(0)); |
| 3464 | Tmp1 = SelectExpr(N.getOperand(1)); |
| 3465 | } else { |
| 3466 | Tmp1 = SelectExpr(N.getOperand(1)); |
| 3467 | Select(N.getOperand(0)); |
| 3468 | } |
Chris Lattner | 18c2f13 | 2005-01-13 20:50:02 +0000 | [diff] [blame] | 3469 | Tmp2 = cast<RegSDNode>(N)->getReg(); |
Misha Brukman | 0e0a7a45 | 2005-04-21 23:38:14 +0000 | [diff] [blame] | 3470 | |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 3471 | if (Tmp1 != Tmp2) { |
| 3472 | switch (N.getOperand(1).getValueType()) { |
| 3473 | default: assert(0 && "Invalid type for operation!"); |
| 3474 | case MVT::i1: |
| 3475 | case MVT::i8: Opc = X86::MOV8rr; break; |
| 3476 | case MVT::i16: Opc = X86::MOV16rr; break; |
| 3477 | case MVT::i32: Opc = X86::MOV32rr; break; |
Chris Lattner | ef7ba07 | 2005-01-11 03:50:45 +0000 | [diff] [blame] | 3478 | case MVT::f64: Opc = X86::FpMOV; ContainsFPCode = true; break; |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 3479 | } |
| 3480 | BuildMI(BB, Opc, 1, Tmp2).addReg(Tmp1); |
| 3481 | } |
| 3482 | return; |
| 3483 | case ISD::RET: |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 3484 | switch (N.getNumOperands()) { |
| 3485 | default: |
| 3486 | assert(0 && "Unknown return instruction!"); |
| 3487 | case 3: |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 3488 | assert(N.getOperand(1).getValueType() == MVT::i32 && |
| 3489 | N.getOperand(2).getValueType() == MVT::i32 && |
| 3490 | "Unknown two-register value!"); |
Chris Lattner | 1133309 | 2005-01-11 03:11:44 +0000 | [diff] [blame] | 3491 | if (getRegPressure(N.getOperand(1)) > getRegPressure(N.getOperand(2))) { |
| 3492 | Tmp1 = SelectExpr(N.getOperand(1)); |
| 3493 | Tmp2 = SelectExpr(N.getOperand(2)); |
| 3494 | } else { |
| 3495 | Tmp2 = SelectExpr(N.getOperand(2)); |
| 3496 | Tmp1 = SelectExpr(N.getOperand(1)); |
| 3497 | } |
| 3498 | Select(N.getOperand(0)); |
| 3499 | |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 3500 | BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1); |
| 3501 | BuildMI(BB, X86::MOV32rr, 1, X86::EDX).addReg(Tmp2); |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 3502 | break; |
| 3503 | case 2: |
Chris Lattner | 1133309 | 2005-01-11 03:11:44 +0000 | [diff] [blame] | 3504 | if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) { |
| 3505 | Select(N.getOperand(0)); |
| 3506 | Tmp1 = SelectExpr(N.getOperand(1)); |
| 3507 | } else { |
| 3508 | Tmp1 = SelectExpr(N.getOperand(1)); |
| 3509 | Select(N.getOperand(0)); |
| 3510 | } |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 3511 | switch (N.getOperand(1).getValueType()) { |
| 3512 | default: assert(0 && "All other types should have been promoted!!"); |
| 3513 | case MVT::f64: |
| 3514 | BuildMI(BB, X86::FpSETRESULT, 1).addReg(Tmp1); |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 3515 | break; |
| 3516 | case MVT::i32: |
| 3517 | BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1); |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 3518 | break; |
| 3519 | } |
| 3520 | break; |
| 3521 | case 1: |
Chris Lattner | 1133309 | 2005-01-11 03:11:44 +0000 | [diff] [blame] | 3522 | Select(N.getOperand(0)); |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 3523 | break; |
| 3524 | } |
Chris Lattner | 3648c67 | 2005-05-13 21:44:04 +0000 | [diff] [blame^] | 3525 | if (X86Lowering.getBytesToPopOnReturn() == 0) |
| 3526 | BuildMI(BB, X86::RET, 0); // Just emit a 'ret' instruction |
| 3527 | else |
| 3528 | BuildMI(BB, X86::RETI, 1).addImm(X86Lowering.getBytesToPopOnReturn()); |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 3529 | return; |
| 3530 | case ISD::BR: { |
| 3531 | Select(N.getOperand(0)); |
| 3532 | MachineBasicBlock *Dest = |
| 3533 | cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock(); |
| 3534 | BuildMI(BB, X86::JMP, 1).addMBB(Dest); |
| 3535 | return; |
| 3536 | } |
| 3537 | |
| 3538 | case ISD::BRCOND: { |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 3539 | MachineBasicBlock *Dest = |
| 3540 | cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock(); |
Chris Lattner | 1133309 | 2005-01-11 03:11:44 +0000 | [diff] [blame] | 3541 | |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 3542 | // Try to fold a setcc into the branch. If this fails, emit a test/jne |
| 3543 | // pair. |
Chris Lattner | 6c07aee | 2005-01-11 04:06:27 +0000 | [diff] [blame] | 3544 | if (EmitBranchCC(Dest, N.getOperand(0), N.getOperand(1))) { |
| 3545 | if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) { |
| 3546 | Select(N.getOperand(0)); |
| 3547 | Tmp1 = SelectExpr(N.getOperand(1)); |
| 3548 | } else { |
| 3549 | Tmp1 = SelectExpr(N.getOperand(1)); |
| 3550 | Select(N.getOperand(0)); |
| 3551 | } |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 3552 | BuildMI(BB, X86::TEST8rr, 2).addReg(Tmp1).addReg(Tmp1); |
| 3553 | BuildMI(BB, X86::JNE, 1).addMBB(Dest); |
| 3554 | } |
Chris Lattner | 1133309 | 2005-01-11 03:11:44 +0000 | [diff] [blame] | 3555 | |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 3556 | return; |
| 3557 | } |
Chris Lattner | e9ef81d | 2005-01-15 05:22:24 +0000 | [diff] [blame] | 3558 | |
Chris Lattner | 4df0de9 | 2005-01-17 00:00:33 +0000 | [diff] [blame] | 3559 | case ISD::LOAD: |
| 3560 | // If this load could be folded into the only using instruction, and if it |
| 3561 | // is safe to emit the instruction here, try to do so now. |
| 3562 | if (Node->hasNUsesOfValue(1, 0)) { |
| 3563 | SDOperand TheVal = N.getValue(0); |
| 3564 | SDNode *User = 0; |
| 3565 | for (SDNode::use_iterator UI = Node->use_begin(); ; ++UI) { |
| 3566 | assert(UI != Node->use_end() && "Didn't find use!"); |
| 3567 | SDNode *UN = *UI; |
| 3568 | for (unsigned i = 0, e = UN->getNumOperands(); i != e; ++i) |
| 3569 | if (UN->getOperand(i) == TheVal) { |
| 3570 | User = UN; |
| 3571 | goto FoundIt; |
| 3572 | } |
| 3573 | } |
| 3574 | FoundIt: |
| 3575 | // Only handle unary operators right now. |
| 3576 | if (User->getNumOperands() == 1) { |
Chris Lattner | 4a10866 | 2005-01-18 03:51:59 +0000 | [diff] [blame] | 3577 | ExprMap.erase(N); |
Chris Lattner | 4df0de9 | 2005-01-17 00:00:33 +0000 | [diff] [blame] | 3578 | SelectExpr(SDOperand(User, 0)); |
| 3579 | return; |
| 3580 | } |
| 3581 | } |
Chris Lattner | b71f8fc | 2005-01-18 04:00:54 +0000 | [diff] [blame] | 3582 | ExprMap.erase(N); |
Chris Lattner | 4df0de9 | 2005-01-17 00:00:33 +0000 | [diff] [blame] | 3583 | SelectExpr(N); |
| 3584 | return; |
Chris Lattner | 966cdfb | 2005-05-09 21:17:38 +0000 | [diff] [blame] | 3585 | case ISD::READPORT: |
Chris Lattner | e9ef81d | 2005-01-15 05:22:24 +0000 | [diff] [blame] | 3586 | case ISD::EXTLOAD: |
| 3587 | case ISD::SEXTLOAD: |
| 3588 | case ISD::ZEXTLOAD: |
Chris Lattner | 2e7714a | 2005-05-13 20:29:13 +0000 | [diff] [blame] | 3589 | case ISD::TAILCALL: |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 3590 | case ISD::CALL: |
| 3591 | case ISD::DYNAMIC_STACKALLOC: |
Chris Lattner | b71f8fc | 2005-01-18 04:00:54 +0000 | [diff] [blame] | 3592 | ExprMap.erase(N); |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 3593 | SelectExpr(N); |
| 3594 | return; |
Chris Lattner | c6f4181 | 2005-05-12 23:06:28 +0000 | [diff] [blame] | 3595 | case ISD::CopyFromReg: |
| 3596 | ExprMap.erase(N); |
| 3597 | SelectExpr(N.getValue(0)); |
| 3598 | return; |
Chris Lattner | e9ef81d | 2005-01-15 05:22:24 +0000 | [diff] [blame] | 3599 | |
| 3600 | case ISD::TRUNCSTORE: { // truncstore chain, val, ptr :storety |
| 3601 | // On X86, we can represent all types except for Bool and Float natively. |
| 3602 | X86AddressMode AM; |
| 3603 | MVT::ValueType StoredTy = cast<MVTSDNode>(Node)->getExtraValueType(); |
Chris Lattner | da2ce11 | 2005-01-16 07:34:08 +0000 | [diff] [blame] | 3604 | assert((StoredTy == MVT::i1 || StoredTy == MVT::f32 || |
| 3605 | StoredTy == MVT::i16 /*FIXME: THIS IS JUST FOR TESTING!*/) |
| 3606 | && "Unsupported TRUNCSTORE for this target!"); |
| 3607 | |
| 3608 | if (StoredTy == MVT::i16) { |
| 3609 | // FIXME: This is here just to allow testing. X86 doesn't really have a |
| 3610 | // TRUNCSTORE i16 operation, but this is required for targets that do not |
| 3611 | // have 16-bit integer registers. We occasionally disable 16-bit integer |
| 3612 | // registers to test the promotion code. |
| 3613 | Select(N.getOperand(0)); |
| 3614 | Tmp1 = SelectExpr(N.getOperand(1)); |
| 3615 | SelectAddress(N.getOperand(2), AM); |
| 3616 | |
| 3617 | BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1); |
| 3618 | addFullAddress(BuildMI(BB, X86::MOV16mr, 5), AM).addReg(X86::AX); |
| 3619 | return; |
| 3620 | } |
Chris Lattner | e9ef81d | 2005-01-15 05:22:24 +0000 | [diff] [blame] | 3621 | |
| 3622 | // Store of constant bool? |
| 3623 | if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) { |
| 3624 | if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(2))) { |
| 3625 | Select(N.getOperand(0)); |
| 3626 | SelectAddress(N.getOperand(2), AM); |
| 3627 | } else { |
| 3628 | SelectAddress(N.getOperand(2), AM); |
| 3629 | Select(N.getOperand(0)); |
| 3630 | } |
| 3631 | addFullAddress(BuildMI(BB, X86::MOV8mi, 5), AM).addImm(CN->getValue()); |
| 3632 | return; |
| 3633 | } |
| 3634 | |
| 3635 | switch (StoredTy) { |
| 3636 | default: assert(0 && "Cannot truncstore this type!"); |
| 3637 | case MVT::i1: Opc = X86::MOV8mr; break; |
| 3638 | case MVT::f32: Opc = X86::FST32m; break; |
| 3639 | } |
Misha Brukman | 0e0a7a45 | 2005-04-21 23:38:14 +0000 | [diff] [blame] | 3640 | |
Chris Lattner | e9ef81d | 2005-01-15 05:22:24 +0000 | [diff] [blame] | 3641 | std::vector<std::pair<unsigned, unsigned> > RP; |
| 3642 | RP.push_back(std::make_pair(getRegPressure(N.getOperand(0)), 0)); |
| 3643 | RP.push_back(std::make_pair(getRegPressure(N.getOperand(1)), 1)); |
| 3644 | RP.push_back(std::make_pair(getRegPressure(N.getOperand(2)), 2)); |
| 3645 | std::sort(RP.begin(), RP.end()); |
| 3646 | |
Chris Lattner | 572dd08 | 2005-02-23 05:57:21 +0000 | [diff] [blame] | 3647 | Tmp1 = 0; // Silence a warning. |
Chris Lattner | e9ef81d | 2005-01-15 05:22:24 +0000 | [diff] [blame] | 3648 | for (unsigned i = 0; i != 3; ++i) |
| 3649 | switch (RP[2-i].second) { |
| 3650 | default: assert(0 && "Unknown operand number!"); |
| 3651 | case 0: Select(N.getOperand(0)); break; |
| 3652 | case 1: Tmp1 = SelectExpr(N.getOperand(1)); break; |
| 3653 | case 2: SelectAddress(N.getOperand(2), AM); break; |
| 3654 | } |
| 3655 | |
| 3656 | addFullAddress(BuildMI(BB, Opc, 4+1), AM).addReg(Tmp1); |
| 3657 | return; |
| 3658 | } |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 3659 | case ISD::STORE: { |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 3660 | X86AddressMode AM; |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 3661 | |
| 3662 | if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) { |
| 3663 | Opc = 0; |
| 3664 | switch (CN->getValueType(0)) { |
| 3665 | default: assert(0 && "Invalid type for operation!"); |
| 3666 | case MVT::i1: |
| 3667 | case MVT::i8: Opc = X86::MOV8mi; break; |
| 3668 | case MVT::i16: Opc = X86::MOV16mi; break; |
| 3669 | case MVT::i32: Opc = X86::MOV32mi; break; |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 3670 | case MVT::f64: break; |
| 3671 | } |
| 3672 | if (Opc) { |
Chris Lattner | 1133309 | 2005-01-11 03:11:44 +0000 | [diff] [blame] | 3673 | if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(2))) { |
| 3674 | Select(N.getOperand(0)); |
| 3675 | SelectAddress(N.getOperand(2), AM); |
| 3676 | } else { |
| 3677 | SelectAddress(N.getOperand(2), AM); |
| 3678 | Select(N.getOperand(0)); |
| 3679 | } |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 3680 | addFullAddress(BuildMI(BB, Opc, 4+1), AM).addImm(CN->getValue()); |
| 3681 | return; |
| 3682 | } |
Chris Lattner | 75f354b | 2005-04-21 19:03:24 +0000 | [diff] [blame] | 3683 | } else if (GlobalAddressSDNode *GA = |
| 3684 | dyn_cast<GlobalAddressSDNode>(N.getOperand(1))) { |
| 3685 | assert(GA->getValueType(0) == MVT::i32 && "Bad pointer operand"); |
| 3686 | |
| 3687 | if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(2))) { |
| 3688 | Select(N.getOperand(0)); |
| 3689 | SelectAddress(N.getOperand(2), AM); |
| 3690 | } else { |
| 3691 | SelectAddress(N.getOperand(2), AM); |
| 3692 | Select(N.getOperand(0)); |
| 3693 | } |
| 3694 | addFullAddress(BuildMI(BB, X86::MOV32mi, 4+1), |
| 3695 | AM).addGlobalAddress(GA->getGlobal()); |
| 3696 | return; |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 3697 | } |
Chris Lattner | 837caa7 | 2005-01-11 23:21:30 +0000 | [diff] [blame] | 3698 | |
| 3699 | // Check to see if this is a load/op/store combination. |
Chris Lattner | e10269b | 2005-01-17 19:25:26 +0000 | [diff] [blame] | 3700 | if (TryToFoldLoadOpStore(Node)) |
| 3701 | return; |
Chris Lattner | 837caa7 | 2005-01-11 23:21:30 +0000 | [diff] [blame] | 3702 | |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 3703 | switch (N.getOperand(1).getValueType()) { |
| 3704 | default: assert(0 && "Cannot store this type!"); |
| 3705 | case MVT::i1: |
| 3706 | case MVT::i8: Opc = X86::MOV8mr; break; |
| 3707 | case MVT::i16: Opc = X86::MOV16mr; break; |
| 3708 | case MVT::i32: Opc = X86::MOV32mr; break; |
Chris Lattner | ef7ba07 | 2005-01-11 03:50:45 +0000 | [diff] [blame] | 3709 | case MVT::f64: Opc = X86::FST64m; break; |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 3710 | } |
Misha Brukman | 0e0a7a45 | 2005-04-21 23:38:14 +0000 | [diff] [blame] | 3711 | |
Chris Lattner | 1133309 | 2005-01-11 03:11:44 +0000 | [diff] [blame] | 3712 | std::vector<std::pair<unsigned, unsigned> > RP; |
| 3713 | RP.push_back(std::make_pair(getRegPressure(N.getOperand(0)), 0)); |
| 3714 | RP.push_back(std::make_pair(getRegPressure(N.getOperand(1)), 1)); |
| 3715 | RP.push_back(std::make_pair(getRegPressure(N.getOperand(2)), 2)); |
| 3716 | std::sort(RP.begin(), RP.end()); |
| 3717 | |
Chris Lattner | 572dd08 | 2005-02-23 05:57:21 +0000 | [diff] [blame] | 3718 | Tmp1 = 0; // Silence a warning. |
Chris Lattner | 1133309 | 2005-01-11 03:11:44 +0000 | [diff] [blame] | 3719 | for (unsigned i = 0; i != 3; ++i) |
| 3720 | switch (RP[2-i].second) { |
| 3721 | default: assert(0 && "Unknown operand number!"); |
| 3722 | case 0: Select(N.getOperand(0)); break; |
| 3723 | case 1: Tmp1 = SelectExpr(N.getOperand(1)); break; |
Chris Lattner | a3aa2e2 | 2005-01-11 03:37:59 +0000 | [diff] [blame] | 3724 | case 2: SelectAddress(N.getOperand(2), AM); break; |
Chris Lattner | 1133309 | 2005-01-11 03:11:44 +0000 | [diff] [blame] | 3725 | } |
| 3726 | |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 3727 | addFullAddress(BuildMI(BB, Opc, 4+1), AM).addReg(Tmp1); |
| 3728 | return; |
| 3729 | } |
Chris Lattner | 16cd04d | 2005-05-12 23:24:06 +0000 | [diff] [blame] | 3730 | case ISD::CALLSEQ_START: |
Chris Lattner | 3648c67 | 2005-05-13 21:44:04 +0000 | [diff] [blame^] | 3731 | Select(N.getOperand(0)); |
| 3732 | // Stack amount |
| 3733 | Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue(); |
| 3734 | BuildMI(BB, X86::ADJCALLSTACKDOWN, 1).addImm(Tmp1); |
| 3735 | return; |
Chris Lattner | 16cd04d | 2005-05-12 23:24:06 +0000 | [diff] [blame] | 3736 | case ISD::CALLSEQ_END: |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 3737 | Select(N.getOperand(0)); |
Chris Lattner | 3648c67 | 2005-05-13 21:44:04 +0000 | [diff] [blame^] | 3738 | // Stack amount |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 3739 | Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue(); |
Misha Brukman | 0e0a7a45 | 2005-04-21 23:38:14 +0000 | [diff] [blame] | 3740 | |
Chris Lattner | 3648c67 | 2005-05-13 21:44:04 +0000 | [diff] [blame^] | 3741 | // Amount the callee added to the stack pointer. |
| 3742 | Tmp2 = cast<ConstantSDNode>(N.getOperand(2))->getValue(); |
| 3743 | BuildMI(BB, X86::ADJCALLSTACKUP, 2).addImm(Tmp1).addImm(Tmp2); |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 3744 | return; |
Chris Lattner | 989de03 | 2005-01-11 06:14:36 +0000 | [diff] [blame] | 3745 | case ISD::MEMSET: { |
| 3746 | Select(N.getOperand(0)); // Select the chain. |
| 3747 | unsigned Align = |
| 3748 | (unsigned)cast<ConstantSDNode>(Node->getOperand(4))->getValue(); |
| 3749 | if (Align == 0) Align = 1; |
| 3750 | |
| 3751 | // Turn the byte code into # iterations |
| 3752 | unsigned CountReg; |
| 3753 | unsigned Opcode; |
| 3754 | if (ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Node->getOperand(2))) { |
| 3755 | unsigned Val = ValC->getValue() & 255; |
| 3756 | |
| 3757 | // If the value is a constant, then we can potentially use larger sets. |
| 3758 | switch (Align & 3) { |
| 3759 | case 2: // WORD aligned |
| 3760 | CountReg = MakeReg(MVT::i32); |
| 3761 | if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) { |
| 3762 | BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/2); |
| 3763 | } else { |
| 3764 | unsigned ByteReg = SelectExpr(Node->getOperand(3)); |
| 3765 | BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(1); |
| 3766 | } |
| 3767 | BuildMI(BB, X86::MOV16ri, 1, X86::AX).addImm((Val << 8) | Val); |
| 3768 | Opcode = X86::REP_STOSW; |
| 3769 | break; |
| 3770 | case 0: // DWORD aligned |
| 3771 | CountReg = MakeReg(MVT::i32); |
| 3772 | if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) { |
| 3773 | BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/4); |
| 3774 | } else { |
| 3775 | unsigned ByteReg = SelectExpr(Node->getOperand(3)); |
| 3776 | BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(2); |
| 3777 | } |
| 3778 | Val = (Val << 8) | Val; |
| 3779 | BuildMI(BB, X86::MOV32ri, 1, X86::EAX).addImm((Val << 16) | Val); |
| 3780 | Opcode = X86::REP_STOSD; |
| 3781 | break; |
| 3782 | default: // BYTE aligned |
| 3783 | CountReg = SelectExpr(Node->getOperand(3)); |
| 3784 | BuildMI(BB, X86::MOV8ri, 1, X86::AL).addImm(Val); |
| 3785 | Opcode = X86::REP_STOSB; |
| 3786 | break; |
| 3787 | } |
| 3788 | } else { |
| 3789 | // If it's not a constant value we are storing, just fall back. We could |
| 3790 | // try to be clever to form 16 bit and 32 bit values, but we don't yet. |
| 3791 | unsigned ValReg = SelectExpr(Node->getOperand(2)); |
| 3792 | BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(ValReg); |
| 3793 | CountReg = SelectExpr(Node->getOperand(3)); |
| 3794 | Opcode = X86::REP_STOSB; |
| 3795 | } |
| 3796 | |
| 3797 | // No matter what the alignment is, we put the source in ESI, the |
| 3798 | // destination in EDI, and the count in ECX. |
| 3799 | unsigned TmpReg1 = SelectExpr(Node->getOperand(1)); |
| 3800 | BuildMI(BB, X86::MOV32rr, 1, X86::ECX).addReg(CountReg); |
| 3801 | BuildMI(BB, X86::MOV32rr, 1, X86::EDI).addReg(TmpReg1); |
| 3802 | BuildMI(BB, Opcode, 0); |
| 3803 | return; |
| 3804 | } |
Chris Lattner | 966cdfb | 2005-05-09 21:17:38 +0000 | [diff] [blame] | 3805 | case ISD::MEMCPY: { |
Chris Lattner | 31805bf | 2005-01-11 06:19:26 +0000 | [diff] [blame] | 3806 | Select(N.getOperand(0)); // Select the chain. |
| 3807 | unsigned Align = |
| 3808 | (unsigned)cast<ConstantSDNode>(Node->getOperand(4))->getValue(); |
| 3809 | if (Align == 0) Align = 1; |
| 3810 | |
| 3811 | // Turn the byte code into # iterations |
| 3812 | unsigned CountReg; |
| 3813 | unsigned Opcode; |
| 3814 | switch (Align & 3) { |
| 3815 | case 2: // WORD aligned |
| 3816 | CountReg = MakeReg(MVT::i32); |
| 3817 | if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) { |
| 3818 | BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/2); |
| 3819 | } else { |
| 3820 | unsigned ByteReg = SelectExpr(Node->getOperand(3)); |
| 3821 | BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(1); |
| 3822 | } |
| 3823 | Opcode = X86::REP_MOVSW; |
| 3824 | break; |
| 3825 | case 0: // DWORD aligned |
| 3826 | CountReg = MakeReg(MVT::i32); |
| 3827 | if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) { |
| 3828 | BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/4); |
| 3829 | } else { |
| 3830 | unsigned ByteReg = SelectExpr(Node->getOperand(3)); |
| 3831 | BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(2); |
| 3832 | } |
| 3833 | Opcode = X86::REP_MOVSD; |
| 3834 | break; |
| 3835 | default: // BYTE aligned |
| 3836 | CountReg = SelectExpr(Node->getOperand(3)); |
| 3837 | Opcode = X86::REP_MOVSB; |
| 3838 | break; |
| 3839 | } |
| 3840 | |
| 3841 | // No matter what the alignment is, we put the source in ESI, the |
| 3842 | // destination in EDI, and the count in ECX. |
| 3843 | unsigned TmpReg1 = SelectExpr(Node->getOperand(1)); |
| 3844 | unsigned TmpReg2 = SelectExpr(Node->getOperand(2)); |
| 3845 | BuildMI(BB, X86::MOV32rr, 1, X86::ECX).addReg(CountReg); |
| 3846 | BuildMI(BB, X86::MOV32rr, 1, X86::EDI).addReg(TmpReg1); |
| 3847 | BuildMI(BB, X86::MOV32rr, 1, X86::ESI).addReg(TmpReg2); |
| 3848 | BuildMI(BB, Opcode, 0); |
| 3849 | return; |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 3850 | } |
Chris Lattner | 966cdfb | 2005-05-09 21:17:38 +0000 | [diff] [blame] | 3851 | case ISD::WRITEPORT: |
| 3852 | if (Node->getOperand(2).getValueType() != MVT::i16) { |
| 3853 | std::cerr << "llvm.writeport: Address size is not 16 bits\n"; |
| 3854 | exit(1); |
| 3855 | } |
| 3856 | Select(Node->getOperand(0)); // Emit the chain. |
| 3857 | |
| 3858 | Tmp1 = SelectExpr(Node->getOperand(1)); |
| 3859 | switch (Node->getOperand(1).getValueType()) { |
| 3860 | case MVT::i8: |
| 3861 | BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(Tmp1); |
| 3862 | Tmp2 = X86::OUT8ir; Opc = X86::OUT8rr; |
| 3863 | break; |
| 3864 | case MVT::i16: |
| 3865 | BuildMI(BB, X86::MOV16rr, 1, X86::AX).addReg(Tmp1); |
| 3866 | Tmp2 = X86::OUT16ir; Opc = X86::OUT16rr; |
| 3867 | break; |
| 3868 | case MVT::i32: |
| 3869 | BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1); |
| 3870 | Tmp2 = X86::OUT32ir; Opc = X86::OUT32rr; |
| 3871 | break; |
| 3872 | default: |
| 3873 | std::cerr << "llvm.writeport: invalid data type for X86 target"; |
| 3874 | exit(1); |
| 3875 | } |
| 3876 | |
| 3877 | // If the port is a single-byte constant, use the immediate form. |
| 3878 | if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Node->getOperand(2))) |
| 3879 | if ((CN->getValue() & 255) == CN->getValue()) { |
| 3880 | BuildMI(BB, Tmp2, 1).addImm(CN->getValue()); |
| 3881 | return; |
| 3882 | } |
| 3883 | |
| 3884 | // Otherwise, move the I/O port address into the DX register. |
| 3885 | unsigned Reg = SelectExpr(Node->getOperand(2)); |
| 3886 | BuildMI(BB, X86::MOV16rr, 1, X86::DX).addReg(Reg); |
| 3887 | BuildMI(BB, Opc, 0); |
| 3888 | return; |
| 3889 | } |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 3890 | assert(0 && "Should not be reached!"); |
| 3891 | } |
| 3892 | |
| 3893 | |
| 3894 | /// createX86PatternInstructionSelector - This pass converts an LLVM function |
| 3895 | /// into a machine code representation using pattern matching and a machine |
| 3896 | /// description file. |
| 3897 | /// |
| 3898 | FunctionPass *llvm::createX86PatternInstructionSelector(TargetMachine &TM) { |
Misha Brukman | 0e0a7a45 | 2005-04-21 23:38:14 +0000 | [diff] [blame] | 3899 | return new ISel(TM); |
Chris Lattner | 8acb1ba | 2005-01-07 07:49:41 +0000 | [diff] [blame] | 3900 | } |