blob: 584d4af3ef01b2170365858b92d7ffed0329465a [file] [log] [blame]
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001//===-- X86ISelPattern.cpp - A pattern matching inst selector for X86 -----===//
Chris Lattner24aad1b2005-01-10 22:10:13 +00002//
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
Misha Brukman0e0a7a452005-04-21 23:38:14 +00007//
Chris Lattner8acb1ba2005-01-07 07:49:41 +00008//===----------------------------------------------------------------------===//
9//
10// This file defines a pattern matching instruction selector for X86.
11//
12//===----------------------------------------------------------------------===//
13
14#include "X86.h"
15#include "X86InstrBuilder.h"
16#include "X86RegisterInfo.h"
Chris Lattnere3e0f272005-05-09 03:36:39 +000017#include "llvm/Constants.h"
18#include "llvm/Instructions.h"
Chris Lattner8acb1ba2005-01-07 07:49:41 +000019#include "llvm/Function.h"
Chris Lattnere3e0f272005-05-09 03:36:39 +000020#include "llvm/CodeGen/MachineConstantPool.h"
Chris Lattner8acb1ba2005-01-07 07:49:41 +000021#include "llvm/CodeGen/MachineFunction.h"
22#include "llvm/CodeGen/MachineFrameInfo.h"
23#include "llvm/CodeGen/SelectionDAG.h"
24#include "llvm/CodeGen/SelectionDAGISel.h"
25#include "llvm/CodeGen/SSARegMap.h"
26#include "llvm/Target/TargetData.h"
27#include "llvm/Target/TargetLowering.h"
Chris Lattnerc5dcb532005-04-30 04:25:35 +000028#include "llvm/Target/TargetOptions.h"
Chris Lattnere3e0f272005-05-09 03:36:39 +000029#include "llvm/Support/CFG.h"
Chris Lattner8acb1ba2005-01-07 07:49:41 +000030#include "llvm/Support/MathExtras.h"
31#include "llvm/ADT/Statistic.h"
32#include <set>
Jeff Cohen603fea92005-01-12 04:29:05 +000033#include <algorithm>
Chris Lattner8acb1ba2005-01-07 07:49:41 +000034using namespace llvm;
35
36//===----------------------------------------------------------------------===//
37// X86TargetLowering - X86 Implementation of the TargetLowering interface
38namespace {
39 class X86TargetLowering : public TargetLowering {
40 int VarArgsFrameIndex; // FrameIndex for start of varargs area.
Chris Lattner14824582005-01-09 00:01:27 +000041 int ReturnAddrIndex; // FrameIndex for return slot.
Chris Lattner8acb1ba2005-01-07 07:49:41 +000042 public:
43 X86TargetLowering(TargetMachine &TM) : TargetLowering(TM) {
44 // Set up the TargetLowering object.
Chris Lattner4df0de92005-01-17 00:00:33 +000045
46 // X86 is wierd, it always uses i8 for shift amounts and setcc results.
47 setShiftAmountType(MVT::i8);
48 setSetCCResultType(MVT::i8);
Chris Lattner6659bd72005-04-07 19:41:46 +000049 setSetCCResultContents(ZeroOrOneSetCCResult);
Chris Lattner009b55b2005-01-19 03:36:30 +000050 setShiftAmountFlavor(Mask); // shl X, 32 == shl X, 0
Chris Lattner4df0de92005-01-17 00:00:33 +000051
52 // Set up the register classes.
Chris Lattner8acb1ba2005-01-07 07:49:41 +000053 addRegisterClass(MVT::i8, X86::R8RegisterClass);
54 addRegisterClass(MVT::i16, X86::R16RegisterClass);
55 addRegisterClass(MVT::i32, X86::R32RegisterClass);
56 addRegisterClass(MVT::f64, X86::RFPRegisterClass);
Misha Brukman0e0a7a452005-04-21 23:38:14 +000057
Chris Lattner8acb1ba2005-01-07 07:49:41 +000058 // FIXME: Eliminate these two classes when legalize can handle promotions
59 // well.
Chris Lattnerda2ce112005-01-16 07:34:08 +000060/**/ addRegisterClass(MVT::i1, X86::R8RegisterClass);
Chris Lattnerda2ce112005-01-16 07:34:08 +000061
Chris Lattnerda4d4692005-04-09 03:22:37 +000062 setOperationAction(ISD::BRCONDTWOWAY , MVT::Other, Expand);
Chris Lattnerda2ce112005-01-16 07:34:08 +000063 setOperationAction(ISD::MEMMOVE , MVT::Other, Expand);
64 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Expand);
Chris Lattnerda2ce112005-01-16 07:34:08 +000065 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
Chris Lattnerda2ce112005-01-16 07:34:08 +000066 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
67 setOperationAction(ISD::SEXTLOAD , MVT::i1 , Expand);
68 setOperationAction(ISD::SREM , MVT::f64 , Expand);
Andrew Lenharth691ef2b2005-05-03 17:19:30 +000069 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
70 setOperationAction(ISD::CTTZ , MVT::i32 , Expand);
Andrew Lenharthb5884d32005-05-04 19:25:37 +000071 setOperationAction(ISD::CTLZ , MVT::i32 , Expand);
Chris Lattner43fdea02005-04-02 05:03:24 +000072
Chris Lattner4e6ce5f2005-05-09 20:37:29 +000073 setOperationAction(ISD::READIO , MVT::i1 , Expand);
74 setOperationAction(ISD::READIO , MVT::i8 , Expand);
75 setOperationAction(ISD::READIO , MVT::i16 , Expand);
76 setOperationAction(ISD::READIO , MVT::i32 , Expand);
77 setOperationAction(ISD::WRITEIO , MVT::i1 , Expand);
78 setOperationAction(ISD::WRITEIO , MVT::i8 , Expand);
79 setOperationAction(ISD::WRITEIO , MVT::i16 , Expand);
80 setOperationAction(ISD::WRITEIO , MVT::i32 , Expand);
81
Chris Lattnerc5dcb532005-04-30 04:25:35 +000082 if (!UnsafeFPMath) {
83 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
84 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
85 }
86
Chris Lattnerda2ce112005-01-16 07:34:08 +000087 // These should be promoted to a larger select which is supported.
88/**/ setOperationAction(ISD::SELECT , MVT::i1 , Promote);
89 setOperationAction(ISD::SELECT , MVT::i8 , Promote);
Misha Brukman0e0a7a452005-04-21 23:38:14 +000090
Chris Lattner8acb1ba2005-01-07 07:49:41 +000091 computeRegisterProperties();
Misha Brukman0e0a7a452005-04-21 23:38:14 +000092
Chris Lattner8acb1ba2005-01-07 07:49:41 +000093 addLegalFPImmediate(+0.0); // FLD0
94 addLegalFPImmediate(+1.0); // FLD1
95 addLegalFPImmediate(-0.0); // FLD0/FCHS
96 addLegalFPImmediate(-1.0); // FLD1/FCHS
97 }
98
99 /// LowerArguments - This hook must be implemented to indicate how we should
100 /// lower the arguments for the specified function, into the specified DAG.
101 virtual std::vector<SDOperand>
102 LowerArguments(Function &F, SelectionDAG &DAG);
103
104 /// LowerCallTo - This hook lowers an abstract call to a function into an
105 /// actual call.
Chris Lattner5188ad72005-01-08 19:28:19 +0000106 virtual std::pair<SDOperand, SDOperand>
Nate Begeman8e21e712005-03-26 01:29:23 +0000107 LowerCallTo(SDOperand Chain, const Type *RetTy, bool isVarArg,
108 SDOperand Callee, ArgListTy &Args, SelectionDAG &DAG);
Chris Lattner14824582005-01-09 00:01:27 +0000109
110 virtual std::pair<SDOperand, SDOperand>
111 LowerVAStart(SDOperand Chain, SelectionDAG &DAG);
112
113 virtual std::pair<SDOperand,SDOperand>
114 LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
115 const Type *ArgTy, SelectionDAG &DAG);
116
117 virtual std::pair<SDOperand, SDOperand>
118 LowerFrameReturnAddress(bool isFrameAddr, SDOperand Chain, unsigned Depth,
119 SelectionDAG &DAG);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000120 };
121}
122
123
124std::vector<SDOperand>
125X86TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
126 std::vector<SDOperand> ArgValues;
127
128 // Add DAG nodes to load the arguments... On entry to a function on the X86,
129 // the stack frame looks like this:
130 //
131 // [ESP] -- return address
132 // [ESP + 4] -- first argument (leftmost lexically)
133 // [ESP + 8] -- second argument, if first argument is four bytes in size
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000134 // ...
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000135 //
136 MachineFunction &MF = DAG.getMachineFunction();
137 MachineFrameInfo *MFI = MF.getFrameInfo();
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000138
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000139 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
Chris Lattnere4d5c442005-03-15 04:54:21 +0000140 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000141 MVT::ValueType ObjectVT = getValueType(I->getType());
142 unsigned ArgIncrement = 4;
143 unsigned ObjSize;
144 switch (ObjectVT) {
145 default: assert(0 && "Unhandled argument type!");
146 case MVT::i1:
147 case MVT::i8: ObjSize = 1; break;
148 case MVT::i16: ObjSize = 2; break;
149 case MVT::i32: ObjSize = 4; break;
150 case MVT::i64: ObjSize = ArgIncrement = 8; break;
151 case MVT::f32: ObjSize = 4; break;
152 case MVT::f64: ObjSize = ArgIncrement = 8; break;
153 }
154 // Create the frame index object for this incoming parameter...
155 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000156
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000157 // Create the SelectionDAG nodes corresponding to a load from this parameter
158 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
159
160 // Don't codegen dead arguments. FIXME: remove this check when we can nuke
161 // dead loads.
162 SDOperand ArgValue;
163 if (!I->use_empty())
Chris Lattnera80d2bd2005-05-09 05:40:26 +0000164 ArgValue = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
165 DAG.getSrcValue(NULL));
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000166 else {
167 if (MVT::isInteger(ObjectVT))
168 ArgValue = DAG.getConstant(0, ObjectVT);
169 else
170 ArgValue = DAG.getConstantFP(0, ObjectVT);
171 }
172 ArgValues.push_back(ArgValue);
173
174 ArgOffset += ArgIncrement; // Move on to the next argument...
175 }
176
177 // If the function takes variable number of arguments, make a frame index for
178 // the start of the first vararg value... for expansion of llvm.va_start.
179 if (F.isVarArg())
180 VarArgsFrameIndex = MFI->CreateFixedObject(1, ArgOffset);
Chris Lattner14824582005-01-09 00:01:27 +0000181 ReturnAddrIndex = 0; // No return address slot generated yet.
Chris Lattner4c52f0e2005-04-09 15:23:56 +0000182
183 // Finally, inform the code generator which regs we return values in.
184 switch (getValueType(F.getReturnType())) {
185 default: assert(0 && "Unknown type!");
186 case MVT::isVoid: break;
187 case MVT::i1:
188 case MVT::i8:
189 case MVT::i16:
190 case MVT::i32:
191 MF.addLiveOut(X86::EAX);
192 break;
193 case MVT::i64:
194 MF.addLiveOut(X86::EAX);
195 MF.addLiveOut(X86::EDX);
196 break;
197 case MVT::f32:
198 case MVT::f64:
199 MF.addLiveOut(X86::ST0);
200 break;
201 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000202 return ArgValues;
203}
204
Chris Lattner5188ad72005-01-08 19:28:19 +0000205std::pair<SDOperand, SDOperand>
206X86TargetLowering::LowerCallTo(SDOperand Chain,
Nate Begeman8e21e712005-03-26 01:29:23 +0000207 const Type *RetTy, bool isVarArg,
208 SDOperand Callee, ArgListTy &Args, SelectionDAG &DAG) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000209 // Count how many bytes are to be pushed on the stack.
210 unsigned NumBytes = 0;
211
212 if (Args.empty()) {
213 // Save zero bytes.
Chris Lattner5188ad72005-01-08 19:28:19 +0000214 Chain = DAG.getNode(ISD::ADJCALLSTACKDOWN, MVT::Other, Chain,
215 DAG.getConstant(0, getPointerTy()));
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000216 } else {
217 for (unsigned i = 0, e = Args.size(); i != e; ++i)
218 switch (getValueType(Args[i].second)) {
219 default: assert(0 && "Unknown value type!");
220 case MVT::i1:
221 case MVT::i8:
222 case MVT::i16:
223 case MVT::i32:
224 case MVT::f32:
225 NumBytes += 4;
226 break;
227 case MVT::i64:
228 case MVT::f64:
229 NumBytes += 8;
230 break;
231 }
232
Chris Lattner5188ad72005-01-08 19:28:19 +0000233 Chain = DAG.getNode(ISD::ADJCALLSTACKDOWN, MVT::Other, Chain,
234 DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000235
236 // Arguments go on the stack in reverse order, as specified by the ABI.
237 unsigned ArgOffset = 0;
Chris Lattner7f2afac2005-01-14 22:37:41 +0000238 SDOperand StackPtr = DAG.getCopyFromReg(X86::ESP, MVT::i32,
239 DAG.getEntryNode());
Chris Lattnerb62e1e22005-01-21 19:46:38 +0000240 std::vector<SDOperand> Stores;
241
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000242 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
243 unsigned ArgReg;
244 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
245 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
246
247 switch (getValueType(Args[i].second)) {
248 default: assert(0 && "Unexpected ValueType for argument!");
249 case MVT::i1:
250 case MVT::i8:
251 case MVT::i16:
252 // Promote the integer to 32 bits. If the input type is signed use a
253 // sign extend, otherwise use a zero extend.
254 if (Args[i].second->isSigned())
255 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
256 else
257 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
258
259 // FALL THROUGH
260 case MVT::i32:
261 case MVT::f32:
Chris Lattnerb62e1e22005-01-21 19:46:38 +0000262 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
Chris Lattnera80d2bd2005-05-09 05:40:26 +0000263 Args[i].first, PtrOff,
264 DAG.getSrcValue(NULL)));
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000265 ArgOffset += 4;
266 break;
267 case MVT::i64:
268 case MVT::f64:
Chris Lattnerb62e1e22005-01-21 19:46:38 +0000269 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
Chris Lattnera80d2bd2005-05-09 05:40:26 +0000270 Args[i].first, PtrOff,
271 DAG.getSrcValue(NULL)));
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000272 ArgOffset += 8;
273 break;
274 }
275 }
Chris Lattnerb62e1e22005-01-21 19:46:38 +0000276 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, Stores);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000277 }
278
279 std::vector<MVT::ValueType> RetVals;
280 MVT::ValueType RetTyVT = getValueType(RetTy);
281 if (RetTyVT != MVT::isVoid)
282 RetVals.push_back(RetTyVT);
283 RetVals.push_back(MVT::Other);
284
Chris Lattner5188ad72005-01-08 19:28:19 +0000285 SDOperand TheCall = SDOperand(DAG.getCall(RetVals, Chain, Callee), 0);
Chris Lattnerb0802652005-01-08 20:51:36 +0000286 Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
Chris Lattner5188ad72005-01-08 19:28:19 +0000287 Chain = DAG.getNode(ISD::ADJCALLSTACKUP, MVT::Other, Chain,
288 DAG.getConstant(NumBytes, getPointerTy()));
289 return std::make_pair(TheCall, Chain);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000290}
291
Chris Lattner14824582005-01-09 00:01:27 +0000292std::pair<SDOperand, SDOperand>
293X86TargetLowering::LowerVAStart(SDOperand Chain, SelectionDAG &DAG) {
294 // vastart just returns the address of the VarArgsFrameIndex slot.
295 return std::make_pair(DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32), Chain);
296}
297
298std::pair<SDOperand,SDOperand> X86TargetLowering::
299LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
300 const Type *ArgTy, SelectionDAG &DAG) {
301 MVT::ValueType ArgVT = getValueType(ArgTy);
302 SDOperand Result;
303 if (!isVANext) {
Chris Lattnera80d2bd2005-05-09 05:40:26 +0000304 Result = DAG.getLoad(ArgVT, DAG.getEntryNode(), VAList,
305 DAG.getSrcValue(NULL));
Chris Lattner14824582005-01-09 00:01:27 +0000306 } else {
307 unsigned Amt;
308 if (ArgVT == MVT::i32)
309 Amt = 4;
310 else {
311 assert((ArgVT == MVT::i64 || ArgVT == MVT::f64) &&
312 "Other types should have been promoted for varargs!");
313 Amt = 8;
314 }
315 Result = DAG.getNode(ISD::ADD, VAList.getValueType(), VAList,
316 DAG.getConstant(Amt, VAList.getValueType()));
317 }
318 return std::make_pair(Result, Chain);
319}
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000320
Chris Lattner14824582005-01-09 00:01:27 +0000321
322std::pair<SDOperand, SDOperand> X86TargetLowering::
323LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
324 SelectionDAG &DAG) {
325 SDOperand Result;
326 if (Depth) // Depths > 0 not supported yet!
327 Result = DAG.getConstant(0, getPointerTy());
328 else {
329 if (ReturnAddrIndex == 0) {
330 // Set up a frame object for the return address.
331 MachineFunction &MF = DAG.getMachineFunction();
332 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(4, -4);
333 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000334
Chris Lattner14824582005-01-09 00:01:27 +0000335 SDOperand RetAddrFI = DAG.getFrameIndex(ReturnAddrIndex, MVT::i32);
336
337 if (!isFrameAddress)
338 // Just load the return address
Andrew Lenharth2d86ea22005-04-27 20:10:01 +0000339 Result = DAG.getLoad(MVT::i32, DAG.getEntryNode(), RetAddrFI, DAG.getSrcValue(NULL));
Chris Lattner14824582005-01-09 00:01:27 +0000340 else
341 Result = DAG.getNode(ISD::SUB, MVT::i32, RetAddrFI,
342 DAG.getConstant(4, MVT::i32));
343 }
344 return std::make_pair(Result, Chain);
345}
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000346
347
Chris Lattner98a8ba02005-01-18 01:06:26 +0000348namespace {
349 /// X86ISelAddressMode - This corresponds to X86AddressMode, but uses
350 /// SDOperand's instead of register numbers for the leaves of the matched
351 /// tree.
352 struct X86ISelAddressMode {
353 enum {
354 RegBase,
355 FrameIndexBase,
356 } BaseType;
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000357
Chris Lattner98a8ba02005-01-18 01:06:26 +0000358 struct { // This is really a union, discriminated by BaseType!
359 SDOperand Reg;
360 int FrameIndex;
361 } Base;
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000362
Chris Lattner98a8ba02005-01-18 01:06:26 +0000363 unsigned Scale;
364 SDOperand IndexReg;
365 unsigned Disp;
366 GlobalValue *GV;
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000367
Chris Lattner98a8ba02005-01-18 01:06:26 +0000368 X86ISelAddressMode()
369 : BaseType(RegBase), Scale(1), IndexReg(), Disp(), GV(0) {
370 }
371 };
372}
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000373
374
375namespace {
376 Statistic<>
377 NumFPKill("x86-codegen", "Number of FP_REG_KILL instructions added");
378
379 //===--------------------------------------------------------------------===//
380 /// ISel - X86 specific code to select X86 machine instructions for
381 /// SelectionDAG operations.
382 ///
383 class ISel : public SelectionDAGISel {
384 /// ContainsFPCode - Every instruction we select that uses or defines a FP
385 /// register should set this to true.
386 bool ContainsFPCode;
387
388 /// X86Lowering - This object fully describes how to lower LLVM code to an
389 /// X86-specific SelectionDAG.
390 X86TargetLowering X86Lowering;
391
Chris Lattner11333092005-01-11 03:11:44 +0000392 /// RegPressureMap - This keeps an approximate count of the number of
393 /// registers required to evaluate each node in the graph.
394 std::map<SDNode*, unsigned> RegPressureMap;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000395
396 /// ExprMap - As shared expressions are codegen'd, we keep track of which
397 /// vreg the value is produced in, so we only emit one copy of each compiled
398 /// tree.
399 std::map<SDOperand, unsigned> ExprMap;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000400
401 public:
402 ISel(TargetMachine &TM) : SelectionDAGISel(X86Lowering), X86Lowering(TM) {
403 }
404
Chris Lattner67b1c3c2005-01-21 21:35:14 +0000405 virtual const char *getPassName() const {
406 return "X86 Pattern Instruction Selection";
407 }
408
Chris Lattner11333092005-01-11 03:11:44 +0000409 unsigned getRegPressure(SDOperand O) {
410 return RegPressureMap[O.Val];
411 }
412 unsigned ComputeRegPressure(SDOperand O);
413
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000414 /// InstructionSelectBasicBlock - This callback is invoked by
415 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
Chris Lattner7dbcb752005-01-12 04:21:28 +0000416 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000417
Chris Lattner44129b52005-01-25 20:03:11 +0000418 bool isFoldableLoad(SDOperand Op, SDOperand OtherOp,
419 bool FloatPromoteOk = false);
Chris Lattnera5ade062005-01-11 21:19:59 +0000420 void EmitFoldedLoad(SDOperand Op, X86AddressMode &AM);
Chris Lattnere10269b2005-01-17 19:25:26 +0000421 bool TryToFoldLoadOpStore(SDNode *Node);
Chris Lattnera5ade062005-01-11 21:19:59 +0000422
Chris Lattner30ea1e92005-01-19 07:37:26 +0000423 bool EmitOrOpOp(SDOperand Op1, SDOperand Op2, unsigned DestReg);
Chris Lattnercb1aa8d2005-01-17 01:34:14 +0000424 void EmitCMP(SDOperand LHS, SDOperand RHS, bool isOnlyUse);
Chris Lattner6c07aee2005-01-11 04:06:27 +0000425 bool EmitBranchCC(MachineBasicBlock *Dest, SDOperand Chain, SDOperand Cond);
Chris Lattner24aad1b2005-01-10 22:10:13 +0000426 void EmitSelectCC(SDOperand Cond, MVT::ValueType SVT,
427 unsigned RTrue, unsigned RFalse, unsigned RDest);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000428 unsigned SelectExpr(SDOperand N);
Chris Lattner98a8ba02005-01-18 01:06:26 +0000429
430 X86AddressMode SelectAddrExprs(const X86ISelAddressMode &IAM);
431 bool MatchAddress(SDOperand N, X86ISelAddressMode &AM);
432 void SelectAddress(SDOperand N, X86AddressMode &AM);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000433 void Select(SDOperand N);
434 };
435}
436
Chris Lattner7dbcb752005-01-12 04:21:28 +0000437/// InstructionSelectBasicBlock - This callback is invoked by SelectionDAGISel
438/// when it has created a SelectionDAG for us to codegen.
439void ISel::InstructionSelectBasicBlock(SelectionDAG &DAG) {
440 // While we're doing this, keep track of whether we see any FP code for
441 // FP_REG_KILL insertion.
442 ContainsFPCode = false;
443
444 // Scan the PHI nodes that already are inserted into this basic block. If any
445 // of them is a PHI of a floating point value, we need to insert an
446 // FP_REG_KILL.
447 SSARegMap *RegMap = BB->getParent()->getSSARegMap();
448 for (MachineBasicBlock::iterator I = BB->begin(), E = BB->end();
449 I != E; ++I) {
450 assert(I->getOpcode() == X86::PHI &&
451 "Isn't just PHI nodes?");
452 if (RegMap->getRegClass(I->getOperand(0).getReg()) ==
453 X86::RFPRegisterClass) {
454 ContainsFPCode = true;
455 break;
456 }
457 }
458
459 // Compute the RegPressureMap, which is an approximation for the number of
460 // registers required to compute each node.
461 ComputeRegPressure(DAG.getRoot());
462
463 // Codegen the basic block.
464 Select(DAG.getRoot());
465
466 // Finally, look at all of the successors of this block. If any contain a PHI
467 // node of FP type, we need to insert an FP_REG_KILL in this block.
468 for (MachineBasicBlock::succ_iterator SI = BB->succ_begin(),
469 E = BB->succ_end(); SI != E && !ContainsFPCode; ++SI)
470 for (MachineBasicBlock::iterator I = (*SI)->begin(), E = (*SI)->end();
471 I != E && I->getOpcode() == X86::PHI; ++I) {
472 if (RegMap->getRegClass(I->getOperand(0).getReg()) ==
473 X86::RFPRegisterClass) {
474 ContainsFPCode = true;
475 break;
476 }
477 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000478
Chris Lattnere3e0f272005-05-09 03:36:39 +0000479 // Final check, check LLVM BB's that are successors to the LLVM BB
480 // corresponding to BB for FP PHI nodes.
481 const BasicBlock *LLVMBB = BB->getBasicBlock();
482 const PHINode *PN;
483 if (!ContainsFPCode)
484 for (succ_const_iterator SI = succ_begin(LLVMBB), E = succ_end(LLVMBB);
485 SI != E && !ContainsFPCode; ++SI)
486 for (BasicBlock::const_iterator II = SI->begin();
487 (PN = dyn_cast<PHINode>(II)); ++II)
488 if (PN->getType()->isFloatingPoint()) {
489 ContainsFPCode = true;
490 break;
491 }
492
493
Chris Lattner7dbcb752005-01-12 04:21:28 +0000494 // Insert FP_REG_KILL instructions into basic blocks that need them. This
495 // only occurs due to the floating point stackifier not being aggressive
496 // enough to handle arbitrary global stackification.
497 //
498 // Currently we insert an FP_REG_KILL instruction into each block that uses or
499 // defines a floating point virtual register.
500 //
501 // When the global register allocators (like linear scan) finally update live
502 // variable analysis, we can keep floating point values in registers across
503 // basic blocks. This will be a huge win, but we are waiting on the global
504 // allocators before we can do this.
505 //
Chris Lattner71df3f82005-03-30 01:10:00 +0000506 if (ContainsFPCode) {
Chris Lattner7dbcb752005-01-12 04:21:28 +0000507 BuildMI(*BB, BB->getFirstTerminator(), X86::FP_REG_KILL, 0);
508 ++NumFPKill;
509 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000510
Chris Lattner7dbcb752005-01-12 04:21:28 +0000511 // Clear state used for selection.
512 ExprMap.clear();
Chris Lattner7dbcb752005-01-12 04:21:28 +0000513 RegPressureMap.clear();
514}
515
516
Chris Lattner11333092005-01-11 03:11:44 +0000517// ComputeRegPressure - Compute the RegPressureMap, which is an approximation
518// for the number of registers required to compute each node. This is basically
519// computing a generalized form of the Sethi-Ullman number for each node.
520unsigned ISel::ComputeRegPressure(SDOperand O) {
521 SDNode *N = O.Val;
522 unsigned &Result = RegPressureMap[N];
523 if (Result) return Result;
524
Chris Lattnera3aa2e22005-01-11 03:37:59 +0000525 // FIXME: Should operations like CALL (which clobber lots o regs) have a
526 // higher fixed cost??
527
Chris Lattnerc4b6a782005-01-11 22:29:12 +0000528 if (N->getNumOperands() == 0) {
529 Result = 1;
530 } else {
531 unsigned MaxRegUse = 0;
532 unsigned NumExtraMaxRegUsers = 0;
533 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
534 unsigned Regs;
535 if (N->getOperand(i).getOpcode() == ISD::Constant)
536 Regs = 0;
537 else
538 Regs = ComputeRegPressure(N->getOperand(i));
539 if (Regs > MaxRegUse) {
540 MaxRegUse = Regs;
541 NumExtraMaxRegUsers = 0;
542 } else if (Regs == MaxRegUse &&
543 N->getOperand(i).getValueType() != MVT::Other) {
544 ++NumExtraMaxRegUsers;
545 }
Chris Lattner11333092005-01-11 03:11:44 +0000546 }
Chris Lattner90d1be72005-01-17 22:56:09 +0000547
548 if (O.getOpcode() != ISD::TokenFactor)
549 Result = MaxRegUse+NumExtraMaxRegUsers;
550 else
Chris Lattner869e0432005-01-17 23:02:13 +0000551 Result = MaxRegUse == 1 ? 0 : MaxRegUse-1;
Chris Lattnerc4b6a782005-01-11 22:29:12 +0000552 }
Chris Lattnerafce4302005-01-12 02:19:06 +0000553
Chris Lattner837caa72005-01-11 23:21:30 +0000554 //std::cerr << " WEIGHT: " << Result << " "; N->dump(); std::cerr << "\n";
Chris Lattnerc4b6a782005-01-11 22:29:12 +0000555 return Result;
Chris Lattner11333092005-01-11 03:11:44 +0000556}
557
Chris Lattnerbf52d492005-01-20 16:50:16 +0000558/// NodeTransitivelyUsesValue - Return true if N or any of its uses uses Op.
559/// The DAG cannot have cycles in it, by definition, so the visited set is not
560/// needed to prevent infinite loops. The DAG CAN, however, have unbounded
561/// reuse, so it prevents exponential cases.
562///
563static bool NodeTransitivelyUsesValue(SDOperand N, SDOperand Op,
564 std::set<SDNode*> &Visited) {
565 if (N == Op) return true; // Found it.
566 SDNode *Node = N.Val;
Chris Lattnerfb0f53f2005-01-21 21:43:02 +0000567 if (Node->getNumOperands() == 0 || // Leaf?
568 Node->getNodeDepth() <= Op.getNodeDepth()) return false; // Can't find it?
Chris Lattnerbf52d492005-01-20 16:50:16 +0000569 if (!Visited.insert(Node).second) return false; // Already visited?
570
571 // Recurse for the first N-1 operands.
572 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i)
573 if (NodeTransitivelyUsesValue(Node->getOperand(i), Op, Visited))
574 return true;
575
576 // Tail recurse for the last operand.
577 return NodeTransitivelyUsesValue(Node->getOperand(0), Op, Visited);
578}
579
Chris Lattner98a8ba02005-01-18 01:06:26 +0000580X86AddressMode ISel::SelectAddrExprs(const X86ISelAddressMode &IAM) {
581 X86AddressMode Result;
582
583 // If we need to emit two register operands, emit the one with the highest
584 // register pressure first.
585 if (IAM.BaseType == X86ISelAddressMode::RegBase &&
586 IAM.Base.Reg.Val && IAM.IndexReg.Val) {
Chris Lattnerbf52d492005-01-20 16:50:16 +0000587 bool EmitBaseThenIndex;
Chris Lattner98a8ba02005-01-18 01:06:26 +0000588 if (getRegPressure(IAM.Base.Reg) > getRegPressure(IAM.IndexReg)) {
Chris Lattnerbf52d492005-01-20 16:50:16 +0000589 std::set<SDNode*> Visited;
590 EmitBaseThenIndex = true;
591 // If Base ends up pointing to Index, we must emit index first. This is
592 // because of the way we fold loads, we may end up doing bad things with
593 // the folded add.
594 if (NodeTransitivelyUsesValue(IAM.Base.Reg, IAM.IndexReg, Visited))
595 EmitBaseThenIndex = false;
596 } else {
597 std::set<SDNode*> Visited;
598 EmitBaseThenIndex = false;
599 // If Base ends up pointing to Index, we must emit index first. This is
600 // because of the way we fold loads, we may end up doing bad things with
601 // the folded add.
602 if (NodeTransitivelyUsesValue(IAM.IndexReg, IAM.Base.Reg, Visited))
603 EmitBaseThenIndex = true;
604 }
605
606 if (EmitBaseThenIndex) {
Chris Lattner98a8ba02005-01-18 01:06:26 +0000607 Result.Base.Reg = SelectExpr(IAM.Base.Reg);
608 Result.IndexReg = SelectExpr(IAM.IndexReg);
609 } else {
610 Result.IndexReg = SelectExpr(IAM.IndexReg);
611 Result.Base.Reg = SelectExpr(IAM.Base.Reg);
612 }
Chris Lattnerbf52d492005-01-20 16:50:16 +0000613
Chris Lattner98a8ba02005-01-18 01:06:26 +0000614 } else if (IAM.BaseType == X86ISelAddressMode::RegBase && IAM.Base.Reg.Val) {
615 Result.Base.Reg = SelectExpr(IAM.Base.Reg);
616 } else if (IAM.IndexReg.Val) {
617 Result.IndexReg = SelectExpr(IAM.IndexReg);
618 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000619
Chris Lattner98a8ba02005-01-18 01:06:26 +0000620 switch (IAM.BaseType) {
621 case X86ISelAddressMode::RegBase:
622 Result.BaseType = X86AddressMode::RegBase;
623 break;
624 case X86ISelAddressMode::FrameIndexBase:
625 Result.BaseType = X86AddressMode::FrameIndexBase;
626 Result.Base.FrameIndex = IAM.Base.FrameIndex;
627 break;
628 default:
629 assert(0 && "Unknown base type!");
630 break;
631 }
632 Result.Scale = IAM.Scale;
633 Result.Disp = IAM.Disp;
634 Result.GV = IAM.GV;
635 return Result;
636}
637
638/// SelectAddress - Pattern match the maximal addressing mode for this node and
639/// emit all of the leaf registers.
640void ISel::SelectAddress(SDOperand N, X86AddressMode &AM) {
641 X86ISelAddressMode IAM;
642 MatchAddress(N, IAM);
643 AM = SelectAddrExprs(IAM);
644}
645
646/// MatchAddress - Add the specified node to the specified addressing mode,
647/// returning true if it cannot be done. This just pattern matches for the
648/// addressing mode, it does not cause any code to be emitted. For that, use
649/// SelectAddress.
650bool ISel::MatchAddress(SDOperand N, X86ISelAddressMode &AM) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000651 switch (N.getOpcode()) {
652 default: break;
653 case ISD::FrameIndex:
Chris Lattner98a8ba02005-01-18 01:06:26 +0000654 if (AM.BaseType == X86ISelAddressMode::RegBase && AM.Base.Reg.Val == 0) {
655 AM.BaseType = X86ISelAddressMode::FrameIndexBase;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000656 AM.Base.FrameIndex = cast<FrameIndexSDNode>(N)->getIndex();
657 return false;
658 }
659 break;
660 case ISD::GlobalAddress:
661 if (AM.GV == 0) {
662 AM.GV = cast<GlobalAddressSDNode>(N)->getGlobal();
663 return false;
664 }
665 break;
666 case ISD::Constant:
667 AM.Disp += cast<ConstantSDNode>(N)->getValue();
668 return false;
669 case ISD::SHL:
Chris Lattner636e79a2005-01-13 05:53:16 +0000670 // We might have folded the load into this shift, so don't regen the value
671 // if so.
672 if (ExprMap.count(N)) break;
673
Chris Lattner98a8ba02005-01-18 01:06:26 +0000674 if (AM.IndexReg.Val == 0 && AM.Scale == 1)
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000675 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1))) {
676 unsigned Val = CN->getValue();
677 if (Val == 1 || Val == 2 || Val == 3) {
678 AM.Scale = 1 << Val;
Chris Lattner51a26342005-01-11 06:36:20 +0000679 SDOperand ShVal = N.Val->getOperand(0);
680
681 // Okay, we know that we have a scale by now. However, if the scaled
682 // value is an add of something and a constant, we can fold the
683 // constant into the disp field here.
Chris Lattner811482a2005-01-18 04:18:32 +0000684 if (ShVal.Val->getOpcode() == ISD::ADD && ShVal.hasOneUse() &&
Chris Lattner51a26342005-01-11 06:36:20 +0000685 isa<ConstantSDNode>(ShVal.Val->getOperand(1))) {
Chris Lattner98a8ba02005-01-18 01:06:26 +0000686 AM.IndexReg = ShVal.Val->getOperand(0);
Chris Lattner51a26342005-01-11 06:36:20 +0000687 ConstantSDNode *AddVal =
688 cast<ConstantSDNode>(ShVal.Val->getOperand(1));
689 AM.Disp += AddVal->getValue() << Val;
Chris Lattner636e79a2005-01-13 05:53:16 +0000690 } else {
Chris Lattner98a8ba02005-01-18 01:06:26 +0000691 AM.IndexReg = ShVal;
Chris Lattner51a26342005-01-11 06:36:20 +0000692 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000693 return false;
694 }
695 }
696 break;
Chris Lattner947d5442005-01-11 19:37:02 +0000697 case ISD::MUL:
Chris Lattner636e79a2005-01-13 05:53:16 +0000698 // We might have folded the load into this mul, so don't regen the value if
699 // so.
700 if (ExprMap.count(N)) break;
701
Chris Lattner947d5442005-01-11 19:37:02 +0000702 // X*[3,5,9] -> X+X*[2,4,8]
Chris Lattner98a8ba02005-01-18 01:06:26 +0000703 if (AM.IndexReg.Val == 0 && AM.BaseType == X86ISelAddressMode::RegBase &&
704 AM.Base.Reg.Val == 0)
Chris Lattner947d5442005-01-11 19:37:02 +0000705 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1)))
706 if (CN->getValue() == 3 || CN->getValue() == 5 || CN->getValue() == 9) {
707 AM.Scale = unsigned(CN->getValue())-1;
708
709 SDOperand MulVal = N.Val->getOperand(0);
Chris Lattner98a8ba02005-01-18 01:06:26 +0000710 SDOperand Reg;
Chris Lattner947d5442005-01-11 19:37:02 +0000711
712 // Okay, we know that we have a scale by now. However, if the scaled
713 // value is an add of something and a constant, we can fold the
714 // constant into the disp field here.
Chris Lattner811482a2005-01-18 04:18:32 +0000715 if (MulVal.Val->getOpcode() == ISD::ADD && MulVal.hasOneUse() &&
Chris Lattner947d5442005-01-11 19:37:02 +0000716 isa<ConstantSDNode>(MulVal.Val->getOperand(1))) {
Chris Lattner98a8ba02005-01-18 01:06:26 +0000717 Reg = MulVal.Val->getOperand(0);
Chris Lattner947d5442005-01-11 19:37:02 +0000718 ConstantSDNode *AddVal =
719 cast<ConstantSDNode>(MulVal.Val->getOperand(1));
720 AM.Disp += AddVal->getValue() * CN->getValue();
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000721 } else {
Chris Lattner98a8ba02005-01-18 01:06:26 +0000722 Reg = N.Val->getOperand(0);
Chris Lattner947d5442005-01-11 19:37:02 +0000723 }
724
725 AM.IndexReg = AM.Base.Reg = Reg;
726 return false;
727 }
728 break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000729
730 case ISD::ADD: {
Chris Lattner636e79a2005-01-13 05:53:16 +0000731 // We might have folded the load into this mul, so don't regen the value if
732 // so.
733 if (ExprMap.count(N)) break;
734
Chris Lattner98a8ba02005-01-18 01:06:26 +0000735 X86ISelAddressMode Backup = AM;
736 if (!MatchAddress(N.Val->getOperand(0), AM) &&
737 !MatchAddress(N.Val->getOperand(1), AM))
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000738 return false;
739 AM = Backup;
Chris Lattner98a8ba02005-01-18 01:06:26 +0000740 if (!MatchAddress(N.Val->getOperand(1), AM) &&
741 !MatchAddress(N.Val->getOperand(0), AM))
Chris Lattner9bbd9922005-01-12 18:08:53 +0000742 return false;
743 AM = Backup;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000744 break;
745 }
746 }
747
Chris Lattnera95589b2005-01-11 04:40:19 +0000748 // Is the base register already occupied?
Chris Lattner98a8ba02005-01-18 01:06:26 +0000749 if (AM.BaseType != X86ISelAddressMode::RegBase || AM.Base.Reg.Val) {
Chris Lattnera95589b2005-01-11 04:40:19 +0000750 // If so, check to see if the scale index register is set.
Chris Lattner98a8ba02005-01-18 01:06:26 +0000751 if (AM.IndexReg.Val == 0) {
752 AM.IndexReg = N;
Chris Lattnera95589b2005-01-11 04:40:19 +0000753 AM.Scale = 1;
754 return false;
755 }
756
757 // Otherwise, we cannot select it.
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000758 return true;
Chris Lattnera95589b2005-01-11 04:40:19 +0000759 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000760
761 // Default, generate it as a register.
Chris Lattner98a8ba02005-01-18 01:06:26 +0000762 AM.BaseType = X86ISelAddressMode::RegBase;
763 AM.Base.Reg = N;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000764 return false;
765}
766
767/// Emit2SetCCsAndLogical - Emit the following sequence of instructions,
768/// assuming that the temporary registers are in the 8-bit register class.
769///
770/// Tmp1 = setcc1
771/// Tmp2 = setcc2
772/// DestReg = logicalop Tmp1, Tmp2
773///
774static void Emit2SetCCsAndLogical(MachineBasicBlock *BB, unsigned SetCC1,
775 unsigned SetCC2, unsigned LogicalOp,
776 unsigned DestReg) {
777 SSARegMap *RegMap = BB->getParent()->getSSARegMap();
778 unsigned Tmp1 = RegMap->createVirtualRegister(X86::R8RegisterClass);
779 unsigned Tmp2 = RegMap->createVirtualRegister(X86::R8RegisterClass);
780 BuildMI(BB, SetCC1, 0, Tmp1);
781 BuildMI(BB, SetCC2, 0, Tmp2);
782 BuildMI(BB, LogicalOp, 2, DestReg).addReg(Tmp1).addReg(Tmp2);
783}
784
785/// EmitSetCC - Emit the code to set the specified 8-bit register to 1 if the
786/// condition codes match the specified SetCCOpcode. Note that some conditions
787/// require multiple instructions to generate the correct value.
788static void EmitSetCC(MachineBasicBlock *BB, unsigned DestReg,
789 ISD::CondCode SetCCOpcode, bool isFP) {
790 unsigned Opc;
791 if (!isFP) {
792 switch (SetCCOpcode) {
793 default: assert(0 && "Illegal integer SetCC!");
794 case ISD::SETEQ: Opc = X86::SETEr; break;
795 case ISD::SETGT: Opc = X86::SETGr; break;
796 case ISD::SETGE: Opc = X86::SETGEr; break;
797 case ISD::SETLT: Opc = X86::SETLr; break;
798 case ISD::SETLE: Opc = X86::SETLEr; break;
799 case ISD::SETNE: Opc = X86::SETNEr; break;
800 case ISD::SETULT: Opc = X86::SETBr; break;
801 case ISD::SETUGT: Opc = X86::SETAr; break;
802 case ISD::SETULE: Opc = X86::SETBEr; break;
803 case ISD::SETUGE: Opc = X86::SETAEr; break;
804 }
805 } else {
806 // On a floating point condition, the flags are set as follows:
807 // ZF PF CF op
808 // 0 | 0 | 0 | X > Y
809 // 0 | 0 | 1 | X < Y
810 // 1 | 0 | 0 | X == Y
811 // 1 | 1 | 1 | unordered
812 //
813 switch (SetCCOpcode) {
814 default: assert(0 && "Invalid FP setcc!");
815 case ISD::SETUEQ:
816 case ISD::SETEQ:
817 Opc = X86::SETEr; // True if ZF = 1
818 break;
819 case ISD::SETOGT:
820 case ISD::SETGT:
821 Opc = X86::SETAr; // True if CF = 0 and ZF = 0
822 break;
823 case ISD::SETOGE:
824 case ISD::SETGE:
825 Opc = X86::SETAEr; // True if CF = 0
826 break;
827 case ISD::SETULT:
828 case ISD::SETLT:
829 Opc = X86::SETBr; // True if CF = 1
830 break;
831 case ISD::SETULE:
832 case ISD::SETLE:
833 Opc = X86::SETBEr; // True if CF = 1 or ZF = 1
834 break;
835 case ISD::SETONE:
836 case ISD::SETNE:
837 Opc = X86::SETNEr; // True if ZF = 0
838 break;
839 case ISD::SETUO:
840 Opc = X86::SETPr; // True if PF = 1
841 break;
842 case ISD::SETO:
843 Opc = X86::SETNPr; // True if PF = 0
844 break;
845 case ISD::SETOEQ: // !PF & ZF
846 Emit2SetCCsAndLogical(BB, X86::SETNPr, X86::SETEr, X86::AND8rr, DestReg);
847 return;
848 case ISD::SETOLT: // !PF & CF
849 Emit2SetCCsAndLogical(BB, X86::SETNPr, X86::SETBr, X86::AND8rr, DestReg);
850 return;
851 case ISD::SETOLE: // !PF & (CF || ZF)
852 Emit2SetCCsAndLogical(BB, X86::SETNPr, X86::SETBEr, X86::AND8rr, DestReg);
853 return;
854 case ISD::SETUGT: // PF | (!ZF & !CF)
855 Emit2SetCCsAndLogical(BB, X86::SETPr, X86::SETAr, X86::OR8rr, DestReg);
856 return;
857 case ISD::SETUGE: // PF | !CF
858 Emit2SetCCsAndLogical(BB, X86::SETPr, X86::SETAEr, X86::OR8rr, DestReg);
859 return;
860 case ISD::SETUNE: // PF | !ZF
861 Emit2SetCCsAndLogical(BB, X86::SETPr, X86::SETNEr, X86::OR8rr, DestReg);
862 return;
863 }
864 }
865 BuildMI(BB, Opc, 0, DestReg);
866}
867
868
869/// EmitBranchCC - Emit code into BB that arranges for control to transfer to
870/// the Dest block if the Cond condition is true. If we cannot fold this
871/// condition into the branch, return true.
872///
Chris Lattner6c07aee2005-01-11 04:06:27 +0000873bool ISel::EmitBranchCC(MachineBasicBlock *Dest, SDOperand Chain,
874 SDOperand Cond) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000875 // FIXME: Evaluate whether it would be good to emit code like (X < Y) | (A >
876 // B) using two conditional branches instead of one condbr, two setcc's, and
877 // an or.
878 if ((Cond.getOpcode() == ISD::OR ||
879 Cond.getOpcode() == ISD::AND) && Cond.Val->hasOneUse()) {
880 // And and or set the flags for us, so there is no need to emit a TST of the
881 // result. It is only safe to do this if there is only a single use of the
882 // AND/OR though, otherwise we don't know it will be emitted here.
Chris Lattner6c07aee2005-01-11 04:06:27 +0000883 Select(Chain);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000884 SelectExpr(Cond);
885 BuildMI(BB, X86::JNE, 1).addMBB(Dest);
886 return false;
887 }
888
889 // Codegen br not C -> JE.
890 if (Cond.getOpcode() == ISD::XOR)
891 if (ConstantSDNode *NC = dyn_cast<ConstantSDNode>(Cond.Val->getOperand(1)))
892 if (NC->isAllOnesValue()) {
Chris Lattner6c07aee2005-01-11 04:06:27 +0000893 unsigned CondR;
894 if (getRegPressure(Chain) > getRegPressure(Cond)) {
895 Select(Chain);
896 CondR = SelectExpr(Cond.Val->getOperand(0));
897 } else {
898 CondR = SelectExpr(Cond.Val->getOperand(0));
899 Select(Chain);
900 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000901 BuildMI(BB, X86::TEST8rr, 2).addReg(CondR).addReg(CondR);
902 BuildMI(BB, X86::JE, 1).addMBB(Dest);
903 return false;
904 }
905
906 SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Cond);
907 if (SetCC == 0)
908 return true; // Can only handle simple setcc's so far.
909
910 unsigned Opc;
911
912 // Handle integer conditions first.
913 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
914 switch (SetCC->getCondition()) {
915 default: assert(0 && "Illegal integer SetCC!");
916 case ISD::SETEQ: Opc = X86::JE; break;
917 case ISD::SETGT: Opc = X86::JG; break;
918 case ISD::SETGE: Opc = X86::JGE; break;
919 case ISD::SETLT: Opc = X86::JL; break;
920 case ISD::SETLE: Opc = X86::JLE; break;
921 case ISD::SETNE: Opc = X86::JNE; break;
922 case ISD::SETULT: Opc = X86::JB; break;
923 case ISD::SETUGT: Opc = X86::JA; break;
924 case ISD::SETULE: Opc = X86::JBE; break;
925 case ISD::SETUGE: Opc = X86::JAE; break;
926 }
Chris Lattner6c07aee2005-01-11 04:06:27 +0000927 Select(Chain);
Chris Lattnercb1aa8d2005-01-17 01:34:14 +0000928 EmitCMP(SetCC->getOperand(0), SetCC->getOperand(1), SetCC->hasOneUse());
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000929 BuildMI(BB, Opc, 1).addMBB(Dest);
930 return false;
931 }
932
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000933 unsigned Opc2 = 0; // Second branch if needed.
934
935 // On a floating point condition, the flags are set as follows:
936 // ZF PF CF op
937 // 0 | 0 | 0 | X > Y
938 // 0 | 0 | 1 | X < Y
939 // 1 | 0 | 0 | X == Y
940 // 1 | 1 | 1 | unordered
941 //
942 switch (SetCC->getCondition()) {
943 default: assert(0 && "Invalid FP setcc!");
944 case ISD::SETUEQ:
945 case ISD::SETEQ: Opc = X86::JE; break; // True if ZF = 1
946 case ISD::SETOGT:
947 case ISD::SETGT: Opc = X86::JA; break; // True if CF = 0 and ZF = 0
948 case ISD::SETOGE:
949 case ISD::SETGE: Opc = X86::JAE; break; // True if CF = 0
950 case ISD::SETULT:
951 case ISD::SETLT: Opc = X86::JB; break; // True if CF = 1
952 case ISD::SETULE:
953 case ISD::SETLE: Opc = X86::JBE; break; // True if CF = 1 or ZF = 1
954 case ISD::SETONE:
955 case ISD::SETNE: Opc = X86::JNE; break; // True if ZF = 0
956 case ISD::SETUO: Opc = X86::JP; break; // True if PF = 1
957 case ISD::SETO: Opc = X86::JNP; break; // True if PF = 0
958 case ISD::SETUGT: // PF = 1 | (ZF = 0 & CF = 0)
959 Opc = X86::JA; // ZF = 0 & CF = 0
960 Opc2 = X86::JP; // PF = 1
961 break;
962 case ISD::SETUGE: // PF = 1 | CF = 0
963 Opc = X86::JAE; // CF = 0
964 Opc2 = X86::JP; // PF = 1
965 break;
966 case ISD::SETUNE: // PF = 1 | ZF = 0
967 Opc = X86::JNE; // ZF = 0
968 Opc2 = X86::JP; // PF = 1
969 break;
970 case ISD::SETOEQ: // PF = 0 & ZF = 1
971 //X86::JNP, X86::JE
972 //X86::AND8rr
973 return true; // FIXME: Emit more efficient code for this branch.
974 case ISD::SETOLT: // PF = 0 & CF = 1
975 //X86::JNP, X86::JB
976 //X86::AND8rr
977 return true; // FIXME: Emit more efficient code for this branch.
978 case ISD::SETOLE: // PF = 0 & (CF = 1 || ZF = 1)
979 //X86::JNP, X86::JBE
980 //X86::AND8rr
981 return true; // FIXME: Emit more efficient code for this branch.
982 }
983
Chris Lattner6c07aee2005-01-11 04:06:27 +0000984 Select(Chain);
Chris Lattnercb1aa8d2005-01-17 01:34:14 +0000985 EmitCMP(SetCC->getOperand(0), SetCC->getOperand(1), SetCC->hasOneUse());
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000986 BuildMI(BB, Opc, 1).addMBB(Dest);
987 if (Opc2)
988 BuildMI(BB, Opc2, 1).addMBB(Dest);
989 return false;
990}
991
Chris Lattner24aad1b2005-01-10 22:10:13 +0000992/// EmitSelectCC - Emit code into BB that performs a select operation between
993/// the two registers RTrue and RFalse, generating a result into RDest. Return
994/// true if the fold cannot be performed.
995///
996void ISel::EmitSelectCC(SDOperand Cond, MVT::ValueType SVT,
997 unsigned RTrue, unsigned RFalse, unsigned RDest) {
998 enum Condition {
999 EQ, NE, LT, LE, GT, GE, B, BE, A, AE, P, NP,
1000 NOT_SET
1001 } CondCode = NOT_SET;
1002
1003 static const unsigned CMOVTAB16[] = {
1004 X86::CMOVE16rr, X86::CMOVNE16rr, X86::CMOVL16rr, X86::CMOVLE16rr,
1005 X86::CMOVG16rr, X86::CMOVGE16rr, X86::CMOVB16rr, X86::CMOVBE16rr,
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001006 X86::CMOVA16rr, X86::CMOVAE16rr, X86::CMOVP16rr, X86::CMOVNP16rr,
Chris Lattner24aad1b2005-01-10 22:10:13 +00001007 };
1008 static const unsigned CMOVTAB32[] = {
1009 X86::CMOVE32rr, X86::CMOVNE32rr, X86::CMOVL32rr, X86::CMOVLE32rr,
1010 X86::CMOVG32rr, X86::CMOVGE32rr, X86::CMOVB32rr, X86::CMOVBE32rr,
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001011 X86::CMOVA32rr, X86::CMOVAE32rr, X86::CMOVP32rr, X86::CMOVNP32rr,
Chris Lattner24aad1b2005-01-10 22:10:13 +00001012 };
1013 static const unsigned CMOVTABFP[] = {
1014 X86::FCMOVE , X86::FCMOVNE, /*missing*/0, /*missing*/0,
1015 /*missing*/0, /*missing*/0, X86::FCMOVB , X86::FCMOVBE,
1016 X86::FCMOVA , X86::FCMOVAE, X86::FCMOVP , X86::FCMOVNP
1017 };
1018
1019 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Cond)) {
1020 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
1021 switch (SetCC->getCondition()) {
1022 default: assert(0 && "Unknown integer comparison!");
1023 case ISD::SETEQ: CondCode = EQ; break;
1024 case ISD::SETGT: CondCode = GT; break;
1025 case ISD::SETGE: CondCode = GE; break;
1026 case ISD::SETLT: CondCode = LT; break;
1027 case ISD::SETLE: CondCode = LE; break;
1028 case ISD::SETNE: CondCode = NE; break;
1029 case ISD::SETULT: CondCode = B; break;
1030 case ISD::SETUGT: CondCode = A; break;
1031 case ISD::SETULE: CondCode = BE; break;
1032 case ISD::SETUGE: CondCode = AE; break;
1033 }
1034 } else {
1035 // On a floating point condition, the flags are set as follows:
1036 // ZF PF CF op
1037 // 0 | 0 | 0 | X > Y
1038 // 0 | 0 | 1 | X < Y
1039 // 1 | 0 | 0 | X == Y
1040 // 1 | 1 | 1 | unordered
1041 //
1042 switch (SetCC->getCondition()) {
1043 default: assert(0 && "Unknown FP comparison!");
1044 case ISD::SETUEQ:
1045 case ISD::SETEQ: CondCode = EQ; break; // True if ZF = 1
1046 case ISD::SETOGT:
1047 case ISD::SETGT: CondCode = A; break; // True if CF = 0 and ZF = 0
1048 case ISD::SETOGE:
1049 case ISD::SETGE: CondCode = AE; break; // True if CF = 0
1050 case ISD::SETULT:
1051 case ISD::SETLT: CondCode = B; break; // True if CF = 1
1052 case ISD::SETULE:
1053 case ISD::SETLE: CondCode = BE; break; // True if CF = 1 or ZF = 1
1054 case ISD::SETONE:
1055 case ISD::SETNE: CondCode = NE; break; // True if ZF = 0
1056 case ISD::SETUO: CondCode = P; break; // True if PF = 1
1057 case ISD::SETO: CondCode = NP; break; // True if PF = 0
1058 case ISD::SETUGT: // PF = 1 | (ZF = 0 & CF = 0)
1059 case ISD::SETUGE: // PF = 1 | CF = 0
1060 case ISD::SETUNE: // PF = 1 | ZF = 0
1061 case ISD::SETOEQ: // PF = 0 & ZF = 1
1062 case ISD::SETOLT: // PF = 0 & CF = 1
1063 case ISD::SETOLE: // PF = 0 & (CF = 1 || ZF = 1)
1064 // We cannot emit this comparison as a single cmov.
1065 break;
1066 }
1067 }
1068 }
1069
1070 unsigned Opc = 0;
1071 if (CondCode != NOT_SET) {
1072 switch (SVT) {
1073 default: assert(0 && "Cannot select this type!");
1074 case MVT::i16: Opc = CMOVTAB16[CondCode]; break;
1075 case MVT::i32: Opc = CMOVTAB32[CondCode]; break;
Chris Lattneref7ba072005-01-11 03:50:45 +00001076 case MVT::f64: Opc = CMOVTABFP[CondCode]; break;
Chris Lattner24aad1b2005-01-10 22:10:13 +00001077 }
1078 }
1079
1080 // Finally, if we weren't able to fold this, just emit the condition and test
1081 // it.
1082 if (CondCode == NOT_SET || Opc == 0) {
1083 // Get the condition into the zero flag.
1084 unsigned CondReg = SelectExpr(Cond);
1085 BuildMI(BB, X86::TEST8rr, 2).addReg(CondReg).addReg(CondReg);
1086
1087 switch (SVT) {
1088 default: assert(0 && "Cannot select this type!");
1089 case MVT::i16: Opc = X86::CMOVE16rr; break;
1090 case MVT::i32: Opc = X86::CMOVE32rr; break;
Chris Lattneref7ba072005-01-11 03:50:45 +00001091 case MVT::f64: Opc = X86::FCMOVE; break;
Chris Lattner24aad1b2005-01-10 22:10:13 +00001092 }
1093 } else {
1094 // FIXME: CMP R, 0 -> TEST R, R
Chris Lattnercb1aa8d2005-01-17 01:34:14 +00001095 EmitCMP(Cond.getOperand(0), Cond.getOperand(1), Cond.Val->hasOneUse());
Chris Lattnera3aa2e22005-01-11 03:37:59 +00001096 std::swap(RTrue, RFalse);
Chris Lattner24aad1b2005-01-10 22:10:13 +00001097 }
1098 BuildMI(BB, Opc, 2, RDest).addReg(RTrue).addReg(RFalse);
1099}
1100
Chris Lattnercb1aa8d2005-01-17 01:34:14 +00001101void ISel::EmitCMP(SDOperand LHS, SDOperand RHS, bool HasOneUse) {
Chris Lattner11333092005-01-11 03:11:44 +00001102 unsigned Opc;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001103 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(RHS)) {
1104 Opc = 0;
Chris Lattner4ff348b2005-01-17 06:26:58 +00001105 if (HasOneUse && isFoldableLoad(LHS, RHS)) {
Chris Lattneref6806c2005-01-12 02:02:48 +00001106 switch (RHS.getValueType()) {
1107 default: break;
1108 case MVT::i1:
1109 case MVT::i8: Opc = X86::CMP8mi; break;
1110 case MVT::i16: Opc = X86::CMP16mi; break;
1111 case MVT::i32: Opc = X86::CMP32mi; break;
1112 }
1113 if (Opc) {
1114 X86AddressMode AM;
1115 EmitFoldedLoad(LHS, AM);
1116 addFullAddress(BuildMI(BB, Opc, 5), AM).addImm(CN->getValue());
1117 return;
1118 }
1119 }
1120
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001121 switch (RHS.getValueType()) {
1122 default: break;
1123 case MVT::i1:
1124 case MVT::i8: Opc = X86::CMP8ri; break;
1125 case MVT::i16: Opc = X86::CMP16ri; break;
1126 case MVT::i32: Opc = X86::CMP32ri; break;
1127 }
1128 if (Opc) {
Chris Lattner11333092005-01-11 03:11:44 +00001129 unsigned Tmp1 = SelectExpr(LHS);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001130 BuildMI(BB, Opc, 2).addReg(Tmp1).addImm(CN->getValue());
1131 return;
1132 }
Chris Lattner7f2afac2005-01-14 22:37:41 +00001133 } else if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(RHS)) {
1134 if (CN->isExactlyValue(+0.0) ||
1135 CN->isExactlyValue(-0.0)) {
1136 unsigned Reg = SelectExpr(LHS);
1137 BuildMI(BB, X86::FTST, 1).addReg(Reg);
1138 BuildMI(BB, X86::FNSTSW8r, 0);
1139 BuildMI(BB, X86::SAHF, 1);
Chris Lattner7805fa42005-03-17 16:29:26 +00001140 return;
Chris Lattner7f2afac2005-01-14 22:37:41 +00001141 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001142 }
1143
Chris Lattneref6806c2005-01-12 02:02:48 +00001144 Opc = 0;
Chris Lattner4ff348b2005-01-17 06:26:58 +00001145 if (HasOneUse && isFoldableLoad(LHS, RHS)) {
Chris Lattneref6806c2005-01-12 02:02:48 +00001146 switch (RHS.getValueType()) {
1147 default: break;
1148 case MVT::i1:
1149 case MVT::i8: Opc = X86::CMP8mr; break;
1150 case MVT::i16: Opc = X86::CMP16mr; break;
1151 case MVT::i32: Opc = X86::CMP32mr; break;
1152 }
1153 if (Opc) {
1154 X86AddressMode AM;
Chris Lattner636e79a2005-01-13 05:53:16 +00001155 EmitFoldedLoad(LHS, AM);
1156 unsigned Reg = SelectExpr(RHS);
Chris Lattneref6806c2005-01-12 02:02:48 +00001157 addFullAddress(BuildMI(BB, Opc, 5), AM).addReg(Reg);
1158 return;
1159 }
1160 }
1161
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001162 switch (LHS.getValueType()) {
1163 default: assert(0 && "Cannot compare this value!");
1164 case MVT::i1:
1165 case MVT::i8: Opc = X86::CMP8rr; break;
1166 case MVT::i16: Opc = X86::CMP16rr; break;
1167 case MVT::i32: Opc = X86::CMP32rr; break;
Chris Lattneref7ba072005-01-11 03:50:45 +00001168 case MVT::f64: Opc = X86::FUCOMIr; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001169 }
Chris Lattner11333092005-01-11 03:11:44 +00001170 unsigned Tmp1, Tmp2;
1171 if (getRegPressure(LHS) > getRegPressure(RHS)) {
1172 Tmp1 = SelectExpr(LHS);
1173 Tmp2 = SelectExpr(RHS);
1174 } else {
1175 Tmp2 = SelectExpr(RHS);
1176 Tmp1 = SelectExpr(LHS);
1177 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001178 BuildMI(BB, Opc, 2).addReg(Tmp1).addReg(Tmp2);
1179}
1180
Chris Lattnera5ade062005-01-11 21:19:59 +00001181/// isFoldableLoad - Return true if this is a load instruction that can safely
1182/// be folded into an operation that uses it.
Chris Lattner44129b52005-01-25 20:03:11 +00001183bool ISel::isFoldableLoad(SDOperand Op, SDOperand OtherOp, bool FloatPromoteOk){
1184 if (Op.getOpcode() == ISD::LOAD) {
1185 // FIXME: currently can't fold constant pool indexes.
1186 if (isa<ConstantPoolSDNode>(Op.getOperand(1)))
1187 return false;
1188 } else if (FloatPromoteOk && Op.getOpcode() == ISD::EXTLOAD &&
1189 cast<MVTSDNode>(Op)->getExtraValueType() == MVT::f32) {
1190 // FIXME: currently can't fold constant pool indexes.
1191 if (isa<ConstantPoolSDNode>(Op.getOperand(1)))
1192 return false;
1193 } else {
Chris Lattnera5ade062005-01-11 21:19:59 +00001194 return false;
Chris Lattner44129b52005-01-25 20:03:11 +00001195 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001196
1197 // If this load has already been emitted, we clearly can't fold it.
Chris Lattner636e79a2005-01-13 05:53:16 +00001198 assert(Op.ResNo == 0 && "Not a use of the value of the load?");
1199 if (ExprMap.count(Op.getValue(1))) return false;
1200 assert(!ExprMap.count(Op.getValue(0)) && "Value in map but not token chain?");
Chris Lattner4a108662005-01-18 03:51:59 +00001201 assert(!ExprMap.count(Op.getValue(1))&&"Token lowered but value not in map?");
Chris Lattnera5ade062005-01-11 21:19:59 +00001202
Chris Lattner4ff348b2005-01-17 06:26:58 +00001203 // If there is not just one use of its value, we cannot fold.
1204 if (!Op.Val->hasNUsesOfValue(1, 0)) return false;
1205
1206 // Finally, we cannot fold the load into the operation if this would induce a
1207 // cycle into the resultant dag. To check for this, see if OtherOp (the other
1208 // operand of the operation we are folding the load into) can possible use the
1209 // chain node defined by the load.
1210 if (OtherOp.Val && !Op.Val->hasNUsesOfValue(0, 1)) { // Has uses of chain?
1211 std::set<SDNode*> Visited;
1212 if (NodeTransitivelyUsesValue(OtherOp, Op.getValue(1), Visited))
1213 return false;
1214 }
1215 return true;
Chris Lattnera5ade062005-01-11 21:19:59 +00001216}
1217
Chris Lattner4ff348b2005-01-17 06:26:58 +00001218
Chris Lattnera5ade062005-01-11 21:19:59 +00001219/// EmitFoldedLoad - Ensure that the arguments of the load are code generated,
1220/// and compute the address being loaded into AM.
1221void ISel::EmitFoldedLoad(SDOperand Op, X86AddressMode &AM) {
1222 SDOperand Chain = Op.getOperand(0);
1223 SDOperand Address = Op.getOperand(1);
Chris Lattner98a8ba02005-01-18 01:06:26 +00001224
Chris Lattnera5ade062005-01-11 21:19:59 +00001225 if (getRegPressure(Chain) > getRegPressure(Address)) {
1226 Select(Chain);
1227 SelectAddress(Address, AM);
1228 } else {
1229 SelectAddress(Address, AM);
1230 Select(Chain);
1231 }
1232
1233 // The chain for this load is now lowered.
Chris Lattner636e79a2005-01-13 05:53:16 +00001234 assert(ExprMap.count(SDOperand(Op.Val, 1)) == 0 &&
1235 "Load emitted more than once?");
Chris Lattner4a108662005-01-18 03:51:59 +00001236 if (!ExprMap.insert(std::make_pair(Op.getValue(1), 1)).second)
Chris Lattner636e79a2005-01-13 05:53:16 +00001237 assert(0 && "Load emitted more than once!");
Chris Lattnera5ade062005-01-11 21:19:59 +00001238}
1239
Chris Lattner30ea1e92005-01-19 07:37:26 +00001240// EmitOrOpOp - Pattern match the expression (Op1|Op2), where we know that op1
1241// and op2 are i8/i16/i32 values with one use each (the or). If we can form a
1242// SHLD or SHRD, emit the instruction (generating the value into DestReg) and
1243// return true.
1244bool ISel::EmitOrOpOp(SDOperand Op1, SDOperand Op2, unsigned DestReg) {
Chris Lattner85716372005-01-19 06:18:43 +00001245 if (Op1.getOpcode() == ISD::SHL && Op2.getOpcode() == ISD::SRL) {
1246 // good!
1247 } else if (Op2.getOpcode() == ISD::SHL && Op1.getOpcode() == ISD::SRL) {
1248 std::swap(Op1, Op2); // Op1 is the SHL now.
1249 } else {
1250 return false; // No match
1251 }
1252
1253 SDOperand ShlVal = Op1.getOperand(0);
1254 SDOperand ShlAmt = Op1.getOperand(1);
1255 SDOperand ShrVal = Op2.getOperand(0);
1256 SDOperand ShrAmt = Op2.getOperand(1);
1257
Chris Lattner30ea1e92005-01-19 07:37:26 +00001258 unsigned RegSize = MVT::getSizeInBits(Op1.getValueType());
1259
Chris Lattner85716372005-01-19 06:18:43 +00001260 // Find out if ShrAmt = 32-ShlAmt or ShlAmt = 32-ShrAmt.
1261 if (ShlAmt.getOpcode() == ISD::SUB && ShlAmt.getOperand(1) == ShrAmt)
1262 if (ConstantSDNode *SubCST = dyn_cast<ConstantSDNode>(ShlAmt.getOperand(0)))
Chris Lattner4053b1e2005-01-19 08:07:05 +00001263 if (SubCST->getValue() == RegSize) {
1264 // (A >> ShrAmt) | (A << (32-ShrAmt)) ==> ROR A, ShrAmt
Chris Lattner85716372005-01-19 06:18:43 +00001265 // (A >> ShrAmt) | (B << (32-ShrAmt)) ==> SHRD A, B, ShrAmt
Chris Lattner4053b1e2005-01-19 08:07:05 +00001266 if (ShrVal == ShlVal) {
1267 unsigned Reg, ShAmt;
1268 if (getRegPressure(ShrVal) > getRegPressure(ShrAmt)) {
1269 Reg = SelectExpr(ShrVal);
1270 ShAmt = SelectExpr(ShrAmt);
1271 } else {
1272 ShAmt = SelectExpr(ShrAmt);
1273 Reg = SelectExpr(ShrVal);
1274 }
1275 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShAmt);
1276 unsigned Opc = RegSize == 8 ? X86::ROR8rCL :
1277 (RegSize == 16 ? X86::ROR16rCL : X86::ROR32rCL);
1278 BuildMI(BB, Opc, 1, DestReg).addReg(Reg);
1279 return true;
1280 } else if (RegSize != 8) {
Chris Lattner85716372005-01-19 06:18:43 +00001281 unsigned AReg, BReg;
1282 if (getRegPressure(ShlVal) > getRegPressure(ShrVal)) {
Chris Lattner85716372005-01-19 06:18:43 +00001283 BReg = SelectExpr(ShlVal);
Chris Lattnerc3c021b2005-01-19 17:24:34 +00001284 AReg = SelectExpr(ShrVal);
Chris Lattner85716372005-01-19 06:18:43 +00001285 } else {
Chris Lattner85716372005-01-19 06:18:43 +00001286 AReg = SelectExpr(ShrVal);
Chris Lattnerc3c021b2005-01-19 17:24:34 +00001287 BReg = SelectExpr(ShlVal);
Chris Lattner85716372005-01-19 06:18:43 +00001288 }
Chris Lattner4053b1e2005-01-19 08:07:05 +00001289 unsigned ShAmt = SelectExpr(ShrAmt);
1290 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShAmt);
1291 unsigned Opc = RegSize == 16 ? X86::SHRD16rrCL : X86::SHRD32rrCL;
1292 BuildMI(BB, Opc, 2, DestReg).addReg(AReg).addReg(BReg);
Chris Lattner85716372005-01-19 06:18:43 +00001293 return true;
1294 }
1295 }
1296
Chris Lattner4053b1e2005-01-19 08:07:05 +00001297 if (ShrAmt.getOpcode() == ISD::SUB && ShrAmt.getOperand(1) == ShlAmt)
1298 if (ConstantSDNode *SubCST = dyn_cast<ConstantSDNode>(ShrAmt.getOperand(0)))
1299 if (SubCST->getValue() == RegSize) {
1300 // (A << ShlAmt) | (A >> (32-ShlAmt)) ==> ROL A, ShrAmt
1301 // (A << ShlAmt) | (B >> (32-ShlAmt)) ==> SHLD A, B, ShrAmt
1302 if (ShrVal == ShlVal) {
1303 unsigned Reg, ShAmt;
1304 if (getRegPressure(ShrVal) > getRegPressure(ShlAmt)) {
1305 Reg = SelectExpr(ShrVal);
1306 ShAmt = SelectExpr(ShlAmt);
1307 } else {
1308 ShAmt = SelectExpr(ShlAmt);
1309 Reg = SelectExpr(ShrVal);
1310 }
1311 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShAmt);
1312 unsigned Opc = RegSize == 8 ? X86::ROL8rCL :
1313 (RegSize == 16 ? X86::ROL16rCL : X86::ROL32rCL);
1314 BuildMI(BB, Opc, 1, DestReg).addReg(Reg);
1315 return true;
1316 } else if (RegSize != 8) {
1317 unsigned AReg, BReg;
1318 if (getRegPressure(ShlVal) > getRegPressure(ShrVal)) {
Chris Lattnerc3c021b2005-01-19 17:24:34 +00001319 AReg = SelectExpr(ShlVal);
1320 BReg = SelectExpr(ShrVal);
Chris Lattner4053b1e2005-01-19 08:07:05 +00001321 } else {
Chris Lattnerc3c021b2005-01-19 17:24:34 +00001322 BReg = SelectExpr(ShrVal);
1323 AReg = SelectExpr(ShlVal);
Chris Lattner4053b1e2005-01-19 08:07:05 +00001324 }
1325 unsigned ShAmt = SelectExpr(ShlAmt);
1326 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShAmt);
1327 unsigned Opc = RegSize == 16 ? X86::SHLD16rrCL : X86::SHLD32rrCL;
1328 BuildMI(BB, Opc, 2, DestReg).addReg(AReg).addReg(BReg);
1329 return true;
1330 }
1331 }
Chris Lattner85716372005-01-19 06:18:43 +00001332
Chris Lattner4053b1e2005-01-19 08:07:05 +00001333 if (ConstantSDNode *ShrCst = dyn_cast<ConstantSDNode>(ShrAmt))
1334 if (ConstantSDNode *ShlCst = dyn_cast<ConstantSDNode>(ShlAmt))
1335 if (ShrCst->getValue() < RegSize && ShlCst->getValue() < RegSize)
1336 if (ShrCst->getValue() == RegSize-ShlCst->getValue()) {
1337 // (A >> 5) | (A << 27) --> ROR A, 5
1338 // (A >> 5) | (B << 27) --> SHRD A, B, 5
1339 if (ShrVal == ShlVal) {
1340 unsigned Reg = SelectExpr(ShrVal);
1341 unsigned Opc = RegSize == 8 ? X86::ROR8ri :
1342 (RegSize == 16 ? X86::ROR16ri : X86::ROR32ri);
1343 BuildMI(BB, Opc, 2, DestReg).addReg(Reg).addImm(ShrCst->getValue());
1344 return true;
1345 } else if (RegSize != 8) {
1346 unsigned AReg, BReg;
1347 if (getRegPressure(ShlVal) > getRegPressure(ShrVal)) {
Chris Lattner4053b1e2005-01-19 08:07:05 +00001348 BReg = SelectExpr(ShlVal);
Chris Lattnerc3c021b2005-01-19 17:24:34 +00001349 AReg = SelectExpr(ShrVal);
Chris Lattner4053b1e2005-01-19 08:07:05 +00001350 } else {
Chris Lattner4053b1e2005-01-19 08:07:05 +00001351 AReg = SelectExpr(ShrVal);
Chris Lattnerc3c021b2005-01-19 17:24:34 +00001352 BReg = SelectExpr(ShlVal);
Chris Lattner4053b1e2005-01-19 08:07:05 +00001353 }
1354 unsigned Opc = RegSize == 16 ? X86::SHRD16rri8 : X86::SHRD32rri8;
1355 BuildMI(BB, Opc, 3, DestReg).addReg(AReg).addReg(BReg)
1356 .addImm(ShrCst->getValue());
1357 return true;
1358 }
1359 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001360
Chris Lattner85716372005-01-19 06:18:43 +00001361 return false;
1362}
1363
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001364unsigned ISel::SelectExpr(SDOperand N) {
1365 unsigned Result;
1366 unsigned Tmp1, Tmp2, Tmp3;
1367 unsigned Opc = 0;
Chris Lattner5188ad72005-01-08 19:28:19 +00001368 SDNode *Node = N.Val;
Chris Lattnera5ade062005-01-11 21:19:59 +00001369 SDOperand Op0, Op1;
Chris Lattner5188ad72005-01-08 19:28:19 +00001370
Chris Lattner7f2afac2005-01-14 22:37:41 +00001371 if (Node->getOpcode() == ISD::CopyFromReg) {
1372 // FIXME: Handle copy from physregs!
1373
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001374 // Just use the specified register as our input.
Chris Lattner18c2f132005-01-13 20:50:02 +00001375 return dyn_cast<RegSDNode>(Node)->getReg();
Chris Lattner7f2afac2005-01-14 22:37:41 +00001376 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001377
Chris Lattnera5ade062005-01-11 21:19:59 +00001378 unsigned &Reg = ExprMap[N];
1379 if (Reg) return Reg;
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001380
Chris Lattnerb38a7492005-04-02 04:01:14 +00001381 switch (N.getOpcode()) {
1382 default:
Chris Lattnera5ade062005-01-11 21:19:59 +00001383 Reg = Result = (N.getValueType() != MVT::Other) ?
Chris Lattnerb38a7492005-04-02 04:01:14 +00001384 MakeReg(N.getValueType()) : 1;
1385 break;
1386 case ISD::CALL:
Chris Lattnera5ade062005-01-11 21:19:59 +00001387 // If this is a call instruction, make sure to prepare ALL of the result
1388 // values as well as the chain.
Chris Lattnerb38a7492005-04-02 04:01:14 +00001389 if (Node->getNumValues() == 1)
1390 Reg = Result = 1; // Void call, just a chain.
1391 else {
Chris Lattnera5ade062005-01-11 21:19:59 +00001392 Result = MakeReg(Node->getValueType(0));
1393 ExprMap[N.getValue(0)] = Result;
Chris Lattnerb38a7492005-04-02 04:01:14 +00001394 for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
Chris Lattnera5ade062005-01-11 21:19:59 +00001395 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
Chris Lattnerb38a7492005-04-02 04:01:14 +00001396 ExprMap[SDOperand(Node, Node->getNumValues()-1)] = 1;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001397 }
Chris Lattnerb38a7492005-04-02 04:01:14 +00001398 break;
1399 case ISD::ADD_PARTS:
1400 case ISD::SUB_PARTS:
1401 case ISD::SHL_PARTS:
1402 case ISD::SRL_PARTS:
1403 case ISD::SRA_PARTS:
1404 Result = MakeReg(Node->getValueType(0));
1405 ExprMap[N.getValue(0)] = Result;
1406 for (unsigned i = 1, e = N.Val->getNumValues(); i != e; ++i)
1407 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
1408 break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001409 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001410
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001411 switch (N.getOpcode()) {
1412 default:
Chris Lattner5188ad72005-01-08 19:28:19 +00001413 Node->dump();
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001414 assert(0 && "Node not handled!\n");
1415 case ISD::FrameIndex:
1416 Tmp1 = cast<FrameIndexSDNode>(N)->getIndex();
1417 addFrameReference(BuildMI(BB, X86::LEA32r, 4, Result), (int)Tmp1);
1418 return Result;
1419 case ISD::ConstantPool:
1420 Tmp1 = cast<ConstantPoolSDNode>(N)->getIndex();
1421 addConstantPoolReference(BuildMI(BB, X86::LEA32r, 4, Result), Tmp1);
1422 return Result;
1423 case ISD::ConstantFP:
1424 ContainsFPCode = true;
1425 Tmp1 = Result; // Intermediate Register
1426 if (cast<ConstantFPSDNode>(N)->getValue() < 0.0 ||
1427 cast<ConstantFPSDNode>(N)->isExactlyValue(-0.0))
1428 Tmp1 = MakeReg(MVT::f64);
1429
1430 if (cast<ConstantFPSDNode>(N)->isExactlyValue(+0.0) ||
1431 cast<ConstantFPSDNode>(N)->isExactlyValue(-0.0))
1432 BuildMI(BB, X86::FLD0, 0, Tmp1);
1433 else if (cast<ConstantFPSDNode>(N)->isExactlyValue(+1.0) ||
1434 cast<ConstantFPSDNode>(N)->isExactlyValue(-1.0))
1435 BuildMI(BB, X86::FLD1, 0, Tmp1);
1436 else
1437 assert(0 && "Unexpected constant!");
1438 if (Tmp1 != Result)
1439 BuildMI(BB, X86::FCHS, 1, Result).addReg(Tmp1);
1440 return Result;
1441 case ISD::Constant:
1442 switch (N.getValueType()) {
1443 default: assert(0 && "Cannot use constants of this type!");
1444 case MVT::i1:
1445 case MVT::i8: Opc = X86::MOV8ri; break;
1446 case MVT::i16: Opc = X86::MOV16ri; break;
1447 case MVT::i32: Opc = X86::MOV32ri; break;
1448 }
1449 BuildMI(BB, Opc, 1,Result).addImm(cast<ConstantSDNode>(N)->getValue());
1450 return Result;
Chris Lattner7ce7eff2005-04-01 22:46:45 +00001451 case ISD::UNDEF:
1452 if (Node->getValueType(0) == MVT::f64) {
1453 // FIXME: SHOULD TEACH STACKIFIER ABOUT UNDEF VALUES!
1454 BuildMI(BB, X86::FLD0, 0, Result);
1455 } else {
1456 BuildMI(BB, X86::IMPLICIT_DEF, 0, Result);
1457 }
1458 return Result;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001459 case ISD::GlobalAddress: {
1460 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
1461 BuildMI(BB, X86::MOV32ri, 1, Result).addGlobalAddress(GV);
1462 return Result;
1463 }
1464 case ISD::ExternalSymbol: {
1465 const char *Sym = cast<ExternalSymbolSDNode>(N)->getSymbol();
1466 BuildMI(BB, X86::MOV32ri, 1, Result).addExternalSymbol(Sym);
1467 return Result;
1468 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001469 case ISD::ZERO_EXTEND: {
1470 int DestIs16 = N.getValueType() == MVT::i16;
1471 int SrcIs16 = N.getOperand(0).getValueType() == MVT::i16;
Chris Lattner590d8002005-01-09 18:52:44 +00001472
1473 // FIXME: This hack is here for zero extension casts from bool to i8. This
1474 // would not be needed if bools were promoted by Legalize.
1475 if (N.getValueType() == MVT::i8) {
Chris Lattnerdbba22f2005-01-11 23:33:00 +00001476 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner590d8002005-01-09 18:52:44 +00001477 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(Tmp1);
1478 return Result;
1479 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001480
Chris Lattner4ff348b2005-01-17 06:26:58 +00001481 if (isFoldableLoad(N.getOperand(0), SDOperand())) {
Chris Lattnerdbba22f2005-01-11 23:33:00 +00001482 static const unsigned Opc[3] = {
1483 X86::MOVZX32rm8, X86::MOVZX32rm16, X86::MOVZX16rm8
1484 };
1485
1486 X86AddressMode AM;
1487 EmitFoldedLoad(N.getOperand(0), AM);
1488 addFullAddress(BuildMI(BB, Opc[SrcIs16+DestIs16*2], 4, Result), AM);
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001489
Chris Lattnerdbba22f2005-01-11 23:33:00 +00001490 return Result;
1491 }
1492
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001493 static const unsigned Opc[3] = {
1494 X86::MOVZX32rr8, X86::MOVZX32rr16, X86::MOVZX16rr8
1495 };
Chris Lattnerdbba22f2005-01-11 23:33:00 +00001496 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001497 BuildMI(BB, Opc[SrcIs16+DestIs16*2], 1, Result).addReg(Tmp1);
1498 return Result;
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001499 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001500 case ISD::SIGN_EXTEND: {
1501 int DestIs16 = N.getValueType() == MVT::i16;
1502 int SrcIs16 = N.getOperand(0).getValueType() == MVT::i16;
1503
Chris Lattner590d8002005-01-09 18:52:44 +00001504 // FIXME: Legalize should promote bools to i8!
1505 assert(N.getOperand(0).getValueType() != MVT::i1 &&
1506 "Sign extend from bool not implemented!");
1507
Chris Lattner4ff348b2005-01-17 06:26:58 +00001508 if (isFoldableLoad(N.getOperand(0), SDOperand())) {
Chris Lattnerdbba22f2005-01-11 23:33:00 +00001509 static const unsigned Opc[3] = {
1510 X86::MOVSX32rm8, X86::MOVSX32rm16, X86::MOVSX16rm8
1511 };
1512
1513 X86AddressMode AM;
1514 EmitFoldedLoad(N.getOperand(0), AM);
1515 addFullAddress(BuildMI(BB, Opc[SrcIs16+DestIs16*2], 4, Result), AM);
1516 return Result;
1517 }
1518
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001519 static const unsigned Opc[3] = {
1520 X86::MOVSX32rr8, X86::MOVSX32rr16, X86::MOVSX16rr8
1521 };
1522 Tmp1 = SelectExpr(N.getOperand(0));
1523 BuildMI(BB, Opc[SrcIs16+DestIs16*2], 1, Result).addReg(Tmp1);
1524 return Result;
1525 }
1526 case ISD::TRUNCATE:
Chris Lattnerafce4302005-01-12 02:19:06 +00001527 // Fold TRUNCATE (LOAD P) into a smaller load from P.
Chris Lattner477c9312005-01-18 20:05:56 +00001528 // FIXME: This should be performed by the DAGCombiner.
Chris Lattner4ff348b2005-01-17 06:26:58 +00001529 if (isFoldableLoad(N.getOperand(0), SDOperand())) {
Chris Lattnerafce4302005-01-12 02:19:06 +00001530 switch (N.getValueType()) {
1531 default: assert(0 && "Unknown truncate!");
1532 case MVT::i1:
1533 case MVT::i8: Opc = X86::MOV8rm; break;
1534 case MVT::i16: Opc = X86::MOV16rm; break;
1535 }
1536 X86AddressMode AM;
1537 EmitFoldedLoad(N.getOperand(0), AM);
1538 addFullAddress(BuildMI(BB, Opc, 4, Result), AM);
1539 return Result;
1540 }
1541
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001542 // Handle cast of LARGER int to SMALLER int using a move to EAX followed by
1543 // a move out of AX or AL.
1544 switch (N.getOperand(0).getValueType()) {
1545 default: assert(0 && "Unknown truncate!");
1546 case MVT::i8: Tmp2 = X86::AL; Opc = X86::MOV8rr; break;
1547 case MVT::i16: Tmp2 = X86::AX; Opc = X86::MOV16rr; break;
1548 case MVT::i32: Tmp2 = X86::EAX; Opc = X86::MOV32rr; break;
1549 }
1550 Tmp1 = SelectExpr(N.getOperand(0));
1551 BuildMI(BB, Opc, 1, Tmp2).addReg(Tmp1);
1552
1553 switch (N.getValueType()) {
1554 default: assert(0 && "Unknown truncate!");
1555 case MVT::i1:
1556 case MVT::i8: Tmp2 = X86::AL; Opc = X86::MOV8rr; break;
1557 case MVT::i16: Tmp2 = X86::AX; Opc = X86::MOV16rr; break;
1558 }
1559 BuildMI(BB, Opc, 1, Result).addReg(Tmp2);
1560 return Result;
1561
Chris Lattner590d8002005-01-09 18:52:44 +00001562 case ISD::SINT_TO_FP:
1563 case ISD::UINT_TO_FP: {
1564 // FIXME: Most of this grunt work should be done by legalize!
Chris Lattneref7ba072005-01-11 03:50:45 +00001565 ContainsFPCode = true;
Chris Lattner590d8002005-01-09 18:52:44 +00001566
1567 // Promote the integer to a type supported by FLD. We do this because there
1568 // are no unsigned FLD instructions, so we must promote an unsigned value to
1569 // a larger signed value, then use FLD on the larger value.
1570 //
1571 MVT::ValueType PromoteType = MVT::Other;
1572 MVT::ValueType SrcTy = N.getOperand(0).getValueType();
1573 unsigned PromoteOpcode = 0;
1574 unsigned RealDestReg = Result;
1575 switch (SrcTy) {
1576 case MVT::i1:
1577 case MVT::i8:
1578 // We don't have the facilities for directly loading byte sized data from
1579 // memory (even signed). Promote it to 16 bits.
1580 PromoteType = MVT::i16;
1581 PromoteOpcode = Node->getOpcode() == ISD::SINT_TO_FP ?
1582 X86::MOVSX16rr8 : X86::MOVZX16rr8;
1583 break;
1584 case MVT::i16:
1585 if (Node->getOpcode() == ISD::UINT_TO_FP) {
1586 PromoteType = MVT::i32;
1587 PromoteOpcode = X86::MOVZX32rr16;
1588 }
1589 break;
1590 default:
1591 // Don't fild into the real destination.
1592 if (Node->getOpcode() == ISD::UINT_TO_FP)
1593 Result = MakeReg(Node->getValueType(0));
1594 break;
1595 }
1596
1597 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001598
Chris Lattner590d8002005-01-09 18:52:44 +00001599 if (PromoteType != MVT::Other) {
1600 Tmp2 = MakeReg(PromoteType);
1601 BuildMI(BB, PromoteOpcode, 1, Tmp2).addReg(Tmp1);
1602 SrcTy = PromoteType;
1603 Tmp1 = Tmp2;
1604 }
1605
1606 // Spill the integer to memory and reload it from there.
1607 unsigned Size = MVT::getSizeInBits(SrcTy)/8;
1608 MachineFunction *F = BB->getParent();
1609 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, Size);
1610
1611 switch (SrcTy) {
Chris Lattner590d8002005-01-09 18:52:44 +00001612 case MVT::i32:
1613 addFrameReference(BuildMI(BB, X86::MOV32mr, 5),
1614 FrameIdx).addReg(Tmp1);
1615 addFrameReference(BuildMI(BB, X86::FILD32m, 5, Result), FrameIdx);
1616 break;
1617 case MVT::i16:
1618 addFrameReference(BuildMI(BB, X86::MOV16mr, 5),
1619 FrameIdx).addReg(Tmp1);
1620 addFrameReference(BuildMI(BB, X86::FILD16m, 5, Result), FrameIdx);
1621 break;
1622 default: break; // No promotion required.
1623 }
1624
Chris Lattner085c9952005-01-12 04:00:00 +00001625 if (Node->getOpcode() == ISD::UINT_TO_FP && Result != RealDestReg) {
Chris Lattner590d8002005-01-09 18:52:44 +00001626 // If this is a cast from uint -> double, we need to be careful when if
1627 // the "sign" bit is set. If so, we don't want to make a negative number,
1628 // we want to make a positive number. Emit code to add an offset if the
1629 // sign bit is set.
1630
1631 // Compute whether the sign bit is set by shifting the reg right 31 bits.
1632 unsigned IsNeg = MakeReg(MVT::i32);
1633 BuildMI(BB, X86::SHR32ri, 2, IsNeg).addReg(Tmp1).addImm(31);
1634
1635 // Create a CP value that has the offset in one word and 0 in the other.
1636 static ConstantInt *TheOffset = ConstantUInt::get(Type::ULongTy,
1637 0x4f80000000000000ULL);
1638 unsigned CPI = F->getConstantPool()->getConstantPoolIndex(TheOffset);
1639 BuildMI(BB, X86::FADD32m, 5, RealDestReg).addReg(Result)
1640 .addConstantPoolIndex(CPI).addZImm(4).addReg(IsNeg).addSImm(0);
Chris Lattner590d8002005-01-09 18:52:44 +00001641 }
1642 return RealDestReg;
1643 }
1644 case ISD::FP_TO_SINT:
1645 case ISD::FP_TO_UINT: {
1646 // FIXME: Most of this grunt work should be done by legalize!
1647 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
1648
1649 // Change the floating point control register to use "round towards zero"
1650 // mode when truncating to an integer value.
1651 //
1652 MachineFunction *F = BB->getParent();
1653 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
1654 addFrameReference(BuildMI(BB, X86::FNSTCW16m, 4), CWFrameIdx);
1655
1656 // Load the old value of the high byte of the control word...
1657 unsigned HighPartOfCW = MakeReg(MVT::i8);
1658 addFrameReference(BuildMI(BB, X86::MOV8rm, 4, HighPartOfCW),
1659 CWFrameIdx, 1);
1660
1661 // Set the high part to be round to zero...
1662 addFrameReference(BuildMI(BB, X86::MOV8mi, 5),
1663 CWFrameIdx, 1).addImm(12);
1664
1665 // Reload the modified control word now...
1666 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001667
Chris Lattner590d8002005-01-09 18:52:44 +00001668 // Restore the memory image of control word to original value
1669 addFrameReference(BuildMI(BB, X86::MOV8mr, 5),
1670 CWFrameIdx, 1).addReg(HighPartOfCW);
1671
1672 // We don't have the facilities for directly storing byte sized data to
1673 // memory. Promote it to 16 bits. We also must promote unsigned values to
1674 // larger classes because we only have signed FP stores.
1675 MVT::ValueType StoreClass = Node->getValueType(0);
1676 if (StoreClass == MVT::i8 || Node->getOpcode() == ISD::FP_TO_UINT)
1677 switch (StoreClass) {
Chris Lattner2afa1912005-05-09 05:33:18 +00001678 case MVT::i1:
Chris Lattner590d8002005-01-09 18:52:44 +00001679 case MVT::i8: StoreClass = MVT::i16; break;
1680 case MVT::i16: StoreClass = MVT::i32; break;
1681 case MVT::i32: StoreClass = MVT::i64; break;
Chris Lattner590d8002005-01-09 18:52:44 +00001682 default: assert(0 && "Unknown store class!");
1683 }
1684
1685 // Spill the integer to memory and reload it from there.
1686 unsigned Size = MVT::getSizeInBits(StoreClass)/8;
1687 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, Size);
1688
1689 switch (StoreClass) {
1690 default: assert(0 && "Unknown store class!");
1691 case MVT::i16:
1692 addFrameReference(BuildMI(BB, X86::FIST16m, 5), FrameIdx).addReg(Tmp1);
1693 break;
1694 case MVT::i32:
Chris Lattner25020852005-01-09 19:49:59 +00001695 addFrameReference(BuildMI(BB, X86::FIST32m, 5), FrameIdx).addReg(Tmp1);
Chris Lattner590d8002005-01-09 18:52:44 +00001696 break;
Chris Lattnera0dbf182005-05-09 18:37:02 +00001697 case MVT::i64:
1698 addFrameReference(BuildMI(BB, X86::FISTP64m, 5), FrameIdx).addReg(Tmp1);
1699 break; }
Chris Lattner590d8002005-01-09 18:52:44 +00001700
1701 switch (Node->getValueType(0)) {
1702 default:
1703 assert(0 && "Unknown integer type!");
Chris Lattner590d8002005-01-09 18:52:44 +00001704 case MVT::i32:
1705 addFrameReference(BuildMI(BB, X86::MOV32rm, 4, Result), FrameIdx);
1706 break;
1707 case MVT::i16:
1708 addFrameReference(BuildMI(BB, X86::MOV16rm, 4, Result), FrameIdx);
1709 break;
1710 case MVT::i8:
Chris Lattner2afa1912005-05-09 05:33:18 +00001711 case MVT::i1:
Chris Lattner590d8002005-01-09 18:52:44 +00001712 addFrameReference(BuildMI(BB, X86::MOV8rm, 4, Result), FrameIdx);
1713 break;
1714 }
1715
1716 // Reload the original control word now.
1717 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1718 return Result;
1719 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001720 case ISD::ADD:
Chris Lattnera5ade062005-01-11 21:19:59 +00001721 Op0 = N.getOperand(0);
1722 Op1 = N.getOperand(1);
1723
Chris Lattner44129b52005-01-25 20:03:11 +00001724 if (isFoldableLoad(Op0, Op1, true)) {
Chris Lattnera5ade062005-01-11 21:19:59 +00001725 std::swap(Op0, Op1);
Chris Lattner4ff348b2005-01-17 06:26:58 +00001726 goto FoldAdd;
1727 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001728
Chris Lattner44129b52005-01-25 20:03:11 +00001729 if (isFoldableLoad(Op1, Op0, true)) {
Chris Lattner4ff348b2005-01-17 06:26:58 +00001730 FoldAdd:
Chris Lattnera5ade062005-01-11 21:19:59 +00001731 switch (N.getValueType()) {
1732 default: assert(0 && "Cannot add this type!");
1733 case MVT::i1:
1734 case MVT::i8: Opc = X86::ADD8rm; break;
1735 case MVT::i16: Opc = X86::ADD16rm; break;
1736 case MVT::i32: Opc = X86::ADD32rm; break;
Chris Lattner44129b52005-01-25 20:03:11 +00001737 case MVT::f64:
1738 // For F64, handle promoted load operations (from F32) as well!
1739 Opc = Op1.getOpcode() == ISD::LOAD ? X86::FADD64m : X86::FADD32m;
1740 break;
Chris Lattnera5ade062005-01-11 21:19:59 +00001741 }
1742 X86AddressMode AM;
Chris Lattner636e79a2005-01-13 05:53:16 +00001743 EmitFoldedLoad(Op1, AM);
1744 Tmp1 = SelectExpr(Op0);
Chris Lattnera5ade062005-01-11 21:19:59 +00001745 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
1746 return Result;
1747 }
1748
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001749 // See if we can codegen this as an LEA to fold operations together.
1750 if (N.getValueType() == MVT::i32) {
Chris Lattner883c86f2005-01-18 02:25:52 +00001751 ExprMap.erase(N);
Chris Lattner98a8ba02005-01-18 01:06:26 +00001752 X86ISelAddressMode AM;
Chris Lattner883c86f2005-01-18 02:25:52 +00001753 MatchAddress(N, AM);
1754 ExprMap[N] = Result;
1755
1756 // If this is not just an add, emit the LEA. For a simple add (like
1757 // reg+reg or reg+imm), we just emit an add. It might be a good idea to
1758 // leave this as LEA, then peephole it to 'ADD' after two address elim
1759 // happens.
1760 if (AM.Scale != 1 || AM.BaseType == X86ISelAddressMode::FrameIndexBase||
1761 AM.GV || (AM.Base.Reg.Val && AM.IndexReg.Val && AM.Disp)) {
1762 X86AddressMode XAM = SelectAddrExprs(AM);
1763 addFullAddress(BuildMI(BB, X86::LEA32r, 4, Result), XAM);
1764 return Result;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001765 }
1766 }
Chris Lattner11333092005-01-11 03:11:44 +00001767
Chris Lattnera5ade062005-01-11 21:19:59 +00001768 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1)) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001769 Opc = 0;
1770 if (CN->getValue() == 1) { // add X, 1 -> inc X
1771 switch (N.getValueType()) {
1772 default: assert(0 && "Cannot integer add this type!");
1773 case MVT::i8: Opc = X86::INC8r; break;
1774 case MVT::i16: Opc = X86::INC16r; break;
1775 case MVT::i32: Opc = X86::INC32r; break;
1776 }
1777 } else if (CN->isAllOnesValue()) { // add X, -1 -> dec X
1778 switch (N.getValueType()) {
1779 default: assert(0 && "Cannot integer add this type!");
1780 case MVT::i8: Opc = X86::DEC8r; break;
1781 case MVT::i16: Opc = X86::DEC16r; break;
1782 case MVT::i32: Opc = X86::DEC32r; break;
1783 }
1784 }
1785
1786 if (Opc) {
Chris Lattnera5ade062005-01-11 21:19:59 +00001787 Tmp1 = SelectExpr(Op0);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001788 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1789 return Result;
1790 }
1791
1792 switch (N.getValueType()) {
1793 default: assert(0 && "Cannot add this type!");
1794 case MVT::i8: Opc = X86::ADD8ri; break;
1795 case MVT::i16: Opc = X86::ADD16ri; break;
1796 case MVT::i32: Opc = X86::ADD32ri; break;
1797 }
1798 if (Opc) {
Chris Lattnera5ade062005-01-11 21:19:59 +00001799 Tmp1 = SelectExpr(Op0);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001800 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
1801 return Result;
1802 }
1803 }
1804
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001805 switch (N.getValueType()) {
1806 default: assert(0 && "Cannot add this type!");
1807 case MVT::i8: Opc = X86::ADD8rr; break;
1808 case MVT::i16: Opc = X86::ADD16rr; break;
1809 case MVT::i32: Opc = X86::ADD32rr; break;
Chris Lattneref7ba072005-01-11 03:50:45 +00001810 case MVT::f64: Opc = X86::FpADD; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001811 }
Chris Lattner11333092005-01-11 03:11:44 +00001812
Chris Lattnera5ade062005-01-11 21:19:59 +00001813 if (getRegPressure(Op0) > getRegPressure(Op1)) {
1814 Tmp1 = SelectExpr(Op0);
1815 Tmp2 = SelectExpr(Op1);
Chris Lattner11333092005-01-11 03:11:44 +00001816 } else {
Chris Lattnera5ade062005-01-11 21:19:59 +00001817 Tmp2 = SelectExpr(Op1);
1818 Tmp1 = SelectExpr(Op0);
Chris Lattner11333092005-01-11 03:11:44 +00001819 }
1820
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001821 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1822 return Result;
Chris Lattnerb7edaa12005-04-02 05:30:17 +00001823
1824 case ISD::FABS:
Chris Lattnerb7edaa12005-04-02 05:30:17 +00001825 case ISD::FNEG:
Chris Lattnerc5dcb532005-04-30 04:25:35 +00001826 case ISD::FSIN:
1827 case ISD::FCOS:
Chris Lattner2c56e8a2005-04-28 22:07:18 +00001828 case ISD::FSQRT:
1829 assert(N.getValueType()==MVT::f64 && "Illegal type for this operation");
Chris Lattnerb7edaa12005-04-02 05:30:17 +00001830 Tmp1 = SelectExpr(Node->getOperand(0));
Chris Lattner2c56e8a2005-04-28 22:07:18 +00001831 switch (N.getOpcode()) {
1832 default: assert(0 && "Unreachable!");
1833 case ISD::FABS: BuildMI(BB, X86::FABS, 1, Result).addReg(Tmp1); break;
1834 case ISD::FNEG: BuildMI(BB, X86::FCHS, 1, Result).addReg(Tmp1); break;
1835 case ISD::FSQRT: BuildMI(BB, X86::FSQRT, 1, Result).addReg(Tmp1); break;
Chris Lattnerc5dcb532005-04-30 04:25:35 +00001836 case ISD::FSIN: BuildMI(BB, X86::FSIN, 1, Result).addReg(Tmp1); break;
1837 case ISD::FCOS: BuildMI(BB, X86::FCOS, 1, Result).addReg(Tmp1); break;
Chris Lattner2c56e8a2005-04-28 22:07:18 +00001838 }
Chris Lattnerb7edaa12005-04-02 05:30:17 +00001839 return Result;
1840
Chris Lattner8db0af12005-04-06 04:21:07 +00001841 case ISD::MULHU:
1842 switch (N.getValueType()) {
1843 default: assert(0 && "Unsupported VT!");
1844 case MVT::i8: Tmp2 = X86::MUL8r; break;
1845 case MVT::i16: Tmp2 = X86::MUL16r; break;
1846 case MVT::i32: Tmp2 = X86::MUL32r; break;
1847 }
1848 // FALL THROUGH
1849 case ISD::MULHS: {
1850 unsigned MovOpc, LowReg, HiReg;
1851 switch (N.getValueType()) {
1852 default: assert(0 && "Unsupported VT!");
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001853 case MVT::i8:
Chris Lattner8db0af12005-04-06 04:21:07 +00001854 MovOpc = X86::MOV8rr;
1855 LowReg = X86::AL;
1856 HiReg = X86::AH;
1857 Opc = X86::IMUL8r;
1858 break;
1859 case MVT::i16:
1860 MovOpc = X86::MOV16rr;
1861 LowReg = X86::AX;
1862 HiReg = X86::DX;
1863 Opc = X86::IMUL16r;
1864 break;
1865 case MVT::i32:
1866 MovOpc = X86::MOV32rr;
1867 LowReg = X86::EAX;
1868 HiReg = X86::EDX;
1869 Opc = X86::IMUL32r;
1870 break;
1871 }
1872 if (Node->getOpcode() != ISD::MULHS)
1873 Opc = Tmp2; // Get the MULHU opcode.
1874
1875 Op0 = Node->getOperand(0);
1876 Op1 = Node->getOperand(1);
1877 if (getRegPressure(Op0) > getRegPressure(Op1)) {
1878 Tmp1 = SelectExpr(Op0);
1879 Tmp2 = SelectExpr(Op1);
1880 } else {
1881 Tmp2 = SelectExpr(Op1);
1882 Tmp1 = SelectExpr(Op0);
1883 }
1884
1885 // FIXME: Implement folding of loads into the memory operands here!
1886 BuildMI(BB, MovOpc, 1, LowReg).addReg(Tmp1);
1887 BuildMI(BB, Opc, 1).addReg(Tmp2);
1888 BuildMI(BB, MovOpc, 1, Result).addReg(HiReg);
1889 return Result;
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001890 }
Chris Lattner8db0af12005-04-06 04:21:07 +00001891
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001892 case ISD::SUB:
Chris Lattnera5ade062005-01-11 21:19:59 +00001893 case ISD::MUL:
1894 case ISD::AND:
1895 case ISD::OR:
Chris Lattnera56cea42005-01-12 04:23:22 +00001896 case ISD::XOR: {
Chris Lattnera5ade062005-01-11 21:19:59 +00001897 static const unsigned SUBTab[] = {
1898 X86::SUB8ri, X86::SUB16ri, X86::SUB32ri, 0, 0,
1899 X86::SUB8rm, X86::SUB16rm, X86::SUB32rm, X86::FSUB32m, X86::FSUB64m,
1900 X86::SUB8rr, X86::SUB16rr, X86::SUB32rr, X86::FpSUB , X86::FpSUB,
1901 };
1902 static const unsigned MULTab[] = {
1903 0, X86::IMUL16rri, X86::IMUL32rri, 0, 0,
1904 0, X86::IMUL16rm , X86::IMUL32rm, X86::FMUL32m, X86::FMUL64m,
1905 0, X86::IMUL16rr , X86::IMUL32rr, X86::FpMUL , X86::FpMUL,
1906 };
1907 static const unsigned ANDTab[] = {
1908 X86::AND8ri, X86::AND16ri, X86::AND32ri, 0, 0,
1909 X86::AND8rm, X86::AND16rm, X86::AND32rm, 0, 0,
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001910 X86::AND8rr, X86::AND16rr, X86::AND32rr, 0, 0,
Chris Lattnera5ade062005-01-11 21:19:59 +00001911 };
1912 static const unsigned ORTab[] = {
1913 X86::OR8ri, X86::OR16ri, X86::OR32ri, 0, 0,
1914 X86::OR8rm, X86::OR16rm, X86::OR32rm, 0, 0,
1915 X86::OR8rr, X86::OR16rr, X86::OR32rr, 0, 0,
1916 };
1917 static const unsigned XORTab[] = {
1918 X86::XOR8ri, X86::XOR16ri, X86::XOR32ri, 0, 0,
1919 X86::XOR8rm, X86::XOR16rm, X86::XOR32rm, 0, 0,
1920 X86::XOR8rr, X86::XOR16rr, X86::XOR32rr, 0, 0,
1921 };
1922
1923 Op0 = Node->getOperand(0);
1924 Op1 = Node->getOperand(1);
1925
Chris Lattner30ea1e92005-01-19 07:37:26 +00001926 if (Node->getOpcode() == ISD::OR && Op0.hasOneUse() && Op1.hasOneUse())
1927 if (EmitOrOpOp(Op0, Op1, Result)) // Match SHLD, SHRD, and rotates.
Chris Lattner85716372005-01-19 06:18:43 +00001928 return Result;
1929
1930 if (Node->getOpcode() == ISD::SUB)
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001931 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(0)))
1932 if (CN->isNullValue()) { // 0 - N -> neg N
1933 switch (N.getValueType()) {
1934 default: assert(0 && "Cannot sub this type!");
1935 case MVT::i1:
1936 case MVT::i8: Opc = X86::NEG8r; break;
1937 case MVT::i16: Opc = X86::NEG16r; break;
1938 case MVT::i32: Opc = X86::NEG32r; break;
1939 }
1940 Tmp1 = SelectExpr(N.getOperand(1));
1941 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1942 return Result;
1943 }
1944
Chris Lattnera5ade062005-01-11 21:19:59 +00001945 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1)) {
1946 if (CN->isAllOnesValue() && Node->getOpcode() == ISD::XOR) {
Chris Lattnerc98279d2005-01-17 00:23:16 +00001947 Opc = 0;
Chris Lattnerd4dab922005-01-11 04:31:30 +00001948 switch (N.getValueType()) {
1949 default: assert(0 && "Cannot add this type!");
Chris Lattnerc98279d2005-01-17 00:23:16 +00001950 case MVT::i1: break; // Not supported, don't invert upper bits!
Chris Lattnerd4dab922005-01-11 04:31:30 +00001951 case MVT::i8: Opc = X86::NOT8r; break;
1952 case MVT::i16: Opc = X86::NOT16r; break;
1953 case MVT::i32: Opc = X86::NOT32r; break;
1954 }
Chris Lattnerc98279d2005-01-17 00:23:16 +00001955 if (Opc) {
1956 Tmp1 = SelectExpr(Op0);
1957 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1958 return Result;
1959 }
Chris Lattnerd4dab922005-01-11 04:31:30 +00001960 }
1961
Chris Lattner2a4e5082005-01-17 06:48:02 +00001962 // Fold common multiplies into LEA instructions.
1963 if (Node->getOpcode() == ISD::MUL && N.getValueType() == MVT::i32) {
1964 switch ((int)CN->getValue()) {
1965 default: break;
1966 case 3:
1967 case 5:
1968 case 9:
Chris Lattner2a4e5082005-01-17 06:48:02 +00001969 // Remove N from exprmap so SelectAddress doesn't get confused.
1970 ExprMap.erase(N);
Chris Lattner98a8ba02005-01-18 01:06:26 +00001971 X86AddressMode AM;
Chris Lattner2a4e5082005-01-17 06:48:02 +00001972 SelectAddress(N, AM);
1973 // Restore it to the map.
1974 ExprMap[N] = Result;
1975 addFullAddress(BuildMI(BB, X86::LEA32r, 4, Result), AM);
1976 return Result;
1977 }
1978 }
1979
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001980 switch (N.getValueType()) {
Chris Lattnerd4dab922005-01-11 04:31:30 +00001981 default: assert(0 && "Cannot xor this type!");
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001982 case MVT::i1:
Chris Lattnera5ade062005-01-11 21:19:59 +00001983 case MVT::i8: Opc = 0; break;
1984 case MVT::i16: Opc = 1; break;
1985 case MVT::i32: Opc = 2; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001986 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001987 switch (Node->getOpcode()) {
1988 default: assert(0 && "Unreachable!");
1989 case ISD::SUB: Opc = SUBTab[Opc]; break;
1990 case ISD::MUL: Opc = MULTab[Opc]; break;
1991 case ISD::AND: Opc = ANDTab[Opc]; break;
1992 case ISD::OR: Opc = ORTab[Opc]; break;
1993 case ISD::XOR: Opc = XORTab[Opc]; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001994 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001995 if (Opc) { // Can't fold MUL:i8 R, imm
1996 Tmp1 = SelectExpr(Op0);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001997 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
1998 return Result;
1999 }
2000 }
Chris Lattner11333092005-01-11 03:11:44 +00002001
Chris Lattner44129b52005-01-25 20:03:11 +00002002 if (isFoldableLoad(Op0, Op1, true))
Chris Lattnera5ade062005-01-11 21:19:59 +00002003 if (Node->getOpcode() != ISD::SUB) {
2004 std::swap(Op0, Op1);
Chris Lattner4ff348b2005-01-17 06:26:58 +00002005 goto FoldOps;
Chris Lattnera5ade062005-01-11 21:19:59 +00002006 } else {
Chris Lattner44129b52005-01-25 20:03:11 +00002007 // For FP, emit 'reverse' subract, with a memory operand.
2008 if (N.getValueType() == MVT::f64) {
2009 if (Op0.getOpcode() == ISD::EXTLOAD)
2010 Opc = X86::FSUBR32m;
2011 else
2012 Opc = X86::FSUBR64m;
2013
Chris Lattnera5ade062005-01-11 21:19:59 +00002014 X86AddressMode AM;
Chris Lattner636e79a2005-01-13 05:53:16 +00002015 EmitFoldedLoad(Op0, AM);
2016 Tmp1 = SelectExpr(Op1);
Chris Lattnera5ade062005-01-11 21:19:59 +00002017 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
2018 return Result;
2019 }
2020 }
2021
Chris Lattner44129b52005-01-25 20:03:11 +00002022 if (isFoldableLoad(Op1, Op0, true)) {
Chris Lattner4ff348b2005-01-17 06:26:58 +00002023 FoldOps:
Chris Lattnera5ade062005-01-11 21:19:59 +00002024 switch (N.getValueType()) {
2025 default: assert(0 && "Cannot operate on this type!");
2026 case MVT::i1:
2027 case MVT::i8: Opc = 5; break;
2028 case MVT::i16: Opc = 6; break;
2029 case MVT::i32: Opc = 7; break;
Chris Lattner44129b52005-01-25 20:03:11 +00002030 // For F64, handle promoted load operations (from F32) as well!
2031 case MVT::f64: Opc = Op1.getOpcode() == ISD::LOAD ? 9 : 8; break;
Chris Lattnera5ade062005-01-11 21:19:59 +00002032 }
2033 switch (Node->getOpcode()) {
2034 default: assert(0 && "Unreachable!");
2035 case ISD::SUB: Opc = SUBTab[Opc]; break;
2036 case ISD::MUL: Opc = MULTab[Opc]; break;
2037 case ISD::AND: Opc = ANDTab[Opc]; break;
2038 case ISD::OR: Opc = ORTab[Opc]; break;
2039 case ISD::XOR: Opc = XORTab[Opc]; break;
2040 }
2041
2042 X86AddressMode AM;
Chris Lattner636e79a2005-01-13 05:53:16 +00002043 EmitFoldedLoad(Op1, AM);
2044 Tmp1 = SelectExpr(Op0);
Chris Lattnera5ade062005-01-11 21:19:59 +00002045 if (Opc) {
2046 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
2047 } else {
2048 assert(Node->getOpcode() == ISD::MUL &&
2049 N.getValueType() == MVT::i8 && "Unexpected situation!");
2050 // Must use the MUL instruction, which forces use of AL.
2051 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(Tmp1);
2052 addFullAddress(BuildMI(BB, X86::MUL8m, 1), AM);
2053 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
2054 }
2055 return Result;
Chris Lattner11333092005-01-11 03:11:44 +00002056 }
Chris Lattnera5ade062005-01-11 21:19:59 +00002057
2058 if (getRegPressure(Op0) > getRegPressure(Op1)) {
2059 Tmp1 = SelectExpr(Op0);
2060 Tmp2 = SelectExpr(Op1);
2061 } else {
2062 Tmp2 = SelectExpr(Op1);
2063 Tmp1 = SelectExpr(Op0);
2064 }
2065
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002066 switch (N.getValueType()) {
2067 default: assert(0 && "Cannot add this type!");
Chris Lattnera5ade062005-01-11 21:19:59 +00002068 case MVT::i1:
2069 case MVT::i8: Opc = 10; break;
2070 case MVT::i16: Opc = 11; break;
2071 case MVT::i32: Opc = 12; break;
2072 case MVT::f32: Opc = 13; break;
2073 case MVT::f64: Opc = 14; break;
2074 }
2075 switch (Node->getOpcode()) {
2076 default: assert(0 && "Unreachable!");
2077 case ISD::SUB: Opc = SUBTab[Opc]; break;
2078 case ISD::MUL: Opc = MULTab[Opc]; break;
2079 case ISD::AND: Opc = ANDTab[Opc]; break;
2080 case ISD::OR: Opc = ORTab[Opc]; break;
2081 case ISD::XOR: Opc = XORTab[Opc]; break;
2082 }
2083 if (Opc) {
2084 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
2085 } else {
2086 assert(Node->getOpcode() == ISD::MUL &&
2087 N.getValueType() == MVT::i8 && "Unexpected situation!");
Chris Lattnera13d3232005-01-10 20:55:48 +00002088 // Must use the MUL instruction, which forces use of AL.
2089 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(Tmp1);
2090 BuildMI(BB, X86::MUL8r, 1).addReg(Tmp2);
2091 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002092 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002093 return Result;
Chris Lattnera56cea42005-01-12 04:23:22 +00002094 }
Chris Lattner19ad0622005-01-20 18:53:00 +00002095 case ISD::ADD_PARTS:
2096 case ISD::SUB_PARTS: {
2097 assert(N.getNumOperands() == 4 && N.getValueType() == MVT::i32 &&
2098 "Not an i64 add/sub!");
2099 // Emit all of the operands.
2100 std::vector<unsigned> InVals;
2101 for (unsigned i = 0, e = N.getNumOperands(); i != e; ++i)
2102 InVals.push_back(SelectExpr(N.getOperand(i)));
2103 if (N.getOpcode() == ISD::ADD_PARTS) {
2104 BuildMI(BB, X86::ADD32rr, 2, Result).addReg(InVals[0]).addReg(InVals[2]);
2105 BuildMI(BB, X86::ADC32rr,2,Result+1).addReg(InVals[1]).addReg(InVals[3]);
2106 } else {
2107 BuildMI(BB, X86::SUB32rr, 2, Result).addReg(InVals[0]).addReg(InVals[2]);
2108 BuildMI(BB, X86::SBB32rr, 2,Result+1).addReg(InVals[1]).addReg(InVals[3]);
2109 }
2110 return Result+N.ResNo;
2111 }
2112
Chris Lattnerb38a7492005-04-02 04:01:14 +00002113 case ISD::SHL_PARTS:
2114 case ISD::SRA_PARTS:
2115 case ISD::SRL_PARTS: {
2116 assert(N.getNumOperands() == 3 && N.getValueType() == MVT::i32 &&
2117 "Not an i64 shift!");
2118 unsigned ShiftOpLo = SelectExpr(N.getOperand(0));
2119 unsigned ShiftOpHi = SelectExpr(N.getOperand(1));
2120 unsigned TmpReg = MakeReg(MVT::i32);
2121 if (N.getOpcode() == ISD::SRA_PARTS) {
2122 // If this is a SHR of a Long, then we need to do funny sign extension
2123 // stuff. TmpReg gets the value to use as the high-part if we are
2124 // shifting more than 32 bits.
2125 BuildMI(BB, X86::SAR32ri, 2, TmpReg).addReg(ShiftOpHi).addImm(31);
2126 } else {
2127 // Other shifts use a fixed zero value if the shift is more than 32 bits.
2128 BuildMI(BB, X86::MOV32ri, 1, TmpReg).addImm(0);
2129 }
2130
2131 // Initialize CL with the shift amount.
2132 unsigned ShiftAmountReg = SelectExpr(N.getOperand(2));
2133 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShiftAmountReg);
2134
2135 unsigned TmpReg2 = MakeReg(MVT::i32);
2136 unsigned TmpReg3 = MakeReg(MVT::i32);
2137 if (N.getOpcode() == ISD::SHL_PARTS) {
2138 // TmpReg2 = shld inHi, inLo
2139 BuildMI(BB, X86::SHLD32rrCL, 2,TmpReg2).addReg(ShiftOpHi)
2140 .addReg(ShiftOpLo);
2141 // TmpReg3 = shl inLo, CL
2142 BuildMI(BB, X86::SHL32rCL, 1, TmpReg3).addReg(ShiftOpLo);
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002143
Chris Lattnerb38a7492005-04-02 04:01:14 +00002144 // Set the flags to indicate whether the shift was by more than 32 bits.
2145 BuildMI(BB, X86::TEST8ri, 2).addReg(X86::CL).addImm(32);
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002146
Chris Lattnerb38a7492005-04-02 04:01:14 +00002147 // DestHi = (>32) ? TmpReg3 : TmpReg2;
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002148 BuildMI(BB, X86::CMOVNE32rr, 2,
Chris Lattnerb38a7492005-04-02 04:01:14 +00002149 Result+1).addReg(TmpReg2).addReg(TmpReg3);
2150 // DestLo = (>32) ? TmpReg : TmpReg3;
2151 BuildMI(BB, X86::CMOVNE32rr, 2,
2152 Result).addReg(TmpReg3).addReg(TmpReg);
2153 } else {
2154 // TmpReg2 = shrd inLo, inHi
2155 BuildMI(BB, X86::SHRD32rrCL,2,TmpReg2).addReg(ShiftOpLo)
2156 .addReg(ShiftOpHi);
2157 // TmpReg3 = s[ah]r inHi, CL
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002158 BuildMI(BB, N.getOpcode() == ISD::SRA_PARTS ? X86::SAR32rCL
Chris Lattnerb38a7492005-04-02 04:01:14 +00002159 : X86::SHR32rCL, 1, TmpReg3)
2160 .addReg(ShiftOpHi);
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002161
Chris Lattnerb38a7492005-04-02 04:01:14 +00002162 // Set the flags to indicate whether the shift was by more than 32 bits.
2163 BuildMI(BB, X86::TEST8ri, 2).addReg(X86::CL).addImm(32);
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002164
Chris Lattnerb38a7492005-04-02 04:01:14 +00002165 // DestLo = (>32) ? TmpReg3 : TmpReg2;
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002166 BuildMI(BB, X86::CMOVNE32rr, 2,
Chris Lattnerb38a7492005-04-02 04:01:14 +00002167 Result).addReg(TmpReg2).addReg(TmpReg3);
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002168
Chris Lattnerb38a7492005-04-02 04:01:14 +00002169 // DestHi = (>32) ? TmpReg : TmpReg3;
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002170 BuildMI(BB, X86::CMOVNE32rr, 2,
Chris Lattnerb38a7492005-04-02 04:01:14 +00002171 Result+1).addReg(TmpReg3).addReg(TmpReg);
2172 }
2173 return Result+N.ResNo;
2174 }
2175
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002176 case ISD::SELECT:
Chris Lattnerda2ce112005-01-16 07:34:08 +00002177 if (getRegPressure(N.getOperand(1)) > getRegPressure(N.getOperand(2))) {
2178 Tmp2 = SelectExpr(N.getOperand(1));
2179 Tmp3 = SelectExpr(N.getOperand(2));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002180 } else {
Chris Lattnerda2ce112005-01-16 07:34:08 +00002181 Tmp3 = SelectExpr(N.getOperand(2));
2182 Tmp2 = SelectExpr(N.getOperand(1));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002183 }
Chris Lattnerda2ce112005-01-16 07:34:08 +00002184 EmitSelectCC(N.getOperand(0), N.getValueType(), Tmp2, Tmp3, Result);
2185 return Result;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002186
2187 case ISD::SDIV:
2188 case ISD::UDIV:
2189 case ISD::SREM:
2190 case ISD::UREM: {
Chris Lattnerda2ce112005-01-16 07:34:08 +00002191 assert((N.getOpcode() != ISD::SREM || MVT::isInteger(N.getValueType())) &&
2192 "We don't support this operator!");
2193
Chris Lattner5bf26862005-04-13 03:29:53 +00002194 if (N.getOpcode() == ISD::SDIV) {
Chris Lattner3576c842005-01-25 20:35:10 +00002195 // We can fold loads into FpDIVs, but not really into any others.
2196 if (N.getValueType() == MVT::f64) {
2197 // Check for reversed and unreversed DIV.
2198 if (isFoldableLoad(N.getOperand(0), N.getOperand(1), true)) {
2199 if (N.getOperand(0).getOpcode() == ISD::EXTLOAD)
2200 Opc = X86::FDIVR32m;
2201 else
2202 Opc = X86::FDIVR64m;
2203 X86AddressMode AM;
2204 EmitFoldedLoad(N.getOperand(0), AM);
2205 Tmp1 = SelectExpr(N.getOperand(1));
2206 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
2207 return Result;
2208 } else if (isFoldableLoad(N.getOperand(1), N.getOperand(0), true) &&
2209 N.getOperand(1).getOpcode() == ISD::LOAD) {
2210 if (N.getOperand(1).getOpcode() == ISD::EXTLOAD)
2211 Opc = X86::FDIV32m;
2212 else
2213 Opc = X86::FDIV64m;
2214 X86AddressMode AM;
2215 EmitFoldedLoad(N.getOperand(1), AM);
2216 Tmp1 = SelectExpr(N.getOperand(0));
2217 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
2218 return Result;
2219 }
2220 }
2221
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002222 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
2223 // FIXME: These special cases should be handled by the lowering impl!
2224 unsigned RHS = CN->getValue();
2225 bool isNeg = false;
2226 if ((int)RHS < 0) {
2227 isNeg = true;
2228 RHS = -RHS;
2229 }
2230 if (RHS && (RHS & (RHS-1)) == 0) { // Signed division by power of 2?
2231 unsigned Log = log2(RHS);
2232 unsigned TmpReg = MakeReg(N.getValueType());
2233 unsigned SAROpc, SHROpc, ADDOpc, NEGOpc;
2234 switch (N.getValueType()) {
2235 default: assert("Unknown type to signed divide!");
2236 case MVT::i8:
2237 SAROpc = X86::SAR8ri;
2238 SHROpc = X86::SHR8ri;
2239 ADDOpc = X86::ADD8rr;
2240 NEGOpc = X86::NEG8r;
2241 break;
2242 case MVT::i16:
2243 SAROpc = X86::SAR16ri;
2244 SHROpc = X86::SHR16ri;
2245 ADDOpc = X86::ADD16rr;
2246 NEGOpc = X86::NEG16r;
2247 break;
2248 case MVT::i32:
2249 SAROpc = X86::SAR32ri;
2250 SHROpc = X86::SHR32ri;
2251 ADDOpc = X86::ADD32rr;
2252 NEGOpc = X86::NEG32r;
2253 break;
2254 }
Chris Lattner11333092005-01-11 03:11:44 +00002255 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002256 BuildMI(BB, SAROpc, 2, TmpReg).addReg(Tmp1).addImm(Log-1);
2257 unsigned TmpReg2 = MakeReg(N.getValueType());
2258 BuildMI(BB, SHROpc, 2, TmpReg2).addReg(TmpReg).addImm(32-Log);
2259 unsigned TmpReg3 = MakeReg(N.getValueType());
2260 BuildMI(BB, ADDOpc, 2, TmpReg3).addReg(Tmp1).addReg(TmpReg2);
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002261
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002262 unsigned TmpReg4 = isNeg ? MakeReg(N.getValueType()) : Result;
2263 BuildMI(BB, SAROpc, 2, TmpReg4).addReg(TmpReg3).addImm(Log);
2264 if (isNeg)
2265 BuildMI(BB, NEGOpc, 1, Result).addReg(TmpReg4);
2266 return Result;
2267 }
2268 }
Chris Lattner5bf26862005-04-13 03:29:53 +00002269 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002270
Chris Lattner11333092005-01-11 03:11:44 +00002271 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2272 Tmp1 = SelectExpr(N.getOperand(0));
2273 Tmp2 = SelectExpr(N.getOperand(1));
2274 } else {
2275 Tmp2 = SelectExpr(N.getOperand(1));
2276 Tmp1 = SelectExpr(N.getOperand(0));
2277 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002278
2279 bool isSigned = N.getOpcode() == ISD::SDIV || N.getOpcode() == ISD::SREM;
2280 bool isDiv = N.getOpcode() == ISD::SDIV || N.getOpcode() == ISD::UDIV;
2281 unsigned LoReg, HiReg, DivOpcode, MovOpcode, ClrOpcode, SExtOpcode;
2282 switch (N.getValueType()) {
2283 default: assert(0 && "Cannot sdiv this type!");
2284 case MVT::i8:
2285 DivOpcode = isSigned ? X86::IDIV8r : X86::DIV8r;
2286 LoReg = X86::AL;
2287 HiReg = X86::AH;
2288 MovOpcode = X86::MOV8rr;
2289 ClrOpcode = X86::MOV8ri;
2290 SExtOpcode = X86::CBW;
2291 break;
2292 case MVT::i16:
2293 DivOpcode = isSigned ? X86::IDIV16r : X86::DIV16r;
2294 LoReg = X86::AX;
2295 HiReg = X86::DX;
2296 MovOpcode = X86::MOV16rr;
2297 ClrOpcode = X86::MOV16ri;
2298 SExtOpcode = X86::CWD;
2299 break;
2300 case MVT::i32:
2301 DivOpcode = isSigned ? X86::IDIV32r : X86::DIV32r;
Chris Lattner42928302005-01-12 03:16:09 +00002302 LoReg = X86::EAX;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002303 HiReg = X86::EDX;
2304 MovOpcode = X86::MOV32rr;
2305 ClrOpcode = X86::MOV32ri;
2306 SExtOpcode = X86::CDQ;
2307 break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002308 case MVT::f64:
Chris Lattnerda2ce112005-01-16 07:34:08 +00002309 BuildMI(BB, X86::FpDIV, 2, Result).addReg(Tmp1).addReg(Tmp2);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002310 return Result;
2311 }
2312
2313 // Set up the low part.
2314 BuildMI(BB, MovOpcode, 1, LoReg).addReg(Tmp1);
2315
2316 if (isSigned) {
2317 // Sign extend the low part into the high part.
2318 BuildMI(BB, SExtOpcode, 0);
2319 } else {
2320 // Zero out the high part, effectively zero extending the input.
2321 BuildMI(BB, ClrOpcode, 1, HiReg).addImm(0);
2322 }
2323
2324 // Emit the DIV/IDIV instruction.
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002325 BuildMI(BB, DivOpcode, 1).addReg(Tmp2);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002326
2327 // Get the result of the divide or rem.
2328 BuildMI(BB, MovOpcode, 1, Result).addReg(isDiv ? LoReg : HiReg);
2329 return Result;
2330 }
2331
2332 case ISD::SHL:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002333 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Chris Lattnera5ade062005-01-11 21:19:59 +00002334 if (CN->getValue() == 1) { // X = SHL Y, 1 -> X = ADD Y, Y
2335 switch (N.getValueType()) {
2336 default: assert(0 && "Cannot shift this type!");
2337 case MVT::i8: Opc = X86::ADD8rr; break;
2338 case MVT::i16: Opc = X86::ADD16rr; break;
2339 case MVT::i32: Opc = X86::ADD32rr; break;
2340 }
2341 Tmp1 = SelectExpr(N.getOperand(0));
2342 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp1);
2343 return Result;
2344 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002345
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002346 switch (N.getValueType()) {
2347 default: assert(0 && "Cannot shift this type!");
2348 case MVT::i8: Opc = X86::SHL8ri; break;
2349 case MVT::i16: Opc = X86::SHL16ri; break;
2350 case MVT::i32: Opc = X86::SHL32ri; break;
2351 }
Chris Lattner11333092005-01-11 03:11:44 +00002352 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002353 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
2354 return Result;
2355 }
Chris Lattner11333092005-01-11 03:11:44 +00002356
2357 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2358 Tmp1 = SelectExpr(N.getOperand(0));
2359 Tmp2 = SelectExpr(N.getOperand(1));
2360 } else {
2361 Tmp2 = SelectExpr(N.getOperand(1));
2362 Tmp1 = SelectExpr(N.getOperand(0));
2363 }
2364
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002365 switch (N.getValueType()) {
2366 default: assert(0 && "Cannot shift this type!");
2367 case MVT::i8 : Opc = X86::SHL8rCL; break;
2368 case MVT::i16: Opc = X86::SHL16rCL; break;
2369 case MVT::i32: Opc = X86::SHL32rCL; break;
2370 }
2371 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
2372 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
2373 return Result;
2374 case ISD::SRL:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002375 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
2376 switch (N.getValueType()) {
2377 default: assert(0 && "Cannot shift this type!");
2378 case MVT::i8: Opc = X86::SHR8ri; break;
2379 case MVT::i16: Opc = X86::SHR16ri; break;
2380 case MVT::i32: Opc = X86::SHR32ri; break;
2381 }
Chris Lattner11333092005-01-11 03:11:44 +00002382 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002383 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
2384 return Result;
2385 }
Chris Lattner11333092005-01-11 03:11:44 +00002386
2387 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2388 Tmp1 = SelectExpr(N.getOperand(0));
2389 Tmp2 = SelectExpr(N.getOperand(1));
2390 } else {
2391 Tmp2 = SelectExpr(N.getOperand(1));
2392 Tmp1 = SelectExpr(N.getOperand(0));
2393 }
2394
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002395 switch (N.getValueType()) {
2396 default: assert(0 && "Cannot shift this type!");
2397 case MVT::i8 : Opc = X86::SHR8rCL; break;
2398 case MVT::i16: Opc = X86::SHR16rCL; break;
2399 case MVT::i32: Opc = X86::SHR32rCL; break;
2400 }
2401 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
2402 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
2403 return Result;
2404 case ISD::SRA:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002405 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
2406 switch (N.getValueType()) {
2407 default: assert(0 && "Cannot shift this type!");
2408 case MVT::i8: Opc = X86::SAR8ri; break;
2409 case MVT::i16: Opc = X86::SAR16ri; break;
2410 case MVT::i32: Opc = X86::SAR32ri; break;
2411 }
Chris Lattner11333092005-01-11 03:11:44 +00002412 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002413 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
2414 return Result;
2415 }
Chris Lattner11333092005-01-11 03:11:44 +00002416
2417 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2418 Tmp1 = SelectExpr(N.getOperand(0));
2419 Tmp2 = SelectExpr(N.getOperand(1));
2420 } else {
2421 Tmp2 = SelectExpr(N.getOperand(1));
2422 Tmp1 = SelectExpr(N.getOperand(0));
2423 }
2424
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002425 switch (N.getValueType()) {
2426 default: assert(0 && "Cannot shift this type!");
2427 case MVT::i8 : Opc = X86::SAR8rCL; break;
2428 case MVT::i16: Opc = X86::SAR16rCL; break;
2429 case MVT::i32: Opc = X86::SAR32rCL; break;
2430 }
2431 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
2432 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
2433 return Result;
2434
2435 case ISD::SETCC:
Chris Lattnercb1aa8d2005-01-17 01:34:14 +00002436 EmitCMP(N.getOperand(0), N.getOperand(1), Node->hasOneUse());
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002437 EmitSetCC(BB, Result, cast<SetCCSDNode>(N)->getCondition(),
2438 MVT::isFloatingPoint(N.getOperand(1).getValueType()));
2439 return Result;
Chris Lattnere9ef81d2005-01-15 05:22:24 +00002440 case ISD::LOAD:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002441 // Make sure we generate both values.
Chris Lattner4a108662005-01-18 03:51:59 +00002442 if (Result != 1) { // Generate the token
2443 if (!ExprMap.insert(std::make_pair(N.getValue(1), 1)).second)
2444 assert(0 && "Load already emitted!?");
2445 } else
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002446 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
2447
Chris Lattner5188ad72005-01-08 19:28:19 +00002448 switch (Node->getValueType(0)) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002449 default: assert(0 && "Cannot load this type!");
2450 case MVT::i1:
2451 case MVT::i8: Opc = X86::MOV8rm; break;
2452 case MVT::i16: Opc = X86::MOV16rm; break;
2453 case MVT::i32: Opc = X86::MOV32rm; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002454 case MVT::f64: Opc = X86::FLD64m; ContainsFPCode = true; break;
2455 }
Chris Lattner11333092005-01-11 03:11:44 +00002456
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002457 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N.getOperand(1))){
Chris Lattner11333092005-01-11 03:11:44 +00002458 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002459 addConstantPoolReference(BuildMI(BB, Opc, 4, Result), CP->getIndex());
2460 } else {
2461 X86AddressMode AM;
Chris Lattner636e79a2005-01-13 05:53:16 +00002462
2463 SDOperand Chain = N.getOperand(0);
2464 SDOperand Address = N.getOperand(1);
2465 if (getRegPressure(Chain) > getRegPressure(Address)) {
2466 Select(Chain);
2467 SelectAddress(Address, AM);
2468 } else {
2469 SelectAddress(Address, AM);
2470 Select(Chain);
2471 }
2472
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002473 addFullAddress(BuildMI(BB, Opc, 4, Result), AM);
2474 }
2475 return Result;
Chris Lattnere9ef81d2005-01-15 05:22:24 +00002476
2477 case ISD::EXTLOAD: // Arbitrarily codegen extloads as MOVZX*
2478 case ISD::ZEXTLOAD: {
2479 // Make sure we generate both values.
2480 if (Result != 1)
2481 ExprMap[N.getValue(1)] = 1; // Generate the token
2482 else
2483 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
2484
Chris Lattnerda2ce112005-01-16 07:34:08 +00002485 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N.getOperand(1)))
2486 if (Node->getValueType(0) == MVT::f64) {
2487 assert(cast<MVTSDNode>(Node)->getExtraValueType() == MVT::f32 &&
2488 "Bad EXTLOAD!");
2489 addConstantPoolReference(BuildMI(BB, X86::FLD32m, 4, Result),
2490 CP->getIndex());
2491 return Result;
2492 }
2493
Chris Lattnere9ef81d2005-01-15 05:22:24 +00002494 X86AddressMode AM;
2495 if (getRegPressure(Node->getOperand(0)) >
2496 getRegPressure(Node->getOperand(1))) {
2497 Select(Node->getOperand(0)); // chain
2498 SelectAddress(Node->getOperand(1), AM);
2499 } else {
2500 SelectAddress(Node->getOperand(1), AM);
2501 Select(Node->getOperand(0)); // chain
2502 }
2503
2504 switch (Node->getValueType(0)) {
2505 default: assert(0 && "Unknown type to sign extend to.");
2506 case MVT::f64:
2507 assert(cast<MVTSDNode>(Node)->getExtraValueType() == MVT::f32 &&
2508 "Bad EXTLOAD!");
2509 addFullAddress(BuildMI(BB, X86::FLD32m, 5, Result), AM);
2510 break;
2511 case MVT::i32:
2512 switch (cast<MVTSDNode>(Node)->getExtraValueType()) {
2513 default:
2514 assert(0 && "Bad zero extend!");
2515 case MVT::i1:
2516 case MVT::i8:
2517 addFullAddress(BuildMI(BB, X86::MOVZX32rm8, 5, Result), AM);
2518 break;
2519 case MVT::i16:
2520 addFullAddress(BuildMI(BB, X86::MOVZX32rm16, 5, Result), AM);
2521 break;
2522 }
2523 break;
2524 case MVT::i16:
2525 assert(cast<MVTSDNode>(Node)->getExtraValueType() <= MVT::i8 &&
2526 "Bad zero extend!");
2527 addFullAddress(BuildMI(BB, X86::MOVSX16rm8, 5, Result), AM);
2528 break;
2529 case MVT::i8:
2530 assert(cast<MVTSDNode>(Node)->getExtraValueType() == MVT::i1 &&
2531 "Bad zero extend!");
2532 addFullAddress(BuildMI(BB, X86::MOV8rm, 5, Result), AM);
2533 break;
2534 }
2535 return Result;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002536 }
Chris Lattnere9ef81d2005-01-15 05:22:24 +00002537 case ISD::SEXTLOAD: {
2538 // Make sure we generate both values.
2539 if (Result != 1)
2540 ExprMap[N.getValue(1)] = 1; // Generate the token
2541 else
2542 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
2543
2544 X86AddressMode AM;
2545 if (getRegPressure(Node->getOperand(0)) >
2546 getRegPressure(Node->getOperand(1))) {
2547 Select(Node->getOperand(0)); // chain
2548 SelectAddress(Node->getOperand(1), AM);
2549 } else {
2550 SelectAddress(Node->getOperand(1), AM);
2551 Select(Node->getOperand(0)); // chain
2552 }
2553
2554 switch (Node->getValueType(0)) {
2555 case MVT::i8: assert(0 && "Cannot sign extend from bool!");
2556 default: assert(0 && "Unknown type to sign extend to.");
2557 case MVT::i32:
2558 switch (cast<MVTSDNode>(Node)->getExtraValueType()) {
2559 default:
2560 case MVT::i1: assert(0 && "Cannot sign extend from bool!");
2561 case MVT::i8:
2562 addFullAddress(BuildMI(BB, X86::MOVSX32rm8, 5, Result), AM);
2563 break;
2564 case MVT::i16:
2565 addFullAddress(BuildMI(BB, X86::MOVSX32rm16, 5, Result), AM);
2566 break;
2567 }
2568 break;
2569 case MVT::i16:
2570 assert(cast<MVTSDNode>(Node)->getExtraValueType() == MVT::i8 &&
2571 "Cannot sign extend from bool!");
2572 addFullAddress(BuildMI(BB, X86::MOVSX16rm8, 5, Result), AM);
2573 break;
2574 }
2575 return Result;
2576 }
2577
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002578 case ISD::DYNAMIC_STACKALLOC:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002579 // Generate both result values.
2580 if (Result != 1)
2581 ExprMap[N.getValue(1)] = 1; // Generate the token
2582 else
2583 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
2584
2585 // FIXME: We are currently ignoring the requested alignment for handling
2586 // greater than the stack alignment. This will need to be revisited at some
2587 // point. Align = N.getOperand(2);
2588
2589 if (!isa<ConstantSDNode>(N.getOperand(2)) ||
2590 cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) {
2591 std::cerr << "Cannot allocate stack object with greater alignment than"
2592 << " the stack alignment yet!";
2593 abort();
2594 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002595
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002596 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Chris Lattner11333092005-01-11 03:11:44 +00002597 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002598 BuildMI(BB, X86::SUB32ri, 2, X86::ESP).addReg(X86::ESP)
2599 .addImm(CN->getValue());
2600 } else {
Chris Lattner11333092005-01-11 03:11:44 +00002601 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2602 Select(N.getOperand(0));
2603 Tmp1 = SelectExpr(N.getOperand(1));
2604 } else {
2605 Tmp1 = SelectExpr(N.getOperand(1));
2606 Select(N.getOperand(0));
2607 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002608
2609 // Subtract size from stack pointer, thereby allocating some space.
2610 BuildMI(BB, X86::SUB32rr, 2, X86::ESP).addReg(X86::ESP).addReg(Tmp1);
2611 }
2612
2613 // Put a pointer to the space into the result register, by copying the stack
2614 // pointer.
2615 BuildMI(BB, X86::MOV32rr, 1, Result).addReg(X86::ESP);
2616 return Result;
2617
2618 case ISD::CALL:
Chris Lattner5188ad72005-01-08 19:28:19 +00002619 // The chain for this call is now lowered.
Chris Lattner4a108662005-01-18 03:51:59 +00002620 ExprMap.insert(std::make_pair(N.getValue(Node->getNumValues()-1), 1));
Chris Lattner5188ad72005-01-08 19:28:19 +00002621
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002622 if (GlobalAddressSDNode *GASD =
2623 dyn_cast<GlobalAddressSDNode>(N.getOperand(1))) {
Chris Lattner11333092005-01-11 03:11:44 +00002624 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002625 BuildMI(BB, X86::CALLpcrel32, 1).addGlobalAddress(GASD->getGlobal(),true);
2626 } else if (ExternalSymbolSDNode *ESSDN =
2627 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1))) {
Chris Lattner11333092005-01-11 03:11:44 +00002628 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002629 BuildMI(BB, X86::CALLpcrel32,
2630 1).addExternalSymbol(ESSDN->getSymbol(), true);
2631 } else {
Chris Lattner11333092005-01-11 03:11:44 +00002632 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2633 Select(N.getOperand(0));
2634 Tmp1 = SelectExpr(N.getOperand(1));
2635 } else {
2636 Tmp1 = SelectExpr(N.getOperand(1));
2637 Select(N.getOperand(0));
2638 }
2639
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002640 BuildMI(BB, X86::CALL32r, 1).addReg(Tmp1);
2641 }
Chris Lattner5188ad72005-01-08 19:28:19 +00002642 switch (Node->getValueType(0)) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002643 default: assert(0 && "Unknown value type for call result!");
2644 case MVT::Other: return 1;
2645 case MVT::i1:
2646 case MVT::i8:
2647 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
2648 break;
2649 case MVT::i16:
2650 BuildMI(BB, X86::MOV16rr, 1, Result).addReg(X86::AX);
2651 break;
2652 case MVT::i32:
2653 BuildMI(BB, X86::MOV32rr, 1, Result).addReg(X86::EAX);
Chris Lattner5188ad72005-01-08 19:28:19 +00002654 if (Node->getValueType(1) == MVT::i32)
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002655 BuildMI(BB, X86::MOV32rr, 1, Result+1).addReg(X86::EDX);
2656 break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002657 case MVT::f64: // Floating-point return values live in %ST(0)
2658 ContainsFPCode = true;
2659 BuildMI(BB, X86::FpGETRESULT, 1, Result);
2660 break;
2661 }
2662 return Result+N.ResNo;
2663 }
2664
2665 return 0;
2666}
2667
Chris Lattnere10269b2005-01-17 19:25:26 +00002668/// TryToFoldLoadOpStore - Given a store node, try to fold together a
2669/// load/op/store instruction. If successful return true.
2670bool ISel::TryToFoldLoadOpStore(SDNode *Node) {
2671 assert(Node->getOpcode() == ISD::STORE && "Can only do this for stores!");
2672 SDOperand Chain = Node->getOperand(0);
2673 SDOperand StVal = Node->getOperand(1);
Chris Lattner5c659812005-01-17 22:10:42 +00002674 SDOperand StPtr = Node->getOperand(2);
Chris Lattnere10269b2005-01-17 19:25:26 +00002675
2676 // The chain has to be a load, the stored value must be an integer binary
2677 // operation with one use.
Chris Lattner5c659812005-01-17 22:10:42 +00002678 if (!StVal.Val->hasOneUse() || StVal.Val->getNumOperands() != 2 ||
Chris Lattnere10269b2005-01-17 19:25:26 +00002679 MVT::isFloatingPoint(StVal.getValueType()))
2680 return false;
2681
Chris Lattner5c659812005-01-17 22:10:42 +00002682 // Token chain must either be a factor node or the load to fold.
2683 if (Chain.getOpcode() != ISD::LOAD && Chain.getOpcode() != ISD::TokenFactor)
2684 return false;
Chris Lattnere10269b2005-01-17 19:25:26 +00002685
Chris Lattner5c659812005-01-17 22:10:42 +00002686 SDOperand TheLoad;
2687
2688 // Check to see if there is a load from the same pointer that we're storing
2689 // to in either operand of the binop.
2690 if (StVal.getOperand(0).getOpcode() == ISD::LOAD &&
2691 StVal.getOperand(0).getOperand(1) == StPtr)
2692 TheLoad = StVal.getOperand(0);
2693 else if (StVal.getOperand(1).getOpcode() == ISD::LOAD &&
2694 StVal.getOperand(1).getOperand(1) == StPtr)
2695 TheLoad = StVal.getOperand(1);
2696 else
2697 return false; // No matching load operand.
2698
2699 // We can only fold the load if there are no intervening side-effecting
2700 // operations. This means that the store uses the load as its token chain, or
2701 // there are only token factor nodes in between the store and load.
2702 if (Chain != TheLoad.getValue(1)) {
2703 // Okay, the other option is that we have a store referring to (possibly
2704 // nested) token factor nodes. For now, just try peeking through one level
2705 // of token factors to see if this is the case.
2706 bool ChainOk = false;
2707 if (Chain.getOpcode() == ISD::TokenFactor) {
2708 for (unsigned i = 0, e = Chain.getNumOperands(); i != e; ++i)
2709 if (Chain.getOperand(i) == TheLoad.getValue(1)) {
2710 ChainOk = true;
2711 break;
2712 }
2713 }
2714
2715 if (!ChainOk) return false;
2716 }
2717
2718 if (TheLoad.getOperand(1) != StPtr)
Chris Lattnere10269b2005-01-17 19:25:26 +00002719 return false;
2720
2721 // Make sure that one of the operands of the binop is the load, and that the
2722 // load folds into the binop.
2723 if (((StVal.getOperand(0) != TheLoad ||
2724 !isFoldableLoad(TheLoad, StVal.getOperand(1))) &&
2725 (StVal.getOperand(1) != TheLoad ||
2726 !isFoldableLoad(TheLoad, StVal.getOperand(0)))))
2727 return false;
2728
2729 // Finally, check to see if this is one of the ops we can handle!
2730 static const unsigned ADDTAB[] = {
2731 X86::ADD8mi, X86::ADD16mi, X86::ADD32mi,
2732 X86::ADD8mr, X86::ADD16mr, X86::ADD32mr,
2733 };
2734 static const unsigned SUBTAB[] = {
2735 X86::SUB8mi, X86::SUB16mi, X86::SUB32mi,
2736 X86::SUB8mr, X86::SUB16mr, X86::SUB32mr,
2737 };
2738 static const unsigned ANDTAB[] = {
2739 X86::AND8mi, X86::AND16mi, X86::AND32mi,
2740 X86::AND8mr, X86::AND16mr, X86::AND32mr,
2741 };
2742 static const unsigned ORTAB[] = {
2743 X86::OR8mi, X86::OR16mi, X86::OR32mi,
2744 X86::OR8mr, X86::OR16mr, X86::OR32mr,
2745 };
2746 static const unsigned XORTAB[] = {
2747 X86::XOR8mi, X86::XOR16mi, X86::XOR32mi,
2748 X86::XOR8mr, X86::XOR16mr, X86::XOR32mr,
2749 };
2750 static const unsigned SHLTAB[] = {
2751 X86::SHL8mi, X86::SHL16mi, X86::SHL32mi,
2752 /*Have to put the reg in CL*/0, 0, 0,
2753 };
2754 static const unsigned SARTAB[] = {
2755 X86::SAR8mi, X86::SAR16mi, X86::SAR32mi,
2756 /*Have to put the reg in CL*/0, 0, 0,
2757 };
2758 static const unsigned SHRTAB[] = {
2759 X86::SHR8mi, X86::SHR16mi, X86::SHR32mi,
2760 /*Have to put the reg in CL*/0, 0, 0,
2761 };
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002762
Chris Lattnere10269b2005-01-17 19:25:26 +00002763 const unsigned *TabPtr = 0;
2764 switch (StVal.getOpcode()) {
2765 default:
2766 std::cerr << "CANNOT [mem] op= val: ";
2767 StVal.Val->dump(); std::cerr << "\n";
2768 case ISD::MUL:
2769 case ISD::SDIV:
2770 case ISD::UDIV:
2771 case ISD::SREM:
2772 case ISD::UREM: return false;
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002773
Chris Lattnere10269b2005-01-17 19:25:26 +00002774 case ISD::ADD: TabPtr = ADDTAB; break;
2775 case ISD::SUB: TabPtr = SUBTAB; break;
2776 case ISD::AND: TabPtr = ANDTAB; break;
2777 case ISD:: OR: TabPtr = ORTAB; break;
2778 case ISD::XOR: TabPtr = XORTAB; break;
2779 case ISD::SHL: TabPtr = SHLTAB; break;
2780 case ISD::SRA: TabPtr = SARTAB; break;
2781 case ISD::SRL: TabPtr = SHRTAB; break;
2782 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002783
Chris Lattnere10269b2005-01-17 19:25:26 +00002784 // Handle: [mem] op= CST
2785 SDOperand Op0 = StVal.getOperand(0);
2786 SDOperand Op1 = StVal.getOperand(1);
Chris Lattner0a078832005-01-23 23:20:06 +00002787 unsigned Opc = 0;
Chris Lattnere10269b2005-01-17 19:25:26 +00002788 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1)) {
2789 switch (Op0.getValueType()) { // Use Op0's type because of shifts.
2790 default: break;
2791 case MVT::i1:
2792 case MVT::i8: Opc = TabPtr[0]; break;
2793 case MVT::i16: Opc = TabPtr[1]; break;
2794 case MVT::i32: Opc = TabPtr[2]; break;
2795 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002796
Chris Lattnere10269b2005-01-17 19:25:26 +00002797 if (Opc) {
Chris Lattner4a108662005-01-18 03:51:59 +00002798 if (!ExprMap.insert(std::make_pair(TheLoad.getValue(1), 1)).second)
2799 assert(0 && "Already emitted?");
Chris Lattner5c659812005-01-17 22:10:42 +00002800 Select(Chain);
2801
Chris Lattnere10269b2005-01-17 19:25:26 +00002802 X86AddressMode AM;
2803 if (getRegPressure(TheLoad.getOperand(0)) >
2804 getRegPressure(TheLoad.getOperand(1))) {
2805 Select(TheLoad.getOperand(0));
2806 SelectAddress(TheLoad.getOperand(1), AM);
2807 } else {
2808 SelectAddress(TheLoad.getOperand(1), AM);
2809 Select(TheLoad.getOperand(0));
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002810 }
Chris Lattner5c659812005-01-17 22:10:42 +00002811
2812 if (StVal.getOpcode() == ISD::ADD) {
2813 if (CN->getValue() == 1) {
2814 switch (Op0.getValueType()) {
2815 default: break;
2816 case MVT::i8:
2817 addFullAddress(BuildMI(BB, X86::INC8m, 4), AM);
2818 return true;
2819 case MVT::i16: Opc = TabPtr[1];
2820 addFullAddress(BuildMI(BB, X86::INC16m, 4), AM);
2821 return true;
2822 case MVT::i32: Opc = TabPtr[2];
2823 addFullAddress(BuildMI(BB, X86::INC32m, 4), AM);
2824 return true;
2825 }
2826 } else if (CN->getValue()+1 == 0) { // [X] += -1 -> DEC [X]
2827 switch (Op0.getValueType()) {
2828 default: break;
2829 case MVT::i8:
2830 addFullAddress(BuildMI(BB, X86::DEC8m, 4), AM);
2831 return true;
2832 case MVT::i16: Opc = TabPtr[1];
2833 addFullAddress(BuildMI(BB, X86::DEC16m, 4), AM);
2834 return true;
2835 case MVT::i32: Opc = TabPtr[2];
2836 addFullAddress(BuildMI(BB, X86::DEC32m, 4), AM);
2837 return true;
2838 }
2839 }
2840 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002841
Chris Lattnere10269b2005-01-17 19:25:26 +00002842 addFullAddress(BuildMI(BB, Opc, 4+1),AM).addImm(CN->getValue());
2843 return true;
2844 }
2845 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002846
Chris Lattnere10269b2005-01-17 19:25:26 +00002847 // If we have [mem] = V op [mem], try to turn it into:
2848 // [mem] = [mem] op V.
2849 if (Op1 == TheLoad && StVal.getOpcode() != ISD::SUB &&
2850 StVal.getOpcode() != ISD::SHL && StVal.getOpcode() != ISD::SRA &&
2851 StVal.getOpcode() != ISD::SRL)
2852 std::swap(Op0, Op1);
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002853
Chris Lattnere10269b2005-01-17 19:25:26 +00002854 if (Op0 != TheLoad) return false;
2855
2856 switch (Op0.getValueType()) {
2857 default: return false;
2858 case MVT::i1:
2859 case MVT::i8: Opc = TabPtr[3]; break;
2860 case MVT::i16: Opc = TabPtr[4]; break;
2861 case MVT::i32: Opc = TabPtr[5]; break;
2862 }
Chris Lattner5c659812005-01-17 22:10:42 +00002863
Chris Lattnerb422aea2005-01-18 17:35:28 +00002864 // Table entry doesn't exist?
2865 if (Opc == 0) return false;
2866
Chris Lattner4a108662005-01-18 03:51:59 +00002867 if (!ExprMap.insert(std::make_pair(TheLoad.getValue(1), 1)).second)
2868 assert(0 && "Already emitted?");
Chris Lattner5c659812005-01-17 22:10:42 +00002869 Select(Chain);
Chris Lattnere10269b2005-01-17 19:25:26 +00002870 Select(TheLoad.getOperand(0));
Chris Lattner98a8ba02005-01-18 01:06:26 +00002871
Chris Lattnere10269b2005-01-17 19:25:26 +00002872 X86AddressMode AM;
2873 SelectAddress(TheLoad.getOperand(1), AM);
2874 unsigned Reg = SelectExpr(Op1);
Chris Lattner98a8ba02005-01-18 01:06:26 +00002875 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addReg(Reg);
Chris Lattnere10269b2005-01-17 19:25:26 +00002876 return true;
2877}
2878
2879
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002880void ISel::Select(SDOperand N) {
2881 unsigned Tmp1, Tmp2, Opc;
2882
Nate Begeman85fdeb22005-03-24 04:39:54 +00002883 if (!ExprMap.insert(std::make_pair(N, 1)).second)
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002884 return; // Already selected.
2885
Chris Lattner989de032005-01-11 06:14:36 +00002886 SDNode *Node = N.Val;
2887
2888 switch (Node->getOpcode()) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002889 default:
Chris Lattner989de032005-01-11 06:14:36 +00002890 Node->dump(); std::cerr << "\n";
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002891 assert(0 && "Node not handled yet!");
2892 case ISD::EntryToken: return; // Noop
Chris Lattnerc3580712005-01-13 18:01:36 +00002893 case ISD::TokenFactor:
Chris Lattner1d50b7f2005-01-13 19:56:00 +00002894 if (Node->getNumOperands() == 2) {
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002895 bool OneFirst =
Chris Lattner1d50b7f2005-01-13 19:56:00 +00002896 getRegPressure(Node->getOperand(1))>getRegPressure(Node->getOperand(0));
2897 Select(Node->getOperand(OneFirst));
2898 Select(Node->getOperand(!OneFirst));
2899 } else {
2900 std::vector<std::pair<unsigned, unsigned> > OpsP;
2901 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
2902 OpsP.push_back(std::make_pair(getRegPressure(Node->getOperand(i)), i));
2903 std::sort(OpsP.begin(), OpsP.end());
2904 std::reverse(OpsP.begin(), OpsP.end());
2905 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
2906 Select(Node->getOperand(OpsP[i].second));
2907 }
Chris Lattnerc3580712005-01-13 18:01:36 +00002908 return;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002909 case ISD::CopyToReg:
Chris Lattneref6806c2005-01-12 02:02:48 +00002910 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2911 Select(N.getOperand(0));
2912 Tmp1 = SelectExpr(N.getOperand(1));
2913 } else {
2914 Tmp1 = SelectExpr(N.getOperand(1));
2915 Select(N.getOperand(0));
2916 }
Chris Lattner18c2f132005-01-13 20:50:02 +00002917 Tmp2 = cast<RegSDNode>(N)->getReg();
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002918
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002919 if (Tmp1 != Tmp2) {
2920 switch (N.getOperand(1).getValueType()) {
2921 default: assert(0 && "Invalid type for operation!");
2922 case MVT::i1:
2923 case MVT::i8: Opc = X86::MOV8rr; break;
2924 case MVT::i16: Opc = X86::MOV16rr; break;
2925 case MVT::i32: Opc = X86::MOV32rr; break;
Chris Lattneref7ba072005-01-11 03:50:45 +00002926 case MVT::f64: Opc = X86::FpMOV; ContainsFPCode = true; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002927 }
2928 BuildMI(BB, Opc, 1, Tmp2).addReg(Tmp1);
2929 }
2930 return;
2931 case ISD::RET:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002932 switch (N.getNumOperands()) {
2933 default:
2934 assert(0 && "Unknown return instruction!");
2935 case 3:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002936 assert(N.getOperand(1).getValueType() == MVT::i32 &&
2937 N.getOperand(2).getValueType() == MVT::i32 &&
2938 "Unknown two-register value!");
Chris Lattner11333092005-01-11 03:11:44 +00002939 if (getRegPressure(N.getOperand(1)) > getRegPressure(N.getOperand(2))) {
2940 Tmp1 = SelectExpr(N.getOperand(1));
2941 Tmp2 = SelectExpr(N.getOperand(2));
2942 } else {
2943 Tmp2 = SelectExpr(N.getOperand(2));
2944 Tmp1 = SelectExpr(N.getOperand(1));
2945 }
2946 Select(N.getOperand(0));
2947
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002948 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
2949 BuildMI(BB, X86::MOV32rr, 1, X86::EDX).addReg(Tmp2);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002950 break;
2951 case 2:
Chris Lattner11333092005-01-11 03:11:44 +00002952 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2953 Select(N.getOperand(0));
2954 Tmp1 = SelectExpr(N.getOperand(1));
2955 } else {
2956 Tmp1 = SelectExpr(N.getOperand(1));
2957 Select(N.getOperand(0));
2958 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002959 switch (N.getOperand(1).getValueType()) {
2960 default: assert(0 && "All other types should have been promoted!!");
2961 case MVT::f64:
2962 BuildMI(BB, X86::FpSETRESULT, 1).addReg(Tmp1);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002963 break;
2964 case MVT::i32:
2965 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002966 break;
2967 }
2968 break;
2969 case 1:
Chris Lattner11333092005-01-11 03:11:44 +00002970 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002971 break;
2972 }
2973 BuildMI(BB, X86::RET, 0); // Just emit a 'ret' instruction
2974 return;
2975 case ISD::BR: {
2976 Select(N.getOperand(0));
2977 MachineBasicBlock *Dest =
2978 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
2979 BuildMI(BB, X86::JMP, 1).addMBB(Dest);
2980 return;
2981 }
2982
2983 case ISD::BRCOND: {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002984 MachineBasicBlock *Dest =
2985 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
Chris Lattner11333092005-01-11 03:11:44 +00002986
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002987 // Try to fold a setcc into the branch. If this fails, emit a test/jne
2988 // pair.
Chris Lattner6c07aee2005-01-11 04:06:27 +00002989 if (EmitBranchCC(Dest, N.getOperand(0), N.getOperand(1))) {
2990 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2991 Select(N.getOperand(0));
2992 Tmp1 = SelectExpr(N.getOperand(1));
2993 } else {
2994 Tmp1 = SelectExpr(N.getOperand(1));
2995 Select(N.getOperand(0));
2996 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002997 BuildMI(BB, X86::TEST8rr, 2).addReg(Tmp1).addReg(Tmp1);
2998 BuildMI(BB, X86::JNE, 1).addMBB(Dest);
2999 }
Chris Lattner11333092005-01-11 03:11:44 +00003000
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003001 return;
3002 }
Chris Lattnere9ef81d2005-01-15 05:22:24 +00003003
Chris Lattner4df0de92005-01-17 00:00:33 +00003004 case ISD::LOAD:
3005 // If this load could be folded into the only using instruction, and if it
3006 // is safe to emit the instruction here, try to do so now.
3007 if (Node->hasNUsesOfValue(1, 0)) {
3008 SDOperand TheVal = N.getValue(0);
3009 SDNode *User = 0;
3010 for (SDNode::use_iterator UI = Node->use_begin(); ; ++UI) {
3011 assert(UI != Node->use_end() && "Didn't find use!");
3012 SDNode *UN = *UI;
3013 for (unsigned i = 0, e = UN->getNumOperands(); i != e; ++i)
3014 if (UN->getOperand(i) == TheVal) {
3015 User = UN;
3016 goto FoundIt;
3017 }
3018 }
3019 FoundIt:
3020 // Only handle unary operators right now.
3021 if (User->getNumOperands() == 1) {
Chris Lattner4a108662005-01-18 03:51:59 +00003022 ExprMap.erase(N);
Chris Lattner4df0de92005-01-17 00:00:33 +00003023 SelectExpr(SDOperand(User, 0));
3024 return;
3025 }
3026 }
Chris Lattnerb71f8fc2005-01-18 04:00:54 +00003027 ExprMap.erase(N);
Chris Lattner4df0de92005-01-17 00:00:33 +00003028 SelectExpr(N);
3029 return;
3030
Chris Lattnere9ef81d2005-01-15 05:22:24 +00003031 case ISD::EXTLOAD:
3032 case ISD::SEXTLOAD:
3033 case ISD::ZEXTLOAD:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003034 case ISD::CALL:
3035 case ISD::DYNAMIC_STACKALLOC:
Chris Lattnerb71f8fc2005-01-18 04:00:54 +00003036 ExprMap.erase(N);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003037 SelectExpr(N);
3038 return;
Chris Lattnere9ef81d2005-01-15 05:22:24 +00003039
3040 case ISD::TRUNCSTORE: { // truncstore chain, val, ptr :storety
3041 // On X86, we can represent all types except for Bool and Float natively.
3042 X86AddressMode AM;
3043 MVT::ValueType StoredTy = cast<MVTSDNode>(Node)->getExtraValueType();
Chris Lattnerda2ce112005-01-16 07:34:08 +00003044 assert((StoredTy == MVT::i1 || StoredTy == MVT::f32 ||
3045 StoredTy == MVT::i16 /*FIXME: THIS IS JUST FOR TESTING!*/)
3046 && "Unsupported TRUNCSTORE for this target!");
3047
3048 if (StoredTy == MVT::i16) {
3049 // FIXME: This is here just to allow testing. X86 doesn't really have a
3050 // TRUNCSTORE i16 operation, but this is required for targets that do not
3051 // have 16-bit integer registers. We occasionally disable 16-bit integer
3052 // registers to test the promotion code.
3053 Select(N.getOperand(0));
3054 Tmp1 = SelectExpr(N.getOperand(1));
3055 SelectAddress(N.getOperand(2), AM);
3056
3057 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
3058 addFullAddress(BuildMI(BB, X86::MOV16mr, 5), AM).addReg(X86::AX);
3059 return;
3060 }
Chris Lattnere9ef81d2005-01-15 05:22:24 +00003061
3062 // Store of constant bool?
3063 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
3064 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(2))) {
3065 Select(N.getOperand(0));
3066 SelectAddress(N.getOperand(2), AM);
3067 } else {
3068 SelectAddress(N.getOperand(2), AM);
3069 Select(N.getOperand(0));
3070 }
3071 addFullAddress(BuildMI(BB, X86::MOV8mi, 5), AM).addImm(CN->getValue());
3072 return;
3073 }
3074
3075 switch (StoredTy) {
3076 default: assert(0 && "Cannot truncstore this type!");
3077 case MVT::i1: Opc = X86::MOV8mr; break;
3078 case MVT::f32: Opc = X86::FST32m; break;
3079 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003080
Chris Lattnere9ef81d2005-01-15 05:22:24 +00003081 std::vector<std::pair<unsigned, unsigned> > RP;
3082 RP.push_back(std::make_pair(getRegPressure(N.getOperand(0)), 0));
3083 RP.push_back(std::make_pair(getRegPressure(N.getOperand(1)), 1));
3084 RP.push_back(std::make_pair(getRegPressure(N.getOperand(2)), 2));
3085 std::sort(RP.begin(), RP.end());
3086
Chris Lattner572dd082005-02-23 05:57:21 +00003087 Tmp1 = 0; // Silence a warning.
Chris Lattnere9ef81d2005-01-15 05:22:24 +00003088 for (unsigned i = 0; i != 3; ++i)
3089 switch (RP[2-i].second) {
3090 default: assert(0 && "Unknown operand number!");
3091 case 0: Select(N.getOperand(0)); break;
3092 case 1: Tmp1 = SelectExpr(N.getOperand(1)); break;
3093 case 2: SelectAddress(N.getOperand(2), AM); break;
3094 }
3095
3096 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addReg(Tmp1);
3097 return;
3098 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003099 case ISD::STORE: {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003100 X86AddressMode AM;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003101
3102 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
3103 Opc = 0;
3104 switch (CN->getValueType(0)) {
3105 default: assert(0 && "Invalid type for operation!");
3106 case MVT::i1:
3107 case MVT::i8: Opc = X86::MOV8mi; break;
3108 case MVT::i16: Opc = X86::MOV16mi; break;
3109 case MVT::i32: Opc = X86::MOV32mi; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003110 case MVT::f64: break;
3111 }
3112 if (Opc) {
Chris Lattner11333092005-01-11 03:11:44 +00003113 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(2))) {
3114 Select(N.getOperand(0));
3115 SelectAddress(N.getOperand(2), AM);
3116 } else {
3117 SelectAddress(N.getOperand(2), AM);
3118 Select(N.getOperand(0));
3119 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003120 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addImm(CN->getValue());
3121 return;
3122 }
Chris Lattner75f354b2005-04-21 19:03:24 +00003123 } else if (GlobalAddressSDNode *GA =
3124 dyn_cast<GlobalAddressSDNode>(N.getOperand(1))) {
3125 assert(GA->getValueType(0) == MVT::i32 && "Bad pointer operand");
3126
3127 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(2))) {
3128 Select(N.getOperand(0));
3129 SelectAddress(N.getOperand(2), AM);
3130 } else {
3131 SelectAddress(N.getOperand(2), AM);
3132 Select(N.getOperand(0));
3133 }
3134 addFullAddress(BuildMI(BB, X86::MOV32mi, 4+1),
3135 AM).addGlobalAddress(GA->getGlobal());
3136 return;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003137 }
Chris Lattner837caa72005-01-11 23:21:30 +00003138
3139 // Check to see if this is a load/op/store combination.
Chris Lattnere10269b2005-01-17 19:25:26 +00003140 if (TryToFoldLoadOpStore(Node))
3141 return;
Chris Lattner837caa72005-01-11 23:21:30 +00003142
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003143 switch (N.getOperand(1).getValueType()) {
3144 default: assert(0 && "Cannot store this type!");
3145 case MVT::i1:
3146 case MVT::i8: Opc = X86::MOV8mr; break;
3147 case MVT::i16: Opc = X86::MOV16mr; break;
3148 case MVT::i32: Opc = X86::MOV32mr; break;
Chris Lattneref7ba072005-01-11 03:50:45 +00003149 case MVT::f64: Opc = X86::FST64m; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003150 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003151
Chris Lattner11333092005-01-11 03:11:44 +00003152 std::vector<std::pair<unsigned, unsigned> > RP;
3153 RP.push_back(std::make_pair(getRegPressure(N.getOperand(0)), 0));
3154 RP.push_back(std::make_pair(getRegPressure(N.getOperand(1)), 1));
3155 RP.push_back(std::make_pair(getRegPressure(N.getOperand(2)), 2));
3156 std::sort(RP.begin(), RP.end());
3157
Chris Lattner572dd082005-02-23 05:57:21 +00003158 Tmp1 = 0; // Silence a warning.
Chris Lattner11333092005-01-11 03:11:44 +00003159 for (unsigned i = 0; i != 3; ++i)
3160 switch (RP[2-i].second) {
3161 default: assert(0 && "Unknown operand number!");
3162 case 0: Select(N.getOperand(0)); break;
3163 case 1: Tmp1 = SelectExpr(N.getOperand(1)); break;
Chris Lattnera3aa2e22005-01-11 03:37:59 +00003164 case 2: SelectAddress(N.getOperand(2), AM); break;
Chris Lattner11333092005-01-11 03:11:44 +00003165 }
3166
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003167 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addReg(Tmp1);
3168 return;
3169 }
3170 case ISD::ADJCALLSTACKDOWN:
3171 case ISD::ADJCALLSTACKUP:
3172 Select(N.getOperand(0));
3173 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003174
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003175 Opc = N.getOpcode() == ISD::ADJCALLSTACKDOWN ? X86::ADJCALLSTACKDOWN :
3176 X86::ADJCALLSTACKUP;
3177 BuildMI(BB, Opc, 1).addImm(Tmp1);
3178 return;
Chris Lattner989de032005-01-11 06:14:36 +00003179 case ISD::MEMSET: {
3180 Select(N.getOperand(0)); // Select the chain.
3181 unsigned Align =
3182 (unsigned)cast<ConstantSDNode>(Node->getOperand(4))->getValue();
3183 if (Align == 0) Align = 1;
3184
3185 // Turn the byte code into # iterations
3186 unsigned CountReg;
3187 unsigned Opcode;
3188 if (ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Node->getOperand(2))) {
3189 unsigned Val = ValC->getValue() & 255;
3190
3191 // If the value is a constant, then we can potentially use larger sets.
3192 switch (Align & 3) {
3193 case 2: // WORD aligned
3194 CountReg = MakeReg(MVT::i32);
3195 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
3196 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/2);
3197 } else {
3198 unsigned ByteReg = SelectExpr(Node->getOperand(3));
3199 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(1);
3200 }
3201 BuildMI(BB, X86::MOV16ri, 1, X86::AX).addImm((Val << 8) | Val);
3202 Opcode = X86::REP_STOSW;
3203 break;
3204 case 0: // DWORD aligned
3205 CountReg = MakeReg(MVT::i32);
3206 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
3207 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/4);
3208 } else {
3209 unsigned ByteReg = SelectExpr(Node->getOperand(3));
3210 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(2);
3211 }
3212 Val = (Val << 8) | Val;
3213 BuildMI(BB, X86::MOV32ri, 1, X86::EAX).addImm((Val << 16) | Val);
3214 Opcode = X86::REP_STOSD;
3215 break;
3216 default: // BYTE aligned
3217 CountReg = SelectExpr(Node->getOperand(3));
3218 BuildMI(BB, X86::MOV8ri, 1, X86::AL).addImm(Val);
3219 Opcode = X86::REP_STOSB;
3220 break;
3221 }
3222 } else {
3223 // If it's not a constant value we are storing, just fall back. We could
3224 // try to be clever to form 16 bit and 32 bit values, but we don't yet.
3225 unsigned ValReg = SelectExpr(Node->getOperand(2));
3226 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(ValReg);
3227 CountReg = SelectExpr(Node->getOperand(3));
3228 Opcode = X86::REP_STOSB;
3229 }
3230
3231 // No matter what the alignment is, we put the source in ESI, the
3232 // destination in EDI, and the count in ECX.
3233 unsigned TmpReg1 = SelectExpr(Node->getOperand(1));
3234 BuildMI(BB, X86::MOV32rr, 1, X86::ECX).addReg(CountReg);
3235 BuildMI(BB, X86::MOV32rr, 1, X86::EDI).addReg(TmpReg1);
3236 BuildMI(BB, Opcode, 0);
3237 return;
3238 }
Chris Lattner31805bf2005-01-11 06:19:26 +00003239 case ISD::MEMCPY:
3240 Select(N.getOperand(0)); // Select the chain.
3241 unsigned Align =
3242 (unsigned)cast<ConstantSDNode>(Node->getOperand(4))->getValue();
3243 if (Align == 0) Align = 1;
3244
3245 // Turn the byte code into # iterations
3246 unsigned CountReg;
3247 unsigned Opcode;
3248 switch (Align & 3) {
3249 case 2: // WORD aligned
3250 CountReg = MakeReg(MVT::i32);
3251 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
3252 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/2);
3253 } else {
3254 unsigned ByteReg = SelectExpr(Node->getOperand(3));
3255 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(1);
3256 }
3257 Opcode = X86::REP_MOVSW;
3258 break;
3259 case 0: // DWORD aligned
3260 CountReg = MakeReg(MVT::i32);
3261 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
3262 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/4);
3263 } else {
3264 unsigned ByteReg = SelectExpr(Node->getOperand(3));
3265 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(2);
3266 }
3267 Opcode = X86::REP_MOVSD;
3268 break;
3269 default: // BYTE aligned
3270 CountReg = SelectExpr(Node->getOperand(3));
3271 Opcode = X86::REP_MOVSB;
3272 break;
3273 }
3274
3275 // No matter what the alignment is, we put the source in ESI, the
3276 // destination in EDI, and the count in ECX.
3277 unsigned TmpReg1 = SelectExpr(Node->getOperand(1));
3278 unsigned TmpReg2 = SelectExpr(Node->getOperand(2));
3279 BuildMI(BB, X86::MOV32rr, 1, X86::ECX).addReg(CountReg);
3280 BuildMI(BB, X86::MOV32rr, 1, X86::EDI).addReg(TmpReg1);
3281 BuildMI(BB, X86::MOV32rr, 1, X86::ESI).addReg(TmpReg2);
3282 BuildMI(BB, Opcode, 0);
3283 return;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003284 }
3285 assert(0 && "Should not be reached!");
3286}
3287
3288
3289/// createX86PatternInstructionSelector - This pass converts an LLVM function
3290/// into a machine code representation using pattern matching and a machine
3291/// description file.
3292///
3293FunctionPass *llvm::createX86PatternInstructionSelector(TargetMachine &TM) {
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003294 return new ISel(TM);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003295}