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