blob: 3345afcc4852ee429e74823447b509aceee82663 [file] [log] [blame]
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001//===-- X86ISelPattern.cpp - A pattern matching inst selector for X86 -----===//
Chris Lattner24aad1b2005-01-10 22:10:13 +00002//
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003// 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 Brukman0e0a7a452005-04-21 23:38:14 +00007//
Chris Lattner8acb1ba2005-01-07 07:49:41 +00008//===----------------------------------------------------------------------===//
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 Lattnerc6f41812005-05-12 23:06:28 +000017#include "llvm/CallingConv.h"
Chris Lattnere3e0f272005-05-09 03:36:39 +000018#include "llvm/Constants.h"
19#include "llvm/Instructions.h"
Chris Lattner8acb1ba2005-01-07 07:49:41 +000020#include "llvm/Function.h"
Chris Lattnere3e0f272005-05-09 03:36:39 +000021#include "llvm/CodeGen/MachineConstantPool.h"
Chris Lattner8acb1ba2005-01-07 07:49:41 +000022#include "llvm/CodeGen/MachineFunction.h"
23#include "llvm/CodeGen/MachineFrameInfo.h"
24#include "llvm/CodeGen/SelectionDAG.h"
25#include "llvm/CodeGen/SelectionDAGISel.h"
26#include "llvm/CodeGen/SSARegMap.h"
27#include "llvm/Target/TargetData.h"
28#include "llvm/Target/TargetLowering.h"
Chris Lattnerc5dcb532005-04-30 04:25:35 +000029#include "llvm/Target/TargetOptions.h"
Chris Lattnere3e0f272005-05-09 03:36:39 +000030#include "llvm/Support/CFG.h"
Chris Lattner8acb1ba2005-01-07 07:49:41 +000031#include "llvm/Support/MathExtras.h"
32#include "llvm/ADT/Statistic.h"
33#include <set>
Jeff Cohen603fea92005-01-12 04:29:05 +000034#include <algorithm>
Chris Lattner8acb1ba2005-01-07 07:49:41 +000035using namespace llvm;
36
Chris Lattnerc6f41812005-05-12 23:06:28 +000037// FIXME: temporary.
38#include "llvm/Support/CommandLine.h"
39static cl::opt<bool> EnableFastCC("enable-x86-fastcc", cl::Hidden,
40 cl::desc("Enable fastcc on X86"));
41
Chris Lattner8acb1ba2005-01-07 07:49:41 +000042//===----------------------------------------------------------------------===//
43// X86TargetLowering - X86 Implementation of the TargetLowering interface
44namespace {
45 class X86TargetLowering : public TargetLowering {
46 int VarArgsFrameIndex; // FrameIndex for start of varargs area.
Chris Lattner14824582005-01-09 00:01:27 +000047 int ReturnAddrIndex; // FrameIndex for return slot.
Chris Lattneradf6a962005-05-13 18:50:42 +000048 int BytesToPopOnReturn; // Number of bytes ret should pop.
Chris Lattner8acb1ba2005-01-07 07:49:41 +000049 public:
50 X86TargetLowering(TargetMachine &TM) : TargetLowering(TM) {
51 // Set up the TargetLowering object.
Chris Lattner4df0de92005-01-17 00:00:33 +000052
53 // X86 is wierd, it always uses i8 for shift amounts and setcc results.
54 setShiftAmountType(MVT::i8);
55 setSetCCResultType(MVT::i8);
Chris Lattner6659bd72005-04-07 19:41:46 +000056 setSetCCResultContents(ZeroOrOneSetCCResult);
Chris Lattner009b55b2005-01-19 03:36:30 +000057 setShiftAmountFlavor(Mask); // shl X, 32 == shl X, 0
Chris Lattner4df0de92005-01-17 00:00:33 +000058
59 // Set up the register classes.
Chris Lattner8acb1ba2005-01-07 07:49:41 +000060 addRegisterClass(MVT::i8, X86::R8RegisterClass);
61 addRegisterClass(MVT::i16, X86::R16RegisterClass);
62 addRegisterClass(MVT::i32, X86::R32RegisterClass);
63 addRegisterClass(MVT::f64, X86::RFPRegisterClass);
Misha Brukman0e0a7a452005-04-21 23:38:14 +000064
Chris Lattner8acb1ba2005-01-07 07:49:41 +000065 // FIXME: Eliminate these two classes when legalize can handle promotions
66 // well.
Chris Lattnerda2ce112005-01-16 07:34:08 +000067/**/ addRegisterClass(MVT::i1, X86::R8RegisterClass);
Chris Lattnerda2ce112005-01-16 07:34:08 +000068
Chris Lattnerda4d4692005-04-09 03:22:37 +000069 setOperationAction(ISD::BRCONDTWOWAY , MVT::Other, Expand);
Chris Lattnerda2ce112005-01-16 07:34:08 +000070 setOperationAction(ISD::MEMMOVE , MVT::Other, Expand);
71 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Expand);
Chris Lattnerda2ce112005-01-16 07:34:08 +000072 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
Chris Lattnerda2ce112005-01-16 07:34:08 +000073 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
74 setOperationAction(ISD::SEXTLOAD , MVT::i1 , Expand);
75 setOperationAction(ISD::SREM , MVT::f64 , Expand);
Chris Lattnerc610d422005-05-11 05:00:34 +000076 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
77 setOperationAction(ISD::CTTZ , MVT::i8 , Expand);
78 setOperationAction(ISD::CTLZ , MVT::i8 , Expand);
79 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
80 setOperationAction(ISD::CTTZ , MVT::i16 , Expand);
81 setOperationAction(ISD::CTLZ , MVT::i16 , Expand);
Andrew Lenharth691ef2b2005-05-03 17:19:30 +000082 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
83 setOperationAction(ISD::CTTZ , MVT::i32 , Expand);
Andrew Lenharthb5884d32005-05-04 19:25:37 +000084 setOperationAction(ISD::CTLZ , MVT::i32 , Expand);
Chris Lattner43fdea02005-04-02 05:03:24 +000085
Chris Lattner4e6ce5f2005-05-09 20:37:29 +000086 setOperationAction(ISD::READIO , MVT::i1 , Expand);
87 setOperationAction(ISD::READIO , MVT::i8 , Expand);
88 setOperationAction(ISD::READIO , MVT::i16 , Expand);
89 setOperationAction(ISD::READIO , MVT::i32 , Expand);
90 setOperationAction(ISD::WRITEIO , MVT::i1 , Expand);
91 setOperationAction(ISD::WRITEIO , MVT::i8 , Expand);
92 setOperationAction(ISD::WRITEIO , MVT::i16 , Expand);
93 setOperationAction(ISD::WRITEIO , MVT::i32 , Expand);
94
Chris Lattnerc5dcb532005-04-30 04:25:35 +000095 if (!UnsafeFPMath) {
96 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
97 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
98 }
99
Chris Lattnerda2ce112005-01-16 07:34:08 +0000100 // These should be promoted to a larger select which is supported.
101/**/ setOperationAction(ISD::SELECT , MVT::i1 , Promote);
102 setOperationAction(ISD::SELECT , MVT::i8 , Promote);
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000103
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000104 computeRegisterProperties();
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000105
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000106 addLegalFPImmediate(+0.0); // FLD0
107 addLegalFPImmediate(+1.0); // FLD1
108 addLegalFPImmediate(-0.0); // FLD0/FCHS
109 addLegalFPImmediate(-1.0); // FLD1/FCHS
110 }
111
Chris Lattner3648c672005-05-13 21:44:04 +0000112 // Return the number of bytes that a function should pop when it returns (in
113 // addition to the space used by the return address).
114 //
115 unsigned getBytesToPopOnReturn() const { return BytesToPopOnReturn; }
116
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000117 /// LowerArguments - This hook must be implemented to indicate how we should
118 /// lower the arguments for the specified function, into the specified DAG.
119 virtual std::vector<SDOperand>
120 LowerArguments(Function &F, SelectionDAG &DAG);
121
122 /// LowerCallTo - This hook lowers an abstract call to a function into an
123 /// actual call.
Chris Lattner5188ad72005-01-08 19:28:19 +0000124 virtual std::pair<SDOperand, SDOperand>
Chris Lattnerc57f6822005-05-12 19:56:45 +0000125 LowerCallTo(SDOperand Chain, const Type *RetTy, bool isVarArg, unsigned CC,
Chris Lattneradf6a962005-05-13 18:50:42 +0000126 bool isTailCall, SDOperand Callee, ArgListTy &Args,
127 SelectionDAG &DAG);
Chris Lattner14824582005-01-09 00:01:27 +0000128
129 virtual std::pair<SDOperand, SDOperand>
130 LowerVAStart(SDOperand Chain, SelectionDAG &DAG);
131
132 virtual std::pair<SDOperand,SDOperand>
133 LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
134 const Type *ArgTy, SelectionDAG &DAG);
135
136 virtual std::pair<SDOperand, SDOperand>
137 LowerFrameReturnAddress(bool isFrameAddr, SDOperand Chain, unsigned Depth,
138 SelectionDAG &DAG);
Chris Lattnerc6f41812005-05-12 23:06:28 +0000139 private:
140 // C Calling Convention implementation.
141 std::vector<SDOperand> LowerCCCArguments(Function &F, SelectionDAG &DAG);
142 std::pair<SDOperand, SDOperand>
143 LowerCCCCallTo(SDOperand Chain, const Type *RetTy, bool isVarArg,
Chris Lattner2e7714a2005-05-13 20:29:13 +0000144 bool isTailCall,
Chris Lattnerc6f41812005-05-12 23:06:28 +0000145 SDOperand Callee, ArgListTy &Args, SelectionDAG &DAG);
146
147 // Fast Calling Convention implementation.
148 std::vector<SDOperand> LowerFastCCArguments(Function &F, SelectionDAG &DAG);
149 std::pair<SDOperand, SDOperand>
Chris Lattner2e7714a2005-05-13 20:29:13 +0000150 LowerFastCCCallTo(SDOperand Chain, const Type *RetTy, bool isTailCall,
Chris Lattnerc6f41812005-05-12 23:06:28 +0000151 SDOperand Callee, ArgListTy &Args, SelectionDAG &DAG);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000152 };
153}
154
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000155std::vector<SDOperand>
156X86TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
Chris Lattnerc6f41812005-05-12 23:06:28 +0000157 if (F.getCallingConv() == CallingConv::Fast && EnableFastCC)
158 return LowerFastCCArguments(F, DAG);
159 return LowerCCCArguments(F, DAG);
160}
161
162std::pair<SDOperand, SDOperand>
163X86TargetLowering::LowerCallTo(SDOperand Chain, const Type *RetTy,
164 bool isVarArg, unsigned CallingConv,
Chris Lattneradf6a962005-05-13 18:50:42 +0000165 bool isTailCall,
Chris Lattnerc6f41812005-05-12 23:06:28 +0000166 SDOperand Callee, ArgListTy &Args,
167 SelectionDAG &DAG) {
168 assert((!isVarArg || CallingConv == CallingConv::C) &&
169 "Only C takes varargs!");
170 if (CallingConv == CallingConv::Fast && EnableFastCC)
Chris Lattner2e7714a2005-05-13 20:29:13 +0000171 return LowerFastCCCallTo(Chain, RetTy, isTailCall, Callee, Args, DAG);
172 return LowerCCCCallTo(Chain, RetTy, isVarArg, isTailCall, Callee, Args, DAG);
Chris Lattnerc6f41812005-05-12 23:06:28 +0000173}
174
175//===----------------------------------------------------------------------===//
176// C Calling Convention implementation
177//===----------------------------------------------------------------------===//
178
179std::vector<SDOperand>
180X86TargetLowering::LowerCCCArguments(Function &F, SelectionDAG &DAG) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000181 std::vector<SDOperand> ArgValues;
182
Chris Lattner6415bb42005-05-10 03:53:18 +0000183 MachineFunction &MF = DAG.getMachineFunction();
184 MachineFrameInfo *MFI = MF.getFrameInfo();
185
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000186 // Add DAG nodes to load the arguments... On entry to a function on the X86,
187 // the stack frame looks like this:
188 //
189 // [ESP] -- return address
190 // [ESP + 4] -- first argument (leftmost lexically)
191 // [ESP + 8] -- second argument, if first argument is four bytes in size
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000192 // ...
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000193 //
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000194 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
Chris Lattnere4d5c442005-03-15 04:54:21 +0000195 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000196 MVT::ValueType ObjectVT = getValueType(I->getType());
197 unsigned ArgIncrement = 4;
198 unsigned ObjSize;
199 switch (ObjectVT) {
200 default: assert(0 && "Unhandled argument type!");
201 case MVT::i1:
202 case MVT::i8: ObjSize = 1; break;
203 case MVT::i16: ObjSize = 2; break;
204 case MVT::i32: ObjSize = 4; break;
205 case MVT::i64: ObjSize = ArgIncrement = 8; break;
206 case MVT::f32: ObjSize = 4; break;
207 case MVT::f64: ObjSize = ArgIncrement = 8; break;
208 }
209 // Create the frame index object for this incoming parameter...
210 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000211
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000212 // Create the SelectionDAG nodes corresponding to a load from this parameter
213 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
214
215 // Don't codegen dead arguments. FIXME: remove this check when we can nuke
216 // dead loads.
217 SDOperand ArgValue;
218 if (!I->use_empty())
Chris Lattnera80d2bd2005-05-09 05:40:26 +0000219 ArgValue = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
220 DAG.getSrcValue(NULL));
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000221 else {
222 if (MVT::isInteger(ObjectVT))
223 ArgValue = DAG.getConstant(0, ObjectVT);
224 else
225 ArgValue = DAG.getConstantFP(0, ObjectVT);
226 }
227 ArgValues.push_back(ArgValue);
228
229 ArgOffset += ArgIncrement; // Move on to the next argument...
230 }
231
232 // If the function takes variable number of arguments, make a frame index for
233 // the start of the first vararg value... for expansion of llvm.va_start.
234 if (F.isVarArg())
235 VarArgsFrameIndex = MFI->CreateFixedObject(1, ArgOffset);
Chris Lattner3648c672005-05-13 21:44:04 +0000236 ReturnAddrIndex = 0; // No return address slot generated yet.
237 BytesToPopOnReturn = 0; // Callee pops nothing.
Chris Lattner4c52f0e2005-04-09 15:23:56 +0000238
239 // Finally, inform the code generator which regs we return values in.
240 switch (getValueType(F.getReturnType())) {
241 default: assert(0 && "Unknown type!");
242 case MVT::isVoid: break;
243 case MVT::i1:
244 case MVT::i8:
245 case MVT::i16:
246 case MVT::i32:
247 MF.addLiveOut(X86::EAX);
248 break;
249 case MVT::i64:
250 MF.addLiveOut(X86::EAX);
251 MF.addLiveOut(X86::EDX);
252 break;
253 case MVT::f32:
254 case MVT::f64:
255 MF.addLiveOut(X86::ST0);
256 break;
257 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000258 return ArgValues;
259}
260
Chris Lattner5188ad72005-01-08 19:28:19 +0000261std::pair<SDOperand, SDOperand>
Chris Lattnerc6f41812005-05-12 23:06:28 +0000262X86TargetLowering::LowerCCCCallTo(SDOperand Chain, const Type *RetTy,
Chris Lattner2e7714a2005-05-13 20:29:13 +0000263 bool isVarArg, bool isTailCall,
264 SDOperand Callee, ArgListTy &Args,
265 SelectionDAG &DAG) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000266 // Count how many bytes are to be pushed on the stack.
267 unsigned NumBytes = 0;
268
269 if (Args.empty()) {
270 // Save zero bytes.
Chris Lattner16cd04d2005-05-12 23:24:06 +0000271 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
Chris Lattner5188ad72005-01-08 19:28:19 +0000272 DAG.getConstant(0, getPointerTy()));
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000273 } else {
274 for (unsigned i = 0, e = Args.size(); i != e; ++i)
275 switch (getValueType(Args[i].second)) {
276 default: assert(0 && "Unknown value type!");
277 case MVT::i1:
278 case MVT::i8:
279 case MVT::i16:
280 case MVT::i32:
281 case MVT::f32:
282 NumBytes += 4;
283 break;
284 case MVT::i64:
285 case MVT::f64:
286 NumBytes += 8;
287 break;
288 }
289
Chris Lattner16cd04d2005-05-12 23:24:06 +0000290 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
Chris Lattner5188ad72005-01-08 19:28:19 +0000291 DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000292
293 // Arguments go on the stack in reverse order, as specified by the ABI.
294 unsigned ArgOffset = 0;
Chris Lattner7f2afac2005-01-14 22:37:41 +0000295 SDOperand StackPtr = DAG.getCopyFromReg(X86::ESP, MVT::i32,
296 DAG.getEntryNode());
Chris Lattnerb62e1e22005-01-21 19:46:38 +0000297 std::vector<SDOperand> Stores;
298
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000299 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000300 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
301 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
302
303 switch (getValueType(Args[i].second)) {
304 default: assert(0 && "Unexpected ValueType for argument!");
305 case MVT::i1:
306 case MVT::i8:
307 case MVT::i16:
308 // Promote the integer to 32 bits. If the input type is signed use a
309 // sign extend, otherwise use a zero extend.
310 if (Args[i].second->isSigned())
311 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
312 else
313 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
314
315 // FALL THROUGH
316 case MVT::i32:
317 case MVT::f32:
Chris Lattnerb62e1e22005-01-21 19:46:38 +0000318 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
Chris Lattnera80d2bd2005-05-09 05:40:26 +0000319 Args[i].first, PtrOff,
320 DAG.getSrcValue(NULL)));
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000321 ArgOffset += 4;
322 break;
323 case MVT::i64:
324 case MVT::f64:
Chris Lattnerb62e1e22005-01-21 19:46:38 +0000325 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
Chris Lattnera80d2bd2005-05-09 05:40:26 +0000326 Args[i].first, PtrOff,
327 DAG.getSrcValue(NULL)));
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000328 ArgOffset += 8;
329 break;
330 }
331 }
Chris Lattnerb62e1e22005-01-21 19:46:38 +0000332 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, Stores);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000333 }
334
335 std::vector<MVT::ValueType> RetVals;
336 MVT::ValueType RetTyVT = getValueType(RetTy);
337 if (RetTyVT != MVT::isVoid)
338 RetVals.push_back(RetTyVT);
339 RetVals.push_back(MVT::Other);
340
Chris Lattner2e7714a2005-05-13 20:29:13 +0000341 SDOperand TheCall = SDOperand(DAG.getCall(RetVals, Chain, Callee,
342 isTailCall), 0);
Chris Lattnerb0802652005-01-08 20:51:36 +0000343 Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
Chris Lattner16cd04d2005-05-12 23:24:06 +0000344 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain,
Chris Lattneradf6a962005-05-13 18:50:42 +0000345 DAG.getConstant(NumBytes, getPointerTy()),
346 DAG.getConstant(0, getPointerTy()));
Chris Lattner5188ad72005-01-08 19:28:19 +0000347 return std::make_pair(TheCall, Chain);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000348}
349
Chris Lattner14824582005-01-09 00:01:27 +0000350std::pair<SDOperand, SDOperand>
351X86TargetLowering::LowerVAStart(SDOperand Chain, SelectionDAG &DAG) {
352 // vastart just returns the address of the VarArgsFrameIndex slot.
353 return std::make_pair(DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32), Chain);
354}
355
356std::pair<SDOperand,SDOperand> X86TargetLowering::
357LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
358 const Type *ArgTy, SelectionDAG &DAG) {
359 MVT::ValueType ArgVT = getValueType(ArgTy);
360 SDOperand Result;
361 if (!isVANext) {
Chris Lattnera80d2bd2005-05-09 05:40:26 +0000362 Result = DAG.getLoad(ArgVT, DAG.getEntryNode(), VAList,
363 DAG.getSrcValue(NULL));
Chris Lattner14824582005-01-09 00:01:27 +0000364 } else {
365 unsigned Amt;
366 if (ArgVT == MVT::i32)
367 Amt = 4;
368 else {
369 assert((ArgVT == MVT::i64 || ArgVT == MVT::f64) &&
370 "Other types should have been promoted for varargs!");
371 Amt = 8;
372 }
373 Result = DAG.getNode(ISD::ADD, VAList.getValueType(), VAList,
374 DAG.getConstant(Amt, VAList.getValueType()));
375 }
376 return std::make_pair(Result, Chain);
377}
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000378
Chris Lattnerc6f41812005-05-12 23:06:28 +0000379//===----------------------------------------------------------------------===//
380// Fast Calling Convention implementation
381//===----------------------------------------------------------------------===//
382//
383// The X86 'fast' calling convention passes up to two integer arguments in
384// registers (an appropriate portion of EAX/EDX), passes arguments in C order,
385// and requires that the callee pop its arguments off the stack (allowing proper
386// tail calls), and has the same return value conventions as C calling convs.
387//
388// Note that this can be enhanced in the future to pass fp vals in registers
389// (when we have a global fp allocator) and do other tricks.
390//
Chris Lattner63602fb2005-05-13 07:38:09 +0000391
392/// AddLiveIn - This helper function adds the specified physical register to the
393/// MachineFunction as a live in value. It also creates a corresponding virtual
394/// register for it.
395static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg,
396 TargetRegisterClass *RC) {
397 assert(RC->contains(PReg) && "Not the correct regclass!");
398 unsigned VReg = MF.getSSARegMap()->createVirtualRegister(RC);
399 MF.addLiveIn(PReg, VReg);
400 return VReg;
401}
402
403
Chris Lattnerc6f41812005-05-12 23:06:28 +0000404std::vector<SDOperand>
405X86TargetLowering::LowerFastCCArguments(Function &F, SelectionDAG &DAG) {
406 std::vector<SDOperand> ArgValues;
407
408 MachineFunction &MF = DAG.getMachineFunction();
409 MachineFrameInfo *MFI = MF.getFrameInfo();
410
411 // Add DAG nodes to load the arguments... On entry to a function the stack
412 // frame looks like this:
413 //
414 // [ESP] -- return address
415 // [ESP + 4] -- first nonreg argument (leftmost lexically)
416 // [ESP + 8] -- second nonreg argument, if first argument is 4 bytes in size
417 // ...
418 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
419
420 // Keep track of the number of integer regs passed so far. This can be either
421 // 0 (neither EAX or EDX used), 1 (EAX is used) or 2 (EAX and EDX are both
422 // used).
423 unsigned NumIntRegs = 0;
424
425 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
426 MVT::ValueType ObjectVT = getValueType(I->getType());
427 unsigned ArgIncrement = 4;
428 unsigned ObjSize = 0;
429 SDOperand ArgValue;
430
431 switch (ObjectVT) {
432 default: assert(0 && "Unhandled argument type!");
433 case MVT::i1:
434 case MVT::i8:
435 if (NumIntRegs < 2) {
436 if (!I->use_empty()) {
Chris Lattner63602fb2005-05-13 07:38:09 +0000437 unsigned VReg = AddLiveIn(MF, NumIntRegs ? X86::DL : X86::AL,
438 X86::R8RegisterClass);
439 ArgValue = DAG.getCopyFromReg(VReg, MVT::i8, DAG.getRoot());
Chris Lattnerc6f41812005-05-12 23:06:28 +0000440 DAG.setRoot(ArgValue.getValue(1));
441 }
442 ++NumIntRegs;
443 break;
444 }
445
446 ObjSize = 1;
447 break;
448 case MVT::i16:
449 if (NumIntRegs < 2) {
450 if (!I->use_empty()) {
Chris Lattner63602fb2005-05-13 07:38:09 +0000451 unsigned VReg = AddLiveIn(MF, NumIntRegs ? X86::DX : X86::AX,
452 X86::R16RegisterClass);
453 ArgValue = DAG.getCopyFromReg(VReg, MVT::i16, DAG.getRoot());
Chris Lattnerc6f41812005-05-12 23:06:28 +0000454 DAG.setRoot(ArgValue.getValue(1));
455 }
456 ++NumIntRegs;
457 break;
458 }
459 ObjSize = 2;
460 break;
461 case MVT::i32:
462 if (NumIntRegs < 2) {
463 if (!I->use_empty()) {
Chris Lattner63602fb2005-05-13 07:38:09 +0000464 unsigned VReg = AddLiveIn(MF,NumIntRegs ? X86::EDX : X86::EAX,
465 X86::R32RegisterClass);
466 ArgValue = DAG.getCopyFromReg(VReg, MVT::i32, DAG.getRoot());
Chris Lattnerc6f41812005-05-12 23:06:28 +0000467 DAG.setRoot(ArgValue.getValue(1));
468 }
469 ++NumIntRegs;
470 break;
471 }
472 ObjSize = 4;
473 break;
474 case MVT::i64:
475 if (NumIntRegs == 0) {
476 if (!I->use_empty()) {
Chris Lattner63602fb2005-05-13 07:38:09 +0000477 unsigned BotReg = AddLiveIn(MF, X86::EAX, X86::R32RegisterClass);
478 unsigned TopReg = AddLiveIn(MF, X86::EDX, X86::R32RegisterClass);
Chris Lattnerc6f41812005-05-12 23:06:28 +0000479
Chris Lattner63602fb2005-05-13 07:38:09 +0000480 SDOperand Low=DAG.getCopyFromReg(BotReg, MVT::i32, DAG.getRoot());
481 SDOperand Hi =DAG.getCopyFromReg(TopReg, MVT::i32, Low.getValue(1));
Chris Lattnerc6f41812005-05-12 23:06:28 +0000482 DAG.setRoot(Hi.getValue(1));
483
484 ArgValue = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Low, Hi);
485 }
486 NumIntRegs = 2;
487 break;
488 } else if (NumIntRegs == 1) {
489 if (!I->use_empty()) {
Chris Lattner63602fb2005-05-13 07:38:09 +0000490 unsigned BotReg = AddLiveIn(MF, X86::EDX, X86::R32RegisterClass);
491 SDOperand Low = DAG.getCopyFromReg(BotReg, MVT::i32, DAG.getRoot());
Chris Lattnerc6f41812005-05-12 23:06:28 +0000492 DAG.setRoot(Low.getValue(1));
493
494 // Load the high part from memory.
495 // Create the frame index object for this incoming parameter...
496 int FI = MFI->CreateFixedObject(4, ArgOffset);
497 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
498 SDOperand Hi = DAG.getLoad(MVT::i32, DAG.getEntryNode(), FIN,
499 DAG.getSrcValue(NULL));
500 ArgValue = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Low, Hi);
501 }
502 ArgOffset += 4;
503 NumIntRegs = 2;
504 break;
505 }
506 ObjSize = ArgIncrement = 8;
507 break;
508 case MVT::f32: ObjSize = 4; break;
509 case MVT::f64: ObjSize = ArgIncrement = 8; break;
510 }
511
512 // Don't codegen dead arguments. FIXME: remove this check when we can nuke
513 // dead loads.
514 if (ObjSize && !I->use_empty()) {
515 // Create the frame index object for this incoming parameter...
516 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
517
518 // Create the SelectionDAG nodes corresponding to a load from this
519 // parameter.
520 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
521
522 ArgValue = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
523 DAG.getSrcValue(NULL));
524 } else if (ArgValue.Val == 0) {
525 if (MVT::isInteger(ObjectVT))
526 ArgValue = DAG.getConstant(0, ObjectVT);
527 else
528 ArgValue = DAG.getConstantFP(0, ObjectVT);
529 }
530 ArgValues.push_back(ArgValue);
531
532 if (ObjSize)
533 ArgOffset += ArgIncrement; // Move on to the next argument.
534 }
535
Chris Lattner3648c672005-05-13 21:44:04 +0000536 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs.
537 ReturnAddrIndex = 0; // No return address slot generated yet.
538 BytesToPopOnReturn = ArgOffset; // Callee pops all stack arguments.
Chris Lattnerc6f41812005-05-12 23:06:28 +0000539
540 // Finally, inform the code generator which regs we return values in.
541 switch (getValueType(F.getReturnType())) {
542 default: assert(0 && "Unknown type!");
543 case MVT::isVoid: break;
544 case MVT::i1:
545 case MVT::i8:
546 case MVT::i16:
547 case MVT::i32:
548 MF.addLiveOut(X86::EAX);
549 break;
550 case MVT::i64:
551 MF.addLiveOut(X86::EAX);
552 MF.addLiveOut(X86::EDX);
553 break;
554 case MVT::f32:
555 case MVT::f64:
556 MF.addLiveOut(X86::ST0);
557 break;
558 }
559 return ArgValues;
560}
561
562std::pair<SDOperand, SDOperand>
563X86TargetLowering::LowerFastCCCallTo(SDOperand Chain, const Type *RetTy,
Chris Lattner2e7714a2005-05-13 20:29:13 +0000564 bool isTailCall, SDOperand Callee,
Chris Lattnerc6f41812005-05-12 23:06:28 +0000565 ArgListTy &Args, SelectionDAG &DAG) {
566 // Count how many bytes are to be pushed on the stack.
567 unsigned NumBytes = 0;
568
569 // Keep track of the number of integer regs passed so far. This can be either
570 // 0 (neither EAX or EDX used), 1 (EAX is used) or 2 (EAX and EDX are both
571 // used).
572 unsigned NumIntRegs = 0;
573
574 for (unsigned i = 0, e = Args.size(); i != e; ++i)
575 switch (getValueType(Args[i].second)) {
576 default: assert(0 && "Unknown value type!");
577 case MVT::i1:
578 case MVT::i8:
579 case MVT::i16:
580 case MVT::i32:
581 if (NumIntRegs < 2) {
582 ++NumIntRegs;
583 break;
584 }
585 // fall through
586 case MVT::f32:
587 NumBytes += 4;
588 break;
589 case MVT::i64:
590 if (NumIntRegs == 0) {
591 NumIntRegs = 2;
592 break;
593 } else if (NumIntRegs == 1) {
594 NumIntRegs = 2;
595 NumBytes += 4;
596 break;
597 }
598
599 // fall through
600 case MVT::f64:
601 NumBytes += 8;
602 break;
603 }
604
Chris Lattner16cd04d2005-05-12 23:24:06 +0000605 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
Chris Lattnerc6f41812005-05-12 23:06:28 +0000606 DAG.getConstant(NumBytes, getPointerTy()));
607
608 // Arguments go on the stack in reverse order, as specified by the ABI.
609 unsigned ArgOffset = 0;
610 SDOperand StackPtr = DAG.getCopyFromReg(X86::ESP, MVT::i32,
611 DAG.getEntryNode());
612 NumIntRegs = 0;
613 std::vector<SDOperand> Stores;
614 std::vector<SDOperand> RegValuesToPass;
615 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
616 switch (getValueType(Args[i].second)) {
617 default: assert(0 && "Unexpected ValueType for argument!");
618 case MVT::i1:
619 case MVT::i8:
620 case MVT::i16:
621 case MVT::i32:
622 if (NumIntRegs < 2) {
623 RegValuesToPass.push_back(Args[i].first);
624 ++NumIntRegs;
625 break;
626 }
627 // Fall through
628 case MVT::f32: {
629 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
630 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
631 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
632 Args[i].first, PtrOff,
633 DAG.getSrcValue(NULL)));
634 ArgOffset += 4;
635 break;
636 }
637 case MVT::i64:
638 if (NumIntRegs < 2) { // Can pass part of it in regs?
639 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
640 Args[i].first, DAG.getConstant(1, MVT::i32));
641 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
642 Args[i].first, DAG.getConstant(0, MVT::i32));
643 RegValuesToPass.push_back(Lo);
644 ++NumIntRegs;
645 if (NumIntRegs < 2) { // Pass both parts in regs?
646 RegValuesToPass.push_back(Hi);
647 ++NumIntRegs;
648 } else {
649 // Pass the high part in memory.
650 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
651 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
652 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
653 Args[i].first, PtrOff,
654 DAG.getSrcValue(NULL)));
655 ArgOffset += 4;
656 }
657 break;
658 }
659 // Fall through
660 case MVT::f64:
661 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
662 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
663 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
664 Args[i].first, PtrOff,
665 DAG.getSrcValue(NULL)));
666 ArgOffset += 8;
667 break;
668 }
669 }
670 if (!Stores.empty())
671 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, Stores);
672
673 std::vector<MVT::ValueType> RetVals;
674 MVT::ValueType RetTyVT = getValueType(RetTy);
675 if (RetTyVT != MVT::isVoid)
676 RetVals.push_back(RetTyVT);
677 RetVals.push_back(MVT::Other);
678
679 SDOperand TheCall = SDOperand(DAG.getCall(RetVals, Chain, Callee,
Chris Lattner2e7714a2005-05-13 20:29:13 +0000680 RegValuesToPass, isTailCall), 0);
Chris Lattnerc6f41812005-05-12 23:06:28 +0000681 Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
Chris Lattner16cd04d2005-05-12 23:24:06 +0000682 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain,
Chris Lattneradf6a962005-05-13 18:50:42 +0000683 DAG.getConstant(NumBytes, getPointerTy()),
684 // The callee pops the arguments off the stack.
685 DAG.getConstant(ArgOffset, getPointerTy()));
Chris Lattnerc6f41812005-05-12 23:06:28 +0000686 return std::make_pair(TheCall, Chain);
687}
688
689
690
Chris Lattner14824582005-01-09 00:01:27 +0000691
692std::pair<SDOperand, SDOperand> X86TargetLowering::
693LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
694 SelectionDAG &DAG) {
695 SDOperand Result;
696 if (Depth) // Depths > 0 not supported yet!
697 Result = DAG.getConstant(0, getPointerTy());
698 else {
699 if (ReturnAddrIndex == 0) {
700 // Set up a frame object for the return address.
701 MachineFunction &MF = DAG.getMachineFunction();
702 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(4, -4);
703 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000704
Chris Lattner14824582005-01-09 00:01:27 +0000705 SDOperand RetAddrFI = DAG.getFrameIndex(ReturnAddrIndex, MVT::i32);
706
707 if (!isFrameAddress)
708 // Just load the return address
Chris Lattnerc6f41812005-05-12 23:06:28 +0000709 Result = DAG.getLoad(MVT::i32, DAG.getEntryNode(), RetAddrFI,
710 DAG.getSrcValue(NULL));
Chris Lattner14824582005-01-09 00:01:27 +0000711 else
712 Result = DAG.getNode(ISD::SUB, MVT::i32, RetAddrFI,
713 DAG.getConstant(4, MVT::i32));
714 }
715 return std::make_pair(Result, Chain);
716}
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000717
718
Chris Lattner98a8ba02005-01-18 01:06:26 +0000719namespace {
720 /// X86ISelAddressMode - This corresponds to X86AddressMode, but uses
721 /// SDOperand's instead of register numbers for the leaves of the matched
722 /// tree.
723 struct X86ISelAddressMode {
724 enum {
725 RegBase,
726 FrameIndexBase,
727 } BaseType;
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000728
Chris Lattner98a8ba02005-01-18 01:06:26 +0000729 struct { // This is really a union, discriminated by BaseType!
730 SDOperand Reg;
731 int FrameIndex;
732 } Base;
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000733
Chris Lattner98a8ba02005-01-18 01:06:26 +0000734 unsigned Scale;
735 SDOperand IndexReg;
736 unsigned Disp;
737 GlobalValue *GV;
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000738
Chris Lattner98a8ba02005-01-18 01:06:26 +0000739 X86ISelAddressMode()
740 : BaseType(RegBase), Scale(1), IndexReg(), Disp(), GV(0) {
741 }
742 };
743}
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000744
745
746namespace {
747 Statistic<>
748 NumFPKill("x86-codegen", "Number of FP_REG_KILL instructions added");
749
750 //===--------------------------------------------------------------------===//
751 /// ISel - X86 specific code to select X86 machine instructions for
752 /// SelectionDAG operations.
753 ///
754 class ISel : public SelectionDAGISel {
755 /// ContainsFPCode - Every instruction we select that uses or defines a FP
756 /// register should set this to true.
757 bool ContainsFPCode;
758
759 /// X86Lowering - This object fully describes how to lower LLVM code to an
760 /// X86-specific SelectionDAG.
761 X86TargetLowering X86Lowering;
762
Chris Lattner11333092005-01-11 03:11:44 +0000763 /// RegPressureMap - This keeps an approximate count of the number of
764 /// registers required to evaluate each node in the graph.
765 std::map<SDNode*, unsigned> RegPressureMap;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000766
767 /// ExprMap - As shared expressions are codegen'd, we keep track of which
768 /// vreg the value is produced in, so we only emit one copy of each compiled
769 /// tree.
770 std::map<SDOperand, unsigned> ExprMap;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000771
772 public:
773 ISel(TargetMachine &TM) : SelectionDAGISel(X86Lowering), X86Lowering(TM) {
774 }
775
Chris Lattner67b1c3c2005-01-21 21:35:14 +0000776 virtual const char *getPassName() const {
777 return "X86 Pattern Instruction Selection";
778 }
779
Chris Lattner11333092005-01-11 03:11:44 +0000780 unsigned getRegPressure(SDOperand O) {
781 return RegPressureMap[O.Val];
782 }
783 unsigned ComputeRegPressure(SDOperand O);
784
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000785 /// InstructionSelectBasicBlock - This callback is invoked by
786 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
Chris Lattner7dbcb752005-01-12 04:21:28 +0000787 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000788
Chris Lattner63602fb2005-05-13 07:38:09 +0000789 virtual void EmitFunctionEntryCode(Function &Fn, MachineFunction &MF);
790
Chris Lattner44129b52005-01-25 20:03:11 +0000791 bool isFoldableLoad(SDOperand Op, SDOperand OtherOp,
792 bool FloatPromoteOk = false);
Chris Lattnera5ade062005-01-11 21:19:59 +0000793 void EmitFoldedLoad(SDOperand Op, X86AddressMode &AM);
Chris Lattnere10269b2005-01-17 19:25:26 +0000794 bool TryToFoldLoadOpStore(SDNode *Node);
Chris Lattnera5ade062005-01-11 21:19:59 +0000795
Chris Lattner30ea1e92005-01-19 07:37:26 +0000796 bool EmitOrOpOp(SDOperand Op1, SDOperand Op2, unsigned DestReg);
Chris Lattnercb1aa8d2005-01-17 01:34:14 +0000797 void EmitCMP(SDOperand LHS, SDOperand RHS, bool isOnlyUse);
Chris Lattner6c07aee2005-01-11 04:06:27 +0000798 bool EmitBranchCC(MachineBasicBlock *Dest, SDOperand Chain, SDOperand Cond);
Chris Lattner24aad1b2005-01-10 22:10:13 +0000799 void EmitSelectCC(SDOperand Cond, MVT::ValueType SVT,
800 unsigned RTrue, unsigned RFalse, unsigned RDest);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000801 unsigned SelectExpr(SDOperand N);
Chris Lattner98a8ba02005-01-18 01:06:26 +0000802
803 X86AddressMode SelectAddrExprs(const X86ISelAddressMode &IAM);
804 bool MatchAddress(SDOperand N, X86ISelAddressMode &AM);
805 void SelectAddress(SDOperand N, X86AddressMode &AM);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000806 void Select(SDOperand N);
807 };
808}
809
Chris Lattner6415bb42005-05-10 03:53:18 +0000810/// EmitSpecialCodeForMain - Emit any code that needs to be executed only in
811/// the main function.
812static void EmitSpecialCodeForMain(MachineBasicBlock *BB,
813 MachineFrameInfo *MFI) {
814 // Switch the FPU to 64-bit precision mode for better compatibility and speed.
815 int CWFrameIdx = MFI->CreateStackObject(2, 2);
816 addFrameReference(BuildMI(BB, X86::FNSTCW16m, 4), CWFrameIdx);
817
818 // Set the high part to be 64-bit precision.
819 addFrameReference(BuildMI(BB, X86::MOV8mi, 5),
820 CWFrameIdx, 1).addImm(2);
821
822 // Reload the modified control word now.
823 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
824}
825
Chris Lattner63602fb2005-05-13 07:38:09 +0000826void ISel::EmitFunctionEntryCode(Function &Fn, MachineFunction &MF) {
827 // If this function has live-in values, emit the copies from pregs to vregs at
828 // the top of the function, before anything else.
829 MachineBasicBlock *BB = MF.begin();
830 if (MF.livein_begin() != MF.livein_end()) {
831 SSARegMap *RegMap = MF.getSSARegMap();
832 for (MachineFunction::livein_iterator LI = MF.livein_begin(),
833 E = MF.livein_end(); LI != E; ++LI) {
834 const TargetRegisterClass *RC = RegMap->getRegClass(LI->second);
835 if (RC == X86::R8RegisterClass) {
836 BuildMI(BB, X86::MOV8rr, 1, LI->second).addReg(LI->first);
837 } else if (RC == X86::R16RegisterClass) {
838 BuildMI(BB, X86::MOV16rr, 1, LI->second).addReg(LI->first);
839 } else if (RC == X86::R32RegisterClass) {
840 BuildMI(BB, X86::MOV32rr, 1, LI->second).addReg(LI->first);
841 } else if (RC == X86::RFPRegisterClass) {
842 BuildMI(BB, X86::FpMOV, 1, LI->second).addReg(LI->first);
843 } else {
844 assert(0 && "Unknown regclass!");
845 }
846 }
847 }
848
849
850 // If this is main, emit special code for main.
851 if (Fn.hasExternalLinkage() && Fn.getName() == "main")
852 EmitSpecialCodeForMain(BB, MF.getFrameInfo());
853}
854
855
Chris Lattner7dbcb752005-01-12 04:21:28 +0000856/// InstructionSelectBasicBlock - This callback is invoked by SelectionDAGISel
857/// when it has created a SelectionDAG for us to codegen.
858void ISel::InstructionSelectBasicBlock(SelectionDAG &DAG) {
859 // While we're doing this, keep track of whether we see any FP code for
860 // FP_REG_KILL insertion.
861 ContainsFPCode = false;
Chris Lattner6415bb42005-05-10 03:53:18 +0000862 MachineFunction *MF = BB->getParent();
Chris Lattner7dbcb752005-01-12 04:21:28 +0000863
864 // Scan the PHI nodes that already are inserted into this basic block. If any
865 // of them is a PHI of a floating point value, we need to insert an
866 // FP_REG_KILL.
Chris Lattner6415bb42005-05-10 03:53:18 +0000867 SSARegMap *RegMap = MF->getSSARegMap();
Chris Lattner63602fb2005-05-13 07:38:09 +0000868 if (BB != MF->begin())
869 for (MachineBasicBlock::iterator I = BB->begin(), E = BB->end();
870 I != E; ++I) {
871 assert(I->getOpcode() == X86::PHI &&
872 "Isn't just PHI nodes?");
873 if (RegMap->getRegClass(I->getOperand(0).getReg()) ==
874 X86::RFPRegisterClass) {
875 ContainsFPCode = true;
876 break;
877 }
Chris Lattner7dbcb752005-01-12 04:21:28 +0000878 }
Chris Lattner6415bb42005-05-10 03:53:18 +0000879
Chris Lattner7dbcb752005-01-12 04:21:28 +0000880 // Compute the RegPressureMap, which is an approximation for the number of
881 // registers required to compute each node.
882 ComputeRegPressure(DAG.getRoot());
883
884 // Codegen the basic block.
885 Select(DAG.getRoot());
886
887 // Finally, look at all of the successors of this block. If any contain a PHI
888 // node of FP type, we need to insert an FP_REG_KILL in this block.
889 for (MachineBasicBlock::succ_iterator SI = BB->succ_begin(),
890 E = BB->succ_end(); SI != E && !ContainsFPCode; ++SI)
891 for (MachineBasicBlock::iterator I = (*SI)->begin(), E = (*SI)->end();
892 I != E && I->getOpcode() == X86::PHI; ++I) {
893 if (RegMap->getRegClass(I->getOperand(0).getReg()) ==
894 X86::RFPRegisterClass) {
895 ContainsFPCode = true;
896 break;
897 }
898 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000899
Chris Lattnere3e0f272005-05-09 03:36:39 +0000900 // Final check, check LLVM BB's that are successors to the LLVM BB
901 // corresponding to BB for FP PHI nodes.
902 const BasicBlock *LLVMBB = BB->getBasicBlock();
903 const PHINode *PN;
904 if (!ContainsFPCode)
905 for (succ_const_iterator SI = succ_begin(LLVMBB), E = succ_end(LLVMBB);
906 SI != E && !ContainsFPCode; ++SI)
907 for (BasicBlock::const_iterator II = SI->begin();
908 (PN = dyn_cast<PHINode>(II)); ++II)
909 if (PN->getType()->isFloatingPoint()) {
910 ContainsFPCode = true;
911 break;
912 }
913
914
Chris Lattner7dbcb752005-01-12 04:21:28 +0000915 // Insert FP_REG_KILL instructions into basic blocks that need them. This
916 // only occurs due to the floating point stackifier not being aggressive
917 // enough to handle arbitrary global stackification.
918 //
919 // Currently we insert an FP_REG_KILL instruction into each block that uses or
920 // defines a floating point virtual register.
921 //
922 // When the global register allocators (like linear scan) finally update live
923 // variable analysis, we can keep floating point values in registers across
924 // basic blocks. This will be a huge win, but we are waiting on the global
925 // allocators before we can do this.
926 //
Chris Lattner71df3f82005-03-30 01:10:00 +0000927 if (ContainsFPCode) {
Chris Lattner7dbcb752005-01-12 04:21:28 +0000928 BuildMI(*BB, BB->getFirstTerminator(), X86::FP_REG_KILL, 0);
929 ++NumFPKill;
930 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000931
Chris Lattner7dbcb752005-01-12 04:21:28 +0000932 // Clear state used for selection.
933 ExprMap.clear();
Chris Lattner7dbcb752005-01-12 04:21:28 +0000934 RegPressureMap.clear();
935}
936
937
Chris Lattner11333092005-01-11 03:11:44 +0000938// ComputeRegPressure - Compute the RegPressureMap, which is an approximation
939// for the number of registers required to compute each node. This is basically
940// computing a generalized form of the Sethi-Ullman number for each node.
941unsigned ISel::ComputeRegPressure(SDOperand O) {
942 SDNode *N = O.Val;
943 unsigned &Result = RegPressureMap[N];
944 if (Result) return Result;
945
Chris Lattnera3aa2e22005-01-11 03:37:59 +0000946 // FIXME: Should operations like CALL (which clobber lots o regs) have a
947 // higher fixed cost??
948
Chris Lattnerc4b6a782005-01-11 22:29:12 +0000949 if (N->getNumOperands() == 0) {
950 Result = 1;
951 } else {
952 unsigned MaxRegUse = 0;
953 unsigned NumExtraMaxRegUsers = 0;
954 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
955 unsigned Regs;
956 if (N->getOperand(i).getOpcode() == ISD::Constant)
957 Regs = 0;
958 else
959 Regs = ComputeRegPressure(N->getOperand(i));
960 if (Regs > MaxRegUse) {
961 MaxRegUse = Regs;
962 NumExtraMaxRegUsers = 0;
963 } else if (Regs == MaxRegUse &&
964 N->getOperand(i).getValueType() != MVT::Other) {
965 ++NumExtraMaxRegUsers;
966 }
Chris Lattner11333092005-01-11 03:11:44 +0000967 }
Chris Lattner90d1be72005-01-17 22:56:09 +0000968
969 if (O.getOpcode() != ISD::TokenFactor)
970 Result = MaxRegUse+NumExtraMaxRegUsers;
971 else
Chris Lattner869e0432005-01-17 23:02:13 +0000972 Result = MaxRegUse == 1 ? 0 : MaxRegUse-1;
Chris Lattnerc4b6a782005-01-11 22:29:12 +0000973 }
Chris Lattnerafce4302005-01-12 02:19:06 +0000974
Chris Lattner837caa72005-01-11 23:21:30 +0000975 //std::cerr << " WEIGHT: " << Result << " "; N->dump(); std::cerr << "\n";
Chris Lattnerc4b6a782005-01-11 22:29:12 +0000976 return Result;
Chris Lattner11333092005-01-11 03:11:44 +0000977}
978
Chris Lattnerbf52d492005-01-20 16:50:16 +0000979/// NodeTransitivelyUsesValue - Return true if N or any of its uses uses Op.
980/// The DAG cannot have cycles in it, by definition, so the visited set is not
981/// needed to prevent infinite loops. The DAG CAN, however, have unbounded
982/// reuse, so it prevents exponential cases.
983///
984static bool NodeTransitivelyUsesValue(SDOperand N, SDOperand Op,
985 std::set<SDNode*> &Visited) {
986 if (N == Op) return true; // Found it.
987 SDNode *Node = N.Val;
Chris Lattnerfb0f53f2005-01-21 21:43:02 +0000988 if (Node->getNumOperands() == 0 || // Leaf?
989 Node->getNodeDepth() <= Op.getNodeDepth()) return false; // Can't find it?
Chris Lattnerbf52d492005-01-20 16:50:16 +0000990 if (!Visited.insert(Node).second) return false; // Already visited?
991
992 // Recurse for the first N-1 operands.
993 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i)
994 if (NodeTransitivelyUsesValue(Node->getOperand(i), Op, Visited))
995 return true;
996
997 // Tail recurse for the last operand.
998 return NodeTransitivelyUsesValue(Node->getOperand(0), Op, Visited);
999}
1000
Chris Lattner98a8ba02005-01-18 01:06:26 +00001001X86AddressMode ISel::SelectAddrExprs(const X86ISelAddressMode &IAM) {
1002 X86AddressMode Result;
1003
1004 // If we need to emit two register operands, emit the one with the highest
1005 // register pressure first.
1006 if (IAM.BaseType == X86ISelAddressMode::RegBase &&
1007 IAM.Base.Reg.Val && IAM.IndexReg.Val) {
Chris Lattnerbf52d492005-01-20 16:50:16 +00001008 bool EmitBaseThenIndex;
Chris Lattner98a8ba02005-01-18 01:06:26 +00001009 if (getRegPressure(IAM.Base.Reg) > getRegPressure(IAM.IndexReg)) {
Chris Lattnerbf52d492005-01-20 16:50:16 +00001010 std::set<SDNode*> Visited;
1011 EmitBaseThenIndex = true;
1012 // If Base ends up pointing to Index, we must emit index first. This is
1013 // because of the way we fold loads, we may end up doing bad things with
1014 // the folded add.
1015 if (NodeTransitivelyUsesValue(IAM.Base.Reg, IAM.IndexReg, Visited))
1016 EmitBaseThenIndex = false;
1017 } else {
1018 std::set<SDNode*> Visited;
1019 EmitBaseThenIndex = false;
1020 // If Base ends up pointing to Index, we must emit index first. This is
1021 // because of the way we fold loads, we may end up doing bad things with
1022 // the folded add.
1023 if (NodeTransitivelyUsesValue(IAM.IndexReg, IAM.Base.Reg, Visited))
1024 EmitBaseThenIndex = true;
1025 }
1026
1027 if (EmitBaseThenIndex) {
Chris Lattner98a8ba02005-01-18 01:06:26 +00001028 Result.Base.Reg = SelectExpr(IAM.Base.Reg);
1029 Result.IndexReg = SelectExpr(IAM.IndexReg);
1030 } else {
1031 Result.IndexReg = SelectExpr(IAM.IndexReg);
1032 Result.Base.Reg = SelectExpr(IAM.Base.Reg);
1033 }
Chris Lattnerbf52d492005-01-20 16:50:16 +00001034
Chris Lattner98a8ba02005-01-18 01:06:26 +00001035 } else if (IAM.BaseType == X86ISelAddressMode::RegBase && IAM.Base.Reg.Val) {
1036 Result.Base.Reg = SelectExpr(IAM.Base.Reg);
1037 } else if (IAM.IndexReg.Val) {
1038 Result.IndexReg = SelectExpr(IAM.IndexReg);
1039 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001040
Chris Lattner98a8ba02005-01-18 01:06:26 +00001041 switch (IAM.BaseType) {
1042 case X86ISelAddressMode::RegBase:
1043 Result.BaseType = X86AddressMode::RegBase;
1044 break;
1045 case X86ISelAddressMode::FrameIndexBase:
1046 Result.BaseType = X86AddressMode::FrameIndexBase;
1047 Result.Base.FrameIndex = IAM.Base.FrameIndex;
1048 break;
1049 default:
1050 assert(0 && "Unknown base type!");
1051 break;
1052 }
1053 Result.Scale = IAM.Scale;
1054 Result.Disp = IAM.Disp;
1055 Result.GV = IAM.GV;
1056 return Result;
1057}
1058
1059/// SelectAddress - Pattern match the maximal addressing mode for this node and
1060/// emit all of the leaf registers.
1061void ISel::SelectAddress(SDOperand N, X86AddressMode &AM) {
1062 X86ISelAddressMode IAM;
1063 MatchAddress(N, IAM);
1064 AM = SelectAddrExprs(IAM);
1065}
1066
1067/// MatchAddress - Add the specified node to the specified addressing mode,
1068/// returning true if it cannot be done. This just pattern matches for the
1069/// addressing mode, it does not cause any code to be emitted. For that, use
1070/// SelectAddress.
1071bool ISel::MatchAddress(SDOperand N, X86ISelAddressMode &AM) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001072 switch (N.getOpcode()) {
1073 default: break;
1074 case ISD::FrameIndex:
Chris Lattner98a8ba02005-01-18 01:06:26 +00001075 if (AM.BaseType == X86ISelAddressMode::RegBase && AM.Base.Reg.Val == 0) {
1076 AM.BaseType = X86ISelAddressMode::FrameIndexBase;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001077 AM.Base.FrameIndex = cast<FrameIndexSDNode>(N)->getIndex();
1078 return false;
1079 }
1080 break;
1081 case ISD::GlobalAddress:
1082 if (AM.GV == 0) {
1083 AM.GV = cast<GlobalAddressSDNode>(N)->getGlobal();
1084 return false;
1085 }
1086 break;
1087 case ISD::Constant:
1088 AM.Disp += cast<ConstantSDNode>(N)->getValue();
1089 return false;
1090 case ISD::SHL:
Chris Lattner636e79a2005-01-13 05:53:16 +00001091 // We might have folded the load into this shift, so don't regen the value
1092 // if so.
1093 if (ExprMap.count(N)) break;
1094
Chris Lattner98a8ba02005-01-18 01:06:26 +00001095 if (AM.IndexReg.Val == 0 && AM.Scale == 1)
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001096 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1))) {
1097 unsigned Val = CN->getValue();
1098 if (Val == 1 || Val == 2 || Val == 3) {
1099 AM.Scale = 1 << Val;
Chris Lattner51a26342005-01-11 06:36:20 +00001100 SDOperand ShVal = N.Val->getOperand(0);
1101
1102 // Okay, we know that we have a scale by now. However, if the scaled
1103 // value is an add of something and a constant, we can fold the
1104 // constant into the disp field here.
Chris Lattner811482a2005-01-18 04:18:32 +00001105 if (ShVal.Val->getOpcode() == ISD::ADD && ShVal.hasOneUse() &&
Chris Lattner51a26342005-01-11 06:36:20 +00001106 isa<ConstantSDNode>(ShVal.Val->getOperand(1))) {
Chris Lattner98a8ba02005-01-18 01:06:26 +00001107 AM.IndexReg = ShVal.Val->getOperand(0);
Chris Lattner51a26342005-01-11 06:36:20 +00001108 ConstantSDNode *AddVal =
1109 cast<ConstantSDNode>(ShVal.Val->getOperand(1));
1110 AM.Disp += AddVal->getValue() << Val;
Chris Lattner636e79a2005-01-13 05:53:16 +00001111 } else {
Chris Lattner98a8ba02005-01-18 01:06:26 +00001112 AM.IndexReg = ShVal;
Chris Lattner51a26342005-01-11 06:36:20 +00001113 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001114 return false;
1115 }
1116 }
1117 break;
Chris Lattner947d5442005-01-11 19:37:02 +00001118 case ISD::MUL:
Chris Lattner636e79a2005-01-13 05:53:16 +00001119 // We might have folded the load into this mul, so don't regen the value if
1120 // so.
1121 if (ExprMap.count(N)) break;
1122
Chris Lattner947d5442005-01-11 19:37:02 +00001123 // X*[3,5,9] -> X+X*[2,4,8]
Chris Lattner98a8ba02005-01-18 01:06:26 +00001124 if (AM.IndexReg.Val == 0 && AM.BaseType == X86ISelAddressMode::RegBase &&
1125 AM.Base.Reg.Val == 0)
Chris Lattner947d5442005-01-11 19:37:02 +00001126 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1)))
1127 if (CN->getValue() == 3 || CN->getValue() == 5 || CN->getValue() == 9) {
1128 AM.Scale = unsigned(CN->getValue())-1;
1129
1130 SDOperand MulVal = N.Val->getOperand(0);
Chris Lattner98a8ba02005-01-18 01:06:26 +00001131 SDOperand Reg;
Chris Lattner947d5442005-01-11 19:37:02 +00001132
1133 // Okay, we know that we have a scale by now. However, if the scaled
1134 // value is an add of something and a constant, we can fold the
1135 // constant into the disp field here.
Chris Lattner811482a2005-01-18 04:18:32 +00001136 if (MulVal.Val->getOpcode() == ISD::ADD && MulVal.hasOneUse() &&
Chris Lattner947d5442005-01-11 19:37:02 +00001137 isa<ConstantSDNode>(MulVal.Val->getOperand(1))) {
Chris Lattner98a8ba02005-01-18 01:06:26 +00001138 Reg = MulVal.Val->getOperand(0);
Chris Lattner947d5442005-01-11 19:37:02 +00001139 ConstantSDNode *AddVal =
1140 cast<ConstantSDNode>(MulVal.Val->getOperand(1));
1141 AM.Disp += AddVal->getValue() * CN->getValue();
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001142 } else {
Chris Lattner98a8ba02005-01-18 01:06:26 +00001143 Reg = N.Val->getOperand(0);
Chris Lattner947d5442005-01-11 19:37:02 +00001144 }
1145
1146 AM.IndexReg = AM.Base.Reg = Reg;
1147 return false;
1148 }
1149 break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001150
1151 case ISD::ADD: {
Chris Lattner636e79a2005-01-13 05:53:16 +00001152 // We might have folded the load into this mul, so don't regen the value if
1153 // so.
1154 if (ExprMap.count(N)) break;
1155
Chris Lattner98a8ba02005-01-18 01:06:26 +00001156 X86ISelAddressMode Backup = AM;
1157 if (!MatchAddress(N.Val->getOperand(0), AM) &&
1158 !MatchAddress(N.Val->getOperand(1), AM))
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001159 return false;
1160 AM = Backup;
Chris Lattner98a8ba02005-01-18 01:06:26 +00001161 if (!MatchAddress(N.Val->getOperand(1), AM) &&
1162 !MatchAddress(N.Val->getOperand(0), AM))
Chris Lattner9bbd9922005-01-12 18:08:53 +00001163 return false;
1164 AM = Backup;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001165 break;
1166 }
1167 }
1168
Chris Lattnera95589b2005-01-11 04:40:19 +00001169 // Is the base register already occupied?
Chris Lattner98a8ba02005-01-18 01:06:26 +00001170 if (AM.BaseType != X86ISelAddressMode::RegBase || AM.Base.Reg.Val) {
Chris Lattnera95589b2005-01-11 04:40:19 +00001171 // If so, check to see if the scale index register is set.
Chris Lattner98a8ba02005-01-18 01:06:26 +00001172 if (AM.IndexReg.Val == 0) {
1173 AM.IndexReg = N;
Chris Lattnera95589b2005-01-11 04:40:19 +00001174 AM.Scale = 1;
1175 return false;
1176 }
1177
1178 // Otherwise, we cannot select it.
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001179 return true;
Chris Lattnera95589b2005-01-11 04:40:19 +00001180 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001181
1182 // Default, generate it as a register.
Chris Lattner98a8ba02005-01-18 01:06:26 +00001183 AM.BaseType = X86ISelAddressMode::RegBase;
1184 AM.Base.Reg = N;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001185 return false;
1186}
1187
1188/// Emit2SetCCsAndLogical - Emit the following sequence of instructions,
1189/// assuming that the temporary registers are in the 8-bit register class.
1190///
1191/// Tmp1 = setcc1
1192/// Tmp2 = setcc2
1193/// DestReg = logicalop Tmp1, Tmp2
1194///
1195static void Emit2SetCCsAndLogical(MachineBasicBlock *BB, unsigned SetCC1,
1196 unsigned SetCC2, unsigned LogicalOp,
1197 unsigned DestReg) {
1198 SSARegMap *RegMap = BB->getParent()->getSSARegMap();
1199 unsigned Tmp1 = RegMap->createVirtualRegister(X86::R8RegisterClass);
1200 unsigned Tmp2 = RegMap->createVirtualRegister(X86::R8RegisterClass);
1201 BuildMI(BB, SetCC1, 0, Tmp1);
1202 BuildMI(BB, SetCC2, 0, Tmp2);
1203 BuildMI(BB, LogicalOp, 2, DestReg).addReg(Tmp1).addReg(Tmp2);
1204}
1205
1206/// EmitSetCC - Emit the code to set the specified 8-bit register to 1 if the
1207/// condition codes match the specified SetCCOpcode. Note that some conditions
1208/// require multiple instructions to generate the correct value.
1209static void EmitSetCC(MachineBasicBlock *BB, unsigned DestReg,
1210 ISD::CondCode SetCCOpcode, bool isFP) {
1211 unsigned Opc;
1212 if (!isFP) {
1213 switch (SetCCOpcode) {
1214 default: assert(0 && "Illegal integer SetCC!");
1215 case ISD::SETEQ: Opc = X86::SETEr; break;
1216 case ISD::SETGT: Opc = X86::SETGr; break;
1217 case ISD::SETGE: Opc = X86::SETGEr; break;
1218 case ISD::SETLT: Opc = X86::SETLr; break;
1219 case ISD::SETLE: Opc = X86::SETLEr; break;
1220 case ISD::SETNE: Opc = X86::SETNEr; break;
1221 case ISD::SETULT: Opc = X86::SETBr; break;
1222 case ISD::SETUGT: Opc = X86::SETAr; break;
1223 case ISD::SETULE: Opc = X86::SETBEr; break;
1224 case ISD::SETUGE: Opc = X86::SETAEr; break;
1225 }
1226 } else {
1227 // On a floating point condition, the flags are set as follows:
1228 // ZF PF CF op
1229 // 0 | 0 | 0 | X > Y
1230 // 0 | 0 | 1 | X < Y
1231 // 1 | 0 | 0 | X == Y
1232 // 1 | 1 | 1 | unordered
1233 //
1234 switch (SetCCOpcode) {
1235 default: assert(0 && "Invalid FP setcc!");
1236 case ISD::SETUEQ:
1237 case ISD::SETEQ:
1238 Opc = X86::SETEr; // True if ZF = 1
1239 break;
1240 case ISD::SETOGT:
1241 case ISD::SETGT:
1242 Opc = X86::SETAr; // True if CF = 0 and ZF = 0
1243 break;
1244 case ISD::SETOGE:
1245 case ISD::SETGE:
1246 Opc = X86::SETAEr; // True if CF = 0
1247 break;
1248 case ISD::SETULT:
1249 case ISD::SETLT:
1250 Opc = X86::SETBr; // True if CF = 1
1251 break;
1252 case ISD::SETULE:
1253 case ISD::SETLE:
1254 Opc = X86::SETBEr; // True if CF = 1 or ZF = 1
1255 break;
1256 case ISD::SETONE:
1257 case ISD::SETNE:
1258 Opc = X86::SETNEr; // True if ZF = 0
1259 break;
1260 case ISD::SETUO:
1261 Opc = X86::SETPr; // True if PF = 1
1262 break;
1263 case ISD::SETO:
1264 Opc = X86::SETNPr; // True if PF = 0
1265 break;
1266 case ISD::SETOEQ: // !PF & ZF
1267 Emit2SetCCsAndLogical(BB, X86::SETNPr, X86::SETEr, X86::AND8rr, DestReg);
1268 return;
1269 case ISD::SETOLT: // !PF & CF
1270 Emit2SetCCsAndLogical(BB, X86::SETNPr, X86::SETBr, X86::AND8rr, DestReg);
1271 return;
1272 case ISD::SETOLE: // !PF & (CF || ZF)
1273 Emit2SetCCsAndLogical(BB, X86::SETNPr, X86::SETBEr, X86::AND8rr, DestReg);
1274 return;
1275 case ISD::SETUGT: // PF | (!ZF & !CF)
1276 Emit2SetCCsAndLogical(BB, X86::SETPr, X86::SETAr, X86::OR8rr, DestReg);
1277 return;
1278 case ISD::SETUGE: // PF | !CF
1279 Emit2SetCCsAndLogical(BB, X86::SETPr, X86::SETAEr, X86::OR8rr, DestReg);
1280 return;
1281 case ISD::SETUNE: // PF | !ZF
1282 Emit2SetCCsAndLogical(BB, X86::SETPr, X86::SETNEr, X86::OR8rr, DestReg);
1283 return;
1284 }
1285 }
1286 BuildMI(BB, Opc, 0, DestReg);
1287}
1288
1289
1290/// EmitBranchCC - Emit code into BB that arranges for control to transfer to
1291/// the Dest block if the Cond condition is true. If we cannot fold this
1292/// condition into the branch, return true.
1293///
Chris Lattner6c07aee2005-01-11 04:06:27 +00001294bool ISel::EmitBranchCC(MachineBasicBlock *Dest, SDOperand Chain,
1295 SDOperand Cond) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001296 // FIXME: Evaluate whether it would be good to emit code like (X < Y) | (A >
1297 // B) using two conditional branches instead of one condbr, two setcc's, and
1298 // an or.
1299 if ((Cond.getOpcode() == ISD::OR ||
1300 Cond.getOpcode() == ISD::AND) && Cond.Val->hasOneUse()) {
1301 // And and or set the flags for us, so there is no need to emit a TST of the
1302 // result. It is only safe to do this if there is only a single use of the
1303 // AND/OR though, otherwise we don't know it will be emitted here.
Chris Lattner6c07aee2005-01-11 04:06:27 +00001304 Select(Chain);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001305 SelectExpr(Cond);
1306 BuildMI(BB, X86::JNE, 1).addMBB(Dest);
1307 return false;
1308 }
1309
1310 // Codegen br not C -> JE.
1311 if (Cond.getOpcode() == ISD::XOR)
1312 if (ConstantSDNode *NC = dyn_cast<ConstantSDNode>(Cond.Val->getOperand(1)))
1313 if (NC->isAllOnesValue()) {
Chris Lattner6c07aee2005-01-11 04:06:27 +00001314 unsigned CondR;
1315 if (getRegPressure(Chain) > getRegPressure(Cond)) {
1316 Select(Chain);
1317 CondR = SelectExpr(Cond.Val->getOperand(0));
1318 } else {
1319 CondR = SelectExpr(Cond.Val->getOperand(0));
1320 Select(Chain);
1321 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001322 BuildMI(BB, X86::TEST8rr, 2).addReg(CondR).addReg(CondR);
1323 BuildMI(BB, X86::JE, 1).addMBB(Dest);
1324 return false;
1325 }
1326
1327 SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Cond);
1328 if (SetCC == 0)
1329 return true; // Can only handle simple setcc's so far.
1330
1331 unsigned Opc;
1332
1333 // Handle integer conditions first.
1334 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
1335 switch (SetCC->getCondition()) {
1336 default: assert(0 && "Illegal integer SetCC!");
1337 case ISD::SETEQ: Opc = X86::JE; break;
1338 case ISD::SETGT: Opc = X86::JG; break;
1339 case ISD::SETGE: Opc = X86::JGE; break;
1340 case ISD::SETLT: Opc = X86::JL; break;
1341 case ISD::SETLE: Opc = X86::JLE; break;
1342 case ISD::SETNE: Opc = X86::JNE; break;
1343 case ISD::SETULT: Opc = X86::JB; break;
1344 case ISD::SETUGT: Opc = X86::JA; break;
1345 case ISD::SETULE: Opc = X86::JBE; break;
1346 case ISD::SETUGE: Opc = X86::JAE; break;
1347 }
Chris Lattner6c07aee2005-01-11 04:06:27 +00001348 Select(Chain);
Chris Lattnercb1aa8d2005-01-17 01:34:14 +00001349 EmitCMP(SetCC->getOperand(0), SetCC->getOperand(1), SetCC->hasOneUse());
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001350 BuildMI(BB, Opc, 1).addMBB(Dest);
1351 return false;
1352 }
1353
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001354 unsigned Opc2 = 0; // Second branch if needed.
1355
1356 // On a floating point condition, the flags are set as follows:
1357 // ZF PF CF op
1358 // 0 | 0 | 0 | X > Y
1359 // 0 | 0 | 1 | X < Y
1360 // 1 | 0 | 0 | X == Y
1361 // 1 | 1 | 1 | unordered
1362 //
1363 switch (SetCC->getCondition()) {
1364 default: assert(0 && "Invalid FP setcc!");
1365 case ISD::SETUEQ:
1366 case ISD::SETEQ: Opc = X86::JE; break; // True if ZF = 1
1367 case ISD::SETOGT:
1368 case ISD::SETGT: Opc = X86::JA; break; // True if CF = 0 and ZF = 0
1369 case ISD::SETOGE:
1370 case ISD::SETGE: Opc = X86::JAE; break; // True if CF = 0
1371 case ISD::SETULT:
1372 case ISD::SETLT: Opc = X86::JB; break; // True if CF = 1
1373 case ISD::SETULE:
1374 case ISD::SETLE: Opc = X86::JBE; break; // True if CF = 1 or ZF = 1
1375 case ISD::SETONE:
1376 case ISD::SETNE: Opc = X86::JNE; break; // True if ZF = 0
1377 case ISD::SETUO: Opc = X86::JP; break; // True if PF = 1
1378 case ISD::SETO: Opc = X86::JNP; break; // True if PF = 0
1379 case ISD::SETUGT: // PF = 1 | (ZF = 0 & CF = 0)
1380 Opc = X86::JA; // ZF = 0 & CF = 0
1381 Opc2 = X86::JP; // PF = 1
1382 break;
1383 case ISD::SETUGE: // PF = 1 | CF = 0
1384 Opc = X86::JAE; // CF = 0
1385 Opc2 = X86::JP; // PF = 1
1386 break;
1387 case ISD::SETUNE: // PF = 1 | ZF = 0
1388 Opc = X86::JNE; // ZF = 0
1389 Opc2 = X86::JP; // PF = 1
1390 break;
1391 case ISD::SETOEQ: // PF = 0 & ZF = 1
1392 //X86::JNP, X86::JE
1393 //X86::AND8rr
1394 return true; // FIXME: Emit more efficient code for this branch.
1395 case ISD::SETOLT: // PF = 0 & CF = 1
1396 //X86::JNP, X86::JB
1397 //X86::AND8rr
1398 return true; // FIXME: Emit more efficient code for this branch.
1399 case ISD::SETOLE: // PF = 0 & (CF = 1 || ZF = 1)
1400 //X86::JNP, X86::JBE
1401 //X86::AND8rr
1402 return true; // FIXME: Emit more efficient code for this branch.
1403 }
1404
Chris Lattner6c07aee2005-01-11 04:06:27 +00001405 Select(Chain);
Chris Lattnercb1aa8d2005-01-17 01:34:14 +00001406 EmitCMP(SetCC->getOperand(0), SetCC->getOperand(1), SetCC->hasOneUse());
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001407 BuildMI(BB, Opc, 1).addMBB(Dest);
1408 if (Opc2)
1409 BuildMI(BB, Opc2, 1).addMBB(Dest);
1410 return false;
1411}
1412
Chris Lattner24aad1b2005-01-10 22:10:13 +00001413/// EmitSelectCC - Emit code into BB that performs a select operation between
1414/// the two registers RTrue and RFalse, generating a result into RDest. Return
1415/// true if the fold cannot be performed.
1416///
1417void ISel::EmitSelectCC(SDOperand Cond, MVT::ValueType SVT,
1418 unsigned RTrue, unsigned RFalse, unsigned RDest) {
1419 enum Condition {
1420 EQ, NE, LT, LE, GT, GE, B, BE, A, AE, P, NP,
1421 NOT_SET
1422 } CondCode = NOT_SET;
1423
1424 static const unsigned CMOVTAB16[] = {
1425 X86::CMOVE16rr, X86::CMOVNE16rr, X86::CMOVL16rr, X86::CMOVLE16rr,
1426 X86::CMOVG16rr, X86::CMOVGE16rr, X86::CMOVB16rr, X86::CMOVBE16rr,
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001427 X86::CMOVA16rr, X86::CMOVAE16rr, X86::CMOVP16rr, X86::CMOVNP16rr,
Chris Lattner24aad1b2005-01-10 22:10:13 +00001428 };
1429 static const unsigned CMOVTAB32[] = {
1430 X86::CMOVE32rr, X86::CMOVNE32rr, X86::CMOVL32rr, X86::CMOVLE32rr,
1431 X86::CMOVG32rr, X86::CMOVGE32rr, X86::CMOVB32rr, X86::CMOVBE32rr,
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001432 X86::CMOVA32rr, X86::CMOVAE32rr, X86::CMOVP32rr, X86::CMOVNP32rr,
Chris Lattner24aad1b2005-01-10 22:10:13 +00001433 };
1434 static const unsigned CMOVTABFP[] = {
1435 X86::FCMOVE , X86::FCMOVNE, /*missing*/0, /*missing*/0,
1436 /*missing*/0, /*missing*/0, X86::FCMOVB , X86::FCMOVBE,
1437 X86::FCMOVA , X86::FCMOVAE, X86::FCMOVP , X86::FCMOVNP
1438 };
1439
1440 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Cond)) {
1441 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
1442 switch (SetCC->getCondition()) {
1443 default: assert(0 && "Unknown integer comparison!");
1444 case ISD::SETEQ: CondCode = EQ; break;
1445 case ISD::SETGT: CondCode = GT; break;
1446 case ISD::SETGE: CondCode = GE; break;
1447 case ISD::SETLT: CondCode = LT; break;
1448 case ISD::SETLE: CondCode = LE; break;
1449 case ISD::SETNE: CondCode = NE; break;
1450 case ISD::SETULT: CondCode = B; break;
1451 case ISD::SETUGT: CondCode = A; break;
1452 case ISD::SETULE: CondCode = BE; break;
1453 case ISD::SETUGE: CondCode = AE; break;
1454 }
1455 } else {
1456 // On a floating point condition, the flags are set as follows:
1457 // ZF PF CF op
1458 // 0 | 0 | 0 | X > Y
1459 // 0 | 0 | 1 | X < Y
1460 // 1 | 0 | 0 | X == Y
1461 // 1 | 1 | 1 | unordered
1462 //
1463 switch (SetCC->getCondition()) {
1464 default: assert(0 && "Unknown FP comparison!");
1465 case ISD::SETUEQ:
1466 case ISD::SETEQ: CondCode = EQ; break; // True if ZF = 1
1467 case ISD::SETOGT:
1468 case ISD::SETGT: CondCode = A; break; // True if CF = 0 and ZF = 0
1469 case ISD::SETOGE:
1470 case ISD::SETGE: CondCode = AE; break; // True if CF = 0
1471 case ISD::SETULT:
1472 case ISD::SETLT: CondCode = B; break; // True if CF = 1
1473 case ISD::SETULE:
1474 case ISD::SETLE: CondCode = BE; break; // True if CF = 1 or ZF = 1
1475 case ISD::SETONE:
1476 case ISD::SETNE: CondCode = NE; break; // True if ZF = 0
1477 case ISD::SETUO: CondCode = P; break; // True if PF = 1
1478 case ISD::SETO: CondCode = NP; break; // True if PF = 0
1479 case ISD::SETUGT: // PF = 1 | (ZF = 0 & CF = 0)
1480 case ISD::SETUGE: // PF = 1 | CF = 0
1481 case ISD::SETUNE: // PF = 1 | ZF = 0
1482 case ISD::SETOEQ: // PF = 0 & ZF = 1
1483 case ISD::SETOLT: // PF = 0 & CF = 1
1484 case ISD::SETOLE: // PF = 0 & (CF = 1 || ZF = 1)
1485 // We cannot emit this comparison as a single cmov.
1486 break;
1487 }
1488 }
1489 }
1490
1491 unsigned Opc = 0;
1492 if (CondCode != NOT_SET) {
1493 switch (SVT) {
1494 default: assert(0 && "Cannot select this type!");
1495 case MVT::i16: Opc = CMOVTAB16[CondCode]; break;
1496 case MVT::i32: Opc = CMOVTAB32[CondCode]; break;
Chris Lattneref7ba072005-01-11 03:50:45 +00001497 case MVT::f64: Opc = CMOVTABFP[CondCode]; break;
Chris Lattner24aad1b2005-01-10 22:10:13 +00001498 }
1499 }
1500
1501 // Finally, if we weren't able to fold this, just emit the condition and test
1502 // it.
1503 if (CondCode == NOT_SET || Opc == 0) {
1504 // Get the condition into the zero flag.
1505 unsigned CondReg = SelectExpr(Cond);
1506 BuildMI(BB, X86::TEST8rr, 2).addReg(CondReg).addReg(CondReg);
1507
1508 switch (SVT) {
1509 default: assert(0 && "Cannot select this type!");
1510 case MVT::i16: Opc = X86::CMOVE16rr; break;
1511 case MVT::i32: Opc = X86::CMOVE32rr; break;
Chris Lattneref7ba072005-01-11 03:50:45 +00001512 case MVT::f64: Opc = X86::FCMOVE; break;
Chris Lattner24aad1b2005-01-10 22:10:13 +00001513 }
1514 } else {
1515 // FIXME: CMP R, 0 -> TEST R, R
Chris Lattnercb1aa8d2005-01-17 01:34:14 +00001516 EmitCMP(Cond.getOperand(0), Cond.getOperand(1), Cond.Val->hasOneUse());
Chris Lattnera3aa2e22005-01-11 03:37:59 +00001517 std::swap(RTrue, RFalse);
Chris Lattner24aad1b2005-01-10 22:10:13 +00001518 }
1519 BuildMI(BB, Opc, 2, RDest).addReg(RTrue).addReg(RFalse);
1520}
1521
Chris Lattnercb1aa8d2005-01-17 01:34:14 +00001522void ISel::EmitCMP(SDOperand LHS, SDOperand RHS, bool HasOneUse) {
Chris Lattner11333092005-01-11 03:11:44 +00001523 unsigned Opc;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001524 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(RHS)) {
1525 Opc = 0;
Chris Lattner4ff348b2005-01-17 06:26:58 +00001526 if (HasOneUse && isFoldableLoad(LHS, RHS)) {
Chris Lattneref6806c2005-01-12 02:02:48 +00001527 switch (RHS.getValueType()) {
1528 default: break;
1529 case MVT::i1:
1530 case MVT::i8: Opc = X86::CMP8mi; break;
1531 case MVT::i16: Opc = X86::CMP16mi; break;
1532 case MVT::i32: Opc = X86::CMP32mi; break;
1533 }
1534 if (Opc) {
1535 X86AddressMode AM;
1536 EmitFoldedLoad(LHS, AM);
1537 addFullAddress(BuildMI(BB, Opc, 5), AM).addImm(CN->getValue());
1538 return;
1539 }
1540 }
1541
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001542 switch (RHS.getValueType()) {
1543 default: break;
1544 case MVT::i1:
1545 case MVT::i8: Opc = X86::CMP8ri; break;
1546 case MVT::i16: Opc = X86::CMP16ri; break;
1547 case MVT::i32: Opc = X86::CMP32ri; break;
1548 }
1549 if (Opc) {
Chris Lattner11333092005-01-11 03:11:44 +00001550 unsigned Tmp1 = SelectExpr(LHS);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001551 BuildMI(BB, Opc, 2).addReg(Tmp1).addImm(CN->getValue());
1552 return;
1553 }
Chris Lattner7f2afac2005-01-14 22:37:41 +00001554 } else if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(RHS)) {
1555 if (CN->isExactlyValue(+0.0) ||
1556 CN->isExactlyValue(-0.0)) {
1557 unsigned Reg = SelectExpr(LHS);
1558 BuildMI(BB, X86::FTST, 1).addReg(Reg);
1559 BuildMI(BB, X86::FNSTSW8r, 0);
1560 BuildMI(BB, X86::SAHF, 1);
Chris Lattner7805fa42005-03-17 16:29:26 +00001561 return;
Chris Lattner7f2afac2005-01-14 22:37:41 +00001562 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001563 }
1564
Chris Lattneref6806c2005-01-12 02:02:48 +00001565 Opc = 0;
Chris Lattner4ff348b2005-01-17 06:26:58 +00001566 if (HasOneUse && isFoldableLoad(LHS, RHS)) {
Chris Lattneref6806c2005-01-12 02:02:48 +00001567 switch (RHS.getValueType()) {
1568 default: break;
1569 case MVT::i1:
1570 case MVT::i8: Opc = X86::CMP8mr; break;
1571 case MVT::i16: Opc = X86::CMP16mr; break;
1572 case MVT::i32: Opc = X86::CMP32mr; break;
1573 }
1574 if (Opc) {
1575 X86AddressMode AM;
Chris Lattner636e79a2005-01-13 05:53:16 +00001576 EmitFoldedLoad(LHS, AM);
1577 unsigned Reg = SelectExpr(RHS);
Chris Lattneref6806c2005-01-12 02:02:48 +00001578 addFullAddress(BuildMI(BB, Opc, 5), AM).addReg(Reg);
1579 return;
1580 }
1581 }
1582
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001583 switch (LHS.getValueType()) {
1584 default: assert(0 && "Cannot compare this value!");
1585 case MVT::i1:
1586 case MVT::i8: Opc = X86::CMP8rr; break;
1587 case MVT::i16: Opc = X86::CMP16rr; break;
1588 case MVT::i32: Opc = X86::CMP32rr; break;
Chris Lattneref7ba072005-01-11 03:50:45 +00001589 case MVT::f64: Opc = X86::FUCOMIr; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001590 }
Chris Lattner11333092005-01-11 03:11:44 +00001591 unsigned Tmp1, Tmp2;
1592 if (getRegPressure(LHS) > getRegPressure(RHS)) {
1593 Tmp1 = SelectExpr(LHS);
1594 Tmp2 = SelectExpr(RHS);
1595 } else {
1596 Tmp2 = SelectExpr(RHS);
1597 Tmp1 = SelectExpr(LHS);
1598 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001599 BuildMI(BB, Opc, 2).addReg(Tmp1).addReg(Tmp2);
1600}
1601
Chris Lattnera5ade062005-01-11 21:19:59 +00001602/// isFoldableLoad - Return true if this is a load instruction that can safely
1603/// be folded into an operation that uses it.
Chris Lattner44129b52005-01-25 20:03:11 +00001604bool ISel::isFoldableLoad(SDOperand Op, SDOperand OtherOp, bool FloatPromoteOk){
1605 if (Op.getOpcode() == ISD::LOAD) {
1606 // FIXME: currently can't fold constant pool indexes.
1607 if (isa<ConstantPoolSDNode>(Op.getOperand(1)))
1608 return false;
1609 } else if (FloatPromoteOk && Op.getOpcode() == ISD::EXTLOAD &&
1610 cast<MVTSDNode>(Op)->getExtraValueType() == MVT::f32) {
1611 // FIXME: currently can't fold constant pool indexes.
1612 if (isa<ConstantPoolSDNode>(Op.getOperand(1)))
1613 return false;
1614 } else {
Chris Lattnera5ade062005-01-11 21:19:59 +00001615 return false;
Chris Lattner44129b52005-01-25 20:03:11 +00001616 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001617
1618 // If this load has already been emitted, we clearly can't fold it.
Chris Lattner636e79a2005-01-13 05:53:16 +00001619 assert(Op.ResNo == 0 && "Not a use of the value of the load?");
1620 if (ExprMap.count(Op.getValue(1))) return false;
1621 assert(!ExprMap.count(Op.getValue(0)) && "Value in map but not token chain?");
Chris Lattner4a108662005-01-18 03:51:59 +00001622 assert(!ExprMap.count(Op.getValue(1))&&"Token lowered but value not in map?");
Chris Lattnera5ade062005-01-11 21:19:59 +00001623
Chris Lattner4ff348b2005-01-17 06:26:58 +00001624 // If there is not just one use of its value, we cannot fold.
1625 if (!Op.Val->hasNUsesOfValue(1, 0)) return false;
1626
1627 // Finally, we cannot fold the load into the operation if this would induce a
1628 // cycle into the resultant dag. To check for this, see if OtherOp (the other
1629 // operand of the operation we are folding the load into) can possible use the
1630 // chain node defined by the load.
1631 if (OtherOp.Val && !Op.Val->hasNUsesOfValue(0, 1)) { // Has uses of chain?
1632 std::set<SDNode*> Visited;
1633 if (NodeTransitivelyUsesValue(OtherOp, Op.getValue(1), Visited))
1634 return false;
1635 }
1636 return true;
Chris Lattnera5ade062005-01-11 21:19:59 +00001637}
1638
Chris Lattner4ff348b2005-01-17 06:26:58 +00001639
Chris Lattnera5ade062005-01-11 21:19:59 +00001640/// EmitFoldedLoad - Ensure that the arguments of the load are code generated,
1641/// and compute the address being loaded into AM.
1642void ISel::EmitFoldedLoad(SDOperand Op, X86AddressMode &AM) {
1643 SDOperand Chain = Op.getOperand(0);
1644 SDOperand Address = Op.getOperand(1);
Chris Lattner98a8ba02005-01-18 01:06:26 +00001645
Chris Lattnera5ade062005-01-11 21:19:59 +00001646 if (getRegPressure(Chain) > getRegPressure(Address)) {
1647 Select(Chain);
1648 SelectAddress(Address, AM);
1649 } else {
1650 SelectAddress(Address, AM);
1651 Select(Chain);
1652 }
1653
1654 // The chain for this load is now lowered.
Chris Lattner636e79a2005-01-13 05:53:16 +00001655 assert(ExprMap.count(SDOperand(Op.Val, 1)) == 0 &&
1656 "Load emitted more than once?");
Chris Lattner4a108662005-01-18 03:51:59 +00001657 if (!ExprMap.insert(std::make_pair(Op.getValue(1), 1)).second)
Chris Lattner636e79a2005-01-13 05:53:16 +00001658 assert(0 && "Load emitted more than once!");
Chris Lattnera5ade062005-01-11 21:19:59 +00001659}
1660
Chris Lattner30ea1e92005-01-19 07:37:26 +00001661// EmitOrOpOp - Pattern match the expression (Op1|Op2), where we know that op1
1662// and op2 are i8/i16/i32 values with one use each (the or). If we can form a
1663// SHLD or SHRD, emit the instruction (generating the value into DestReg) and
1664// return true.
1665bool ISel::EmitOrOpOp(SDOperand Op1, SDOperand Op2, unsigned DestReg) {
Chris Lattner85716372005-01-19 06:18:43 +00001666 if (Op1.getOpcode() == ISD::SHL && Op2.getOpcode() == ISD::SRL) {
1667 // good!
1668 } else if (Op2.getOpcode() == ISD::SHL && Op1.getOpcode() == ISD::SRL) {
1669 std::swap(Op1, Op2); // Op1 is the SHL now.
1670 } else {
1671 return false; // No match
1672 }
1673
1674 SDOperand ShlVal = Op1.getOperand(0);
1675 SDOperand ShlAmt = Op1.getOperand(1);
1676 SDOperand ShrVal = Op2.getOperand(0);
1677 SDOperand ShrAmt = Op2.getOperand(1);
1678
Chris Lattner30ea1e92005-01-19 07:37:26 +00001679 unsigned RegSize = MVT::getSizeInBits(Op1.getValueType());
1680
Chris Lattner85716372005-01-19 06:18:43 +00001681 // Find out if ShrAmt = 32-ShlAmt or ShlAmt = 32-ShrAmt.
1682 if (ShlAmt.getOpcode() == ISD::SUB && ShlAmt.getOperand(1) == ShrAmt)
1683 if (ConstantSDNode *SubCST = dyn_cast<ConstantSDNode>(ShlAmt.getOperand(0)))
Chris Lattner4053b1e2005-01-19 08:07:05 +00001684 if (SubCST->getValue() == RegSize) {
1685 // (A >> ShrAmt) | (A << (32-ShrAmt)) ==> ROR A, ShrAmt
Chris Lattner85716372005-01-19 06:18:43 +00001686 // (A >> ShrAmt) | (B << (32-ShrAmt)) ==> SHRD A, B, ShrAmt
Chris Lattner4053b1e2005-01-19 08:07:05 +00001687 if (ShrVal == ShlVal) {
1688 unsigned Reg, ShAmt;
1689 if (getRegPressure(ShrVal) > getRegPressure(ShrAmt)) {
1690 Reg = SelectExpr(ShrVal);
1691 ShAmt = SelectExpr(ShrAmt);
1692 } else {
1693 ShAmt = SelectExpr(ShrAmt);
1694 Reg = SelectExpr(ShrVal);
1695 }
1696 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShAmt);
1697 unsigned Opc = RegSize == 8 ? X86::ROR8rCL :
1698 (RegSize == 16 ? X86::ROR16rCL : X86::ROR32rCL);
1699 BuildMI(BB, Opc, 1, DestReg).addReg(Reg);
1700 return true;
1701 } else if (RegSize != 8) {
Chris Lattner85716372005-01-19 06:18:43 +00001702 unsigned AReg, BReg;
1703 if (getRegPressure(ShlVal) > getRegPressure(ShrVal)) {
Chris Lattner85716372005-01-19 06:18:43 +00001704 BReg = SelectExpr(ShlVal);
Chris Lattnerc3c021b2005-01-19 17:24:34 +00001705 AReg = SelectExpr(ShrVal);
Chris Lattner85716372005-01-19 06:18:43 +00001706 } else {
Chris Lattner85716372005-01-19 06:18:43 +00001707 AReg = SelectExpr(ShrVal);
Chris Lattnerc3c021b2005-01-19 17:24:34 +00001708 BReg = SelectExpr(ShlVal);
Chris Lattner85716372005-01-19 06:18:43 +00001709 }
Chris Lattner4053b1e2005-01-19 08:07:05 +00001710 unsigned ShAmt = SelectExpr(ShrAmt);
1711 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShAmt);
1712 unsigned Opc = RegSize == 16 ? X86::SHRD16rrCL : X86::SHRD32rrCL;
1713 BuildMI(BB, Opc, 2, DestReg).addReg(AReg).addReg(BReg);
Chris Lattner85716372005-01-19 06:18:43 +00001714 return true;
1715 }
1716 }
1717
Chris Lattner4053b1e2005-01-19 08:07:05 +00001718 if (ShrAmt.getOpcode() == ISD::SUB && ShrAmt.getOperand(1) == ShlAmt)
1719 if (ConstantSDNode *SubCST = dyn_cast<ConstantSDNode>(ShrAmt.getOperand(0)))
1720 if (SubCST->getValue() == RegSize) {
1721 // (A << ShlAmt) | (A >> (32-ShlAmt)) ==> ROL A, ShrAmt
1722 // (A << ShlAmt) | (B >> (32-ShlAmt)) ==> SHLD A, B, ShrAmt
1723 if (ShrVal == ShlVal) {
1724 unsigned Reg, ShAmt;
1725 if (getRegPressure(ShrVal) > getRegPressure(ShlAmt)) {
1726 Reg = SelectExpr(ShrVal);
1727 ShAmt = SelectExpr(ShlAmt);
1728 } else {
1729 ShAmt = SelectExpr(ShlAmt);
1730 Reg = SelectExpr(ShrVal);
1731 }
1732 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShAmt);
1733 unsigned Opc = RegSize == 8 ? X86::ROL8rCL :
1734 (RegSize == 16 ? X86::ROL16rCL : X86::ROL32rCL);
1735 BuildMI(BB, Opc, 1, DestReg).addReg(Reg);
1736 return true;
1737 } else if (RegSize != 8) {
1738 unsigned AReg, BReg;
1739 if (getRegPressure(ShlVal) > getRegPressure(ShrVal)) {
Chris Lattnerc3c021b2005-01-19 17:24:34 +00001740 AReg = SelectExpr(ShlVal);
1741 BReg = SelectExpr(ShrVal);
Chris Lattner4053b1e2005-01-19 08:07:05 +00001742 } else {
Chris Lattnerc3c021b2005-01-19 17:24:34 +00001743 BReg = SelectExpr(ShrVal);
1744 AReg = SelectExpr(ShlVal);
Chris Lattner4053b1e2005-01-19 08:07:05 +00001745 }
1746 unsigned ShAmt = SelectExpr(ShlAmt);
1747 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShAmt);
1748 unsigned Opc = RegSize == 16 ? X86::SHLD16rrCL : X86::SHLD32rrCL;
1749 BuildMI(BB, Opc, 2, DestReg).addReg(AReg).addReg(BReg);
1750 return true;
1751 }
1752 }
Chris Lattner85716372005-01-19 06:18:43 +00001753
Chris Lattner4053b1e2005-01-19 08:07:05 +00001754 if (ConstantSDNode *ShrCst = dyn_cast<ConstantSDNode>(ShrAmt))
1755 if (ConstantSDNode *ShlCst = dyn_cast<ConstantSDNode>(ShlAmt))
1756 if (ShrCst->getValue() < RegSize && ShlCst->getValue() < RegSize)
1757 if (ShrCst->getValue() == RegSize-ShlCst->getValue()) {
1758 // (A >> 5) | (A << 27) --> ROR A, 5
1759 // (A >> 5) | (B << 27) --> SHRD A, B, 5
1760 if (ShrVal == ShlVal) {
1761 unsigned Reg = SelectExpr(ShrVal);
1762 unsigned Opc = RegSize == 8 ? X86::ROR8ri :
1763 (RegSize == 16 ? X86::ROR16ri : X86::ROR32ri);
1764 BuildMI(BB, Opc, 2, DestReg).addReg(Reg).addImm(ShrCst->getValue());
1765 return true;
1766 } else if (RegSize != 8) {
1767 unsigned AReg, BReg;
1768 if (getRegPressure(ShlVal) > getRegPressure(ShrVal)) {
Chris Lattner4053b1e2005-01-19 08:07:05 +00001769 BReg = SelectExpr(ShlVal);
Chris Lattnerc3c021b2005-01-19 17:24:34 +00001770 AReg = SelectExpr(ShrVal);
Chris Lattner4053b1e2005-01-19 08:07:05 +00001771 } else {
Chris Lattner4053b1e2005-01-19 08:07:05 +00001772 AReg = SelectExpr(ShrVal);
Chris Lattnerc3c021b2005-01-19 17:24:34 +00001773 BReg = SelectExpr(ShlVal);
Chris Lattner4053b1e2005-01-19 08:07:05 +00001774 }
1775 unsigned Opc = RegSize == 16 ? X86::SHRD16rri8 : X86::SHRD32rri8;
1776 BuildMI(BB, Opc, 3, DestReg).addReg(AReg).addReg(BReg)
1777 .addImm(ShrCst->getValue());
1778 return true;
1779 }
1780 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001781
Chris Lattner85716372005-01-19 06:18:43 +00001782 return false;
1783}
1784
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001785unsigned ISel::SelectExpr(SDOperand N) {
1786 unsigned Result;
1787 unsigned Tmp1, Tmp2, Tmp3;
1788 unsigned Opc = 0;
Chris Lattner5188ad72005-01-08 19:28:19 +00001789 SDNode *Node = N.Val;
Chris Lattnera5ade062005-01-11 21:19:59 +00001790 SDOperand Op0, Op1;
Chris Lattner5188ad72005-01-08 19:28:19 +00001791
Chris Lattner7f2afac2005-01-14 22:37:41 +00001792 if (Node->getOpcode() == ISD::CopyFromReg) {
Chris Lattnerc6f41812005-05-12 23:06:28 +00001793 if (MRegisterInfo::isVirtualRegister(cast<RegSDNode>(Node)->getReg()) ||
1794 cast<RegSDNode>(Node)->getReg() == X86::ESP) {
1795 // Just use the specified register as our input.
1796 return cast<RegSDNode>(Node)->getReg();
1797 }
Chris Lattner7f2afac2005-01-14 22:37:41 +00001798 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001799
Chris Lattnera5ade062005-01-11 21:19:59 +00001800 unsigned &Reg = ExprMap[N];
1801 if (Reg) return Reg;
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001802
Chris Lattnerb38a7492005-04-02 04:01:14 +00001803 switch (N.getOpcode()) {
1804 default:
Chris Lattnera5ade062005-01-11 21:19:59 +00001805 Reg = Result = (N.getValueType() != MVT::Other) ?
Chris Lattnerb38a7492005-04-02 04:01:14 +00001806 MakeReg(N.getValueType()) : 1;
1807 break;
Chris Lattner2e7714a2005-05-13 20:29:13 +00001808 case ISD::TAILCALL:
Chris Lattnerb38a7492005-04-02 04:01:14 +00001809 case ISD::CALL:
Chris Lattnera5ade062005-01-11 21:19:59 +00001810 // If this is a call instruction, make sure to prepare ALL of the result
1811 // values as well as the chain.
Chris Lattnerb38a7492005-04-02 04:01:14 +00001812 if (Node->getNumValues() == 1)
1813 Reg = Result = 1; // Void call, just a chain.
1814 else {
Chris Lattnera5ade062005-01-11 21:19:59 +00001815 Result = MakeReg(Node->getValueType(0));
1816 ExprMap[N.getValue(0)] = Result;
Chris Lattnerb38a7492005-04-02 04:01:14 +00001817 for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
Chris Lattnera5ade062005-01-11 21:19:59 +00001818 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
Chris Lattnerb38a7492005-04-02 04:01:14 +00001819 ExprMap[SDOperand(Node, Node->getNumValues()-1)] = 1;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001820 }
Chris Lattnerb38a7492005-04-02 04:01:14 +00001821 break;
1822 case ISD::ADD_PARTS:
1823 case ISD::SUB_PARTS:
1824 case ISD::SHL_PARTS:
1825 case ISD::SRL_PARTS:
1826 case ISD::SRA_PARTS:
1827 Result = MakeReg(Node->getValueType(0));
1828 ExprMap[N.getValue(0)] = Result;
1829 for (unsigned i = 1, e = N.Val->getNumValues(); i != e; ++i)
1830 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
1831 break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001832 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001833
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001834 switch (N.getOpcode()) {
1835 default:
Chris Lattner5188ad72005-01-08 19:28:19 +00001836 Node->dump();
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001837 assert(0 && "Node not handled!\n");
Chris Lattnerc6f41812005-05-12 23:06:28 +00001838 case ISD::CopyFromReg:
1839 Select(N.getOperand(0));
1840 if (Result == 1) {
1841 Reg = Result = ExprMap[N.getValue(0)] =
1842 MakeReg(N.getValue(0).getValueType());
1843 }
1844 switch (Node->getValueType(0)) {
1845 default: assert(0 && "Cannot CopyFromReg this!");
1846 case MVT::i1:
1847 case MVT::i8:
1848 BuildMI(BB, X86::MOV8rr, 1,
1849 Result).addReg(cast<RegSDNode>(Node)->getReg());
1850 return Result;
1851 case MVT::i16:
1852 BuildMI(BB, X86::MOV16rr, 1,
1853 Result).addReg(cast<RegSDNode>(Node)->getReg());
1854 return Result;
1855 case MVT::i32:
1856 BuildMI(BB, X86::MOV32rr, 1,
1857 Result).addReg(cast<RegSDNode>(Node)->getReg());
1858 return Result;
1859 }
1860
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001861 case ISD::FrameIndex:
1862 Tmp1 = cast<FrameIndexSDNode>(N)->getIndex();
1863 addFrameReference(BuildMI(BB, X86::LEA32r, 4, Result), (int)Tmp1);
1864 return Result;
1865 case ISD::ConstantPool:
1866 Tmp1 = cast<ConstantPoolSDNode>(N)->getIndex();
1867 addConstantPoolReference(BuildMI(BB, X86::LEA32r, 4, Result), Tmp1);
1868 return Result;
1869 case ISD::ConstantFP:
1870 ContainsFPCode = true;
1871 Tmp1 = Result; // Intermediate Register
1872 if (cast<ConstantFPSDNode>(N)->getValue() < 0.0 ||
1873 cast<ConstantFPSDNode>(N)->isExactlyValue(-0.0))
1874 Tmp1 = MakeReg(MVT::f64);
1875
1876 if (cast<ConstantFPSDNode>(N)->isExactlyValue(+0.0) ||
1877 cast<ConstantFPSDNode>(N)->isExactlyValue(-0.0))
1878 BuildMI(BB, X86::FLD0, 0, Tmp1);
1879 else if (cast<ConstantFPSDNode>(N)->isExactlyValue(+1.0) ||
1880 cast<ConstantFPSDNode>(N)->isExactlyValue(-1.0))
1881 BuildMI(BB, X86::FLD1, 0, Tmp1);
1882 else
1883 assert(0 && "Unexpected constant!");
1884 if (Tmp1 != Result)
1885 BuildMI(BB, X86::FCHS, 1, Result).addReg(Tmp1);
1886 return Result;
1887 case ISD::Constant:
1888 switch (N.getValueType()) {
1889 default: assert(0 && "Cannot use constants of this type!");
1890 case MVT::i1:
1891 case MVT::i8: Opc = X86::MOV8ri; break;
1892 case MVT::i16: Opc = X86::MOV16ri; break;
1893 case MVT::i32: Opc = X86::MOV32ri; break;
1894 }
1895 BuildMI(BB, Opc, 1,Result).addImm(cast<ConstantSDNode>(N)->getValue());
1896 return Result;
Chris Lattner7ce7eff2005-04-01 22:46:45 +00001897 case ISD::UNDEF:
1898 if (Node->getValueType(0) == MVT::f64) {
1899 // FIXME: SHOULD TEACH STACKIFIER ABOUT UNDEF VALUES!
1900 BuildMI(BB, X86::FLD0, 0, Result);
1901 } else {
1902 BuildMI(BB, X86::IMPLICIT_DEF, 0, Result);
1903 }
1904 return Result;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001905 case ISD::GlobalAddress: {
1906 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
1907 BuildMI(BB, X86::MOV32ri, 1, Result).addGlobalAddress(GV);
1908 return Result;
1909 }
1910 case ISD::ExternalSymbol: {
1911 const char *Sym = cast<ExternalSymbolSDNode>(N)->getSymbol();
1912 BuildMI(BB, X86::MOV32ri, 1, Result).addExternalSymbol(Sym);
1913 return Result;
1914 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001915 case ISD::ZERO_EXTEND: {
1916 int DestIs16 = N.getValueType() == MVT::i16;
1917 int SrcIs16 = N.getOperand(0).getValueType() == MVT::i16;
Chris Lattner590d8002005-01-09 18:52:44 +00001918
1919 // FIXME: This hack is here for zero extension casts from bool to i8. This
1920 // would not be needed if bools were promoted by Legalize.
1921 if (N.getValueType() == MVT::i8) {
Chris Lattnerdbba22f2005-01-11 23:33:00 +00001922 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner590d8002005-01-09 18:52:44 +00001923 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(Tmp1);
1924 return Result;
1925 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001926
Chris Lattner4ff348b2005-01-17 06:26:58 +00001927 if (isFoldableLoad(N.getOperand(0), SDOperand())) {
Chris Lattnerdbba22f2005-01-11 23:33:00 +00001928 static const unsigned Opc[3] = {
1929 X86::MOVZX32rm8, X86::MOVZX32rm16, X86::MOVZX16rm8
1930 };
1931
1932 X86AddressMode AM;
1933 EmitFoldedLoad(N.getOperand(0), AM);
1934 addFullAddress(BuildMI(BB, Opc[SrcIs16+DestIs16*2], 4, Result), AM);
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001935
Chris Lattnerdbba22f2005-01-11 23:33:00 +00001936 return Result;
1937 }
1938
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001939 static const unsigned Opc[3] = {
1940 X86::MOVZX32rr8, X86::MOVZX32rr16, X86::MOVZX16rr8
1941 };
Chris Lattnerdbba22f2005-01-11 23:33:00 +00001942 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001943 BuildMI(BB, Opc[SrcIs16+DestIs16*2], 1, Result).addReg(Tmp1);
1944 return Result;
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001945 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001946 case ISD::SIGN_EXTEND: {
1947 int DestIs16 = N.getValueType() == MVT::i16;
1948 int SrcIs16 = N.getOperand(0).getValueType() == MVT::i16;
1949
Chris Lattner590d8002005-01-09 18:52:44 +00001950 // FIXME: Legalize should promote bools to i8!
1951 assert(N.getOperand(0).getValueType() != MVT::i1 &&
1952 "Sign extend from bool not implemented!");
1953
Chris Lattner4ff348b2005-01-17 06:26:58 +00001954 if (isFoldableLoad(N.getOperand(0), SDOperand())) {
Chris Lattnerdbba22f2005-01-11 23:33:00 +00001955 static const unsigned Opc[3] = {
1956 X86::MOVSX32rm8, X86::MOVSX32rm16, X86::MOVSX16rm8
1957 };
1958
1959 X86AddressMode AM;
1960 EmitFoldedLoad(N.getOperand(0), AM);
1961 addFullAddress(BuildMI(BB, Opc[SrcIs16+DestIs16*2], 4, Result), AM);
1962 return Result;
1963 }
1964
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001965 static const unsigned Opc[3] = {
1966 X86::MOVSX32rr8, X86::MOVSX32rr16, X86::MOVSX16rr8
1967 };
1968 Tmp1 = SelectExpr(N.getOperand(0));
1969 BuildMI(BB, Opc[SrcIs16+DestIs16*2], 1, Result).addReg(Tmp1);
1970 return Result;
1971 }
1972 case ISD::TRUNCATE:
Chris Lattnerafce4302005-01-12 02:19:06 +00001973 // Fold TRUNCATE (LOAD P) into a smaller load from P.
Chris Lattner477c9312005-01-18 20:05:56 +00001974 // FIXME: This should be performed by the DAGCombiner.
Chris Lattner4ff348b2005-01-17 06:26:58 +00001975 if (isFoldableLoad(N.getOperand(0), SDOperand())) {
Chris Lattnerafce4302005-01-12 02:19:06 +00001976 switch (N.getValueType()) {
1977 default: assert(0 && "Unknown truncate!");
1978 case MVT::i1:
1979 case MVT::i8: Opc = X86::MOV8rm; break;
1980 case MVT::i16: Opc = X86::MOV16rm; break;
1981 }
1982 X86AddressMode AM;
1983 EmitFoldedLoad(N.getOperand(0), AM);
1984 addFullAddress(BuildMI(BB, Opc, 4, Result), AM);
1985 return Result;
1986 }
1987
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001988 // Handle cast of LARGER int to SMALLER int using a move to EAX followed by
1989 // a move out of AX or AL.
1990 switch (N.getOperand(0).getValueType()) {
1991 default: assert(0 && "Unknown truncate!");
1992 case MVT::i8: Tmp2 = X86::AL; Opc = X86::MOV8rr; break;
1993 case MVT::i16: Tmp2 = X86::AX; Opc = X86::MOV16rr; break;
1994 case MVT::i32: Tmp2 = X86::EAX; Opc = X86::MOV32rr; break;
1995 }
1996 Tmp1 = SelectExpr(N.getOperand(0));
1997 BuildMI(BB, Opc, 1, Tmp2).addReg(Tmp1);
1998
1999 switch (N.getValueType()) {
2000 default: assert(0 && "Unknown truncate!");
2001 case MVT::i1:
2002 case MVT::i8: Tmp2 = X86::AL; Opc = X86::MOV8rr; break;
2003 case MVT::i16: Tmp2 = X86::AX; Opc = X86::MOV16rr; break;
2004 }
2005 BuildMI(BB, Opc, 1, Result).addReg(Tmp2);
2006 return Result;
2007
Chris Lattner590d8002005-01-09 18:52:44 +00002008 case ISD::SINT_TO_FP:
2009 case ISD::UINT_TO_FP: {
2010 // FIXME: Most of this grunt work should be done by legalize!
Chris Lattneref7ba072005-01-11 03:50:45 +00002011 ContainsFPCode = true;
Chris Lattner590d8002005-01-09 18:52:44 +00002012
2013 // Promote the integer to a type supported by FLD. We do this because there
2014 // are no unsigned FLD instructions, so we must promote an unsigned value to
2015 // a larger signed value, then use FLD on the larger value.
2016 //
2017 MVT::ValueType PromoteType = MVT::Other;
2018 MVT::ValueType SrcTy = N.getOperand(0).getValueType();
2019 unsigned PromoteOpcode = 0;
2020 unsigned RealDestReg = Result;
2021 switch (SrcTy) {
2022 case MVT::i1:
2023 case MVT::i8:
2024 // We don't have the facilities for directly loading byte sized data from
2025 // memory (even signed). Promote it to 16 bits.
2026 PromoteType = MVT::i16;
2027 PromoteOpcode = Node->getOpcode() == ISD::SINT_TO_FP ?
2028 X86::MOVSX16rr8 : X86::MOVZX16rr8;
2029 break;
2030 case MVT::i16:
2031 if (Node->getOpcode() == ISD::UINT_TO_FP) {
2032 PromoteType = MVT::i32;
2033 PromoteOpcode = X86::MOVZX32rr16;
2034 }
2035 break;
2036 default:
2037 // Don't fild into the real destination.
2038 if (Node->getOpcode() == ISD::UINT_TO_FP)
2039 Result = MakeReg(Node->getValueType(0));
2040 break;
2041 }
2042
2043 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002044
Chris Lattner590d8002005-01-09 18:52:44 +00002045 if (PromoteType != MVT::Other) {
2046 Tmp2 = MakeReg(PromoteType);
2047 BuildMI(BB, PromoteOpcode, 1, Tmp2).addReg(Tmp1);
2048 SrcTy = PromoteType;
2049 Tmp1 = Tmp2;
2050 }
2051
2052 // Spill the integer to memory and reload it from there.
2053 unsigned Size = MVT::getSizeInBits(SrcTy)/8;
2054 MachineFunction *F = BB->getParent();
2055 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, Size);
2056
2057 switch (SrcTy) {
Chris Lattner590d8002005-01-09 18:52:44 +00002058 case MVT::i32:
2059 addFrameReference(BuildMI(BB, X86::MOV32mr, 5),
2060 FrameIdx).addReg(Tmp1);
2061 addFrameReference(BuildMI(BB, X86::FILD32m, 5, Result), FrameIdx);
2062 break;
2063 case MVT::i16:
2064 addFrameReference(BuildMI(BB, X86::MOV16mr, 5),
2065 FrameIdx).addReg(Tmp1);
2066 addFrameReference(BuildMI(BB, X86::FILD16m, 5, Result), FrameIdx);
2067 break;
2068 default: break; // No promotion required.
2069 }
2070
Chris Lattner085c9952005-01-12 04:00:00 +00002071 if (Node->getOpcode() == ISD::UINT_TO_FP && Result != RealDestReg) {
Chris Lattner590d8002005-01-09 18:52:44 +00002072 // If this is a cast from uint -> double, we need to be careful when if
2073 // the "sign" bit is set. If so, we don't want to make a negative number,
2074 // we want to make a positive number. Emit code to add an offset if the
2075 // sign bit is set.
2076
2077 // Compute whether the sign bit is set by shifting the reg right 31 bits.
2078 unsigned IsNeg = MakeReg(MVT::i32);
2079 BuildMI(BB, X86::SHR32ri, 2, IsNeg).addReg(Tmp1).addImm(31);
2080
2081 // Create a CP value that has the offset in one word and 0 in the other.
2082 static ConstantInt *TheOffset = ConstantUInt::get(Type::ULongTy,
2083 0x4f80000000000000ULL);
2084 unsigned CPI = F->getConstantPool()->getConstantPoolIndex(TheOffset);
2085 BuildMI(BB, X86::FADD32m, 5, RealDestReg).addReg(Result)
2086 .addConstantPoolIndex(CPI).addZImm(4).addReg(IsNeg).addSImm(0);
Chris Lattner590d8002005-01-09 18:52:44 +00002087 }
2088 return RealDestReg;
2089 }
2090 case ISD::FP_TO_SINT:
2091 case ISD::FP_TO_UINT: {
2092 // FIXME: Most of this grunt work should be done by legalize!
2093 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
2094
2095 // Change the floating point control register to use "round towards zero"
2096 // mode when truncating to an integer value.
2097 //
2098 MachineFunction *F = BB->getParent();
2099 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
2100 addFrameReference(BuildMI(BB, X86::FNSTCW16m, 4), CWFrameIdx);
2101
2102 // Load the old value of the high byte of the control word...
2103 unsigned HighPartOfCW = MakeReg(MVT::i8);
2104 addFrameReference(BuildMI(BB, X86::MOV8rm, 4, HighPartOfCW),
2105 CWFrameIdx, 1);
2106
2107 // Set the high part to be round to zero...
2108 addFrameReference(BuildMI(BB, X86::MOV8mi, 5),
2109 CWFrameIdx, 1).addImm(12);
2110
2111 // Reload the modified control word now...
2112 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002113
Chris Lattner590d8002005-01-09 18:52:44 +00002114 // Restore the memory image of control word to original value
2115 addFrameReference(BuildMI(BB, X86::MOV8mr, 5),
2116 CWFrameIdx, 1).addReg(HighPartOfCW);
2117
2118 // We don't have the facilities for directly storing byte sized data to
2119 // memory. Promote it to 16 bits. We also must promote unsigned values to
2120 // larger classes because we only have signed FP stores.
2121 MVT::ValueType StoreClass = Node->getValueType(0);
2122 if (StoreClass == MVT::i8 || Node->getOpcode() == ISD::FP_TO_UINT)
2123 switch (StoreClass) {
Chris Lattner2afa1912005-05-09 05:33:18 +00002124 case MVT::i1:
Chris Lattner590d8002005-01-09 18:52:44 +00002125 case MVT::i8: StoreClass = MVT::i16; break;
2126 case MVT::i16: StoreClass = MVT::i32; break;
2127 case MVT::i32: StoreClass = MVT::i64; break;
Chris Lattner590d8002005-01-09 18:52:44 +00002128 default: assert(0 && "Unknown store class!");
2129 }
2130
2131 // Spill the integer to memory and reload it from there.
2132 unsigned Size = MVT::getSizeInBits(StoreClass)/8;
2133 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, Size);
2134
2135 switch (StoreClass) {
2136 default: assert(0 && "Unknown store class!");
2137 case MVT::i16:
2138 addFrameReference(BuildMI(BB, X86::FIST16m, 5), FrameIdx).addReg(Tmp1);
2139 break;
2140 case MVT::i32:
Chris Lattner25020852005-01-09 19:49:59 +00002141 addFrameReference(BuildMI(BB, X86::FIST32m, 5), FrameIdx).addReg(Tmp1);
Chris Lattner590d8002005-01-09 18:52:44 +00002142 break;
Chris Lattnera0dbf182005-05-09 18:37:02 +00002143 case MVT::i64:
2144 addFrameReference(BuildMI(BB, X86::FISTP64m, 5), FrameIdx).addReg(Tmp1);
2145 break; }
Chris Lattner590d8002005-01-09 18:52:44 +00002146
2147 switch (Node->getValueType(0)) {
2148 default:
2149 assert(0 && "Unknown integer type!");
Chris Lattner590d8002005-01-09 18:52:44 +00002150 case MVT::i32:
2151 addFrameReference(BuildMI(BB, X86::MOV32rm, 4, Result), FrameIdx);
2152 break;
2153 case MVT::i16:
2154 addFrameReference(BuildMI(BB, X86::MOV16rm, 4, Result), FrameIdx);
2155 break;
2156 case MVT::i8:
Chris Lattner2afa1912005-05-09 05:33:18 +00002157 case MVT::i1:
Chris Lattner590d8002005-01-09 18:52:44 +00002158 addFrameReference(BuildMI(BB, X86::MOV8rm, 4, Result), FrameIdx);
2159 break;
2160 }
2161
2162 // Reload the original control word now.
2163 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
2164 return Result;
2165 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002166 case ISD::ADD:
Chris Lattnera5ade062005-01-11 21:19:59 +00002167 Op0 = N.getOperand(0);
2168 Op1 = N.getOperand(1);
2169
Chris Lattner44129b52005-01-25 20:03:11 +00002170 if (isFoldableLoad(Op0, Op1, true)) {
Chris Lattnera5ade062005-01-11 21:19:59 +00002171 std::swap(Op0, Op1);
Chris Lattner4ff348b2005-01-17 06:26:58 +00002172 goto FoldAdd;
2173 }
Chris Lattnera5ade062005-01-11 21:19:59 +00002174
Chris Lattner44129b52005-01-25 20:03:11 +00002175 if (isFoldableLoad(Op1, Op0, true)) {
Chris Lattner4ff348b2005-01-17 06:26:58 +00002176 FoldAdd:
Chris Lattnera5ade062005-01-11 21:19:59 +00002177 switch (N.getValueType()) {
2178 default: assert(0 && "Cannot add this type!");
2179 case MVT::i1:
2180 case MVT::i8: Opc = X86::ADD8rm; break;
2181 case MVT::i16: Opc = X86::ADD16rm; break;
2182 case MVT::i32: Opc = X86::ADD32rm; break;
Chris Lattner44129b52005-01-25 20:03:11 +00002183 case MVT::f64:
2184 // For F64, handle promoted load operations (from F32) as well!
2185 Opc = Op1.getOpcode() == ISD::LOAD ? X86::FADD64m : X86::FADD32m;
2186 break;
Chris Lattnera5ade062005-01-11 21:19:59 +00002187 }
2188 X86AddressMode AM;
Chris Lattner636e79a2005-01-13 05:53:16 +00002189 EmitFoldedLoad(Op1, AM);
2190 Tmp1 = SelectExpr(Op0);
Chris Lattnera5ade062005-01-11 21:19:59 +00002191 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
2192 return Result;
2193 }
2194
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002195 // See if we can codegen this as an LEA to fold operations together.
2196 if (N.getValueType() == MVT::i32) {
Chris Lattner883c86f2005-01-18 02:25:52 +00002197 ExprMap.erase(N);
Chris Lattner98a8ba02005-01-18 01:06:26 +00002198 X86ISelAddressMode AM;
Chris Lattner883c86f2005-01-18 02:25:52 +00002199 MatchAddress(N, AM);
2200 ExprMap[N] = Result;
2201
2202 // If this is not just an add, emit the LEA. For a simple add (like
2203 // reg+reg or reg+imm), we just emit an add. It might be a good idea to
2204 // leave this as LEA, then peephole it to 'ADD' after two address elim
2205 // happens.
2206 if (AM.Scale != 1 || AM.BaseType == X86ISelAddressMode::FrameIndexBase||
2207 AM.GV || (AM.Base.Reg.Val && AM.IndexReg.Val && AM.Disp)) {
2208 X86AddressMode XAM = SelectAddrExprs(AM);
2209 addFullAddress(BuildMI(BB, X86::LEA32r, 4, Result), XAM);
2210 return Result;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002211 }
2212 }
Chris Lattner11333092005-01-11 03:11:44 +00002213
Chris Lattnera5ade062005-01-11 21:19:59 +00002214 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1)) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002215 Opc = 0;
2216 if (CN->getValue() == 1) { // add X, 1 -> inc X
2217 switch (N.getValueType()) {
2218 default: assert(0 && "Cannot integer add this type!");
2219 case MVT::i8: Opc = X86::INC8r; break;
2220 case MVT::i16: Opc = X86::INC16r; break;
2221 case MVT::i32: Opc = X86::INC32r; break;
2222 }
2223 } else if (CN->isAllOnesValue()) { // add X, -1 -> dec X
2224 switch (N.getValueType()) {
2225 default: assert(0 && "Cannot integer add this type!");
2226 case MVT::i8: Opc = X86::DEC8r; break;
2227 case MVT::i16: Opc = X86::DEC16r; break;
2228 case MVT::i32: Opc = X86::DEC32r; break;
2229 }
2230 }
2231
2232 if (Opc) {
Chris Lattnera5ade062005-01-11 21:19:59 +00002233 Tmp1 = SelectExpr(Op0);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002234 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
2235 return Result;
2236 }
2237
2238 switch (N.getValueType()) {
2239 default: assert(0 && "Cannot add this type!");
2240 case MVT::i8: Opc = X86::ADD8ri; break;
2241 case MVT::i16: Opc = X86::ADD16ri; break;
2242 case MVT::i32: Opc = X86::ADD32ri; break;
2243 }
2244 if (Opc) {
Chris Lattnera5ade062005-01-11 21:19:59 +00002245 Tmp1 = SelectExpr(Op0);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002246 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
2247 return Result;
2248 }
2249 }
2250
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002251 switch (N.getValueType()) {
2252 default: assert(0 && "Cannot add this type!");
2253 case MVT::i8: Opc = X86::ADD8rr; break;
2254 case MVT::i16: Opc = X86::ADD16rr; break;
2255 case MVT::i32: Opc = X86::ADD32rr; break;
Chris Lattneref7ba072005-01-11 03:50:45 +00002256 case MVT::f64: Opc = X86::FpADD; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002257 }
Chris Lattner11333092005-01-11 03:11:44 +00002258
Chris Lattnera5ade062005-01-11 21:19:59 +00002259 if (getRegPressure(Op0) > getRegPressure(Op1)) {
2260 Tmp1 = SelectExpr(Op0);
2261 Tmp2 = SelectExpr(Op1);
Chris Lattner11333092005-01-11 03:11:44 +00002262 } else {
Chris Lattnera5ade062005-01-11 21:19:59 +00002263 Tmp2 = SelectExpr(Op1);
2264 Tmp1 = SelectExpr(Op0);
Chris Lattner11333092005-01-11 03:11:44 +00002265 }
2266
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002267 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
2268 return Result;
Chris Lattnerb7edaa12005-04-02 05:30:17 +00002269
2270 case ISD::FABS:
Chris Lattnerb7edaa12005-04-02 05:30:17 +00002271 case ISD::FNEG:
Chris Lattnerc5dcb532005-04-30 04:25:35 +00002272 case ISD::FSIN:
2273 case ISD::FCOS:
Chris Lattner2c56e8a2005-04-28 22:07:18 +00002274 case ISD::FSQRT:
2275 assert(N.getValueType()==MVT::f64 && "Illegal type for this operation");
Chris Lattnerb7edaa12005-04-02 05:30:17 +00002276 Tmp1 = SelectExpr(Node->getOperand(0));
Chris Lattner2c56e8a2005-04-28 22:07:18 +00002277 switch (N.getOpcode()) {
2278 default: assert(0 && "Unreachable!");
2279 case ISD::FABS: BuildMI(BB, X86::FABS, 1, Result).addReg(Tmp1); break;
2280 case ISD::FNEG: BuildMI(BB, X86::FCHS, 1, Result).addReg(Tmp1); break;
2281 case ISD::FSQRT: BuildMI(BB, X86::FSQRT, 1, Result).addReg(Tmp1); break;
Chris Lattnerc5dcb532005-04-30 04:25:35 +00002282 case ISD::FSIN: BuildMI(BB, X86::FSIN, 1, Result).addReg(Tmp1); break;
2283 case ISD::FCOS: BuildMI(BB, X86::FCOS, 1, Result).addReg(Tmp1); break;
Chris Lattner2c56e8a2005-04-28 22:07:18 +00002284 }
Chris Lattnerb7edaa12005-04-02 05:30:17 +00002285 return Result;
2286
Chris Lattner8db0af12005-04-06 04:21:07 +00002287 case ISD::MULHU:
2288 switch (N.getValueType()) {
2289 default: assert(0 && "Unsupported VT!");
2290 case MVT::i8: Tmp2 = X86::MUL8r; break;
2291 case MVT::i16: Tmp2 = X86::MUL16r; break;
2292 case MVT::i32: Tmp2 = X86::MUL32r; break;
2293 }
2294 // FALL THROUGH
2295 case ISD::MULHS: {
2296 unsigned MovOpc, LowReg, HiReg;
2297 switch (N.getValueType()) {
2298 default: assert(0 && "Unsupported VT!");
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002299 case MVT::i8:
Chris Lattner8db0af12005-04-06 04:21:07 +00002300 MovOpc = X86::MOV8rr;
2301 LowReg = X86::AL;
2302 HiReg = X86::AH;
2303 Opc = X86::IMUL8r;
2304 break;
2305 case MVT::i16:
2306 MovOpc = X86::MOV16rr;
2307 LowReg = X86::AX;
2308 HiReg = X86::DX;
2309 Opc = X86::IMUL16r;
2310 break;
2311 case MVT::i32:
2312 MovOpc = X86::MOV32rr;
2313 LowReg = X86::EAX;
2314 HiReg = X86::EDX;
2315 Opc = X86::IMUL32r;
2316 break;
2317 }
2318 if (Node->getOpcode() != ISD::MULHS)
2319 Opc = Tmp2; // Get the MULHU opcode.
2320
2321 Op0 = Node->getOperand(0);
2322 Op1 = Node->getOperand(1);
2323 if (getRegPressure(Op0) > getRegPressure(Op1)) {
2324 Tmp1 = SelectExpr(Op0);
2325 Tmp2 = SelectExpr(Op1);
2326 } else {
2327 Tmp2 = SelectExpr(Op1);
2328 Tmp1 = SelectExpr(Op0);
2329 }
2330
2331 // FIXME: Implement folding of loads into the memory operands here!
2332 BuildMI(BB, MovOpc, 1, LowReg).addReg(Tmp1);
2333 BuildMI(BB, Opc, 1).addReg(Tmp2);
2334 BuildMI(BB, MovOpc, 1, Result).addReg(HiReg);
2335 return Result;
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002336 }
Chris Lattner8db0af12005-04-06 04:21:07 +00002337
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002338 case ISD::SUB:
Chris Lattnera5ade062005-01-11 21:19:59 +00002339 case ISD::MUL:
2340 case ISD::AND:
2341 case ISD::OR:
Chris Lattnera56cea42005-01-12 04:23:22 +00002342 case ISD::XOR: {
Chris Lattnera5ade062005-01-11 21:19:59 +00002343 static const unsigned SUBTab[] = {
2344 X86::SUB8ri, X86::SUB16ri, X86::SUB32ri, 0, 0,
2345 X86::SUB8rm, X86::SUB16rm, X86::SUB32rm, X86::FSUB32m, X86::FSUB64m,
2346 X86::SUB8rr, X86::SUB16rr, X86::SUB32rr, X86::FpSUB , X86::FpSUB,
2347 };
2348 static const unsigned MULTab[] = {
2349 0, X86::IMUL16rri, X86::IMUL32rri, 0, 0,
2350 0, X86::IMUL16rm , X86::IMUL32rm, X86::FMUL32m, X86::FMUL64m,
2351 0, X86::IMUL16rr , X86::IMUL32rr, X86::FpMUL , X86::FpMUL,
2352 };
2353 static const unsigned ANDTab[] = {
2354 X86::AND8ri, X86::AND16ri, X86::AND32ri, 0, 0,
2355 X86::AND8rm, X86::AND16rm, X86::AND32rm, 0, 0,
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002356 X86::AND8rr, X86::AND16rr, X86::AND32rr, 0, 0,
Chris Lattnera5ade062005-01-11 21:19:59 +00002357 };
2358 static const unsigned ORTab[] = {
2359 X86::OR8ri, X86::OR16ri, X86::OR32ri, 0, 0,
2360 X86::OR8rm, X86::OR16rm, X86::OR32rm, 0, 0,
2361 X86::OR8rr, X86::OR16rr, X86::OR32rr, 0, 0,
2362 };
2363 static const unsigned XORTab[] = {
2364 X86::XOR8ri, X86::XOR16ri, X86::XOR32ri, 0, 0,
2365 X86::XOR8rm, X86::XOR16rm, X86::XOR32rm, 0, 0,
2366 X86::XOR8rr, X86::XOR16rr, X86::XOR32rr, 0, 0,
2367 };
2368
2369 Op0 = Node->getOperand(0);
2370 Op1 = Node->getOperand(1);
2371
Chris Lattner30ea1e92005-01-19 07:37:26 +00002372 if (Node->getOpcode() == ISD::OR && Op0.hasOneUse() && Op1.hasOneUse())
2373 if (EmitOrOpOp(Op0, Op1, Result)) // Match SHLD, SHRD, and rotates.
Chris Lattner85716372005-01-19 06:18:43 +00002374 return Result;
2375
2376 if (Node->getOpcode() == ISD::SUB)
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002377 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(0)))
2378 if (CN->isNullValue()) { // 0 - N -> neg N
2379 switch (N.getValueType()) {
2380 default: assert(0 && "Cannot sub this type!");
2381 case MVT::i1:
2382 case MVT::i8: Opc = X86::NEG8r; break;
2383 case MVT::i16: Opc = X86::NEG16r; break;
2384 case MVT::i32: Opc = X86::NEG32r; break;
2385 }
2386 Tmp1 = SelectExpr(N.getOperand(1));
2387 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
2388 return Result;
2389 }
2390
Chris Lattnera5ade062005-01-11 21:19:59 +00002391 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1)) {
2392 if (CN->isAllOnesValue() && Node->getOpcode() == ISD::XOR) {
Chris Lattnerc98279d2005-01-17 00:23:16 +00002393 Opc = 0;
Chris Lattnerd4dab922005-01-11 04:31:30 +00002394 switch (N.getValueType()) {
2395 default: assert(0 && "Cannot add this type!");
Chris Lattnerc98279d2005-01-17 00:23:16 +00002396 case MVT::i1: break; // Not supported, don't invert upper bits!
Chris Lattnerd4dab922005-01-11 04:31:30 +00002397 case MVT::i8: Opc = X86::NOT8r; break;
2398 case MVT::i16: Opc = X86::NOT16r; break;
2399 case MVT::i32: Opc = X86::NOT32r; break;
2400 }
Chris Lattnerc98279d2005-01-17 00:23:16 +00002401 if (Opc) {
2402 Tmp1 = SelectExpr(Op0);
2403 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
2404 return Result;
2405 }
Chris Lattnerd4dab922005-01-11 04:31:30 +00002406 }
2407
Chris Lattner2a4e5082005-01-17 06:48:02 +00002408 // Fold common multiplies into LEA instructions.
2409 if (Node->getOpcode() == ISD::MUL && N.getValueType() == MVT::i32) {
2410 switch ((int)CN->getValue()) {
2411 default: break;
2412 case 3:
2413 case 5:
2414 case 9:
Chris Lattner2a4e5082005-01-17 06:48:02 +00002415 // Remove N from exprmap so SelectAddress doesn't get confused.
2416 ExprMap.erase(N);
Chris Lattner98a8ba02005-01-18 01:06:26 +00002417 X86AddressMode AM;
Chris Lattner2a4e5082005-01-17 06:48:02 +00002418 SelectAddress(N, AM);
2419 // Restore it to the map.
2420 ExprMap[N] = Result;
2421 addFullAddress(BuildMI(BB, X86::LEA32r, 4, Result), AM);
2422 return Result;
2423 }
2424 }
2425
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002426 switch (N.getValueType()) {
Chris Lattnerd4dab922005-01-11 04:31:30 +00002427 default: assert(0 && "Cannot xor this type!");
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002428 case MVT::i1:
Chris Lattnera5ade062005-01-11 21:19:59 +00002429 case MVT::i8: Opc = 0; break;
2430 case MVT::i16: Opc = 1; break;
2431 case MVT::i32: Opc = 2; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002432 }
Chris Lattnera5ade062005-01-11 21:19:59 +00002433 switch (Node->getOpcode()) {
2434 default: assert(0 && "Unreachable!");
2435 case ISD::SUB: Opc = SUBTab[Opc]; break;
2436 case ISD::MUL: Opc = MULTab[Opc]; break;
2437 case ISD::AND: Opc = ANDTab[Opc]; break;
2438 case ISD::OR: Opc = ORTab[Opc]; break;
2439 case ISD::XOR: Opc = XORTab[Opc]; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002440 }
Chris Lattnera5ade062005-01-11 21:19:59 +00002441 if (Opc) { // Can't fold MUL:i8 R, imm
2442 Tmp1 = SelectExpr(Op0);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002443 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
2444 return Result;
2445 }
2446 }
Chris Lattner11333092005-01-11 03:11:44 +00002447
Chris Lattner44129b52005-01-25 20:03:11 +00002448 if (isFoldableLoad(Op0, Op1, true))
Chris Lattnera5ade062005-01-11 21:19:59 +00002449 if (Node->getOpcode() != ISD::SUB) {
2450 std::swap(Op0, Op1);
Chris Lattner4ff348b2005-01-17 06:26:58 +00002451 goto FoldOps;
Chris Lattnera5ade062005-01-11 21:19:59 +00002452 } else {
Chris Lattner44129b52005-01-25 20:03:11 +00002453 // For FP, emit 'reverse' subract, with a memory operand.
2454 if (N.getValueType() == MVT::f64) {
2455 if (Op0.getOpcode() == ISD::EXTLOAD)
2456 Opc = X86::FSUBR32m;
2457 else
2458 Opc = X86::FSUBR64m;
2459
Chris Lattnera5ade062005-01-11 21:19:59 +00002460 X86AddressMode AM;
Chris Lattner636e79a2005-01-13 05:53:16 +00002461 EmitFoldedLoad(Op0, AM);
2462 Tmp1 = SelectExpr(Op1);
Chris Lattnera5ade062005-01-11 21:19:59 +00002463 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
2464 return Result;
2465 }
2466 }
2467
Chris Lattner44129b52005-01-25 20:03:11 +00002468 if (isFoldableLoad(Op1, Op0, true)) {
Chris Lattner4ff348b2005-01-17 06:26:58 +00002469 FoldOps:
Chris Lattnera5ade062005-01-11 21:19:59 +00002470 switch (N.getValueType()) {
2471 default: assert(0 && "Cannot operate on this type!");
2472 case MVT::i1:
2473 case MVT::i8: Opc = 5; break;
2474 case MVT::i16: Opc = 6; break;
2475 case MVT::i32: Opc = 7; break;
Chris Lattner44129b52005-01-25 20:03:11 +00002476 // For F64, handle promoted load operations (from F32) as well!
2477 case MVT::f64: Opc = Op1.getOpcode() == ISD::LOAD ? 9 : 8; break;
Chris Lattnera5ade062005-01-11 21:19:59 +00002478 }
2479 switch (Node->getOpcode()) {
2480 default: assert(0 && "Unreachable!");
2481 case ISD::SUB: Opc = SUBTab[Opc]; break;
2482 case ISD::MUL: Opc = MULTab[Opc]; break;
2483 case ISD::AND: Opc = ANDTab[Opc]; break;
2484 case ISD::OR: Opc = ORTab[Opc]; break;
2485 case ISD::XOR: Opc = XORTab[Opc]; break;
2486 }
2487
2488 X86AddressMode AM;
Chris Lattner636e79a2005-01-13 05:53:16 +00002489 EmitFoldedLoad(Op1, AM);
2490 Tmp1 = SelectExpr(Op0);
Chris Lattnera5ade062005-01-11 21:19:59 +00002491 if (Opc) {
2492 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
2493 } else {
2494 assert(Node->getOpcode() == ISD::MUL &&
2495 N.getValueType() == MVT::i8 && "Unexpected situation!");
2496 // Must use the MUL instruction, which forces use of AL.
2497 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(Tmp1);
2498 addFullAddress(BuildMI(BB, X86::MUL8m, 1), AM);
2499 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
2500 }
2501 return Result;
Chris Lattner11333092005-01-11 03:11:44 +00002502 }
Chris Lattnera5ade062005-01-11 21:19:59 +00002503
2504 if (getRegPressure(Op0) > getRegPressure(Op1)) {
2505 Tmp1 = SelectExpr(Op0);
2506 Tmp2 = SelectExpr(Op1);
2507 } else {
2508 Tmp2 = SelectExpr(Op1);
2509 Tmp1 = SelectExpr(Op0);
2510 }
2511
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002512 switch (N.getValueType()) {
2513 default: assert(0 && "Cannot add this type!");
Chris Lattnera5ade062005-01-11 21:19:59 +00002514 case MVT::i1:
2515 case MVT::i8: Opc = 10; break;
2516 case MVT::i16: Opc = 11; break;
2517 case MVT::i32: Opc = 12; break;
2518 case MVT::f32: Opc = 13; break;
2519 case MVT::f64: Opc = 14; break;
2520 }
2521 switch (Node->getOpcode()) {
2522 default: assert(0 && "Unreachable!");
2523 case ISD::SUB: Opc = SUBTab[Opc]; break;
2524 case ISD::MUL: Opc = MULTab[Opc]; break;
2525 case ISD::AND: Opc = ANDTab[Opc]; break;
2526 case ISD::OR: Opc = ORTab[Opc]; break;
2527 case ISD::XOR: Opc = XORTab[Opc]; break;
2528 }
2529 if (Opc) {
2530 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
2531 } else {
2532 assert(Node->getOpcode() == ISD::MUL &&
2533 N.getValueType() == MVT::i8 && "Unexpected situation!");
Chris Lattnera13d3232005-01-10 20:55:48 +00002534 // Must use the MUL instruction, which forces use of AL.
2535 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(Tmp1);
2536 BuildMI(BB, X86::MUL8r, 1).addReg(Tmp2);
2537 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002538 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002539 return Result;
Chris Lattnera56cea42005-01-12 04:23:22 +00002540 }
Chris Lattner19ad0622005-01-20 18:53:00 +00002541 case ISD::ADD_PARTS:
2542 case ISD::SUB_PARTS: {
2543 assert(N.getNumOperands() == 4 && N.getValueType() == MVT::i32 &&
2544 "Not an i64 add/sub!");
2545 // Emit all of the operands.
2546 std::vector<unsigned> InVals;
2547 for (unsigned i = 0, e = N.getNumOperands(); i != e; ++i)
2548 InVals.push_back(SelectExpr(N.getOperand(i)));
2549 if (N.getOpcode() == ISD::ADD_PARTS) {
2550 BuildMI(BB, X86::ADD32rr, 2, Result).addReg(InVals[0]).addReg(InVals[2]);
2551 BuildMI(BB, X86::ADC32rr,2,Result+1).addReg(InVals[1]).addReg(InVals[3]);
2552 } else {
2553 BuildMI(BB, X86::SUB32rr, 2, Result).addReg(InVals[0]).addReg(InVals[2]);
2554 BuildMI(BB, X86::SBB32rr, 2,Result+1).addReg(InVals[1]).addReg(InVals[3]);
2555 }
2556 return Result+N.ResNo;
2557 }
2558
Chris Lattnerb38a7492005-04-02 04:01:14 +00002559 case ISD::SHL_PARTS:
2560 case ISD::SRA_PARTS:
2561 case ISD::SRL_PARTS: {
2562 assert(N.getNumOperands() == 3 && N.getValueType() == MVT::i32 &&
2563 "Not an i64 shift!");
2564 unsigned ShiftOpLo = SelectExpr(N.getOperand(0));
2565 unsigned ShiftOpHi = SelectExpr(N.getOperand(1));
2566 unsigned TmpReg = MakeReg(MVT::i32);
2567 if (N.getOpcode() == ISD::SRA_PARTS) {
2568 // If this is a SHR of a Long, then we need to do funny sign extension
2569 // stuff. TmpReg gets the value to use as the high-part if we are
2570 // shifting more than 32 bits.
2571 BuildMI(BB, X86::SAR32ri, 2, TmpReg).addReg(ShiftOpHi).addImm(31);
2572 } else {
2573 // Other shifts use a fixed zero value if the shift is more than 32 bits.
2574 BuildMI(BB, X86::MOV32ri, 1, TmpReg).addImm(0);
2575 }
2576
2577 // Initialize CL with the shift amount.
2578 unsigned ShiftAmountReg = SelectExpr(N.getOperand(2));
2579 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShiftAmountReg);
2580
2581 unsigned TmpReg2 = MakeReg(MVT::i32);
2582 unsigned TmpReg3 = MakeReg(MVT::i32);
2583 if (N.getOpcode() == ISD::SHL_PARTS) {
2584 // TmpReg2 = shld inHi, inLo
2585 BuildMI(BB, X86::SHLD32rrCL, 2,TmpReg2).addReg(ShiftOpHi)
2586 .addReg(ShiftOpLo);
2587 // TmpReg3 = shl inLo, CL
2588 BuildMI(BB, X86::SHL32rCL, 1, TmpReg3).addReg(ShiftOpLo);
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002589
Chris Lattnerb38a7492005-04-02 04:01:14 +00002590 // Set the flags to indicate whether the shift was by more than 32 bits.
2591 BuildMI(BB, X86::TEST8ri, 2).addReg(X86::CL).addImm(32);
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002592
Chris Lattnerb38a7492005-04-02 04:01:14 +00002593 // DestHi = (>32) ? TmpReg3 : TmpReg2;
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002594 BuildMI(BB, X86::CMOVNE32rr, 2,
Chris Lattnerb38a7492005-04-02 04:01:14 +00002595 Result+1).addReg(TmpReg2).addReg(TmpReg3);
2596 // DestLo = (>32) ? TmpReg : TmpReg3;
2597 BuildMI(BB, X86::CMOVNE32rr, 2,
2598 Result).addReg(TmpReg3).addReg(TmpReg);
2599 } else {
2600 // TmpReg2 = shrd inLo, inHi
2601 BuildMI(BB, X86::SHRD32rrCL,2,TmpReg2).addReg(ShiftOpLo)
2602 .addReg(ShiftOpHi);
2603 // TmpReg3 = s[ah]r inHi, CL
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002604 BuildMI(BB, N.getOpcode() == ISD::SRA_PARTS ? X86::SAR32rCL
Chris Lattnerb38a7492005-04-02 04:01:14 +00002605 : X86::SHR32rCL, 1, TmpReg3)
2606 .addReg(ShiftOpHi);
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002607
Chris Lattnerb38a7492005-04-02 04:01:14 +00002608 // Set the flags to indicate whether the shift was by more than 32 bits.
2609 BuildMI(BB, X86::TEST8ri, 2).addReg(X86::CL).addImm(32);
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002610
Chris Lattnerb38a7492005-04-02 04:01:14 +00002611 // DestLo = (>32) ? TmpReg3 : TmpReg2;
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002612 BuildMI(BB, X86::CMOVNE32rr, 2,
Chris Lattnerb38a7492005-04-02 04:01:14 +00002613 Result).addReg(TmpReg2).addReg(TmpReg3);
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002614
Chris Lattnerb38a7492005-04-02 04:01:14 +00002615 // DestHi = (>32) ? TmpReg : TmpReg3;
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002616 BuildMI(BB, X86::CMOVNE32rr, 2,
Chris Lattnerb38a7492005-04-02 04:01:14 +00002617 Result+1).addReg(TmpReg3).addReg(TmpReg);
2618 }
2619 return Result+N.ResNo;
2620 }
2621
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002622 case ISD::SELECT:
Chris Lattnerda2ce112005-01-16 07:34:08 +00002623 if (getRegPressure(N.getOperand(1)) > getRegPressure(N.getOperand(2))) {
2624 Tmp2 = SelectExpr(N.getOperand(1));
2625 Tmp3 = SelectExpr(N.getOperand(2));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002626 } else {
Chris Lattnerda2ce112005-01-16 07:34:08 +00002627 Tmp3 = SelectExpr(N.getOperand(2));
2628 Tmp2 = SelectExpr(N.getOperand(1));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002629 }
Chris Lattnerda2ce112005-01-16 07:34:08 +00002630 EmitSelectCC(N.getOperand(0), N.getValueType(), Tmp2, Tmp3, Result);
2631 return Result;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002632
2633 case ISD::SDIV:
2634 case ISD::UDIV:
2635 case ISD::SREM:
2636 case ISD::UREM: {
Chris Lattnerda2ce112005-01-16 07:34:08 +00002637 assert((N.getOpcode() != ISD::SREM || MVT::isInteger(N.getValueType())) &&
2638 "We don't support this operator!");
2639
Chris Lattner5bf26862005-04-13 03:29:53 +00002640 if (N.getOpcode() == ISD::SDIV) {
Chris Lattner3576c842005-01-25 20:35:10 +00002641 // We can fold loads into FpDIVs, but not really into any others.
2642 if (N.getValueType() == MVT::f64) {
2643 // Check for reversed and unreversed DIV.
2644 if (isFoldableLoad(N.getOperand(0), N.getOperand(1), true)) {
2645 if (N.getOperand(0).getOpcode() == ISD::EXTLOAD)
2646 Opc = X86::FDIVR32m;
2647 else
2648 Opc = X86::FDIVR64m;
2649 X86AddressMode AM;
2650 EmitFoldedLoad(N.getOperand(0), AM);
2651 Tmp1 = SelectExpr(N.getOperand(1));
2652 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
2653 return Result;
2654 } else if (isFoldableLoad(N.getOperand(1), N.getOperand(0), true) &&
2655 N.getOperand(1).getOpcode() == ISD::LOAD) {
2656 if (N.getOperand(1).getOpcode() == ISD::EXTLOAD)
2657 Opc = X86::FDIV32m;
2658 else
2659 Opc = X86::FDIV64m;
2660 X86AddressMode AM;
2661 EmitFoldedLoad(N.getOperand(1), AM);
2662 Tmp1 = SelectExpr(N.getOperand(0));
2663 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
2664 return Result;
2665 }
2666 }
2667
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002668 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
2669 // FIXME: These special cases should be handled by the lowering impl!
2670 unsigned RHS = CN->getValue();
2671 bool isNeg = false;
2672 if ((int)RHS < 0) {
2673 isNeg = true;
2674 RHS = -RHS;
2675 }
2676 if (RHS && (RHS & (RHS-1)) == 0) { // Signed division by power of 2?
2677 unsigned Log = log2(RHS);
2678 unsigned TmpReg = MakeReg(N.getValueType());
2679 unsigned SAROpc, SHROpc, ADDOpc, NEGOpc;
2680 switch (N.getValueType()) {
2681 default: assert("Unknown type to signed divide!");
2682 case MVT::i8:
2683 SAROpc = X86::SAR8ri;
2684 SHROpc = X86::SHR8ri;
2685 ADDOpc = X86::ADD8rr;
2686 NEGOpc = X86::NEG8r;
2687 break;
2688 case MVT::i16:
2689 SAROpc = X86::SAR16ri;
2690 SHROpc = X86::SHR16ri;
2691 ADDOpc = X86::ADD16rr;
2692 NEGOpc = X86::NEG16r;
2693 break;
2694 case MVT::i32:
2695 SAROpc = X86::SAR32ri;
2696 SHROpc = X86::SHR32ri;
2697 ADDOpc = X86::ADD32rr;
2698 NEGOpc = X86::NEG32r;
2699 break;
2700 }
Chris Lattner11333092005-01-11 03:11:44 +00002701 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002702 BuildMI(BB, SAROpc, 2, TmpReg).addReg(Tmp1).addImm(Log-1);
2703 unsigned TmpReg2 = MakeReg(N.getValueType());
2704 BuildMI(BB, SHROpc, 2, TmpReg2).addReg(TmpReg).addImm(32-Log);
2705 unsigned TmpReg3 = MakeReg(N.getValueType());
2706 BuildMI(BB, ADDOpc, 2, TmpReg3).addReg(Tmp1).addReg(TmpReg2);
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002707
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002708 unsigned TmpReg4 = isNeg ? MakeReg(N.getValueType()) : Result;
2709 BuildMI(BB, SAROpc, 2, TmpReg4).addReg(TmpReg3).addImm(Log);
2710 if (isNeg)
2711 BuildMI(BB, NEGOpc, 1, Result).addReg(TmpReg4);
2712 return Result;
2713 }
2714 }
Chris Lattner5bf26862005-04-13 03:29:53 +00002715 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002716
Chris Lattner11333092005-01-11 03:11:44 +00002717 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2718 Tmp1 = SelectExpr(N.getOperand(0));
2719 Tmp2 = SelectExpr(N.getOperand(1));
2720 } else {
2721 Tmp2 = SelectExpr(N.getOperand(1));
2722 Tmp1 = SelectExpr(N.getOperand(0));
2723 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002724
2725 bool isSigned = N.getOpcode() == ISD::SDIV || N.getOpcode() == ISD::SREM;
2726 bool isDiv = N.getOpcode() == ISD::SDIV || N.getOpcode() == ISD::UDIV;
2727 unsigned LoReg, HiReg, DivOpcode, MovOpcode, ClrOpcode, SExtOpcode;
2728 switch (N.getValueType()) {
2729 default: assert(0 && "Cannot sdiv this type!");
2730 case MVT::i8:
2731 DivOpcode = isSigned ? X86::IDIV8r : X86::DIV8r;
2732 LoReg = X86::AL;
2733 HiReg = X86::AH;
2734 MovOpcode = X86::MOV8rr;
2735 ClrOpcode = X86::MOV8ri;
2736 SExtOpcode = X86::CBW;
2737 break;
2738 case MVT::i16:
2739 DivOpcode = isSigned ? X86::IDIV16r : X86::DIV16r;
2740 LoReg = X86::AX;
2741 HiReg = X86::DX;
2742 MovOpcode = X86::MOV16rr;
2743 ClrOpcode = X86::MOV16ri;
2744 SExtOpcode = X86::CWD;
2745 break;
2746 case MVT::i32:
2747 DivOpcode = isSigned ? X86::IDIV32r : X86::DIV32r;
Chris Lattner42928302005-01-12 03:16:09 +00002748 LoReg = X86::EAX;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002749 HiReg = X86::EDX;
2750 MovOpcode = X86::MOV32rr;
2751 ClrOpcode = X86::MOV32ri;
2752 SExtOpcode = X86::CDQ;
2753 break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002754 case MVT::f64:
Chris Lattnerda2ce112005-01-16 07:34:08 +00002755 BuildMI(BB, X86::FpDIV, 2, Result).addReg(Tmp1).addReg(Tmp2);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002756 return Result;
2757 }
2758
2759 // Set up the low part.
2760 BuildMI(BB, MovOpcode, 1, LoReg).addReg(Tmp1);
2761
2762 if (isSigned) {
2763 // Sign extend the low part into the high part.
2764 BuildMI(BB, SExtOpcode, 0);
2765 } else {
2766 // Zero out the high part, effectively zero extending the input.
2767 BuildMI(BB, ClrOpcode, 1, HiReg).addImm(0);
2768 }
2769
2770 // Emit the DIV/IDIV instruction.
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002771 BuildMI(BB, DivOpcode, 1).addReg(Tmp2);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002772
2773 // Get the result of the divide or rem.
2774 BuildMI(BB, MovOpcode, 1, Result).addReg(isDiv ? LoReg : HiReg);
2775 return Result;
2776 }
2777
2778 case ISD::SHL:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002779 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Chris Lattnera5ade062005-01-11 21:19:59 +00002780 if (CN->getValue() == 1) { // X = SHL Y, 1 -> X = ADD Y, Y
2781 switch (N.getValueType()) {
2782 default: assert(0 && "Cannot shift this type!");
2783 case MVT::i8: Opc = X86::ADD8rr; break;
2784 case MVT::i16: Opc = X86::ADD16rr; break;
2785 case MVT::i32: Opc = X86::ADD32rr; break;
2786 }
2787 Tmp1 = SelectExpr(N.getOperand(0));
2788 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp1);
2789 return Result;
2790 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002791
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002792 switch (N.getValueType()) {
2793 default: assert(0 && "Cannot shift this type!");
2794 case MVT::i8: Opc = X86::SHL8ri; break;
2795 case MVT::i16: Opc = X86::SHL16ri; break;
2796 case MVT::i32: Opc = X86::SHL32ri; break;
2797 }
Chris Lattner11333092005-01-11 03:11:44 +00002798 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002799 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
2800 return Result;
2801 }
Chris Lattner11333092005-01-11 03:11:44 +00002802
2803 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2804 Tmp1 = SelectExpr(N.getOperand(0));
2805 Tmp2 = SelectExpr(N.getOperand(1));
2806 } else {
2807 Tmp2 = SelectExpr(N.getOperand(1));
2808 Tmp1 = SelectExpr(N.getOperand(0));
2809 }
2810
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002811 switch (N.getValueType()) {
2812 default: assert(0 && "Cannot shift this type!");
2813 case MVT::i8 : Opc = X86::SHL8rCL; break;
2814 case MVT::i16: Opc = X86::SHL16rCL; break;
2815 case MVT::i32: Opc = X86::SHL32rCL; break;
2816 }
2817 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
2818 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
2819 return Result;
2820 case ISD::SRL:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002821 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
2822 switch (N.getValueType()) {
2823 default: assert(0 && "Cannot shift this type!");
2824 case MVT::i8: Opc = X86::SHR8ri; break;
2825 case MVT::i16: Opc = X86::SHR16ri; break;
2826 case MVT::i32: Opc = X86::SHR32ri; break;
2827 }
Chris Lattner11333092005-01-11 03:11:44 +00002828 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002829 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
2830 return Result;
2831 }
Chris Lattner11333092005-01-11 03:11:44 +00002832
2833 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2834 Tmp1 = SelectExpr(N.getOperand(0));
2835 Tmp2 = SelectExpr(N.getOperand(1));
2836 } else {
2837 Tmp2 = SelectExpr(N.getOperand(1));
2838 Tmp1 = SelectExpr(N.getOperand(0));
2839 }
2840
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002841 switch (N.getValueType()) {
2842 default: assert(0 && "Cannot shift this type!");
2843 case MVT::i8 : Opc = X86::SHR8rCL; break;
2844 case MVT::i16: Opc = X86::SHR16rCL; break;
2845 case MVT::i32: Opc = X86::SHR32rCL; break;
2846 }
2847 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
2848 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
2849 return Result;
2850 case ISD::SRA:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002851 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
2852 switch (N.getValueType()) {
2853 default: assert(0 && "Cannot shift this type!");
2854 case MVT::i8: Opc = X86::SAR8ri; break;
2855 case MVT::i16: Opc = X86::SAR16ri; break;
2856 case MVT::i32: Opc = X86::SAR32ri; break;
2857 }
Chris Lattner11333092005-01-11 03:11:44 +00002858 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002859 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
2860 return Result;
2861 }
Chris Lattner11333092005-01-11 03:11:44 +00002862
2863 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2864 Tmp1 = SelectExpr(N.getOperand(0));
2865 Tmp2 = SelectExpr(N.getOperand(1));
2866 } else {
2867 Tmp2 = SelectExpr(N.getOperand(1));
2868 Tmp1 = SelectExpr(N.getOperand(0));
2869 }
2870
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002871 switch (N.getValueType()) {
2872 default: assert(0 && "Cannot shift this type!");
2873 case MVT::i8 : Opc = X86::SAR8rCL; break;
2874 case MVT::i16: Opc = X86::SAR16rCL; break;
2875 case MVT::i32: Opc = X86::SAR32rCL; break;
2876 }
2877 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
2878 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
2879 return Result;
2880
2881 case ISD::SETCC:
Chris Lattnercb1aa8d2005-01-17 01:34:14 +00002882 EmitCMP(N.getOperand(0), N.getOperand(1), Node->hasOneUse());
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002883 EmitSetCC(BB, Result, cast<SetCCSDNode>(N)->getCondition(),
2884 MVT::isFloatingPoint(N.getOperand(1).getValueType()));
2885 return Result;
Chris Lattnere9ef81d2005-01-15 05:22:24 +00002886 case ISD::LOAD:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002887 // Make sure we generate both values.
Chris Lattner4a108662005-01-18 03:51:59 +00002888 if (Result != 1) { // Generate the token
2889 if (!ExprMap.insert(std::make_pair(N.getValue(1), 1)).second)
2890 assert(0 && "Load already emitted!?");
2891 } else
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002892 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
2893
Chris Lattner5188ad72005-01-08 19:28:19 +00002894 switch (Node->getValueType(0)) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002895 default: assert(0 && "Cannot load this type!");
2896 case MVT::i1:
2897 case MVT::i8: Opc = X86::MOV8rm; break;
2898 case MVT::i16: Opc = X86::MOV16rm; break;
2899 case MVT::i32: Opc = X86::MOV32rm; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002900 case MVT::f64: Opc = X86::FLD64m; ContainsFPCode = true; break;
2901 }
Chris Lattner11333092005-01-11 03:11:44 +00002902
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002903 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N.getOperand(1))){
Chris Lattner11333092005-01-11 03:11:44 +00002904 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002905 addConstantPoolReference(BuildMI(BB, Opc, 4, Result), CP->getIndex());
2906 } else {
2907 X86AddressMode AM;
Chris Lattner636e79a2005-01-13 05:53:16 +00002908
2909 SDOperand Chain = N.getOperand(0);
2910 SDOperand Address = N.getOperand(1);
2911 if (getRegPressure(Chain) > getRegPressure(Address)) {
2912 Select(Chain);
2913 SelectAddress(Address, AM);
2914 } else {
2915 SelectAddress(Address, AM);
2916 Select(Chain);
2917 }
2918
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002919 addFullAddress(BuildMI(BB, Opc, 4, Result), AM);
2920 }
2921 return Result;
Chris Lattnere9ef81d2005-01-15 05:22:24 +00002922
2923 case ISD::EXTLOAD: // Arbitrarily codegen extloads as MOVZX*
2924 case ISD::ZEXTLOAD: {
2925 // Make sure we generate both values.
2926 if (Result != 1)
2927 ExprMap[N.getValue(1)] = 1; // Generate the token
2928 else
2929 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
2930
Chris Lattnerda2ce112005-01-16 07:34:08 +00002931 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N.getOperand(1)))
2932 if (Node->getValueType(0) == MVT::f64) {
2933 assert(cast<MVTSDNode>(Node)->getExtraValueType() == MVT::f32 &&
2934 "Bad EXTLOAD!");
2935 addConstantPoolReference(BuildMI(BB, X86::FLD32m, 4, Result),
2936 CP->getIndex());
2937 return Result;
2938 }
2939
Chris Lattnere9ef81d2005-01-15 05:22:24 +00002940 X86AddressMode AM;
2941 if (getRegPressure(Node->getOperand(0)) >
2942 getRegPressure(Node->getOperand(1))) {
2943 Select(Node->getOperand(0)); // chain
2944 SelectAddress(Node->getOperand(1), AM);
2945 } else {
2946 SelectAddress(Node->getOperand(1), AM);
2947 Select(Node->getOperand(0)); // chain
2948 }
2949
2950 switch (Node->getValueType(0)) {
2951 default: assert(0 && "Unknown type to sign extend to.");
2952 case MVT::f64:
2953 assert(cast<MVTSDNode>(Node)->getExtraValueType() == MVT::f32 &&
2954 "Bad EXTLOAD!");
2955 addFullAddress(BuildMI(BB, X86::FLD32m, 5, Result), AM);
2956 break;
2957 case MVT::i32:
2958 switch (cast<MVTSDNode>(Node)->getExtraValueType()) {
2959 default:
2960 assert(0 && "Bad zero extend!");
2961 case MVT::i1:
2962 case MVT::i8:
2963 addFullAddress(BuildMI(BB, X86::MOVZX32rm8, 5, Result), AM);
2964 break;
2965 case MVT::i16:
2966 addFullAddress(BuildMI(BB, X86::MOVZX32rm16, 5, Result), AM);
2967 break;
2968 }
2969 break;
2970 case MVT::i16:
2971 assert(cast<MVTSDNode>(Node)->getExtraValueType() <= MVT::i8 &&
2972 "Bad zero extend!");
2973 addFullAddress(BuildMI(BB, X86::MOVSX16rm8, 5, Result), AM);
2974 break;
2975 case MVT::i8:
2976 assert(cast<MVTSDNode>(Node)->getExtraValueType() == MVT::i1 &&
2977 "Bad zero extend!");
2978 addFullAddress(BuildMI(BB, X86::MOV8rm, 5, Result), AM);
2979 break;
2980 }
2981 return Result;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002982 }
Chris Lattnere9ef81d2005-01-15 05:22:24 +00002983 case ISD::SEXTLOAD: {
2984 // Make sure we generate both values.
2985 if (Result != 1)
2986 ExprMap[N.getValue(1)] = 1; // Generate the token
2987 else
2988 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
2989
2990 X86AddressMode AM;
2991 if (getRegPressure(Node->getOperand(0)) >
2992 getRegPressure(Node->getOperand(1))) {
2993 Select(Node->getOperand(0)); // chain
2994 SelectAddress(Node->getOperand(1), AM);
2995 } else {
2996 SelectAddress(Node->getOperand(1), AM);
2997 Select(Node->getOperand(0)); // chain
2998 }
2999
3000 switch (Node->getValueType(0)) {
3001 case MVT::i8: assert(0 && "Cannot sign extend from bool!");
3002 default: assert(0 && "Unknown type to sign extend to.");
3003 case MVT::i32:
3004 switch (cast<MVTSDNode>(Node)->getExtraValueType()) {
3005 default:
3006 case MVT::i1: assert(0 && "Cannot sign extend from bool!");
3007 case MVT::i8:
3008 addFullAddress(BuildMI(BB, X86::MOVSX32rm8, 5, Result), AM);
3009 break;
3010 case MVT::i16:
3011 addFullAddress(BuildMI(BB, X86::MOVSX32rm16, 5, Result), AM);
3012 break;
3013 }
3014 break;
3015 case MVT::i16:
3016 assert(cast<MVTSDNode>(Node)->getExtraValueType() == MVT::i8 &&
3017 "Cannot sign extend from bool!");
3018 addFullAddress(BuildMI(BB, X86::MOVSX16rm8, 5, Result), AM);
3019 break;
3020 }
3021 return Result;
3022 }
3023
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003024 case ISD::DYNAMIC_STACKALLOC:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003025 // Generate both result values.
3026 if (Result != 1)
3027 ExprMap[N.getValue(1)] = 1; // Generate the token
3028 else
3029 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
3030
3031 // FIXME: We are currently ignoring the requested alignment for handling
3032 // greater than the stack alignment. This will need to be revisited at some
3033 // point. Align = N.getOperand(2);
3034
3035 if (!isa<ConstantSDNode>(N.getOperand(2)) ||
3036 cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) {
3037 std::cerr << "Cannot allocate stack object with greater alignment than"
3038 << " the stack alignment yet!";
3039 abort();
3040 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003041
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003042 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Chris Lattner11333092005-01-11 03:11:44 +00003043 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003044 BuildMI(BB, X86::SUB32ri, 2, X86::ESP).addReg(X86::ESP)
3045 .addImm(CN->getValue());
3046 } else {
Chris Lattner11333092005-01-11 03:11:44 +00003047 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
3048 Select(N.getOperand(0));
3049 Tmp1 = SelectExpr(N.getOperand(1));
3050 } else {
3051 Tmp1 = SelectExpr(N.getOperand(1));
3052 Select(N.getOperand(0));
3053 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003054
3055 // Subtract size from stack pointer, thereby allocating some space.
3056 BuildMI(BB, X86::SUB32rr, 2, X86::ESP).addReg(X86::ESP).addReg(Tmp1);
3057 }
3058
3059 // Put a pointer to the space into the result register, by copying the stack
3060 // pointer.
3061 BuildMI(BB, X86::MOV32rr, 1, Result).addReg(X86::ESP);
3062 return Result;
3063
Chris Lattner2e7714a2005-05-13 20:29:13 +00003064 case ISD::TAILCALL:
Chris Lattnerc6f41812005-05-12 23:06:28 +00003065 case ISD::CALL: {
Chris Lattner5188ad72005-01-08 19:28:19 +00003066 // The chain for this call is now lowered.
Chris Lattner4a108662005-01-18 03:51:59 +00003067 ExprMap.insert(std::make_pair(N.getValue(Node->getNumValues()-1), 1));
Chris Lattner5188ad72005-01-08 19:28:19 +00003068
Chris Lattnerc6f41812005-05-12 23:06:28 +00003069 bool isDirect = isa<GlobalAddressSDNode>(N.getOperand(1)) ||
3070 isa<ExternalSymbolSDNode>(N.getOperand(1));
3071 unsigned Callee = 0;
3072 if (isDirect) {
3073 Select(N.getOperand(0));
3074 } else {
3075 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
3076 Select(N.getOperand(0));
3077 Callee = SelectExpr(N.getOperand(1));
3078 } else {
3079 Callee = SelectExpr(N.getOperand(1));
3080 Select(N.getOperand(0));
3081 }
3082 }
3083
3084 // If this call has values to pass in registers, do so now.
3085 if (Node->getNumOperands() > 2) {
3086 // The first value is passed in (a part of) EAX, the second in EDX.
3087 unsigned RegOp1 = SelectExpr(N.getOperand(2));
3088 unsigned RegOp2 =
3089 Node->getNumOperands() > 3 ? SelectExpr(N.getOperand(3)) : 0;
3090
3091 switch (N.getOperand(2).getValueType()) {
3092 default: assert(0 && "Bad thing to pass in regs");
3093 case MVT::i1:
3094 case MVT::i8: BuildMI(BB, X86::MOV8rr , 1,X86::AL).addReg(RegOp1); break;
3095 case MVT::i16: BuildMI(BB, X86::MOV16rr, 1,X86::AX).addReg(RegOp1); break;
3096 case MVT::i32: BuildMI(BB, X86::MOV32rr, 1,X86::EAX).addReg(RegOp1);break;
3097 }
3098 if (RegOp2)
3099 switch (N.getOperand(3).getValueType()) {
3100 default: assert(0 && "Bad thing to pass in regs");
3101 case MVT::i1:
3102 case MVT::i8:
3103 BuildMI(BB, X86::MOV8rr , 1, X86::DL).addReg(RegOp2);
3104 break;
3105 case MVT::i16:
3106 BuildMI(BB, X86::MOV16rr, 1, X86::DX).addReg(RegOp2);
3107 break;
3108 case MVT::i32:
3109 BuildMI(BB, X86::MOV32rr, 1, X86::EDX).addReg(RegOp2);
3110 break;
3111 }
3112 }
3113
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003114 if (GlobalAddressSDNode *GASD =
3115 dyn_cast<GlobalAddressSDNode>(N.getOperand(1))) {
3116 BuildMI(BB, X86::CALLpcrel32, 1).addGlobalAddress(GASD->getGlobal(),true);
3117 } else if (ExternalSymbolSDNode *ESSDN =
3118 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1))) {
3119 BuildMI(BB, X86::CALLpcrel32,
3120 1).addExternalSymbol(ESSDN->getSymbol(), true);
3121 } else {
Chris Lattner11333092005-01-11 03:11:44 +00003122 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
3123 Select(N.getOperand(0));
3124 Tmp1 = SelectExpr(N.getOperand(1));
3125 } else {
3126 Tmp1 = SelectExpr(N.getOperand(1));
3127 Select(N.getOperand(0));
3128 }
3129
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003130 BuildMI(BB, X86::CALL32r, 1).addReg(Tmp1);
3131 }
Chris Lattner5188ad72005-01-08 19:28:19 +00003132 switch (Node->getValueType(0)) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003133 default: assert(0 && "Unknown value type for call result!");
3134 case MVT::Other: return 1;
3135 case MVT::i1:
3136 case MVT::i8:
3137 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
3138 break;
3139 case MVT::i16:
3140 BuildMI(BB, X86::MOV16rr, 1, Result).addReg(X86::AX);
3141 break;
3142 case MVT::i32:
3143 BuildMI(BB, X86::MOV32rr, 1, Result).addReg(X86::EAX);
Chris Lattner5188ad72005-01-08 19:28:19 +00003144 if (Node->getValueType(1) == MVT::i32)
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003145 BuildMI(BB, X86::MOV32rr, 1, Result+1).addReg(X86::EDX);
3146 break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003147 case MVT::f64: // Floating-point return values live in %ST(0)
3148 ContainsFPCode = true;
3149 BuildMI(BB, X86::FpGETRESULT, 1, Result);
3150 break;
3151 }
3152 return Result+N.ResNo;
Chris Lattnerc6f41812005-05-12 23:06:28 +00003153 }
Chris Lattner966cdfb2005-05-09 21:17:38 +00003154 case ISD::READPORT:
3155 // First, determine that the size of the operand falls within the acceptable
3156 // range for this architecture.
3157 //
3158 if (Node->getOperand(1).getValueType() != MVT::i16) {
3159 std::cerr << "llvm.readport: Address size is not 16 bits\n";
3160 exit(1);
3161 }
3162
3163 // Make sure we generate both values.
3164 if (Result != 1) { // Generate the token
3165 if (!ExprMap.insert(std::make_pair(N.getValue(1), 1)).second)
3166 assert(0 && "readport already emitted!?");
3167 } else
3168 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
3169
3170 Select(Node->getOperand(0)); // Select the chain.
3171
3172 // If the port is a single-byte constant, use the immediate form.
3173 if (ConstantSDNode *Port = dyn_cast<ConstantSDNode>(Node->getOperand(1)))
3174 if ((Port->getValue() & 255) == Port->getValue()) {
3175 switch (Node->getValueType(0)) {
3176 case MVT::i8:
3177 BuildMI(BB, X86::IN8ri, 1).addImm(Port->getValue());
3178 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
3179 return Result;
3180 case MVT::i16:
3181 BuildMI(BB, X86::IN16ri, 1).addImm(Port->getValue());
3182 BuildMI(BB, X86::MOV16rr, 1, Result).addReg(X86::AX);
3183 return Result;
3184 case MVT::i32:
3185 BuildMI(BB, X86::IN32ri, 1).addImm(Port->getValue());
3186 BuildMI(BB, X86::MOV32rr, 1, Result).addReg(X86::EAX);
3187 return Result;
3188 default: break;
3189 }
3190 }
3191
3192 // Now, move the I/O port address into the DX register and use the IN
3193 // instruction to get the input data.
3194 //
3195 Tmp1 = SelectExpr(Node->getOperand(1));
3196 BuildMI(BB, X86::MOV16rr, 1, X86::DX).addReg(Tmp1);
3197 switch (Node->getValueType(0)) {
3198 case MVT::i8:
3199 BuildMI(BB, X86::IN8rr, 0);
3200 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
3201 return Result;
3202 case MVT::i16:
3203 BuildMI(BB, X86::IN16rr, 0);
3204 BuildMI(BB, X86::MOV16rr, 1, Result).addReg(X86::AX);
3205 return Result;
3206 case MVT::i32:
3207 BuildMI(BB, X86::IN32rr, 0);
3208 BuildMI(BB, X86::MOV32rr, 1, Result).addReg(X86::EAX);
3209 return Result;
3210 default:
3211 std::cerr << "Cannot do input on this data type";
3212 exit(1);
3213 }
3214
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003215 }
3216
3217 return 0;
3218}
3219
Chris Lattnere10269b2005-01-17 19:25:26 +00003220/// TryToFoldLoadOpStore - Given a store node, try to fold together a
3221/// load/op/store instruction. If successful return true.
3222bool ISel::TryToFoldLoadOpStore(SDNode *Node) {
3223 assert(Node->getOpcode() == ISD::STORE && "Can only do this for stores!");
3224 SDOperand Chain = Node->getOperand(0);
3225 SDOperand StVal = Node->getOperand(1);
Chris Lattner5c659812005-01-17 22:10:42 +00003226 SDOperand StPtr = Node->getOperand(2);
Chris Lattnere10269b2005-01-17 19:25:26 +00003227
3228 // The chain has to be a load, the stored value must be an integer binary
3229 // operation with one use.
Chris Lattner5c659812005-01-17 22:10:42 +00003230 if (!StVal.Val->hasOneUse() || StVal.Val->getNumOperands() != 2 ||
Chris Lattnere10269b2005-01-17 19:25:26 +00003231 MVT::isFloatingPoint(StVal.getValueType()))
3232 return false;
3233
Chris Lattner5c659812005-01-17 22:10:42 +00003234 // Token chain must either be a factor node or the load to fold.
3235 if (Chain.getOpcode() != ISD::LOAD && Chain.getOpcode() != ISD::TokenFactor)
3236 return false;
Chris Lattnere10269b2005-01-17 19:25:26 +00003237
Chris Lattner5c659812005-01-17 22:10:42 +00003238 SDOperand TheLoad;
3239
3240 // Check to see if there is a load from the same pointer that we're storing
3241 // to in either operand of the binop.
3242 if (StVal.getOperand(0).getOpcode() == ISD::LOAD &&
3243 StVal.getOperand(0).getOperand(1) == StPtr)
3244 TheLoad = StVal.getOperand(0);
3245 else if (StVal.getOperand(1).getOpcode() == ISD::LOAD &&
3246 StVal.getOperand(1).getOperand(1) == StPtr)
3247 TheLoad = StVal.getOperand(1);
3248 else
3249 return false; // No matching load operand.
3250
3251 // We can only fold the load if there are no intervening side-effecting
3252 // operations. This means that the store uses the load as its token chain, or
3253 // there are only token factor nodes in between the store and load.
3254 if (Chain != TheLoad.getValue(1)) {
3255 // Okay, the other option is that we have a store referring to (possibly
3256 // nested) token factor nodes. For now, just try peeking through one level
3257 // of token factors to see if this is the case.
3258 bool ChainOk = false;
3259 if (Chain.getOpcode() == ISD::TokenFactor) {
3260 for (unsigned i = 0, e = Chain.getNumOperands(); i != e; ++i)
3261 if (Chain.getOperand(i) == TheLoad.getValue(1)) {
3262 ChainOk = true;
3263 break;
3264 }
3265 }
3266
3267 if (!ChainOk) return false;
3268 }
3269
3270 if (TheLoad.getOperand(1) != StPtr)
Chris Lattnere10269b2005-01-17 19:25:26 +00003271 return false;
3272
3273 // Make sure that one of the operands of the binop is the load, and that the
3274 // load folds into the binop.
3275 if (((StVal.getOperand(0) != TheLoad ||
3276 !isFoldableLoad(TheLoad, StVal.getOperand(1))) &&
3277 (StVal.getOperand(1) != TheLoad ||
3278 !isFoldableLoad(TheLoad, StVal.getOperand(0)))))
3279 return false;
3280
3281 // Finally, check to see if this is one of the ops we can handle!
3282 static const unsigned ADDTAB[] = {
3283 X86::ADD8mi, X86::ADD16mi, X86::ADD32mi,
3284 X86::ADD8mr, X86::ADD16mr, X86::ADD32mr,
3285 };
3286 static const unsigned SUBTAB[] = {
3287 X86::SUB8mi, X86::SUB16mi, X86::SUB32mi,
3288 X86::SUB8mr, X86::SUB16mr, X86::SUB32mr,
3289 };
3290 static const unsigned ANDTAB[] = {
3291 X86::AND8mi, X86::AND16mi, X86::AND32mi,
3292 X86::AND8mr, X86::AND16mr, X86::AND32mr,
3293 };
3294 static const unsigned ORTAB[] = {
3295 X86::OR8mi, X86::OR16mi, X86::OR32mi,
3296 X86::OR8mr, X86::OR16mr, X86::OR32mr,
3297 };
3298 static const unsigned XORTAB[] = {
3299 X86::XOR8mi, X86::XOR16mi, X86::XOR32mi,
3300 X86::XOR8mr, X86::XOR16mr, X86::XOR32mr,
3301 };
3302 static const unsigned SHLTAB[] = {
3303 X86::SHL8mi, X86::SHL16mi, X86::SHL32mi,
3304 /*Have to put the reg in CL*/0, 0, 0,
3305 };
3306 static const unsigned SARTAB[] = {
3307 X86::SAR8mi, X86::SAR16mi, X86::SAR32mi,
3308 /*Have to put the reg in CL*/0, 0, 0,
3309 };
3310 static const unsigned SHRTAB[] = {
3311 X86::SHR8mi, X86::SHR16mi, X86::SHR32mi,
3312 /*Have to put the reg in CL*/0, 0, 0,
3313 };
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003314
Chris Lattnere10269b2005-01-17 19:25:26 +00003315 const unsigned *TabPtr = 0;
3316 switch (StVal.getOpcode()) {
3317 default:
3318 std::cerr << "CANNOT [mem] op= val: ";
3319 StVal.Val->dump(); std::cerr << "\n";
3320 case ISD::MUL:
3321 case ISD::SDIV:
3322 case ISD::UDIV:
3323 case ISD::SREM:
3324 case ISD::UREM: return false;
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003325
Chris Lattnere10269b2005-01-17 19:25:26 +00003326 case ISD::ADD: TabPtr = ADDTAB; break;
3327 case ISD::SUB: TabPtr = SUBTAB; break;
3328 case ISD::AND: TabPtr = ANDTAB; break;
3329 case ISD:: OR: TabPtr = ORTAB; break;
3330 case ISD::XOR: TabPtr = XORTAB; break;
3331 case ISD::SHL: TabPtr = SHLTAB; break;
3332 case ISD::SRA: TabPtr = SARTAB; break;
3333 case ISD::SRL: TabPtr = SHRTAB; break;
3334 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003335
Chris Lattnere10269b2005-01-17 19:25:26 +00003336 // Handle: [mem] op= CST
3337 SDOperand Op0 = StVal.getOperand(0);
3338 SDOperand Op1 = StVal.getOperand(1);
Chris Lattner0a078832005-01-23 23:20:06 +00003339 unsigned Opc = 0;
Chris Lattnere10269b2005-01-17 19:25:26 +00003340 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1)) {
3341 switch (Op0.getValueType()) { // Use Op0's type because of shifts.
3342 default: break;
3343 case MVT::i1:
3344 case MVT::i8: Opc = TabPtr[0]; break;
3345 case MVT::i16: Opc = TabPtr[1]; break;
3346 case MVT::i32: Opc = TabPtr[2]; break;
3347 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003348
Chris Lattnere10269b2005-01-17 19:25:26 +00003349 if (Opc) {
Chris Lattner4a108662005-01-18 03:51:59 +00003350 if (!ExprMap.insert(std::make_pair(TheLoad.getValue(1), 1)).second)
3351 assert(0 && "Already emitted?");
Chris Lattner5c659812005-01-17 22:10:42 +00003352 Select(Chain);
3353
Chris Lattnere10269b2005-01-17 19:25:26 +00003354 X86AddressMode AM;
3355 if (getRegPressure(TheLoad.getOperand(0)) >
3356 getRegPressure(TheLoad.getOperand(1))) {
3357 Select(TheLoad.getOperand(0));
3358 SelectAddress(TheLoad.getOperand(1), AM);
3359 } else {
3360 SelectAddress(TheLoad.getOperand(1), AM);
3361 Select(TheLoad.getOperand(0));
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003362 }
Chris Lattner5c659812005-01-17 22:10:42 +00003363
3364 if (StVal.getOpcode() == ISD::ADD) {
3365 if (CN->getValue() == 1) {
3366 switch (Op0.getValueType()) {
3367 default: break;
3368 case MVT::i8:
3369 addFullAddress(BuildMI(BB, X86::INC8m, 4), AM);
3370 return true;
3371 case MVT::i16: Opc = TabPtr[1];
3372 addFullAddress(BuildMI(BB, X86::INC16m, 4), AM);
3373 return true;
3374 case MVT::i32: Opc = TabPtr[2];
3375 addFullAddress(BuildMI(BB, X86::INC32m, 4), AM);
3376 return true;
3377 }
3378 } else if (CN->getValue()+1 == 0) { // [X] += -1 -> DEC [X]
3379 switch (Op0.getValueType()) {
3380 default: break;
3381 case MVT::i8:
3382 addFullAddress(BuildMI(BB, X86::DEC8m, 4), AM);
3383 return true;
3384 case MVT::i16: Opc = TabPtr[1];
3385 addFullAddress(BuildMI(BB, X86::DEC16m, 4), AM);
3386 return true;
3387 case MVT::i32: Opc = TabPtr[2];
3388 addFullAddress(BuildMI(BB, X86::DEC32m, 4), AM);
3389 return true;
3390 }
3391 }
3392 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003393
Chris Lattnere10269b2005-01-17 19:25:26 +00003394 addFullAddress(BuildMI(BB, Opc, 4+1),AM).addImm(CN->getValue());
3395 return true;
3396 }
3397 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003398
Chris Lattnere10269b2005-01-17 19:25:26 +00003399 // If we have [mem] = V op [mem], try to turn it into:
3400 // [mem] = [mem] op V.
3401 if (Op1 == TheLoad && StVal.getOpcode() != ISD::SUB &&
3402 StVal.getOpcode() != ISD::SHL && StVal.getOpcode() != ISD::SRA &&
3403 StVal.getOpcode() != ISD::SRL)
3404 std::swap(Op0, Op1);
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003405
Chris Lattnere10269b2005-01-17 19:25:26 +00003406 if (Op0 != TheLoad) return false;
3407
3408 switch (Op0.getValueType()) {
3409 default: return false;
3410 case MVT::i1:
3411 case MVT::i8: Opc = TabPtr[3]; break;
3412 case MVT::i16: Opc = TabPtr[4]; break;
3413 case MVT::i32: Opc = TabPtr[5]; break;
3414 }
Chris Lattner5c659812005-01-17 22:10:42 +00003415
Chris Lattnerb422aea2005-01-18 17:35:28 +00003416 // Table entry doesn't exist?
3417 if (Opc == 0) return false;
3418
Chris Lattner4a108662005-01-18 03:51:59 +00003419 if (!ExprMap.insert(std::make_pair(TheLoad.getValue(1), 1)).second)
3420 assert(0 && "Already emitted?");
Chris Lattner5c659812005-01-17 22:10:42 +00003421 Select(Chain);
Chris Lattnere10269b2005-01-17 19:25:26 +00003422 Select(TheLoad.getOperand(0));
Chris Lattner98a8ba02005-01-18 01:06:26 +00003423
Chris Lattnere10269b2005-01-17 19:25:26 +00003424 X86AddressMode AM;
3425 SelectAddress(TheLoad.getOperand(1), AM);
3426 unsigned Reg = SelectExpr(Op1);
Chris Lattner98a8ba02005-01-18 01:06:26 +00003427 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addReg(Reg);
Chris Lattnere10269b2005-01-17 19:25:26 +00003428 return true;
3429}
3430
3431
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003432void ISel::Select(SDOperand N) {
3433 unsigned Tmp1, Tmp2, Opc;
3434
Nate Begeman85fdeb22005-03-24 04:39:54 +00003435 if (!ExprMap.insert(std::make_pair(N, 1)).second)
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003436 return; // Already selected.
3437
Chris Lattner989de032005-01-11 06:14:36 +00003438 SDNode *Node = N.Val;
3439
3440 switch (Node->getOpcode()) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003441 default:
Chris Lattner989de032005-01-11 06:14:36 +00003442 Node->dump(); std::cerr << "\n";
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003443 assert(0 && "Node not handled yet!");
3444 case ISD::EntryToken: return; // Noop
Chris Lattnerc3580712005-01-13 18:01:36 +00003445 case ISD::TokenFactor:
Chris Lattner1d50b7f2005-01-13 19:56:00 +00003446 if (Node->getNumOperands() == 2) {
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003447 bool OneFirst =
Chris Lattner1d50b7f2005-01-13 19:56:00 +00003448 getRegPressure(Node->getOperand(1))>getRegPressure(Node->getOperand(0));
3449 Select(Node->getOperand(OneFirst));
3450 Select(Node->getOperand(!OneFirst));
3451 } else {
3452 std::vector<std::pair<unsigned, unsigned> > OpsP;
3453 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
3454 OpsP.push_back(std::make_pair(getRegPressure(Node->getOperand(i)), i));
3455 std::sort(OpsP.begin(), OpsP.end());
3456 std::reverse(OpsP.begin(), OpsP.end());
3457 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
3458 Select(Node->getOperand(OpsP[i].second));
3459 }
Chris Lattnerc3580712005-01-13 18:01:36 +00003460 return;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003461 case ISD::CopyToReg:
Chris Lattneref6806c2005-01-12 02:02:48 +00003462 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
3463 Select(N.getOperand(0));
3464 Tmp1 = SelectExpr(N.getOperand(1));
3465 } else {
3466 Tmp1 = SelectExpr(N.getOperand(1));
3467 Select(N.getOperand(0));
3468 }
Chris Lattner18c2f132005-01-13 20:50:02 +00003469 Tmp2 = cast<RegSDNode>(N)->getReg();
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003470
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003471 if (Tmp1 != Tmp2) {
3472 switch (N.getOperand(1).getValueType()) {
3473 default: assert(0 && "Invalid type for operation!");
3474 case MVT::i1:
3475 case MVT::i8: Opc = X86::MOV8rr; break;
3476 case MVT::i16: Opc = X86::MOV16rr; break;
3477 case MVT::i32: Opc = X86::MOV32rr; break;
Chris Lattneref7ba072005-01-11 03:50:45 +00003478 case MVT::f64: Opc = X86::FpMOV; ContainsFPCode = true; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003479 }
3480 BuildMI(BB, Opc, 1, Tmp2).addReg(Tmp1);
3481 }
3482 return;
3483 case ISD::RET:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003484 switch (N.getNumOperands()) {
3485 default:
3486 assert(0 && "Unknown return instruction!");
3487 case 3:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003488 assert(N.getOperand(1).getValueType() == MVT::i32 &&
3489 N.getOperand(2).getValueType() == MVT::i32 &&
3490 "Unknown two-register value!");
Chris Lattner11333092005-01-11 03:11:44 +00003491 if (getRegPressure(N.getOperand(1)) > getRegPressure(N.getOperand(2))) {
3492 Tmp1 = SelectExpr(N.getOperand(1));
3493 Tmp2 = SelectExpr(N.getOperand(2));
3494 } else {
3495 Tmp2 = SelectExpr(N.getOperand(2));
3496 Tmp1 = SelectExpr(N.getOperand(1));
3497 }
3498 Select(N.getOperand(0));
3499
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003500 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
3501 BuildMI(BB, X86::MOV32rr, 1, X86::EDX).addReg(Tmp2);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003502 break;
3503 case 2:
Chris Lattner11333092005-01-11 03:11:44 +00003504 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
3505 Select(N.getOperand(0));
3506 Tmp1 = SelectExpr(N.getOperand(1));
3507 } else {
3508 Tmp1 = SelectExpr(N.getOperand(1));
3509 Select(N.getOperand(0));
3510 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003511 switch (N.getOperand(1).getValueType()) {
3512 default: assert(0 && "All other types should have been promoted!!");
3513 case MVT::f64:
3514 BuildMI(BB, X86::FpSETRESULT, 1).addReg(Tmp1);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003515 break;
3516 case MVT::i32:
3517 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003518 break;
3519 }
3520 break;
3521 case 1:
Chris Lattner11333092005-01-11 03:11:44 +00003522 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003523 break;
3524 }
Chris Lattner3648c672005-05-13 21:44:04 +00003525 if (X86Lowering.getBytesToPopOnReturn() == 0)
3526 BuildMI(BB, X86::RET, 0); // Just emit a 'ret' instruction
3527 else
3528 BuildMI(BB, X86::RETI, 1).addImm(X86Lowering.getBytesToPopOnReturn());
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003529 return;
3530 case ISD::BR: {
3531 Select(N.getOperand(0));
3532 MachineBasicBlock *Dest =
3533 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
3534 BuildMI(BB, X86::JMP, 1).addMBB(Dest);
3535 return;
3536 }
3537
3538 case ISD::BRCOND: {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003539 MachineBasicBlock *Dest =
3540 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
Chris Lattner11333092005-01-11 03:11:44 +00003541
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003542 // Try to fold a setcc into the branch. If this fails, emit a test/jne
3543 // pair.
Chris Lattner6c07aee2005-01-11 04:06:27 +00003544 if (EmitBranchCC(Dest, N.getOperand(0), N.getOperand(1))) {
3545 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
3546 Select(N.getOperand(0));
3547 Tmp1 = SelectExpr(N.getOperand(1));
3548 } else {
3549 Tmp1 = SelectExpr(N.getOperand(1));
3550 Select(N.getOperand(0));
3551 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003552 BuildMI(BB, X86::TEST8rr, 2).addReg(Tmp1).addReg(Tmp1);
3553 BuildMI(BB, X86::JNE, 1).addMBB(Dest);
3554 }
Chris Lattner11333092005-01-11 03:11:44 +00003555
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003556 return;
3557 }
Chris Lattnere9ef81d2005-01-15 05:22:24 +00003558
Chris Lattner4df0de92005-01-17 00:00:33 +00003559 case ISD::LOAD:
3560 // If this load could be folded into the only using instruction, and if it
3561 // is safe to emit the instruction here, try to do so now.
3562 if (Node->hasNUsesOfValue(1, 0)) {
3563 SDOperand TheVal = N.getValue(0);
3564 SDNode *User = 0;
3565 for (SDNode::use_iterator UI = Node->use_begin(); ; ++UI) {
3566 assert(UI != Node->use_end() && "Didn't find use!");
3567 SDNode *UN = *UI;
3568 for (unsigned i = 0, e = UN->getNumOperands(); i != e; ++i)
3569 if (UN->getOperand(i) == TheVal) {
3570 User = UN;
3571 goto FoundIt;
3572 }
3573 }
3574 FoundIt:
3575 // Only handle unary operators right now.
3576 if (User->getNumOperands() == 1) {
Chris Lattner4a108662005-01-18 03:51:59 +00003577 ExprMap.erase(N);
Chris Lattner4df0de92005-01-17 00:00:33 +00003578 SelectExpr(SDOperand(User, 0));
3579 return;
3580 }
3581 }
Chris Lattnerb71f8fc2005-01-18 04:00:54 +00003582 ExprMap.erase(N);
Chris Lattner4df0de92005-01-17 00:00:33 +00003583 SelectExpr(N);
3584 return;
Chris Lattner966cdfb2005-05-09 21:17:38 +00003585 case ISD::READPORT:
Chris Lattnere9ef81d2005-01-15 05:22:24 +00003586 case ISD::EXTLOAD:
3587 case ISD::SEXTLOAD:
3588 case ISD::ZEXTLOAD:
Chris Lattner2e7714a2005-05-13 20:29:13 +00003589 case ISD::TAILCALL:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003590 case ISD::CALL:
3591 case ISD::DYNAMIC_STACKALLOC:
Chris Lattnerb71f8fc2005-01-18 04:00:54 +00003592 ExprMap.erase(N);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003593 SelectExpr(N);
3594 return;
Chris Lattnerc6f41812005-05-12 23:06:28 +00003595 case ISD::CopyFromReg:
3596 ExprMap.erase(N);
3597 SelectExpr(N.getValue(0));
3598 return;
Chris Lattnere9ef81d2005-01-15 05:22:24 +00003599
3600 case ISD::TRUNCSTORE: { // truncstore chain, val, ptr :storety
3601 // On X86, we can represent all types except for Bool and Float natively.
3602 X86AddressMode AM;
3603 MVT::ValueType StoredTy = cast<MVTSDNode>(Node)->getExtraValueType();
Chris Lattnerda2ce112005-01-16 07:34:08 +00003604 assert((StoredTy == MVT::i1 || StoredTy == MVT::f32 ||
3605 StoredTy == MVT::i16 /*FIXME: THIS IS JUST FOR TESTING!*/)
3606 && "Unsupported TRUNCSTORE for this target!");
3607
3608 if (StoredTy == MVT::i16) {
3609 // FIXME: This is here just to allow testing. X86 doesn't really have a
3610 // TRUNCSTORE i16 operation, but this is required for targets that do not
3611 // have 16-bit integer registers. We occasionally disable 16-bit integer
3612 // registers to test the promotion code.
3613 Select(N.getOperand(0));
3614 Tmp1 = SelectExpr(N.getOperand(1));
3615 SelectAddress(N.getOperand(2), AM);
3616
3617 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
3618 addFullAddress(BuildMI(BB, X86::MOV16mr, 5), AM).addReg(X86::AX);
3619 return;
3620 }
Chris Lattnere9ef81d2005-01-15 05:22:24 +00003621
3622 // Store of constant bool?
3623 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
3624 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(2))) {
3625 Select(N.getOperand(0));
3626 SelectAddress(N.getOperand(2), AM);
3627 } else {
3628 SelectAddress(N.getOperand(2), AM);
3629 Select(N.getOperand(0));
3630 }
3631 addFullAddress(BuildMI(BB, X86::MOV8mi, 5), AM).addImm(CN->getValue());
3632 return;
3633 }
3634
3635 switch (StoredTy) {
3636 default: assert(0 && "Cannot truncstore this type!");
3637 case MVT::i1: Opc = X86::MOV8mr; break;
3638 case MVT::f32: Opc = X86::FST32m; break;
3639 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003640
Chris Lattnere9ef81d2005-01-15 05:22:24 +00003641 std::vector<std::pair<unsigned, unsigned> > RP;
3642 RP.push_back(std::make_pair(getRegPressure(N.getOperand(0)), 0));
3643 RP.push_back(std::make_pair(getRegPressure(N.getOperand(1)), 1));
3644 RP.push_back(std::make_pair(getRegPressure(N.getOperand(2)), 2));
3645 std::sort(RP.begin(), RP.end());
3646
Chris Lattner572dd082005-02-23 05:57:21 +00003647 Tmp1 = 0; // Silence a warning.
Chris Lattnere9ef81d2005-01-15 05:22:24 +00003648 for (unsigned i = 0; i != 3; ++i)
3649 switch (RP[2-i].second) {
3650 default: assert(0 && "Unknown operand number!");
3651 case 0: Select(N.getOperand(0)); break;
3652 case 1: Tmp1 = SelectExpr(N.getOperand(1)); break;
3653 case 2: SelectAddress(N.getOperand(2), AM); break;
3654 }
3655
3656 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addReg(Tmp1);
3657 return;
3658 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003659 case ISD::STORE: {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003660 X86AddressMode AM;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003661
3662 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
3663 Opc = 0;
3664 switch (CN->getValueType(0)) {
3665 default: assert(0 && "Invalid type for operation!");
3666 case MVT::i1:
3667 case MVT::i8: Opc = X86::MOV8mi; break;
3668 case MVT::i16: Opc = X86::MOV16mi; break;
3669 case MVT::i32: Opc = X86::MOV32mi; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003670 case MVT::f64: break;
3671 }
3672 if (Opc) {
Chris Lattner11333092005-01-11 03:11:44 +00003673 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(2))) {
3674 Select(N.getOperand(0));
3675 SelectAddress(N.getOperand(2), AM);
3676 } else {
3677 SelectAddress(N.getOperand(2), AM);
3678 Select(N.getOperand(0));
3679 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003680 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addImm(CN->getValue());
3681 return;
3682 }
Chris Lattner75f354b2005-04-21 19:03:24 +00003683 } else if (GlobalAddressSDNode *GA =
3684 dyn_cast<GlobalAddressSDNode>(N.getOperand(1))) {
3685 assert(GA->getValueType(0) == MVT::i32 && "Bad pointer operand");
3686
3687 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(2))) {
3688 Select(N.getOperand(0));
3689 SelectAddress(N.getOperand(2), AM);
3690 } else {
3691 SelectAddress(N.getOperand(2), AM);
3692 Select(N.getOperand(0));
3693 }
3694 addFullAddress(BuildMI(BB, X86::MOV32mi, 4+1),
3695 AM).addGlobalAddress(GA->getGlobal());
3696 return;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003697 }
Chris Lattner837caa72005-01-11 23:21:30 +00003698
3699 // Check to see if this is a load/op/store combination.
Chris Lattnere10269b2005-01-17 19:25:26 +00003700 if (TryToFoldLoadOpStore(Node))
3701 return;
Chris Lattner837caa72005-01-11 23:21:30 +00003702
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003703 switch (N.getOperand(1).getValueType()) {
3704 default: assert(0 && "Cannot store this type!");
3705 case MVT::i1:
3706 case MVT::i8: Opc = X86::MOV8mr; break;
3707 case MVT::i16: Opc = X86::MOV16mr; break;
3708 case MVT::i32: Opc = X86::MOV32mr; break;
Chris Lattneref7ba072005-01-11 03:50:45 +00003709 case MVT::f64: Opc = X86::FST64m; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003710 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003711
Chris Lattner11333092005-01-11 03:11:44 +00003712 std::vector<std::pair<unsigned, unsigned> > RP;
3713 RP.push_back(std::make_pair(getRegPressure(N.getOperand(0)), 0));
3714 RP.push_back(std::make_pair(getRegPressure(N.getOperand(1)), 1));
3715 RP.push_back(std::make_pair(getRegPressure(N.getOperand(2)), 2));
3716 std::sort(RP.begin(), RP.end());
3717
Chris Lattner572dd082005-02-23 05:57:21 +00003718 Tmp1 = 0; // Silence a warning.
Chris Lattner11333092005-01-11 03:11:44 +00003719 for (unsigned i = 0; i != 3; ++i)
3720 switch (RP[2-i].second) {
3721 default: assert(0 && "Unknown operand number!");
3722 case 0: Select(N.getOperand(0)); break;
3723 case 1: Tmp1 = SelectExpr(N.getOperand(1)); break;
Chris Lattnera3aa2e22005-01-11 03:37:59 +00003724 case 2: SelectAddress(N.getOperand(2), AM); break;
Chris Lattner11333092005-01-11 03:11:44 +00003725 }
3726
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003727 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addReg(Tmp1);
3728 return;
3729 }
Chris Lattner16cd04d2005-05-12 23:24:06 +00003730 case ISD::CALLSEQ_START:
Chris Lattner3648c672005-05-13 21:44:04 +00003731 Select(N.getOperand(0));
3732 // Stack amount
3733 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
3734 BuildMI(BB, X86::ADJCALLSTACKDOWN, 1).addImm(Tmp1);
3735 return;
Chris Lattner16cd04d2005-05-12 23:24:06 +00003736 case ISD::CALLSEQ_END:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003737 Select(N.getOperand(0));
Chris Lattner3648c672005-05-13 21:44:04 +00003738 // Stack amount
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003739 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003740
Chris Lattner3648c672005-05-13 21:44:04 +00003741 // Amount the callee added to the stack pointer.
3742 Tmp2 = cast<ConstantSDNode>(N.getOperand(2))->getValue();
3743 BuildMI(BB, X86::ADJCALLSTACKUP, 2).addImm(Tmp1).addImm(Tmp2);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003744 return;
Chris Lattner989de032005-01-11 06:14:36 +00003745 case ISD::MEMSET: {
3746 Select(N.getOperand(0)); // Select the chain.
3747 unsigned Align =
3748 (unsigned)cast<ConstantSDNode>(Node->getOperand(4))->getValue();
3749 if (Align == 0) Align = 1;
3750
3751 // Turn the byte code into # iterations
3752 unsigned CountReg;
3753 unsigned Opcode;
3754 if (ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Node->getOperand(2))) {
3755 unsigned Val = ValC->getValue() & 255;
3756
3757 // If the value is a constant, then we can potentially use larger sets.
3758 switch (Align & 3) {
3759 case 2: // WORD aligned
3760 CountReg = MakeReg(MVT::i32);
3761 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
3762 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/2);
3763 } else {
3764 unsigned ByteReg = SelectExpr(Node->getOperand(3));
3765 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(1);
3766 }
3767 BuildMI(BB, X86::MOV16ri, 1, X86::AX).addImm((Val << 8) | Val);
3768 Opcode = X86::REP_STOSW;
3769 break;
3770 case 0: // DWORD aligned
3771 CountReg = MakeReg(MVT::i32);
3772 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
3773 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/4);
3774 } else {
3775 unsigned ByteReg = SelectExpr(Node->getOperand(3));
3776 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(2);
3777 }
3778 Val = (Val << 8) | Val;
3779 BuildMI(BB, X86::MOV32ri, 1, X86::EAX).addImm((Val << 16) | Val);
3780 Opcode = X86::REP_STOSD;
3781 break;
3782 default: // BYTE aligned
3783 CountReg = SelectExpr(Node->getOperand(3));
3784 BuildMI(BB, X86::MOV8ri, 1, X86::AL).addImm(Val);
3785 Opcode = X86::REP_STOSB;
3786 break;
3787 }
3788 } else {
3789 // If it's not a constant value we are storing, just fall back. We could
3790 // try to be clever to form 16 bit and 32 bit values, but we don't yet.
3791 unsigned ValReg = SelectExpr(Node->getOperand(2));
3792 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(ValReg);
3793 CountReg = SelectExpr(Node->getOperand(3));
3794 Opcode = X86::REP_STOSB;
3795 }
3796
3797 // No matter what the alignment is, we put the source in ESI, the
3798 // destination in EDI, and the count in ECX.
3799 unsigned TmpReg1 = SelectExpr(Node->getOperand(1));
3800 BuildMI(BB, X86::MOV32rr, 1, X86::ECX).addReg(CountReg);
3801 BuildMI(BB, X86::MOV32rr, 1, X86::EDI).addReg(TmpReg1);
3802 BuildMI(BB, Opcode, 0);
3803 return;
3804 }
Chris Lattner966cdfb2005-05-09 21:17:38 +00003805 case ISD::MEMCPY: {
Chris Lattner31805bf2005-01-11 06:19:26 +00003806 Select(N.getOperand(0)); // Select the chain.
3807 unsigned Align =
3808 (unsigned)cast<ConstantSDNode>(Node->getOperand(4))->getValue();
3809 if (Align == 0) Align = 1;
3810
3811 // Turn the byte code into # iterations
3812 unsigned CountReg;
3813 unsigned Opcode;
3814 switch (Align & 3) {
3815 case 2: // WORD aligned
3816 CountReg = MakeReg(MVT::i32);
3817 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
3818 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/2);
3819 } else {
3820 unsigned ByteReg = SelectExpr(Node->getOperand(3));
3821 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(1);
3822 }
3823 Opcode = X86::REP_MOVSW;
3824 break;
3825 case 0: // DWORD aligned
3826 CountReg = MakeReg(MVT::i32);
3827 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
3828 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/4);
3829 } else {
3830 unsigned ByteReg = SelectExpr(Node->getOperand(3));
3831 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(2);
3832 }
3833 Opcode = X86::REP_MOVSD;
3834 break;
3835 default: // BYTE aligned
3836 CountReg = SelectExpr(Node->getOperand(3));
3837 Opcode = X86::REP_MOVSB;
3838 break;
3839 }
3840
3841 // No matter what the alignment is, we put the source in ESI, the
3842 // destination in EDI, and the count in ECX.
3843 unsigned TmpReg1 = SelectExpr(Node->getOperand(1));
3844 unsigned TmpReg2 = SelectExpr(Node->getOperand(2));
3845 BuildMI(BB, X86::MOV32rr, 1, X86::ECX).addReg(CountReg);
3846 BuildMI(BB, X86::MOV32rr, 1, X86::EDI).addReg(TmpReg1);
3847 BuildMI(BB, X86::MOV32rr, 1, X86::ESI).addReg(TmpReg2);
3848 BuildMI(BB, Opcode, 0);
3849 return;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003850 }
Chris Lattner966cdfb2005-05-09 21:17:38 +00003851 case ISD::WRITEPORT:
3852 if (Node->getOperand(2).getValueType() != MVT::i16) {
3853 std::cerr << "llvm.writeport: Address size is not 16 bits\n";
3854 exit(1);
3855 }
3856 Select(Node->getOperand(0)); // Emit the chain.
3857
3858 Tmp1 = SelectExpr(Node->getOperand(1));
3859 switch (Node->getOperand(1).getValueType()) {
3860 case MVT::i8:
3861 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(Tmp1);
3862 Tmp2 = X86::OUT8ir; Opc = X86::OUT8rr;
3863 break;
3864 case MVT::i16:
3865 BuildMI(BB, X86::MOV16rr, 1, X86::AX).addReg(Tmp1);
3866 Tmp2 = X86::OUT16ir; Opc = X86::OUT16rr;
3867 break;
3868 case MVT::i32:
3869 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
3870 Tmp2 = X86::OUT32ir; Opc = X86::OUT32rr;
3871 break;
3872 default:
3873 std::cerr << "llvm.writeport: invalid data type for X86 target";
3874 exit(1);
3875 }
3876
3877 // If the port is a single-byte constant, use the immediate form.
3878 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Node->getOperand(2)))
3879 if ((CN->getValue() & 255) == CN->getValue()) {
3880 BuildMI(BB, Tmp2, 1).addImm(CN->getValue());
3881 return;
3882 }
3883
3884 // Otherwise, move the I/O port address into the DX register.
3885 unsigned Reg = SelectExpr(Node->getOperand(2));
3886 BuildMI(BB, X86::MOV16rr, 1, X86::DX).addReg(Reg);
3887 BuildMI(BB, Opc, 0);
3888 return;
3889 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003890 assert(0 && "Should not be reached!");
3891}
3892
3893
3894/// createX86PatternInstructionSelector - This pass converts an LLVM function
3895/// into a machine code representation using pattern matching and a machine
3896/// description file.
3897///
3898FunctionPass *llvm::createX86PatternInstructionSelector(TargetMachine &TM) {
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003899 return new ISel(TM);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003900}