blob: 88ad79dbc8edfb3337838016553ddaa57ee758db [file] [log] [blame]
Nate Begemana9795f82005-03-24 04:41:43 +00001//===-- PPC32ISelPattern.cpp - A pattern matching inst selector for PPC32 -===//
2//
3// The LLVM Compiler Infrastructure
4//
Nate Begeman5e966612005-03-24 06:28:42 +00005// This file was developed by Nate Begeman and is distributed under
Nate Begemana9795f82005-03-24 04:41:43 +00006// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines a pattern matching instruction selector for 32 bit PowerPC.
11//
12//===----------------------------------------------------------------------===//
13
14#include "PowerPC.h"
15#include "PowerPCInstrBuilder.h"
16#include "PowerPCInstrInfo.h"
17#include "PPC32RegisterInfo.h"
18#include "llvm/Constants.h" // FIXME: REMOVE
19#include "llvm/Function.h"
20#include "llvm/CodeGen/MachineConstantPool.h" // FIXME: REMOVE
21#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"
28#include "llvm/Support/Debug.h"
29#include "llvm/Support/MathExtras.h"
30#include "llvm/ADT/Statistic.h"
31#include <set>
32#include <algorithm>
33using namespace llvm;
34
35//===----------------------------------------------------------------------===//
36// PPC32TargetLowering - PPC32 Implementation of the TargetLowering interface
37namespace {
38 class PPC32TargetLowering : public TargetLowering {
39 int VarArgsFrameIndex; // FrameIndex for start of varargs area.
40 int ReturnAddrIndex; // FrameIndex for return slot.
41 public:
42 PPC32TargetLowering(TargetMachine &TM) : TargetLowering(TM) {
Nate Begemana9795f82005-03-24 04:41:43 +000043 // Set up the register classes.
44 addRegisterClass(MVT::i32, PPC32::GPRCRegisterClass);
Nate Begeman7532e2f2005-03-26 08:25:22 +000045 addRegisterClass(MVT::f32, PPC32::FPRCRegisterClass);
Nate Begemana9795f82005-03-24 04:41:43 +000046 addRegisterClass(MVT::f64, PPC32::FPRCRegisterClass);
47
Nate Begeman74d73452005-03-31 00:15:26 +000048 // PowerPC has no intrinsics for these particular operations
Nate Begeman01d05262005-03-30 01:45:43 +000049 setOperationAction(ISD::MEMMOVE, MVT::Other, Expand);
50 setOperationAction(ISD::MEMSET, MVT::Other, Expand);
51 setOperationAction(ISD::MEMCPY, MVT::Other, Expand);
52
Nate Begeman74d73452005-03-31 00:15:26 +000053 // PowerPC has an i16 but no i8 (or i1) SEXTLOAD
54 setOperationAction(ISD::SEXTLOAD, MVT::i1, Expand);
55 setOperationAction(ISD::SEXTLOAD, MVT::i8, Expand);
Chris Lattner43fdea02005-04-02 05:03:24 +000056
Nate Begeman27eeb002005-04-02 05:59:34 +000057 setShiftAmountFlavor(Extend); // shl X, 32 == 0
Nate Begeman3e897162005-03-31 23:55:40 +000058 addLegalFPImmediate(+0.0); // Necessary for FSEL
59 addLegalFPImmediate(-0.0); //
60
Nate Begemana9795f82005-03-24 04:41:43 +000061 computeRegisterProperties();
62 }
63
64 /// LowerArguments - This hook must be implemented to indicate how we should
65 /// lower the arguments for the specified function, into the specified DAG.
66 virtual std::vector<SDOperand>
67 LowerArguments(Function &F, SelectionDAG &DAG);
68
69 /// LowerCallTo - This hook lowers an abstract call to a function into an
70 /// actual call.
71 virtual std::pair<SDOperand, SDOperand>
Nate Begeman307e7442005-03-26 01:28:53 +000072 LowerCallTo(SDOperand Chain, const Type *RetTy, bool isVarArg,
73 SDOperand Callee, ArgListTy &Args, SelectionDAG &DAG);
Nate Begemana9795f82005-03-24 04:41:43 +000074
75 virtual std::pair<SDOperand, SDOperand>
76 LowerVAStart(SDOperand Chain, SelectionDAG &DAG);
77
78 virtual std::pair<SDOperand,SDOperand>
79 LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
80 const Type *ArgTy, SelectionDAG &DAG);
81
82 virtual std::pair<SDOperand, SDOperand>
83 LowerFrameReturnAddress(bool isFrameAddr, SDOperand Chain, unsigned Depth,
84 SelectionDAG &DAG);
85 };
86}
87
88
89std::vector<SDOperand>
90PPC32TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
91 //
92 // add beautiful description of PPC stack frame format, or at least some docs
93 //
94 MachineFunction &MF = DAG.getMachineFunction();
95 MachineFrameInfo *MFI = MF.getFrameInfo();
96 MachineBasicBlock& BB = MF.front();
97 std::vector<SDOperand> ArgValues;
98
99 // Due to the rather complicated nature of the PowerPC ABI, rather than a
100 // fixed size array of physical args, for the sake of simplicity let the STL
101 // handle tracking them for us.
102 std::vector<unsigned> argVR, argPR, argOp;
103 unsigned ArgOffset = 24;
104 unsigned GPR_remaining = 8;
105 unsigned FPR_remaining = 13;
106 unsigned GPR_idx = 0, FPR_idx = 0;
107 static const unsigned GPR[] = {
108 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
109 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
110 };
111 static const unsigned FPR[] = {
112 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
113 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
114 };
115
116 // Add DAG nodes to load the arguments... On entry to a function on PPC,
117 // the arguments start at offset 24, although they are likely to be passed
118 // in registers.
119 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
120 SDOperand newroot, argt;
121 unsigned ObjSize;
122 bool needsLoad = false;
123 MVT::ValueType ObjectVT = getValueType(I->getType());
124
125 switch (ObjectVT) {
126 default: assert(0 && "Unhandled argument type!");
127 case MVT::i1:
128 case MVT::i8:
129 case MVT::i16:
130 case MVT::i32:
131 ObjSize = 4;
132 if (GPR_remaining > 0) {
133 BuildMI(&BB, PPC::IMPLICIT_DEF, 0, GPR[GPR_idx]);
Nate Begemanf70b5762005-03-28 23:08:54 +0000134 argt = newroot = DAG.getCopyFromReg(GPR[GPR_idx], MVT::i32,
135 DAG.getRoot());
Nate Begemana9795f82005-03-24 04:41:43 +0000136 if (ObjectVT != MVT::i32)
137 argt = DAG.getNode(ISD::TRUNCATE, ObjectVT, newroot);
Nate Begemana9795f82005-03-24 04:41:43 +0000138 } else {
139 needsLoad = true;
140 }
141 break;
Nate Begemanf7e43382005-03-26 07:46:36 +0000142 case MVT::i64: ObjSize = 8;
143 // FIXME: can split 64b load between reg/mem if it is last arg in regs
Nate Begemana9795f82005-03-24 04:41:43 +0000144 if (GPR_remaining > 1) {
145 BuildMI(&BB, PPC::IMPLICIT_DEF, 0, GPR[GPR_idx]);
146 BuildMI(&BB, PPC::IMPLICIT_DEF, 0, GPR[GPR_idx+1]);
Nate Begemanca12a2b2005-03-28 22:28:37 +0000147 // Copy the extracted halves into the virtual registers
Nate Begemanf70b5762005-03-28 23:08:54 +0000148 SDOperand argHi = DAG.getCopyFromReg(GPR[GPR_idx], MVT::i32,
149 DAG.getRoot());
150 SDOperand argLo = DAG.getCopyFromReg(GPR[GPR_idx+1], MVT::i32, argHi);
Nate Begemanca12a2b2005-03-28 22:28:37 +0000151 // Build the outgoing arg thingy
Nate Begemanf70b5762005-03-28 23:08:54 +0000152 argt = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, argLo, argHi);
153 newroot = argLo;
Nate Begemana9795f82005-03-24 04:41:43 +0000154 } else {
155 needsLoad = true;
156 }
157 break;
158 case MVT::f32: ObjSize = 4;
159 case MVT::f64: ObjSize = 8;
160 if (FPR_remaining > 0) {
161 BuildMI(&BB, PPC::IMPLICIT_DEF, 0, FPR[FPR_idx]);
Nate Begemanf70b5762005-03-28 23:08:54 +0000162 argt = newroot = DAG.getCopyFromReg(FPR[FPR_idx], ObjectVT,
163 DAG.getRoot());
Nate Begemana9795f82005-03-24 04:41:43 +0000164 --FPR_remaining;
165 ++FPR_idx;
166 } else {
167 needsLoad = true;
168 }
169 break;
170 }
171
172 // We need to load the argument to a virtual register if we determined above
173 // that we ran out of physical registers of the appropriate type
174 if (needsLoad) {
175 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
176 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
177 argt = newroot = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN);
178 }
179
180 // Every 4 bytes of argument space consumes one of the GPRs available for
181 // argument passing.
182 if (GPR_remaining > 0) {
183 unsigned delta = (GPR_remaining > 1 && ObjSize == 8) ? 2 : 1;
184 GPR_remaining -= delta;
185 GPR_idx += delta;
186 }
187 ArgOffset += ObjSize;
188
189 DAG.setRoot(newroot.getValue(1));
190 ArgValues.push_back(argt);
191 }
192
Nate Begemana9795f82005-03-24 04:41:43 +0000193 // If the function takes variable number of arguments, make a frame index for
194 // the start of the first vararg value... for expansion of llvm.va_start.
195 if (F.isVarArg())
196 VarArgsFrameIndex = MFI->CreateFixedObject(4, ArgOffset);
197
198 return ArgValues;
199}
200
201std::pair<SDOperand, SDOperand>
202PPC32TargetLowering::LowerCallTo(SDOperand Chain,
Nate Begeman307e7442005-03-26 01:28:53 +0000203 const Type *RetTy, bool isVarArg,
204 SDOperand Callee, ArgListTy &Args, SelectionDAG &DAG) {
205 // args_to_use will accumulate outgoing args for the ISD::CALL case in
206 // SelectExpr to use to put the arguments in the appropriate registers.
Nate Begemana9795f82005-03-24 04:41:43 +0000207 std::vector<SDOperand> args_to_use;
Nate Begeman307e7442005-03-26 01:28:53 +0000208
209 // Count how many bytes are to be pushed on the stack, including the linkage
210 // area, and parameter passing area.
211 unsigned NumBytes = 24;
212
213 if (Args.empty()) {
Nate Begemana7e11a42005-04-01 05:57:17 +0000214 Chain = DAG.getNode(ISD::ADJCALLSTACKDOWN, MVT::Other, Chain,
215 DAG.getConstant(NumBytes, getPointerTy()));
Nate Begeman307e7442005-03-26 01:28:53 +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
233 // Just to be safe, we'll always reserve the full 24 bytes of linkage area
234 // plus 32 bytes of argument space in case any called code gets funky on us.
235 if (NumBytes < 56) NumBytes = 56;
236
237 // Adjust the stack pointer for the new arguments...
238 // These operations are automatically eliminated by the prolog/epilog pass
239 Chain = DAG.getNode(ISD::ADJCALLSTACKDOWN, MVT::Other, Chain,
240 DAG.getConstant(NumBytes, getPointerTy()));
241
242 // Set up a copy of the stack pointer for use loading and storing any
243 // arguments that may not fit in the registers available for argument
244 // passing.
245 SDOperand StackPtr = DAG.getCopyFromReg(PPC::R1, MVT::i32,
246 DAG.getEntryNode());
247
248 // Figure out which arguments are going to go in registers, and which in
249 // memory. Also, if this is a vararg function, floating point operations
250 // must be stored to our stack, and loaded into integer regs as well, if
251 // any integer regs are available for argument passing.
252 unsigned ArgOffset = 24;
253 unsigned GPR_remaining = 8;
254 unsigned FPR_remaining = 13;
Nate Begeman74d73452005-03-31 00:15:26 +0000255
256 std::vector<SDOperand> MemOps;
Nate Begeman307e7442005-03-26 01:28:53 +0000257 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
258 // PtrOff will be used to store the current argument to the stack if a
259 // register cannot be found for it.
260 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
261 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
Nate Begemanf7e43382005-03-26 07:46:36 +0000262 MVT::ValueType ArgVT = getValueType(Args[i].second);
Nate Begeman307e7442005-03-26 01:28:53 +0000263
Nate Begemanf7e43382005-03-26 07:46:36 +0000264 switch (ArgVT) {
Nate Begeman307e7442005-03-26 01:28:53 +0000265 default: assert(0 && "Unexpected ValueType for argument!");
266 case MVT::i1:
267 case MVT::i8:
268 case MVT::i16:
269 // Promote the integer to 32 bits. If the input type is signed use a
270 // sign extend, otherwise use a zero extend.
271 if (Args[i].second->isSigned())
272 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
273 else
274 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
275 // FALL THROUGH
276 case MVT::i32:
277 if (GPR_remaining > 0) {
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000278 args_to_use.push_back(Args[i].first);
Nate Begeman307e7442005-03-26 01:28:53 +0000279 --GPR_remaining;
280 } else {
Nate Begeman74d73452005-03-31 00:15:26 +0000281 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
282 Args[i].first, PtrOff));
Nate Begeman307e7442005-03-26 01:28:53 +0000283 }
284 ArgOffset += 4;
285 break;
286 case MVT::i64:
Nate Begemanf7e43382005-03-26 07:46:36 +0000287 // If we have one free GPR left, we can place the upper half of the i64
288 // in it, and store the other half to the stack. If we have two or more
289 // free GPRs, then we can pass both halves of the i64 in registers.
290 if (GPR_remaining > 0) {
Nate Begemanf2622612005-03-26 02:17:46 +0000291 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
292 Args[i].first, DAG.getConstant(1, MVT::i32));
293 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
294 Args[i].first, DAG.getConstant(0, MVT::i32));
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000295 args_to_use.push_back(Hi);
Nate Begeman74d73452005-03-31 00:15:26 +0000296 --GPR_remaining;
Nate Begeman74d73452005-03-31 00:15:26 +0000297 if (GPR_remaining > 0) {
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000298 args_to_use.push_back(Lo);
Nate Begeman74d73452005-03-31 00:15:26 +0000299 --GPR_remaining;
Nate Begemanf7e43382005-03-26 07:46:36 +0000300 } else {
301 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
302 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
Nate Begeman74d73452005-03-31 00:15:26 +0000303 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
304 Lo, PtrOff));
Nate Begemanf7e43382005-03-26 07:46:36 +0000305 }
Nate Begeman307e7442005-03-26 01:28:53 +0000306 } else {
Nate Begeman74d73452005-03-31 00:15:26 +0000307 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
308 Args[i].first, PtrOff));
Nate Begeman307e7442005-03-26 01:28:53 +0000309 }
310 ArgOffset += 8;
311 break;
312 case MVT::f32:
Nate Begeman307e7442005-03-26 01:28:53 +0000313 case MVT::f64:
Nate Begemanf7e43382005-03-26 07:46:36 +0000314 if (FPR_remaining > 0) {
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000315 args_to_use.push_back(Args[i].first);
316 --FPR_remaining;
Nate Begemanf7e43382005-03-26 07:46:36 +0000317 if (isVarArg) {
Nate Begeman96fc6812005-03-31 02:05:53 +0000318 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Chain,
319 Args[i].first, PtrOff);
320 MemOps.push_back(Store);
Nate Begeman74d73452005-03-31 00:15:26 +0000321 // Float varargs are always shadowed in available integer registers
322 if (GPR_remaining > 0) {
Nate Begeman96fc6812005-03-31 02:05:53 +0000323 SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff);
Nate Begeman74d73452005-03-31 00:15:26 +0000324 MemOps.push_back(Load);
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000325 args_to_use.push_back(Load);
326 --GPR_remaining;
Nate Begeman74d73452005-03-31 00:15:26 +0000327 }
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000328 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
Nate Begeman74d73452005-03-31 00:15:26 +0000329 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
330 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
Nate Begeman96fc6812005-03-31 02:05:53 +0000331 SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff);
Nate Begeman74d73452005-03-31 00:15:26 +0000332 MemOps.push_back(Load);
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000333 args_to_use.push_back(Load);
334 --GPR_remaining;
Nate Begeman74d73452005-03-31 00:15:26 +0000335 }
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000336 } else {
337 // If we have any FPRs remaining, we may also have GPRs remaining.
338 // Args passed in FPRs consume either 1 (f32) or 2 (f64) available
339 // GPRs.
340 if (GPR_remaining > 0) {
341 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
342 --GPR_remaining;
343 }
344 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
345 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
346 --GPR_remaining;
347 }
Nate Begeman74d73452005-03-31 00:15:26 +0000348 }
Nate Begeman307e7442005-03-26 01:28:53 +0000349 } else {
Nate Begeman74d73452005-03-31 00:15:26 +0000350 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
351 Args[i].first, PtrOff));
Nate Begeman307e7442005-03-26 01:28:53 +0000352 }
Nate Begemanf7e43382005-03-26 07:46:36 +0000353 ArgOffset += (ArgVT == MVT::f32) ? 4 : 8;
Nate Begeman307e7442005-03-26 01:28:53 +0000354 break;
355 }
Nate Begemana9795f82005-03-24 04:41:43 +0000356 }
Nate Begeman74d73452005-03-31 00:15:26 +0000357 if (!MemOps.empty())
358 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps);
Nate Begemana9795f82005-03-24 04:41:43 +0000359 }
360
361 std::vector<MVT::ValueType> RetVals;
362 MVT::ValueType RetTyVT = getValueType(RetTy);
363 if (RetTyVT != MVT::isVoid)
364 RetVals.push_back(RetTyVT);
365 RetVals.push_back(MVT::Other);
366
367 SDOperand TheCall = SDOperand(DAG.getCall(RetVals,
368 Chain, Callee, args_to_use), 0);
369 Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
370 Chain = DAG.getNode(ISD::ADJCALLSTACKUP, MVT::Other, Chain,
371 DAG.getConstant(NumBytes, getPointerTy()));
372 return std::make_pair(TheCall, Chain);
373}
374
375std::pair<SDOperand, SDOperand>
376PPC32TargetLowering::LowerVAStart(SDOperand Chain, SelectionDAG &DAG) {
377 //vastart just returns the address of the VarArgsFrameIndex slot.
378 return std::make_pair(DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32), Chain);
379}
380
381std::pair<SDOperand,SDOperand> PPC32TargetLowering::
382LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
383 const Type *ArgTy, SelectionDAG &DAG) {
Nate Begemanc7b09f12005-03-25 08:34:25 +0000384 MVT::ValueType ArgVT = getValueType(ArgTy);
385 SDOperand Result;
386 if (!isVANext) {
387 Result = DAG.getLoad(ArgVT, DAG.getEntryNode(), VAList);
388 } else {
389 unsigned Amt;
390 if (ArgVT == MVT::i32 || ArgVT == MVT::f32)
391 Amt = 4;
392 else {
393 assert((ArgVT == MVT::i64 || ArgVT == MVT::f64) &&
394 "Other types should have been promoted for varargs!");
395 Amt = 8;
396 }
397 Result = DAG.getNode(ISD::ADD, VAList.getValueType(), VAList,
398 DAG.getConstant(Amt, VAList.getValueType()));
399 }
400 return std::make_pair(Result, Chain);
Nate Begemana9795f82005-03-24 04:41:43 +0000401}
402
403
404std::pair<SDOperand, SDOperand> PPC32TargetLowering::
405LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
406 SelectionDAG &DAG) {
Nate Begeman01d05262005-03-30 01:45:43 +0000407 assert(0 && "LowerFrameReturnAddress unimplemented");
Nate Begemana9795f82005-03-24 04:41:43 +0000408 abort();
409}
410
411namespace {
Nate Begemanaa73a9f2005-04-03 11:20:20 +0000412Statistic<>NotLogic("ppc-codegen", "Number of inverted logical ops");
Nate Begemana9795f82005-03-24 04:41:43 +0000413//===--------------------------------------------------------------------===//
414/// ISel - PPC32 specific code to select PPC32 machine instructions for
415/// SelectionDAG operations.
416//===--------------------------------------------------------------------===//
417class ISel : public SelectionDAGISel {
418
419 /// Comment Here.
420 PPC32TargetLowering PPC32Lowering;
421
422 /// ExprMap - As shared expressions are codegen'd, we keep track of which
423 /// vreg the value is produced in, so we only emit one copy of each compiled
424 /// tree.
425 std::map<SDOperand, unsigned> ExprMap;
Nate Begemanc7b09f12005-03-25 08:34:25 +0000426
427 unsigned GlobalBaseReg;
428 bool GlobalBaseInitialized;
Nate Begemana9795f82005-03-24 04:41:43 +0000429
430public:
431 ISel(TargetMachine &TM) : SelectionDAGISel(PPC32Lowering), PPC32Lowering(TM)
432 {}
433
Nate Begemanc7b09f12005-03-25 08:34:25 +0000434 /// runOnFunction - Override this function in order to reset our per-function
435 /// variables.
436 virtual bool runOnFunction(Function &Fn) {
437 // Make sure we re-emit a set of the global base reg if necessary
438 GlobalBaseInitialized = false;
439 return SelectionDAGISel::runOnFunction(Fn);
440 }
441
Nate Begemana9795f82005-03-24 04:41:43 +0000442 /// InstructionSelectBasicBlock - This callback is invoked by
443 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
444 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG) {
445 DEBUG(BB->dump());
446 // Codegen the basic block.
447 Select(DAG.getRoot());
448
449 // Clear state used for selection.
450 ExprMap.clear();
451 }
452
Nate Begemandffcfcc2005-04-01 00:32:34 +0000453 unsigned getGlobalBaseReg();
Nate Begeman6b559972005-04-01 02:59:27 +0000454 unsigned getConstDouble(double floatVal, unsigned Result);
Nate Begemandffcfcc2005-04-01 00:32:34 +0000455 unsigned SelectSetCR0(SDOperand CC);
Nate Begemana9795f82005-03-24 04:41:43 +0000456 unsigned SelectExpr(SDOperand N);
457 unsigned SelectExprFP(SDOperand N, unsigned Result);
458 void Select(SDOperand N);
459
Nate Begeman04730362005-04-01 04:45:11 +0000460 bool SelectAddr(SDOperand N, unsigned& Reg, int& offset);
Nate Begemana9795f82005-03-24 04:41:43 +0000461 void SelectBranchCC(SDOperand N);
462};
463
464/// canUseAsImmediateForOpcode - This method returns a value indicating whether
465/// the ConstantSDNode N can be used as an immediate to Opcode. The return
466/// values are either 0, 1 or 2. 0 indicates that either N is not a
467/// ConstantSDNode, or is not suitable for use by that opcode. A return value
468/// of 1 indicates that the constant may be used in normal immediate form. A
469/// return value of 2 indicates that the constant may be used in shifted
470/// immediate form. If the return value is nonzero, the constant value is
471/// placed in Imm.
472///
473static unsigned canUseAsImmediateForOpcode(SDOperand N, unsigned Opcode,
Nate Begeman3e897162005-03-31 23:55:40 +0000474 unsigned& Imm, bool U = false) {
Nate Begemana9795f82005-03-24 04:41:43 +0000475 if (N.getOpcode() != ISD::Constant) return 0;
476
477 int v = (int)cast<ConstantSDNode>(N)->getSignExtended();
478
479 switch(Opcode) {
480 default: return 0;
481 case ISD::ADD:
482 if (v <= 32767 && v >= -32768) { Imm = v & 0xFFFF; return 1; }
483 if ((v & 0x0000FFFF) == 0) { Imm = v >> 16; return 2; }
484 break;
485 case ISD::AND:
486 case ISD::XOR:
487 case ISD::OR:
488 if (v >= 0 && v <= 65535) { Imm = v & 0xFFFF; return 1; }
489 if ((v & 0x0000FFFF) == 0) { Imm = v >> 16; return 2; }
490 break;
Nate Begeman307e7442005-03-26 01:28:53 +0000491 case ISD::MUL:
Nate Begeman27523a12005-04-02 00:42:16 +0000492 case ISD::SUB:
Nate Begeman307e7442005-03-26 01:28:53 +0000493 if (v <= 32767 && v >= -32768) { Imm = v & 0xFFFF; return 1; }
494 break;
Nate Begeman3e897162005-03-31 23:55:40 +0000495 case ISD::SETCC:
496 if (U && (v >= 0 && v <= 65535)) { Imm = v & 0xFFFF; return 1; }
497 if (!U && (v <= 32767 && v >= -32768)) { Imm = v & 0xFFFF; return 1; }
498 break;
Nate Begemana9795f82005-03-24 04:41:43 +0000499 }
500 return 0;
501}
Nate Begeman3e897162005-03-31 23:55:40 +0000502
503/// getBCCForSetCC - Returns the PowerPC condition branch mnemonic corresponding
504/// to Condition. If the Condition is unordered or unsigned, the bool argument
505/// U is set to true, otherwise it is set to false.
506static unsigned getBCCForSetCC(unsigned Condition, bool& U) {
507 U = false;
508 switch (Condition) {
509 default: assert(0 && "Unknown condition!"); abort();
510 case ISD::SETEQ: return PPC::BEQ;
511 case ISD::SETNE: return PPC::BNE;
512 case ISD::SETULT: U = true;
513 case ISD::SETLT: return PPC::BLT;
514 case ISD::SETULE: U = true;
515 case ISD::SETLE: return PPC::BLE;
516 case ISD::SETUGT: U = true;
517 case ISD::SETGT: return PPC::BGT;
518 case ISD::SETUGE: U = true;
519 case ISD::SETGE: return PPC::BGE;
520 }
Nate Begeman04730362005-04-01 04:45:11 +0000521 return 0;
522}
523
524/// IndexedOpForOp - Return the indexed variant for each of the PowerPC load
525/// and store immediate instructions.
526static unsigned IndexedOpForOp(unsigned Opcode) {
527 switch(Opcode) {
528 default: assert(0 && "Unknown opcode!"); abort();
529 case PPC::LBZ: return PPC::LBZX; case PPC::STB: return PPC::STBX;
530 case PPC::LHZ: return PPC::LHZX; case PPC::STH: return PPC::STHX;
531 case PPC::LHA: return PPC::LHAX; case PPC::STW: return PPC::STWX;
532 case PPC::LWZ: return PPC::LWZX; case PPC::STFS: return PPC::STFSX;
533 case PPC::LFS: return PPC::LFSX; case PPC::STFD: return PPC::STFDX;
534 case PPC::LFD: return PPC::LFDX;
535 }
536 return 0;
Nate Begeman3e897162005-03-31 23:55:40 +0000537}
Nate Begemana9795f82005-03-24 04:41:43 +0000538}
539
Nate Begemanc7b09f12005-03-25 08:34:25 +0000540/// getGlobalBaseReg - Output the instructions required to put the
541/// base address to use for accessing globals into a register.
542///
543unsigned ISel::getGlobalBaseReg() {
544 if (!GlobalBaseInitialized) {
545 // Insert the set of GlobalBaseReg into the first MBB of the function
546 MachineBasicBlock &FirstMBB = BB->getParent()->front();
547 MachineBasicBlock::iterator MBBI = FirstMBB.begin();
548 GlobalBaseReg = MakeReg(MVT::i32);
549 BuildMI(FirstMBB, MBBI, PPC::MovePCtoLR, 0, PPC::LR);
550 BuildMI(FirstMBB, MBBI, PPC::MFLR, 1, GlobalBaseReg).addReg(PPC::LR);
551 GlobalBaseInitialized = true;
552 }
553 return GlobalBaseReg;
554}
555
Nate Begeman6b559972005-04-01 02:59:27 +0000556/// getConstDouble - Loads a floating point value into a register, via the
557/// Constant Pool. Optionally takes a register in which to load the value.
558unsigned ISel::getConstDouble(double doubleVal, unsigned Result=0) {
559 unsigned Tmp1 = MakeReg(MVT::i32);
560 if (0 == Result) Result = MakeReg(MVT::f64);
561 MachineConstantPool *CP = BB->getParent()->getConstantPool();
562 ConstantFP *CFP = ConstantFP::get(Type::DoubleTy, doubleVal);
563 unsigned CPI = CP->getConstantPoolIndex(CFP);
564 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp1).addReg(getGlobalBaseReg())
565 .addConstantPoolIndex(CPI);
566 BuildMI(BB, PPC::LFD, 2, Result).addConstantPoolIndex(CPI).addReg(Tmp1);
567 return Result;
568}
569
Nate Begemandffcfcc2005-04-01 00:32:34 +0000570unsigned ISel::SelectSetCR0(SDOperand CC) {
571 unsigned Opc, Tmp1, Tmp2;
572 static const unsigned CompareOpcodes[] =
573 { PPC::FCMPU, PPC::FCMPU, PPC::CMPW, PPC::CMPLW };
574
575 // If the first operand to the select is a SETCC node, then we can fold it
576 // into the branch that selects which value to return.
577 SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(CC.Val);
578 if (SetCC && CC.getOpcode() == ISD::SETCC) {
579 bool U;
580 Opc = getBCCForSetCC(SetCC->getCondition(), U);
581 Tmp1 = SelectExpr(SetCC->getOperand(0));
582
583 // Pass the optional argument U to canUseAsImmediateForOpcode for SETCC,
584 // so that it knows whether the SETCC immediate range is signed or not.
585 if (1 == canUseAsImmediateForOpcode(SetCC->getOperand(1), ISD::SETCC,
586 Tmp2, U)) {
587 if (U)
588 BuildMI(BB, PPC::CMPLWI, 2, PPC::CR0).addReg(Tmp1).addImm(Tmp2);
589 else
590 BuildMI(BB, PPC::CMPWI, 2, PPC::CR0).addReg(Tmp1).addSImm(Tmp2);
591 } else {
592 bool IsInteger = MVT::isInteger(SetCC->getOperand(0).getValueType());
593 unsigned CompareOpc = CompareOpcodes[2 * IsInteger + U];
594 Tmp2 = SelectExpr(SetCC->getOperand(1));
595 BuildMI(BB, CompareOpc, 2, PPC::CR0).addReg(Tmp1).addReg(Tmp2);
596 }
597 } else {
598 Tmp1 = SelectExpr(CC);
599 BuildMI(BB, PPC::CMPLWI, 2, PPC::CR0).addReg(Tmp1).addImm(0);
600 Opc = PPC::BNE;
601 }
602 return Opc;
603}
604
605/// Check to see if the load is a constant offset from a base register
Nate Begeman04730362005-04-01 04:45:11 +0000606bool ISel::SelectAddr(SDOperand N, unsigned& Reg, int& offset)
Nate Begemana9795f82005-03-24 04:41:43 +0000607{
Nate Begeman96fc6812005-03-31 02:05:53 +0000608 unsigned imm = 0, opcode = N.getOpcode();
Nate Begeman04730362005-04-01 04:45:11 +0000609 if (N.getOpcode() == ISD::ADD) {
610 Reg = SelectExpr(N.getOperand(0));
Nate Begeman96fc6812005-03-31 02:05:53 +0000611 if (1 == canUseAsImmediateForOpcode(N.getOperand(1), opcode, imm)) {
Nate Begeman96fc6812005-03-31 02:05:53 +0000612 offset = imm;
Nate Begeman04730362005-04-01 04:45:11 +0000613 return false;
614 }
615 offset = SelectExpr(N.getOperand(1));
616 return true;
617 }
Nate Begemana9795f82005-03-24 04:41:43 +0000618 Reg = SelectExpr(N);
619 offset = 0;
Nate Begeman04730362005-04-01 04:45:11 +0000620 return false;
Nate Begemana9795f82005-03-24 04:41:43 +0000621}
622
623void ISel::SelectBranchCC(SDOperand N)
624{
625 assert(N.getOpcode() == ISD::BRCOND && "Not a BranchCC???");
626 MachineBasicBlock *Dest =
627 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
Nate Begeman3e897162005-03-31 23:55:40 +0000628
Nate Begemana9795f82005-03-24 04:41:43 +0000629 Select(N.getOperand(0)); //chain
Nate Begemandffcfcc2005-04-01 00:32:34 +0000630 unsigned Opc = SelectSetCR0(N.getOperand(1));
Nate Begeman3e897162005-03-31 23:55:40 +0000631 BuildMI(BB, Opc, 2).addReg(PPC::CR0).addMBB(Dest);
Nate Begemana9795f82005-03-24 04:41:43 +0000632 return;
633}
634
635unsigned ISel::SelectExprFP(SDOperand N, unsigned Result)
636{
637 unsigned Tmp1, Tmp2, Tmp3;
638 unsigned Opc = 0;
639 SDNode *Node = N.Val;
640 MVT::ValueType DestType = N.getValueType();
641 unsigned opcode = N.getOpcode();
642
643 switch (opcode) {
644 default:
645 Node->dump();
646 assert(0 && "Node not handled!\n");
647
Nate Begeman23afcfb2005-03-29 22:48:55 +0000648 case ISD::SELECT: {
Nate Begeman3e897162005-03-31 23:55:40 +0000649 // Attempt to generate FSEL. We can do this whenever we have an FP result,
650 // and an FP comparison in the SetCC node.
651 SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(N.getOperand(0).Val);
652 if (SetCC && N.getOperand(0).getOpcode() == ISD::SETCC &&
653 !MVT::isInteger(SetCC->getOperand(0).getValueType()) &&
654 SetCC->getCondition() != ISD::SETEQ &&
655 SetCC->getCondition() != ISD::SETNE) {
656 MVT::ValueType VT = SetCC->getOperand(0).getValueType();
657 Tmp1 = SelectExpr(SetCC->getOperand(0)); // Val to compare against
658 unsigned TV = SelectExpr(N.getOperand(1)); // Use if TRUE
659 unsigned FV = SelectExpr(N.getOperand(2)); // Use if FALSE
660
661 ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(1));
662 if (CN && (CN->isExactlyValue(-0.0) || CN->isExactlyValue(0.0))) {
663 switch(SetCC->getCondition()) {
664 default: assert(0 && "Invalid FSEL condition"); abort();
665 case ISD::SETULT:
666 case ISD::SETLT:
667 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp1).addReg(FV).addReg(TV);
668 return Result;
669 case ISD::SETUGE:
670 case ISD::SETGE:
671 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp1).addReg(TV).addReg(FV);
672 return Result;
673 case ISD::SETUGT:
674 case ISD::SETGT: {
675 Tmp2 = MakeReg(VT);
676 BuildMI(BB, PPC::FNEG, 1, Tmp2).addReg(Tmp1);
677 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp2).addReg(FV).addReg(TV);
678 return Result;
679 }
680 case ISD::SETULE:
681 case ISD::SETLE: {
682 Tmp2 = MakeReg(VT);
683 BuildMI(BB, PPC::FNEG, 1, Tmp2).addReg(Tmp1);
684 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp2).addReg(TV).addReg(FV);
685 return Result;
686 }
687 }
688 } else {
689 Opc = (MVT::f64 == VT) ? PPC::FSUB : PPC::FSUBS;
690 Tmp2 = SelectExpr(SetCC->getOperand(1));
691 Tmp3 = MakeReg(VT);
692 switch(SetCC->getCondition()) {
693 default: assert(0 && "Invalid FSEL condition"); abort();
694 case ISD::SETULT:
695 case ISD::SETLT:
696 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
697 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(FV).addReg(TV);
698 return Result;
699 case ISD::SETUGE:
700 case ISD::SETGE:
701 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
702 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(TV).addReg(FV);
703 return Result;
704 case ISD::SETUGT:
705 case ISD::SETGT:
706 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp2).addReg(Tmp1);
707 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(FV).addReg(TV);
708 return Result;
709 case ISD::SETULE:
710 case ISD::SETLE:
711 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp2).addReg(Tmp1);
712 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(TV).addReg(FV);
713 return Result;
714 }
715 }
716 assert(0 && "Should never get here");
717 return 0;
718 }
719
Nate Begeman31318e42005-04-01 07:21:30 +0000720 unsigned TrueValue = SelectExpr(N.getOperand(1)); //Use if TRUE
721 unsigned FalseValue = SelectExpr(N.getOperand(2)); //Use if FALSE
Nate Begeman6cb2e1b2005-04-01 08:57:43 +0000722 Opc = SelectSetCR0(N.getOperand(0));
Nate Begeman31318e42005-04-01 07:21:30 +0000723
Nate Begeman23afcfb2005-03-29 22:48:55 +0000724 // Create an iterator with which to insert the MBB for copying the false
725 // value and the MBB to hold the PHI instruction for this SetCC.
726 MachineBasicBlock *thisMBB = BB;
727 const BasicBlock *LLVM_BB = BB->getBasicBlock();
728 ilist<MachineBasicBlock>::iterator It = BB;
729 ++It;
730
731 // thisMBB:
732 // ...
733 // TrueVal = ...
734 // cmpTY cr0, r1, r2
735 // bCC copy1MBB
736 // fallthrough --> copy0MBB
Nate Begeman23afcfb2005-03-29 22:48:55 +0000737 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
738 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
Nate Begeman6cb2e1b2005-04-01 08:57:43 +0000739 BuildMI(BB, Opc, 2).addReg(PPC::CR0).addMBB(sinkMBB);
Nate Begeman23afcfb2005-03-29 22:48:55 +0000740 MachineFunction *F = BB->getParent();
741 F->getBasicBlockList().insert(It, copy0MBB);
742 F->getBasicBlockList().insert(It, sinkMBB);
743 // Update machine-CFG edges
744 BB->addSuccessor(copy0MBB);
745 BB->addSuccessor(sinkMBB);
746
747 // copy0MBB:
748 // %FalseValue = ...
749 // # fallthrough to sinkMBB
750 BB = copy0MBB;
Nate Begeman23afcfb2005-03-29 22:48:55 +0000751 // Update machine-CFG edges
752 BB->addSuccessor(sinkMBB);
753
754 // sinkMBB:
755 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
756 // ...
757 BB = sinkMBB;
758 BuildMI(BB, PPC::PHI, 4, Result).addReg(FalseValue)
759 .addMBB(copy0MBB).addReg(TrueValue).addMBB(thisMBB);
760 return Result;
761 }
Nate Begeman27eeb002005-04-02 05:59:34 +0000762
763 case ISD::FNEG:
764 if (ISD::FABS == N.getOperand(0).getOpcode()) {
765 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
766 BuildMI(BB, PPC::FNABS, 1, Result).addReg(Tmp1);
767 } else {
768 Tmp1 = SelectExpr(N.getOperand(0));
769 BuildMI(BB, PPC::FNEG, 1, Result).addReg(Tmp1);
770 }
771 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +0000772
Nate Begeman27eeb002005-04-02 05:59:34 +0000773 case ISD::FABS:
774 Tmp1 = SelectExpr(N.getOperand(0));
775 BuildMI(BB, PPC::FABS, 1, Result).addReg(Tmp1);
776 return Result;
777
Nate Begemana9795f82005-03-24 04:41:43 +0000778 case ISD::FP_ROUND:
779 assert (DestType == MVT::f32 &&
780 N.getOperand(0).getValueType() == MVT::f64 &&
781 "only f64 to f32 conversion supported here");
782 Tmp1 = SelectExpr(N.getOperand(0));
783 BuildMI(BB, PPC::FRSP, 1, Result).addReg(Tmp1);
784 return Result;
785
786 case ISD::FP_EXTEND:
787 assert (DestType == MVT::f64 &&
788 N.getOperand(0).getValueType() == MVT::f32 &&
789 "only f32 to f64 conversion supported here");
790 Tmp1 = SelectExpr(N.getOperand(0));
791 BuildMI(BB, PPC::FMR, 1, Result).addReg(Tmp1);
792 return Result;
793
794 case ISD::CopyFromReg:
Nate Begemanf2622612005-03-26 02:17:46 +0000795 if (Result == 1)
796 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
797 Tmp1 = dyn_cast<RegSDNode>(Node)->getReg();
798 BuildMI(BB, PPC::FMR, 1, Result).addReg(Tmp1);
799 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +0000800
Nate Begeman6d369cc2005-04-01 01:08:07 +0000801 case ISD::ConstantFP: {
Nate Begeman6d369cc2005-04-01 01:08:07 +0000802 ConstantFPSDNode *CN = cast<ConstantFPSDNode>(N);
Nate Begeman6b559972005-04-01 02:59:27 +0000803 Result = getConstDouble(CN->getValue(), Result);
Nate Begeman6d369cc2005-04-01 01:08:07 +0000804 return Result;
805 }
Nate Begemana9795f82005-03-24 04:41:43 +0000806
807 case ISD::MUL:
808 case ISD::ADD:
809 case ISD::SUB:
810 case ISD::SDIV:
811 switch( opcode ) {
812 case ISD::MUL: Opc = DestType == MVT::f64 ? PPC::FMUL : PPC::FMULS; break;
813 case ISD::ADD: Opc = DestType == MVT::f64 ? PPC::FADD : PPC::FADDS; break;
814 case ISD::SUB: Opc = DestType == MVT::f64 ? PPC::FSUB : PPC::FSUBS; break;
815 case ISD::SDIV: Opc = DestType == MVT::f64 ? PPC::FDIV : PPC::FDIVS; break;
816 };
Nate Begemana9795f82005-03-24 04:41:43 +0000817 Tmp1 = SelectExpr(N.getOperand(0));
818 Tmp2 = SelectExpr(N.getOperand(1));
819 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
820 return Result;
821
Nate Begemana9795f82005-03-24 04:41:43 +0000822 case ISD::UINT_TO_FP:
Nate Begemanfdcf3412005-03-30 19:38:35 +0000823 case ISD::SINT_TO_FP: {
824 assert (N.getOperand(0).getValueType() == MVT::i32
825 && "int to float must operate on i32");
826 bool IsUnsigned = (ISD::UINT_TO_FP == opcode);
827 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
828 Tmp2 = MakeReg(MVT::f64); // temp reg to load the integer value into
829 Tmp3 = MakeReg(MVT::i32); // temp reg to hold the conversion constant
830 unsigned ConstF = MakeReg(MVT::f64); // temp reg to hold the fp constant
831
832 int FrameIdx = BB->getParent()->getFrameInfo()->CreateStackObject(8, 8);
833 MachineConstantPool *CP = BB->getParent()->getConstantPool();
834
835 // FIXME: pull this FP constant generation stuff out into something like
836 // the simple ISel's getReg.
837 if (IsUnsigned) {
838 ConstantFP *CFP = ConstantFP::get(Type::DoubleTy, 0x1.000000p52);
839 unsigned CPI = CP->getConstantPoolIndex(CFP);
840 // Load constant fp value
841 unsigned Tmp4 = MakeReg(MVT::i32);
842 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp4).addReg(getGlobalBaseReg())
843 .addConstantPoolIndex(CPI);
844 BuildMI(BB, PPC::LFD, 2, ConstF).addConstantPoolIndex(CPI).addReg(Tmp4);
845 // Store the hi & low halves of the fp value, currently in int regs
846 BuildMI(BB, PPC::LIS, 1, Tmp3).addSImm(0x4330);
847 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(Tmp3), FrameIdx);
848 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(Tmp1), FrameIdx, 4);
849 addFrameReference(BuildMI(BB, PPC::LFD, 2, Tmp2), FrameIdx);
850 // Generate the return value with a subtract
851 BuildMI(BB, PPC::FSUB, 2, Result).addReg(Tmp2).addReg(ConstF);
852 } else {
853 ConstantFP *CFP = ConstantFP::get(Type::DoubleTy, 0x1.000008p52);
854 unsigned CPI = CP->getConstantPoolIndex(CFP);
855 // Load constant fp value
856 unsigned Tmp4 = MakeReg(MVT::i32);
857 unsigned TmpL = MakeReg(MVT::i32);
858 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp4).addReg(getGlobalBaseReg())
859 .addConstantPoolIndex(CPI);
860 BuildMI(BB, PPC::LFD, 2, ConstF).addConstantPoolIndex(CPI).addReg(Tmp4);
861 // Store the hi & low halves of the fp value, currently in int regs
862 BuildMI(BB, PPC::LIS, 1, Tmp3).addSImm(0x4330);
863 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(Tmp3), FrameIdx);
864 BuildMI(BB, PPC::XORIS, 2, TmpL).addReg(Tmp1).addImm(0x8000);
865 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(TmpL), FrameIdx, 4);
866 addFrameReference(BuildMI(BB, PPC::LFD, 2, Tmp2), FrameIdx);
867 // Generate the return value with a subtract
868 BuildMI(BB, PPC::FSUB, 2, Result).addReg(Tmp2).addReg(ConstF);
869 }
870 return Result;
871 }
Nate Begemana9795f82005-03-24 04:41:43 +0000872 }
Nate Begeman6b559972005-04-01 02:59:27 +0000873 assert(0 && "Should never get here");
Nate Begemana9795f82005-03-24 04:41:43 +0000874 return 0;
875}
876
877unsigned ISel::SelectExpr(SDOperand N) {
878 unsigned Result;
879 unsigned Tmp1, Tmp2, Tmp3;
880 unsigned Opc = 0;
881 unsigned opcode = N.getOpcode();
882
883 SDNode *Node = N.Val;
884 MVT::ValueType DestType = N.getValueType();
885
886 unsigned &Reg = ExprMap[N];
887 if (Reg) return Reg;
888
Nate Begeman27eeb002005-04-02 05:59:34 +0000889 switch (N.getOpcode()) {
890 default:
Nate Begemana9795f82005-03-24 04:41:43 +0000891 Reg = Result = (N.getValueType() != MVT::Other) ?
Nate Begeman27eeb002005-04-02 05:59:34 +0000892 MakeReg(N.getValueType()) : 1;
893 break;
894 case ISD::CALL:
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000895 // If this is a call instruction, make sure to prepare ALL of the result
896 // values as well as the chain.
Nate Begeman27eeb002005-04-02 05:59:34 +0000897 if (Node->getNumValues() == 1)
898 Reg = Result = 1; // Void call, just a chain.
899 else {
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000900 Result = MakeReg(Node->getValueType(0));
901 ExprMap[N.getValue(0)] = Result;
Nate Begeman27eeb002005-04-02 05:59:34 +0000902 for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000903 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
Nate Begeman27eeb002005-04-02 05:59:34 +0000904 ExprMap[SDOperand(Node, Node->getNumValues()-1)] = 1;
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000905 }
Nate Begeman27eeb002005-04-02 05:59:34 +0000906 break;
907 case ISD::ADD_PARTS:
908 case ISD::SUB_PARTS:
909 case ISD::SHL_PARTS:
910 case ISD::SRL_PARTS:
911 case ISD::SRA_PARTS:
912 Result = MakeReg(Node->getValueType(0));
913 ExprMap[N.getValue(0)] = Result;
914 for (unsigned i = 1, e = N.Val->getNumValues(); i != e; ++i)
915 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
916 break;
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000917 }
918
919 if (DestType == MVT::f64 || DestType == MVT::f32)
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000920 if (ISD::LOAD != opcode && ISD::EXTLOAD != opcode && ISD::UNDEF != opcode)
Nate Begeman74d73452005-03-31 00:15:26 +0000921 return SelectExprFP(N, Result);
Nate Begemana9795f82005-03-24 04:41:43 +0000922
923 switch (opcode) {
924 default:
925 Node->dump();
926 assert(0 && "Node not handled!\n");
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000927 case ISD::UNDEF:
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000928 BuildMI(BB, PPC::IMPLICIT_DEF, 0, Result);
929 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +0000930 case ISD::DYNAMIC_STACKALLOC:
Nate Begeman5e966612005-03-24 06:28:42 +0000931 // Generate both result values. FIXME: Need a better commment here?
932 if (Result != 1)
933 ExprMap[N.getValue(1)] = 1;
934 else
935 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
936
937 // FIXME: We are currently ignoring the requested alignment for handling
938 // greater than the stack alignment. This will need to be revisited at some
939 // point. Align = N.getOperand(2);
940 if (!isa<ConstantSDNode>(N.getOperand(2)) ||
941 cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) {
942 std::cerr << "Cannot allocate stack object with greater alignment than"
943 << " the stack alignment yet!";
944 abort();
945 }
946 Select(N.getOperand(0));
947 Tmp1 = SelectExpr(N.getOperand(1));
948 // Subtract size from stack pointer, thereby allocating some space.
949 BuildMI(BB, PPC::SUBF, 2, PPC::R1).addReg(Tmp1).addReg(PPC::R1);
950 // Put a pointer to the space into the result register by copying the SP
951 BuildMI(BB, PPC::OR, 2, Result).addReg(PPC::R1).addReg(PPC::R1);
952 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +0000953
954 case ISD::ConstantPool:
Nate Begemanca12a2b2005-03-28 22:28:37 +0000955 Tmp1 = cast<ConstantPoolSDNode>(N)->getIndex();
956 Tmp2 = MakeReg(MVT::i32);
957 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp2).addReg(getGlobalBaseReg())
958 .addConstantPoolIndex(Tmp1);
959 BuildMI(BB, PPC::LA, 2, Result).addReg(Tmp2).addConstantPoolIndex(Tmp1);
960 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +0000961
962 case ISD::FrameIndex:
Nate Begemanf3d08f32005-03-29 00:03:27 +0000963 Tmp1 = cast<FrameIndexSDNode>(N)->getIndex();
Nate Begeman58f718c2005-03-30 02:23:08 +0000964 addFrameReference(BuildMI(BB, PPC::ADDI, 2, Result), (int)Tmp1, 0, false);
Nate Begemanf3d08f32005-03-29 00:03:27 +0000965 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +0000966
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000967 case ISD::GlobalAddress: {
968 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
Nate Begemanca12a2b2005-03-28 22:28:37 +0000969 Tmp1 = MakeReg(MVT::i32);
Nate Begemanc7b09f12005-03-25 08:34:25 +0000970 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp1).addReg(getGlobalBaseReg())
971 .addGlobalAddress(GV);
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000972 if (GV->hasWeakLinkage() || GV->isExternal()) {
973 BuildMI(BB, PPC::LWZ, 2, Result).addGlobalAddress(GV).addReg(Tmp1);
974 } else {
975 BuildMI(BB, PPC::LA, 2, Result).addReg(Tmp1).addGlobalAddress(GV);
976 }
977 return Result;
978 }
979
Nate Begeman5e966612005-03-24 06:28:42 +0000980 case ISD::LOAD:
Nate Begemana9795f82005-03-24 04:41:43 +0000981 case ISD::EXTLOAD:
982 case ISD::ZEXTLOAD:
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000983 case ISD::SEXTLOAD: {
Nate Begeman9db505c2005-03-28 19:36:43 +0000984 MVT::ValueType TypeBeingLoaded = (ISD::LOAD == opcode) ?
985 Node->getValueType(0) : cast<MVTSDNode>(Node)->getExtraValueType();
Nate Begeman74d73452005-03-31 00:15:26 +0000986 bool sext = (ISD::SEXTLOAD == opcode);
987 bool byte = (MVT::i8 == TypeBeingLoaded);
988
Nate Begeman5e966612005-03-24 06:28:42 +0000989 // Make sure we generate both values.
990 if (Result != 1)
991 ExprMap[N.getValue(1)] = 1; // Generate the token
992 else
993 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
994
995 SDOperand Chain = N.getOperand(0);
996 SDOperand Address = N.getOperand(1);
997 Select(Chain);
998
Nate Begeman9db505c2005-03-28 19:36:43 +0000999 switch (TypeBeingLoaded) {
Nate Begeman74d73452005-03-31 00:15:26 +00001000 default: Node->dump(); assert(0 && "Cannot load this type!");
Nate Begeman9db505c2005-03-28 19:36:43 +00001001 case MVT::i1: Opc = PPC::LBZ; break;
1002 case MVT::i8: Opc = PPC::LBZ; break;
1003 case MVT::i16: Opc = sext ? PPC::LHA : PPC::LHZ; break;
1004 case MVT::i32: Opc = PPC::LWZ; break;
Nate Begeman74d73452005-03-31 00:15:26 +00001005 case MVT::f32: Opc = PPC::LFS; break;
1006 case MVT::f64: Opc = PPC::LFD; break;
Nate Begeman5e966612005-03-24 06:28:42 +00001007 }
1008
Nate Begeman74d73452005-03-31 00:15:26 +00001009 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Address)) {
1010 Tmp1 = MakeReg(MVT::i32);
1011 int CPI = CP->getIndex();
1012 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp1).addReg(getGlobalBaseReg())
1013 .addConstantPoolIndex(CPI);
1014 BuildMI(BB, Opc, 2, Result).addConstantPoolIndex(CPI).addReg(Tmp1);
Nate Begeman9db505c2005-03-28 19:36:43 +00001015 }
Nate Begeman74d73452005-03-31 00:15:26 +00001016 else if(Address.getOpcode() == ISD::FrameIndex) {
Nate Begeman58f718c2005-03-30 02:23:08 +00001017 Tmp1 = cast<FrameIndexSDNode>(Address)->getIndex();
1018 addFrameReference(BuildMI(BB, Opc, 2, Result), (int)Tmp1);
Nate Begeman5e966612005-03-24 06:28:42 +00001019 } else {
1020 int offset;
Nate Begeman04730362005-04-01 04:45:11 +00001021 bool idx = SelectAddr(Address, Tmp1, offset);
1022 if (idx) {
1023 Opc = IndexedOpForOp(Opc);
1024 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(offset);
1025 } else {
1026 BuildMI(BB, Opc, 2, Result).addSImm(offset).addReg(Tmp1);
1027 }
Nate Begeman5e966612005-03-24 06:28:42 +00001028 }
1029 return Result;
1030 }
1031
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001032 case ISD::CALL: {
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001033 unsigned GPR_idx = 0, FPR_idx = 0;
1034 static const unsigned GPR[] = {
1035 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
1036 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
1037 };
1038 static const unsigned FPR[] = {
1039 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
1040 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
1041 };
1042
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001043 // Lower the chain for this call.
1044 Select(N.getOperand(0));
1045 ExprMap[N.getValue(Node->getNumValues()-1)] = 1;
Nate Begeman74d73452005-03-31 00:15:26 +00001046
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001047 // Load the register args to virtual regs
1048 std::vector<unsigned> ArgVR;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001049 for(int i = 2, e = Node->getNumOperands(); i < e; ++i)
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001050 ArgVR.push_back(SelectExpr(N.getOperand(i)));
1051
1052 // Copy the virtual registers into the appropriate argument register
1053 for(int i = 0, e = ArgVR.size(); i < e; ++i) {
1054 switch(N.getOperand(i+2).getValueType()) {
1055 default: Node->dump(); assert(0 && "Unknown value type for call");
1056 case MVT::i1:
1057 case MVT::i8:
1058 case MVT::i16:
1059 case MVT::i32:
1060 assert(GPR_idx < 8 && "Too many int args");
1061 if (N.getOperand(i+2).getOpcode() != ISD::UNDEF)
1062 BuildMI(BB, PPC::OR,2,GPR[GPR_idx]).addReg(ArgVR[i]).addReg(ArgVR[i]);
1063 ++GPR_idx;
1064 break;
1065 case MVT::f64:
1066 case MVT::f32:
1067 assert(FPR_idx < 13 && "Too many fp args");
1068 BuildMI(BB, PPC::FMR, 1, FPR[FPR_idx]).addReg(ArgVR[i]);
1069 ++FPR_idx;
1070 break;
1071 }
1072 }
1073
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001074 // Emit the correct call instruction based on the type of symbol called.
1075 if (GlobalAddressSDNode *GASD =
1076 dyn_cast<GlobalAddressSDNode>(N.getOperand(1))) {
1077 BuildMI(BB, PPC::CALLpcrel, 1).addGlobalAddress(GASD->getGlobal(), true);
1078 } else if (ExternalSymbolSDNode *ESSDN =
1079 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1))) {
1080 BuildMI(BB, PPC::CALLpcrel, 1).addExternalSymbol(ESSDN->getSymbol(), true);
1081 } else {
1082 Tmp1 = SelectExpr(N.getOperand(1));
1083 BuildMI(BB, PPC::OR, 2, PPC::R12).addReg(Tmp1).addReg(Tmp1);
1084 BuildMI(BB, PPC::MTCTR, 1).addReg(PPC::R12);
1085 BuildMI(BB, PPC::CALLindirect, 3).addImm(20).addImm(0).addReg(PPC::R12);
1086 }
1087
1088 switch (Node->getValueType(0)) {
1089 default: assert(0 && "Unknown value type for call result!");
1090 case MVT::Other: return 1;
1091 case MVT::i1:
1092 case MVT::i8:
1093 case MVT::i16:
1094 case MVT::i32:
Nate Begemanc7b09f12005-03-25 08:34:25 +00001095 BuildMI(BB, PPC::OR, 2, Result).addReg(PPC::R3).addReg(PPC::R3);
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001096 if (Node->getValueType(1) == MVT::i32)
Nate Begemanc7b09f12005-03-25 08:34:25 +00001097 BuildMI(BB, PPC::OR, 2, Result+1).addReg(PPC::R4).addReg(PPC::R4);
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001098 break;
1099 case MVT::f32:
1100 case MVT::f64:
1101 BuildMI(BB, PPC::FMR, 1, Result).addReg(PPC::F1);
1102 break;
1103 }
1104 return Result+N.ResNo;
1105 }
Nate Begemana9795f82005-03-24 04:41:43 +00001106
1107 case ISD::SIGN_EXTEND:
1108 case ISD::SIGN_EXTEND_INREG:
1109 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman9db505c2005-03-28 19:36:43 +00001110 switch(cast<MVTSDNode>(Node)->getExtraValueType()) {
1111 default: Node->dump(); assert(0 && "Unhandled SIGN_EXTEND type"); break;
1112 case MVT::i16:
1113 BuildMI(BB, PPC::EXTSH, 1, Result).addReg(Tmp1);
1114 break;
1115 case MVT::i8:
1116 BuildMI(BB, PPC::EXTSB, 1, Result).addReg(Tmp1);
1117 break;
Nate Begeman74747862005-03-29 22:24:51 +00001118 case MVT::i1:
1119 BuildMI(BB, PPC::SUBFIC, 2, Result).addReg(Tmp1).addSImm(0);
1120 break;
Nate Begeman9db505c2005-03-28 19:36:43 +00001121 }
Nate Begemana9795f82005-03-24 04:41:43 +00001122 return Result;
1123
1124 case ISD::ZERO_EXTEND_INREG:
1125 Tmp1 = SelectExpr(N.getOperand(0));
1126 switch(cast<MVTSDNode>(Node)->getExtraValueType()) {
Nate Begeman9db505c2005-03-28 19:36:43 +00001127 default: Node->dump(); assert(0 && "Unhandled ZERO_EXTEND type"); break;
Nate Begemana9795f82005-03-24 04:41:43 +00001128 case MVT::i16: Tmp2 = 16; break;
1129 case MVT::i8: Tmp2 = 24; break;
1130 case MVT::i1: Tmp2 = 31; break;
1131 }
Nate Begeman33162522005-03-29 21:54:38 +00001132 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(0).addImm(Tmp2)
1133 .addImm(31);
Nate Begemana9795f82005-03-24 04:41:43 +00001134 return Result;
1135
Nate Begemana9795f82005-03-24 04:41:43 +00001136 case ISD::CopyFromReg:
1137 if (Result == 1)
1138 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1139 Tmp1 = dyn_cast<RegSDNode>(Node)->getReg();
1140 BuildMI(BB, PPC::OR, 2, Result).addReg(Tmp1).addReg(Tmp1);
1141 return Result;
1142
1143 case ISD::SHL:
Nate Begeman5e966612005-03-24 06:28:42 +00001144 Tmp1 = SelectExpr(N.getOperand(0));
1145 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1146 Tmp2 = CN->getValue() & 0x1F;
Nate Begeman33162522005-03-29 21:54:38 +00001147 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(Tmp2).addImm(0)
Nate Begeman5e966612005-03-24 06:28:42 +00001148 .addImm(31-Tmp2);
1149 } else {
1150 Tmp2 = SelectExpr(N.getOperand(1));
1151 BuildMI(BB, PPC::SLW, 2, Result).addReg(Tmp1).addReg(Tmp2);
1152 }
1153 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001154
Nate Begeman5e966612005-03-24 06:28:42 +00001155 case ISD::SRL:
1156 Tmp1 = SelectExpr(N.getOperand(0));
1157 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1158 Tmp2 = CN->getValue() & 0x1F;
Nate Begeman33162522005-03-29 21:54:38 +00001159 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(32-Tmp2)
Nate Begeman5e966612005-03-24 06:28:42 +00001160 .addImm(Tmp2).addImm(31);
1161 } else {
1162 Tmp2 = SelectExpr(N.getOperand(1));
1163 BuildMI(BB, PPC::SRW, 2, Result).addReg(Tmp1).addReg(Tmp2);
1164 }
1165 return Result;
1166
1167 case ISD::SRA:
1168 Tmp1 = SelectExpr(N.getOperand(0));
1169 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1170 Tmp2 = CN->getValue() & 0x1F;
1171 BuildMI(BB, PPC::SRAWI, 2, Result).addReg(Tmp1).addImm(Tmp2);
1172 } else {
1173 Tmp2 = SelectExpr(N.getOperand(1));
1174 BuildMI(BB, PPC::SRAW, 2, Result).addReg(Tmp1).addReg(Tmp2);
1175 }
1176 return Result;
1177
Nate Begemana9795f82005-03-24 04:41:43 +00001178 case ISD::ADD:
1179 assert (DestType == MVT::i32 && "Only do arithmetic on i32s!");
1180 Tmp1 = SelectExpr(N.getOperand(0));
1181 switch(canUseAsImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
1182 default: assert(0 && "unhandled result code");
1183 case 0: // No immediate
1184 Tmp2 = SelectExpr(N.getOperand(1));
1185 BuildMI(BB, PPC::ADD, 2, Result).addReg(Tmp1).addReg(Tmp2);
1186 break;
1187 case 1: // Low immediate
1188 BuildMI(BB, PPC::ADDI, 2, Result).addReg(Tmp1).addSImm(Tmp2);
1189 break;
1190 case 2: // Shifted immediate
1191 BuildMI(BB, PPC::ADDIS, 2, Result).addReg(Tmp1).addSImm(Tmp2);
1192 break;
1193 }
1194 return Result;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001195
Nate Begemana9795f82005-03-24 04:41:43 +00001196 case ISD::AND:
1197 case ISD::OR:
Nate Begemana9795f82005-03-24 04:41:43 +00001198 Tmp1 = SelectExpr(N.getOperand(0));
1199 switch(canUseAsImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
1200 default: assert(0 && "unhandled result code");
1201 case 0: // No immediate
1202 Tmp2 = SelectExpr(N.getOperand(1));
1203 switch (opcode) {
Nate Begeman5e966612005-03-24 06:28:42 +00001204 case ISD::AND: Opc = PPC::AND; break;
1205 case ISD::OR: Opc = PPC::OR; break;
Nate Begemana9795f82005-03-24 04:41:43 +00001206 }
Nate Begeman5e966612005-03-24 06:28:42 +00001207 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +00001208 break;
1209 case 1: // Low immediate
1210 switch (opcode) {
Nate Begeman5e966612005-03-24 06:28:42 +00001211 case ISD::AND: Opc = PPC::ANDIo; break;
1212 case ISD::OR: Opc = PPC::ORI; break;
Nate Begemana9795f82005-03-24 04:41:43 +00001213 }
Nate Begeman5e966612005-03-24 06:28:42 +00001214 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +00001215 break;
1216 case 2: // Shifted immediate
1217 switch (opcode) {
Nate Begeman5e966612005-03-24 06:28:42 +00001218 case ISD::AND: Opc = PPC::ANDISo; break;
1219 case ISD::OR: Opc = PPC::ORIS; break;
Nate Begemana9795f82005-03-24 04:41:43 +00001220 }
Nate Begeman5e966612005-03-24 06:28:42 +00001221 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +00001222 break;
1223 }
1224 return Result;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001225
Nate Begemanaa73a9f2005-04-03 11:20:20 +00001226 case ISD::XOR: {
1227 // Check for EQV: xor, (xor a, -1), b
1228 if (N.getOperand(0).getOpcode() == ISD::XOR &&
1229 N.getOperand(0).getOperand(1).getOpcode() == ISD::Constant &&
1230 cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->isAllOnesValue()) {
1231 ++NotLogic;
1232 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1233 Tmp2 = SelectExpr(N.getOperand(1));
1234 BuildMI(BB, PPC::EQV, 2, Result).addReg(Tmp1).addReg(Tmp2);
1235 return Result;
1236 }
1237 // Check for NOT, NOR, and NAND: xor (copy, or, and), -1
1238 if (N.getOperand(1).getOpcode() == ISD::Constant &&
1239 cast<ConstantSDNode>(N.getOperand(1))->isAllOnesValue()) {
1240 ++NotLogic;
1241 switch(N.getOperand(0).getOpcode()) {
1242 case ISD::OR:
1243 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1244 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1245 BuildMI(BB, PPC::NOR, 2, Result).addReg(Tmp1).addReg(Tmp2);
1246 break;
1247 case ISD::AND:
1248 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1249 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1250 BuildMI(BB, PPC::NAND, 2, Result).addReg(Tmp1).addReg(Tmp2);
1251 break;
1252 default:
1253 Tmp1 = SelectExpr(N.getOperand(0));
1254 BuildMI(BB, PPC::NOR, 2, Result).addReg(Tmp1).addReg(Tmp1);
1255 break;
1256 }
1257 return Result;
1258 }
1259 Tmp1 = SelectExpr(N.getOperand(0));
1260 switch(canUseAsImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
1261 default: assert(0 && "unhandled result code");
1262 case 0: // No immediate
1263 Tmp2 = SelectExpr(N.getOperand(1));
1264 BuildMI(BB, PPC::XOR, 2, Result).addReg(Tmp1).addReg(Tmp2);
1265 break;
1266 case 1: // Low immediate
1267 BuildMI(BB, PPC::XORI, 2, Result).addReg(Tmp1).addImm(Tmp2);
1268 break;
1269 case 2: // Shifted immediate
1270 BuildMI(BB, PPC::XORIS, 2, Result).addReg(Tmp1).addImm(Tmp2);
1271 break;
1272 }
1273 return Result;
1274 }
1275
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001276 case ISD::SUB:
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001277 Tmp2 = SelectExpr(N.getOperand(1));
Nate Begeman27523a12005-04-02 00:42:16 +00001278 if (1 == canUseAsImmediateForOpcode(N.getOperand(0), opcode, Tmp1))
1279 BuildMI(BB, PPC::SUBFIC, 2, Result).addReg(Tmp2).addSImm(Tmp1);
1280 else {
1281 Tmp1 = SelectExpr(N.getOperand(0));
1282 BuildMI(BB, PPC::SUBF, 2, Result).addReg(Tmp2).addReg(Tmp1);
1283 }
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001284 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001285
Nate Begeman5e966612005-03-24 06:28:42 +00001286 case ISD::MUL:
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001287 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman307e7442005-03-26 01:28:53 +00001288 if (1 == canUseAsImmediateForOpcode(N.getOperand(1), opcode, Tmp2))
1289 BuildMI(BB, PPC::MULLI, 2, Result).addReg(Tmp1).addSImm(Tmp2);
1290 else {
1291 Tmp2 = SelectExpr(N.getOperand(1));
1292 BuildMI(BB, PPC::MULLW, 2, Result).addReg(Tmp1).addReg(Tmp2);
1293 }
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001294 return Result;
1295
Nate Begemanf3d08f32005-03-29 00:03:27 +00001296 case ISD::SDIV:
1297 case ISD::UDIV:
Nate Begemanf3d08f32005-03-29 00:03:27 +00001298 Tmp1 = SelectExpr(N.getOperand(0));
1299 Tmp2 = SelectExpr(N.getOperand(1));
1300 Opc = (ISD::UDIV == opcode) ? PPC::DIVWU : PPC::DIVW;
1301 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1302 return Result;
1303
1304 case ISD::UREM:
1305 case ISD::SREM: {
Nate Begemanf3d08f32005-03-29 00:03:27 +00001306 Tmp1 = SelectExpr(N.getOperand(0));
1307 Tmp2 = SelectExpr(N.getOperand(1));
1308 Tmp3 = MakeReg(MVT::i32);
1309 unsigned Tmp4 = MakeReg(MVT::i32);
1310 Opc = (ISD::UREM == opcode) ? PPC::DIVWU : PPC::DIVW;
1311 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
1312 BuildMI(BB, PPC::MULLW, 2, Tmp4).addReg(Tmp3).addReg(Tmp2);
1313 BuildMI(BB, PPC::SUBF, 2, Result).addReg(Tmp4).addReg(Tmp1);
1314 return Result;
1315 }
1316
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001317 case ISD::ADD_PARTS:
Nate Begemanca12a2b2005-03-28 22:28:37 +00001318 case ISD::SUB_PARTS: {
1319 assert(N.getNumOperands() == 4 && N.getValueType() == MVT::i32 &&
1320 "Not an i64 add/sub!");
1321 // Emit all of the operands.
1322 std::vector<unsigned> InVals;
1323 for (unsigned i = 0, e = N.getNumOperands(); i != e; ++i)
1324 InVals.push_back(SelectExpr(N.getOperand(i)));
1325 if (N.getOpcode() == ISD::ADD_PARTS) {
Nate Begeman27eeb002005-04-02 05:59:34 +00001326 BuildMI(BB, PPC::ADDC, 2, Result).addReg(InVals[0]).addReg(InVals[2]);
1327 BuildMI(BB, PPC::ADDE, 2, Result+1).addReg(InVals[1]).addReg(InVals[3]);
Nate Begemanca12a2b2005-03-28 22:28:37 +00001328 } else {
Nate Begeman27eeb002005-04-02 05:59:34 +00001329 BuildMI(BB, PPC::SUBFC, 2, Result).addReg(InVals[2]).addReg(InVals[0]);
1330 BuildMI(BB, PPC::SUBFE, 2, Result+1).addReg(InVals[3]).addReg(InVals[1]);
1331 }
1332 return Result+N.ResNo;
1333 }
1334
1335 case ISD::SHL_PARTS:
1336 case ISD::SRA_PARTS:
1337 case ISD::SRL_PARTS: {
1338 assert(N.getNumOperands() == 3 && N.getValueType() == MVT::i32 &&
1339 "Not an i64 shift!");
1340 unsigned ShiftOpLo = SelectExpr(N.getOperand(0));
1341 unsigned ShiftOpHi = SelectExpr(N.getOperand(1));
1342 unsigned SHReg = SelectExpr(N.getOperand(2));
1343 Tmp1 = MakeReg(MVT::i32);
1344 Tmp2 = MakeReg(MVT::i32);
1345 Tmp3 = MakeReg(MVT::i32);
1346 unsigned Tmp4 = MakeReg(MVT::i32);
1347 unsigned Tmp5 = MakeReg(MVT::i32);
1348 unsigned Tmp6 = MakeReg(MVT::i32);
1349 BuildMI(BB, PPC::SUBFIC, 2, Tmp1).addReg(SHReg).addSImm(32);
1350 if (ISD::SHL_PARTS == opcode) {
1351 BuildMI(BB, PPC::SLW, 2, Tmp2).addReg(ShiftOpHi).addReg(SHReg);
1352 BuildMI(BB, PPC::SRW, 2, Tmp3).addReg(ShiftOpLo).addReg(Tmp1);
1353 BuildMI(BB, PPC::OR, 2, Tmp4).addReg(Tmp2).addReg(Tmp3);
1354 BuildMI(BB, PPC::ADDI, 2, Tmp5).addReg(SHReg).addSImm(-32);
1355 BuildMI(BB, PPC::SLW, 2, Tmp6).addReg(ShiftOpHi).addReg(Tmp5);
1356 BuildMI(BB, PPC::OR, 2, Result+1).addReg(Tmp4).addReg(Tmp6);
1357 BuildMI(BB, PPC::SLW, 2, Result).addReg(ShiftOpLo).addReg(SHReg);
1358 } else if (ISD::SRL_PARTS == opcode) {
1359 BuildMI(BB, PPC::SRW, 2, Tmp2).addReg(ShiftOpLo).addReg(SHReg);
1360 BuildMI(BB, PPC::SLW, 2, Tmp3).addReg(ShiftOpHi).addReg(Tmp1);
1361 BuildMI(BB, PPC::OR, 2, Tmp4).addReg(Tmp2).addReg(Tmp3);
1362 BuildMI(BB, PPC::ADDI, 2, Tmp5).addReg(SHReg).addSImm(-32);
1363 BuildMI(BB, PPC::SRW, 2, Tmp6).addReg(ShiftOpHi).addReg(Tmp5);
1364 BuildMI(BB, PPC::OR, 2, Result).addReg(Tmp4).addReg(Tmp6);
1365 BuildMI(BB, PPC::SRW, 2, Result+1).addReg(ShiftOpHi).addReg(SHReg);
1366 } else {
1367 MachineBasicBlock *TmpMBB = new MachineBasicBlock(BB->getBasicBlock());
1368 MachineBasicBlock *PhiMBB = new MachineBasicBlock(BB->getBasicBlock());
1369 MachineBasicBlock *OldMBB = BB;
1370 MachineFunction *F = BB->getParent();
1371 ilist<MachineBasicBlock>::iterator It = BB; ++It;
1372 F->getBasicBlockList().insert(It, TmpMBB);
1373 F->getBasicBlockList().insert(It, PhiMBB);
1374 BB->addSuccessor(TmpMBB);
1375 BB->addSuccessor(PhiMBB);
1376 BuildMI(BB, PPC::SRW, 2, Tmp2).addReg(ShiftOpLo).addReg(SHReg);
1377 BuildMI(BB, PPC::SLW, 2, Tmp3).addReg(ShiftOpHi).addReg(Tmp1);
1378 BuildMI(BB, PPC::OR, 2, Tmp4).addReg(Tmp2).addReg(Tmp3);
1379 BuildMI(BB, PPC::ADDICo, 2, Tmp5).addReg(SHReg).addSImm(-32);
1380 BuildMI(BB, PPC::SRAW, 2, Tmp6).addReg(ShiftOpHi).addReg(Tmp5);
1381 BuildMI(BB, PPC::SRAW, 2, Result+1).addReg(ShiftOpHi).addReg(SHReg);
1382 BuildMI(BB, PPC::BLE, 2).addReg(PPC::CR0).addMBB(PhiMBB);
1383 // Select correct least significant half if the shift amount > 32
1384 BB = TmpMBB;
1385 unsigned Tmp7 = MakeReg(MVT::i32);
1386 BuildMI(BB, PPC::OR, 2, Tmp7).addReg(Tmp6).addReg(Tmp6);
1387 TmpMBB->addSuccessor(PhiMBB);
1388 BB = PhiMBB;
1389 BuildMI(BB, PPC::PHI, 4, Result).addReg(Tmp4).addMBB(OldMBB)
1390 .addReg(Tmp7).addMBB(TmpMBB);
Nate Begemanca12a2b2005-03-28 22:28:37 +00001391 }
1392 return Result+N.ResNo;
1393 }
1394
Nate Begemana9795f82005-03-24 04:41:43 +00001395 case ISD::FP_TO_UINT:
Nate Begeman6b559972005-04-01 02:59:27 +00001396 case ISD::FP_TO_SINT: {
1397 bool U = (ISD::FP_TO_UINT == opcode);
1398 Tmp1 = SelectExpr(N.getOperand(0));
1399 if (!U) {
1400 Tmp2 = MakeReg(MVT::f64);
1401 BuildMI(BB, PPC::FCTIWZ, 1, Tmp2).addReg(Tmp1);
1402 int FrameIdx = BB->getParent()->getFrameInfo()->CreateStackObject(8, 8);
1403 addFrameReference(BuildMI(BB, PPC::STFD, 3).addReg(Tmp2), FrameIdx);
1404 addFrameReference(BuildMI(BB, PPC::LWZ, 2, Result), FrameIdx, 4);
1405 return Result;
1406 } else {
1407 unsigned Zero = getConstDouble(0.0);
1408 unsigned MaxInt = getConstDouble((1LL << 32) - 1);
1409 unsigned Border = getConstDouble(1LL << 31);
1410 unsigned UseZero = MakeReg(MVT::f64);
1411 unsigned UseMaxInt = MakeReg(MVT::f64);
1412 unsigned UseChoice = MakeReg(MVT::f64);
1413 unsigned TmpReg = MakeReg(MVT::f64);
1414 unsigned TmpReg2 = MakeReg(MVT::f64);
1415 unsigned ConvReg = MakeReg(MVT::f64);
1416 unsigned IntTmp = MakeReg(MVT::i32);
1417 unsigned XorReg = MakeReg(MVT::i32);
1418 MachineFunction *F = BB->getParent();
1419 int FrameIdx = F->getFrameInfo()->CreateStackObject(8, 8);
1420 // Update machine-CFG edges
1421 MachineBasicBlock *XorMBB = new MachineBasicBlock(BB->getBasicBlock());
1422 MachineBasicBlock *PhiMBB = new MachineBasicBlock(BB->getBasicBlock());
1423 MachineBasicBlock *OldMBB = BB;
1424 ilist<MachineBasicBlock>::iterator It = BB; ++It;
1425 F->getBasicBlockList().insert(It, XorMBB);
1426 F->getBasicBlockList().insert(It, PhiMBB);
1427 BB->addSuccessor(XorMBB);
1428 BB->addSuccessor(PhiMBB);
1429 // Convert from floating point to unsigned 32-bit value
1430 // Use 0 if incoming value is < 0.0
1431 BuildMI(BB, PPC::FSEL, 3, UseZero).addReg(Tmp1).addReg(Tmp1).addReg(Zero);
1432 // Use 2**32 - 1 if incoming value is >= 2**32
1433 BuildMI(BB, PPC::FSUB, 2, UseMaxInt).addReg(MaxInt).addReg(Tmp1);
1434 BuildMI(BB, PPC::FSEL, 3, UseChoice).addReg(UseMaxInt).addReg(UseZero)
1435 .addReg(MaxInt);
1436 // Subtract 2**31
1437 BuildMI(BB, PPC::FSUB, 2, TmpReg).addReg(UseChoice).addReg(Border);
1438 // Use difference if >= 2**31
1439 BuildMI(BB, PPC::FCMPU, 2, PPC::CR0).addReg(UseChoice).addReg(Border);
1440 BuildMI(BB, PPC::FSEL, 3, TmpReg2).addReg(TmpReg).addReg(TmpReg)
1441 .addReg(UseChoice);
1442 // Convert to integer
1443 BuildMI(BB, PPC::FCTIWZ, 1, ConvReg).addReg(TmpReg2);
1444 addFrameReference(BuildMI(BB, PPC::STFD, 3).addReg(ConvReg), FrameIdx);
1445 addFrameReference(BuildMI(BB, PPC::LWZ, 2, IntTmp), FrameIdx, 4);
1446 BuildMI(BB, PPC::BLT, 2).addReg(PPC::CR0).addMBB(PhiMBB);
1447 BuildMI(BB, PPC::B, 1).addMBB(XorMBB);
1448
1449 // XorMBB:
1450 // add 2**31 if input was >= 2**31
1451 BB = XorMBB;
1452 BuildMI(BB, PPC::XORIS, 2, XorReg).addReg(IntTmp).addImm(0x8000);
1453 XorMBB->addSuccessor(PhiMBB);
1454
1455 // PhiMBB:
1456 // DestReg = phi [ IntTmp, OldMBB ], [ XorReg, XorMBB ]
1457 BB = PhiMBB;
1458 BuildMI(BB, PPC::PHI, 4, Result).addReg(IntTmp).addMBB(OldMBB)
1459 .addReg(XorReg).addMBB(XorMBB);
1460 return Result;
1461 }
1462 assert(0 && "Should never get here");
1463 return 0;
1464 }
Nate Begemana9795f82005-03-24 04:41:43 +00001465
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001466 case ISD::SETCC:
Nate Begeman33162522005-03-29 21:54:38 +00001467 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Node)) {
Nate Begemandffcfcc2005-04-01 00:32:34 +00001468 Opc = SelectSetCR0(N);
Nate Begeman33162522005-03-29 21:54:38 +00001469
Nate Begeman31318e42005-04-01 07:21:30 +00001470 unsigned TrueValue = MakeReg(MVT::i32);
1471 BuildMI(BB, PPC::LI, 1, TrueValue).addSImm(1);
1472 unsigned FalseValue = MakeReg(MVT::i32);
1473 BuildMI(BB, PPC::LI, 1, FalseValue).addSImm(0);
1474
Nate Begeman33162522005-03-29 21:54:38 +00001475 // Create an iterator with which to insert the MBB for copying the false
1476 // value and the MBB to hold the PHI instruction for this SetCC.
1477 MachineBasicBlock *thisMBB = BB;
1478 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1479 ilist<MachineBasicBlock>::iterator It = BB;
1480 ++It;
1481
1482 // thisMBB:
1483 // ...
1484 // cmpTY cr0, r1, r2
1485 // %TrueValue = li 1
1486 // bCC sinkMBB
Nate Begeman33162522005-03-29 21:54:38 +00001487 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
1488 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
1489 BuildMI(BB, Opc, 2).addReg(PPC::CR0).addMBB(sinkMBB);
1490 MachineFunction *F = BB->getParent();
1491 F->getBasicBlockList().insert(It, copy0MBB);
1492 F->getBasicBlockList().insert(It, sinkMBB);
1493 // Update machine-CFG edges
1494 BB->addSuccessor(copy0MBB);
1495 BB->addSuccessor(sinkMBB);
1496
1497 // copy0MBB:
1498 // %FalseValue = li 0
1499 // fallthrough
1500 BB = copy0MBB;
Nate Begeman33162522005-03-29 21:54:38 +00001501 // Update machine-CFG edges
1502 BB->addSuccessor(sinkMBB);
1503
1504 // sinkMBB:
1505 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1506 // ...
1507 BB = sinkMBB;
1508 BuildMI(BB, PPC::PHI, 4, Result).addReg(FalseValue)
1509 .addMBB(copy0MBB).addReg(TrueValue).addMBB(thisMBB);
1510 return Result;
1511 }
1512 assert(0 && "Is this legal?");
1513 return 0;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001514
Nate Begeman74747862005-03-29 22:24:51 +00001515 case ISD::SELECT: {
Chris Lattner30710192005-04-01 07:10:02 +00001516 unsigned TrueValue = SelectExpr(N.getOperand(1)); //Use if TRUE
1517 unsigned FalseValue = SelectExpr(N.getOperand(2)); //Use if FALSE
Nate Begeman6cb2e1b2005-04-01 08:57:43 +00001518 Opc = SelectSetCR0(N.getOperand(0));
Chris Lattner30710192005-04-01 07:10:02 +00001519
Nate Begeman74747862005-03-29 22:24:51 +00001520 // Create an iterator with which to insert the MBB for copying the false
1521 // value and the MBB to hold the PHI instruction for this SetCC.
1522 MachineBasicBlock *thisMBB = BB;
1523 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1524 ilist<MachineBasicBlock>::iterator It = BB;
1525 ++It;
1526
1527 // thisMBB:
1528 // ...
1529 // TrueVal = ...
1530 // cmpTY cr0, r1, r2
1531 // bCC copy1MBB
1532 // fallthrough --> copy0MBB
Nate Begeman74747862005-03-29 22:24:51 +00001533 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
1534 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
Nate Begeman3e897162005-03-31 23:55:40 +00001535 BuildMI(BB, Opc, 2).addReg(PPC::CR0).addMBB(sinkMBB);
Nate Begeman74747862005-03-29 22:24:51 +00001536 MachineFunction *F = BB->getParent();
1537 F->getBasicBlockList().insert(It, copy0MBB);
1538 F->getBasicBlockList().insert(It, sinkMBB);
1539 // Update machine-CFG edges
1540 BB->addSuccessor(copy0MBB);
1541 BB->addSuccessor(sinkMBB);
1542
1543 // copy0MBB:
1544 // %FalseValue = ...
1545 // # fallthrough to sinkMBB
1546 BB = copy0MBB;
Nate Begeman74747862005-03-29 22:24:51 +00001547 // Update machine-CFG edges
1548 BB->addSuccessor(sinkMBB);
1549
1550 // sinkMBB:
1551 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1552 // ...
1553 BB = sinkMBB;
1554 BuildMI(BB, PPC::PHI, 4, Result).addReg(FalseValue)
1555 .addMBB(copy0MBB).addReg(TrueValue).addMBB(thisMBB);
1556
1557 // FIXME: Select i64?
1558 return Result;
1559 }
Nate Begemana9795f82005-03-24 04:41:43 +00001560
1561 case ISD::Constant:
1562 switch (N.getValueType()) {
1563 default: assert(0 && "Cannot use constants of this type!");
1564 case MVT::i1:
1565 BuildMI(BB, PPC::LI, 1, Result)
1566 .addSImm(!cast<ConstantSDNode>(N)->isNullValue());
1567 break;
1568 case MVT::i32:
1569 {
1570 int v = (int)cast<ConstantSDNode>(N)->getSignExtended();
1571 if (v < 32768 && v >= -32768) {
1572 BuildMI(BB, PPC::LI, 1, Result).addSImm(v);
1573 } else {
Nate Begeman5e966612005-03-24 06:28:42 +00001574 Tmp1 = MakeReg(MVT::i32);
1575 BuildMI(BB, PPC::LIS, 1, Tmp1).addSImm(v >> 16);
1576 BuildMI(BB, PPC::ORI, 2, Result).addReg(Tmp1).addImm(v & 0xFFFF);
Nate Begemana9795f82005-03-24 04:41:43 +00001577 }
1578 }
1579 }
1580 return Result;
1581 }
1582
1583 return 0;
1584}
1585
1586void ISel::Select(SDOperand N) {
1587 unsigned Tmp1, Tmp2, Opc;
1588 unsigned opcode = N.getOpcode();
1589
1590 if (!ExprMap.insert(std::make_pair(N, 1)).second)
1591 return; // Already selected.
1592
1593 SDNode *Node = N.Val;
1594
1595 switch (Node->getOpcode()) {
1596 default:
1597 Node->dump(); std::cerr << "\n";
1598 assert(0 && "Node not handled yet!");
1599 case ISD::EntryToken: return; // Noop
1600 case ISD::TokenFactor:
1601 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1602 Select(Node->getOperand(i));
1603 return;
1604 case ISD::ADJCALLSTACKDOWN:
1605 case ISD::ADJCALLSTACKUP:
1606 Select(N.getOperand(0));
1607 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1608 Opc = N.getOpcode() == ISD::ADJCALLSTACKDOWN ? PPC::ADJCALLSTACKDOWN :
1609 PPC::ADJCALLSTACKUP;
1610 BuildMI(BB, Opc, 1).addImm(Tmp1);
1611 return;
1612 case ISD::BR: {
1613 MachineBasicBlock *Dest =
1614 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
Nate Begemana9795f82005-03-24 04:41:43 +00001615 Select(N.getOperand(0));
1616 BuildMI(BB, PPC::B, 1).addMBB(Dest);
1617 return;
1618 }
1619 case ISD::BRCOND:
1620 SelectBranchCC(N);
1621 return;
1622 case ISD::CopyToReg:
1623 Select(N.getOperand(0));
1624 Tmp1 = SelectExpr(N.getOperand(1));
1625 Tmp2 = cast<RegSDNode>(N)->getReg();
1626
1627 if (Tmp1 != Tmp2) {
1628 if (N.getOperand(1).getValueType() == MVT::f64 ||
1629 N.getOperand(1).getValueType() == MVT::f32)
1630 BuildMI(BB, PPC::FMR, 1, Tmp2).addReg(Tmp1);
1631 else
1632 BuildMI(BB, PPC::OR, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
1633 }
1634 return;
1635 case ISD::ImplicitDef:
1636 Select(N.getOperand(0));
1637 BuildMI(BB, PPC::IMPLICIT_DEF, 0, cast<RegSDNode>(N)->getReg());
1638 return;
1639 case ISD::RET:
1640 switch (N.getNumOperands()) {
1641 default:
1642 assert(0 && "Unknown return instruction!");
1643 case 3:
1644 assert(N.getOperand(1).getValueType() == MVT::i32 &&
1645 N.getOperand(2).getValueType() == MVT::i32 &&
1646 "Unknown two-register value!");
1647 Select(N.getOperand(0));
1648 Tmp1 = SelectExpr(N.getOperand(1));
1649 Tmp2 = SelectExpr(N.getOperand(2));
Nate Begeman27523a12005-04-02 00:42:16 +00001650 BuildMI(BB, PPC::OR, 2, PPC::R3).addReg(Tmp2).addReg(Tmp2);
1651 BuildMI(BB, PPC::OR, 2, PPC::R4).addReg(Tmp1).addReg(Tmp1);
Nate Begemana9795f82005-03-24 04:41:43 +00001652 break;
1653 case 2:
1654 Select(N.getOperand(0));
1655 Tmp1 = SelectExpr(N.getOperand(1));
1656 switch (N.getOperand(1).getValueType()) {
1657 default:
1658 assert(0 && "Unknown return type!");
1659 case MVT::f64:
1660 case MVT::f32:
1661 BuildMI(BB, PPC::FMR, 1, PPC::F1).addReg(Tmp1);
1662 break;
1663 case MVT::i32:
1664 BuildMI(BB, PPC::OR, 2, PPC::R3).addReg(Tmp1).addReg(Tmp1);
1665 break;
1666 }
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001667 case 1:
1668 Select(N.getOperand(0));
1669 break;
Nate Begemana9795f82005-03-24 04:41:43 +00001670 }
1671 BuildMI(BB, PPC::BLR, 0); // Just emit a 'ret' instruction
1672 return;
Nate Begemana9795f82005-03-24 04:41:43 +00001673 case ISD::TRUNCSTORE:
1674 case ISD::STORE:
1675 {
1676 SDOperand Chain = N.getOperand(0);
1677 SDOperand Value = N.getOperand(1);
1678 SDOperand Address = N.getOperand(2);
1679 Select(Chain);
1680
1681 Tmp1 = SelectExpr(Value); //value
1682
1683 if (opcode == ISD::STORE) {
1684 switch(Value.getValueType()) {
1685 default: assert(0 && "unknown Type in store");
1686 case MVT::i32: Opc = PPC::STW; break;
1687 case MVT::f64: Opc = PPC::STFD; break;
1688 case MVT::f32: Opc = PPC::STFS; break;
1689 }
1690 } else { //ISD::TRUNCSTORE
1691 switch(cast<MVTSDNode>(Node)->getExtraValueType()) {
1692 default: assert(0 && "unknown Type in store");
1693 case MVT::i1: //FIXME: DAG does not promote this load
1694 case MVT::i8: Opc = PPC::STB; break;
1695 case MVT::i16: Opc = PPC::STH; break;
1696 }
1697 }
1698
Nate Begemana7e11a42005-04-01 05:57:17 +00001699 if(Address.getOpcode() == ISD::FrameIndex)
Nate Begemana9795f82005-03-24 04:41:43 +00001700 {
Nate Begeman58f718c2005-03-30 02:23:08 +00001701 Tmp2 = cast<FrameIndexSDNode>(Address)->getIndex();
1702 addFrameReference(BuildMI(BB, Opc, 3).addReg(Tmp1), (int)Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +00001703 }
1704 else
1705 {
1706 int offset;
Nate Begeman04730362005-04-01 04:45:11 +00001707 bool idx = SelectAddr(Address, Tmp2, offset);
1708 if (idx) {
1709 Opc = IndexedOpForOp(Opc);
1710 BuildMI(BB, Opc, 3).addReg(Tmp1).addReg(Tmp2).addReg(offset);
1711 } else {
1712 BuildMI(BB, Opc, 3).addReg(Tmp1).addImm(offset).addReg(Tmp2);
1713 }
Nate Begemana9795f82005-03-24 04:41:43 +00001714 }
1715 return;
1716 }
1717 case ISD::EXTLOAD:
1718 case ISD::SEXTLOAD:
1719 case ISD::ZEXTLOAD:
1720 case ISD::LOAD:
1721 case ISD::CopyFromReg:
1722 case ISD::CALL:
1723 case ISD::DYNAMIC_STACKALLOC:
1724 ExprMap.erase(N);
1725 SelectExpr(N);
1726 return;
1727 }
1728 assert(0 && "Should not be reached!");
1729}
1730
1731
1732/// createPPC32PatternInstructionSelector - This pass converts an LLVM function
1733/// into a machine code representation using pattern matching and a machine
1734/// description file.
1735///
1736FunctionPass *llvm::createPPC32ISelPattern(TargetMachine &TM) {
1737 return new ISel(TM);
Chris Lattner246fa632005-03-24 06:16:18 +00001738}
1739