blob: d7ea0e3c8e6cb69f2f462ecf403ffa9a4b00e30d [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 Lattner590d8002005-01-09 18:52:44 +000017#include "llvm/Constants.h" // FIXME: REMOVE
Chris Lattner8acb1ba2005-01-07 07:49:41 +000018#include "llvm/Function.h"
Chris Lattner590d8002005-01-09 18:52:44 +000019#include "llvm/CodeGen/MachineConstantPool.h" // FIXME: REMOVE
Chris Lattner8acb1ba2005-01-07 07:49:41 +000020#include "llvm/CodeGen/MachineFunction.h"
21#include "llvm/CodeGen/MachineFrameInfo.h"
22#include "llvm/CodeGen/SelectionDAG.h"
23#include "llvm/CodeGen/SelectionDAGISel.h"
24#include "llvm/CodeGen/SSARegMap.h"
25#include "llvm/Target/TargetData.h"
26#include "llvm/Target/TargetLowering.h"
27#include "llvm/Support/MathExtras.h"
28#include "llvm/ADT/Statistic.h"
29#include <set>
Jeff Cohen603fea92005-01-12 04:29:05 +000030#include <algorithm>
Chris Lattner8acb1ba2005-01-07 07:49:41 +000031using namespace llvm;
32
33//===----------------------------------------------------------------------===//
34// X86TargetLowering - X86 Implementation of the TargetLowering interface
35namespace {
36 class X86TargetLowering : public TargetLowering {
37 int VarArgsFrameIndex; // FrameIndex for start of varargs area.
Chris Lattner14824582005-01-09 00:01:27 +000038 int ReturnAddrIndex; // FrameIndex for return slot.
Chris Lattner8acb1ba2005-01-07 07:49:41 +000039 public:
40 X86TargetLowering(TargetMachine &TM) : TargetLowering(TM) {
41 // Set up the TargetLowering object.
Chris Lattner4df0de92005-01-17 00:00:33 +000042
43 // X86 is wierd, it always uses i8 for shift amounts and setcc results.
44 setShiftAmountType(MVT::i8);
45 setSetCCResultType(MVT::i8);
Chris Lattner6659bd72005-04-07 19:41:46 +000046 setSetCCResultContents(ZeroOrOneSetCCResult);
Chris Lattner009b55b2005-01-19 03:36:30 +000047 setShiftAmountFlavor(Mask); // shl X, 32 == shl X, 0
Chris Lattner4df0de92005-01-17 00:00:33 +000048
49 // Set up the register classes.
Chris Lattner8acb1ba2005-01-07 07:49:41 +000050 addRegisterClass(MVT::i8, X86::R8RegisterClass);
51 addRegisterClass(MVT::i16, X86::R16RegisterClass);
52 addRegisterClass(MVT::i32, X86::R32RegisterClass);
53 addRegisterClass(MVT::f64, X86::RFPRegisterClass);
Misha Brukman0e0a7a452005-04-21 23:38:14 +000054
Chris Lattner8acb1ba2005-01-07 07:49:41 +000055 // FIXME: Eliminate these two classes when legalize can handle promotions
56 // well.
Chris Lattnerda2ce112005-01-16 07:34:08 +000057/**/ addRegisterClass(MVT::i1, X86::R8RegisterClass);
Chris Lattnerda2ce112005-01-16 07:34:08 +000058
Chris Lattnerda4d4692005-04-09 03:22:37 +000059 setOperationAction(ISD::BRCONDTWOWAY , MVT::Other, Expand);
Chris Lattnerda2ce112005-01-16 07:34:08 +000060 setOperationAction(ISD::MEMMOVE , MVT::Other, Expand);
61 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Expand);
Chris Lattnerda2ce112005-01-16 07:34:08 +000062 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
Chris Lattnerda2ce112005-01-16 07:34:08 +000063 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
64 setOperationAction(ISD::SEXTLOAD , MVT::i1 , Expand);
65 setOperationAction(ISD::SREM , MVT::f64 , Expand);
Chris Lattner43fdea02005-04-02 05:03:24 +000066
Chris Lattnerda2ce112005-01-16 07:34:08 +000067 // These should be promoted to a larger select which is supported.
68/**/ setOperationAction(ISD::SELECT , MVT::i1 , Promote);
69 setOperationAction(ISD::SELECT , MVT::i8 , Promote);
Misha Brukman0e0a7a452005-04-21 23:38:14 +000070
Chris Lattner8acb1ba2005-01-07 07:49:41 +000071 computeRegisterProperties();
Misha Brukman0e0a7a452005-04-21 23:38:14 +000072
Chris Lattner8acb1ba2005-01-07 07:49:41 +000073 addLegalFPImmediate(+0.0); // FLD0
74 addLegalFPImmediate(+1.0); // FLD1
75 addLegalFPImmediate(-0.0); // FLD0/FCHS
76 addLegalFPImmediate(-1.0); // FLD1/FCHS
77 }
78
79 /// LowerArguments - This hook must be implemented to indicate how we should
80 /// lower the arguments for the specified function, into the specified DAG.
81 virtual std::vector<SDOperand>
82 LowerArguments(Function &F, SelectionDAG &DAG);
83
84 /// LowerCallTo - This hook lowers an abstract call to a function into an
85 /// actual call.
Chris Lattner5188ad72005-01-08 19:28:19 +000086 virtual std::pair<SDOperand, SDOperand>
Nate Begeman8e21e712005-03-26 01:29:23 +000087 LowerCallTo(SDOperand Chain, const Type *RetTy, bool isVarArg,
88 SDOperand Callee, ArgListTy &Args, SelectionDAG &DAG);
Chris Lattner14824582005-01-09 00:01:27 +000089
90 virtual std::pair<SDOperand, SDOperand>
91 LowerVAStart(SDOperand Chain, SelectionDAG &DAG);
92
93 virtual std::pair<SDOperand,SDOperand>
94 LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
95 const Type *ArgTy, SelectionDAG &DAG);
96
97 virtual std::pair<SDOperand, SDOperand>
98 LowerFrameReturnAddress(bool isFrameAddr, SDOperand Chain, unsigned Depth,
99 SelectionDAG &DAG);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000100 };
101}
102
103
104std::vector<SDOperand>
105X86TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
106 std::vector<SDOperand> ArgValues;
107
108 // Add DAG nodes to load the arguments... On entry to a function on the X86,
109 // the stack frame looks like this:
110 //
111 // [ESP] -- return address
112 // [ESP + 4] -- first argument (leftmost lexically)
113 // [ESP + 8] -- second argument, if first argument is four bytes in size
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000114 // ...
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000115 //
116 MachineFunction &MF = DAG.getMachineFunction();
117 MachineFrameInfo *MFI = MF.getFrameInfo();
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000118
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000119 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
Chris Lattnere4d5c442005-03-15 04:54:21 +0000120 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000121 MVT::ValueType ObjectVT = getValueType(I->getType());
122 unsigned ArgIncrement = 4;
123 unsigned ObjSize;
124 switch (ObjectVT) {
125 default: assert(0 && "Unhandled argument type!");
126 case MVT::i1:
127 case MVT::i8: ObjSize = 1; break;
128 case MVT::i16: ObjSize = 2; break;
129 case MVT::i32: ObjSize = 4; break;
130 case MVT::i64: ObjSize = ArgIncrement = 8; break;
131 case MVT::f32: ObjSize = 4; break;
132 case MVT::f64: ObjSize = ArgIncrement = 8; break;
133 }
134 // Create the frame index object for this incoming parameter...
135 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000136
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000137 // Create the SelectionDAG nodes corresponding to a load from this parameter
138 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
139
140 // Don't codegen dead arguments. FIXME: remove this check when we can nuke
141 // dead loads.
142 SDOperand ArgValue;
143 if (!I->use_empty())
Andrew Lenharth2d86ea22005-04-27 20:10:01 +0000144 ArgValue = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN, DAG.getSrcValue(NULL));
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000145 else {
146 if (MVT::isInteger(ObjectVT))
147 ArgValue = DAG.getConstant(0, ObjectVT);
148 else
149 ArgValue = DAG.getConstantFP(0, ObjectVT);
150 }
151 ArgValues.push_back(ArgValue);
152
153 ArgOffset += ArgIncrement; // Move on to the next argument...
154 }
155
156 // If the function takes variable number of arguments, make a frame index for
157 // the start of the first vararg value... for expansion of llvm.va_start.
158 if (F.isVarArg())
159 VarArgsFrameIndex = MFI->CreateFixedObject(1, ArgOffset);
Chris Lattner14824582005-01-09 00:01:27 +0000160 ReturnAddrIndex = 0; // No return address slot generated yet.
Chris Lattner4c52f0e2005-04-09 15:23:56 +0000161
162 // Finally, inform the code generator which regs we return values in.
163 switch (getValueType(F.getReturnType())) {
164 default: assert(0 && "Unknown type!");
165 case MVT::isVoid: break;
166 case MVT::i1:
167 case MVT::i8:
168 case MVT::i16:
169 case MVT::i32:
170 MF.addLiveOut(X86::EAX);
171 break;
172 case MVT::i64:
173 MF.addLiveOut(X86::EAX);
174 MF.addLiveOut(X86::EDX);
175 break;
176 case MVT::f32:
177 case MVT::f64:
178 MF.addLiveOut(X86::ST0);
179 break;
180 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000181 return ArgValues;
182}
183
Chris Lattner5188ad72005-01-08 19:28:19 +0000184std::pair<SDOperand, SDOperand>
185X86TargetLowering::LowerCallTo(SDOperand Chain,
Nate Begeman8e21e712005-03-26 01:29:23 +0000186 const Type *RetTy, bool isVarArg,
187 SDOperand Callee, ArgListTy &Args, SelectionDAG &DAG) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000188 // Count how many bytes are to be pushed on the stack.
189 unsigned NumBytes = 0;
190
191 if (Args.empty()) {
192 // Save zero bytes.
Chris Lattner5188ad72005-01-08 19:28:19 +0000193 Chain = DAG.getNode(ISD::ADJCALLSTACKDOWN, MVT::Other, Chain,
194 DAG.getConstant(0, getPointerTy()));
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000195 } else {
196 for (unsigned i = 0, e = Args.size(); i != e; ++i)
197 switch (getValueType(Args[i].second)) {
198 default: assert(0 && "Unknown value type!");
199 case MVT::i1:
200 case MVT::i8:
201 case MVT::i16:
202 case MVT::i32:
203 case MVT::f32:
204 NumBytes += 4;
205 break;
206 case MVT::i64:
207 case MVT::f64:
208 NumBytes += 8;
209 break;
210 }
211
Chris Lattner5188ad72005-01-08 19:28:19 +0000212 Chain = DAG.getNode(ISD::ADJCALLSTACKDOWN, MVT::Other, Chain,
213 DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000214
215 // Arguments go on the stack in reverse order, as specified by the ABI.
216 unsigned ArgOffset = 0;
Chris Lattner7f2afac2005-01-14 22:37:41 +0000217 SDOperand StackPtr = DAG.getCopyFromReg(X86::ESP, MVT::i32,
218 DAG.getEntryNode());
Chris Lattnerb62e1e22005-01-21 19:46:38 +0000219 std::vector<SDOperand> Stores;
220
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000221 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
222 unsigned ArgReg;
223 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
224 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
225
226 switch (getValueType(Args[i].second)) {
227 default: assert(0 && "Unexpected ValueType for argument!");
228 case MVT::i1:
229 case MVT::i8:
230 case MVT::i16:
231 // Promote the integer to 32 bits. If the input type is signed use a
232 // sign extend, otherwise use a zero extend.
233 if (Args[i].second->isSigned())
234 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
235 else
236 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
237
238 // FALL THROUGH
239 case MVT::i32:
240 case MVT::f32:
Chris Lattnerb62e1e22005-01-21 19:46:38 +0000241 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +0000242 Args[i].first, PtrOff, DAG.getSrcValue(NULL)));
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000243 ArgOffset += 4;
244 break;
245 case MVT::i64:
246 case MVT::f64:
Chris Lattnerb62e1e22005-01-21 19:46:38 +0000247 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +0000248 Args[i].first, PtrOff, DAG.getSrcValue(NULL)));
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000249 ArgOffset += 8;
250 break;
251 }
252 }
Chris Lattnerb62e1e22005-01-21 19:46:38 +0000253 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, Stores);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000254 }
255
256 std::vector<MVT::ValueType> RetVals;
257 MVT::ValueType RetTyVT = getValueType(RetTy);
258 if (RetTyVT != MVT::isVoid)
259 RetVals.push_back(RetTyVT);
260 RetVals.push_back(MVT::Other);
261
Chris Lattner5188ad72005-01-08 19:28:19 +0000262 SDOperand TheCall = SDOperand(DAG.getCall(RetVals, Chain, Callee), 0);
Chris Lattnerb0802652005-01-08 20:51:36 +0000263 Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
Chris Lattner5188ad72005-01-08 19:28:19 +0000264 Chain = DAG.getNode(ISD::ADJCALLSTACKUP, MVT::Other, Chain,
265 DAG.getConstant(NumBytes, getPointerTy()));
266 return std::make_pair(TheCall, Chain);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000267}
268
Chris Lattner14824582005-01-09 00:01:27 +0000269std::pair<SDOperand, SDOperand>
270X86TargetLowering::LowerVAStart(SDOperand Chain, SelectionDAG &DAG) {
271 // vastart just returns the address of the VarArgsFrameIndex slot.
272 return std::make_pair(DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32), Chain);
273}
274
275std::pair<SDOperand,SDOperand> X86TargetLowering::
276LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
277 const Type *ArgTy, SelectionDAG &DAG) {
278 MVT::ValueType ArgVT = getValueType(ArgTy);
279 SDOperand Result;
280 if (!isVANext) {
Andrew Lenharth2d86ea22005-04-27 20:10:01 +0000281 Result = DAG.getLoad(ArgVT, DAG.getEntryNode(), VAList, DAG.getSrcValue(NULL));
Chris Lattner14824582005-01-09 00:01:27 +0000282 } else {
283 unsigned Amt;
284 if (ArgVT == MVT::i32)
285 Amt = 4;
286 else {
287 assert((ArgVT == MVT::i64 || ArgVT == MVT::f64) &&
288 "Other types should have been promoted for varargs!");
289 Amt = 8;
290 }
291 Result = DAG.getNode(ISD::ADD, VAList.getValueType(), VAList,
292 DAG.getConstant(Amt, VAList.getValueType()));
293 }
294 return std::make_pair(Result, Chain);
295}
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000296
Chris Lattner14824582005-01-09 00:01:27 +0000297
298std::pair<SDOperand, SDOperand> X86TargetLowering::
299LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
300 SelectionDAG &DAG) {
301 SDOperand Result;
302 if (Depth) // Depths > 0 not supported yet!
303 Result = DAG.getConstant(0, getPointerTy());
304 else {
305 if (ReturnAddrIndex == 0) {
306 // Set up a frame object for the return address.
307 MachineFunction &MF = DAG.getMachineFunction();
308 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(4, -4);
309 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000310
Chris Lattner14824582005-01-09 00:01:27 +0000311 SDOperand RetAddrFI = DAG.getFrameIndex(ReturnAddrIndex, MVT::i32);
312
313 if (!isFrameAddress)
314 // Just load the return address
Andrew Lenharth2d86ea22005-04-27 20:10:01 +0000315 Result = DAG.getLoad(MVT::i32, DAG.getEntryNode(), RetAddrFI, DAG.getSrcValue(NULL));
Chris Lattner14824582005-01-09 00:01:27 +0000316 else
317 Result = DAG.getNode(ISD::SUB, MVT::i32, RetAddrFI,
318 DAG.getConstant(4, MVT::i32));
319 }
320 return std::make_pair(Result, Chain);
321}
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000322
323
Chris Lattner98a8ba02005-01-18 01:06:26 +0000324namespace {
325 /// X86ISelAddressMode - This corresponds to X86AddressMode, but uses
326 /// SDOperand's instead of register numbers for the leaves of the matched
327 /// tree.
328 struct X86ISelAddressMode {
329 enum {
330 RegBase,
331 FrameIndexBase,
332 } BaseType;
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000333
Chris Lattner98a8ba02005-01-18 01:06:26 +0000334 struct { // This is really a union, discriminated by BaseType!
335 SDOperand Reg;
336 int FrameIndex;
337 } Base;
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000338
Chris Lattner98a8ba02005-01-18 01:06:26 +0000339 unsigned Scale;
340 SDOperand IndexReg;
341 unsigned Disp;
342 GlobalValue *GV;
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000343
Chris Lattner98a8ba02005-01-18 01:06:26 +0000344 X86ISelAddressMode()
345 : BaseType(RegBase), Scale(1), IndexReg(), Disp(), GV(0) {
346 }
347 };
348}
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000349
350
351namespace {
352 Statistic<>
353 NumFPKill("x86-codegen", "Number of FP_REG_KILL instructions added");
354
355 //===--------------------------------------------------------------------===//
356 /// ISel - X86 specific code to select X86 machine instructions for
357 /// SelectionDAG operations.
358 ///
359 class ISel : public SelectionDAGISel {
360 /// ContainsFPCode - Every instruction we select that uses or defines a FP
361 /// register should set this to true.
362 bool ContainsFPCode;
363
364 /// X86Lowering - This object fully describes how to lower LLVM code to an
365 /// X86-specific SelectionDAG.
366 X86TargetLowering X86Lowering;
367
Chris Lattner11333092005-01-11 03:11:44 +0000368 /// RegPressureMap - This keeps an approximate count of the number of
369 /// registers required to evaluate each node in the graph.
370 std::map<SDNode*, unsigned> RegPressureMap;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000371
372 /// ExprMap - As shared expressions are codegen'd, we keep track of which
373 /// vreg the value is produced in, so we only emit one copy of each compiled
374 /// tree.
375 std::map<SDOperand, unsigned> ExprMap;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000376
377 public:
378 ISel(TargetMachine &TM) : SelectionDAGISel(X86Lowering), X86Lowering(TM) {
379 }
380
Chris Lattner67b1c3c2005-01-21 21:35:14 +0000381 virtual const char *getPassName() const {
382 return "X86 Pattern Instruction Selection";
383 }
384
Chris Lattner11333092005-01-11 03:11:44 +0000385 unsigned getRegPressure(SDOperand O) {
386 return RegPressureMap[O.Val];
387 }
388 unsigned ComputeRegPressure(SDOperand O);
389
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000390 /// InstructionSelectBasicBlock - This callback is invoked by
391 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
Chris Lattner7dbcb752005-01-12 04:21:28 +0000392 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000393
Chris Lattner44129b52005-01-25 20:03:11 +0000394 bool isFoldableLoad(SDOperand Op, SDOperand OtherOp,
395 bool FloatPromoteOk = false);
Chris Lattnera5ade062005-01-11 21:19:59 +0000396 void EmitFoldedLoad(SDOperand Op, X86AddressMode &AM);
Chris Lattnere10269b2005-01-17 19:25:26 +0000397 bool TryToFoldLoadOpStore(SDNode *Node);
Chris Lattnera5ade062005-01-11 21:19:59 +0000398
Chris Lattner30ea1e92005-01-19 07:37:26 +0000399 bool EmitOrOpOp(SDOperand Op1, SDOperand Op2, unsigned DestReg);
Chris Lattnercb1aa8d2005-01-17 01:34:14 +0000400 void EmitCMP(SDOperand LHS, SDOperand RHS, bool isOnlyUse);
Chris Lattner6c07aee2005-01-11 04:06:27 +0000401 bool EmitBranchCC(MachineBasicBlock *Dest, SDOperand Chain, SDOperand Cond);
Chris Lattner24aad1b2005-01-10 22:10:13 +0000402 void EmitSelectCC(SDOperand Cond, MVT::ValueType SVT,
403 unsigned RTrue, unsigned RFalse, unsigned RDest);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000404 unsigned SelectExpr(SDOperand N);
Chris Lattner98a8ba02005-01-18 01:06:26 +0000405
406 X86AddressMode SelectAddrExprs(const X86ISelAddressMode &IAM);
407 bool MatchAddress(SDOperand N, X86ISelAddressMode &AM);
408 void SelectAddress(SDOperand N, X86AddressMode &AM);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000409 void Select(SDOperand N);
410 };
411}
412
Chris Lattner7dbcb752005-01-12 04:21:28 +0000413/// InstructionSelectBasicBlock - This callback is invoked by SelectionDAGISel
414/// when it has created a SelectionDAG for us to codegen.
415void ISel::InstructionSelectBasicBlock(SelectionDAG &DAG) {
416 // While we're doing this, keep track of whether we see any FP code for
417 // FP_REG_KILL insertion.
418 ContainsFPCode = false;
419
420 // Scan the PHI nodes that already are inserted into this basic block. If any
421 // of them is a PHI of a floating point value, we need to insert an
422 // FP_REG_KILL.
423 SSARegMap *RegMap = BB->getParent()->getSSARegMap();
424 for (MachineBasicBlock::iterator I = BB->begin(), E = BB->end();
425 I != E; ++I) {
426 assert(I->getOpcode() == X86::PHI &&
427 "Isn't just PHI nodes?");
428 if (RegMap->getRegClass(I->getOperand(0).getReg()) ==
429 X86::RFPRegisterClass) {
430 ContainsFPCode = true;
431 break;
432 }
433 }
434
435 // Compute the RegPressureMap, which is an approximation for the number of
436 // registers required to compute each node.
437 ComputeRegPressure(DAG.getRoot());
438
439 // Codegen the basic block.
440 Select(DAG.getRoot());
441
442 // Finally, look at all of the successors of this block. If any contain a PHI
443 // node of FP type, we need to insert an FP_REG_KILL in this block.
444 for (MachineBasicBlock::succ_iterator SI = BB->succ_begin(),
445 E = BB->succ_end(); SI != E && !ContainsFPCode; ++SI)
446 for (MachineBasicBlock::iterator I = (*SI)->begin(), E = (*SI)->end();
447 I != E && I->getOpcode() == X86::PHI; ++I) {
448 if (RegMap->getRegClass(I->getOperand(0).getReg()) ==
449 X86::RFPRegisterClass) {
450 ContainsFPCode = true;
451 break;
452 }
453 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000454
Chris Lattner7dbcb752005-01-12 04:21:28 +0000455 // Insert FP_REG_KILL instructions into basic blocks that need them. This
456 // only occurs due to the floating point stackifier not being aggressive
457 // enough to handle arbitrary global stackification.
458 //
459 // Currently we insert an FP_REG_KILL instruction into each block that uses or
460 // defines a floating point virtual register.
461 //
462 // When the global register allocators (like linear scan) finally update live
463 // variable analysis, we can keep floating point values in registers across
464 // basic blocks. This will be a huge win, but we are waiting on the global
465 // allocators before we can do this.
466 //
Chris Lattner71df3f82005-03-30 01:10:00 +0000467 if (ContainsFPCode) {
Chris Lattner7dbcb752005-01-12 04:21:28 +0000468 BuildMI(*BB, BB->getFirstTerminator(), X86::FP_REG_KILL, 0);
469 ++NumFPKill;
470 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000471
Chris Lattner7dbcb752005-01-12 04:21:28 +0000472 // Clear state used for selection.
473 ExprMap.clear();
Chris Lattner7dbcb752005-01-12 04:21:28 +0000474 RegPressureMap.clear();
475}
476
477
Chris Lattner11333092005-01-11 03:11:44 +0000478// ComputeRegPressure - Compute the RegPressureMap, which is an approximation
479// for the number of registers required to compute each node. This is basically
480// computing a generalized form of the Sethi-Ullman number for each node.
481unsigned ISel::ComputeRegPressure(SDOperand O) {
482 SDNode *N = O.Val;
483 unsigned &Result = RegPressureMap[N];
484 if (Result) return Result;
485
Chris Lattnera3aa2e22005-01-11 03:37:59 +0000486 // FIXME: Should operations like CALL (which clobber lots o regs) have a
487 // higher fixed cost??
488
Chris Lattnerc4b6a782005-01-11 22:29:12 +0000489 if (N->getNumOperands() == 0) {
490 Result = 1;
491 } else {
492 unsigned MaxRegUse = 0;
493 unsigned NumExtraMaxRegUsers = 0;
494 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
495 unsigned Regs;
496 if (N->getOperand(i).getOpcode() == ISD::Constant)
497 Regs = 0;
498 else
499 Regs = ComputeRegPressure(N->getOperand(i));
500 if (Regs > MaxRegUse) {
501 MaxRegUse = Regs;
502 NumExtraMaxRegUsers = 0;
503 } else if (Regs == MaxRegUse &&
504 N->getOperand(i).getValueType() != MVT::Other) {
505 ++NumExtraMaxRegUsers;
506 }
Chris Lattner11333092005-01-11 03:11:44 +0000507 }
Chris Lattner90d1be72005-01-17 22:56:09 +0000508
509 if (O.getOpcode() != ISD::TokenFactor)
510 Result = MaxRegUse+NumExtraMaxRegUsers;
511 else
Chris Lattner869e0432005-01-17 23:02:13 +0000512 Result = MaxRegUse == 1 ? 0 : MaxRegUse-1;
Chris Lattnerc4b6a782005-01-11 22:29:12 +0000513 }
Chris Lattnerafce4302005-01-12 02:19:06 +0000514
Chris Lattner837caa72005-01-11 23:21:30 +0000515 //std::cerr << " WEIGHT: " << Result << " "; N->dump(); std::cerr << "\n";
Chris Lattnerc4b6a782005-01-11 22:29:12 +0000516 return Result;
Chris Lattner11333092005-01-11 03:11:44 +0000517}
518
Chris Lattnerbf52d492005-01-20 16:50:16 +0000519/// NodeTransitivelyUsesValue - Return true if N or any of its uses uses Op.
520/// The DAG cannot have cycles in it, by definition, so the visited set is not
521/// needed to prevent infinite loops. The DAG CAN, however, have unbounded
522/// reuse, so it prevents exponential cases.
523///
524static bool NodeTransitivelyUsesValue(SDOperand N, SDOperand Op,
525 std::set<SDNode*> &Visited) {
526 if (N == Op) return true; // Found it.
527 SDNode *Node = N.Val;
Chris Lattnerfb0f53f2005-01-21 21:43:02 +0000528 if (Node->getNumOperands() == 0 || // Leaf?
529 Node->getNodeDepth() <= Op.getNodeDepth()) return false; // Can't find it?
Chris Lattnerbf52d492005-01-20 16:50:16 +0000530 if (!Visited.insert(Node).second) return false; // Already visited?
531
532 // Recurse for the first N-1 operands.
533 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i)
534 if (NodeTransitivelyUsesValue(Node->getOperand(i), Op, Visited))
535 return true;
536
537 // Tail recurse for the last operand.
538 return NodeTransitivelyUsesValue(Node->getOperand(0), Op, Visited);
539}
540
Chris Lattner98a8ba02005-01-18 01:06:26 +0000541X86AddressMode ISel::SelectAddrExprs(const X86ISelAddressMode &IAM) {
542 X86AddressMode Result;
543
544 // If we need to emit two register operands, emit the one with the highest
545 // register pressure first.
546 if (IAM.BaseType == X86ISelAddressMode::RegBase &&
547 IAM.Base.Reg.Val && IAM.IndexReg.Val) {
Chris Lattnerbf52d492005-01-20 16:50:16 +0000548 bool EmitBaseThenIndex;
Chris Lattner98a8ba02005-01-18 01:06:26 +0000549 if (getRegPressure(IAM.Base.Reg) > getRegPressure(IAM.IndexReg)) {
Chris Lattnerbf52d492005-01-20 16:50:16 +0000550 std::set<SDNode*> Visited;
551 EmitBaseThenIndex = true;
552 // If Base ends up pointing to Index, we must emit index first. This is
553 // because of the way we fold loads, we may end up doing bad things with
554 // the folded add.
555 if (NodeTransitivelyUsesValue(IAM.Base.Reg, IAM.IndexReg, Visited))
556 EmitBaseThenIndex = false;
557 } else {
558 std::set<SDNode*> Visited;
559 EmitBaseThenIndex = false;
560 // If Base ends up pointing to Index, we must emit index first. This is
561 // because of the way we fold loads, we may end up doing bad things with
562 // the folded add.
563 if (NodeTransitivelyUsesValue(IAM.IndexReg, IAM.Base.Reg, Visited))
564 EmitBaseThenIndex = true;
565 }
566
567 if (EmitBaseThenIndex) {
Chris Lattner98a8ba02005-01-18 01:06:26 +0000568 Result.Base.Reg = SelectExpr(IAM.Base.Reg);
569 Result.IndexReg = SelectExpr(IAM.IndexReg);
570 } else {
571 Result.IndexReg = SelectExpr(IAM.IndexReg);
572 Result.Base.Reg = SelectExpr(IAM.Base.Reg);
573 }
Chris Lattnerbf52d492005-01-20 16:50:16 +0000574
Chris Lattner98a8ba02005-01-18 01:06:26 +0000575 } else if (IAM.BaseType == X86ISelAddressMode::RegBase && IAM.Base.Reg.Val) {
576 Result.Base.Reg = SelectExpr(IAM.Base.Reg);
577 } else if (IAM.IndexReg.Val) {
578 Result.IndexReg = SelectExpr(IAM.IndexReg);
579 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000580
Chris Lattner98a8ba02005-01-18 01:06:26 +0000581 switch (IAM.BaseType) {
582 case X86ISelAddressMode::RegBase:
583 Result.BaseType = X86AddressMode::RegBase;
584 break;
585 case X86ISelAddressMode::FrameIndexBase:
586 Result.BaseType = X86AddressMode::FrameIndexBase;
587 Result.Base.FrameIndex = IAM.Base.FrameIndex;
588 break;
589 default:
590 assert(0 && "Unknown base type!");
591 break;
592 }
593 Result.Scale = IAM.Scale;
594 Result.Disp = IAM.Disp;
595 Result.GV = IAM.GV;
596 return Result;
597}
598
599/// SelectAddress - Pattern match the maximal addressing mode for this node and
600/// emit all of the leaf registers.
601void ISel::SelectAddress(SDOperand N, X86AddressMode &AM) {
602 X86ISelAddressMode IAM;
603 MatchAddress(N, IAM);
604 AM = SelectAddrExprs(IAM);
605}
606
607/// MatchAddress - Add the specified node to the specified addressing mode,
608/// returning true if it cannot be done. This just pattern matches for the
609/// addressing mode, it does not cause any code to be emitted. For that, use
610/// SelectAddress.
611bool ISel::MatchAddress(SDOperand N, X86ISelAddressMode &AM) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000612 switch (N.getOpcode()) {
613 default: break;
614 case ISD::FrameIndex:
Chris Lattner98a8ba02005-01-18 01:06:26 +0000615 if (AM.BaseType == X86ISelAddressMode::RegBase && AM.Base.Reg.Val == 0) {
616 AM.BaseType = X86ISelAddressMode::FrameIndexBase;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000617 AM.Base.FrameIndex = cast<FrameIndexSDNode>(N)->getIndex();
618 return false;
619 }
620 break;
621 case ISD::GlobalAddress:
622 if (AM.GV == 0) {
623 AM.GV = cast<GlobalAddressSDNode>(N)->getGlobal();
624 return false;
625 }
626 break;
627 case ISD::Constant:
628 AM.Disp += cast<ConstantSDNode>(N)->getValue();
629 return false;
630 case ISD::SHL:
Chris Lattner636e79a2005-01-13 05:53:16 +0000631 // We might have folded the load into this shift, so don't regen the value
632 // if so.
633 if (ExprMap.count(N)) break;
634
Chris Lattner98a8ba02005-01-18 01:06:26 +0000635 if (AM.IndexReg.Val == 0 && AM.Scale == 1)
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000636 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1))) {
637 unsigned Val = CN->getValue();
638 if (Val == 1 || Val == 2 || Val == 3) {
639 AM.Scale = 1 << Val;
Chris Lattner51a26342005-01-11 06:36:20 +0000640 SDOperand ShVal = N.Val->getOperand(0);
641
642 // Okay, we know that we have a scale by now. However, if the scaled
643 // value is an add of something and a constant, we can fold the
644 // constant into the disp field here.
Chris Lattner811482a2005-01-18 04:18:32 +0000645 if (ShVal.Val->getOpcode() == ISD::ADD && ShVal.hasOneUse() &&
Chris Lattner51a26342005-01-11 06:36:20 +0000646 isa<ConstantSDNode>(ShVal.Val->getOperand(1))) {
Chris Lattner98a8ba02005-01-18 01:06:26 +0000647 AM.IndexReg = ShVal.Val->getOperand(0);
Chris Lattner51a26342005-01-11 06:36:20 +0000648 ConstantSDNode *AddVal =
649 cast<ConstantSDNode>(ShVal.Val->getOperand(1));
650 AM.Disp += AddVal->getValue() << Val;
Chris Lattner636e79a2005-01-13 05:53:16 +0000651 } else {
Chris Lattner98a8ba02005-01-18 01:06:26 +0000652 AM.IndexReg = ShVal;
Chris Lattner51a26342005-01-11 06:36:20 +0000653 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000654 return false;
655 }
656 }
657 break;
Chris Lattner947d5442005-01-11 19:37:02 +0000658 case ISD::MUL:
Chris Lattner636e79a2005-01-13 05:53:16 +0000659 // We might have folded the load into this mul, so don't regen the value if
660 // so.
661 if (ExprMap.count(N)) break;
662
Chris Lattner947d5442005-01-11 19:37:02 +0000663 // X*[3,5,9] -> X+X*[2,4,8]
Chris Lattner98a8ba02005-01-18 01:06:26 +0000664 if (AM.IndexReg.Val == 0 && AM.BaseType == X86ISelAddressMode::RegBase &&
665 AM.Base.Reg.Val == 0)
Chris Lattner947d5442005-01-11 19:37:02 +0000666 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1)))
667 if (CN->getValue() == 3 || CN->getValue() == 5 || CN->getValue() == 9) {
668 AM.Scale = unsigned(CN->getValue())-1;
669
670 SDOperand MulVal = N.Val->getOperand(0);
Chris Lattner98a8ba02005-01-18 01:06:26 +0000671 SDOperand Reg;
Chris Lattner947d5442005-01-11 19:37:02 +0000672
673 // Okay, we know that we have a scale by now. However, if the scaled
674 // value is an add of something and a constant, we can fold the
675 // constant into the disp field here.
Chris Lattner811482a2005-01-18 04:18:32 +0000676 if (MulVal.Val->getOpcode() == ISD::ADD && MulVal.hasOneUse() &&
Chris Lattner947d5442005-01-11 19:37:02 +0000677 isa<ConstantSDNode>(MulVal.Val->getOperand(1))) {
Chris Lattner98a8ba02005-01-18 01:06:26 +0000678 Reg = MulVal.Val->getOperand(0);
Chris Lattner947d5442005-01-11 19:37:02 +0000679 ConstantSDNode *AddVal =
680 cast<ConstantSDNode>(MulVal.Val->getOperand(1));
681 AM.Disp += AddVal->getValue() * CN->getValue();
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000682 } else {
Chris Lattner98a8ba02005-01-18 01:06:26 +0000683 Reg = N.Val->getOperand(0);
Chris Lattner947d5442005-01-11 19:37:02 +0000684 }
685
686 AM.IndexReg = AM.Base.Reg = Reg;
687 return false;
688 }
689 break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000690
691 case ISD::ADD: {
Chris Lattner636e79a2005-01-13 05:53:16 +0000692 // We might have folded the load into this mul, so don't regen the value if
693 // so.
694 if (ExprMap.count(N)) break;
695
Chris Lattner98a8ba02005-01-18 01:06:26 +0000696 X86ISelAddressMode Backup = AM;
697 if (!MatchAddress(N.Val->getOperand(0), AM) &&
698 !MatchAddress(N.Val->getOperand(1), AM))
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000699 return false;
700 AM = Backup;
Chris Lattner98a8ba02005-01-18 01:06:26 +0000701 if (!MatchAddress(N.Val->getOperand(1), AM) &&
702 !MatchAddress(N.Val->getOperand(0), AM))
Chris Lattner9bbd9922005-01-12 18:08:53 +0000703 return false;
704 AM = Backup;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000705 break;
706 }
707 }
708
Chris Lattnera95589b2005-01-11 04:40:19 +0000709 // Is the base register already occupied?
Chris Lattner98a8ba02005-01-18 01:06:26 +0000710 if (AM.BaseType != X86ISelAddressMode::RegBase || AM.Base.Reg.Val) {
Chris Lattnera95589b2005-01-11 04:40:19 +0000711 // If so, check to see if the scale index register is set.
Chris Lattner98a8ba02005-01-18 01:06:26 +0000712 if (AM.IndexReg.Val == 0) {
713 AM.IndexReg = N;
Chris Lattnera95589b2005-01-11 04:40:19 +0000714 AM.Scale = 1;
715 return false;
716 }
717
718 // Otherwise, we cannot select it.
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000719 return true;
Chris Lattnera95589b2005-01-11 04:40:19 +0000720 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000721
722 // Default, generate it as a register.
Chris Lattner98a8ba02005-01-18 01:06:26 +0000723 AM.BaseType = X86ISelAddressMode::RegBase;
724 AM.Base.Reg = N;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000725 return false;
726}
727
728/// Emit2SetCCsAndLogical - Emit the following sequence of instructions,
729/// assuming that the temporary registers are in the 8-bit register class.
730///
731/// Tmp1 = setcc1
732/// Tmp2 = setcc2
733/// DestReg = logicalop Tmp1, Tmp2
734///
735static void Emit2SetCCsAndLogical(MachineBasicBlock *BB, unsigned SetCC1,
736 unsigned SetCC2, unsigned LogicalOp,
737 unsigned DestReg) {
738 SSARegMap *RegMap = BB->getParent()->getSSARegMap();
739 unsigned Tmp1 = RegMap->createVirtualRegister(X86::R8RegisterClass);
740 unsigned Tmp2 = RegMap->createVirtualRegister(X86::R8RegisterClass);
741 BuildMI(BB, SetCC1, 0, Tmp1);
742 BuildMI(BB, SetCC2, 0, Tmp2);
743 BuildMI(BB, LogicalOp, 2, DestReg).addReg(Tmp1).addReg(Tmp2);
744}
745
746/// EmitSetCC - Emit the code to set the specified 8-bit register to 1 if the
747/// condition codes match the specified SetCCOpcode. Note that some conditions
748/// require multiple instructions to generate the correct value.
749static void EmitSetCC(MachineBasicBlock *BB, unsigned DestReg,
750 ISD::CondCode SetCCOpcode, bool isFP) {
751 unsigned Opc;
752 if (!isFP) {
753 switch (SetCCOpcode) {
754 default: assert(0 && "Illegal integer SetCC!");
755 case ISD::SETEQ: Opc = X86::SETEr; break;
756 case ISD::SETGT: Opc = X86::SETGr; break;
757 case ISD::SETGE: Opc = X86::SETGEr; break;
758 case ISD::SETLT: Opc = X86::SETLr; break;
759 case ISD::SETLE: Opc = X86::SETLEr; break;
760 case ISD::SETNE: Opc = X86::SETNEr; break;
761 case ISD::SETULT: Opc = X86::SETBr; break;
762 case ISD::SETUGT: Opc = X86::SETAr; break;
763 case ISD::SETULE: Opc = X86::SETBEr; break;
764 case ISD::SETUGE: Opc = X86::SETAEr; break;
765 }
766 } else {
767 // On a floating point condition, the flags are set as follows:
768 // ZF PF CF op
769 // 0 | 0 | 0 | X > Y
770 // 0 | 0 | 1 | X < Y
771 // 1 | 0 | 0 | X == Y
772 // 1 | 1 | 1 | unordered
773 //
774 switch (SetCCOpcode) {
775 default: assert(0 && "Invalid FP setcc!");
776 case ISD::SETUEQ:
777 case ISD::SETEQ:
778 Opc = X86::SETEr; // True if ZF = 1
779 break;
780 case ISD::SETOGT:
781 case ISD::SETGT:
782 Opc = X86::SETAr; // True if CF = 0 and ZF = 0
783 break;
784 case ISD::SETOGE:
785 case ISD::SETGE:
786 Opc = X86::SETAEr; // True if CF = 0
787 break;
788 case ISD::SETULT:
789 case ISD::SETLT:
790 Opc = X86::SETBr; // True if CF = 1
791 break;
792 case ISD::SETULE:
793 case ISD::SETLE:
794 Opc = X86::SETBEr; // True if CF = 1 or ZF = 1
795 break;
796 case ISD::SETONE:
797 case ISD::SETNE:
798 Opc = X86::SETNEr; // True if ZF = 0
799 break;
800 case ISD::SETUO:
801 Opc = X86::SETPr; // True if PF = 1
802 break;
803 case ISD::SETO:
804 Opc = X86::SETNPr; // True if PF = 0
805 break;
806 case ISD::SETOEQ: // !PF & ZF
807 Emit2SetCCsAndLogical(BB, X86::SETNPr, X86::SETEr, X86::AND8rr, DestReg);
808 return;
809 case ISD::SETOLT: // !PF & CF
810 Emit2SetCCsAndLogical(BB, X86::SETNPr, X86::SETBr, X86::AND8rr, DestReg);
811 return;
812 case ISD::SETOLE: // !PF & (CF || ZF)
813 Emit2SetCCsAndLogical(BB, X86::SETNPr, X86::SETBEr, X86::AND8rr, DestReg);
814 return;
815 case ISD::SETUGT: // PF | (!ZF & !CF)
816 Emit2SetCCsAndLogical(BB, X86::SETPr, X86::SETAr, X86::OR8rr, DestReg);
817 return;
818 case ISD::SETUGE: // PF | !CF
819 Emit2SetCCsAndLogical(BB, X86::SETPr, X86::SETAEr, X86::OR8rr, DestReg);
820 return;
821 case ISD::SETUNE: // PF | !ZF
822 Emit2SetCCsAndLogical(BB, X86::SETPr, X86::SETNEr, X86::OR8rr, DestReg);
823 return;
824 }
825 }
826 BuildMI(BB, Opc, 0, DestReg);
827}
828
829
830/// EmitBranchCC - Emit code into BB that arranges for control to transfer to
831/// the Dest block if the Cond condition is true. If we cannot fold this
832/// condition into the branch, return true.
833///
Chris Lattner6c07aee2005-01-11 04:06:27 +0000834bool ISel::EmitBranchCC(MachineBasicBlock *Dest, SDOperand Chain,
835 SDOperand Cond) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000836 // FIXME: Evaluate whether it would be good to emit code like (X < Y) | (A >
837 // B) using two conditional branches instead of one condbr, two setcc's, and
838 // an or.
839 if ((Cond.getOpcode() == ISD::OR ||
840 Cond.getOpcode() == ISD::AND) && Cond.Val->hasOneUse()) {
841 // And and or set the flags for us, so there is no need to emit a TST of the
842 // result. It is only safe to do this if there is only a single use of the
843 // AND/OR though, otherwise we don't know it will be emitted here.
Chris Lattner6c07aee2005-01-11 04:06:27 +0000844 Select(Chain);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000845 SelectExpr(Cond);
846 BuildMI(BB, X86::JNE, 1).addMBB(Dest);
847 return false;
848 }
849
850 // Codegen br not C -> JE.
851 if (Cond.getOpcode() == ISD::XOR)
852 if (ConstantSDNode *NC = dyn_cast<ConstantSDNode>(Cond.Val->getOperand(1)))
853 if (NC->isAllOnesValue()) {
Chris Lattner6c07aee2005-01-11 04:06:27 +0000854 unsigned CondR;
855 if (getRegPressure(Chain) > getRegPressure(Cond)) {
856 Select(Chain);
857 CondR = SelectExpr(Cond.Val->getOperand(0));
858 } else {
859 CondR = SelectExpr(Cond.Val->getOperand(0));
860 Select(Chain);
861 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000862 BuildMI(BB, X86::TEST8rr, 2).addReg(CondR).addReg(CondR);
863 BuildMI(BB, X86::JE, 1).addMBB(Dest);
864 return false;
865 }
866
867 SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Cond);
868 if (SetCC == 0)
869 return true; // Can only handle simple setcc's so far.
870
871 unsigned Opc;
872
873 // Handle integer conditions first.
874 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
875 switch (SetCC->getCondition()) {
876 default: assert(0 && "Illegal integer SetCC!");
877 case ISD::SETEQ: Opc = X86::JE; break;
878 case ISD::SETGT: Opc = X86::JG; break;
879 case ISD::SETGE: Opc = X86::JGE; break;
880 case ISD::SETLT: Opc = X86::JL; break;
881 case ISD::SETLE: Opc = X86::JLE; break;
882 case ISD::SETNE: Opc = X86::JNE; break;
883 case ISD::SETULT: Opc = X86::JB; break;
884 case ISD::SETUGT: Opc = X86::JA; break;
885 case ISD::SETULE: Opc = X86::JBE; break;
886 case ISD::SETUGE: Opc = X86::JAE; break;
887 }
Chris Lattner6c07aee2005-01-11 04:06:27 +0000888 Select(Chain);
Chris Lattnercb1aa8d2005-01-17 01:34:14 +0000889 EmitCMP(SetCC->getOperand(0), SetCC->getOperand(1), SetCC->hasOneUse());
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000890 BuildMI(BB, Opc, 1).addMBB(Dest);
891 return false;
892 }
893
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000894 unsigned Opc2 = 0; // Second branch if needed.
895
896 // On a floating point condition, the flags are set as follows:
897 // ZF PF CF op
898 // 0 | 0 | 0 | X > Y
899 // 0 | 0 | 1 | X < Y
900 // 1 | 0 | 0 | X == Y
901 // 1 | 1 | 1 | unordered
902 //
903 switch (SetCC->getCondition()) {
904 default: assert(0 && "Invalid FP setcc!");
905 case ISD::SETUEQ:
906 case ISD::SETEQ: Opc = X86::JE; break; // True if ZF = 1
907 case ISD::SETOGT:
908 case ISD::SETGT: Opc = X86::JA; break; // True if CF = 0 and ZF = 0
909 case ISD::SETOGE:
910 case ISD::SETGE: Opc = X86::JAE; break; // True if CF = 0
911 case ISD::SETULT:
912 case ISD::SETLT: Opc = X86::JB; break; // True if CF = 1
913 case ISD::SETULE:
914 case ISD::SETLE: Opc = X86::JBE; break; // True if CF = 1 or ZF = 1
915 case ISD::SETONE:
916 case ISD::SETNE: Opc = X86::JNE; break; // True if ZF = 0
917 case ISD::SETUO: Opc = X86::JP; break; // True if PF = 1
918 case ISD::SETO: Opc = X86::JNP; break; // True if PF = 0
919 case ISD::SETUGT: // PF = 1 | (ZF = 0 & CF = 0)
920 Opc = X86::JA; // ZF = 0 & CF = 0
921 Opc2 = X86::JP; // PF = 1
922 break;
923 case ISD::SETUGE: // PF = 1 | CF = 0
924 Opc = X86::JAE; // CF = 0
925 Opc2 = X86::JP; // PF = 1
926 break;
927 case ISD::SETUNE: // PF = 1 | ZF = 0
928 Opc = X86::JNE; // ZF = 0
929 Opc2 = X86::JP; // PF = 1
930 break;
931 case ISD::SETOEQ: // PF = 0 & ZF = 1
932 //X86::JNP, X86::JE
933 //X86::AND8rr
934 return true; // FIXME: Emit more efficient code for this branch.
935 case ISD::SETOLT: // PF = 0 & CF = 1
936 //X86::JNP, X86::JB
937 //X86::AND8rr
938 return true; // FIXME: Emit more efficient code for this branch.
939 case ISD::SETOLE: // PF = 0 & (CF = 1 || ZF = 1)
940 //X86::JNP, X86::JBE
941 //X86::AND8rr
942 return true; // FIXME: Emit more efficient code for this branch.
943 }
944
Chris Lattner6c07aee2005-01-11 04:06:27 +0000945 Select(Chain);
Chris Lattnercb1aa8d2005-01-17 01:34:14 +0000946 EmitCMP(SetCC->getOperand(0), SetCC->getOperand(1), SetCC->hasOneUse());
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000947 BuildMI(BB, Opc, 1).addMBB(Dest);
948 if (Opc2)
949 BuildMI(BB, Opc2, 1).addMBB(Dest);
950 return false;
951}
952
Chris Lattner24aad1b2005-01-10 22:10:13 +0000953/// EmitSelectCC - Emit code into BB that performs a select operation between
954/// the two registers RTrue and RFalse, generating a result into RDest. Return
955/// true if the fold cannot be performed.
956///
957void ISel::EmitSelectCC(SDOperand Cond, MVT::ValueType SVT,
958 unsigned RTrue, unsigned RFalse, unsigned RDest) {
959 enum Condition {
960 EQ, NE, LT, LE, GT, GE, B, BE, A, AE, P, NP,
961 NOT_SET
962 } CondCode = NOT_SET;
963
964 static const unsigned CMOVTAB16[] = {
965 X86::CMOVE16rr, X86::CMOVNE16rr, X86::CMOVL16rr, X86::CMOVLE16rr,
966 X86::CMOVG16rr, X86::CMOVGE16rr, X86::CMOVB16rr, X86::CMOVBE16rr,
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000967 X86::CMOVA16rr, X86::CMOVAE16rr, X86::CMOVP16rr, X86::CMOVNP16rr,
Chris Lattner24aad1b2005-01-10 22:10:13 +0000968 };
969 static const unsigned CMOVTAB32[] = {
970 X86::CMOVE32rr, X86::CMOVNE32rr, X86::CMOVL32rr, X86::CMOVLE32rr,
971 X86::CMOVG32rr, X86::CMOVGE32rr, X86::CMOVB32rr, X86::CMOVBE32rr,
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000972 X86::CMOVA32rr, X86::CMOVAE32rr, X86::CMOVP32rr, X86::CMOVNP32rr,
Chris Lattner24aad1b2005-01-10 22:10:13 +0000973 };
974 static const unsigned CMOVTABFP[] = {
975 X86::FCMOVE , X86::FCMOVNE, /*missing*/0, /*missing*/0,
976 /*missing*/0, /*missing*/0, X86::FCMOVB , X86::FCMOVBE,
977 X86::FCMOVA , X86::FCMOVAE, X86::FCMOVP , X86::FCMOVNP
978 };
979
980 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Cond)) {
981 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
982 switch (SetCC->getCondition()) {
983 default: assert(0 && "Unknown integer comparison!");
984 case ISD::SETEQ: CondCode = EQ; break;
985 case ISD::SETGT: CondCode = GT; break;
986 case ISD::SETGE: CondCode = GE; break;
987 case ISD::SETLT: CondCode = LT; break;
988 case ISD::SETLE: CondCode = LE; break;
989 case ISD::SETNE: CondCode = NE; break;
990 case ISD::SETULT: CondCode = B; break;
991 case ISD::SETUGT: CondCode = A; break;
992 case ISD::SETULE: CondCode = BE; break;
993 case ISD::SETUGE: CondCode = AE; break;
994 }
995 } else {
996 // On a floating point condition, the flags are set as follows:
997 // ZF PF CF op
998 // 0 | 0 | 0 | X > Y
999 // 0 | 0 | 1 | X < Y
1000 // 1 | 0 | 0 | X == Y
1001 // 1 | 1 | 1 | unordered
1002 //
1003 switch (SetCC->getCondition()) {
1004 default: assert(0 && "Unknown FP comparison!");
1005 case ISD::SETUEQ:
1006 case ISD::SETEQ: CondCode = EQ; break; // True if ZF = 1
1007 case ISD::SETOGT:
1008 case ISD::SETGT: CondCode = A; break; // True if CF = 0 and ZF = 0
1009 case ISD::SETOGE:
1010 case ISD::SETGE: CondCode = AE; break; // True if CF = 0
1011 case ISD::SETULT:
1012 case ISD::SETLT: CondCode = B; break; // True if CF = 1
1013 case ISD::SETULE:
1014 case ISD::SETLE: CondCode = BE; break; // True if CF = 1 or ZF = 1
1015 case ISD::SETONE:
1016 case ISD::SETNE: CondCode = NE; break; // True if ZF = 0
1017 case ISD::SETUO: CondCode = P; break; // True if PF = 1
1018 case ISD::SETO: CondCode = NP; break; // True if PF = 0
1019 case ISD::SETUGT: // PF = 1 | (ZF = 0 & CF = 0)
1020 case ISD::SETUGE: // PF = 1 | CF = 0
1021 case ISD::SETUNE: // PF = 1 | ZF = 0
1022 case ISD::SETOEQ: // PF = 0 & ZF = 1
1023 case ISD::SETOLT: // PF = 0 & CF = 1
1024 case ISD::SETOLE: // PF = 0 & (CF = 1 || ZF = 1)
1025 // We cannot emit this comparison as a single cmov.
1026 break;
1027 }
1028 }
1029 }
1030
1031 unsigned Opc = 0;
1032 if (CondCode != NOT_SET) {
1033 switch (SVT) {
1034 default: assert(0 && "Cannot select this type!");
1035 case MVT::i16: Opc = CMOVTAB16[CondCode]; break;
1036 case MVT::i32: Opc = CMOVTAB32[CondCode]; break;
Chris Lattneref7ba072005-01-11 03:50:45 +00001037 case MVT::f64: Opc = CMOVTABFP[CondCode]; break;
Chris Lattner24aad1b2005-01-10 22:10:13 +00001038 }
1039 }
1040
1041 // Finally, if we weren't able to fold this, just emit the condition and test
1042 // it.
1043 if (CondCode == NOT_SET || Opc == 0) {
1044 // Get the condition into the zero flag.
1045 unsigned CondReg = SelectExpr(Cond);
1046 BuildMI(BB, X86::TEST8rr, 2).addReg(CondReg).addReg(CondReg);
1047
1048 switch (SVT) {
1049 default: assert(0 && "Cannot select this type!");
1050 case MVT::i16: Opc = X86::CMOVE16rr; break;
1051 case MVT::i32: Opc = X86::CMOVE32rr; break;
Chris Lattneref7ba072005-01-11 03:50:45 +00001052 case MVT::f64: Opc = X86::FCMOVE; break;
Chris Lattner24aad1b2005-01-10 22:10:13 +00001053 }
1054 } else {
1055 // FIXME: CMP R, 0 -> TEST R, R
Chris Lattnercb1aa8d2005-01-17 01:34:14 +00001056 EmitCMP(Cond.getOperand(0), Cond.getOperand(1), Cond.Val->hasOneUse());
Chris Lattnera3aa2e22005-01-11 03:37:59 +00001057 std::swap(RTrue, RFalse);
Chris Lattner24aad1b2005-01-10 22:10:13 +00001058 }
1059 BuildMI(BB, Opc, 2, RDest).addReg(RTrue).addReg(RFalse);
1060}
1061
Chris Lattnercb1aa8d2005-01-17 01:34:14 +00001062void ISel::EmitCMP(SDOperand LHS, SDOperand RHS, bool HasOneUse) {
Chris Lattner11333092005-01-11 03:11:44 +00001063 unsigned Opc;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001064 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(RHS)) {
1065 Opc = 0;
Chris Lattner4ff348b2005-01-17 06:26:58 +00001066 if (HasOneUse && isFoldableLoad(LHS, RHS)) {
Chris Lattneref6806c2005-01-12 02:02:48 +00001067 switch (RHS.getValueType()) {
1068 default: break;
1069 case MVT::i1:
1070 case MVT::i8: Opc = X86::CMP8mi; break;
1071 case MVT::i16: Opc = X86::CMP16mi; break;
1072 case MVT::i32: Opc = X86::CMP32mi; break;
1073 }
1074 if (Opc) {
1075 X86AddressMode AM;
1076 EmitFoldedLoad(LHS, AM);
1077 addFullAddress(BuildMI(BB, Opc, 5), AM).addImm(CN->getValue());
1078 return;
1079 }
1080 }
1081
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001082 switch (RHS.getValueType()) {
1083 default: break;
1084 case MVT::i1:
1085 case MVT::i8: Opc = X86::CMP8ri; break;
1086 case MVT::i16: Opc = X86::CMP16ri; break;
1087 case MVT::i32: Opc = X86::CMP32ri; break;
1088 }
1089 if (Opc) {
Chris Lattner11333092005-01-11 03:11:44 +00001090 unsigned Tmp1 = SelectExpr(LHS);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001091 BuildMI(BB, Opc, 2).addReg(Tmp1).addImm(CN->getValue());
1092 return;
1093 }
Chris Lattner7f2afac2005-01-14 22:37:41 +00001094 } else if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(RHS)) {
1095 if (CN->isExactlyValue(+0.0) ||
1096 CN->isExactlyValue(-0.0)) {
1097 unsigned Reg = SelectExpr(LHS);
1098 BuildMI(BB, X86::FTST, 1).addReg(Reg);
1099 BuildMI(BB, X86::FNSTSW8r, 0);
1100 BuildMI(BB, X86::SAHF, 1);
Chris Lattner7805fa42005-03-17 16:29:26 +00001101 return;
Chris Lattner7f2afac2005-01-14 22:37:41 +00001102 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001103 }
1104
Chris Lattneref6806c2005-01-12 02:02:48 +00001105 Opc = 0;
Chris Lattner4ff348b2005-01-17 06:26:58 +00001106 if (HasOneUse && isFoldableLoad(LHS, RHS)) {
Chris Lattneref6806c2005-01-12 02:02:48 +00001107 switch (RHS.getValueType()) {
1108 default: break;
1109 case MVT::i1:
1110 case MVT::i8: Opc = X86::CMP8mr; break;
1111 case MVT::i16: Opc = X86::CMP16mr; break;
1112 case MVT::i32: Opc = X86::CMP32mr; break;
1113 }
1114 if (Opc) {
1115 X86AddressMode AM;
Chris Lattner636e79a2005-01-13 05:53:16 +00001116 EmitFoldedLoad(LHS, AM);
1117 unsigned Reg = SelectExpr(RHS);
Chris Lattneref6806c2005-01-12 02:02:48 +00001118 addFullAddress(BuildMI(BB, Opc, 5), AM).addReg(Reg);
1119 return;
1120 }
1121 }
1122
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001123 switch (LHS.getValueType()) {
1124 default: assert(0 && "Cannot compare this value!");
1125 case MVT::i1:
1126 case MVT::i8: Opc = X86::CMP8rr; break;
1127 case MVT::i16: Opc = X86::CMP16rr; break;
1128 case MVT::i32: Opc = X86::CMP32rr; break;
Chris Lattneref7ba072005-01-11 03:50:45 +00001129 case MVT::f64: Opc = X86::FUCOMIr; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001130 }
Chris Lattner11333092005-01-11 03:11:44 +00001131 unsigned Tmp1, Tmp2;
1132 if (getRegPressure(LHS) > getRegPressure(RHS)) {
1133 Tmp1 = SelectExpr(LHS);
1134 Tmp2 = SelectExpr(RHS);
1135 } else {
1136 Tmp2 = SelectExpr(RHS);
1137 Tmp1 = SelectExpr(LHS);
1138 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001139 BuildMI(BB, Opc, 2).addReg(Tmp1).addReg(Tmp2);
1140}
1141
Chris Lattnera5ade062005-01-11 21:19:59 +00001142/// isFoldableLoad - Return true if this is a load instruction that can safely
1143/// be folded into an operation that uses it.
Chris Lattner44129b52005-01-25 20:03:11 +00001144bool ISel::isFoldableLoad(SDOperand Op, SDOperand OtherOp, bool FloatPromoteOk){
1145 if (Op.getOpcode() == ISD::LOAD) {
1146 // FIXME: currently can't fold constant pool indexes.
1147 if (isa<ConstantPoolSDNode>(Op.getOperand(1)))
1148 return false;
1149 } else if (FloatPromoteOk && Op.getOpcode() == ISD::EXTLOAD &&
1150 cast<MVTSDNode>(Op)->getExtraValueType() == MVT::f32) {
1151 // FIXME: currently can't fold constant pool indexes.
1152 if (isa<ConstantPoolSDNode>(Op.getOperand(1)))
1153 return false;
1154 } else {
Chris Lattnera5ade062005-01-11 21:19:59 +00001155 return false;
Chris Lattner44129b52005-01-25 20:03:11 +00001156 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001157
1158 // If this load has already been emitted, we clearly can't fold it.
Chris Lattner636e79a2005-01-13 05:53:16 +00001159 assert(Op.ResNo == 0 && "Not a use of the value of the load?");
1160 if (ExprMap.count(Op.getValue(1))) return false;
1161 assert(!ExprMap.count(Op.getValue(0)) && "Value in map but not token chain?");
Chris Lattner4a108662005-01-18 03:51:59 +00001162 assert(!ExprMap.count(Op.getValue(1))&&"Token lowered but value not in map?");
Chris Lattnera5ade062005-01-11 21:19:59 +00001163
Chris Lattner4ff348b2005-01-17 06:26:58 +00001164 // If there is not just one use of its value, we cannot fold.
1165 if (!Op.Val->hasNUsesOfValue(1, 0)) return false;
1166
1167 // Finally, we cannot fold the load into the operation if this would induce a
1168 // cycle into the resultant dag. To check for this, see if OtherOp (the other
1169 // operand of the operation we are folding the load into) can possible use the
1170 // chain node defined by the load.
1171 if (OtherOp.Val && !Op.Val->hasNUsesOfValue(0, 1)) { // Has uses of chain?
1172 std::set<SDNode*> Visited;
1173 if (NodeTransitivelyUsesValue(OtherOp, Op.getValue(1), Visited))
1174 return false;
1175 }
1176 return true;
Chris Lattnera5ade062005-01-11 21:19:59 +00001177}
1178
Chris Lattner4ff348b2005-01-17 06:26:58 +00001179
Chris Lattnera5ade062005-01-11 21:19:59 +00001180/// EmitFoldedLoad - Ensure that the arguments of the load are code generated,
1181/// and compute the address being loaded into AM.
1182void ISel::EmitFoldedLoad(SDOperand Op, X86AddressMode &AM) {
1183 SDOperand Chain = Op.getOperand(0);
1184 SDOperand Address = Op.getOperand(1);
Chris Lattner98a8ba02005-01-18 01:06:26 +00001185
Chris Lattnera5ade062005-01-11 21:19:59 +00001186 if (getRegPressure(Chain) > getRegPressure(Address)) {
1187 Select(Chain);
1188 SelectAddress(Address, AM);
1189 } else {
1190 SelectAddress(Address, AM);
1191 Select(Chain);
1192 }
1193
1194 // The chain for this load is now lowered.
Chris Lattner636e79a2005-01-13 05:53:16 +00001195 assert(ExprMap.count(SDOperand(Op.Val, 1)) == 0 &&
1196 "Load emitted more than once?");
Chris Lattner4a108662005-01-18 03:51:59 +00001197 if (!ExprMap.insert(std::make_pair(Op.getValue(1), 1)).second)
Chris Lattner636e79a2005-01-13 05:53:16 +00001198 assert(0 && "Load emitted more than once!");
Chris Lattnera5ade062005-01-11 21:19:59 +00001199}
1200
Chris Lattner30ea1e92005-01-19 07:37:26 +00001201// EmitOrOpOp - Pattern match the expression (Op1|Op2), where we know that op1
1202// and op2 are i8/i16/i32 values with one use each (the or). If we can form a
1203// SHLD or SHRD, emit the instruction (generating the value into DestReg) and
1204// return true.
1205bool ISel::EmitOrOpOp(SDOperand Op1, SDOperand Op2, unsigned DestReg) {
Chris Lattner85716372005-01-19 06:18:43 +00001206 if (Op1.getOpcode() == ISD::SHL && Op2.getOpcode() == ISD::SRL) {
1207 // good!
1208 } else if (Op2.getOpcode() == ISD::SHL && Op1.getOpcode() == ISD::SRL) {
1209 std::swap(Op1, Op2); // Op1 is the SHL now.
1210 } else {
1211 return false; // No match
1212 }
1213
1214 SDOperand ShlVal = Op1.getOperand(0);
1215 SDOperand ShlAmt = Op1.getOperand(1);
1216 SDOperand ShrVal = Op2.getOperand(0);
1217 SDOperand ShrAmt = Op2.getOperand(1);
1218
Chris Lattner30ea1e92005-01-19 07:37:26 +00001219 unsigned RegSize = MVT::getSizeInBits(Op1.getValueType());
1220
Chris Lattner85716372005-01-19 06:18:43 +00001221 // Find out if ShrAmt = 32-ShlAmt or ShlAmt = 32-ShrAmt.
1222 if (ShlAmt.getOpcode() == ISD::SUB && ShlAmt.getOperand(1) == ShrAmt)
1223 if (ConstantSDNode *SubCST = dyn_cast<ConstantSDNode>(ShlAmt.getOperand(0)))
Chris Lattner4053b1e2005-01-19 08:07:05 +00001224 if (SubCST->getValue() == RegSize) {
1225 // (A >> ShrAmt) | (A << (32-ShrAmt)) ==> ROR A, ShrAmt
Chris Lattner85716372005-01-19 06:18:43 +00001226 // (A >> ShrAmt) | (B << (32-ShrAmt)) ==> SHRD A, B, ShrAmt
Chris Lattner4053b1e2005-01-19 08:07:05 +00001227 if (ShrVal == ShlVal) {
1228 unsigned Reg, ShAmt;
1229 if (getRegPressure(ShrVal) > getRegPressure(ShrAmt)) {
1230 Reg = SelectExpr(ShrVal);
1231 ShAmt = SelectExpr(ShrAmt);
1232 } else {
1233 ShAmt = SelectExpr(ShrAmt);
1234 Reg = SelectExpr(ShrVal);
1235 }
1236 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShAmt);
1237 unsigned Opc = RegSize == 8 ? X86::ROR8rCL :
1238 (RegSize == 16 ? X86::ROR16rCL : X86::ROR32rCL);
1239 BuildMI(BB, Opc, 1, DestReg).addReg(Reg);
1240 return true;
1241 } else if (RegSize != 8) {
Chris Lattner85716372005-01-19 06:18:43 +00001242 unsigned AReg, BReg;
1243 if (getRegPressure(ShlVal) > getRegPressure(ShrVal)) {
Chris Lattner85716372005-01-19 06:18:43 +00001244 BReg = SelectExpr(ShlVal);
Chris Lattnerc3c021b2005-01-19 17:24:34 +00001245 AReg = SelectExpr(ShrVal);
Chris Lattner85716372005-01-19 06:18:43 +00001246 } else {
Chris Lattner85716372005-01-19 06:18:43 +00001247 AReg = SelectExpr(ShrVal);
Chris Lattnerc3c021b2005-01-19 17:24:34 +00001248 BReg = SelectExpr(ShlVal);
Chris Lattner85716372005-01-19 06:18:43 +00001249 }
Chris Lattner4053b1e2005-01-19 08:07:05 +00001250 unsigned ShAmt = SelectExpr(ShrAmt);
1251 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShAmt);
1252 unsigned Opc = RegSize == 16 ? X86::SHRD16rrCL : X86::SHRD32rrCL;
1253 BuildMI(BB, Opc, 2, DestReg).addReg(AReg).addReg(BReg);
Chris Lattner85716372005-01-19 06:18:43 +00001254 return true;
1255 }
1256 }
1257
Chris Lattner4053b1e2005-01-19 08:07:05 +00001258 if (ShrAmt.getOpcode() == ISD::SUB && ShrAmt.getOperand(1) == ShlAmt)
1259 if (ConstantSDNode *SubCST = dyn_cast<ConstantSDNode>(ShrAmt.getOperand(0)))
1260 if (SubCST->getValue() == RegSize) {
1261 // (A << ShlAmt) | (A >> (32-ShlAmt)) ==> ROL A, ShrAmt
1262 // (A << ShlAmt) | (B >> (32-ShlAmt)) ==> SHLD A, B, ShrAmt
1263 if (ShrVal == ShlVal) {
1264 unsigned Reg, ShAmt;
1265 if (getRegPressure(ShrVal) > getRegPressure(ShlAmt)) {
1266 Reg = SelectExpr(ShrVal);
1267 ShAmt = SelectExpr(ShlAmt);
1268 } else {
1269 ShAmt = SelectExpr(ShlAmt);
1270 Reg = SelectExpr(ShrVal);
1271 }
1272 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShAmt);
1273 unsigned Opc = RegSize == 8 ? X86::ROL8rCL :
1274 (RegSize == 16 ? X86::ROL16rCL : X86::ROL32rCL);
1275 BuildMI(BB, Opc, 1, DestReg).addReg(Reg);
1276 return true;
1277 } else if (RegSize != 8) {
1278 unsigned AReg, BReg;
1279 if (getRegPressure(ShlVal) > getRegPressure(ShrVal)) {
Chris Lattnerc3c021b2005-01-19 17:24:34 +00001280 AReg = SelectExpr(ShlVal);
1281 BReg = SelectExpr(ShrVal);
Chris Lattner4053b1e2005-01-19 08:07:05 +00001282 } else {
Chris Lattnerc3c021b2005-01-19 17:24:34 +00001283 BReg = SelectExpr(ShrVal);
1284 AReg = SelectExpr(ShlVal);
Chris Lattner4053b1e2005-01-19 08:07:05 +00001285 }
1286 unsigned ShAmt = SelectExpr(ShlAmt);
1287 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShAmt);
1288 unsigned Opc = RegSize == 16 ? X86::SHLD16rrCL : X86::SHLD32rrCL;
1289 BuildMI(BB, Opc, 2, DestReg).addReg(AReg).addReg(BReg);
1290 return true;
1291 }
1292 }
Chris Lattner85716372005-01-19 06:18:43 +00001293
Chris Lattner4053b1e2005-01-19 08:07:05 +00001294 if (ConstantSDNode *ShrCst = dyn_cast<ConstantSDNode>(ShrAmt))
1295 if (ConstantSDNode *ShlCst = dyn_cast<ConstantSDNode>(ShlAmt))
1296 if (ShrCst->getValue() < RegSize && ShlCst->getValue() < RegSize)
1297 if (ShrCst->getValue() == RegSize-ShlCst->getValue()) {
1298 // (A >> 5) | (A << 27) --> ROR A, 5
1299 // (A >> 5) | (B << 27) --> SHRD A, B, 5
1300 if (ShrVal == ShlVal) {
1301 unsigned Reg = SelectExpr(ShrVal);
1302 unsigned Opc = RegSize == 8 ? X86::ROR8ri :
1303 (RegSize == 16 ? X86::ROR16ri : X86::ROR32ri);
1304 BuildMI(BB, Opc, 2, DestReg).addReg(Reg).addImm(ShrCst->getValue());
1305 return true;
1306 } else if (RegSize != 8) {
1307 unsigned AReg, BReg;
1308 if (getRegPressure(ShlVal) > getRegPressure(ShrVal)) {
Chris Lattner4053b1e2005-01-19 08:07:05 +00001309 BReg = SelectExpr(ShlVal);
Chris Lattnerc3c021b2005-01-19 17:24:34 +00001310 AReg = SelectExpr(ShrVal);
Chris Lattner4053b1e2005-01-19 08:07:05 +00001311 } else {
Chris Lattner4053b1e2005-01-19 08:07:05 +00001312 AReg = SelectExpr(ShrVal);
Chris Lattnerc3c021b2005-01-19 17:24:34 +00001313 BReg = SelectExpr(ShlVal);
Chris Lattner4053b1e2005-01-19 08:07:05 +00001314 }
1315 unsigned Opc = RegSize == 16 ? X86::SHRD16rri8 : X86::SHRD32rri8;
1316 BuildMI(BB, Opc, 3, DestReg).addReg(AReg).addReg(BReg)
1317 .addImm(ShrCst->getValue());
1318 return true;
1319 }
1320 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001321
Chris Lattner85716372005-01-19 06:18:43 +00001322 return false;
1323}
1324
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001325unsigned ISel::SelectExpr(SDOperand N) {
1326 unsigned Result;
1327 unsigned Tmp1, Tmp2, Tmp3;
1328 unsigned Opc = 0;
Chris Lattner5188ad72005-01-08 19:28:19 +00001329 SDNode *Node = N.Val;
Chris Lattnera5ade062005-01-11 21:19:59 +00001330 SDOperand Op0, Op1;
Chris Lattner5188ad72005-01-08 19:28:19 +00001331
Chris Lattner7f2afac2005-01-14 22:37:41 +00001332 if (Node->getOpcode() == ISD::CopyFromReg) {
1333 // FIXME: Handle copy from physregs!
1334
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001335 // Just use the specified register as our input.
Chris Lattner18c2f132005-01-13 20:50:02 +00001336 return dyn_cast<RegSDNode>(Node)->getReg();
Chris Lattner7f2afac2005-01-14 22:37:41 +00001337 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001338
Chris Lattnera5ade062005-01-11 21:19:59 +00001339 unsigned &Reg = ExprMap[N];
1340 if (Reg) return Reg;
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001341
Chris Lattnerb38a7492005-04-02 04:01:14 +00001342 switch (N.getOpcode()) {
1343 default:
Chris Lattnera5ade062005-01-11 21:19:59 +00001344 Reg = Result = (N.getValueType() != MVT::Other) ?
Chris Lattnerb38a7492005-04-02 04:01:14 +00001345 MakeReg(N.getValueType()) : 1;
1346 break;
1347 case ISD::CALL:
Chris Lattnera5ade062005-01-11 21:19:59 +00001348 // If this is a call instruction, make sure to prepare ALL of the result
1349 // values as well as the chain.
Chris Lattnerb38a7492005-04-02 04:01:14 +00001350 if (Node->getNumValues() == 1)
1351 Reg = Result = 1; // Void call, just a chain.
1352 else {
Chris Lattnera5ade062005-01-11 21:19:59 +00001353 Result = MakeReg(Node->getValueType(0));
1354 ExprMap[N.getValue(0)] = Result;
Chris Lattnerb38a7492005-04-02 04:01:14 +00001355 for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
Chris Lattnera5ade062005-01-11 21:19:59 +00001356 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
Chris Lattnerb38a7492005-04-02 04:01:14 +00001357 ExprMap[SDOperand(Node, Node->getNumValues()-1)] = 1;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001358 }
Chris Lattnerb38a7492005-04-02 04:01:14 +00001359 break;
1360 case ISD::ADD_PARTS:
1361 case ISD::SUB_PARTS:
1362 case ISD::SHL_PARTS:
1363 case ISD::SRL_PARTS:
1364 case ISD::SRA_PARTS:
1365 Result = MakeReg(Node->getValueType(0));
1366 ExprMap[N.getValue(0)] = Result;
1367 for (unsigned i = 1, e = N.Val->getNumValues(); i != e; ++i)
1368 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
1369 break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001370 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001371
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001372 switch (N.getOpcode()) {
1373 default:
Chris Lattner5188ad72005-01-08 19:28:19 +00001374 Node->dump();
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001375 assert(0 && "Node not handled!\n");
1376 case ISD::FrameIndex:
1377 Tmp1 = cast<FrameIndexSDNode>(N)->getIndex();
1378 addFrameReference(BuildMI(BB, X86::LEA32r, 4, Result), (int)Tmp1);
1379 return Result;
1380 case ISD::ConstantPool:
1381 Tmp1 = cast<ConstantPoolSDNode>(N)->getIndex();
1382 addConstantPoolReference(BuildMI(BB, X86::LEA32r, 4, Result), Tmp1);
1383 return Result;
1384 case ISD::ConstantFP:
1385 ContainsFPCode = true;
1386 Tmp1 = Result; // Intermediate Register
1387 if (cast<ConstantFPSDNode>(N)->getValue() < 0.0 ||
1388 cast<ConstantFPSDNode>(N)->isExactlyValue(-0.0))
1389 Tmp1 = MakeReg(MVT::f64);
1390
1391 if (cast<ConstantFPSDNode>(N)->isExactlyValue(+0.0) ||
1392 cast<ConstantFPSDNode>(N)->isExactlyValue(-0.0))
1393 BuildMI(BB, X86::FLD0, 0, Tmp1);
1394 else if (cast<ConstantFPSDNode>(N)->isExactlyValue(+1.0) ||
1395 cast<ConstantFPSDNode>(N)->isExactlyValue(-1.0))
1396 BuildMI(BB, X86::FLD1, 0, Tmp1);
1397 else
1398 assert(0 && "Unexpected constant!");
1399 if (Tmp1 != Result)
1400 BuildMI(BB, X86::FCHS, 1, Result).addReg(Tmp1);
1401 return Result;
1402 case ISD::Constant:
1403 switch (N.getValueType()) {
1404 default: assert(0 && "Cannot use constants of this type!");
1405 case MVT::i1:
1406 case MVT::i8: Opc = X86::MOV8ri; break;
1407 case MVT::i16: Opc = X86::MOV16ri; break;
1408 case MVT::i32: Opc = X86::MOV32ri; break;
1409 }
1410 BuildMI(BB, Opc, 1,Result).addImm(cast<ConstantSDNode>(N)->getValue());
1411 return Result;
Chris Lattner7ce7eff2005-04-01 22:46:45 +00001412 case ISD::UNDEF:
1413 if (Node->getValueType(0) == MVT::f64) {
1414 // FIXME: SHOULD TEACH STACKIFIER ABOUT UNDEF VALUES!
1415 BuildMI(BB, X86::FLD0, 0, Result);
1416 } else {
1417 BuildMI(BB, X86::IMPLICIT_DEF, 0, Result);
1418 }
1419 return Result;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001420 case ISD::GlobalAddress: {
1421 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
1422 BuildMI(BB, X86::MOV32ri, 1, Result).addGlobalAddress(GV);
1423 return Result;
1424 }
1425 case ISD::ExternalSymbol: {
1426 const char *Sym = cast<ExternalSymbolSDNode>(N)->getSymbol();
1427 BuildMI(BB, X86::MOV32ri, 1, Result).addExternalSymbol(Sym);
1428 return Result;
1429 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001430 case ISD::ZERO_EXTEND: {
1431 int DestIs16 = N.getValueType() == MVT::i16;
1432 int SrcIs16 = N.getOperand(0).getValueType() == MVT::i16;
Chris Lattner590d8002005-01-09 18:52:44 +00001433
1434 // FIXME: This hack is here for zero extension casts from bool to i8. This
1435 // would not be needed if bools were promoted by Legalize.
1436 if (N.getValueType() == MVT::i8) {
Chris Lattnerdbba22f2005-01-11 23:33:00 +00001437 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner590d8002005-01-09 18:52:44 +00001438 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(Tmp1);
1439 return Result;
1440 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001441
Chris Lattner4ff348b2005-01-17 06:26:58 +00001442 if (isFoldableLoad(N.getOperand(0), SDOperand())) {
Chris Lattnerdbba22f2005-01-11 23:33:00 +00001443 static const unsigned Opc[3] = {
1444 X86::MOVZX32rm8, X86::MOVZX32rm16, X86::MOVZX16rm8
1445 };
1446
1447 X86AddressMode AM;
1448 EmitFoldedLoad(N.getOperand(0), AM);
1449 addFullAddress(BuildMI(BB, Opc[SrcIs16+DestIs16*2], 4, Result), AM);
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001450
Chris Lattnerdbba22f2005-01-11 23:33:00 +00001451 return Result;
1452 }
1453
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001454 static const unsigned Opc[3] = {
1455 X86::MOVZX32rr8, X86::MOVZX32rr16, X86::MOVZX16rr8
1456 };
Chris Lattnerdbba22f2005-01-11 23:33:00 +00001457 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001458 BuildMI(BB, Opc[SrcIs16+DestIs16*2], 1, Result).addReg(Tmp1);
1459 return Result;
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001460 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001461 case ISD::SIGN_EXTEND: {
1462 int DestIs16 = N.getValueType() == MVT::i16;
1463 int SrcIs16 = N.getOperand(0).getValueType() == MVT::i16;
1464
Chris Lattner590d8002005-01-09 18:52:44 +00001465 // FIXME: Legalize should promote bools to i8!
1466 assert(N.getOperand(0).getValueType() != MVT::i1 &&
1467 "Sign extend from bool not implemented!");
1468
Chris Lattner4ff348b2005-01-17 06:26:58 +00001469 if (isFoldableLoad(N.getOperand(0), SDOperand())) {
Chris Lattnerdbba22f2005-01-11 23:33:00 +00001470 static const unsigned Opc[3] = {
1471 X86::MOVSX32rm8, X86::MOVSX32rm16, X86::MOVSX16rm8
1472 };
1473
1474 X86AddressMode AM;
1475 EmitFoldedLoad(N.getOperand(0), AM);
1476 addFullAddress(BuildMI(BB, Opc[SrcIs16+DestIs16*2], 4, Result), AM);
1477 return Result;
1478 }
1479
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001480 static const unsigned Opc[3] = {
1481 X86::MOVSX32rr8, X86::MOVSX32rr16, X86::MOVSX16rr8
1482 };
1483 Tmp1 = SelectExpr(N.getOperand(0));
1484 BuildMI(BB, Opc[SrcIs16+DestIs16*2], 1, Result).addReg(Tmp1);
1485 return Result;
1486 }
1487 case ISD::TRUNCATE:
Chris Lattnerafce4302005-01-12 02:19:06 +00001488 // Fold TRUNCATE (LOAD P) into a smaller load from P.
Chris Lattner477c9312005-01-18 20:05:56 +00001489 // FIXME: This should be performed by the DAGCombiner.
Chris Lattner4ff348b2005-01-17 06:26:58 +00001490 if (isFoldableLoad(N.getOperand(0), SDOperand())) {
Chris Lattnerafce4302005-01-12 02:19:06 +00001491 switch (N.getValueType()) {
1492 default: assert(0 && "Unknown truncate!");
1493 case MVT::i1:
1494 case MVT::i8: Opc = X86::MOV8rm; break;
1495 case MVT::i16: Opc = X86::MOV16rm; break;
1496 }
1497 X86AddressMode AM;
1498 EmitFoldedLoad(N.getOperand(0), AM);
1499 addFullAddress(BuildMI(BB, Opc, 4, Result), AM);
1500 return Result;
1501 }
1502
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001503 // Handle cast of LARGER int to SMALLER int using a move to EAX followed by
1504 // a move out of AX or AL.
1505 switch (N.getOperand(0).getValueType()) {
1506 default: assert(0 && "Unknown truncate!");
1507 case MVT::i8: Tmp2 = X86::AL; Opc = X86::MOV8rr; break;
1508 case MVT::i16: Tmp2 = X86::AX; Opc = X86::MOV16rr; break;
1509 case MVT::i32: Tmp2 = X86::EAX; Opc = X86::MOV32rr; break;
1510 }
1511 Tmp1 = SelectExpr(N.getOperand(0));
1512 BuildMI(BB, Opc, 1, Tmp2).addReg(Tmp1);
1513
1514 switch (N.getValueType()) {
1515 default: assert(0 && "Unknown truncate!");
1516 case MVT::i1:
1517 case MVT::i8: Tmp2 = X86::AL; Opc = X86::MOV8rr; break;
1518 case MVT::i16: Tmp2 = X86::AX; Opc = X86::MOV16rr; break;
1519 }
1520 BuildMI(BB, Opc, 1, Result).addReg(Tmp2);
1521 return Result;
1522
Chris Lattner590d8002005-01-09 18:52:44 +00001523 case ISD::SINT_TO_FP:
1524 case ISD::UINT_TO_FP: {
1525 // FIXME: Most of this grunt work should be done by legalize!
Chris Lattneref7ba072005-01-11 03:50:45 +00001526 ContainsFPCode = true;
Chris Lattner590d8002005-01-09 18:52:44 +00001527
1528 // Promote the integer to a type supported by FLD. We do this because there
1529 // are no unsigned FLD instructions, so we must promote an unsigned value to
1530 // a larger signed value, then use FLD on the larger value.
1531 //
1532 MVT::ValueType PromoteType = MVT::Other;
1533 MVT::ValueType SrcTy = N.getOperand(0).getValueType();
1534 unsigned PromoteOpcode = 0;
1535 unsigned RealDestReg = Result;
1536 switch (SrcTy) {
1537 case MVT::i1:
1538 case MVT::i8:
1539 // We don't have the facilities for directly loading byte sized data from
1540 // memory (even signed). Promote it to 16 bits.
1541 PromoteType = MVT::i16;
1542 PromoteOpcode = Node->getOpcode() == ISD::SINT_TO_FP ?
1543 X86::MOVSX16rr8 : X86::MOVZX16rr8;
1544 break;
1545 case MVT::i16:
1546 if (Node->getOpcode() == ISD::UINT_TO_FP) {
1547 PromoteType = MVT::i32;
1548 PromoteOpcode = X86::MOVZX32rr16;
1549 }
1550 break;
1551 default:
1552 // Don't fild into the real destination.
1553 if (Node->getOpcode() == ISD::UINT_TO_FP)
1554 Result = MakeReg(Node->getValueType(0));
1555 break;
1556 }
1557
1558 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001559
Chris Lattner590d8002005-01-09 18:52:44 +00001560 if (PromoteType != MVT::Other) {
1561 Tmp2 = MakeReg(PromoteType);
1562 BuildMI(BB, PromoteOpcode, 1, Tmp2).addReg(Tmp1);
1563 SrcTy = PromoteType;
1564 Tmp1 = Tmp2;
1565 }
1566
1567 // Spill the integer to memory and reload it from there.
1568 unsigned Size = MVT::getSizeInBits(SrcTy)/8;
1569 MachineFunction *F = BB->getParent();
1570 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, Size);
1571
1572 switch (SrcTy) {
1573 case MVT::i64:
Chris Lattner7dbcb752005-01-12 04:21:28 +00001574 assert(0 && "Cast ulong to FP not implemented yet!");
Chris Lattner590d8002005-01-09 18:52:44 +00001575 // FIXME: this won't work for cast [u]long to FP
1576 addFrameReference(BuildMI(BB, X86::MOV32mr, 5),
1577 FrameIdx).addReg(Tmp1);
1578 addFrameReference(BuildMI(BB, X86::MOV32mr, 5),
1579 FrameIdx, 4).addReg(Tmp1+1);
1580 addFrameReference(BuildMI(BB, X86::FILD64m, 5, Result), FrameIdx);
1581 break;
1582 case MVT::i32:
1583 addFrameReference(BuildMI(BB, X86::MOV32mr, 5),
1584 FrameIdx).addReg(Tmp1);
1585 addFrameReference(BuildMI(BB, X86::FILD32m, 5, Result), FrameIdx);
1586 break;
1587 case MVT::i16:
1588 addFrameReference(BuildMI(BB, X86::MOV16mr, 5),
1589 FrameIdx).addReg(Tmp1);
1590 addFrameReference(BuildMI(BB, X86::FILD16m, 5, Result), FrameIdx);
1591 break;
1592 default: break; // No promotion required.
1593 }
1594
Chris Lattner085c9952005-01-12 04:00:00 +00001595 if (Node->getOpcode() == ISD::UINT_TO_FP && Result != RealDestReg) {
Chris Lattner590d8002005-01-09 18:52:44 +00001596 // If this is a cast from uint -> double, we need to be careful when if
1597 // the "sign" bit is set. If so, we don't want to make a negative number,
1598 // we want to make a positive number. Emit code to add an offset if the
1599 // sign bit is set.
1600
1601 // Compute whether the sign bit is set by shifting the reg right 31 bits.
1602 unsigned IsNeg = MakeReg(MVT::i32);
1603 BuildMI(BB, X86::SHR32ri, 2, IsNeg).addReg(Tmp1).addImm(31);
1604
1605 // Create a CP value that has the offset in one word and 0 in the other.
1606 static ConstantInt *TheOffset = ConstantUInt::get(Type::ULongTy,
1607 0x4f80000000000000ULL);
1608 unsigned CPI = F->getConstantPool()->getConstantPoolIndex(TheOffset);
1609 BuildMI(BB, X86::FADD32m, 5, RealDestReg).addReg(Result)
1610 .addConstantPoolIndex(CPI).addZImm(4).addReg(IsNeg).addSImm(0);
1611
1612 } else if (Node->getOpcode() == ISD::UINT_TO_FP && SrcTy == MVT::i64) {
1613 // We need special handling for unsigned 64-bit integer sources. If the
1614 // input number has the "sign bit" set, then we loaded it incorrectly as a
1615 // negative 64-bit number. In this case, add an offset value.
1616
1617 // Emit a test instruction to see if the dynamic input value was signed.
1618 BuildMI(BB, X86::TEST32rr, 2).addReg(Tmp1+1).addReg(Tmp1+1);
1619
1620 // If the sign bit is set, get a pointer to an offset, otherwise get a
1621 // pointer to a zero.
1622 MachineConstantPool *CP = F->getConstantPool();
1623 unsigned Zero = MakeReg(MVT::i32);
1624 Constant *Null = Constant::getNullValue(Type::UIntTy);
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001625 addConstantPoolReference(BuildMI(BB, X86::LEA32r, 5, Zero),
Chris Lattner590d8002005-01-09 18:52:44 +00001626 CP->getConstantPoolIndex(Null));
1627 unsigned Offset = MakeReg(MVT::i32);
1628 Constant *OffsetCst = ConstantUInt::get(Type::UIntTy, 0x5f800000);
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001629
Chris Lattner590d8002005-01-09 18:52:44 +00001630 addConstantPoolReference(BuildMI(BB, X86::LEA32r, 5, Offset),
1631 CP->getConstantPoolIndex(OffsetCst));
1632 unsigned Addr = MakeReg(MVT::i32);
1633 BuildMI(BB, X86::CMOVS32rr, 2, Addr).addReg(Zero).addReg(Offset);
1634
1635 // Load the constant for an add. FIXME: this could make an 'fadd' that
1636 // reads directly from memory, but we don't support these yet.
1637 unsigned ConstReg = MakeReg(MVT::f64);
1638 addDirectMem(BuildMI(BB, X86::FLD32m, 4, ConstReg), Addr);
1639
1640 BuildMI(BB, X86::FpADD, 2, RealDestReg).addReg(ConstReg).addReg(Result);
1641 }
1642 return RealDestReg;
1643 }
1644 case ISD::FP_TO_SINT:
1645 case ISD::FP_TO_UINT: {
1646 // FIXME: Most of this grunt work should be done by legalize!
1647 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
1648
1649 // Change the floating point control register to use "round towards zero"
1650 // mode when truncating to an integer value.
1651 //
1652 MachineFunction *F = BB->getParent();
1653 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
1654 addFrameReference(BuildMI(BB, X86::FNSTCW16m, 4), CWFrameIdx);
1655
1656 // Load the old value of the high byte of the control word...
1657 unsigned HighPartOfCW = MakeReg(MVT::i8);
1658 addFrameReference(BuildMI(BB, X86::MOV8rm, 4, HighPartOfCW),
1659 CWFrameIdx, 1);
1660
1661 // Set the high part to be round to zero...
1662 addFrameReference(BuildMI(BB, X86::MOV8mi, 5),
1663 CWFrameIdx, 1).addImm(12);
1664
1665 // Reload the modified control word now...
1666 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001667
Chris Lattner590d8002005-01-09 18:52:44 +00001668 // Restore the memory image of control word to original value
1669 addFrameReference(BuildMI(BB, X86::MOV8mr, 5),
1670 CWFrameIdx, 1).addReg(HighPartOfCW);
1671
1672 // We don't have the facilities for directly storing byte sized data to
1673 // memory. Promote it to 16 bits. We also must promote unsigned values to
1674 // larger classes because we only have signed FP stores.
1675 MVT::ValueType StoreClass = Node->getValueType(0);
1676 if (StoreClass == MVT::i8 || Node->getOpcode() == ISD::FP_TO_UINT)
1677 switch (StoreClass) {
1678 case MVT::i8: StoreClass = MVT::i16; break;
1679 case MVT::i16: StoreClass = MVT::i32; break;
1680 case MVT::i32: StoreClass = MVT::i64; break;
1681 // The following treatment of cLong may not be perfectly right,
1682 // but it survives chains of casts of the form
1683 // double->ulong->double.
1684 case MVT::i64: StoreClass = MVT::i64; break;
1685 default: assert(0 && "Unknown store class!");
1686 }
1687
1688 // Spill the integer to memory and reload it from there.
1689 unsigned Size = MVT::getSizeInBits(StoreClass)/8;
1690 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, Size);
1691
1692 switch (StoreClass) {
1693 default: assert(0 && "Unknown store class!");
1694 case MVT::i16:
1695 addFrameReference(BuildMI(BB, X86::FIST16m, 5), FrameIdx).addReg(Tmp1);
1696 break;
1697 case MVT::i32:
Chris Lattner25020852005-01-09 19:49:59 +00001698 addFrameReference(BuildMI(BB, X86::FIST32m, 5), FrameIdx).addReg(Tmp1);
Chris Lattner590d8002005-01-09 18:52:44 +00001699 break;
1700 case MVT::i64:
Chris Lattner25020852005-01-09 19:49:59 +00001701 addFrameReference(BuildMI(BB, X86::FISTP64m, 5), FrameIdx).addReg(Tmp1);
Chris Lattner590d8002005-01-09 18:52:44 +00001702 break;
1703 }
1704
1705 switch (Node->getValueType(0)) {
1706 default:
1707 assert(0 && "Unknown integer type!");
1708 case MVT::i64:
1709 // FIXME: this isn't gunna work.
Chris Lattner7dbcb752005-01-12 04:21:28 +00001710 assert(0 && "Cast FP to long not implemented yet!");
Chris Lattner590d8002005-01-09 18:52:44 +00001711 addFrameReference(BuildMI(BB, X86::MOV32rm, 4, Result), FrameIdx);
1712 addFrameReference(BuildMI(BB, X86::MOV32rm, 4, Result+1), FrameIdx, 4);
1713 case MVT::i32:
1714 addFrameReference(BuildMI(BB, X86::MOV32rm, 4, Result), FrameIdx);
1715 break;
1716 case MVT::i16:
1717 addFrameReference(BuildMI(BB, X86::MOV16rm, 4, Result), FrameIdx);
1718 break;
1719 case MVT::i8:
1720 addFrameReference(BuildMI(BB, X86::MOV8rm, 4, Result), FrameIdx);
1721 break;
1722 }
1723
1724 // Reload the original control word now.
1725 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1726 return Result;
1727 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001728 case ISD::ADD:
Chris Lattnera5ade062005-01-11 21:19:59 +00001729 Op0 = N.getOperand(0);
1730 Op1 = N.getOperand(1);
1731
Chris Lattner44129b52005-01-25 20:03:11 +00001732 if (isFoldableLoad(Op0, Op1, true)) {
Chris Lattnera5ade062005-01-11 21:19:59 +00001733 std::swap(Op0, Op1);
Chris Lattner4ff348b2005-01-17 06:26:58 +00001734 goto FoldAdd;
1735 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001736
Chris Lattner44129b52005-01-25 20:03:11 +00001737 if (isFoldableLoad(Op1, Op0, true)) {
Chris Lattner4ff348b2005-01-17 06:26:58 +00001738 FoldAdd:
Chris Lattnera5ade062005-01-11 21:19:59 +00001739 switch (N.getValueType()) {
1740 default: assert(0 && "Cannot add this type!");
1741 case MVT::i1:
1742 case MVT::i8: Opc = X86::ADD8rm; break;
1743 case MVT::i16: Opc = X86::ADD16rm; break;
1744 case MVT::i32: Opc = X86::ADD32rm; break;
Chris Lattner44129b52005-01-25 20:03:11 +00001745 case MVT::f64:
1746 // For F64, handle promoted load operations (from F32) as well!
1747 Opc = Op1.getOpcode() == ISD::LOAD ? X86::FADD64m : X86::FADD32m;
1748 break;
Chris Lattnera5ade062005-01-11 21:19:59 +00001749 }
1750 X86AddressMode AM;
Chris Lattner636e79a2005-01-13 05:53:16 +00001751 EmitFoldedLoad(Op1, AM);
1752 Tmp1 = SelectExpr(Op0);
Chris Lattnera5ade062005-01-11 21:19:59 +00001753 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
1754 return Result;
1755 }
1756
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001757 // See if we can codegen this as an LEA to fold operations together.
1758 if (N.getValueType() == MVT::i32) {
Chris Lattner883c86f2005-01-18 02:25:52 +00001759 ExprMap.erase(N);
Chris Lattner98a8ba02005-01-18 01:06:26 +00001760 X86ISelAddressMode AM;
Chris Lattner883c86f2005-01-18 02:25:52 +00001761 MatchAddress(N, AM);
1762 ExprMap[N] = Result;
1763
1764 // If this is not just an add, emit the LEA. For a simple add (like
1765 // reg+reg or reg+imm), we just emit an add. It might be a good idea to
1766 // leave this as LEA, then peephole it to 'ADD' after two address elim
1767 // happens.
1768 if (AM.Scale != 1 || AM.BaseType == X86ISelAddressMode::FrameIndexBase||
1769 AM.GV || (AM.Base.Reg.Val && AM.IndexReg.Val && AM.Disp)) {
1770 X86AddressMode XAM = SelectAddrExprs(AM);
1771 addFullAddress(BuildMI(BB, X86::LEA32r, 4, Result), XAM);
1772 return Result;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001773 }
1774 }
Chris Lattner11333092005-01-11 03:11:44 +00001775
Chris Lattnera5ade062005-01-11 21:19:59 +00001776 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1)) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001777 Opc = 0;
1778 if (CN->getValue() == 1) { // add X, 1 -> inc X
1779 switch (N.getValueType()) {
1780 default: assert(0 && "Cannot integer add this type!");
1781 case MVT::i8: Opc = X86::INC8r; break;
1782 case MVT::i16: Opc = X86::INC16r; break;
1783 case MVT::i32: Opc = X86::INC32r; break;
1784 }
1785 } else if (CN->isAllOnesValue()) { // add X, -1 -> dec X
1786 switch (N.getValueType()) {
1787 default: assert(0 && "Cannot integer add this type!");
1788 case MVT::i8: Opc = X86::DEC8r; break;
1789 case MVT::i16: Opc = X86::DEC16r; break;
1790 case MVT::i32: Opc = X86::DEC32r; break;
1791 }
1792 }
1793
1794 if (Opc) {
Chris Lattnera5ade062005-01-11 21:19:59 +00001795 Tmp1 = SelectExpr(Op0);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001796 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1797 return Result;
1798 }
1799
1800 switch (N.getValueType()) {
1801 default: assert(0 && "Cannot add this type!");
1802 case MVT::i8: Opc = X86::ADD8ri; break;
1803 case MVT::i16: Opc = X86::ADD16ri; break;
1804 case MVT::i32: Opc = X86::ADD32ri; break;
1805 }
1806 if (Opc) {
Chris Lattnera5ade062005-01-11 21:19:59 +00001807 Tmp1 = SelectExpr(Op0);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001808 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
1809 return Result;
1810 }
1811 }
1812
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001813 switch (N.getValueType()) {
1814 default: assert(0 && "Cannot add this type!");
1815 case MVT::i8: Opc = X86::ADD8rr; break;
1816 case MVT::i16: Opc = X86::ADD16rr; break;
1817 case MVT::i32: Opc = X86::ADD32rr; break;
Chris Lattneref7ba072005-01-11 03:50:45 +00001818 case MVT::f64: Opc = X86::FpADD; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001819 }
Chris Lattner11333092005-01-11 03:11:44 +00001820
Chris Lattnera5ade062005-01-11 21:19:59 +00001821 if (getRegPressure(Op0) > getRegPressure(Op1)) {
1822 Tmp1 = SelectExpr(Op0);
1823 Tmp2 = SelectExpr(Op1);
Chris Lattner11333092005-01-11 03:11:44 +00001824 } else {
Chris Lattnera5ade062005-01-11 21:19:59 +00001825 Tmp2 = SelectExpr(Op1);
1826 Tmp1 = SelectExpr(Op0);
Chris Lattner11333092005-01-11 03:11:44 +00001827 }
1828
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001829 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1830 return Result;
Chris Lattnerb7edaa12005-04-02 05:30:17 +00001831
1832 case ISD::FABS:
Chris Lattnerb7edaa12005-04-02 05:30:17 +00001833 case ISD::FNEG:
Chris Lattner2c56e8a2005-04-28 22:07:18 +00001834 case ISD::FSQRT:
1835 assert(N.getValueType()==MVT::f64 && "Illegal type for this operation");
Chris Lattnerb7edaa12005-04-02 05:30:17 +00001836 Tmp1 = SelectExpr(Node->getOperand(0));
Chris Lattner2c56e8a2005-04-28 22:07:18 +00001837 switch (N.getOpcode()) {
1838 default: assert(0 && "Unreachable!");
1839 case ISD::FABS: BuildMI(BB, X86::FABS, 1, Result).addReg(Tmp1); break;
1840 case ISD::FNEG: BuildMI(BB, X86::FCHS, 1, Result).addReg(Tmp1); break;
1841 case ISD::FSQRT: BuildMI(BB, X86::FSQRT, 1, Result).addReg(Tmp1); break;
1842 }
Chris Lattnerb7edaa12005-04-02 05:30:17 +00001843 return Result;
1844
Chris Lattner8db0af12005-04-06 04:21:07 +00001845 case ISD::MULHU:
1846 switch (N.getValueType()) {
1847 default: assert(0 && "Unsupported VT!");
1848 case MVT::i8: Tmp2 = X86::MUL8r; break;
1849 case MVT::i16: Tmp2 = X86::MUL16r; break;
1850 case MVT::i32: Tmp2 = X86::MUL32r; break;
1851 }
1852 // FALL THROUGH
1853 case ISD::MULHS: {
1854 unsigned MovOpc, LowReg, HiReg;
1855 switch (N.getValueType()) {
1856 default: assert(0 && "Unsupported VT!");
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001857 case MVT::i8:
Chris Lattner8db0af12005-04-06 04:21:07 +00001858 MovOpc = X86::MOV8rr;
1859 LowReg = X86::AL;
1860 HiReg = X86::AH;
1861 Opc = X86::IMUL8r;
1862 break;
1863 case MVT::i16:
1864 MovOpc = X86::MOV16rr;
1865 LowReg = X86::AX;
1866 HiReg = X86::DX;
1867 Opc = X86::IMUL16r;
1868 break;
1869 case MVT::i32:
1870 MovOpc = X86::MOV32rr;
1871 LowReg = X86::EAX;
1872 HiReg = X86::EDX;
1873 Opc = X86::IMUL32r;
1874 break;
1875 }
1876 if (Node->getOpcode() != ISD::MULHS)
1877 Opc = Tmp2; // Get the MULHU opcode.
1878
1879 Op0 = Node->getOperand(0);
1880 Op1 = Node->getOperand(1);
1881 if (getRegPressure(Op0) > getRegPressure(Op1)) {
1882 Tmp1 = SelectExpr(Op0);
1883 Tmp2 = SelectExpr(Op1);
1884 } else {
1885 Tmp2 = SelectExpr(Op1);
1886 Tmp1 = SelectExpr(Op0);
1887 }
1888
1889 // FIXME: Implement folding of loads into the memory operands here!
1890 BuildMI(BB, MovOpc, 1, LowReg).addReg(Tmp1);
1891 BuildMI(BB, Opc, 1).addReg(Tmp2);
1892 BuildMI(BB, MovOpc, 1, Result).addReg(HiReg);
1893 return Result;
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001894 }
Chris Lattner8db0af12005-04-06 04:21:07 +00001895
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001896 case ISD::SUB:
Chris Lattnera5ade062005-01-11 21:19:59 +00001897 case ISD::MUL:
1898 case ISD::AND:
1899 case ISD::OR:
Chris Lattnera56cea42005-01-12 04:23:22 +00001900 case ISD::XOR: {
Chris Lattnera5ade062005-01-11 21:19:59 +00001901 static const unsigned SUBTab[] = {
1902 X86::SUB8ri, X86::SUB16ri, X86::SUB32ri, 0, 0,
1903 X86::SUB8rm, X86::SUB16rm, X86::SUB32rm, X86::FSUB32m, X86::FSUB64m,
1904 X86::SUB8rr, X86::SUB16rr, X86::SUB32rr, X86::FpSUB , X86::FpSUB,
1905 };
1906 static const unsigned MULTab[] = {
1907 0, X86::IMUL16rri, X86::IMUL32rri, 0, 0,
1908 0, X86::IMUL16rm , X86::IMUL32rm, X86::FMUL32m, X86::FMUL64m,
1909 0, X86::IMUL16rr , X86::IMUL32rr, X86::FpMUL , X86::FpMUL,
1910 };
1911 static const unsigned ANDTab[] = {
1912 X86::AND8ri, X86::AND16ri, X86::AND32ri, 0, 0,
1913 X86::AND8rm, X86::AND16rm, X86::AND32rm, 0, 0,
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001914 X86::AND8rr, X86::AND16rr, X86::AND32rr, 0, 0,
Chris Lattnera5ade062005-01-11 21:19:59 +00001915 };
1916 static const unsigned ORTab[] = {
1917 X86::OR8ri, X86::OR16ri, X86::OR32ri, 0, 0,
1918 X86::OR8rm, X86::OR16rm, X86::OR32rm, 0, 0,
1919 X86::OR8rr, X86::OR16rr, X86::OR32rr, 0, 0,
1920 };
1921 static const unsigned XORTab[] = {
1922 X86::XOR8ri, X86::XOR16ri, X86::XOR32ri, 0, 0,
1923 X86::XOR8rm, X86::XOR16rm, X86::XOR32rm, 0, 0,
1924 X86::XOR8rr, X86::XOR16rr, X86::XOR32rr, 0, 0,
1925 };
1926
1927 Op0 = Node->getOperand(0);
1928 Op1 = Node->getOperand(1);
1929
Chris Lattner30ea1e92005-01-19 07:37:26 +00001930 if (Node->getOpcode() == ISD::OR && Op0.hasOneUse() && Op1.hasOneUse())
1931 if (EmitOrOpOp(Op0, Op1, Result)) // Match SHLD, SHRD, and rotates.
Chris Lattner85716372005-01-19 06:18:43 +00001932 return Result;
1933
1934 if (Node->getOpcode() == ISD::SUB)
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001935 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(0)))
1936 if (CN->isNullValue()) { // 0 - N -> neg N
1937 switch (N.getValueType()) {
1938 default: assert(0 && "Cannot sub this type!");
1939 case MVT::i1:
1940 case MVT::i8: Opc = X86::NEG8r; break;
1941 case MVT::i16: Opc = X86::NEG16r; break;
1942 case MVT::i32: Opc = X86::NEG32r; break;
1943 }
1944 Tmp1 = SelectExpr(N.getOperand(1));
1945 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1946 return Result;
1947 }
1948
Chris Lattnera5ade062005-01-11 21:19:59 +00001949 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1)) {
1950 if (CN->isAllOnesValue() && Node->getOpcode() == ISD::XOR) {
Chris Lattnerc98279d2005-01-17 00:23:16 +00001951 Opc = 0;
Chris Lattnerd4dab922005-01-11 04:31:30 +00001952 switch (N.getValueType()) {
1953 default: assert(0 && "Cannot add this type!");
Chris Lattnerc98279d2005-01-17 00:23:16 +00001954 case MVT::i1: break; // Not supported, don't invert upper bits!
Chris Lattnerd4dab922005-01-11 04:31:30 +00001955 case MVT::i8: Opc = X86::NOT8r; break;
1956 case MVT::i16: Opc = X86::NOT16r; break;
1957 case MVT::i32: Opc = X86::NOT32r; break;
1958 }
Chris Lattnerc98279d2005-01-17 00:23:16 +00001959 if (Opc) {
1960 Tmp1 = SelectExpr(Op0);
1961 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1962 return Result;
1963 }
Chris Lattnerd4dab922005-01-11 04:31:30 +00001964 }
1965
Chris Lattner2a4e5082005-01-17 06:48:02 +00001966 // Fold common multiplies into LEA instructions.
1967 if (Node->getOpcode() == ISD::MUL && N.getValueType() == MVT::i32) {
1968 switch ((int)CN->getValue()) {
1969 default: break;
1970 case 3:
1971 case 5:
1972 case 9:
Chris Lattner2a4e5082005-01-17 06:48:02 +00001973 // Remove N from exprmap so SelectAddress doesn't get confused.
1974 ExprMap.erase(N);
Chris Lattner98a8ba02005-01-18 01:06:26 +00001975 X86AddressMode AM;
Chris Lattner2a4e5082005-01-17 06:48:02 +00001976 SelectAddress(N, AM);
1977 // Restore it to the map.
1978 ExprMap[N] = Result;
1979 addFullAddress(BuildMI(BB, X86::LEA32r, 4, Result), AM);
1980 return Result;
1981 }
1982 }
1983
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001984 switch (N.getValueType()) {
Chris Lattnerd4dab922005-01-11 04:31:30 +00001985 default: assert(0 && "Cannot xor this type!");
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001986 case MVT::i1:
Chris Lattnera5ade062005-01-11 21:19:59 +00001987 case MVT::i8: Opc = 0; break;
1988 case MVT::i16: Opc = 1; break;
1989 case MVT::i32: Opc = 2; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001990 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001991 switch (Node->getOpcode()) {
1992 default: assert(0 && "Unreachable!");
1993 case ISD::SUB: Opc = SUBTab[Opc]; break;
1994 case ISD::MUL: Opc = MULTab[Opc]; break;
1995 case ISD::AND: Opc = ANDTab[Opc]; break;
1996 case ISD::OR: Opc = ORTab[Opc]; break;
1997 case ISD::XOR: Opc = XORTab[Opc]; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001998 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001999 if (Opc) { // Can't fold MUL:i8 R, imm
2000 Tmp1 = SelectExpr(Op0);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002001 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
2002 return Result;
2003 }
2004 }
Chris Lattner11333092005-01-11 03:11:44 +00002005
Chris Lattner44129b52005-01-25 20:03:11 +00002006 if (isFoldableLoad(Op0, Op1, true))
Chris Lattnera5ade062005-01-11 21:19:59 +00002007 if (Node->getOpcode() != ISD::SUB) {
2008 std::swap(Op0, Op1);
Chris Lattner4ff348b2005-01-17 06:26:58 +00002009 goto FoldOps;
Chris Lattnera5ade062005-01-11 21:19:59 +00002010 } else {
Chris Lattner44129b52005-01-25 20:03:11 +00002011 // For FP, emit 'reverse' subract, with a memory operand.
2012 if (N.getValueType() == MVT::f64) {
2013 if (Op0.getOpcode() == ISD::EXTLOAD)
2014 Opc = X86::FSUBR32m;
2015 else
2016 Opc = X86::FSUBR64m;
2017
Chris Lattnera5ade062005-01-11 21:19:59 +00002018 X86AddressMode AM;
Chris Lattner636e79a2005-01-13 05:53:16 +00002019 EmitFoldedLoad(Op0, AM);
2020 Tmp1 = SelectExpr(Op1);
Chris Lattnera5ade062005-01-11 21:19:59 +00002021 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
2022 return Result;
2023 }
2024 }
2025
Chris Lattner44129b52005-01-25 20:03:11 +00002026 if (isFoldableLoad(Op1, Op0, true)) {
Chris Lattner4ff348b2005-01-17 06:26:58 +00002027 FoldOps:
Chris Lattnera5ade062005-01-11 21:19:59 +00002028 switch (N.getValueType()) {
2029 default: assert(0 && "Cannot operate on this type!");
2030 case MVT::i1:
2031 case MVT::i8: Opc = 5; break;
2032 case MVT::i16: Opc = 6; break;
2033 case MVT::i32: Opc = 7; break;
Chris Lattner44129b52005-01-25 20:03:11 +00002034 // For F64, handle promoted load operations (from F32) as well!
2035 case MVT::f64: Opc = Op1.getOpcode() == ISD::LOAD ? 9 : 8; break;
Chris Lattnera5ade062005-01-11 21:19:59 +00002036 }
2037 switch (Node->getOpcode()) {
2038 default: assert(0 && "Unreachable!");
2039 case ISD::SUB: Opc = SUBTab[Opc]; break;
2040 case ISD::MUL: Opc = MULTab[Opc]; break;
2041 case ISD::AND: Opc = ANDTab[Opc]; break;
2042 case ISD::OR: Opc = ORTab[Opc]; break;
2043 case ISD::XOR: Opc = XORTab[Opc]; break;
2044 }
2045
2046 X86AddressMode AM;
Chris Lattner636e79a2005-01-13 05:53:16 +00002047 EmitFoldedLoad(Op1, AM);
2048 Tmp1 = SelectExpr(Op0);
Chris Lattnera5ade062005-01-11 21:19:59 +00002049 if (Opc) {
2050 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
2051 } else {
2052 assert(Node->getOpcode() == ISD::MUL &&
2053 N.getValueType() == MVT::i8 && "Unexpected situation!");
2054 // Must use the MUL instruction, which forces use of AL.
2055 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(Tmp1);
2056 addFullAddress(BuildMI(BB, X86::MUL8m, 1), AM);
2057 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
2058 }
2059 return Result;
Chris Lattner11333092005-01-11 03:11:44 +00002060 }
Chris Lattnera5ade062005-01-11 21:19:59 +00002061
2062 if (getRegPressure(Op0) > getRegPressure(Op1)) {
2063 Tmp1 = SelectExpr(Op0);
2064 Tmp2 = SelectExpr(Op1);
2065 } else {
2066 Tmp2 = SelectExpr(Op1);
2067 Tmp1 = SelectExpr(Op0);
2068 }
2069
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002070 switch (N.getValueType()) {
2071 default: assert(0 && "Cannot add this type!");
Chris Lattnera5ade062005-01-11 21:19:59 +00002072 case MVT::i1:
2073 case MVT::i8: Opc = 10; break;
2074 case MVT::i16: Opc = 11; break;
2075 case MVT::i32: Opc = 12; break;
2076 case MVT::f32: Opc = 13; break;
2077 case MVT::f64: Opc = 14; break;
2078 }
2079 switch (Node->getOpcode()) {
2080 default: assert(0 && "Unreachable!");
2081 case ISD::SUB: Opc = SUBTab[Opc]; break;
2082 case ISD::MUL: Opc = MULTab[Opc]; break;
2083 case ISD::AND: Opc = ANDTab[Opc]; break;
2084 case ISD::OR: Opc = ORTab[Opc]; break;
2085 case ISD::XOR: Opc = XORTab[Opc]; break;
2086 }
2087 if (Opc) {
2088 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
2089 } else {
2090 assert(Node->getOpcode() == ISD::MUL &&
2091 N.getValueType() == MVT::i8 && "Unexpected situation!");
Chris Lattnera13d3232005-01-10 20:55:48 +00002092 // Must use the MUL instruction, which forces use of AL.
2093 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(Tmp1);
2094 BuildMI(BB, X86::MUL8r, 1).addReg(Tmp2);
2095 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002096 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002097 return Result;
Chris Lattnera56cea42005-01-12 04:23:22 +00002098 }
Chris Lattner19ad0622005-01-20 18:53:00 +00002099 case ISD::ADD_PARTS:
2100 case ISD::SUB_PARTS: {
2101 assert(N.getNumOperands() == 4 && N.getValueType() == MVT::i32 &&
2102 "Not an i64 add/sub!");
2103 // Emit all of the operands.
2104 std::vector<unsigned> InVals;
2105 for (unsigned i = 0, e = N.getNumOperands(); i != e; ++i)
2106 InVals.push_back(SelectExpr(N.getOperand(i)));
2107 if (N.getOpcode() == ISD::ADD_PARTS) {
2108 BuildMI(BB, X86::ADD32rr, 2, Result).addReg(InVals[0]).addReg(InVals[2]);
2109 BuildMI(BB, X86::ADC32rr,2,Result+1).addReg(InVals[1]).addReg(InVals[3]);
2110 } else {
2111 BuildMI(BB, X86::SUB32rr, 2, Result).addReg(InVals[0]).addReg(InVals[2]);
2112 BuildMI(BB, X86::SBB32rr, 2,Result+1).addReg(InVals[1]).addReg(InVals[3]);
2113 }
2114 return Result+N.ResNo;
2115 }
2116
Chris Lattnerb38a7492005-04-02 04:01:14 +00002117 case ISD::SHL_PARTS:
2118 case ISD::SRA_PARTS:
2119 case ISD::SRL_PARTS: {
2120 assert(N.getNumOperands() == 3 && N.getValueType() == MVT::i32 &&
2121 "Not an i64 shift!");
2122 unsigned ShiftOpLo = SelectExpr(N.getOperand(0));
2123 unsigned ShiftOpHi = SelectExpr(N.getOperand(1));
2124 unsigned TmpReg = MakeReg(MVT::i32);
2125 if (N.getOpcode() == ISD::SRA_PARTS) {
2126 // If this is a SHR of a Long, then we need to do funny sign extension
2127 // stuff. TmpReg gets the value to use as the high-part if we are
2128 // shifting more than 32 bits.
2129 BuildMI(BB, X86::SAR32ri, 2, TmpReg).addReg(ShiftOpHi).addImm(31);
2130 } else {
2131 // Other shifts use a fixed zero value if the shift is more than 32 bits.
2132 BuildMI(BB, X86::MOV32ri, 1, TmpReg).addImm(0);
2133 }
2134
2135 // Initialize CL with the shift amount.
2136 unsigned ShiftAmountReg = SelectExpr(N.getOperand(2));
2137 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShiftAmountReg);
2138
2139 unsigned TmpReg2 = MakeReg(MVT::i32);
2140 unsigned TmpReg3 = MakeReg(MVT::i32);
2141 if (N.getOpcode() == ISD::SHL_PARTS) {
2142 // TmpReg2 = shld inHi, inLo
2143 BuildMI(BB, X86::SHLD32rrCL, 2,TmpReg2).addReg(ShiftOpHi)
2144 .addReg(ShiftOpLo);
2145 // TmpReg3 = shl inLo, CL
2146 BuildMI(BB, X86::SHL32rCL, 1, TmpReg3).addReg(ShiftOpLo);
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002147
Chris Lattnerb38a7492005-04-02 04:01:14 +00002148 // Set the flags to indicate whether the shift was by more than 32 bits.
2149 BuildMI(BB, X86::TEST8ri, 2).addReg(X86::CL).addImm(32);
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002150
Chris Lattnerb38a7492005-04-02 04:01:14 +00002151 // DestHi = (>32) ? TmpReg3 : TmpReg2;
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002152 BuildMI(BB, X86::CMOVNE32rr, 2,
Chris Lattnerb38a7492005-04-02 04:01:14 +00002153 Result+1).addReg(TmpReg2).addReg(TmpReg3);
2154 // DestLo = (>32) ? TmpReg : TmpReg3;
2155 BuildMI(BB, X86::CMOVNE32rr, 2,
2156 Result).addReg(TmpReg3).addReg(TmpReg);
2157 } else {
2158 // TmpReg2 = shrd inLo, inHi
2159 BuildMI(BB, X86::SHRD32rrCL,2,TmpReg2).addReg(ShiftOpLo)
2160 .addReg(ShiftOpHi);
2161 // TmpReg3 = s[ah]r inHi, CL
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002162 BuildMI(BB, N.getOpcode() == ISD::SRA_PARTS ? X86::SAR32rCL
Chris Lattnerb38a7492005-04-02 04:01:14 +00002163 : X86::SHR32rCL, 1, TmpReg3)
2164 .addReg(ShiftOpHi);
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002165
Chris Lattnerb38a7492005-04-02 04:01:14 +00002166 // Set the flags to indicate whether the shift was by more than 32 bits.
2167 BuildMI(BB, X86::TEST8ri, 2).addReg(X86::CL).addImm(32);
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002168
Chris Lattnerb38a7492005-04-02 04:01:14 +00002169 // DestLo = (>32) ? TmpReg3 : TmpReg2;
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002170 BuildMI(BB, X86::CMOVNE32rr, 2,
Chris Lattnerb38a7492005-04-02 04:01:14 +00002171 Result).addReg(TmpReg2).addReg(TmpReg3);
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002172
Chris Lattnerb38a7492005-04-02 04:01:14 +00002173 // DestHi = (>32) ? TmpReg : TmpReg3;
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002174 BuildMI(BB, X86::CMOVNE32rr, 2,
Chris Lattnerb38a7492005-04-02 04:01:14 +00002175 Result+1).addReg(TmpReg3).addReg(TmpReg);
2176 }
2177 return Result+N.ResNo;
2178 }
2179
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002180 case ISD::SELECT:
Chris Lattnerda2ce112005-01-16 07:34:08 +00002181 if (getRegPressure(N.getOperand(1)) > getRegPressure(N.getOperand(2))) {
2182 Tmp2 = SelectExpr(N.getOperand(1));
2183 Tmp3 = SelectExpr(N.getOperand(2));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002184 } else {
Chris Lattnerda2ce112005-01-16 07:34:08 +00002185 Tmp3 = SelectExpr(N.getOperand(2));
2186 Tmp2 = SelectExpr(N.getOperand(1));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002187 }
Chris Lattnerda2ce112005-01-16 07:34:08 +00002188 EmitSelectCC(N.getOperand(0), N.getValueType(), Tmp2, Tmp3, Result);
2189 return Result;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002190
2191 case ISD::SDIV:
2192 case ISD::UDIV:
2193 case ISD::SREM:
2194 case ISD::UREM: {
Chris Lattnerda2ce112005-01-16 07:34:08 +00002195 assert((N.getOpcode() != ISD::SREM || MVT::isInteger(N.getValueType())) &&
2196 "We don't support this operator!");
2197
Chris Lattner5bf26862005-04-13 03:29:53 +00002198 if (N.getOpcode() == ISD::SDIV) {
Chris Lattner3576c842005-01-25 20:35:10 +00002199 // We can fold loads into FpDIVs, but not really into any others.
2200 if (N.getValueType() == MVT::f64) {
2201 // Check for reversed and unreversed DIV.
2202 if (isFoldableLoad(N.getOperand(0), N.getOperand(1), true)) {
2203 if (N.getOperand(0).getOpcode() == ISD::EXTLOAD)
2204 Opc = X86::FDIVR32m;
2205 else
2206 Opc = X86::FDIVR64m;
2207 X86AddressMode AM;
2208 EmitFoldedLoad(N.getOperand(0), AM);
2209 Tmp1 = SelectExpr(N.getOperand(1));
2210 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
2211 return Result;
2212 } else if (isFoldableLoad(N.getOperand(1), N.getOperand(0), true) &&
2213 N.getOperand(1).getOpcode() == ISD::LOAD) {
2214 if (N.getOperand(1).getOpcode() == ISD::EXTLOAD)
2215 Opc = X86::FDIV32m;
2216 else
2217 Opc = X86::FDIV64m;
2218 X86AddressMode AM;
2219 EmitFoldedLoad(N.getOperand(1), AM);
2220 Tmp1 = SelectExpr(N.getOperand(0));
2221 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
2222 return Result;
2223 }
2224 }
2225
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002226 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
2227 // FIXME: These special cases should be handled by the lowering impl!
2228 unsigned RHS = CN->getValue();
2229 bool isNeg = false;
2230 if ((int)RHS < 0) {
2231 isNeg = true;
2232 RHS = -RHS;
2233 }
2234 if (RHS && (RHS & (RHS-1)) == 0) { // Signed division by power of 2?
2235 unsigned Log = log2(RHS);
2236 unsigned TmpReg = MakeReg(N.getValueType());
2237 unsigned SAROpc, SHROpc, ADDOpc, NEGOpc;
2238 switch (N.getValueType()) {
2239 default: assert("Unknown type to signed divide!");
2240 case MVT::i8:
2241 SAROpc = X86::SAR8ri;
2242 SHROpc = X86::SHR8ri;
2243 ADDOpc = X86::ADD8rr;
2244 NEGOpc = X86::NEG8r;
2245 break;
2246 case MVT::i16:
2247 SAROpc = X86::SAR16ri;
2248 SHROpc = X86::SHR16ri;
2249 ADDOpc = X86::ADD16rr;
2250 NEGOpc = X86::NEG16r;
2251 break;
2252 case MVT::i32:
2253 SAROpc = X86::SAR32ri;
2254 SHROpc = X86::SHR32ri;
2255 ADDOpc = X86::ADD32rr;
2256 NEGOpc = X86::NEG32r;
2257 break;
2258 }
Chris Lattner11333092005-01-11 03:11:44 +00002259 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002260 BuildMI(BB, SAROpc, 2, TmpReg).addReg(Tmp1).addImm(Log-1);
2261 unsigned TmpReg2 = MakeReg(N.getValueType());
2262 BuildMI(BB, SHROpc, 2, TmpReg2).addReg(TmpReg).addImm(32-Log);
2263 unsigned TmpReg3 = MakeReg(N.getValueType());
2264 BuildMI(BB, ADDOpc, 2, TmpReg3).addReg(Tmp1).addReg(TmpReg2);
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002265
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002266 unsigned TmpReg4 = isNeg ? MakeReg(N.getValueType()) : Result;
2267 BuildMI(BB, SAROpc, 2, TmpReg4).addReg(TmpReg3).addImm(Log);
2268 if (isNeg)
2269 BuildMI(BB, NEGOpc, 1, Result).addReg(TmpReg4);
2270 return Result;
2271 }
2272 }
Chris Lattner5bf26862005-04-13 03:29:53 +00002273 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002274
Chris Lattner11333092005-01-11 03:11:44 +00002275 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2276 Tmp1 = SelectExpr(N.getOperand(0));
2277 Tmp2 = SelectExpr(N.getOperand(1));
2278 } else {
2279 Tmp2 = SelectExpr(N.getOperand(1));
2280 Tmp1 = SelectExpr(N.getOperand(0));
2281 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002282
2283 bool isSigned = N.getOpcode() == ISD::SDIV || N.getOpcode() == ISD::SREM;
2284 bool isDiv = N.getOpcode() == ISD::SDIV || N.getOpcode() == ISD::UDIV;
2285 unsigned LoReg, HiReg, DivOpcode, MovOpcode, ClrOpcode, SExtOpcode;
2286 switch (N.getValueType()) {
2287 default: assert(0 && "Cannot sdiv this type!");
2288 case MVT::i8:
2289 DivOpcode = isSigned ? X86::IDIV8r : X86::DIV8r;
2290 LoReg = X86::AL;
2291 HiReg = X86::AH;
2292 MovOpcode = X86::MOV8rr;
2293 ClrOpcode = X86::MOV8ri;
2294 SExtOpcode = X86::CBW;
2295 break;
2296 case MVT::i16:
2297 DivOpcode = isSigned ? X86::IDIV16r : X86::DIV16r;
2298 LoReg = X86::AX;
2299 HiReg = X86::DX;
2300 MovOpcode = X86::MOV16rr;
2301 ClrOpcode = X86::MOV16ri;
2302 SExtOpcode = X86::CWD;
2303 break;
2304 case MVT::i32:
2305 DivOpcode = isSigned ? X86::IDIV32r : X86::DIV32r;
Chris Lattner42928302005-01-12 03:16:09 +00002306 LoReg = X86::EAX;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002307 HiReg = X86::EDX;
2308 MovOpcode = X86::MOV32rr;
2309 ClrOpcode = X86::MOV32ri;
2310 SExtOpcode = X86::CDQ;
2311 break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002312 case MVT::f64:
Chris Lattnerda2ce112005-01-16 07:34:08 +00002313 BuildMI(BB, X86::FpDIV, 2, Result).addReg(Tmp1).addReg(Tmp2);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002314 return Result;
2315 }
2316
2317 // Set up the low part.
2318 BuildMI(BB, MovOpcode, 1, LoReg).addReg(Tmp1);
2319
2320 if (isSigned) {
2321 // Sign extend the low part into the high part.
2322 BuildMI(BB, SExtOpcode, 0);
2323 } else {
2324 // Zero out the high part, effectively zero extending the input.
2325 BuildMI(BB, ClrOpcode, 1, HiReg).addImm(0);
2326 }
2327
2328 // Emit the DIV/IDIV instruction.
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002329 BuildMI(BB, DivOpcode, 1).addReg(Tmp2);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002330
2331 // Get the result of the divide or rem.
2332 BuildMI(BB, MovOpcode, 1, Result).addReg(isDiv ? LoReg : HiReg);
2333 return Result;
2334 }
2335
2336 case ISD::SHL:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002337 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Chris Lattnera5ade062005-01-11 21:19:59 +00002338 if (CN->getValue() == 1) { // X = SHL Y, 1 -> X = ADD Y, Y
2339 switch (N.getValueType()) {
2340 default: assert(0 && "Cannot shift this type!");
2341 case MVT::i8: Opc = X86::ADD8rr; break;
2342 case MVT::i16: Opc = X86::ADD16rr; break;
2343 case MVT::i32: Opc = X86::ADD32rr; break;
2344 }
2345 Tmp1 = SelectExpr(N.getOperand(0));
2346 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp1);
2347 return Result;
2348 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002349
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002350 switch (N.getValueType()) {
2351 default: assert(0 && "Cannot shift this type!");
2352 case MVT::i8: Opc = X86::SHL8ri; break;
2353 case MVT::i16: Opc = X86::SHL16ri; break;
2354 case MVT::i32: Opc = X86::SHL32ri; break;
2355 }
Chris Lattner11333092005-01-11 03:11:44 +00002356 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002357 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
2358 return Result;
2359 }
Chris Lattner11333092005-01-11 03:11:44 +00002360
2361 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2362 Tmp1 = SelectExpr(N.getOperand(0));
2363 Tmp2 = SelectExpr(N.getOperand(1));
2364 } else {
2365 Tmp2 = SelectExpr(N.getOperand(1));
2366 Tmp1 = SelectExpr(N.getOperand(0));
2367 }
2368
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002369 switch (N.getValueType()) {
2370 default: assert(0 && "Cannot shift this type!");
2371 case MVT::i8 : Opc = X86::SHL8rCL; break;
2372 case MVT::i16: Opc = X86::SHL16rCL; break;
2373 case MVT::i32: Opc = X86::SHL32rCL; break;
2374 }
2375 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
2376 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
2377 return Result;
2378 case ISD::SRL:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002379 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
2380 switch (N.getValueType()) {
2381 default: assert(0 && "Cannot shift this type!");
2382 case MVT::i8: Opc = X86::SHR8ri; break;
2383 case MVT::i16: Opc = X86::SHR16ri; break;
2384 case MVT::i32: Opc = X86::SHR32ri; break;
2385 }
Chris Lattner11333092005-01-11 03:11:44 +00002386 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002387 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
2388 return Result;
2389 }
Chris Lattner11333092005-01-11 03:11:44 +00002390
2391 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2392 Tmp1 = SelectExpr(N.getOperand(0));
2393 Tmp2 = SelectExpr(N.getOperand(1));
2394 } else {
2395 Tmp2 = SelectExpr(N.getOperand(1));
2396 Tmp1 = SelectExpr(N.getOperand(0));
2397 }
2398
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002399 switch (N.getValueType()) {
2400 default: assert(0 && "Cannot shift this type!");
2401 case MVT::i8 : Opc = X86::SHR8rCL; break;
2402 case MVT::i16: Opc = X86::SHR16rCL; break;
2403 case MVT::i32: Opc = X86::SHR32rCL; break;
2404 }
2405 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
2406 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
2407 return Result;
2408 case ISD::SRA:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002409 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
2410 switch (N.getValueType()) {
2411 default: assert(0 && "Cannot shift this type!");
2412 case MVT::i8: Opc = X86::SAR8ri; break;
2413 case MVT::i16: Opc = X86::SAR16ri; break;
2414 case MVT::i32: Opc = X86::SAR32ri; break;
2415 }
Chris Lattner11333092005-01-11 03:11:44 +00002416 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002417 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
2418 return Result;
2419 }
Chris Lattner11333092005-01-11 03:11:44 +00002420
2421 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2422 Tmp1 = SelectExpr(N.getOperand(0));
2423 Tmp2 = SelectExpr(N.getOperand(1));
2424 } else {
2425 Tmp2 = SelectExpr(N.getOperand(1));
2426 Tmp1 = SelectExpr(N.getOperand(0));
2427 }
2428
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002429 switch (N.getValueType()) {
2430 default: assert(0 && "Cannot shift this type!");
2431 case MVT::i8 : Opc = X86::SAR8rCL; break;
2432 case MVT::i16: Opc = X86::SAR16rCL; break;
2433 case MVT::i32: Opc = X86::SAR32rCL; break;
2434 }
2435 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
2436 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
2437 return Result;
2438
2439 case ISD::SETCC:
Chris Lattnercb1aa8d2005-01-17 01:34:14 +00002440 EmitCMP(N.getOperand(0), N.getOperand(1), Node->hasOneUse());
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002441 EmitSetCC(BB, Result, cast<SetCCSDNode>(N)->getCondition(),
2442 MVT::isFloatingPoint(N.getOperand(1).getValueType()));
2443 return Result;
Chris Lattnere9ef81d2005-01-15 05:22:24 +00002444 case ISD::LOAD:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002445 // Make sure we generate both values.
Chris Lattner4a108662005-01-18 03:51:59 +00002446 if (Result != 1) { // Generate the token
2447 if (!ExprMap.insert(std::make_pair(N.getValue(1), 1)).second)
2448 assert(0 && "Load already emitted!?");
2449 } else
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002450 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
2451
Chris Lattner5188ad72005-01-08 19:28:19 +00002452 switch (Node->getValueType(0)) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002453 default: assert(0 && "Cannot load this type!");
2454 case MVT::i1:
2455 case MVT::i8: Opc = X86::MOV8rm; break;
2456 case MVT::i16: Opc = X86::MOV16rm; break;
2457 case MVT::i32: Opc = X86::MOV32rm; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002458 case MVT::f64: Opc = X86::FLD64m; ContainsFPCode = true; break;
2459 }
Chris Lattner11333092005-01-11 03:11:44 +00002460
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002461 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N.getOperand(1))){
Chris Lattner11333092005-01-11 03:11:44 +00002462 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002463 addConstantPoolReference(BuildMI(BB, Opc, 4, Result), CP->getIndex());
2464 } else {
2465 X86AddressMode AM;
Chris Lattner636e79a2005-01-13 05:53:16 +00002466
2467 SDOperand Chain = N.getOperand(0);
2468 SDOperand Address = N.getOperand(1);
2469 if (getRegPressure(Chain) > getRegPressure(Address)) {
2470 Select(Chain);
2471 SelectAddress(Address, AM);
2472 } else {
2473 SelectAddress(Address, AM);
2474 Select(Chain);
2475 }
2476
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002477 addFullAddress(BuildMI(BB, Opc, 4, Result), AM);
2478 }
2479 return Result;
Chris Lattnere9ef81d2005-01-15 05:22:24 +00002480
2481 case ISD::EXTLOAD: // Arbitrarily codegen extloads as MOVZX*
2482 case ISD::ZEXTLOAD: {
2483 // Make sure we generate both values.
2484 if (Result != 1)
2485 ExprMap[N.getValue(1)] = 1; // Generate the token
2486 else
2487 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
2488
Chris Lattnerda2ce112005-01-16 07:34:08 +00002489 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N.getOperand(1)))
2490 if (Node->getValueType(0) == MVT::f64) {
2491 assert(cast<MVTSDNode>(Node)->getExtraValueType() == MVT::f32 &&
2492 "Bad EXTLOAD!");
2493 addConstantPoolReference(BuildMI(BB, X86::FLD32m, 4, Result),
2494 CP->getIndex());
2495 return Result;
2496 }
2497
Chris Lattnere9ef81d2005-01-15 05:22:24 +00002498 X86AddressMode AM;
2499 if (getRegPressure(Node->getOperand(0)) >
2500 getRegPressure(Node->getOperand(1))) {
2501 Select(Node->getOperand(0)); // chain
2502 SelectAddress(Node->getOperand(1), AM);
2503 } else {
2504 SelectAddress(Node->getOperand(1), AM);
2505 Select(Node->getOperand(0)); // chain
2506 }
2507
2508 switch (Node->getValueType(0)) {
2509 default: assert(0 && "Unknown type to sign extend to.");
2510 case MVT::f64:
2511 assert(cast<MVTSDNode>(Node)->getExtraValueType() == MVT::f32 &&
2512 "Bad EXTLOAD!");
2513 addFullAddress(BuildMI(BB, X86::FLD32m, 5, Result), AM);
2514 break;
2515 case MVT::i32:
2516 switch (cast<MVTSDNode>(Node)->getExtraValueType()) {
2517 default:
2518 assert(0 && "Bad zero extend!");
2519 case MVT::i1:
2520 case MVT::i8:
2521 addFullAddress(BuildMI(BB, X86::MOVZX32rm8, 5, Result), AM);
2522 break;
2523 case MVT::i16:
2524 addFullAddress(BuildMI(BB, X86::MOVZX32rm16, 5, Result), AM);
2525 break;
2526 }
2527 break;
2528 case MVT::i16:
2529 assert(cast<MVTSDNode>(Node)->getExtraValueType() <= MVT::i8 &&
2530 "Bad zero extend!");
2531 addFullAddress(BuildMI(BB, X86::MOVSX16rm8, 5, Result), AM);
2532 break;
2533 case MVT::i8:
2534 assert(cast<MVTSDNode>(Node)->getExtraValueType() == MVT::i1 &&
2535 "Bad zero extend!");
2536 addFullAddress(BuildMI(BB, X86::MOV8rm, 5, Result), AM);
2537 break;
2538 }
2539 return Result;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002540 }
Chris Lattnere9ef81d2005-01-15 05:22:24 +00002541 case ISD::SEXTLOAD: {
2542 // Make sure we generate both values.
2543 if (Result != 1)
2544 ExprMap[N.getValue(1)] = 1; // Generate the token
2545 else
2546 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
2547
2548 X86AddressMode AM;
2549 if (getRegPressure(Node->getOperand(0)) >
2550 getRegPressure(Node->getOperand(1))) {
2551 Select(Node->getOperand(0)); // chain
2552 SelectAddress(Node->getOperand(1), AM);
2553 } else {
2554 SelectAddress(Node->getOperand(1), AM);
2555 Select(Node->getOperand(0)); // chain
2556 }
2557
2558 switch (Node->getValueType(0)) {
2559 case MVT::i8: assert(0 && "Cannot sign extend from bool!");
2560 default: assert(0 && "Unknown type to sign extend to.");
2561 case MVT::i32:
2562 switch (cast<MVTSDNode>(Node)->getExtraValueType()) {
2563 default:
2564 case MVT::i1: assert(0 && "Cannot sign extend from bool!");
2565 case MVT::i8:
2566 addFullAddress(BuildMI(BB, X86::MOVSX32rm8, 5, Result), AM);
2567 break;
2568 case MVT::i16:
2569 addFullAddress(BuildMI(BB, X86::MOVSX32rm16, 5, Result), AM);
2570 break;
2571 }
2572 break;
2573 case MVT::i16:
2574 assert(cast<MVTSDNode>(Node)->getExtraValueType() == MVT::i8 &&
2575 "Cannot sign extend from bool!");
2576 addFullAddress(BuildMI(BB, X86::MOVSX16rm8, 5, Result), AM);
2577 break;
2578 }
2579 return Result;
2580 }
2581
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002582 case ISD::DYNAMIC_STACKALLOC:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002583 // Generate both result values.
2584 if (Result != 1)
2585 ExprMap[N.getValue(1)] = 1; // Generate the token
2586 else
2587 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
2588
2589 // FIXME: We are currently ignoring the requested alignment for handling
2590 // greater than the stack alignment. This will need to be revisited at some
2591 // point. Align = N.getOperand(2);
2592
2593 if (!isa<ConstantSDNode>(N.getOperand(2)) ||
2594 cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) {
2595 std::cerr << "Cannot allocate stack object with greater alignment than"
2596 << " the stack alignment yet!";
2597 abort();
2598 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002599
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002600 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Chris Lattner11333092005-01-11 03:11:44 +00002601 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002602 BuildMI(BB, X86::SUB32ri, 2, X86::ESP).addReg(X86::ESP)
2603 .addImm(CN->getValue());
2604 } else {
Chris Lattner11333092005-01-11 03:11:44 +00002605 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2606 Select(N.getOperand(0));
2607 Tmp1 = SelectExpr(N.getOperand(1));
2608 } else {
2609 Tmp1 = SelectExpr(N.getOperand(1));
2610 Select(N.getOperand(0));
2611 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002612
2613 // Subtract size from stack pointer, thereby allocating some space.
2614 BuildMI(BB, X86::SUB32rr, 2, X86::ESP).addReg(X86::ESP).addReg(Tmp1);
2615 }
2616
2617 // Put a pointer to the space into the result register, by copying the stack
2618 // pointer.
2619 BuildMI(BB, X86::MOV32rr, 1, Result).addReg(X86::ESP);
2620 return Result;
2621
2622 case ISD::CALL:
Chris Lattner5188ad72005-01-08 19:28:19 +00002623 // The chain for this call is now lowered.
Chris Lattner4a108662005-01-18 03:51:59 +00002624 ExprMap.insert(std::make_pair(N.getValue(Node->getNumValues()-1), 1));
Chris Lattner5188ad72005-01-08 19:28:19 +00002625
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002626 if (GlobalAddressSDNode *GASD =
2627 dyn_cast<GlobalAddressSDNode>(N.getOperand(1))) {
Chris Lattner11333092005-01-11 03:11:44 +00002628 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002629 BuildMI(BB, X86::CALLpcrel32, 1).addGlobalAddress(GASD->getGlobal(),true);
2630 } else if (ExternalSymbolSDNode *ESSDN =
2631 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1))) {
Chris Lattner11333092005-01-11 03:11:44 +00002632 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002633 BuildMI(BB, X86::CALLpcrel32,
2634 1).addExternalSymbol(ESSDN->getSymbol(), true);
2635 } else {
Chris Lattner11333092005-01-11 03:11:44 +00002636 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2637 Select(N.getOperand(0));
2638 Tmp1 = SelectExpr(N.getOperand(1));
2639 } else {
2640 Tmp1 = SelectExpr(N.getOperand(1));
2641 Select(N.getOperand(0));
2642 }
2643
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002644 BuildMI(BB, X86::CALL32r, 1).addReg(Tmp1);
2645 }
Chris Lattner5188ad72005-01-08 19:28:19 +00002646 switch (Node->getValueType(0)) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002647 default: assert(0 && "Unknown value type for call result!");
2648 case MVT::Other: return 1;
2649 case MVT::i1:
2650 case MVT::i8:
2651 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
2652 break;
2653 case MVT::i16:
2654 BuildMI(BB, X86::MOV16rr, 1, Result).addReg(X86::AX);
2655 break;
2656 case MVT::i32:
2657 BuildMI(BB, X86::MOV32rr, 1, Result).addReg(X86::EAX);
Chris Lattner5188ad72005-01-08 19:28:19 +00002658 if (Node->getValueType(1) == MVT::i32)
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002659 BuildMI(BB, X86::MOV32rr, 1, Result+1).addReg(X86::EDX);
2660 break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002661 case MVT::f64: // Floating-point return values live in %ST(0)
2662 ContainsFPCode = true;
2663 BuildMI(BB, X86::FpGETRESULT, 1, Result);
2664 break;
2665 }
2666 return Result+N.ResNo;
2667 }
2668
2669 return 0;
2670}
2671
Chris Lattnere10269b2005-01-17 19:25:26 +00002672/// TryToFoldLoadOpStore - Given a store node, try to fold together a
2673/// load/op/store instruction. If successful return true.
2674bool ISel::TryToFoldLoadOpStore(SDNode *Node) {
2675 assert(Node->getOpcode() == ISD::STORE && "Can only do this for stores!");
2676 SDOperand Chain = Node->getOperand(0);
2677 SDOperand StVal = Node->getOperand(1);
Chris Lattner5c659812005-01-17 22:10:42 +00002678 SDOperand StPtr = Node->getOperand(2);
Chris Lattnere10269b2005-01-17 19:25:26 +00002679
2680 // The chain has to be a load, the stored value must be an integer binary
2681 // operation with one use.
Chris Lattner5c659812005-01-17 22:10:42 +00002682 if (!StVal.Val->hasOneUse() || StVal.Val->getNumOperands() != 2 ||
Chris Lattnere10269b2005-01-17 19:25:26 +00002683 MVT::isFloatingPoint(StVal.getValueType()))
2684 return false;
2685
Chris Lattner5c659812005-01-17 22:10:42 +00002686 // Token chain must either be a factor node or the load to fold.
2687 if (Chain.getOpcode() != ISD::LOAD && Chain.getOpcode() != ISD::TokenFactor)
2688 return false;
Chris Lattnere10269b2005-01-17 19:25:26 +00002689
Chris Lattner5c659812005-01-17 22:10:42 +00002690 SDOperand TheLoad;
2691
2692 // Check to see if there is a load from the same pointer that we're storing
2693 // to in either operand of the binop.
2694 if (StVal.getOperand(0).getOpcode() == ISD::LOAD &&
2695 StVal.getOperand(0).getOperand(1) == StPtr)
2696 TheLoad = StVal.getOperand(0);
2697 else if (StVal.getOperand(1).getOpcode() == ISD::LOAD &&
2698 StVal.getOperand(1).getOperand(1) == StPtr)
2699 TheLoad = StVal.getOperand(1);
2700 else
2701 return false; // No matching load operand.
2702
2703 // We can only fold the load if there are no intervening side-effecting
2704 // operations. This means that the store uses the load as its token chain, or
2705 // there are only token factor nodes in between the store and load.
2706 if (Chain != TheLoad.getValue(1)) {
2707 // Okay, the other option is that we have a store referring to (possibly
2708 // nested) token factor nodes. For now, just try peeking through one level
2709 // of token factors to see if this is the case.
2710 bool ChainOk = false;
2711 if (Chain.getOpcode() == ISD::TokenFactor) {
2712 for (unsigned i = 0, e = Chain.getNumOperands(); i != e; ++i)
2713 if (Chain.getOperand(i) == TheLoad.getValue(1)) {
2714 ChainOk = true;
2715 break;
2716 }
2717 }
2718
2719 if (!ChainOk) return false;
2720 }
2721
2722 if (TheLoad.getOperand(1) != StPtr)
Chris Lattnere10269b2005-01-17 19:25:26 +00002723 return false;
2724
2725 // Make sure that one of the operands of the binop is the load, and that the
2726 // load folds into the binop.
2727 if (((StVal.getOperand(0) != TheLoad ||
2728 !isFoldableLoad(TheLoad, StVal.getOperand(1))) &&
2729 (StVal.getOperand(1) != TheLoad ||
2730 !isFoldableLoad(TheLoad, StVal.getOperand(0)))))
2731 return false;
2732
2733 // Finally, check to see if this is one of the ops we can handle!
2734 static const unsigned ADDTAB[] = {
2735 X86::ADD8mi, X86::ADD16mi, X86::ADD32mi,
2736 X86::ADD8mr, X86::ADD16mr, X86::ADD32mr,
2737 };
2738 static const unsigned SUBTAB[] = {
2739 X86::SUB8mi, X86::SUB16mi, X86::SUB32mi,
2740 X86::SUB8mr, X86::SUB16mr, X86::SUB32mr,
2741 };
2742 static const unsigned ANDTAB[] = {
2743 X86::AND8mi, X86::AND16mi, X86::AND32mi,
2744 X86::AND8mr, X86::AND16mr, X86::AND32mr,
2745 };
2746 static const unsigned ORTAB[] = {
2747 X86::OR8mi, X86::OR16mi, X86::OR32mi,
2748 X86::OR8mr, X86::OR16mr, X86::OR32mr,
2749 };
2750 static const unsigned XORTAB[] = {
2751 X86::XOR8mi, X86::XOR16mi, X86::XOR32mi,
2752 X86::XOR8mr, X86::XOR16mr, X86::XOR32mr,
2753 };
2754 static const unsigned SHLTAB[] = {
2755 X86::SHL8mi, X86::SHL16mi, X86::SHL32mi,
2756 /*Have to put the reg in CL*/0, 0, 0,
2757 };
2758 static const unsigned SARTAB[] = {
2759 X86::SAR8mi, X86::SAR16mi, X86::SAR32mi,
2760 /*Have to put the reg in CL*/0, 0, 0,
2761 };
2762 static const unsigned SHRTAB[] = {
2763 X86::SHR8mi, X86::SHR16mi, X86::SHR32mi,
2764 /*Have to put the reg in CL*/0, 0, 0,
2765 };
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002766
Chris Lattnere10269b2005-01-17 19:25:26 +00002767 const unsigned *TabPtr = 0;
2768 switch (StVal.getOpcode()) {
2769 default:
2770 std::cerr << "CANNOT [mem] op= val: ";
2771 StVal.Val->dump(); std::cerr << "\n";
2772 case ISD::MUL:
2773 case ISD::SDIV:
2774 case ISD::UDIV:
2775 case ISD::SREM:
2776 case ISD::UREM: return false;
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002777
Chris Lattnere10269b2005-01-17 19:25:26 +00002778 case ISD::ADD: TabPtr = ADDTAB; break;
2779 case ISD::SUB: TabPtr = SUBTAB; break;
2780 case ISD::AND: TabPtr = ANDTAB; break;
2781 case ISD:: OR: TabPtr = ORTAB; break;
2782 case ISD::XOR: TabPtr = XORTAB; break;
2783 case ISD::SHL: TabPtr = SHLTAB; break;
2784 case ISD::SRA: TabPtr = SARTAB; break;
2785 case ISD::SRL: TabPtr = SHRTAB; break;
2786 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002787
Chris Lattnere10269b2005-01-17 19:25:26 +00002788 // Handle: [mem] op= CST
2789 SDOperand Op0 = StVal.getOperand(0);
2790 SDOperand Op1 = StVal.getOperand(1);
Chris Lattner0a078832005-01-23 23:20:06 +00002791 unsigned Opc = 0;
Chris Lattnere10269b2005-01-17 19:25:26 +00002792 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1)) {
2793 switch (Op0.getValueType()) { // Use Op0's type because of shifts.
2794 default: break;
2795 case MVT::i1:
2796 case MVT::i8: Opc = TabPtr[0]; break;
2797 case MVT::i16: Opc = TabPtr[1]; break;
2798 case MVT::i32: Opc = TabPtr[2]; break;
2799 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002800
Chris Lattnere10269b2005-01-17 19:25:26 +00002801 if (Opc) {
Chris Lattner4a108662005-01-18 03:51:59 +00002802 if (!ExprMap.insert(std::make_pair(TheLoad.getValue(1), 1)).second)
2803 assert(0 && "Already emitted?");
Chris Lattner5c659812005-01-17 22:10:42 +00002804 Select(Chain);
2805
Chris Lattnere10269b2005-01-17 19:25:26 +00002806 X86AddressMode AM;
2807 if (getRegPressure(TheLoad.getOperand(0)) >
2808 getRegPressure(TheLoad.getOperand(1))) {
2809 Select(TheLoad.getOperand(0));
2810 SelectAddress(TheLoad.getOperand(1), AM);
2811 } else {
2812 SelectAddress(TheLoad.getOperand(1), AM);
2813 Select(TheLoad.getOperand(0));
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002814 }
Chris Lattner5c659812005-01-17 22:10:42 +00002815
2816 if (StVal.getOpcode() == ISD::ADD) {
2817 if (CN->getValue() == 1) {
2818 switch (Op0.getValueType()) {
2819 default: break;
2820 case MVT::i8:
2821 addFullAddress(BuildMI(BB, X86::INC8m, 4), AM);
2822 return true;
2823 case MVT::i16: Opc = TabPtr[1];
2824 addFullAddress(BuildMI(BB, X86::INC16m, 4), AM);
2825 return true;
2826 case MVT::i32: Opc = TabPtr[2];
2827 addFullAddress(BuildMI(BB, X86::INC32m, 4), AM);
2828 return true;
2829 }
2830 } else if (CN->getValue()+1 == 0) { // [X] += -1 -> DEC [X]
2831 switch (Op0.getValueType()) {
2832 default: break;
2833 case MVT::i8:
2834 addFullAddress(BuildMI(BB, X86::DEC8m, 4), AM);
2835 return true;
2836 case MVT::i16: Opc = TabPtr[1];
2837 addFullAddress(BuildMI(BB, X86::DEC16m, 4), AM);
2838 return true;
2839 case MVT::i32: Opc = TabPtr[2];
2840 addFullAddress(BuildMI(BB, X86::DEC32m, 4), AM);
2841 return true;
2842 }
2843 }
2844 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002845
Chris Lattnere10269b2005-01-17 19:25:26 +00002846 addFullAddress(BuildMI(BB, Opc, 4+1),AM).addImm(CN->getValue());
2847 return true;
2848 }
2849 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002850
Chris Lattnere10269b2005-01-17 19:25:26 +00002851 // If we have [mem] = V op [mem], try to turn it into:
2852 // [mem] = [mem] op V.
2853 if (Op1 == TheLoad && StVal.getOpcode() != ISD::SUB &&
2854 StVal.getOpcode() != ISD::SHL && StVal.getOpcode() != ISD::SRA &&
2855 StVal.getOpcode() != ISD::SRL)
2856 std::swap(Op0, Op1);
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002857
Chris Lattnere10269b2005-01-17 19:25:26 +00002858 if (Op0 != TheLoad) return false;
2859
2860 switch (Op0.getValueType()) {
2861 default: return false;
2862 case MVT::i1:
2863 case MVT::i8: Opc = TabPtr[3]; break;
2864 case MVT::i16: Opc = TabPtr[4]; break;
2865 case MVT::i32: Opc = TabPtr[5]; break;
2866 }
Chris Lattner5c659812005-01-17 22:10:42 +00002867
Chris Lattnerb422aea2005-01-18 17:35:28 +00002868 // Table entry doesn't exist?
2869 if (Opc == 0) return false;
2870
Chris Lattner4a108662005-01-18 03:51:59 +00002871 if (!ExprMap.insert(std::make_pair(TheLoad.getValue(1), 1)).second)
2872 assert(0 && "Already emitted?");
Chris Lattner5c659812005-01-17 22:10:42 +00002873 Select(Chain);
Chris Lattnere10269b2005-01-17 19:25:26 +00002874 Select(TheLoad.getOperand(0));
Chris Lattner98a8ba02005-01-18 01:06:26 +00002875
Chris Lattnere10269b2005-01-17 19:25:26 +00002876 X86AddressMode AM;
2877 SelectAddress(TheLoad.getOperand(1), AM);
2878 unsigned Reg = SelectExpr(Op1);
Chris Lattner98a8ba02005-01-18 01:06:26 +00002879 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addReg(Reg);
Chris Lattnere10269b2005-01-17 19:25:26 +00002880 return true;
2881}
2882
2883
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002884void ISel::Select(SDOperand N) {
2885 unsigned Tmp1, Tmp2, Opc;
2886
Nate Begeman85fdeb22005-03-24 04:39:54 +00002887 if (!ExprMap.insert(std::make_pair(N, 1)).second)
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002888 return; // Already selected.
2889
Chris Lattner989de032005-01-11 06:14:36 +00002890 SDNode *Node = N.Val;
2891
2892 switch (Node->getOpcode()) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002893 default:
Chris Lattner989de032005-01-11 06:14:36 +00002894 Node->dump(); std::cerr << "\n";
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002895 assert(0 && "Node not handled yet!");
2896 case ISD::EntryToken: return; // Noop
Chris Lattnerc3580712005-01-13 18:01:36 +00002897 case ISD::TokenFactor:
Chris Lattner1d50b7f2005-01-13 19:56:00 +00002898 if (Node->getNumOperands() == 2) {
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002899 bool OneFirst =
Chris Lattner1d50b7f2005-01-13 19:56:00 +00002900 getRegPressure(Node->getOperand(1))>getRegPressure(Node->getOperand(0));
2901 Select(Node->getOperand(OneFirst));
2902 Select(Node->getOperand(!OneFirst));
2903 } else {
2904 std::vector<std::pair<unsigned, unsigned> > OpsP;
2905 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
2906 OpsP.push_back(std::make_pair(getRegPressure(Node->getOperand(i)), i));
2907 std::sort(OpsP.begin(), OpsP.end());
2908 std::reverse(OpsP.begin(), OpsP.end());
2909 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
2910 Select(Node->getOperand(OpsP[i].second));
2911 }
Chris Lattnerc3580712005-01-13 18:01:36 +00002912 return;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002913 case ISD::CopyToReg:
Chris Lattneref6806c2005-01-12 02:02:48 +00002914 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2915 Select(N.getOperand(0));
2916 Tmp1 = SelectExpr(N.getOperand(1));
2917 } else {
2918 Tmp1 = SelectExpr(N.getOperand(1));
2919 Select(N.getOperand(0));
2920 }
Chris Lattner18c2f132005-01-13 20:50:02 +00002921 Tmp2 = cast<RegSDNode>(N)->getReg();
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002922
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002923 if (Tmp1 != Tmp2) {
2924 switch (N.getOperand(1).getValueType()) {
2925 default: assert(0 && "Invalid type for operation!");
2926 case MVT::i1:
2927 case MVT::i8: Opc = X86::MOV8rr; break;
2928 case MVT::i16: Opc = X86::MOV16rr; break;
2929 case MVT::i32: Opc = X86::MOV32rr; break;
Chris Lattneref7ba072005-01-11 03:50:45 +00002930 case MVT::f64: Opc = X86::FpMOV; ContainsFPCode = true; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002931 }
2932 BuildMI(BB, Opc, 1, Tmp2).addReg(Tmp1);
2933 }
2934 return;
2935 case ISD::RET:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002936 switch (N.getNumOperands()) {
2937 default:
2938 assert(0 && "Unknown return instruction!");
2939 case 3:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002940 assert(N.getOperand(1).getValueType() == MVT::i32 &&
2941 N.getOperand(2).getValueType() == MVT::i32 &&
2942 "Unknown two-register value!");
Chris Lattner11333092005-01-11 03:11:44 +00002943 if (getRegPressure(N.getOperand(1)) > getRegPressure(N.getOperand(2))) {
2944 Tmp1 = SelectExpr(N.getOperand(1));
2945 Tmp2 = SelectExpr(N.getOperand(2));
2946 } else {
2947 Tmp2 = SelectExpr(N.getOperand(2));
2948 Tmp1 = SelectExpr(N.getOperand(1));
2949 }
2950 Select(N.getOperand(0));
2951
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002952 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
2953 BuildMI(BB, X86::MOV32rr, 1, X86::EDX).addReg(Tmp2);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002954 break;
2955 case 2:
Chris Lattner11333092005-01-11 03:11:44 +00002956 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2957 Select(N.getOperand(0));
2958 Tmp1 = SelectExpr(N.getOperand(1));
2959 } else {
2960 Tmp1 = SelectExpr(N.getOperand(1));
2961 Select(N.getOperand(0));
2962 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002963 switch (N.getOperand(1).getValueType()) {
2964 default: assert(0 && "All other types should have been promoted!!");
2965 case MVT::f64:
2966 BuildMI(BB, X86::FpSETRESULT, 1).addReg(Tmp1);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002967 break;
2968 case MVT::i32:
2969 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002970 break;
2971 }
2972 break;
2973 case 1:
Chris Lattner11333092005-01-11 03:11:44 +00002974 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002975 break;
2976 }
2977 BuildMI(BB, X86::RET, 0); // Just emit a 'ret' instruction
2978 return;
2979 case ISD::BR: {
2980 Select(N.getOperand(0));
2981 MachineBasicBlock *Dest =
2982 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
2983 BuildMI(BB, X86::JMP, 1).addMBB(Dest);
2984 return;
2985 }
2986
2987 case ISD::BRCOND: {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002988 MachineBasicBlock *Dest =
2989 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
Chris Lattner11333092005-01-11 03:11:44 +00002990
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002991 // Try to fold a setcc into the branch. If this fails, emit a test/jne
2992 // pair.
Chris Lattner6c07aee2005-01-11 04:06:27 +00002993 if (EmitBranchCC(Dest, N.getOperand(0), N.getOperand(1))) {
2994 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2995 Select(N.getOperand(0));
2996 Tmp1 = SelectExpr(N.getOperand(1));
2997 } else {
2998 Tmp1 = SelectExpr(N.getOperand(1));
2999 Select(N.getOperand(0));
3000 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003001 BuildMI(BB, X86::TEST8rr, 2).addReg(Tmp1).addReg(Tmp1);
3002 BuildMI(BB, X86::JNE, 1).addMBB(Dest);
3003 }
Chris Lattner11333092005-01-11 03:11:44 +00003004
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003005 return;
3006 }
Chris Lattnere9ef81d2005-01-15 05:22:24 +00003007
Chris Lattner4df0de92005-01-17 00:00:33 +00003008 case ISD::LOAD:
3009 // If this load could be folded into the only using instruction, and if it
3010 // is safe to emit the instruction here, try to do so now.
3011 if (Node->hasNUsesOfValue(1, 0)) {
3012 SDOperand TheVal = N.getValue(0);
3013 SDNode *User = 0;
3014 for (SDNode::use_iterator UI = Node->use_begin(); ; ++UI) {
3015 assert(UI != Node->use_end() && "Didn't find use!");
3016 SDNode *UN = *UI;
3017 for (unsigned i = 0, e = UN->getNumOperands(); i != e; ++i)
3018 if (UN->getOperand(i) == TheVal) {
3019 User = UN;
3020 goto FoundIt;
3021 }
3022 }
3023 FoundIt:
3024 // Only handle unary operators right now.
3025 if (User->getNumOperands() == 1) {
Chris Lattner4a108662005-01-18 03:51:59 +00003026 ExprMap.erase(N);
Chris Lattner4df0de92005-01-17 00:00:33 +00003027 SelectExpr(SDOperand(User, 0));
3028 return;
3029 }
3030 }
Chris Lattnerb71f8fc2005-01-18 04:00:54 +00003031 ExprMap.erase(N);
Chris Lattner4df0de92005-01-17 00:00:33 +00003032 SelectExpr(N);
3033 return;
3034
Chris Lattnere9ef81d2005-01-15 05:22:24 +00003035 case ISD::EXTLOAD:
3036 case ISD::SEXTLOAD:
3037 case ISD::ZEXTLOAD:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003038 case ISD::CALL:
3039 case ISD::DYNAMIC_STACKALLOC:
Chris Lattnerb71f8fc2005-01-18 04:00:54 +00003040 ExprMap.erase(N);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003041 SelectExpr(N);
3042 return;
Chris Lattnere9ef81d2005-01-15 05:22:24 +00003043
3044 case ISD::TRUNCSTORE: { // truncstore chain, val, ptr :storety
3045 // On X86, we can represent all types except for Bool and Float natively.
3046 X86AddressMode AM;
3047 MVT::ValueType StoredTy = cast<MVTSDNode>(Node)->getExtraValueType();
Chris Lattnerda2ce112005-01-16 07:34:08 +00003048 assert((StoredTy == MVT::i1 || StoredTy == MVT::f32 ||
3049 StoredTy == MVT::i16 /*FIXME: THIS IS JUST FOR TESTING!*/)
3050 && "Unsupported TRUNCSTORE for this target!");
3051
3052 if (StoredTy == MVT::i16) {
3053 // FIXME: This is here just to allow testing. X86 doesn't really have a
3054 // TRUNCSTORE i16 operation, but this is required for targets that do not
3055 // have 16-bit integer registers. We occasionally disable 16-bit integer
3056 // registers to test the promotion code.
3057 Select(N.getOperand(0));
3058 Tmp1 = SelectExpr(N.getOperand(1));
3059 SelectAddress(N.getOperand(2), AM);
3060
3061 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
3062 addFullAddress(BuildMI(BB, X86::MOV16mr, 5), AM).addReg(X86::AX);
3063 return;
3064 }
Chris Lattnere9ef81d2005-01-15 05:22:24 +00003065
3066 // Store of constant bool?
3067 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
3068 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(2))) {
3069 Select(N.getOperand(0));
3070 SelectAddress(N.getOperand(2), AM);
3071 } else {
3072 SelectAddress(N.getOperand(2), AM);
3073 Select(N.getOperand(0));
3074 }
3075 addFullAddress(BuildMI(BB, X86::MOV8mi, 5), AM).addImm(CN->getValue());
3076 return;
3077 }
3078
3079 switch (StoredTy) {
3080 default: assert(0 && "Cannot truncstore this type!");
3081 case MVT::i1: Opc = X86::MOV8mr; break;
3082 case MVT::f32: Opc = X86::FST32m; break;
3083 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003084
Chris Lattnere9ef81d2005-01-15 05:22:24 +00003085 std::vector<std::pair<unsigned, unsigned> > RP;
3086 RP.push_back(std::make_pair(getRegPressure(N.getOperand(0)), 0));
3087 RP.push_back(std::make_pair(getRegPressure(N.getOperand(1)), 1));
3088 RP.push_back(std::make_pair(getRegPressure(N.getOperand(2)), 2));
3089 std::sort(RP.begin(), RP.end());
3090
Chris Lattner572dd082005-02-23 05:57:21 +00003091 Tmp1 = 0; // Silence a warning.
Chris Lattnere9ef81d2005-01-15 05:22:24 +00003092 for (unsigned i = 0; i != 3; ++i)
3093 switch (RP[2-i].second) {
3094 default: assert(0 && "Unknown operand number!");
3095 case 0: Select(N.getOperand(0)); break;
3096 case 1: Tmp1 = SelectExpr(N.getOperand(1)); break;
3097 case 2: SelectAddress(N.getOperand(2), AM); break;
3098 }
3099
3100 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addReg(Tmp1);
3101 return;
3102 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003103 case ISD::STORE: {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003104 X86AddressMode AM;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003105
3106 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
3107 Opc = 0;
3108 switch (CN->getValueType(0)) {
3109 default: assert(0 && "Invalid type for operation!");
3110 case MVT::i1:
3111 case MVT::i8: Opc = X86::MOV8mi; break;
3112 case MVT::i16: Opc = X86::MOV16mi; break;
3113 case MVT::i32: Opc = X86::MOV32mi; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003114 case MVT::f64: break;
3115 }
3116 if (Opc) {
Chris Lattner11333092005-01-11 03:11:44 +00003117 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(2))) {
3118 Select(N.getOperand(0));
3119 SelectAddress(N.getOperand(2), AM);
3120 } else {
3121 SelectAddress(N.getOperand(2), AM);
3122 Select(N.getOperand(0));
3123 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003124 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addImm(CN->getValue());
3125 return;
3126 }
Chris Lattner75f354b2005-04-21 19:03:24 +00003127 } else if (GlobalAddressSDNode *GA =
3128 dyn_cast<GlobalAddressSDNode>(N.getOperand(1))) {
3129 assert(GA->getValueType(0) == MVT::i32 && "Bad pointer operand");
3130
3131 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(2))) {
3132 Select(N.getOperand(0));
3133 SelectAddress(N.getOperand(2), AM);
3134 } else {
3135 SelectAddress(N.getOperand(2), AM);
3136 Select(N.getOperand(0));
3137 }
3138 addFullAddress(BuildMI(BB, X86::MOV32mi, 4+1),
3139 AM).addGlobalAddress(GA->getGlobal());
3140 return;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003141 }
Chris Lattner837caa72005-01-11 23:21:30 +00003142
3143 // Check to see if this is a load/op/store combination.
Chris Lattnere10269b2005-01-17 19:25:26 +00003144 if (TryToFoldLoadOpStore(Node))
3145 return;
Chris Lattner837caa72005-01-11 23:21:30 +00003146
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003147 switch (N.getOperand(1).getValueType()) {
3148 default: assert(0 && "Cannot store this type!");
3149 case MVT::i1:
3150 case MVT::i8: Opc = X86::MOV8mr; break;
3151 case MVT::i16: Opc = X86::MOV16mr; break;
3152 case MVT::i32: Opc = X86::MOV32mr; break;
Chris Lattneref7ba072005-01-11 03:50:45 +00003153 case MVT::f64: Opc = X86::FST64m; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003154 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003155
Chris Lattner11333092005-01-11 03:11:44 +00003156 std::vector<std::pair<unsigned, unsigned> > RP;
3157 RP.push_back(std::make_pair(getRegPressure(N.getOperand(0)), 0));
3158 RP.push_back(std::make_pair(getRegPressure(N.getOperand(1)), 1));
3159 RP.push_back(std::make_pair(getRegPressure(N.getOperand(2)), 2));
3160 std::sort(RP.begin(), RP.end());
3161
Chris Lattner572dd082005-02-23 05:57:21 +00003162 Tmp1 = 0; // Silence a warning.
Chris Lattner11333092005-01-11 03:11:44 +00003163 for (unsigned i = 0; i != 3; ++i)
3164 switch (RP[2-i].second) {
3165 default: assert(0 && "Unknown operand number!");
3166 case 0: Select(N.getOperand(0)); break;
3167 case 1: Tmp1 = SelectExpr(N.getOperand(1)); break;
Chris Lattnera3aa2e22005-01-11 03:37:59 +00003168 case 2: SelectAddress(N.getOperand(2), AM); break;
Chris Lattner11333092005-01-11 03:11:44 +00003169 }
3170
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003171 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addReg(Tmp1);
3172 return;
3173 }
3174 case ISD::ADJCALLSTACKDOWN:
3175 case ISD::ADJCALLSTACKUP:
3176 Select(N.getOperand(0));
3177 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003178
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003179 Opc = N.getOpcode() == ISD::ADJCALLSTACKDOWN ? X86::ADJCALLSTACKDOWN :
3180 X86::ADJCALLSTACKUP;
3181 BuildMI(BB, Opc, 1).addImm(Tmp1);
3182 return;
Chris Lattner989de032005-01-11 06:14:36 +00003183 case ISD::MEMSET: {
3184 Select(N.getOperand(0)); // Select the chain.
3185 unsigned Align =
3186 (unsigned)cast<ConstantSDNode>(Node->getOperand(4))->getValue();
3187 if (Align == 0) Align = 1;
3188
3189 // Turn the byte code into # iterations
3190 unsigned CountReg;
3191 unsigned Opcode;
3192 if (ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Node->getOperand(2))) {
3193 unsigned Val = ValC->getValue() & 255;
3194
3195 // If the value is a constant, then we can potentially use larger sets.
3196 switch (Align & 3) {
3197 case 2: // WORD aligned
3198 CountReg = MakeReg(MVT::i32);
3199 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
3200 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/2);
3201 } else {
3202 unsigned ByteReg = SelectExpr(Node->getOperand(3));
3203 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(1);
3204 }
3205 BuildMI(BB, X86::MOV16ri, 1, X86::AX).addImm((Val << 8) | Val);
3206 Opcode = X86::REP_STOSW;
3207 break;
3208 case 0: // DWORD aligned
3209 CountReg = MakeReg(MVT::i32);
3210 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
3211 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/4);
3212 } else {
3213 unsigned ByteReg = SelectExpr(Node->getOperand(3));
3214 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(2);
3215 }
3216 Val = (Val << 8) | Val;
3217 BuildMI(BB, X86::MOV32ri, 1, X86::EAX).addImm((Val << 16) | Val);
3218 Opcode = X86::REP_STOSD;
3219 break;
3220 default: // BYTE aligned
3221 CountReg = SelectExpr(Node->getOperand(3));
3222 BuildMI(BB, X86::MOV8ri, 1, X86::AL).addImm(Val);
3223 Opcode = X86::REP_STOSB;
3224 break;
3225 }
3226 } else {
3227 // If it's not a constant value we are storing, just fall back. We could
3228 // try to be clever to form 16 bit and 32 bit values, but we don't yet.
3229 unsigned ValReg = SelectExpr(Node->getOperand(2));
3230 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(ValReg);
3231 CountReg = SelectExpr(Node->getOperand(3));
3232 Opcode = X86::REP_STOSB;
3233 }
3234
3235 // No matter what the alignment is, we put the source in ESI, the
3236 // destination in EDI, and the count in ECX.
3237 unsigned TmpReg1 = SelectExpr(Node->getOperand(1));
3238 BuildMI(BB, X86::MOV32rr, 1, X86::ECX).addReg(CountReg);
3239 BuildMI(BB, X86::MOV32rr, 1, X86::EDI).addReg(TmpReg1);
3240 BuildMI(BB, Opcode, 0);
3241 return;
3242 }
Chris Lattner31805bf2005-01-11 06:19:26 +00003243 case ISD::MEMCPY:
3244 Select(N.getOperand(0)); // Select the chain.
3245 unsigned Align =
3246 (unsigned)cast<ConstantSDNode>(Node->getOperand(4))->getValue();
3247 if (Align == 0) Align = 1;
3248
3249 // Turn the byte code into # iterations
3250 unsigned CountReg;
3251 unsigned Opcode;
3252 switch (Align & 3) {
3253 case 2: // WORD aligned
3254 CountReg = MakeReg(MVT::i32);
3255 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
3256 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/2);
3257 } else {
3258 unsigned ByteReg = SelectExpr(Node->getOperand(3));
3259 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(1);
3260 }
3261 Opcode = X86::REP_MOVSW;
3262 break;
3263 case 0: // DWORD aligned
3264 CountReg = MakeReg(MVT::i32);
3265 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
3266 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/4);
3267 } else {
3268 unsigned ByteReg = SelectExpr(Node->getOperand(3));
3269 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(2);
3270 }
3271 Opcode = X86::REP_MOVSD;
3272 break;
3273 default: // BYTE aligned
3274 CountReg = SelectExpr(Node->getOperand(3));
3275 Opcode = X86::REP_MOVSB;
3276 break;
3277 }
3278
3279 // No matter what the alignment is, we put the source in ESI, the
3280 // destination in EDI, and the count in ECX.
3281 unsigned TmpReg1 = SelectExpr(Node->getOperand(1));
3282 unsigned TmpReg2 = SelectExpr(Node->getOperand(2));
3283 BuildMI(BB, X86::MOV32rr, 1, X86::ECX).addReg(CountReg);
3284 BuildMI(BB, X86::MOV32rr, 1, X86::EDI).addReg(TmpReg1);
3285 BuildMI(BB, X86::MOV32rr, 1, X86::ESI).addReg(TmpReg2);
3286 BuildMI(BB, Opcode, 0);
3287 return;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003288 }
3289 assert(0 && "Should not be reached!");
3290}
3291
3292
3293/// createX86PatternInstructionSelector - This pass converts an LLVM function
3294/// into a machine code representation using pattern matching and a machine
3295/// description file.
3296///
3297FunctionPass *llvm::createX86PatternInstructionSelector(TargetMachine &TM) {
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003298 return new ISel(TM);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003299}