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