blob: d4b2b1a99e8f828ecdd7fe0d45796eef76147570 [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 Lattnerc5dcb532005-04-30 04:25:35 +000073 if (!UnsafeFPMath) {
74 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
75 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
76 }
77
Chris Lattnerda2ce112005-01-16 07:34:08 +000078 // These should be promoted to a larger select which is supported.
79/**/ setOperationAction(ISD::SELECT , MVT::i1 , Promote);
80 setOperationAction(ISD::SELECT , MVT::i8 , Promote);
Misha Brukman0e0a7a452005-04-21 23:38:14 +000081
Chris Lattner8acb1ba2005-01-07 07:49:41 +000082 computeRegisterProperties();
Misha Brukman0e0a7a452005-04-21 23:38:14 +000083
Chris Lattner8acb1ba2005-01-07 07:49:41 +000084 addLegalFPImmediate(+0.0); // FLD0
85 addLegalFPImmediate(+1.0); // FLD1
86 addLegalFPImmediate(-0.0); // FLD0/FCHS
87 addLegalFPImmediate(-1.0); // FLD1/FCHS
88 }
89
90 /// LowerArguments - This hook must be implemented to indicate how we should
91 /// lower the arguments for the specified function, into the specified DAG.
92 virtual std::vector<SDOperand>
93 LowerArguments(Function &F, SelectionDAG &DAG);
94
95 /// LowerCallTo - This hook lowers an abstract call to a function into an
96 /// actual call.
Chris Lattner5188ad72005-01-08 19:28:19 +000097 virtual std::pair<SDOperand, SDOperand>
Nate Begeman8e21e712005-03-26 01:29:23 +000098 LowerCallTo(SDOperand Chain, const Type *RetTy, bool isVarArg,
99 SDOperand Callee, ArgListTy &Args, SelectionDAG &DAG);
Chris Lattner14824582005-01-09 00:01:27 +0000100
101 virtual std::pair<SDOperand, SDOperand>
102 LowerVAStart(SDOperand Chain, SelectionDAG &DAG);
103
104 virtual std::pair<SDOperand,SDOperand>
105 LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
106 const Type *ArgTy, SelectionDAG &DAG);
107
108 virtual std::pair<SDOperand, SDOperand>
109 LowerFrameReturnAddress(bool isFrameAddr, SDOperand Chain, unsigned Depth,
110 SelectionDAG &DAG);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000111 };
112}
113
114
115std::vector<SDOperand>
116X86TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
117 std::vector<SDOperand> ArgValues;
118
119 // Add DAG nodes to load the arguments... On entry to a function on the X86,
120 // the stack frame looks like this:
121 //
122 // [ESP] -- return address
123 // [ESP + 4] -- first argument (leftmost lexically)
124 // [ESP + 8] -- second argument, if first argument is four bytes in size
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000125 // ...
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000126 //
127 MachineFunction &MF = DAG.getMachineFunction();
128 MachineFrameInfo *MFI = MF.getFrameInfo();
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000129
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000130 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
Chris Lattnere4d5c442005-03-15 04:54:21 +0000131 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000132 MVT::ValueType ObjectVT = getValueType(I->getType());
133 unsigned ArgIncrement = 4;
134 unsigned ObjSize;
135 switch (ObjectVT) {
136 default: assert(0 && "Unhandled argument type!");
137 case MVT::i1:
138 case MVT::i8: ObjSize = 1; break;
139 case MVT::i16: ObjSize = 2; break;
140 case MVT::i32: ObjSize = 4; break;
141 case MVT::i64: ObjSize = ArgIncrement = 8; break;
142 case MVT::f32: ObjSize = 4; break;
143 case MVT::f64: ObjSize = ArgIncrement = 8; break;
144 }
145 // Create the frame index object for this incoming parameter...
146 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000147
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000148 // Create the SelectionDAG nodes corresponding to a load from this parameter
149 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
150
151 // Don't codegen dead arguments. FIXME: remove this check when we can nuke
152 // dead loads.
153 SDOperand ArgValue;
154 if (!I->use_empty())
Chris Lattnera80d2bd2005-05-09 05:40:26 +0000155 ArgValue = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
156 DAG.getSrcValue(NULL));
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000157 else {
158 if (MVT::isInteger(ObjectVT))
159 ArgValue = DAG.getConstant(0, ObjectVT);
160 else
161 ArgValue = DAG.getConstantFP(0, ObjectVT);
162 }
163 ArgValues.push_back(ArgValue);
164
165 ArgOffset += ArgIncrement; // Move on to the next argument...
166 }
167
168 // If the function takes variable number of arguments, make a frame index for
169 // the start of the first vararg value... for expansion of llvm.va_start.
170 if (F.isVarArg())
171 VarArgsFrameIndex = MFI->CreateFixedObject(1, ArgOffset);
Chris Lattner14824582005-01-09 00:01:27 +0000172 ReturnAddrIndex = 0; // No return address slot generated yet.
Chris Lattner4c52f0e2005-04-09 15:23:56 +0000173
174 // Finally, inform the code generator which regs we return values in.
175 switch (getValueType(F.getReturnType())) {
176 default: assert(0 && "Unknown type!");
177 case MVT::isVoid: break;
178 case MVT::i1:
179 case MVT::i8:
180 case MVT::i16:
181 case MVT::i32:
182 MF.addLiveOut(X86::EAX);
183 break;
184 case MVT::i64:
185 MF.addLiveOut(X86::EAX);
186 MF.addLiveOut(X86::EDX);
187 break;
188 case MVT::f32:
189 case MVT::f64:
190 MF.addLiveOut(X86::ST0);
191 break;
192 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000193 return ArgValues;
194}
195
Chris Lattner5188ad72005-01-08 19:28:19 +0000196std::pair<SDOperand, SDOperand>
197X86TargetLowering::LowerCallTo(SDOperand Chain,
Nate Begeman8e21e712005-03-26 01:29:23 +0000198 const Type *RetTy, bool isVarArg,
199 SDOperand Callee, ArgListTy &Args, SelectionDAG &DAG) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000200 // Count how many bytes are to be pushed on the stack.
201 unsigned NumBytes = 0;
202
203 if (Args.empty()) {
204 // Save zero bytes.
Chris Lattner5188ad72005-01-08 19:28:19 +0000205 Chain = DAG.getNode(ISD::ADJCALLSTACKDOWN, MVT::Other, Chain,
206 DAG.getConstant(0, getPointerTy()));
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000207 } else {
208 for (unsigned i = 0, e = Args.size(); i != e; ++i)
209 switch (getValueType(Args[i].second)) {
210 default: assert(0 && "Unknown value type!");
211 case MVT::i1:
212 case MVT::i8:
213 case MVT::i16:
214 case MVT::i32:
215 case MVT::f32:
216 NumBytes += 4;
217 break;
218 case MVT::i64:
219 case MVT::f64:
220 NumBytes += 8;
221 break;
222 }
223
Chris Lattner5188ad72005-01-08 19:28:19 +0000224 Chain = DAG.getNode(ISD::ADJCALLSTACKDOWN, MVT::Other, Chain,
225 DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000226
227 // Arguments go on the stack in reverse order, as specified by the ABI.
228 unsigned ArgOffset = 0;
Chris Lattner7f2afac2005-01-14 22:37:41 +0000229 SDOperand StackPtr = DAG.getCopyFromReg(X86::ESP, MVT::i32,
230 DAG.getEntryNode());
Chris Lattnerb62e1e22005-01-21 19:46:38 +0000231 std::vector<SDOperand> Stores;
232
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000233 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
234 unsigned ArgReg;
235 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
236 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
237
238 switch (getValueType(Args[i].second)) {
239 default: assert(0 && "Unexpected ValueType for argument!");
240 case MVT::i1:
241 case MVT::i8:
242 case MVT::i16:
243 // Promote the integer to 32 bits. If the input type is signed use a
244 // sign extend, otherwise use a zero extend.
245 if (Args[i].second->isSigned())
246 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
247 else
248 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
249
250 // FALL THROUGH
251 case MVT::i32:
252 case MVT::f32:
Chris Lattnerb62e1e22005-01-21 19:46:38 +0000253 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
Chris Lattnera80d2bd2005-05-09 05:40:26 +0000254 Args[i].first, PtrOff,
255 DAG.getSrcValue(NULL)));
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000256 ArgOffset += 4;
257 break;
258 case MVT::i64:
259 case MVT::f64:
Chris Lattnerb62e1e22005-01-21 19:46:38 +0000260 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
Chris Lattnera80d2bd2005-05-09 05:40:26 +0000261 Args[i].first, PtrOff,
262 DAG.getSrcValue(NULL)));
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000263 ArgOffset += 8;
264 break;
265 }
266 }
Chris Lattnerb62e1e22005-01-21 19:46:38 +0000267 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, Stores);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000268 }
269
270 std::vector<MVT::ValueType> RetVals;
271 MVT::ValueType RetTyVT = getValueType(RetTy);
272 if (RetTyVT != MVT::isVoid)
273 RetVals.push_back(RetTyVT);
274 RetVals.push_back(MVT::Other);
275
Chris Lattner5188ad72005-01-08 19:28:19 +0000276 SDOperand TheCall = SDOperand(DAG.getCall(RetVals, Chain, Callee), 0);
Chris Lattnerb0802652005-01-08 20:51:36 +0000277 Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
Chris Lattner5188ad72005-01-08 19:28:19 +0000278 Chain = DAG.getNode(ISD::ADJCALLSTACKUP, MVT::Other, Chain,
279 DAG.getConstant(NumBytes, getPointerTy()));
280 return std::make_pair(TheCall, Chain);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000281}
282
Chris Lattner14824582005-01-09 00:01:27 +0000283std::pair<SDOperand, SDOperand>
284X86TargetLowering::LowerVAStart(SDOperand Chain, SelectionDAG &DAG) {
285 // vastart just returns the address of the VarArgsFrameIndex slot.
286 return std::make_pair(DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32), Chain);
287}
288
289std::pair<SDOperand,SDOperand> X86TargetLowering::
290LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
291 const Type *ArgTy, SelectionDAG &DAG) {
292 MVT::ValueType ArgVT = getValueType(ArgTy);
293 SDOperand Result;
294 if (!isVANext) {
Chris Lattnera80d2bd2005-05-09 05:40:26 +0000295 Result = DAG.getLoad(ArgVT, DAG.getEntryNode(), VAList,
296 DAG.getSrcValue(NULL));
Chris Lattner14824582005-01-09 00:01:27 +0000297 } else {
298 unsigned Amt;
299 if (ArgVT == MVT::i32)
300 Amt = 4;
301 else {
302 assert((ArgVT == MVT::i64 || ArgVT == MVT::f64) &&
303 "Other types should have been promoted for varargs!");
304 Amt = 8;
305 }
306 Result = DAG.getNode(ISD::ADD, VAList.getValueType(), VAList,
307 DAG.getConstant(Amt, VAList.getValueType()));
308 }
309 return std::make_pair(Result, Chain);
310}
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000311
Chris Lattner14824582005-01-09 00:01:27 +0000312
313std::pair<SDOperand, SDOperand> X86TargetLowering::
314LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
315 SelectionDAG &DAG) {
316 SDOperand Result;
317 if (Depth) // Depths > 0 not supported yet!
318 Result = DAG.getConstant(0, getPointerTy());
319 else {
320 if (ReturnAddrIndex == 0) {
321 // Set up a frame object for the return address.
322 MachineFunction &MF = DAG.getMachineFunction();
323 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(4, -4);
324 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000325
Chris Lattner14824582005-01-09 00:01:27 +0000326 SDOperand RetAddrFI = DAG.getFrameIndex(ReturnAddrIndex, MVT::i32);
327
328 if (!isFrameAddress)
329 // Just load the return address
Andrew Lenharth2d86ea22005-04-27 20:10:01 +0000330 Result = DAG.getLoad(MVT::i32, DAG.getEntryNode(), RetAddrFI, DAG.getSrcValue(NULL));
Chris Lattner14824582005-01-09 00:01:27 +0000331 else
332 Result = DAG.getNode(ISD::SUB, MVT::i32, RetAddrFI,
333 DAG.getConstant(4, MVT::i32));
334 }
335 return std::make_pair(Result, Chain);
336}
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000337
338
Chris Lattner98a8ba02005-01-18 01:06:26 +0000339namespace {
340 /// X86ISelAddressMode - This corresponds to X86AddressMode, but uses
341 /// SDOperand's instead of register numbers for the leaves of the matched
342 /// tree.
343 struct X86ISelAddressMode {
344 enum {
345 RegBase,
346 FrameIndexBase,
347 } BaseType;
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000348
Chris Lattner98a8ba02005-01-18 01:06:26 +0000349 struct { // This is really a union, discriminated by BaseType!
350 SDOperand Reg;
351 int FrameIndex;
352 } Base;
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000353
Chris Lattner98a8ba02005-01-18 01:06:26 +0000354 unsigned Scale;
355 SDOperand IndexReg;
356 unsigned Disp;
357 GlobalValue *GV;
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000358
Chris Lattner98a8ba02005-01-18 01:06:26 +0000359 X86ISelAddressMode()
360 : BaseType(RegBase), Scale(1), IndexReg(), Disp(), GV(0) {
361 }
362 };
363}
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000364
365
366namespace {
367 Statistic<>
368 NumFPKill("x86-codegen", "Number of FP_REG_KILL instructions added");
369
370 //===--------------------------------------------------------------------===//
371 /// ISel - X86 specific code to select X86 machine instructions for
372 /// SelectionDAG operations.
373 ///
374 class ISel : public SelectionDAGISel {
375 /// ContainsFPCode - Every instruction we select that uses or defines a FP
376 /// register should set this to true.
377 bool ContainsFPCode;
378
379 /// X86Lowering - This object fully describes how to lower LLVM code to an
380 /// X86-specific SelectionDAG.
381 X86TargetLowering X86Lowering;
382
Chris Lattner11333092005-01-11 03:11:44 +0000383 /// RegPressureMap - This keeps an approximate count of the number of
384 /// registers required to evaluate each node in the graph.
385 std::map<SDNode*, unsigned> RegPressureMap;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000386
387 /// ExprMap - As shared expressions are codegen'd, we keep track of which
388 /// vreg the value is produced in, so we only emit one copy of each compiled
389 /// tree.
390 std::map<SDOperand, unsigned> ExprMap;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000391
392 public:
393 ISel(TargetMachine &TM) : SelectionDAGISel(X86Lowering), X86Lowering(TM) {
394 }
395
Chris Lattner67b1c3c2005-01-21 21:35:14 +0000396 virtual const char *getPassName() const {
397 return "X86 Pattern Instruction Selection";
398 }
399
Chris Lattner11333092005-01-11 03:11:44 +0000400 unsigned getRegPressure(SDOperand O) {
401 return RegPressureMap[O.Val];
402 }
403 unsigned ComputeRegPressure(SDOperand O);
404
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000405 /// InstructionSelectBasicBlock - This callback is invoked by
406 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
Chris Lattner7dbcb752005-01-12 04:21:28 +0000407 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000408
Chris Lattner44129b52005-01-25 20:03:11 +0000409 bool isFoldableLoad(SDOperand Op, SDOperand OtherOp,
410 bool FloatPromoteOk = false);
Chris Lattnera5ade062005-01-11 21:19:59 +0000411 void EmitFoldedLoad(SDOperand Op, X86AddressMode &AM);
Chris Lattnere10269b2005-01-17 19:25:26 +0000412 bool TryToFoldLoadOpStore(SDNode *Node);
Chris Lattnera5ade062005-01-11 21:19:59 +0000413
Chris Lattner30ea1e92005-01-19 07:37:26 +0000414 bool EmitOrOpOp(SDOperand Op1, SDOperand Op2, unsigned DestReg);
Chris Lattnercb1aa8d2005-01-17 01:34:14 +0000415 void EmitCMP(SDOperand LHS, SDOperand RHS, bool isOnlyUse);
Chris Lattner6c07aee2005-01-11 04:06:27 +0000416 bool EmitBranchCC(MachineBasicBlock *Dest, SDOperand Chain, SDOperand Cond);
Chris Lattner24aad1b2005-01-10 22:10:13 +0000417 void EmitSelectCC(SDOperand Cond, MVT::ValueType SVT,
418 unsigned RTrue, unsigned RFalse, unsigned RDest);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000419 unsigned SelectExpr(SDOperand N);
Chris Lattner98a8ba02005-01-18 01:06:26 +0000420
421 X86AddressMode SelectAddrExprs(const X86ISelAddressMode &IAM);
422 bool MatchAddress(SDOperand N, X86ISelAddressMode &AM);
423 void SelectAddress(SDOperand N, X86AddressMode &AM);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000424 void Select(SDOperand N);
425 };
426}
427
Chris Lattner7dbcb752005-01-12 04:21:28 +0000428/// InstructionSelectBasicBlock - This callback is invoked by SelectionDAGISel
429/// when it has created a SelectionDAG for us to codegen.
430void ISel::InstructionSelectBasicBlock(SelectionDAG &DAG) {
431 // While we're doing this, keep track of whether we see any FP code for
432 // FP_REG_KILL insertion.
433 ContainsFPCode = false;
434
435 // Scan the PHI nodes that already are inserted into this basic block. If any
436 // of them is a PHI of a floating point value, we need to insert an
437 // FP_REG_KILL.
438 SSARegMap *RegMap = BB->getParent()->getSSARegMap();
439 for (MachineBasicBlock::iterator I = BB->begin(), E = BB->end();
440 I != E; ++I) {
441 assert(I->getOpcode() == X86::PHI &&
442 "Isn't just PHI nodes?");
443 if (RegMap->getRegClass(I->getOperand(0).getReg()) ==
444 X86::RFPRegisterClass) {
445 ContainsFPCode = true;
446 break;
447 }
448 }
449
450 // Compute the RegPressureMap, which is an approximation for the number of
451 // registers required to compute each node.
452 ComputeRegPressure(DAG.getRoot());
453
454 // Codegen the basic block.
455 Select(DAG.getRoot());
456
457 // Finally, look at all of the successors of this block. If any contain a PHI
458 // node of FP type, we need to insert an FP_REG_KILL in this block.
459 for (MachineBasicBlock::succ_iterator SI = BB->succ_begin(),
460 E = BB->succ_end(); SI != E && !ContainsFPCode; ++SI)
461 for (MachineBasicBlock::iterator I = (*SI)->begin(), E = (*SI)->end();
462 I != E && I->getOpcode() == X86::PHI; ++I) {
463 if (RegMap->getRegClass(I->getOperand(0).getReg()) ==
464 X86::RFPRegisterClass) {
465 ContainsFPCode = true;
466 break;
467 }
468 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000469
Chris Lattnere3e0f272005-05-09 03:36:39 +0000470 // Final check, check LLVM BB's that are successors to the LLVM BB
471 // corresponding to BB for FP PHI nodes.
472 const BasicBlock *LLVMBB = BB->getBasicBlock();
473 const PHINode *PN;
474 if (!ContainsFPCode)
475 for (succ_const_iterator SI = succ_begin(LLVMBB), E = succ_end(LLVMBB);
476 SI != E && !ContainsFPCode; ++SI)
477 for (BasicBlock::const_iterator II = SI->begin();
478 (PN = dyn_cast<PHINode>(II)); ++II)
479 if (PN->getType()->isFloatingPoint()) {
480 ContainsFPCode = true;
481 break;
482 }
483
484
Chris Lattner7dbcb752005-01-12 04:21:28 +0000485 // Insert FP_REG_KILL instructions into basic blocks that need them. This
486 // only occurs due to the floating point stackifier not being aggressive
487 // enough to handle arbitrary global stackification.
488 //
489 // Currently we insert an FP_REG_KILL instruction into each block that uses or
490 // defines a floating point virtual register.
491 //
492 // When the global register allocators (like linear scan) finally update live
493 // variable analysis, we can keep floating point values in registers across
494 // basic blocks. This will be a huge win, but we are waiting on the global
495 // allocators before we can do this.
496 //
Chris Lattner71df3f82005-03-30 01:10:00 +0000497 if (ContainsFPCode) {
Chris Lattner7dbcb752005-01-12 04:21:28 +0000498 BuildMI(*BB, BB->getFirstTerminator(), X86::FP_REG_KILL, 0);
499 ++NumFPKill;
500 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000501
Chris Lattner7dbcb752005-01-12 04:21:28 +0000502 // Clear state used for selection.
503 ExprMap.clear();
Chris Lattner7dbcb752005-01-12 04:21:28 +0000504 RegPressureMap.clear();
505}
506
507
Chris Lattner11333092005-01-11 03:11:44 +0000508// ComputeRegPressure - Compute the RegPressureMap, which is an approximation
509// for the number of registers required to compute each node. This is basically
510// computing a generalized form of the Sethi-Ullman number for each node.
511unsigned ISel::ComputeRegPressure(SDOperand O) {
512 SDNode *N = O.Val;
513 unsigned &Result = RegPressureMap[N];
514 if (Result) return Result;
515
Chris Lattnera3aa2e22005-01-11 03:37:59 +0000516 // FIXME: Should operations like CALL (which clobber lots o regs) have a
517 // higher fixed cost??
518
Chris Lattnerc4b6a782005-01-11 22:29:12 +0000519 if (N->getNumOperands() == 0) {
520 Result = 1;
521 } else {
522 unsigned MaxRegUse = 0;
523 unsigned NumExtraMaxRegUsers = 0;
524 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
525 unsigned Regs;
526 if (N->getOperand(i).getOpcode() == ISD::Constant)
527 Regs = 0;
528 else
529 Regs = ComputeRegPressure(N->getOperand(i));
530 if (Regs > MaxRegUse) {
531 MaxRegUse = Regs;
532 NumExtraMaxRegUsers = 0;
533 } else if (Regs == MaxRegUse &&
534 N->getOperand(i).getValueType() != MVT::Other) {
535 ++NumExtraMaxRegUsers;
536 }
Chris Lattner11333092005-01-11 03:11:44 +0000537 }
Chris Lattner90d1be72005-01-17 22:56:09 +0000538
539 if (O.getOpcode() != ISD::TokenFactor)
540 Result = MaxRegUse+NumExtraMaxRegUsers;
541 else
Chris Lattner869e0432005-01-17 23:02:13 +0000542 Result = MaxRegUse == 1 ? 0 : MaxRegUse-1;
Chris Lattnerc4b6a782005-01-11 22:29:12 +0000543 }
Chris Lattnerafce4302005-01-12 02:19:06 +0000544
Chris Lattner837caa72005-01-11 23:21:30 +0000545 //std::cerr << " WEIGHT: " << Result << " "; N->dump(); std::cerr << "\n";
Chris Lattnerc4b6a782005-01-11 22:29:12 +0000546 return Result;
Chris Lattner11333092005-01-11 03:11:44 +0000547}
548
Chris Lattnerbf52d492005-01-20 16:50:16 +0000549/// NodeTransitivelyUsesValue - Return true if N or any of its uses uses Op.
550/// The DAG cannot have cycles in it, by definition, so the visited set is not
551/// needed to prevent infinite loops. The DAG CAN, however, have unbounded
552/// reuse, so it prevents exponential cases.
553///
554static bool NodeTransitivelyUsesValue(SDOperand N, SDOperand Op,
555 std::set<SDNode*> &Visited) {
556 if (N == Op) return true; // Found it.
557 SDNode *Node = N.Val;
Chris Lattnerfb0f53f2005-01-21 21:43:02 +0000558 if (Node->getNumOperands() == 0 || // Leaf?
559 Node->getNodeDepth() <= Op.getNodeDepth()) return false; // Can't find it?
Chris Lattnerbf52d492005-01-20 16:50:16 +0000560 if (!Visited.insert(Node).second) return false; // Already visited?
561
562 // Recurse for the first N-1 operands.
563 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i)
564 if (NodeTransitivelyUsesValue(Node->getOperand(i), Op, Visited))
565 return true;
566
567 // Tail recurse for the last operand.
568 return NodeTransitivelyUsesValue(Node->getOperand(0), Op, Visited);
569}
570
Chris Lattner98a8ba02005-01-18 01:06:26 +0000571X86AddressMode ISel::SelectAddrExprs(const X86ISelAddressMode &IAM) {
572 X86AddressMode Result;
573
574 // If we need to emit two register operands, emit the one with the highest
575 // register pressure first.
576 if (IAM.BaseType == X86ISelAddressMode::RegBase &&
577 IAM.Base.Reg.Val && IAM.IndexReg.Val) {
Chris Lattnerbf52d492005-01-20 16:50:16 +0000578 bool EmitBaseThenIndex;
Chris Lattner98a8ba02005-01-18 01:06:26 +0000579 if (getRegPressure(IAM.Base.Reg) > getRegPressure(IAM.IndexReg)) {
Chris Lattnerbf52d492005-01-20 16:50:16 +0000580 std::set<SDNode*> Visited;
581 EmitBaseThenIndex = true;
582 // If Base ends up pointing to Index, we must emit index first. This is
583 // because of the way we fold loads, we may end up doing bad things with
584 // the folded add.
585 if (NodeTransitivelyUsesValue(IAM.Base.Reg, IAM.IndexReg, Visited))
586 EmitBaseThenIndex = false;
587 } else {
588 std::set<SDNode*> Visited;
589 EmitBaseThenIndex = false;
590 // If Base ends up pointing to Index, we must emit index first. This is
591 // because of the way we fold loads, we may end up doing bad things with
592 // the folded add.
593 if (NodeTransitivelyUsesValue(IAM.IndexReg, IAM.Base.Reg, Visited))
594 EmitBaseThenIndex = true;
595 }
596
597 if (EmitBaseThenIndex) {
Chris Lattner98a8ba02005-01-18 01:06:26 +0000598 Result.Base.Reg = SelectExpr(IAM.Base.Reg);
599 Result.IndexReg = SelectExpr(IAM.IndexReg);
600 } else {
601 Result.IndexReg = SelectExpr(IAM.IndexReg);
602 Result.Base.Reg = SelectExpr(IAM.Base.Reg);
603 }
Chris Lattnerbf52d492005-01-20 16:50:16 +0000604
Chris Lattner98a8ba02005-01-18 01:06:26 +0000605 } else if (IAM.BaseType == X86ISelAddressMode::RegBase && IAM.Base.Reg.Val) {
606 Result.Base.Reg = SelectExpr(IAM.Base.Reg);
607 } else if (IAM.IndexReg.Val) {
608 Result.IndexReg = SelectExpr(IAM.IndexReg);
609 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000610
Chris Lattner98a8ba02005-01-18 01:06:26 +0000611 switch (IAM.BaseType) {
612 case X86ISelAddressMode::RegBase:
613 Result.BaseType = X86AddressMode::RegBase;
614 break;
615 case X86ISelAddressMode::FrameIndexBase:
616 Result.BaseType = X86AddressMode::FrameIndexBase;
617 Result.Base.FrameIndex = IAM.Base.FrameIndex;
618 break;
619 default:
620 assert(0 && "Unknown base type!");
621 break;
622 }
623 Result.Scale = IAM.Scale;
624 Result.Disp = IAM.Disp;
625 Result.GV = IAM.GV;
626 return Result;
627}
628
629/// SelectAddress - Pattern match the maximal addressing mode for this node and
630/// emit all of the leaf registers.
631void ISel::SelectAddress(SDOperand N, X86AddressMode &AM) {
632 X86ISelAddressMode IAM;
633 MatchAddress(N, IAM);
634 AM = SelectAddrExprs(IAM);
635}
636
637/// MatchAddress - Add the specified node to the specified addressing mode,
638/// returning true if it cannot be done. This just pattern matches for the
639/// addressing mode, it does not cause any code to be emitted. For that, use
640/// SelectAddress.
641bool ISel::MatchAddress(SDOperand N, X86ISelAddressMode &AM) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000642 switch (N.getOpcode()) {
643 default: break;
644 case ISD::FrameIndex:
Chris Lattner98a8ba02005-01-18 01:06:26 +0000645 if (AM.BaseType == X86ISelAddressMode::RegBase && AM.Base.Reg.Val == 0) {
646 AM.BaseType = X86ISelAddressMode::FrameIndexBase;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000647 AM.Base.FrameIndex = cast<FrameIndexSDNode>(N)->getIndex();
648 return false;
649 }
650 break;
651 case ISD::GlobalAddress:
652 if (AM.GV == 0) {
653 AM.GV = cast<GlobalAddressSDNode>(N)->getGlobal();
654 return false;
655 }
656 break;
657 case ISD::Constant:
658 AM.Disp += cast<ConstantSDNode>(N)->getValue();
659 return false;
660 case ISD::SHL:
Chris Lattner636e79a2005-01-13 05:53:16 +0000661 // We might have folded the load into this shift, so don't regen the value
662 // if so.
663 if (ExprMap.count(N)) break;
664
Chris Lattner98a8ba02005-01-18 01:06:26 +0000665 if (AM.IndexReg.Val == 0 && AM.Scale == 1)
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000666 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1))) {
667 unsigned Val = CN->getValue();
668 if (Val == 1 || Val == 2 || Val == 3) {
669 AM.Scale = 1 << Val;
Chris Lattner51a26342005-01-11 06:36:20 +0000670 SDOperand ShVal = N.Val->getOperand(0);
671
672 // Okay, we know that we have a scale by now. However, if the scaled
673 // value is an add of something and a constant, we can fold the
674 // constant into the disp field here.
Chris Lattner811482a2005-01-18 04:18:32 +0000675 if (ShVal.Val->getOpcode() == ISD::ADD && ShVal.hasOneUse() &&
Chris Lattner51a26342005-01-11 06:36:20 +0000676 isa<ConstantSDNode>(ShVal.Val->getOperand(1))) {
Chris Lattner98a8ba02005-01-18 01:06:26 +0000677 AM.IndexReg = ShVal.Val->getOperand(0);
Chris Lattner51a26342005-01-11 06:36:20 +0000678 ConstantSDNode *AddVal =
679 cast<ConstantSDNode>(ShVal.Val->getOperand(1));
680 AM.Disp += AddVal->getValue() << Val;
Chris Lattner636e79a2005-01-13 05:53:16 +0000681 } else {
Chris Lattner98a8ba02005-01-18 01:06:26 +0000682 AM.IndexReg = ShVal;
Chris Lattner51a26342005-01-11 06:36:20 +0000683 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000684 return false;
685 }
686 }
687 break;
Chris Lattner947d5442005-01-11 19:37:02 +0000688 case ISD::MUL:
Chris Lattner636e79a2005-01-13 05:53:16 +0000689 // We might have folded the load into this mul, so don't regen the value if
690 // so.
691 if (ExprMap.count(N)) break;
692
Chris Lattner947d5442005-01-11 19:37:02 +0000693 // X*[3,5,9] -> X+X*[2,4,8]
Chris Lattner98a8ba02005-01-18 01:06:26 +0000694 if (AM.IndexReg.Val == 0 && AM.BaseType == X86ISelAddressMode::RegBase &&
695 AM.Base.Reg.Val == 0)
Chris Lattner947d5442005-01-11 19:37:02 +0000696 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1)))
697 if (CN->getValue() == 3 || CN->getValue() == 5 || CN->getValue() == 9) {
698 AM.Scale = unsigned(CN->getValue())-1;
699
700 SDOperand MulVal = N.Val->getOperand(0);
Chris Lattner98a8ba02005-01-18 01:06:26 +0000701 SDOperand Reg;
Chris Lattner947d5442005-01-11 19:37:02 +0000702
703 // Okay, we know that we have a scale by now. However, if the scaled
704 // value is an add of something and a constant, we can fold the
705 // constant into the disp field here.
Chris Lattner811482a2005-01-18 04:18:32 +0000706 if (MulVal.Val->getOpcode() == ISD::ADD && MulVal.hasOneUse() &&
Chris Lattner947d5442005-01-11 19:37:02 +0000707 isa<ConstantSDNode>(MulVal.Val->getOperand(1))) {
Chris Lattner98a8ba02005-01-18 01:06:26 +0000708 Reg = MulVal.Val->getOperand(0);
Chris Lattner947d5442005-01-11 19:37:02 +0000709 ConstantSDNode *AddVal =
710 cast<ConstantSDNode>(MulVal.Val->getOperand(1));
711 AM.Disp += AddVal->getValue() * CN->getValue();
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000712 } else {
Chris Lattner98a8ba02005-01-18 01:06:26 +0000713 Reg = N.Val->getOperand(0);
Chris Lattner947d5442005-01-11 19:37:02 +0000714 }
715
716 AM.IndexReg = AM.Base.Reg = Reg;
717 return false;
718 }
719 break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000720
721 case ISD::ADD: {
Chris Lattner636e79a2005-01-13 05:53:16 +0000722 // We might have folded the load into this mul, so don't regen the value if
723 // so.
724 if (ExprMap.count(N)) break;
725
Chris Lattner98a8ba02005-01-18 01:06:26 +0000726 X86ISelAddressMode Backup = AM;
727 if (!MatchAddress(N.Val->getOperand(0), AM) &&
728 !MatchAddress(N.Val->getOperand(1), AM))
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000729 return false;
730 AM = Backup;
Chris Lattner98a8ba02005-01-18 01:06:26 +0000731 if (!MatchAddress(N.Val->getOperand(1), AM) &&
732 !MatchAddress(N.Val->getOperand(0), AM))
Chris Lattner9bbd9922005-01-12 18:08:53 +0000733 return false;
734 AM = Backup;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000735 break;
736 }
737 }
738
Chris Lattnera95589b2005-01-11 04:40:19 +0000739 // Is the base register already occupied?
Chris Lattner98a8ba02005-01-18 01:06:26 +0000740 if (AM.BaseType != X86ISelAddressMode::RegBase || AM.Base.Reg.Val) {
Chris Lattnera95589b2005-01-11 04:40:19 +0000741 // If so, check to see if the scale index register is set.
Chris Lattner98a8ba02005-01-18 01:06:26 +0000742 if (AM.IndexReg.Val == 0) {
743 AM.IndexReg = N;
Chris Lattnera95589b2005-01-11 04:40:19 +0000744 AM.Scale = 1;
745 return false;
746 }
747
748 // Otherwise, we cannot select it.
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000749 return true;
Chris Lattnera95589b2005-01-11 04:40:19 +0000750 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000751
752 // Default, generate it as a register.
Chris Lattner98a8ba02005-01-18 01:06:26 +0000753 AM.BaseType = X86ISelAddressMode::RegBase;
754 AM.Base.Reg = N;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000755 return false;
756}
757
758/// Emit2SetCCsAndLogical - Emit the following sequence of instructions,
759/// assuming that the temporary registers are in the 8-bit register class.
760///
761/// Tmp1 = setcc1
762/// Tmp2 = setcc2
763/// DestReg = logicalop Tmp1, Tmp2
764///
765static void Emit2SetCCsAndLogical(MachineBasicBlock *BB, unsigned SetCC1,
766 unsigned SetCC2, unsigned LogicalOp,
767 unsigned DestReg) {
768 SSARegMap *RegMap = BB->getParent()->getSSARegMap();
769 unsigned Tmp1 = RegMap->createVirtualRegister(X86::R8RegisterClass);
770 unsigned Tmp2 = RegMap->createVirtualRegister(X86::R8RegisterClass);
771 BuildMI(BB, SetCC1, 0, Tmp1);
772 BuildMI(BB, SetCC2, 0, Tmp2);
773 BuildMI(BB, LogicalOp, 2, DestReg).addReg(Tmp1).addReg(Tmp2);
774}
775
776/// EmitSetCC - Emit the code to set the specified 8-bit register to 1 if the
777/// condition codes match the specified SetCCOpcode. Note that some conditions
778/// require multiple instructions to generate the correct value.
779static void EmitSetCC(MachineBasicBlock *BB, unsigned DestReg,
780 ISD::CondCode SetCCOpcode, bool isFP) {
781 unsigned Opc;
782 if (!isFP) {
783 switch (SetCCOpcode) {
784 default: assert(0 && "Illegal integer SetCC!");
785 case ISD::SETEQ: Opc = X86::SETEr; break;
786 case ISD::SETGT: Opc = X86::SETGr; break;
787 case ISD::SETGE: Opc = X86::SETGEr; break;
788 case ISD::SETLT: Opc = X86::SETLr; break;
789 case ISD::SETLE: Opc = X86::SETLEr; break;
790 case ISD::SETNE: Opc = X86::SETNEr; break;
791 case ISD::SETULT: Opc = X86::SETBr; break;
792 case ISD::SETUGT: Opc = X86::SETAr; break;
793 case ISD::SETULE: Opc = X86::SETBEr; break;
794 case ISD::SETUGE: Opc = X86::SETAEr; break;
795 }
796 } else {
797 // On a floating point condition, the flags are set as follows:
798 // ZF PF CF op
799 // 0 | 0 | 0 | X > Y
800 // 0 | 0 | 1 | X < Y
801 // 1 | 0 | 0 | X == Y
802 // 1 | 1 | 1 | unordered
803 //
804 switch (SetCCOpcode) {
805 default: assert(0 && "Invalid FP setcc!");
806 case ISD::SETUEQ:
807 case ISD::SETEQ:
808 Opc = X86::SETEr; // True if ZF = 1
809 break;
810 case ISD::SETOGT:
811 case ISD::SETGT:
812 Opc = X86::SETAr; // True if CF = 0 and ZF = 0
813 break;
814 case ISD::SETOGE:
815 case ISD::SETGE:
816 Opc = X86::SETAEr; // True if CF = 0
817 break;
818 case ISD::SETULT:
819 case ISD::SETLT:
820 Opc = X86::SETBr; // True if CF = 1
821 break;
822 case ISD::SETULE:
823 case ISD::SETLE:
824 Opc = X86::SETBEr; // True if CF = 1 or ZF = 1
825 break;
826 case ISD::SETONE:
827 case ISD::SETNE:
828 Opc = X86::SETNEr; // True if ZF = 0
829 break;
830 case ISD::SETUO:
831 Opc = X86::SETPr; // True if PF = 1
832 break;
833 case ISD::SETO:
834 Opc = X86::SETNPr; // True if PF = 0
835 break;
836 case ISD::SETOEQ: // !PF & ZF
837 Emit2SetCCsAndLogical(BB, X86::SETNPr, X86::SETEr, X86::AND8rr, DestReg);
838 return;
839 case ISD::SETOLT: // !PF & CF
840 Emit2SetCCsAndLogical(BB, X86::SETNPr, X86::SETBr, X86::AND8rr, DestReg);
841 return;
842 case ISD::SETOLE: // !PF & (CF || ZF)
843 Emit2SetCCsAndLogical(BB, X86::SETNPr, X86::SETBEr, X86::AND8rr, DestReg);
844 return;
845 case ISD::SETUGT: // PF | (!ZF & !CF)
846 Emit2SetCCsAndLogical(BB, X86::SETPr, X86::SETAr, X86::OR8rr, DestReg);
847 return;
848 case ISD::SETUGE: // PF | !CF
849 Emit2SetCCsAndLogical(BB, X86::SETPr, X86::SETAEr, X86::OR8rr, DestReg);
850 return;
851 case ISD::SETUNE: // PF | !ZF
852 Emit2SetCCsAndLogical(BB, X86::SETPr, X86::SETNEr, X86::OR8rr, DestReg);
853 return;
854 }
855 }
856 BuildMI(BB, Opc, 0, DestReg);
857}
858
859
860/// EmitBranchCC - Emit code into BB that arranges for control to transfer to
861/// the Dest block if the Cond condition is true. If we cannot fold this
862/// condition into the branch, return true.
863///
Chris Lattner6c07aee2005-01-11 04:06:27 +0000864bool ISel::EmitBranchCC(MachineBasicBlock *Dest, SDOperand Chain,
865 SDOperand Cond) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000866 // FIXME: Evaluate whether it would be good to emit code like (X < Y) | (A >
867 // B) using two conditional branches instead of one condbr, two setcc's, and
868 // an or.
869 if ((Cond.getOpcode() == ISD::OR ||
870 Cond.getOpcode() == ISD::AND) && Cond.Val->hasOneUse()) {
871 // And and or set the flags for us, so there is no need to emit a TST of the
872 // result. It is only safe to do this if there is only a single use of the
873 // AND/OR though, otherwise we don't know it will be emitted here.
Chris Lattner6c07aee2005-01-11 04:06:27 +0000874 Select(Chain);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000875 SelectExpr(Cond);
876 BuildMI(BB, X86::JNE, 1).addMBB(Dest);
877 return false;
878 }
879
880 // Codegen br not C -> JE.
881 if (Cond.getOpcode() == ISD::XOR)
882 if (ConstantSDNode *NC = dyn_cast<ConstantSDNode>(Cond.Val->getOperand(1)))
883 if (NC->isAllOnesValue()) {
Chris Lattner6c07aee2005-01-11 04:06:27 +0000884 unsigned CondR;
885 if (getRegPressure(Chain) > getRegPressure(Cond)) {
886 Select(Chain);
887 CondR = SelectExpr(Cond.Val->getOperand(0));
888 } else {
889 CondR = SelectExpr(Cond.Val->getOperand(0));
890 Select(Chain);
891 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000892 BuildMI(BB, X86::TEST8rr, 2).addReg(CondR).addReg(CondR);
893 BuildMI(BB, X86::JE, 1).addMBB(Dest);
894 return false;
895 }
896
897 SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Cond);
898 if (SetCC == 0)
899 return true; // Can only handle simple setcc's so far.
900
901 unsigned Opc;
902
903 // Handle integer conditions first.
904 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
905 switch (SetCC->getCondition()) {
906 default: assert(0 && "Illegal integer SetCC!");
907 case ISD::SETEQ: Opc = X86::JE; break;
908 case ISD::SETGT: Opc = X86::JG; break;
909 case ISD::SETGE: Opc = X86::JGE; break;
910 case ISD::SETLT: Opc = X86::JL; break;
911 case ISD::SETLE: Opc = X86::JLE; break;
912 case ISD::SETNE: Opc = X86::JNE; break;
913 case ISD::SETULT: Opc = X86::JB; break;
914 case ISD::SETUGT: Opc = X86::JA; break;
915 case ISD::SETULE: Opc = X86::JBE; break;
916 case ISD::SETUGE: Opc = X86::JAE; break;
917 }
Chris Lattner6c07aee2005-01-11 04:06:27 +0000918 Select(Chain);
Chris Lattnercb1aa8d2005-01-17 01:34:14 +0000919 EmitCMP(SetCC->getOperand(0), SetCC->getOperand(1), SetCC->hasOneUse());
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000920 BuildMI(BB, Opc, 1).addMBB(Dest);
921 return false;
922 }
923
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000924 unsigned Opc2 = 0; // Second branch if needed.
925
926 // On a floating point condition, the flags are set as follows:
927 // ZF PF CF op
928 // 0 | 0 | 0 | X > Y
929 // 0 | 0 | 1 | X < Y
930 // 1 | 0 | 0 | X == Y
931 // 1 | 1 | 1 | unordered
932 //
933 switch (SetCC->getCondition()) {
934 default: assert(0 && "Invalid FP setcc!");
935 case ISD::SETUEQ:
936 case ISD::SETEQ: Opc = X86::JE; break; // True if ZF = 1
937 case ISD::SETOGT:
938 case ISD::SETGT: Opc = X86::JA; break; // True if CF = 0 and ZF = 0
939 case ISD::SETOGE:
940 case ISD::SETGE: Opc = X86::JAE; break; // True if CF = 0
941 case ISD::SETULT:
942 case ISD::SETLT: Opc = X86::JB; break; // True if CF = 1
943 case ISD::SETULE:
944 case ISD::SETLE: Opc = X86::JBE; break; // True if CF = 1 or ZF = 1
945 case ISD::SETONE:
946 case ISD::SETNE: Opc = X86::JNE; break; // True if ZF = 0
947 case ISD::SETUO: Opc = X86::JP; break; // True if PF = 1
948 case ISD::SETO: Opc = X86::JNP; break; // True if PF = 0
949 case ISD::SETUGT: // PF = 1 | (ZF = 0 & CF = 0)
950 Opc = X86::JA; // ZF = 0 & CF = 0
951 Opc2 = X86::JP; // PF = 1
952 break;
953 case ISD::SETUGE: // PF = 1 | CF = 0
954 Opc = X86::JAE; // CF = 0
955 Opc2 = X86::JP; // PF = 1
956 break;
957 case ISD::SETUNE: // PF = 1 | ZF = 0
958 Opc = X86::JNE; // ZF = 0
959 Opc2 = X86::JP; // PF = 1
960 break;
961 case ISD::SETOEQ: // PF = 0 & ZF = 1
962 //X86::JNP, X86::JE
963 //X86::AND8rr
964 return true; // FIXME: Emit more efficient code for this branch.
965 case ISD::SETOLT: // PF = 0 & CF = 1
966 //X86::JNP, X86::JB
967 //X86::AND8rr
968 return true; // FIXME: Emit more efficient code for this branch.
969 case ISD::SETOLE: // PF = 0 & (CF = 1 || ZF = 1)
970 //X86::JNP, X86::JBE
971 //X86::AND8rr
972 return true; // FIXME: Emit more efficient code for this branch.
973 }
974
Chris Lattner6c07aee2005-01-11 04:06:27 +0000975 Select(Chain);
Chris Lattnercb1aa8d2005-01-17 01:34:14 +0000976 EmitCMP(SetCC->getOperand(0), SetCC->getOperand(1), SetCC->hasOneUse());
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000977 BuildMI(BB, Opc, 1).addMBB(Dest);
978 if (Opc2)
979 BuildMI(BB, Opc2, 1).addMBB(Dest);
980 return false;
981}
982
Chris Lattner24aad1b2005-01-10 22:10:13 +0000983/// EmitSelectCC - Emit code into BB that performs a select operation between
984/// the two registers RTrue and RFalse, generating a result into RDest. Return
985/// true if the fold cannot be performed.
986///
987void ISel::EmitSelectCC(SDOperand Cond, MVT::ValueType SVT,
988 unsigned RTrue, unsigned RFalse, unsigned RDest) {
989 enum Condition {
990 EQ, NE, LT, LE, GT, GE, B, BE, A, AE, P, NP,
991 NOT_SET
992 } CondCode = NOT_SET;
993
994 static const unsigned CMOVTAB16[] = {
995 X86::CMOVE16rr, X86::CMOVNE16rr, X86::CMOVL16rr, X86::CMOVLE16rr,
996 X86::CMOVG16rr, X86::CMOVGE16rr, X86::CMOVB16rr, X86::CMOVBE16rr,
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000997 X86::CMOVA16rr, X86::CMOVAE16rr, X86::CMOVP16rr, X86::CMOVNP16rr,
Chris Lattner24aad1b2005-01-10 22:10:13 +0000998 };
999 static const unsigned CMOVTAB32[] = {
1000 X86::CMOVE32rr, X86::CMOVNE32rr, X86::CMOVL32rr, X86::CMOVLE32rr,
1001 X86::CMOVG32rr, X86::CMOVGE32rr, X86::CMOVB32rr, X86::CMOVBE32rr,
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001002 X86::CMOVA32rr, X86::CMOVAE32rr, X86::CMOVP32rr, X86::CMOVNP32rr,
Chris Lattner24aad1b2005-01-10 22:10:13 +00001003 };
1004 static const unsigned CMOVTABFP[] = {
1005 X86::FCMOVE , X86::FCMOVNE, /*missing*/0, /*missing*/0,
1006 /*missing*/0, /*missing*/0, X86::FCMOVB , X86::FCMOVBE,
1007 X86::FCMOVA , X86::FCMOVAE, X86::FCMOVP , X86::FCMOVNP
1008 };
1009
1010 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Cond)) {
1011 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
1012 switch (SetCC->getCondition()) {
1013 default: assert(0 && "Unknown integer comparison!");
1014 case ISD::SETEQ: CondCode = EQ; break;
1015 case ISD::SETGT: CondCode = GT; break;
1016 case ISD::SETGE: CondCode = GE; break;
1017 case ISD::SETLT: CondCode = LT; break;
1018 case ISD::SETLE: CondCode = LE; break;
1019 case ISD::SETNE: CondCode = NE; break;
1020 case ISD::SETULT: CondCode = B; break;
1021 case ISD::SETUGT: CondCode = A; break;
1022 case ISD::SETULE: CondCode = BE; break;
1023 case ISD::SETUGE: CondCode = AE; break;
1024 }
1025 } else {
1026 // On a floating point condition, the flags are set as follows:
1027 // ZF PF CF op
1028 // 0 | 0 | 0 | X > Y
1029 // 0 | 0 | 1 | X < Y
1030 // 1 | 0 | 0 | X == Y
1031 // 1 | 1 | 1 | unordered
1032 //
1033 switch (SetCC->getCondition()) {
1034 default: assert(0 && "Unknown FP comparison!");
1035 case ISD::SETUEQ:
1036 case ISD::SETEQ: CondCode = EQ; break; // True if ZF = 1
1037 case ISD::SETOGT:
1038 case ISD::SETGT: CondCode = A; break; // True if CF = 0 and ZF = 0
1039 case ISD::SETOGE:
1040 case ISD::SETGE: CondCode = AE; break; // True if CF = 0
1041 case ISD::SETULT:
1042 case ISD::SETLT: CondCode = B; break; // True if CF = 1
1043 case ISD::SETULE:
1044 case ISD::SETLE: CondCode = BE; break; // True if CF = 1 or ZF = 1
1045 case ISD::SETONE:
1046 case ISD::SETNE: CondCode = NE; break; // True if ZF = 0
1047 case ISD::SETUO: CondCode = P; break; // True if PF = 1
1048 case ISD::SETO: CondCode = NP; break; // True if PF = 0
1049 case ISD::SETUGT: // PF = 1 | (ZF = 0 & CF = 0)
1050 case ISD::SETUGE: // PF = 1 | CF = 0
1051 case ISD::SETUNE: // PF = 1 | ZF = 0
1052 case ISD::SETOEQ: // PF = 0 & ZF = 1
1053 case ISD::SETOLT: // PF = 0 & CF = 1
1054 case ISD::SETOLE: // PF = 0 & (CF = 1 || ZF = 1)
1055 // We cannot emit this comparison as a single cmov.
1056 break;
1057 }
1058 }
1059 }
1060
1061 unsigned Opc = 0;
1062 if (CondCode != NOT_SET) {
1063 switch (SVT) {
1064 default: assert(0 && "Cannot select this type!");
1065 case MVT::i16: Opc = CMOVTAB16[CondCode]; break;
1066 case MVT::i32: Opc = CMOVTAB32[CondCode]; break;
Chris Lattneref7ba072005-01-11 03:50:45 +00001067 case MVT::f64: Opc = CMOVTABFP[CondCode]; break;
Chris Lattner24aad1b2005-01-10 22:10:13 +00001068 }
1069 }
1070
1071 // Finally, if we weren't able to fold this, just emit the condition and test
1072 // it.
1073 if (CondCode == NOT_SET || Opc == 0) {
1074 // Get the condition into the zero flag.
1075 unsigned CondReg = SelectExpr(Cond);
1076 BuildMI(BB, X86::TEST8rr, 2).addReg(CondReg).addReg(CondReg);
1077
1078 switch (SVT) {
1079 default: assert(0 && "Cannot select this type!");
1080 case MVT::i16: Opc = X86::CMOVE16rr; break;
1081 case MVT::i32: Opc = X86::CMOVE32rr; break;
Chris Lattneref7ba072005-01-11 03:50:45 +00001082 case MVT::f64: Opc = X86::FCMOVE; break;
Chris Lattner24aad1b2005-01-10 22:10:13 +00001083 }
1084 } else {
1085 // FIXME: CMP R, 0 -> TEST R, R
Chris Lattnercb1aa8d2005-01-17 01:34:14 +00001086 EmitCMP(Cond.getOperand(0), Cond.getOperand(1), Cond.Val->hasOneUse());
Chris Lattnera3aa2e22005-01-11 03:37:59 +00001087 std::swap(RTrue, RFalse);
Chris Lattner24aad1b2005-01-10 22:10:13 +00001088 }
1089 BuildMI(BB, Opc, 2, RDest).addReg(RTrue).addReg(RFalse);
1090}
1091
Chris Lattnercb1aa8d2005-01-17 01:34:14 +00001092void ISel::EmitCMP(SDOperand LHS, SDOperand RHS, bool HasOneUse) {
Chris Lattner11333092005-01-11 03:11:44 +00001093 unsigned Opc;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001094 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(RHS)) {
1095 Opc = 0;
Chris Lattner4ff348b2005-01-17 06:26:58 +00001096 if (HasOneUse && isFoldableLoad(LHS, RHS)) {
Chris Lattneref6806c2005-01-12 02:02:48 +00001097 switch (RHS.getValueType()) {
1098 default: break;
1099 case MVT::i1:
1100 case MVT::i8: Opc = X86::CMP8mi; break;
1101 case MVT::i16: Opc = X86::CMP16mi; break;
1102 case MVT::i32: Opc = X86::CMP32mi; break;
1103 }
1104 if (Opc) {
1105 X86AddressMode AM;
1106 EmitFoldedLoad(LHS, AM);
1107 addFullAddress(BuildMI(BB, Opc, 5), AM).addImm(CN->getValue());
1108 return;
1109 }
1110 }
1111
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001112 switch (RHS.getValueType()) {
1113 default: break;
1114 case MVT::i1:
1115 case MVT::i8: Opc = X86::CMP8ri; break;
1116 case MVT::i16: Opc = X86::CMP16ri; break;
1117 case MVT::i32: Opc = X86::CMP32ri; break;
1118 }
1119 if (Opc) {
Chris Lattner11333092005-01-11 03:11:44 +00001120 unsigned Tmp1 = SelectExpr(LHS);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001121 BuildMI(BB, Opc, 2).addReg(Tmp1).addImm(CN->getValue());
1122 return;
1123 }
Chris Lattner7f2afac2005-01-14 22:37:41 +00001124 } else if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(RHS)) {
1125 if (CN->isExactlyValue(+0.0) ||
1126 CN->isExactlyValue(-0.0)) {
1127 unsigned Reg = SelectExpr(LHS);
1128 BuildMI(BB, X86::FTST, 1).addReg(Reg);
1129 BuildMI(BB, X86::FNSTSW8r, 0);
1130 BuildMI(BB, X86::SAHF, 1);
Chris Lattner7805fa42005-03-17 16:29:26 +00001131 return;
Chris Lattner7f2afac2005-01-14 22:37:41 +00001132 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001133 }
1134
Chris Lattneref6806c2005-01-12 02:02:48 +00001135 Opc = 0;
Chris Lattner4ff348b2005-01-17 06:26:58 +00001136 if (HasOneUse && isFoldableLoad(LHS, RHS)) {
Chris Lattneref6806c2005-01-12 02:02:48 +00001137 switch (RHS.getValueType()) {
1138 default: break;
1139 case MVT::i1:
1140 case MVT::i8: Opc = X86::CMP8mr; break;
1141 case MVT::i16: Opc = X86::CMP16mr; break;
1142 case MVT::i32: Opc = X86::CMP32mr; break;
1143 }
1144 if (Opc) {
1145 X86AddressMode AM;
Chris Lattner636e79a2005-01-13 05:53:16 +00001146 EmitFoldedLoad(LHS, AM);
1147 unsigned Reg = SelectExpr(RHS);
Chris Lattneref6806c2005-01-12 02:02:48 +00001148 addFullAddress(BuildMI(BB, Opc, 5), AM).addReg(Reg);
1149 return;
1150 }
1151 }
1152
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001153 switch (LHS.getValueType()) {
1154 default: assert(0 && "Cannot compare this value!");
1155 case MVT::i1:
1156 case MVT::i8: Opc = X86::CMP8rr; break;
1157 case MVT::i16: Opc = X86::CMP16rr; break;
1158 case MVT::i32: Opc = X86::CMP32rr; break;
Chris Lattneref7ba072005-01-11 03:50:45 +00001159 case MVT::f64: Opc = X86::FUCOMIr; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001160 }
Chris Lattner11333092005-01-11 03:11:44 +00001161 unsigned Tmp1, Tmp2;
1162 if (getRegPressure(LHS) > getRegPressure(RHS)) {
1163 Tmp1 = SelectExpr(LHS);
1164 Tmp2 = SelectExpr(RHS);
1165 } else {
1166 Tmp2 = SelectExpr(RHS);
1167 Tmp1 = SelectExpr(LHS);
1168 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001169 BuildMI(BB, Opc, 2).addReg(Tmp1).addReg(Tmp2);
1170}
1171
Chris Lattnera5ade062005-01-11 21:19:59 +00001172/// isFoldableLoad - Return true if this is a load instruction that can safely
1173/// be folded into an operation that uses it.
Chris Lattner44129b52005-01-25 20:03:11 +00001174bool ISel::isFoldableLoad(SDOperand Op, SDOperand OtherOp, bool FloatPromoteOk){
1175 if (Op.getOpcode() == ISD::LOAD) {
1176 // FIXME: currently can't fold constant pool indexes.
1177 if (isa<ConstantPoolSDNode>(Op.getOperand(1)))
1178 return false;
1179 } else if (FloatPromoteOk && Op.getOpcode() == ISD::EXTLOAD &&
1180 cast<MVTSDNode>(Op)->getExtraValueType() == MVT::f32) {
1181 // FIXME: currently can't fold constant pool indexes.
1182 if (isa<ConstantPoolSDNode>(Op.getOperand(1)))
1183 return false;
1184 } else {
Chris Lattnera5ade062005-01-11 21:19:59 +00001185 return false;
Chris Lattner44129b52005-01-25 20:03:11 +00001186 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001187
1188 // If this load has already been emitted, we clearly can't fold it.
Chris Lattner636e79a2005-01-13 05:53:16 +00001189 assert(Op.ResNo == 0 && "Not a use of the value of the load?");
1190 if (ExprMap.count(Op.getValue(1))) return false;
1191 assert(!ExprMap.count(Op.getValue(0)) && "Value in map but not token chain?");
Chris Lattner4a108662005-01-18 03:51:59 +00001192 assert(!ExprMap.count(Op.getValue(1))&&"Token lowered but value not in map?");
Chris Lattnera5ade062005-01-11 21:19:59 +00001193
Chris Lattner4ff348b2005-01-17 06:26:58 +00001194 // If there is not just one use of its value, we cannot fold.
1195 if (!Op.Val->hasNUsesOfValue(1, 0)) return false;
1196
1197 // Finally, we cannot fold the load into the operation if this would induce a
1198 // cycle into the resultant dag. To check for this, see if OtherOp (the other
1199 // operand of the operation we are folding the load into) can possible use the
1200 // chain node defined by the load.
1201 if (OtherOp.Val && !Op.Val->hasNUsesOfValue(0, 1)) { // Has uses of chain?
1202 std::set<SDNode*> Visited;
1203 if (NodeTransitivelyUsesValue(OtherOp, Op.getValue(1), Visited))
1204 return false;
1205 }
1206 return true;
Chris Lattnera5ade062005-01-11 21:19:59 +00001207}
1208
Chris Lattner4ff348b2005-01-17 06:26:58 +00001209
Chris Lattnera5ade062005-01-11 21:19:59 +00001210/// EmitFoldedLoad - Ensure that the arguments of the load are code generated,
1211/// and compute the address being loaded into AM.
1212void ISel::EmitFoldedLoad(SDOperand Op, X86AddressMode &AM) {
1213 SDOperand Chain = Op.getOperand(0);
1214 SDOperand Address = Op.getOperand(1);
Chris Lattner98a8ba02005-01-18 01:06:26 +00001215
Chris Lattnera5ade062005-01-11 21:19:59 +00001216 if (getRegPressure(Chain) > getRegPressure(Address)) {
1217 Select(Chain);
1218 SelectAddress(Address, AM);
1219 } else {
1220 SelectAddress(Address, AM);
1221 Select(Chain);
1222 }
1223
1224 // The chain for this load is now lowered.
Chris Lattner636e79a2005-01-13 05:53:16 +00001225 assert(ExprMap.count(SDOperand(Op.Val, 1)) == 0 &&
1226 "Load emitted more than once?");
Chris Lattner4a108662005-01-18 03:51:59 +00001227 if (!ExprMap.insert(std::make_pair(Op.getValue(1), 1)).second)
Chris Lattner636e79a2005-01-13 05:53:16 +00001228 assert(0 && "Load emitted more than once!");
Chris Lattnera5ade062005-01-11 21:19:59 +00001229}
1230
Chris Lattner30ea1e92005-01-19 07:37:26 +00001231// EmitOrOpOp - Pattern match the expression (Op1|Op2), where we know that op1
1232// and op2 are i8/i16/i32 values with one use each (the or). If we can form a
1233// SHLD or SHRD, emit the instruction (generating the value into DestReg) and
1234// return true.
1235bool ISel::EmitOrOpOp(SDOperand Op1, SDOperand Op2, unsigned DestReg) {
Chris Lattner85716372005-01-19 06:18:43 +00001236 if (Op1.getOpcode() == ISD::SHL && Op2.getOpcode() == ISD::SRL) {
1237 // good!
1238 } else if (Op2.getOpcode() == ISD::SHL && Op1.getOpcode() == ISD::SRL) {
1239 std::swap(Op1, Op2); // Op1 is the SHL now.
1240 } else {
1241 return false; // No match
1242 }
1243
1244 SDOperand ShlVal = Op1.getOperand(0);
1245 SDOperand ShlAmt = Op1.getOperand(1);
1246 SDOperand ShrVal = Op2.getOperand(0);
1247 SDOperand ShrAmt = Op2.getOperand(1);
1248
Chris Lattner30ea1e92005-01-19 07:37:26 +00001249 unsigned RegSize = MVT::getSizeInBits(Op1.getValueType());
1250
Chris Lattner85716372005-01-19 06:18:43 +00001251 // Find out if ShrAmt = 32-ShlAmt or ShlAmt = 32-ShrAmt.
1252 if (ShlAmt.getOpcode() == ISD::SUB && ShlAmt.getOperand(1) == ShrAmt)
1253 if (ConstantSDNode *SubCST = dyn_cast<ConstantSDNode>(ShlAmt.getOperand(0)))
Chris Lattner4053b1e2005-01-19 08:07:05 +00001254 if (SubCST->getValue() == RegSize) {
1255 // (A >> ShrAmt) | (A << (32-ShrAmt)) ==> ROR A, ShrAmt
Chris Lattner85716372005-01-19 06:18:43 +00001256 // (A >> ShrAmt) | (B << (32-ShrAmt)) ==> SHRD A, B, ShrAmt
Chris Lattner4053b1e2005-01-19 08:07:05 +00001257 if (ShrVal == ShlVal) {
1258 unsigned Reg, ShAmt;
1259 if (getRegPressure(ShrVal) > getRegPressure(ShrAmt)) {
1260 Reg = SelectExpr(ShrVal);
1261 ShAmt = SelectExpr(ShrAmt);
1262 } else {
1263 ShAmt = SelectExpr(ShrAmt);
1264 Reg = SelectExpr(ShrVal);
1265 }
1266 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShAmt);
1267 unsigned Opc = RegSize == 8 ? X86::ROR8rCL :
1268 (RegSize == 16 ? X86::ROR16rCL : X86::ROR32rCL);
1269 BuildMI(BB, Opc, 1, DestReg).addReg(Reg);
1270 return true;
1271 } else if (RegSize != 8) {
Chris Lattner85716372005-01-19 06:18:43 +00001272 unsigned AReg, BReg;
1273 if (getRegPressure(ShlVal) > getRegPressure(ShrVal)) {
Chris Lattner85716372005-01-19 06:18:43 +00001274 BReg = SelectExpr(ShlVal);
Chris Lattnerc3c021b2005-01-19 17:24:34 +00001275 AReg = SelectExpr(ShrVal);
Chris Lattner85716372005-01-19 06:18:43 +00001276 } else {
Chris Lattner85716372005-01-19 06:18:43 +00001277 AReg = SelectExpr(ShrVal);
Chris Lattnerc3c021b2005-01-19 17:24:34 +00001278 BReg = SelectExpr(ShlVal);
Chris Lattner85716372005-01-19 06:18:43 +00001279 }
Chris Lattner4053b1e2005-01-19 08:07:05 +00001280 unsigned ShAmt = SelectExpr(ShrAmt);
1281 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShAmt);
1282 unsigned Opc = RegSize == 16 ? X86::SHRD16rrCL : X86::SHRD32rrCL;
1283 BuildMI(BB, Opc, 2, DestReg).addReg(AReg).addReg(BReg);
Chris Lattner85716372005-01-19 06:18:43 +00001284 return true;
1285 }
1286 }
1287
Chris Lattner4053b1e2005-01-19 08:07:05 +00001288 if (ShrAmt.getOpcode() == ISD::SUB && ShrAmt.getOperand(1) == ShlAmt)
1289 if (ConstantSDNode *SubCST = dyn_cast<ConstantSDNode>(ShrAmt.getOperand(0)))
1290 if (SubCST->getValue() == RegSize) {
1291 // (A << ShlAmt) | (A >> (32-ShlAmt)) ==> ROL A, ShrAmt
1292 // (A << ShlAmt) | (B >> (32-ShlAmt)) ==> SHLD A, B, ShrAmt
1293 if (ShrVal == ShlVal) {
1294 unsigned Reg, ShAmt;
1295 if (getRegPressure(ShrVal) > getRegPressure(ShlAmt)) {
1296 Reg = SelectExpr(ShrVal);
1297 ShAmt = SelectExpr(ShlAmt);
1298 } else {
1299 ShAmt = SelectExpr(ShlAmt);
1300 Reg = SelectExpr(ShrVal);
1301 }
1302 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShAmt);
1303 unsigned Opc = RegSize == 8 ? X86::ROL8rCL :
1304 (RegSize == 16 ? X86::ROL16rCL : X86::ROL32rCL);
1305 BuildMI(BB, Opc, 1, DestReg).addReg(Reg);
1306 return true;
1307 } else if (RegSize != 8) {
1308 unsigned AReg, BReg;
1309 if (getRegPressure(ShlVal) > getRegPressure(ShrVal)) {
Chris Lattnerc3c021b2005-01-19 17:24:34 +00001310 AReg = SelectExpr(ShlVal);
1311 BReg = SelectExpr(ShrVal);
Chris Lattner4053b1e2005-01-19 08:07:05 +00001312 } else {
Chris Lattnerc3c021b2005-01-19 17:24:34 +00001313 BReg = SelectExpr(ShrVal);
1314 AReg = SelectExpr(ShlVal);
Chris Lattner4053b1e2005-01-19 08:07:05 +00001315 }
1316 unsigned ShAmt = SelectExpr(ShlAmt);
1317 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShAmt);
1318 unsigned Opc = RegSize == 16 ? X86::SHLD16rrCL : X86::SHLD32rrCL;
1319 BuildMI(BB, Opc, 2, DestReg).addReg(AReg).addReg(BReg);
1320 return true;
1321 }
1322 }
Chris Lattner85716372005-01-19 06:18:43 +00001323
Chris Lattner4053b1e2005-01-19 08:07:05 +00001324 if (ConstantSDNode *ShrCst = dyn_cast<ConstantSDNode>(ShrAmt))
1325 if (ConstantSDNode *ShlCst = dyn_cast<ConstantSDNode>(ShlAmt))
1326 if (ShrCst->getValue() < RegSize && ShlCst->getValue() < RegSize)
1327 if (ShrCst->getValue() == RegSize-ShlCst->getValue()) {
1328 // (A >> 5) | (A << 27) --> ROR A, 5
1329 // (A >> 5) | (B << 27) --> SHRD A, B, 5
1330 if (ShrVal == ShlVal) {
1331 unsigned Reg = SelectExpr(ShrVal);
1332 unsigned Opc = RegSize == 8 ? X86::ROR8ri :
1333 (RegSize == 16 ? X86::ROR16ri : X86::ROR32ri);
1334 BuildMI(BB, Opc, 2, DestReg).addReg(Reg).addImm(ShrCst->getValue());
1335 return true;
1336 } else if (RegSize != 8) {
1337 unsigned AReg, BReg;
1338 if (getRegPressure(ShlVal) > getRegPressure(ShrVal)) {
Chris Lattner4053b1e2005-01-19 08:07:05 +00001339 BReg = SelectExpr(ShlVal);
Chris Lattnerc3c021b2005-01-19 17:24:34 +00001340 AReg = SelectExpr(ShrVal);
Chris Lattner4053b1e2005-01-19 08:07:05 +00001341 } else {
Chris Lattner4053b1e2005-01-19 08:07:05 +00001342 AReg = SelectExpr(ShrVal);
Chris Lattnerc3c021b2005-01-19 17:24:34 +00001343 BReg = SelectExpr(ShlVal);
Chris Lattner4053b1e2005-01-19 08:07:05 +00001344 }
1345 unsigned Opc = RegSize == 16 ? X86::SHRD16rri8 : X86::SHRD32rri8;
1346 BuildMI(BB, Opc, 3, DestReg).addReg(AReg).addReg(BReg)
1347 .addImm(ShrCst->getValue());
1348 return true;
1349 }
1350 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001351
Chris Lattner85716372005-01-19 06:18:43 +00001352 return false;
1353}
1354
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001355unsigned ISel::SelectExpr(SDOperand N) {
1356 unsigned Result;
1357 unsigned Tmp1, Tmp2, Tmp3;
1358 unsigned Opc = 0;
Chris Lattner5188ad72005-01-08 19:28:19 +00001359 SDNode *Node = N.Val;
Chris Lattnera5ade062005-01-11 21:19:59 +00001360 SDOperand Op0, Op1;
Chris Lattner5188ad72005-01-08 19:28:19 +00001361
Chris Lattner7f2afac2005-01-14 22:37:41 +00001362 if (Node->getOpcode() == ISD::CopyFromReg) {
1363 // FIXME: Handle copy from physregs!
1364
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001365 // Just use the specified register as our input.
Chris Lattner18c2f132005-01-13 20:50:02 +00001366 return dyn_cast<RegSDNode>(Node)->getReg();
Chris Lattner7f2afac2005-01-14 22:37:41 +00001367 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001368
Chris Lattnera5ade062005-01-11 21:19:59 +00001369 unsigned &Reg = ExprMap[N];
1370 if (Reg) return Reg;
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001371
Chris Lattnerb38a7492005-04-02 04:01:14 +00001372 switch (N.getOpcode()) {
1373 default:
Chris Lattnera5ade062005-01-11 21:19:59 +00001374 Reg = Result = (N.getValueType() != MVT::Other) ?
Chris Lattnerb38a7492005-04-02 04:01:14 +00001375 MakeReg(N.getValueType()) : 1;
1376 break;
1377 case ISD::CALL:
Chris Lattnera5ade062005-01-11 21:19:59 +00001378 // If this is a call instruction, make sure to prepare ALL of the result
1379 // values as well as the chain.
Chris Lattnerb38a7492005-04-02 04:01:14 +00001380 if (Node->getNumValues() == 1)
1381 Reg = Result = 1; // Void call, just a chain.
1382 else {
Chris Lattnera5ade062005-01-11 21:19:59 +00001383 Result = MakeReg(Node->getValueType(0));
1384 ExprMap[N.getValue(0)] = Result;
Chris Lattnerb38a7492005-04-02 04:01:14 +00001385 for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
Chris Lattnera5ade062005-01-11 21:19:59 +00001386 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
Chris Lattnerb38a7492005-04-02 04:01:14 +00001387 ExprMap[SDOperand(Node, Node->getNumValues()-1)] = 1;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001388 }
Chris Lattnerb38a7492005-04-02 04:01:14 +00001389 break;
1390 case ISD::ADD_PARTS:
1391 case ISD::SUB_PARTS:
1392 case ISD::SHL_PARTS:
1393 case ISD::SRL_PARTS:
1394 case ISD::SRA_PARTS:
1395 Result = MakeReg(Node->getValueType(0));
1396 ExprMap[N.getValue(0)] = Result;
1397 for (unsigned i = 1, e = N.Val->getNumValues(); i != e; ++i)
1398 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
1399 break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001400 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001401
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001402 switch (N.getOpcode()) {
1403 default:
Chris Lattner5188ad72005-01-08 19:28:19 +00001404 Node->dump();
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001405 assert(0 && "Node not handled!\n");
1406 case ISD::FrameIndex:
1407 Tmp1 = cast<FrameIndexSDNode>(N)->getIndex();
1408 addFrameReference(BuildMI(BB, X86::LEA32r, 4, Result), (int)Tmp1);
1409 return Result;
1410 case ISD::ConstantPool:
1411 Tmp1 = cast<ConstantPoolSDNode>(N)->getIndex();
1412 addConstantPoolReference(BuildMI(BB, X86::LEA32r, 4, Result), Tmp1);
1413 return Result;
1414 case ISD::ConstantFP:
1415 ContainsFPCode = true;
1416 Tmp1 = Result; // Intermediate Register
1417 if (cast<ConstantFPSDNode>(N)->getValue() < 0.0 ||
1418 cast<ConstantFPSDNode>(N)->isExactlyValue(-0.0))
1419 Tmp1 = MakeReg(MVT::f64);
1420
1421 if (cast<ConstantFPSDNode>(N)->isExactlyValue(+0.0) ||
1422 cast<ConstantFPSDNode>(N)->isExactlyValue(-0.0))
1423 BuildMI(BB, X86::FLD0, 0, Tmp1);
1424 else if (cast<ConstantFPSDNode>(N)->isExactlyValue(+1.0) ||
1425 cast<ConstantFPSDNode>(N)->isExactlyValue(-1.0))
1426 BuildMI(BB, X86::FLD1, 0, Tmp1);
1427 else
1428 assert(0 && "Unexpected constant!");
1429 if (Tmp1 != Result)
1430 BuildMI(BB, X86::FCHS, 1, Result).addReg(Tmp1);
1431 return Result;
1432 case ISD::Constant:
1433 switch (N.getValueType()) {
1434 default: assert(0 && "Cannot use constants of this type!");
1435 case MVT::i1:
1436 case MVT::i8: Opc = X86::MOV8ri; break;
1437 case MVT::i16: Opc = X86::MOV16ri; break;
1438 case MVT::i32: Opc = X86::MOV32ri; break;
1439 }
1440 BuildMI(BB, Opc, 1,Result).addImm(cast<ConstantSDNode>(N)->getValue());
1441 return Result;
Chris Lattner7ce7eff2005-04-01 22:46:45 +00001442 case ISD::UNDEF:
1443 if (Node->getValueType(0) == MVT::f64) {
1444 // FIXME: SHOULD TEACH STACKIFIER ABOUT UNDEF VALUES!
1445 BuildMI(BB, X86::FLD0, 0, Result);
1446 } else {
1447 BuildMI(BB, X86::IMPLICIT_DEF, 0, Result);
1448 }
1449 return Result;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001450 case ISD::GlobalAddress: {
1451 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
1452 BuildMI(BB, X86::MOV32ri, 1, Result).addGlobalAddress(GV);
1453 return Result;
1454 }
1455 case ISD::ExternalSymbol: {
1456 const char *Sym = cast<ExternalSymbolSDNode>(N)->getSymbol();
1457 BuildMI(BB, X86::MOV32ri, 1, Result).addExternalSymbol(Sym);
1458 return Result;
1459 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001460 case ISD::ZERO_EXTEND: {
1461 int DestIs16 = N.getValueType() == MVT::i16;
1462 int SrcIs16 = N.getOperand(0).getValueType() == MVT::i16;
Chris Lattner590d8002005-01-09 18:52:44 +00001463
1464 // FIXME: This hack is here for zero extension casts from bool to i8. This
1465 // would not be needed if bools were promoted by Legalize.
1466 if (N.getValueType() == MVT::i8) {
Chris Lattnerdbba22f2005-01-11 23:33:00 +00001467 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner590d8002005-01-09 18:52:44 +00001468 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(Tmp1);
1469 return Result;
1470 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001471
Chris Lattner4ff348b2005-01-17 06:26:58 +00001472 if (isFoldableLoad(N.getOperand(0), SDOperand())) {
Chris Lattnerdbba22f2005-01-11 23:33:00 +00001473 static const unsigned Opc[3] = {
1474 X86::MOVZX32rm8, X86::MOVZX32rm16, X86::MOVZX16rm8
1475 };
1476
1477 X86AddressMode AM;
1478 EmitFoldedLoad(N.getOperand(0), AM);
1479 addFullAddress(BuildMI(BB, Opc[SrcIs16+DestIs16*2], 4, Result), AM);
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001480
Chris Lattnerdbba22f2005-01-11 23:33:00 +00001481 return Result;
1482 }
1483
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001484 static const unsigned Opc[3] = {
1485 X86::MOVZX32rr8, X86::MOVZX32rr16, X86::MOVZX16rr8
1486 };
Chris Lattnerdbba22f2005-01-11 23:33:00 +00001487 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001488 BuildMI(BB, Opc[SrcIs16+DestIs16*2], 1, Result).addReg(Tmp1);
1489 return Result;
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001490 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001491 case ISD::SIGN_EXTEND: {
1492 int DestIs16 = N.getValueType() == MVT::i16;
1493 int SrcIs16 = N.getOperand(0).getValueType() == MVT::i16;
1494
Chris Lattner590d8002005-01-09 18:52:44 +00001495 // FIXME: Legalize should promote bools to i8!
1496 assert(N.getOperand(0).getValueType() != MVT::i1 &&
1497 "Sign extend from bool not implemented!");
1498
Chris Lattner4ff348b2005-01-17 06:26:58 +00001499 if (isFoldableLoad(N.getOperand(0), SDOperand())) {
Chris Lattnerdbba22f2005-01-11 23:33:00 +00001500 static const unsigned Opc[3] = {
1501 X86::MOVSX32rm8, X86::MOVSX32rm16, X86::MOVSX16rm8
1502 };
1503
1504 X86AddressMode AM;
1505 EmitFoldedLoad(N.getOperand(0), AM);
1506 addFullAddress(BuildMI(BB, Opc[SrcIs16+DestIs16*2], 4, Result), AM);
1507 return Result;
1508 }
1509
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001510 static const unsigned Opc[3] = {
1511 X86::MOVSX32rr8, X86::MOVSX32rr16, X86::MOVSX16rr8
1512 };
1513 Tmp1 = SelectExpr(N.getOperand(0));
1514 BuildMI(BB, Opc[SrcIs16+DestIs16*2], 1, Result).addReg(Tmp1);
1515 return Result;
1516 }
1517 case ISD::TRUNCATE:
Chris Lattnerafce4302005-01-12 02:19:06 +00001518 // Fold TRUNCATE (LOAD P) into a smaller load from P.
Chris Lattner477c9312005-01-18 20:05:56 +00001519 // FIXME: This should be performed by the DAGCombiner.
Chris Lattner4ff348b2005-01-17 06:26:58 +00001520 if (isFoldableLoad(N.getOperand(0), SDOperand())) {
Chris Lattnerafce4302005-01-12 02:19:06 +00001521 switch (N.getValueType()) {
1522 default: assert(0 && "Unknown truncate!");
1523 case MVT::i1:
1524 case MVT::i8: Opc = X86::MOV8rm; break;
1525 case MVT::i16: Opc = X86::MOV16rm; break;
1526 }
1527 X86AddressMode AM;
1528 EmitFoldedLoad(N.getOperand(0), AM);
1529 addFullAddress(BuildMI(BB, Opc, 4, Result), AM);
1530 return Result;
1531 }
1532
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001533 // Handle cast of LARGER int to SMALLER int using a move to EAX followed by
1534 // a move out of AX or AL.
1535 switch (N.getOperand(0).getValueType()) {
1536 default: assert(0 && "Unknown truncate!");
1537 case MVT::i8: Tmp2 = X86::AL; Opc = X86::MOV8rr; break;
1538 case MVT::i16: Tmp2 = X86::AX; Opc = X86::MOV16rr; break;
1539 case MVT::i32: Tmp2 = X86::EAX; Opc = X86::MOV32rr; break;
1540 }
1541 Tmp1 = SelectExpr(N.getOperand(0));
1542 BuildMI(BB, Opc, 1, Tmp2).addReg(Tmp1);
1543
1544 switch (N.getValueType()) {
1545 default: assert(0 && "Unknown truncate!");
1546 case MVT::i1:
1547 case MVT::i8: Tmp2 = X86::AL; Opc = X86::MOV8rr; break;
1548 case MVT::i16: Tmp2 = X86::AX; Opc = X86::MOV16rr; break;
1549 }
1550 BuildMI(BB, Opc, 1, Result).addReg(Tmp2);
1551 return Result;
1552
Chris Lattner590d8002005-01-09 18:52:44 +00001553 case ISD::SINT_TO_FP:
1554 case ISD::UINT_TO_FP: {
1555 // FIXME: Most of this grunt work should be done by legalize!
Chris Lattneref7ba072005-01-11 03:50:45 +00001556 ContainsFPCode = true;
Chris Lattner590d8002005-01-09 18:52:44 +00001557
1558 // Promote the integer to a type supported by FLD. We do this because there
1559 // are no unsigned FLD instructions, so we must promote an unsigned value to
1560 // a larger signed value, then use FLD on the larger value.
1561 //
1562 MVT::ValueType PromoteType = MVT::Other;
1563 MVT::ValueType SrcTy = N.getOperand(0).getValueType();
1564 unsigned PromoteOpcode = 0;
1565 unsigned RealDestReg = Result;
1566 switch (SrcTy) {
1567 case MVT::i1:
1568 case MVT::i8:
1569 // We don't have the facilities for directly loading byte sized data from
1570 // memory (even signed). Promote it to 16 bits.
1571 PromoteType = MVT::i16;
1572 PromoteOpcode = Node->getOpcode() == ISD::SINT_TO_FP ?
1573 X86::MOVSX16rr8 : X86::MOVZX16rr8;
1574 break;
1575 case MVT::i16:
1576 if (Node->getOpcode() == ISD::UINT_TO_FP) {
1577 PromoteType = MVT::i32;
1578 PromoteOpcode = X86::MOVZX32rr16;
1579 }
1580 break;
1581 default:
1582 // Don't fild into the real destination.
1583 if (Node->getOpcode() == ISD::UINT_TO_FP)
1584 Result = MakeReg(Node->getValueType(0));
1585 break;
1586 }
1587
1588 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001589
Chris Lattner590d8002005-01-09 18:52:44 +00001590 if (PromoteType != MVT::Other) {
1591 Tmp2 = MakeReg(PromoteType);
1592 BuildMI(BB, PromoteOpcode, 1, Tmp2).addReg(Tmp1);
1593 SrcTy = PromoteType;
1594 Tmp1 = Tmp2;
1595 }
1596
1597 // Spill the integer to memory and reload it from there.
1598 unsigned Size = MVT::getSizeInBits(SrcTy)/8;
1599 MachineFunction *F = BB->getParent();
1600 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, Size);
1601
1602 switch (SrcTy) {
Chris Lattner590d8002005-01-09 18:52:44 +00001603 case MVT::i32:
1604 addFrameReference(BuildMI(BB, X86::MOV32mr, 5),
1605 FrameIdx).addReg(Tmp1);
1606 addFrameReference(BuildMI(BB, X86::FILD32m, 5, Result), FrameIdx);
1607 break;
1608 case MVT::i16:
1609 addFrameReference(BuildMI(BB, X86::MOV16mr, 5),
1610 FrameIdx).addReg(Tmp1);
1611 addFrameReference(BuildMI(BB, X86::FILD16m, 5, Result), FrameIdx);
1612 break;
1613 default: break; // No promotion required.
1614 }
1615
Chris Lattner085c9952005-01-12 04:00:00 +00001616 if (Node->getOpcode() == ISD::UINT_TO_FP && Result != RealDestReg) {
Chris Lattner590d8002005-01-09 18:52:44 +00001617 // If this is a cast from uint -> double, we need to be careful when if
1618 // the "sign" bit is set. If so, we don't want to make a negative number,
1619 // we want to make a positive number. Emit code to add an offset if the
1620 // sign bit is set.
1621
1622 // Compute whether the sign bit is set by shifting the reg right 31 bits.
1623 unsigned IsNeg = MakeReg(MVT::i32);
1624 BuildMI(BB, X86::SHR32ri, 2, IsNeg).addReg(Tmp1).addImm(31);
1625
1626 // Create a CP value that has the offset in one word and 0 in the other.
1627 static ConstantInt *TheOffset = ConstantUInt::get(Type::ULongTy,
1628 0x4f80000000000000ULL);
1629 unsigned CPI = F->getConstantPool()->getConstantPoolIndex(TheOffset);
1630 BuildMI(BB, X86::FADD32m, 5, RealDestReg).addReg(Result)
1631 .addConstantPoolIndex(CPI).addZImm(4).addReg(IsNeg).addSImm(0);
Chris Lattner590d8002005-01-09 18:52:44 +00001632 }
1633 return RealDestReg;
1634 }
1635 case ISD::FP_TO_SINT:
1636 case ISD::FP_TO_UINT: {
1637 // FIXME: Most of this grunt work should be done by legalize!
1638 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
1639
1640 // Change the floating point control register to use "round towards zero"
1641 // mode when truncating to an integer value.
1642 //
1643 MachineFunction *F = BB->getParent();
1644 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
1645 addFrameReference(BuildMI(BB, X86::FNSTCW16m, 4), CWFrameIdx);
1646
1647 // Load the old value of the high byte of the control word...
1648 unsigned HighPartOfCW = MakeReg(MVT::i8);
1649 addFrameReference(BuildMI(BB, X86::MOV8rm, 4, HighPartOfCW),
1650 CWFrameIdx, 1);
1651
1652 // Set the high part to be round to zero...
1653 addFrameReference(BuildMI(BB, X86::MOV8mi, 5),
1654 CWFrameIdx, 1).addImm(12);
1655
1656 // Reload the modified control word now...
1657 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001658
Chris Lattner590d8002005-01-09 18:52:44 +00001659 // Restore the memory image of control word to original value
1660 addFrameReference(BuildMI(BB, X86::MOV8mr, 5),
1661 CWFrameIdx, 1).addReg(HighPartOfCW);
1662
1663 // We don't have the facilities for directly storing byte sized data to
1664 // memory. Promote it to 16 bits. We also must promote unsigned values to
1665 // larger classes because we only have signed FP stores.
1666 MVT::ValueType StoreClass = Node->getValueType(0);
1667 if (StoreClass == MVT::i8 || Node->getOpcode() == ISD::FP_TO_UINT)
1668 switch (StoreClass) {
Chris Lattner2afa1912005-05-09 05:33:18 +00001669 case MVT::i1:
Chris Lattner590d8002005-01-09 18:52:44 +00001670 case MVT::i8: StoreClass = MVT::i16; break;
1671 case MVT::i16: StoreClass = MVT::i32; break;
1672 case MVT::i32: StoreClass = MVT::i64; break;
Chris Lattner590d8002005-01-09 18:52:44 +00001673 default: assert(0 && "Unknown store class!");
1674 }
1675
1676 // Spill the integer to memory and reload it from there.
1677 unsigned Size = MVT::getSizeInBits(StoreClass)/8;
1678 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, Size);
1679
1680 switch (StoreClass) {
1681 default: assert(0 && "Unknown store class!");
1682 case MVT::i16:
1683 addFrameReference(BuildMI(BB, X86::FIST16m, 5), FrameIdx).addReg(Tmp1);
1684 break;
1685 case MVT::i32:
Chris Lattner25020852005-01-09 19:49:59 +00001686 addFrameReference(BuildMI(BB, X86::FIST32m, 5), FrameIdx).addReg(Tmp1);
Chris Lattner590d8002005-01-09 18:52:44 +00001687 break;
Chris Lattnera0dbf182005-05-09 18:37:02 +00001688 case MVT::i64:
1689 addFrameReference(BuildMI(BB, X86::FISTP64m, 5), FrameIdx).addReg(Tmp1);
1690 break; }
Chris Lattner590d8002005-01-09 18:52:44 +00001691
1692 switch (Node->getValueType(0)) {
1693 default:
1694 assert(0 && "Unknown integer type!");
Chris Lattner590d8002005-01-09 18:52:44 +00001695 case MVT::i32:
1696 addFrameReference(BuildMI(BB, X86::MOV32rm, 4, Result), FrameIdx);
1697 break;
1698 case MVT::i16:
1699 addFrameReference(BuildMI(BB, X86::MOV16rm, 4, Result), FrameIdx);
1700 break;
1701 case MVT::i8:
Chris Lattner2afa1912005-05-09 05:33:18 +00001702 case MVT::i1:
Chris Lattner590d8002005-01-09 18:52:44 +00001703 addFrameReference(BuildMI(BB, X86::MOV8rm, 4, Result), FrameIdx);
1704 break;
1705 }
1706
1707 // Reload the original control word now.
1708 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1709 return Result;
1710 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001711 case ISD::ADD:
Chris Lattnera5ade062005-01-11 21:19:59 +00001712 Op0 = N.getOperand(0);
1713 Op1 = N.getOperand(1);
1714
Chris Lattner44129b52005-01-25 20:03:11 +00001715 if (isFoldableLoad(Op0, Op1, true)) {
Chris Lattnera5ade062005-01-11 21:19:59 +00001716 std::swap(Op0, Op1);
Chris Lattner4ff348b2005-01-17 06:26:58 +00001717 goto FoldAdd;
1718 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001719
Chris Lattner44129b52005-01-25 20:03:11 +00001720 if (isFoldableLoad(Op1, Op0, true)) {
Chris Lattner4ff348b2005-01-17 06:26:58 +00001721 FoldAdd:
Chris Lattnera5ade062005-01-11 21:19:59 +00001722 switch (N.getValueType()) {
1723 default: assert(0 && "Cannot add this type!");
1724 case MVT::i1:
1725 case MVT::i8: Opc = X86::ADD8rm; break;
1726 case MVT::i16: Opc = X86::ADD16rm; break;
1727 case MVT::i32: Opc = X86::ADD32rm; break;
Chris Lattner44129b52005-01-25 20:03:11 +00001728 case MVT::f64:
1729 // For F64, handle promoted load operations (from F32) as well!
1730 Opc = Op1.getOpcode() == ISD::LOAD ? X86::FADD64m : X86::FADD32m;
1731 break;
Chris Lattnera5ade062005-01-11 21:19:59 +00001732 }
1733 X86AddressMode AM;
Chris Lattner636e79a2005-01-13 05:53:16 +00001734 EmitFoldedLoad(Op1, AM);
1735 Tmp1 = SelectExpr(Op0);
Chris Lattnera5ade062005-01-11 21:19:59 +00001736 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
1737 return Result;
1738 }
1739
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001740 // See if we can codegen this as an LEA to fold operations together.
1741 if (N.getValueType() == MVT::i32) {
Chris Lattner883c86f2005-01-18 02:25:52 +00001742 ExprMap.erase(N);
Chris Lattner98a8ba02005-01-18 01:06:26 +00001743 X86ISelAddressMode AM;
Chris Lattner883c86f2005-01-18 02:25:52 +00001744 MatchAddress(N, AM);
1745 ExprMap[N] = Result;
1746
1747 // If this is not just an add, emit the LEA. For a simple add (like
1748 // reg+reg or reg+imm), we just emit an add. It might be a good idea to
1749 // leave this as LEA, then peephole it to 'ADD' after two address elim
1750 // happens.
1751 if (AM.Scale != 1 || AM.BaseType == X86ISelAddressMode::FrameIndexBase||
1752 AM.GV || (AM.Base.Reg.Val && AM.IndexReg.Val && AM.Disp)) {
1753 X86AddressMode XAM = SelectAddrExprs(AM);
1754 addFullAddress(BuildMI(BB, X86::LEA32r, 4, Result), XAM);
1755 return Result;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001756 }
1757 }
Chris Lattner11333092005-01-11 03:11:44 +00001758
Chris Lattnera5ade062005-01-11 21:19:59 +00001759 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1)) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001760 Opc = 0;
1761 if (CN->getValue() == 1) { // add X, 1 -> inc X
1762 switch (N.getValueType()) {
1763 default: assert(0 && "Cannot integer add this type!");
1764 case MVT::i8: Opc = X86::INC8r; break;
1765 case MVT::i16: Opc = X86::INC16r; break;
1766 case MVT::i32: Opc = X86::INC32r; break;
1767 }
1768 } else if (CN->isAllOnesValue()) { // add X, -1 -> dec X
1769 switch (N.getValueType()) {
1770 default: assert(0 && "Cannot integer add this type!");
1771 case MVT::i8: Opc = X86::DEC8r; break;
1772 case MVT::i16: Opc = X86::DEC16r; break;
1773 case MVT::i32: Opc = X86::DEC32r; break;
1774 }
1775 }
1776
1777 if (Opc) {
Chris Lattnera5ade062005-01-11 21:19:59 +00001778 Tmp1 = SelectExpr(Op0);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001779 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1780 return Result;
1781 }
1782
1783 switch (N.getValueType()) {
1784 default: assert(0 && "Cannot add this type!");
1785 case MVT::i8: Opc = X86::ADD8ri; break;
1786 case MVT::i16: Opc = X86::ADD16ri; break;
1787 case MVT::i32: Opc = X86::ADD32ri; break;
1788 }
1789 if (Opc) {
Chris Lattnera5ade062005-01-11 21:19:59 +00001790 Tmp1 = SelectExpr(Op0);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001791 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
1792 return Result;
1793 }
1794 }
1795
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001796 switch (N.getValueType()) {
1797 default: assert(0 && "Cannot add this type!");
1798 case MVT::i8: Opc = X86::ADD8rr; break;
1799 case MVT::i16: Opc = X86::ADD16rr; break;
1800 case MVT::i32: Opc = X86::ADD32rr; break;
Chris Lattneref7ba072005-01-11 03:50:45 +00001801 case MVT::f64: Opc = X86::FpADD; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001802 }
Chris Lattner11333092005-01-11 03:11:44 +00001803
Chris Lattnera5ade062005-01-11 21:19:59 +00001804 if (getRegPressure(Op0) > getRegPressure(Op1)) {
1805 Tmp1 = SelectExpr(Op0);
1806 Tmp2 = SelectExpr(Op1);
Chris Lattner11333092005-01-11 03:11:44 +00001807 } else {
Chris Lattnera5ade062005-01-11 21:19:59 +00001808 Tmp2 = SelectExpr(Op1);
1809 Tmp1 = SelectExpr(Op0);
Chris Lattner11333092005-01-11 03:11:44 +00001810 }
1811
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001812 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1813 return Result;
Chris Lattnerb7edaa12005-04-02 05:30:17 +00001814
1815 case ISD::FABS:
Chris Lattnerb7edaa12005-04-02 05:30:17 +00001816 case ISD::FNEG:
Chris Lattnerc5dcb532005-04-30 04:25:35 +00001817 case ISD::FSIN:
1818 case ISD::FCOS:
Chris Lattner2c56e8a2005-04-28 22:07:18 +00001819 case ISD::FSQRT:
1820 assert(N.getValueType()==MVT::f64 && "Illegal type for this operation");
Chris Lattnerb7edaa12005-04-02 05:30:17 +00001821 Tmp1 = SelectExpr(Node->getOperand(0));
Chris Lattner2c56e8a2005-04-28 22:07:18 +00001822 switch (N.getOpcode()) {
1823 default: assert(0 && "Unreachable!");
1824 case ISD::FABS: BuildMI(BB, X86::FABS, 1, Result).addReg(Tmp1); break;
1825 case ISD::FNEG: BuildMI(BB, X86::FCHS, 1, Result).addReg(Tmp1); break;
1826 case ISD::FSQRT: BuildMI(BB, X86::FSQRT, 1, Result).addReg(Tmp1); break;
Chris Lattnerc5dcb532005-04-30 04:25:35 +00001827 case ISD::FSIN: BuildMI(BB, X86::FSIN, 1, Result).addReg(Tmp1); break;
1828 case ISD::FCOS: BuildMI(BB, X86::FCOS, 1, Result).addReg(Tmp1); break;
Chris Lattner2c56e8a2005-04-28 22:07:18 +00001829 }
Chris Lattnerb7edaa12005-04-02 05:30:17 +00001830 return Result;
1831
Chris Lattner8db0af12005-04-06 04:21:07 +00001832 case ISD::MULHU:
1833 switch (N.getValueType()) {
1834 default: assert(0 && "Unsupported VT!");
1835 case MVT::i8: Tmp2 = X86::MUL8r; break;
1836 case MVT::i16: Tmp2 = X86::MUL16r; break;
1837 case MVT::i32: Tmp2 = X86::MUL32r; break;
1838 }
1839 // FALL THROUGH
1840 case ISD::MULHS: {
1841 unsigned MovOpc, LowReg, HiReg;
1842 switch (N.getValueType()) {
1843 default: assert(0 && "Unsupported VT!");
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001844 case MVT::i8:
Chris Lattner8db0af12005-04-06 04:21:07 +00001845 MovOpc = X86::MOV8rr;
1846 LowReg = X86::AL;
1847 HiReg = X86::AH;
1848 Opc = X86::IMUL8r;
1849 break;
1850 case MVT::i16:
1851 MovOpc = X86::MOV16rr;
1852 LowReg = X86::AX;
1853 HiReg = X86::DX;
1854 Opc = X86::IMUL16r;
1855 break;
1856 case MVT::i32:
1857 MovOpc = X86::MOV32rr;
1858 LowReg = X86::EAX;
1859 HiReg = X86::EDX;
1860 Opc = X86::IMUL32r;
1861 break;
1862 }
1863 if (Node->getOpcode() != ISD::MULHS)
1864 Opc = Tmp2; // Get the MULHU opcode.
1865
1866 Op0 = Node->getOperand(0);
1867 Op1 = Node->getOperand(1);
1868 if (getRegPressure(Op0) > getRegPressure(Op1)) {
1869 Tmp1 = SelectExpr(Op0);
1870 Tmp2 = SelectExpr(Op1);
1871 } else {
1872 Tmp2 = SelectExpr(Op1);
1873 Tmp1 = SelectExpr(Op0);
1874 }
1875
1876 // FIXME: Implement folding of loads into the memory operands here!
1877 BuildMI(BB, MovOpc, 1, LowReg).addReg(Tmp1);
1878 BuildMI(BB, Opc, 1).addReg(Tmp2);
1879 BuildMI(BB, MovOpc, 1, Result).addReg(HiReg);
1880 return Result;
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001881 }
Chris Lattner8db0af12005-04-06 04:21:07 +00001882
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001883 case ISD::SUB:
Chris Lattnera5ade062005-01-11 21:19:59 +00001884 case ISD::MUL:
1885 case ISD::AND:
1886 case ISD::OR:
Chris Lattnera56cea42005-01-12 04:23:22 +00001887 case ISD::XOR: {
Chris Lattnera5ade062005-01-11 21:19:59 +00001888 static const unsigned SUBTab[] = {
1889 X86::SUB8ri, X86::SUB16ri, X86::SUB32ri, 0, 0,
1890 X86::SUB8rm, X86::SUB16rm, X86::SUB32rm, X86::FSUB32m, X86::FSUB64m,
1891 X86::SUB8rr, X86::SUB16rr, X86::SUB32rr, X86::FpSUB , X86::FpSUB,
1892 };
1893 static const unsigned MULTab[] = {
1894 0, X86::IMUL16rri, X86::IMUL32rri, 0, 0,
1895 0, X86::IMUL16rm , X86::IMUL32rm, X86::FMUL32m, X86::FMUL64m,
1896 0, X86::IMUL16rr , X86::IMUL32rr, X86::FpMUL , X86::FpMUL,
1897 };
1898 static const unsigned ANDTab[] = {
1899 X86::AND8ri, X86::AND16ri, X86::AND32ri, 0, 0,
1900 X86::AND8rm, X86::AND16rm, X86::AND32rm, 0, 0,
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001901 X86::AND8rr, X86::AND16rr, X86::AND32rr, 0, 0,
Chris Lattnera5ade062005-01-11 21:19:59 +00001902 };
1903 static const unsigned ORTab[] = {
1904 X86::OR8ri, X86::OR16ri, X86::OR32ri, 0, 0,
1905 X86::OR8rm, X86::OR16rm, X86::OR32rm, 0, 0,
1906 X86::OR8rr, X86::OR16rr, X86::OR32rr, 0, 0,
1907 };
1908 static const unsigned XORTab[] = {
1909 X86::XOR8ri, X86::XOR16ri, X86::XOR32ri, 0, 0,
1910 X86::XOR8rm, X86::XOR16rm, X86::XOR32rm, 0, 0,
1911 X86::XOR8rr, X86::XOR16rr, X86::XOR32rr, 0, 0,
1912 };
1913
1914 Op0 = Node->getOperand(0);
1915 Op1 = Node->getOperand(1);
1916
Chris Lattner30ea1e92005-01-19 07:37:26 +00001917 if (Node->getOpcode() == ISD::OR && Op0.hasOneUse() && Op1.hasOneUse())
1918 if (EmitOrOpOp(Op0, Op1, Result)) // Match SHLD, SHRD, and rotates.
Chris Lattner85716372005-01-19 06:18:43 +00001919 return Result;
1920
1921 if (Node->getOpcode() == ISD::SUB)
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001922 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(0)))
1923 if (CN->isNullValue()) { // 0 - N -> neg N
1924 switch (N.getValueType()) {
1925 default: assert(0 && "Cannot sub this type!");
1926 case MVT::i1:
1927 case MVT::i8: Opc = X86::NEG8r; break;
1928 case MVT::i16: Opc = X86::NEG16r; break;
1929 case MVT::i32: Opc = X86::NEG32r; break;
1930 }
1931 Tmp1 = SelectExpr(N.getOperand(1));
1932 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1933 return Result;
1934 }
1935
Chris Lattnera5ade062005-01-11 21:19:59 +00001936 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1)) {
1937 if (CN->isAllOnesValue() && Node->getOpcode() == ISD::XOR) {
Chris Lattnerc98279d2005-01-17 00:23:16 +00001938 Opc = 0;
Chris Lattnerd4dab922005-01-11 04:31:30 +00001939 switch (N.getValueType()) {
1940 default: assert(0 && "Cannot add this type!");
Chris Lattnerc98279d2005-01-17 00:23:16 +00001941 case MVT::i1: break; // Not supported, don't invert upper bits!
Chris Lattnerd4dab922005-01-11 04:31:30 +00001942 case MVT::i8: Opc = X86::NOT8r; break;
1943 case MVT::i16: Opc = X86::NOT16r; break;
1944 case MVT::i32: Opc = X86::NOT32r; break;
1945 }
Chris Lattnerc98279d2005-01-17 00:23:16 +00001946 if (Opc) {
1947 Tmp1 = SelectExpr(Op0);
1948 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1949 return Result;
1950 }
Chris Lattnerd4dab922005-01-11 04:31:30 +00001951 }
1952
Chris Lattner2a4e5082005-01-17 06:48:02 +00001953 // Fold common multiplies into LEA instructions.
1954 if (Node->getOpcode() == ISD::MUL && N.getValueType() == MVT::i32) {
1955 switch ((int)CN->getValue()) {
1956 default: break;
1957 case 3:
1958 case 5:
1959 case 9:
Chris Lattner2a4e5082005-01-17 06:48:02 +00001960 // Remove N from exprmap so SelectAddress doesn't get confused.
1961 ExprMap.erase(N);
Chris Lattner98a8ba02005-01-18 01:06:26 +00001962 X86AddressMode AM;
Chris Lattner2a4e5082005-01-17 06:48:02 +00001963 SelectAddress(N, AM);
1964 // Restore it to the map.
1965 ExprMap[N] = Result;
1966 addFullAddress(BuildMI(BB, X86::LEA32r, 4, Result), AM);
1967 return Result;
1968 }
1969 }
1970
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001971 switch (N.getValueType()) {
Chris Lattnerd4dab922005-01-11 04:31:30 +00001972 default: assert(0 && "Cannot xor this type!");
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001973 case MVT::i1:
Chris Lattnera5ade062005-01-11 21:19:59 +00001974 case MVT::i8: Opc = 0; break;
1975 case MVT::i16: Opc = 1; break;
1976 case MVT::i32: Opc = 2; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001977 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001978 switch (Node->getOpcode()) {
1979 default: assert(0 && "Unreachable!");
1980 case ISD::SUB: Opc = SUBTab[Opc]; break;
1981 case ISD::MUL: Opc = MULTab[Opc]; break;
1982 case ISD::AND: Opc = ANDTab[Opc]; break;
1983 case ISD::OR: Opc = ORTab[Opc]; break;
1984 case ISD::XOR: Opc = XORTab[Opc]; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001985 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001986 if (Opc) { // Can't fold MUL:i8 R, imm
1987 Tmp1 = SelectExpr(Op0);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001988 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
1989 return Result;
1990 }
1991 }
Chris Lattner11333092005-01-11 03:11:44 +00001992
Chris Lattner44129b52005-01-25 20:03:11 +00001993 if (isFoldableLoad(Op0, Op1, true))
Chris Lattnera5ade062005-01-11 21:19:59 +00001994 if (Node->getOpcode() != ISD::SUB) {
1995 std::swap(Op0, Op1);
Chris Lattner4ff348b2005-01-17 06:26:58 +00001996 goto FoldOps;
Chris Lattnera5ade062005-01-11 21:19:59 +00001997 } else {
Chris Lattner44129b52005-01-25 20:03:11 +00001998 // For FP, emit 'reverse' subract, with a memory operand.
1999 if (N.getValueType() == MVT::f64) {
2000 if (Op0.getOpcode() == ISD::EXTLOAD)
2001 Opc = X86::FSUBR32m;
2002 else
2003 Opc = X86::FSUBR64m;
2004
Chris Lattnera5ade062005-01-11 21:19:59 +00002005 X86AddressMode AM;
Chris Lattner636e79a2005-01-13 05:53:16 +00002006 EmitFoldedLoad(Op0, AM);
2007 Tmp1 = SelectExpr(Op1);
Chris Lattnera5ade062005-01-11 21:19:59 +00002008 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
2009 return Result;
2010 }
2011 }
2012
Chris Lattner44129b52005-01-25 20:03:11 +00002013 if (isFoldableLoad(Op1, Op0, true)) {
Chris Lattner4ff348b2005-01-17 06:26:58 +00002014 FoldOps:
Chris Lattnera5ade062005-01-11 21:19:59 +00002015 switch (N.getValueType()) {
2016 default: assert(0 && "Cannot operate on this type!");
2017 case MVT::i1:
2018 case MVT::i8: Opc = 5; break;
2019 case MVT::i16: Opc = 6; break;
2020 case MVT::i32: Opc = 7; break;
Chris Lattner44129b52005-01-25 20:03:11 +00002021 // For F64, handle promoted load operations (from F32) as well!
2022 case MVT::f64: Opc = Op1.getOpcode() == ISD::LOAD ? 9 : 8; break;
Chris Lattnera5ade062005-01-11 21:19:59 +00002023 }
2024 switch (Node->getOpcode()) {
2025 default: assert(0 && "Unreachable!");
2026 case ISD::SUB: Opc = SUBTab[Opc]; break;
2027 case ISD::MUL: Opc = MULTab[Opc]; break;
2028 case ISD::AND: Opc = ANDTab[Opc]; break;
2029 case ISD::OR: Opc = ORTab[Opc]; break;
2030 case ISD::XOR: Opc = XORTab[Opc]; break;
2031 }
2032
2033 X86AddressMode AM;
Chris Lattner636e79a2005-01-13 05:53:16 +00002034 EmitFoldedLoad(Op1, AM);
2035 Tmp1 = SelectExpr(Op0);
Chris Lattnera5ade062005-01-11 21:19:59 +00002036 if (Opc) {
2037 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
2038 } else {
2039 assert(Node->getOpcode() == ISD::MUL &&
2040 N.getValueType() == MVT::i8 && "Unexpected situation!");
2041 // Must use the MUL instruction, which forces use of AL.
2042 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(Tmp1);
2043 addFullAddress(BuildMI(BB, X86::MUL8m, 1), AM);
2044 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
2045 }
2046 return Result;
Chris Lattner11333092005-01-11 03:11:44 +00002047 }
Chris Lattnera5ade062005-01-11 21:19:59 +00002048
2049 if (getRegPressure(Op0) > getRegPressure(Op1)) {
2050 Tmp1 = SelectExpr(Op0);
2051 Tmp2 = SelectExpr(Op1);
2052 } else {
2053 Tmp2 = SelectExpr(Op1);
2054 Tmp1 = SelectExpr(Op0);
2055 }
2056
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002057 switch (N.getValueType()) {
2058 default: assert(0 && "Cannot add this type!");
Chris Lattnera5ade062005-01-11 21:19:59 +00002059 case MVT::i1:
2060 case MVT::i8: Opc = 10; break;
2061 case MVT::i16: Opc = 11; break;
2062 case MVT::i32: Opc = 12; break;
2063 case MVT::f32: Opc = 13; break;
2064 case MVT::f64: Opc = 14; break;
2065 }
2066 switch (Node->getOpcode()) {
2067 default: assert(0 && "Unreachable!");
2068 case ISD::SUB: Opc = SUBTab[Opc]; break;
2069 case ISD::MUL: Opc = MULTab[Opc]; break;
2070 case ISD::AND: Opc = ANDTab[Opc]; break;
2071 case ISD::OR: Opc = ORTab[Opc]; break;
2072 case ISD::XOR: Opc = XORTab[Opc]; break;
2073 }
2074 if (Opc) {
2075 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
2076 } else {
2077 assert(Node->getOpcode() == ISD::MUL &&
2078 N.getValueType() == MVT::i8 && "Unexpected situation!");
Chris Lattnera13d3232005-01-10 20:55:48 +00002079 // Must use the MUL instruction, which forces use of AL.
2080 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(Tmp1);
2081 BuildMI(BB, X86::MUL8r, 1).addReg(Tmp2);
2082 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002083 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002084 return Result;
Chris Lattnera56cea42005-01-12 04:23:22 +00002085 }
Chris Lattner19ad0622005-01-20 18:53:00 +00002086 case ISD::ADD_PARTS:
2087 case ISD::SUB_PARTS: {
2088 assert(N.getNumOperands() == 4 && N.getValueType() == MVT::i32 &&
2089 "Not an i64 add/sub!");
2090 // Emit all of the operands.
2091 std::vector<unsigned> InVals;
2092 for (unsigned i = 0, e = N.getNumOperands(); i != e; ++i)
2093 InVals.push_back(SelectExpr(N.getOperand(i)));
2094 if (N.getOpcode() == ISD::ADD_PARTS) {
2095 BuildMI(BB, X86::ADD32rr, 2, Result).addReg(InVals[0]).addReg(InVals[2]);
2096 BuildMI(BB, X86::ADC32rr,2,Result+1).addReg(InVals[1]).addReg(InVals[3]);
2097 } else {
2098 BuildMI(BB, X86::SUB32rr, 2, Result).addReg(InVals[0]).addReg(InVals[2]);
2099 BuildMI(BB, X86::SBB32rr, 2,Result+1).addReg(InVals[1]).addReg(InVals[3]);
2100 }
2101 return Result+N.ResNo;
2102 }
2103
Chris Lattnerb38a7492005-04-02 04:01:14 +00002104 case ISD::SHL_PARTS:
2105 case ISD::SRA_PARTS:
2106 case ISD::SRL_PARTS: {
2107 assert(N.getNumOperands() == 3 && N.getValueType() == MVT::i32 &&
2108 "Not an i64 shift!");
2109 unsigned ShiftOpLo = SelectExpr(N.getOperand(0));
2110 unsigned ShiftOpHi = SelectExpr(N.getOperand(1));
2111 unsigned TmpReg = MakeReg(MVT::i32);
2112 if (N.getOpcode() == ISD::SRA_PARTS) {
2113 // If this is a SHR of a Long, then we need to do funny sign extension
2114 // stuff. TmpReg gets the value to use as the high-part if we are
2115 // shifting more than 32 bits.
2116 BuildMI(BB, X86::SAR32ri, 2, TmpReg).addReg(ShiftOpHi).addImm(31);
2117 } else {
2118 // Other shifts use a fixed zero value if the shift is more than 32 bits.
2119 BuildMI(BB, X86::MOV32ri, 1, TmpReg).addImm(0);
2120 }
2121
2122 // Initialize CL with the shift amount.
2123 unsigned ShiftAmountReg = SelectExpr(N.getOperand(2));
2124 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShiftAmountReg);
2125
2126 unsigned TmpReg2 = MakeReg(MVT::i32);
2127 unsigned TmpReg3 = MakeReg(MVT::i32);
2128 if (N.getOpcode() == ISD::SHL_PARTS) {
2129 // TmpReg2 = shld inHi, inLo
2130 BuildMI(BB, X86::SHLD32rrCL, 2,TmpReg2).addReg(ShiftOpHi)
2131 .addReg(ShiftOpLo);
2132 // TmpReg3 = shl inLo, CL
2133 BuildMI(BB, X86::SHL32rCL, 1, TmpReg3).addReg(ShiftOpLo);
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002134
Chris Lattnerb38a7492005-04-02 04:01:14 +00002135 // Set the flags to indicate whether the shift was by more than 32 bits.
2136 BuildMI(BB, X86::TEST8ri, 2).addReg(X86::CL).addImm(32);
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002137
Chris Lattnerb38a7492005-04-02 04:01:14 +00002138 // DestHi = (>32) ? TmpReg3 : TmpReg2;
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002139 BuildMI(BB, X86::CMOVNE32rr, 2,
Chris Lattnerb38a7492005-04-02 04:01:14 +00002140 Result+1).addReg(TmpReg2).addReg(TmpReg3);
2141 // DestLo = (>32) ? TmpReg : TmpReg3;
2142 BuildMI(BB, X86::CMOVNE32rr, 2,
2143 Result).addReg(TmpReg3).addReg(TmpReg);
2144 } else {
2145 // TmpReg2 = shrd inLo, inHi
2146 BuildMI(BB, X86::SHRD32rrCL,2,TmpReg2).addReg(ShiftOpLo)
2147 .addReg(ShiftOpHi);
2148 // TmpReg3 = s[ah]r inHi, CL
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002149 BuildMI(BB, N.getOpcode() == ISD::SRA_PARTS ? X86::SAR32rCL
Chris Lattnerb38a7492005-04-02 04:01:14 +00002150 : X86::SHR32rCL, 1, TmpReg3)
2151 .addReg(ShiftOpHi);
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002152
Chris Lattnerb38a7492005-04-02 04:01:14 +00002153 // Set the flags to indicate whether the shift was by more than 32 bits.
2154 BuildMI(BB, X86::TEST8ri, 2).addReg(X86::CL).addImm(32);
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002155
Chris Lattnerb38a7492005-04-02 04:01:14 +00002156 // DestLo = (>32) ? TmpReg3 : TmpReg2;
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002157 BuildMI(BB, X86::CMOVNE32rr, 2,
Chris Lattnerb38a7492005-04-02 04:01:14 +00002158 Result).addReg(TmpReg2).addReg(TmpReg3);
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002159
Chris Lattnerb38a7492005-04-02 04:01:14 +00002160 // DestHi = (>32) ? TmpReg : TmpReg3;
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002161 BuildMI(BB, X86::CMOVNE32rr, 2,
Chris Lattnerb38a7492005-04-02 04:01:14 +00002162 Result+1).addReg(TmpReg3).addReg(TmpReg);
2163 }
2164 return Result+N.ResNo;
2165 }
2166
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002167 case ISD::SELECT:
Chris Lattnerda2ce112005-01-16 07:34:08 +00002168 if (getRegPressure(N.getOperand(1)) > getRegPressure(N.getOperand(2))) {
2169 Tmp2 = SelectExpr(N.getOperand(1));
2170 Tmp3 = SelectExpr(N.getOperand(2));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002171 } else {
Chris Lattnerda2ce112005-01-16 07:34:08 +00002172 Tmp3 = SelectExpr(N.getOperand(2));
2173 Tmp2 = SelectExpr(N.getOperand(1));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002174 }
Chris Lattnerda2ce112005-01-16 07:34:08 +00002175 EmitSelectCC(N.getOperand(0), N.getValueType(), Tmp2, Tmp3, Result);
2176 return Result;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002177
2178 case ISD::SDIV:
2179 case ISD::UDIV:
2180 case ISD::SREM:
2181 case ISD::UREM: {
Chris Lattnerda2ce112005-01-16 07:34:08 +00002182 assert((N.getOpcode() != ISD::SREM || MVT::isInteger(N.getValueType())) &&
2183 "We don't support this operator!");
2184
Chris Lattner5bf26862005-04-13 03:29:53 +00002185 if (N.getOpcode() == ISD::SDIV) {
Chris Lattner3576c842005-01-25 20:35:10 +00002186 // We can fold loads into FpDIVs, but not really into any others.
2187 if (N.getValueType() == MVT::f64) {
2188 // Check for reversed and unreversed DIV.
2189 if (isFoldableLoad(N.getOperand(0), N.getOperand(1), true)) {
2190 if (N.getOperand(0).getOpcode() == ISD::EXTLOAD)
2191 Opc = X86::FDIVR32m;
2192 else
2193 Opc = X86::FDIVR64m;
2194 X86AddressMode AM;
2195 EmitFoldedLoad(N.getOperand(0), AM);
2196 Tmp1 = SelectExpr(N.getOperand(1));
2197 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
2198 return Result;
2199 } else if (isFoldableLoad(N.getOperand(1), N.getOperand(0), true) &&
2200 N.getOperand(1).getOpcode() == ISD::LOAD) {
2201 if (N.getOperand(1).getOpcode() == ISD::EXTLOAD)
2202 Opc = X86::FDIV32m;
2203 else
2204 Opc = X86::FDIV64m;
2205 X86AddressMode AM;
2206 EmitFoldedLoad(N.getOperand(1), AM);
2207 Tmp1 = SelectExpr(N.getOperand(0));
2208 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
2209 return Result;
2210 }
2211 }
2212
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002213 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
2214 // FIXME: These special cases should be handled by the lowering impl!
2215 unsigned RHS = CN->getValue();
2216 bool isNeg = false;
2217 if ((int)RHS < 0) {
2218 isNeg = true;
2219 RHS = -RHS;
2220 }
2221 if (RHS && (RHS & (RHS-1)) == 0) { // Signed division by power of 2?
2222 unsigned Log = log2(RHS);
2223 unsigned TmpReg = MakeReg(N.getValueType());
2224 unsigned SAROpc, SHROpc, ADDOpc, NEGOpc;
2225 switch (N.getValueType()) {
2226 default: assert("Unknown type to signed divide!");
2227 case MVT::i8:
2228 SAROpc = X86::SAR8ri;
2229 SHROpc = X86::SHR8ri;
2230 ADDOpc = X86::ADD8rr;
2231 NEGOpc = X86::NEG8r;
2232 break;
2233 case MVT::i16:
2234 SAROpc = X86::SAR16ri;
2235 SHROpc = X86::SHR16ri;
2236 ADDOpc = X86::ADD16rr;
2237 NEGOpc = X86::NEG16r;
2238 break;
2239 case MVT::i32:
2240 SAROpc = X86::SAR32ri;
2241 SHROpc = X86::SHR32ri;
2242 ADDOpc = X86::ADD32rr;
2243 NEGOpc = X86::NEG32r;
2244 break;
2245 }
Chris Lattner11333092005-01-11 03:11:44 +00002246 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002247 BuildMI(BB, SAROpc, 2, TmpReg).addReg(Tmp1).addImm(Log-1);
2248 unsigned TmpReg2 = MakeReg(N.getValueType());
2249 BuildMI(BB, SHROpc, 2, TmpReg2).addReg(TmpReg).addImm(32-Log);
2250 unsigned TmpReg3 = MakeReg(N.getValueType());
2251 BuildMI(BB, ADDOpc, 2, TmpReg3).addReg(Tmp1).addReg(TmpReg2);
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002252
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002253 unsigned TmpReg4 = isNeg ? MakeReg(N.getValueType()) : Result;
2254 BuildMI(BB, SAROpc, 2, TmpReg4).addReg(TmpReg3).addImm(Log);
2255 if (isNeg)
2256 BuildMI(BB, NEGOpc, 1, Result).addReg(TmpReg4);
2257 return Result;
2258 }
2259 }
Chris Lattner5bf26862005-04-13 03:29:53 +00002260 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002261
Chris Lattner11333092005-01-11 03:11:44 +00002262 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2263 Tmp1 = SelectExpr(N.getOperand(0));
2264 Tmp2 = SelectExpr(N.getOperand(1));
2265 } else {
2266 Tmp2 = SelectExpr(N.getOperand(1));
2267 Tmp1 = SelectExpr(N.getOperand(0));
2268 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002269
2270 bool isSigned = N.getOpcode() == ISD::SDIV || N.getOpcode() == ISD::SREM;
2271 bool isDiv = N.getOpcode() == ISD::SDIV || N.getOpcode() == ISD::UDIV;
2272 unsigned LoReg, HiReg, DivOpcode, MovOpcode, ClrOpcode, SExtOpcode;
2273 switch (N.getValueType()) {
2274 default: assert(0 && "Cannot sdiv this type!");
2275 case MVT::i8:
2276 DivOpcode = isSigned ? X86::IDIV8r : X86::DIV8r;
2277 LoReg = X86::AL;
2278 HiReg = X86::AH;
2279 MovOpcode = X86::MOV8rr;
2280 ClrOpcode = X86::MOV8ri;
2281 SExtOpcode = X86::CBW;
2282 break;
2283 case MVT::i16:
2284 DivOpcode = isSigned ? X86::IDIV16r : X86::DIV16r;
2285 LoReg = X86::AX;
2286 HiReg = X86::DX;
2287 MovOpcode = X86::MOV16rr;
2288 ClrOpcode = X86::MOV16ri;
2289 SExtOpcode = X86::CWD;
2290 break;
2291 case MVT::i32:
2292 DivOpcode = isSigned ? X86::IDIV32r : X86::DIV32r;
Chris Lattner42928302005-01-12 03:16:09 +00002293 LoReg = X86::EAX;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002294 HiReg = X86::EDX;
2295 MovOpcode = X86::MOV32rr;
2296 ClrOpcode = X86::MOV32ri;
2297 SExtOpcode = X86::CDQ;
2298 break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002299 case MVT::f64:
Chris Lattnerda2ce112005-01-16 07:34:08 +00002300 BuildMI(BB, X86::FpDIV, 2, Result).addReg(Tmp1).addReg(Tmp2);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002301 return Result;
2302 }
2303
2304 // Set up the low part.
2305 BuildMI(BB, MovOpcode, 1, LoReg).addReg(Tmp1);
2306
2307 if (isSigned) {
2308 // Sign extend the low part into the high part.
2309 BuildMI(BB, SExtOpcode, 0);
2310 } else {
2311 // Zero out the high part, effectively zero extending the input.
2312 BuildMI(BB, ClrOpcode, 1, HiReg).addImm(0);
2313 }
2314
2315 // Emit the DIV/IDIV instruction.
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002316 BuildMI(BB, DivOpcode, 1).addReg(Tmp2);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002317
2318 // Get the result of the divide or rem.
2319 BuildMI(BB, MovOpcode, 1, Result).addReg(isDiv ? LoReg : HiReg);
2320 return Result;
2321 }
2322
2323 case ISD::SHL:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002324 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Chris Lattnera5ade062005-01-11 21:19:59 +00002325 if (CN->getValue() == 1) { // X = SHL Y, 1 -> X = ADD Y, Y
2326 switch (N.getValueType()) {
2327 default: assert(0 && "Cannot shift this type!");
2328 case MVT::i8: Opc = X86::ADD8rr; break;
2329 case MVT::i16: Opc = X86::ADD16rr; break;
2330 case MVT::i32: Opc = X86::ADD32rr; break;
2331 }
2332 Tmp1 = SelectExpr(N.getOperand(0));
2333 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp1);
2334 return Result;
2335 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002336
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002337 switch (N.getValueType()) {
2338 default: assert(0 && "Cannot shift this type!");
2339 case MVT::i8: Opc = X86::SHL8ri; break;
2340 case MVT::i16: Opc = X86::SHL16ri; break;
2341 case MVT::i32: Opc = X86::SHL32ri; break;
2342 }
Chris Lattner11333092005-01-11 03:11:44 +00002343 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002344 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
2345 return Result;
2346 }
Chris Lattner11333092005-01-11 03:11:44 +00002347
2348 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2349 Tmp1 = SelectExpr(N.getOperand(0));
2350 Tmp2 = SelectExpr(N.getOperand(1));
2351 } else {
2352 Tmp2 = SelectExpr(N.getOperand(1));
2353 Tmp1 = SelectExpr(N.getOperand(0));
2354 }
2355
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002356 switch (N.getValueType()) {
2357 default: assert(0 && "Cannot shift this type!");
2358 case MVT::i8 : Opc = X86::SHL8rCL; break;
2359 case MVT::i16: Opc = X86::SHL16rCL; break;
2360 case MVT::i32: Opc = X86::SHL32rCL; break;
2361 }
2362 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
2363 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
2364 return Result;
2365 case ISD::SRL:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002366 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
2367 switch (N.getValueType()) {
2368 default: assert(0 && "Cannot shift this type!");
2369 case MVT::i8: Opc = X86::SHR8ri; break;
2370 case MVT::i16: Opc = X86::SHR16ri; break;
2371 case MVT::i32: Opc = X86::SHR32ri; break;
2372 }
Chris Lattner11333092005-01-11 03:11:44 +00002373 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002374 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
2375 return Result;
2376 }
Chris Lattner11333092005-01-11 03:11:44 +00002377
2378 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2379 Tmp1 = SelectExpr(N.getOperand(0));
2380 Tmp2 = SelectExpr(N.getOperand(1));
2381 } else {
2382 Tmp2 = SelectExpr(N.getOperand(1));
2383 Tmp1 = SelectExpr(N.getOperand(0));
2384 }
2385
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002386 switch (N.getValueType()) {
2387 default: assert(0 && "Cannot shift this type!");
2388 case MVT::i8 : Opc = X86::SHR8rCL; break;
2389 case MVT::i16: Opc = X86::SHR16rCL; break;
2390 case MVT::i32: Opc = X86::SHR32rCL; break;
2391 }
2392 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
2393 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
2394 return Result;
2395 case ISD::SRA:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002396 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
2397 switch (N.getValueType()) {
2398 default: assert(0 && "Cannot shift this type!");
2399 case MVT::i8: Opc = X86::SAR8ri; break;
2400 case MVT::i16: Opc = X86::SAR16ri; break;
2401 case MVT::i32: Opc = X86::SAR32ri; break;
2402 }
Chris Lattner11333092005-01-11 03:11:44 +00002403 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002404 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
2405 return Result;
2406 }
Chris Lattner11333092005-01-11 03:11:44 +00002407
2408 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2409 Tmp1 = SelectExpr(N.getOperand(0));
2410 Tmp2 = SelectExpr(N.getOperand(1));
2411 } else {
2412 Tmp2 = SelectExpr(N.getOperand(1));
2413 Tmp1 = SelectExpr(N.getOperand(0));
2414 }
2415
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002416 switch (N.getValueType()) {
2417 default: assert(0 && "Cannot shift this type!");
2418 case MVT::i8 : Opc = X86::SAR8rCL; break;
2419 case MVT::i16: Opc = X86::SAR16rCL; break;
2420 case MVT::i32: Opc = X86::SAR32rCL; break;
2421 }
2422 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
2423 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
2424 return Result;
2425
2426 case ISD::SETCC:
Chris Lattnercb1aa8d2005-01-17 01:34:14 +00002427 EmitCMP(N.getOperand(0), N.getOperand(1), Node->hasOneUse());
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002428 EmitSetCC(BB, Result, cast<SetCCSDNode>(N)->getCondition(),
2429 MVT::isFloatingPoint(N.getOperand(1).getValueType()));
2430 return Result;
Chris Lattnere9ef81d2005-01-15 05:22:24 +00002431 case ISD::LOAD:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002432 // Make sure we generate both values.
Chris Lattner4a108662005-01-18 03:51:59 +00002433 if (Result != 1) { // Generate the token
2434 if (!ExprMap.insert(std::make_pair(N.getValue(1), 1)).second)
2435 assert(0 && "Load already emitted!?");
2436 } else
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002437 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
2438
Chris Lattner5188ad72005-01-08 19:28:19 +00002439 switch (Node->getValueType(0)) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002440 default: assert(0 && "Cannot load this type!");
2441 case MVT::i1:
2442 case MVT::i8: Opc = X86::MOV8rm; break;
2443 case MVT::i16: Opc = X86::MOV16rm; break;
2444 case MVT::i32: Opc = X86::MOV32rm; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002445 case MVT::f64: Opc = X86::FLD64m; ContainsFPCode = true; break;
2446 }
Chris Lattner11333092005-01-11 03:11:44 +00002447
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002448 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N.getOperand(1))){
Chris Lattner11333092005-01-11 03:11:44 +00002449 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002450 addConstantPoolReference(BuildMI(BB, Opc, 4, Result), CP->getIndex());
2451 } else {
2452 X86AddressMode AM;
Chris Lattner636e79a2005-01-13 05:53:16 +00002453
2454 SDOperand Chain = N.getOperand(0);
2455 SDOperand Address = N.getOperand(1);
2456 if (getRegPressure(Chain) > getRegPressure(Address)) {
2457 Select(Chain);
2458 SelectAddress(Address, AM);
2459 } else {
2460 SelectAddress(Address, AM);
2461 Select(Chain);
2462 }
2463
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002464 addFullAddress(BuildMI(BB, Opc, 4, Result), AM);
2465 }
2466 return Result;
Chris Lattnere9ef81d2005-01-15 05:22:24 +00002467
2468 case ISD::EXTLOAD: // Arbitrarily codegen extloads as MOVZX*
2469 case ISD::ZEXTLOAD: {
2470 // Make sure we generate both values.
2471 if (Result != 1)
2472 ExprMap[N.getValue(1)] = 1; // Generate the token
2473 else
2474 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
2475
Chris Lattnerda2ce112005-01-16 07:34:08 +00002476 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N.getOperand(1)))
2477 if (Node->getValueType(0) == MVT::f64) {
2478 assert(cast<MVTSDNode>(Node)->getExtraValueType() == MVT::f32 &&
2479 "Bad EXTLOAD!");
2480 addConstantPoolReference(BuildMI(BB, X86::FLD32m, 4, Result),
2481 CP->getIndex());
2482 return Result;
2483 }
2484
Chris Lattnere9ef81d2005-01-15 05:22:24 +00002485 X86AddressMode AM;
2486 if (getRegPressure(Node->getOperand(0)) >
2487 getRegPressure(Node->getOperand(1))) {
2488 Select(Node->getOperand(0)); // chain
2489 SelectAddress(Node->getOperand(1), AM);
2490 } else {
2491 SelectAddress(Node->getOperand(1), AM);
2492 Select(Node->getOperand(0)); // chain
2493 }
2494
2495 switch (Node->getValueType(0)) {
2496 default: assert(0 && "Unknown type to sign extend to.");
2497 case MVT::f64:
2498 assert(cast<MVTSDNode>(Node)->getExtraValueType() == MVT::f32 &&
2499 "Bad EXTLOAD!");
2500 addFullAddress(BuildMI(BB, X86::FLD32m, 5, Result), AM);
2501 break;
2502 case MVT::i32:
2503 switch (cast<MVTSDNode>(Node)->getExtraValueType()) {
2504 default:
2505 assert(0 && "Bad zero extend!");
2506 case MVT::i1:
2507 case MVT::i8:
2508 addFullAddress(BuildMI(BB, X86::MOVZX32rm8, 5, Result), AM);
2509 break;
2510 case MVT::i16:
2511 addFullAddress(BuildMI(BB, X86::MOVZX32rm16, 5, Result), AM);
2512 break;
2513 }
2514 break;
2515 case MVT::i16:
2516 assert(cast<MVTSDNode>(Node)->getExtraValueType() <= MVT::i8 &&
2517 "Bad zero extend!");
2518 addFullAddress(BuildMI(BB, X86::MOVSX16rm8, 5, Result), AM);
2519 break;
2520 case MVT::i8:
2521 assert(cast<MVTSDNode>(Node)->getExtraValueType() == MVT::i1 &&
2522 "Bad zero extend!");
2523 addFullAddress(BuildMI(BB, X86::MOV8rm, 5, Result), AM);
2524 break;
2525 }
2526 return Result;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002527 }
Chris Lattnere9ef81d2005-01-15 05:22:24 +00002528 case ISD::SEXTLOAD: {
2529 // Make sure we generate both values.
2530 if (Result != 1)
2531 ExprMap[N.getValue(1)] = 1; // Generate the token
2532 else
2533 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
2534
2535 X86AddressMode AM;
2536 if (getRegPressure(Node->getOperand(0)) >
2537 getRegPressure(Node->getOperand(1))) {
2538 Select(Node->getOperand(0)); // chain
2539 SelectAddress(Node->getOperand(1), AM);
2540 } else {
2541 SelectAddress(Node->getOperand(1), AM);
2542 Select(Node->getOperand(0)); // chain
2543 }
2544
2545 switch (Node->getValueType(0)) {
2546 case MVT::i8: assert(0 && "Cannot sign extend from bool!");
2547 default: assert(0 && "Unknown type to sign extend to.");
2548 case MVT::i32:
2549 switch (cast<MVTSDNode>(Node)->getExtraValueType()) {
2550 default:
2551 case MVT::i1: assert(0 && "Cannot sign extend from bool!");
2552 case MVT::i8:
2553 addFullAddress(BuildMI(BB, X86::MOVSX32rm8, 5, Result), AM);
2554 break;
2555 case MVT::i16:
2556 addFullAddress(BuildMI(BB, X86::MOVSX32rm16, 5, Result), AM);
2557 break;
2558 }
2559 break;
2560 case MVT::i16:
2561 assert(cast<MVTSDNode>(Node)->getExtraValueType() == MVT::i8 &&
2562 "Cannot sign extend from bool!");
2563 addFullAddress(BuildMI(BB, X86::MOVSX16rm8, 5, Result), AM);
2564 break;
2565 }
2566 return Result;
2567 }
2568
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002569 case ISD::DYNAMIC_STACKALLOC:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002570 // Generate both result values.
2571 if (Result != 1)
2572 ExprMap[N.getValue(1)] = 1; // Generate the token
2573 else
2574 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
2575
2576 // FIXME: We are currently ignoring the requested alignment for handling
2577 // greater than the stack alignment. This will need to be revisited at some
2578 // point. Align = N.getOperand(2);
2579
2580 if (!isa<ConstantSDNode>(N.getOperand(2)) ||
2581 cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) {
2582 std::cerr << "Cannot allocate stack object with greater alignment than"
2583 << " the stack alignment yet!";
2584 abort();
2585 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002586
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002587 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Chris Lattner11333092005-01-11 03:11:44 +00002588 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002589 BuildMI(BB, X86::SUB32ri, 2, X86::ESP).addReg(X86::ESP)
2590 .addImm(CN->getValue());
2591 } else {
Chris Lattner11333092005-01-11 03:11:44 +00002592 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2593 Select(N.getOperand(0));
2594 Tmp1 = SelectExpr(N.getOperand(1));
2595 } else {
2596 Tmp1 = SelectExpr(N.getOperand(1));
2597 Select(N.getOperand(0));
2598 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002599
2600 // Subtract size from stack pointer, thereby allocating some space.
2601 BuildMI(BB, X86::SUB32rr, 2, X86::ESP).addReg(X86::ESP).addReg(Tmp1);
2602 }
2603
2604 // Put a pointer to the space into the result register, by copying the stack
2605 // pointer.
2606 BuildMI(BB, X86::MOV32rr, 1, Result).addReg(X86::ESP);
2607 return Result;
2608
2609 case ISD::CALL:
Chris Lattner5188ad72005-01-08 19:28:19 +00002610 // The chain for this call is now lowered.
Chris Lattner4a108662005-01-18 03:51:59 +00002611 ExprMap.insert(std::make_pair(N.getValue(Node->getNumValues()-1), 1));
Chris Lattner5188ad72005-01-08 19:28:19 +00002612
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002613 if (GlobalAddressSDNode *GASD =
2614 dyn_cast<GlobalAddressSDNode>(N.getOperand(1))) {
Chris Lattner11333092005-01-11 03:11:44 +00002615 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002616 BuildMI(BB, X86::CALLpcrel32, 1).addGlobalAddress(GASD->getGlobal(),true);
2617 } else if (ExternalSymbolSDNode *ESSDN =
2618 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1))) {
Chris Lattner11333092005-01-11 03:11:44 +00002619 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002620 BuildMI(BB, X86::CALLpcrel32,
2621 1).addExternalSymbol(ESSDN->getSymbol(), true);
2622 } else {
Chris Lattner11333092005-01-11 03:11:44 +00002623 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2624 Select(N.getOperand(0));
2625 Tmp1 = SelectExpr(N.getOperand(1));
2626 } else {
2627 Tmp1 = SelectExpr(N.getOperand(1));
2628 Select(N.getOperand(0));
2629 }
2630
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002631 BuildMI(BB, X86::CALL32r, 1).addReg(Tmp1);
2632 }
Chris Lattner5188ad72005-01-08 19:28:19 +00002633 switch (Node->getValueType(0)) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002634 default: assert(0 && "Unknown value type for call result!");
2635 case MVT::Other: return 1;
2636 case MVT::i1:
2637 case MVT::i8:
2638 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
2639 break;
2640 case MVT::i16:
2641 BuildMI(BB, X86::MOV16rr, 1, Result).addReg(X86::AX);
2642 break;
2643 case MVT::i32:
2644 BuildMI(BB, X86::MOV32rr, 1, Result).addReg(X86::EAX);
Chris Lattner5188ad72005-01-08 19:28:19 +00002645 if (Node->getValueType(1) == MVT::i32)
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002646 BuildMI(BB, X86::MOV32rr, 1, Result+1).addReg(X86::EDX);
2647 break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002648 case MVT::f64: // Floating-point return values live in %ST(0)
2649 ContainsFPCode = true;
2650 BuildMI(BB, X86::FpGETRESULT, 1, Result);
2651 break;
2652 }
2653 return Result+N.ResNo;
2654 }
2655
2656 return 0;
2657}
2658
Chris Lattnere10269b2005-01-17 19:25:26 +00002659/// TryToFoldLoadOpStore - Given a store node, try to fold together a
2660/// load/op/store instruction. If successful return true.
2661bool ISel::TryToFoldLoadOpStore(SDNode *Node) {
2662 assert(Node->getOpcode() == ISD::STORE && "Can only do this for stores!");
2663 SDOperand Chain = Node->getOperand(0);
2664 SDOperand StVal = Node->getOperand(1);
Chris Lattner5c659812005-01-17 22:10:42 +00002665 SDOperand StPtr = Node->getOperand(2);
Chris Lattnere10269b2005-01-17 19:25:26 +00002666
2667 // The chain has to be a load, the stored value must be an integer binary
2668 // operation with one use.
Chris Lattner5c659812005-01-17 22:10:42 +00002669 if (!StVal.Val->hasOneUse() || StVal.Val->getNumOperands() != 2 ||
Chris Lattnere10269b2005-01-17 19:25:26 +00002670 MVT::isFloatingPoint(StVal.getValueType()))
2671 return false;
2672
Chris Lattner5c659812005-01-17 22:10:42 +00002673 // Token chain must either be a factor node or the load to fold.
2674 if (Chain.getOpcode() != ISD::LOAD && Chain.getOpcode() != ISD::TokenFactor)
2675 return false;
Chris Lattnere10269b2005-01-17 19:25:26 +00002676
Chris Lattner5c659812005-01-17 22:10:42 +00002677 SDOperand TheLoad;
2678
2679 // Check to see if there is a load from the same pointer that we're storing
2680 // to in either operand of the binop.
2681 if (StVal.getOperand(0).getOpcode() == ISD::LOAD &&
2682 StVal.getOperand(0).getOperand(1) == StPtr)
2683 TheLoad = StVal.getOperand(0);
2684 else if (StVal.getOperand(1).getOpcode() == ISD::LOAD &&
2685 StVal.getOperand(1).getOperand(1) == StPtr)
2686 TheLoad = StVal.getOperand(1);
2687 else
2688 return false; // No matching load operand.
2689
2690 // We can only fold the load if there are no intervening side-effecting
2691 // operations. This means that the store uses the load as its token chain, or
2692 // there are only token factor nodes in between the store and load.
2693 if (Chain != TheLoad.getValue(1)) {
2694 // Okay, the other option is that we have a store referring to (possibly
2695 // nested) token factor nodes. For now, just try peeking through one level
2696 // of token factors to see if this is the case.
2697 bool ChainOk = false;
2698 if (Chain.getOpcode() == ISD::TokenFactor) {
2699 for (unsigned i = 0, e = Chain.getNumOperands(); i != e; ++i)
2700 if (Chain.getOperand(i) == TheLoad.getValue(1)) {
2701 ChainOk = true;
2702 break;
2703 }
2704 }
2705
2706 if (!ChainOk) return false;
2707 }
2708
2709 if (TheLoad.getOperand(1) != StPtr)
Chris Lattnere10269b2005-01-17 19:25:26 +00002710 return false;
2711
2712 // Make sure that one of the operands of the binop is the load, and that the
2713 // load folds into the binop.
2714 if (((StVal.getOperand(0) != TheLoad ||
2715 !isFoldableLoad(TheLoad, StVal.getOperand(1))) &&
2716 (StVal.getOperand(1) != TheLoad ||
2717 !isFoldableLoad(TheLoad, StVal.getOperand(0)))))
2718 return false;
2719
2720 // Finally, check to see if this is one of the ops we can handle!
2721 static const unsigned ADDTAB[] = {
2722 X86::ADD8mi, X86::ADD16mi, X86::ADD32mi,
2723 X86::ADD8mr, X86::ADD16mr, X86::ADD32mr,
2724 };
2725 static const unsigned SUBTAB[] = {
2726 X86::SUB8mi, X86::SUB16mi, X86::SUB32mi,
2727 X86::SUB8mr, X86::SUB16mr, X86::SUB32mr,
2728 };
2729 static const unsigned ANDTAB[] = {
2730 X86::AND8mi, X86::AND16mi, X86::AND32mi,
2731 X86::AND8mr, X86::AND16mr, X86::AND32mr,
2732 };
2733 static const unsigned ORTAB[] = {
2734 X86::OR8mi, X86::OR16mi, X86::OR32mi,
2735 X86::OR8mr, X86::OR16mr, X86::OR32mr,
2736 };
2737 static const unsigned XORTAB[] = {
2738 X86::XOR8mi, X86::XOR16mi, X86::XOR32mi,
2739 X86::XOR8mr, X86::XOR16mr, X86::XOR32mr,
2740 };
2741 static const unsigned SHLTAB[] = {
2742 X86::SHL8mi, X86::SHL16mi, X86::SHL32mi,
2743 /*Have to put the reg in CL*/0, 0, 0,
2744 };
2745 static const unsigned SARTAB[] = {
2746 X86::SAR8mi, X86::SAR16mi, X86::SAR32mi,
2747 /*Have to put the reg in CL*/0, 0, 0,
2748 };
2749 static const unsigned SHRTAB[] = {
2750 X86::SHR8mi, X86::SHR16mi, X86::SHR32mi,
2751 /*Have to put the reg in CL*/0, 0, 0,
2752 };
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002753
Chris Lattnere10269b2005-01-17 19:25:26 +00002754 const unsigned *TabPtr = 0;
2755 switch (StVal.getOpcode()) {
2756 default:
2757 std::cerr << "CANNOT [mem] op= val: ";
2758 StVal.Val->dump(); std::cerr << "\n";
2759 case ISD::MUL:
2760 case ISD::SDIV:
2761 case ISD::UDIV:
2762 case ISD::SREM:
2763 case ISD::UREM: return false;
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002764
Chris Lattnere10269b2005-01-17 19:25:26 +00002765 case ISD::ADD: TabPtr = ADDTAB; break;
2766 case ISD::SUB: TabPtr = SUBTAB; break;
2767 case ISD::AND: TabPtr = ANDTAB; break;
2768 case ISD:: OR: TabPtr = ORTAB; break;
2769 case ISD::XOR: TabPtr = XORTAB; break;
2770 case ISD::SHL: TabPtr = SHLTAB; break;
2771 case ISD::SRA: TabPtr = SARTAB; break;
2772 case ISD::SRL: TabPtr = SHRTAB; break;
2773 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002774
Chris Lattnere10269b2005-01-17 19:25:26 +00002775 // Handle: [mem] op= CST
2776 SDOperand Op0 = StVal.getOperand(0);
2777 SDOperand Op1 = StVal.getOperand(1);
Chris Lattner0a078832005-01-23 23:20:06 +00002778 unsigned Opc = 0;
Chris Lattnere10269b2005-01-17 19:25:26 +00002779 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1)) {
2780 switch (Op0.getValueType()) { // Use Op0's type because of shifts.
2781 default: break;
2782 case MVT::i1:
2783 case MVT::i8: Opc = TabPtr[0]; break;
2784 case MVT::i16: Opc = TabPtr[1]; break;
2785 case MVT::i32: Opc = TabPtr[2]; break;
2786 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002787
Chris Lattnere10269b2005-01-17 19:25:26 +00002788 if (Opc) {
Chris Lattner4a108662005-01-18 03:51:59 +00002789 if (!ExprMap.insert(std::make_pair(TheLoad.getValue(1), 1)).second)
2790 assert(0 && "Already emitted?");
Chris Lattner5c659812005-01-17 22:10:42 +00002791 Select(Chain);
2792
Chris Lattnere10269b2005-01-17 19:25:26 +00002793 X86AddressMode AM;
2794 if (getRegPressure(TheLoad.getOperand(0)) >
2795 getRegPressure(TheLoad.getOperand(1))) {
2796 Select(TheLoad.getOperand(0));
2797 SelectAddress(TheLoad.getOperand(1), AM);
2798 } else {
2799 SelectAddress(TheLoad.getOperand(1), AM);
2800 Select(TheLoad.getOperand(0));
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002801 }
Chris Lattner5c659812005-01-17 22:10:42 +00002802
2803 if (StVal.getOpcode() == ISD::ADD) {
2804 if (CN->getValue() == 1) {
2805 switch (Op0.getValueType()) {
2806 default: break;
2807 case MVT::i8:
2808 addFullAddress(BuildMI(BB, X86::INC8m, 4), AM);
2809 return true;
2810 case MVT::i16: Opc = TabPtr[1];
2811 addFullAddress(BuildMI(BB, X86::INC16m, 4), AM);
2812 return true;
2813 case MVT::i32: Opc = TabPtr[2];
2814 addFullAddress(BuildMI(BB, X86::INC32m, 4), AM);
2815 return true;
2816 }
2817 } else if (CN->getValue()+1 == 0) { // [X] += -1 -> DEC [X]
2818 switch (Op0.getValueType()) {
2819 default: break;
2820 case MVT::i8:
2821 addFullAddress(BuildMI(BB, X86::DEC8m, 4), AM);
2822 return true;
2823 case MVT::i16: Opc = TabPtr[1];
2824 addFullAddress(BuildMI(BB, X86::DEC16m, 4), AM);
2825 return true;
2826 case MVT::i32: Opc = TabPtr[2];
2827 addFullAddress(BuildMI(BB, X86::DEC32m, 4), AM);
2828 return true;
2829 }
2830 }
2831 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002832
Chris Lattnere10269b2005-01-17 19:25:26 +00002833 addFullAddress(BuildMI(BB, Opc, 4+1),AM).addImm(CN->getValue());
2834 return true;
2835 }
2836 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002837
Chris Lattnere10269b2005-01-17 19:25:26 +00002838 // If we have [mem] = V op [mem], try to turn it into:
2839 // [mem] = [mem] op V.
2840 if (Op1 == TheLoad && StVal.getOpcode() != ISD::SUB &&
2841 StVal.getOpcode() != ISD::SHL && StVal.getOpcode() != ISD::SRA &&
2842 StVal.getOpcode() != ISD::SRL)
2843 std::swap(Op0, Op1);
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002844
Chris Lattnere10269b2005-01-17 19:25:26 +00002845 if (Op0 != TheLoad) return false;
2846
2847 switch (Op0.getValueType()) {
2848 default: return false;
2849 case MVT::i1:
2850 case MVT::i8: Opc = TabPtr[3]; break;
2851 case MVT::i16: Opc = TabPtr[4]; break;
2852 case MVT::i32: Opc = TabPtr[5]; break;
2853 }
Chris Lattner5c659812005-01-17 22:10:42 +00002854
Chris Lattnerb422aea2005-01-18 17:35:28 +00002855 // Table entry doesn't exist?
2856 if (Opc == 0) return false;
2857
Chris Lattner4a108662005-01-18 03:51:59 +00002858 if (!ExprMap.insert(std::make_pair(TheLoad.getValue(1), 1)).second)
2859 assert(0 && "Already emitted?");
Chris Lattner5c659812005-01-17 22:10:42 +00002860 Select(Chain);
Chris Lattnere10269b2005-01-17 19:25:26 +00002861 Select(TheLoad.getOperand(0));
Chris Lattner98a8ba02005-01-18 01:06:26 +00002862
Chris Lattnere10269b2005-01-17 19:25:26 +00002863 X86AddressMode AM;
2864 SelectAddress(TheLoad.getOperand(1), AM);
2865 unsigned Reg = SelectExpr(Op1);
Chris Lattner98a8ba02005-01-18 01:06:26 +00002866 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addReg(Reg);
Chris Lattnere10269b2005-01-17 19:25:26 +00002867 return true;
2868}
2869
2870
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002871void ISel::Select(SDOperand N) {
2872 unsigned Tmp1, Tmp2, Opc;
2873
Nate Begeman85fdeb22005-03-24 04:39:54 +00002874 if (!ExprMap.insert(std::make_pair(N, 1)).second)
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002875 return; // Already selected.
2876
Chris Lattner989de032005-01-11 06:14:36 +00002877 SDNode *Node = N.Val;
2878
2879 switch (Node->getOpcode()) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002880 default:
Chris Lattner989de032005-01-11 06:14:36 +00002881 Node->dump(); std::cerr << "\n";
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002882 assert(0 && "Node not handled yet!");
2883 case ISD::EntryToken: return; // Noop
Chris Lattnerc3580712005-01-13 18:01:36 +00002884 case ISD::TokenFactor:
Chris Lattner1d50b7f2005-01-13 19:56:00 +00002885 if (Node->getNumOperands() == 2) {
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002886 bool OneFirst =
Chris Lattner1d50b7f2005-01-13 19:56:00 +00002887 getRegPressure(Node->getOperand(1))>getRegPressure(Node->getOperand(0));
2888 Select(Node->getOperand(OneFirst));
2889 Select(Node->getOperand(!OneFirst));
2890 } else {
2891 std::vector<std::pair<unsigned, unsigned> > OpsP;
2892 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
2893 OpsP.push_back(std::make_pair(getRegPressure(Node->getOperand(i)), i));
2894 std::sort(OpsP.begin(), OpsP.end());
2895 std::reverse(OpsP.begin(), OpsP.end());
2896 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
2897 Select(Node->getOperand(OpsP[i].second));
2898 }
Chris Lattnerc3580712005-01-13 18:01:36 +00002899 return;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002900 case ISD::CopyToReg:
Chris Lattneref6806c2005-01-12 02:02:48 +00002901 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2902 Select(N.getOperand(0));
2903 Tmp1 = SelectExpr(N.getOperand(1));
2904 } else {
2905 Tmp1 = SelectExpr(N.getOperand(1));
2906 Select(N.getOperand(0));
2907 }
Chris Lattner18c2f132005-01-13 20:50:02 +00002908 Tmp2 = cast<RegSDNode>(N)->getReg();
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002909
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002910 if (Tmp1 != Tmp2) {
2911 switch (N.getOperand(1).getValueType()) {
2912 default: assert(0 && "Invalid type for operation!");
2913 case MVT::i1:
2914 case MVT::i8: Opc = X86::MOV8rr; break;
2915 case MVT::i16: Opc = X86::MOV16rr; break;
2916 case MVT::i32: Opc = X86::MOV32rr; break;
Chris Lattneref7ba072005-01-11 03:50:45 +00002917 case MVT::f64: Opc = X86::FpMOV; ContainsFPCode = true; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002918 }
2919 BuildMI(BB, Opc, 1, Tmp2).addReg(Tmp1);
2920 }
2921 return;
2922 case ISD::RET:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002923 switch (N.getNumOperands()) {
2924 default:
2925 assert(0 && "Unknown return instruction!");
2926 case 3:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002927 assert(N.getOperand(1).getValueType() == MVT::i32 &&
2928 N.getOperand(2).getValueType() == MVT::i32 &&
2929 "Unknown two-register value!");
Chris Lattner11333092005-01-11 03:11:44 +00002930 if (getRegPressure(N.getOperand(1)) > getRegPressure(N.getOperand(2))) {
2931 Tmp1 = SelectExpr(N.getOperand(1));
2932 Tmp2 = SelectExpr(N.getOperand(2));
2933 } else {
2934 Tmp2 = SelectExpr(N.getOperand(2));
2935 Tmp1 = SelectExpr(N.getOperand(1));
2936 }
2937 Select(N.getOperand(0));
2938
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002939 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
2940 BuildMI(BB, X86::MOV32rr, 1, X86::EDX).addReg(Tmp2);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002941 break;
2942 case 2:
Chris Lattner11333092005-01-11 03:11:44 +00002943 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2944 Select(N.getOperand(0));
2945 Tmp1 = SelectExpr(N.getOperand(1));
2946 } else {
2947 Tmp1 = SelectExpr(N.getOperand(1));
2948 Select(N.getOperand(0));
2949 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002950 switch (N.getOperand(1).getValueType()) {
2951 default: assert(0 && "All other types should have been promoted!!");
2952 case MVT::f64:
2953 BuildMI(BB, X86::FpSETRESULT, 1).addReg(Tmp1);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002954 break;
2955 case MVT::i32:
2956 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002957 break;
2958 }
2959 break;
2960 case 1:
Chris Lattner11333092005-01-11 03:11:44 +00002961 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002962 break;
2963 }
2964 BuildMI(BB, X86::RET, 0); // Just emit a 'ret' instruction
2965 return;
2966 case ISD::BR: {
2967 Select(N.getOperand(0));
2968 MachineBasicBlock *Dest =
2969 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
2970 BuildMI(BB, X86::JMP, 1).addMBB(Dest);
2971 return;
2972 }
2973
2974 case ISD::BRCOND: {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002975 MachineBasicBlock *Dest =
2976 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
Chris Lattner11333092005-01-11 03:11:44 +00002977
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002978 // Try to fold a setcc into the branch. If this fails, emit a test/jne
2979 // pair.
Chris Lattner6c07aee2005-01-11 04:06:27 +00002980 if (EmitBranchCC(Dest, N.getOperand(0), N.getOperand(1))) {
2981 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2982 Select(N.getOperand(0));
2983 Tmp1 = SelectExpr(N.getOperand(1));
2984 } else {
2985 Tmp1 = SelectExpr(N.getOperand(1));
2986 Select(N.getOperand(0));
2987 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002988 BuildMI(BB, X86::TEST8rr, 2).addReg(Tmp1).addReg(Tmp1);
2989 BuildMI(BB, X86::JNE, 1).addMBB(Dest);
2990 }
Chris Lattner11333092005-01-11 03:11:44 +00002991
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002992 return;
2993 }
Chris Lattnere9ef81d2005-01-15 05:22:24 +00002994
Chris Lattner4df0de92005-01-17 00:00:33 +00002995 case ISD::LOAD:
2996 // If this load could be folded into the only using instruction, and if it
2997 // is safe to emit the instruction here, try to do so now.
2998 if (Node->hasNUsesOfValue(1, 0)) {
2999 SDOperand TheVal = N.getValue(0);
3000 SDNode *User = 0;
3001 for (SDNode::use_iterator UI = Node->use_begin(); ; ++UI) {
3002 assert(UI != Node->use_end() && "Didn't find use!");
3003 SDNode *UN = *UI;
3004 for (unsigned i = 0, e = UN->getNumOperands(); i != e; ++i)
3005 if (UN->getOperand(i) == TheVal) {
3006 User = UN;
3007 goto FoundIt;
3008 }
3009 }
3010 FoundIt:
3011 // Only handle unary operators right now.
3012 if (User->getNumOperands() == 1) {
Chris Lattner4a108662005-01-18 03:51:59 +00003013 ExprMap.erase(N);
Chris Lattner4df0de92005-01-17 00:00:33 +00003014 SelectExpr(SDOperand(User, 0));
3015 return;
3016 }
3017 }
Chris Lattnerb71f8fc2005-01-18 04:00:54 +00003018 ExprMap.erase(N);
Chris Lattner4df0de92005-01-17 00:00:33 +00003019 SelectExpr(N);
3020 return;
3021
Chris Lattnere9ef81d2005-01-15 05:22:24 +00003022 case ISD::EXTLOAD:
3023 case ISD::SEXTLOAD:
3024 case ISD::ZEXTLOAD:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003025 case ISD::CALL:
3026 case ISD::DYNAMIC_STACKALLOC:
Chris Lattnerb71f8fc2005-01-18 04:00:54 +00003027 ExprMap.erase(N);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003028 SelectExpr(N);
3029 return;
Chris Lattnere9ef81d2005-01-15 05:22:24 +00003030
3031 case ISD::TRUNCSTORE: { // truncstore chain, val, ptr :storety
3032 // On X86, we can represent all types except for Bool and Float natively.
3033 X86AddressMode AM;
3034 MVT::ValueType StoredTy = cast<MVTSDNode>(Node)->getExtraValueType();
Chris Lattnerda2ce112005-01-16 07:34:08 +00003035 assert((StoredTy == MVT::i1 || StoredTy == MVT::f32 ||
3036 StoredTy == MVT::i16 /*FIXME: THIS IS JUST FOR TESTING!*/)
3037 && "Unsupported TRUNCSTORE for this target!");
3038
3039 if (StoredTy == MVT::i16) {
3040 // FIXME: This is here just to allow testing. X86 doesn't really have a
3041 // TRUNCSTORE i16 operation, but this is required for targets that do not
3042 // have 16-bit integer registers. We occasionally disable 16-bit integer
3043 // registers to test the promotion code.
3044 Select(N.getOperand(0));
3045 Tmp1 = SelectExpr(N.getOperand(1));
3046 SelectAddress(N.getOperand(2), AM);
3047
3048 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
3049 addFullAddress(BuildMI(BB, X86::MOV16mr, 5), AM).addReg(X86::AX);
3050 return;
3051 }
Chris Lattnere9ef81d2005-01-15 05:22:24 +00003052
3053 // Store of constant bool?
3054 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
3055 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(2))) {
3056 Select(N.getOperand(0));
3057 SelectAddress(N.getOperand(2), AM);
3058 } else {
3059 SelectAddress(N.getOperand(2), AM);
3060 Select(N.getOperand(0));
3061 }
3062 addFullAddress(BuildMI(BB, X86::MOV8mi, 5), AM).addImm(CN->getValue());
3063 return;
3064 }
3065
3066 switch (StoredTy) {
3067 default: assert(0 && "Cannot truncstore this type!");
3068 case MVT::i1: Opc = X86::MOV8mr; break;
3069 case MVT::f32: Opc = X86::FST32m; break;
3070 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003071
Chris Lattnere9ef81d2005-01-15 05:22:24 +00003072 std::vector<std::pair<unsigned, unsigned> > RP;
3073 RP.push_back(std::make_pair(getRegPressure(N.getOperand(0)), 0));
3074 RP.push_back(std::make_pair(getRegPressure(N.getOperand(1)), 1));
3075 RP.push_back(std::make_pair(getRegPressure(N.getOperand(2)), 2));
3076 std::sort(RP.begin(), RP.end());
3077
Chris Lattner572dd082005-02-23 05:57:21 +00003078 Tmp1 = 0; // Silence a warning.
Chris Lattnere9ef81d2005-01-15 05:22:24 +00003079 for (unsigned i = 0; i != 3; ++i)
3080 switch (RP[2-i].second) {
3081 default: assert(0 && "Unknown operand number!");
3082 case 0: Select(N.getOperand(0)); break;
3083 case 1: Tmp1 = SelectExpr(N.getOperand(1)); break;
3084 case 2: SelectAddress(N.getOperand(2), AM); break;
3085 }
3086
3087 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addReg(Tmp1);
3088 return;
3089 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003090 case ISD::STORE: {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003091 X86AddressMode AM;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003092
3093 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
3094 Opc = 0;
3095 switch (CN->getValueType(0)) {
3096 default: assert(0 && "Invalid type for operation!");
3097 case MVT::i1:
3098 case MVT::i8: Opc = X86::MOV8mi; break;
3099 case MVT::i16: Opc = X86::MOV16mi; break;
3100 case MVT::i32: Opc = X86::MOV32mi; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003101 case MVT::f64: break;
3102 }
3103 if (Opc) {
Chris Lattner11333092005-01-11 03:11:44 +00003104 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(2))) {
3105 Select(N.getOperand(0));
3106 SelectAddress(N.getOperand(2), AM);
3107 } else {
3108 SelectAddress(N.getOperand(2), AM);
3109 Select(N.getOperand(0));
3110 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003111 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addImm(CN->getValue());
3112 return;
3113 }
Chris Lattner75f354b2005-04-21 19:03:24 +00003114 } else if (GlobalAddressSDNode *GA =
3115 dyn_cast<GlobalAddressSDNode>(N.getOperand(1))) {
3116 assert(GA->getValueType(0) == MVT::i32 && "Bad pointer operand");
3117
3118 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(2))) {
3119 Select(N.getOperand(0));
3120 SelectAddress(N.getOperand(2), AM);
3121 } else {
3122 SelectAddress(N.getOperand(2), AM);
3123 Select(N.getOperand(0));
3124 }
3125 addFullAddress(BuildMI(BB, X86::MOV32mi, 4+1),
3126 AM).addGlobalAddress(GA->getGlobal());
3127 return;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003128 }
Chris Lattner837caa72005-01-11 23:21:30 +00003129
3130 // Check to see if this is a load/op/store combination.
Chris Lattnere10269b2005-01-17 19:25:26 +00003131 if (TryToFoldLoadOpStore(Node))
3132 return;
Chris Lattner837caa72005-01-11 23:21:30 +00003133
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003134 switch (N.getOperand(1).getValueType()) {
3135 default: assert(0 && "Cannot store this type!");
3136 case MVT::i1:
3137 case MVT::i8: Opc = X86::MOV8mr; break;
3138 case MVT::i16: Opc = X86::MOV16mr; break;
3139 case MVT::i32: Opc = X86::MOV32mr; break;
Chris Lattneref7ba072005-01-11 03:50:45 +00003140 case MVT::f64: Opc = X86::FST64m; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003141 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003142
Chris Lattner11333092005-01-11 03:11:44 +00003143 std::vector<std::pair<unsigned, unsigned> > RP;
3144 RP.push_back(std::make_pair(getRegPressure(N.getOperand(0)), 0));
3145 RP.push_back(std::make_pair(getRegPressure(N.getOperand(1)), 1));
3146 RP.push_back(std::make_pair(getRegPressure(N.getOperand(2)), 2));
3147 std::sort(RP.begin(), RP.end());
3148
Chris Lattner572dd082005-02-23 05:57:21 +00003149 Tmp1 = 0; // Silence a warning.
Chris Lattner11333092005-01-11 03:11:44 +00003150 for (unsigned i = 0; i != 3; ++i)
3151 switch (RP[2-i].second) {
3152 default: assert(0 && "Unknown operand number!");
3153 case 0: Select(N.getOperand(0)); break;
3154 case 1: Tmp1 = SelectExpr(N.getOperand(1)); break;
Chris Lattnera3aa2e22005-01-11 03:37:59 +00003155 case 2: SelectAddress(N.getOperand(2), AM); break;
Chris Lattner11333092005-01-11 03:11:44 +00003156 }
3157
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003158 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addReg(Tmp1);
3159 return;
3160 }
3161 case ISD::ADJCALLSTACKDOWN:
3162 case ISD::ADJCALLSTACKUP:
3163 Select(N.getOperand(0));
3164 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003165
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003166 Opc = N.getOpcode() == ISD::ADJCALLSTACKDOWN ? X86::ADJCALLSTACKDOWN :
3167 X86::ADJCALLSTACKUP;
3168 BuildMI(BB, Opc, 1).addImm(Tmp1);
3169 return;
Chris Lattner989de032005-01-11 06:14:36 +00003170 case ISD::MEMSET: {
3171 Select(N.getOperand(0)); // Select the chain.
3172 unsigned Align =
3173 (unsigned)cast<ConstantSDNode>(Node->getOperand(4))->getValue();
3174 if (Align == 0) Align = 1;
3175
3176 // Turn the byte code into # iterations
3177 unsigned CountReg;
3178 unsigned Opcode;
3179 if (ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Node->getOperand(2))) {
3180 unsigned Val = ValC->getValue() & 255;
3181
3182 // If the value is a constant, then we can potentially use larger sets.
3183 switch (Align & 3) {
3184 case 2: // WORD aligned
3185 CountReg = MakeReg(MVT::i32);
3186 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
3187 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/2);
3188 } else {
3189 unsigned ByteReg = SelectExpr(Node->getOperand(3));
3190 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(1);
3191 }
3192 BuildMI(BB, X86::MOV16ri, 1, X86::AX).addImm((Val << 8) | Val);
3193 Opcode = X86::REP_STOSW;
3194 break;
3195 case 0: // DWORD aligned
3196 CountReg = MakeReg(MVT::i32);
3197 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
3198 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/4);
3199 } else {
3200 unsigned ByteReg = SelectExpr(Node->getOperand(3));
3201 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(2);
3202 }
3203 Val = (Val << 8) | Val;
3204 BuildMI(BB, X86::MOV32ri, 1, X86::EAX).addImm((Val << 16) | Val);
3205 Opcode = X86::REP_STOSD;
3206 break;
3207 default: // BYTE aligned
3208 CountReg = SelectExpr(Node->getOperand(3));
3209 BuildMI(BB, X86::MOV8ri, 1, X86::AL).addImm(Val);
3210 Opcode = X86::REP_STOSB;
3211 break;
3212 }
3213 } else {
3214 // If it's not a constant value we are storing, just fall back. We could
3215 // try to be clever to form 16 bit and 32 bit values, but we don't yet.
3216 unsigned ValReg = SelectExpr(Node->getOperand(2));
3217 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(ValReg);
3218 CountReg = SelectExpr(Node->getOperand(3));
3219 Opcode = X86::REP_STOSB;
3220 }
3221
3222 // No matter what the alignment is, we put the source in ESI, the
3223 // destination in EDI, and the count in ECX.
3224 unsigned TmpReg1 = SelectExpr(Node->getOperand(1));
3225 BuildMI(BB, X86::MOV32rr, 1, X86::ECX).addReg(CountReg);
3226 BuildMI(BB, X86::MOV32rr, 1, X86::EDI).addReg(TmpReg1);
3227 BuildMI(BB, Opcode, 0);
3228 return;
3229 }
Chris Lattner31805bf2005-01-11 06:19:26 +00003230 case ISD::MEMCPY:
3231 Select(N.getOperand(0)); // Select the chain.
3232 unsigned Align =
3233 (unsigned)cast<ConstantSDNode>(Node->getOperand(4))->getValue();
3234 if (Align == 0) Align = 1;
3235
3236 // Turn the byte code into # iterations
3237 unsigned CountReg;
3238 unsigned Opcode;
3239 switch (Align & 3) {
3240 case 2: // WORD aligned
3241 CountReg = MakeReg(MVT::i32);
3242 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
3243 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/2);
3244 } else {
3245 unsigned ByteReg = SelectExpr(Node->getOperand(3));
3246 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(1);
3247 }
3248 Opcode = X86::REP_MOVSW;
3249 break;
3250 case 0: // DWORD aligned
3251 CountReg = MakeReg(MVT::i32);
3252 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
3253 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/4);
3254 } else {
3255 unsigned ByteReg = SelectExpr(Node->getOperand(3));
3256 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(2);
3257 }
3258 Opcode = X86::REP_MOVSD;
3259 break;
3260 default: // BYTE aligned
3261 CountReg = SelectExpr(Node->getOperand(3));
3262 Opcode = X86::REP_MOVSB;
3263 break;
3264 }
3265
3266 // No matter what the alignment is, we put the source in ESI, the
3267 // destination in EDI, and the count in ECX.
3268 unsigned TmpReg1 = SelectExpr(Node->getOperand(1));
3269 unsigned TmpReg2 = SelectExpr(Node->getOperand(2));
3270 BuildMI(BB, X86::MOV32rr, 1, X86::ECX).addReg(CountReg);
3271 BuildMI(BB, X86::MOV32rr, 1, X86::EDI).addReg(TmpReg1);
3272 BuildMI(BB, X86::MOV32rr, 1, X86::ESI).addReg(TmpReg2);
3273 BuildMI(BB, Opcode, 0);
3274 return;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003275 }
3276 assert(0 && "Should not be reached!");
3277}
3278
3279
3280/// createX86PatternInstructionSelector - This pass converts an LLVM function
3281/// into a machine code representation using pattern matching and a machine
3282/// description file.
3283///
3284FunctionPass *llvm::createX86PatternInstructionSelector(TargetMachine &TM) {
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003285 return new ISel(TM);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003286}