blob: a8e88818a85292589ead1c2b807e0e9bdf9b5681 [file] [log] [blame]
Chris Lattner88c8a232005-01-07 07:49:41 +00001//===-- X86ISelPattern.cpp - A pattern matching inst selector for X86 -----===//
Chris Lattner1d13a922005-01-10 22:10:13 +00002//
Chris Lattner88c8a232005-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 Brukmanc88330a2005-04-21 23:38:14 +00007//
Chris Lattner88c8a232005-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 Lattner7ce7a8f2005-05-12 23:06:28 +000017#include "llvm/CallingConv.h"
Chris Lattner6972c312005-05-09 03:36:39 +000018#include "llvm/Constants.h"
19#include "llvm/Instructions.h"
Chris Lattner88c8a232005-01-07 07:49:41 +000020#include "llvm/Function.h"
Chris Lattner6972c312005-05-09 03:36:39 +000021#include "llvm/CodeGen/MachineConstantPool.h"
Chris Lattner88c8a232005-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 Lattnerdb68d392005-04-30 04:25:35 +000029#include "llvm/Target/TargetOptions.h"
Chris Lattner6972c312005-05-09 03:36:39 +000030#include "llvm/Support/CFG.h"
Chris Lattner88c8a232005-01-07 07:49:41 +000031#include "llvm/Support/MathExtras.h"
32#include "llvm/ADT/Statistic.h"
33#include <set>
Jeff Cohen407aa012005-01-12 04:29:05 +000034#include <algorithm>
Chris Lattner88c8a232005-01-07 07:49:41 +000035using namespace llvm;
36
Chris Lattner7ce7a8f2005-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 Lattner88c8a232005-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 Lattner9f59d282005-01-09 00:01:27 +000047 int ReturnAddrIndex; // FrameIndex for return slot.
Chris Lattner88c8a232005-01-07 07:49:41 +000048 public:
49 X86TargetLowering(TargetMachine &TM) : TargetLowering(TM) {
50 // Set up the TargetLowering object.
Chris Lattnerc1f386c2005-01-17 00:00:33 +000051
52 // X86 is wierd, it always uses i8 for shift amounts and setcc results.
53 setShiftAmountType(MVT::i8);
54 setSetCCResultType(MVT::i8);
Chris Lattner38fd9702005-04-07 19:41:46 +000055 setSetCCResultContents(ZeroOrOneSetCCResult);
Chris Lattnerd8d30662005-01-19 03:36:30 +000056 setShiftAmountFlavor(Mask); // shl X, 32 == shl X, 0
Chris Lattnerc1f386c2005-01-17 00:00:33 +000057
58 // Set up the register classes.
Chris Lattner88c8a232005-01-07 07:49:41 +000059 addRegisterClass(MVT::i8, X86::R8RegisterClass);
60 addRegisterClass(MVT::i16, X86::R16RegisterClass);
61 addRegisterClass(MVT::i32, X86::R32RegisterClass);
62 addRegisterClass(MVT::f64, X86::RFPRegisterClass);
Misha Brukmanc88330a2005-04-21 23:38:14 +000063
Chris Lattner88c8a232005-01-07 07:49:41 +000064 // FIXME: Eliminate these two classes when legalize can handle promotions
65 // well.
Chris Lattnerb14a63a2005-01-16 07:34:08 +000066/**/ addRegisterClass(MVT::i1, X86::R8RegisterClass);
Chris Lattnerb14a63a2005-01-16 07:34:08 +000067
Chris Lattnera3a135a2005-04-09 03:22:37 +000068 setOperationAction(ISD::BRCONDTWOWAY , MVT::Other, Expand);
Chris Lattnerb14a63a2005-01-16 07:34:08 +000069 setOperationAction(ISD::MEMMOVE , MVT::Other, Expand);
70 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Expand);
Chris Lattnerb14a63a2005-01-16 07:34:08 +000071 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
Chris Lattnerb14a63a2005-01-16 07:34:08 +000072 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
73 setOperationAction(ISD::SEXTLOAD , MVT::i1 , Expand);
74 setOperationAction(ISD::SREM , MVT::f64 , Expand);
Chris Lattner05ad4b82005-05-11 05:00:34 +000075 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
76 setOperationAction(ISD::CTTZ , MVT::i8 , Expand);
77 setOperationAction(ISD::CTLZ , MVT::i8 , Expand);
78 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
79 setOperationAction(ISD::CTTZ , MVT::i16 , Expand);
80 setOperationAction(ISD::CTLZ , MVT::i16 , Expand);
Andrew Lenharth5e177822005-05-03 17:19:30 +000081 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
82 setOperationAction(ISD::CTTZ , MVT::i32 , Expand);
Andrew Lenharthb8e94c32005-05-04 19:25:37 +000083 setOperationAction(ISD::CTLZ , MVT::i32 , Expand);
Chris Lattner0b7e4cd2005-04-02 05:03:24 +000084
Chris Lattner6c6a39a2005-05-09 20:37:29 +000085 setOperationAction(ISD::READIO , MVT::i1 , Expand);
86 setOperationAction(ISD::READIO , MVT::i8 , Expand);
87 setOperationAction(ISD::READIO , MVT::i16 , Expand);
88 setOperationAction(ISD::READIO , MVT::i32 , Expand);
89 setOperationAction(ISD::WRITEIO , MVT::i1 , Expand);
90 setOperationAction(ISD::WRITEIO , MVT::i8 , Expand);
91 setOperationAction(ISD::WRITEIO , MVT::i16 , Expand);
92 setOperationAction(ISD::WRITEIO , MVT::i32 , Expand);
93
Chris Lattnerdb68d392005-04-30 04:25:35 +000094 if (!UnsafeFPMath) {
95 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
96 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
97 }
98
Chris Lattnerb14a63a2005-01-16 07:34:08 +000099 // These should be promoted to a larger select which is supported.
100/**/ setOperationAction(ISD::SELECT , MVT::i1 , Promote);
101 setOperationAction(ISD::SELECT , MVT::i8 , Promote);
Misha Brukmanc88330a2005-04-21 23:38:14 +0000102
Chris Lattner88c8a232005-01-07 07:49:41 +0000103 computeRegisterProperties();
Misha Brukmanc88330a2005-04-21 23:38:14 +0000104
Chris Lattner88c8a232005-01-07 07:49:41 +0000105 addLegalFPImmediate(+0.0); // FLD0
106 addLegalFPImmediate(+1.0); // FLD1
107 addLegalFPImmediate(-0.0); // FLD0/FCHS
108 addLegalFPImmediate(-1.0); // FLD1/FCHS
109 }
110
111 /// LowerArguments - This hook must be implemented to indicate how we should
112 /// lower the arguments for the specified function, into the specified DAG.
113 virtual std::vector<SDOperand>
114 LowerArguments(Function &F, SelectionDAG &DAG);
115
116 /// LowerCallTo - This hook lowers an abstract call to a function into an
117 /// actual call.
Chris Lattnerb52e0412005-01-08 19:28:19 +0000118 virtual std::pair<SDOperand, SDOperand>
Chris Lattner36674a12005-05-12 19:56:45 +0000119 LowerCallTo(SDOperand Chain, const Type *RetTy, bool isVarArg, unsigned CC,
Nate Begemanf6565252005-03-26 01:29:23 +0000120 SDOperand Callee, ArgListTy &Args, SelectionDAG &DAG);
Chris Lattner9f59d282005-01-09 00:01:27 +0000121
122 virtual std::pair<SDOperand, SDOperand>
123 LowerVAStart(SDOperand Chain, SelectionDAG &DAG);
124
125 virtual std::pair<SDOperand,SDOperand>
126 LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
127 const Type *ArgTy, SelectionDAG &DAG);
128
129 virtual std::pair<SDOperand, SDOperand>
130 LowerFrameReturnAddress(bool isFrameAddr, SDOperand Chain, unsigned Depth,
131 SelectionDAG &DAG);
Chris Lattner7ce7a8f2005-05-12 23:06:28 +0000132 private:
133 // C Calling Convention implementation.
134 std::vector<SDOperand> LowerCCCArguments(Function &F, SelectionDAG &DAG);
135 std::pair<SDOperand, SDOperand>
136 LowerCCCCallTo(SDOperand Chain, const Type *RetTy, bool isVarArg,
137 SDOperand Callee, ArgListTy &Args, SelectionDAG &DAG);
138
139 // Fast Calling Convention implementation.
140 std::vector<SDOperand> LowerFastCCArguments(Function &F, SelectionDAG &DAG);
141 std::pair<SDOperand, SDOperand>
142 LowerFastCCCallTo(SDOperand Chain, const Type *RetTy,
143 SDOperand Callee, ArgListTy &Args, SelectionDAG &DAG);
Chris Lattner88c8a232005-01-07 07:49:41 +0000144 };
145}
146
Chris Lattner88c8a232005-01-07 07:49:41 +0000147std::vector<SDOperand>
148X86TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
Chris Lattner7ce7a8f2005-05-12 23:06:28 +0000149 if (F.getCallingConv() == CallingConv::Fast && EnableFastCC)
150 return LowerFastCCArguments(F, DAG);
151 return LowerCCCArguments(F, DAG);
152}
153
154std::pair<SDOperand, SDOperand>
155X86TargetLowering::LowerCallTo(SDOperand Chain, const Type *RetTy,
156 bool isVarArg, unsigned CallingConv,
157 SDOperand Callee, ArgListTy &Args,
158 SelectionDAG &DAG) {
159 assert((!isVarArg || CallingConv == CallingConv::C) &&
160 "Only C takes varargs!");
161 if (CallingConv == CallingConv::Fast && EnableFastCC)
162 return LowerFastCCCallTo(Chain, RetTy, Callee, Args, DAG);
163 return LowerCCCCallTo(Chain, RetTy, isVarArg, Callee, Args, DAG);
164}
165
166//===----------------------------------------------------------------------===//
167// C Calling Convention implementation
168//===----------------------------------------------------------------------===//
169
170std::vector<SDOperand>
171X86TargetLowering::LowerCCCArguments(Function &F, SelectionDAG &DAG) {
Chris Lattner88c8a232005-01-07 07:49:41 +0000172 std::vector<SDOperand> ArgValues;
173
Chris Lattnerd8145bc2005-05-10 03:53:18 +0000174 MachineFunction &MF = DAG.getMachineFunction();
175 MachineFrameInfo *MFI = MF.getFrameInfo();
176
Chris Lattner88c8a232005-01-07 07:49:41 +0000177 // Add DAG nodes to load the arguments... On entry to a function on the X86,
178 // the stack frame looks like this:
179 //
180 // [ESP] -- return address
181 // [ESP + 4] -- first argument (leftmost lexically)
182 // [ESP + 8] -- second argument, if first argument is four bytes in size
Misha Brukmanc88330a2005-04-21 23:38:14 +0000183 // ...
Chris Lattner88c8a232005-01-07 07:49:41 +0000184 //
Chris Lattner88c8a232005-01-07 07:49:41 +0000185 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
Chris Lattner531f9e92005-03-15 04:54:21 +0000186 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
Chris Lattner88c8a232005-01-07 07:49:41 +0000187 MVT::ValueType ObjectVT = getValueType(I->getType());
188 unsigned ArgIncrement = 4;
189 unsigned ObjSize;
190 switch (ObjectVT) {
191 default: assert(0 && "Unhandled argument type!");
192 case MVT::i1:
193 case MVT::i8: ObjSize = 1; break;
194 case MVT::i16: ObjSize = 2; break;
195 case MVT::i32: ObjSize = 4; break;
196 case MVT::i64: ObjSize = ArgIncrement = 8; break;
197 case MVT::f32: ObjSize = 4; break;
198 case MVT::f64: ObjSize = ArgIncrement = 8; break;
199 }
200 // Create the frame index object for this incoming parameter...
201 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
Misha Brukmanc88330a2005-04-21 23:38:14 +0000202
Chris Lattner88c8a232005-01-07 07:49:41 +0000203 // Create the SelectionDAG nodes corresponding to a load from this parameter
204 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
205
206 // Don't codegen dead arguments. FIXME: remove this check when we can nuke
207 // dead loads.
208 SDOperand ArgValue;
209 if (!I->use_empty())
Chris Lattnerdaa064d2005-05-09 05:40:26 +0000210 ArgValue = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
211 DAG.getSrcValue(NULL));
Chris Lattner88c8a232005-01-07 07:49:41 +0000212 else {
213 if (MVT::isInteger(ObjectVT))
214 ArgValue = DAG.getConstant(0, ObjectVT);
215 else
216 ArgValue = DAG.getConstantFP(0, ObjectVT);
217 }
218 ArgValues.push_back(ArgValue);
219
220 ArgOffset += ArgIncrement; // Move on to the next argument...
221 }
222
223 // If the function takes variable number of arguments, make a frame index for
224 // the start of the first vararg value... for expansion of llvm.va_start.
225 if (F.isVarArg())
226 VarArgsFrameIndex = MFI->CreateFixedObject(1, ArgOffset);
Chris Lattner9f59d282005-01-09 00:01:27 +0000227 ReturnAddrIndex = 0; // No return address slot generated yet.
Chris Lattnerb59006c2005-04-09 15:23:56 +0000228
229 // Finally, inform the code generator which regs we return values in.
230 switch (getValueType(F.getReturnType())) {
231 default: assert(0 && "Unknown type!");
232 case MVT::isVoid: break;
233 case MVT::i1:
234 case MVT::i8:
235 case MVT::i16:
236 case MVT::i32:
237 MF.addLiveOut(X86::EAX);
238 break;
239 case MVT::i64:
240 MF.addLiveOut(X86::EAX);
241 MF.addLiveOut(X86::EDX);
242 break;
243 case MVT::f32:
244 case MVT::f64:
245 MF.addLiveOut(X86::ST0);
246 break;
247 }
Chris Lattner88c8a232005-01-07 07:49:41 +0000248 return ArgValues;
249}
250
Chris Lattnerb52e0412005-01-08 19:28:19 +0000251std::pair<SDOperand, SDOperand>
Chris Lattner7ce7a8f2005-05-12 23:06:28 +0000252X86TargetLowering::LowerCCCCallTo(SDOperand Chain, const Type *RetTy,
253 bool isVarArg, SDOperand Callee,
254 ArgListTy &Args, SelectionDAG &DAG) {
Chris Lattner88c8a232005-01-07 07:49:41 +0000255 // Count how many bytes are to be pushed on the stack.
256 unsigned NumBytes = 0;
257
258 if (Args.empty()) {
259 // Save zero bytes.
Chris Lattner2dce7032005-05-12 23:24:06 +0000260 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
Chris Lattnerb52e0412005-01-08 19:28:19 +0000261 DAG.getConstant(0, getPointerTy()));
Chris Lattner88c8a232005-01-07 07:49:41 +0000262 } else {
263 for (unsigned i = 0, e = Args.size(); i != e; ++i)
264 switch (getValueType(Args[i].second)) {
265 default: assert(0 && "Unknown value type!");
266 case MVT::i1:
267 case MVT::i8:
268 case MVT::i16:
269 case MVT::i32:
270 case MVT::f32:
271 NumBytes += 4;
272 break;
273 case MVT::i64:
274 case MVT::f64:
275 NumBytes += 8;
276 break;
277 }
278
Chris Lattner2dce7032005-05-12 23:24:06 +0000279 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
Chris Lattnerb52e0412005-01-08 19:28:19 +0000280 DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattner88c8a232005-01-07 07:49:41 +0000281
282 // Arguments go on the stack in reverse order, as specified by the ABI.
283 unsigned ArgOffset = 0;
Chris Lattner720a62e2005-01-14 22:37:41 +0000284 SDOperand StackPtr = DAG.getCopyFromReg(X86::ESP, MVT::i32,
285 DAG.getEntryNode());
Chris Lattnerc78776d2005-01-21 19:46:38 +0000286 std::vector<SDOperand> Stores;
287
Chris Lattner88c8a232005-01-07 07:49:41 +0000288 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
Chris Lattner88c8a232005-01-07 07:49:41 +0000289 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
290 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
291
292 switch (getValueType(Args[i].second)) {
293 default: assert(0 && "Unexpected ValueType for argument!");
294 case MVT::i1:
295 case MVT::i8:
296 case MVT::i16:
297 // Promote the integer to 32 bits. If the input type is signed use a
298 // sign extend, otherwise use a zero extend.
299 if (Args[i].second->isSigned())
300 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
301 else
302 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
303
304 // FALL THROUGH
305 case MVT::i32:
306 case MVT::f32:
Chris Lattnerc78776d2005-01-21 19:46:38 +0000307 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
Chris Lattnerdaa064d2005-05-09 05:40:26 +0000308 Args[i].first, PtrOff,
309 DAG.getSrcValue(NULL)));
Chris Lattner88c8a232005-01-07 07:49:41 +0000310 ArgOffset += 4;
311 break;
312 case MVT::i64:
313 case MVT::f64:
Chris Lattnerc78776d2005-01-21 19:46:38 +0000314 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
Chris Lattnerdaa064d2005-05-09 05:40:26 +0000315 Args[i].first, PtrOff,
316 DAG.getSrcValue(NULL)));
Chris Lattner88c8a232005-01-07 07:49:41 +0000317 ArgOffset += 8;
318 break;
319 }
320 }
Chris Lattnerc78776d2005-01-21 19:46:38 +0000321 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, Stores);
Chris Lattner88c8a232005-01-07 07:49:41 +0000322 }
323
324 std::vector<MVT::ValueType> RetVals;
325 MVT::ValueType RetTyVT = getValueType(RetTy);
326 if (RetTyVT != MVT::isVoid)
327 RetVals.push_back(RetTyVT);
328 RetVals.push_back(MVT::Other);
329
Chris Lattnerb52e0412005-01-08 19:28:19 +0000330 SDOperand TheCall = SDOperand(DAG.getCall(RetVals, Chain, Callee), 0);
Chris Lattner313ddb52005-01-08 20:51:36 +0000331 Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
Chris Lattner2dce7032005-05-12 23:24:06 +0000332 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain,
Chris Lattnerb52e0412005-01-08 19:28:19 +0000333 DAG.getConstant(NumBytes, getPointerTy()));
334 return std::make_pair(TheCall, Chain);
Chris Lattner88c8a232005-01-07 07:49:41 +0000335}
336
Chris Lattner9f59d282005-01-09 00:01:27 +0000337std::pair<SDOperand, SDOperand>
338X86TargetLowering::LowerVAStart(SDOperand Chain, SelectionDAG &DAG) {
339 // vastart just returns the address of the VarArgsFrameIndex slot.
340 return std::make_pair(DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32), Chain);
341}
342
343std::pair<SDOperand,SDOperand> X86TargetLowering::
344LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
345 const Type *ArgTy, SelectionDAG &DAG) {
346 MVT::ValueType ArgVT = getValueType(ArgTy);
347 SDOperand Result;
348 if (!isVANext) {
Chris Lattnerdaa064d2005-05-09 05:40:26 +0000349 Result = DAG.getLoad(ArgVT, DAG.getEntryNode(), VAList,
350 DAG.getSrcValue(NULL));
Chris Lattner9f59d282005-01-09 00:01:27 +0000351 } else {
352 unsigned Amt;
353 if (ArgVT == MVT::i32)
354 Amt = 4;
355 else {
356 assert((ArgVT == MVT::i64 || ArgVT == MVT::f64) &&
357 "Other types should have been promoted for varargs!");
358 Amt = 8;
359 }
360 Result = DAG.getNode(ISD::ADD, VAList.getValueType(), VAList,
361 DAG.getConstant(Amt, VAList.getValueType()));
362 }
363 return std::make_pair(Result, Chain);
364}
Misha Brukmanc88330a2005-04-21 23:38:14 +0000365
Chris Lattner7ce7a8f2005-05-12 23:06:28 +0000366//===----------------------------------------------------------------------===//
367// Fast Calling Convention implementation
368//===----------------------------------------------------------------------===//
369//
370// The X86 'fast' calling convention passes up to two integer arguments in
371// registers (an appropriate portion of EAX/EDX), passes arguments in C order,
372// and requires that the callee pop its arguments off the stack (allowing proper
373// tail calls), and has the same return value conventions as C calling convs.
374//
375// Note that this can be enhanced in the future to pass fp vals in registers
376// (when we have a global fp allocator) and do other tricks.
377//
Chris Lattner0b17b452005-05-13 07:38:09 +0000378
379/// AddLiveIn - This helper function adds the specified physical register to the
380/// MachineFunction as a live in value. It also creates a corresponding virtual
381/// register for it.
382static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg,
383 TargetRegisterClass *RC) {
384 assert(RC->contains(PReg) && "Not the correct regclass!");
385 unsigned VReg = MF.getSSARegMap()->createVirtualRegister(RC);
386 MF.addLiveIn(PReg, VReg);
387 return VReg;
388}
389
390
Chris Lattner7ce7a8f2005-05-12 23:06:28 +0000391std::vector<SDOperand>
392X86TargetLowering::LowerFastCCArguments(Function &F, SelectionDAG &DAG) {
393 std::vector<SDOperand> ArgValues;
394
395 MachineFunction &MF = DAG.getMachineFunction();
396 MachineFrameInfo *MFI = MF.getFrameInfo();
397
398 // Add DAG nodes to load the arguments... On entry to a function the stack
399 // frame looks like this:
400 //
401 // [ESP] -- return address
402 // [ESP + 4] -- first nonreg argument (leftmost lexically)
403 // [ESP + 8] -- second nonreg argument, if first argument is 4 bytes in size
404 // ...
405 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
406
407 // Keep track of the number of integer regs passed so far. This can be either
408 // 0 (neither EAX or EDX used), 1 (EAX is used) or 2 (EAX and EDX are both
409 // used).
410 unsigned NumIntRegs = 0;
411
412 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
413 MVT::ValueType ObjectVT = getValueType(I->getType());
414 unsigned ArgIncrement = 4;
415 unsigned ObjSize = 0;
416 SDOperand ArgValue;
417
418 switch (ObjectVT) {
419 default: assert(0 && "Unhandled argument type!");
420 case MVT::i1:
421 case MVT::i8:
422 if (NumIntRegs < 2) {
423 if (!I->use_empty()) {
Chris Lattner0b17b452005-05-13 07:38:09 +0000424 unsigned VReg = AddLiveIn(MF, NumIntRegs ? X86::DL : X86::AL,
425 X86::R8RegisterClass);
426 ArgValue = DAG.getCopyFromReg(VReg, MVT::i8, DAG.getRoot());
Chris Lattner7ce7a8f2005-05-12 23:06:28 +0000427 DAG.setRoot(ArgValue.getValue(1));
428 }
429 ++NumIntRegs;
430 break;
431 }
432
433 ObjSize = 1;
434 break;
435 case MVT::i16:
436 if (NumIntRegs < 2) {
437 if (!I->use_empty()) {
Chris Lattner0b17b452005-05-13 07:38:09 +0000438 unsigned VReg = AddLiveIn(MF, NumIntRegs ? X86::DX : X86::AX,
439 X86::R16RegisterClass);
440 ArgValue = DAG.getCopyFromReg(VReg, MVT::i16, DAG.getRoot());
Chris Lattner7ce7a8f2005-05-12 23:06:28 +0000441 DAG.setRoot(ArgValue.getValue(1));
442 }
443 ++NumIntRegs;
444 break;
445 }
446 ObjSize = 2;
447 break;
448 case MVT::i32:
449 if (NumIntRegs < 2) {
450 if (!I->use_empty()) {
Chris Lattner0b17b452005-05-13 07:38:09 +0000451 unsigned VReg = AddLiveIn(MF,NumIntRegs ? X86::EDX : X86::EAX,
452 X86::R32RegisterClass);
453 ArgValue = DAG.getCopyFromReg(VReg, MVT::i32, DAG.getRoot());
Chris Lattner7ce7a8f2005-05-12 23:06:28 +0000454 DAG.setRoot(ArgValue.getValue(1));
455 }
456 ++NumIntRegs;
457 break;
458 }
459 ObjSize = 4;
460 break;
461 case MVT::i64:
462 if (NumIntRegs == 0) {
463 if (!I->use_empty()) {
Chris Lattner0b17b452005-05-13 07:38:09 +0000464 unsigned BotReg = AddLiveIn(MF, X86::EAX, X86::R32RegisterClass);
465 unsigned TopReg = AddLiveIn(MF, X86::EDX, X86::R32RegisterClass);
Chris Lattner7ce7a8f2005-05-12 23:06:28 +0000466
Chris Lattner0b17b452005-05-13 07:38:09 +0000467 SDOperand Low=DAG.getCopyFromReg(BotReg, MVT::i32, DAG.getRoot());
468 SDOperand Hi =DAG.getCopyFromReg(TopReg, MVT::i32, Low.getValue(1));
Chris Lattner7ce7a8f2005-05-12 23:06:28 +0000469 DAG.setRoot(Hi.getValue(1));
470
471 ArgValue = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Low, Hi);
472 }
473 NumIntRegs = 2;
474 break;
475 } else if (NumIntRegs == 1) {
476 if (!I->use_empty()) {
Chris Lattner0b17b452005-05-13 07:38:09 +0000477 unsigned BotReg = AddLiveIn(MF, X86::EDX, X86::R32RegisterClass);
478 SDOperand Low = DAG.getCopyFromReg(BotReg, MVT::i32, DAG.getRoot());
Chris Lattner7ce7a8f2005-05-12 23:06:28 +0000479 DAG.setRoot(Low.getValue(1));
480
481 // Load the high part from memory.
482 // Create the frame index object for this incoming parameter...
483 int FI = MFI->CreateFixedObject(4, ArgOffset);
484 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
485 SDOperand Hi = DAG.getLoad(MVT::i32, DAG.getEntryNode(), FIN,
486 DAG.getSrcValue(NULL));
487 ArgValue = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Low, Hi);
488 }
489 ArgOffset += 4;
490 NumIntRegs = 2;
491 break;
492 }
493 ObjSize = ArgIncrement = 8;
494 break;
495 case MVT::f32: ObjSize = 4; break;
496 case MVT::f64: ObjSize = ArgIncrement = 8; break;
497 }
498
499 // Don't codegen dead arguments. FIXME: remove this check when we can nuke
500 // dead loads.
501 if (ObjSize && !I->use_empty()) {
502 // Create the frame index object for this incoming parameter...
503 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
504
505 // Create the SelectionDAG nodes corresponding to a load from this
506 // parameter.
507 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
508
509 ArgValue = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
510 DAG.getSrcValue(NULL));
511 } else if (ArgValue.Val == 0) {
512 if (MVT::isInteger(ObjectVT))
513 ArgValue = DAG.getConstant(0, ObjectVT);
514 else
515 ArgValue = DAG.getConstantFP(0, ObjectVT);
516 }
517 ArgValues.push_back(ArgValue);
518
519 if (ObjSize)
520 ArgOffset += ArgIncrement; // Move on to the next argument.
521 }
522
523 // If the function takes variable number of arguments, make a frame index for
524 // the start of the first vararg value... for expansion of llvm.va_start.
525 if (F.isVarArg())
526 VarArgsFrameIndex = MFI->CreateFixedObject(1, ArgOffset);
527 ReturnAddrIndex = 0; // No return address slot generated yet.
528
529 // Finally, inform the code generator which regs we return values in.
530 switch (getValueType(F.getReturnType())) {
531 default: assert(0 && "Unknown type!");
532 case MVT::isVoid: break;
533 case MVT::i1:
534 case MVT::i8:
535 case MVT::i16:
536 case MVT::i32:
537 MF.addLiveOut(X86::EAX);
538 break;
539 case MVT::i64:
540 MF.addLiveOut(X86::EAX);
541 MF.addLiveOut(X86::EDX);
542 break;
543 case MVT::f32:
544 case MVT::f64:
545 MF.addLiveOut(X86::ST0);
546 break;
547 }
548 return ArgValues;
549}
550
551std::pair<SDOperand, SDOperand>
552X86TargetLowering::LowerFastCCCallTo(SDOperand Chain, const Type *RetTy,
553 SDOperand Callee,
554 ArgListTy &Args, SelectionDAG &DAG) {
555 // Count how many bytes are to be pushed on the stack.
556 unsigned NumBytes = 0;
557
558 // Keep track of the number of integer regs passed so far. This can be either
559 // 0 (neither EAX or EDX used), 1 (EAX is used) or 2 (EAX and EDX are both
560 // used).
561 unsigned NumIntRegs = 0;
562
563 for (unsigned i = 0, e = Args.size(); i != e; ++i)
564 switch (getValueType(Args[i].second)) {
565 default: assert(0 && "Unknown value type!");
566 case MVT::i1:
567 case MVT::i8:
568 case MVT::i16:
569 case MVT::i32:
570 if (NumIntRegs < 2) {
571 ++NumIntRegs;
572 break;
573 }
574 // fall through
575 case MVT::f32:
576 NumBytes += 4;
577 break;
578 case MVT::i64:
579 if (NumIntRegs == 0) {
580 NumIntRegs = 2;
581 break;
582 } else if (NumIntRegs == 1) {
583 NumIntRegs = 2;
584 NumBytes += 4;
585 break;
586 }
587
588 // fall through
589 case MVT::f64:
590 NumBytes += 8;
591 break;
592 }
593
Chris Lattner2dce7032005-05-12 23:24:06 +0000594 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
Chris Lattner7ce7a8f2005-05-12 23:06:28 +0000595 DAG.getConstant(NumBytes, getPointerTy()));
596
597 // Arguments go on the stack in reverse order, as specified by the ABI.
598 unsigned ArgOffset = 0;
599 SDOperand StackPtr = DAG.getCopyFromReg(X86::ESP, MVT::i32,
600 DAG.getEntryNode());
601 NumIntRegs = 0;
602 std::vector<SDOperand> Stores;
603 std::vector<SDOperand> RegValuesToPass;
604 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
605 switch (getValueType(Args[i].second)) {
606 default: assert(0 && "Unexpected ValueType for argument!");
607 case MVT::i1:
608 case MVT::i8:
609 case MVT::i16:
610 case MVT::i32:
611 if (NumIntRegs < 2) {
612 RegValuesToPass.push_back(Args[i].first);
613 ++NumIntRegs;
614 break;
615 }
616 // Fall through
617 case MVT::f32: {
618 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
619 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
620 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
621 Args[i].first, PtrOff,
622 DAG.getSrcValue(NULL)));
623 ArgOffset += 4;
624 break;
625 }
626 case MVT::i64:
627 if (NumIntRegs < 2) { // Can pass part of it in regs?
628 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
629 Args[i].first, DAG.getConstant(1, MVT::i32));
630 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
631 Args[i].first, DAG.getConstant(0, MVT::i32));
632 RegValuesToPass.push_back(Lo);
633 ++NumIntRegs;
634 if (NumIntRegs < 2) { // Pass both parts in regs?
635 RegValuesToPass.push_back(Hi);
636 ++NumIntRegs;
637 } else {
638 // Pass the high part in memory.
639 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
640 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
641 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
642 Args[i].first, PtrOff,
643 DAG.getSrcValue(NULL)));
644 ArgOffset += 4;
645 }
646 break;
647 }
648 // Fall through
649 case MVT::f64:
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 += 8;
656 break;
657 }
658 }
659 if (!Stores.empty())
660 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, Stores);
661
662 std::vector<MVT::ValueType> RetVals;
663 MVT::ValueType RetTyVT = getValueType(RetTy);
664 if (RetTyVT != MVT::isVoid)
665 RetVals.push_back(RetTyVT);
666 RetVals.push_back(MVT::Other);
667
668 SDOperand TheCall = SDOperand(DAG.getCall(RetVals, Chain, Callee,
669 RegValuesToPass), 0);
670 Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
Chris Lattner2dce7032005-05-12 23:24:06 +0000671 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain,
Chris Lattner7ce7a8f2005-05-12 23:06:28 +0000672 DAG.getConstant(NumBytes, getPointerTy()));
673 return std::make_pair(TheCall, Chain);
674}
675
676
677
Chris Lattner9f59d282005-01-09 00:01:27 +0000678
679std::pair<SDOperand, SDOperand> X86TargetLowering::
680LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
681 SelectionDAG &DAG) {
682 SDOperand Result;
683 if (Depth) // Depths > 0 not supported yet!
684 Result = DAG.getConstant(0, getPointerTy());
685 else {
686 if (ReturnAddrIndex == 0) {
687 // Set up a frame object for the return address.
688 MachineFunction &MF = DAG.getMachineFunction();
689 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(4, -4);
690 }
Misha Brukmanc88330a2005-04-21 23:38:14 +0000691
Chris Lattner9f59d282005-01-09 00:01:27 +0000692 SDOperand RetAddrFI = DAG.getFrameIndex(ReturnAddrIndex, MVT::i32);
693
694 if (!isFrameAddress)
695 // Just load the return address
Chris Lattner7ce7a8f2005-05-12 23:06:28 +0000696 Result = DAG.getLoad(MVT::i32, DAG.getEntryNode(), RetAddrFI,
697 DAG.getSrcValue(NULL));
Chris Lattner9f59d282005-01-09 00:01:27 +0000698 else
699 Result = DAG.getNode(ISD::SUB, MVT::i32, RetAddrFI,
700 DAG.getConstant(4, MVT::i32));
701 }
702 return std::make_pair(Result, Chain);
703}
Chris Lattner88c8a232005-01-07 07:49:41 +0000704
705
Chris Lattnera7acdda2005-01-18 01:06:26 +0000706namespace {
707 /// X86ISelAddressMode - This corresponds to X86AddressMode, but uses
708 /// SDOperand's instead of register numbers for the leaves of the matched
709 /// tree.
710 struct X86ISelAddressMode {
711 enum {
712 RegBase,
713 FrameIndexBase,
714 } BaseType;
Misha Brukmanc88330a2005-04-21 23:38:14 +0000715
Chris Lattnera7acdda2005-01-18 01:06:26 +0000716 struct { // This is really a union, discriminated by BaseType!
717 SDOperand Reg;
718 int FrameIndex;
719 } Base;
Misha Brukmanc88330a2005-04-21 23:38:14 +0000720
Chris Lattnera7acdda2005-01-18 01:06:26 +0000721 unsigned Scale;
722 SDOperand IndexReg;
723 unsigned Disp;
724 GlobalValue *GV;
Misha Brukmanc88330a2005-04-21 23:38:14 +0000725
Chris Lattnera7acdda2005-01-18 01:06:26 +0000726 X86ISelAddressMode()
727 : BaseType(RegBase), Scale(1), IndexReg(), Disp(), GV(0) {
728 }
729 };
730}
Chris Lattner88c8a232005-01-07 07:49:41 +0000731
732
733namespace {
734 Statistic<>
735 NumFPKill("x86-codegen", "Number of FP_REG_KILL instructions added");
736
737 //===--------------------------------------------------------------------===//
738 /// ISel - X86 specific code to select X86 machine instructions for
739 /// SelectionDAG operations.
740 ///
741 class ISel : public SelectionDAGISel {
742 /// ContainsFPCode - Every instruction we select that uses or defines a FP
743 /// register should set this to true.
744 bool ContainsFPCode;
745
746 /// X86Lowering - This object fully describes how to lower LLVM code to an
747 /// X86-specific SelectionDAG.
748 X86TargetLowering X86Lowering;
749
Chris Lattner0d1f82a2005-01-11 03:11:44 +0000750 /// RegPressureMap - This keeps an approximate count of the number of
751 /// registers required to evaluate each node in the graph.
752 std::map<SDNode*, unsigned> RegPressureMap;
Chris Lattner88c8a232005-01-07 07:49:41 +0000753
754 /// ExprMap - As shared expressions are codegen'd, we keep track of which
755 /// vreg the value is produced in, so we only emit one copy of each compiled
756 /// tree.
757 std::map<SDOperand, unsigned> ExprMap;
Chris Lattner88c8a232005-01-07 07:49:41 +0000758
759 public:
760 ISel(TargetMachine &TM) : SelectionDAGISel(X86Lowering), X86Lowering(TM) {
761 }
762
Chris Lattnere1e844c2005-01-21 21:35:14 +0000763 virtual const char *getPassName() const {
764 return "X86 Pattern Instruction Selection";
765 }
766
Chris Lattner0d1f82a2005-01-11 03:11:44 +0000767 unsigned getRegPressure(SDOperand O) {
768 return RegPressureMap[O.Val];
769 }
770 unsigned ComputeRegPressure(SDOperand O);
771
Chris Lattner88c8a232005-01-07 07:49:41 +0000772 /// InstructionSelectBasicBlock - This callback is invoked by
773 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
Chris Lattner6fba62d62005-01-12 04:21:28 +0000774 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG);
Chris Lattner88c8a232005-01-07 07:49:41 +0000775
Chris Lattner0b17b452005-05-13 07:38:09 +0000776 virtual void EmitFunctionEntryCode(Function &Fn, MachineFunction &MF);
777
Chris Lattner30607ec2005-01-25 20:03:11 +0000778 bool isFoldableLoad(SDOperand Op, SDOperand OtherOp,
779 bool FloatPromoteOk = false);
Chris Lattner62b22422005-01-11 21:19:59 +0000780 void EmitFoldedLoad(SDOperand Op, X86AddressMode &AM);
Chris Lattner96113fd2005-01-17 19:25:26 +0000781 bool TryToFoldLoadOpStore(SDNode *Node);
Chris Lattner62b22422005-01-11 21:19:59 +0000782
Chris Lattner29f58192005-01-19 07:37:26 +0000783 bool EmitOrOpOp(SDOperand Op1, SDOperand Op2, unsigned DestReg);
Chris Lattner3be6cd52005-01-17 01:34:14 +0000784 void EmitCMP(SDOperand LHS, SDOperand RHS, bool isOnlyUse);
Chris Lattner37ed2852005-01-11 04:06:27 +0000785 bool EmitBranchCC(MachineBasicBlock *Dest, SDOperand Chain, SDOperand Cond);
Chris Lattner1d13a922005-01-10 22:10:13 +0000786 void EmitSelectCC(SDOperand Cond, MVT::ValueType SVT,
787 unsigned RTrue, unsigned RFalse, unsigned RDest);
Chris Lattner88c8a232005-01-07 07:49:41 +0000788 unsigned SelectExpr(SDOperand N);
Chris Lattnera7acdda2005-01-18 01:06:26 +0000789
790 X86AddressMode SelectAddrExprs(const X86ISelAddressMode &IAM);
791 bool MatchAddress(SDOperand N, X86ISelAddressMode &AM);
792 void SelectAddress(SDOperand N, X86AddressMode &AM);
Chris Lattner88c8a232005-01-07 07:49:41 +0000793 void Select(SDOperand N);
794 };
795}
796
Chris Lattnerd8145bc2005-05-10 03:53:18 +0000797/// EmitSpecialCodeForMain - Emit any code that needs to be executed only in
798/// the main function.
799static void EmitSpecialCodeForMain(MachineBasicBlock *BB,
800 MachineFrameInfo *MFI) {
801 // Switch the FPU to 64-bit precision mode for better compatibility and speed.
802 int CWFrameIdx = MFI->CreateStackObject(2, 2);
803 addFrameReference(BuildMI(BB, X86::FNSTCW16m, 4), CWFrameIdx);
804
805 // Set the high part to be 64-bit precision.
806 addFrameReference(BuildMI(BB, X86::MOV8mi, 5),
807 CWFrameIdx, 1).addImm(2);
808
809 // Reload the modified control word now.
810 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
811}
812
Chris Lattner0b17b452005-05-13 07:38:09 +0000813void ISel::EmitFunctionEntryCode(Function &Fn, MachineFunction &MF) {
814 // If this function has live-in values, emit the copies from pregs to vregs at
815 // the top of the function, before anything else.
816 MachineBasicBlock *BB = MF.begin();
817 if (MF.livein_begin() != MF.livein_end()) {
818 SSARegMap *RegMap = MF.getSSARegMap();
819 for (MachineFunction::livein_iterator LI = MF.livein_begin(),
820 E = MF.livein_end(); LI != E; ++LI) {
821 const TargetRegisterClass *RC = RegMap->getRegClass(LI->second);
822 if (RC == X86::R8RegisterClass) {
823 BuildMI(BB, X86::MOV8rr, 1, LI->second).addReg(LI->first);
824 } else if (RC == X86::R16RegisterClass) {
825 BuildMI(BB, X86::MOV16rr, 1, LI->second).addReg(LI->first);
826 } else if (RC == X86::R32RegisterClass) {
827 BuildMI(BB, X86::MOV32rr, 1, LI->second).addReg(LI->first);
828 } else if (RC == X86::RFPRegisterClass) {
829 BuildMI(BB, X86::FpMOV, 1, LI->second).addReg(LI->first);
830 } else {
831 assert(0 && "Unknown regclass!");
832 }
833 }
834 }
835
836
837 // If this is main, emit special code for main.
838 if (Fn.hasExternalLinkage() && Fn.getName() == "main")
839 EmitSpecialCodeForMain(BB, MF.getFrameInfo());
840}
841
842
Chris Lattner6fba62d62005-01-12 04:21:28 +0000843/// InstructionSelectBasicBlock - This callback is invoked by SelectionDAGISel
844/// when it has created a SelectionDAG for us to codegen.
845void ISel::InstructionSelectBasicBlock(SelectionDAG &DAG) {
846 // While we're doing this, keep track of whether we see any FP code for
847 // FP_REG_KILL insertion.
848 ContainsFPCode = false;
Chris Lattnerd8145bc2005-05-10 03:53:18 +0000849 MachineFunction *MF = BB->getParent();
Chris Lattner6fba62d62005-01-12 04:21:28 +0000850
851 // Scan the PHI nodes that already are inserted into this basic block. If any
852 // of them is a PHI of a floating point value, we need to insert an
853 // FP_REG_KILL.
Chris Lattnerd8145bc2005-05-10 03:53:18 +0000854 SSARegMap *RegMap = MF->getSSARegMap();
Chris Lattner0b17b452005-05-13 07:38:09 +0000855 if (BB != MF->begin())
856 for (MachineBasicBlock::iterator I = BB->begin(), E = BB->end();
857 I != E; ++I) {
858 assert(I->getOpcode() == X86::PHI &&
859 "Isn't just PHI nodes?");
860 if (RegMap->getRegClass(I->getOperand(0).getReg()) ==
861 X86::RFPRegisterClass) {
862 ContainsFPCode = true;
863 break;
864 }
Chris Lattner6fba62d62005-01-12 04:21:28 +0000865 }
Chris Lattnerd8145bc2005-05-10 03:53:18 +0000866
Chris Lattner6fba62d62005-01-12 04:21:28 +0000867 // Compute the RegPressureMap, which is an approximation for the number of
868 // registers required to compute each node.
869 ComputeRegPressure(DAG.getRoot());
870
871 // Codegen the basic block.
872 Select(DAG.getRoot());
873
874 // Finally, look at all of the successors of this block. If any contain a PHI
875 // node of FP type, we need to insert an FP_REG_KILL in this block.
876 for (MachineBasicBlock::succ_iterator SI = BB->succ_begin(),
877 E = BB->succ_end(); SI != E && !ContainsFPCode; ++SI)
878 for (MachineBasicBlock::iterator I = (*SI)->begin(), E = (*SI)->end();
879 I != E && I->getOpcode() == X86::PHI; ++I) {
880 if (RegMap->getRegClass(I->getOperand(0).getReg()) ==
881 X86::RFPRegisterClass) {
882 ContainsFPCode = true;
883 break;
884 }
885 }
Misha Brukmanc88330a2005-04-21 23:38:14 +0000886
Chris Lattner6972c312005-05-09 03:36:39 +0000887 // Final check, check LLVM BB's that are successors to the LLVM BB
888 // corresponding to BB for FP PHI nodes.
889 const BasicBlock *LLVMBB = BB->getBasicBlock();
890 const PHINode *PN;
891 if (!ContainsFPCode)
892 for (succ_const_iterator SI = succ_begin(LLVMBB), E = succ_end(LLVMBB);
893 SI != E && !ContainsFPCode; ++SI)
894 for (BasicBlock::const_iterator II = SI->begin();
895 (PN = dyn_cast<PHINode>(II)); ++II)
896 if (PN->getType()->isFloatingPoint()) {
897 ContainsFPCode = true;
898 break;
899 }
900
901
Chris Lattner6fba62d62005-01-12 04:21:28 +0000902 // Insert FP_REG_KILL instructions into basic blocks that need them. This
903 // only occurs due to the floating point stackifier not being aggressive
904 // enough to handle arbitrary global stackification.
905 //
906 // Currently we insert an FP_REG_KILL instruction into each block that uses or
907 // defines a floating point virtual register.
908 //
909 // When the global register allocators (like linear scan) finally update live
910 // variable analysis, we can keep floating point values in registers across
911 // basic blocks. This will be a huge win, but we are waiting on the global
912 // allocators before we can do this.
913 //
Chris Lattner472a2652005-03-30 01:10:00 +0000914 if (ContainsFPCode) {
Chris Lattner6fba62d62005-01-12 04:21:28 +0000915 BuildMI(*BB, BB->getFirstTerminator(), X86::FP_REG_KILL, 0);
916 ++NumFPKill;
917 }
Misha Brukmanc88330a2005-04-21 23:38:14 +0000918
Chris Lattner6fba62d62005-01-12 04:21:28 +0000919 // Clear state used for selection.
920 ExprMap.clear();
Chris Lattner6fba62d62005-01-12 04:21:28 +0000921 RegPressureMap.clear();
922}
923
924
Chris Lattner0d1f82a2005-01-11 03:11:44 +0000925// ComputeRegPressure - Compute the RegPressureMap, which is an approximation
926// for the number of registers required to compute each node. This is basically
927// computing a generalized form of the Sethi-Ullman number for each node.
928unsigned ISel::ComputeRegPressure(SDOperand O) {
929 SDNode *N = O.Val;
930 unsigned &Result = RegPressureMap[N];
931 if (Result) return Result;
932
Chris Lattner8fea42b2005-01-11 03:37:59 +0000933 // FIXME: Should operations like CALL (which clobber lots o regs) have a
934 // higher fixed cost??
935
Chris Lattner8aa10fc2005-01-11 22:29:12 +0000936 if (N->getNumOperands() == 0) {
937 Result = 1;
938 } else {
939 unsigned MaxRegUse = 0;
940 unsigned NumExtraMaxRegUsers = 0;
941 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
942 unsigned Regs;
943 if (N->getOperand(i).getOpcode() == ISD::Constant)
944 Regs = 0;
945 else
946 Regs = ComputeRegPressure(N->getOperand(i));
947 if (Regs > MaxRegUse) {
948 MaxRegUse = Regs;
949 NumExtraMaxRegUsers = 0;
950 } else if (Regs == MaxRegUse &&
951 N->getOperand(i).getValueType() != MVT::Other) {
952 ++NumExtraMaxRegUsers;
953 }
Chris Lattner0d1f82a2005-01-11 03:11:44 +0000954 }
Chris Lattnerca318ed2005-01-17 22:56:09 +0000955
956 if (O.getOpcode() != ISD::TokenFactor)
957 Result = MaxRegUse+NumExtraMaxRegUsers;
958 else
Chris Lattnera5d137f2005-01-17 23:02:13 +0000959 Result = MaxRegUse == 1 ? 0 : MaxRegUse-1;
Chris Lattner8aa10fc2005-01-11 22:29:12 +0000960 }
Chris Lattnerb7fe57a2005-01-12 02:19:06 +0000961
Chris Lattner75bac9f2005-01-11 23:21:30 +0000962 //std::cerr << " WEIGHT: " << Result << " "; N->dump(); std::cerr << "\n";
Chris Lattner8aa10fc2005-01-11 22:29:12 +0000963 return Result;
Chris Lattner0d1f82a2005-01-11 03:11:44 +0000964}
965
Chris Lattner5b04f332005-01-20 16:50:16 +0000966/// NodeTransitivelyUsesValue - Return true if N or any of its uses uses Op.
967/// The DAG cannot have cycles in it, by definition, so the visited set is not
968/// needed to prevent infinite loops. The DAG CAN, however, have unbounded
969/// reuse, so it prevents exponential cases.
970///
971static bool NodeTransitivelyUsesValue(SDOperand N, SDOperand Op,
972 std::set<SDNode*> &Visited) {
973 if (N == Op) return true; // Found it.
974 SDNode *Node = N.Val;
Chris Lattnere70eb9da2005-01-21 21:43:02 +0000975 if (Node->getNumOperands() == 0 || // Leaf?
976 Node->getNodeDepth() <= Op.getNodeDepth()) return false; // Can't find it?
Chris Lattner5b04f332005-01-20 16:50:16 +0000977 if (!Visited.insert(Node).second) return false; // Already visited?
978
979 // Recurse for the first N-1 operands.
980 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i)
981 if (NodeTransitivelyUsesValue(Node->getOperand(i), Op, Visited))
982 return true;
983
984 // Tail recurse for the last operand.
985 return NodeTransitivelyUsesValue(Node->getOperand(0), Op, Visited);
986}
987
Chris Lattnera7acdda2005-01-18 01:06:26 +0000988X86AddressMode ISel::SelectAddrExprs(const X86ISelAddressMode &IAM) {
989 X86AddressMode Result;
990
991 // If we need to emit two register operands, emit the one with the highest
992 // register pressure first.
993 if (IAM.BaseType == X86ISelAddressMode::RegBase &&
994 IAM.Base.Reg.Val && IAM.IndexReg.Val) {
Chris Lattner5b04f332005-01-20 16:50:16 +0000995 bool EmitBaseThenIndex;
Chris Lattnera7acdda2005-01-18 01:06:26 +0000996 if (getRegPressure(IAM.Base.Reg) > getRegPressure(IAM.IndexReg)) {
Chris Lattner5b04f332005-01-20 16:50:16 +0000997 std::set<SDNode*> Visited;
998 EmitBaseThenIndex = true;
999 // If Base ends up pointing to Index, we must emit index first. This is
1000 // because of the way we fold loads, we may end up doing bad things with
1001 // the folded add.
1002 if (NodeTransitivelyUsesValue(IAM.Base.Reg, IAM.IndexReg, Visited))
1003 EmitBaseThenIndex = false;
1004 } else {
1005 std::set<SDNode*> Visited;
1006 EmitBaseThenIndex = false;
1007 // If Base ends up pointing to Index, we must emit index first. This is
1008 // because of the way we fold loads, we may end up doing bad things with
1009 // the folded add.
1010 if (NodeTransitivelyUsesValue(IAM.IndexReg, IAM.Base.Reg, Visited))
1011 EmitBaseThenIndex = true;
1012 }
1013
1014 if (EmitBaseThenIndex) {
Chris Lattnera7acdda2005-01-18 01:06:26 +00001015 Result.Base.Reg = SelectExpr(IAM.Base.Reg);
1016 Result.IndexReg = SelectExpr(IAM.IndexReg);
1017 } else {
1018 Result.IndexReg = SelectExpr(IAM.IndexReg);
1019 Result.Base.Reg = SelectExpr(IAM.Base.Reg);
1020 }
Chris Lattner5b04f332005-01-20 16:50:16 +00001021
Chris Lattnera7acdda2005-01-18 01:06:26 +00001022 } else if (IAM.BaseType == X86ISelAddressMode::RegBase && IAM.Base.Reg.Val) {
1023 Result.Base.Reg = SelectExpr(IAM.Base.Reg);
1024 } else if (IAM.IndexReg.Val) {
1025 Result.IndexReg = SelectExpr(IAM.IndexReg);
1026 }
Misha Brukmanc88330a2005-04-21 23:38:14 +00001027
Chris Lattnera7acdda2005-01-18 01:06:26 +00001028 switch (IAM.BaseType) {
1029 case X86ISelAddressMode::RegBase:
1030 Result.BaseType = X86AddressMode::RegBase;
1031 break;
1032 case X86ISelAddressMode::FrameIndexBase:
1033 Result.BaseType = X86AddressMode::FrameIndexBase;
1034 Result.Base.FrameIndex = IAM.Base.FrameIndex;
1035 break;
1036 default:
1037 assert(0 && "Unknown base type!");
1038 break;
1039 }
1040 Result.Scale = IAM.Scale;
1041 Result.Disp = IAM.Disp;
1042 Result.GV = IAM.GV;
1043 return Result;
1044}
1045
1046/// SelectAddress - Pattern match the maximal addressing mode for this node and
1047/// emit all of the leaf registers.
1048void ISel::SelectAddress(SDOperand N, X86AddressMode &AM) {
1049 X86ISelAddressMode IAM;
1050 MatchAddress(N, IAM);
1051 AM = SelectAddrExprs(IAM);
1052}
1053
1054/// MatchAddress - Add the specified node to the specified addressing mode,
1055/// returning true if it cannot be done. This just pattern matches for the
1056/// addressing mode, it does not cause any code to be emitted. For that, use
1057/// SelectAddress.
1058bool ISel::MatchAddress(SDOperand N, X86ISelAddressMode &AM) {
Chris Lattner88c8a232005-01-07 07:49:41 +00001059 switch (N.getOpcode()) {
1060 default: break;
1061 case ISD::FrameIndex:
Chris Lattnera7acdda2005-01-18 01:06:26 +00001062 if (AM.BaseType == X86ISelAddressMode::RegBase && AM.Base.Reg.Val == 0) {
1063 AM.BaseType = X86ISelAddressMode::FrameIndexBase;
Chris Lattner88c8a232005-01-07 07:49:41 +00001064 AM.Base.FrameIndex = cast<FrameIndexSDNode>(N)->getIndex();
1065 return false;
1066 }
1067 break;
1068 case ISD::GlobalAddress:
1069 if (AM.GV == 0) {
1070 AM.GV = cast<GlobalAddressSDNode>(N)->getGlobal();
1071 return false;
1072 }
1073 break;
1074 case ISD::Constant:
1075 AM.Disp += cast<ConstantSDNode>(N)->getValue();
1076 return false;
1077 case ISD::SHL:
Chris Lattner3676cd62005-01-13 05:53:16 +00001078 // We might have folded the load into this shift, so don't regen the value
1079 // if so.
1080 if (ExprMap.count(N)) break;
1081
Chris Lattnera7acdda2005-01-18 01:06:26 +00001082 if (AM.IndexReg.Val == 0 && AM.Scale == 1)
Chris Lattner88c8a232005-01-07 07:49:41 +00001083 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1))) {
1084 unsigned Val = CN->getValue();
1085 if (Val == 1 || Val == 2 || Val == 3) {
1086 AM.Scale = 1 << Val;
Chris Lattnerb74ec4c2005-01-11 06:36:20 +00001087 SDOperand ShVal = N.Val->getOperand(0);
1088
1089 // Okay, we know that we have a scale by now. However, if the scaled
1090 // value is an add of something and a constant, we can fold the
1091 // constant into the disp field here.
Chris Lattnered246ec2005-01-18 04:18:32 +00001092 if (ShVal.Val->getOpcode() == ISD::ADD && ShVal.hasOneUse() &&
Chris Lattnerb74ec4c2005-01-11 06:36:20 +00001093 isa<ConstantSDNode>(ShVal.Val->getOperand(1))) {
Chris Lattnera7acdda2005-01-18 01:06:26 +00001094 AM.IndexReg = ShVal.Val->getOperand(0);
Chris Lattnerb74ec4c2005-01-11 06:36:20 +00001095 ConstantSDNode *AddVal =
1096 cast<ConstantSDNode>(ShVal.Val->getOperand(1));
1097 AM.Disp += AddVal->getValue() << Val;
Chris Lattner3676cd62005-01-13 05:53:16 +00001098 } else {
Chris Lattnera7acdda2005-01-18 01:06:26 +00001099 AM.IndexReg = ShVal;
Chris Lattnerb74ec4c2005-01-11 06:36:20 +00001100 }
Chris Lattner88c8a232005-01-07 07:49:41 +00001101 return false;
1102 }
1103 }
1104 break;
Chris Lattner8cf9cda2005-01-11 19:37:02 +00001105 case ISD::MUL:
Chris Lattner3676cd62005-01-13 05:53:16 +00001106 // We might have folded the load into this mul, so don't regen the value if
1107 // so.
1108 if (ExprMap.count(N)) break;
1109
Chris Lattner8cf9cda2005-01-11 19:37:02 +00001110 // X*[3,5,9] -> X+X*[2,4,8]
Chris Lattnera7acdda2005-01-18 01:06:26 +00001111 if (AM.IndexReg.Val == 0 && AM.BaseType == X86ISelAddressMode::RegBase &&
1112 AM.Base.Reg.Val == 0)
Chris Lattner8cf9cda2005-01-11 19:37:02 +00001113 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1)))
1114 if (CN->getValue() == 3 || CN->getValue() == 5 || CN->getValue() == 9) {
1115 AM.Scale = unsigned(CN->getValue())-1;
1116
1117 SDOperand MulVal = N.Val->getOperand(0);
Chris Lattnera7acdda2005-01-18 01:06:26 +00001118 SDOperand Reg;
Chris Lattner8cf9cda2005-01-11 19:37:02 +00001119
1120 // Okay, we know that we have a scale by now. However, if the scaled
1121 // value is an add of something and a constant, we can fold the
1122 // constant into the disp field here.
Chris Lattnered246ec2005-01-18 04:18:32 +00001123 if (MulVal.Val->getOpcode() == ISD::ADD && MulVal.hasOneUse() &&
Chris Lattner8cf9cda2005-01-11 19:37:02 +00001124 isa<ConstantSDNode>(MulVal.Val->getOperand(1))) {
Chris Lattnera7acdda2005-01-18 01:06:26 +00001125 Reg = MulVal.Val->getOperand(0);
Chris Lattner8cf9cda2005-01-11 19:37:02 +00001126 ConstantSDNode *AddVal =
1127 cast<ConstantSDNode>(MulVal.Val->getOperand(1));
1128 AM.Disp += AddVal->getValue() * CN->getValue();
Misha Brukmanc88330a2005-04-21 23:38:14 +00001129 } else {
Chris Lattnera7acdda2005-01-18 01:06:26 +00001130 Reg = N.Val->getOperand(0);
Chris Lattner8cf9cda2005-01-11 19:37:02 +00001131 }
1132
1133 AM.IndexReg = AM.Base.Reg = Reg;
1134 return false;
1135 }
1136 break;
Chris Lattner88c8a232005-01-07 07:49:41 +00001137
1138 case ISD::ADD: {
Chris Lattner3676cd62005-01-13 05:53:16 +00001139 // We might have folded the load into this mul, so don't regen the value if
1140 // so.
1141 if (ExprMap.count(N)) break;
1142
Chris Lattnera7acdda2005-01-18 01:06:26 +00001143 X86ISelAddressMode Backup = AM;
1144 if (!MatchAddress(N.Val->getOperand(0), AM) &&
1145 !MatchAddress(N.Val->getOperand(1), AM))
Chris Lattner88c8a232005-01-07 07:49:41 +00001146 return false;
1147 AM = Backup;
Chris Lattnera7acdda2005-01-18 01:06:26 +00001148 if (!MatchAddress(N.Val->getOperand(1), AM) &&
1149 !MatchAddress(N.Val->getOperand(0), AM))
Chris Lattner17553602005-01-12 18:08:53 +00001150 return false;
1151 AM = Backup;
Chris Lattner88c8a232005-01-07 07:49:41 +00001152 break;
1153 }
1154 }
1155
Chris Lattner378262d2005-01-11 04:40:19 +00001156 // Is the base register already occupied?
Chris Lattnera7acdda2005-01-18 01:06:26 +00001157 if (AM.BaseType != X86ISelAddressMode::RegBase || AM.Base.Reg.Val) {
Chris Lattner378262d2005-01-11 04:40:19 +00001158 // If so, check to see if the scale index register is set.
Chris Lattnera7acdda2005-01-18 01:06:26 +00001159 if (AM.IndexReg.Val == 0) {
1160 AM.IndexReg = N;
Chris Lattner378262d2005-01-11 04:40:19 +00001161 AM.Scale = 1;
1162 return false;
1163 }
1164
1165 // Otherwise, we cannot select it.
Chris Lattner88c8a232005-01-07 07:49:41 +00001166 return true;
Chris Lattner378262d2005-01-11 04:40:19 +00001167 }
Chris Lattner88c8a232005-01-07 07:49:41 +00001168
1169 // Default, generate it as a register.
Chris Lattnera7acdda2005-01-18 01:06:26 +00001170 AM.BaseType = X86ISelAddressMode::RegBase;
1171 AM.Base.Reg = N;
Chris Lattner88c8a232005-01-07 07:49:41 +00001172 return false;
1173}
1174
1175/// Emit2SetCCsAndLogical - Emit the following sequence of instructions,
1176/// assuming that the temporary registers are in the 8-bit register class.
1177///
1178/// Tmp1 = setcc1
1179/// Tmp2 = setcc2
1180/// DestReg = logicalop Tmp1, Tmp2
1181///
1182static void Emit2SetCCsAndLogical(MachineBasicBlock *BB, unsigned SetCC1,
1183 unsigned SetCC2, unsigned LogicalOp,
1184 unsigned DestReg) {
1185 SSARegMap *RegMap = BB->getParent()->getSSARegMap();
1186 unsigned Tmp1 = RegMap->createVirtualRegister(X86::R8RegisterClass);
1187 unsigned Tmp2 = RegMap->createVirtualRegister(X86::R8RegisterClass);
1188 BuildMI(BB, SetCC1, 0, Tmp1);
1189 BuildMI(BB, SetCC2, 0, Tmp2);
1190 BuildMI(BB, LogicalOp, 2, DestReg).addReg(Tmp1).addReg(Tmp2);
1191}
1192
1193/// EmitSetCC - Emit the code to set the specified 8-bit register to 1 if the
1194/// condition codes match the specified SetCCOpcode. Note that some conditions
1195/// require multiple instructions to generate the correct value.
1196static void EmitSetCC(MachineBasicBlock *BB, unsigned DestReg,
1197 ISD::CondCode SetCCOpcode, bool isFP) {
1198 unsigned Opc;
1199 if (!isFP) {
1200 switch (SetCCOpcode) {
1201 default: assert(0 && "Illegal integer SetCC!");
1202 case ISD::SETEQ: Opc = X86::SETEr; break;
1203 case ISD::SETGT: Opc = X86::SETGr; break;
1204 case ISD::SETGE: Opc = X86::SETGEr; break;
1205 case ISD::SETLT: Opc = X86::SETLr; break;
1206 case ISD::SETLE: Opc = X86::SETLEr; break;
1207 case ISD::SETNE: Opc = X86::SETNEr; break;
1208 case ISD::SETULT: Opc = X86::SETBr; break;
1209 case ISD::SETUGT: Opc = X86::SETAr; break;
1210 case ISD::SETULE: Opc = X86::SETBEr; break;
1211 case ISD::SETUGE: Opc = X86::SETAEr; break;
1212 }
1213 } else {
1214 // On a floating point condition, the flags are set as follows:
1215 // ZF PF CF op
1216 // 0 | 0 | 0 | X > Y
1217 // 0 | 0 | 1 | X < Y
1218 // 1 | 0 | 0 | X == Y
1219 // 1 | 1 | 1 | unordered
1220 //
1221 switch (SetCCOpcode) {
1222 default: assert(0 && "Invalid FP setcc!");
1223 case ISD::SETUEQ:
1224 case ISD::SETEQ:
1225 Opc = X86::SETEr; // True if ZF = 1
1226 break;
1227 case ISD::SETOGT:
1228 case ISD::SETGT:
1229 Opc = X86::SETAr; // True if CF = 0 and ZF = 0
1230 break;
1231 case ISD::SETOGE:
1232 case ISD::SETGE:
1233 Opc = X86::SETAEr; // True if CF = 0
1234 break;
1235 case ISD::SETULT:
1236 case ISD::SETLT:
1237 Opc = X86::SETBr; // True if CF = 1
1238 break;
1239 case ISD::SETULE:
1240 case ISD::SETLE:
1241 Opc = X86::SETBEr; // True if CF = 1 or ZF = 1
1242 break;
1243 case ISD::SETONE:
1244 case ISD::SETNE:
1245 Opc = X86::SETNEr; // True if ZF = 0
1246 break;
1247 case ISD::SETUO:
1248 Opc = X86::SETPr; // True if PF = 1
1249 break;
1250 case ISD::SETO:
1251 Opc = X86::SETNPr; // True if PF = 0
1252 break;
1253 case ISD::SETOEQ: // !PF & ZF
1254 Emit2SetCCsAndLogical(BB, X86::SETNPr, X86::SETEr, X86::AND8rr, DestReg);
1255 return;
1256 case ISD::SETOLT: // !PF & CF
1257 Emit2SetCCsAndLogical(BB, X86::SETNPr, X86::SETBr, X86::AND8rr, DestReg);
1258 return;
1259 case ISD::SETOLE: // !PF & (CF || ZF)
1260 Emit2SetCCsAndLogical(BB, X86::SETNPr, X86::SETBEr, X86::AND8rr, DestReg);
1261 return;
1262 case ISD::SETUGT: // PF | (!ZF & !CF)
1263 Emit2SetCCsAndLogical(BB, X86::SETPr, X86::SETAr, X86::OR8rr, DestReg);
1264 return;
1265 case ISD::SETUGE: // PF | !CF
1266 Emit2SetCCsAndLogical(BB, X86::SETPr, X86::SETAEr, X86::OR8rr, DestReg);
1267 return;
1268 case ISD::SETUNE: // PF | !ZF
1269 Emit2SetCCsAndLogical(BB, X86::SETPr, X86::SETNEr, X86::OR8rr, DestReg);
1270 return;
1271 }
1272 }
1273 BuildMI(BB, Opc, 0, DestReg);
1274}
1275
1276
1277/// EmitBranchCC - Emit code into BB that arranges for control to transfer to
1278/// the Dest block if the Cond condition is true. If we cannot fold this
1279/// condition into the branch, return true.
1280///
Chris Lattner37ed2852005-01-11 04:06:27 +00001281bool ISel::EmitBranchCC(MachineBasicBlock *Dest, SDOperand Chain,
1282 SDOperand Cond) {
Chris Lattner88c8a232005-01-07 07:49:41 +00001283 // FIXME: Evaluate whether it would be good to emit code like (X < Y) | (A >
1284 // B) using two conditional branches instead of one condbr, two setcc's, and
1285 // an or.
1286 if ((Cond.getOpcode() == ISD::OR ||
1287 Cond.getOpcode() == ISD::AND) && Cond.Val->hasOneUse()) {
1288 // And and or set the flags for us, so there is no need to emit a TST of the
1289 // result. It is only safe to do this if there is only a single use of the
1290 // AND/OR though, otherwise we don't know it will be emitted here.
Chris Lattner37ed2852005-01-11 04:06:27 +00001291 Select(Chain);
Chris Lattner88c8a232005-01-07 07:49:41 +00001292 SelectExpr(Cond);
1293 BuildMI(BB, X86::JNE, 1).addMBB(Dest);
1294 return false;
1295 }
1296
1297 // Codegen br not C -> JE.
1298 if (Cond.getOpcode() == ISD::XOR)
1299 if (ConstantSDNode *NC = dyn_cast<ConstantSDNode>(Cond.Val->getOperand(1)))
1300 if (NC->isAllOnesValue()) {
Chris Lattner37ed2852005-01-11 04:06:27 +00001301 unsigned CondR;
1302 if (getRegPressure(Chain) > getRegPressure(Cond)) {
1303 Select(Chain);
1304 CondR = SelectExpr(Cond.Val->getOperand(0));
1305 } else {
1306 CondR = SelectExpr(Cond.Val->getOperand(0));
1307 Select(Chain);
1308 }
Chris Lattner88c8a232005-01-07 07:49:41 +00001309 BuildMI(BB, X86::TEST8rr, 2).addReg(CondR).addReg(CondR);
1310 BuildMI(BB, X86::JE, 1).addMBB(Dest);
1311 return false;
1312 }
1313
1314 SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Cond);
1315 if (SetCC == 0)
1316 return true; // Can only handle simple setcc's so far.
1317
1318 unsigned Opc;
1319
1320 // Handle integer conditions first.
1321 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
1322 switch (SetCC->getCondition()) {
1323 default: assert(0 && "Illegal integer SetCC!");
1324 case ISD::SETEQ: Opc = X86::JE; break;
1325 case ISD::SETGT: Opc = X86::JG; break;
1326 case ISD::SETGE: Opc = X86::JGE; break;
1327 case ISD::SETLT: Opc = X86::JL; break;
1328 case ISD::SETLE: Opc = X86::JLE; break;
1329 case ISD::SETNE: Opc = X86::JNE; break;
1330 case ISD::SETULT: Opc = X86::JB; break;
1331 case ISD::SETUGT: Opc = X86::JA; break;
1332 case ISD::SETULE: Opc = X86::JBE; break;
1333 case ISD::SETUGE: Opc = X86::JAE; break;
1334 }
Chris Lattner37ed2852005-01-11 04:06:27 +00001335 Select(Chain);
Chris Lattner3be6cd52005-01-17 01:34:14 +00001336 EmitCMP(SetCC->getOperand(0), SetCC->getOperand(1), SetCC->hasOneUse());
Chris Lattner88c8a232005-01-07 07:49:41 +00001337 BuildMI(BB, Opc, 1).addMBB(Dest);
1338 return false;
1339 }
1340
Chris Lattner88c8a232005-01-07 07:49:41 +00001341 unsigned Opc2 = 0; // Second branch if needed.
1342
1343 // On a floating point condition, the flags are set as follows:
1344 // ZF PF CF op
1345 // 0 | 0 | 0 | X > Y
1346 // 0 | 0 | 1 | X < Y
1347 // 1 | 0 | 0 | X == Y
1348 // 1 | 1 | 1 | unordered
1349 //
1350 switch (SetCC->getCondition()) {
1351 default: assert(0 && "Invalid FP setcc!");
1352 case ISD::SETUEQ:
1353 case ISD::SETEQ: Opc = X86::JE; break; // True if ZF = 1
1354 case ISD::SETOGT:
1355 case ISD::SETGT: Opc = X86::JA; break; // True if CF = 0 and ZF = 0
1356 case ISD::SETOGE:
1357 case ISD::SETGE: Opc = X86::JAE; break; // True if CF = 0
1358 case ISD::SETULT:
1359 case ISD::SETLT: Opc = X86::JB; break; // True if CF = 1
1360 case ISD::SETULE:
1361 case ISD::SETLE: Opc = X86::JBE; break; // True if CF = 1 or ZF = 1
1362 case ISD::SETONE:
1363 case ISD::SETNE: Opc = X86::JNE; break; // True if ZF = 0
1364 case ISD::SETUO: Opc = X86::JP; break; // True if PF = 1
1365 case ISD::SETO: Opc = X86::JNP; break; // True if PF = 0
1366 case ISD::SETUGT: // PF = 1 | (ZF = 0 & CF = 0)
1367 Opc = X86::JA; // ZF = 0 & CF = 0
1368 Opc2 = X86::JP; // PF = 1
1369 break;
1370 case ISD::SETUGE: // PF = 1 | CF = 0
1371 Opc = X86::JAE; // CF = 0
1372 Opc2 = X86::JP; // PF = 1
1373 break;
1374 case ISD::SETUNE: // PF = 1 | ZF = 0
1375 Opc = X86::JNE; // ZF = 0
1376 Opc2 = X86::JP; // PF = 1
1377 break;
1378 case ISD::SETOEQ: // PF = 0 & ZF = 1
1379 //X86::JNP, X86::JE
1380 //X86::AND8rr
1381 return true; // FIXME: Emit more efficient code for this branch.
1382 case ISD::SETOLT: // PF = 0 & CF = 1
1383 //X86::JNP, X86::JB
1384 //X86::AND8rr
1385 return true; // FIXME: Emit more efficient code for this branch.
1386 case ISD::SETOLE: // PF = 0 & (CF = 1 || ZF = 1)
1387 //X86::JNP, X86::JBE
1388 //X86::AND8rr
1389 return true; // FIXME: Emit more efficient code for this branch.
1390 }
1391
Chris Lattner37ed2852005-01-11 04:06:27 +00001392 Select(Chain);
Chris Lattner3be6cd52005-01-17 01:34:14 +00001393 EmitCMP(SetCC->getOperand(0), SetCC->getOperand(1), SetCC->hasOneUse());
Chris Lattner88c8a232005-01-07 07:49:41 +00001394 BuildMI(BB, Opc, 1).addMBB(Dest);
1395 if (Opc2)
1396 BuildMI(BB, Opc2, 1).addMBB(Dest);
1397 return false;
1398}
1399
Chris Lattner1d13a922005-01-10 22:10:13 +00001400/// EmitSelectCC - Emit code into BB that performs a select operation between
1401/// the two registers RTrue and RFalse, generating a result into RDest. Return
1402/// true if the fold cannot be performed.
1403///
1404void ISel::EmitSelectCC(SDOperand Cond, MVT::ValueType SVT,
1405 unsigned RTrue, unsigned RFalse, unsigned RDest) {
1406 enum Condition {
1407 EQ, NE, LT, LE, GT, GE, B, BE, A, AE, P, NP,
1408 NOT_SET
1409 } CondCode = NOT_SET;
1410
1411 static const unsigned CMOVTAB16[] = {
1412 X86::CMOVE16rr, X86::CMOVNE16rr, X86::CMOVL16rr, X86::CMOVLE16rr,
1413 X86::CMOVG16rr, X86::CMOVGE16rr, X86::CMOVB16rr, X86::CMOVBE16rr,
Misha Brukmanc88330a2005-04-21 23:38:14 +00001414 X86::CMOVA16rr, X86::CMOVAE16rr, X86::CMOVP16rr, X86::CMOVNP16rr,
Chris Lattner1d13a922005-01-10 22:10:13 +00001415 };
1416 static const unsigned CMOVTAB32[] = {
1417 X86::CMOVE32rr, X86::CMOVNE32rr, X86::CMOVL32rr, X86::CMOVLE32rr,
1418 X86::CMOVG32rr, X86::CMOVGE32rr, X86::CMOVB32rr, X86::CMOVBE32rr,
Misha Brukmanc88330a2005-04-21 23:38:14 +00001419 X86::CMOVA32rr, X86::CMOVAE32rr, X86::CMOVP32rr, X86::CMOVNP32rr,
Chris Lattner1d13a922005-01-10 22:10:13 +00001420 };
1421 static const unsigned CMOVTABFP[] = {
1422 X86::FCMOVE , X86::FCMOVNE, /*missing*/0, /*missing*/0,
1423 /*missing*/0, /*missing*/0, X86::FCMOVB , X86::FCMOVBE,
1424 X86::FCMOVA , X86::FCMOVAE, X86::FCMOVP , X86::FCMOVNP
1425 };
1426
1427 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Cond)) {
1428 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
1429 switch (SetCC->getCondition()) {
1430 default: assert(0 && "Unknown integer comparison!");
1431 case ISD::SETEQ: CondCode = EQ; break;
1432 case ISD::SETGT: CondCode = GT; break;
1433 case ISD::SETGE: CondCode = GE; break;
1434 case ISD::SETLT: CondCode = LT; break;
1435 case ISD::SETLE: CondCode = LE; break;
1436 case ISD::SETNE: CondCode = NE; break;
1437 case ISD::SETULT: CondCode = B; break;
1438 case ISD::SETUGT: CondCode = A; break;
1439 case ISD::SETULE: CondCode = BE; break;
1440 case ISD::SETUGE: CondCode = AE; break;
1441 }
1442 } else {
1443 // On a floating point condition, the flags are set as follows:
1444 // ZF PF CF op
1445 // 0 | 0 | 0 | X > Y
1446 // 0 | 0 | 1 | X < Y
1447 // 1 | 0 | 0 | X == Y
1448 // 1 | 1 | 1 | unordered
1449 //
1450 switch (SetCC->getCondition()) {
1451 default: assert(0 && "Unknown FP comparison!");
1452 case ISD::SETUEQ:
1453 case ISD::SETEQ: CondCode = EQ; break; // True if ZF = 1
1454 case ISD::SETOGT:
1455 case ISD::SETGT: CondCode = A; break; // True if CF = 0 and ZF = 0
1456 case ISD::SETOGE:
1457 case ISD::SETGE: CondCode = AE; break; // True if CF = 0
1458 case ISD::SETULT:
1459 case ISD::SETLT: CondCode = B; break; // True if CF = 1
1460 case ISD::SETULE:
1461 case ISD::SETLE: CondCode = BE; break; // True if CF = 1 or ZF = 1
1462 case ISD::SETONE:
1463 case ISD::SETNE: CondCode = NE; break; // True if ZF = 0
1464 case ISD::SETUO: CondCode = P; break; // True if PF = 1
1465 case ISD::SETO: CondCode = NP; break; // True if PF = 0
1466 case ISD::SETUGT: // PF = 1 | (ZF = 0 & CF = 0)
1467 case ISD::SETUGE: // PF = 1 | CF = 0
1468 case ISD::SETUNE: // PF = 1 | ZF = 0
1469 case ISD::SETOEQ: // PF = 0 & ZF = 1
1470 case ISD::SETOLT: // PF = 0 & CF = 1
1471 case ISD::SETOLE: // PF = 0 & (CF = 1 || ZF = 1)
1472 // We cannot emit this comparison as a single cmov.
1473 break;
1474 }
1475 }
1476 }
1477
1478 unsigned Opc = 0;
1479 if (CondCode != NOT_SET) {
1480 switch (SVT) {
1481 default: assert(0 && "Cannot select this type!");
1482 case MVT::i16: Opc = CMOVTAB16[CondCode]; break;
1483 case MVT::i32: Opc = CMOVTAB32[CondCode]; break;
Chris Lattnere44e6d12005-01-11 03:50:45 +00001484 case MVT::f64: Opc = CMOVTABFP[CondCode]; break;
Chris Lattner1d13a922005-01-10 22:10:13 +00001485 }
1486 }
1487
1488 // Finally, if we weren't able to fold this, just emit the condition and test
1489 // it.
1490 if (CondCode == NOT_SET || Opc == 0) {
1491 // Get the condition into the zero flag.
1492 unsigned CondReg = SelectExpr(Cond);
1493 BuildMI(BB, X86::TEST8rr, 2).addReg(CondReg).addReg(CondReg);
1494
1495 switch (SVT) {
1496 default: assert(0 && "Cannot select this type!");
1497 case MVT::i16: Opc = X86::CMOVE16rr; break;
1498 case MVT::i32: Opc = X86::CMOVE32rr; break;
Chris Lattnere44e6d12005-01-11 03:50:45 +00001499 case MVT::f64: Opc = X86::FCMOVE; break;
Chris Lattner1d13a922005-01-10 22:10:13 +00001500 }
1501 } else {
1502 // FIXME: CMP R, 0 -> TEST R, R
Chris Lattner3be6cd52005-01-17 01:34:14 +00001503 EmitCMP(Cond.getOperand(0), Cond.getOperand(1), Cond.Val->hasOneUse());
Chris Lattner8fea42b2005-01-11 03:37:59 +00001504 std::swap(RTrue, RFalse);
Chris Lattner1d13a922005-01-10 22:10:13 +00001505 }
1506 BuildMI(BB, Opc, 2, RDest).addReg(RTrue).addReg(RFalse);
1507}
1508
Chris Lattner3be6cd52005-01-17 01:34:14 +00001509void ISel::EmitCMP(SDOperand LHS, SDOperand RHS, bool HasOneUse) {
Chris Lattner0d1f82a2005-01-11 03:11:44 +00001510 unsigned Opc;
Chris Lattner88c8a232005-01-07 07:49:41 +00001511 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(RHS)) {
1512 Opc = 0;
Chris Lattnera56d29d2005-01-17 06:26:58 +00001513 if (HasOneUse && isFoldableLoad(LHS, RHS)) {
Chris Lattner2cfce682005-01-12 02:02:48 +00001514 switch (RHS.getValueType()) {
1515 default: break;
1516 case MVT::i1:
1517 case MVT::i8: Opc = X86::CMP8mi; break;
1518 case MVT::i16: Opc = X86::CMP16mi; break;
1519 case MVT::i32: Opc = X86::CMP32mi; break;
1520 }
1521 if (Opc) {
1522 X86AddressMode AM;
1523 EmitFoldedLoad(LHS, AM);
1524 addFullAddress(BuildMI(BB, Opc, 5), AM).addImm(CN->getValue());
1525 return;
1526 }
1527 }
1528
Chris Lattner88c8a232005-01-07 07:49:41 +00001529 switch (RHS.getValueType()) {
1530 default: break;
1531 case MVT::i1:
1532 case MVT::i8: Opc = X86::CMP8ri; break;
1533 case MVT::i16: Opc = X86::CMP16ri; break;
1534 case MVT::i32: Opc = X86::CMP32ri; break;
1535 }
1536 if (Opc) {
Chris Lattner0d1f82a2005-01-11 03:11:44 +00001537 unsigned Tmp1 = SelectExpr(LHS);
Chris Lattner88c8a232005-01-07 07:49:41 +00001538 BuildMI(BB, Opc, 2).addReg(Tmp1).addImm(CN->getValue());
1539 return;
1540 }
Chris Lattner720a62e2005-01-14 22:37:41 +00001541 } else if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(RHS)) {
1542 if (CN->isExactlyValue(+0.0) ||
1543 CN->isExactlyValue(-0.0)) {
1544 unsigned Reg = SelectExpr(LHS);
1545 BuildMI(BB, X86::FTST, 1).addReg(Reg);
1546 BuildMI(BB, X86::FNSTSW8r, 0);
1547 BuildMI(BB, X86::SAHF, 1);
Chris Lattner43832b02005-03-17 16:29:26 +00001548 return;
Chris Lattner720a62e2005-01-14 22:37:41 +00001549 }
Chris Lattner88c8a232005-01-07 07:49:41 +00001550 }
1551
Chris Lattner2cfce682005-01-12 02:02:48 +00001552 Opc = 0;
Chris Lattnera56d29d2005-01-17 06:26:58 +00001553 if (HasOneUse && isFoldableLoad(LHS, RHS)) {
Chris Lattner2cfce682005-01-12 02:02:48 +00001554 switch (RHS.getValueType()) {
1555 default: break;
1556 case MVT::i1:
1557 case MVT::i8: Opc = X86::CMP8mr; break;
1558 case MVT::i16: Opc = X86::CMP16mr; break;
1559 case MVT::i32: Opc = X86::CMP32mr; break;
1560 }
1561 if (Opc) {
1562 X86AddressMode AM;
Chris Lattner3676cd62005-01-13 05:53:16 +00001563 EmitFoldedLoad(LHS, AM);
1564 unsigned Reg = SelectExpr(RHS);
Chris Lattner2cfce682005-01-12 02:02:48 +00001565 addFullAddress(BuildMI(BB, Opc, 5), AM).addReg(Reg);
1566 return;
1567 }
1568 }
1569
Chris Lattner88c8a232005-01-07 07:49:41 +00001570 switch (LHS.getValueType()) {
1571 default: assert(0 && "Cannot compare this value!");
1572 case MVT::i1:
1573 case MVT::i8: Opc = X86::CMP8rr; break;
1574 case MVT::i16: Opc = X86::CMP16rr; break;
1575 case MVT::i32: Opc = X86::CMP32rr; break;
Chris Lattnere44e6d12005-01-11 03:50:45 +00001576 case MVT::f64: Opc = X86::FUCOMIr; break;
Chris Lattner88c8a232005-01-07 07:49:41 +00001577 }
Chris Lattner0d1f82a2005-01-11 03:11:44 +00001578 unsigned Tmp1, Tmp2;
1579 if (getRegPressure(LHS) > getRegPressure(RHS)) {
1580 Tmp1 = SelectExpr(LHS);
1581 Tmp2 = SelectExpr(RHS);
1582 } else {
1583 Tmp2 = SelectExpr(RHS);
1584 Tmp1 = SelectExpr(LHS);
1585 }
Chris Lattner88c8a232005-01-07 07:49:41 +00001586 BuildMI(BB, Opc, 2).addReg(Tmp1).addReg(Tmp2);
1587}
1588
Chris Lattner62b22422005-01-11 21:19:59 +00001589/// isFoldableLoad - Return true if this is a load instruction that can safely
1590/// be folded into an operation that uses it.
Chris Lattner30607ec2005-01-25 20:03:11 +00001591bool ISel::isFoldableLoad(SDOperand Op, SDOperand OtherOp, bool FloatPromoteOk){
1592 if (Op.getOpcode() == ISD::LOAD) {
1593 // FIXME: currently can't fold constant pool indexes.
1594 if (isa<ConstantPoolSDNode>(Op.getOperand(1)))
1595 return false;
1596 } else if (FloatPromoteOk && Op.getOpcode() == ISD::EXTLOAD &&
1597 cast<MVTSDNode>(Op)->getExtraValueType() == MVT::f32) {
1598 // FIXME: currently can't fold constant pool indexes.
1599 if (isa<ConstantPoolSDNode>(Op.getOperand(1)))
1600 return false;
1601 } else {
Chris Lattner62b22422005-01-11 21:19:59 +00001602 return false;
Chris Lattner30607ec2005-01-25 20:03:11 +00001603 }
Chris Lattner62b22422005-01-11 21:19:59 +00001604
1605 // If this load has already been emitted, we clearly can't fold it.
Chris Lattner3676cd62005-01-13 05:53:16 +00001606 assert(Op.ResNo == 0 && "Not a use of the value of the load?");
1607 if (ExprMap.count(Op.getValue(1))) return false;
1608 assert(!ExprMap.count(Op.getValue(0)) && "Value in map but not token chain?");
Chris Lattner78d30282005-01-18 03:51:59 +00001609 assert(!ExprMap.count(Op.getValue(1))&&"Token lowered but value not in map?");
Chris Lattner62b22422005-01-11 21:19:59 +00001610
Chris Lattnera56d29d2005-01-17 06:26:58 +00001611 // If there is not just one use of its value, we cannot fold.
1612 if (!Op.Val->hasNUsesOfValue(1, 0)) return false;
1613
1614 // Finally, we cannot fold the load into the operation if this would induce a
1615 // cycle into the resultant dag. To check for this, see if OtherOp (the other
1616 // operand of the operation we are folding the load into) can possible use the
1617 // chain node defined by the load.
1618 if (OtherOp.Val && !Op.Val->hasNUsesOfValue(0, 1)) { // Has uses of chain?
1619 std::set<SDNode*> Visited;
1620 if (NodeTransitivelyUsesValue(OtherOp, Op.getValue(1), Visited))
1621 return false;
1622 }
1623 return true;
Chris Lattner62b22422005-01-11 21:19:59 +00001624}
1625
Chris Lattnera56d29d2005-01-17 06:26:58 +00001626
Chris Lattner62b22422005-01-11 21:19:59 +00001627/// EmitFoldedLoad - Ensure that the arguments of the load are code generated,
1628/// and compute the address being loaded into AM.
1629void ISel::EmitFoldedLoad(SDOperand Op, X86AddressMode &AM) {
1630 SDOperand Chain = Op.getOperand(0);
1631 SDOperand Address = Op.getOperand(1);
Chris Lattnera7acdda2005-01-18 01:06:26 +00001632
Chris Lattner62b22422005-01-11 21:19:59 +00001633 if (getRegPressure(Chain) > getRegPressure(Address)) {
1634 Select(Chain);
1635 SelectAddress(Address, AM);
1636 } else {
1637 SelectAddress(Address, AM);
1638 Select(Chain);
1639 }
1640
1641 // The chain for this load is now lowered.
Chris Lattner3676cd62005-01-13 05:53:16 +00001642 assert(ExprMap.count(SDOperand(Op.Val, 1)) == 0 &&
1643 "Load emitted more than once?");
Chris Lattner78d30282005-01-18 03:51:59 +00001644 if (!ExprMap.insert(std::make_pair(Op.getValue(1), 1)).second)
Chris Lattner3676cd62005-01-13 05:53:16 +00001645 assert(0 && "Load emitted more than once!");
Chris Lattner62b22422005-01-11 21:19:59 +00001646}
1647
Chris Lattner29f58192005-01-19 07:37:26 +00001648// EmitOrOpOp - Pattern match the expression (Op1|Op2), where we know that op1
1649// and op2 are i8/i16/i32 values with one use each (the or). If we can form a
1650// SHLD or SHRD, emit the instruction (generating the value into DestReg) and
1651// return true.
1652bool ISel::EmitOrOpOp(SDOperand Op1, SDOperand Op2, unsigned DestReg) {
Chris Lattner41fe2012005-01-19 06:18:43 +00001653 if (Op1.getOpcode() == ISD::SHL && Op2.getOpcode() == ISD::SRL) {
1654 // good!
1655 } else if (Op2.getOpcode() == ISD::SHL && Op1.getOpcode() == ISD::SRL) {
1656 std::swap(Op1, Op2); // Op1 is the SHL now.
1657 } else {
1658 return false; // No match
1659 }
1660
1661 SDOperand ShlVal = Op1.getOperand(0);
1662 SDOperand ShlAmt = Op1.getOperand(1);
1663 SDOperand ShrVal = Op2.getOperand(0);
1664 SDOperand ShrAmt = Op2.getOperand(1);
1665
Chris Lattner29f58192005-01-19 07:37:26 +00001666 unsigned RegSize = MVT::getSizeInBits(Op1.getValueType());
1667
Chris Lattner41fe2012005-01-19 06:18:43 +00001668 // Find out if ShrAmt = 32-ShlAmt or ShlAmt = 32-ShrAmt.
1669 if (ShlAmt.getOpcode() == ISD::SUB && ShlAmt.getOperand(1) == ShrAmt)
1670 if (ConstantSDNode *SubCST = dyn_cast<ConstantSDNode>(ShlAmt.getOperand(0)))
Chris Lattnerde87d1462005-01-19 08:07:05 +00001671 if (SubCST->getValue() == RegSize) {
1672 // (A >> ShrAmt) | (A << (32-ShrAmt)) ==> ROR A, ShrAmt
Chris Lattner41fe2012005-01-19 06:18:43 +00001673 // (A >> ShrAmt) | (B << (32-ShrAmt)) ==> SHRD A, B, ShrAmt
Chris Lattnerde87d1462005-01-19 08:07:05 +00001674 if (ShrVal == ShlVal) {
1675 unsigned Reg, ShAmt;
1676 if (getRegPressure(ShrVal) > getRegPressure(ShrAmt)) {
1677 Reg = SelectExpr(ShrVal);
1678 ShAmt = SelectExpr(ShrAmt);
1679 } else {
1680 ShAmt = SelectExpr(ShrAmt);
1681 Reg = SelectExpr(ShrVal);
1682 }
1683 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShAmt);
1684 unsigned Opc = RegSize == 8 ? X86::ROR8rCL :
1685 (RegSize == 16 ? X86::ROR16rCL : X86::ROR32rCL);
1686 BuildMI(BB, Opc, 1, DestReg).addReg(Reg);
1687 return true;
1688 } else if (RegSize != 8) {
Chris Lattner41fe2012005-01-19 06:18:43 +00001689 unsigned AReg, BReg;
1690 if (getRegPressure(ShlVal) > getRegPressure(ShrVal)) {
Chris Lattner41fe2012005-01-19 06:18:43 +00001691 BReg = SelectExpr(ShlVal);
Chris Lattner474aac42005-01-19 17:24:34 +00001692 AReg = SelectExpr(ShrVal);
Chris Lattner41fe2012005-01-19 06:18:43 +00001693 } else {
Chris Lattner41fe2012005-01-19 06:18:43 +00001694 AReg = SelectExpr(ShrVal);
Chris Lattner474aac42005-01-19 17:24:34 +00001695 BReg = SelectExpr(ShlVal);
Chris Lattner41fe2012005-01-19 06:18:43 +00001696 }
Chris Lattnerde87d1462005-01-19 08:07:05 +00001697 unsigned ShAmt = SelectExpr(ShrAmt);
1698 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShAmt);
1699 unsigned Opc = RegSize == 16 ? X86::SHRD16rrCL : X86::SHRD32rrCL;
1700 BuildMI(BB, Opc, 2, DestReg).addReg(AReg).addReg(BReg);
Chris Lattner41fe2012005-01-19 06:18:43 +00001701 return true;
1702 }
1703 }
1704
Chris Lattnerde87d1462005-01-19 08:07:05 +00001705 if (ShrAmt.getOpcode() == ISD::SUB && ShrAmt.getOperand(1) == ShlAmt)
1706 if (ConstantSDNode *SubCST = dyn_cast<ConstantSDNode>(ShrAmt.getOperand(0)))
1707 if (SubCST->getValue() == RegSize) {
1708 // (A << ShlAmt) | (A >> (32-ShlAmt)) ==> ROL A, ShrAmt
1709 // (A << ShlAmt) | (B >> (32-ShlAmt)) ==> SHLD A, B, ShrAmt
1710 if (ShrVal == ShlVal) {
1711 unsigned Reg, ShAmt;
1712 if (getRegPressure(ShrVal) > getRegPressure(ShlAmt)) {
1713 Reg = SelectExpr(ShrVal);
1714 ShAmt = SelectExpr(ShlAmt);
1715 } else {
1716 ShAmt = SelectExpr(ShlAmt);
1717 Reg = SelectExpr(ShrVal);
1718 }
1719 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShAmt);
1720 unsigned Opc = RegSize == 8 ? X86::ROL8rCL :
1721 (RegSize == 16 ? X86::ROL16rCL : X86::ROL32rCL);
1722 BuildMI(BB, Opc, 1, DestReg).addReg(Reg);
1723 return true;
1724 } else if (RegSize != 8) {
1725 unsigned AReg, BReg;
1726 if (getRegPressure(ShlVal) > getRegPressure(ShrVal)) {
Chris Lattner474aac42005-01-19 17:24:34 +00001727 AReg = SelectExpr(ShlVal);
1728 BReg = SelectExpr(ShrVal);
Chris Lattnerde87d1462005-01-19 08:07:05 +00001729 } else {
Chris Lattner474aac42005-01-19 17:24:34 +00001730 BReg = SelectExpr(ShrVal);
1731 AReg = SelectExpr(ShlVal);
Chris Lattnerde87d1462005-01-19 08:07:05 +00001732 }
1733 unsigned ShAmt = SelectExpr(ShlAmt);
1734 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShAmt);
1735 unsigned Opc = RegSize == 16 ? X86::SHLD16rrCL : X86::SHLD32rrCL;
1736 BuildMI(BB, Opc, 2, DestReg).addReg(AReg).addReg(BReg);
1737 return true;
1738 }
1739 }
Chris Lattner41fe2012005-01-19 06:18:43 +00001740
Chris Lattnerde87d1462005-01-19 08:07:05 +00001741 if (ConstantSDNode *ShrCst = dyn_cast<ConstantSDNode>(ShrAmt))
1742 if (ConstantSDNode *ShlCst = dyn_cast<ConstantSDNode>(ShlAmt))
1743 if (ShrCst->getValue() < RegSize && ShlCst->getValue() < RegSize)
1744 if (ShrCst->getValue() == RegSize-ShlCst->getValue()) {
1745 // (A >> 5) | (A << 27) --> ROR A, 5
1746 // (A >> 5) | (B << 27) --> SHRD A, B, 5
1747 if (ShrVal == ShlVal) {
1748 unsigned Reg = SelectExpr(ShrVal);
1749 unsigned Opc = RegSize == 8 ? X86::ROR8ri :
1750 (RegSize == 16 ? X86::ROR16ri : X86::ROR32ri);
1751 BuildMI(BB, Opc, 2, DestReg).addReg(Reg).addImm(ShrCst->getValue());
1752 return true;
1753 } else if (RegSize != 8) {
1754 unsigned AReg, BReg;
1755 if (getRegPressure(ShlVal) > getRegPressure(ShrVal)) {
Chris Lattnerde87d1462005-01-19 08:07:05 +00001756 BReg = SelectExpr(ShlVal);
Chris Lattner474aac42005-01-19 17:24:34 +00001757 AReg = SelectExpr(ShrVal);
Chris Lattnerde87d1462005-01-19 08:07:05 +00001758 } else {
Chris Lattnerde87d1462005-01-19 08:07:05 +00001759 AReg = SelectExpr(ShrVal);
Chris Lattner474aac42005-01-19 17:24:34 +00001760 BReg = SelectExpr(ShlVal);
Chris Lattnerde87d1462005-01-19 08:07:05 +00001761 }
1762 unsigned Opc = RegSize == 16 ? X86::SHRD16rri8 : X86::SHRD32rri8;
1763 BuildMI(BB, Opc, 3, DestReg).addReg(AReg).addReg(BReg)
1764 .addImm(ShrCst->getValue());
1765 return true;
1766 }
1767 }
Misha Brukmanc88330a2005-04-21 23:38:14 +00001768
Chris Lattner41fe2012005-01-19 06:18:43 +00001769 return false;
1770}
1771
Chris Lattner88c8a232005-01-07 07:49:41 +00001772unsigned ISel::SelectExpr(SDOperand N) {
1773 unsigned Result;
1774 unsigned Tmp1, Tmp2, Tmp3;
1775 unsigned Opc = 0;
Chris Lattnerb52e0412005-01-08 19:28:19 +00001776 SDNode *Node = N.Val;
Chris Lattner62b22422005-01-11 21:19:59 +00001777 SDOperand Op0, Op1;
Chris Lattnerb52e0412005-01-08 19:28:19 +00001778
Chris Lattner720a62e2005-01-14 22:37:41 +00001779 if (Node->getOpcode() == ISD::CopyFromReg) {
Chris Lattner7ce7a8f2005-05-12 23:06:28 +00001780 if (MRegisterInfo::isVirtualRegister(cast<RegSDNode>(Node)->getReg()) ||
1781 cast<RegSDNode>(Node)->getReg() == X86::ESP) {
1782 // Just use the specified register as our input.
1783 return cast<RegSDNode>(Node)->getReg();
1784 }
Chris Lattner720a62e2005-01-14 22:37:41 +00001785 }
Misha Brukmanc88330a2005-04-21 23:38:14 +00001786
Chris Lattner62b22422005-01-11 21:19:59 +00001787 unsigned &Reg = ExprMap[N];
1788 if (Reg) return Reg;
Misha Brukmanc88330a2005-04-21 23:38:14 +00001789
Chris Lattnera31d4c72005-04-02 04:01:14 +00001790 switch (N.getOpcode()) {
1791 default:
Chris Lattner62b22422005-01-11 21:19:59 +00001792 Reg = Result = (N.getValueType() != MVT::Other) ?
Chris Lattnera31d4c72005-04-02 04:01:14 +00001793 MakeReg(N.getValueType()) : 1;
1794 break;
1795 case ISD::CALL:
Chris Lattner62b22422005-01-11 21:19:59 +00001796 // If this is a call instruction, make sure to prepare ALL of the result
1797 // values as well as the chain.
Chris Lattnera31d4c72005-04-02 04:01:14 +00001798 if (Node->getNumValues() == 1)
1799 Reg = Result = 1; // Void call, just a chain.
1800 else {
Chris Lattner62b22422005-01-11 21:19:59 +00001801 Result = MakeReg(Node->getValueType(0));
1802 ExprMap[N.getValue(0)] = Result;
Chris Lattnera31d4c72005-04-02 04:01:14 +00001803 for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
Chris Lattner62b22422005-01-11 21:19:59 +00001804 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
Chris Lattnera31d4c72005-04-02 04:01:14 +00001805 ExprMap[SDOperand(Node, Node->getNumValues()-1)] = 1;
Chris Lattner88c8a232005-01-07 07:49:41 +00001806 }
Chris Lattnera31d4c72005-04-02 04:01:14 +00001807 break;
1808 case ISD::ADD_PARTS:
1809 case ISD::SUB_PARTS:
1810 case ISD::SHL_PARTS:
1811 case ISD::SRL_PARTS:
1812 case ISD::SRA_PARTS:
1813 Result = MakeReg(Node->getValueType(0));
1814 ExprMap[N.getValue(0)] = Result;
1815 for (unsigned i = 1, e = N.Val->getNumValues(); i != e; ++i)
1816 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
1817 break;
Chris Lattner88c8a232005-01-07 07:49:41 +00001818 }
Misha Brukmanc88330a2005-04-21 23:38:14 +00001819
Chris Lattner88c8a232005-01-07 07:49:41 +00001820 switch (N.getOpcode()) {
1821 default:
Chris Lattnerb52e0412005-01-08 19:28:19 +00001822 Node->dump();
Chris Lattner88c8a232005-01-07 07:49:41 +00001823 assert(0 && "Node not handled!\n");
Chris Lattner7ce7a8f2005-05-12 23:06:28 +00001824 case ISD::CopyFromReg:
1825 Select(N.getOperand(0));
1826 if (Result == 1) {
1827 Reg = Result = ExprMap[N.getValue(0)] =
1828 MakeReg(N.getValue(0).getValueType());
1829 }
1830 switch (Node->getValueType(0)) {
1831 default: assert(0 && "Cannot CopyFromReg this!");
1832 case MVT::i1:
1833 case MVT::i8:
1834 BuildMI(BB, X86::MOV8rr, 1,
1835 Result).addReg(cast<RegSDNode>(Node)->getReg());
1836 return Result;
1837 case MVT::i16:
1838 BuildMI(BB, X86::MOV16rr, 1,
1839 Result).addReg(cast<RegSDNode>(Node)->getReg());
1840 return Result;
1841 case MVT::i32:
1842 BuildMI(BB, X86::MOV32rr, 1,
1843 Result).addReg(cast<RegSDNode>(Node)->getReg());
1844 return Result;
1845 }
1846
Chris Lattner88c8a232005-01-07 07:49:41 +00001847 case ISD::FrameIndex:
1848 Tmp1 = cast<FrameIndexSDNode>(N)->getIndex();
1849 addFrameReference(BuildMI(BB, X86::LEA32r, 4, Result), (int)Tmp1);
1850 return Result;
1851 case ISD::ConstantPool:
1852 Tmp1 = cast<ConstantPoolSDNode>(N)->getIndex();
1853 addConstantPoolReference(BuildMI(BB, X86::LEA32r, 4, Result), Tmp1);
1854 return Result;
1855 case ISD::ConstantFP:
1856 ContainsFPCode = true;
1857 Tmp1 = Result; // Intermediate Register
1858 if (cast<ConstantFPSDNode>(N)->getValue() < 0.0 ||
1859 cast<ConstantFPSDNode>(N)->isExactlyValue(-0.0))
1860 Tmp1 = MakeReg(MVT::f64);
1861
1862 if (cast<ConstantFPSDNode>(N)->isExactlyValue(+0.0) ||
1863 cast<ConstantFPSDNode>(N)->isExactlyValue(-0.0))
1864 BuildMI(BB, X86::FLD0, 0, Tmp1);
1865 else if (cast<ConstantFPSDNode>(N)->isExactlyValue(+1.0) ||
1866 cast<ConstantFPSDNode>(N)->isExactlyValue(-1.0))
1867 BuildMI(BB, X86::FLD1, 0, Tmp1);
1868 else
1869 assert(0 && "Unexpected constant!");
1870 if (Tmp1 != Result)
1871 BuildMI(BB, X86::FCHS, 1, Result).addReg(Tmp1);
1872 return Result;
1873 case ISD::Constant:
1874 switch (N.getValueType()) {
1875 default: assert(0 && "Cannot use constants of this type!");
1876 case MVT::i1:
1877 case MVT::i8: Opc = X86::MOV8ri; break;
1878 case MVT::i16: Opc = X86::MOV16ri; break;
1879 case MVT::i32: Opc = X86::MOV32ri; break;
1880 }
1881 BuildMI(BB, Opc, 1,Result).addImm(cast<ConstantSDNode>(N)->getValue());
1882 return Result;
Chris Lattnerf4b985d2005-04-01 22:46:45 +00001883 case ISD::UNDEF:
1884 if (Node->getValueType(0) == MVT::f64) {
1885 // FIXME: SHOULD TEACH STACKIFIER ABOUT UNDEF VALUES!
1886 BuildMI(BB, X86::FLD0, 0, Result);
1887 } else {
1888 BuildMI(BB, X86::IMPLICIT_DEF, 0, Result);
1889 }
1890 return Result;
Chris Lattner88c8a232005-01-07 07:49:41 +00001891 case ISD::GlobalAddress: {
1892 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
1893 BuildMI(BB, X86::MOV32ri, 1, Result).addGlobalAddress(GV);
1894 return Result;
1895 }
1896 case ISD::ExternalSymbol: {
1897 const char *Sym = cast<ExternalSymbolSDNode>(N)->getSymbol();
1898 BuildMI(BB, X86::MOV32ri, 1, Result).addExternalSymbol(Sym);
1899 return Result;
1900 }
Chris Lattner88c8a232005-01-07 07:49:41 +00001901 case ISD::ZERO_EXTEND: {
1902 int DestIs16 = N.getValueType() == MVT::i16;
1903 int SrcIs16 = N.getOperand(0).getValueType() == MVT::i16;
Chris Lattner282781c2005-01-09 18:52:44 +00001904
1905 // FIXME: This hack is here for zero extension casts from bool to i8. This
1906 // would not be needed if bools were promoted by Legalize.
1907 if (N.getValueType() == MVT::i8) {
Chris Lattnerb0eef822005-01-11 23:33:00 +00001908 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner282781c2005-01-09 18:52:44 +00001909 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(Tmp1);
1910 return Result;
1911 }
Chris Lattner88c8a232005-01-07 07:49:41 +00001912
Chris Lattnera56d29d2005-01-17 06:26:58 +00001913 if (isFoldableLoad(N.getOperand(0), SDOperand())) {
Chris Lattnerb0eef822005-01-11 23:33:00 +00001914 static const unsigned Opc[3] = {
1915 X86::MOVZX32rm8, X86::MOVZX32rm16, X86::MOVZX16rm8
1916 };
1917
1918 X86AddressMode AM;
1919 EmitFoldedLoad(N.getOperand(0), AM);
1920 addFullAddress(BuildMI(BB, Opc[SrcIs16+DestIs16*2], 4, Result), AM);
Misha Brukmanc88330a2005-04-21 23:38:14 +00001921
Chris Lattnerb0eef822005-01-11 23:33:00 +00001922 return Result;
1923 }
1924
Chris Lattner88c8a232005-01-07 07:49:41 +00001925 static const unsigned Opc[3] = {
1926 X86::MOVZX32rr8, X86::MOVZX32rr16, X86::MOVZX16rr8
1927 };
Chris Lattnerb0eef822005-01-11 23:33:00 +00001928 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner88c8a232005-01-07 07:49:41 +00001929 BuildMI(BB, Opc[SrcIs16+DestIs16*2], 1, Result).addReg(Tmp1);
1930 return Result;
Misha Brukmanc88330a2005-04-21 23:38:14 +00001931 }
Chris Lattner88c8a232005-01-07 07:49:41 +00001932 case ISD::SIGN_EXTEND: {
1933 int DestIs16 = N.getValueType() == MVT::i16;
1934 int SrcIs16 = N.getOperand(0).getValueType() == MVT::i16;
1935
Chris Lattner282781c2005-01-09 18:52:44 +00001936 // FIXME: Legalize should promote bools to i8!
1937 assert(N.getOperand(0).getValueType() != MVT::i1 &&
1938 "Sign extend from bool not implemented!");
1939
Chris Lattnera56d29d2005-01-17 06:26:58 +00001940 if (isFoldableLoad(N.getOperand(0), SDOperand())) {
Chris Lattnerb0eef822005-01-11 23:33:00 +00001941 static const unsigned Opc[3] = {
1942 X86::MOVSX32rm8, X86::MOVSX32rm16, X86::MOVSX16rm8
1943 };
1944
1945 X86AddressMode AM;
1946 EmitFoldedLoad(N.getOperand(0), AM);
1947 addFullAddress(BuildMI(BB, Opc[SrcIs16+DestIs16*2], 4, Result), AM);
1948 return Result;
1949 }
1950
Chris Lattner88c8a232005-01-07 07:49:41 +00001951 static const unsigned Opc[3] = {
1952 X86::MOVSX32rr8, X86::MOVSX32rr16, X86::MOVSX16rr8
1953 };
1954 Tmp1 = SelectExpr(N.getOperand(0));
1955 BuildMI(BB, Opc[SrcIs16+DestIs16*2], 1, Result).addReg(Tmp1);
1956 return Result;
1957 }
1958 case ISD::TRUNCATE:
Chris Lattnerb7fe57a2005-01-12 02:19:06 +00001959 // Fold TRUNCATE (LOAD P) into a smaller load from P.
Chris Lattner14947c32005-01-18 20:05:56 +00001960 // FIXME: This should be performed by the DAGCombiner.
Chris Lattnera56d29d2005-01-17 06:26:58 +00001961 if (isFoldableLoad(N.getOperand(0), SDOperand())) {
Chris Lattnerb7fe57a2005-01-12 02:19:06 +00001962 switch (N.getValueType()) {
1963 default: assert(0 && "Unknown truncate!");
1964 case MVT::i1:
1965 case MVT::i8: Opc = X86::MOV8rm; break;
1966 case MVT::i16: Opc = X86::MOV16rm; break;
1967 }
1968 X86AddressMode AM;
1969 EmitFoldedLoad(N.getOperand(0), AM);
1970 addFullAddress(BuildMI(BB, Opc, 4, Result), AM);
1971 return Result;
1972 }
1973
Chris Lattner88c8a232005-01-07 07:49:41 +00001974 // Handle cast of LARGER int to SMALLER int using a move to EAX followed by
1975 // a move out of AX or AL.
1976 switch (N.getOperand(0).getValueType()) {
1977 default: assert(0 && "Unknown truncate!");
1978 case MVT::i8: Tmp2 = X86::AL; Opc = X86::MOV8rr; break;
1979 case MVT::i16: Tmp2 = X86::AX; Opc = X86::MOV16rr; break;
1980 case MVT::i32: Tmp2 = X86::EAX; Opc = X86::MOV32rr; break;
1981 }
1982 Tmp1 = SelectExpr(N.getOperand(0));
1983 BuildMI(BB, Opc, 1, Tmp2).addReg(Tmp1);
1984
1985 switch (N.getValueType()) {
1986 default: assert(0 && "Unknown truncate!");
1987 case MVT::i1:
1988 case MVT::i8: Tmp2 = X86::AL; Opc = X86::MOV8rr; break;
1989 case MVT::i16: Tmp2 = X86::AX; Opc = X86::MOV16rr; break;
1990 }
1991 BuildMI(BB, Opc, 1, Result).addReg(Tmp2);
1992 return Result;
1993
Chris Lattner282781c2005-01-09 18:52:44 +00001994 case ISD::SINT_TO_FP:
1995 case ISD::UINT_TO_FP: {
1996 // FIXME: Most of this grunt work should be done by legalize!
Chris Lattnere44e6d12005-01-11 03:50:45 +00001997 ContainsFPCode = true;
Chris Lattner282781c2005-01-09 18:52:44 +00001998
1999 // Promote the integer to a type supported by FLD. We do this because there
2000 // are no unsigned FLD instructions, so we must promote an unsigned value to
2001 // a larger signed value, then use FLD on the larger value.
2002 //
2003 MVT::ValueType PromoteType = MVT::Other;
2004 MVT::ValueType SrcTy = N.getOperand(0).getValueType();
2005 unsigned PromoteOpcode = 0;
2006 unsigned RealDestReg = Result;
2007 switch (SrcTy) {
2008 case MVT::i1:
2009 case MVT::i8:
2010 // We don't have the facilities for directly loading byte sized data from
2011 // memory (even signed). Promote it to 16 bits.
2012 PromoteType = MVT::i16;
2013 PromoteOpcode = Node->getOpcode() == ISD::SINT_TO_FP ?
2014 X86::MOVSX16rr8 : X86::MOVZX16rr8;
2015 break;
2016 case MVT::i16:
2017 if (Node->getOpcode() == ISD::UINT_TO_FP) {
2018 PromoteType = MVT::i32;
2019 PromoteOpcode = X86::MOVZX32rr16;
2020 }
2021 break;
2022 default:
2023 // Don't fild into the real destination.
2024 if (Node->getOpcode() == ISD::UINT_TO_FP)
2025 Result = MakeReg(Node->getValueType(0));
2026 break;
2027 }
2028
2029 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
Misha Brukmanc88330a2005-04-21 23:38:14 +00002030
Chris Lattner282781c2005-01-09 18:52:44 +00002031 if (PromoteType != MVT::Other) {
2032 Tmp2 = MakeReg(PromoteType);
2033 BuildMI(BB, PromoteOpcode, 1, Tmp2).addReg(Tmp1);
2034 SrcTy = PromoteType;
2035 Tmp1 = Tmp2;
2036 }
2037
2038 // Spill the integer to memory and reload it from there.
2039 unsigned Size = MVT::getSizeInBits(SrcTy)/8;
2040 MachineFunction *F = BB->getParent();
2041 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, Size);
2042
2043 switch (SrcTy) {
Chris Lattner282781c2005-01-09 18:52:44 +00002044 case MVT::i32:
2045 addFrameReference(BuildMI(BB, X86::MOV32mr, 5),
2046 FrameIdx).addReg(Tmp1);
2047 addFrameReference(BuildMI(BB, X86::FILD32m, 5, Result), FrameIdx);
2048 break;
2049 case MVT::i16:
2050 addFrameReference(BuildMI(BB, X86::MOV16mr, 5),
2051 FrameIdx).addReg(Tmp1);
2052 addFrameReference(BuildMI(BB, X86::FILD16m, 5, Result), FrameIdx);
2053 break;
2054 default: break; // No promotion required.
2055 }
2056
Chris Lattnerf8f79c42005-01-12 04:00:00 +00002057 if (Node->getOpcode() == ISD::UINT_TO_FP && Result != RealDestReg) {
Chris Lattner282781c2005-01-09 18:52:44 +00002058 // If this is a cast from uint -> double, we need to be careful when if
2059 // the "sign" bit is set. If so, we don't want to make a negative number,
2060 // we want to make a positive number. Emit code to add an offset if the
2061 // sign bit is set.
2062
2063 // Compute whether the sign bit is set by shifting the reg right 31 bits.
2064 unsigned IsNeg = MakeReg(MVT::i32);
2065 BuildMI(BB, X86::SHR32ri, 2, IsNeg).addReg(Tmp1).addImm(31);
2066
2067 // Create a CP value that has the offset in one word and 0 in the other.
2068 static ConstantInt *TheOffset = ConstantUInt::get(Type::ULongTy,
2069 0x4f80000000000000ULL);
2070 unsigned CPI = F->getConstantPool()->getConstantPoolIndex(TheOffset);
2071 BuildMI(BB, X86::FADD32m, 5, RealDestReg).addReg(Result)
2072 .addConstantPoolIndex(CPI).addZImm(4).addReg(IsNeg).addSImm(0);
Chris Lattner282781c2005-01-09 18:52:44 +00002073 }
2074 return RealDestReg;
2075 }
2076 case ISD::FP_TO_SINT:
2077 case ISD::FP_TO_UINT: {
2078 // FIXME: Most of this grunt work should be done by legalize!
2079 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
2080
2081 // Change the floating point control register to use "round towards zero"
2082 // mode when truncating to an integer value.
2083 //
2084 MachineFunction *F = BB->getParent();
2085 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
2086 addFrameReference(BuildMI(BB, X86::FNSTCW16m, 4), CWFrameIdx);
2087
2088 // Load the old value of the high byte of the control word...
2089 unsigned HighPartOfCW = MakeReg(MVT::i8);
2090 addFrameReference(BuildMI(BB, X86::MOV8rm, 4, HighPartOfCW),
2091 CWFrameIdx, 1);
2092
2093 // Set the high part to be round to zero...
2094 addFrameReference(BuildMI(BB, X86::MOV8mi, 5),
2095 CWFrameIdx, 1).addImm(12);
2096
2097 // Reload the modified control word now...
2098 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
Misha Brukmanc88330a2005-04-21 23:38:14 +00002099
Chris Lattner282781c2005-01-09 18:52:44 +00002100 // Restore the memory image of control word to original value
2101 addFrameReference(BuildMI(BB, X86::MOV8mr, 5),
2102 CWFrameIdx, 1).addReg(HighPartOfCW);
2103
2104 // We don't have the facilities for directly storing byte sized data to
2105 // memory. Promote it to 16 bits. We also must promote unsigned values to
2106 // larger classes because we only have signed FP stores.
2107 MVT::ValueType StoreClass = Node->getValueType(0);
2108 if (StoreClass == MVT::i8 || Node->getOpcode() == ISD::FP_TO_UINT)
2109 switch (StoreClass) {
Chris Lattnere6266112005-05-09 05:33:18 +00002110 case MVT::i1:
Chris Lattner282781c2005-01-09 18:52:44 +00002111 case MVT::i8: StoreClass = MVT::i16; break;
2112 case MVT::i16: StoreClass = MVT::i32; break;
2113 case MVT::i32: StoreClass = MVT::i64; break;
Chris Lattner282781c2005-01-09 18:52:44 +00002114 default: assert(0 && "Unknown store class!");
2115 }
2116
2117 // Spill the integer to memory and reload it from there.
2118 unsigned Size = MVT::getSizeInBits(StoreClass)/8;
2119 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, Size);
2120
2121 switch (StoreClass) {
2122 default: assert(0 && "Unknown store class!");
2123 case MVT::i16:
2124 addFrameReference(BuildMI(BB, X86::FIST16m, 5), FrameIdx).addReg(Tmp1);
2125 break;
2126 case MVT::i32:
Chris Lattner66d34302005-01-09 19:49:59 +00002127 addFrameReference(BuildMI(BB, X86::FIST32m, 5), FrameIdx).addReg(Tmp1);
Chris Lattner282781c2005-01-09 18:52:44 +00002128 break;
Chris Lattner4ccd1f62005-05-09 18:37:02 +00002129 case MVT::i64:
2130 addFrameReference(BuildMI(BB, X86::FISTP64m, 5), FrameIdx).addReg(Tmp1);
2131 break; }
Chris Lattner282781c2005-01-09 18:52:44 +00002132
2133 switch (Node->getValueType(0)) {
2134 default:
2135 assert(0 && "Unknown integer type!");
Chris Lattner282781c2005-01-09 18:52:44 +00002136 case MVT::i32:
2137 addFrameReference(BuildMI(BB, X86::MOV32rm, 4, Result), FrameIdx);
2138 break;
2139 case MVT::i16:
2140 addFrameReference(BuildMI(BB, X86::MOV16rm, 4, Result), FrameIdx);
2141 break;
2142 case MVT::i8:
Chris Lattnere6266112005-05-09 05:33:18 +00002143 case MVT::i1:
Chris Lattner282781c2005-01-09 18:52:44 +00002144 addFrameReference(BuildMI(BB, X86::MOV8rm, 4, Result), FrameIdx);
2145 break;
2146 }
2147
2148 // Reload the original control word now.
2149 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
2150 return Result;
2151 }
Chris Lattner88c8a232005-01-07 07:49:41 +00002152 case ISD::ADD:
Chris Lattner62b22422005-01-11 21:19:59 +00002153 Op0 = N.getOperand(0);
2154 Op1 = N.getOperand(1);
2155
Chris Lattner30607ec2005-01-25 20:03:11 +00002156 if (isFoldableLoad(Op0, Op1, true)) {
Chris Lattner62b22422005-01-11 21:19:59 +00002157 std::swap(Op0, Op1);
Chris Lattnera56d29d2005-01-17 06:26:58 +00002158 goto FoldAdd;
2159 }
Chris Lattner62b22422005-01-11 21:19:59 +00002160
Chris Lattner30607ec2005-01-25 20:03:11 +00002161 if (isFoldableLoad(Op1, Op0, true)) {
Chris Lattnera56d29d2005-01-17 06:26:58 +00002162 FoldAdd:
Chris Lattner62b22422005-01-11 21:19:59 +00002163 switch (N.getValueType()) {
2164 default: assert(0 && "Cannot add this type!");
2165 case MVT::i1:
2166 case MVT::i8: Opc = X86::ADD8rm; break;
2167 case MVT::i16: Opc = X86::ADD16rm; break;
2168 case MVT::i32: Opc = X86::ADD32rm; break;
Chris Lattner30607ec2005-01-25 20:03:11 +00002169 case MVT::f64:
2170 // For F64, handle promoted load operations (from F32) as well!
2171 Opc = Op1.getOpcode() == ISD::LOAD ? X86::FADD64m : X86::FADD32m;
2172 break;
Chris Lattner62b22422005-01-11 21:19:59 +00002173 }
2174 X86AddressMode AM;
Chris Lattner3676cd62005-01-13 05:53:16 +00002175 EmitFoldedLoad(Op1, AM);
2176 Tmp1 = SelectExpr(Op0);
Chris Lattner62b22422005-01-11 21:19:59 +00002177 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
2178 return Result;
2179 }
2180
Chris Lattner88c8a232005-01-07 07:49:41 +00002181 // See if we can codegen this as an LEA to fold operations together.
2182 if (N.getValueType() == MVT::i32) {
Chris Lattnerd7f93952005-01-18 02:25:52 +00002183 ExprMap.erase(N);
Chris Lattnera7acdda2005-01-18 01:06:26 +00002184 X86ISelAddressMode AM;
Chris Lattnerd7f93952005-01-18 02:25:52 +00002185 MatchAddress(N, AM);
2186 ExprMap[N] = Result;
2187
2188 // If this is not just an add, emit the LEA. For a simple add (like
2189 // reg+reg or reg+imm), we just emit an add. It might be a good idea to
2190 // leave this as LEA, then peephole it to 'ADD' after two address elim
2191 // happens.
2192 if (AM.Scale != 1 || AM.BaseType == X86ISelAddressMode::FrameIndexBase||
2193 AM.GV || (AM.Base.Reg.Val && AM.IndexReg.Val && AM.Disp)) {
2194 X86AddressMode XAM = SelectAddrExprs(AM);
2195 addFullAddress(BuildMI(BB, X86::LEA32r, 4, Result), XAM);
2196 return Result;
Chris Lattner88c8a232005-01-07 07:49:41 +00002197 }
2198 }
Chris Lattner0d1f82a2005-01-11 03:11:44 +00002199
Chris Lattner62b22422005-01-11 21:19:59 +00002200 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1)) {
Chris Lattner88c8a232005-01-07 07:49:41 +00002201 Opc = 0;
2202 if (CN->getValue() == 1) { // add X, 1 -> inc X
2203 switch (N.getValueType()) {
2204 default: assert(0 && "Cannot integer add this type!");
2205 case MVT::i8: Opc = X86::INC8r; break;
2206 case MVT::i16: Opc = X86::INC16r; break;
2207 case MVT::i32: Opc = X86::INC32r; break;
2208 }
2209 } else if (CN->isAllOnesValue()) { // add X, -1 -> dec X
2210 switch (N.getValueType()) {
2211 default: assert(0 && "Cannot integer add this type!");
2212 case MVT::i8: Opc = X86::DEC8r; break;
2213 case MVT::i16: Opc = X86::DEC16r; break;
2214 case MVT::i32: Opc = X86::DEC32r; break;
2215 }
2216 }
2217
2218 if (Opc) {
Chris Lattner62b22422005-01-11 21:19:59 +00002219 Tmp1 = SelectExpr(Op0);
Chris Lattner88c8a232005-01-07 07:49:41 +00002220 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
2221 return Result;
2222 }
2223
2224 switch (N.getValueType()) {
2225 default: assert(0 && "Cannot add this type!");
2226 case MVT::i8: Opc = X86::ADD8ri; break;
2227 case MVT::i16: Opc = X86::ADD16ri; break;
2228 case MVT::i32: Opc = X86::ADD32ri; break;
2229 }
2230 if (Opc) {
Chris Lattner62b22422005-01-11 21:19:59 +00002231 Tmp1 = SelectExpr(Op0);
Chris Lattner88c8a232005-01-07 07:49:41 +00002232 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
2233 return Result;
2234 }
2235 }
2236
Chris Lattner88c8a232005-01-07 07:49:41 +00002237 switch (N.getValueType()) {
2238 default: assert(0 && "Cannot add this type!");
2239 case MVT::i8: Opc = X86::ADD8rr; break;
2240 case MVT::i16: Opc = X86::ADD16rr; break;
2241 case MVT::i32: Opc = X86::ADD32rr; break;
Chris Lattnere44e6d12005-01-11 03:50:45 +00002242 case MVT::f64: Opc = X86::FpADD; break;
Chris Lattner88c8a232005-01-07 07:49:41 +00002243 }
Chris Lattner0d1f82a2005-01-11 03:11:44 +00002244
Chris Lattner62b22422005-01-11 21:19:59 +00002245 if (getRegPressure(Op0) > getRegPressure(Op1)) {
2246 Tmp1 = SelectExpr(Op0);
2247 Tmp2 = SelectExpr(Op1);
Chris Lattner0d1f82a2005-01-11 03:11:44 +00002248 } else {
Chris Lattner62b22422005-01-11 21:19:59 +00002249 Tmp2 = SelectExpr(Op1);
2250 Tmp1 = SelectExpr(Op0);
Chris Lattner0d1f82a2005-01-11 03:11:44 +00002251 }
2252
Chris Lattner88c8a232005-01-07 07:49:41 +00002253 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
2254 return Result;
Chris Lattner0e0b5992005-04-02 05:30:17 +00002255
2256 case ISD::FABS:
Chris Lattner0e0b5992005-04-02 05:30:17 +00002257 case ISD::FNEG:
Chris Lattnerdb68d392005-04-30 04:25:35 +00002258 case ISD::FSIN:
2259 case ISD::FCOS:
Chris Lattner014d2c42005-04-28 22:07:18 +00002260 case ISD::FSQRT:
2261 assert(N.getValueType()==MVT::f64 && "Illegal type for this operation");
Chris Lattner0e0b5992005-04-02 05:30:17 +00002262 Tmp1 = SelectExpr(Node->getOperand(0));
Chris Lattner014d2c42005-04-28 22:07:18 +00002263 switch (N.getOpcode()) {
2264 default: assert(0 && "Unreachable!");
2265 case ISD::FABS: BuildMI(BB, X86::FABS, 1, Result).addReg(Tmp1); break;
2266 case ISD::FNEG: BuildMI(BB, X86::FCHS, 1, Result).addReg(Tmp1); break;
2267 case ISD::FSQRT: BuildMI(BB, X86::FSQRT, 1, Result).addReg(Tmp1); break;
Chris Lattnerdb68d392005-04-30 04:25:35 +00002268 case ISD::FSIN: BuildMI(BB, X86::FSIN, 1, Result).addReg(Tmp1); break;
2269 case ISD::FCOS: BuildMI(BB, X86::FCOS, 1, Result).addReg(Tmp1); break;
Chris Lattner014d2c42005-04-28 22:07:18 +00002270 }
Chris Lattner0e0b5992005-04-02 05:30:17 +00002271 return Result;
2272
Chris Lattner4fbb4af2005-04-06 04:21:07 +00002273 case ISD::MULHU:
2274 switch (N.getValueType()) {
2275 default: assert(0 && "Unsupported VT!");
2276 case MVT::i8: Tmp2 = X86::MUL8r; break;
2277 case MVT::i16: Tmp2 = X86::MUL16r; break;
2278 case MVT::i32: Tmp2 = X86::MUL32r; break;
2279 }
2280 // FALL THROUGH
2281 case ISD::MULHS: {
2282 unsigned MovOpc, LowReg, HiReg;
2283 switch (N.getValueType()) {
2284 default: assert(0 && "Unsupported VT!");
Misha Brukmanc88330a2005-04-21 23:38:14 +00002285 case MVT::i8:
Chris Lattner4fbb4af2005-04-06 04:21:07 +00002286 MovOpc = X86::MOV8rr;
2287 LowReg = X86::AL;
2288 HiReg = X86::AH;
2289 Opc = X86::IMUL8r;
2290 break;
2291 case MVT::i16:
2292 MovOpc = X86::MOV16rr;
2293 LowReg = X86::AX;
2294 HiReg = X86::DX;
2295 Opc = X86::IMUL16r;
2296 break;
2297 case MVT::i32:
2298 MovOpc = X86::MOV32rr;
2299 LowReg = X86::EAX;
2300 HiReg = X86::EDX;
2301 Opc = X86::IMUL32r;
2302 break;
2303 }
2304 if (Node->getOpcode() != ISD::MULHS)
2305 Opc = Tmp2; // Get the MULHU opcode.
2306
2307 Op0 = Node->getOperand(0);
2308 Op1 = Node->getOperand(1);
2309 if (getRegPressure(Op0) > getRegPressure(Op1)) {
2310 Tmp1 = SelectExpr(Op0);
2311 Tmp2 = SelectExpr(Op1);
2312 } else {
2313 Tmp2 = SelectExpr(Op1);
2314 Tmp1 = SelectExpr(Op0);
2315 }
2316
2317 // FIXME: Implement folding of loads into the memory operands here!
2318 BuildMI(BB, MovOpc, 1, LowReg).addReg(Tmp1);
2319 BuildMI(BB, Opc, 1).addReg(Tmp2);
2320 BuildMI(BB, MovOpc, 1, Result).addReg(HiReg);
2321 return Result;
Misha Brukmanc88330a2005-04-21 23:38:14 +00002322 }
Chris Lattner4fbb4af2005-04-06 04:21:07 +00002323
Chris Lattner88c8a232005-01-07 07:49:41 +00002324 case ISD::SUB:
Chris Lattner62b22422005-01-11 21:19:59 +00002325 case ISD::MUL:
2326 case ISD::AND:
2327 case ISD::OR:
Chris Lattnerefe90202005-01-12 04:23:22 +00002328 case ISD::XOR: {
Chris Lattner62b22422005-01-11 21:19:59 +00002329 static const unsigned SUBTab[] = {
2330 X86::SUB8ri, X86::SUB16ri, X86::SUB32ri, 0, 0,
2331 X86::SUB8rm, X86::SUB16rm, X86::SUB32rm, X86::FSUB32m, X86::FSUB64m,
2332 X86::SUB8rr, X86::SUB16rr, X86::SUB32rr, X86::FpSUB , X86::FpSUB,
2333 };
2334 static const unsigned MULTab[] = {
2335 0, X86::IMUL16rri, X86::IMUL32rri, 0, 0,
2336 0, X86::IMUL16rm , X86::IMUL32rm, X86::FMUL32m, X86::FMUL64m,
2337 0, X86::IMUL16rr , X86::IMUL32rr, X86::FpMUL , X86::FpMUL,
2338 };
2339 static const unsigned ANDTab[] = {
2340 X86::AND8ri, X86::AND16ri, X86::AND32ri, 0, 0,
2341 X86::AND8rm, X86::AND16rm, X86::AND32rm, 0, 0,
Misha Brukmanc88330a2005-04-21 23:38:14 +00002342 X86::AND8rr, X86::AND16rr, X86::AND32rr, 0, 0,
Chris Lattner62b22422005-01-11 21:19:59 +00002343 };
2344 static const unsigned ORTab[] = {
2345 X86::OR8ri, X86::OR16ri, X86::OR32ri, 0, 0,
2346 X86::OR8rm, X86::OR16rm, X86::OR32rm, 0, 0,
2347 X86::OR8rr, X86::OR16rr, X86::OR32rr, 0, 0,
2348 };
2349 static const unsigned XORTab[] = {
2350 X86::XOR8ri, X86::XOR16ri, X86::XOR32ri, 0, 0,
2351 X86::XOR8rm, X86::XOR16rm, X86::XOR32rm, 0, 0,
2352 X86::XOR8rr, X86::XOR16rr, X86::XOR32rr, 0, 0,
2353 };
2354
2355 Op0 = Node->getOperand(0);
2356 Op1 = Node->getOperand(1);
2357
Chris Lattner29f58192005-01-19 07:37:26 +00002358 if (Node->getOpcode() == ISD::OR && Op0.hasOneUse() && Op1.hasOneUse())
2359 if (EmitOrOpOp(Op0, Op1, Result)) // Match SHLD, SHRD, and rotates.
Chris Lattner41fe2012005-01-19 06:18:43 +00002360 return Result;
2361
2362 if (Node->getOpcode() == ISD::SUB)
Chris Lattner88c8a232005-01-07 07:49:41 +00002363 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(0)))
2364 if (CN->isNullValue()) { // 0 - N -> neg N
2365 switch (N.getValueType()) {
2366 default: assert(0 && "Cannot sub this type!");
2367 case MVT::i1:
2368 case MVT::i8: Opc = X86::NEG8r; break;
2369 case MVT::i16: Opc = X86::NEG16r; break;
2370 case MVT::i32: Opc = X86::NEG32r; break;
2371 }
2372 Tmp1 = SelectExpr(N.getOperand(1));
2373 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
2374 return Result;
2375 }
2376
Chris Lattner62b22422005-01-11 21:19:59 +00002377 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1)) {
2378 if (CN->isAllOnesValue() && Node->getOpcode() == ISD::XOR) {
Chris Lattner0cd6b9a2005-01-17 00:23:16 +00002379 Opc = 0;
Chris Lattner9d7cf992005-01-11 04:31:30 +00002380 switch (N.getValueType()) {
2381 default: assert(0 && "Cannot add this type!");
Chris Lattner0cd6b9a2005-01-17 00:23:16 +00002382 case MVT::i1: break; // Not supported, don't invert upper bits!
Chris Lattner9d7cf992005-01-11 04:31:30 +00002383 case MVT::i8: Opc = X86::NOT8r; break;
2384 case MVT::i16: Opc = X86::NOT16r; break;
2385 case MVT::i32: Opc = X86::NOT32r; break;
2386 }
Chris Lattner0cd6b9a2005-01-17 00:23:16 +00002387 if (Opc) {
2388 Tmp1 = SelectExpr(Op0);
2389 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
2390 return Result;
2391 }
Chris Lattner9d7cf992005-01-11 04:31:30 +00002392 }
2393
Chris Lattnerb72ea1b2005-01-17 06:48:02 +00002394 // Fold common multiplies into LEA instructions.
2395 if (Node->getOpcode() == ISD::MUL && N.getValueType() == MVT::i32) {
2396 switch ((int)CN->getValue()) {
2397 default: break;
2398 case 3:
2399 case 5:
2400 case 9:
Chris Lattnerb72ea1b2005-01-17 06:48:02 +00002401 // Remove N from exprmap so SelectAddress doesn't get confused.
2402 ExprMap.erase(N);
Chris Lattnera7acdda2005-01-18 01:06:26 +00002403 X86AddressMode AM;
Chris Lattnerb72ea1b2005-01-17 06:48:02 +00002404 SelectAddress(N, AM);
2405 // Restore it to the map.
2406 ExprMap[N] = Result;
2407 addFullAddress(BuildMI(BB, X86::LEA32r, 4, Result), AM);
2408 return Result;
2409 }
2410 }
2411
Chris Lattner88c8a232005-01-07 07:49:41 +00002412 switch (N.getValueType()) {
Chris Lattner9d7cf992005-01-11 04:31:30 +00002413 default: assert(0 && "Cannot xor this type!");
Chris Lattner88c8a232005-01-07 07:49:41 +00002414 case MVT::i1:
Chris Lattner62b22422005-01-11 21:19:59 +00002415 case MVT::i8: Opc = 0; break;
2416 case MVT::i16: Opc = 1; break;
2417 case MVT::i32: Opc = 2; break;
Chris Lattner88c8a232005-01-07 07:49:41 +00002418 }
Chris Lattner62b22422005-01-11 21:19:59 +00002419 switch (Node->getOpcode()) {
2420 default: assert(0 && "Unreachable!");
2421 case ISD::SUB: Opc = SUBTab[Opc]; break;
2422 case ISD::MUL: Opc = MULTab[Opc]; break;
2423 case ISD::AND: Opc = ANDTab[Opc]; break;
2424 case ISD::OR: Opc = ORTab[Opc]; break;
2425 case ISD::XOR: Opc = XORTab[Opc]; break;
Chris Lattner88c8a232005-01-07 07:49:41 +00002426 }
Chris Lattner62b22422005-01-11 21:19:59 +00002427 if (Opc) { // Can't fold MUL:i8 R, imm
2428 Tmp1 = SelectExpr(Op0);
Chris Lattner88c8a232005-01-07 07:49:41 +00002429 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
2430 return Result;
2431 }
2432 }
Chris Lattner0d1f82a2005-01-11 03:11:44 +00002433
Chris Lattner30607ec2005-01-25 20:03:11 +00002434 if (isFoldableLoad(Op0, Op1, true))
Chris Lattner62b22422005-01-11 21:19:59 +00002435 if (Node->getOpcode() != ISD::SUB) {
2436 std::swap(Op0, Op1);
Chris Lattnera56d29d2005-01-17 06:26:58 +00002437 goto FoldOps;
Chris Lattner62b22422005-01-11 21:19:59 +00002438 } else {
Chris Lattner30607ec2005-01-25 20:03:11 +00002439 // For FP, emit 'reverse' subract, with a memory operand.
2440 if (N.getValueType() == MVT::f64) {
2441 if (Op0.getOpcode() == ISD::EXTLOAD)
2442 Opc = X86::FSUBR32m;
2443 else
2444 Opc = X86::FSUBR64m;
2445
Chris Lattner62b22422005-01-11 21:19:59 +00002446 X86AddressMode AM;
Chris Lattner3676cd62005-01-13 05:53:16 +00002447 EmitFoldedLoad(Op0, AM);
2448 Tmp1 = SelectExpr(Op1);
Chris Lattner62b22422005-01-11 21:19:59 +00002449 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
2450 return Result;
2451 }
2452 }
2453
Chris Lattner30607ec2005-01-25 20:03:11 +00002454 if (isFoldableLoad(Op1, Op0, true)) {
Chris Lattnera56d29d2005-01-17 06:26:58 +00002455 FoldOps:
Chris Lattner62b22422005-01-11 21:19:59 +00002456 switch (N.getValueType()) {
2457 default: assert(0 && "Cannot operate on this type!");
2458 case MVT::i1:
2459 case MVT::i8: Opc = 5; break;
2460 case MVT::i16: Opc = 6; break;
2461 case MVT::i32: Opc = 7; break;
Chris Lattner30607ec2005-01-25 20:03:11 +00002462 // For F64, handle promoted load operations (from F32) as well!
2463 case MVT::f64: Opc = Op1.getOpcode() == ISD::LOAD ? 9 : 8; break;
Chris Lattner62b22422005-01-11 21:19:59 +00002464 }
2465 switch (Node->getOpcode()) {
2466 default: assert(0 && "Unreachable!");
2467 case ISD::SUB: Opc = SUBTab[Opc]; break;
2468 case ISD::MUL: Opc = MULTab[Opc]; break;
2469 case ISD::AND: Opc = ANDTab[Opc]; break;
2470 case ISD::OR: Opc = ORTab[Opc]; break;
2471 case ISD::XOR: Opc = XORTab[Opc]; break;
2472 }
2473
2474 X86AddressMode AM;
Chris Lattner3676cd62005-01-13 05:53:16 +00002475 EmitFoldedLoad(Op1, AM);
2476 Tmp1 = SelectExpr(Op0);
Chris Lattner62b22422005-01-11 21:19:59 +00002477 if (Opc) {
2478 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
2479 } else {
2480 assert(Node->getOpcode() == ISD::MUL &&
2481 N.getValueType() == MVT::i8 && "Unexpected situation!");
2482 // Must use the MUL instruction, which forces use of AL.
2483 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(Tmp1);
2484 addFullAddress(BuildMI(BB, X86::MUL8m, 1), AM);
2485 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
2486 }
2487 return Result;
Chris Lattner0d1f82a2005-01-11 03:11:44 +00002488 }
Chris Lattner62b22422005-01-11 21:19:59 +00002489
2490 if (getRegPressure(Op0) > getRegPressure(Op1)) {
2491 Tmp1 = SelectExpr(Op0);
2492 Tmp2 = SelectExpr(Op1);
2493 } else {
2494 Tmp2 = SelectExpr(Op1);
2495 Tmp1 = SelectExpr(Op0);
2496 }
2497
Chris Lattner88c8a232005-01-07 07:49:41 +00002498 switch (N.getValueType()) {
2499 default: assert(0 && "Cannot add this type!");
Chris Lattner62b22422005-01-11 21:19:59 +00002500 case MVT::i1:
2501 case MVT::i8: Opc = 10; break;
2502 case MVT::i16: Opc = 11; break;
2503 case MVT::i32: Opc = 12; break;
2504 case MVT::f32: Opc = 13; break;
2505 case MVT::f64: Opc = 14; break;
2506 }
2507 switch (Node->getOpcode()) {
2508 default: assert(0 && "Unreachable!");
2509 case ISD::SUB: Opc = SUBTab[Opc]; break;
2510 case ISD::MUL: Opc = MULTab[Opc]; break;
2511 case ISD::AND: Opc = ANDTab[Opc]; break;
2512 case ISD::OR: Opc = ORTab[Opc]; break;
2513 case ISD::XOR: Opc = XORTab[Opc]; break;
2514 }
2515 if (Opc) {
2516 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
2517 } else {
2518 assert(Node->getOpcode() == ISD::MUL &&
2519 N.getValueType() == MVT::i8 && "Unexpected situation!");
Chris Lattner750d38b2005-01-10 20:55:48 +00002520 // Must use the MUL instruction, which forces use of AL.
2521 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(Tmp1);
2522 BuildMI(BB, X86::MUL8r, 1).addReg(Tmp2);
2523 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
Chris Lattner88c8a232005-01-07 07:49:41 +00002524 }
Chris Lattner88c8a232005-01-07 07:49:41 +00002525 return Result;
Chris Lattnerefe90202005-01-12 04:23:22 +00002526 }
Chris Lattner2a631fa2005-01-20 18:53:00 +00002527 case ISD::ADD_PARTS:
2528 case ISD::SUB_PARTS: {
2529 assert(N.getNumOperands() == 4 && N.getValueType() == MVT::i32 &&
2530 "Not an i64 add/sub!");
2531 // Emit all of the operands.
2532 std::vector<unsigned> InVals;
2533 for (unsigned i = 0, e = N.getNumOperands(); i != e; ++i)
2534 InVals.push_back(SelectExpr(N.getOperand(i)));
2535 if (N.getOpcode() == ISD::ADD_PARTS) {
2536 BuildMI(BB, X86::ADD32rr, 2, Result).addReg(InVals[0]).addReg(InVals[2]);
2537 BuildMI(BB, X86::ADC32rr,2,Result+1).addReg(InVals[1]).addReg(InVals[3]);
2538 } else {
2539 BuildMI(BB, X86::SUB32rr, 2, Result).addReg(InVals[0]).addReg(InVals[2]);
2540 BuildMI(BB, X86::SBB32rr, 2,Result+1).addReg(InVals[1]).addReg(InVals[3]);
2541 }
2542 return Result+N.ResNo;
2543 }
2544
Chris Lattnera31d4c72005-04-02 04:01:14 +00002545 case ISD::SHL_PARTS:
2546 case ISD::SRA_PARTS:
2547 case ISD::SRL_PARTS: {
2548 assert(N.getNumOperands() == 3 && N.getValueType() == MVT::i32 &&
2549 "Not an i64 shift!");
2550 unsigned ShiftOpLo = SelectExpr(N.getOperand(0));
2551 unsigned ShiftOpHi = SelectExpr(N.getOperand(1));
2552 unsigned TmpReg = MakeReg(MVT::i32);
2553 if (N.getOpcode() == ISD::SRA_PARTS) {
2554 // If this is a SHR of a Long, then we need to do funny sign extension
2555 // stuff. TmpReg gets the value to use as the high-part if we are
2556 // shifting more than 32 bits.
2557 BuildMI(BB, X86::SAR32ri, 2, TmpReg).addReg(ShiftOpHi).addImm(31);
2558 } else {
2559 // Other shifts use a fixed zero value if the shift is more than 32 bits.
2560 BuildMI(BB, X86::MOV32ri, 1, TmpReg).addImm(0);
2561 }
2562
2563 // Initialize CL with the shift amount.
2564 unsigned ShiftAmountReg = SelectExpr(N.getOperand(2));
2565 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShiftAmountReg);
2566
2567 unsigned TmpReg2 = MakeReg(MVT::i32);
2568 unsigned TmpReg3 = MakeReg(MVT::i32);
2569 if (N.getOpcode() == ISD::SHL_PARTS) {
2570 // TmpReg2 = shld inHi, inLo
2571 BuildMI(BB, X86::SHLD32rrCL, 2,TmpReg2).addReg(ShiftOpHi)
2572 .addReg(ShiftOpLo);
2573 // TmpReg3 = shl inLo, CL
2574 BuildMI(BB, X86::SHL32rCL, 1, TmpReg3).addReg(ShiftOpLo);
Misha Brukmanc88330a2005-04-21 23:38:14 +00002575
Chris Lattnera31d4c72005-04-02 04:01:14 +00002576 // Set the flags to indicate whether the shift was by more than 32 bits.
2577 BuildMI(BB, X86::TEST8ri, 2).addReg(X86::CL).addImm(32);
Misha Brukmanc88330a2005-04-21 23:38:14 +00002578
Chris Lattnera31d4c72005-04-02 04:01:14 +00002579 // DestHi = (>32) ? TmpReg3 : TmpReg2;
Misha Brukmanc88330a2005-04-21 23:38:14 +00002580 BuildMI(BB, X86::CMOVNE32rr, 2,
Chris Lattnera31d4c72005-04-02 04:01:14 +00002581 Result+1).addReg(TmpReg2).addReg(TmpReg3);
2582 // DestLo = (>32) ? TmpReg : TmpReg3;
2583 BuildMI(BB, X86::CMOVNE32rr, 2,
2584 Result).addReg(TmpReg3).addReg(TmpReg);
2585 } else {
2586 // TmpReg2 = shrd inLo, inHi
2587 BuildMI(BB, X86::SHRD32rrCL,2,TmpReg2).addReg(ShiftOpLo)
2588 .addReg(ShiftOpHi);
2589 // TmpReg3 = s[ah]r inHi, CL
Misha Brukmanc88330a2005-04-21 23:38:14 +00002590 BuildMI(BB, N.getOpcode() == ISD::SRA_PARTS ? X86::SAR32rCL
Chris Lattnera31d4c72005-04-02 04:01:14 +00002591 : X86::SHR32rCL, 1, TmpReg3)
2592 .addReg(ShiftOpHi);
Misha Brukmanc88330a2005-04-21 23:38:14 +00002593
Chris Lattnera31d4c72005-04-02 04:01:14 +00002594 // Set the flags to indicate whether the shift was by more than 32 bits.
2595 BuildMI(BB, X86::TEST8ri, 2).addReg(X86::CL).addImm(32);
Misha Brukmanc88330a2005-04-21 23:38:14 +00002596
Chris Lattnera31d4c72005-04-02 04:01:14 +00002597 // DestLo = (>32) ? TmpReg3 : TmpReg2;
Misha Brukmanc88330a2005-04-21 23:38:14 +00002598 BuildMI(BB, X86::CMOVNE32rr, 2,
Chris Lattnera31d4c72005-04-02 04:01:14 +00002599 Result).addReg(TmpReg2).addReg(TmpReg3);
Misha Brukmanc88330a2005-04-21 23:38:14 +00002600
Chris Lattnera31d4c72005-04-02 04:01:14 +00002601 // DestHi = (>32) ? TmpReg : TmpReg3;
Misha Brukmanc88330a2005-04-21 23:38:14 +00002602 BuildMI(BB, X86::CMOVNE32rr, 2,
Chris Lattnera31d4c72005-04-02 04:01:14 +00002603 Result+1).addReg(TmpReg3).addReg(TmpReg);
2604 }
2605 return Result+N.ResNo;
2606 }
2607
Chris Lattner88c8a232005-01-07 07:49:41 +00002608 case ISD::SELECT:
Chris Lattnerb14a63a2005-01-16 07:34:08 +00002609 if (getRegPressure(N.getOperand(1)) > getRegPressure(N.getOperand(2))) {
2610 Tmp2 = SelectExpr(N.getOperand(1));
2611 Tmp3 = SelectExpr(N.getOperand(2));
Chris Lattner88c8a232005-01-07 07:49:41 +00002612 } else {
Chris Lattnerb14a63a2005-01-16 07:34:08 +00002613 Tmp3 = SelectExpr(N.getOperand(2));
2614 Tmp2 = SelectExpr(N.getOperand(1));
Chris Lattner88c8a232005-01-07 07:49:41 +00002615 }
Chris Lattnerb14a63a2005-01-16 07:34:08 +00002616 EmitSelectCC(N.getOperand(0), N.getValueType(), Tmp2, Tmp3, Result);
2617 return Result;
Chris Lattner88c8a232005-01-07 07:49:41 +00002618
2619 case ISD::SDIV:
2620 case ISD::UDIV:
2621 case ISD::SREM:
2622 case ISD::UREM: {
Chris Lattnerb14a63a2005-01-16 07:34:08 +00002623 assert((N.getOpcode() != ISD::SREM || MVT::isInteger(N.getValueType())) &&
2624 "We don't support this operator!");
2625
Chris Lattner60c23bd2005-04-13 03:29:53 +00002626 if (N.getOpcode() == ISD::SDIV) {
Chris Lattner1b206152005-01-25 20:35:10 +00002627 // We can fold loads into FpDIVs, but not really into any others.
2628 if (N.getValueType() == MVT::f64) {
2629 // Check for reversed and unreversed DIV.
2630 if (isFoldableLoad(N.getOperand(0), N.getOperand(1), true)) {
2631 if (N.getOperand(0).getOpcode() == ISD::EXTLOAD)
2632 Opc = X86::FDIVR32m;
2633 else
2634 Opc = X86::FDIVR64m;
2635 X86AddressMode AM;
2636 EmitFoldedLoad(N.getOperand(0), AM);
2637 Tmp1 = SelectExpr(N.getOperand(1));
2638 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
2639 return Result;
2640 } else if (isFoldableLoad(N.getOperand(1), N.getOperand(0), true) &&
2641 N.getOperand(1).getOpcode() == ISD::LOAD) {
2642 if (N.getOperand(1).getOpcode() == ISD::EXTLOAD)
2643 Opc = X86::FDIV32m;
2644 else
2645 Opc = X86::FDIV64m;
2646 X86AddressMode AM;
2647 EmitFoldedLoad(N.getOperand(1), AM);
2648 Tmp1 = SelectExpr(N.getOperand(0));
2649 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
2650 return Result;
2651 }
2652 }
2653
Chris Lattner88c8a232005-01-07 07:49:41 +00002654 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
2655 // FIXME: These special cases should be handled by the lowering impl!
2656 unsigned RHS = CN->getValue();
2657 bool isNeg = false;
2658 if ((int)RHS < 0) {
2659 isNeg = true;
2660 RHS = -RHS;
2661 }
2662 if (RHS && (RHS & (RHS-1)) == 0) { // Signed division by power of 2?
2663 unsigned Log = log2(RHS);
2664 unsigned TmpReg = MakeReg(N.getValueType());
2665 unsigned SAROpc, SHROpc, ADDOpc, NEGOpc;
2666 switch (N.getValueType()) {
2667 default: assert("Unknown type to signed divide!");
2668 case MVT::i8:
2669 SAROpc = X86::SAR8ri;
2670 SHROpc = X86::SHR8ri;
2671 ADDOpc = X86::ADD8rr;
2672 NEGOpc = X86::NEG8r;
2673 break;
2674 case MVT::i16:
2675 SAROpc = X86::SAR16ri;
2676 SHROpc = X86::SHR16ri;
2677 ADDOpc = X86::ADD16rr;
2678 NEGOpc = X86::NEG16r;
2679 break;
2680 case MVT::i32:
2681 SAROpc = X86::SAR32ri;
2682 SHROpc = X86::SHR32ri;
2683 ADDOpc = X86::ADD32rr;
2684 NEGOpc = X86::NEG32r;
2685 break;
2686 }
Chris Lattner0d1f82a2005-01-11 03:11:44 +00002687 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner88c8a232005-01-07 07:49:41 +00002688 BuildMI(BB, SAROpc, 2, TmpReg).addReg(Tmp1).addImm(Log-1);
2689 unsigned TmpReg2 = MakeReg(N.getValueType());
2690 BuildMI(BB, SHROpc, 2, TmpReg2).addReg(TmpReg).addImm(32-Log);
2691 unsigned TmpReg3 = MakeReg(N.getValueType());
2692 BuildMI(BB, ADDOpc, 2, TmpReg3).addReg(Tmp1).addReg(TmpReg2);
Misha Brukmanc88330a2005-04-21 23:38:14 +00002693
Chris Lattner88c8a232005-01-07 07:49:41 +00002694 unsigned TmpReg4 = isNeg ? MakeReg(N.getValueType()) : Result;
2695 BuildMI(BB, SAROpc, 2, TmpReg4).addReg(TmpReg3).addImm(Log);
2696 if (isNeg)
2697 BuildMI(BB, NEGOpc, 1, Result).addReg(TmpReg4);
2698 return Result;
2699 }
2700 }
Chris Lattner60c23bd2005-04-13 03:29:53 +00002701 }
Chris Lattner88c8a232005-01-07 07:49:41 +00002702
Chris Lattner0d1f82a2005-01-11 03:11:44 +00002703 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2704 Tmp1 = SelectExpr(N.getOperand(0));
2705 Tmp2 = SelectExpr(N.getOperand(1));
2706 } else {
2707 Tmp2 = SelectExpr(N.getOperand(1));
2708 Tmp1 = SelectExpr(N.getOperand(0));
2709 }
Chris Lattner88c8a232005-01-07 07:49:41 +00002710
2711 bool isSigned = N.getOpcode() == ISD::SDIV || N.getOpcode() == ISD::SREM;
2712 bool isDiv = N.getOpcode() == ISD::SDIV || N.getOpcode() == ISD::UDIV;
2713 unsigned LoReg, HiReg, DivOpcode, MovOpcode, ClrOpcode, SExtOpcode;
2714 switch (N.getValueType()) {
2715 default: assert(0 && "Cannot sdiv this type!");
2716 case MVT::i8:
2717 DivOpcode = isSigned ? X86::IDIV8r : X86::DIV8r;
2718 LoReg = X86::AL;
2719 HiReg = X86::AH;
2720 MovOpcode = X86::MOV8rr;
2721 ClrOpcode = X86::MOV8ri;
2722 SExtOpcode = X86::CBW;
2723 break;
2724 case MVT::i16:
2725 DivOpcode = isSigned ? X86::IDIV16r : X86::DIV16r;
2726 LoReg = X86::AX;
2727 HiReg = X86::DX;
2728 MovOpcode = X86::MOV16rr;
2729 ClrOpcode = X86::MOV16ri;
2730 SExtOpcode = X86::CWD;
2731 break;
2732 case MVT::i32:
2733 DivOpcode = isSigned ? X86::IDIV32r : X86::DIV32r;
Chris Lattner3278ce82005-01-12 03:16:09 +00002734 LoReg = X86::EAX;
Chris Lattner88c8a232005-01-07 07:49:41 +00002735 HiReg = X86::EDX;
2736 MovOpcode = X86::MOV32rr;
2737 ClrOpcode = X86::MOV32ri;
2738 SExtOpcode = X86::CDQ;
2739 break;
Chris Lattner88c8a232005-01-07 07:49:41 +00002740 case MVT::f64:
Chris Lattnerb14a63a2005-01-16 07:34:08 +00002741 BuildMI(BB, X86::FpDIV, 2, Result).addReg(Tmp1).addReg(Tmp2);
Chris Lattner88c8a232005-01-07 07:49:41 +00002742 return Result;
2743 }
2744
2745 // Set up the low part.
2746 BuildMI(BB, MovOpcode, 1, LoReg).addReg(Tmp1);
2747
2748 if (isSigned) {
2749 // Sign extend the low part into the high part.
2750 BuildMI(BB, SExtOpcode, 0);
2751 } else {
2752 // Zero out the high part, effectively zero extending the input.
2753 BuildMI(BB, ClrOpcode, 1, HiReg).addImm(0);
2754 }
2755
2756 // Emit the DIV/IDIV instruction.
Misha Brukmanc88330a2005-04-21 23:38:14 +00002757 BuildMI(BB, DivOpcode, 1).addReg(Tmp2);
Chris Lattner88c8a232005-01-07 07:49:41 +00002758
2759 // Get the result of the divide or rem.
2760 BuildMI(BB, MovOpcode, 1, Result).addReg(isDiv ? LoReg : HiReg);
2761 return Result;
2762 }
2763
2764 case ISD::SHL:
Chris Lattner88c8a232005-01-07 07:49:41 +00002765 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Chris Lattner62b22422005-01-11 21:19:59 +00002766 if (CN->getValue() == 1) { // X = SHL Y, 1 -> X = ADD Y, Y
2767 switch (N.getValueType()) {
2768 default: assert(0 && "Cannot shift this type!");
2769 case MVT::i8: Opc = X86::ADD8rr; break;
2770 case MVT::i16: Opc = X86::ADD16rr; break;
2771 case MVT::i32: Opc = X86::ADD32rr; break;
2772 }
2773 Tmp1 = SelectExpr(N.getOperand(0));
2774 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp1);
2775 return Result;
2776 }
Misha Brukmanc88330a2005-04-21 23:38:14 +00002777
Chris Lattner88c8a232005-01-07 07:49:41 +00002778 switch (N.getValueType()) {
2779 default: assert(0 && "Cannot shift this type!");
2780 case MVT::i8: Opc = X86::SHL8ri; break;
2781 case MVT::i16: Opc = X86::SHL16ri; break;
2782 case MVT::i32: Opc = X86::SHL32ri; break;
2783 }
Chris Lattner0d1f82a2005-01-11 03:11:44 +00002784 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner88c8a232005-01-07 07:49:41 +00002785 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
2786 return Result;
2787 }
Chris Lattner0d1f82a2005-01-11 03:11:44 +00002788
2789 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2790 Tmp1 = SelectExpr(N.getOperand(0));
2791 Tmp2 = SelectExpr(N.getOperand(1));
2792 } else {
2793 Tmp2 = SelectExpr(N.getOperand(1));
2794 Tmp1 = SelectExpr(N.getOperand(0));
2795 }
2796
Chris Lattner88c8a232005-01-07 07:49:41 +00002797 switch (N.getValueType()) {
2798 default: assert(0 && "Cannot shift this type!");
2799 case MVT::i8 : Opc = X86::SHL8rCL; break;
2800 case MVT::i16: Opc = X86::SHL16rCL; break;
2801 case MVT::i32: Opc = X86::SHL32rCL; break;
2802 }
2803 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
2804 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
2805 return Result;
2806 case ISD::SRL:
Chris Lattner88c8a232005-01-07 07:49:41 +00002807 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
2808 switch (N.getValueType()) {
2809 default: assert(0 && "Cannot shift this type!");
2810 case MVT::i8: Opc = X86::SHR8ri; break;
2811 case MVT::i16: Opc = X86::SHR16ri; break;
2812 case MVT::i32: Opc = X86::SHR32ri; break;
2813 }
Chris Lattner0d1f82a2005-01-11 03:11:44 +00002814 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner88c8a232005-01-07 07:49:41 +00002815 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
2816 return Result;
2817 }
Chris Lattner0d1f82a2005-01-11 03:11:44 +00002818
2819 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2820 Tmp1 = SelectExpr(N.getOperand(0));
2821 Tmp2 = SelectExpr(N.getOperand(1));
2822 } else {
2823 Tmp2 = SelectExpr(N.getOperand(1));
2824 Tmp1 = SelectExpr(N.getOperand(0));
2825 }
2826
Chris Lattner88c8a232005-01-07 07:49:41 +00002827 switch (N.getValueType()) {
2828 default: assert(0 && "Cannot shift this type!");
2829 case MVT::i8 : Opc = X86::SHR8rCL; break;
2830 case MVT::i16: Opc = X86::SHR16rCL; break;
2831 case MVT::i32: Opc = X86::SHR32rCL; break;
2832 }
2833 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
2834 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
2835 return Result;
2836 case ISD::SRA:
Chris Lattner88c8a232005-01-07 07:49:41 +00002837 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
2838 switch (N.getValueType()) {
2839 default: assert(0 && "Cannot shift this type!");
2840 case MVT::i8: Opc = X86::SAR8ri; break;
2841 case MVT::i16: Opc = X86::SAR16ri; break;
2842 case MVT::i32: Opc = X86::SAR32ri; break;
2843 }
Chris Lattner0d1f82a2005-01-11 03:11:44 +00002844 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner88c8a232005-01-07 07:49:41 +00002845 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
2846 return Result;
2847 }
Chris Lattner0d1f82a2005-01-11 03:11:44 +00002848
2849 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2850 Tmp1 = SelectExpr(N.getOperand(0));
2851 Tmp2 = SelectExpr(N.getOperand(1));
2852 } else {
2853 Tmp2 = SelectExpr(N.getOperand(1));
2854 Tmp1 = SelectExpr(N.getOperand(0));
2855 }
2856
Chris Lattner88c8a232005-01-07 07:49:41 +00002857 switch (N.getValueType()) {
2858 default: assert(0 && "Cannot shift this type!");
2859 case MVT::i8 : Opc = X86::SAR8rCL; break;
2860 case MVT::i16: Opc = X86::SAR16rCL; break;
2861 case MVT::i32: Opc = X86::SAR32rCL; break;
2862 }
2863 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
2864 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
2865 return Result;
2866
2867 case ISD::SETCC:
Chris Lattner3be6cd52005-01-17 01:34:14 +00002868 EmitCMP(N.getOperand(0), N.getOperand(1), Node->hasOneUse());
Chris Lattner88c8a232005-01-07 07:49:41 +00002869 EmitSetCC(BB, Result, cast<SetCCSDNode>(N)->getCondition(),
2870 MVT::isFloatingPoint(N.getOperand(1).getValueType()));
2871 return Result;
Chris Lattnere18a4c42005-01-15 05:22:24 +00002872 case ISD::LOAD:
Chris Lattner88c8a232005-01-07 07:49:41 +00002873 // Make sure we generate both values.
Chris Lattner78d30282005-01-18 03:51:59 +00002874 if (Result != 1) { // Generate the token
2875 if (!ExprMap.insert(std::make_pair(N.getValue(1), 1)).second)
2876 assert(0 && "Load already emitted!?");
2877 } else
Chris Lattner88c8a232005-01-07 07:49:41 +00002878 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
2879
Chris Lattnerb52e0412005-01-08 19:28:19 +00002880 switch (Node->getValueType(0)) {
Chris Lattner88c8a232005-01-07 07:49:41 +00002881 default: assert(0 && "Cannot load this type!");
2882 case MVT::i1:
2883 case MVT::i8: Opc = X86::MOV8rm; break;
2884 case MVT::i16: Opc = X86::MOV16rm; break;
2885 case MVT::i32: Opc = X86::MOV32rm; break;
Chris Lattner88c8a232005-01-07 07:49:41 +00002886 case MVT::f64: Opc = X86::FLD64m; ContainsFPCode = true; break;
2887 }
Chris Lattner0d1f82a2005-01-11 03:11:44 +00002888
Chris Lattner88c8a232005-01-07 07:49:41 +00002889 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N.getOperand(1))){
Chris Lattner0d1f82a2005-01-11 03:11:44 +00002890 Select(N.getOperand(0));
Chris Lattner88c8a232005-01-07 07:49:41 +00002891 addConstantPoolReference(BuildMI(BB, Opc, 4, Result), CP->getIndex());
2892 } else {
2893 X86AddressMode AM;
Chris Lattner3676cd62005-01-13 05:53:16 +00002894
2895 SDOperand Chain = N.getOperand(0);
2896 SDOperand Address = N.getOperand(1);
2897 if (getRegPressure(Chain) > getRegPressure(Address)) {
2898 Select(Chain);
2899 SelectAddress(Address, AM);
2900 } else {
2901 SelectAddress(Address, AM);
2902 Select(Chain);
2903 }
2904
Chris Lattner88c8a232005-01-07 07:49:41 +00002905 addFullAddress(BuildMI(BB, Opc, 4, Result), AM);
2906 }
2907 return Result;
Chris Lattnere18a4c42005-01-15 05:22:24 +00002908
2909 case ISD::EXTLOAD: // Arbitrarily codegen extloads as MOVZX*
2910 case ISD::ZEXTLOAD: {
2911 // Make sure we generate both values.
2912 if (Result != 1)
2913 ExprMap[N.getValue(1)] = 1; // Generate the token
2914 else
2915 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
2916
Chris Lattnerb14a63a2005-01-16 07:34:08 +00002917 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N.getOperand(1)))
2918 if (Node->getValueType(0) == MVT::f64) {
2919 assert(cast<MVTSDNode>(Node)->getExtraValueType() == MVT::f32 &&
2920 "Bad EXTLOAD!");
2921 addConstantPoolReference(BuildMI(BB, X86::FLD32m, 4, Result),
2922 CP->getIndex());
2923 return Result;
2924 }
2925
Chris Lattnere18a4c42005-01-15 05:22:24 +00002926 X86AddressMode AM;
2927 if (getRegPressure(Node->getOperand(0)) >
2928 getRegPressure(Node->getOperand(1))) {
2929 Select(Node->getOperand(0)); // chain
2930 SelectAddress(Node->getOperand(1), AM);
2931 } else {
2932 SelectAddress(Node->getOperand(1), AM);
2933 Select(Node->getOperand(0)); // chain
2934 }
2935
2936 switch (Node->getValueType(0)) {
2937 default: assert(0 && "Unknown type to sign extend to.");
2938 case MVT::f64:
2939 assert(cast<MVTSDNode>(Node)->getExtraValueType() == MVT::f32 &&
2940 "Bad EXTLOAD!");
2941 addFullAddress(BuildMI(BB, X86::FLD32m, 5, Result), AM);
2942 break;
2943 case MVT::i32:
2944 switch (cast<MVTSDNode>(Node)->getExtraValueType()) {
2945 default:
2946 assert(0 && "Bad zero extend!");
2947 case MVT::i1:
2948 case MVT::i8:
2949 addFullAddress(BuildMI(BB, X86::MOVZX32rm8, 5, Result), AM);
2950 break;
2951 case MVT::i16:
2952 addFullAddress(BuildMI(BB, X86::MOVZX32rm16, 5, Result), AM);
2953 break;
2954 }
2955 break;
2956 case MVT::i16:
2957 assert(cast<MVTSDNode>(Node)->getExtraValueType() <= MVT::i8 &&
2958 "Bad zero extend!");
2959 addFullAddress(BuildMI(BB, X86::MOVSX16rm8, 5, Result), AM);
2960 break;
2961 case MVT::i8:
2962 assert(cast<MVTSDNode>(Node)->getExtraValueType() == MVT::i1 &&
2963 "Bad zero extend!");
2964 addFullAddress(BuildMI(BB, X86::MOV8rm, 5, Result), AM);
2965 break;
2966 }
2967 return Result;
Chris Lattner88c8a232005-01-07 07:49:41 +00002968 }
Chris Lattnere18a4c42005-01-15 05:22:24 +00002969 case ISD::SEXTLOAD: {
2970 // Make sure we generate both values.
2971 if (Result != 1)
2972 ExprMap[N.getValue(1)] = 1; // Generate the token
2973 else
2974 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
2975
2976 X86AddressMode AM;
2977 if (getRegPressure(Node->getOperand(0)) >
2978 getRegPressure(Node->getOperand(1))) {
2979 Select(Node->getOperand(0)); // chain
2980 SelectAddress(Node->getOperand(1), AM);
2981 } else {
2982 SelectAddress(Node->getOperand(1), AM);
2983 Select(Node->getOperand(0)); // chain
2984 }
2985
2986 switch (Node->getValueType(0)) {
2987 case MVT::i8: assert(0 && "Cannot sign extend from bool!");
2988 default: assert(0 && "Unknown type to sign extend to.");
2989 case MVT::i32:
2990 switch (cast<MVTSDNode>(Node)->getExtraValueType()) {
2991 default:
2992 case MVT::i1: assert(0 && "Cannot sign extend from bool!");
2993 case MVT::i8:
2994 addFullAddress(BuildMI(BB, X86::MOVSX32rm8, 5, Result), AM);
2995 break;
2996 case MVT::i16:
2997 addFullAddress(BuildMI(BB, X86::MOVSX32rm16, 5, Result), AM);
2998 break;
2999 }
3000 break;
3001 case MVT::i16:
3002 assert(cast<MVTSDNode>(Node)->getExtraValueType() == MVT::i8 &&
3003 "Cannot sign extend from bool!");
3004 addFullAddress(BuildMI(BB, X86::MOVSX16rm8, 5, Result), AM);
3005 break;
3006 }
3007 return Result;
3008 }
3009
Chris Lattner88c8a232005-01-07 07:49:41 +00003010 case ISD::DYNAMIC_STACKALLOC:
Chris Lattner88c8a232005-01-07 07:49:41 +00003011 // Generate both result values.
3012 if (Result != 1)
3013 ExprMap[N.getValue(1)] = 1; // Generate the token
3014 else
3015 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
3016
3017 // FIXME: We are currently ignoring the requested alignment for handling
3018 // greater than the stack alignment. This will need to be revisited at some
3019 // point. Align = N.getOperand(2);
3020
3021 if (!isa<ConstantSDNode>(N.getOperand(2)) ||
3022 cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) {
3023 std::cerr << "Cannot allocate stack object with greater alignment than"
3024 << " the stack alignment yet!";
3025 abort();
3026 }
Misha Brukmanc88330a2005-04-21 23:38:14 +00003027
Chris Lattner88c8a232005-01-07 07:49:41 +00003028 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Chris Lattner0d1f82a2005-01-11 03:11:44 +00003029 Select(N.getOperand(0));
Chris Lattner88c8a232005-01-07 07:49:41 +00003030 BuildMI(BB, X86::SUB32ri, 2, X86::ESP).addReg(X86::ESP)
3031 .addImm(CN->getValue());
3032 } else {
Chris Lattner0d1f82a2005-01-11 03:11:44 +00003033 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
3034 Select(N.getOperand(0));
3035 Tmp1 = SelectExpr(N.getOperand(1));
3036 } else {
3037 Tmp1 = SelectExpr(N.getOperand(1));
3038 Select(N.getOperand(0));
3039 }
Chris Lattner88c8a232005-01-07 07:49:41 +00003040
3041 // Subtract size from stack pointer, thereby allocating some space.
3042 BuildMI(BB, X86::SUB32rr, 2, X86::ESP).addReg(X86::ESP).addReg(Tmp1);
3043 }
3044
3045 // Put a pointer to the space into the result register, by copying the stack
3046 // pointer.
3047 BuildMI(BB, X86::MOV32rr, 1, Result).addReg(X86::ESP);
3048 return Result;
3049
Chris Lattner7ce7a8f2005-05-12 23:06:28 +00003050 case ISD::CALL: {
Chris Lattnerb52e0412005-01-08 19:28:19 +00003051 // The chain for this call is now lowered.
Chris Lattner78d30282005-01-18 03:51:59 +00003052 ExprMap.insert(std::make_pair(N.getValue(Node->getNumValues()-1), 1));
Chris Lattnerb52e0412005-01-08 19:28:19 +00003053
Chris Lattner7ce7a8f2005-05-12 23:06:28 +00003054 bool isDirect = isa<GlobalAddressSDNode>(N.getOperand(1)) ||
3055 isa<ExternalSymbolSDNode>(N.getOperand(1));
3056 unsigned Callee = 0;
3057 if (isDirect) {
3058 Select(N.getOperand(0));
3059 } else {
3060 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
3061 Select(N.getOperand(0));
3062 Callee = SelectExpr(N.getOperand(1));
3063 } else {
3064 Callee = SelectExpr(N.getOperand(1));
3065 Select(N.getOperand(0));
3066 }
3067 }
3068
3069 // If this call has values to pass in registers, do so now.
3070 if (Node->getNumOperands() > 2) {
3071 // The first value is passed in (a part of) EAX, the second in EDX.
3072 unsigned RegOp1 = SelectExpr(N.getOperand(2));
3073 unsigned RegOp2 =
3074 Node->getNumOperands() > 3 ? SelectExpr(N.getOperand(3)) : 0;
3075
3076 switch (N.getOperand(2).getValueType()) {
3077 default: assert(0 && "Bad thing to pass in regs");
3078 case MVT::i1:
3079 case MVT::i8: BuildMI(BB, X86::MOV8rr , 1,X86::AL).addReg(RegOp1); break;
3080 case MVT::i16: BuildMI(BB, X86::MOV16rr, 1,X86::AX).addReg(RegOp1); break;
3081 case MVT::i32: BuildMI(BB, X86::MOV32rr, 1,X86::EAX).addReg(RegOp1);break;
3082 }
3083 if (RegOp2)
3084 switch (N.getOperand(3).getValueType()) {
3085 default: assert(0 && "Bad thing to pass in regs");
3086 case MVT::i1:
3087 case MVT::i8:
3088 BuildMI(BB, X86::MOV8rr , 1, X86::DL).addReg(RegOp2);
3089 break;
3090 case MVT::i16:
3091 BuildMI(BB, X86::MOV16rr, 1, X86::DX).addReg(RegOp2);
3092 break;
3093 case MVT::i32:
3094 BuildMI(BB, X86::MOV32rr, 1, X86::EDX).addReg(RegOp2);
3095 break;
3096 }
3097 }
3098
Chris Lattner88c8a232005-01-07 07:49:41 +00003099 if (GlobalAddressSDNode *GASD =
3100 dyn_cast<GlobalAddressSDNode>(N.getOperand(1))) {
3101 BuildMI(BB, X86::CALLpcrel32, 1).addGlobalAddress(GASD->getGlobal(),true);
3102 } else if (ExternalSymbolSDNode *ESSDN =
3103 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1))) {
3104 BuildMI(BB, X86::CALLpcrel32,
3105 1).addExternalSymbol(ESSDN->getSymbol(), true);
3106 } else {
Chris Lattner0d1f82a2005-01-11 03:11:44 +00003107 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
3108 Select(N.getOperand(0));
3109 Tmp1 = SelectExpr(N.getOperand(1));
3110 } else {
3111 Tmp1 = SelectExpr(N.getOperand(1));
3112 Select(N.getOperand(0));
3113 }
3114
Chris Lattner88c8a232005-01-07 07:49:41 +00003115 BuildMI(BB, X86::CALL32r, 1).addReg(Tmp1);
3116 }
Chris Lattnerb52e0412005-01-08 19:28:19 +00003117 switch (Node->getValueType(0)) {
Chris Lattner88c8a232005-01-07 07:49:41 +00003118 default: assert(0 && "Unknown value type for call result!");
3119 case MVT::Other: return 1;
3120 case MVT::i1:
3121 case MVT::i8:
3122 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
3123 break;
3124 case MVT::i16:
3125 BuildMI(BB, X86::MOV16rr, 1, Result).addReg(X86::AX);
3126 break;
3127 case MVT::i32:
3128 BuildMI(BB, X86::MOV32rr, 1, Result).addReg(X86::EAX);
Chris Lattnerb52e0412005-01-08 19:28:19 +00003129 if (Node->getValueType(1) == MVT::i32)
Chris Lattner88c8a232005-01-07 07:49:41 +00003130 BuildMI(BB, X86::MOV32rr, 1, Result+1).addReg(X86::EDX);
3131 break;
Chris Lattner88c8a232005-01-07 07:49:41 +00003132 case MVT::f64: // Floating-point return values live in %ST(0)
3133 ContainsFPCode = true;
3134 BuildMI(BB, X86::FpGETRESULT, 1, Result);
3135 break;
3136 }
3137 return Result+N.ResNo;
Chris Lattner7ce7a8f2005-05-12 23:06:28 +00003138 }
Chris Lattner70ea07c2005-05-09 21:17:38 +00003139 case ISD::READPORT:
3140 // First, determine that the size of the operand falls within the acceptable
3141 // range for this architecture.
3142 //
3143 if (Node->getOperand(1).getValueType() != MVT::i16) {
3144 std::cerr << "llvm.readport: Address size is not 16 bits\n";
3145 exit(1);
3146 }
3147
3148 // Make sure we generate both values.
3149 if (Result != 1) { // Generate the token
3150 if (!ExprMap.insert(std::make_pair(N.getValue(1), 1)).second)
3151 assert(0 && "readport already emitted!?");
3152 } else
3153 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
3154
3155 Select(Node->getOperand(0)); // Select the chain.
3156
3157 // If the port is a single-byte constant, use the immediate form.
3158 if (ConstantSDNode *Port = dyn_cast<ConstantSDNode>(Node->getOperand(1)))
3159 if ((Port->getValue() & 255) == Port->getValue()) {
3160 switch (Node->getValueType(0)) {
3161 case MVT::i8:
3162 BuildMI(BB, X86::IN8ri, 1).addImm(Port->getValue());
3163 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
3164 return Result;
3165 case MVT::i16:
3166 BuildMI(BB, X86::IN16ri, 1).addImm(Port->getValue());
3167 BuildMI(BB, X86::MOV16rr, 1, Result).addReg(X86::AX);
3168 return Result;
3169 case MVT::i32:
3170 BuildMI(BB, X86::IN32ri, 1).addImm(Port->getValue());
3171 BuildMI(BB, X86::MOV32rr, 1, Result).addReg(X86::EAX);
3172 return Result;
3173 default: break;
3174 }
3175 }
3176
3177 // Now, move the I/O port address into the DX register and use the IN
3178 // instruction to get the input data.
3179 //
3180 Tmp1 = SelectExpr(Node->getOperand(1));
3181 BuildMI(BB, X86::MOV16rr, 1, X86::DX).addReg(Tmp1);
3182 switch (Node->getValueType(0)) {
3183 case MVT::i8:
3184 BuildMI(BB, X86::IN8rr, 0);
3185 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
3186 return Result;
3187 case MVT::i16:
3188 BuildMI(BB, X86::IN16rr, 0);
3189 BuildMI(BB, X86::MOV16rr, 1, Result).addReg(X86::AX);
3190 return Result;
3191 case MVT::i32:
3192 BuildMI(BB, X86::IN32rr, 0);
3193 BuildMI(BB, X86::MOV32rr, 1, Result).addReg(X86::EAX);
3194 return Result;
3195 default:
3196 std::cerr << "Cannot do input on this data type";
3197 exit(1);
3198 }
3199
Chris Lattner88c8a232005-01-07 07:49:41 +00003200 }
3201
3202 return 0;
3203}
3204
Chris Lattner96113fd2005-01-17 19:25:26 +00003205/// TryToFoldLoadOpStore - Given a store node, try to fold together a
3206/// load/op/store instruction. If successful return true.
3207bool ISel::TryToFoldLoadOpStore(SDNode *Node) {
3208 assert(Node->getOpcode() == ISD::STORE && "Can only do this for stores!");
3209 SDOperand Chain = Node->getOperand(0);
3210 SDOperand StVal = Node->getOperand(1);
Chris Lattnere86c9332005-01-17 22:10:42 +00003211 SDOperand StPtr = Node->getOperand(2);
Chris Lattner96113fd2005-01-17 19:25:26 +00003212
3213 // The chain has to be a load, the stored value must be an integer binary
3214 // operation with one use.
Chris Lattnere86c9332005-01-17 22:10:42 +00003215 if (!StVal.Val->hasOneUse() || StVal.Val->getNumOperands() != 2 ||
Chris Lattner96113fd2005-01-17 19:25:26 +00003216 MVT::isFloatingPoint(StVal.getValueType()))
3217 return false;
3218
Chris Lattnere86c9332005-01-17 22:10:42 +00003219 // Token chain must either be a factor node or the load to fold.
3220 if (Chain.getOpcode() != ISD::LOAD && Chain.getOpcode() != ISD::TokenFactor)
3221 return false;
Chris Lattner96113fd2005-01-17 19:25:26 +00003222
Chris Lattnere86c9332005-01-17 22:10:42 +00003223 SDOperand TheLoad;
3224
3225 // Check to see if there is a load from the same pointer that we're storing
3226 // to in either operand of the binop.
3227 if (StVal.getOperand(0).getOpcode() == ISD::LOAD &&
3228 StVal.getOperand(0).getOperand(1) == StPtr)
3229 TheLoad = StVal.getOperand(0);
3230 else if (StVal.getOperand(1).getOpcode() == ISD::LOAD &&
3231 StVal.getOperand(1).getOperand(1) == StPtr)
3232 TheLoad = StVal.getOperand(1);
3233 else
3234 return false; // No matching load operand.
3235
3236 // We can only fold the load if there are no intervening side-effecting
3237 // operations. This means that the store uses the load as its token chain, or
3238 // there are only token factor nodes in between the store and load.
3239 if (Chain != TheLoad.getValue(1)) {
3240 // Okay, the other option is that we have a store referring to (possibly
3241 // nested) token factor nodes. For now, just try peeking through one level
3242 // of token factors to see if this is the case.
3243 bool ChainOk = false;
3244 if (Chain.getOpcode() == ISD::TokenFactor) {
3245 for (unsigned i = 0, e = Chain.getNumOperands(); i != e; ++i)
3246 if (Chain.getOperand(i) == TheLoad.getValue(1)) {
3247 ChainOk = true;
3248 break;
3249 }
3250 }
3251
3252 if (!ChainOk) return false;
3253 }
3254
3255 if (TheLoad.getOperand(1) != StPtr)
Chris Lattner96113fd2005-01-17 19:25:26 +00003256 return false;
3257
3258 // Make sure that one of the operands of the binop is the load, and that the
3259 // load folds into the binop.
3260 if (((StVal.getOperand(0) != TheLoad ||
3261 !isFoldableLoad(TheLoad, StVal.getOperand(1))) &&
3262 (StVal.getOperand(1) != TheLoad ||
3263 !isFoldableLoad(TheLoad, StVal.getOperand(0)))))
3264 return false;
3265
3266 // Finally, check to see if this is one of the ops we can handle!
3267 static const unsigned ADDTAB[] = {
3268 X86::ADD8mi, X86::ADD16mi, X86::ADD32mi,
3269 X86::ADD8mr, X86::ADD16mr, X86::ADD32mr,
3270 };
3271 static const unsigned SUBTAB[] = {
3272 X86::SUB8mi, X86::SUB16mi, X86::SUB32mi,
3273 X86::SUB8mr, X86::SUB16mr, X86::SUB32mr,
3274 };
3275 static const unsigned ANDTAB[] = {
3276 X86::AND8mi, X86::AND16mi, X86::AND32mi,
3277 X86::AND8mr, X86::AND16mr, X86::AND32mr,
3278 };
3279 static const unsigned ORTAB[] = {
3280 X86::OR8mi, X86::OR16mi, X86::OR32mi,
3281 X86::OR8mr, X86::OR16mr, X86::OR32mr,
3282 };
3283 static const unsigned XORTAB[] = {
3284 X86::XOR8mi, X86::XOR16mi, X86::XOR32mi,
3285 X86::XOR8mr, X86::XOR16mr, X86::XOR32mr,
3286 };
3287 static const unsigned SHLTAB[] = {
3288 X86::SHL8mi, X86::SHL16mi, X86::SHL32mi,
3289 /*Have to put the reg in CL*/0, 0, 0,
3290 };
3291 static const unsigned SARTAB[] = {
3292 X86::SAR8mi, X86::SAR16mi, X86::SAR32mi,
3293 /*Have to put the reg in CL*/0, 0, 0,
3294 };
3295 static const unsigned SHRTAB[] = {
3296 X86::SHR8mi, X86::SHR16mi, X86::SHR32mi,
3297 /*Have to put the reg in CL*/0, 0, 0,
3298 };
Misha Brukmanc88330a2005-04-21 23:38:14 +00003299
Chris Lattner96113fd2005-01-17 19:25:26 +00003300 const unsigned *TabPtr = 0;
3301 switch (StVal.getOpcode()) {
3302 default:
3303 std::cerr << "CANNOT [mem] op= val: ";
3304 StVal.Val->dump(); std::cerr << "\n";
3305 case ISD::MUL:
3306 case ISD::SDIV:
3307 case ISD::UDIV:
3308 case ISD::SREM:
3309 case ISD::UREM: return false;
Misha Brukmanc88330a2005-04-21 23:38:14 +00003310
Chris Lattner96113fd2005-01-17 19:25:26 +00003311 case ISD::ADD: TabPtr = ADDTAB; break;
3312 case ISD::SUB: TabPtr = SUBTAB; break;
3313 case ISD::AND: TabPtr = ANDTAB; break;
3314 case ISD:: OR: TabPtr = ORTAB; break;
3315 case ISD::XOR: TabPtr = XORTAB; break;
3316 case ISD::SHL: TabPtr = SHLTAB; break;
3317 case ISD::SRA: TabPtr = SARTAB; break;
3318 case ISD::SRL: TabPtr = SHRTAB; break;
3319 }
Misha Brukmanc88330a2005-04-21 23:38:14 +00003320
Chris Lattner96113fd2005-01-17 19:25:26 +00003321 // Handle: [mem] op= CST
3322 SDOperand Op0 = StVal.getOperand(0);
3323 SDOperand Op1 = StVal.getOperand(1);
Chris Lattner0e1de102005-01-23 23:20:06 +00003324 unsigned Opc = 0;
Chris Lattner96113fd2005-01-17 19:25:26 +00003325 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1)) {
3326 switch (Op0.getValueType()) { // Use Op0's type because of shifts.
3327 default: break;
3328 case MVT::i1:
3329 case MVT::i8: Opc = TabPtr[0]; break;
3330 case MVT::i16: Opc = TabPtr[1]; break;
3331 case MVT::i32: Opc = TabPtr[2]; break;
3332 }
Misha Brukmanc88330a2005-04-21 23:38:14 +00003333
Chris Lattner96113fd2005-01-17 19:25:26 +00003334 if (Opc) {
Chris Lattner78d30282005-01-18 03:51:59 +00003335 if (!ExprMap.insert(std::make_pair(TheLoad.getValue(1), 1)).second)
3336 assert(0 && "Already emitted?");
Chris Lattnere86c9332005-01-17 22:10:42 +00003337 Select(Chain);
3338
Chris Lattner96113fd2005-01-17 19:25:26 +00003339 X86AddressMode AM;
3340 if (getRegPressure(TheLoad.getOperand(0)) >
3341 getRegPressure(TheLoad.getOperand(1))) {
3342 Select(TheLoad.getOperand(0));
3343 SelectAddress(TheLoad.getOperand(1), AM);
3344 } else {
3345 SelectAddress(TheLoad.getOperand(1), AM);
3346 Select(TheLoad.getOperand(0));
Misha Brukmanc88330a2005-04-21 23:38:14 +00003347 }
Chris Lattnere86c9332005-01-17 22:10:42 +00003348
3349 if (StVal.getOpcode() == ISD::ADD) {
3350 if (CN->getValue() == 1) {
3351 switch (Op0.getValueType()) {
3352 default: break;
3353 case MVT::i8:
3354 addFullAddress(BuildMI(BB, X86::INC8m, 4), AM);
3355 return true;
3356 case MVT::i16: Opc = TabPtr[1];
3357 addFullAddress(BuildMI(BB, X86::INC16m, 4), AM);
3358 return true;
3359 case MVT::i32: Opc = TabPtr[2];
3360 addFullAddress(BuildMI(BB, X86::INC32m, 4), AM);
3361 return true;
3362 }
3363 } else if (CN->getValue()+1 == 0) { // [X] += -1 -> DEC [X]
3364 switch (Op0.getValueType()) {
3365 default: break;
3366 case MVT::i8:
3367 addFullAddress(BuildMI(BB, X86::DEC8m, 4), AM);
3368 return true;
3369 case MVT::i16: Opc = TabPtr[1];
3370 addFullAddress(BuildMI(BB, X86::DEC16m, 4), AM);
3371 return true;
3372 case MVT::i32: Opc = TabPtr[2];
3373 addFullAddress(BuildMI(BB, X86::DEC32m, 4), AM);
3374 return true;
3375 }
3376 }
3377 }
Misha Brukmanc88330a2005-04-21 23:38:14 +00003378
Chris Lattner96113fd2005-01-17 19:25:26 +00003379 addFullAddress(BuildMI(BB, Opc, 4+1),AM).addImm(CN->getValue());
3380 return true;
3381 }
3382 }
Misha Brukmanc88330a2005-04-21 23:38:14 +00003383
Chris Lattner96113fd2005-01-17 19:25:26 +00003384 // If we have [mem] = V op [mem], try to turn it into:
3385 // [mem] = [mem] op V.
3386 if (Op1 == TheLoad && StVal.getOpcode() != ISD::SUB &&
3387 StVal.getOpcode() != ISD::SHL && StVal.getOpcode() != ISD::SRA &&
3388 StVal.getOpcode() != ISD::SRL)
3389 std::swap(Op0, Op1);
Misha Brukmanc88330a2005-04-21 23:38:14 +00003390
Chris Lattner96113fd2005-01-17 19:25:26 +00003391 if (Op0 != TheLoad) return false;
3392
3393 switch (Op0.getValueType()) {
3394 default: return false;
3395 case MVT::i1:
3396 case MVT::i8: Opc = TabPtr[3]; break;
3397 case MVT::i16: Opc = TabPtr[4]; break;
3398 case MVT::i32: Opc = TabPtr[5]; break;
3399 }
Chris Lattnere86c9332005-01-17 22:10:42 +00003400
Chris Lattner479c7112005-01-18 17:35:28 +00003401 // Table entry doesn't exist?
3402 if (Opc == 0) return false;
3403
Chris Lattner78d30282005-01-18 03:51:59 +00003404 if (!ExprMap.insert(std::make_pair(TheLoad.getValue(1), 1)).second)
3405 assert(0 && "Already emitted?");
Chris Lattnere86c9332005-01-17 22:10:42 +00003406 Select(Chain);
Chris Lattner96113fd2005-01-17 19:25:26 +00003407 Select(TheLoad.getOperand(0));
Chris Lattnera7acdda2005-01-18 01:06:26 +00003408
Chris Lattner96113fd2005-01-17 19:25:26 +00003409 X86AddressMode AM;
3410 SelectAddress(TheLoad.getOperand(1), AM);
3411 unsigned Reg = SelectExpr(Op1);
Chris Lattnera7acdda2005-01-18 01:06:26 +00003412 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addReg(Reg);
Chris Lattner96113fd2005-01-17 19:25:26 +00003413 return true;
3414}
3415
3416
Chris Lattner88c8a232005-01-07 07:49:41 +00003417void ISel::Select(SDOperand N) {
3418 unsigned Tmp1, Tmp2, Opc;
3419
Nate Begeman95210522005-03-24 04:39:54 +00003420 if (!ExprMap.insert(std::make_pair(N, 1)).second)
Chris Lattner88c8a232005-01-07 07:49:41 +00003421 return; // Already selected.
3422
Chris Lattner36f78482005-01-11 06:14:36 +00003423 SDNode *Node = N.Val;
3424
3425 switch (Node->getOpcode()) {
Chris Lattner88c8a232005-01-07 07:49:41 +00003426 default:
Chris Lattner36f78482005-01-11 06:14:36 +00003427 Node->dump(); std::cerr << "\n";
Chris Lattner88c8a232005-01-07 07:49:41 +00003428 assert(0 && "Node not handled yet!");
3429 case ISD::EntryToken: return; // Noop
Chris Lattnerc251fb62005-01-13 18:01:36 +00003430 case ISD::TokenFactor:
Chris Lattner15bd19d2005-01-13 19:56:00 +00003431 if (Node->getNumOperands() == 2) {
Misha Brukmanc88330a2005-04-21 23:38:14 +00003432 bool OneFirst =
Chris Lattner15bd19d2005-01-13 19:56:00 +00003433 getRegPressure(Node->getOperand(1))>getRegPressure(Node->getOperand(0));
3434 Select(Node->getOperand(OneFirst));
3435 Select(Node->getOperand(!OneFirst));
3436 } else {
3437 std::vector<std::pair<unsigned, unsigned> > OpsP;
3438 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
3439 OpsP.push_back(std::make_pair(getRegPressure(Node->getOperand(i)), i));
3440 std::sort(OpsP.begin(), OpsP.end());
3441 std::reverse(OpsP.begin(), OpsP.end());
3442 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
3443 Select(Node->getOperand(OpsP[i].second));
3444 }
Chris Lattnerc251fb62005-01-13 18:01:36 +00003445 return;
Chris Lattner88c8a232005-01-07 07:49:41 +00003446 case ISD::CopyToReg:
Chris Lattner2cfce682005-01-12 02:02:48 +00003447 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
3448 Select(N.getOperand(0));
3449 Tmp1 = SelectExpr(N.getOperand(1));
3450 } else {
3451 Tmp1 = SelectExpr(N.getOperand(1));
3452 Select(N.getOperand(0));
3453 }
Chris Lattnere727af02005-01-13 20:50:02 +00003454 Tmp2 = cast<RegSDNode>(N)->getReg();
Misha Brukmanc88330a2005-04-21 23:38:14 +00003455
Chris Lattner88c8a232005-01-07 07:49:41 +00003456 if (Tmp1 != Tmp2) {
3457 switch (N.getOperand(1).getValueType()) {
3458 default: assert(0 && "Invalid type for operation!");
3459 case MVT::i1:
3460 case MVT::i8: Opc = X86::MOV8rr; break;
3461 case MVT::i16: Opc = X86::MOV16rr; break;
3462 case MVT::i32: Opc = X86::MOV32rr; break;
Chris Lattnere44e6d12005-01-11 03:50:45 +00003463 case MVT::f64: Opc = X86::FpMOV; ContainsFPCode = true; break;
Chris Lattner88c8a232005-01-07 07:49:41 +00003464 }
3465 BuildMI(BB, Opc, 1, Tmp2).addReg(Tmp1);
3466 }
3467 return;
3468 case ISD::RET:
Chris Lattner88c8a232005-01-07 07:49:41 +00003469 switch (N.getNumOperands()) {
3470 default:
3471 assert(0 && "Unknown return instruction!");
3472 case 3:
Chris Lattner88c8a232005-01-07 07:49:41 +00003473 assert(N.getOperand(1).getValueType() == MVT::i32 &&
3474 N.getOperand(2).getValueType() == MVT::i32 &&
3475 "Unknown two-register value!");
Chris Lattner0d1f82a2005-01-11 03:11:44 +00003476 if (getRegPressure(N.getOperand(1)) > getRegPressure(N.getOperand(2))) {
3477 Tmp1 = SelectExpr(N.getOperand(1));
3478 Tmp2 = SelectExpr(N.getOperand(2));
3479 } else {
3480 Tmp2 = SelectExpr(N.getOperand(2));
3481 Tmp1 = SelectExpr(N.getOperand(1));
3482 }
3483 Select(N.getOperand(0));
3484
Chris Lattner88c8a232005-01-07 07:49:41 +00003485 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
3486 BuildMI(BB, X86::MOV32rr, 1, X86::EDX).addReg(Tmp2);
Chris Lattner88c8a232005-01-07 07:49:41 +00003487 break;
3488 case 2:
Chris Lattner0d1f82a2005-01-11 03:11:44 +00003489 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
3490 Select(N.getOperand(0));
3491 Tmp1 = SelectExpr(N.getOperand(1));
3492 } else {
3493 Tmp1 = SelectExpr(N.getOperand(1));
3494 Select(N.getOperand(0));
3495 }
Chris Lattner88c8a232005-01-07 07:49:41 +00003496 switch (N.getOperand(1).getValueType()) {
3497 default: assert(0 && "All other types should have been promoted!!");
3498 case MVT::f64:
3499 BuildMI(BB, X86::FpSETRESULT, 1).addReg(Tmp1);
Chris Lattner88c8a232005-01-07 07:49:41 +00003500 break;
3501 case MVT::i32:
3502 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
Chris Lattner88c8a232005-01-07 07:49:41 +00003503 break;
3504 }
3505 break;
3506 case 1:
Chris Lattner0d1f82a2005-01-11 03:11:44 +00003507 Select(N.getOperand(0));
Chris Lattner88c8a232005-01-07 07:49:41 +00003508 break;
3509 }
3510 BuildMI(BB, X86::RET, 0); // Just emit a 'ret' instruction
3511 return;
3512 case ISD::BR: {
3513 Select(N.getOperand(0));
3514 MachineBasicBlock *Dest =
3515 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
3516 BuildMI(BB, X86::JMP, 1).addMBB(Dest);
3517 return;
3518 }
3519
3520 case ISD::BRCOND: {
Chris Lattner88c8a232005-01-07 07:49:41 +00003521 MachineBasicBlock *Dest =
3522 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
Chris Lattner0d1f82a2005-01-11 03:11:44 +00003523
Chris Lattner88c8a232005-01-07 07:49:41 +00003524 // Try to fold a setcc into the branch. If this fails, emit a test/jne
3525 // pair.
Chris Lattner37ed2852005-01-11 04:06:27 +00003526 if (EmitBranchCC(Dest, N.getOperand(0), N.getOperand(1))) {
3527 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
3528 Select(N.getOperand(0));
3529 Tmp1 = SelectExpr(N.getOperand(1));
3530 } else {
3531 Tmp1 = SelectExpr(N.getOperand(1));
3532 Select(N.getOperand(0));
3533 }
Chris Lattner88c8a232005-01-07 07:49:41 +00003534 BuildMI(BB, X86::TEST8rr, 2).addReg(Tmp1).addReg(Tmp1);
3535 BuildMI(BB, X86::JNE, 1).addMBB(Dest);
3536 }
Chris Lattner0d1f82a2005-01-11 03:11:44 +00003537
Chris Lattner88c8a232005-01-07 07:49:41 +00003538 return;
3539 }
Chris Lattnere18a4c42005-01-15 05:22:24 +00003540
Chris Lattnerc1f386c2005-01-17 00:00:33 +00003541 case ISD::LOAD:
3542 // If this load could be folded into the only using instruction, and if it
3543 // is safe to emit the instruction here, try to do so now.
3544 if (Node->hasNUsesOfValue(1, 0)) {
3545 SDOperand TheVal = N.getValue(0);
3546 SDNode *User = 0;
3547 for (SDNode::use_iterator UI = Node->use_begin(); ; ++UI) {
3548 assert(UI != Node->use_end() && "Didn't find use!");
3549 SDNode *UN = *UI;
3550 for (unsigned i = 0, e = UN->getNumOperands(); i != e; ++i)
3551 if (UN->getOperand(i) == TheVal) {
3552 User = UN;
3553 goto FoundIt;
3554 }
3555 }
3556 FoundIt:
3557 // Only handle unary operators right now.
3558 if (User->getNumOperands() == 1) {
Chris Lattner78d30282005-01-18 03:51:59 +00003559 ExprMap.erase(N);
Chris Lattnerc1f386c2005-01-17 00:00:33 +00003560 SelectExpr(SDOperand(User, 0));
3561 return;
3562 }
3563 }
Chris Lattner28a205e2005-01-18 04:00:54 +00003564 ExprMap.erase(N);
Chris Lattnerc1f386c2005-01-17 00:00:33 +00003565 SelectExpr(N);
3566 return;
Chris Lattner70ea07c2005-05-09 21:17:38 +00003567 case ISD::READPORT:
Chris Lattnere18a4c42005-01-15 05:22:24 +00003568 case ISD::EXTLOAD:
3569 case ISD::SEXTLOAD:
3570 case ISD::ZEXTLOAD:
Chris Lattner88c8a232005-01-07 07:49:41 +00003571 case ISD::CALL:
3572 case ISD::DYNAMIC_STACKALLOC:
Chris Lattner28a205e2005-01-18 04:00:54 +00003573 ExprMap.erase(N);
Chris Lattner88c8a232005-01-07 07:49:41 +00003574 SelectExpr(N);
3575 return;
Chris Lattner7ce7a8f2005-05-12 23:06:28 +00003576 case ISD::CopyFromReg:
3577 ExprMap.erase(N);
3578 SelectExpr(N.getValue(0));
3579 return;
Chris Lattnere18a4c42005-01-15 05:22:24 +00003580
3581 case ISD::TRUNCSTORE: { // truncstore chain, val, ptr :storety
3582 // On X86, we can represent all types except for Bool and Float natively.
3583 X86AddressMode AM;
3584 MVT::ValueType StoredTy = cast<MVTSDNode>(Node)->getExtraValueType();
Chris Lattnerb14a63a2005-01-16 07:34:08 +00003585 assert((StoredTy == MVT::i1 || StoredTy == MVT::f32 ||
3586 StoredTy == MVT::i16 /*FIXME: THIS IS JUST FOR TESTING!*/)
3587 && "Unsupported TRUNCSTORE for this target!");
3588
3589 if (StoredTy == MVT::i16) {
3590 // FIXME: This is here just to allow testing. X86 doesn't really have a
3591 // TRUNCSTORE i16 operation, but this is required for targets that do not
3592 // have 16-bit integer registers. We occasionally disable 16-bit integer
3593 // registers to test the promotion code.
3594 Select(N.getOperand(0));
3595 Tmp1 = SelectExpr(N.getOperand(1));
3596 SelectAddress(N.getOperand(2), AM);
3597
3598 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
3599 addFullAddress(BuildMI(BB, X86::MOV16mr, 5), AM).addReg(X86::AX);
3600 return;
3601 }
Chris Lattnere18a4c42005-01-15 05:22:24 +00003602
3603 // Store of constant bool?
3604 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
3605 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(2))) {
3606 Select(N.getOperand(0));
3607 SelectAddress(N.getOperand(2), AM);
3608 } else {
3609 SelectAddress(N.getOperand(2), AM);
3610 Select(N.getOperand(0));
3611 }
3612 addFullAddress(BuildMI(BB, X86::MOV8mi, 5), AM).addImm(CN->getValue());
3613 return;
3614 }
3615
3616 switch (StoredTy) {
3617 default: assert(0 && "Cannot truncstore this type!");
3618 case MVT::i1: Opc = X86::MOV8mr; break;
3619 case MVT::f32: Opc = X86::FST32m; break;
3620 }
Misha Brukmanc88330a2005-04-21 23:38:14 +00003621
Chris Lattnere18a4c42005-01-15 05:22:24 +00003622 std::vector<std::pair<unsigned, unsigned> > RP;
3623 RP.push_back(std::make_pair(getRegPressure(N.getOperand(0)), 0));
3624 RP.push_back(std::make_pair(getRegPressure(N.getOperand(1)), 1));
3625 RP.push_back(std::make_pair(getRegPressure(N.getOperand(2)), 2));
3626 std::sort(RP.begin(), RP.end());
3627
Chris Lattner80c5b972005-02-23 05:57:21 +00003628 Tmp1 = 0; // Silence a warning.
Chris Lattnere18a4c42005-01-15 05:22:24 +00003629 for (unsigned i = 0; i != 3; ++i)
3630 switch (RP[2-i].second) {
3631 default: assert(0 && "Unknown operand number!");
3632 case 0: Select(N.getOperand(0)); break;
3633 case 1: Tmp1 = SelectExpr(N.getOperand(1)); break;
3634 case 2: SelectAddress(N.getOperand(2), AM); break;
3635 }
3636
3637 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addReg(Tmp1);
3638 return;
3639 }
Chris Lattner88c8a232005-01-07 07:49:41 +00003640 case ISD::STORE: {
Chris Lattner88c8a232005-01-07 07:49:41 +00003641 X86AddressMode AM;
Chris Lattner88c8a232005-01-07 07:49:41 +00003642
3643 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
3644 Opc = 0;
3645 switch (CN->getValueType(0)) {
3646 default: assert(0 && "Invalid type for operation!");
3647 case MVT::i1:
3648 case MVT::i8: Opc = X86::MOV8mi; break;
3649 case MVT::i16: Opc = X86::MOV16mi; break;
3650 case MVT::i32: Opc = X86::MOV32mi; break;
Chris Lattner88c8a232005-01-07 07:49:41 +00003651 case MVT::f64: break;
3652 }
3653 if (Opc) {
Chris Lattner0d1f82a2005-01-11 03:11:44 +00003654 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(2))) {
3655 Select(N.getOperand(0));
3656 SelectAddress(N.getOperand(2), AM);
3657 } else {
3658 SelectAddress(N.getOperand(2), AM);
3659 Select(N.getOperand(0));
3660 }
Chris Lattner88c8a232005-01-07 07:49:41 +00003661 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addImm(CN->getValue());
3662 return;
3663 }
Chris Lattneradcfc172005-04-21 19:03:24 +00003664 } else if (GlobalAddressSDNode *GA =
3665 dyn_cast<GlobalAddressSDNode>(N.getOperand(1))) {
3666 assert(GA->getValueType(0) == MVT::i32 && "Bad pointer operand");
3667
3668 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(2))) {
3669 Select(N.getOperand(0));
3670 SelectAddress(N.getOperand(2), AM);
3671 } else {
3672 SelectAddress(N.getOperand(2), AM);
3673 Select(N.getOperand(0));
3674 }
3675 addFullAddress(BuildMI(BB, X86::MOV32mi, 4+1),
3676 AM).addGlobalAddress(GA->getGlobal());
3677 return;
Chris Lattner88c8a232005-01-07 07:49:41 +00003678 }
Chris Lattner75bac9f2005-01-11 23:21:30 +00003679
3680 // Check to see if this is a load/op/store combination.
Chris Lattner96113fd2005-01-17 19:25:26 +00003681 if (TryToFoldLoadOpStore(Node))
3682 return;
Chris Lattner75bac9f2005-01-11 23:21:30 +00003683
Chris Lattner88c8a232005-01-07 07:49:41 +00003684 switch (N.getOperand(1).getValueType()) {
3685 default: assert(0 && "Cannot store this type!");
3686 case MVT::i1:
3687 case MVT::i8: Opc = X86::MOV8mr; break;
3688 case MVT::i16: Opc = X86::MOV16mr; break;
3689 case MVT::i32: Opc = X86::MOV32mr; break;
Chris Lattnere44e6d12005-01-11 03:50:45 +00003690 case MVT::f64: Opc = X86::FST64m; break;
Chris Lattner88c8a232005-01-07 07:49:41 +00003691 }
Misha Brukmanc88330a2005-04-21 23:38:14 +00003692
Chris Lattner0d1f82a2005-01-11 03:11:44 +00003693 std::vector<std::pair<unsigned, unsigned> > RP;
3694 RP.push_back(std::make_pair(getRegPressure(N.getOperand(0)), 0));
3695 RP.push_back(std::make_pair(getRegPressure(N.getOperand(1)), 1));
3696 RP.push_back(std::make_pair(getRegPressure(N.getOperand(2)), 2));
3697 std::sort(RP.begin(), RP.end());
3698
Chris Lattner80c5b972005-02-23 05:57:21 +00003699 Tmp1 = 0; // Silence a warning.
Chris Lattner0d1f82a2005-01-11 03:11:44 +00003700 for (unsigned i = 0; i != 3; ++i)
3701 switch (RP[2-i].second) {
3702 default: assert(0 && "Unknown operand number!");
3703 case 0: Select(N.getOperand(0)); break;
3704 case 1: Tmp1 = SelectExpr(N.getOperand(1)); break;
Chris Lattner8fea42b2005-01-11 03:37:59 +00003705 case 2: SelectAddress(N.getOperand(2), AM); break;
Chris Lattner0d1f82a2005-01-11 03:11:44 +00003706 }
3707
Chris Lattner88c8a232005-01-07 07:49:41 +00003708 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addReg(Tmp1);
3709 return;
3710 }
Chris Lattner2dce7032005-05-12 23:24:06 +00003711 case ISD::CALLSEQ_START:
3712 case ISD::CALLSEQ_END:
Chris Lattner88c8a232005-01-07 07:49:41 +00003713 Select(N.getOperand(0));
3714 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
Misha Brukmanc88330a2005-04-21 23:38:14 +00003715
Chris Lattner2dce7032005-05-12 23:24:06 +00003716 Opc = N.getOpcode() == ISD::CALLSEQ_START ? X86::ADJCALLSTACKDOWN :
3717 X86::ADJCALLSTACKUP;
Chris Lattner88c8a232005-01-07 07:49:41 +00003718 BuildMI(BB, Opc, 1).addImm(Tmp1);
3719 return;
Chris Lattner36f78482005-01-11 06:14:36 +00003720 case ISD::MEMSET: {
3721 Select(N.getOperand(0)); // Select the chain.
3722 unsigned Align =
3723 (unsigned)cast<ConstantSDNode>(Node->getOperand(4))->getValue();
3724 if (Align == 0) Align = 1;
3725
3726 // Turn the byte code into # iterations
3727 unsigned CountReg;
3728 unsigned Opcode;
3729 if (ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Node->getOperand(2))) {
3730 unsigned Val = ValC->getValue() & 255;
3731
3732 // If the value is a constant, then we can potentially use larger sets.
3733 switch (Align & 3) {
3734 case 2: // WORD aligned
3735 CountReg = MakeReg(MVT::i32);
3736 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
3737 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/2);
3738 } else {
3739 unsigned ByteReg = SelectExpr(Node->getOperand(3));
3740 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(1);
3741 }
3742 BuildMI(BB, X86::MOV16ri, 1, X86::AX).addImm((Val << 8) | Val);
3743 Opcode = X86::REP_STOSW;
3744 break;
3745 case 0: // DWORD aligned
3746 CountReg = MakeReg(MVT::i32);
3747 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
3748 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/4);
3749 } else {
3750 unsigned ByteReg = SelectExpr(Node->getOperand(3));
3751 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(2);
3752 }
3753 Val = (Val << 8) | Val;
3754 BuildMI(BB, X86::MOV32ri, 1, X86::EAX).addImm((Val << 16) | Val);
3755 Opcode = X86::REP_STOSD;
3756 break;
3757 default: // BYTE aligned
3758 CountReg = SelectExpr(Node->getOperand(3));
3759 BuildMI(BB, X86::MOV8ri, 1, X86::AL).addImm(Val);
3760 Opcode = X86::REP_STOSB;
3761 break;
3762 }
3763 } else {
3764 // If it's not a constant value we are storing, just fall back. We could
3765 // try to be clever to form 16 bit and 32 bit values, but we don't yet.
3766 unsigned ValReg = SelectExpr(Node->getOperand(2));
3767 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(ValReg);
3768 CountReg = SelectExpr(Node->getOperand(3));
3769 Opcode = X86::REP_STOSB;
3770 }
3771
3772 // No matter what the alignment is, we put the source in ESI, the
3773 // destination in EDI, and the count in ECX.
3774 unsigned TmpReg1 = SelectExpr(Node->getOperand(1));
3775 BuildMI(BB, X86::MOV32rr, 1, X86::ECX).addReg(CountReg);
3776 BuildMI(BB, X86::MOV32rr, 1, X86::EDI).addReg(TmpReg1);
3777 BuildMI(BB, Opcode, 0);
3778 return;
3779 }
Chris Lattner70ea07c2005-05-09 21:17:38 +00003780 case ISD::MEMCPY: {
Chris Lattnerc07164e2005-01-11 06:19:26 +00003781 Select(N.getOperand(0)); // Select the chain.
3782 unsigned Align =
3783 (unsigned)cast<ConstantSDNode>(Node->getOperand(4))->getValue();
3784 if (Align == 0) Align = 1;
3785
3786 // Turn the byte code into # iterations
3787 unsigned CountReg;
3788 unsigned Opcode;
3789 switch (Align & 3) {
3790 case 2: // WORD aligned
3791 CountReg = MakeReg(MVT::i32);
3792 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
3793 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/2);
3794 } else {
3795 unsigned ByteReg = SelectExpr(Node->getOperand(3));
3796 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(1);
3797 }
3798 Opcode = X86::REP_MOVSW;
3799 break;
3800 case 0: // DWORD aligned
3801 CountReg = MakeReg(MVT::i32);
3802 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
3803 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/4);
3804 } else {
3805 unsigned ByteReg = SelectExpr(Node->getOperand(3));
3806 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(2);
3807 }
3808 Opcode = X86::REP_MOVSD;
3809 break;
3810 default: // BYTE aligned
3811 CountReg = SelectExpr(Node->getOperand(3));
3812 Opcode = X86::REP_MOVSB;
3813 break;
3814 }
3815
3816 // No matter what the alignment is, we put the source in ESI, the
3817 // destination in EDI, and the count in ECX.
3818 unsigned TmpReg1 = SelectExpr(Node->getOperand(1));
3819 unsigned TmpReg2 = SelectExpr(Node->getOperand(2));
3820 BuildMI(BB, X86::MOV32rr, 1, X86::ECX).addReg(CountReg);
3821 BuildMI(BB, X86::MOV32rr, 1, X86::EDI).addReg(TmpReg1);
3822 BuildMI(BB, X86::MOV32rr, 1, X86::ESI).addReg(TmpReg2);
3823 BuildMI(BB, Opcode, 0);
3824 return;
Chris Lattner88c8a232005-01-07 07:49:41 +00003825 }
Chris Lattner70ea07c2005-05-09 21:17:38 +00003826 case ISD::WRITEPORT:
3827 if (Node->getOperand(2).getValueType() != MVT::i16) {
3828 std::cerr << "llvm.writeport: Address size is not 16 bits\n";
3829 exit(1);
3830 }
3831 Select(Node->getOperand(0)); // Emit the chain.
3832
3833 Tmp1 = SelectExpr(Node->getOperand(1));
3834 switch (Node->getOperand(1).getValueType()) {
3835 case MVT::i8:
3836 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(Tmp1);
3837 Tmp2 = X86::OUT8ir; Opc = X86::OUT8rr;
3838 break;
3839 case MVT::i16:
3840 BuildMI(BB, X86::MOV16rr, 1, X86::AX).addReg(Tmp1);
3841 Tmp2 = X86::OUT16ir; Opc = X86::OUT16rr;
3842 break;
3843 case MVT::i32:
3844 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
3845 Tmp2 = X86::OUT32ir; Opc = X86::OUT32rr;
3846 break;
3847 default:
3848 std::cerr << "llvm.writeport: invalid data type for X86 target";
3849 exit(1);
3850 }
3851
3852 // If the port is a single-byte constant, use the immediate form.
3853 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Node->getOperand(2)))
3854 if ((CN->getValue() & 255) == CN->getValue()) {
3855 BuildMI(BB, Tmp2, 1).addImm(CN->getValue());
3856 return;
3857 }
3858
3859 // Otherwise, move the I/O port address into the DX register.
3860 unsigned Reg = SelectExpr(Node->getOperand(2));
3861 BuildMI(BB, X86::MOV16rr, 1, X86::DX).addReg(Reg);
3862 BuildMI(BB, Opc, 0);
3863 return;
3864 }
Chris Lattner88c8a232005-01-07 07:49:41 +00003865 assert(0 && "Should not be reached!");
3866}
3867
3868
3869/// createX86PatternInstructionSelector - This pass converts an LLVM function
3870/// into a machine code representation using pattern matching and a machine
3871/// description file.
3872///
3873FunctionPass *llvm::createX86PatternInstructionSelector(TargetMachine &TM) {
Misha Brukmanc88330a2005-04-21 23:38:14 +00003874 return new ISel(TM);
Chris Lattner88c8a232005-01-07 07:49:41 +00003875}