blob: cadc742ff48c3696ea2a17ffd4dcb22c5395ee0b [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 Lattner2e77db62005-05-13 18:50:42 +000048 int BytesToPopOnReturn; // Number of bytes ret should pop.
Chris Lattner88c8a232005-01-07 07:49:41 +000049 public:
50 X86TargetLowering(TargetMachine &TM) : TargetLowering(TM) {
51 // Set up the TargetLowering object.
Chris Lattnerc1f386c2005-01-17 00:00:33 +000052
53 // X86 is wierd, it always uses i8 for shift amounts and setcc results.
54 setShiftAmountType(MVT::i8);
55 setSetCCResultType(MVT::i8);
Chris Lattner38fd9702005-04-07 19:41:46 +000056 setSetCCResultContents(ZeroOrOneSetCCResult);
Chris Lattnerd8d30662005-01-19 03:36:30 +000057 setShiftAmountFlavor(Mask); // shl X, 32 == shl X, 0
Chris Lattnerc1f386c2005-01-17 00:00:33 +000058
59 // Set up the register classes.
Chris Lattner88c8a232005-01-07 07:49:41 +000060 addRegisterClass(MVT::i8, X86::R8RegisterClass);
61 addRegisterClass(MVT::i16, X86::R16RegisterClass);
62 addRegisterClass(MVT::i32, X86::R32RegisterClass);
63 addRegisterClass(MVT::f64, X86::RFPRegisterClass);
Misha Brukmanc88330a2005-04-21 23:38:14 +000064
Chris Lattner88c8a232005-01-07 07:49:41 +000065 // FIXME: Eliminate these two classes when legalize can handle promotions
66 // well.
Chris Lattnerb14a63a2005-01-16 07:34:08 +000067/**/ addRegisterClass(MVT::i1, X86::R8RegisterClass);
Chris Lattnerb14a63a2005-01-16 07:34:08 +000068
Chris Lattnera3a135a2005-04-09 03:22:37 +000069 setOperationAction(ISD::BRCONDTWOWAY , MVT::Other, Expand);
Chris Lattnerb14a63a2005-01-16 07:34:08 +000070 setOperationAction(ISD::MEMMOVE , MVT::Other, Expand);
71 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Expand);
Chris Lattnerb14a63a2005-01-16 07:34:08 +000072 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
Chris Lattnerb14a63a2005-01-16 07:34:08 +000073 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
74 setOperationAction(ISD::SEXTLOAD , MVT::i1 , Expand);
75 setOperationAction(ISD::SREM , MVT::f64 , Expand);
Chris Lattner05ad4b82005-05-11 05:00:34 +000076 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
77 setOperationAction(ISD::CTTZ , MVT::i8 , Expand);
78 setOperationAction(ISD::CTLZ , MVT::i8 , Expand);
79 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
80 setOperationAction(ISD::CTTZ , MVT::i16 , Expand);
81 setOperationAction(ISD::CTLZ , MVT::i16 , Expand);
Andrew Lenharth5e177822005-05-03 17:19:30 +000082 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
83 setOperationAction(ISD::CTTZ , MVT::i32 , Expand);
Andrew Lenharthb8e94c32005-05-04 19:25:37 +000084 setOperationAction(ISD::CTLZ , MVT::i32 , Expand);
Chris Lattner0b7e4cd2005-04-02 05:03:24 +000085
Chris Lattner6c6a39a2005-05-09 20:37:29 +000086 setOperationAction(ISD::READIO , MVT::i1 , Expand);
87 setOperationAction(ISD::READIO , MVT::i8 , Expand);
88 setOperationAction(ISD::READIO , MVT::i16 , Expand);
89 setOperationAction(ISD::READIO , MVT::i32 , Expand);
90 setOperationAction(ISD::WRITEIO , MVT::i1 , Expand);
91 setOperationAction(ISD::WRITEIO , MVT::i8 , Expand);
92 setOperationAction(ISD::WRITEIO , MVT::i16 , Expand);
93 setOperationAction(ISD::WRITEIO , MVT::i32 , Expand);
94
Chris Lattnerdb68d392005-04-30 04:25:35 +000095 if (!UnsafeFPMath) {
96 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
97 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
98 }
99
Chris Lattnerb14a63a2005-01-16 07:34:08 +0000100 // These should be promoted to a larger select which is supported.
101/**/ setOperationAction(ISD::SELECT , MVT::i1 , Promote);
102 setOperationAction(ISD::SELECT , MVT::i8 , Promote);
Misha Brukmanc88330a2005-04-21 23:38:14 +0000103
Chris Lattner88c8a232005-01-07 07:49:41 +0000104 computeRegisterProperties();
Misha Brukmanc88330a2005-04-21 23:38:14 +0000105
Chris Lattner88c8a232005-01-07 07:49:41 +0000106 addLegalFPImmediate(+0.0); // FLD0
107 addLegalFPImmediate(+1.0); // FLD1
108 addLegalFPImmediate(-0.0); // FLD0/FCHS
109 addLegalFPImmediate(-1.0); // FLD1/FCHS
110 }
111
112 /// LowerArguments - This hook must be implemented to indicate how we should
113 /// lower the arguments for the specified function, into the specified DAG.
114 virtual std::vector<SDOperand>
115 LowerArguments(Function &F, SelectionDAG &DAG);
116
117 /// LowerCallTo - This hook lowers an abstract call to a function into an
118 /// actual call.
Chris Lattnerb52e0412005-01-08 19:28:19 +0000119 virtual std::pair<SDOperand, SDOperand>
Chris Lattner36674a12005-05-12 19:56:45 +0000120 LowerCallTo(SDOperand Chain, const Type *RetTy, bool isVarArg, unsigned CC,
Chris Lattner2e77db62005-05-13 18:50:42 +0000121 bool isTailCall, SDOperand Callee, ArgListTy &Args,
122 SelectionDAG &DAG);
Chris Lattner9f59d282005-01-09 00:01:27 +0000123
124 virtual std::pair<SDOperand, SDOperand>
125 LowerVAStart(SDOperand Chain, SelectionDAG &DAG);
126
127 virtual std::pair<SDOperand,SDOperand>
128 LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
129 const Type *ArgTy, SelectionDAG &DAG);
130
131 virtual std::pair<SDOperand, SDOperand>
132 LowerFrameReturnAddress(bool isFrameAddr, SDOperand Chain, unsigned Depth,
133 SelectionDAG &DAG);
Chris Lattner7ce7a8f2005-05-12 23:06:28 +0000134 private:
135 // C Calling Convention implementation.
136 std::vector<SDOperand> LowerCCCArguments(Function &F, SelectionDAG &DAG);
137 std::pair<SDOperand, SDOperand>
138 LowerCCCCallTo(SDOperand Chain, const Type *RetTy, bool isVarArg,
139 SDOperand Callee, ArgListTy &Args, SelectionDAG &DAG);
140
141 // Fast Calling Convention implementation.
142 std::vector<SDOperand> LowerFastCCArguments(Function &F, SelectionDAG &DAG);
143 std::pair<SDOperand, SDOperand>
144 LowerFastCCCallTo(SDOperand Chain, const Type *RetTy,
145 SDOperand Callee, ArgListTy &Args, SelectionDAG &DAG);
Chris Lattner88c8a232005-01-07 07:49:41 +0000146 };
147}
148
Chris Lattner88c8a232005-01-07 07:49:41 +0000149std::vector<SDOperand>
150X86TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
Chris Lattner7ce7a8f2005-05-12 23:06:28 +0000151 if (F.getCallingConv() == CallingConv::Fast && EnableFastCC)
152 return LowerFastCCArguments(F, DAG);
153 return LowerCCCArguments(F, DAG);
154}
155
156std::pair<SDOperand, SDOperand>
157X86TargetLowering::LowerCallTo(SDOperand Chain, const Type *RetTy,
158 bool isVarArg, unsigned CallingConv,
Chris Lattner2e77db62005-05-13 18:50:42 +0000159 bool isTailCall,
Chris Lattner7ce7a8f2005-05-12 23:06:28 +0000160 SDOperand Callee, ArgListTy &Args,
161 SelectionDAG &DAG) {
162 assert((!isVarArg || CallingConv == CallingConv::C) &&
163 "Only C takes varargs!");
164 if (CallingConv == CallingConv::Fast && EnableFastCC)
165 return LowerFastCCCallTo(Chain, RetTy, Callee, Args, DAG);
166 return LowerCCCCallTo(Chain, RetTy, isVarArg, Callee, Args, DAG);
167}
168
169//===----------------------------------------------------------------------===//
170// C Calling Convention implementation
171//===----------------------------------------------------------------------===//
172
173std::vector<SDOperand>
174X86TargetLowering::LowerCCCArguments(Function &F, SelectionDAG &DAG) {
Chris Lattner88c8a232005-01-07 07:49:41 +0000175 std::vector<SDOperand> ArgValues;
176
Chris Lattnerd8145bc2005-05-10 03:53:18 +0000177 MachineFunction &MF = DAG.getMachineFunction();
178 MachineFrameInfo *MFI = MF.getFrameInfo();
179
Chris Lattner88c8a232005-01-07 07:49:41 +0000180 // Add DAG nodes to load the arguments... On entry to a function on the X86,
181 // the stack frame looks like this:
182 //
183 // [ESP] -- return address
184 // [ESP + 4] -- first argument (leftmost lexically)
185 // [ESP + 8] -- second argument, if first argument is four bytes in size
Misha Brukmanc88330a2005-04-21 23:38:14 +0000186 // ...
Chris Lattner88c8a232005-01-07 07:49:41 +0000187 //
Chris Lattner88c8a232005-01-07 07:49:41 +0000188 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
Chris Lattner531f9e92005-03-15 04:54:21 +0000189 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
Chris Lattner88c8a232005-01-07 07:49:41 +0000190 MVT::ValueType ObjectVT = getValueType(I->getType());
191 unsigned ArgIncrement = 4;
192 unsigned ObjSize;
193 switch (ObjectVT) {
194 default: assert(0 && "Unhandled argument type!");
195 case MVT::i1:
196 case MVT::i8: ObjSize = 1; break;
197 case MVT::i16: ObjSize = 2; break;
198 case MVT::i32: ObjSize = 4; break;
199 case MVT::i64: ObjSize = ArgIncrement = 8; break;
200 case MVT::f32: ObjSize = 4; break;
201 case MVT::f64: ObjSize = ArgIncrement = 8; break;
202 }
203 // Create the frame index object for this incoming parameter...
204 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
Misha Brukmanc88330a2005-04-21 23:38:14 +0000205
Chris Lattner88c8a232005-01-07 07:49:41 +0000206 // Create the SelectionDAG nodes corresponding to a load from this parameter
207 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
208
209 // Don't codegen dead arguments. FIXME: remove this check when we can nuke
210 // dead loads.
211 SDOperand ArgValue;
212 if (!I->use_empty())
Chris Lattnerdaa064d2005-05-09 05:40:26 +0000213 ArgValue = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
214 DAG.getSrcValue(NULL));
Chris Lattner88c8a232005-01-07 07:49:41 +0000215 else {
216 if (MVT::isInteger(ObjectVT))
217 ArgValue = DAG.getConstant(0, ObjectVT);
218 else
219 ArgValue = DAG.getConstantFP(0, ObjectVT);
220 }
221 ArgValues.push_back(ArgValue);
222
223 ArgOffset += ArgIncrement; // Move on to the next argument...
224 }
225
226 // If the function takes variable number of arguments, make a frame index for
227 // the start of the first vararg value... for expansion of llvm.va_start.
228 if (F.isVarArg())
229 VarArgsFrameIndex = MFI->CreateFixedObject(1, ArgOffset);
Chris Lattner9f59d282005-01-09 00:01:27 +0000230 ReturnAddrIndex = 0; // No return address slot generated yet.
Chris Lattnerb59006c2005-04-09 15:23:56 +0000231
232 // Finally, inform the code generator which regs we return values in.
233 switch (getValueType(F.getReturnType())) {
234 default: assert(0 && "Unknown type!");
235 case MVT::isVoid: break;
236 case MVT::i1:
237 case MVT::i8:
238 case MVT::i16:
239 case MVT::i32:
240 MF.addLiveOut(X86::EAX);
241 break;
242 case MVT::i64:
243 MF.addLiveOut(X86::EAX);
244 MF.addLiveOut(X86::EDX);
245 break;
246 case MVT::f32:
247 case MVT::f64:
248 MF.addLiveOut(X86::ST0);
249 break;
250 }
Chris Lattner88c8a232005-01-07 07:49:41 +0000251 return ArgValues;
252}
253
Chris Lattnerb52e0412005-01-08 19:28:19 +0000254std::pair<SDOperand, SDOperand>
Chris Lattner7ce7a8f2005-05-12 23:06:28 +0000255X86TargetLowering::LowerCCCCallTo(SDOperand Chain, const Type *RetTy,
256 bool isVarArg, SDOperand Callee,
257 ArgListTy &Args, SelectionDAG &DAG) {
Chris Lattner88c8a232005-01-07 07:49:41 +0000258 // Count how many bytes are to be pushed on the stack.
259 unsigned NumBytes = 0;
260
261 if (Args.empty()) {
262 // Save zero bytes.
Chris Lattner2dce7032005-05-12 23:24:06 +0000263 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
Chris Lattnerb52e0412005-01-08 19:28:19 +0000264 DAG.getConstant(0, getPointerTy()));
Chris Lattner88c8a232005-01-07 07:49:41 +0000265 } else {
266 for (unsigned i = 0, e = Args.size(); i != e; ++i)
267 switch (getValueType(Args[i].second)) {
268 default: assert(0 && "Unknown value type!");
269 case MVT::i1:
270 case MVT::i8:
271 case MVT::i16:
272 case MVT::i32:
273 case MVT::f32:
274 NumBytes += 4;
275 break;
276 case MVT::i64:
277 case MVT::f64:
278 NumBytes += 8;
279 break;
280 }
281
Chris Lattner2dce7032005-05-12 23:24:06 +0000282 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
Chris Lattnerb52e0412005-01-08 19:28:19 +0000283 DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattner88c8a232005-01-07 07:49:41 +0000284
285 // Arguments go on the stack in reverse order, as specified by the ABI.
286 unsigned ArgOffset = 0;
Chris Lattner720a62e2005-01-14 22:37:41 +0000287 SDOperand StackPtr = DAG.getCopyFromReg(X86::ESP, MVT::i32,
288 DAG.getEntryNode());
Chris Lattnerc78776d2005-01-21 19:46:38 +0000289 std::vector<SDOperand> Stores;
290
Chris Lattner88c8a232005-01-07 07:49:41 +0000291 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
Chris Lattner88c8a232005-01-07 07:49:41 +0000292 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
293 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
294
295 switch (getValueType(Args[i].second)) {
296 default: assert(0 && "Unexpected ValueType for argument!");
297 case MVT::i1:
298 case MVT::i8:
299 case MVT::i16:
300 // Promote the integer to 32 bits. If the input type is signed use a
301 // sign extend, otherwise use a zero extend.
302 if (Args[i].second->isSigned())
303 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
304 else
305 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
306
307 // FALL THROUGH
308 case MVT::i32:
309 case MVT::f32:
Chris Lattnerc78776d2005-01-21 19:46:38 +0000310 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
Chris Lattnerdaa064d2005-05-09 05:40:26 +0000311 Args[i].first, PtrOff,
312 DAG.getSrcValue(NULL)));
Chris Lattner88c8a232005-01-07 07:49:41 +0000313 ArgOffset += 4;
314 break;
315 case MVT::i64:
316 case MVT::f64:
Chris Lattnerc78776d2005-01-21 19:46:38 +0000317 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
Chris Lattnerdaa064d2005-05-09 05:40:26 +0000318 Args[i].first, PtrOff,
319 DAG.getSrcValue(NULL)));
Chris Lattner88c8a232005-01-07 07:49:41 +0000320 ArgOffset += 8;
321 break;
322 }
323 }
Chris Lattnerc78776d2005-01-21 19:46:38 +0000324 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, Stores);
Chris Lattner88c8a232005-01-07 07:49:41 +0000325 }
326
327 std::vector<MVT::ValueType> RetVals;
328 MVT::ValueType RetTyVT = getValueType(RetTy);
329 if (RetTyVT != MVT::isVoid)
330 RetVals.push_back(RetTyVT);
331 RetVals.push_back(MVT::Other);
332
Chris Lattnerb52e0412005-01-08 19:28:19 +0000333 SDOperand TheCall = SDOperand(DAG.getCall(RetVals, Chain, Callee), 0);
Chris Lattner313ddb52005-01-08 20:51:36 +0000334 Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
Chris Lattner2dce7032005-05-12 23:24:06 +0000335 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain,
Chris Lattner2e77db62005-05-13 18:50:42 +0000336 DAG.getConstant(NumBytes, getPointerTy()),
337 DAG.getConstant(0, getPointerTy()));
Chris Lattnerb52e0412005-01-08 19:28:19 +0000338 return std::make_pair(TheCall, Chain);
Chris Lattner88c8a232005-01-07 07:49:41 +0000339}
340
Chris Lattner9f59d282005-01-09 00:01:27 +0000341std::pair<SDOperand, SDOperand>
342X86TargetLowering::LowerVAStart(SDOperand Chain, SelectionDAG &DAG) {
343 // vastart just returns the address of the VarArgsFrameIndex slot.
344 return std::make_pair(DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32), Chain);
345}
346
347std::pair<SDOperand,SDOperand> X86TargetLowering::
348LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
349 const Type *ArgTy, SelectionDAG &DAG) {
350 MVT::ValueType ArgVT = getValueType(ArgTy);
351 SDOperand Result;
352 if (!isVANext) {
Chris Lattnerdaa064d2005-05-09 05:40:26 +0000353 Result = DAG.getLoad(ArgVT, DAG.getEntryNode(), VAList,
354 DAG.getSrcValue(NULL));
Chris Lattner9f59d282005-01-09 00:01:27 +0000355 } else {
356 unsigned Amt;
357 if (ArgVT == MVT::i32)
358 Amt = 4;
359 else {
360 assert((ArgVT == MVT::i64 || ArgVT == MVT::f64) &&
361 "Other types should have been promoted for varargs!");
362 Amt = 8;
363 }
364 Result = DAG.getNode(ISD::ADD, VAList.getValueType(), VAList,
365 DAG.getConstant(Amt, VAList.getValueType()));
366 }
367 return std::make_pair(Result, Chain);
368}
Misha Brukmanc88330a2005-04-21 23:38:14 +0000369
Chris Lattner7ce7a8f2005-05-12 23:06:28 +0000370//===----------------------------------------------------------------------===//
371// Fast Calling Convention implementation
372//===----------------------------------------------------------------------===//
373//
374// The X86 'fast' calling convention passes up to two integer arguments in
375// registers (an appropriate portion of EAX/EDX), passes arguments in C order,
376// and requires that the callee pop its arguments off the stack (allowing proper
377// tail calls), and has the same return value conventions as C calling convs.
378//
379// Note that this can be enhanced in the future to pass fp vals in registers
380// (when we have a global fp allocator) and do other tricks.
381//
Chris Lattner0b17b452005-05-13 07:38:09 +0000382
383/// AddLiveIn - This helper function adds the specified physical register to the
384/// MachineFunction as a live in value. It also creates a corresponding virtual
385/// register for it.
386static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg,
387 TargetRegisterClass *RC) {
388 assert(RC->contains(PReg) && "Not the correct regclass!");
389 unsigned VReg = MF.getSSARegMap()->createVirtualRegister(RC);
390 MF.addLiveIn(PReg, VReg);
391 return VReg;
392}
393
394
Chris Lattner7ce7a8f2005-05-12 23:06:28 +0000395std::vector<SDOperand>
396X86TargetLowering::LowerFastCCArguments(Function &F, SelectionDAG &DAG) {
397 std::vector<SDOperand> ArgValues;
398
399 MachineFunction &MF = DAG.getMachineFunction();
400 MachineFrameInfo *MFI = MF.getFrameInfo();
401
402 // Add DAG nodes to load the arguments... On entry to a function the stack
403 // frame looks like this:
404 //
405 // [ESP] -- return address
406 // [ESP + 4] -- first nonreg argument (leftmost lexically)
407 // [ESP + 8] -- second nonreg argument, if first argument is 4 bytes in size
408 // ...
409 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
410
411 // Keep track of the number of integer regs passed so far. This can be either
412 // 0 (neither EAX or EDX used), 1 (EAX is used) or 2 (EAX and EDX are both
413 // used).
414 unsigned NumIntRegs = 0;
415
416 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
417 MVT::ValueType ObjectVT = getValueType(I->getType());
418 unsigned ArgIncrement = 4;
419 unsigned ObjSize = 0;
420 SDOperand ArgValue;
421
422 switch (ObjectVT) {
423 default: assert(0 && "Unhandled argument type!");
424 case MVT::i1:
425 case MVT::i8:
426 if (NumIntRegs < 2) {
427 if (!I->use_empty()) {
Chris Lattner0b17b452005-05-13 07:38:09 +0000428 unsigned VReg = AddLiveIn(MF, NumIntRegs ? X86::DL : X86::AL,
429 X86::R8RegisterClass);
430 ArgValue = DAG.getCopyFromReg(VReg, MVT::i8, DAG.getRoot());
Chris Lattner7ce7a8f2005-05-12 23:06:28 +0000431 DAG.setRoot(ArgValue.getValue(1));
432 }
433 ++NumIntRegs;
434 break;
435 }
436
437 ObjSize = 1;
438 break;
439 case MVT::i16:
440 if (NumIntRegs < 2) {
441 if (!I->use_empty()) {
Chris Lattner0b17b452005-05-13 07:38:09 +0000442 unsigned VReg = AddLiveIn(MF, NumIntRegs ? X86::DX : X86::AX,
443 X86::R16RegisterClass);
444 ArgValue = DAG.getCopyFromReg(VReg, MVT::i16, DAG.getRoot());
Chris Lattner7ce7a8f2005-05-12 23:06:28 +0000445 DAG.setRoot(ArgValue.getValue(1));
446 }
447 ++NumIntRegs;
448 break;
449 }
450 ObjSize = 2;
451 break;
452 case MVT::i32:
453 if (NumIntRegs < 2) {
454 if (!I->use_empty()) {
Chris Lattner0b17b452005-05-13 07:38:09 +0000455 unsigned VReg = AddLiveIn(MF,NumIntRegs ? X86::EDX : X86::EAX,
456 X86::R32RegisterClass);
457 ArgValue = DAG.getCopyFromReg(VReg, MVT::i32, DAG.getRoot());
Chris Lattner7ce7a8f2005-05-12 23:06:28 +0000458 DAG.setRoot(ArgValue.getValue(1));
459 }
460 ++NumIntRegs;
461 break;
462 }
463 ObjSize = 4;
464 break;
465 case MVT::i64:
466 if (NumIntRegs == 0) {
467 if (!I->use_empty()) {
Chris Lattner0b17b452005-05-13 07:38:09 +0000468 unsigned BotReg = AddLiveIn(MF, X86::EAX, X86::R32RegisterClass);
469 unsigned TopReg = AddLiveIn(MF, X86::EDX, X86::R32RegisterClass);
Chris Lattner7ce7a8f2005-05-12 23:06:28 +0000470
Chris Lattner0b17b452005-05-13 07:38:09 +0000471 SDOperand Low=DAG.getCopyFromReg(BotReg, MVT::i32, DAG.getRoot());
472 SDOperand Hi =DAG.getCopyFromReg(TopReg, MVT::i32, Low.getValue(1));
Chris Lattner7ce7a8f2005-05-12 23:06:28 +0000473 DAG.setRoot(Hi.getValue(1));
474
475 ArgValue = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Low, Hi);
476 }
477 NumIntRegs = 2;
478 break;
479 } else if (NumIntRegs == 1) {
480 if (!I->use_empty()) {
Chris Lattner0b17b452005-05-13 07:38:09 +0000481 unsigned BotReg = AddLiveIn(MF, X86::EDX, X86::R32RegisterClass);
482 SDOperand Low = DAG.getCopyFromReg(BotReg, MVT::i32, DAG.getRoot());
Chris Lattner7ce7a8f2005-05-12 23:06:28 +0000483 DAG.setRoot(Low.getValue(1));
484
485 // Load the high part from memory.
486 // Create the frame index object for this incoming parameter...
487 int FI = MFI->CreateFixedObject(4, ArgOffset);
488 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
489 SDOperand Hi = DAG.getLoad(MVT::i32, DAG.getEntryNode(), FIN,
490 DAG.getSrcValue(NULL));
491 ArgValue = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Low, Hi);
492 }
493 ArgOffset += 4;
494 NumIntRegs = 2;
495 break;
496 }
497 ObjSize = ArgIncrement = 8;
498 break;
499 case MVT::f32: ObjSize = 4; break;
500 case MVT::f64: ObjSize = ArgIncrement = 8; break;
501 }
502
503 // Don't codegen dead arguments. FIXME: remove this check when we can nuke
504 // dead loads.
505 if (ObjSize && !I->use_empty()) {
506 // Create the frame index object for this incoming parameter...
507 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
508
509 // Create the SelectionDAG nodes corresponding to a load from this
510 // parameter.
511 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
512
513 ArgValue = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
514 DAG.getSrcValue(NULL));
515 } else if (ArgValue.Val == 0) {
516 if (MVT::isInteger(ObjectVT))
517 ArgValue = DAG.getConstant(0, ObjectVT);
518 else
519 ArgValue = DAG.getConstantFP(0, ObjectVT);
520 }
521 ArgValues.push_back(ArgValue);
522
523 if (ObjSize)
524 ArgOffset += ArgIncrement; // Move on to the next argument.
525 }
526
527 // If the function takes variable number of arguments, make a frame index for
528 // the start of the first vararg value... for expansion of llvm.va_start.
529 if (F.isVarArg())
530 VarArgsFrameIndex = MFI->CreateFixedObject(1, ArgOffset);
531 ReturnAddrIndex = 0; // No return address slot generated yet.
532
533 // Finally, inform the code generator which regs we return values in.
534 switch (getValueType(F.getReturnType())) {
535 default: assert(0 && "Unknown type!");
536 case MVT::isVoid: break;
537 case MVT::i1:
538 case MVT::i8:
539 case MVT::i16:
540 case MVT::i32:
541 MF.addLiveOut(X86::EAX);
542 break;
543 case MVT::i64:
544 MF.addLiveOut(X86::EAX);
545 MF.addLiveOut(X86::EDX);
546 break;
547 case MVT::f32:
548 case MVT::f64:
549 MF.addLiveOut(X86::ST0);
550 break;
551 }
552 return ArgValues;
553}
554
555std::pair<SDOperand, SDOperand>
556X86TargetLowering::LowerFastCCCallTo(SDOperand Chain, const Type *RetTy,
557 SDOperand Callee,
558 ArgListTy &Args, SelectionDAG &DAG) {
559 // Count how many bytes are to be pushed on the stack.
560 unsigned NumBytes = 0;
561
562 // Keep track of the number of integer regs passed so far. This can be either
563 // 0 (neither EAX or EDX used), 1 (EAX is used) or 2 (EAX and EDX are both
564 // used).
565 unsigned NumIntRegs = 0;
566
567 for (unsigned i = 0, e = Args.size(); i != e; ++i)
568 switch (getValueType(Args[i].second)) {
569 default: assert(0 && "Unknown value type!");
570 case MVT::i1:
571 case MVT::i8:
572 case MVT::i16:
573 case MVT::i32:
574 if (NumIntRegs < 2) {
575 ++NumIntRegs;
576 break;
577 }
578 // fall through
579 case MVT::f32:
580 NumBytes += 4;
581 break;
582 case MVT::i64:
583 if (NumIntRegs == 0) {
584 NumIntRegs = 2;
585 break;
586 } else if (NumIntRegs == 1) {
587 NumIntRegs = 2;
588 NumBytes += 4;
589 break;
590 }
591
592 // fall through
593 case MVT::f64:
594 NumBytes += 8;
595 break;
596 }
597
Chris Lattner2dce7032005-05-12 23:24:06 +0000598 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
Chris Lattner7ce7a8f2005-05-12 23:06:28 +0000599 DAG.getConstant(NumBytes, getPointerTy()));
600
601 // Arguments go on the stack in reverse order, as specified by the ABI.
602 unsigned ArgOffset = 0;
603 SDOperand StackPtr = DAG.getCopyFromReg(X86::ESP, MVT::i32,
604 DAG.getEntryNode());
605 NumIntRegs = 0;
606 std::vector<SDOperand> Stores;
607 std::vector<SDOperand> RegValuesToPass;
608 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
609 switch (getValueType(Args[i].second)) {
610 default: assert(0 && "Unexpected ValueType for argument!");
611 case MVT::i1:
612 case MVT::i8:
613 case MVT::i16:
614 case MVT::i32:
615 if (NumIntRegs < 2) {
616 RegValuesToPass.push_back(Args[i].first);
617 ++NumIntRegs;
618 break;
619 }
620 // Fall through
621 case MVT::f32: {
622 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
623 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
624 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
625 Args[i].first, PtrOff,
626 DAG.getSrcValue(NULL)));
627 ArgOffset += 4;
628 break;
629 }
630 case MVT::i64:
631 if (NumIntRegs < 2) { // Can pass part of it in regs?
632 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
633 Args[i].first, DAG.getConstant(1, MVT::i32));
634 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
635 Args[i].first, DAG.getConstant(0, MVT::i32));
636 RegValuesToPass.push_back(Lo);
637 ++NumIntRegs;
638 if (NumIntRegs < 2) { // Pass both parts in regs?
639 RegValuesToPass.push_back(Hi);
640 ++NumIntRegs;
641 } else {
642 // Pass the high part in memory.
643 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
644 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
645 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
646 Args[i].first, PtrOff,
647 DAG.getSrcValue(NULL)));
648 ArgOffset += 4;
649 }
650 break;
651 }
652 // Fall through
653 case MVT::f64:
654 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
655 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
656 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
657 Args[i].first, PtrOff,
658 DAG.getSrcValue(NULL)));
659 ArgOffset += 8;
660 break;
661 }
662 }
663 if (!Stores.empty())
664 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, Stores);
665
666 std::vector<MVT::ValueType> RetVals;
667 MVT::ValueType RetTyVT = getValueType(RetTy);
668 if (RetTyVT != MVT::isVoid)
669 RetVals.push_back(RetTyVT);
670 RetVals.push_back(MVT::Other);
671
672 SDOperand TheCall = SDOperand(DAG.getCall(RetVals, Chain, Callee,
673 RegValuesToPass), 0);
674 Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
Chris Lattner2dce7032005-05-12 23:24:06 +0000675 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain,
Chris Lattner2e77db62005-05-13 18:50:42 +0000676 DAG.getConstant(NumBytes, getPointerTy()),
677 // The callee pops the arguments off the stack.
678 DAG.getConstant(ArgOffset, getPointerTy()));
Chris Lattner7ce7a8f2005-05-12 23:06:28 +0000679 return std::make_pair(TheCall, Chain);
680}
681
682
683
Chris Lattner9f59d282005-01-09 00:01:27 +0000684
685std::pair<SDOperand, SDOperand> X86TargetLowering::
686LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
687 SelectionDAG &DAG) {
688 SDOperand Result;
689 if (Depth) // Depths > 0 not supported yet!
690 Result = DAG.getConstant(0, getPointerTy());
691 else {
692 if (ReturnAddrIndex == 0) {
693 // Set up a frame object for the return address.
694 MachineFunction &MF = DAG.getMachineFunction();
695 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(4, -4);
696 }
Misha Brukmanc88330a2005-04-21 23:38:14 +0000697
Chris Lattner9f59d282005-01-09 00:01:27 +0000698 SDOperand RetAddrFI = DAG.getFrameIndex(ReturnAddrIndex, MVT::i32);
699
700 if (!isFrameAddress)
701 // Just load the return address
Chris Lattner7ce7a8f2005-05-12 23:06:28 +0000702 Result = DAG.getLoad(MVT::i32, DAG.getEntryNode(), RetAddrFI,
703 DAG.getSrcValue(NULL));
Chris Lattner9f59d282005-01-09 00:01:27 +0000704 else
705 Result = DAG.getNode(ISD::SUB, MVT::i32, RetAddrFI,
706 DAG.getConstant(4, MVT::i32));
707 }
708 return std::make_pair(Result, Chain);
709}
Chris Lattner88c8a232005-01-07 07:49:41 +0000710
711
Chris Lattnera7acdda2005-01-18 01:06:26 +0000712namespace {
713 /// X86ISelAddressMode - This corresponds to X86AddressMode, but uses
714 /// SDOperand's instead of register numbers for the leaves of the matched
715 /// tree.
716 struct X86ISelAddressMode {
717 enum {
718 RegBase,
719 FrameIndexBase,
720 } BaseType;
Misha Brukmanc88330a2005-04-21 23:38:14 +0000721
Chris Lattnera7acdda2005-01-18 01:06:26 +0000722 struct { // This is really a union, discriminated by BaseType!
723 SDOperand Reg;
724 int FrameIndex;
725 } Base;
Misha Brukmanc88330a2005-04-21 23:38:14 +0000726
Chris Lattnera7acdda2005-01-18 01:06:26 +0000727 unsigned Scale;
728 SDOperand IndexReg;
729 unsigned Disp;
730 GlobalValue *GV;
Misha Brukmanc88330a2005-04-21 23:38:14 +0000731
Chris Lattnera7acdda2005-01-18 01:06:26 +0000732 X86ISelAddressMode()
733 : BaseType(RegBase), Scale(1), IndexReg(), Disp(), GV(0) {
734 }
735 };
736}
Chris Lattner88c8a232005-01-07 07:49:41 +0000737
738
739namespace {
740 Statistic<>
741 NumFPKill("x86-codegen", "Number of FP_REG_KILL instructions added");
742
743 //===--------------------------------------------------------------------===//
744 /// ISel - X86 specific code to select X86 machine instructions for
745 /// SelectionDAG operations.
746 ///
747 class ISel : public SelectionDAGISel {
748 /// ContainsFPCode - Every instruction we select that uses or defines a FP
749 /// register should set this to true.
750 bool ContainsFPCode;
751
752 /// X86Lowering - This object fully describes how to lower LLVM code to an
753 /// X86-specific SelectionDAG.
754 X86TargetLowering X86Lowering;
755
Chris Lattner0d1f82a2005-01-11 03:11:44 +0000756 /// RegPressureMap - This keeps an approximate count of the number of
757 /// registers required to evaluate each node in the graph.
758 std::map<SDNode*, unsigned> RegPressureMap;
Chris Lattner88c8a232005-01-07 07:49:41 +0000759
760 /// ExprMap - As shared expressions are codegen'd, we keep track of which
761 /// vreg the value is produced in, so we only emit one copy of each compiled
762 /// tree.
763 std::map<SDOperand, unsigned> ExprMap;
Chris Lattner88c8a232005-01-07 07:49:41 +0000764
765 public:
766 ISel(TargetMachine &TM) : SelectionDAGISel(X86Lowering), X86Lowering(TM) {
767 }
768
Chris Lattnere1e844c2005-01-21 21:35:14 +0000769 virtual const char *getPassName() const {
770 return "X86 Pattern Instruction Selection";
771 }
772
Chris Lattner0d1f82a2005-01-11 03:11:44 +0000773 unsigned getRegPressure(SDOperand O) {
774 return RegPressureMap[O.Val];
775 }
776 unsigned ComputeRegPressure(SDOperand O);
777
Chris Lattner88c8a232005-01-07 07:49:41 +0000778 /// InstructionSelectBasicBlock - This callback is invoked by
779 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
Chris Lattner6fba62d62005-01-12 04:21:28 +0000780 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG);
Chris Lattner88c8a232005-01-07 07:49:41 +0000781
Chris Lattner0b17b452005-05-13 07:38:09 +0000782 virtual void EmitFunctionEntryCode(Function &Fn, MachineFunction &MF);
783
Chris Lattner30607ec2005-01-25 20:03:11 +0000784 bool isFoldableLoad(SDOperand Op, SDOperand OtherOp,
785 bool FloatPromoteOk = false);
Chris Lattner62b22422005-01-11 21:19:59 +0000786 void EmitFoldedLoad(SDOperand Op, X86AddressMode &AM);
Chris Lattner96113fd2005-01-17 19:25:26 +0000787 bool TryToFoldLoadOpStore(SDNode *Node);
Chris Lattner62b22422005-01-11 21:19:59 +0000788
Chris Lattner29f58192005-01-19 07:37:26 +0000789 bool EmitOrOpOp(SDOperand Op1, SDOperand Op2, unsigned DestReg);
Chris Lattner3be6cd52005-01-17 01:34:14 +0000790 void EmitCMP(SDOperand LHS, SDOperand RHS, bool isOnlyUse);
Chris Lattner37ed2852005-01-11 04:06:27 +0000791 bool EmitBranchCC(MachineBasicBlock *Dest, SDOperand Chain, SDOperand Cond);
Chris Lattner1d13a922005-01-10 22:10:13 +0000792 void EmitSelectCC(SDOperand Cond, MVT::ValueType SVT,
793 unsigned RTrue, unsigned RFalse, unsigned RDest);
Chris Lattner88c8a232005-01-07 07:49:41 +0000794 unsigned SelectExpr(SDOperand N);
Chris Lattnera7acdda2005-01-18 01:06:26 +0000795
796 X86AddressMode SelectAddrExprs(const X86ISelAddressMode &IAM);
797 bool MatchAddress(SDOperand N, X86ISelAddressMode &AM);
798 void SelectAddress(SDOperand N, X86AddressMode &AM);
Chris Lattner88c8a232005-01-07 07:49:41 +0000799 void Select(SDOperand N);
800 };
801}
802
Chris Lattnerd8145bc2005-05-10 03:53:18 +0000803/// EmitSpecialCodeForMain - Emit any code that needs to be executed only in
804/// the main function.
805static void EmitSpecialCodeForMain(MachineBasicBlock *BB,
806 MachineFrameInfo *MFI) {
807 // Switch the FPU to 64-bit precision mode for better compatibility and speed.
808 int CWFrameIdx = MFI->CreateStackObject(2, 2);
809 addFrameReference(BuildMI(BB, X86::FNSTCW16m, 4), CWFrameIdx);
810
811 // Set the high part to be 64-bit precision.
812 addFrameReference(BuildMI(BB, X86::MOV8mi, 5),
813 CWFrameIdx, 1).addImm(2);
814
815 // Reload the modified control word now.
816 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
817}
818
Chris Lattner0b17b452005-05-13 07:38:09 +0000819void ISel::EmitFunctionEntryCode(Function &Fn, MachineFunction &MF) {
820 // If this function has live-in values, emit the copies from pregs to vregs at
821 // the top of the function, before anything else.
822 MachineBasicBlock *BB = MF.begin();
823 if (MF.livein_begin() != MF.livein_end()) {
824 SSARegMap *RegMap = MF.getSSARegMap();
825 for (MachineFunction::livein_iterator LI = MF.livein_begin(),
826 E = MF.livein_end(); LI != E; ++LI) {
827 const TargetRegisterClass *RC = RegMap->getRegClass(LI->second);
828 if (RC == X86::R8RegisterClass) {
829 BuildMI(BB, X86::MOV8rr, 1, LI->second).addReg(LI->first);
830 } else if (RC == X86::R16RegisterClass) {
831 BuildMI(BB, X86::MOV16rr, 1, LI->second).addReg(LI->first);
832 } else if (RC == X86::R32RegisterClass) {
833 BuildMI(BB, X86::MOV32rr, 1, LI->second).addReg(LI->first);
834 } else if (RC == X86::RFPRegisterClass) {
835 BuildMI(BB, X86::FpMOV, 1, LI->second).addReg(LI->first);
836 } else {
837 assert(0 && "Unknown regclass!");
838 }
839 }
840 }
841
842
843 // If this is main, emit special code for main.
844 if (Fn.hasExternalLinkage() && Fn.getName() == "main")
845 EmitSpecialCodeForMain(BB, MF.getFrameInfo());
846}
847
848
Chris Lattner6fba62d62005-01-12 04:21:28 +0000849/// InstructionSelectBasicBlock - This callback is invoked by SelectionDAGISel
850/// when it has created a SelectionDAG for us to codegen.
851void ISel::InstructionSelectBasicBlock(SelectionDAG &DAG) {
852 // While we're doing this, keep track of whether we see any FP code for
853 // FP_REG_KILL insertion.
854 ContainsFPCode = false;
Chris Lattnerd8145bc2005-05-10 03:53:18 +0000855 MachineFunction *MF = BB->getParent();
Chris Lattner6fba62d62005-01-12 04:21:28 +0000856
857 // Scan the PHI nodes that already are inserted into this basic block. If any
858 // of them is a PHI of a floating point value, we need to insert an
859 // FP_REG_KILL.
Chris Lattnerd8145bc2005-05-10 03:53:18 +0000860 SSARegMap *RegMap = MF->getSSARegMap();
Chris Lattner0b17b452005-05-13 07:38:09 +0000861 if (BB != MF->begin())
862 for (MachineBasicBlock::iterator I = BB->begin(), E = BB->end();
863 I != E; ++I) {
864 assert(I->getOpcode() == X86::PHI &&
865 "Isn't just PHI nodes?");
866 if (RegMap->getRegClass(I->getOperand(0).getReg()) ==
867 X86::RFPRegisterClass) {
868 ContainsFPCode = true;
869 break;
870 }
Chris Lattner6fba62d62005-01-12 04:21:28 +0000871 }
Chris Lattnerd8145bc2005-05-10 03:53:18 +0000872
Chris Lattner6fba62d62005-01-12 04:21:28 +0000873 // Compute the RegPressureMap, which is an approximation for the number of
874 // registers required to compute each node.
875 ComputeRegPressure(DAG.getRoot());
876
877 // Codegen the basic block.
878 Select(DAG.getRoot());
879
880 // Finally, look at all of the successors of this block. If any contain a PHI
881 // node of FP type, we need to insert an FP_REG_KILL in this block.
882 for (MachineBasicBlock::succ_iterator SI = BB->succ_begin(),
883 E = BB->succ_end(); SI != E && !ContainsFPCode; ++SI)
884 for (MachineBasicBlock::iterator I = (*SI)->begin(), E = (*SI)->end();
885 I != E && I->getOpcode() == X86::PHI; ++I) {
886 if (RegMap->getRegClass(I->getOperand(0).getReg()) ==
887 X86::RFPRegisterClass) {
888 ContainsFPCode = true;
889 break;
890 }
891 }
Misha Brukmanc88330a2005-04-21 23:38:14 +0000892
Chris Lattner6972c312005-05-09 03:36:39 +0000893 // Final check, check LLVM BB's that are successors to the LLVM BB
894 // corresponding to BB for FP PHI nodes.
895 const BasicBlock *LLVMBB = BB->getBasicBlock();
896 const PHINode *PN;
897 if (!ContainsFPCode)
898 for (succ_const_iterator SI = succ_begin(LLVMBB), E = succ_end(LLVMBB);
899 SI != E && !ContainsFPCode; ++SI)
900 for (BasicBlock::const_iterator II = SI->begin();
901 (PN = dyn_cast<PHINode>(II)); ++II)
902 if (PN->getType()->isFloatingPoint()) {
903 ContainsFPCode = true;
904 break;
905 }
906
907
Chris Lattner6fba62d62005-01-12 04:21:28 +0000908 // Insert FP_REG_KILL instructions into basic blocks that need them. This
909 // only occurs due to the floating point stackifier not being aggressive
910 // enough to handle arbitrary global stackification.
911 //
912 // Currently we insert an FP_REG_KILL instruction into each block that uses or
913 // defines a floating point virtual register.
914 //
915 // When the global register allocators (like linear scan) finally update live
916 // variable analysis, we can keep floating point values in registers across
917 // basic blocks. This will be a huge win, but we are waiting on the global
918 // allocators before we can do this.
919 //
Chris Lattner472a2652005-03-30 01:10:00 +0000920 if (ContainsFPCode) {
Chris Lattner6fba62d62005-01-12 04:21:28 +0000921 BuildMI(*BB, BB->getFirstTerminator(), X86::FP_REG_KILL, 0);
922 ++NumFPKill;
923 }
Misha Brukmanc88330a2005-04-21 23:38:14 +0000924
Chris Lattner6fba62d62005-01-12 04:21:28 +0000925 // Clear state used for selection.
926 ExprMap.clear();
Chris Lattner6fba62d62005-01-12 04:21:28 +0000927 RegPressureMap.clear();
928}
929
930
Chris Lattner0d1f82a2005-01-11 03:11:44 +0000931// ComputeRegPressure - Compute the RegPressureMap, which is an approximation
932// for the number of registers required to compute each node. This is basically
933// computing a generalized form of the Sethi-Ullman number for each node.
934unsigned ISel::ComputeRegPressure(SDOperand O) {
935 SDNode *N = O.Val;
936 unsigned &Result = RegPressureMap[N];
937 if (Result) return Result;
938
Chris Lattner8fea42b2005-01-11 03:37:59 +0000939 // FIXME: Should operations like CALL (which clobber lots o regs) have a
940 // higher fixed cost??
941
Chris Lattner8aa10fc2005-01-11 22:29:12 +0000942 if (N->getNumOperands() == 0) {
943 Result = 1;
944 } else {
945 unsigned MaxRegUse = 0;
946 unsigned NumExtraMaxRegUsers = 0;
947 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
948 unsigned Regs;
949 if (N->getOperand(i).getOpcode() == ISD::Constant)
950 Regs = 0;
951 else
952 Regs = ComputeRegPressure(N->getOperand(i));
953 if (Regs > MaxRegUse) {
954 MaxRegUse = Regs;
955 NumExtraMaxRegUsers = 0;
956 } else if (Regs == MaxRegUse &&
957 N->getOperand(i).getValueType() != MVT::Other) {
958 ++NumExtraMaxRegUsers;
959 }
Chris Lattner0d1f82a2005-01-11 03:11:44 +0000960 }
Chris Lattnerca318ed2005-01-17 22:56:09 +0000961
962 if (O.getOpcode() != ISD::TokenFactor)
963 Result = MaxRegUse+NumExtraMaxRegUsers;
964 else
Chris Lattnera5d137f2005-01-17 23:02:13 +0000965 Result = MaxRegUse == 1 ? 0 : MaxRegUse-1;
Chris Lattner8aa10fc2005-01-11 22:29:12 +0000966 }
Chris Lattnerb7fe57a2005-01-12 02:19:06 +0000967
Chris Lattner75bac9f2005-01-11 23:21:30 +0000968 //std::cerr << " WEIGHT: " << Result << " "; N->dump(); std::cerr << "\n";
Chris Lattner8aa10fc2005-01-11 22:29:12 +0000969 return Result;
Chris Lattner0d1f82a2005-01-11 03:11:44 +0000970}
971
Chris Lattner5b04f332005-01-20 16:50:16 +0000972/// NodeTransitivelyUsesValue - Return true if N or any of its uses uses Op.
973/// The DAG cannot have cycles in it, by definition, so the visited set is not
974/// needed to prevent infinite loops. The DAG CAN, however, have unbounded
975/// reuse, so it prevents exponential cases.
976///
977static bool NodeTransitivelyUsesValue(SDOperand N, SDOperand Op,
978 std::set<SDNode*> &Visited) {
979 if (N == Op) return true; // Found it.
980 SDNode *Node = N.Val;
Chris Lattnere70eb9da2005-01-21 21:43:02 +0000981 if (Node->getNumOperands() == 0 || // Leaf?
982 Node->getNodeDepth() <= Op.getNodeDepth()) return false; // Can't find it?
Chris Lattner5b04f332005-01-20 16:50:16 +0000983 if (!Visited.insert(Node).second) return false; // Already visited?
984
985 // Recurse for the first N-1 operands.
986 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i)
987 if (NodeTransitivelyUsesValue(Node->getOperand(i), Op, Visited))
988 return true;
989
990 // Tail recurse for the last operand.
991 return NodeTransitivelyUsesValue(Node->getOperand(0), Op, Visited);
992}
993
Chris Lattnera7acdda2005-01-18 01:06:26 +0000994X86AddressMode ISel::SelectAddrExprs(const X86ISelAddressMode &IAM) {
995 X86AddressMode Result;
996
997 // If we need to emit two register operands, emit the one with the highest
998 // register pressure first.
999 if (IAM.BaseType == X86ISelAddressMode::RegBase &&
1000 IAM.Base.Reg.Val && IAM.IndexReg.Val) {
Chris Lattner5b04f332005-01-20 16:50:16 +00001001 bool EmitBaseThenIndex;
Chris Lattnera7acdda2005-01-18 01:06:26 +00001002 if (getRegPressure(IAM.Base.Reg) > getRegPressure(IAM.IndexReg)) {
Chris Lattner5b04f332005-01-20 16:50:16 +00001003 std::set<SDNode*> Visited;
1004 EmitBaseThenIndex = true;
1005 // If Base ends up pointing to Index, we must emit index first. This is
1006 // because of the way we fold loads, we may end up doing bad things with
1007 // the folded add.
1008 if (NodeTransitivelyUsesValue(IAM.Base.Reg, IAM.IndexReg, Visited))
1009 EmitBaseThenIndex = false;
1010 } else {
1011 std::set<SDNode*> Visited;
1012 EmitBaseThenIndex = false;
1013 // If Base ends up pointing to Index, we must emit index first. This is
1014 // because of the way we fold loads, we may end up doing bad things with
1015 // the folded add.
1016 if (NodeTransitivelyUsesValue(IAM.IndexReg, IAM.Base.Reg, Visited))
1017 EmitBaseThenIndex = true;
1018 }
1019
1020 if (EmitBaseThenIndex) {
Chris Lattnera7acdda2005-01-18 01:06:26 +00001021 Result.Base.Reg = SelectExpr(IAM.Base.Reg);
1022 Result.IndexReg = SelectExpr(IAM.IndexReg);
1023 } else {
1024 Result.IndexReg = SelectExpr(IAM.IndexReg);
1025 Result.Base.Reg = SelectExpr(IAM.Base.Reg);
1026 }
Chris Lattner5b04f332005-01-20 16:50:16 +00001027
Chris Lattnera7acdda2005-01-18 01:06:26 +00001028 } else if (IAM.BaseType == X86ISelAddressMode::RegBase && IAM.Base.Reg.Val) {
1029 Result.Base.Reg = SelectExpr(IAM.Base.Reg);
1030 } else if (IAM.IndexReg.Val) {
1031 Result.IndexReg = SelectExpr(IAM.IndexReg);
1032 }
Misha Brukmanc88330a2005-04-21 23:38:14 +00001033
Chris Lattnera7acdda2005-01-18 01:06:26 +00001034 switch (IAM.BaseType) {
1035 case X86ISelAddressMode::RegBase:
1036 Result.BaseType = X86AddressMode::RegBase;
1037 break;
1038 case X86ISelAddressMode::FrameIndexBase:
1039 Result.BaseType = X86AddressMode::FrameIndexBase;
1040 Result.Base.FrameIndex = IAM.Base.FrameIndex;
1041 break;
1042 default:
1043 assert(0 && "Unknown base type!");
1044 break;
1045 }
1046 Result.Scale = IAM.Scale;
1047 Result.Disp = IAM.Disp;
1048 Result.GV = IAM.GV;
1049 return Result;
1050}
1051
1052/// SelectAddress - Pattern match the maximal addressing mode for this node and
1053/// emit all of the leaf registers.
1054void ISel::SelectAddress(SDOperand N, X86AddressMode &AM) {
1055 X86ISelAddressMode IAM;
1056 MatchAddress(N, IAM);
1057 AM = SelectAddrExprs(IAM);
1058}
1059
1060/// MatchAddress - Add the specified node to the specified addressing mode,
1061/// returning true if it cannot be done. This just pattern matches for the
1062/// addressing mode, it does not cause any code to be emitted. For that, use
1063/// SelectAddress.
1064bool ISel::MatchAddress(SDOperand N, X86ISelAddressMode &AM) {
Chris Lattner88c8a232005-01-07 07:49:41 +00001065 switch (N.getOpcode()) {
1066 default: break;
1067 case ISD::FrameIndex:
Chris Lattnera7acdda2005-01-18 01:06:26 +00001068 if (AM.BaseType == X86ISelAddressMode::RegBase && AM.Base.Reg.Val == 0) {
1069 AM.BaseType = X86ISelAddressMode::FrameIndexBase;
Chris Lattner88c8a232005-01-07 07:49:41 +00001070 AM.Base.FrameIndex = cast<FrameIndexSDNode>(N)->getIndex();
1071 return false;
1072 }
1073 break;
1074 case ISD::GlobalAddress:
1075 if (AM.GV == 0) {
1076 AM.GV = cast<GlobalAddressSDNode>(N)->getGlobal();
1077 return false;
1078 }
1079 break;
1080 case ISD::Constant:
1081 AM.Disp += cast<ConstantSDNode>(N)->getValue();
1082 return false;
1083 case ISD::SHL:
Chris Lattner3676cd62005-01-13 05:53:16 +00001084 // We might have folded the load into this shift, so don't regen the value
1085 // if so.
1086 if (ExprMap.count(N)) break;
1087
Chris Lattnera7acdda2005-01-18 01:06:26 +00001088 if (AM.IndexReg.Val == 0 && AM.Scale == 1)
Chris Lattner88c8a232005-01-07 07:49:41 +00001089 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1))) {
1090 unsigned Val = CN->getValue();
1091 if (Val == 1 || Val == 2 || Val == 3) {
1092 AM.Scale = 1 << Val;
Chris Lattnerb74ec4c2005-01-11 06:36:20 +00001093 SDOperand ShVal = N.Val->getOperand(0);
1094
1095 // Okay, we know that we have a scale by now. However, if the scaled
1096 // value is an add of something and a constant, we can fold the
1097 // constant into the disp field here.
Chris Lattnered246ec2005-01-18 04:18:32 +00001098 if (ShVal.Val->getOpcode() == ISD::ADD && ShVal.hasOneUse() &&
Chris Lattnerb74ec4c2005-01-11 06:36:20 +00001099 isa<ConstantSDNode>(ShVal.Val->getOperand(1))) {
Chris Lattnera7acdda2005-01-18 01:06:26 +00001100 AM.IndexReg = ShVal.Val->getOperand(0);
Chris Lattnerb74ec4c2005-01-11 06:36:20 +00001101 ConstantSDNode *AddVal =
1102 cast<ConstantSDNode>(ShVal.Val->getOperand(1));
1103 AM.Disp += AddVal->getValue() << Val;
Chris Lattner3676cd62005-01-13 05:53:16 +00001104 } else {
Chris Lattnera7acdda2005-01-18 01:06:26 +00001105 AM.IndexReg = ShVal;
Chris Lattnerb74ec4c2005-01-11 06:36:20 +00001106 }
Chris Lattner88c8a232005-01-07 07:49:41 +00001107 return false;
1108 }
1109 }
1110 break;
Chris Lattner8cf9cda2005-01-11 19:37:02 +00001111 case ISD::MUL:
Chris Lattner3676cd62005-01-13 05:53:16 +00001112 // We might have folded the load into this mul, so don't regen the value if
1113 // so.
1114 if (ExprMap.count(N)) break;
1115
Chris Lattner8cf9cda2005-01-11 19:37:02 +00001116 // X*[3,5,9] -> X+X*[2,4,8]
Chris Lattnera7acdda2005-01-18 01:06:26 +00001117 if (AM.IndexReg.Val == 0 && AM.BaseType == X86ISelAddressMode::RegBase &&
1118 AM.Base.Reg.Val == 0)
Chris Lattner8cf9cda2005-01-11 19:37:02 +00001119 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1)))
1120 if (CN->getValue() == 3 || CN->getValue() == 5 || CN->getValue() == 9) {
1121 AM.Scale = unsigned(CN->getValue())-1;
1122
1123 SDOperand MulVal = N.Val->getOperand(0);
Chris Lattnera7acdda2005-01-18 01:06:26 +00001124 SDOperand Reg;
Chris Lattner8cf9cda2005-01-11 19:37:02 +00001125
1126 // Okay, we know that we have a scale by now. However, if the scaled
1127 // value is an add of something and a constant, we can fold the
1128 // constant into the disp field here.
Chris Lattnered246ec2005-01-18 04:18:32 +00001129 if (MulVal.Val->getOpcode() == ISD::ADD && MulVal.hasOneUse() &&
Chris Lattner8cf9cda2005-01-11 19:37:02 +00001130 isa<ConstantSDNode>(MulVal.Val->getOperand(1))) {
Chris Lattnera7acdda2005-01-18 01:06:26 +00001131 Reg = MulVal.Val->getOperand(0);
Chris Lattner8cf9cda2005-01-11 19:37:02 +00001132 ConstantSDNode *AddVal =
1133 cast<ConstantSDNode>(MulVal.Val->getOperand(1));
1134 AM.Disp += AddVal->getValue() * CN->getValue();
Misha Brukmanc88330a2005-04-21 23:38:14 +00001135 } else {
Chris Lattnera7acdda2005-01-18 01:06:26 +00001136 Reg = N.Val->getOperand(0);
Chris Lattner8cf9cda2005-01-11 19:37:02 +00001137 }
1138
1139 AM.IndexReg = AM.Base.Reg = Reg;
1140 return false;
1141 }
1142 break;
Chris Lattner88c8a232005-01-07 07:49:41 +00001143
1144 case ISD::ADD: {
Chris Lattner3676cd62005-01-13 05:53:16 +00001145 // We might have folded the load into this mul, so don't regen the value if
1146 // so.
1147 if (ExprMap.count(N)) break;
1148
Chris Lattnera7acdda2005-01-18 01:06:26 +00001149 X86ISelAddressMode Backup = AM;
1150 if (!MatchAddress(N.Val->getOperand(0), AM) &&
1151 !MatchAddress(N.Val->getOperand(1), AM))
Chris Lattner88c8a232005-01-07 07:49:41 +00001152 return false;
1153 AM = Backup;
Chris Lattnera7acdda2005-01-18 01:06:26 +00001154 if (!MatchAddress(N.Val->getOperand(1), AM) &&
1155 !MatchAddress(N.Val->getOperand(0), AM))
Chris Lattner17553602005-01-12 18:08:53 +00001156 return false;
1157 AM = Backup;
Chris Lattner88c8a232005-01-07 07:49:41 +00001158 break;
1159 }
1160 }
1161
Chris Lattner378262d2005-01-11 04:40:19 +00001162 // Is the base register already occupied?
Chris Lattnera7acdda2005-01-18 01:06:26 +00001163 if (AM.BaseType != X86ISelAddressMode::RegBase || AM.Base.Reg.Val) {
Chris Lattner378262d2005-01-11 04:40:19 +00001164 // If so, check to see if the scale index register is set.
Chris Lattnera7acdda2005-01-18 01:06:26 +00001165 if (AM.IndexReg.Val == 0) {
1166 AM.IndexReg = N;
Chris Lattner378262d2005-01-11 04:40:19 +00001167 AM.Scale = 1;
1168 return false;
1169 }
1170
1171 // Otherwise, we cannot select it.
Chris Lattner88c8a232005-01-07 07:49:41 +00001172 return true;
Chris Lattner378262d2005-01-11 04:40:19 +00001173 }
Chris Lattner88c8a232005-01-07 07:49:41 +00001174
1175 // Default, generate it as a register.
Chris Lattnera7acdda2005-01-18 01:06:26 +00001176 AM.BaseType = X86ISelAddressMode::RegBase;
1177 AM.Base.Reg = N;
Chris Lattner88c8a232005-01-07 07:49:41 +00001178 return false;
1179}
1180
1181/// Emit2SetCCsAndLogical - Emit the following sequence of instructions,
1182/// assuming that the temporary registers are in the 8-bit register class.
1183///
1184/// Tmp1 = setcc1
1185/// Tmp2 = setcc2
1186/// DestReg = logicalop Tmp1, Tmp2
1187///
1188static void Emit2SetCCsAndLogical(MachineBasicBlock *BB, unsigned SetCC1,
1189 unsigned SetCC2, unsigned LogicalOp,
1190 unsigned DestReg) {
1191 SSARegMap *RegMap = BB->getParent()->getSSARegMap();
1192 unsigned Tmp1 = RegMap->createVirtualRegister(X86::R8RegisterClass);
1193 unsigned Tmp2 = RegMap->createVirtualRegister(X86::R8RegisterClass);
1194 BuildMI(BB, SetCC1, 0, Tmp1);
1195 BuildMI(BB, SetCC2, 0, Tmp2);
1196 BuildMI(BB, LogicalOp, 2, DestReg).addReg(Tmp1).addReg(Tmp2);
1197}
1198
1199/// EmitSetCC - Emit the code to set the specified 8-bit register to 1 if the
1200/// condition codes match the specified SetCCOpcode. Note that some conditions
1201/// require multiple instructions to generate the correct value.
1202static void EmitSetCC(MachineBasicBlock *BB, unsigned DestReg,
1203 ISD::CondCode SetCCOpcode, bool isFP) {
1204 unsigned Opc;
1205 if (!isFP) {
1206 switch (SetCCOpcode) {
1207 default: assert(0 && "Illegal integer SetCC!");
1208 case ISD::SETEQ: Opc = X86::SETEr; break;
1209 case ISD::SETGT: Opc = X86::SETGr; break;
1210 case ISD::SETGE: Opc = X86::SETGEr; break;
1211 case ISD::SETLT: Opc = X86::SETLr; break;
1212 case ISD::SETLE: Opc = X86::SETLEr; break;
1213 case ISD::SETNE: Opc = X86::SETNEr; break;
1214 case ISD::SETULT: Opc = X86::SETBr; break;
1215 case ISD::SETUGT: Opc = X86::SETAr; break;
1216 case ISD::SETULE: Opc = X86::SETBEr; break;
1217 case ISD::SETUGE: Opc = X86::SETAEr; break;
1218 }
1219 } else {
1220 // On a floating point condition, the flags are set as follows:
1221 // ZF PF CF op
1222 // 0 | 0 | 0 | X > Y
1223 // 0 | 0 | 1 | X < Y
1224 // 1 | 0 | 0 | X == Y
1225 // 1 | 1 | 1 | unordered
1226 //
1227 switch (SetCCOpcode) {
1228 default: assert(0 && "Invalid FP setcc!");
1229 case ISD::SETUEQ:
1230 case ISD::SETEQ:
1231 Opc = X86::SETEr; // True if ZF = 1
1232 break;
1233 case ISD::SETOGT:
1234 case ISD::SETGT:
1235 Opc = X86::SETAr; // True if CF = 0 and ZF = 0
1236 break;
1237 case ISD::SETOGE:
1238 case ISD::SETGE:
1239 Opc = X86::SETAEr; // True if CF = 0
1240 break;
1241 case ISD::SETULT:
1242 case ISD::SETLT:
1243 Opc = X86::SETBr; // True if CF = 1
1244 break;
1245 case ISD::SETULE:
1246 case ISD::SETLE:
1247 Opc = X86::SETBEr; // True if CF = 1 or ZF = 1
1248 break;
1249 case ISD::SETONE:
1250 case ISD::SETNE:
1251 Opc = X86::SETNEr; // True if ZF = 0
1252 break;
1253 case ISD::SETUO:
1254 Opc = X86::SETPr; // True if PF = 1
1255 break;
1256 case ISD::SETO:
1257 Opc = X86::SETNPr; // True if PF = 0
1258 break;
1259 case ISD::SETOEQ: // !PF & ZF
1260 Emit2SetCCsAndLogical(BB, X86::SETNPr, X86::SETEr, X86::AND8rr, DestReg);
1261 return;
1262 case ISD::SETOLT: // !PF & CF
1263 Emit2SetCCsAndLogical(BB, X86::SETNPr, X86::SETBr, X86::AND8rr, DestReg);
1264 return;
1265 case ISD::SETOLE: // !PF & (CF || ZF)
1266 Emit2SetCCsAndLogical(BB, X86::SETNPr, X86::SETBEr, X86::AND8rr, DestReg);
1267 return;
1268 case ISD::SETUGT: // PF | (!ZF & !CF)
1269 Emit2SetCCsAndLogical(BB, X86::SETPr, X86::SETAr, X86::OR8rr, DestReg);
1270 return;
1271 case ISD::SETUGE: // PF | !CF
1272 Emit2SetCCsAndLogical(BB, X86::SETPr, X86::SETAEr, X86::OR8rr, DestReg);
1273 return;
1274 case ISD::SETUNE: // PF | !ZF
1275 Emit2SetCCsAndLogical(BB, X86::SETPr, X86::SETNEr, X86::OR8rr, DestReg);
1276 return;
1277 }
1278 }
1279 BuildMI(BB, Opc, 0, DestReg);
1280}
1281
1282
1283/// EmitBranchCC - Emit code into BB that arranges for control to transfer to
1284/// the Dest block if the Cond condition is true. If we cannot fold this
1285/// condition into the branch, return true.
1286///
Chris Lattner37ed2852005-01-11 04:06:27 +00001287bool ISel::EmitBranchCC(MachineBasicBlock *Dest, SDOperand Chain,
1288 SDOperand Cond) {
Chris Lattner88c8a232005-01-07 07:49:41 +00001289 // FIXME: Evaluate whether it would be good to emit code like (X < Y) | (A >
1290 // B) using two conditional branches instead of one condbr, two setcc's, and
1291 // an or.
1292 if ((Cond.getOpcode() == ISD::OR ||
1293 Cond.getOpcode() == ISD::AND) && Cond.Val->hasOneUse()) {
1294 // And and or set the flags for us, so there is no need to emit a TST of the
1295 // result. It is only safe to do this if there is only a single use of the
1296 // AND/OR though, otherwise we don't know it will be emitted here.
Chris Lattner37ed2852005-01-11 04:06:27 +00001297 Select(Chain);
Chris Lattner88c8a232005-01-07 07:49:41 +00001298 SelectExpr(Cond);
1299 BuildMI(BB, X86::JNE, 1).addMBB(Dest);
1300 return false;
1301 }
1302
1303 // Codegen br not C -> JE.
1304 if (Cond.getOpcode() == ISD::XOR)
1305 if (ConstantSDNode *NC = dyn_cast<ConstantSDNode>(Cond.Val->getOperand(1)))
1306 if (NC->isAllOnesValue()) {
Chris Lattner37ed2852005-01-11 04:06:27 +00001307 unsigned CondR;
1308 if (getRegPressure(Chain) > getRegPressure(Cond)) {
1309 Select(Chain);
1310 CondR = SelectExpr(Cond.Val->getOperand(0));
1311 } else {
1312 CondR = SelectExpr(Cond.Val->getOperand(0));
1313 Select(Chain);
1314 }
Chris Lattner88c8a232005-01-07 07:49:41 +00001315 BuildMI(BB, X86::TEST8rr, 2).addReg(CondR).addReg(CondR);
1316 BuildMI(BB, X86::JE, 1).addMBB(Dest);
1317 return false;
1318 }
1319
1320 SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Cond);
1321 if (SetCC == 0)
1322 return true; // Can only handle simple setcc's so far.
1323
1324 unsigned Opc;
1325
1326 // Handle integer conditions first.
1327 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
1328 switch (SetCC->getCondition()) {
1329 default: assert(0 && "Illegal integer SetCC!");
1330 case ISD::SETEQ: Opc = X86::JE; break;
1331 case ISD::SETGT: Opc = X86::JG; break;
1332 case ISD::SETGE: Opc = X86::JGE; break;
1333 case ISD::SETLT: Opc = X86::JL; break;
1334 case ISD::SETLE: Opc = X86::JLE; break;
1335 case ISD::SETNE: Opc = X86::JNE; break;
1336 case ISD::SETULT: Opc = X86::JB; break;
1337 case ISD::SETUGT: Opc = X86::JA; break;
1338 case ISD::SETULE: Opc = X86::JBE; break;
1339 case ISD::SETUGE: Opc = X86::JAE; break;
1340 }
Chris Lattner37ed2852005-01-11 04:06:27 +00001341 Select(Chain);
Chris Lattner3be6cd52005-01-17 01:34:14 +00001342 EmitCMP(SetCC->getOperand(0), SetCC->getOperand(1), SetCC->hasOneUse());
Chris Lattner88c8a232005-01-07 07:49:41 +00001343 BuildMI(BB, Opc, 1).addMBB(Dest);
1344 return false;
1345 }
1346
Chris Lattner88c8a232005-01-07 07:49:41 +00001347 unsigned Opc2 = 0; // Second branch if needed.
1348
1349 // On a floating point condition, the flags are set as follows:
1350 // ZF PF CF op
1351 // 0 | 0 | 0 | X > Y
1352 // 0 | 0 | 1 | X < Y
1353 // 1 | 0 | 0 | X == Y
1354 // 1 | 1 | 1 | unordered
1355 //
1356 switch (SetCC->getCondition()) {
1357 default: assert(0 && "Invalid FP setcc!");
1358 case ISD::SETUEQ:
1359 case ISD::SETEQ: Opc = X86::JE; break; // True if ZF = 1
1360 case ISD::SETOGT:
1361 case ISD::SETGT: Opc = X86::JA; break; // True if CF = 0 and ZF = 0
1362 case ISD::SETOGE:
1363 case ISD::SETGE: Opc = X86::JAE; break; // True if CF = 0
1364 case ISD::SETULT:
1365 case ISD::SETLT: Opc = X86::JB; break; // True if CF = 1
1366 case ISD::SETULE:
1367 case ISD::SETLE: Opc = X86::JBE; break; // True if CF = 1 or ZF = 1
1368 case ISD::SETONE:
1369 case ISD::SETNE: Opc = X86::JNE; break; // True if ZF = 0
1370 case ISD::SETUO: Opc = X86::JP; break; // True if PF = 1
1371 case ISD::SETO: Opc = X86::JNP; break; // True if PF = 0
1372 case ISD::SETUGT: // PF = 1 | (ZF = 0 & CF = 0)
1373 Opc = X86::JA; // ZF = 0 & CF = 0
1374 Opc2 = X86::JP; // PF = 1
1375 break;
1376 case ISD::SETUGE: // PF = 1 | CF = 0
1377 Opc = X86::JAE; // CF = 0
1378 Opc2 = X86::JP; // PF = 1
1379 break;
1380 case ISD::SETUNE: // PF = 1 | ZF = 0
1381 Opc = X86::JNE; // ZF = 0
1382 Opc2 = X86::JP; // PF = 1
1383 break;
1384 case ISD::SETOEQ: // PF = 0 & ZF = 1
1385 //X86::JNP, X86::JE
1386 //X86::AND8rr
1387 return true; // FIXME: Emit more efficient code for this branch.
1388 case ISD::SETOLT: // PF = 0 & CF = 1
1389 //X86::JNP, X86::JB
1390 //X86::AND8rr
1391 return true; // FIXME: Emit more efficient code for this branch.
1392 case ISD::SETOLE: // PF = 0 & (CF = 1 || ZF = 1)
1393 //X86::JNP, X86::JBE
1394 //X86::AND8rr
1395 return true; // FIXME: Emit more efficient code for this branch.
1396 }
1397
Chris Lattner37ed2852005-01-11 04:06:27 +00001398 Select(Chain);
Chris Lattner3be6cd52005-01-17 01:34:14 +00001399 EmitCMP(SetCC->getOperand(0), SetCC->getOperand(1), SetCC->hasOneUse());
Chris Lattner88c8a232005-01-07 07:49:41 +00001400 BuildMI(BB, Opc, 1).addMBB(Dest);
1401 if (Opc2)
1402 BuildMI(BB, Opc2, 1).addMBB(Dest);
1403 return false;
1404}
1405
Chris Lattner1d13a922005-01-10 22:10:13 +00001406/// EmitSelectCC - Emit code into BB that performs a select operation between
1407/// the two registers RTrue and RFalse, generating a result into RDest. Return
1408/// true if the fold cannot be performed.
1409///
1410void ISel::EmitSelectCC(SDOperand Cond, MVT::ValueType SVT,
1411 unsigned RTrue, unsigned RFalse, unsigned RDest) {
1412 enum Condition {
1413 EQ, NE, LT, LE, GT, GE, B, BE, A, AE, P, NP,
1414 NOT_SET
1415 } CondCode = NOT_SET;
1416
1417 static const unsigned CMOVTAB16[] = {
1418 X86::CMOVE16rr, X86::CMOVNE16rr, X86::CMOVL16rr, X86::CMOVLE16rr,
1419 X86::CMOVG16rr, X86::CMOVGE16rr, X86::CMOVB16rr, X86::CMOVBE16rr,
Misha Brukmanc88330a2005-04-21 23:38:14 +00001420 X86::CMOVA16rr, X86::CMOVAE16rr, X86::CMOVP16rr, X86::CMOVNP16rr,
Chris Lattner1d13a922005-01-10 22:10:13 +00001421 };
1422 static const unsigned CMOVTAB32[] = {
1423 X86::CMOVE32rr, X86::CMOVNE32rr, X86::CMOVL32rr, X86::CMOVLE32rr,
1424 X86::CMOVG32rr, X86::CMOVGE32rr, X86::CMOVB32rr, X86::CMOVBE32rr,
Misha Brukmanc88330a2005-04-21 23:38:14 +00001425 X86::CMOVA32rr, X86::CMOVAE32rr, X86::CMOVP32rr, X86::CMOVNP32rr,
Chris Lattner1d13a922005-01-10 22:10:13 +00001426 };
1427 static const unsigned CMOVTABFP[] = {
1428 X86::FCMOVE , X86::FCMOVNE, /*missing*/0, /*missing*/0,
1429 /*missing*/0, /*missing*/0, X86::FCMOVB , X86::FCMOVBE,
1430 X86::FCMOVA , X86::FCMOVAE, X86::FCMOVP , X86::FCMOVNP
1431 };
1432
1433 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Cond)) {
1434 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
1435 switch (SetCC->getCondition()) {
1436 default: assert(0 && "Unknown integer comparison!");
1437 case ISD::SETEQ: CondCode = EQ; break;
1438 case ISD::SETGT: CondCode = GT; break;
1439 case ISD::SETGE: CondCode = GE; break;
1440 case ISD::SETLT: CondCode = LT; break;
1441 case ISD::SETLE: CondCode = LE; break;
1442 case ISD::SETNE: CondCode = NE; break;
1443 case ISD::SETULT: CondCode = B; break;
1444 case ISD::SETUGT: CondCode = A; break;
1445 case ISD::SETULE: CondCode = BE; break;
1446 case ISD::SETUGE: CondCode = AE; break;
1447 }
1448 } else {
1449 // On a floating point condition, the flags are set as follows:
1450 // ZF PF CF op
1451 // 0 | 0 | 0 | X > Y
1452 // 0 | 0 | 1 | X < Y
1453 // 1 | 0 | 0 | X == Y
1454 // 1 | 1 | 1 | unordered
1455 //
1456 switch (SetCC->getCondition()) {
1457 default: assert(0 && "Unknown FP comparison!");
1458 case ISD::SETUEQ:
1459 case ISD::SETEQ: CondCode = EQ; break; // True if ZF = 1
1460 case ISD::SETOGT:
1461 case ISD::SETGT: CondCode = A; break; // True if CF = 0 and ZF = 0
1462 case ISD::SETOGE:
1463 case ISD::SETGE: CondCode = AE; break; // True if CF = 0
1464 case ISD::SETULT:
1465 case ISD::SETLT: CondCode = B; break; // True if CF = 1
1466 case ISD::SETULE:
1467 case ISD::SETLE: CondCode = BE; break; // True if CF = 1 or ZF = 1
1468 case ISD::SETONE:
1469 case ISD::SETNE: CondCode = NE; break; // True if ZF = 0
1470 case ISD::SETUO: CondCode = P; break; // True if PF = 1
1471 case ISD::SETO: CondCode = NP; break; // True if PF = 0
1472 case ISD::SETUGT: // PF = 1 | (ZF = 0 & CF = 0)
1473 case ISD::SETUGE: // PF = 1 | CF = 0
1474 case ISD::SETUNE: // PF = 1 | ZF = 0
1475 case ISD::SETOEQ: // PF = 0 & ZF = 1
1476 case ISD::SETOLT: // PF = 0 & CF = 1
1477 case ISD::SETOLE: // PF = 0 & (CF = 1 || ZF = 1)
1478 // We cannot emit this comparison as a single cmov.
1479 break;
1480 }
1481 }
1482 }
1483
1484 unsigned Opc = 0;
1485 if (CondCode != NOT_SET) {
1486 switch (SVT) {
1487 default: assert(0 && "Cannot select this type!");
1488 case MVT::i16: Opc = CMOVTAB16[CondCode]; break;
1489 case MVT::i32: Opc = CMOVTAB32[CondCode]; break;
Chris Lattnere44e6d12005-01-11 03:50:45 +00001490 case MVT::f64: Opc = CMOVTABFP[CondCode]; break;
Chris Lattner1d13a922005-01-10 22:10:13 +00001491 }
1492 }
1493
1494 // Finally, if we weren't able to fold this, just emit the condition and test
1495 // it.
1496 if (CondCode == NOT_SET || Opc == 0) {
1497 // Get the condition into the zero flag.
1498 unsigned CondReg = SelectExpr(Cond);
1499 BuildMI(BB, X86::TEST8rr, 2).addReg(CondReg).addReg(CondReg);
1500
1501 switch (SVT) {
1502 default: assert(0 && "Cannot select this type!");
1503 case MVT::i16: Opc = X86::CMOVE16rr; break;
1504 case MVT::i32: Opc = X86::CMOVE32rr; break;
Chris Lattnere44e6d12005-01-11 03:50:45 +00001505 case MVT::f64: Opc = X86::FCMOVE; break;
Chris Lattner1d13a922005-01-10 22:10:13 +00001506 }
1507 } else {
1508 // FIXME: CMP R, 0 -> TEST R, R
Chris Lattner3be6cd52005-01-17 01:34:14 +00001509 EmitCMP(Cond.getOperand(0), Cond.getOperand(1), Cond.Val->hasOneUse());
Chris Lattner8fea42b2005-01-11 03:37:59 +00001510 std::swap(RTrue, RFalse);
Chris Lattner1d13a922005-01-10 22:10:13 +00001511 }
1512 BuildMI(BB, Opc, 2, RDest).addReg(RTrue).addReg(RFalse);
1513}
1514
Chris Lattner3be6cd52005-01-17 01:34:14 +00001515void ISel::EmitCMP(SDOperand LHS, SDOperand RHS, bool HasOneUse) {
Chris Lattner0d1f82a2005-01-11 03:11:44 +00001516 unsigned Opc;
Chris Lattner88c8a232005-01-07 07:49:41 +00001517 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(RHS)) {
1518 Opc = 0;
Chris Lattnera56d29d2005-01-17 06:26:58 +00001519 if (HasOneUse && isFoldableLoad(LHS, RHS)) {
Chris Lattner2cfce682005-01-12 02:02:48 +00001520 switch (RHS.getValueType()) {
1521 default: break;
1522 case MVT::i1:
1523 case MVT::i8: Opc = X86::CMP8mi; break;
1524 case MVT::i16: Opc = X86::CMP16mi; break;
1525 case MVT::i32: Opc = X86::CMP32mi; break;
1526 }
1527 if (Opc) {
1528 X86AddressMode AM;
1529 EmitFoldedLoad(LHS, AM);
1530 addFullAddress(BuildMI(BB, Opc, 5), AM).addImm(CN->getValue());
1531 return;
1532 }
1533 }
1534
Chris Lattner88c8a232005-01-07 07:49:41 +00001535 switch (RHS.getValueType()) {
1536 default: break;
1537 case MVT::i1:
1538 case MVT::i8: Opc = X86::CMP8ri; break;
1539 case MVT::i16: Opc = X86::CMP16ri; break;
1540 case MVT::i32: Opc = X86::CMP32ri; break;
1541 }
1542 if (Opc) {
Chris Lattner0d1f82a2005-01-11 03:11:44 +00001543 unsigned Tmp1 = SelectExpr(LHS);
Chris Lattner88c8a232005-01-07 07:49:41 +00001544 BuildMI(BB, Opc, 2).addReg(Tmp1).addImm(CN->getValue());
1545 return;
1546 }
Chris Lattner720a62e2005-01-14 22:37:41 +00001547 } else if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(RHS)) {
1548 if (CN->isExactlyValue(+0.0) ||
1549 CN->isExactlyValue(-0.0)) {
1550 unsigned Reg = SelectExpr(LHS);
1551 BuildMI(BB, X86::FTST, 1).addReg(Reg);
1552 BuildMI(BB, X86::FNSTSW8r, 0);
1553 BuildMI(BB, X86::SAHF, 1);
Chris Lattner43832b02005-03-17 16:29:26 +00001554 return;
Chris Lattner720a62e2005-01-14 22:37:41 +00001555 }
Chris Lattner88c8a232005-01-07 07:49:41 +00001556 }
1557
Chris Lattner2cfce682005-01-12 02:02:48 +00001558 Opc = 0;
Chris Lattnera56d29d2005-01-17 06:26:58 +00001559 if (HasOneUse && isFoldableLoad(LHS, RHS)) {
Chris Lattner2cfce682005-01-12 02:02:48 +00001560 switch (RHS.getValueType()) {
1561 default: break;
1562 case MVT::i1:
1563 case MVT::i8: Opc = X86::CMP8mr; break;
1564 case MVT::i16: Opc = X86::CMP16mr; break;
1565 case MVT::i32: Opc = X86::CMP32mr; break;
1566 }
1567 if (Opc) {
1568 X86AddressMode AM;
Chris Lattner3676cd62005-01-13 05:53:16 +00001569 EmitFoldedLoad(LHS, AM);
1570 unsigned Reg = SelectExpr(RHS);
Chris Lattner2cfce682005-01-12 02:02:48 +00001571 addFullAddress(BuildMI(BB, Opc, 5), AM).addReg(Reg);
1572 return;
1573 }
1574 }
1575
Chris Lattner88c8a232005-01-07 07:49:41 +00001576 switch (LHS.getValueType()) {
1577 default: assert(0 && "Cannot compare this value!");
1578 case MVT::i1:
1579 case MVT::i8: Opc = X86::CMP8rr; break;
1580 case MVT::i16: Opc = X86::CMP16rr; break;
1581 case MVT::i32: Opc = X86::CMP32rr; break;
Chris Lattnere44e6d12005-01-11 03:50:45 +00001582 case MVT::f64: Opc = X86::FUCOMIr; break;
Chris Lattner88c8a232005-01-07 07:49:41 +00001583 }
Chris Lattner0d1f82a2005-01-11 03:11:44 +00001584 unsigned Tmp1, Tmp2;
1585 if (getRegPressure(LHS) > getRegPressure(RHS)) {
1586 Tmp1 = SelectExpr(LHS);
1587 Tmp2 = SelectExpr(RHS);
1588 } else {
1589 Tmp2 = SelectExpr(RHS);
1590 Tmp1 = SelectExpr(LHS);
1591 }
Chris Lattner88c8a232005-01-07 07:49:41 +00001592 BuildMI(BB, Opc, 2).addReg(Tmp1).addReg(Tmp2);
1593}
1594
Chris Lattner62b22422005-01-11 21:19:59 +00001595/// isFoldableLoad - Return true if this is a load instruction that can safely
1596/// be folded into an operation that uses it.
Chris Lattner30607ec2005-01-25 20:03:11 +00001597bool ISel::isFoldableLoad(SDOperand Op, SDOperand OtherOp, bool FloatPromoteOk){
1598 if (Op.getOpcode() == ISD::LOAD) {
1599 // FIXME: currently can't fold constant pool indexes.
1600 if (isa<ConstantPoolSDNode>(Op.getOperand(1)))
1601 return false;
1602 } else if (FloatPromoteOk && Op.getOpcode() == ISD::EXTLOAD &&
1603 cast<MVTSDNode>(Op)->getExtraValueType() == MVT::f32) {
1604 // FIXME: currently can't fold constant pool indexes.
1605 if (isa<ConstantPoolSDNode>(Op.getOperand(1)))
1606 return false;
1607 } else {
Chris Lattner62b22422005-01-11 21:19:59 +00001608 return false;
Chris Lattner30607ec2005-01-25 20:03:11 +00001609 }
Chris Lattner62b22422005-01-11 21:19:59 +00001610
1611 // If this load has already been emitted, we clearly can't fold it.
Chris Lattner3676cd62005-01-13 05:53:16 +00001612 assert(Op.ResNo == 0 && "Not a use of the value of the load?");
1613 if (ExprMap.count(Op.getValue(1))) return false;
1614 assert(!ExprMap.count(Op.getValue(0)) && "Value in map but not token chain?");
Chris Lattner78d30282005-01-18 03:51:59 +00001615 assert(!ExprMap.count(Op.getValue(1))&&"Token lowered but value not in map?");
Chris Lattner62b22422005-01-11 21:19:59 +00001616
Chris Lattnera56d29d2005-01-17 06:26:58 +00001617 // If there is not just one use of its value, we cannot fold.
1618 if (!Op.Val->hasNUsesOfValue(1, 0)) return false;
1619
1620 // Finally, we cannot fold the load into the operation if this would induce a
1621 // cycle into the resultant dag. To check for this, see if OtherOp (the other
1622 // operand of the operation we are folding the load into) can possible use the
1623 // chain node defined by the load.
1624 if (OtherOp.Val && !Op.Val->hasNUsesOfValue(0, 1)) { // Has uses of chain?
1625 std::set<SDNode*> Visited;
1626 if (NodeTransitivelyUsesValue(OtherOp, Op.getValue(1), Visited))
1627 return false;
1628 }
1629 return true;
Chris Lattner62b22422005-01-11 21:19:59 +00001630}
1631
Chris Lattnera56d29d2005-01-17 06:26:58 +00001632
Chris Lattner62b22422005-01-11 21:19:59 +00001633/// EmitFoldedLoad - Ensure that the arguments of the load are code generated,
1634/// and compute the address being loaded into AM.
1635void ISel::EmitFoldedLoad(SDOperand Op, X86AddressMode &AM) {
1636 SDOperand Chain = Op.getOperand(0);
1637 SDOperand Address = Op.getOperand(1);
Chris Lattnera7acdda2005-01-18 01:06:26 +00001638
Chris Lattner62b22422005-01-11 21:19:59 +00001639 if (getRegPressure(Chain) > getRegPressure(Address)) {
1640 Select(Chain);
1641 SelectAddress(Address, AM);
1642 } else {
1643 SelectAddress(Address, AM);
1644 Select(Chain);
1645 }
1646
1647 // The chain for this load is now lowered.
Chris Lattner3676cd62005-01-13 05:53:16 +00001648 assert(ExprMap.count(SDOperand(Op.Val, 1)) == 0 &&
1649 "Load emitted more than once?");
Chris Lattner78d30282005-01-18 03:51:59 +00001650 if (!ExprMap.insert(std::make_pair(Op.getValue(1), 1)).second)
Chris Lattner3676cd62005-01-13 05:53:16 +00001651 assert(0 && "Load emitted more than once!");
Chris Lattner62b22422005-01-11 21:19:59 +00001652}
1653
Chris Lattner29f58192005-01-19 07:37:26 +00001654// EmitOrOpOp - Pattern match the expression (Op1|Op2), where we know that op1
1655// and op2 are i8/i16/i32 values with one use each (the or). If we can form a
1656// SHLD or SHRD, emit the instruction (generating the value into DestReg) and
1657// return true.
1658bool ISel::EmitOrOpOp(SDOperand Op1, SDOperand Op2, unsigned DestReg) {
Chris Lattner41fe2012005-01-19 06:18:43 +00001659 if (Op1.getOpcode() == ISD::SHL && Op2.getOpcode() == ISD::SRL) {
1660 // good!
1661 } else if (Op2.getOpcode() == ISD::SHL && Op1.getOpcode() == ISD::SRL) {
1662 std::swap(Op1, Op2); // Op1 is the SHL now.
1663 } else {
1664 return false; // No match
1665 }
1666
1667 SDOperand ShlVal = Op1.getOperand(0);
1668 SDOperand ShlAmt = Op1.getOperand(1);
1669 SDOperand ShrVal = Op2.getOperand(0);
1670 SDOperand ShrAmt = Op2.getOperand(1);
1671
Chris Lattner29f58192005-01-19 07:37:26 +00001672 unsigned RegSize = MVT::getSizeInBits(Op1.getValueType());
1673
Chris Lattner41fe2012005-01-19 06:18:43 +00001674 // Find out if ShrAmt = 32-ShlAmt or ShlAmt = 32-ShrAmt.
1675 if (ShlAmt.getOpcode() == ISD::SUB && ShlAmt.getOperand(1) == ShrAmt)
1676 if (ConstantSDNode *SubCST = dyn_cast<ConstantSDNode>(ShlAmt.getOperand(0)))
Chris Lattnerde87d1462005-01-19 08:07:05 +00001677 if (SubCST->getValue() == RegSize) {
1678 // (A >> ShrAmt) | (A << (32-ShrAmt)) ==> ROR A, ShrAmt
Chris Lattner41fe2012005-01-19 06:18:43 +00001679 // (A >> ShrAmt) | (B << (32-ShrAmt)) ==> SHRD A, B, ShrAmt
Chris Lattnerde87d1462005-01-19 08:07:05 +00001680 if (ShrVal == ShlVal) {
1681 unsigned Reg, ShAmt;
1682 if (getRegPressure(ShrVal) > getRegPressure(ShrAmt)) {
1683 Reg = SelectExpr(ShrVal);
1684 ShAmt = SelectExpr(ShrAmt);
1685 } else {
1686 ShAmt = SelectExpr(ShrAmt);
1687 Reg = SelectExpr(ShrVal);
1688 }
1689 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShAmt);
1690 unsigned Opc = RegSize == 8 ? X86::ROR8rCL :
1691 (RegSize == 16 ? X86::ROR16rCL : X86::ROR32rCL);
1692 BuildMI(BB, Opc, 1, DestReg).addReg(Reg);
1693 return true;
1694 } else if (RegSize != 8) {
Chris Lattner41fe2012005-01-19 06:18:43 +00001695 unsigned AReg, BReg;
1696 if (getRegPressure(ShlVal) > getRegPressure(ShrVal)) {
Chris Lattner41fe2012005-01-19 06:18:43 +00001697 BReg = SelectExpr(ShlVal);
Chris Lattner474aac42005-01-19 17:24:34 +00001698 AReg = SelectExpr(ShrVal);
Chris Lattner41fe2012005-01-19 06:18:43 +00001699 } else {
Chris Lattner41fe2012005-01-19 06:18:43 +00001700 AReg = SelectExpr(ShrVal);
Chris Lattner474aac42005-01-19 17:24:34 +00001701 BReg = SelectExpr(ShlVal);
Chris Lattner41fe2012005-01-19 06:18:43 +00001702 }
Chris Lattnerde87d1462005-01-19 08:07:05 +00001703 unsigned ShAmt = SelectExpr(ShrAmt);
1704 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShAmt);
1705 unsigned Opc = RegSize == 16 ? X86::SHRD16rrCL : X86::SHRD32rrCL;
1706 BuildMI(BB, Opc, 2, DestReg).addReg(AReg).addReg(BReg);
Chris Lattner41fe2012005-01-19 06:18:43 +00001707 return true;
1708 }
1709 }
1710
Chris Lattnerde87d1462005-01-19 08:07:05 +00001711 if (ShrAmt.getOpcode() == ISD::SUB && ShrAmt.getOperand(1) == ShlAmt)
1712 if (ConstantSDNode *SubCST = dyn_cast<ConstantSDNode>(ShrAmt.getOperand(0)))
1713 if (SubCST->getValue() == RegSize) {
1714 // (A << ShlAmt) | (A >> (32-ShlAmt)) ==> ROL A, ShrAmt
1715 // (A << ShlAmt) | (B >> (32-ShlAmt)) ==> SHLD A, B, ShrAmt
1716 if (ShrVal == ShlVal) {
1717 unsigned Reg, ShAmt;
1718 if (getRegPressure(ShrVal) > getRegPressure(ShlAmt)) {
1719 Reg = SelectExpr(ShrVal);
1720 ShAmt = SelectExpr(ShlAmt);
1721 } else {
1722 ShAmt = SelectExpr(ShlAmt);
1723 Reg = SelectExpr(ShrVal);
1724 }
1725 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShAmt);
1726 unsigned Opc = RegSize == 8 ? X86::ROL8rCL :
1727 (RegSize == 16 ? X86::ROL16rCL : X86::ROL32rCL);
1728 BuildMI(BB, Opc, 1, DestReg).addReg(Reg);
1729 return true;
1730 } else if (RegSize != 8) {
1731 unsigned AReg, BReg;
1732 if (getRegPressure(ShlVal) > getRegPressure(ShrVal)) {
Chris Lattner474aac42005-01-19 17:24:34 +00001733 AReg = SelectExpr(ShlVal);
1734 BReg = SelectExpr(ShrVal);
Chris Lattnerde87d1462005-01-19 08:07:05 +00001735 } else {
Chris Lattner474aac42005-01-19 17:24:34 +00001736 BReg = SelectExpr(ShrVal);
1737 AReg = SelectExpr(ShlVal);
Chris Lattnerde87d1462005-01-19 08:07:05 +00001738 }
1739 unsigned ShAmt = SelectExpr(ShlAmt);
1740 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShAmt);
1741 unsigned Opc = RegSize == 16 ? X86::SHLD16rrCL : X86::SHLD32rrCL;
1742 BuildMI(BB, Opc, 2, DestReg).addReg(AReg).addReg(BReg);
1743 return true;
1744 }
1745 }
Chris Lattner41fe2012005-01-19 06:18:43 +00001746
Chris Lattnerde87d1462005-01-19 08:07:05 +00001747 if (ConstantSDNode *ShrCst = dyn_cast<ConstantSDNode>(ShrAmt))
1748 if (ConstantSDNode *ShlCst = dyn_cast<ConstantSDNode>(ShlAmt))
1749 if (ShrCst->getValue() < RegSize && ShlCst->getValue() < RegSize)
1750 if (ShrCst->getValue() == RegSize-ShlCst->getValue()) {
1751 // (A >> 5) | (A << 27) --> ROR A, 5
1752 // (A >> 5) | (B << 27) --> SHRD A, B, 5
1753 if (ShrVal == ShlVal) {
1754 unsigned Reg = SelectExpr(ShrVal);
1755 unsigned Opc = RegSize == 8 ? X86::ROR8ri :
1756 (RegSize == 16 ? X86::ROR16ri : X86::ROR32ri);
1757 BuildMI(BB, Opc, 2, DestReg).addReg(Reg).addImm(ShrCst->getValue());
1758 return true;
1759 } else if (RegSize != 8) {
1760 unsigned AReg, BReg;
1761 if (getRegPressure(ShlVal) > getRegPressure(ShrVal)) {
Chris Lattnerde87d1462005-01-19 08:07:05 +00001762 BReg = SelectExpr(ShlVal);
Chris Lattner474aac42005-01-19 17:24:34 +00001763 AReg = SelectExpr(ShrVal);
Chris Lattnerde87d1462005-01-19 08:07:05 +00001764 } else {
Chris Lattnerde87d1462005-01-19 08:07:05 +00001765 AReg = SelectExpr(ShrVal);
Chris Lattner474aac42005-01-19 17:24:34 +00001766 BReg = SelectExpr(ShlVal);
Chris Lattnerde87d1462005-01-19 08:07:05 +00001767 }
1768 unsigned Opc = RegSize == 16 ? X86::SHRD16rri8 : X86::SHRD32rri8;
1769 BuildMI(BB, Opc, 3, DestReg).addReg(AReg).addReg(BReg)
1770 .addImm(ShrCst->getValue());
1771 return true;
1772 }
1773 }
Misha Brukmanc88330a2005-04-21 23:38:14 +00001774
Chris Lattner41fe2012005-01-19 06:18:43 +00001775 return false;
1776}
1777
Chris Lattner88c8a232005-01-07 07:49:41 +00001778unsigned ISel::SelectExpr(SDOperand N) {
1779 unsigned Result;
1780 unsigned Tmp1, Tmp2, Tmp3;
1781 unsigned Opc = 0;
Chris Lattnerb52e0412005-01-08 19:28:19 +00001782 SDNode *Node = N.Val;
Chris Lattner62b22422005-01-11 21:19:59 +00001783 SDOperand Op0, Op1;
Chris Lattnerb52e0412005-01-08 19:28:19 +00001784
Chris Lattner720a62e2005-01-14 22:37:41 +00001785 if (Node->getOpcode() == ISD::CopyFromReg) {
Chris Lattner7ce7a8f2005-05-12 23:06:28 +00001786 if (MRegisterInfo::isVirtualRegister(cast<RegSDNode>(Node)->getReg()) ||
1787 cast<RegSDNode>(Node)->getReg() == X86::ESP) {
1788 // Just use the specified register as our input.
1789 return cast<RegSDNode>(Node)->getReg();
1790 }
Chris Lattner720a62e2005-01-14 22:37:41 +00001791 }
Misha Brukmanc88330a2005-04-21 23:38:14 +00001792
Chris Lattner62b22422005-01-11 21:19:59 +00001793 unsigned &Reg = ExprMap[N];
1794 if (Reg) return Reg;
Misha Brukmanc88330a2005-04-21 23:38:14 +00001795
Chris Lattnera31d4c72005-04-02 04:01:14 +00001796 switch (N.getOpcode()) {
1797 default:
Chris Lattner62b22422005-01-11 21:19:59 +00001798 Reg = Result = (N.getValueType() != MVT::Other) ?
Chris Lattnera31d4c72005-04-02 04:01:14 +00001799 MakeReg(N.getValueType()) : 1;
1800 break;
1801 case ISD::CALL:
Chris Lattner62b22422005-01-11 21:19:59 +00001802 // If this is a call instruction, make sure to prepare ALL of the result
1803 // values as well as the chain.
Chris Lattnera31d4c72005-04-02 04:01:14 +00001804 if (Node->getNumValues() == 1)
1805 Reg = Result = 1; // Void call, just a chain.
1806 else {
Chris Lattner62b22422005-01-11 21:19:59 +00001807 Result = MakeReg(Node->getValueType(0));
1808 ExprMap[N.getValue(0)] = Result;
Chris Lattnera31d4c72005-04-02 04:01:14 +00001809 for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
Chris Lattner62b22422005-01-11 21:19:59 +00001810 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
Chris Lattnera31d4c72005-04-02 04:01:14 +00001811 ExprMap[SDOperand(Node, Node->getNumValues()-1)] = 1;
Chris Lattner88c8a232005-01-07 07:49:41 +00001812 }
Chris Lattnera31d4c72005-04-02 04:01:14 +00001813 break;
1814 case ISD::ADD_PARTS:
1815 case ISD::SUB_PARTS:
1816 case ISD::SHL_PARTS:
1817 case ISD::SRL_PARTS:
1818 case ISD::SRA_PARTS:
1819 Result = MakeReg(Node->getValueType(0));
1820 ExprMap[N.getValue(0)] = Result;
1821 for (unsigned i = 1, e = N.Val->getNumValues(); i != e; ++i)
1822 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
1823 break;
Chris Lattner88c8a232005-01-07 07:49:41 +00001824 }
Misha Brukmanc88330a2005-04-21 23:38:14 +00001825
Chris Lattner88c8a232005-01-07 07:49:41 +00001826 switch (N.getOpcode()) {
1827 default:
Chris Lattnerb52e0412005-01-08 19:28:19 +00001828 Node->dump();
Chris Lattner88c8a232005-01-07 07:49:41 +00001829 assert(0 && "Node not handled!\n");
Chris Lattner7ce7a8f2005-05-12 23:06:28 +00001830 case ISD::CopyFromReg:
1831 Select(N.getOperand(0));
1832 if (Result == 1) {
1833 Reg = Result = ExprMap[N.getValue(0)] =
1834 MakeReg(N.getValue(0).getValueType());
1835 }
1836 switch (Node->getValueType(0)) {
1837 default: assert(0 && "Cannot CopyFromReg this!");
1838 case MVT::i1:
1839 case MVT::i8:
1840 BuildMI(BB, X86::MOV8rr, 1,
1841 Result).addReg(cast<RegSDNode>(Node)->getReg());
1842 return Result;
1843 case MVT::i16:
1844 BuildMI(BB, X86::MOV16rr, 1,
1845 Result).addReg(cast<RegSDNode>(Node)->getReg());
1846 return Result;
1847 case MVT::i32:
1848 BuildMI(BB, X86::MOV32rr, 1,
1849 Result).addReg(cast<RegSDNode>(Node)->getReg());
1850 return Result;
1851 }
1852
Chris Lattner88c8a232005-01-07 07:49:41 +00001853 case ISD::FrameIndex:
1854 Tmp1 = cast<FrameIndexSDNode>(N)->getIndex();
1855 addFrameReference(BuildMI(BB, X86::LEA32r, 4, Result), (int)Tmp1);
1856 return Result;
1857 case ISD::ConstantPool:
1858 Tmp1 = cast<ConstantPoolSDNode>(N)->getIndex();
1859 addConstantPoolReference(BuildMI(BB, X86::LEA32r, 4, Result), Tmp1);
1860 return Result;
1861 case ISD::ConstantFP:
1862 ContainsFPCode = true;
1863 Tmp1 = Result; // Intermediate Register
1864 if (cast<ConstantFPSDNode>(N)->getValue() < 0.0 ||
1865 cast<ConstantFPSDNode>(N)->isExactlyValue(-0.0))
1866 Tmp1 = MakeReg(MVT::f64);
1867
1868 if (cast<ConstantFPSDNode>(N)->isExactlyValue(+0.0) ||
1869 cast<ConstantFPSDNode>(N)->isExactlyValue(-0.0))
1870 BuildMI(BB, X86::FLD0, 0, Tmp1);
1871 else if (cast<ConstantFPSDNode>(N)->isExactlyValue(+1.0) ||
1872 cast<ConstantFPSDNode>(N)->isExactlyValue(-1.0))
1873 BuildMI(BB, X86::FLD1, 0, Tmp1);
1874 else
1875 assert(0 && "Unexpected constant!");
1876 if (Tmp1 != Result)
1877 BuildMI(BB, X86::FCHS, 1, Result).addReg(Tmp1);
1878 return Result;
1879 case ISD::Constant:
1880 switch (N.getValueType()) {
1881 default: assert(0 && "Cannot use constants of this type!");
1882 case MVT::i1:
1883 case MVT::i8: Opc = X86::MOV8ri; break;
1884 case MVT::i16: Opc = X86::MOV16ri; break;
1885 case MVT::i32: Opc = X86::MOV32ri; break;
1886 }
1887 BuildMI(BB, Opc, 1,Result).addImm(cast<ConstantSDNode>(N)->getValue());
1888 return Result;
Chris Lattnerf4b985d2005-04-01 22:46:45 +00001889 case ISD::UNDEF:
1890 if (Node->getValueType(0) == MVT::f64) {
1891 // FIXME: SHOULD TEACH STACKIFIER ABOUT UNDEF VALUES!
1892 BuildMI(BB, X86::FLD0, 0, Result);
1893 } else {
1894 BuildMI(BB, X86::IMPLICIT_DEF, 0, Result);
1895 }
1896 return Result;
Chris Lattner88c8a232005-01-07 07:49:41 +00001897 case ISD::GlobalAddress: {
1898 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
1899 BuildMI(BB, X86::MOV32ri, 1, Result).addGlobalAddress(GV);
1900 return Result;
1901 }
1902 case ISD::ExternalSymbol: {
1903 const char *Sym = cast<ExternalSymbolSDNode>(N)->getSymbol();
1904 BuildMI(BB, X86::MOV32ri, 1, Result).addExternalSymbol(Sym);
1905 return Result;
1906 }
Chris Lattner88c8a232005-01-07 07:49:41 +00001907 case ISD::ZERO_EXTEND: {
1908 int DestIs16 = N.getValueType() == MVT::i16;
1909 int SrcIs16 = N.getOperand(0).getValueType() == MVT::i16;
Chris Lattner282781c2005-01-09 18:52:44 +00001910
1911 // FIXME: This hack is here for zero extension casts from bool to i8. This
1912 // would not be needed if bools were promoted by Legalize.
1913 if (N.getValueType() == MVT::i8) {
Chris Lattnerb0eef822005-01-11 23:33:00 +00001914 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner282781c2005-01-09 18:52:44 +00001915 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(Tmp1);
1916 return Result;
1917 }
Chris Lattner88c8a232005-01-07 07:49:41 +00001918
Chris Lattnera56d29d2005-01-17 06:26:58 +00001919 if (isFoldableLoad(N.getOperand(0), SDOperand())) {
Chris Lattnerb0eef822005-01-11 23:33:00 +00001920 static const unsigned Opc[3] = {
1921 X86::MOVZX32rm8, X86::MOVZX32rm16, X86::MOVZX16rm8
1922 };
1923
1924 X86AddressMode AM;
1925 EmitFoldedLoad(N.getOperand(0), AM);
1926 addFullAddress(BuildMI(BB, Opc[SrcIs16+DestIs16*2], 4, Result), AM);
Misha Brukmanc88330a2005-04-21 23:38:14 +00001927
Chris Lattnerb0eef822005-01-11 23:33:00 +00001928 return Result;
1929 }
1930
Chris Lattner88c8a232005-01-07 07:49:41 +00001931 static const unsigned Opc[3] = {
1932 X86::MOVZX32rr8, X86::MOVZX32rr16, X86::MOVZX16rr8
1933 };
Chris Lattnerb0eef822005-01-11 23:33:00 +00001934 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner88c8a232005-01-07 07:49:41 +00001935 BuildMI(BB, Opc[SrcIs16+DestIs16*2], 1, Result).addReg(Tmp1);
1936 return Result;
Misha Brukmanc88330a2005-04-21 23:38:14 +00001937 }
Chris Lattner88c8a232005-01-07 07:49:41 +00001938 case ISD::SIGN_EXTEND: {
1939 int DestIs16 = N.getValueType() == MVT::i16;
1940 int SrcIs16 = N.getOperand(0).getValueType() == MVT::i16;
1941
Chris Lattner282781c2005-01-09 18:52:44 +00001942 // FIXME: Legalize should promote bools to i8!
1943 assert(N.getOperand(0).getValueType() != MVT::i1 &&
1944 "Sign extend from bool not implemented!");
1945
Chris Lattnera56d29d2005-01-17 06:26:58 +00001946 if (isFoldableLoad(N.getOperand(0), SDOperand())) {
Chris Lattnerb0eef822005-01-11 23:33:00 +00001947 static const unsigned Opc[3] = {
1948 X86::MOVSX32rm8, X86::MOVSX32rm16, X86::MOVSX16rm8
1949 };
1950
1951 X86AddressMode AM;
1952 EmitFoldedLoad(N.getOperand(0), AM);
1953 addFullAddress(BuildMI(BB, Opc[SrcIs16+DestIs16*2], 4, Result), AM);
1954 return Result;
1955 }
1956
Chris Lattner88c8a232005-01-07 07:49:41 +00001957 static const unsigned Opc[3] = {
1958 X86::MOVSX32rr8, X86::MOVSX32rr16, X86::MOVSX16rr8
1959 };
1960 Tmp1 = SelectExpr(N.getOperand(0));
1961 BuildMI(BB, Opc[SrcIs16+DestIs16*2], 1, Result).addReg(Tmp1);
1962 return Result;
1963 }
1964 case ISD::TRUNCATE:
Chris Lattnerb7fe57a2005-01-12 02:19:06 +00001965 // Fold TRUNCATE (LOAD P) into a smaller load from P.
Chris Lattner14947c32005-01-18 20:05:56 +00001966 // FIXME: This should be performed by the DAGCombiner.
Chris Lattnera56d29d2005-01-17 06:26:58 +00001967 if (isFoldableLoad(N.getOperand(0), SDOperand())) {
Chris Lattnerb7fe57a2005-01-12 02:19:06 +00001968 switch (N.getValueType()) {
1969 default: assert(0 && "Unknown truncate!");
1970 case MVT::i1:
1971 case MVT::i8: Opc = X86::MOV8rm; break;
1972 case MVT::i16: Opc = X86::MOV16rm; break;
1973 }
1974 X86AddressMode AM;
1975 EmitFoldedLoad(N.getOperand(0), AM);
1976 addFullAddress(BuildMI(BB, Opc, 4, Result), AM);
1977 return Result;
1978 }
1979
Chris Lattner88c8a232005-01-07 07:49:41 +00001980 // Handle cast of LARGER int to SMALLER int using a move to EAX followed by
1981 // a move out of AX or AL.
1982 switch (N.getOperand(0).getValueType()) {
1983 default: assert(0 && "Unknown truncate!");
1984 case MVT::i8: Tmp2 = X86::AL; Opc = X86::MOV8rr; break;
1985 case MVT::i16: Tmp2 = X86::AX; Opc = X86::MOV16rr; break;
1986 case MVT::i32: Tmp2 = X86::EAX; Opc = X86::MOV32rr; break;
1987 }
1988 Tmp1 = SelectExpr(N.getOperand(0));
1989 BuildMI(BB, Opc, 1, Tmp2).addReg(Tmp1);
1990
1991 switch (N.getValueType()) {
1992 default: assert(0 && "Unknown truncate!");
1993 case MVT::i1:
1994 case MVT::i8: Tmp2 = X86::AL; Opc = X86::MOV8rr; break;
1995 case MVT::i16: Tmp2 = X86::AX; Opc = X86::MOV16rr; break;
1996 }
1997 BuildMI(BB, Opc, 1, Result).addReg(Tmp2);
1998 return Result;
1999
Chris Lattner282781c2005-01-09 18:52:44 +00002000 case ISD::SINT_TO_FP:
2001 case ISD::UINT_TO_FP: {
2002 // FIXME: Most of this grunt work should be done by legalize!
Chris Lattnere44e6d12005-01-11 03:50:45 +00002003 ContainsFPCode = true;
Chris Lattner282781c2005-01-09 18:52:44 +00002004
2005 // Promote the integer to a type supported by FLD. We do this because there
2006 // are no unsigned FLD instructions, so we must promote an unsigned value to
2007 // a larger signed value, then use FLD on the larger value.
2008 //
2009 MVT::ValueType PromoteType = MVT::Other;
2010 MVT::ValueType SrcTy = N.getOperand(0).getValueType();
2011 unsigned PromoteOpcode = 0;
2012 unsigned RealDestReg = Result;
2013 switch (SrcTy) {
2014 case MVT::i1:
2015 case MVT::i8:
2016 // We don't have the facilities for directly loading byte sized data from
2017 // memory (even signed). Promote it to 16 bits.
2018 PromoteType = MVT::i16;
2019 PromoteOpcode = Node->getOpcode() == ISD::SINT_TO_FP ?
2020 X86::MOVSX16rr8 : X86::MOVZX16rr8;
2021 break;
2022 case MVT::i16:
2023 if (Node->getOpcode() == ISD::UINT_TO_FP) {
2024 PromoteType = MVT::i32;
2025 PromoteOpcode = X86::MOVZX32rr16;
2026 }
2027 break;
2028 default:
2029 // Don't fild into the real destination.
2030 if (Node->getOpcode() == ISD::UINT_TO_FP)
2031 Result = MakeReg(Node->getValueType(0));
2032 break;
2033 }
2034
2035 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
Misha Brukmanc88330a2005-04-21 23:38:14 +00002036
Chris Lattner282781c2005-01-09 18:52:44 +00002037 if (PromoteType != MVT::Other) {
2038 Tmp2 = MakeReg(PromoteType);
2039 BuildMI(BB, PromoteOpcode, 1, Tmp2).addReg(Tmp1);
2040 SrcTy = PromoteType;
2041 Tmp1 = Tmp2;
2042 }
2043
2044 // Spill the integer to memory and reload it from there.
2045 unsigned Size = MVT::getSizeInBits(SrcTy)/8;
2046 MachineFunction *F = BB->getParent();
2047 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, Size);
2048
2049 switch (SrcTy) {
Chris Lattner282781c2005-01-09 18:52:44 +00002050 case MVT::i32:
2051 addFrameReference(BuildMI(BB, X86::MOV32mr, 5),
2052 FrameIdx).addReg(Tmp1);
2053 addFrameReference(BuildMI(BB, X86::FILD32m, 5, Result), FrameIdx);
2054 break;
2055 case MVT::i16:
2056 addFrameReference(BuildMI(BB, X86::MOV16mr, 5),
2057 FrameIdx).addReg(Tmp1);
2058 addFrameReference(BuildMI(BB, X86::FILD16m, 5, Result), FrameIdx);
2059 break;
2060 default: break; // No promotion required.
2061 }
2062
Chris Lattnerf8f79c42005-01-12 04:00:00 +00002063 if (Node->getOpcode() == ISD::UINT_TO_FP && Result != RealDestReg) {
Chris Lattner282781c2005-01-09 18:52:44 +00002064 // If this is a cast from uint -> double, we need to be careful when if
2065 // the "sign" bit is set. If so, we don't want to make a negative number,
2066 // we want to make a positive number. Emit code to add an offset if the
2067 // sign bit is set.
2068
2069 // Compute whether the sign bit is set by shifting the reg right 31 bits.
2070 unsigned IsNeg = MakeReg(MVT::i32);
2071 BuildMI(BB, X86::SHR32ri, 2, IsNeg).addReg(Tmp1).addImm(31);
2072
2073 // Create a CP value that has the offset in one word and 0 in the other.
2074 static ConstantInt *TheOffset = ConstantUInt::get(Type::ULongTy,
2075 0x4f80000000000000ULL);
2076 unsigned CPI = F->getConstantPool()->getConstantPoolIndex(TheOffset);
2077 BuildMI(BB, X86::FADD32m, 5, RealDestReg).addReg(Result)
2078 .addConstantPoolIndex(CPI).addZImm(4).addReg(IsNeg).addSImm(0);
Chris Lattner282781c2005-01-09 18:52:44 +00002079 }
2080 return RealDestReg;
2081 }
2082 case ISD::FP_TO_SINT:
2083 case ISD::FP_TO_UINT: {
2084 // FIXME: Most of this grunt work should be done by legalize!
2085 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
2086
2087 // Change the floating point control register to use "round towards zero"
2088 // mode when truncating to an integer value.
2089 //
2090 MachineFunction *F = BB->getParent();
2091 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
2092 addFrameReference(BuildMI(BB, X86::FNSTCW16m, 4), CWFrameIdx);
2093
2094 // Load the old value of the high byte of the control word...
2095 unsigned HighPartOfCW = MakeReg(MVT::i8);
2096 addFrameReference(BuildMI(BB, X86::MOV8rm, 4, HighPartOfCW),
2097 CWFrameIdx, 1);
2098
2099 // Set the high part to be round to zero...
2100 addFrameReference(BuildMI(BB, X86::MOV8mi, 5),
2101 CWFrameIdx, 1).addImm(12);
2102
2103 // Reload the modified control word now...
2104 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
Misha Brukmanc88330a2005-04-21 23:38:14 +00002105
Chris Lattner282781c2005-01-09 18:52:44 +00002106 // Restore the memory image of control word to original value
2107 addFrameReference(BuildMI(BB, X86::MOV8mr, 5),
2108 CWFrameIdx, 1).addReg(HighPartOfCW);
2109
2110 // We don't have the facilities for directly storing byte sized data to
2111 // memory. Promote it to 16 bits. We also must promote unsigned values to
2112 // larger classes because we only have signed FP stores.
2113 MVT::ValueType StoreClass = Node->getValueType(0);
2114 if (StoreClass == MVT::i8 || Node->getOpcode() == ISD::FP_TO_UINT)
2115 switch (StoreClass) {
Chris Lattnere6266112005-05-09 05:33:18 +00002116 case MVT::i1:
Chris Lattner282781c2005-01-09 18:52:44 +00002117 case MVT::i8: StoreClass = MVT::i16; break;
2118 case MVT::i16: StoreClass = MVT::i32; break;
2119 case MVT::i32: StoreClass = MVT::i64; break;
Chris Lattner282781c2005-01-09 18:52:44 +00002120 default: assert(0 && "Unknown store class!");
2121 }
2122
2123 // Spill the integer to memory and reload it from there.
2124 unsigned Size = MVT::getSizeInBits(StoreClass)/8;
2125 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, Size);
2126
2127 switch (StoreClass) {
2128 default: assert(0 && "Unknown store class!");
2129 case MVT::i16:
2130 addFrameReference(BuildMI(BB, X86::FIST16m, 5), FrameIdx).addReg(Tmp1);
2131 break;
2132 case MVT::i32:
Chris Lattner66d34302005-01-09 19:49:59 +00002133 addFrameReference(BuildMI(BB, X86::FIST32m, 5), FrameIdx).addReg(Tmp1);
Chris Lattner282781c2005-01-09 18:52:44 +00002134 break;
Chris Lattner4ccd1f62005-05-09 18:37:02 +00002135 case MVT::i64:
2136 addFrameReference(BuildMI(BB, X86::FISTP64m, 5), FrameIdx).addReg(Tmp1);
2137 break; }
Chris Lattner282781c2005-01-09 18:52:44 +00002138
2139 switch (Node->getValueType(0)) {
2140 default:
2141 assert(0 && "Unknown integer type!");
Chris Lattner282781c2005-01-09 18:52:44 +00002142 case MVT::i32:
2143 addFrameReference(BuildMI(BB, X86::MOV32rm, 4, Result), FrameIdx);
2144 break;
2145 case MVT::i16:
2146 addFrameReference(BuildMI(BB, X86::MOV16rm, 4, Result), FrameIdx);
2147 break;
2148 case MVT::i8:
Chris Lattnere6266112005-05-09 05:33:18 +00002149 case MVT::i1:
Chris Lattner282781c2005-01-09 18:52:44 +00002150 addFrameReference(BuildMI(BB, X86::MOV8rm, 4, Result), FrameIdx);
2151 break;
2152 }
2153
2154 // Reload the original control word now.
2155 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
2156 return Result;
2157 }
Chris Lattner88c8a232005-01-07 07:49:41 +00002158 case ISD::ADD:
Chris Lattner62b22422005-01-11 21:19:59 +00002159 Op0 = N.getOperand(0);
2160 Op1 = N.getOperand(1);
2161
Chris Lattner30607ec2005-01-25 20:03:11 +00002162 if (isFoldableLoad(Op0, Op1, true)) {
Chris Lattner62b22422005-01-11 21:19:59 +00002163 std::swap(Op0, Op1);
Chris Lattnera56d29d2005-01-17 06:26:58 +00002164 goto FoldAdd;
2165 }
Chris Lattner62b22422005-01-11 21:19:59 +00002166
Chris Lattner30607ec2005-01-25 20:03:11 +00002167 if (isFoldableLoad(Op1, Op0, true)) {
Chris Lattnera56d29d2005-01-17 06:26:58 +00002168 FoldAdd:
Chris Lattner62b22422005-01-11 21:19:59 +00002169 switch (N.getValueType()) {
2170 default: assert(0 && "Cannot add this type!");
2171 case MVT::i1:
2172 case MVT::i8: Opc = X86::ADD8rm; break;
2173 case MVT::i16: Opc = X86::ADD16rm; break;
2174 case MVT::i32: Opc = X86::ADD32rm; break;
Chris Lattner30607ec2005-01-25 20:03:11 +00002175 case MVT::f64:
2176 // For F64, handle promoted load operations (from F32) as well!
2177 Opc = Op1.getOpcode() == ISD::LOAD ? X86::FADD64m : X86::FADD32m;
2178 break;
Chris Lattner62b22422005-01-11 21:19:59 +00002179 }
2180 X86AddressMode AM;
Chris Lattner3676cd62005-01-13 05:53:16 +00002181 EmitFoldedLoad(Op1, AM);
2182 Tmp1 = SelectExpr(Op0);
Chris Lattner62b22422005-01-11 21:19:59 +00002183 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
2184 return Result;
2185 }
2186
Chris Lattner88c8a232005-01-07 07:49:41 +00002187 // See if we can codegen this as an LEA to fold operations together.
2188 if (N.getValueType() == MVT::i32) {
Chris Lattnerd7f93952005-01-18 02:25:52 +00002189 ExprMap.erase(N);
Chris Lattnera7acdda2005-01-18 01:06:26 +00002190 X86ISelAddressMode AM;
Chris Lattnerd7f93952005-01-18 02:25:52 +00002191 MatchAddress(N, AM);
2192 ExprMap[N] = Result;
2193
2194 // If this is not just an add, emit the LEA. For a simple add (like
2195 // reg+reg or reg+imm), we just emit an add. It might be a good idea to
2196 // leave this as LEA, then peephole it to 'ADD' after two address elim
2197 // happens.
2198 if (AM.Scale != 1 || AM.BaseType == X86ISelAddressMode::FrameIndexBase||
2199 AM.GV || (AM.Base.Reg.Val && AM.IndexReg.Val && AM.Disp)) {
2200 X86AddressMode XAM = SelectAddrExprs(AM);
2201 addFullAddress(BuildMI(BB, X86::LEA32r, 4, Result), XAM);
2202 return Result;
Chris Lattner88c8a232005-01-07 07:49:41 +00002203 }
2204 }
Chris Lattner0d1f82a2005-01-11 03:11:44 +00002205
Chris Lattner62b22422005-01-11 21:19:59 +00002206 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1)) {
Chris Lattner88c8a232005-01-07 07:49:41 +00002207 Opc = 0;
2208 if (CN->getValue() == 1) { // add X, 1 -> inc X
2209 switch (N.getValueType()) {
2210 default: assert(0 && "Cannot integer add this type!");
2211 case MVT::i8: Opc = X86::INC8r; break;
2212 case MVT::i16: Opc = X86::INC16r; break;
2213 case MVT::i32: Opc = X86::INC32r; break;
2214 }
2215 } else if (CN->isAllOnesValue()) { // add X, -1 -> dec X
2216 switch (N.getValueType()) {
2217 default: assert(0 && "Cannot integer add this type!");
2218 case MVT::i8: Opc = X86::DEC8r; break;
2219 case MVT::i16: Opc = X86::DEC16r; break;
2220 case MVT::i32: Opc = X86::DEC32r; break;
2221 }
2222 }
2223
2224 if (Opc) {
Chris Lattner62b22422005-01-11 21:19:59 +00002225 Tmp1 = SelectExpr(Op0);
Chris Lattner88c8a232005-01-07 07:49:41 +00002226 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
2227 return Result;
2228 }
2229
2230 switch (N.getValueType()) {
2231 default: assert(0 && "Cannot add this type!");
2232 case MVT::i8: Opc = X86::ADD8ri; break;
2233 case MVT::i16: Opc = X86::ADD16ri; break;
2234 case MVT::i32: Opc = X86::ADD32ri; break;
2235 }
2236 if (Opc) {
Chris Lattner62b22422005-01-11 21:19:59 +00002237 Tmp1 = SelectExpr(Op0);
Chris Lattner88c8a232005-01-07 07:49:41 +00002238 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
2239 return Result;
2240 }
2241 }
2242
Chris Lattner88c8a232005-01-07 07:49:41 +00002243 switch (N.getValueType()) {
2244 default: assert(0 && "Cannot add this type!");
2245 case MVT::i8: Opc = X86::ADD8rr; break;
2246 case MVT::i16: Opc = X86::ADD16rr; break;
2247 case MVT::i32: Opc = X86::ADD32rr; break;
Chris Lattnere44e6d12005-01-11 03:50:45 +00002248 case MVT::f64: Opc = X86::FpADD; break;
Chris Lattner88c8a232005-01-07 07:49:41 +00002249 }
Chris Lattner0d1f82a2005-01-11 03:11:44 +00002250
Chris Lattner62b22422005-01-11 21:19:59 +00002251 if (getRegPressure(Op0) > getRegPressure(Op1)) {
2252 Tmp1 = SelectExpr(Op0);
2253 Tmp2 = SelectExpr(Op1);
Chris Lattner0d1f82a2005-01-11 03:11:44 +00002254 } else {
Chris Lattner62b22422005-01-11 21:19:59 +00002255 Tmp2 = SelectExpr(Op1);
2256 Tmp1 = SelectExpr(Op0);
Chris Lattner0d1f82a2005-01-11 03:11:44 +00002257 }
2258
Chris Lattner88c8a232005-01-07 07:49:41 +00002259 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
2260 return Result;
Chris Lattner0e0b5992005-04-02 05:30:17 +00002261
2262 case ISD::FABS:
Chris Lattner0e0b5992005-04-02 05:30:17 +00002263 case ISD::FNEG:
Chris Lattnerdb68d392005-04-30 04:25:35 +00002264 case ISD::FSIN:
2265 case ISD::FCOS:
Chris Lattner014d2c42005-04-28 22:07:18 +00002266 case ISD::FSQRT:
2267 assert(N.getValueType()==MVT::f64 && "Illegal type for this operation");
Chris Lattner0e0b5992005-04-02 05:30:17 +00002268 Tmp1 = SelectExpr(Node->getOperand(0));
Chris Lattner014d2c42005-04-28 22:07:18 +00002269 switch (N.getOpcode()) {
2270 default: assert(0 && "Unreachable!");
2271 case ISD::FABS: BuildMI(BB, X86::FABS, 1, Result).addReg(Tmp1); break;
2272 case ISD::FNEG: BuildMI(BB, X86::FCHS, 1, Result).addReg(Tmp1); break;
2273 case ISD::FSQRT: BuildMI(BB, X86::FSQRT, 1, Result).addReg(Tmp1); break;
Chris Lattnerdb68d392005-04-30 04:25:35 +00002274 case ISD::FSIN: BuildMI(BB, X86::FSIN, 1, Result).addReg(Tmp1); break;
2275 case ISD::FCOS: BuildMI(BB, X86::FCOS, 1, Result).addReg(Tmp1); break;
Chris Lattner014d2c42005-04-28 22:07:18 +00002276 }
Chris Lattner0e0b5992005-04-02 05:30:17 +00002277 return Result;
2278
Chris Lattner4fbb4af2005-04-06 04:21:07 +00002279 case ISD::MULHU:
2280 switch (N.getValueType()) {
2281 default: assert(0 && "Unsupported VT!");
2282 case MVT::i8: Tmp2 = X86::MUL8r; break;
2283 case MVT::i16: Tmp2 = X86::MUL16r; break;
2284 case MVT::i32: Tmp2 = X86::MUL32r; break;
2285 }
2286 // FALL THROUGH
2287 case ISD::MULHS: {
2288 unsigned MovOpc, LowReg, HiReg;
2289 switch (N.getValueType()) {
2290 default: assert(0 && "Unsupported VT!");
Misha Brukmanc88330a2005-04-21 23:38:14 +00002291 case MVT::i8:
Chris Lattner4fbb4af2005-04-06 04:21:07 +00002292 MovOpc = X86::MOV8rr;
2293 LowReg = X86::AL;
2294 HiReg = X86::AH;
2295 Opc = X86::IMUL8r;
2296 break;
2297 case MVT::i16:
2298 MovOpc = X86::MOV16rr;
2299 LowReg = X86::AX;
2300 HiReg = X86::DX;
2301 Opc = X86::IMUL16r;
2302 break;
2303 case MVT::i32:
2304 MovOpc = X86::MOV32rr;
2305 LowReg = X86::EAX;
2306 HiReg = X86::EDX;
2307 Opc = X86::IMUL32r;
2308 break;
2309 }
2310 if (Node->getOpcode() != ISD::MULHS)
2311 Opc = Tmp2; // Get the MULHU opcode.
2312
2313 Op0 = Node->getOperand(0);
2314 Op1 = Node->getOperand(1);
2315 if (getRegPressure(Op0) > getRegPressure(Op1)) {
2316 Tmp1 = SelectExpr(Op0);
2317 Tmp2 = SelectExpr(Op1);
2318 } else {
2319 Tmp2 = SelectExpr(Op1);
2320 Tmp1 = SelectExpr(Op0);
2321 }
2322
2323 // FIXME: Implement folding of loads into the memory operands here!
2324 BuildMI(BB, MovOpc, 1, LowReg).addReg(Tmp1);
2325 BuildMI(BB, Opc, 1).addReg(Tmp2);
2326 BuildMI(BB, MovOpc, 1, Result).addReg(HiReg);
2327 return Result;
Misha Brukmanc88330a2005-04-21 23:38:14 +00002328 }
Chris Lattner4fbb4af2005-04-06 04:21:07 +00002329
Chris Lattner88c8a232005-01-07 07:49:41 +00002330 case ISD::SUB:
Chris Lattner62b22422005-01-11 21:19:59 +00002331 case ISD::MUL:
2332 case ISD::AND:
2333 case ISD::OR:
Chris Lattnerefe90202005-01-12 04:23:22 +00002334 case ISD::XOR: {
Chris Lattner62b22422005-01-11 21:19:59 +00002335 static const unsigned SUBTab[] = {
2336 X86::SUB8ri, X86::SUB16ri, X86::SUB32ri, 0, 0,
2337 X86::SUB8rm, X86::SUB16rm, X86::SUB32rm, X86::FSUB32m, X86::FSUB64m,
2338 X86::SUB8rr, X86::SUB16rr, X86::SUB32rr, X86::FpSUB , X86::FpSUB,
2339 };
2340 static const unsigned MULTab[] = {
2341 0, X86::IMUL16rri, X86::IMUL32rri, 0, 0,
2342 0, X86::IMUL16rm , X86::IMUL32rm, X86::FMUL32m, X86::FMUL64m,
2343 0, X86::IMUL16rr , X86::IMUL32rr, X86::FpMUL , X86::FpMUL,
2344 };
2345 static const unsigned ANDTab[] = {
2346 X86::AND8ri, X86::AND16ri, X86::AND32ri, 0, 0,
2347 X86::AND8rm, X86::AND16rm, X86::AND32rm, 0, 0,
Misha Brukmanc88330a2005-04-21 23:38:14 +00002348 X86::AND8rr, X86::AND16rr, X86::AND32rr, 0, 0,
Chris Lattner62b22422005-01-11 21:19:59 +00002349 };
2350 static const unsigned ORTab[] = {
2351 X86::OR8ri, X86::OR16ri, X86::OR32ri, 0, 0,
2352 X86::OR8rm, X86::OR16rm, X86::OR32rm, 0, 0,
2353 X86::OR8rr, X86::OR16rr, X86::OR32rr, 0, 0,
2354 };
2355 static const unsigned XORTab[] = {
2356 X86::XOR8ri, X86::XOR16ri, X86::XOR32ri, 0, 0,
2357 X86::XOR8rm, X86::XOR16rm, X86::XOR32rm, 0, 0,
2358 X86::XOR8rr, X86::XOR16rr, X86::XOR32rr, 0, 0,
2359 };
2360
2361 Op0 = Node->getOperand(0);
2362 Op1 = Node->getOperand(1);
2363
Chris Lattner29f58192005-01-19 07:37:26 +00002364 if (Node->getOpcode() == ISD::OR && Op0.hasOneUse() && Op1.hasOneUse())
2365 if (EmitOrOpOp(Op0, Op1, Result)) // Match SHLD, SHRD, and rotates.
Chris Lattner41fe2012005-01-19 06:18:43 +00002366 return Result;
2367
2368 if (Node->getOpcode() == ISD::SUB)
Chris Lattner88c8a232005-01-07 07:49:41 +00002369 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(0)))
2370 if (CN->isNullValue()) { // 0 - N -> neg N
2371 switch (N.getValueType()) {
2372 default: assert(0 && "Cannot sub this type!");
2373 case MVT::i1:
2374 case MVT::i8: Opc = X86::NEG8r; break;
2375 case MVT::i16: Opc = X86::NEG16r; break;
2376 case MVT::i32: Opc = X86::NEG32r; break;
2377 }
2378 Tmp1 = SelectExpr(N.getOperand(1));
2379 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
2380 return Result;
2381 }
2382
Chris Lattner62b22422005-01-11 21:19:59 +00002383 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1)) {
2384 if (CN->isAllOnesValue() && Node->getOpcode() == ISD::XOR) {
Chris Lattner0cd6b9a2005-01-17 00:23:16 +00002385 Opc = 0;
Chris Lattner9d7cf992005-01-11 04:31:30 +00002386 switch (N.getValueType()) {
2387 default: assert(0 && "Cannot add this type!");
Chris Lattner0cd6b9a2005-01-17 00:23:16 +00002388 case MVT::i1: break; // Not supported, don't invert upper bits!
Chris Lattner9d7cf992005-01-11 04:31:30 +00002389 case MVT::i8: Opc = X86::NOT8r; break;
2390 case MVT::i16: Opc = X86::NOT16r; break;
2391 case MVT::i32: Opc = X86::NOT32r; break;
2392 }
Chris Lattner0cd6b9a2005-01-17 00:23:16 +00002393 if (Opc) {
2394 Tmp1 = SelectExpr(Op0);
2395 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
2396 return Result;
2397 }
Chris Lattner9d7cf992005-01-11 04:31:30 +00002398 }
2399
Chris Lattnerb72ea1b2005-01-17 06:48:02 +00002400 // Fold common multiplies into LEA instructions.
2401 if (Node->getOpcode() == ISD::MUL && N.getValueType() == MVT::i32) {
2402 switch ((int)CN->getValue()) {
2403 default: break;
2404 case 3:
2405 case 5:
2406 case 9:
Chris Lattnerb72ea1b2005-01-17 06:48:02 +00002407 // Remove N from exprmap so SelectAddress doesn't get confused.
2408 ExprMap.erase(N);
Chris Lattnera7acdda2005-01-18 01:06:26 +00002409 X86AddressMode AM;
Chris Lattnerb72ea1b2005-01-17 06:48:02 +00002410 SelectAddress(N, AM);
2411 // Restore it to the map.
2412 ExprMap[N] = Result;
2413 addFullAddress(BuildMI(BB, X86::LEA32r, 4, Result), AM);
2414 return Result;
2415 }
2416 }
2417
Chris Lattner88c8a232005-01-07 07:49:41 +00002418 switch (N.getValueType()) {
Chris Lattner9d7cf992005-01-11 04:31:30 +00002419 default: assert(0 && "Cannot xor this type!");
Chris Lattner88c8a232005-01-07 07:49:41 +00002420 case MVT::i1:
Chris Lattner62b22422005-01-11 21:19:59 +00002421 case MVT::i8: Opc = 0; break;
2422 case MVT::i16: Opc = 1; break;
2423 case MVT::i32: Opc = 2; break;
Chris Lattner88c8a232005-01-07 07:49:41 +00002424 }
Chris Lattner62b22422005-01-11 21:19:59 +00002425 switch (Node->getOpcode()) {
2426 default: assert(0 && "Unreachable!");
2427 case ISD::SUB: Opc = SUBTab[Opc]; break;
2428 case ISD::MUL: Opc = MULTab[Opc]; break;
2429 case ISD::AND: Opc = ANDTab[Opc]; break;
2430 case ISD::OR: Opc = ORTab[Opc]; break;
2431 case ISD::XOR: Opc = XORTab[Opc]; break;
Chris Lattner88c8a232005-01-07 07:49:41 +00002432 }
Chris Lattner62b22422005-01-11 21:19:59 +00002433 if (Opc) { // Can't fold MUL:i8 R, imm
2434 Tmp1 = SelectExpr(Op0);
Chris Lattner88c8a232005-01-07 07:49:41 +00002435 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
2436 return Result;
2437 }
2438 }
Chris Lattner0d1f82a2005-01-11 03:11:44 +00002439
Chris Lattner30607ec2005-01-25 20:03:11 +00002440 if (isFoldableLoad(Op0, Op1, true))
Chris Lattner62b22422005-01-11 21:19:59 +00002441 if (Node->getOpcode() != ISD::SUB) {
2442 std::swap(Op0, Op1);
Chris Lattnera56d29d2005-01-17 06:26:58 +00002443 goto FoldOps;
Chris Lattner62b22422005-01-11 21:19:59 +00002444 } else {
Chris Lattner30607ec2005-01-25 20:03:11 +00002445 // For FP, emit 'reverse' subract, with a memory operand.
2446 if (N.getValueType() == MVT::f64) {
2447 if (Op0.getOpcode() == ISD::EXTLOAD)
2448 Opc = X86::FSUBR32m;
2449 else
2450 Opc = X86::FSUBR64m;
2451
Chris Lattner62b22422005-01-11 21:19:59 +00002452 X86AddressMode AM;
Chris Lattner3676cd62005-01-13 05:53:16 +00002453 EmitFoldedLoad(Op0, AM);
2454 Tmp1 = SelectExpr(Op1);
Chris Lattner62b22422005-01-11 21:19:59 +00002455 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
2456 return Result;
2457 }
2458 }
2459
Chris Lattner30607ec2005-01-25 20:03:11 +00002460 if (isFoldableLoad(Op1, Op0, true)) {
Chris Lattnera56d29d2005-01-17 06:26:58 +00002461 FoldOps:
Chris Lattner62b22422005-01-11 21:19:59 +00002462 switch (N.getValueType()) {
2463 default: assert(0 && "Cannot operate on this type!");
2464 case MVT::i1:
2465 case MVT::i8: Opc = 5; break;
2466 case MVT::i16: Opc = 6; break;
2467 case MVT::i32: Opc = 7; break;
Chris Lattner30607ec2005-01-25 20:03:11 +00002468 // For F64, handle promoted load operations (from F32) as well!
2469 case MVT::f64: Opc = Op1.getOpcode() == ISD::LOAD ? 9 : 8; break;
Chris Lattner62b22422005-01-11 21:19:59 +00002470 }
2471 switch (Node->getOpcode()) {
2472 default: assert(0 && "Unreachable!");
2473 case ISD::SUB: Opc = SUBTab[Opc]; break;
2474 case ISD::MUL: Opc = MULTab[Opc]; break;
2475 case ISD::AND: Opc = ANDTab[Opc]; break;
2476 case ISD::OR: Opc = ORTab[Opc]; break;
2477 case ISD::XOR: Opc = XORTab[Opc]; break;
2478 }
2479
2480 X86AddressMode AM;
Chris Lattner3676cd62005-01-13 05:53:16 +00002481 EmitFoldedLoad(Op1, AM);
2482 Tmp1 = SelectExpr(Op0);
Chris Lattner62b22422005-01-11 21:19:59 +00002483 if (Opc) {
2484 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
2485 } else {
2486 assert(Node->getOpcode() == ISD::MUL &&
2487 N.getValueType() == MVT::i8 && "Unexpected situation!");
2488 // Must use the MUL instruction, which forces use of AL.
2489 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(Tmp1);
2490 addFullAddress(BuildMI(BB, X86::MUL8m, 1), AM);
2491 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
2492 }
2493 return Result;
Chris Lattner0d1f82a2005-01-11 03:11:44 +00002494 }
Chris Lattner62b22422005-01-11 21:19:59 +00002495
2496 if (getRegPressure(Op0) > getRegPressure(Op1)) {
2497 Tmp1 = SelectExpr(Op0);
2498 Tmp2 = SelectExpr(Op1);
2499 } else {
2500 Tmp2 = SelectExpr(Op1);
2501 Tmp1 = SelectExpr(Op0);
2502 }
2503
Chris Lattner88c8a232005-01-07 07:49:41 +00002504 switch (N.getValueType()) {
2505 default: assert(0 && "Cannot add this type!");
Chris Lattner62b22422005-01-11 21:19:59 +00002506 case MVT::i1:
2507 case MVT::i8: Opc = 10; break;
2508 case MVT::i16: Opc = 11; break;
2509 case MVT::i32: Opc = 12; break;
2510 case MVT::f32: Opc = 13; break;
2511 case MVT::f64: Opc = 14; break;
2512 }
2513 switch (Node->getOpcode()) {
2514 default: assert(0 && "Unreachable!");
2515 case ISD::SUB: Opc = SUBTab[Opc]; break;
2516 case ISD::MUL: Opc = MULTab[Opc]; break;
2517 case ISD::AND: Opc = ANDTab[Opc]; break;
2518 case ISD::OR: Opc = ORTab[Opc]; break;
2519 case ISD::XOR: Opc = XORTab[Opc]; break;
2520 }
2521 if (Opc) {
2522 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
2523 } else {
2524 assert(Node->getOpcode() == ISD::MUL &&
2525 N.getValueType() == MVT::i8 && "Unexpected situation!");
Chris Lattner750d38b2005-01-10 20:55:48 +00002526 // Must use the MUL instruction, which forces use of AL.
2527 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(Tmp1);
2528 BuildMI(BB, X86::MUL8r, 1).addReg(Tmp2);
2529 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
Chris Lattner88c8a232005-01-07 07:49:41 +00002530 }
Chris Lattner88c8a232005-01-07 07:49:41 +00002531 return Result;
Chris Lattnerefe90202005-01-12 04:23:22 +00002532 }
Chris Lattner2a631fa2005-01-20 18:53:00 +00002533 case ISD::ADD_PARTS:
2534 case ISD::SUB_PARTS: {
2535 assert(N.getNumOperands() == 4 && N.getValueType() == MVT::i32 &&
2536 "Not an i64 add/sub!");
2537 // Emit all of the operands.
2538 std::vector<unsigned> InVals;
2539 for (unsigned i = 0, e = N.getNumOperands(); i != e; ++i)
2540 InVals.push_back(SelectExpr(N.getOperand(i)));
2541 if (N.getOpcode() == ISD::ADD_PARTS) {
2542 BuildMI(BB, X86::ADD32rr, 2, Result).addReg(InVals[0]).addReg(InVals[2]);
2543 BuildMI(BB, X86::ADC32rr,2,Result+1).addReg(InVals[1]).addReg(InVals[3]);
2544 } else {
2545 BuildMI(BB, X86::SUB32rr, 2, Result).addReg(InVals[0]).addReg(InVals[2]);
2546 BuildMI(BB, X86::SBB32rr, 2,Result+1).addReg(InVals[1]).addReg(InVals[3]);
2547 }
2548 return Result+N.ResNo;
2549 }
2550
Chris Lattnera31d4c72005-04-02 04:01:14 +00002551 case ISD::SHL_PARTS:
2552 case ISD::SRA_PARTS:
2553 case ISD::SRL_PARTS: {
2554 assert(N.getNumOperands() == 3 && N.getValueType() == MVT::i32 &&
2555 "Not an i64 shift!");
2556 unsigned ShiftOpLo = SelectExpr(N.getOperand(0));
2557 unsigned ShiftOpHi = SelectExpr(N.getOperand(1));
2558 unsigned TmpReg = MakeReg(MVT::i32);
2559 if (N.getOpcode() == ISD::SRA_PARTS) {
2560 // If this is a SHR of a Long, then we need to do funny sign extension
2561 // stuff. TmpReg gets the value to use as the high-part if we are
2562 // shifting more than 32 bits.
2563 BuildMI(BB, X86::SAR32ri, 2, TmpReg).addReg(ShiftOpHi).addImm(31);
2564 } else {
2565 // Other shifts use a fixed zero value if the shift is more than 32 bits.
2566 BuildMI(BB, X86::MOV32ri, 1, TmpReg).addImm(0);
2567 }
2568
2569 // Initialize CL with the shift amount.
2570 unsigned ShiftAmountReg = SelectExpr(N.getOperand(2));
2571 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShiftAmountReg);
2572
2573 unsigned TmpReg2 = MakeReg(MVT::i32);
2574 unsigned TmpReg3 = MakeReg(MVT::i32);
2575 if (N.getOpcode() == ISD::SHL_PARTS) {
2576 // TmpReg2 = shld inHi, inLo
2577 BuildMI(BB, X86::SHLD32rrCL, 2,TmpReg2).addReg(ShiftOpHi)
2578 .addReg(ShiftOpLo);
2579 // TmpReg3 = shl inLo, CL
2580 BuildMI(BB, X86::SHL32rCL, 1, TmpReg3).addReg(ShiftOpLo);
Misha Brukmanc88330a2005-04-21 23:38:14 +00002581
Chris Lattnera31d4c72005-04-02 04:01:14 +00002582 // Set the flags to indicate whether the shift was by more than 32 bits.
2583 BuildMI(BB, X86::TEST8ri, 2).addReg(X86::CL).addImm(32);
Misha Brukmanc88330a2005-04-21 23:38:14 +00002584
Chris Lattnera31d4c72005-04-02 04:01:14 +00002585 // DestHi = (>32) ? TmpReg3 : TmpReg2;
Misha Brukmanc88330a2005-04-21 23:38:14 +00002586 BuildMI(BB, X86::CMOVNE32rr, 2,
Chris Lattnera31d4c72005-04-02 04:01:14 +00002587 Result+1).addReg(TmpReg2).addReg(TmpReg3);
2588 // DestLo = (>32) ? TmpReg : TmpReg3;
2589 BuildMI(BB, X86::CMOVNE32rr, 2,
2590 Result).addReg(TmpReg3).addReg(TmpReg);
2591 } else {
2592 // TmpReg2 = shrd inLo, inHi
2593 BuildMI(BB, X86::SHRD32rrCL,2,TmpReg2).addReg(ShiftOpLo)
2594 .addReg(ShiftOpHi);
2595 // TmpReg3 = s[ah]r inHi, CL
Misha Brukmanc88330a2005-04-21 23:38:14 +00002596 BuildMI(BB, N.getOpcode() == ISD::SRA_PARTS ? X86::SAR32rCL
Chris Lattnera31d4c72005-04-02 04:01:14 +00002597 : X86::SHR32rCL, 1, TmpReg3)
2598 .addReg(ShiftOpHi);
Misha Brukmanc88330a2005-04-21 23:38:14 +00002599
Chris Lattnera31d4c72005-04-02 04:01:14 +00002600 // Set the flags to indicate whether the shift was by more than 32 bits.
2601 BuildMI(BB, X86::TEST8ri, 2).addReg(X86::CL).addImm(32);
Misha Brukmanc88330a2005-04-21 23:38:14 +00002602
Chris Lattnera31d4c72005-04-02 04:01:14 +00002603 // DestLo = (>32) ? TmpReg3 : TmpReg2;
Misha Brukmanc88330a2005-04-21 23:38:14 +00002604 BuildMI(BB, X86::CMOVNE32rr, 2,
Chris Lattnera31d4c72005-04-02 04:01:14 +00002605 Result).addReg(TmpReg2).addReg(TmpReg3);
Misha Brukmanc88330a2005-04-21 23:38:14 +00002606
Chris Lattnera31d4c72005-04-02 04:01:14 +00002607 // DestHi = (>32) ? TmpReg : TmpReg3;
Misha Brukmanc88330a2005-04-21 23:38:14 +00002608 BuildMI(BB, X86::CMOVNE32rr, 2,
Chris Lattnera31d4c72005-04-02 04:01:14 +00002609 Result+1).addReg(TmpReg3).addReg(TmpReg);
2610 }
2611 return Result+N.ResNo;
2612 }
2613
Chris Lattner88c8a232005-01-07 07:49:41 +00002614 case ISD::SELECT:
Chris Lattnerb14a63a2005-01-16 07:34:08 +00002615 if (getRegPressure(N.getOperand(1)) > getRegPressure(N.getOperand(2))) {
2616 Tmp2 = SelectExpr(N.getOperand(1));
2617 Tmp3 = SelectExpr(N.getOperand(2));
Chris Lattner88c8a232005-01-07 07:49:41 +00002618 } else {
Chris Lattnerb14a63a2005-01-16 07:34:08 +00002619 Tmp3 = SelectExpr(N.getOperand(2));
2620 Tmp2 = SelectExpr(N.getOperand(1));
Chris Lattner88c8a232005-01-07 07:49:41 +00002621 }
Chris Lattnerb14a63a2005-01-16 07:34:08 +00002622 EmitSelectCC(N.getOperand(0), N.getValueType(), Tmp2, Tmp3, Result);
2623 return Result;
Chris Lattner88c8a232005-01-07 07:49:41 +00002624
2625 case ISD::SDIV:
2626 case ISD::UDIV:
2627 case ISD::SREM:
2628 case ISD::UREM: {
Chris Lattnerb14a63a2005-01-16 07:34:08 +00002629 assert((N.getOpcode() != ISD::SREM || MVT::isInteger(N.getValueType())) &&
2630 "We don't support this operator!");
2631
Chris Lattner60c23bd2005-04-13 03:29:53 +00002632 if (N.getOpcode() == ISD::SDIV) {
Chris Lattner1b206152005-01-25 20:35:10 +00002633 // We can fold loads into FpDIVs, but not really into any others.
2634 if (N.getValueType() == MVT::f64) {
2635 // Check for reversed and unreversed DIV.
2636 if (isFoldableLoad(N.getOperand(0), N.getOperand(1), true)) {
2637 if (N.getOperand(0).getOpcode() == ISD::EXTLOAD)
2638 Opc = X86::FDIVR32m;
2639 else
2640 Opc = X86::FDIVR64m;
2641 X86AddressMode AM;
2642 EmitFoldedLoad(N.getOperand(0), AM);
2643 Tmp1 = SelectExpr(N.getOperand(1));
2644 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
2645 return Result;
2646 } else if (isFoldableLoad(N.getOperand(1), N.getOperand(0), true) &&
2647 N.getOperand(1).getOpcode() == ISD::LOAD) {
2648 if (N.getOperand(1).getOpcode() == ISD::EXTLOAD)
2649 Opc = X86::FDIV32m;
2650 else
2651 Opc = X86::FDIV64m;
2652 X86AddressMode AM;
2653 EmitFoldedLoad(N.getOperand(1), AM);
2654 Tmp1 = SelectExpr(N.getOperand(0));
2655 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
2656 return Result;
2657 }
2658 }
2659
Chris Lattner88c8a232005-01-07 07:49:41 +00002660 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
2661 // FIXME: These special cases should be handled by the lowering impl!
2662 unsigned RHS = CN->getValue();
2663 bool isNeg = false;
2664 if ((int)RHS < 0) {
2665 isNeg = true;
2666 RHS = -RHS;
2667 }
2668 if (RHS && (RHS & (RHS-1)) == 0) { // Signed division by power of 2?
2669 unsigned Log = log2(RHS);
2670 unsigned TmpReg = MakeReg(N.getValueType());
2671 unsigned SAROpc, SHROpc, ADDOpc, NEGOpc;
2672 switch (N.getValueType()) {
2673 default: assert("Unknown type to signed divide!");
2674 case MVT::i8:
2675 SAROpc = X86::SAR8ri;
2676 SHROpc = X86::SHR8ri;
2677 ADDOpc = X86::ADD8rr;
2678 NEGOpc = X86::NEG8r;
2679 break;
2680 case MVT::i16:
2681 SAROpc = X86::SAR16ri;
2682 SHROpc = X86::SHR16ri;
2683 ADDOpc = X86::ADD16rr;
2684 NEGOpc = X86::NEG16r;
2685 break;
2686 case MVT::i32:
2687 SAROpc = X86::SAR32ri;
2688 SHROpc = X86::SHR32ri;
2689 ADDOpc = X86::ADD32rr;
2690 NEGOpc = X86::NEG32r;
2691 break;
2692 }
Chris Lattner0d1f82a2005-01-11 03:11:44 +00002693 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner88c8a232005-01-07 07:49:41 +00002694 BuildMI(BB, SAROpc, 2, TmpReg).addReg(Tmp1).addImm(Log-1);
2695 unsigned TmpReg2 = MakeReg(N.getValueType());
2696 BuildMI(BB, SHROpc, 2, TmpReg2).addReg(TmpReg).addImm(32-Log);
2697 unsigned TmpReg3 = MakeReg(N.getValueType());
2698 BuildMI(BB, ADDOpc, 2, TmpReg3).addReg(Tmp1).addReg(TmpReg2);
Misha Brukmanc88330a2005-04-21 23:38:14 +00002699
Chris Lattner88c8a232005-01-07 07:49:41 +00002700 unsigned TmpReg4 = isNeg ? MakeReg(N.getValueType()) : Result;
2701 BuildMI(BB, SAROpc, 2, TmpReg4).addReg(TmpReg3).addImm(Log);
2702 if (isNeg)
2703 BuildMI(BB, NEGOpc, 1, Result).addReg(TmpReg4);
2704 return Result;
2705 }
2706 }
Chris Lattner60c23bd2005-04-13 03:29:53 +00002707 }
Chris Lattner88c8a232005-01-07 07:49:41 +00002708
Chris Lattner0d1f82a2005-01-11 03:11:44 +00002709 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2710 Tmp1 = SelectExpr(N.getOperand(0));
2711 Tmp2 = SelectExpr(N.getOperand(1));
2712 } else {
2713 Tmp2 = SelectExpr(N.getOperand(1));
2714 Tmp1 = SelectExpr(N.getOperand(0));
2715 }
Chris Lattner88c8a232005-01-07 07:49:41 +00002716
2717 bool isSigned = N.getOpcode() == ISD::SDIV || N.getOpcode() == ISD::SREM;
2718 bool isDiv = N.getOpcode() == ISD::SDIV || N.getOpcode() == ISD::UDIV;
2719 unsigned LoReg, HiReg, DivOpcode, MovOpcode, ClrOpcode, SExtOpcode;
2720 switch (N.getValueType()) {
2721 default: assert(0 && "Cannot sdiv this type!");
2722 case MVT::i8:
2723 DivOpcode = isSigned ? X86::IDIV8r : X86::DIV8r;
2724 LoReg = X86::AL;
2725 HiReg = X86::AH;
2726 MovOpcode = X86::MOV8rr;
2727 ClrOpcode = X86::MOV8ri;
2728 SExtOpcode = X86::CBW;
2729 break;
2730 case MVT::i16:
2731 DivOpcode = isSigned ? X86::IDIV16r : X86::DIV16r;
2732 LoReg = X86::AX;
2733 HiReg = X86::DX;
2734 MovOpcode = X86::MOV16rr;
2735 ClrOpcode = X86::MOV16ri;
2736 SExtOpcode = X86::CWD;
2737 break;
2738 case MVT::i32:
2739 DivOpcode = isSigned ? X86::IDIV32r : X86::DIV32r;
Chris Lattner3278ce82005-01-12 03:16:09 +00002740 LoReg = X86::EAX;
Chris Lattner88c8a232005-01-07 07:49:41 +00002741 HiReg = X86::EDX;
2742 MovOpcode = X86::MOV32rr;
2743 ClrOpcode = X86::MOV32ri;
2744 SExtOpcode = X86::CDQ;
2745 break;
Chris Lattner88c8a232005-01-07 07:49:41 +00002746 case MVT::f64:
Chris Lattnerb14a63a2005-01-16 07:34:08 +00002747 BuildMI(BB, X86::FpDIV, 2, Result).addReg(Tmp1).addReg(Tmp2);
Chris Lattner88c8a232005-01-07 07:49:41 +00002748 return Result;
2749 }
2750
2751 // Set up the low part.
2752 BuildMI(BB, MovOpcode, 1, LoReg).addReg(Tmp1);
2753
2754 if (isSigned) {
2755 // Sign extend the low part into the high part.
2756 BuildMI(BB, SExtOpcode, 0);
2757 } else {
2758 // Zero out the high part, effectively zero extending the input.
2759 BuildMI(BB, ClrOpcode, 1, HiReg).addImm(0);
2760 }
2761
2762 // Emit the DIV/IDIV instruction.
Misha Brukmanc88330a2005-04-21 23:38:14 +00002763 BuildMI(BB, DivOpcode, 1).addReg(Tmp2);
Chris Lattner88c8a232005-01-07 07:49:41 +00002764
2765 // Get the result of the divide or rem.
2766 BuildMI(BB, MovOpcode, 1, Result).addReg(isDiv ? LoReg : HiReg);
2767 return Result;
2768 }
2769
2770 case ISD::SHL:
Chris Lattner88c8a232005-01-07 07:49:41 +00002771 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Chris Lattner62b22422005-01-11 21:19:59 +00002772 if (CN->getValue() == 1) { // X = SHL Y, 1 -> X = ADD Y, Y
2773 switch (N.getValueType()) {
2774 default: assert(0 && "Cannot shift this type!");
2775 case MVT::i8: Opc = X86::ADD8rr; break;
2776 case MVT::i16: Opc = X86::ADD16rr; break;
2777 case MVT::i32: Opc = X86::ADD32rr; break;
2778 }
2779 Tmp1 = SelectExpr(N.getOperand(0));
2780 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp1);
2781 return Result;
2782 }
Misha Brukmanc88330a2005-04-21 23:38:14 +00002783
Chris Lattner88c8a232005-01-07 07:49:41 +00002784 switch (N.getValueType()) {
2785 default: assert(0 && "Cannot shift this type!");
2786 case MVT::i8: Opc = X86::SHL8ri; break;
2787 case MVT::i16: Opc = X86::SHL16ri; break;
2788 case MVT::i32: Opc = X86::SHL32ri; break;
2789 }
Chris Lattner0d1f82a2005-01-11 03:11:44 +00002790 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner88c8a232005-01-07 07:49:41 +00002791 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
2792 return Result;
2793 }
Chris Lattner0d1f82a2005-01-11 03:11:44 +00002794
2795 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2796 Tmp1 = SelectExpr(N.getOperand(0));
2797 Tmp2 = SelectExpr(N.getOperand(1));
2798 } else {
2799 Tmp2 = SelectExpr(N.getOperand(1));
2800 Tmp1 = SelectExpr(N.getOperand(0));
2801 }
2802
Chris Lattner88c8a232005-01-07 07:49:41 +00002803 switch (N.getValueType()) {
2804 default: assert(0 && "Cannot shift this type!");
2805 case MVT::i8 : Opc = X86::SHL8rCL; break;
2806 case MVT::i16: Opc = X86::SHL16rCL; break;
2807 case MVT::i32: Opc = X86::SHL32rCL; break;
2808 }
2809 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
2810 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
2811 return Result;
2812 case ISD::SRL:
Chris Lattner88c8a232005-01-07 07:49:41 +00002813 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
2814 switch (N.getValueType()) {
2815 default: assert(0 && "Cannot shift this type!");
2816 case MVT::i8: Opc = X86::SHR8ri; break;
2817 case MVT::i16: Opc = X86::SHR16ri; break;
2818 case MVT::i32: Opc = X86::SHR32ri; break;
2819 }
Chris Lattner0d1f82a2005-01-11 03:11:44 +00002820 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner88c8a232005-01-07 07:49:41 +00002821 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
2822 return Result;
2823 }
Chris Lattner0d1f82a2005-01-11 03:11:44 +00002824
2825 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2826 Tmp1 = SelectExpr(N.getOperand(0));
2827 Tmp2 = SelectExpr(N.getOperand(1));
2828 } else {
2829 Tmp2 = SelectExpr(N.getOperand(1));
2830 Tmp1 = SelectExpr(N.getOperand(0));
2831 }
2832
Chris Lattner88c8a232005-01-07 07:49:41 +00002833 switch (N.getValueType()) {
2834 default: assert(0 && "Cannot shift this type!");
2835 case MVT::i8 : Opc = X86::SHR8rCL; break;
2836 case MVT::i16: Opc = X86::SHR16rCL; break;
2837 case MVT::i32: Opc = X86::SHR32rCL; break;
2838 }
2839 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
2840 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
2841 return Result;
2842 case ISD::SRA:
Chris Lattner88c8a232005-01-07 07:49:41 +00002843 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
2844 switch (N.getValueType()) {
2845 default: assert(0 && "Cannot shift this type!");
2846 case MVT::i8: Opc = X86::SAR8ri; break;
2847 case MVT::i16: Opc = X86::SAR16ri; break;
2848 case MVT::i32: Opc = X86::SAR32ri; break;
2849 }
Chris Lattner0d1f82a2005-01-11 03:11:44 +00002850 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner88c8a232005-01-07 07:49:41 +00002851 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
2852 return Result;
2853 }
Chris Lattner0d1f82a2005-01-11 03:11:44 +00002854
2855 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2856 Tmp1 = SelectExpr(N.getOperand(0));
2857 Tmp2 = SelectExpr(N.getOperand(1));
2858 } else {
2859 Tmp2 = SelectExpr(N.getOperand(1));
2860 Tmp1 = SelectExpr(N.getOperand(0));
2861 }
2862
Chris Lattner88c8a232005-01-07 07:49:41 +00002863 switch (N.getValueType()) {
2864 default: assert(0 && "Cannot shift this type!");
2865 case MVT::i8 : Opc = X86::SAR8rCL; break;
2866 case MVT::i16: Opc = X86::SAR16rCL; break;
2867 case MVT::i32: Opc = X86::SAR32rCL; break;
2868 }
2869 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
2870 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
2871 return Result;
2872
2873 case ISD::SETCC:
Chris Lattner3be6cd52005-01-17 01:34:14 +00002874 EmitCMP(N.getOperand(0), N.getOperand(1), Node->hasOneUse());
Chris Lattner88c8a232005-01-07 07:49:41 +00002875 EmitSetCC(BB, Result, cast<SetCCSDNode>(N)->getCondition(),
2876 MVT::isFloatingPoint(N.getOperand(1).getValueType()));
2877 return Result;
Chris Lattnere18a4c42005-01-15 05:22:24 +00002878 case ISD::LOAD:
Chris Lattner88c8a232005-01-07 07:49:41 +00002879 // Make sure we generate both values.
Chris Lattner78d30282005-01-18 03:51:59 +00002880 if (Result != 1) { // Generate the token
2881 if (!ExprMap.insert(std::make_pair(N.getValue(1), 1)).second)
2882 assert(0 && "Load already emitted!?");
2883 } else
Chris Lattner88c8a232005-01-07 07:49:41 +00002884 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
2885
Chris Lattnerb52e0412005-01-08 19:28:19 +00002886 switch (Node->getValueType(0)) {
Chris Lattner88c8a232005-01-07 07:49:41 +00002887 default: assert(0 && "Cannot load this type!");
2888 case MVT::i1:
2889 case MVT::i8: Opc = X86::MOV8rm; break;
2890 case MVT::i16: Opc = X86::MOV16rm; break;
2891 case MVT::i32: Opc = X86::MOV32rm; break;
Chris Lattner88c8a232005-01-07 07:49:41 +00002892 case MVT::f64: Opc = X86::FLD64m; ContainsFPCode = true; break;
2893 }
Chris Lattner0d1f82a2005-01-11 03:11:44 +00002894
Chris Lattner88c8a232005-01-07 07:49:41 +00002895 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N.getOperand(1))){
Chris Lattner0d1f82a2005-01-11 03:11:44 +00002896 Select(N.getOperand(0));
Chris Lattner88c8a232005-01-07 07:49:41 +00002897 addConstantPoolReference(BuildMI(BB, Opc, 4, Result), CP->getIndex());
2898 } else {
2899 X86AddressMode AM;
Chris Lattner3676cd62005-01-13 05:53:16 +00002900
2901 SDOperand Chain = N.getOperand(0);
2902 SDOperand Address = N.getOperand(1);
2903 if (getRegPressure(Chain) > getRegPressure(Address)) {
2904 Select(Chain);
2905 SelectAddress(Address, AM);
2906 } else {
2907 SelectAddress(Address, AM);
2908 Select(Chain);
2909 }
2910
Chris Lattner88c8a232005-01-07 07:49:41 +00002911 addFullAddress(BuildMI(BB, Opc, 4, Result), AM);
2912 }
2913 return Result;
Chris Lattnere18a4c42005-01-15 05:22:24 +00002914
2915 case ISD::EXTLOAD: // Arbitrarily codegen extloads as MOVZX*
2916 case ISD::ZEXTLOAD: {
2917 // Make sure we generate both values.
2918 if (Result != 1)
2919 ExprMap[N.getValue(1)] = 1; // Generate the token
2920 else
2921 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
2922
Chris Lattnerb14a63a2005-01-16 07:34:08 +00002923 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N.getOperand(1)))
2924 if (Node->getValueType(0) == MVT::f64) {
2925 assert(cast<MVTSDNode>(Node)->getExtraValueType() == MVT::f32 &&
2926 "Bad EXTLOAD!");
2927 addConstantPoolReference(BuildMI(BB, X86::FLD32m, 4, Result),
2928 CP->getIndex());
2929 return Result;
2930 }
2931
Chris Lattnere18a4c42005-01-15 05:22:24 +00002932 X86AddressMode AM;
2933 if (getRegPressure(Node->getOperand(0)) >
2934 getRegPressure(Node->getOperand(1))) {
2935 Select(Node->getOperand(0)); // chain
2936 SelectAddress(Node->getOperand(1), AM);
2937 } else {
2938 SelectAddress(Node->getOperand(1), AM);
2939 Select(Node->getOperand(0)); // chain
2940 }
2941
2942 switch (Node->getValueType(0)) {
2943 default: assert(0 && "Unknown type to sign extend to.");
2944 case MVT::f64:
2945 assert(cast<MVTSDNode>(Node)->getExtraValueType() == MVT::f32 &&
2946 "Bad EXTLOAD!");
2947 addFullAddress(BuildMI(BB, X86::FLD32m, 5, Result), AM);
2948 break;
2949 case MVT::i32:
2950 switch (cast<MVTSDNode>(Node)->getExtraValueType()) {
2951 default:
2952 assert(0 && "Bad zero extend!");
2953 case MVT::i1:
2954 case MVT::i8:
2955 addFullAddress(BuildMI(BB, X86::MOVZX32rm8, 5, Result), AM);
2956 break;
2957 case MVT::i16:
2958 addFullAddress(BuildMI(BB, X86::MOVZX32rm16, 5, Result), AM);
2959 break;
2960 }
2961 break;
2962 case MVT::i16:
2963 assert(cast<MVTSDNode>(Node)->getExtraValueType() <= MVT::i8 &&
2964 "Bad zero extend!");
2965 addFullAddress(BuildMI(BB, X86::MOVSX16rm8, 5, Result), AM);
2966 break;
2967 case MVT::i8:
2968 assert(cast<MVTSDNode>(Node)->getExtraValueType() == MVT::i1 &&
2969 "Bad zero extend!");
2970 addFullAddress(BuildMI(BB, X86::MOV8rm, 5, Result), AM);
2971 break;
2972 }
2973 return Result;
Chris Lattner88c8a232005-01-07 07:49:41 +00002974 }
Chris Lattnere18a4c42005-01-15 05:22:24 +00002975 case ISD::SEXTLOAD: {
2976 // Make sure we generate both values.
2977 if (Result != 1)
2978 ExprMap[N.getValue(1)] = 1; // Generate the token
2979 else
2980 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
2981
2982 X86AddressMode AM;
2983 if (getRegPressure(Node->getOperand(0)) >
2984 getRegPressure(Node->getOperand(1))) {
2985 Select(Node->getOperand(0)); // chain
2986 SelectAddress(Node->getOperand(1), AM);
2987 } else {
2988 SelectAddress(Node->getOperand(1), AM);
2989 Select(Node->getOperand(0)); // chain
2990 }
2991
2992 switch (Node->getValueType(0)) {
2993 case MVT::i8: assert(0 && "Cannot sign extend from bool!");
2994 default: assert(0 && "Unknown type to sign extend to.");
2995 case MVT::i32:
2996 switch (cast<MVTSDNode>(Node)->getExtraValueType()) {
2997 default:
2998 case MVT::i1: assert(0 && "Cannot sign extend from bool!");
2999 case MVT::i8:
3000 addFullAddress(BuildMI(BB, X86::MOVSX32rm8, 5, Result), AM);
3001 break;
3002 case MVT::i16:
3003 addFullAddress(BuildMI(BB, X86::MOVSX32rm16, 5, Result), AM);
3004 break;
3005 }
3006 break;
3007 case MVT::i16:
3008 assert(cast<MVTSDNode>(Node)->getExtraValueType() == MVT::i8 &&
3009 "Cannot sign extend from bool!");
3010 addFullAddress(BuildMI(BB, X86::MOVSX16rm8, 5, Result), AM);
3011 break;
3012 }
3013 return Result;
3014 }
3015
Chris Lattner88c8a232005-01-07 07:49:41 +00003016 case ISD::DYNAMIC_STACKALLOC:
Chris Lattner88c8a232005-01-07 07:49:41 +00003017 // Generate both result values.
3018 if (Result != 1)
3019 ExprMap[N.getValue(1)] = 1; // Generate the token
3020 else
3021 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
3022
3023 // FIXME: We are currently ignoring the requested alignment for handling
3024 // greater than the stack alignment. This will need to be revisited at some
3025 // point. Align = N.getOperand(2);
3026
3027 if (!isa<ConstantSDNode>(N.getOperand(2)) ||
3028 cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) {
3029 std::cerr << "Cannot allocate stack object with greater alignment than"
3030 << " the stack alignment yet!";
3031 abort();
3032 }
Misha Brukmanc88330a2005-04-21 23:38:14 +00003033
Chris Lattner88c8a232005-01-07 07:49:41 +00003034 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Chris Lattner0d1f82a2005-01-11 03:11:44 +00003035 Select(N.getOperand(0));
Chris Lattner88c8a232005-01-07 07:49:41 +00003036 BuildMI(BB, X86::SUB32ri, 2, X86::ESP).addReg(X86::ESP)
3037 .addImm(CN->getValue());
3038 } else {
Chris Lattner0d1f82a2005-01-11 03:11:44 +00003039 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
3040 Select(N.getOperand(0));
3041 Tmp1 = SelectExpr(N.getOperand(1));
3042 } else {
3043 Tmp1 = SelectExpr(N.getOperand(1));
3044 Select(N.getOperand(0));
3045 }
Chris Lattner88c8a232005-01-07 07:49:41 +00003046
3047 // Subtract size from stack pointer, thereby allocating some space.
3048 BuildMI(BB, X86::SUB32rr, 2, X86::ESP).addReg(X86::ESP).addReg(Tmp1);
3049 }
3050
3051 // Put a pointer to the space into the result register, by copying the stack
3052 // pointer.
3053 BuildMI(BB, X86::MOV32rr, 1, Result).addReg(X86::ESP);
3054 return Result;
3055
Chris Lattner7ce7a8f2005-05-12 23:06:28 +00003056 case ISD::CALL: {
Chris Lattnerb52e0412005-01-08 19:28:19 +00003057 // The chain for this call is now lowered.
Chris Lattner78d30282005-01-18 03:51:59 +00003058 ExprMap.insert(std::make_pair(N.getValue(Node->getNumValues()-1), 1));
Chris Lattnerb52e0412005-01-08 19:28:19 +00003059
Chris Lattner7ce7a8f2005-05-12 23:06:28 +00003060 bool isDirect = isa<GlobalAddressSDNode>(N.getOperand(1)) ||
3061 isa<ExternalSymbolSDNode>(N.getOperand(1));
3062 unsigned Callee = 0;
3063 if (isDirect) {
3064 Select(N.getOperand(0));
3065 } else {
3066 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
3067 Select(N.getOperand(0));
3068 Callee = SelectExpr(N.getOperand(1));
3069 } else {
3070 Callee = SelectExpr(N.getOperand(1));
3071 Select(N.getOperand(0));
3072 }
3073 }
3074
3075 // If this call has values to pass in registers, do so now.
3076 if (Node->getNumOperands() > 2) {
3077 // The first value is passed in (a part of) EAX, the second in EDX.
3078 unsigned RegOp1 = SelectExpr(N.getOperand(2));
3079 unsigned RegOp2 =
3080 Node->getNumOperands() > 3 ? SelectExpr(N.getOperand(3)) : 0;
3081
3082 switch (N.getOperand(2).getValueType()) {
3083 default: assert(0 && "Bad thing to pass in regs");
3084 case MVT::i1:
3085 case MVT::i8: BuildMI(BB, X86::MOV8rr , 1,X86::AL).addReg(RegOp1); break;
3086 case MVT::i16: BuildMI(BB, X86::MOV16rr, 1,X86::AX).addReg(RegOp1); break;
3087 case MVT::i32: BuildMI(BB, X86::MOV32rr, 1,X86::EAX).addReg(RegOp1);break;
3088 }
3089 if (RegOp2)
3090 switch (N.getOperand(3).getValueType()) {
3091 default: assert(0 && "Bad thing to pass in regs");
3092 case MVT::i1:
3093 case MVT::i8:
3094 BuildMI(BB, X86::MOV8rr , 1, X86::DL).addReg(RegOp2);
3095 break;
3096 case MVT::i16:
3097 BuildMI(BB, X86::MOV16rr, 1, X86::DX).addReg(RegOp2);
3098 break;
3099 case MVT::i32:
3100 BuildMI(BB, X86::MOV32rr, 1, X86::EDX).addReg(RegOp2);
3101 break;
3102 }
3103 }
3104
Chris Lattner88c8a232005-01-07 07:49:41 +00003105 if (GlobalAddressSDNode *GASD =
3106 dyn_cast<GlobalAddressSDNode>(N.getOperand(1))) {
3107 BuildMI(BB, X86::CALLpcrel32, 1).addGlobalAddress(GASD->getGlobal(),true);
3108 } else if (ExternalSymbolSDNode *ESSDN =
3109 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1))) {
3110 BuildMI(BB, X86::CALLpcrel32,
3111 1).addExternalSymbol(ESSDN->getSymbol(), true);
3112 } else {
Chris Lattner0d1f82a2005-01-11 03:11:44 +00003113 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
3114 Select(N.getOperand(0));
3115 Tmp1 = SelectExpr(N.getOperand(1));
3116 } else {
3117 Tmp1 = SelectExpr(N.getOperand(1));
3118 Select(N.getOperand(0));
3119 }
3120
Chris Lattner88c8a232005-01-07 07:49:41 +00003121 BuildMI(BB, X86::CALL32r, 1).addReg(Tmp1);
3122 }
Chris Lattnerb52e0412005-01-08 19:28:19 +00003123 switch (Node->getValueType(0)) {
Chris Lattner88c8a232005-01-07 07:49:41 +00003124 default: assert(0 && "Unknown value type for call result!");
3125 case MVT::Other: return 1;
3126 case MVT::i1:
3127 case MVT::i8:
3128 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
3129 break;
3130 case MVT::i16:
3131 BuildMI(BB, X86::MOV16rr, 1, Result).addReg(X86::AX);
3132 break;
3133 case MVT::i32:
3134 BuildMI(BB, X86::MOV32rr, 1, Result).addReg(X86::EAX);
Chris Lattnerb52e0412005-01-08 19:28:19 +00003135 if (Node->getValueType(1) == MVT::i32)
Chris Lattner88c8a232005-01-07 07:49:41 +00003136 BuildMI(BB, X86::MOV32rr, 1, Result+1).addReg(X86::EDX);
3137 break;
Chris Lattner88c8a232005-01-07 07:49:41 +00003138 case MVT::f64: // Floating-point return values live in %ST(0)
3139 ContainsFPCode = true;
3140 BuildMI(BB, X86::FpGETRESULT, 1, Result);
3141 break;
3142 }
3143 return Result+N.ResNo;
Chris Lattner7ce7a8f2005-05-12 23:06:28 +00003144 }
Chris Lattner70ea07c2005-05-09 21:17:38 +00003145 case ISD::READPORT:
3146 // First, determine that the size of the operand falls within the acceptable
3147 // range for this architecture.
3148 //
3149 if (Node->getOperand(1).getValueType() != MVT::i16) {
3150 std::cerr << "llvm.readport: Address size is not 16 bits\n";
3151 exit(1);
3152 }
3153
3154 // Make sure we generate both values.
3155 if (Result != 1) { // Generate the token
3156 if (!ExprMap.insert(std::make_pair(N.getValue(1), 1)).second)
3157 assert(0 && "readport already emitted!?");
3158 } else
3159 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
3160
3161 Select(Node->getOperand(0)); // Select the chain.
3162
3163 // If the port is a single-byte constant, use the immediate form.
3164 if (ConstantSDNode *Port = dyn_cast<ConstantSDNode>(Node->getOperand(1)))
3165 if ((Port->getValue() & 255) == Port->getValue()) {
3166 switch (Node->getValueType(0)) {
3167 case MVT::i8:
3168 BuildMI(BB, X86::IN8ri, 1).addImm(Port->getValue());
3169 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
3170 return Result;
3171 case MVT::i16:
3172 BuildMI(BB, X86::IN16ri, 1).addImm(Port->getValue());
3173 BuildMI(BB, X86::MOV16rr, 1, Result).addReg(X86::AX);
3174 return Result;
3175 case MVT::i32:
3176 BuildMI(BB, X86::IN32ri, 1).addImm(Port->getValue());
3177 BuildMI(BB, X86::MOV32rr, 1, Result).addReg(X86::EAX);
3178 return Result;
3179 default: break;
3180 }
3181 }
3182
3183 // Now, move the I/O port address into the DX register and use the IN
3184 // instruction to get the input data.
3185 //
3186 Tmp1 = SelectExpr(Node->getOperand(1));
3187 BuildMI(BB, X86::MOV16rr, 1, X86::DX).addReg(Tmp1);
3188 switch (Node->getValueType(0)) {
3189 case MVT::i8:
3190 BuildMI(BB, X86::IN8rr, 0);
3191 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
3192 return Result;
3193 case MVT::i16:
3194 BuildMI(BB, X86::IN16rr, 0);
3195 BuildMI(BB, X86::MOV16rr, 1, Result).addReg(X86::AX);
3196 return Result;
3197 case MVT::i32:
3198 BuildMI(BB, X86::IN32rr, 0);
3199 BuildMI(BB, X86::MOV32rr, 1, Result).addReg(X86::EAX);
3200 return Result;
3201 default:
3202 std::cerr << "Cannot do input on this data type";
3203 exit(1);
3204 }
3205
Chris Lattner88c8a232005-01-07 07:49:41 +00003206 }
3207
3208 return 0;
3209}
3210
Chris Lattner96113fd2005-01-17 19:25:26 +00003211/// TryToFoldLoadOpStore - Given a store node, try to fold together a
3212/// load/op/store instruction. If successful return true.
3213bool ISel::TryToFoldLoadOpStore(SDNode *Node) {
3214 assert(Node->getOpcode() == ISD::STORE && "Can only do this for stores!");
3215 SDOperand Chain = Node->getOperand(0);
3216 SDOperand StVal = Node->getOperand(1);
Chris Lattnere86c9332005-01-17 22:10:42 +00003217 SDOperand StPtr = Node->getOperand(2);
Chris Lattner96113fd2005-01-17 19:25:26 +00003218
3219 // The chain has to be a load, the stored value must be an integer binary
3220 // operation with one use.
Chris Lattnere86c9332005-01-17 22:10:42 +00003221 if (!StVal.Val->hasOneUse() || StVal.Val->getNumOperands() != 2 ||
Chris Lattner96113fd2005-01-17 19:25:26 +00003222 MVT::isFloatingPoint(StVal.getValueType()))
3223 return false;
3224
Chris Lattnere86c9332005-01-17 22:10:42 +00003225 // Token chain must either be a factor node or the load to fold.
3226 if (Chain.getOpcode() != ISD::LOAD && Chain.getOpcode() != ISD::TokenFactor)
3227 return false;
Chris Lattner96113fd2005-01-17 19:25:26 +00003228
Chris Lattnere86c9332005-01-17 22:10:42 +00003229 SDOperand TheLoad;
3230
3231 // Check to see if there is a load from the same pointer that we're storing
3232 // to in either operand of the binop.
3233 if (StVal.getOperand(0).getOpcode() == ISD::LOAD &&
3234 StVal.getOperand(0).getOperand(1) == StPtr)
3235 TheLoad = StVal.getOperand(0);
3236 else if (StVal.getOperand(1).getOpcode() == ISD::LOAD &&
3237 StVal.getOperand(1).getOperand(1) == StPtr)
3238 TheLoad = StVal.getOperand(1);
3239 else
3240 return false; // No matching load operand.
3241
3242 // We can only fold the load if there are no intervening side-effecting
3243 // operations. This means that the store uses the load as its token chain, or
3244 // there are only token factor nodes in between the store and load.
3245 if (Chain != TheLoad.getValue(1)) {
3246 // Okay, the other option is that we have a store referring to (possibly
3247 // nested) token factor nodes. For now, just try peeking through one level
3248 // of token factors to see if this is the case.
3249 bool ChainOk = false;
3250 if (Chain.getOpcode() == ISD::TokenFactor) {
3251 for (unsigned i = 0, e = Chain.getNumOperands(); i != e; ++i)
3252 if (Chain.getOperand(i) == TheLoad.getValue(1)) {
3253 ChainOk = true;
3254 break;
3255 }
3256 }
3257
3258 if (!ChainOk) return false;
3259 }
3260
3261 if (TheLoad.getOperand(1) != StPtr)
Chris Lattner96113fd2005-01-17 19:25:26 +00003262 return false;
3263
3264 // Make sure that one of the operands of the binop is the load, and that the
3265 // load folds into the binop.
3266 if (((StVal.getOperand(0) != TheLoad ||
3267 !isFoldableLoad(TheLoad, StVal.getOperand(1))) &&
3268 (StVal.getOperand(1) != TheLoad ||
3269 !isFoldableLoad(TheLoad, StVal.getOperand(0)))))
3270 return false;
3271
3272 // Finally, check to see if this is one of the ops we can handle!
3273 static const unsigned ADDTAB[] = {
3274 X86::ADD8mi, X86::ADD16mi, X86::ADD32mi,
3275 X86::ADD8mr, X86::ADD16mr, X86::ADD32mr,
3276 };
3277 static const unsigned SUBTAB[] = {
3278 X86::SUB8mi, X86::SUB16mi, X86::SUB32mi,
3279 X86::SUB8mr, X86::SUB16mr, X86::SUB32mr,
3280 };
3281 static const unsigned ANDTAB[] = {
3282 X86::AND8mi, X86::AND16mi, X86::AND32mi,
3283 X86::AND8mr, X86::AND16mr, X86::AND32mr,
3284 };
3285 static const unsigned ORTAB[] = {
3286 X86::OR8mi, X86::OR16mi, X86::OR32mi,
3287 X86::OR8mr, X86::OR16mr, X86::OR32mr,
3288 };
3289 static const unsigned XORTAB[] = {
3290 X86::XOR8mi, X86::XOR16mi, X86::XOR32mi,
3291 X86::XOR8mr, X86::XOR16mr, X86::XOR32mr,
3292 };
3293 static const unsigned SHLTAB[] = {
3294 X86::SHL8mi, X86::SHL16mi, X86::SHL32mi,
3295 /*Have to put the reg in CL*/0, 0, 0,
3296 };
3297 static const unsigned SARTAB[] = {
3298 X86::SAR8mi, X86::SAR16mi, X86::SAR32mi,
3299 /*Have to put the reg in CL*/0, 0, 0,
3300 };
3301 static const unsigned SHRTAB[] = {
3302 X86::SHR8mi, X86::SHR16mi, X86::SHR32mi,
3303 /*Have to put the reg in CL*/0, 0, 0,
3304 };
Misha Brukmanc88330a2005-04-21 23:38:14 +00003305
Chris Lattner96113fd2005-01-17 19:25:26 +00003306 const unsigned *TabPtr = 0;
3307 switch (StVal.getOpcode()) {
3308 default:
3309 std::cerr << "CANNOT [mem] op= val: ";
3310 StVal.Val->dump(); std::cerr << "\n";
3311 case ISD::MUL:
3312 case ISD::SDIV:
3313 case ISD::UDIV:
3314 case ISD::SREM:
3315 case ISD::UREM: return false;
Misha Brukmanc88330a2005-04-21 23:38:14 +00003316
Chris Lattner96113fd2005-01-17 19:25:26 +00003317 case ISD::ADD: TabPtr = ADDTAB; break;
3318 case ISD::SUB: TabPtr = SUBTAB; break;
3319 case ISD::AND: TabPtr = ANDTAB; break;
3320 case ISD:: OR: TabPtr = ORTAB; break;
3321 case ISD::XOR: TabPtr = XORTAB; break;
3322 case ISD::SHL: TabPtr = SHLTAB; break;
3323 case ISD::SRA: TabPtr = SARTAB; break;
3324 case ISD::SRL: TabPtr = SHRTAB; break;
3325 }
Misha Brukmanc88330a2005-04-21 23:38:14 +00003326
Chris Lattner96113fd2005-01-17 19:25:26 +00003327 // Handle: [mem] op= CST
3328 SDOperand Op0 = StVal.getOperand(0);
3329 SDOperand Op1 = StVal.getOperand(1);
Chris Lattner0e1de102005-01-23 23:20:06 +00003330 unsigned Opc = 0;
Chris Lattner96113fd2005-01-17 19:25:26 +00003331 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1)) {
3332 switch (Op0.getValueType()) { // Use Op0's type because of shifts.
3333 default: break;
3334 case MVT::i1:
3335 case MVT::i8: Opc = TabPtr[0]; break;
3336 case MVT::i16: Opc = TabPtr[1]; break;
3337 case MVT::i32: Opc = TabPtr[2]; break;
3338 }
Misha Brukmanc88330a2005-04-21 23:38:14 +00003339
Chris Lattner96113fd2005-01-17 19:25:26 +00003340 if (Opc) {
Chris Lattner78d30282005-01-18 03:51:59 +00003341 if (!ExprMap.insert(std::make_pair(TheLoad.getValue(1), 1)).second)
3342 assert(0 && "Already emitted?");
Chris Lattnere86c9332005-01-17 22:10:42 +00003343 Select(Chain);
3344
Chris Lattner96113fd2005-01-17 19:25:26 +00003345 X86AddressMode AM;
3346 if (getRegPressure(TheLoad.getOperand(0)) >
3347 getRegPressure(TheLoad.getOperand(1))) {
3348 Select(TheLoad.getOperand(0));
3349 SelectAddress(TheLoad.getOperand(1), AM);
3350 } else {
3351 SelectAddress(TheLoad.getOperand(1), AM);
3352 Select(TheLoad.getOperand(0));
Misha Brukmanc88330a2005-04-21 23:38:14 +00003353 }
Chris Lattnere86c9332005-01-17 22:10:42 +00003354
3355 if (StVal.getOpcode() == ISD::ADD) {
3356 if (CN->getValue() == 1) {
3357 switch (Op0.getValueType()) {
3358 default: break;
3359 case MVT::i8:
3360 addFullAddress(BuildMI(BB, X86::INC8m, 4), AM);
3361 return true;
3362 case MVT::i16: Opc = TabPtr[1];
3363 addFullAddress(BuildMI(BB, X86::INC16m, 4), AM);
3364 return true;
3365 case MVT::i32: Opc = TabPtr[2];
3366 addFullAddress(BuildMI(BB, X86::INC32m, 4), AM);
3367 return true;
3368 }
3369 } else if (CN->getValue()+1 == 0) { // [X] += -1 -> DEC [X]
3370 switch (Op0.getValueType()) {
3371 default: break;
3372 case MVT::i8:
3373 addFullAddress(BuildMI(BB, X86::DEC8m, 4), AM);
3374 return true;
3375 case MVT::i16: Opc = TabPtr[1];
3376 addFullAddress(BuildMI(BB, X86::DEC16m, 4), AM);
3377 return true;
3378 case MVT::i32: Opc = TabPtr[2];
3379 addFullAddress(BuildMI(BB, X86::DEC32m, 4), AM);
3380 return true;
3381 }
3382 }
3383 }
Misha Brukmanc88330a2005-04-21 23:38:14 +00003384
Chris Lattner96113fd2005-01-17 19:25:26 +00003385 addFullAddress(BuildMI(BB, Opc, 4+1),AM).addImm(CN->getValue());
3386 return true;
3387 }
3388 }
Misha Brukmanc88330a2005-04-21 23:38:14 +00003389
Chris Lattner96113fd2005-01-17 19:25:26 +00003390 // If we have [mem] = V op [mem], try to turn it into:
3391 // [mem] = [mem] op V.
3392 if (Op1 == TheLoad && StVal.getOpcode() != ISD::SUB &&
3393 StVal.getOpcode() != ISD::SHL && StVal.getOpcode() != ISD::SRA &&
3394 StVal.getOpcode() != ISD::SRL)
3395 std::swap(Op0, Op1);
Misha Brukmanc88330a2005-04-21 23:38:14 +00003396
Chris Lattner96113fd2005-01-17 19:25:26 +00003397 if (Op0 != TheLoad) return false;
3398
3399 switch (Op0.getValueType()) {
3400 default: return false;
3401 case MVT::i1:
3402 case MVT::i8: Opc = TabPtr[3]; break;
3403 case MVT::i16: Opc = TabPtr[4]; break;
3404 case MVT::i32: Opc = TabPtr[5]; break;
3405 }
Chris Lattnere86c9332005-01-17 22:10:42 +00003406
Chris Lattner479c7112005-01-18 17:35:28 +00003407 // Table entry doesn't exist?
3408 if (Opc == 0) return false;
3409
Chris Lattner78d30282005-01-18 03:51:59 +00003410 if (!ExprMap.insert(std::make_pair(TheLoad.getValue(1), 1)).second)
3411 assert(0 && "Already emitted?");
Chris Lattnere86c9332005-01-17 22:10:42 +00003412 Select(Chain);
Chris Lattner96113fd2005-01-17 19:25:26 +00003413 Select(TheLoad.getOperand(0));
Chris Lattnera7acdda2005-01-18 01:06:26 +00003414
Chris Lattner96113fd2005-01-17 19:25:26 +00003415 X86AddressMode AM;
3416 SelectAddress(TheLoad.getOperand(1), AM);
3417 unsigned Reg = SelectExpr(Op1);
Chris Lattnera7acdda2005-01-18 01:06:26 +00003418 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addReg(Reg);
Chris Lattner96113fd2005-01-17 19:25:26 +00003419 return true;
3420}
3421
3422
Chris Lattner88c8a232005-01-07 07:49:41 +00003423void ISel::Select(SDOperand N) {
3424 unsigned Tmp1, Tmp2, Opc;
3425
Nate Begeman95210522005-03-24 04:39:54 +00003426 if (!ExprMap.insert(std::make_pair(N, 1)).second)
Chris Lattner88c8a232005-01-07 07:49:41 +00003427 return; // Already selected.
3428
Chris Lattner36f78482005-01-11 06:14:36 +00003429 SDNode *Node = N.Val;
3430
3431 switch (Node->getOpcode()) {
Chris Lattner88c8a232005-01-07 07:49:41 +00003432 default:
Chris Lattner36f78482005-01-11 06:14:36 +00003433 Node->dump(); std::cerr << "\n";
Chris Lattner88c8a232005-01-07 07:49:41 +00003434 assert(0 && "Node not handled yet!");
3435 case ISD::EntryToken: return; // Noop
Chris Lattnerc251fb62005-01-13 18:01:36 +00003436 case ISD::TokenFactor:
Chris Lattner15bd19d2005-01-13 19:56:00 +00003437 if (Node->getNumOperands() == 2) {
Misha Brukmanc88330a2005-04-21 23:38:14 +00003438 bool OneFirst =
Chris Lattner15bd19d2005-01-13 19:56:00 +00003439 getRegPressure(Node->getOperand(1))>getRegPressure(Node->getOperand(0));
3440 Select(Node->getOperand(OneFirst));
3441 Select(Node->getOperand(!OneFirst));
3442 } else {
3443 std::vector<std::pair<unsigned, unsigned> > OpsP;
3444 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
3445 OpsP.push_back(std::make_pair(getRegPressure(Node->getOperand(i)), i));
3446 std::sort(OpsP.begin(), OpsP.end());
3447 std::reverse(OpsP.begin(), OpsP.end());
3448 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
3449 Select(Node->getOperand(OpsP[i].second));
3450 }
Chris Lattnerc251fb62005-01-13 18:01:36 +00003451 return;
Chris Lattner88c8a232005-01-07 07:49:41 +00003452 case ISD::CopyToReg:
Chris Lattner2cfce682005-01-12 02:02:48 +00003453 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
3454 Select(N.getOperand(0));
3455 Tmp1 = SelectExpr(N.getOperand(1));
3456 } else {
3457 Tmp1 = SelectExpr(N.getOperand(1));
3458 Select(N.getOperand(0));
3459 }
Chris Lattnere727af02005-01-13 20:50:02 +00003460 Tmp2 = cast<RegSDNode>(N)->getReg();
Misha Brukmanc88330a2005-04-21 23:38:14 +00003461
Chris Lattner88c8a232005-01-07 07:49:41 +00003462 if (Tmp1 != Tmp2) {
3463 switch (N.getOperand(1).getValueType()) {
3464 default: assert(0 && "Invalid type for operation!");
3465 case MVT::i1:
3466 case MVT::i8: Opc = X86::MOV8rr; break;
3467 case MVT::i16: Opc = X86::MOV16rr; break;
3468 case MVT::i32: Opc = X86::MOV32rr; break;
Chris Lattnere44e6d12005-01-11 03:50:45 +00003469 case MVT::f64: Opc = X86::FpMOV; ContainsFPCode = true; break;
Chris Lattner88c8a232005-01-07 07:49:41 +00003470 }
3471 BuildMI(BB, Opc, 1, Tmp2).addReg(Tmp1);
3472 }
3473 return;
3474 case ISD::RET:
Chris Lattner88c8a232005-01-07 07:49:41 +00003475 switch (N.getNumOperands()) {
3476 default:
3477 assert(0 && "Unknown return instruction!");
3478 case 3:
Chris Lattner88c8a232005-01-07 07:49:41 +00003479 assert(N.getOperand(1).getValueType() == MVT::i32 &&
3480 N.getOperand(2).getValueType() == MVT::i32 &&
3481 "Unknown two-register value!");
Chris Lattner0d1f82a2005-01-11 03:11:44 +00003482 if (getRegPressure(N.getOperand(1)) > getRegPressure(N.getOperand(2))) {
3483 Tmp1 = SelectExpr(N.getOperand(1));
3484 Tmp2 = SelectExpr(N.getOperand(2));
3485 } else {
3486 Tmp2 = SelectExpr(N.getOperand(2));
3487 Tmp1 = SelectExpr(N.getOperand(1));
3488 }
3489 Select(N.getOperand(0));
3490
Chris Lattner88c8a232005-01-07 07:49:41 +00003491 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
3492 BuildMI(BB, X86::MOV32rr, 1, X86::EDX).addReg(Tmp2);
Chris Lattner88c8a232005-01-07 07:49:41 +00003493 break;
3494 case 2:
Chris Lattner0d1f82a2005-01-11 03:11:44 +00003495 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
3496 Select(N.getOperand(0));
3497 Tmp1 = SelectExpr(N.getOperand(1));
3498 } else {
3499 Tmp1 = SelectExpr(N.getOperand(1));
3500 Select(N.getOperand(0));
3501 }
Chris Lattner88c8a232005-01-07 07:49:41 +00003502 switch (N.getOperand(1).getValueType()) {
3503 default: assert(0 && "All other types should have been promoted!!");
3504 case MVT::f64:
3505 BuildMI(BB, X86::FpSETRESULT, 1).addReg(Tmp1);
Chris Lattner88c8a232005-01-07 07:49:41 +00003506 break;
3507 case MVT::i32:
3508 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
Chris Lattner88c8a232005-01-07 07:49:41 +00003509 break;
3510 }
3511 break;
3512 case 1:
Chris Lattner0d1f82a2005-01-11 03:11:44 +00003513 Select(N.getOperand(0));
Chris Lattner88c8a232005-01-07 07:49:41 +00003514 break;
3515 }
3516 BuildMI(BB, X86::RET, 0); // Just emit a 'ret' instruction
3517 return;
3518 case ISD::BR: {
3519 Select(N.getOperand(0));
3520 MachineBasicBlock *Dest =
3521 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
3522 BuildMI(BB, X86::JMP, 1).addMBB(Dest);
3523 return;
3524 }
3525
3526 case ISD::BRCOND: {
Chris Lattner88c8a232005-01-07 07:49:41 +00003527 MachineBasicBlock *Dest =
3528 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
Chris Lattner0d1f82a2005-01-11 03:11:44 +00003529
Chris Lattner88c8a232005-01-07 07:49:41 +00003530 // Try to fold a setcc into the branch. If this fails, emit a test/jne
3531 // pair.
Chris Lattner37ed2852005-01-11 04:06:27 +00003532 if (EmitBranchCC(Dest, N.getOperand(0), N.getOperand(1))) {
3533 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
3534 Select(N.getOperand(0));
3535 Tmp1 = SelectExpr(N.getOperand(1));
3536 } else {
3537 Tmp1 = SelectExpr(N.getOperand(1));
3538 Select(N.getOperand(0));
3539 }
Chris Lattner88c8a232005-01-07 07:49:41 +00003540 BuildMI(BB, X86::TEST8rr, 2).addReg(Tmp1).addReg(Tmp1);
3541 BuildMI(BB, X86::JNE, 1).addMBB(Dest);
3542 }
Chris Lattner0d1f82a2005-01-11 03:11:44 +00003543
Chris Lattner88c8a232005-01-07 07:49:41 +00003544 return;
3545 }
Chris Lattnere18a4c42005-01-15 05:22:24 +00003546
Chris Lattnerc1f386c2005-01-17 00:00:33 +00003547 case ISD::LOAD:
3548 // If this load could be folded into the only using instruction, and if it
3549 // is safe to emit the instruction here, try to do so now.
3550 if (Node->hasNUsesOfValue(1, 0)) {
3551 SDOperand TheVal = N.getValue(0);
3552 SDNode *User = 0;
3553 for (SDNode::use_iterator UI = Node->use_begin(); ; ++UI) {
3554 assert(UI != Node->use_end() && "Didn't find use!");
3555 SDNode *UN = *UI;
3556 for (unsigned i = 0, e = UN->getNumOperands(); i != e; ++i)
3557 if (UN->getOperand(i) == TheVal) {
3558 User = UN;
3559 goto FoundIt;
3560 }
3561 }
3562 FoundIt:
3563 // Only handle unary operators right now.
3564 if (User->getNumOperands() == 1) {
Chris Lattner78d30282005-01-18 03:51:59 +00003565 ExprMap.erase(N);
Chris Lattnerc1f386c2005-01-17 00:00:33 +00003566 SelectExpr(SDOperand(User, 0));
3567 return;
3568 }
3569 }
Chris Lattner28a205e2005-01-18 04:00:54 +00003570 ExprMap.erase(N);
Chris Lattnerc1f386c2005-01-17 00:00:33 +00003571 SelectExpr(N);
3572 return;
Chris Lattner70ea07c2005-05-09 21:17:38 +00003573 case ISD::READPORT:
Chris Lattnere18a4c42005-01-15 05:22:24 +00003574 case ISD::EXTLOAD:
3575 case ISD::SEXTLOAD:
3576 case ISD::ZEXTLOAD:
Chris Lattner88c8a232005-01-07 07:49:41 +00003577 case ISD::CALL:
3578 case ISD::DYNAMIC_STACKALLOC:
Chris Lattner28a205e2005-01-18 04:00:54 +00003579 ExprMap.erase(N);
Chris Lattner88c8a232005-01-07 07:49:41 +00003580 SelectExpr(N);
3581 return;
Chris Lattner7ce7a8f2005-05-12 23:06:28 +00003582 case ISD::CopyFromReg:
3583 ExprMap.erase(N);
3584 SelectExpr(N.getValue(0));
3585 return;
Chris Lattnere18a4c42005-01-15 05:22:24 +00003586
3587 case ISD::TRUNCSTORE: { // truncstore chain, val, ptr :storety
3588 // On X86, we can represent all types except for Bool and Float natively.
3589 X86AddressMode AM;
3590 MVT::ValueType StoredTy = cast<MVTSDNode>(Node)->getExtraValueType();
Chris Lattnerb14a63a2005-01-16 07:34:08 +00003591 assert((StoredTy == MVT::i1 || StoredTy == MVT::f32 ||
3592 StoredTy == MVT::i16 /*FIXME: THIS IS JUST FOR TESTING!*/)
3593 && "Unsupported TRUNCSTORE for this target!");
3594
3595 if (StoredTy == MVT::i16) {
3596 // FIXME: This is here just to allow testing. X86 doesn't really have a
3597 // TRUNCSTORE i16 operation, but this is required for targets that do not
3598 // have 16-bit integer registers. We occasionally disable 16-bit integer
3599 // registers to test the promotion code.
3600 Select(N.getOperand(0));
3601 Tmp1 = SelectExpr(N.getOperand(1));
3602 SelectAddress(N.getOperand(2), AM);
3603
3604 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
3605 addFullAddress(BuildMI(BB, X86::MOV16mr, 5), AM).addReg(X86::AX);
3606 return;
3607 }
Chris Lattnere18a4c42005-01-15 05:22:24 +00003608
3609 // Store of constant bool?
3610 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
3611 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(2))) {
3612 Select(N.getOperand(0));
3613 SelectAddress(N.getOperand(2), AM);
3614 } else {
3615 SelectAddress(N.getOperand(2), AM);
3616 Select(N.getOperand(0));
3617 }
3618 addFullAddress(BuildMI(BB, X86::MOV8mi, 5), AM).addImm(CN->getValue());
3619 return;
3620 }
3621
3622 switch (StoredTy) {
3623 default: assert(0 && "Cannot truncstore this type!");
3624 case MVT::i1: Opc = X86::MOV8mr; break;
3625 case MVT::f32: Opc = X86::FST32m; break;
3626 }
Misha Brukmanc88330a2005-04-21 23:38:14 +00003627
Chris Lattnere18a4c42005-01-15 05:22:24 +00003628 std::vector<std::pair<unsigned, unsigned> > RP;
3629 RP.push_back(std::make_pair(getRegPressure(N.getOperand(0)), 0));
3630 RP.push_back(std::make_pair(getRegPressure(N.getOperand(1)), 1));
3631 RP.push_back(std::make_pair(getRegPressure(N.getOperand(2)), 2));
3632 std::sort(RP.begin(), RP.end());
3633
Chris Lattner80c5b972005-02-23 05:57:21 +00003634 Tmp1 = 0; // Silence a warning.
Chris Lattnere18a4c42005-01-15 05:22:24 +00003635 for (unsigned i = 0; i != 3; ++i)
3636 switch (RP[2-i].second) {
3637 default: assert(0 && "Unknown operand number!");
3638 case 0: Select(N.getOperand(0)); break;
3639 case 1: Tmp1 = SelectExpr(N.getOperand(1)); break;
3640 case 2: SelectAddress(N.getOperand(2), AM); break;
3641 }
3642
3643 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addReg(Tmp1);
3644 return;
3645 }
Chris Lattner88c8a232005-01-07 07:49:41 +00003646 case ISD::STORE: {
Chris Lattner88c8a232005-01-07 07:49:41 +00003647 X86AddressMode AM;
Chris Lattner88c8a232005-01-07 07:49:41 +00003648
3649 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
3650 Opc = 0;
3651 switch (CN->getValueType(0)) {
3652 default: assert(0 && "Invalid type for operation!");
3653 case MVT::i1:
3654 case MVT::i8: Opc = X86::MOV8mi; break;
3655 case MVT::i16: Opc = X86::MOV16mi; break;
3656 case MVT::i32: Opc = X86::MOV32mi; break;
Chris Lattner88c8a232005-01-07 07:49:41 +00003657 case MVT::f64: break;
3658 }
3659 if (Opc) {
Chris Lattner0d1f82a2005-01-11 03:11:44 +00003660 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(2))) {
3661 Select(N.getOperand(0));
3662 SelectAddress(N.getOperand(2), AM);
3663 } else {
3664 SelectAddress(N.getOperand(2), AM);
3665 Select(N.getOperand(0));
3666 }
Chris Lattner88c8a232005-01-07 07:49:41 +00003667 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addImm(CN->getValue());
3668 return;
3669 }
Chris Lattneradcfc172005-04-21 19:03:24 +00003670 } else if (GlobalAddressSDNode *GA =
3671 dyn_cast<GlobalAddressSDNode>(N.getOperand(1))) {
3672 assert(GA->getValueType(0) == MVT::i32 && "Bad pointer operand");
3673
3674 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(2))) {
3675 Select(N.getOperand(0));
3676 SelectAddress(N.getOperand(2), AM);
3677 } else {
3678 SelectAddress(N.getOperand(2), AM);
3679 Select(N.getOperand(0));
3680 }
3681 addFullAddress(BuildMI(BB, X86::MOV32mi, 4+1),
3682 AM).addGlobalAddress(GA->getGlobal());
3683 return;
Chris Lattner88c8a232005-01-07 07:49:41 +00003684 }
Chris Lattner75bac9f2005-01-11 23:21:30 +00003685
3686 // Check to see if this is a load/op/store combination.
Chris Lattner96113fd2005-01-17 19:25:26 +00003687 if (TryToFoldLoadOpStore(Node))
3688 return;
Chris Lattner75bac9f2005-01-11 23:21:30 +00003689
Chris Lattner88c8a232005-01-07 07:49:41 +00003690 switch (N.getOperand(1).getValueType()) {
3691 default: assert(0 && "Cannot store this type!");
3692 case MVT::i1:
3693 case MVT::i8: Opc = X86::MOV8mr; break;
3694 case MVT::i16: Opc = X86::MOV16mr; break;
3695 case MVT::i32: Opc = X86::MOV32mr; break;
Chris Lattnere44e6d12005-01-11 03:50:45 +00003696 case MVT::f64: Opc = X86::FST64m; break;
Chris Lattner88c8a232005-01-07 07:49:41 +00003697 }
Misha Brukmanc88330a2005-04-21 23:38:14 +00003698
Chris Lattner0d1f82a2005-01-11 03:11:44 +00003699 std::vector<std::pair<unsigned, unsigned> > RP;
3700 RP.push_back(std::make_pair(getRegPressure(N.getOperand(0)), 0));
3701 RP.push_back(std::make_pair(getRegPressure(N.getOperand(1)), 1));
3702 RP.push_back(std::make_pair(getRegPressure(N.getOperand(2)), 2));
3703 std::sort(RP.begin(), RP.end());
3704
Chris Lattner80c5b972005-02-23 05:57:21 +00003705 Tmp1 = 0; // Silence a warning.
Chris Lattner0d1f82a2005-01-11 03:11:44 +00003706 for (unsigned i = 0; i != 3; ++i)
3707 switch (RP[2-i].second) {
3708 default: assert(0 && "Unknown operand number!");
3709 case 0: Select(N.getOperand(0)); break;
3710 case 1: Tmp1 = SelectExpr(N.getOperand(1)); break;
Chris Lattner8fea42b2005-01-11 03:37:59 +00003711 case 2: SelectAddress(N.getOperand(2), AM); break;
Chris Lattner0d1f82a2005-01-11 03:11:44 +00003712 }
3713
Chris Lattner88c8a232005-01-07 07:49:41 +00003714 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addReg(Tmp1);
3715 return;
3716 }
Chris Lattner2dce7032005-05-12 23:24:06 +00003717 case ISD::CALLSEQ_START:
3718 case ISD::CALLSEQ_END:
Chris Lattner88c8a232005-01-07 07:49:41 +00003719 Select(N.getOperand(0));
3720 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
Misha Brukmanc88330a2005-04-21 23:38:14 +00003721
Chris Lattner2dce7032005-05-12 23:24:06 +00003722 Opc = N.getOpcode() == ISD::CALLSEQ_START ? X86::ADJCALLSTACKDOWN :
3723 X86::ADJCALLSTACKUP;
Chris Lattner88c8a232005-01-07 07:49:41 +00003724 BuildMI(BB, Opc, 1).addImm(Tmp1);
3725 return;
Chris Lattner36f78482005-01-11 06:14:36 +00003726 case ISD::MEMSET: {
3727 Select(N.getOperand(0)); // Select the chain.
3728 unsigned Align =
3729 (unsigned)cast<ConstantSDNode>(Node->getOperand(4))->getValue();
3730 if (Align == 0) Align = 1;
3731
3732 // Turn the byte code into # iterations
3733 unsigned CountReg;
3734 unsigned Opcode;
3735 if (ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Node->getOperand(2))) {
3736 unsigned Val = ValC->getValue() & 255;
3737
3738 // If the value is a constant, then we can potentially use larger sets.
3739 switch (Align & 3) {
3740 case 2: // WORD aligned
3741 CountReg = MakeReg(MVT::i32);
3742 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
3743 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/2);
3744 } else {
3745 unsigned ByteReg = SelectExpr(Node->getOperand(3));
3746 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(1);
3747 }
3748 BuildMI(BB, X86::MOV16ri, 1, X86::AX).addImm((Val << 8) | Val);
3749 Opcode = X86::REP_STOSW;
3750 break;
3751 case 0: // DWORD aligned
3752 CountReg = MakeReg(MVT::i32);
3753 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
3754 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/4);
3755 } else {
3756 unsigned ByteReg = SelectExpr(Node->getOperand(3));
3757 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(2);
3758 }
3759 Val = (Val << 8) | Val;
3760 BuildMI(BB, X86::MOV32ri, 1, X86::EAX).addImm((Val << 16) | Val);
3761 Opcode = X86::REP_STOSD;
3762 break;
3763 default: // BYTE aligned
3764 CountReg = SelectExpr(Node->getOperand(3));
3765 BuildMI(BB, X86::MOV8ri, 1, X86::AL).addImm(Val);
3766 Opcode = X86::REP_STOSB;
3767 break;
3768 }
3769 } else {
3770 // If it's not a constant value we are storing, just fall back. We could
3771 // try to be clever to form 16 bit and 32 bit values, but we don't yet.
3772 unsigned ValReg = SelectExpr(Node->getOperand(2));
3773 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(ValReg);
3774 CountReg = SelectExpr(Node->getOperand(3));
3775 Opcode = X86::REP_STOSB;
3776 }
3777
3778 // No matter what the alignment is, we put the source in ESI, the
3779 // destination in EDI, and the count in ECX.
3780 unsigned TmpReg1 = SelectExpr(Node->getOperand(1));
3781 BuildMI(BB, X86::MOV32rr, 1, X86::ECX).addReg(CountReg);
3782 BuildMI(BB, X86::MOV32rr, 1, X86::EDI).addReg(TmpReg1);
3783 BuildMI(BB, Opcode, 0);
3784 return;
3785 }
Chris Lattner70ea07c2005-05-09 21:17:38 +00003786 case ISD::MEMCPY: {
Chris Lattnerc07164e2005-01-11 06:19:26 +00003787 Select(N.getOperand(0)); // Select the chain.
3788 unsigned Align =
3789 (unsigned)cast<ConstantSDNode>(Node->getOperand(4))->getValue();
3790 if (Align == 0) Align = 1;
3791
3792 // Turn the byte code into # iterations
3793 unsigned CountReg;
3794 unsigned Opcode;
3795 switch (Align & 3) {
3796 case 2: // WORD aligned
3797 CountReg = MakeReg(MVT::i32);
3798 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
3799 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/2);
3800 } else {
3801 unsigned ByteReg = SelectExpr(Node->getOperand(3));
3802 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(1);
3803 }
3804 Opcode = X86::REP_MOVSW;
3805 break;
3806 case 0: // DWORD aligned
3807 CountReg = MakeReg(MVT::i32);
3808 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
3809 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/4);
3810 } else {
3811 unsigned ByteReg = SelectExpr(Node->getOperand(3));
3812 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(2);
3813 }
3814 Opcode = X86::REP_MOVSD;
3815 break;
3816 default: // BYTE aligned
3817 CountReg = SelectExpr(Node->getOperand(3));
3818 Opcode = X86::REP_MOVSB;
3819 break;
3820 }
3821
3822 // No matter what the alignment is, we put the source in ESI, the
3823 // destination in EDI, and the count in ECX.
3824 unsigned TmpReg1 = SelectExpr(Node->getOperand(1));
3825 unsigned TmpReg2 = SelectExpr(Node->getOperand(2));
3826 BuildMI(BB, X86::MOV32rr, 1, X86::ECX).addReg(CountReg);
3827 BuildMI(BB, X86::MOV32rr, 1, X86::EDI).addReg(TmpReg1);
3828 BuildMI(BB, X86::MOV32rr, 1, X86::ESI).addReg(TmpReg2);
3829 BuildMI(BB, Opcode, 0);
3830 return;
Chris Lattner88c8a232005-01-07 07:49:41 +00003831 }
Chris Lattner70ea07c2005-05-09 21:17:38 +00003832 case ISD::WRITEPORT:
3833 if (Node->getOperand(2).getValueType() != MVT::i16) {
3834 std::cerr << "llvm.writeport: Address size is not 16 bits\n";
3835 exit(1);
3836 }
3837 Select(Node->getOperand(0)); // Emit the chain.
3838
3839 Tmp1 = SelectExpr(Node->getOperand(1));
3840 switch (Node->getOperand(1).getValueType()) {
3841 case MVT::i8:
3842 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(Tmp1);
3843 Tmp2 = X86::OUT8ir; Opc = X86::OUT8rr;
3844 break;
3845 case MVT::i16:
3846 BuildMI(BB, X86::MOV16rr, 1, X86::AX).addReg(Tmp1);
3847 Tmp2 = X86::OUT16ir; Opc = X86::OUT16rr;
3848 break;
3849 case MVT::i32:
3850 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
3851 Tmp2 = X86::OUT32ir; Opc = X86::OUT32rr;
3852 break;
3853 default:
3854 std::cerr << "llvm.writeport: invalid data type for X86 target";
3855 exit(1);
3856 }
3857
3858 // If the port is a single-byte constant, use the immediate form.
3859 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Node->getOperand(2)))
3860 if ((CN->getValue() & 255) == CN->getValue()) {
3861 BuildMI(BB, Tmp2, 1).addImm(CN->getValue());
3862 return;
3863 }
3864
3865 // Otherwise, move the I/O port address into the DX register.
3866 unsigned Reg = SelectExpr(Node->getOperand(2));
3867 BuildMI(BB, X86::MOV16rr, 1, X86::DX).addReg(Reg);
3868 BuildMI(BB, Opc, 0);
3869 return;
3870 }
Chris Lattner88c8a232005-01-07 07:49:41 +00003871 assert(0 && "Should not be reached!");
3872}
3873
3874
3875/// createX86PatternInstructionSelector - This pass converts an LLVM function
3876/// into a machine code representation using pattern matching and a machine
3877/// description file.
3878///
3879FunctionPass *llvm::createX86PatternInstructionSelector(TargetMachine &TM) {
Misha Brukmanc88330a2005-04-21 23:38:14 +00003880 return new ISel(TM);
Chris Lattner88c8a232005-01-07 07:49:41 +00003881}