blob: f244d3ad39ab98d21c65e9039dd2ca739d21d343 [file] [log] [blame]
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001//===-- X86ISelPattern.cpp - A pattern matching inst selector for X86 -----===//
Chris Lattner24aad1b2005-01-10 22:10:13 +00002//
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
Misha Brukman0e0a7a452005-04-21 23:38:14 +00007//
Chris Lattner8acb1ba2005-01-07 07:49:41 +00008//===----------------------------------------------------------------------===//
9//
10// This file defines a pattern matching instruction selector for X86.
11//
12//===----------------------------------------------------------------------===//
13
14#include "X86.h"
15#include "X86InstrBuilder.h"
16#include "X86RegisterInfo.h"
Chris Lattnere3e0f272005-05-09 03:36:39 +000017#include "llvm/Constants.h"
18#include "llvm/Instructions.h"
Chris Lattner8acb1ba2005-01-07 07:49:41 +000019#include "llvm/Function.h"
Chris Lattnere3e0f272005-05-09 03:36:39 +000020#include "llvm/CodeGen/MachineConstantPool.h"
Chris Lattner8acb1ba2005-01-07 07:49:41 +000021#include "llvm/CodeGen/MachineFunction.h"
22#include "llvm/CodeGen/MachineFrameInfo.h"
23#include "llvm/CodeGen/SelectionDAG.h"
24#include "llvm/CodeGen/SelectionDAGISel.h"
25#include "llvm/CodeGen/SSARegMap.h"
26#include "llvm/Target/TargetData.h"
27#include "llvm/Target/TargetLowering.h"
Chris Lattnerc5dcb532005-04-30 04:25:35 +000028#include "llvm/Target/TargetOptions.h"
Chris Lattnere3e0f272005-05-09 03:36:39 +000029#include "llvm/Support/CFG.h"
Chris Lattner8acb1ba2005-01-07 07:49:41 +000030#include "llvm/Support/MathExtras.h"
31#include "llvm/ADT/Statistic.h"
32#include <set>
Jeff Cohen603fea92005-01-12 04:29:05 +000033#include <algorithm>
Chris Lattner8acb1ba2005-01-07 07:49:41 +000034using namespace llvm;
35
36//===----------------------------------------------------------------------===//
37// X86TargetLowering - X86 Implementation of the TargetLowering interface
38namespace {
39 class X86TargetLowering : public TargetLowering {
40 int VarArgsFrameIndex; // FrameIndex for start of varargs area.
Chris Lattner14824582005-01-09 00:01:27 +000041 int ReturnAddrIndex; // FrameIndex for return slot.
Chris Lattner8acb1ba2005-01-07 07:49:41 +000042 public:
43 X86TargetLowering(TargetMachine &TM) : TargetLowering(TM) {
44 // Set up the TargetLowering object.
Chris Lattner4df0de92005-01-17 00:00:33 +000045
46 // X86 is wierd, it always uses i8 for shift amounts and setcc results.
47 setShiftAmountType(MVT::i8);
48 setSetCCResultType(MVT::i8);
Chris Lattner6659bd72005-04-07 19:41:46 +000049 setSetCCResultContents(ZeroOrOneSetCCResult);
Chris Lattner009b55b2005-01-19 03:36:30 +000050 setShiftAmountFlavor(Mask); // shl X, 32 == shl X, 0
Chris Lattner4df0de92005-01-17 00:00:33 +000051
52 // Set up the register classes.
Chris Lattner8acb1ba2005-01-07 07:49:41 +000053 addRegisterClass(MVT::i8, X86::R8RegisterClass);
54 addRegisterClass(MVT::i16, X86::R16RegisterClass);
55 addRegisterClass(MVT::i32, X86::R32RegisterClass);
56 addRegisterClass(MVT::f64, X86::RFPRegisterClass);
Misha Brukman0e0a7a452005-04-21 23:38:14 +000057
Chris Lattner8acb1ba2005-01-07 07:49:41 +000058 // FIXME: Eliminate these two classes when legalize can handle promotions
59 // well.
Chris Lattnerda2ce112005-01-16 07:34:08 +000060/**/ addRegisterClass(MVT::i1, X86::R8RegisterClass);
Chris Lattnerda2ce112005-01-16 07:34:08 +000061
Chris Lattnerda4d4692005-04-09 03:22:37 +000062 setOperationAction(ISD::BRCONDTWOWAY , MVT::Other, Expand);
Chris Lattnerda2ce112005-01-16 07:34:08 +000063 setOperationAction(ISD::MEMMOVE , MVT::Other, Expand);
64 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Expand);
Chris Lattnerda2ce112005-01-16 07:34:08 +000065 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
Chris Lattnerda2ce112005-01-16 07:34:08 +000066 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
67 setOperationAction(ISD::SEXTLOAD , MVT::i1 , Expand);
68 setOperationAction(ISD::SREM , MVT::f64 , Expand);
Andrew Lenharth691ef2b2005-05-03 17:19:30 +000069 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
70 setOperationAction(ISD::CTTZ , MVT::i32 , Expand);
Andrew Lenharthb5884d32005-05-04 19:25:37 +000071 setOperationAction(ISD::CTLZ , MVT::i32 , Expand);
Chris Lattner43fdea02005-04-02 05:03:24 +000072
Chris Lattner4e6ce5f2005-05-09 20:37:29 +000073 setOperationAction(ISD::READIO , MVT::i1 , Expand);
74 setOperationAction(ISD::READIO , MVT::i8 , Expand);
75 setOperationAction(ISD::READIO , MVT::i16 , Expand);
76 setOperationAction(ISD::READIO , MVT::i32 , Expand);
77 setOperationAction(ISD::WRITEIO , MVT::i1 , Expand);
78 setOperationAction(ISD::WRITEIO , MVT::i8 , Expand);
79 setOperationAction(ISD::WRITEIO , MVT::i16 , Expand);
80 setOperationAction(ISD::WRITEIO , MVT::i32 , Expand);
81
Chris Lattnerc5dcb532005-04-30 04:25:35 +000082 if (!UnsafeFPMath) {
83 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
84 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
85 }
86
Chris Lattnerda2ce112005-01-16 07:34:08 +000087 // These should be promoted to a larger select which is supported.
88/**/ setOperationAction(ISD::SELECT , MVT::i1 , Promote);
89 setOperationAction(ISD::SELECT , MVT::i8 , Promote);
Misha Brukman0e0a7a452005-04-21 23:38:14 +000090
Chris Lattner8acb1ba2005-01-07 07:49:41 +000091 computeRegisterProperties();
Misha Brukman0e0a7a452005-04-21 23:38:14 +000092
Chris Lattner8acb1ba2005-01-07 07:49:41 +000093 addLegalFPImmediate(+0.0); // FLD0
94 addLegalFPImmediate(+1.0); // FLD1
95 addLegalFPImmediate(-0.0); // FLD0/FCHS
96 addLegalFPImmediate(-1.0); // FLD1/FCHS
97 }
98
99 /// LowerArguments - This hook must be implemented to indicate how we should
100 /// lower the arguments for the specified function, into the specified DAG.
101 virtual std::vector<SDOperand>
102 LowerArguments(Function &F, SelectionDAG &DAG);
103
104 /// LowerCallTo - This hook lowers an abstract call to a function into an
105 /// actual call.
Chris Lattner5188ad72005-01-08 19:28:19 +0000106 virtual std::pair<SDOperand, SDOperand>
Nate Begeman8e21e712005-03-26 01:29:23 +0000107 LowerCallTo(SDOperand Chain, const Type *RetTy, bool isVarArg,
108 SDOperand Callee, ArgListTy &Args, SelectionDAG &DAG);
Chris Lattner14824582005-01-09 00:01:27 +0000109
110 virtual std::pair<SDOperand, SDOperand>
111 LowerVAStart(SDOperand Chain, SelectionDAG &DAG);
112
113 virtual std::pair<SDOperand,SDOperand>
114 LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
115 const Type *ArgTy, SelectionDAG &DAG);
116
117 virtual std::pair<SDOperand, SDOperand>
118 LowerFrameReturnAddress(bool isFrameAddr, SDOperand Chain, unsigned Depth,
119 SelectionDAG &DAG);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000120 };
121}
122
123
124std::vector<SDOperand>
125X86TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
126 std::vector<SDOperand> ArgValues;
127
128 // Add DAG nodes to load the arguments... On entry to a function on the X86,
129 // the stack frame looks like this:
130 //
131 // [ESP] -- return address
132 // [ESP + 4] -- first argument (leftmost lexically)
133 // [ESP + 8] -- second argument, if first argument is four bytes in size
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000134 // ...
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000135 //
136 MachineFunction &MF = DAG.getMachineFunction();
137 MachineFrameInfo *MFI = MF.getFrameInfo();
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000138
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000139 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
Chris Lattnere4d5c442005-03-15 04:54:21 +0000140 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000141 MVT::ValueType ObjectVT = getValueType(I->getType());
142 unsigned ArgIncrement = 4;
143 unsigned ObjSize;
144 switch (ObjectVT) {
145 default: assert(0 && "Unhandled argument type!");
146 case MVT::i1:
147 case MVT::i8: ObjSize = 1; break;
148 case MVT::i16: ObjSize = 2; break;
149 case MVT::i32: ObjSize = 4; break;
150 case MVT::i64: ObjSize = ArgIncrement = 8; break;
151 case MVT::f32: ObjSize = 4; break;
152 case MVT::f64: ObjSize = ArgIncrement = 8; break;
153 }
154 // Create the frame index object for this incoming parameter...
155 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000156
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000157 // Create the SelectionDAG nodes corresponding to a load from this parameter
158 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
159
160 // Don't codegen dead arguments. FIXME: remove this check when we can nuke
161 // dead loads.
162 SDOperand ArgValue;
163 if (!I->use_empty())
Chris Lattnera80d2bd2005-05-09 05:40:26 +0000164 ArgValue = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
165 DAG.getSrcValue(NULL));
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000166 else {
167 if (MVT::isInteger(ObjectVT))
168 ArgValue = DAG.getConstant(0, ObjectVT);
169 else
170 ArgValue = DAG.getConstantFP(0, ObjectVT);
171 }
172 ArgValues.push_back(ArgValue);
173
174 ArgOffset += ArgIncrement; // Move on to the next argument...
175 }
176
177 // If the function takes variable number of arguments, make a frame index for
178 // the start of the first vararg value... for expansion of llvm.va_start.
179 if (F.isVarArg())
180 VarArgsFrameIndex = MFI->CreateFixedObject(1, ArgOffset);
Chris Lattner14824582005-01-09 00:01:27 +0000181 ReturnAddrIndex = 0; // No return address slot generated yet.
Chris Lattner4c52f0e2005-04-09 15:23:56 +0000182
183 // Finally, inform the code generator which regs we return values in.
184 switch (getValueType(F.getReturnType())) {
185 default: assert(0 && "Unknown type!");
186 case MVT::isVoid: break;
187 case MVT::i1:
188 case MVT::i8:
189 case MVT::i16:
190 case MVT::i32:
191 MF.addLiveOut(X86::EAX);
192 break;
193 case MVT::i64:
194 MF.addLiveOut(X86::EAX);
195 MF.addLiveOut(X86::EDX);
196 break;
197 case MVT::f32:
198 case MVT::f64:
199 MF.addLiveOut(X86::ST0);
200 break;
201 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000202 return ArgValues;
203}
204
Chris Lattner5188ad72005-01-08 19:28:19 +0000205std::pair<SDOperand, SDOperand>
206X86TargetLowering::LowerCallTo(SDOperand Chain,
Nate Begeman8e21e712005-03-26 01:29:23 +0000207 const Type *RetTy, bool isVarArg,
208 SDOperand Callee, ArgListTy &Args, SelectionDAG &DAG) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000209 // Count how many bytes are to be pushed on the stack.
210 unsigned NumBytes = 0;
211
212 if (Args.empty()) {
213 // Save zero bytes.
Chris Lattner5188ad72005-01-08 19:28:19 +0000214 Chain = DAG.getNode(ISD::ADJCALLSTACKDOWN, MVT::Other, Chain,
215 DAG.getConstant(0, getPointerTy()));
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000216 } else {
217 for (unsigned i = 0, e = Args.size(); i != e; ++i)
218 switch (getValueType(Args[i].second)) {
219 default: assert(0 && "Unknown value type!");
220 case MVT::i1:
221 case MVT::i8:
222 case MVT::i16:
223 case MVT::i32:
224 case MVT::f32:
225 NumBytes += 4;
226 break;
227 case MVT::i64:
228 case MVT::f64:
229 NumBytes += 8;
230 break;
231 }
232
Chris Lattner5188ad72005-01-08 19:28:19 +0000233 Chain = DAG.getNode(ISD::ADJCALLSTACKDOWN, MVT::Other, Chain,
234 DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000235
236 // Arguments go on the stack in reverse order, as specified by the ABI.
237 unsigned ArgOffset = 0;
Chris Lattner7f2afac2005-01-14 22:37:41 +0000238 SDOperand StackPtr = DAG.getCopyFromReg(X86::ESP, MVT::i32,
239 DAG.getEntryNode());
Chris Lattnerb62e1e22005-01-21 19:46:38 +0000240 std::vector<SDOperand> Stores;
241
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000242 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000243 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
244 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
245
246 switch (getValueType(Args[i].second)) {
247 default: assert(0 && "Unexpected ValueType for argument!");
248 case MVT::i1:
249 case MVT::i8:
250 case MVT::i16:
251 // Promote the integer to 32 bits. If the input type is signed use a
252 // sign extend, otherwise use a zero extend.
253 if (Args[i].second->isSigned())
254 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
255 else
256 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
257
258 // FALL THROUGH
259 case MVT::i32:
260 case MVT::f32:
Chris Lattnerb62e1e22005-01-21 19:46:38 +0000261 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
Chris Lattnera80d2bd2005-05-09 05:40:26 +0000262 Args[i].first, PtrOff,
263 DAG.getSrcValue(NULL)));
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000264 ArgOffset += 4;
265 break;
266 case MVT::i64:
267 case MVT::f64:
Chris Lattnerb62e1e22005-01-21 19:46:38 +0000268 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
Chris Lattnera80d2bd2005-05-09 05:40:26 +0000269 Args[i].first, PtrOff,
270 DAG.getSrcValue(NULL)));
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000271 ArgOffset += 8;
272 break;
273 }
274 }
Chris Lattnerb62e1e22005-01-21 19:46:38 +0000275 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, Stores);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000276 }
277
278 std::vector<MVT::ValueType> RetVals;
279 MVT::ValueType RetTyVT = getValueType(RetTy);
280 if (RetTyVT != MVT::isVoid)
281 RetVals.push_back(RetTyVT);
282 RetVals.push_back(MVT::Other);
283
Chris Lattner5188ad72005-01-08 19:28:19 +0000284 SDOperand TheCall = SDOperand(DAG.getCall(RetVals, Chain, Callee), 0);
Chris Lattnerb0802652005-01-08 20:51:36 +0000285 Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
Chris Lattner5188ad72005-01-08 19:28:19 +0000286 Chain = DAG.getNode(ISD::ADJCALLSTACKUP, MVT::Other, Chain,
287 DAG.getConstant(NumBytes, getPointerTy()));
288 return std::make_pair(TheCall, Chain);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000289}
290
Chris Lattner14824582005-01-09 00:01:27 +0000291std::pair<SDOperand, SDOperand>
292X86TargetLowering::LowerVAStart(SDOperand Chain, SelectionDAG &DAG) {
293 // vastart just returns the address of the VarArgsFrameIndex slot.
294 return std::make_pair(DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32), Chain);
295}
296
297std::pair<SDOperand,SDOperand> X86TargetLowering::
298LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
299 const Type *ArgTy, SelectionDAG &DAG) {
300 MVT::ValueType ArgVT = getValueType(ArgTy);
301 SDOperand Result;
302 if (!isVANext) {
Chris Lattnera80d2bd2005-05-09 05:40:26 +0000303 Result = DAG.getLoad(ArgVT, DAG.getEntryNode(), VAList,
304 DAG.getSrcValue(NULL));
Chris Lattner14824582005-01-09 00:01:27 +0000305 } else {
306 unsigned Amt;
307 if (ArgVT == MVT::i32)
308 Amt = 4;
309 else {
310 assert((ArgVT == MVT::i64 || ArgVT == MVT::f64) &&
311 "Other types should have been promoted for varargs!");
312 Amt = 8;
313 }
314 Result = DAG.getNode(ISD::ADD, VAList.getValueType(), VAList,
315 DAG.getConstant(Amt, VAList.getValueType()));
316 }
317 return std::make_pair(Result, Chain);
318}
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000319
Chris Lattner14824582005-01-09 00:01:27 +0000320
321std::pair<SDOperand, SDOperand> X86TargetLowering::
322LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
323 SelectionDAG &DAG) {
324 SDOperand Result;
325 if (Depth) // Depths > 0 not supported yet!
326 Result = DAG.getConstant(0, getPointerTy());
327 else {
328 if (ReturnAddrIndex == 0) {
329 // Set up a frame object for the return address.
330 MachineFunction &MF = DAG.getMachineFunction();
331 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(4, -4);
332 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000333
Chris Lattner14824582005-01-09 00:01:27 +0000334 SDOperand RetAddrFI = DAG.getFrameIndex(ReturnAddrIndex, MVT::i32);
335
336 if (!isFrameAddress)
337 // Just load the return address
Andrew Lenharth2d86ea22005-04-27 20:10:01 +0000338 Result = DAG.getLoad(MVT::i32, DAG.getEntryNode(), RetAddrFI, DAG.getSrcValue(NULL));
Chris Lattner14824582005-01-09 00:01:27 +0000339 else
340 Result = DAG.getNode(ISD::SUB, MVT::i32, RetAddrFI,
341 DAG.getConstant(4, MVT::i32));
342 }
343 return std::make_pair(Result, Chain);
344}
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000345
346
Chris Lattner98a8ba02005-01-18 01:06:26 +0000347namespace {
348 /// X86ISelAddressMode - This corresponds to X86AddressMode, but uses
349 /// SDOperand's instead of register numbers for the leaves of the matched
350 /// tree.
351 struct X86ISelAddressMode {
352 enum {
353 RegBase,
354 FrameIndexBase,
355 } BaseType;
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000356
Chris Lattner98a8ba02005-01-18 01:06:26 +0000357 struct { // This is really a union, discriminated by BaseType!
358 SDOperand Reg;
359 int FrameIndex;
360 } Base;
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000361
Chris Lattner98a8ba02005-01-18 01:06:26 +0000362 unsigned Scale;
363 SDOperand IndexReg;
364 unsigned Disp;
365 GlobalValue *GV;
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000366
Chris Lattner98a8ba02005-01-18 01:06:26 +0000367 X86ISelAddressMode()
368 : BaseType(RegBase), Scale(1), IndexReg(), Disp(), GV(0) {
369 }
370 };
371}
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000372
373
374namespace {
375 Statistic<>
376 NumFPKill("x86-codegen", "Number of FP_REG_KILL instructions added");
377
378 //===--------------------------------------------------------------------===//
379 /// ISel - X86 specific code to select X86 machine instructions for
380 /// SelectionDAG operations.
381 ///
382 class ISel : public SelectionDAGISel {
383 /// ContainsFPCode - Every instruction we select that uses or defines a FP
384 /// register should set this to true.
385 bool ContainsFPCode;
386
387 /// X86Lowering - This object fully describes how to lower LLVM code to an
388 /// X86-specific SelectionDAG.
389 X86TargetLowering X86Lowering;
390
Chris Lattner11333092005-01-11 03:11:44 +0000391 /// RegPressureMap - This keeps an approximate count of the number of
392 /// registers required to evaluate each node in the graph.
393 std::map<SDNode*, unsigned> RegPressureMap;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000394
395 /// ExprMap - As shared expressions are codegen'd, we keep track of which
396 /// vreg the value is produced in, so we only emit one copy of each compiled
397 /// tree.
398 std::map<SDOperand, unsigned> ExprMap;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000399
400 public:
401 ISel(TargetMachine &TM) : SelectionDAGISel(X86Lowering), X86Lowering(TM) {
402 }
403
Chris Lattner67b1c3c2005-01-21 21:35:14 +0000404 virtual const char *getPassName() const {
405 return "X86 Pattern Instruction Selection";
406 }
407
Chris Lattner11333092005-01-11 03:11:44 +0000408 unsigned getRegPressure(SDOperand O) {
409 return RegPressureMap[O.Val];
410 }
411 unsigned ComputeRegPressure(SDOperand O);
412
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000413 /// InstructionSelectBasicBlock - This callback is invoked by
414 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
Chris Lattner7dbcb752005-01-12 04:21:28 +0000415 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000416
Chris Lattner44129b52005-01-25 20:03:11 +0000417 bool isFoldableLoad(SDOperand Op, SDOperand OtherOp,
418 bool FloatPromoteOk = false);
Chris Lattnera5ade062005-01-11 21:19:59 +0000419 void EmitFoldedLoad(SDOperand Op, X86AddressMode &AM);
Chris Lattnere10269b2005-01-17 19:25:26 +0000420 bool TryToFoldLoadOpStore(SDNode *Node);
Chris Lattnera5ade062005-01-11 21:19:59 +0000421
Chris Lattner30ea1e92005-01-19 07:37:26 +0000422 bool EmitOrOpOp(SDOperand Op1, SDOperand Op2, unsigned DestReg);
Chris Lattnercb1aa8d2005-01-17 01:34:14 +0000423 void EmitCMP(SDOperand LHS, SDOperand RHS, bool isOnlyUse);
Chris Lattner6c07aee2005-01-11 04:06:27 +0000424 bool EmitBranchCC(MachineBasicBlock *Dest, SDOperand Chain, SDOperand Cond);
Chris Lattner24aad1b2005-01-10 22:10:13 +0000425 void EmitSelectCC(SDOperand Cond, MVT::ValueType SVT,
426 unsigned RTrue, unsigned RFalse, unsigned RDest);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000427 unsigned SelectExpr(SDOperand N);
Chris Lattner98a8ba02005-01-18 01:06:26 +0000428
429 X86AddressMode SelectAddrExprs(const X86ISelAddressMode &IAM);
430 bool MatchAddress(SDOperand N, X86ISelAddressMode &AM);
431 void SelectAddress(SDOperand N, X86AddressMode &AM);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000432 void Select(SDOperand N);
433 };
434}
435
Chris Lattner7dbcb752005-01-12 04:21:28 +0000436/// InstructionSelectBasicBlock - This callback is invoked by SelectionDAGISel
437/// when it has created a SelectionDAG for us to codegen.
438void ISel::InstructionSelectBasicBlock(SelectionDAG &DAG) {
439 // While we're doing this, keep track of whether we see any FP code for
440 // FP_REG_KILL insertion.
441 ContainsFPCode = false;
442
443 // Scan the PHI nodes that already are inserted into this basic block. If any
444 // of them is a PHI of a floating point value, we need to insert an
445 // FP_REG_KILL.
446 SSARegMap *RegMap = BB->getParent()->getSSARegMap();
447 for (MachineBasicBlock::iterator I = BB->begin(), E = BB->end();
448 I != E; ++I) {
449 assert(I->getOpcode() == X86::PHI &&
450 "Isn't just PHI nodes?");
451 if (RegMap->getRegClass(I->getOperand(0).getReg()) ==
452 X86::RFPRegisterClass) {
453 ContainsFPCode = true;
454 break;
455 }
456 }
457
458 // Compute the RegPressureMap, which is an approximation for the number of
459 // registers required to compute each node.
460 ComputeRegPressure(DAG.getRoot());
461
462 // Codegen the basic block.
463 Select(DAG.getRoot());
464
465 // Finally, look at all of the successors of this block. If any contain a PHI
466 // node of FP type, we need to insert an FP_REG_KILL in this block.
467 for (MachineBasicBlock::succ_iterator SI = BB->succ_begin(),
468 E = BB->succ_end(); SI != E && !ContainsFPCode; ++SI)
469 for (MachineBasicBlock::iterator I = (*SI)->begin(), E = (*SI)->end();
470 I != E && I->getOpcode() == X86::PHI; ++I) {
471 if (RegMap->getRegClass(I->getOperand(0).getReg()) ==
472 X86::RFPRegisterClass) {
473 ContainsFPCode = true;
474 break;
475 }
476 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000477
Chris Lattnere3e0f272005-05-09 03:36:39 +0000478 // Final check, check LLVM BB's that are successors to the LLVM BB
479 // corresponding to BB for FP PHI nodes.
480 const BasicBlock *LLVMBB = BB->getBasicBlock();
481 const PHINode *PN;
482 if (!ContainsFPCode)
483 for (succ_const_iterator SI = succ_begin(LLVMBB), E = succ_end(LLVMBB);
484 SI != E && !ContainsFPCode; ++SI)
485 for (BasicBlock::const_iterator II = SI->begin();
486 (PN = dyn_cast<PHINode>(II)); ++II)
487 if (PN->getType()->isFloatingPoint()) {
488 ContainsFPCode = true;
489 break;
490 }
491
492
Chris Lattner7dbcb752005-01-12 04:21:28 +0000493 // Insert FP_REG_KILL instructions into basic blocks that need them. This
494 // only occurs due to the floating point stackifier not being aggressive
495 // enough to handle arbitrary global stackification.
496 //
497 // Currently we insert an FP_REG_KILL instruction into each block that uses or
498 // defines a floating point virtual register.
499 //
500 // When the global register allocators (like linear scan) finally update live
501 // variable analysis, we can keep floating point values in registers across
502 // basic blocks. This will be a huge win, but we are waiting on the global
503 // allocators before we can do this.
504 //
Chris Lattner71df3f82005-03-30 01:10:00 +0000505 if (ContainsFPCode) {
Chris Lattner7dbcb752005-01-12 04:21:28 +0000506 BuildMI(*BB, BB->getFirstTerminator(), X86::FP_REG_KILL, 0);
507 ++NumFPKill;
508 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000509
Chris Lattner7dbcb752005-01-12 04:21:28 +0000510 // Clear state used for selection.
511 ExprMap.clear();
Chris Lattner7dbcb752005-01-12 04:21:28 +0000512 RegPressureMap.clear();
513}
514
515
Chris Lattner11333092005-01-11 03:11:44 +0000516// ComputeRegPressure - Compute the RegPressureMap, which is an approximation
517// for the number of registers required to compute each node. This is basically
518// computing a generalized form of the Sethi-Ullman number for each node.
519unsigned ISel::ComputeRegPressure(SDOperand O) {
520 SDNode *N = O.Val;
521 unsigned &Result = RegPressureMap[N];
522 if (Result) return Result;
523
Chris Lattnera3aa2e22005-01-11 03:37:59 +0000524 // FIXME: Should operations like CALL (which clobber lots o regs) have a
525 // higher fixed cost??
526
Chris Lattnerc4b6a782005-01-11 22:29:12 +0000527 if (N->getNumOperands() == 0) {
528 Result = 1;
529 } else {
530 unsigned MaxRegUse = 0;
531 unsigned NumExtraMaxRegUsers = 0;
532 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
533 unsigned Regs;
534 if (N->getOperand(i).getOpcode() == ISD::Constant)
535 Regs = 0;
536 else
537 Regs = ComputeRegPressure(N->getOperand(i));
538 if (Regs > MaxRegUse) {
539 MaxRegUse = Regs;
540 NumExtraMaxRegUsers = 0;
541 } else if (Regs == MaxRegUse &&
542 N->getOperand(i).getValueType() != MVT::Other) {
543 ++NumExtraMaxRegUsers;
544 }
Chris Lattner11333092005-01-11 03:11:44 +0000545 }
Chris Lattner90d1be72005-01-17 22:56:09 +0000546
547 if (O.getOpcode() != ISD::TokenFactor)
548 Result = MaxRegUse+NumExtraMaxRegUsers;
549 else
Chris Lattner869e0432005-01-17 23:02:13 +0000550 Result = MaxRegUse == 1 ? 0 : MaxRegUse-1;
Chris Lattnerc4b6a782005-01-11 22:29:12 +0000551 }
Chris Lattnerafce4302005-01-12 02:19:06 +0000552
Chris Lattner837caa72005-01-11 23:21:30 +0000553 //std::cerr << " WEIGHT: " << Result << " "; N->dump(); std::cerr << "\n";
Chris Lattnerc4b6a782005-01-11 22:29:12 +0000554 return Result;
Chris Lattner11333092005-01-11 03:11:44 +0000555}
556
Chris Lattnerbf52d492005-01-20 16:50:16 +0000557/// NodeTransitivelyUsesValue - Return true if N or any of its uses uses Op.
558/// The DAG cannot have cycles in it, by definition, so the visited set is not
559/// needed to prevent infinite loops. The DAG CAN, however, have unbounded
560/// reuse, so it prevents exponential cases.
561///
562static bool NodeTransitivelyUsesValue(SDOperand N, SDOperand Op,
563 std::set<SDNode*> &Visited) {
564 if (N == Op) return true; // Found it.
565 SDNode *Node = N.Val;
Chris Lattnerfb0f53f2005-01-21 21:43:02 +0000566 if (Node->getNumOperands() == 0 || // Leaf?
567 Node->getNodeDepth() <= Op.getNodeDepth()) return false; // Can't find it?
Chris Lattnerbf52d492005-01-20 16:50:16 +0000568 if (!Visited.insert(Node).second) return false; // Already visited?
569
570 // Recurse for the first N-1 operands.
571 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i)
572 if (NodeTransitivelyUsesValue(Node->getOperand(i), Op, Visited))
573 return true;
574
575 // Tail recurse for the last operand.
576 return NodeTransitivelyUsesValue(Node->getOperand(0), Op, Visited);
577}
578
Chris Lattner98a8ba02005-01-18 01:06:26 +0000579X86AddressMode ISel::SelectAddrExprs(const X86ISelAddressMode &IAM) {
580 X86AddressMode Result;
581
582 // If we need to emit two register operands, emit the one with the highest
583 // register pressure first.
584 if (IAM.BaseType == X86ISelAddressMode::RegBase &&
585 IAM.Base.Reg.Val && IAM.IndexReg.Val) {
Chris Lattnerbf52d492005-01-20 16:50:16 +0000586 bool EmitBaseThenIndex;
Chris Lattner98a8ba02005-01-18 01:06:26 +0000587 if (getRegPressure(IAM.Base.Reg) > getRegPressure(IAM.IndexReg)) {
Chris Lattnerbf52d492005-01-20 16:50:16 +0000588 std::set<SDNode*> Visited;
589 EmitBaseThenIndex = true;
590 // If Base ends up pointing to Index, we must emit index first. This is
591 // because of the way we fold loads, we may end up doing bad things with
592 // the folded add.
593 if (NodeTransitivelyUsesValue(IAM.Base.Reg, IAM.IndexReg, Visited))
594 EmitBaseThenIndex = false;
595 } else {
596 std::set<SDNode*> Visited;
597 EmitBaseThenIndex = false;
598 // If Base ends up pointing to Index, we must emit index first. This is
599 // because of the way we fold loads, we may end up doing bad things with
600 // the folded add.
601 if (NodeTransitivelyUsesValue(IAM.IndexReg, IAM.Base.Reg, Visited))
602 EmitBaseThenIndex = true;
603 }
604
605 if (EmitBaseThenIndex) {
Chris Lattner98a8ba02005-01-18 01:06:26 +0000606 Result.Base.Reg = SelectExpr(IAM.Base.Reg);
607 Result.IndexReg = SelectExpr(IAM.IndexReg);
608 } else {
609 Result.IndexReg = SelectExpr(IAM.IndexReg);
610 Result.Base.Reg = SelectExpr(IAM.Base.Reg);
611 }
Chris Lattnerbf52d492005-01-20 16:50:16 +0000612
Chris Lattner98a8ba02005-01-18 01:06:26 +0000613 } else if (IAM.BaseType == X86ISelAddressMode::RegBase && IAM.Base.Reg.Val) {
614 Result.Base.Reg = SelectExpr(IAM.Base.Reg);
615 } else if (IAM.IndexReg.Val) {
616 Result.IndexReg = SelectExpr(IAM.IndexReg);
617 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000618
Chris Lattner98a8ba02005-01-18 01:06:26 +0000619 switch (IAM.BaseType) {
620 case X86ISelAddressMode::RegBase:
621 Result.BaseType = X86AddressMode::RegBase;
622 break;
623 case X86ISelAddressMode::FrameIndexBase:
624 Result.BaseType = X86AddressMode::FrameIndexBase;
625 Result.Base.FrameIndex = IAM.Base.FrameIndex;
626 break;
627 default:
628 assert(0 && "Unknown base type!");
629 break;
630 }
631 Result.Scale = IAM.Scale;
632 Result.Disp = IAM.Disp;
633 Result.GV = IAM.GV;
634 return Result;
635}
636
637/// SelectAddress - Pattern match the maximal addressing mode for this node and
638/// emit all of the leaf registers.
639void ISel::SelectAddress(SDOperand N, X86AddressMode &AM) {
640 X86ISelAddressMode IAM;
641 MatchAddress(N, IAM);
642 AM = SelectAddrExprs(IAM);
643}
644
645/// MatchAddress - Add the specified node to the specified addressing mode,
646/// returning true if it cannot be done. This just pattern matches for the
647/// addressing mode, it does not cause any code to be emitted. For that, use
648/// SelectAddress.
649bool ISel::MatchAddress(SDOperand N, X86ISelAddressMode &AM) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000650 switch (N.getOpcode()) {
651 default: break;
652 case ISD::FrameIndex:
Chris Lattner98a8ba02005-01-18 01:06:26 +0000653 if (AM.BaseType == X86ISelAddressMode::RegBase && AM.Base.Reg.Val == 0) {
654 AM.BaseType = X86ISelAddressMode::FrameIndexBase;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000655 AM.Base.FrameIndex = cast<FrameIndexSDNode>(N)->getIndex();
656 return false;
657 }
658 break;
659 case ISD::GlobalAddress:
660 if (AM.GV == 0) {
661 AM.GV = cast<GlobalAddressSDNode>(N)->getGlobal();
662 return false;
663 }
664 break;
665 case ISD::Constant:
666 AM.Disp += cast<ConstantSDNode>(N)->getValue();
667 return false;
668 case ISD::SHL:
Chris Lattner636e79a2005-01-13 05:53:16 +0000669 // We might have folded the load into this shift, so don't regen the value
670 // if so.
671 if (ExprMap.count(N)) break;
672
Chris Lattner98a8ba02005-01-18 01:06:26 +0000673 if (AM.IndexReg.Val == 0 && AM.Scale == 1)
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000674 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1))) {
675 unsigned Val = CN->getValue();
676 if (Val == 1 || Val == 2 || Val == 3) {
677 AM.Scale = 1 << Val;
Chris Lattner51a26342005-01-11 06:36:20 +0000678 SDOperand ShVal = N.Val->getOperand(0);
679
680 // Okay, we know that we have a scale by now. However, if the scaled
681 // value is an add of something and a constant, we can fold the
682 // constant into the disp field here.
Chris Lattner811482a2005-01-18 04:18:32 +0000683 if (ShVal.Val->getOpcode() == ISD::ADD && ShVal.hasOneUse() &&
Chris Lattner51a26342005-01-11 06:36:20 +0000684 isa<ConstantSDNode>(ShVal.Val->getOperand(1))) {
Chris Lattner98a8ba02005-01-18 01:06:26 +0000685 AM.IndexReg = ShVal.Val->getOperand(0);
Chris Lattner51a26342005-01-11 06:36:20 +0000686 ConstantSDNode *AddVal =
687 cast<ConstantSDNode>(ShVal.Val->getOperand(1));
688 AM.Disp += AddVal->getValue() << Val;
Chris Lattner636e79a2005-01-13 05:53:16 +0000689 } else {
Chris Lattner98a8ba02005-01-18 01:06:26 +0000690 AM.IndexReg = ShVal;
Chris Lattner51a26342005-01-11 06:36:20 +0000691 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000692 return false;
693 }
694 }
695 break;
Chris Lattner947d5442005-01-11 19:37:02 +0000696 case ISD::MUL:
Chris Lattner636e79a2005-01-13 05:53:16 +0000697 // We might have folded the load into this mul, so don't regen the value if
698 // so.
699 if (ExprMap.count(N)) break;
700
Chris Lattner947d5442005-01-11 19:37:02 +0000701 // X*[3,5,9] -> X+X*[2,4,8]
Chris Lattner98a8ba02005-01-18 01:06:26 +0000702 if (AM.IndexReg.Val == 0 && AM.BaseType == X86ISelAddressMode::RegBase &&
703 AM.Base.Reg.Val == 0)
Chris Lattner947d5442005-01-11 19:37:02 +0000704 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.Val->getOperand(1)))
705 if (CN->getValue() == 3 || CN->getValue() == 5 || CN->getValue() == 9) {
706 AM.Scale = unsigned(CN->getValue())-1;
707
708 SDOperand MulVal = N.Val->getOperand(0);
Chris Lattner98a8ba02005-01-18 01:06:26 +0000709 SDOperand Reg;
Chris Lattner947d5442005-01-11 19:37:02 +0000710
711 // Okay, we know that we have a scale by now. However, if the scaled
712 // value is an add of something and a constant, we can fold the
713 // constant into the disp field here.
Chris Lattner811482a2005-01-18 04:18:32 +0000714 if (MulVal.Val->getOpcode() == ISD::ADD && MulVal.hasOneUse() &&
Chris Lattner947d5442005-01-11 19:37:02 +0000715 isa<ConstantSDNode>(MulVal.Val->getOperand(1))) {
Chris Lattner98a8ba02005-01-18 01:06:26 +0000716 Reg = MulVal.Val->getOperand(0);
Chris Lattner947d5442005-01-11 19:37:02 +0000717 ConstantSDNode *AddVal =
718 cast<ConstantSDNode>(MulVal.Val->getOperand(1));
719 AM.Disp += AddVal->getValue() * CN->getValue();
Misha Brukman0e0a7a452005-04-21 23:38:14 +0000720 } else {
Chris Lattner98a8ba02005-01-18 01:06:26 +0000721 Reg = N.Val->getOperand(0);
Chris Lattner947d5442005-01-11 19:37:02 +0000722 }
723
724 AM.IndexReg = AM.Base.Reg = Reg;
725 return false;
726 }
727 break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000728
729 case ISD::ADD: {
Chris Lattner636e79a2005-01-13 05:53:16 +0000730 // We might have folded the load into this mul, so don't regen the value if
731 // so.
732 if (ExprMap.count(N)) break;
733
Chris Lattner98a8ba02005-01-18 01:06:26 +0000734 X86ISelAddressMode Backup = AM;
735 if (!MatchAddress(N.Val->getOperand(0), AM) &&
736 !MatchAddress(N.Val->getOperand(1), AM))
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000737 return false;
738 AM = Backup;
Chris Lattner98a8ba02005-01-18 01:06:26 +0000739 if (!MatchAddress(N.Val->getOperand(1), AM) &&
740 !MatchAddress(N.Val->getOperand(0), AM))
Chris Lattner9bbd9922005-01-12 18:08:53 +0000741 return false;
742 AM = Backup;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000743 break;
744 }
745 }
746
Chris Lattnera95589b2005-01-11 04:40:19 +0000747 // Is the base register already occupied?
Chris Lattner98a8ba02005-01-18 01:06:26 +0000748 if (AM.BaseType != X86ISelAddressMode::RegBase || AM.Base.Reg.Val) {
Chris Lattnera95589b2005-01-11 04:40:19 +0000749 // If so, check to see if the scale index register is set.
Chris Lattner98a8ba02005-01-18 01:06:26 +0000750 if (AM.IndexReg.Val == 0) {
751 AM.IndexReg = N;
Chris Lattnera95589b2005-01-11 04:40:19 +0000752 AM.Scale = 1;
753 return false;
754 }
755
756 // Otherwise, we cannot select it.
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000757 return true;
Chris Lattnera95589b2005-01-11 04:40:19 +0000758 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000759
760 // Default, generate it as a register.
Chris Lattner98a8ba02005-01-18 01:06:26 +0000761 AM.BaseType = X86ISelAddressMode::RegBase;
762 AM.Base.Reg = N;
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000763 return false;
764}
765
766/// Emit2SetCCsAndLogical - Emit the following sequence of instructions,
767/// assuming that the temporary registers are in the 8-bit register class.
768///
769/// Tmp1 = setcc1
770/// Tmp2 = setcc2
771/// DestReg = logicalop Tmp1, Tmp2
772///
773static void Emit2SetCCsAndLogical(MachineBasicBlock *BB, unsigned SetCC1,
774 unsigned SetCC2, unsigned LogicalOp,
775 unsigned DestReg) {
776 SSARegMap *RegMap = BB->getParent()->getSSARegMap();
777 unsigned Tmp1 = RegMap->createVirtualRegister(X86::R8RegisterClass);
778 unsigned Tmp2 = RegMap->createVirtualRegister(X86::R8RegisterClass);
779 BuildMI(BB, SetCC1, 0, Tmp1);
780 BuildMI(BB, SetCC2, 0, Tmp2);
781 BuildMI(BB, LogicalOp, 2, DestReg).addReg(Tmp1).addReg(Tmp2);
782}
783
784/// EmitSetCC - Emit the code to set the specified 8-bit register to 1 if the
785/// condition codes match the specified SetCCOpcode. Note that some conditions
786/// require multiple instructions to generate the correct value.
787static void EmitSetCC(MachineBasicBlock *BB, unsigned DestReg,
788 ISD::CondCode SetCCOpcode, bool isFP) {
789 unsigned Opc;
790 if (!isFP) {
791 switch (SetCCOpcode) {
792 default: assert(0 && "Illegal integer SetCC!");
793 case ISD::SETEQ: Opc = X86::SETEr; break;
794 case ISD::SETGT: Opc = X86::SETGr; break;
795 case ISD::SETGE: Opc = X86::SETGEr; break;
796 case ISD::SETLT: Opc = X86::SETLr; break;
797 case ISD::SETLE: Opc = X86::SETLEr; break;
798 case ISD::SETNE: Opc = X86::SETNEr; break;
799 case ISD::SETULT: Opc = X86::SETBr; break;
800 case ISD::SETUGT: Opc = X86::SETAr; break;
801 case ISD::SETULE: Opc = X86::SETBEr; break;
802 case ISD::SETUGE: Opc = X86::SETAEr; break;
803 }
804 } else {
805 // On a floating point condition, the flags are set as follows:
806 // ZF PF CF op
807 // 0 | 0 | 0 | X > Y
808 // 0 | 0 | 1 | X < Y
809 // 1 | 0 | 0 | X == Y
810 // 1 | 1 | 1 | unordered
811 //
812 switch (SetCCOpcode) {
813 default: assert(0 && "Invalid FP setcc!");
814 case ISD::SETUEQ:
815 case ISD::SETEQ:
816 Opc = X86::SETEr; // True if ZF = 1
817 break;
818 case ISD::SETOGT:
819 case ISD::SETGT:
820 Opc = X86::SETAr; // True if CF = 0 and ZF = 0
821 break;
822 case ISD::SETOGE:
823 case ISD::SETGE:
824 Opc = X86::SETAEr; // True if CF = 0
825 break;
826 case ISD::SETULT:
827 case ISD::SETLT:
828 Opc = X86::SETBr; // True if CF = 1
829 break;
830 case ISD::SETULE:
831 case ISD::SETLE:
832 Opc = X86::SETBEr; // True if CF = 1 or ZF = 1
833 break;
834 case ISD::SETONE:
835 case ISD::SETNE:
836 Opc = X86::SETNEr; // True if ZF = 0
837 break;
838 case ISD::SETUO:
839 Opc = X86::SETPr; // True if PF = 1
840 break;
841 case ISD::SETO:
842 Opc = X86::SETNPr; // True if PF = 0
843 break;
844 case ISD::SETOEQ: // !PF & ZF
845 Emit2SetCCsAndLogical(BB, X86::SETNPr, X86::SETEr, X86::AND8rr, DestReg);
846 return;
847 case ISD::SETOLT: // !PF & CF
848 Emit2SetCCsAndLogical(BB, X86::SETNPr, X86::SETBr, X86::AND8rr, DestReg);
849 return;
850 case ISD::SETOLE: // !PF & (CF || ZF)
851 Emit2SetCCsAndLogical(BB, X86::SETNPr, X86::SETBEr, X86::AND8rr, DestReg);
852 return;
853 case ISD::SETUGT: // PF | (!ZF & !CF)
854 Emit2SetCCsAndLogical(BB, X86::SETPr, X86::SETAr, X86::OR8rr, DestReg);
855 return;
856 case ISD::SETUGE: // PF | !CF
857 Emit2SetCCsAndLogical(BB, X86::SETPr, X86::SETAEr, X86::OR8rr, DestReg);
858 return;
859 case ISD::SETUNE: // PF | !ZF
860 Emit2SetCCsAndLogical(BB, X86::SETPr, X86::SETNEr, X86::OR8rr, DestReg);
861 return;
862 }
863 }
864 BuildMI(BB, Opc, 0, DestReg);
865}
866
867
868/// EmitBranchCC - Emit code into BB that arranges for control to transfer to
869/// the Dest block if the Cond condition is true. If we cannot fold this
870/// condition into the branch, return true.
871///
Chris Lattner6c07aee2005-01-11 04:06:27 +0000872bool ISel::EmitBranchCC(MachineBasicBlock *Dest, SDOperand Chain,
873 SDOperand Cond) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000874 // FIXME: Evaluate whether it would be good to emit code like (X < Y) | (A >
875 // B) using two conditional branches instead of one condbr, two setcc's, and
876 // an or.
877 if ((Cond.getOpcode() == ISD::OR ||
878 Cond.getOpcode() == ISD::AND) && Cond.Val->hasOneUse()) {
879 // And and or set the flags for us, so there is no need to emit a TST of the
880 // result. It is only safe to do this if there is only a single use of the
881 // AND/OR though, otherwise we don't know it will be emitted here.
Chris Lattner6c07aee2005-01-11 04:06:27 +0000882 Select(Chain);
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000883 SelectExpr(Cond);
884 BuildMI(BB, X86::JNE, 1).addMBB(Dest);
885 return false;
886 }
887
888 // Codegen br not C -> JE.
889 if (Cond.getOpcode() == ISD::XOR)
890 if (ConstantSDNode *NC = dyn_cast<ConstantSDNode>(Cond.Val->getOperand(1)))
891 if (NC->isAllOnesValue()) {
Chris Lattner6c07aee2005-01-11 04:06:27 +0000892 unsigned CondR;
893 if (getRegPressure(Chain) > getRegPressure(Cond)) {
894 Select(Chain);
895 CondR = SelectExpr(Cond.Val->getOperand(0));
896 } else {
897 CondR = SelectExpr(Cond.Val->getOperand(0));
898 Select(Chain);
899 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000900 BuildMI(BB, X86::TEST8rr, 2).addReg(CondR).addReg(CondR);
901 BuildMI(BB, X86::JE, 1).addMBB(Dest);
902 return false;
903 }
904
905 SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Cond);
906 if (SetCC == 0)
907 return true; // Can only handle simple setcc's so far.
908
909 unsigned Opc;
910
911 // Handle integer conditions first.
912 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
913 switch (SetCC->getCondition()) {
914 default: assert(0 && "Illegal integer SetCC!");
915 case ISD::SETEQ: Opc = X86::JE; break;
916 case ISD::SETGT: Opc = X86::JG; break;
917 case ISD::SETGE: Opc = X86::JGE; break;
918 case ISD::SETLT: Opc = X86::JL; break;
919 case ISD::SETLE: Opc = X86::JLE; break;
920 case ISD::SETNE: Opc = X86::JNE; break;
921 case ISD::SETULT: Opc = X86::JB; break;
922 case ISD::SETUGT: Opc = X86::JA; break;
923 case ISD::SETULE: Opc = X86::JBE; break;
924 case ISD::SETUGE: Opc = X86::JAE; break;
925 }
Chris Lattner6c07aee2005-01-11 04:06:27 +0000926 Select(Chain);
Chris Lattnercb1aa8d2005-01-17 01:34:14 +0000927 EmitCMP(SetCC->getOperand(0), SetCC->getOperand(1), SetCC->hasOneUse());
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000928 BuildMI(BB, Opc, 1).addMBB(Dest);
929 return false;
930 }
931
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000932 unsigned Opc2 = 0; // Second branch if needed.
933
934 // On a floating point condition, the flags are set as follows:
935 // ZF PF CF op
936 // 0 | 0 | 0 | X > Y
937 // 0 | 0 | 1 | X < Y
938 // 1 | 0 | 0 | X == Y
939 // 1 | 1 | 1 | unordered
940 //
941 switch (SetCC->getCondition()) {
942 default: assert(0 && "Invalid FP setcc!");
943 case ISD::SETUEQ:
944 case ISD::SETEQ: Opc = X86::JE; break; // True if ZF = 1
945 case ISD::SETOGT:
946 case ISD::SETGT: Opc = X86::JA; break; // True if CF = 0 and ZF = 0
947 case ISD::SETOGE:
948 case ISD::SETGE: Opc = X86::JAE; break; // True if CF = 0
949 case ISD::SETULT:
950 case ISD::SETLT: Opc = X86::JB; break; // True if CF = 1
951 case ISD::SETULE:
952 case ISD::SETLE: Opc = X86::JBE; break; // True if CF = 1 or ZF = 1
953 case ISD::SETONE:
954 case ISD::SETNE: Opc = X86::JNE; break; // True if ZF = 0
955 case ISD::SETUO: Opc = X86::JP; break; // True if PF = 1
956 case ISD::SETO: Opc = X86::JNP; break; // True if PF = 0
957 case ISD::SETUGT: // PF = 1 | (ZF = 0 & CF = 0)
958 Opc = X86::JA; // ZF = 0 & CF = 0
959 Opc2 = X86::JP; // PF = 1
960 break;
961 case ISD::SETUGE: // PF = 1 | CF = 0
962 Opc = X86::JAE; // CF = 0
963 Opc2 = X86::JP; // PF = 1
964 break;
965 case ISD::SETUNE: // PF = 1 | ZF = 0
966 Opc = X86::JNE; // ZF = 0
967 Opc2 = X86::JP; // PF = 1
968 break;
969 case ISD::SETOEQ: // PF = 0 & ZF = 1
970 //X86::JNP, X86::JE
971 //X86::AND8rr
972 return true; // FIXME: Emit more efficient code for this branch.
973 case ISD::SETOLT: // PF = 0 & CF = 1
974 //X86::JNP, X86::JB
975 //X86::AND8rr
976 return true; // FIXME: Emit more efficient code for this branch.
977 case ISD::SETOLE: // PF = 0 & (CF = 1 || ZF = 1)
978 //X86::JNP, X86::JBE
979 //X86::AND8rr
980 return true; // FIXME: Emit more efficient code for this branch.
981 }
982
Chris Lattner6c07aee2005-01-11 04:06:27 +0000983 Select(Chain);
Chris Lattnercb1aa8d2005-01-17 01:34:14 +0000984 EmitCMP(SetCC->getOperand(0), SetCC->getOperand(1), SetCC->hasOneUse());
Chris Lattner8acb1ba2005-01-07 07:49:41 +0000985 BuildMI(BB, Opc, 1).addMBB(Dest);
986 if (Opc2)
987 BuildMI(BB, Opc2, 1).addMBB(Dest);
988 return false;
989}
990
Chris Lattner24aad1b2005-01-10 22:10:13 +0000991/// EmitSelectCC - Emit code into BB that performs a select operation between
992/// the two registers RTrue and RFalse, generating a result into RDest. Return
993/// true if the fold cannot be performed.
994///
995void ISel::EmitSelectCC(SDOperand Cond, MVT::ValueType SVT,
996 unsigned RTrue, unsigned RFalse, unsigned RDest) {
997 enum Condition {
998 EQ, NE, LT, LE, GT, GE, B, BE, A, AE, P, NP,
999 NOT_SET
1000 } CondCode = NOT_SET;
1001
1002 static const unsigned CMOVTAB16[] = {
1003 X86::CMOVE16rr, X86::CMOVNE16rr, X86::CMOVL16rr, X86::CMOVLE16rr,
1004 X86::CMOVG16rr, X86::CMOVGE16rr, X86::CMOVB16rr, X86::CMOVBE16rr,
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001005 X86::CMOVA16rr, X86::CMOVAE16rr, X86::CMOVP16rr, X86::CMOVNP16rr,
Chris Lattner24aad1b2005-01-10 22:10:13 +00001006 };
1007 static const unsigned CMOVTAB32[] = {
1008 X86::CMOVE32rr, X86::CMOVNE32rr, X86::CMOVL32rr, X86::CMOVLE32rr,
1009 X86::CMOVG32rr, X86::CMOVGE32rr, X86::CMOVB32rr, X86::CMOVBE32rr,
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001010 X86::CMOVA32rr, X86::CMOVAE32rr, X86::CMOVP32rr, X86::CMOVNP32rr,
Chris Lattner24aad1b2005-01-10 22:10:13 +00001011 };
1012 static const unsigned CMOVTABFP[] = {
1013 X86::FCMOVE , X86::FCMOVNE, /*missing*/0, /*missing*/0,
1014 /*missing*/0, /*missing*/0, X86::FCMOVB , X86::FCMOVBE,
1015 X86::FCMOVA , X86::FCMOVAE, X86::FCMOVP , X86::FCMOVNP
1016 };
1017
1018 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Cond)) {
1019 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
1020 switch (SetCC->getCondition()) {
1021 default: assert(0 && "Unknown integer comparison!");
1022 case ISD::SETEQ: CondCode = EQ; break;
1023 case ISD::SETGT: CondCode = GT; break;
1024 case ISD::SETGE: CondCode = GE; break;
1025 case ISD::SETLT: CondCode = LT; break;
1026 case ISD::SETLE: CondCode = LE; break;
1027 case ISD::SETNE: CondCode = NE; break;
1028 case ISD::SETULT: CondCode = B; break;
1029 case ISD::SETUGT: CondCode = A; break;
1030 case ISD::SETULE: CondCode = BE; break;
1031 case ISD::SETUGE: CondCode = AE; break;
1032 }
1033 } else {
1034 // On a floating point condition, the flags are set as follows:
1035 // ZF PF CF op
1036 // 0 | 0 | 0 | X > Y
1037 // 0 | 0 | 1 | X < Y
1038 // 1 | 0 | 0 | X == Y
1039 // 1 | 1 | 1 | unordered
1040 //
1041 switch (SetCC->getCondition()) {
1042 default: assert(0 && "Unknown FP comparison!");
1043 case ISD::SETUEQ:
1044 case ISD::SETEQ: CondCode = EQ; break; // True if ZF = 1
1045 case ISD::SETOGT:
1046 case ISD::SETGT: CondCode = A; break; // True if CF = 0 and ZF = 0
1047 case ISD::SETOGE:
1048 case ISD::SETGE: CondCode = AE; break; // True if CF = 0
1049 case ISD::SETULT:
1050 case ISD::SETLT: CondCode = B; break; // True if CF = 1
1051 case ISD::SETULE:
1052 case ISD::SETLE: CondCode = BE; break; // True if CF = 1 or ZF = 1
1053 case ISD::SETONE:
1054 case ISD::SETNE: CondCode = NE; break; // True if ZF = 0
1055 case ISD::SETUO: CondCode = P; break; // True if PF = 1
1056 case ISD::SETO: CondCode = NP; break; // True if PF = 0
1057 case ISD::SETUGT: // PF = 1 | (ZF = 0 & CF = 0)
1058 case ISD::SETUGE: // PF = 1 | CF = 0
1059 case ISD::SETUNE: // PF = 1 | ZF = 0
1060 case ISD::SETOEQ: // PF = 0 & ZF = 1
1061 case ISD::SETOLT: // PF = 0 & CF = 1
1062 case ISD::SETOLE: // PF = 0 & (CF = 1 || ZF = 1)
1063 // We cannot emit this comparison as a single cmov.
1064 break;
1065 }
1066 }
1067 }
1068
1069 unsigned Opc = 0;
1070 if (CondCode != NOT_SET) {
1071 switch (SVT) {
1072 default: assert(0 && "Cannot select this type!");
1073 case MVT::i16: Opc = CMOVTAB16[CondCode]; break;
1074 case MVT::i32: Opc = CMOVTAB32[CondCode]; break;
Chris Lattneref7ba072005-01-11 03:50:45 +00001075 case MVT::f64: Opc = CMOVTABFP[CondCode]; break;
Chris Lattner24aad1b2005-01-10 22:10:13 +00001076 }
1077 }
1078
1079 // Finally, if we weren't able to fold this, just emit the condition and test
1080 // it.
1081 if (CondCode == NOT_SET || Opc == 0) {
1082 // Get the condition into the zero flag.
1083 unsigned CondReg = SelectExpr(Cond);
1084 BuildMI(BB, X86::TEST8rr, 2).addReg(CondReg).addReg(CondReg);
1085
1086 switch (SVT) {
1087 default: assert(0 && "Cannot select this type!");
1088 case MVT::i16: Opc = X86::CMOVE16rr; break;
1089 case MVT::i32: Opc = X86::CMOVE32rr; break;
Chris Lattneref7ba072005-01-11 03:50:45 +00001090 case MVT::f64: Opc = X86::FCMOVE; break;
Chris Lattner24aad1b2005-01-10 22:10:13 +00001091 }
1092 } else {
1093 // FIXME: CMP R, 0 -> TEST R, R
Chris Lattnercb1aa8d2005-01-17 01:34:14 +00001094 EmitCMP(Cond.getOperand(0), Cond.getOperand(1), Cond.Val->hasOneUse());
Chris Lattnera3aa2e22005-01-11 03:37:59 +00001095 std::swap(RTrue, RFalse);
Chris Lattner24aad1b2005-01-10 22:10:13 +00001096 }
1097 BuildMI(BB, Opc, 2, RDest).addReg(RTrue).addReg(RFalse);
1098}
1099
Chris Lattnercb1aa8d2005-01-17 01:34:14 +00001100void ISel::EmitCMP(SDOperand LHS, SDOperand RHS, bool HasOneUse) {
Chris Lattner11333092005-01-11 03:11:44 +00001101 unsigned Opc;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001102 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(RHS)) {
1103 Opc = 0;
Chris Lattner4ff348b2005-01-17 06:26:58 +00001104 if (HasOneUse && isFoldableLoad(LHS, RHS)) {
Chris Lattneref6806c2005-01-12 02:02:48 +00001105 switch (RHS.getValueType()) {
1106 default: break;
1107 case MVT::i1:
1108 case MVT::i8: Opc = X86::CMP8mi; break;
1109 case MVT::i16: Opc = X86::CMP16mi; break;
1110 case MVT::i32: Opc = X86::CMP32mi; break;
1111 }
1112 if (Opc) {
1113 X86AddressMode AM;
1114 EmitFoldedLoad(LHS, AM);
1115 addFullAddress(BuildMI(BB, Opc, 5), AM).addImm(CN->getValue());
1116 return;
1117 }
1118 }
1119
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001120 switch (RHS.getValueType()) {
1121 default: break;
1122 case MVT::i1:
1123 case MVT::i8: Opc = X86::CMP8ri; break;
1124 case MVT::i16: Opc = X86::CMP16ri; break;
1125 case MVT::i32: Opc = X86::CMP32ri; break;
1126 }
1127 if (Opc) {
Chris Lattner11333092005-01-11 03:11:44 +00001128 unsigned Tmp1 = SelectExpr(LHS);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001129 BuildMI(BB, Opc, 2).addReg(Tmp1).addImm(CN->getValue());
1130 return;
1131 }
Chris Lattner7f2afac2005-01-14 22:37:41 +00001132 } else if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(RHS)) {
1133 if (CN->isExactlyValue(+0.0) ||
1134 CN->isExactlyValue(-0.0)) {
1135 unsigned Reg = SelectExpr(LHS);
1136 BuildMI(BB, X86::FTST, 1).addReg(Reg);
1137 BuildMI(BB, X86::FNSTSW8r, 0);
1138 BuildMI(BB, X86::SAHF, 1);
Chris Lattner7805fa42005-03-17 16:29:26 +00001139 return;
Chris Lattner7f2afac2005-01-14 22:37:41 +00001140 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001141 }
1142
Chris Lattneref6806c2005-01-12 02:02:48 +00001143 Opc = 0;
Chris Lattner4ff348b2005-01-17 06:26:58 +00001144 if (HasOneUse && isFoldableLoad(LHS, RHS)) {
Chris Lattneref6806c2005-01-12 02:02:48 +00001145 switch (RHS.getValueType()) {
1146 default: break;
1147 case MVT::i1:
1148 case MVT::i8: Opc = X86::CMP8mr; break;
1149 case MVT::i16: Opc = X86::CMP16mr; break;
1150 case MVT::i32: Opc = X86::CMP32mr; break;
1151 }
1152 if (Opc) {
1153 X86AddressMode AM;
Chris Lattner636e79a2005-01-13 05:53:16 +00001154 EmitFoldedLoad(LHS, AM);
1155 unsigned Reg = SelectExpr(RHS);
Chris Lattneref6806c2005-01-12 02:02:48 +00001156 addFullAddress(BuildMI(BB, Opc, 5), AM).addReg(Reg);
1157 return;
1158 }
1159 }
1160
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001161 switch (LHS.getValueType()) {
1162 default: assert(0 && "Cannot compare this value!");
1163 case MVT::i1:
1164 case MVT::i8: Opc = X86::CMP8rr; break;
1165 case MVT::i16: Opc = X86::CMP16rr; break;
1166 case MVT::i32: Opc = X86::CMP32rr; break;
Chris Lattneref7ba072005-01-11 03:50:45 +00001167 case MVT::f64: Opc = X86::FUCOMIr; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001168 }
Chris Lattner11333092005-01-11 03:11:44 +00001169 unsigned Tmp1, Tmp2;
1170 if (getRegPressure(LHS) > getRegPressure(RHS)) {
1171 Tmp1 = SelectExpr(LHS);
1172 Tmp2 = SelectExpr(RHS);
1173 } else {
1174 Tmp2 = SelectExpr(RHS);
1175 Tmp1 = SelectExpr(LHS);
1176 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001177 BuildMI(BB, Opc, 2).addReg(Tmp1).addReg(Tmp2);
1178}
1179
Chris Lattnera5ade062005-01-11 21:19:59 +00001180/// isFoldableLoad - Return true if this is a load instruction that can safely
1181/// be folded into an operation that uses it.
Chris Lattner44129b52005-01-25 20:03:11 +00001182bool ISel::isFoldableLoad(SDOperand Op, SDOperand OtherOp, bool FloatPromoteOk){
1183 if (Op.getOpcode() == ISD::LOAD) {
1184 // FIXME: currently can't fold constant pool indexes.
1185 if (isa<ConstantPoolSDNode>(Op.getOperand(1)))
1186 return false;
1187 } else if (FloatPromoteOk && Op.getOpcode() == ISD::EXTLOAD &&
1188 cast<MVTSDNode>(Op)->getExtraValueType() == MVT::f32) {
1189 // FIXME: currently can't fold constant pool indexes.
1190 if (isa<ConstantPoolSDNode>(Op.getOperand(1)))
1191 return false;
1192 } else {
Chris Lattnera5ade062005-01-11 21:19:59 +00001193 return false;
Chris Lattner44129b52005-01-25 20:03:11 +00001194 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001195
1196 // If this load has already been emitted, we clearly can't fold it.
Chris Lattner636e79a2005-01-13 05:53:16 +00001197 assert(Op.ResNo == 0 && "Not a use of the value of the load?");
1198 if (ExprMap.count(Op.getValue(1))) return false;
1199 assert(!ExprMap.count(Op.getValue(0)) && "Value in map but not token chain?");
Chris Lattner4a108662005-01-18 03:51:59 +00001200 assert(!ExprMap.count(Op.getValue(1))&&"Token lowered but value not in map?");
Chris Lattnera5ade062005-01-11 21:19:59 +00001201
Chris Lattner4ff348b2005-01-17 06:26:58 +00001202 // If there is not just one use of its value, we cannot fold.
1203 if (!Op.Val->hasNUsesOfValue(1, 0)) return false;
1204
1205 // Finally, we cannot fold the load into the operation if this would induce a
1206 // cycle into the resultant dag. To check for this, see if OtherOp (the other
1207 // operand of the operation we are folding the load into) can possible use the
1208 // chain node defined by the load.
1209 if (OtherOp.Val && !Op.Val->hasNUsesOfValue(0, 1)) { // Has uses of chain?
1210 std::set<SDNode*> Visited;
1211 if (NodeTransitivelyUsesValue(OtherOp, Op.getValue(1), Visited))
1212 return false;
1213 }
1214 return true;
Chris Lattnera5ade062005-01-11 21:19:59 +00001215}
1216
Chris Lattner4ff348b2005-01-17 06:26:58 +00001217
Chris Lattnera5ade062005-01-11 21:19:59 +00001218/// EmitFoldedLoad - Ensure that the arguments of the load are code generated,
1219/// and compute the address being loaded into AM.
1220void ISel::EmitFoldedLoad(SDOperand Op, X86AddressMode &AM) {
1221 SDOperand Chain = Op.getOperand(0);
1222 SDOperand Address = Op.getOperand(1);
Chris Lattner98a8ba02005-01-18 01:06:26 +00001223
Chris Lattnera5ade062005-01-11 21:19:59 +00001224 if (getRegPressure(Chain) > getRegPressure(Address)) {
1225 Select(Chain);
1226 SelectAddress(Address, AM);
1227 } else {
1228 SelectAddress(Address, AM);
1229 Select(Chain);
1230 }
1231
1232 // The chain for this load is now lowered.
Chris Lattner636e79a2005-01-13 05:53:16 +00001233 assert(ExprMap.count(SDOperand(Op.Val, 1)) == 0 &&
1234 "Load emitted more than once?");
Chris Lattner4a108662005-01-18 03:51:59 +00001235 if (!ExprMap.insert(std::make_pair(Op.getValue(1), 1)).second)
Chris Lattner636e79a2005-01-13 05:53:16 +00001236 assert(0 && "Load emitted more than once!");
Chris Lattnera5ade062005-01-11 21:19:59 +00001237}
1238
Chris Lattner30ea1e92005-01-19 07:37:26 +00001239// EmitOrOpOp - Pattern match the expression (Op1|Op2), where we know that op1
1240// and op2 are i8/i16/i32 values with one use each (the or). If we can form a
1241// SHLD or SHRD, emit the instruction (generating the value into DestReg) and
1242// return true.
1243bool ISel::EmitOrOpOp(SDOperand Op1, SDOperand Op2, unsigned DestReg) {
Chris Lattner85716372005-01-19 06:18:43 +00001244 if (Op1.getOpcode() == ISD::SHL && Op2.getOpcode() == ISD::SRL) {
1245 // good!
1246 } else if (Op2.getOpcode() == ISD::SHL && Op1.getOpcode() == ISD::SRL) {
1247 std::swap(Op1, Op2); // Op1 is the SHL now.
1248 } else {
1249 return false; // No match
1250 }
1251
1252 SDOperand ShlVal = Op1.getOperand(0);
1253 SDOperand ShlAmt = Op1.getOperand(1);
1254 SDOperand ShrVal = Op2.getOperand(0);
1255 SDOperand ShrAmt = Op2.getOperand(1);
1256
Chris Lattner30ea1e92005-01-19 07:37:26 +00001257 unsigned RegSize = MVT::getSizeInBits(Op1.getValueType());
1258
Chris Lattner85716372005-01-19 06:18:43 +00001259 // Find out if ShrAmt = 32-ShlAmt or ShlAmt = 32-ShrAmt.
1260 if (ShlAmt.getOpcode() == ISD::SUB && ShlAmt.getOperand(1) == ShrAmt)
1261 if (ConstantSDNode *SubCST = dyn_cast<ConstantSDNode>(ShlAmt.getOperand(0)))
Chris Lattner4053b1e2005-01-19 08:07:05 +00001262 if (SubCST->getValue() == RegSize) {
1263 // (A >> ShrAmt) | (A << (32-ShrAmt)) ==> ROR A, ShrAmt
Chris Lattner85716372005-01-19 06:18:43 +00001264 // (A >> ShrAmt) | (B << (32-ShrAmt)) ==> SHRD A, B, ShrAmt
Chris Lattner4053b1e2005-01-19 08:07:05 +00001265 if (ShrVal == ShlVal) {
1266 unsigned Reg, ShAmt;
1267 if (getRegPressure(ShrVal) > getRegPressure(ShrAmt)) {
1268 Reg = SelectExpr(ShrVal);
1269 ShAmt = SelectExpr(ShrAmt);
1270 } else {
1271 ShAmt = SelectExpr(ShrAmt);
1272 Reg = SelectExpr(ShrVal);
1273 }
1274 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShAmt);
1275 unsigned Opc = RegSize == 8 ? X86::ROR8rCL :
1276 (RegSize == 16 ? X86::ROR16rCL : X86::ROR32rCL);
1277 BuildMI(BB, Opc, 1, DestReg).addReg(Reg);
1278 return true;
1279 } else if (RegSize != 8) {
Chris Lattner85716372005-01-19 06:18:43 +00001280 unsigned AReg, BReg;
1281 if (getRegPressure(ShlVal) > getRegPressure(ShrVal)) {
Chris Lattner85716372005-01-19 06:18:43 +00001282 BReg = SelectExpr(ShlVal);
Chris Lattnerc3c021b2005-01-19 17:24:34 +00001283 AReg = SelectExpr(ShrVal);
Chris Lattner85716372005-01-19 06:18:43 +00001284 } else {
Chris Lattner85716372005-01-19 06:18:43 +00001285 AReg = SelectExpr(ShrVal);
Chris Lattnerc3c021b2005-01-19 17:24:34 +00001286 BReg = SelectExpr(ShlVal);
Chris Lattner85716372005-01-19 06:18:43 +00001287 }
Chris Lattner4053b1e2005-01-19 08:07:05 +00001288 unsigned ShAmt = SelectExpr(ShrAmt);
1289 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShAmt);
1290 unsigned Opc = RegSize == 16 ? X86::SHRD16rrCL : X86::SHRD32rrCL;
1291 BuildMI(BB, Opc, 2, DestReg).addReg(AReg).addReg(BReg);
Chris Lattner85716372005-01-19 06:18:43 +00001292 return true;
1293 }
1294 }
1295
Chris Lattner4053b1e2005-01-19 08:07:05 +00001296 if (ShrAmt.getOpcode() == ISD::SUB && ShrAmt.getOperand(1) == ShlAmt)
1297 if (ConstantSDNode *SubCST = dyn_cast<ConstantSDNode>(ShrAmt.getOperand(0)))
1298 if (SubCST->getValue() == RegSize) {
1299 // (A << ShlAmt) | (A >> (32-ShlAmt)) ==> ROL A, ShrAmt
1300 // (A << ShlAmt) | (B >> (32-ShlAmt)) ==> SHLD A, B, ShrAmt
1301 if (ShrVal == ShlVal) {
1302 unsigned Reg, ShAmt;
1303 if (getRegPressure(ShrVal) > getRegPressure(ShlAmt)) {
1304 Reg = SelectExpr(ShrVal);
1305 ShAmt = SelectExpr(ShlAmt);
1306 } else {
1307 ShAmt = SelectExpr(ShlAmt);
1308 Reg = SelectExpr(ShrVal);
1309 }
1310 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShAmt);
1311 unsigned Opc = RegSize == 8 ? X86::ROL8rCL :
1312 (RegSize == 16 ? X86::ROL16rCL : X86::ROL32rCL);
1313 BuildMI(BB, Opc, 1, DestReg).addReg(Reg);
1314 return true;
1315 } else if (RegSize != 8) {
1316 unsigned AReg, BReg;
1317 if (getRegPressure(ShlVal) > getRegPressure(ShrVal)) {
Chris Lattnerc3c021b2005-01-19 17:24:34 +00001318 AReg = SelectExpr(ShlVal);
1319 BReg = SelectExpr(ShrVal);
Chris Lattner4053b1e2005-01-19 08:07:05 +00001320 } else {
Chris Lattnerc3c021b2005-01-19 17:24:34 +00001321 BReg = SelectExpr(ShrVal);
1322 AReg = SelectExpr(ShlVal);
Chris Lattner4053b1e2005-01-19 08:07:05 +00001323 }
1324 unsigned ShAmt = SelectExpr(ShlAmt);
1325 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShAmt);
1326 unsigned Opc = RegSize == 16 ? X86::SHLD16rrCL : X86::SHLD32rrCL;
1327 BuildMI(BB, Opc, 2, DestReg).addReg(AReg).addReg(BReg);
1328 return true;
1329 }
1330 }
Chris Lattner85716372005-01-19 06:18:43 +00001331
Chris Lattner4053b1e2005-01-19 08:07:05 +00001332 if (ConstantSDNode *ShrCst = dyn_cast<ConstantSDNode>(ShrAmt))
1333 if (ConstantSDNode *ShlCst = dyn_cast<ConstantSDNode>(ShlAmt))
1334 if (ShrCst->getValue() < RegSize && ShlCst->getValue() < RegSize)
1335 if (ShrCst->getValue() == RegSize-ShlCst->getValue()) {
1336 // (A >> 5) | (A << 27) --> ROR A, 5
1337 // (A >> 5) | (B << 27) --> SHRD A, B, 5
1338 if (ShrVal == ShlVal) {
1339 unsigned Reg = SelectExpr(ShrVal);
1340 unsigned Opc = RegSize == 8 ? X86::ROR8ri :
1341 (RegSize == 16 ? X86::ROR16ri : X86::ROR32ri);
1342 BuildMI(BB, Opc, 2, DestReg).addReg(Reg).addImm(ShrCst->getValue());
1343 return true;
1344 } else if (RegSize != 8) {
1345 unsigned AReg, BReg;
1346 if (getRegPressure(ShlVal) > getRegPressure(ShrVal)) {
Chris Lattner4053b1e2005-01-19 08:07:05 +00001347 BReg = SelectExpr(ShlVal);
Chris Lattnerc3c021b2005-01-19 17:24:34 +00001348 AReg = SelectExpr(ShrVal);
Chris Lattner4053b1e2005-01-19 08:07:05 +00001349 } else {
Chris Lattner4053b1e2005-01-19 08:07:05 +00001350 AReg = SelectExpr(ShrVal);
Chris Lattnerc3c021b2005-01-19 17:24:34 +00001351 BReg = SelectExpr(ShlVal);
Chris Lattner4053b1e2005-01-19 08:07:05 +00001352 }
1353 unsigned Opc = RegSize == 16 ? X86::SHRD16rri8 : X86::SHRD32rri8;
1354 BuildMI(BB, Opc, 3, DestReg).addReg(AReg).addReg(BReg)
1355 .addImm(ShrCst->getValue());
1356 return true;
1357 }
1358 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001359
Chris Lattner85716372005-01-19 06:18:43 +00001360 return false;
1361}
1362
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001363unsigned ISel::SelectExpr(SDOperand N) {
1364 unsigned Result;
1365 unsigned Tmp1, Tmp2, Tmp3;
1366 unsigned Opc = 0;
Chris Lattner5188ad72005-01-08 19:28:19 +00001367 SDNode *Node = N.Val;
Chris Lattnera5ade062005-01-11 21:19:59 +00001368 SDOperand Op0, Op1;
Chris Lattner5188ad72005-01-08 19:28:19 +00001369
Chris Lattner7f2afac2005-01-14 22:37:41 +00001370 if (Node->getOpcode() == ISD::CopyFromReg) {
1371 // FIXME: Handle copy from physregs!
1372
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001373 // Just use the specified register as our input.
Chris Lattner18c2f132005-01-13 20:50:02 +00001374 return dyn_cast<RegSDNode>(Node)->getReg();
Chris Lattner7f2afac2005-01-14 22:37:41 +00001375 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001376
Chris Lattnera5ade062005-01-11 21:19:59 +00001377 unsigned &Reg = ExprMap[N];
1378 if (Reg) return Reg;
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001379
Chris Lattnerb38a7492005-04-02 04:01:14 +00001380 switch (N.getOpcode()) {
1381 default:
Chris Lattnera5ade062005-01-11 21:19:59 +00001382 Reg = Result = (N.getValueType() != MVT::Other) ?
Chris Lattnerb38a7492005-04-02 04:01:14 +00001383 MakeReg(N.getValueType()) : 1;
1384 break;
1385 case ISD::CALL:
Chris Lattnera5ade062005-01-11 21:19:59 +00001386 // If this is a call instruction, make sure to prepare ALL of the result
1387 // values as well as the chain.
Chris Lattnerb38a7492005-04-02 04:01:14 +00001388 if (Node->getNumValues() == 1)
1389 Reg = Result = 1; // Void call, just a chain.
1390 else {
Chris Lattnera5ade062005-01-11 21:19:59 +00001391 Result = MakeReg(Node->getValueType(0));
1392 ExprMap[N.getValue(0)] = Result;
Chris Lattnerb38a7492005-04-02 04:01:14 +00001393 for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
Chris Lattnera5ade062005-01-11 21:19:59 +00001394 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
Chris Lattnerb38a7492005-04-02 04:01:14 +00001395 ExprMap[SDOperand(Node, Node->getNumValues()-1)] = 1;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001396 }
Chris Lattnerb38a7492005-04-02 04:01:14 +00001397 break;
1398 case ISD::ADD_PARTS:
1399 case ISD::SUB_PARTS:
1400 case ISD::SHL_PARTS:
1401 case ISD::SRL_PARTS:
1402 case ISD::SRA_PARTS:
1403 Result = MakeReg(Node->getValueType(0));
1404 ExprMap[N.getValue(0)] = Result;
1405 for (unsigned i = 1, e = N.Val->getNumValues(); i != e; ++i)
1406 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
1407 break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001408 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001409
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001410 switch (N.getOpcode()) {
1411 default:
Chris Lattner5188ad72005-01-08 19:28:19 +00001412 Node->dump();
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001413 assert(0 && "Node not handled!\n");
1414 case ISD::FrameIndex:
1415 Tmp1 = cast<FrameIndexSDNode>(N)->getIndex();
1416 addFrameReference(BuildMI(BB, X86::LEA32r, 4, Result), (int)Tmp1);
1417 return Result;
1418 case ISD::ConstantPool:
1419 Tmp1 = cast<ConstantPoolSDNode>(N)->getIndex();
1420 addConstantPoolReference(BuildMI(BB, X86::LEA32r, 4, Result), Tmp1);
1421 return Result;
1422 case ISD::ConstantFP:
1423 ContainsFPCode = true;
1424 Tmp1 = Result; // Intermediate Register
1425 if (cast<ConstantFPSDNode>(N)->getValue() < 0.0 ||
1426 cast<ConstantFPSDNode>(N)->isExactlyValue(-0.0))
1427 Tmp1 = MakeReg(MVT::f64);
1428
1429 if (cast<ConstantFPSDNode>(N)->isExactlyValue(+0.0) ||
1430 cast<ConstantFPSDNode>(N)->isExactlyValue(-0.0))
1431 BuildMI(BB, X86::FLD0, 0, Tmp1);
1432 else if (cast<ConstantFPSDNode>(N)->isExactlyValue(+1.0) ||
1433 cast<ConstantFPSDNode>(N)->isExactlyValue(-1.0))
1434 BuildMI(BB, X86::FLD1, 0, Tmp1);
1435 else
1436 assert(0 && "Unexpected constant!");
1437 if (Tmp1 != Result)
1438 BuildMI(BB, X86::FCHS, 1, Result).addReg(Tmp1);
1439 return Result;
1440 case ISD::Constant:
1441 switch (N.getValueType()) {
1442 default: assert(0 && "Cannot use constants of this type!");
1443 case MVT::i1:
1444 case MVT::i8: Opc = X86::MOV8ri; break;
1445 case MVT::i16: Opc = X86::MOV16ri; break;
1446 case MVT::i32: Opc = X86::MOV32ri; break;
1447 }
1448 BuildMI(BB, Opc, 1,Result).addImm(cast<ConstantSDNode>(N)->getValue());
1449 return Result;
Chris Lattner7ce7eff2005-04-01 22:46:45 +00001450 case ISD::UNDEF:
1451 if (Node->getValueType(0) == MVT::f64) {
1452 // FIXME: SHOULD TEACH STACKIFIER ABOUT UNDEF VALUES!
1453 BuildMI(BB, X86::FLD0, 0, Result);
1454 } else {
1455 BuildMI(BB, X86::IMPLICIT_DEF, 0, Result);
1456 }
1457 return Result;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001458 case ISD::GlobalAddress: {
1459 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
1460 BuildMI(BB, X86::MOV32ri, 1, Result).addGlobalAddress(GV);
1461 return Result;
1462 }
1463 case ISD::ExternalSymbol: {
1464 const char *Sym = cast<ExternalSymbolSDNode>(N)->getSymbol();
1465 BuildMI(BB, X86::MOV32ri, 1, Result).addExternalSymbol(Sym);
1466 return Result;
1467 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001468 case ISD::ZERO_EXTEND: {
1469 int DestIs16 = N.getValueType() == MVT::i16;
1470 int SrcIs16 = N.getOperand(0).getValueType() == MVT::i16;
Chris Lattner590d8002005-01-09 18:52:44 +00001471
1472 // FIXME: This hack is here for zero extension casts from bool to i8. This
1473 // would not be needed if bools were promoted by Legalize.
1474 if (N.getValueType() == MVT::i8) {
Chris Lattnerdbba22f2005-01-11 23:33:00 +00001475 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner590d8002005-01-09 18:52:44 +00001476 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(Tmp1);
1477 return Result;
1478 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001479
Chris Lattner4ff348b2005-01-17 06:26:58 +00001480 if (isFoldableLoad(N.getOperand(0), SDOperand())) {
Chris Lattnerdbba22f2005-01-11 23:33:00 +00001481 static const unsigned Opc[3] = {
1482 X86::MOVZX32rm8, X86::MOVZX32rm16, X86::MOVZX16rm8
1483 };
1484
1485 X86AddressMode AM;
1486 EmitFoldedLoad(N.getOperand(0), AM);
1487 addFullAddress(BuildMI(BB, Opc[SrcIs16+DestIs16*2], 4, Result), AM);
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001488
Chris Lattnerdbba22f2005-01-11 23:33:00 +00001489 return Result;
1490 }
1491
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001492 static const unsigned Opc[3] = {
1493 X86::MOVZX32rr8, X86::MOVZX32rr16, X86::MOVZX16rr8
1494 };
Chris Lattnerdbba22f2005-01-11 23:33:00 +00001495 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001496 BuildMI(BB, Opc[SrcIs16+DestIs16*2], 1, Result).addReg(Tmp1);
1497 return Result;
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001498 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001499 case ISD::SIGN_EXTEND: {
1500 int DestIs16 = N.getValueType() == MVT::i16;
1501 int SrcIs16 = N.getOperand(0).getValueType() == MVT::i16;
1502
Chris Lattner590d8002005-01-09 18:52:44 +00001503 // FIXME: Legalize should promote bools to i8!
1504 assert(N.getOperand(0).getValueType() != MVT::i1 &&
1505 "Sign extend from bool not implemented!");
1506
Chris Lattner4ff348b2005-01-17 06:26:58 +00001507 if (isFoldableLoad(N.getOperand(0), SDOperand())) {
Chris Lattnerdbba22f2005-01-11 23:33:00 +00001508 static const unsigned Opc[3] = {
1509 X86::MOVSX32rm8, X86::MOVSX32rm16, X86::MOVSX16rm8
1510 };
1511
1512 X86AddressMode AM;
1513 EmitFoldedLoad(N.getOperand(0), AM);
1514 addFullAddress(BuildMI(BB, Opc[SrcIs16+DestIs16*2], 4, Result), AM);
1515 return Result;
1516 }
1517
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001518 static const unsigned Opc[3] = {
1519 X86::MOVSX32rr8, X86::MOVSX32rr16, X86::MOVSX16rr8
1520 };
1521 Tmp1 = SelectExpr(N.getOperand(0));
1522 BuildMI(BB, Opc[SrcIs16+DestIs16*2], 1, Result).addReg(Tmp1);
1523 return Result;
1524 }
1525 case ISD::TRUNCATE:
Chris Lattnerafce4302005-01-12 02:19:06 +00001526 // Fold TRUNCATE (LOAD P) into a smaller load from P.
Chris Lattner477c9312005-01-18 20:05:56 +00001527 // FIXME: This should be performed by the DAGCombiner.
Chris Lattner4ff348b2005-01-17 06:26:58 +00001528 if (isFoldableLoad(N.getOperand(0), SDOperand())) {
Chris Lattnerafce4302005-01-12 02:19:06 +00001529 switch (N.getValueType()) {
1530 default: assert(0 && "Unknown truncate!");
1531 case MVT::i1:
1532 case MVT::i8: Opc = X86::MOV8rm; break;
1533 case MVT::i16: Opc = X86::MOV16rm; break;
1534 }
1535 X86AddressMode AM;
1536 EmitFoldedLoad(N.getOperand(0), AM);
1537 addFullAddress(BuildMI(BB, Opc, 4, Result), AM);
1538 return Result;
1539 }
1540
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001541 // Handle cast of LARGER int to SMALLER int using a move to EAX followed by
1542 // a move out of AX or AL.
1543 switch (N.getOperand(0).getValueType()) {
1544 default: assert(0 && "Unknown truncate!");
1545 case MVT::i8: Tmp2 = X86::AL; Opc = X86::MOV8rr; break;
1546 case MVT::i16: Tmp2 = X86::AX; Opc = X86::MOV16rr; break;
1547 case MVT::i32: Tmp2 = X86::EAX; Opc = X86::MOV32rr; break;
1548 }
1549 Tmp1 = SelectExpr(N.getOperand(0));
1550 BuildMI(BB, Opc, 1, Tmp2).addReg(Tmp1);
1551
1552 switch (N.getValueType()) {
1553 default: assert(0 && "Unknown truncate!");
1554 case MVT::i1:
1555 case MVT::i8: Tmp2 = X86::AL; Opc = X86::MOV8rr; break;
1556 case MVT::i16: Tmp2 = X86::AX; Opc = X86::MOV16rr; break;
1557 }
1558 BuildMI(BB, Opc, 1, Result).addReg(Tmp2);
1559 return Result;
1560
Chris Lattner590d8002005-01-09 18:52:44 +00001561 case ISD::SINT_TO_FP:
1562 case ISD::UINT_TO_FP: {
1563 // FIXME: Most of this grunt work should be done by legalize!
Chris Lattneref7ba072005-01-11 03:50:45 +00001564 ContainsFPCode = true;
Chris Lattner590d8002005-01-09 18:52:44 +00001565
1566 // Promote the integer to a type supported by FLD. We do this because there
1567 // are no unsigned FLD instructions, so we must promote an unsigned value to
1568 // a larger signed value, then use FLD on the larger value.
1569 //
1570 MVT::ValueType PromoteType = MVT::Other;
1571 MVT::ValueType SrcTy = N.getOperand(0).getValueType();
1572 unsigned PromoteOpcode = 0;
1573 unsigned RealDestReg = Result;
1574 switch (SrcTy) {
1575 case MVT::i1:
1576 case MVT::i8:
1577 // We don't have the facilities for directly loading byte sized data from
1578 // memory (even signed). Promote it to 16 bits.
1579 PromoteType = MVT::i16;
1580 PromoteOpcode = Node->getOpcode() == ISD::SINT_TO_FP ?
1581 X86::MOVSX16rr8 : X86::MOVZX16rr8;
1582 break;
1583 case MVT::i16:
1584 if (Node->getOpcode() == ISD::UINT_TO_FP) {
1585 PromoteType = MVT::i32;
1586 PromoteOpcode = X86::MOVZX32rr16;
1587 }
1588 break;
1589 default:
1590 // Don't fild into the real destination.
1591 if (Node->getOpcode() == ISD::UINT_TO_FP)
1592 Result = MakeReg(Node->getValueType(0));
1593 break;
1594 }
1595
1596 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001597
Chris Lattner590d8002005-01-09 18:52:44 +00001598 if (PromoteType != MVT::Other) {
1599 Tmp2 = MakeReg(PromoteType);
1600 BuildMI(BB, PromoteOpcode, 1, Tmp2).addReg(Tmp1);
1601 SrcTy = PromoteType;
1602 Tmp1 = Tmp2;
1603 }
1604
1605 // Spill the integer to memory and reload it from there.
1606 unsigned Size = MVT::getSizeInBits(SrcTy)/8;
1607 MachineFunction *F = BB->getParent();
1608 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, Size);
1609
1610 switch (SrcTy) {
Chris Lattner590d8002005-01-09 18:52:44 +00001611 case MVT::i32:
1612 addFrameReference(BuildMI(BB, X86::MOV32mr, 5),
1613 FrameIdx).addReg(Tmp1);
1614 addFrameReference(BuildMI(BB, X86::FILD32m, 5, Result), FrameIdx);
1615 break;
1616 case MVT::i16:
1617 addFrameReference(BuildMI(BB, X86::MOV16mr, 5),
1618 FrameIdx).addReg(Tmp1);
1619 addFrameReference(BuildMI(BB, X86::FILD16m, 5, Result), FrameIdx);
1620 break;
1621 default: break; // No promotion required.
1622 }
1623
Chris Lattner085c9952005-01-12 04:00:00 +00001624 if (Node->getOpcode() == ISD::UINT_TO_FP && Result != RealDestReg) {
Chris Lattner590d8002005-01-09 18:52:44 +00001625 // If this is a cast from uint -> double, we need to be careful when if
1626 // the "sign" bit is set. If so, we don't want to make a negative number,
1627 // we want to make a positive number. Emit code to add an offset if the
1628 // sign bit is set.
1629
1630 // Compute whether the sign bit is set by shifting the reg right 31 bits.
1631 unsigned IsNeg = MakeReg(MVT::i32);
1632 BuildMI(BB, X86::SHR32ri, 2, IsNeg).addReg(Tmp1).addImm(31);
1633
1634 // Create a CP value that has the offset in one word and 0 in the other.
1635 static ConstantInt *TheOffset = ConstantUInt::get(Type::ULongTy,
1636 0x4f80000000000000ULL);
1637 unsigned CPI = F->getConstantPool()->getConstantPoolIndex(TheOffset);
1638 BuildMI(BB, X86::FADD32m, 5, RealDestReg).addReg(Result)
1639 .addConstantPoolIndex(CPI).addZImm(4).addReg(IsNeg).addSImm(0);
Chris Lattner590d8002005-01-09 18:52:44 +00001640 }
1641 return RealDestReg;
1642 }
1643 case ISD::FP_TO_SINT:
1644 case ISD::FP_TO_UINT: {
1645 // FIXME: Most of this grunt work should be done by legalize!
1646 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
1647
1648 // Change the floating point control register to use "round towards zero"
1649 // mode when truncating to an integer value.
1650 //
1651 MachineFunction *F = BB->getParent();
1652 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
1653 addFrameReference(BuildMI(BB, X86::FNSTCW16m, 4), CWFrameIdx);
1654
1655 // Load the old value of the high byte of the control word...
1656 unsigned HighPartOfCW = MakeReg(MVT::i8);
1657 addFrameReference(BuildMI(BB, X86::MOV8rm, 4, HighPartOfCW),
1658 CWFrameIdx, 1);
1659
1660 // Set the high part to be round to zero...
1661 addFrameReference(BuildMI(BB, X86::MOV8mi, 5),
1662 CWFrameIdx, 1).addImm(12);
1663
1664 // Reload the modified control word now...
1665 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001666
Chris Lattner590d8002005-01-09 18:52:44 +00001667 // Restore the memory image of control word to original value
1668 addFrameReference(BuildMI(BB, X86::MOV8mr, 5),
1669 CWFrameIdx, 1).addReg(HighPartOfCW);
1670
1671 // We don't have the facilities for directly storing byte sized data to
1672 // memory. Promote it to 16 bits. We also must promote unsigned values to
1673 // larger classes because we only have signed FP stores.
1674 MVT::ValueType StoreClass = Node->getValueType(0);
1675 if (StoreClass == MVT::i8 || Node->getOpcode() == ISD::FP_TO_UINT)
1676 switch (StoreClass) {
Chris Lattner2afa1912005-05-09 05:33:18 +00001677 case MVT::i1:
Chris Lattner590d8002005-01-09 18:52:44 +00001678 case MVT::i8: StoreClass = MVT::i16; break;
1679 case MVT::i16: StoreClass = MVT::i32; break;
1680 case MVT::i32: StoreClass = MVT::i64; break;
Chris Lattner590d8002005-01-09 18:52:44 +00001681 default: assert(0 && "Unknown store class!");
1682 }
1683
1684 // Spill the integer to memory and reload it from there.
1685 unsigned Size = MVT::getSizeInBits(StoreClass)/8;
1686 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, Size);
1687
1688 switch (StoreClass) {
1689 default: assert(0 && "Unknown store class!");
1690 case MVT::i16:
1691 addFrameReference(BuildMI(BB, X86::FIST16m, 5), FrameIdx).addReg(Tmp1);
1692 break;
1693 case MVT::i32:
Chris Lattner25020852005-01-09 19:49:59 +00001694 addFrameReference(BuildMI(BB, X86::FIST32m, 5), FrameIdx).addReg(Tmp1);
Chris Lattner590d8002005-01-09 18:52:44 +00001695 break;
Chris Lattnera0dbf182005-05-09 18:37:02 +00001696 case MVT::i64:
1697 addFrameReference(BuildMI(BB, X86::FISTP64m, 5), FrameIdx).addReg(Tmp1);
1698 break; }
Chris Lattner590d8002005-01-09 18:52:44 +00001699
1700 switch (Node->getValueType(0)) {
1701 default:
1702 assert(0 && "Unknown integer type!");
Chris Lattner590d8002005-01-09 18:52:44 +00001703 case MVT::i32:
1704 addFrameReference(BuildMI(BB, X86::MOV32rm, 4, Result), FrameIdx);
1705 break;
1706 case MVT::i16:
1707 addFrameReference(BuildMI(BB, X86::MOV16rm, 4, Result), FrameIdx);
1708 break;
1709 case MVT::i8:
Chris Lattner2afa1912005-05-09 05:33:18 +00001710 case MVT::i1:
Chris Lattner590d8002005-01-09 18:52:44 +00001711 addFrameReference(BuildMI(BB, X86::MOV8rm, 4, Result), FrameIdx);
1712 break;
1713 }
1714
1715 // Reload the original control word now.
1716 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1717 return Result;
1718 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001719 case ISD::ADD:
Chris Lattnera5ade062005-01-11 21:19:59 +00001720 Op0 = N.getOperand(0);
1721 Op1 = N.getOperand(1);
1722
Chris Lattner44129b52005-01-25 20:03:11 +00001723 if (isFoldableLoad(Op0, Op1, true)) {
Chris Lattnera5ade062005-01-11 21:19:59 +00001724 std::swap(Op0, Op1);
Chris Lattner4ff348b2005-01-17 06:26:58 +00001725 goto FoldAdd;
1726 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001727
Chris Lattner44129b52005-01-25 20:03:11 +00001728 if (isFoldableLoad(Op1, Op0, true)) {
Chris Lattner4ff348b2005-01-17 06:26:58 +00001729 FoldAdd:
Chris Lattnera5ade062005-01-11 21:19:59 +00001730 switch (N.getValueType()) {
1731 default: assert(0 && "Cannot add this type!");
1732 case MVT::i1:
1733 case MVT::i8: Opc = X86::ADD8rm; break;
1734 case MVT::i16: Opc = X86::ADD16rm; break;
1735 case MVT::i32: Opc = X86::ADD32rm; break;
Chris Lattner44129b52005-01-25 20:03:11 +00001736 case MVT::f64:
1737 // For F64, handle promoted load operations (from F32) as well!
1738 Opc = Op1.getOpcode() == ISD::LOAD ? X86::FADD64m : X86::FADD32m;
1739 break;
Chris Lattnera5ade062005-01-11 21:19:59 +00001740 }
1741 X86AddressMode AM;
Chris Lattner636e79a2005-01-13 05:53:16 +00001742 EmitFoldedLoad(Op1, AM);
1743 Tmp1 = SelectExpr(Op0);
Chris Lattnera5ade062005-01-11 21:19:59 +00001744 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
1745 return Result;
1746 }
1747
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001748 // See if we can codegen this as an LEA to fold operations together.
1749 if (N.getValueType() == MVT::i32) {
Chris Lattner883c86f2005-01-18 02:25:52 +00001750 ExprMap.erase(N);
Chris Lattner98a8ba02005-01-18 01:06:26 +00001751 X86ISelAddressMode AM;
Chris Lattner883c86f2005-01-18 02:25:52 +00001752 MatchAddress(N, AM);
1753 ExprMap[N] = Result;
1754
1755 // If this is not just an add, emit the LEA. For a simple add (like
1756 // reg+reg or reg+imm), we just emit an add. It might be a good idea to
1757 // leave this as LEA, then peephole it to 'ADD' after two address elim
1758 // happens.
1759 if (AM.Scale != 1 || AM.BaseType == X86ISelAddressMode::FrameIndexBase||
1760 AM.GV || (AM.Base.Reg.Val && AM.IndexReg.Val && AM.Disp)) {
1761 X86AddressMode XAM = SelectAddrExprs(AM);
1762 addFullAddress(BuildMI(BB, X86::LEA32r, 4, Result), XAM);
1763 return Result;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001764 }
1765 }
Chris Lattner11333092005-01-11 03:11:44 +00001766
Chris Lattnera5ade062005-01-11 21:19:59 +00001767 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1)) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001768 Opc = 0;
1769 if (CN->getValue() == 1) { // add X, 1 -> inc X
1770 switch (N.getValueType()) {
1771 default: assert(0 && "Cannot integer add this type!");
1772 case MVT::i8: Opc = X86::INC8r; break;
1773 case MVT::i16: Opc = X86::INC16r; break;
1774 case MVT::i32: Opc = X86::INC32r; break;
1775 }
1776 } else if (CN->isAllOnesValue()) { // add X, -1 -> dec X
1777 switch (N.getValueType()) {
1778 default: assert(0 && "Cannot integer add this type!");
1779 case MVT::i8: Opc = X86::DEC8r; break;
1780 case MVT::i16: Opc = X86::DEC16r; break;
1781 case MVT::i32: Opc = X86::DEC32r; break;
1782 }
1783 }
1784
1785 if (Opc) {
Chris Lattnera5ade062005-01-11 21:19:59 +00001786 Tmp1 = SelectExpr(Op0);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001787 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1788 return Result;
1789 }
1790
1791 switch (N.getValueType()) {
1792 default: assert(0 && "Cannot add this type!");
1793 case MVT::i8: Opc = X86::ADD8ri; break;
1794 case MVT::i16: Opc = X86::ADD16ri; break;
1795 case MVT::i32: Opc = X86::ADD32ri; break;
1796 }
1797 if (Opc) {
Chris Lattnera5ade062005-01-11 21:19:59 +00001798 Tmp1 = SelectExpr(Op0);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001799 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
1800 return Result;
1801 }
1802 }
1803
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001804 switch (N.getValueType()) {
1805 default: assert(0 && "Cannot add this type!");
1806 case MVT::i8: Opc = X86::ADD8rr; break;
1807 case MVT::i16: Opc = X86::ADD16rr; break;
1808 case MVT::i32: Opc = X86::ADD32rr; break;
Chris Lattneref7ba072005-01-11 03:50:45 +00001809 case MVT::f64: Opc = X86::FpADD; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001810 }
Chris Lattner11333092005-01-11 03:11:44 +00001811
Chris Lattnera5ade062005-01-11 21:19:59 +00001812 if (getRegPressure(Op0) > getRegPressure(Op1)) {
1813 Tmp1 = SelectExpr(Op0);
1814 Tmp2 = SelectExpr(Op1);
Chris Lattner11333092005-01-11 03:11:44 +00001815 } else {
Chris Lattnera5ade062005-01-11 21:19:59 +00001816 Tmp2 = SelectExpr(Op1);
1817 Tmp1 = SelectExpr(Op0);
Chris Lattner11333092005-01-11 03:11:44 +00001818 }
1819
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001820 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1821 return Result;
Chris Lattnerb7edaa12005-04-02 05:30:17 +00001822
1823 case ISD::FABS:
Chris Lattnerb7edaa12005-04-02 05:30:17 +00001824 case ISD::FNEG:
Chris Lattnerc5dcb532005-04-30 04:25:35 +00001825 case ISD::FSIN:
1826 case ISD::FCOS:
Chris Lattner2c56e8a2005-04-28 22:07:18 +00001827 case ISD::FSQRT:
1828 assert(N.getValueType()==MVT::f64 && "Illegal type for this operation");
Chris Lattnerb7edaa12005-04-02 05:30:17 +00001829 Tmp1 = SelectExpr(Node->getOperand(0));
Chris Lattner2c56e8a2005-04-28 22:07:18 +00001830 switch (N.getOpcode()) {
1831 default: assert(0 && "Unreachable!");
1832 case ISD::FABS: BuildMI(BB, X86::FABS, 1, Result).addReg(Tmp1); break;
1833 case ISD::FNEG: BuildMI(BB, X86::FCHS, 1, Result).addReg(Tmp1); break;
1834 case ISD::FSQRT: BuildMI(BB, X86::FSQRT, 1, Result).addReg(Tmp1); break;
Chris Lattnerc5dcb532005-04-30 04:25:35 +00001835 case ISD::FSIN: BuildMI(BB, X86::FSIN, 1, Result).addReg(Tmp1); break;
1836 case ISD::FCOS: BuildMI(BB, X86::FCOS, 1, Result).addReg(Tmp1); break;
Chris Lattner2c56e8a2005-04-28 22:07:18 +00001837 }
Chris Lattnerb7edaa12005-04-02 05:30:17 +00001838 return Result;
1839
Chris Lattner8db0af12005-04-06 04:21:07 +00001840 case ISD::MULHU:
1841 switch (N.getValueType()) {
1842 default: assert(0 && "Unsupported VT!");
1843 case MVT::i8: Tmp2 = X86::MUL8r; break;
1844 case MVT::i16: Tmp2 = X86::MUL16r; break;
1845 case MVT::i32: Tmp2 = X86::MUL32r; break;
1846 }
1847 // FALL THROUGH
1848 case ISD::MULHS: {
1849 unsigned MovOpc, LowReg, HiReg;
1850 switch (N.getValueType()) {
1851 default: assert(0 && "Unsupported VT!");
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001852 case MVT::i8:
Chris Lattner8db0af12005-04-06 04:21:07 +00001853 MovOpc = X86::MOV8rr;
1854 LowReg = X86::AL;
1855 HiReg = X86::AH;
1856 Opc = X86::IMUL8r;
1857 break;
1858 case MVT::i16:
1859 MovOpc = X86::MOV16rr;
1860 LowReg = X86::AX;
1861 HiReg = X86::DX;
1862 Opc = X86::IMUL16r;
1863 break;
1864 case MVT::i32:
1865 MovOpc = X86::MOV32rr;
1866 LowReg = X86::EAX;
1867 HiReg = X86::EDX;
1868 Opc = X86::IMUL32r;
1869 break;
1870 }
1871 if (Node->getOpcode() != ISD::MULHS)
1872 Opc = Tmp2; // Get the MULHU opcode.
1873
1874 Op0 = Node->getOperand(0);
1875 Op1 = Node->getOperand(1);
1876 if (getRegPressure(Op0) > getRegPressure(Op1)) {
1877 Tmp1 = SelectExpr(Op0);
1878 Tmp2 = SelectExpr(Op1);
1879 } else {
1880 Tmp2 = SelectExpr(Op1);
1881 Tmp1 = SelectExpr(Op0);
1882 }
1883
1884 // FIXME: Implement folding of loads into the memory operands here!
1885 BuildMI(BB, MovOpc, 1, LowReg).addReg(Tmp1);
1886 BuildMI(BB, Opc, 1).addReg(Tmp2);
1887 BuildMI(BB, MovOpc, 1, Result).addReg(HiReg);
1888 return Result;
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001889 }
Chris Lattner8db0af12005-04-06 04:21:07 +00001890
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001891 case ISD::SUB:
Chris Lattnera5ade062005-01-11 21:19:59 +00001892 case ISD::MUL:
1893 case ISD::AND:
1894 case ISD::OR:
Chris Lattnera56cea42005-01-12 04:23:22 +00001895 case ISD::XOR: {
Chris Lattnera5ade062005-01-11 21:19:59 +00001896 static const unsigned SUBTab[] = {
1897 X86::SUB8ri, X86::SUB16ri, X86::SUB32ri, 0, 0,
1898 X86::SUB8rm, X86::SUB16rm, X86::SUB32rm, X86::FSUB32m, X86::FSUB64m,
1899 X86::SUB8rr, X86::SUB16rr, X86::SUB32rr, X86::FpSUB , X86::FpSUB,
1900 };
1901 static const unsigned MULTab[] = {
1902 0, X86::IMUL16rri, X86::IMUL32rri, 0, 0,
1903 0, X86::IMUL16rm , X86::IMUL32rm, X86::FMUL32m, X86::FMUL64m,
1904 0, X86::IMUL16rr , X86::IMUL32rr, X86::FpMUL , X86::FpMUL,
1905 };
1906 static const unsigned ANDTab[] = {
1907 X86::AND8ri, X86::AND16ri, X86::AND32ri, 0, 0,
1908 X86::AND8rm, X86::AND16rm, X86::AND32rm, 0, 0,
Misha Brukman0e0a7a452005-04-21 23:38:14 +00001909 X86::AND8rr, X86::AND16rr, X86::AND32rr, 0, 0,
Chris Lattnera5ade062005-01-11 21:19:59 +00001910 };
1911 static const unsigned ORTab[] = {
1912 X86::OR8ri, X86::OR16ri, X86::OR32ri, 0, 0,
1913 X86::OR8rm, X86::OR16rm, X86::OR32rm, 0, 0,
1914 X86::OR8rr, X86::OR16rr, X86::OR32rr, 0, 0,
1915 };
1916 static const unsigned XORTab[] = {
1917 X86::XOR8ri, X86::XOR16ri, X86::XOR32ri, 0, 0,
1918 X86::XOR8rm, X86::XOR16rm, X86::XOR32rm, 0, 0,
1919 X86::XOR8rr, X86::XOR16rr, X86::XOR32rr, 0, 0,
1920 };
1921
1922 Op0 = Node->getOperand(0);
1923 Op1 = Node->getOperand(1);
1924
Chris Lattner30ea1e92005-01-19 07:37:26 +00001925 if (Node->getOpcode() == ISD::OR && Op0.hasOneUse() && Op1.hasOneUse())
1926 if (EmitOrOpOp(Op0, Op1, Result)) // Match SHLD, SHRD, and rotates.
Chris Lattner85716372005-01-19 06:18:43 +00001927 return Result;
1928
1929 if (Node->getOpcode() == ISD::SUB)
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001930 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(0)))
1931 if (CN->isNullValue()) { // 0 - N -> neg N
1932 switch (N.getValueType()) {
1933 default: assert(0 && "Cannot sub this type!");
1934 case MVT::i1:
1935 case MVT::i8: Opc = X86::NEG8r; break;
1936 case MVT::i16: Opc = X86::NEG16r; break;
1937 case MVT::i32: Opc = X86::NEG32r; break;
1938 }
1939 Tmp1 = SelectExpr(N.getOperand(1));
1940 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1941 return Result;
1942 }
1943
Chris Lattnera5ade062005-01-11 21:19:59 +00001944 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1)) {
1945 if (CN->isAllOnesValue() && Node->getOpcode() == ISD::XOR) {
Chris Lattnerc98279d2005-01-17 00:23:16 +00001946 Opc = 0;
Chris Lattnerd4dab922005-01-11 04:31:30 +00001947 switch (N.getValueType()) {
1948 default: assert(0 && "Cannot add this type!");
Chris Lattnerc98279d2005-01-17 00:23:16 +00001949 case MVT::i1: break; // Not supported, don't invert upper bits!
Chris Lattnerd4dab922005-01-11 04:31:30 +00001950 case MVT::i8: Opc = X86::NOT8r; break;
1951 case MVT::i16: Opc = X86::NOT16r; break;
1952 case MVT::i32: Opc = X86::NOT32r; break;
1953 }
Chris Lattnerc98279d2005-01-17 00:23:16 +00001954 if (Opc) {
1955 Tmp1 = SelectExpr(Op0);
1956 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1957 return Result;
1958 }
Chris Lattnerd4dab922005-01-11 04:31:30 +00001959 }
1960
Chris Lattner2a4e5082005-01-17 06:48:02 +00001961 // Fold common multiplies into LEA instructions.
1962 if (Node->getOpcode() == ISD::MUL && N.getValueType() == MVT::i32) {
1963 switch ((int)CN->getValue()) {
1964 default: break;
1965 case 3:
1966 case 5:
1967 case 9:
Chris Lattner2a4e5082005-01-17 06:48:02 +00001968 // Remove N from exprmap so SelectAddress doesn't get confused.
1969 ExprMap.erase(N);
Chris Lattner98a8ba02005-01-18 01:06:26 +00001970 X86AddressMode AM;
Chris Lattner2a4e5082005-01-17 06:48:02 +00001971 SelectAddress(N, AM);
1972 // Restore it to the map.
1973 ExprMap[N] = Result;
1974 addFullAddress(BuildMI(BB, X86::LEA32r, 4, Result), AM);
1975 return Result;
1976 }
1977 }
1978
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001979 switch (N.getValueType()) {
Chris Lattnerd4dab922005-01-11 04:31:30 +00001980 default: assert(0 && "Cannot xor this type!");
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001981 case MVT::i1:
Chris Lattnera5ade062005-01-11 21:19:59 +00001982 case MVT::i8: Opc = 0; break;
1983 case MVT::i16: Opc = 1; break;
1984 case MVT::i32: Opc = 2; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001985 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001986 switch (Node->getOpcode()) {
1987 default: assert(0 && "Unreachable!");
1988 case ISD::SUB: Opc = SUBTab[Opc]; break;
1989 case ISD::MUL: Opc = MULTab[Opc]; break;
1990 case ISD::AND: Opc = ANDTab[Opc]; break;
1991 case ISD::OR: Opc = ORTab[Opc]; break;
1992 case ISD::XOR: Opc = XORTab[Opc]; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001993 }
Chris Lattnera5ade062005-01-11 21:19:59 +00001994 if (Opc) { // Can't fold MUL:i8 R, imm
1995 Tmp1 = SelectExpr(Op0);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00001996 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
1997 return Result;
1998 }
1999 }
Chris Lattner11333092005-01-11 03:11:44 +00002000
Chris Lattner44129b52005-01-25 20:03:11 +00002001 if (isFoldableLoad(Op0, Op1, true))
Chris Lattnera5ade062005-01-11 21:19:59 +00002002 if (Node->getOpcode() != ISD::SUB) {
2003 std::swap(Op0, Op1);
Chris Lattner4ff348b2005-01-17 06:26:58 +00002004 goto FoldOps;
Chris Lattnera5ade062005-01-11 21:19:59 +00002005 } else {
Chris Lattner44129b52005-01-25 20:03:11 +00002006 // For FP, emit 'reverse' subract, with a memory operand.
2007 if (N.getValueType() == MVT::f64) {
2008 if (Op0.getOpcode() == ISD::EXTLOAD)
2009 Opc = X86::FSUBR32m;
2010 else
2011 Opc = X86::FSUBR64m;
2012
Chris Lattnera5ade062005-01-11 21:19:59 +00002013 X86AddressMode AM;
Chris Lattner636e79a2005-01-13 05:53:16 +00002014 EmitFoldedLoad(Op0, AM);
2015 Tmp1 = SelectExpr(Op1);
Chris Lattnera5ade062005-01-11 21:19:59 +00002016 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
2017 return Result;
2018 }
2019 }
2020
Chris Lattner44129b52005-01-25 20:03:11 +00002021 if (isFoldableLoad(Op1, Op0, true)) {
Chris Lattner4ff348b2005-01-17 06:26:58 +00002022 FoldOps:
Chris Lattnera5ade062005-01-11 21:19:59 +00002023 switch (N.getValueType()) {
2024 default: assert(0 && "Cannot operate on this type!");
2025 case MVT::i1:
2026 case MVT::i8: Opc = 5; break;
2027 case MVT::i16: Opc = 6; break;
2028 case MVT::i32: Opc = 7; break;
Chris Lattner44129b52005-01-25 20:03:11 +00002029 // For F64, handle promoted load operations (from F32) as well!
2030 case MVT::f64: Opc = Op1.getOpcode() == ISD::LOAD ? 9 : 8; break;
Chris Lattnera5ade062005-01-11 21:19:59 +00002031 }
2032 switch (Node->getOpcode()) {
2033 default: assert(0 && "Unreachable!");
2034 case ISD::SUB: Opc = SUBTab[Opc]; break;
2035 case ISD::MUL: Opc = MULTab[Opc]; break;
2036 case ISD::AND: Opc = ANDTab[Opc]; break;
2037 case ISD::OR: Opc = ORTab[Opc]; break;
2038 case ISD::XOR: Opc = XORTab[Opc]; break;
2039 }
2040
2041 X86AddressMode AM;
Chris Lattner636e79a2005-01-13 05:53:16 +00002042 EmitFoldedLoad(Op1, AM);
2043 Tmp1 = SelectExpr(Op0);
Chris Lattnera5ade062005-01-11 21:19:59 +00002044 if (Opc) {
2045 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
2046 } else {
2047 assert(Node->getOpcode() == ISD::MUL &&
2048 N.getValueType() == MVT::i8 && "Unexpected situation!");
2049 // Must use the MUL instruction, which forces use of AL.
2050 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(Tmp1);
2051 addFullAddress(BuildMI(BB, X86::MUL8m, 1), AM);
2052 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
2053 }
2054 return Result;
Chris Lattner11333092005-01-11 03:11:44 +00002055 }
Chris Lattnera5ade062005-01-11 21:19:59 +00002056
2057 if (getRegPressure(Op0) > getRegPressure(Op1)) {
2058 Tmp1 = SelectExpr(Op0);
2059 Tmp2 = SelectExpr(Op1);
2060 } else {
2061 Tmp2 = SelectExpr(Op1);
2062 Tmp1 = SelectExpr(Op0);
2063 }
2064
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002065 switch (N.getValueType()) {
2066 default: assert(0 && "Cannot add this type!");
Chris Lattnera5ade062005-01-11 21:19:59 +00002067 case MVT::i1:
2068 case MVT::i8: Opc = 10; break;
2069 case MVT::i16: Opc = 11; break;
2070 case MVT::i32: Opc = 12; break;
2071 case MVT::f32: Opc = 13; break;
2072 case MVT::f64: Opc = 14; break;
2073 }
2074 switch (Node->getOpcode()) {
2075 default: assert(0 && "Unreachable!");
2076 case ISD::SUB: Opc = SUBTab[Opc]; break;
2077 case ISD::MUL: Opc = MULTab[Opc]; break;
2078 case ISD::AND: Opc = ANDTab[Opc]; break;
2079 case ISD::OR: Opc = ORTab[Opc]; break;
2080 case ISD::XOR: Opc = XORTab[Opc]; break;
2081 }
2082 if (Opc) {
2083 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
2084 } else {
2085 assert(Node->getOpcode() == ISD::MUL &&
2086 N.getValueType() == MVT::i8 && "Unexpected situation!");
Chris Lattnera13d3232005-01-10 20:55:48 +00002087 // Must use the MUL instruction, which forces use of AL.
2088 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(Tmp1);
2089 BuildMI(BB, X86::MUL8r, 1).addReg(Tmp2);
2090 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002091 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002092 return Result;
Chris Lattnera56cea42005-01-12 04:23:22 +00002093 }
Chris Lattner19ad0622005-01-20 18:53:00 +00002094 case ISD::ADD_PARTS:
2095 case ISD::SUB_PARTS: {
2096 assert(N.getNumOperands() == 4 && N.getValueType() == MVT::i32 &&
2097 "Not an i64 add/sub!");
2098 // Emit all of the operands.
2099 std::vector<unsigned> InVals;
2100 for (unsigned i = 0, e = N.getNumOperands(); i != e; ++i)
2101 InVals.push_back(SelectExpr(N.getOperand(i)));
2102 if (N.getOpcode() == ISD::ADD_PARTS) {
2103 BuildMI(BB, X86::ADD32rr, 2, Result).addReg(InVals[0]).addReg(InVals[2]);
2104 BuildMI(BB, X86::ADC32rr,2,Result+1).addReg(InVals[1]).addReg(InVals[3]);
2105 } else {
2106 BuildMI(BB, X86::SUB32rr, 2, Result).addReg(InVals[0]).addReg(InVals[2]);
2107 BuildMI(BB, X86::SBB32rr, 2,Result+1).addReg(InVals[1]).addReg(InVals[3]);
2108 }
2109 return Result+N.ResNo;
2110 }
2111
Chris Lattnerb38a7492005-04-02 04:01:14 +00002112 case ISD::SHL_PARTS:
2113 case ISD::SRA_PARTS:
2114 case ISD::SRL_PARTS: {
2115 assert(N.getNumOperands() == 3 && N.getValueType() == MVT::i32 &&
2116 "Not an i64 shift!");
2117 unsigned ShiftOpLo = SelectExpr(N.getOperand(0));
2118 unsigned ShiftOpHi = SelectExpr(N.getOperand(1));
2119 unsigned TmpReg = MakeReg(MVT::i32);
2120 if (N.getOpcode() == ISD::SRA_PARTS) {
2121 // If this is a SHR of a Long, then we need to do funny sign extension
2122 // stuff. TmpReg gets the value to use as the high-part if we are
2123 // shifting more than 32 bits.
2124 BuildMI(BB, X86::SAR32ri, 2, TmpReg).addReg(ShiftOpHi).addImm(31);
2125 } else {
2126 // Other shifts use a fixed zero value if the shift is more than 32 bits.
2127 BuildMI(BB, X86::MOV32ri, 1, TmpReg).addImm(0);
2128 }
2129
2130 // Initialize CL with the shift amount.
2131 unsigned ShiftAmountReg = SelectExpr(N.getOperand(2));
2132 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(ShiftAmountReg);
2133
2134 unsigned TmpReg2 = MakeReg(MVT::i32);
2135 unsigned TmpReg3 = MakeReg(MVT::i32);
2136 if (N.getOpcode() == ISD::SHL_PARTS) {
2137 // TmpReg2 = shld inHi, inLo
2138 BuildMI(BB, X86::SHLD32rrCL, 2,TmpReg2).addReg(ShiftOpHi)
2139 .addReg(ShiftOpLo);
2140 // TmpReg3 = shl inLo, CL
2141 BuildMI(BB, X86::SHL32rCL, 1, TmpReg3).addReg(ShiftOpLo);
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002142
Chris Lattnerb38a7492005-04-02 04:01:14 +00002143 // Set the flags to indicate whether the shift was by more than 32 bits.
2144 BuildMI(BB, X86::TEST8ri, 2).addReg(X86::CL).addImm(32);
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002145
Chris Lattnerb38a7492005-04-02 04:01:14 +00002146 // DestHi = (>32) ? TmpReg3 : TmpReg2;
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002147 BuildMI(BB, X86::CMOVNE32rr, 2,
Chris Lattnerb38a7492005-04-02 04:01:14 +00002148 Result+1).addReg(TmpReg2).addReg(TmpReg3);
2149 // DestLo = (>32) ? TmpReg : TmpReg3;
2150 BuildMI(BB, X86::CMOVNE32rr, 2,
2151 Result).addReg(TmpReg3).addReg(TmpReg);
2152 } else {
2153 // TmpReg2 = shrd inLo, inHi
2154 BuildMI(BB, X86::SHRD32rrCL,2,TmpReg2).addReg(ShiftOpLo)
2155 .addReg(ShiftOpHi);
2156 // TmpReg3 = s[ah]r inHi, CL
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002157 BuildMI(BB, N.getOpcode() == ISD::SRA_PARTS ? X86::SAR32rCL
Chris Lattnerb38a7492005-04-02 04:01:14 +00002158 : X86::SHR32rCL, 1, TmpReg3)
2159 .addReg(ShiftOpHi);
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002160
Chris Lattnerb38a7492005-04-02 04:01:14 +00002161 // Set the flags to indicate whether the shift was by more than 32 bits.
2162 BuildMI(BB, X86::TEST8ri, 2).addReg(X86::CL).addImm(32);
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002163
Chris Lattnerb38a7492005-04-02 04:01:14 +00002164 // DestLo = (>32) ? TmpReg3 : TmpReg2;
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002165 BuildMI(BB, X86::CMOVNE32rr, 2,
Chris Lattnerb38a7492005-04-02 04:01:14 +00002166 Result).addReg(TmpReg2).addReg(TmpReg3);
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002167
Chris Lattnerb38a7492005-04-02 04:01:14 +00002168 // DestHi = (>32) ? TmpReg : TmpReg3;
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002169 BuildMI(BB, X86::CMOVNE32rr, 2,
Chris Lattnerb38a7492005-04-02 04:01:14 +00002170 Result+1).addReg(TmpReg3).addReg(TmpReg);
2171 }
2172 return Result+N.ResNo;
2173 }
2174
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002175 case ISD::SELECT:
Chris Lattnerda2ce112005-01-16 07:34:08 +00002176 if (getRegPressure(N.getOperand(1)) > getRegPressure(N.getOperand(2))) {
2177 Tmp2 = SelectExpr(N.getOperand(1));
2178 Tmp3 = SelectExpr(N.getOperand(2));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002179 } else {
Chris Lattnerda2ce112005-01-16 07:34:08 +00002180 Tmp3 = SelectExpr(N.getOperand(2));
2181 Tmp2 = SelectExpr(N.getOperand(1));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002182 }
Chris Lattnerda2ce112005-01-16 07:34:08 +00002183 EmitSelectCC(N.getOperand(0), N.getValueType(), Tmp2, Tmp3, Result);
2184 return Result;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002185
2186 case ISD::SDIV:
2187 case ISD::UDIV:
2188 case ISD::SREM:
2189 case ISD::UREM: {
Chris Lattnerda2ce112005-01-16 07:34:08 +00002190 assert((N.getOpcode() != ISD::SREM || MVT::isInteger(N.getValueType())) &&
2191 "We don't support this operator!");
2192
Chris Lattner5bf26862005-04-13 03:29:53 +00002193 if (N.getOpcode() == ISD::SDIV) {
Chris Lattner3576c842005-01-25 20:35:10 +00002194 // We can fold loads into FpDIVs, but not really into any others.
2195 if (N.getValueType() == MVT::f64) {
2196 // Check for reversed and unreversed DIV.
2197 if (isFoldableLoad(N.getOperand(0), N.getOperand(1), true)) {
2198 if (N.getOperand(0).getOpcode() == ISD::EXTLOAD)
2199 Opc = X86::FDIVR32m;
2200 else
2201 Opc = X86::FDIVR64m;
2202 X86AddressMode AM;
2203 EmitFoldedLoad(N.getOperand(0), AM);
2204 Tmp1 = SelectExpr(N.getOperand(1));
2205 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
2206 return Result;
2207 } else if (isFoldableLoad(N.getOperand(1), N.getOperand(0), true) &&
2208 N.getOperand(1).getOpcode() == ISD::LOAD) {
2209 if (N.getOperand(1).getOpcode() == ISD::EXTLOAD)
2210 Opc = X86::FDIV32m;
2211 else
2212 Opc = X86::FDIV64m;
2213 X86AddressMode AM;
2214 EmitFoldedLoad(N.getOperand(1), AM);
2215 Tmp1 = SelectExpr(N.getOperand(0));
2216 addFullAddress(BuildMI(BB, Opc, 5, Result).addReg(Tmp1), AM);
2217 return Result;
2218 }
2219 }
2220
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002221 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
2222 // FIXME: These special cases should be handled by the lowering impl!
2223 unsigned RHS = CN->getValue();
2224 bool isNeg = false;
2225 if ((int)RHS < 0) {
2226 isNeg = true;
2227 RHS = -RHS;
2228 }
2229 if (RHS && (RHS & (RHS-1)) == 0) { // Signed division by power of 2?
2230 unsigned Log = log2(RHS);
2231 unsigned TmpReg = MakeReg(N.getValueType());
2232 unsigned SAROpc, SHROpc, ADDOpc, NEGOpc;
2233 switch (N.getValueType()) {
2234 default: assert("Unknown type to signed divide!");
2235 case MVT::i8:
2236 SAROpc = X86::SAR8ri;
2237 SHROpc = X86::SHR8ri;
2238 ADDOpc = X86::ADD8rr;
2239 NEGOpc = X86::NEG8r;
2240 break;
2241 case MVT::i16:
2242 SAROpc = X86::SAR16ri;
2243 SHROpc = X86::SHR16ri;
2244 ADDOpc = X86::ADD16rr;
2245 NEGOpc = X86::NEG16r;
2246 break;
2247 case MVT::i32:
2248 SAROpc = X86::SAR32ri;
2249 SHROpc = X86::SHR32ri;
2250 ADDOpc = X86::ADD32rr;
2251 NEGOpc = X86::NEG32r;
2252 break;
2253 }
Chris Lattner11333092005-01-11 03:11:44 +00002254 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002255 BuildMI(BB, SAROpc, 2, TmpReg).addReg(Tmp1).addImm(Log-1);
2256 unsigned TmpReg2 = MakeReg(N.getValueType());
2257 BuildMI(BB, SHROpc, 2, TmpReg2).addReg(TmpReg).addImm(32-Log);
2258 unsigned TmpReg3 = MakeReg(N.getValueType());
2259 BuildMI(BB, ADDOpc, 2, TmpReg3).addReg(Tmp1).addReg(TmpReg2);
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002260
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002261 unsigned TmpReg4 = isNeg ? MakeReg(N.getValueType()) : Result;
2262 BuildMI(BB, SAROpc, 2, TmpReg4).addReg(TmpReg3).addImm(Log);
2263 if (isNeg)
2264 BuildMI(BB, NEGOpc, 1, Result).addReg(TmpReg4);
2265 return Result;
2266 }
2267 }
Chris Lattner5bf26862005-04-13 03:29:53 +00002268 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002269
Chris Lattner11333092005-01-11 03:11:44 +00002270 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2271 Tmp1 = SelectExpr(N.getOperand(0));
2272 Tmp2 = SelectExpr(N.getOperand(1));
2273 } else {
2274 Tmp2 = SelectExpr(N.getOperand(1));
2275 Tmp1 = SelectExpr(N.getOperand(0));
2276 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002277
2278 bool isSigned = N.getOpcode() == ISD::SDIV || N.getOpcode() == ISD::SREM;
2279 bool isDiv = N.getOpcode() == ISD::SDIV || N.getOpcode() == ISD::UDIV;
2280 unsigned LoReg, HiReg, DivOpcode, MovOpcode, ClrOpcode, SExtOpcode;
2281 switch (N.getValueType()) {
2282 default: assert(0 && "Cannot sdiv this type!");
2283 case MVT::i8:
2284 DivOpcode = isSigned ? X86::IDIV8r : X86::DIV8r;
2285 LoReg = X86::AL;
2286 HiReg = X86::AH;
2287 MovOpcode = X86::MOV8rr;
2288 ClrOpcode = X86::MOV8ri;
2289 SExtOpcode = X86::CBW;
2290 break;
2291 case MVT::i16:
2292 DivOpcode = isSigned ? X86::IDIV16r : X86::DIV16r;
2293 LoReg = X86::AX;
2294 HiReg = X86::DX;
2295 MovOpcode = X86::MOV16rr;
2296 ClrOpcode = X86::MOV16ri;
2297 SExtOpcode = X86::CWD;
2298 break;
2299 case MVT::i32:
2300 DivOpcode = isSigned ? X86::IDIV32r : X86::DIV32r;
Chris Lattner42928302005-01-12 03:16:09 +00002301 LoReg = X86::EAX;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002302 HiReg = X86::EDX;
2303 MovOpcode = X86::MOV32rr;
2304 ClrOpcode = X86::MOV32ri;
2305 SExtOpcode = X86::CDQ;
2306 break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002307 case MVT::f64:
Chris Lattnerda2ce112005-01-16 07:34:08 +00002308 BuildMI(BB, X86::FpDIV, 2, Result).addReg(Tmp1).addReg(Tmp2);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002309 return Result;
2310 }
2311
2312 // Set up the low part.
2313 BuildMI(BB, MovOpcode, 1, LoReg).addReg(Tmp1);
2314
2315 if (isSigned) {
2316 // Sign extend the low part into the high part.
2317 BuildMI(BB, SExtOpcode, 0);
2318 } else {
2319 // Zero out the high part, effectively zero extending the input.
2320 BuildMI(BB, ClrOpcode, 1, HiReg).addImm(0);
2321 }
2322
2323 // Emit the DIV/IDIV instruction.
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002324 BuildMI(BB, DivOpcode, 1).addReg(Tmp2);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002325
2326 // Get the result of the divide or rem.
2327 BuildMI(BB, MovOpcode, 1, Result).addReg(isDiv ? LoReg : HiReg);
2328 return Result;
2329 }
2330
2331 case ISD::SHL:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002332 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Chris Lattnera5ade062005-01-11 21:19:59 +00002333 if (CN->getValue() == 1) { // X = SHL Y, 1 -> X = ADD Y, Y
2334 switch (N.getValueType()) {
2335 default: assert(0 && "Cannot shift this type!");
2336 case MVT::i8: Opc = X86::ADD8rr; break;
2337 case MVT::i16: Opc = X86::ADD16rr; break;
2338 case MVT::i32: Opc = X86::ADD32rr; break;
2339 }
2340 Tmp1 = SelectExpr(N.getOperand(0));
2341 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp1);
2342 return Result;
2343 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002344
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002345 switch (N.getValueType()) {
2346 default: assert(0 && "Cannot shift this type!");
2347 case MVT::i8: Opc = X86::SHL8ri; break;
2348 case MVT::i16: Opc = X86::SHL16ri; break;
2349 case MVT::i32: Opc = X86::SHL32ri; break;
2350 }
Chris Lattner11333092005-01-11 03:11:44 +00002351 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002352 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
2353 return Result;
2354 }
Chris Lattner11333092005-01-11 03:11:44 +00002355
2356 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2357 Tmp1 = SelectExpr(N.getOperand(0));
2358 Tmp2 = SelectExpr(N.getOperand(1));
2359 } else {
2360 Tmp2 = SelectExpr(N.getOperand(1));
2361 Tmp1 = SelectExpr(N.getOperand(0));
2362 }
2363
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002364 switch (N.getValueType()) {
2365 default: assert(0 && "Cannot shift this type!");
2366 case MVT::i8 : Opc = X86::SHL8rCL; break;
2367 case MVT::i16: Opc = X86::SHL16rCL; break;
2368 case MVT::i32: Opc = X86::SHL32rCL; break;
2369 }
2370 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
2371 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
2372 return Result;
2373 case ISD::SRL:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002374 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
2375 switch (N.getValueType()) {
2376 default: assert(0 && "Cannot shift this type!");
2377 case MVT::i8: Opc = X86::SHR8ri; break;
2378 case MVT::i16: Opc = X86::SHR16ri; break;
2379 case MVT::i32: Opc = X86::SHR32ri; break;
2380 }
Chris Lattner11333092005-01-11 03:11:44 +00002381 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002382 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
2383 return Result;
2384 }
Chris Lattner11333092005-01-11 03:11:44 +00002385
2386 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2387 Tmp1 = SelectExpr(N.getOperand(0));
2388 Tmp2 = SelectExpr(N.getOperand(1));
2389 } else {
2390 Tmp2 = SelectExpr(N.getOperand(1));
2391 Tmp1 = SelectExpr(N.getOperand(0));
2392 }
2393
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002394 switch (N.getValueType()) {
2395 default: assert(0 && "Cannot shift this type!");
2396 case MVT::i8 : Opc = X86::SHR8rCL; break;
2397 case MVT::i16: Opc = X86::SHR16rCL; break;
2398 case MVT::i32: Opc = X86::SHR32rCL; break;
2399 }
2400 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
2401 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
2402 return Result;
2403 case ISD::SRA:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002404 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
2405 switch (N.getValueType()) {
2406 default: assert(0 && "Cannot shift this type!");
2407 case MVT::i8: Opc = X86::SAR8ri; break;
2408 case MVT::i16: Opc = X86::SAR16ri; break;
2409 case MVT::i32: Opc = X86::SAR32ri; break;
2410 }
Chris Lattner11333092005-01-11 03:11:44 +00002411 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002412 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CN->getValue());
2413 return Result;
2414 }
Chris Lattner11333092005-01-11 03:11:44 +00002415
2416 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2417 Tmp1 = SelectExpr(N.getOperand(0));
2418 Tmp2 = SelectExpr(N.getOperand(1));
2419 } else {
2420 Tmp2 = SelectExpr(N.getOperand(1));
2421 Tmp1 = SelectExpr(N.getOperand(0));
2422 }
2423
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002424 switch (N.getValueType()) {
2425 default: assert(0 && "Cannot shift this type!");
2426 case MVT::i8 : Opc = X86::SAR8rCL; break;
2427 case MVT::i16: Opc = X86::SAR16rCL; break;
2428 case MVT::i32: Opc = X86::SAR32rCL; break;
2429 }
2430 BuildMI(BB, X86::MOV8rr, 1, X86::CL).addReg(Tmp2);
2431 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
2432 return Result;
2433
2434 case ISD::SETCC:
Chris Lattnercb1aa8d2005-01-17 01:34:14 +00002435 EmitCMP(N.getOperand(0), N.getOperand(1), Node->hasOneUse());
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002436 EmitSetCC(BB, Result, cast<SetCCSDNode>(N)->getCondition(),
2437 MVT::isFloatingPoint(N.getOperand(1).getValueType()));
2438 return Result;
Chris Lattnere9ef81d2005-01-15 05:22:24 +00002439 case ISD::LOAD:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002440 // Make sure we generate both values.
Chris Lattner4a108662005-01-18 03:51:59 +00002441 if (Result != 1) { // Generate the token
2442 if (!ExprMap.insert(std::make_pair(N.getValue(1), 1)).second)
2443 assert(0 && "Load already emitted!?");
2444 } else
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002445 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
2446
Chris Lattner5188ad72005-01-08 19:28:19 +00002447 switch (Node->getValueType(0)) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002448 default: assert(0 && "Cannot load this type!");
2449 case MVT::i1:
2450 case MVT::i8: Opc = X86::MOV8rm; break;
2451 case MVT::i16: Opc = X86::MOV16rm; break;
2452 case MVT::i32: Opc = X86::MOV32rm; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002453 case MVT::f64: Opc = X86::FLD64m; ContainsFPCode = true; break;
2454 }
Chris Lattner11333092005-01-11 03:11:44 +00002455
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002456 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N.getOperand(1))){
Chris Lattner11333092005-01-11 03:11:44 +00002457 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002458 addConstantPoolReference(BuildMI(BB, Opc, 4, Result), CP->getIndex());
2459 } else {
2460 X86AddressMode AM;
Chris Lattner636e79a2005-01-13 05:53:16 +00002461
2462 SDOperand Chain = N.getOperand(0);
2463 SDOperand Address = N.getOperand(1);
2464 if (getRegPressure(Chain) > getRegPressure(Address)) {
2465 Select(Chain);
2466 SelectAddress(Address, AM);
2467 } else {
2468 SelectAddress(Address, AM);
2469 Select(Chain);
2470 }
2471
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002472 addFullAddress(BuildMI(BB, Opc, 4, Result), AM);
2473 }
2474 return Result;
Chris Lattnere9ef81d2005-01-15 05:22:24 +00002475
2476 case ISD::EXTLOAD: // Arbitrarily codegen extloads as MOVZX*
2477 case ISD::ZEXTLOAD: {
2478 // Make sure we generate both values.
2479 if (Result != 1)
2480 ExprMap[N.getValue(1)] = 1; // Generate the token
2481 else
2482 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
2483
Chris Lattnerda2ce112005-01-16 07:34:08 +00002484 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N.getOperand(1)))
2485 if (Node->getValueType(0) == MVT::f64) {
2486 assert(cast<MVTSDNode>(Node)->getExtraValueType() == MVT::f32 &&
2487 "Bad EXTLOAD!");
2488 addConstantPoolReference(BuildMI(BB, X86::FLD32m, 4, Result),
2489 CP->getIndex());
2490 return Result;
2491 }
2492
Chris Lattnere9ef81d2005-01-15 05:22:24 +00002493 X86AddressMode AM;
2494 if (getRegPressure(Node->getOperand(0)) >
2495 getRegPressure(Node->getOperand(1))) {
2496 Select(Node->getOperand(0)); // chain
2497 SelectAddress(Node->getOperand(1), AM);
2498 } else {
2499 SelectAddress(Node->getOperand(1), AM);
2500 Select(Node->getOperand(0)); // chain
2501 }
2502
2503 switch (Node->getValueType(0)) {
2504 default: assert(0 && "Unknown type to sign extend to.");
2505 case MVT::f64:
2506 assert(cast<MVTSDNode>(Node)->getExtraValueType() == MVT::f32 &&
2507 "Bad EXTLOAD!");
2508 addFullAddress(BuildMI(BB, X86::FLD32m, 5, Result), AM);
2509 break;
2510 case MVT::i32:
2511 switch (cast<MVTSDNode>(Node)->getExtraValueType()) {
2512 default:
2513 assert(0 && "Bad zero extend!");
2514 case MVT::i1:
2515 case MVT::i8:
2516 addFullAddress(BuildMI(BB, X86::MOVZX32rm8, 5, Result), AM);
2517 break;
2518 case MVT::i16:
2519 addFullAddress(BuildMI(BB, X86::MOVZX32rm16, 5, Result), AM);
2520 break;
2521 }
2522 break;
2523 case MVT::i16:
2524 assert(cast<MVTSDNode>(Node)->getExtraValueType() <= MVT::i8 &&
2525 "Bad zero extend!");
2526 addFullAddress(BuildMI(BB, X86::MOVSX16rm8, 5, Result), AM);
2527 break;
2528 case MVT::i8:
2529 assert(cast<MVTSDNode>(Node)->getExtraValueType() == MVT::i1 &&
2530 "Bad zero extend!");
2531 addFullAddress(BuildMI(BB, X86::MOV8rm, 5, Result), AM);
2532 break;
2533 }
2534 return Result;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002535 }
Chris Lattnere9ef81d2005-01-15 05:22:24 +00002536 case ISD::SEXTLOAD: {
2537 // Make sure we generate both values.
2538 if (Result != 1)
2539 ExprMap[N.getValue(1)] = 1; // Generate the token
2540 else
2541 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
2542
2543 X86AddressMode AM;
2544 if (getRegPressure(Node->getOperand(0)) >
2545 getRegPressure(Node->getOperand(1))) {
2546 Select(Node->getOperand(0)); // chain
2547 SelectAddress(Node->getOperand(1), AM);
2548 } else {
2549 SelectAddress(Node->getOperand(1), AM);
2550 Select(Node->getOperand(0)); // chain
2551 }
2552
2553 switch (Node->getValueType(0)) {
2554 case MVT::i8: assert(0 && "Cannot sign extend from bool!");
2555 default: assert(0 && "Unknown type to sign extend to.");
2556 case MVT::i32:
2557 switch (cast<MVTSDNode>(Node)->getExtraValueType()) {
2558 default:
2559 case MVT::i1: assert(0 && "Cannot sign extend from bool!");
2560 case MVT::i8:
2561 addFullAddress(BuildMI(BB, X86::MOVSX32rm8, 5, Result), AM);
2562 break;
2563 case MVT::i16:
2564 addFullAddress(BuildMI(BB, X86::MOVSX32rm16, 5, Result), AM);
2565 break;
2566 }
2567 break;
2568 case MVT::i16:
2569 assert(cast<MVTSDNode>(Node)->getExtraValueType() == MVT::i8 &&
2570 "Cannot sign extend from bool!");
2571 addFullAddress(BuildMI(BB, X86::MOVSX16rm8, 5, Result), AM);
2572 break;
2573 }
2574 return Result;
2575 }
2576
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002577 case ISD::DYNAMIC_STACKALLOC:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002578 // Generate both result values.
2579 if (Result != 1)
2580 ExprMap[N.getValue(1)] = 1; // Generate the token
2581 else
2582 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
2583
2584 // FIXME: We are currently ignoring the requested alignment for handling
2585 // greater than the stack alignment. This will need to be revisited at some
2586 // point. Align = N.getOperand(2);
2587
2588 if (!isa<ConstantSDNode>(N.getOperand(2)) ||
2589 cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) {
2590 std::cerr << "Cannot allocate stack object with greater alignment than"
2591 << " the stack alignment yet!";
2592 abort();
2593 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002594
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002595 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
Chris Lattner11333092005-01-11 03:11:44 +00002596 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002597 BuildMI(BB, X86::SUB32ri, 2, X86::ESP).addReg(X86::ESP)
2598 .addImm(CN->getValue());
2599 } else {
Chris Lattner11333092005-01-11 03:11:44 +00002600 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2601 Select(N.getOperand(0));
2602 Tmp1 = SelectExpr(N.getOperand(1));
2603 } else {
2604 Tmp1 = SelectExpr(N.getOperand(1));
2605 Select(N.getOperand(0));
2606 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002607
2608 // Subtract size from stack pointer, thereby allocating some space.
2609 BuildMI(BB, X86::SUB32rr, 2, X86::ESP).addReg(X86::ESP).addReg(Tmp1);
2610 }
2611
2612 // Put a pointer to the space into the result register, by copying the stack
2613 // pointer.
2614 BuildMI(BB, X86::MOV32rr, 1, Result).addReg(X86::ESP);
2615 return Result;
2616
2617 case ISD::CALL:
Chris Lattner5188ad72005-01-08 19:28:19 +00002618 // The chain for this call is now lowered.
Chris Lattner4a108662005-01-18 03:51:59 +00002619 ExprMap.insert(std::make_pair(N.getValue(Node->getNumValues()-1), 1));
Chris Lattner5188ad72005-01-08 19:28:19 +00002620
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002621 if (GlobalAddressSDNode *GASD =
2622 dyn_cast<GlobalAddressSDNode>(N.getOperand(1))) {
Chris Lattner11333092005-01-11 03:11:44 +00002623 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002624 BuildMI(BB, X86::CALLpcrel32, 1).addGlobalAddress(GASD->getGlobal(),true);
2625 } else if (ExternalSymbolSDNode *ESSDN =
2626 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1))) {
Chris Lattner11333092005-01-11 03:11:44 +00002627 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002628 BuildMI(BB, X86::CALLpcrel32,
2629 1).addExternalSymbol(ESSDN->getSymbol(), true);
2630 } else {
Chris Lattner11333092005-01-11 03:11:44 +00002631 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2632 Select(N.getOperand(0));
2633 Tmp1 = SelectExpr(N.getOperand(1));
2634 } else {
2635 Tmp1 = SelectExpr(N.getOperand(1));
2636 Select(N.getOperand(0));
2637 }
2638
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002639 BuildMI(BB, X86::CALL32r, 1).addReg(Tmp1);
2640 }
Chris Lattner5188ad72005-01-08 19:28:19 +00002641 switch (Node->getValueType(0)) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002642 default: assert(0 && "Unknown value type for call result!");
2643 case MVT::Other: return 1;
2644 case MVT::i1:
2645 case MVT::i8:
2646 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
2647 break;
2648 case MVT::i16:
2649 BuildMI(BB, X86::MOV16rr, 1, Result).addReg(X86::AX);
2650 break;
2651 case MVT::i32:
2652 BuildMI(BB, X86::MOV32rr, 1, Result).addReg(X86::EAX);
Chris Lattner5188ad72005-01-08 19:28:19 +00002653 if (Node->getValueType(1) == MVT::i32)
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002654 BuildMI(BB, X86::MOV32rr, 1, Result+1).addReg(X86::EDX);
2655 break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002656 case MVT::f64: // Floating-point return values live in %ST(0)
2657 ContainsFPCode = true;
2658 BuildMI(BB, X86::FpGETRESULT, 1, Result);
2659 break;
2660 }
2661 return Result+N.ResNo;
Chris Lattner966cdfb2005-05-09 21:17:38 +00002662 case ISD::READPORT:
2663 // First, determine that the size of the operand falls within the acceptable
2664 // range for this architecture.
2665 //
2666 if (Node->getOperand(1).getValueType() != MVT::i16) {
2667 std::cerr << "llvm.readport: Address size is not 16 bits\n";
2668 exit(1);
2669 }
2670
2671 // Make sure we generate both values.
2672 if (Result != 1) { // Generate the token
2673 if (!ExprMap.insert(std::make_pair(N.getValue(1), 1)).second)
2674 assert(0 && "readport already emitted!?");
2675 } else
2676 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
2677
2678 Select(Node->getOperand(0)); // Select the chain.
2679
2680 // If the port is a single-byte constant, use the immediate form.
2681 if (ConstantSDNode *Port = dyn_cast<ConstantSDNode>(Node->getOperand(1)))
2682 if ((Port->getValue() & 255) == Port->getValue()) {
2683 switch (Node->getValueType(0)) {
2684 case MVT::i8:
2685 BuildMI(BB, X86::IN8ri, 1).addImm(Port->getValue());
2686 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
2687 return Result;
2688 case MVT::i16:
2689 BuildMI(BB, X86::IN16ri, 1).addImm(Port->getValue());
2690 BuildMI(BB, X86::MOV16rr, 1, Result).addReg(X86::AX);
2691 return Result;
2692 case MVT::i32:
2693 BuildMI(BB, X86::IN32ri, 1).addImm(Port->getValue());
2694 BuildMI(BB, X86::MOV32rr, 1, Result).addReg(X86::EAX);
2695 return Result;
2696 default: break;
2697 }
2698 }
2699
2700 // Now, move the I/O port address into the DX register and use the IN
2701 // instruction to get the input data.
2702 //
2703 Tmp1 = SelectExpr(Node->getOperand(1));
2704 BuildMI(BB, X86::MOV16rr, 1, X86::DX).addReg(Tmp1);
2705 switch (Node->getValueType(0)) {
2706 case MVT::i8:
2707 BuildMI(BB, X86::IN8rr, 0);
2708 BuildMI(BB, X86::MOV8rr, 1, Result).addReg(X86::AL);
2709 return Result;
2710 case MVT::i16:
2711 BuildMI(BB, X86::IN16rr, 0);
2712 BuildMI(BB, X86::MOV16rr, 1, Result).addReg(X86::AX);
2713 return Result;
2714 case MVT::i32:
2715 BuildMI(BB, X86::IN32rr, 0);
2716 BuildMI(BB, X86::MOV32rr, 1, Result).addReg(X86::EAX);
2717 return Result;
2718 default:
2719 std::cerr << "Cannot do input on this data type";
2720 exit(1);
2721 }
2722
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002723 }
2724
2725 return 0;
2726}
2727
Chris Lattnere10269b2005-01-17 19:25:26 +00002728/// TryToFoldLoadOpStore - Given a store node, try to fold together a
2729/// load/op/store instruction. If successful return true.
2730bool ISel::TryToFoldLoadOpStore(SDNode *Node) {
2731 assert(Node->getOpcode() == ISD::STORE && "Can only do this for stores!");
2732 SDOperand Chain = Node->getOperand(0);
2733 SDOperand StVal = Node->getOperand(1);
Chris Lattner5c659812005-01-17 22:10:42 +00002734 SDOperand StPtr = Node->getOperand(2);
Chris Lattnere10269b2005-01-17 19:25:26 +00002735
2736 // The chain has to be a load, the stored value must be an integer binary
2737 // operation with one use.
Chris Lattner5c659812005-01-17 22:10:42 +00002738 if (!StVal.Val->hasOneUse() || StVal.Val->getNumOperands() != 2 ||
Chris Lattnere10269b2005-01-17 19:25:26 +00002739 MVT::isFloatingPoint(StVal.getValueType()))
2740 return false;
2741
Chris Lattner5c659812005-01-17 22:10:42 +00002742 // Token chain must either be a factor node or the load to fold.
2743 if (Chain.getOpcode() != ISD::LOAD && Chain.getOpcode() != ISD::TokenFactor)
2744 return false;
Chris Lattnere10269b2005-01-17 19:25:26 +00002745
Chris Lattner5c659812005-01-17 22:10:42 +00002746 SDOperand TheLoad;
2747
2748 // Check to see if there is a load from the same pointer that we're storing
2749 // to in either operand of the binop.
2750 if (StVal.getOperand(0).getOpcode() == ISD::LOAD &&
2751 StVal.getOperand(0).getOperand(1) == StPtr)
2752 TheLoad = StVal.getOperand(0);
2753 else if (StVal.getOperand(1).getOpcode() == ISD::LOAD &&
2754 StVal.getOperand(1).getOperand(1) == StPtr)
2755 TheLoad = StVal.getOperand(1);
2756 else
2757 return false; // No matching load operand.
2758
2759 // We can only fold the load if there are no intervening side-effecting
2760 // operations. This means that the store uses the load as its token chain, or
2761 // there are only token factor nodes in between the store and load.
2762 if (Chain != TheLoad.getValue(1)) {
2763 // Okay, the other option is that we have a store referring to (possibly
2764 // nested) token factor nodes. For now, just try peeking through one level
2765 // of token factors to see if this is the case.
2766 bool ChainOk = false;
2767 if (Chain.getOpcode() == ISD::TokenFactor) {
2768 for (unsigned i = 0, e = Chain.getNumOperands(); i != e; ++i)
2769 if (Chain.getOperand(i) == TheLoad.getValue(1)) {
2770 ChainOk = true;
2771 break;
2772 }
2773 }
2774
2775 if (!ChainOk) return false;
2776 }
2777
2778 if (TheLoad.getOperand(1) != StPtr)
Chris Lattnere10269b2005-01-17 19:25:26 +00002779 return false;
2780
2781 // Make sure that one of the operands of the binop is the load, and that the
2782 // load folds into the binop.
2783 if (((StVal.getOperand(0) != TheLoad ||
2784 !isFoldableLoad(TheLoad, StVal.getOperand(1))) &&
2785 (StVal.getOperand(1) != TheLoad ||
2786 !isFoldableLoad(TheLoad, StVal.getOperand(0)))))
2787 return false;
2788
2789 // Finally, check to see if this is one of the ops we can handle!
2790 static const unsigned ADDTAB[] = {
2791 X86::ADD8mi, X86::ADD16mi, X86::ADD32mi,
2792 X86::ADD8mr, X86::ADD16mr, X86::ADD32mr,
2793 };
2794 static const unsigned SUBTAB[] = {
2795 X86::SUB8mi, X86::SUB16mi, X86::SUB32mi,
2796 X86::SUB8mr, X86::SUB16mr, X86::SUB32mr,
2797 };
2798 static const unsigned ANDTAB[] = {
2799 X86::AND8mi, X86::AND16mi, X86::AND32mi,
2800 X86::AND8mr, X86::AND16mr, X86::AND32mr,
2801 };
2802 static const unsigned ORTAB[] = {
2803 X86::OR8mi, X86::OR16mi, X86::OR32mi,
2804 X86::OR8mr, X86::OR16mr, X86::OR32mr,
2805 };
2806 static const unsigned XORTAB[] = {
2807 X86::XOR8mi, X86::XOR16mi, X86::XOR32mi,
2808 X86::XOR8mr, X86::XOR16mr, X86::XOR32mr,
2809 };
2810 static const unsigned SHLTAB[] = {
2811 X86::SHL8mi, X86::SHL16mi, X86::SHL32mi,
2812 /*Have to put the reg in CL*/0, 0, 0,
2813 };
2814 static const unsigned SARTAB[] = {
2815 X86::SAR8mi, X86::SAR16mi, X86::SAR32mi,
2816 /*Have to put the reg in CL*/0, 0, 0,
2817 };
2818 static const unsigned SHRTAB[] = {
2819 X86::SHR8mi, X86::SHR16mi, X86::SHR32mi,
2820 /*Have to put the reg in CL*/0, 0, 0,
2821 };
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002822
Chris Lattnere10269b2005-01-17 19:25:26 +00002823 const unsigned *TabPtr = 0;
2824 switch (StVal.getOpcode()) {
2825 default:
2826 std::cerr << "CANNOT [mem] op= val: ";
2827 StVal.Val->dump(); std::cerr << "\n";
2828 case ISD::MUL:
2829 case ISD::SDIV:
2830 case ISD::UDIV:
2831 case ISD::SREM:
2832 case ISD::UREM: return false;
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002833
Chris Lattnere10269b2005-01-17 19:25:26 +00002834 case ISD::ADD: TabPtr = ADDTAB; break;
2835 case ISD::SUB: TabPtr = SUBTAB; break;
2836 case ISD::AND: TabPtr = ANDTAB; break;
2837 case ISD:: OR: TabPtr = ORTAB; break;
2838 case ISD::XOR: TabPtr = XORTAB; break;
2839 case ISD::SHL: TabPtr = SHLTAB; break;
2840 case ISD::SRA: TabPtr = SARTAB; break;
2841 case ISD::SRL: TabPtr = SHRTAB; break;
2842 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002843
Chris Lattnere10269b2005-01-17 19:25:26 +00002844 // Handle: [mem] op= CST
2845 SDOperand Op0 = StVal.getOperand(0);
2846 SDOperand Op1 = StVal.getOperand(1);
Chris Lattner0a078832005-01-23 23:20:06 +00002847 unsigned Opc = 0;
Chris Lattnere10269b2005-01-17 19:25:26 +00002848 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1)) {
2849 switch (Op0.getValueType()) { // Use Op0's type because of shifts.
2850 default: break;
2851 case MVT::i1:
2852 case MVT::i8: Opc = TabPtr[0]; break;
2853 case MVT::i16: Opc = TabPtr[1]; break;
2854 case MVT::i32: Opc = TabPtr[2]; break;
2855 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002856
Chris Lattnere10269b2005-01-17 19:25:26 +00002857 if (Opc) {
Chris Lattner4a108662005-01-18 03:51:59 +00002858 if (!ExprMap.insert(std::make_pair(TheLoad.getValue(1), 1)).second)
2859 assert(0 && "Already emitted?");
Chris Lattner5c659812005-01-17 22:10:42 +00002860 Select(Chain);
2861
Chris Lattnere10269b2005-01-17 19:25:26 +00002862 X86AddressMode AM;
2863 if (getRegPressure(TheLoad.getOperand(0)) >
2864 getRegPressure(TheLoad.getOperand(1))) {
2865 Select(TheLoad.getOperand(0));
2866 SelectAddress(TheLoad.getOperand(1), AM);
2867 } else {
2868 SelectAddress(TheLoad.getOperand(1), AM);
2869 Select(TheLoad.getOperand(0));
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002870 }
Chris Lattner5c659812005-01-17 22:10:42 +00002871
2872 if (StVal.getOpcode() == ISD::ADD) {
2873 if (CN->getValue() == 1) {
2874 switch (Op0.getValueType()) {
2875 default: break;
2876 case MVT::i8:
2877 addFullAddress(BuildMI(BB, X86::INC8m, 4), AM);
2878 return true;
2879 case MVT::i16: Opc = TabPtr[1];
2880 addFullAddress(BuildMI(BB, X86::INC16m, 4), AM);
2881 return true;
2882 case MVT::i32: Opc = TabPtr[2];
2883 addFullAddress(BuildMI(BB, X86::INC32m, 4), AM);
2884 return true;
2885 }
2886 } else if (CN->getValue()+1 == 0) { // [X] += -1 -> DEC [X]
2887 switch (Op0.getValueType()) {
2888 default: break;
2889 case MVT::i8:
2890 addFullAddress(BuildMI(BB, X86::DEC8m, 4), AM);
2891 return true;
2892 case MVT::i16: Opc = TabPtr[1];
2893 addFullAddress(BuildMI(BB, X86::DEC16m, 4), AM);
2894 return true;
2895 case MVT::i32: Opc = TabPtr[2];
2896 addFullAddress(BuildMI(BB, X86::DEC32m, 4), AM);
2897 return true;
2898 }
2899 }
2900 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002901
Chris Lattnere10269b2005-01-17 19:25:26 +00002902 addFullAddress(BuildMI(BB, Opc, 4+1),AM).addImm(CN->getValue());
2903 return true;
2904 }
2905 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002906
Chris Lattnere10269b2005-01-17 19:25:26 +00002907 // If we have [mem] = V op [mem], try to turn it into:
2908 // [mem] = [mem] op V.
2909 if (Op1 == TheLoad && StVal.getOpcode() != ISD::SUB &&
2910 StVal.getOpcode() != ISD::SHL && StVal.getOpcode() != ISD::SRA &&
2911 StVal.getOpcode() != ISD::SRL)
2912 std::swap(Op0, Op1);
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002913
Chris Lattnere10269b2005-01-17 19:25:26 +00002914 if (Op0 != TheLoad) return false;
2915
2916 switch (Op0.getValueType()) {
2917 default: return false;
2918 case MVT::i1:
2919 case MVT::i8: Opc = TabPtr[3]; break;
2920 case MVT::i16: Opc = TabPtr[4]; break;
2921 case MVT::i32: Opc = TabPtr[5]; break;
2922 }
Chris Lattner5c659812005-01-17 22:10:42 +00002923
Chris Lattnerb422aea2005-01-18 17:35:28 +00002924 // Table entry doesn't exist?
2925 if (Opc == 0) return false;
2926
Chris Lattner4a108662005-01-18 03:51:59 +00002927 if (!ExprMap.insert(std::make_pair(TheLoad.getValue(1), 1)).second)
2928 assert(0 && "Already emitted?");
Chris Lattner5c659812005-01-17 22:10:42 +00002929 Select(Chain);
Chris Lattnere10269b2005-01-17 19:25:26 +00002930 Select(TheLoad.getOperand(0));
Chris Lattner98a8ba02005-01-18 01:06:26 +00002931
Chris Lattnere10269b2005-01-17 19:25:26 +00002932 X86AddressMode AM;
2933 SelectAddress(TheLoad.getOperand(1), AM);
2934 unsigned Reg = SelectExpr(Op1);
Chris Lattner98a8ba02005-01-18 01:06:26 +00002935 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addReg(Reg);
Chris Lattnere10269b2005-01-17 19:25:26 +00002936 return true;
2937}
2938
2939
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002940void ISel::Select(SDOperand N) {
2941 unsigned Tmp1, Tmp2, Opc;
2942
Nate Begeman85fdeb22005-03-24 04:39:54 +00002943 if (!ExprMap.insert(std::make_pair(N, 1)).second)
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002944 return; // Already selected.
2945
Chris Lattner989de032005-01-11 06:14:36 +00002946 SDNode *Node = N.Val;
2947
2948 switch (Node->getOpcode()) {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002949 default:
Chris Lattner989de032005-01-11 06:14:36 +00002950 Node->dump(); std::cerr << "\n";
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002951 assert(0 && "Node not handled yet!");
2952 case ISD::EntryToken: return; // Noop
Chris Lattnerc3580712005-01-13 18:01:36 +00002953 case ISD::TokenFactor:
Chris Lattner1d50b7f2005-01-13 19:56:00 +00002954 if (Node->getNumOperands() == 2) {
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002955 bool OneFirst =
Chris Lattner1d50b7f2005-01-13 19:56:00 +00002956 getRegPressure(Node->getOperand(1))>getRegPressure(Node->getOperand(0));
2957 Select(Node->getOperand(OneFirst));
2958 Select(Node->getOperand(!OneFirst));
2959 } else {
2960 std::vector<std::pair<unsigned, unsigned> > OpsP;
2961 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
2962 OpsP.push_back(std::make_pair(getRegPressure(Node->getOperand(i)), i));
2963 std::sort(OpsP.begin(), OpsP.end());
2964 std::reverse(OpsP.begin(), OpsP.end());
2965 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
2966 Select(Node->getOperand(OpsP[i].second));
2967 }
Chris Lattnerc3580712005-01-13 18:01:36 +00002968 return;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002969 case ISD::CopyToReg:
Chris Lattneref6806c2005-01-12 02:02:48 +00002970 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
2971 Select(N.getOperand(0));
2972 Tmp1 = SelectExpr(N.getOperand(1));
2973 } else {
2974 Tmp1 = SelectExpr(N.getOperand(1));
2975 Select(N.getOperand(0));
2976 }
Chris Lattner18c2f132005-01-13 20:50:02 +00002977 Tmp2 = cast<RegSDNode>(N)->getReg();
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002978
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002979 if (Tmp1 != Tmp2) {
2980 switch (N.getOperand(1).getValueType()) {
2981 default: assert(0 && "Invalid type for operation!");
2982 case MVT::i1:
2983 case MVT::i8: Opc = X86::MOV8rr; break;
2984 case MVT::i16: Opc = X86::MOV16rr; break;
2985 case MVT::i32: Opc = X86::MOV32rr; break;
Chris Lattneref7ba072005-01-11 03:50:45 +00002986 case MVT::f64: Opc = X86::FpMOV; ContainsFPCode = true; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002987 }
2988 BuildMI(BB, Opc, 1, Tmp2).addReg(Tmp1);
2989 }
2990 return;
2991 case ISD::RET:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002992 switch (N.getNumOperands()) {
2993 default:
2994 assert(0 && "Unknown return instruction!");
2995 case 3:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00002996 assert(N.getOperand(1).getValueType() == MVT::i32 &&
2997 N.getOperand(2).getValueType() == MVT::i32 &&
2998 "Unknown two-register value!");
Chris Lattner11333092005-01-11 03:11:44 +00002999 if (getRegPressure(N.getOperand(1)) > getRegPressure(N.getOperand(2))) {
3000 Tmp1 = SelectExpr(N.getOperand(1));
3001 Tmp2 = SelectExpr(N.getOperand(2));
3002 } else {
3003 Tmp2 = SelectExpr(N.getOperand(2));
3004 Tmp1 = SelectExpr(N.getOperand(1));
3005 }
3006 Select(N.getOperand(0));
3007
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003008 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
3009 BuildMI(BB, X86::MOV32rr, 1, X86::EDX).addReg(Tmp2);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003010 break;
3011 case 2:
Chris Lattner11333092005-01-11 03:11:44 +00003012 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
3013 Select(N.getOperand(0));
3014 Tmp1 = SelectExpr(N.getOperand(1));
3015 } else {
3016 Tmp1 = SelectExpr(N.getOperand(1));
3017 Select(N.getOperand(0));
3018 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003019 switch (N.getOperand(1).getValueType()) {
3020 default: assert(0 && "All other types should have been promoted!!");
3021 case MVT::f64:
3022 BuildMI(BB, X86::FpSETRESULT, 1).addReg(Tmp1);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003023 break;
3024 case MVT::i32:
3025 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003026 break;
3027 }
3028 break;
3029 case 1:
Chris Lattner11333092005-01-11 03:11:44 +00003030 Select(N.getOperand(0));
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003031 break;
3032 }
3033 BuildMI(BB, X86::RET, 0); // Just emit a 'ret' instruction
3034 return;
3035 case ISD::BR: {
3036 Select(N.getOperand(0));
3037 MachineBasicBlock *Dest =
3038 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
3039 BuildMI(BB, X86::JMP, 1).addMBB(Dest);
3040 return;
3041 }
3042
3043 case ISD::BRCOND: {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003044 MachineBasicBlock *Dest =
3045 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
Chris Lattner11333092005-01-11 03:11:44 +00003046
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003047 // Try to fold a setcc into the branch. If this fails, emit a test/jne
3048 // pair.
Chris Lattner6c07aee2005-01-11 04:06:27 +00003049 if (EmitBranchCC(Dest, N.getOperand(0), N.getOperand(1))) {
3050 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(1))) {
3051 Select(N.getOperand(0));
3052 Tmp1 = SelectExpr(N.getOperand(1));
3053 } else {
3054 Tmp1 = SelectExpr(N.getOperand(1));
3055 Select(N.getOperand(0));
3056 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003057 BuildMI(BB, X86::TEST8rr, 2).addReg(Tmp1).addReg(Tmp1);
3058 BuildMI(BB, X86::JNE, 1).addMBB(Dest);
3059 }
Chris Lattner11333092005-01-11 03:11:44 +00003060
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003061 return;
3062 }
Chris Lattnere9ef81d2005-01-15 05:22:24 +00003063
Chris Lattner4df0de92005-01-17 00:00:33 +00003064 case ISD::LOAD:
3065 // If this load could be folded into the only using instruction, and if it
3066 // is safe to emit the instruction here, try to do so now.
3067 if (Node->hasNUsesOfValue(1, 0)) {
3068 SDOperand TheVal = N.getValue(0);
3069 SDNode *User = 0;
3070 for (SDNode::use_iterator UI = Node->use_begin(); ; ++UI) {
3071 assert(UI != Node->use_end() && "Didn't find use!");
3072 SDNode *UN = *UI;
3073 for (unsigned i = 0, e = UN->getNumOperands(); i != e; ++i)
3074 if (UN->getOperand(i) == TheVal) {
3075 User = UN;
3076 goto FoundIt;
3077 }
3078 }
3079 FoundIt:
3080 // Only handle unary operators right now.
3081 if (User->getNumOperands() == 1) {
Chris Lattner4a108662005-01-18 03:51:59 +00003082 ExprMap.erase(N);
Chris Lattner4df0de92005-01-17 00:00:33 +00003083 SelectExpr(SDOperand(User, 0));
3084 return;
3085 }
3086 }
Chris Lattnerb71f8fc2005-01-18 04:00:54 +00003087 ExprMap.erase(N);
Chris Lattner4df0de92005-01-17 00:00:33 +00003088 SelectExpr(N);
3089 return;
Chris Lattner966cdfb2005-05-09 21:17:38 +00003090 case ISD::READPORT:
Chris Lattnere9ef81d2005-01-15 05:22:24 +00003091 case ISD::EXTLOAD:
3092 case ISD::SEXTLOAD:
3093 case ISD::ZEXTLOAD:
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003094 case ISD::CALL:
3095 case ISD::DYNAMIC_STACKALLOC:
Chris Lattnerb71f8fc2005-01-18 04:00:54 +00003096 ExprMap.erase(N);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003097 SelectExpr(N);
3098 return;
Chris Lattnere9ef81d2005-01-15 05:22:24 +00003099
3100 case ISD::TRUNCSTORE: { // truncstore chain, val, ptr :storety
3101 // On X86, we can represent all types except for Bool and Float natively.
3102 X86AddressMode AM;
3103 MVT::ValueType StoredTy = cast<MVTSDNode>(Node)->getExtraValueType();
Chris Lattnerda2ce112005-01-16 07:34:08 +00003104 assert((StoredTy == MVT::i1 || StoredTy == MVT::f32 ||
3105 StoredTy == MVT::i16 /*FIXME: THIS IS JUST FOR TESTING!*/)
3106 && "Unsupported TRUNCSTORE for this target!");
3107
3108 if (StoredTy == MVT::i16) {
3109 // FIXME: This is here just to allow testing. X86 doesn't really have a
3110 // TRUNCSTORE i16 operation, but this is required for targets that do not
3111 // have 16-bit integer registers. We occasionally disable 16-bit integer
3112 // registers to test the promotion code.
3113 Select(N.getOperand(0));
3114 Tmp1 = SelectExpr(N.getOperand(1));
3115 SelectAddress(N.getOperand(2), AM);
3116
3117 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
3118 addFullAddress(BuildMI(BB, X86::MOV16mr, 5), AM).addReg(X86::AX);
3119 return;
3120 }
Chris Lattnere9ef81d2005-01-15 05:22:24 +00003121
3122 // Store of constant bool?
3123 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
3124 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(2))) {
3125 Select(N.getOperand(0));
3126 SelectAddress(N.getOperand(2), AM);
3127 } else {
3128 SelectAddress(N.getOperand(2), AM);
3129 Select(N.getOperand(0));
3130 }
3131 addFullAddress(BuildMI(BB, X86::MOV8mi, 5), AM).addImm(CN->getValue());
3132 return;
3133 }
3134
3135 switch (StoredTy) {
3136 default: assert(0 && "Cannot truncstore this type!");
3137 case MVT::i1: Opc = X86::MOV8mr; break;
3138 case MVT::f32: Opc = X86::FST32m; break;
3139 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003140
Chris Lattnere9ef81d2005-01-15 05:22:24 +00003141 std::vector<std::pair<unsigned, unsigned> > RP;
3142 RP.push_back(std::make_pair(getRegPressure(N.getOperand(0)), 0));
3143 RP.push_back(std::make_pair(getRegPressure(N.getOperand(1)), 1));
3144 RP.push_back(std::make_pair(getRegPressure(N.getOperand(2)), 2));
3145 std::sort(RP.begin(), RP.end());
3146
Chris Lattner572dd082005-02-23 05:57:21 +00003147 Tmp1 = 0; // Silence a warning.
Chris Lattnere9ef81d2005-01-15 05:22:24 +00003148 for (unsigned i = 0; i != 3; ++i)
3149 switch (RP[2-i].second) {
3150 default: assert(0 && "Unknown operand number!");
3151 case 0: Select(N.getOperand(0)); break;
3152 case 1: Tmp1 = SelectExpr(N.getOperand(1)); break;
3153 case 2: SelectAddress(N.getOperand(2), AM); break;
3154 }
3155
3156 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addReg(Tmp1);
3157 return;
3158 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003159 case ISD::STORE: {
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003160 X86AddressMode AM;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003161
3162 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
3163 Opc = 0;
3164 switch (CN->getValueType(0)) {
3165 default: assert(0 && "Invalid type for operation!");
3166 case MVT::i1:
3167 case MVT::i8: Opc = X86::MOV8mi; break;
3168 case MVT::i16: Opc = X86::MOV16mi; break;
3169 case MVT::i32: Opc = X86::MOV32mi; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003170 case MVT::f64: break;
3171 }
3172 if (Opc) {
Chris Lattner11333092005-01-11 03:11:44 +00003173 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(2))) {
3174 Select(N.getOperand(0));
3175 SelectAddress(N.getOperand(2), AM);
3176 } else {
3177 SelectAddress(N.getOperand(2), AM);
3178 Select(N.getOperand(0));
3179 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003180 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addImm(CN->getValue());
3181 return;
3182 }
Chris Lattner75f354b2005-04-21 19:03:24 +00003183 } else if (GlobalAddressSDNode *GA =
3184 dyn_cast<GlobalAddressSDNode>(N.getOperand(1))) {
3185 assert(GA->getValueType(0) == MVT::i32 && "Bad pointer operand");
3186
3187 if (getRegPressure(N.getOperand(0)) > getRegPressure(N.getOperand(2))) {
3188 Select(N.getOperand(0));
3189 SelectAddress(N.getOperand(2), AM);
3190 } else {
3191 SelectAddress(N.getOperand(2), AM);
3192 Select(N.getOperand(0));
3193 }
3194 addFullAddress(BuildMI(BB, X86::MOV32mi, 4+1),
3195 AM).addGlobalAddress(GA->getGlobal());
3196 return;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003197 }
Chris Lattner837caa72005-01-11 23:21:30 +00003198
3199 // Check to see if this is a load/op/store combination.
Chris Lattnere10269b2005-01-17 19:25:26 +00003200 if (TryToFoldLoadOpStore(Node))
3201 return;
Chris Lattner837caa72005-01-11 23:21:30 +00003202
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003203 switch (N.getOperand(1).getValueType()) {
3204 default: assert(0 && "Cannot store this type!");
3205 case MVT::i1:
3206 case MVT::i8: Opc = X86::MOV8mr; break;
3207 case MVT::i16: Opc = X86::MOV16mr; break;
3208 case MVT::i32: Opc = X86::MOV32mr; break;
Chris Lattneref7ba072005-01-11 03:50:45 +00003209 case MVT::f64: Opc = X86::FST64m; break;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003210 }
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003211
Chris Lattner11333092005-01-11 03:11:44 +00003212 std::vector<std::pair<unsigned, unsigned> > RP;
3213 RP.push_back(std::make_pair(getRegPressure(N.getOperand(0)), 0));
3214 RP.push_back(std::make_pair(getRegPressure(N.getOperand(1)), 1));
3215 RP.push_back(std::make_pair(getRegPressure(N.getOperand(2)), 2));
3216 std::sort(RP.begin(), RP.end());
3217
Chris Lattner572dd082005-02-23 05:57:21 +00003218 Tmp1 = 0; // Silence a warning.
Chris Lattner11333092005-01-11 03:11:44 +00003219 for (unsigned i = 0; i != 3; ++i)
3220 switch (RP[2-i].second) {
3221 default: assert(0 && "Unknown operand number!");
3222 case 0: Select(N.getOperand(0)); break;
3223 case 1: Tmp1 = SelectExpr(N.getOperand(1)); break;
Chris Lattnera3aa2e22005-01-11 03:37:59 +00003224 case 2: SelectAddress(N.getOperand(2), AM); break;
Chris Lattner11333092005-01-11 03:11:44 +00003225 }
3226
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003227 addFullAddress(BuildMI(BB, Opc, 4+1), AM).addReg(Tmp1);
3228 return;
3229 }
3230 case ISD::ADJCALLSTACKDOWN:
3231 case ISD::ADJCALLSTACKUP:
3232 Select(N.getOperand(0));
3233 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003234
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003235 Opc = N.getOpcode() == ISD::ADJCALLSTACKDOWN ? X86::ADJCALLSTACKDOWN :
3236 X86::ADJCALLSTACKUP;
3237 BuildMI(BB, Opc, 1).addImm(Tmp1);
3238 return;
Chris Lattner989de032005-01-11 06:14:36 +00003239 case ISD::MEMSET: {
3240 Select(N.getOperand(0)); // Select the chain.
3241 unsigned Align =
3242 (unsigned)cast<ConstantSDNode>(Node->getOperand(4))->getValue();
3243 if (Align == 0) Align = 1;
3244
3245 // Turn the byte code into # iterations
3246 unsigned CountReg;
3247 unsigned Opcode;
3248 if (ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Node->getOperand(2))) {
3249 unsigned Val = ValC->getValue() & 255;
3250
3251 // If the value is a constant, then we can potentially use larger sets.
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 BuildMI(BB, X86::MOV16ri, 1, X86::AX).addImm((Val << 8) | Val);
3262 Opcode = X86::REP_STOSW;
3263 break;
3264 case 0: // DWORD aligned
3265 CountReg = MakeReg(MVT::i32);
3266 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
3267 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/4);
3268 } else {
3269 unsigned ByteReg = SelectExpr(Node->getOperand(3));
3270 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(2);
3271 }
3272 Val = (Val << 8) | Val;
3273 BuildMI(BB, X86::MOV32ri, 1, X86::EAX).addImm((Val << 16) | Val);
3274 Opcode = X86::REP_STOSD;
3275 break;
3276 default: // BYTE aligned
3277 CountReg = SelectExpr(Node->getOperand(3));
3278 BuildMI(BB, X86::MOV8ri, 1, X86::AL).addImm(Val);
3279 Opcode = X86::REP_STOSB;
3280 break;
3281 }
3282 } else {
3283 // If it's not a constant value we are storing, just fall back. We could
3284 // try to be clever to form 16 bit and 32 bit values, but we don't yet.
3285 unsigned ValReg = SelectExpr(Node->getOperand(2));
3286 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(ValReg);
3287 CountReg = SelectExpr(Node->getOperand(3));
3288 Opcode = X86::REP_STOSB;
3289 }
3290
3291 // No matter what the alignment is, we put the source in ESI, the
3292 // destination in EDI, and the count in ECX.
3293 unsigned TmpReg1 = SelectExpr(Node->getOperand(1));
3294 BuildMI(BB, X86::MOV32rr, 1, X86::ECX).addReg(CountReg);
3295 BuildMI(BB, X86::MOV32rr, 1, X86::EDI).addReg(TmpReg1);
3296 BuildMI(BB, Opcode, 0);
3297 return;
3298 }
Chris Lattner966cdfb2005-05-09 21:17:38 +00003299 case ISD::MEMCPY: {
Chris Lattner31805bf2005-01-11 06:19:26 +00003300 Select(N.getOperand(0)); // Select the chain.
3301 unsigned Align =
3302 (unsigned)cast<ConstantSDNode>(Node->getOperand(4))->getValue();
3303 if (Align == 0) Align = 1;
3304
3305 // Turn the byte code into # iterations
3306 unsigned CountReg;
3307 unsigned Opcode;
3308 switch (Align & 3) {
3309 case 2: // WORD aligned
3310 CountReg = MakeReg(MVT::i32);
3311 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
3312 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/2);
3313 } else {
3314 unsigned ByteReg = SelectExpr(Node->getOperand(3));
3315 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(1);
3316 }
3317 Opcode = X86::REP_MOVSW;
3318 break;
3319 case 0: // DWORD aligned
3320 CountReg = MakeReg(MVT::i32);
3321 if (ConstantSDNode *I = dyn_cast<ConstantSDNode>(Node->getOperand(3))) {
3322 BuildMI(BB, X86::MOV32ri, 1, CountReg).addImm(I->getValue()/4);
3323 } else {
3324 unsigned ByteReg = SelectExpr(Node->getOperand(3));
3325 BuildMI(BB, X86::SHR32ri, 2, CountReg).addReg(ByteReg).addImm(2);
3326 }
3327 Opcode = X86::REP_MOVSD;
3328 break;
3329 default: // BYTE aligned
3330 CountReg = SelectExpr(Node->getOperand(3));
3331 Opcode = X86::REP_MOVSB;
3332 break;
3333 }
3334
3335 // No matter what the alignment is, we put the source in ESI, the
3336 // destination in EDI, and the count in ECX.
3337 unsigned TmpReg1 = SelectExpr(Node->getOperand(1));
3338 unsigned TmpReg2 = SelectExpr(Node->getOperand(2));
3339 BuildMI(BB, X86::MOV32rr, 1, X86::ECX).addReg(CountReg);
3340 BuildMI(BB, X86::MOV32rr, 1, X86::EDI).addReg(TmpReg1);
3341 BuildMI(BB, X86::MOV32rr, 1, X86::ESI).addReg(TmpReg2);
3342 BuildMI(BB, Opcode, 0);
3343 return;
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003344 }
Chris Lattner966cdfb2005-05-09 21:17:38 +00003345 case ISD::WRITEPORT:
3346 if (Node->getOperand(2).getValueType() != MVT::i16) {
3347 std::cerr << "llvm.writeport: Address size is not 16 bits\n";
3348 exit(1);
3349 }
3350 Select(Node->getOperand(0)); // Emit the chain.
3351
3352 Tmp1 = SelectExpr(Node->getOperand(1));
3353 switch (Node->getOperand(1).getValueType()) {
3354 case MVT::i8:
3355 BuildMI(BB, X86::MOV8rr, 1, X86::AL).addReg(Tmp1);
3356 Tmp2 = X86::OUT8ir; Opc = X86::OUT8rr;
3357 break;
3358 case MVT::i16:
3359 BuildMI(BB, X86::MOV16rr, 1, X86::AX).addReg(Tmp1);
3360 Tmp2 = X86::OUT16ir; Opc = X86::OUT16rr;
3361 break;
3362 case MVT::i32:
3363 BuildMI(BB, X86::MOV32rr, 1, X86::EAX).addReg(Tmp1);
3364 Tmp2 = X86::OUT32ir; Opc = X86::OUT32rr;
3365 break;
3366 default:
3367 std::cerr << "llvm.writeport: invalid data type for X86 target";
3368 exit(1);
3369 }
3370
3371 // If the port is a single-byte constant, use the immediate form.
3372 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Node->getOperand(2)))
3373 if ((CN->getValue() & 255) == CN->getValue()) {
3374 BuildMI(BB, Tmp2, 1).addImm(CN->getValue());
3375 return;
3376 }
3377
3378 // Otherwise, move the I/O port address into the DX register.
3379 unsigned Reg = SelectExpr(Node->getOperand(2));
3380 BuildMI(BB, X86::MOV16rr, 1, X86::DX).addReg(Reg);
3381 BuildMI(BB, Opc, 0);
3382 return;
3383 }
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003384 assert(0 && "Should not be reached!");
3385}
3386
3387
3388/// createX86PatternInstructionSelector - This pass converts an LLVM function
3389/// into a machine code representation using pattern matching and a machine
3390/// description file.
3391///
3392FunctionPass *llvm::createX86PatternInstructionSelector(TargetMachine &TM) {
Misha Brukman0e0a7a452005-04-21 23:38:14 +00003393 return new ISel(TM);
Chris Lattner8acb1ba2005-01-07 07:49:41 +00003394}