blob: 6c6ad337b949bf8a190e38bed7fb5e3e01a8fbc7 [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.
Nate Begemanfa554702005-04-03 22:13:27 +0000195 if (F.isVarArg()) {
Nate Begemana9795f82005-03-24 04:41:43 +0000196 VarArgsFrameIndex = MFI->CreateFixedObject(4, ArgOffset);
Nate Begemanfa554702005-04-03 22:13:27 +0000197 // If this function is vararg, store r4-r10 to their spots on the stack so
198 // that they may be loaded by dereferencing va_next
199 SDOperand FIN = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
200 SDOperand Val = DAG.getCopyFromReg(PPC::R4, MVT::i32, DAG.getRoot());
201 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Val, Val, FIN);
202 DAG.setRoot(Val.getValue(1));
203 ArgValues.push_back(Store);
204 }
Nate Begemana9795f82005-03-24 04:41:43 +0000205
206 return ArgValues;
207}
208
209std::pair<SDOperand, SDOperand>
210PPC32TargetLowering::LowerCallTo(SDOperand Chain,
Nate Begeman307e7442005-03-26 01:28:53 +0000211 const Type *RetTy, bool isVarArg,
212 SDOperand Callee, ArgListTy &Args, SelectionDAG &DAG) {
213 // args_to_use will accumulate outgoing args for the ISD::CALL case in
214 // SelectExpr to use to put the arguments in the appropriate registers.
Nate Begemana9795f82005-03-24 04:41:43 +0000215 std::vector<SDOperand> args_to_use;
Nate Begeman307e7442005-03-26 01:28:53 +0000216
217 // Count how many bytes are to be pushed on the stack, including the linkage
218 // area, and parameter passing area.
219 unsigned NumBytes = 24;
220
221 if (Args.empty()) {
Nate Begemana7e11a42005-04-01 05:57:17 +0000222 Chain = DAG.getNode(ISD::ADJCALLSTACKDOWN, MVT::Other, Chain,
223 DAG.getConstant(NumBytes, getPointerTy()));
Nate Begeman307e7442005-03-26 01:28:53 +0000224 } else {
225 for (unsigned i = 0, e = Args.size(); i != e; ++i)
226 switch (getValueType(Args[i].second)) {
227 default: assert(0 && "Unknown value type!");
228 case MVT::i1:
229 case MVT::i8:
230 case MVT::i16:
231 case MVT::i32:
232 case MVT::f32:
233 NumBytes += 4;
234 break;
235 case MVT::i64:
236 case MVT::f64:
237 NumBytes += 8;
238 break;
239 }
240
241 // Just to be safe, we'll always reserve the full 24 bytes of linkage area
242 // plus 32 bytes of argument space in case any called code gets funky on us.
243 if (NumBytes < 56) NumBytes = 56;
244
245 // Adjust the stack pointer for the new arguments...
246 // These operations are automatically eliminated by the prolog/epilog pass
247 Chain = DAG.getNode(ISD::ADJCALLSTACKDOWN, MVT::Other, Chain,
248 DAG.getConstant(NumBytes, getPointerTy()));
249
250 // Set up a copy of the stack pointer for use loading and storing any
251 // arguments that may not fit in the registers available for argument
252 // passing.
253 SDOperand StackPtr = DAG.getCopyFromReg(PPC::R1, MVT::i32,
254 DAG.getEntryNode());
255
256 // Figure out which arguments are going to go in registers, and which in
257 // memory. Also, if this is a vararg function, floating point operations
258 // must be stored to our stack, and loaded into integer regs as well, if
259 // any integer regs are available for argument passing.
260 unsigned ArgOffset = 24;
261 unsigned GPR_remaining = 8;
262 unsigned FPR_remaining = 13;
Nate Begeman74d73452005-03-31 00:15:26 +0000263
264 std::vector<SDOperand> MemOps;
Nate Begeman307e7442005-03-26 01:28:53 +0000265 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
266 // PtrOff will be used to store the current argument to the stack if a
267 // register cannot be found for it.
268 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
269 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
Nate Begemanf7e43382005-03-26 07:46:36 +0000270 MVT::ValueType ArgVT = getValueType(Args[i].second);
Nate Begeman307e7442005-03-26 01:28:53 +0000271
Nate Begemanf7e43382005-03-26 07:46:36 +0000272 switch (ArgVT) {
Nate Begeman307e7442005-03-26 01:28:53 +0000273 default: assert(0 && "Unexpected ValueType for argument!");
274 case MVT::i1:
275 case MVT::i8:
276 case MVT::i16:
277 // Promote the integer to 32 bits. If the input type is signed use a
278 // sign extend, otherwise use a zero extend.
279 if (Args[i].second->isSigned())
280 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
281 else
282 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
283 // FALL THROUGH
284 case MVT::i32:
285 if (GPR_remaining > 0) {
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000286 args_to_use.push_back(Args[i].first);
Nate Begeman307e7442005-03-26 01:28:53 +0000287 --GPR_remaining;
288 } else {
Nate Begeman74d73452005-03-31 00:15:26 +0000289 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
290 Args[i].first, PtrOff));
Nate Begeman307e7442005-03-26 01:28:53 +0000291 }
292 ArgOffset += 4;
293 break;
294 case MVT::i64:
Nate Begemanf7e43382005-03-26 07:46:36 +0000295 // If we have one free GPR left, we can place the upper half of the i64
296 // in it, and store the other half to the stack. If we have two or more
297 // free GPRs, then we can pass both halves of the i64 in registers.
298 if (GPR_remaining > 0) {
Nate Begemanf2622612005-03-26 02:17:46 +0000299 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
300 Args[i].first, DAG.getConstant(1, MVT::i32));
301 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
302 Args[i].first, DAG.getConstant(0, MVT::i32));
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000303 args_to_use.push_back(Hi);
Nate Begeman74d73452005-03-31 00:15:26 +0000304 --GPR_remaining;
Nate Begeman74d73452005-03-31 00:15:26 +0000305 if (GPR_remaining > 0) {
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000306 args_to_use.push_back(Lo);
Nate Begeman74d73452005-03-31 00:15:26 +0000307 --GPR_remaining;
Nate Begemanf7e43382005-03-26 07:46:36 +0000308 } else {
309 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
310 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
Nate Begeman74d73452005-03-31 00:15:26 +0000311 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
312 Lo, PtrOff));
Nate Begemanf7e43382005-03-26 07:46:36 +0000313 }
Nate Begeman307e7442005-03-26 01:28:53 +0000314 } else {
Nate Begeman74d73452005-03-31 00:15:26 +0000315 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
316 Args[i].first, PtrOff));
Nate Begeman307e7442005-03-26 01:28:53 +0000317 }
318 ArgOffset += 8;
319 break;
320 case MVT::f32:
Nate Begeman307e7442005-03-26 01:28:53 +0000321 case MVT::f64:
Nate Begemanf7e43382005-03-26 07:46:36 +0000322 if (FPR_remaining > 0) {
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000323 args_to_use.push_back(Args[i].first);
324 --FPR_remaining;
Nate Begemanf7e43382005-03-26 07:46:36 +0000325 if (isVarArg) {
Nate Begeman96fc6812005-03-31 02:05:53 +0000326 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Chain,
327 Args[i].first, PtrOff);
328 MemOps.push_back(Store);
Nate Begeman74d73452005-03-31 00:15:26 +0000329 // Float varargs are always shadowed in available integer registers
330 if (GPR_remaining > 0) {
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 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
Nate Begeman74d73452005-03-31 00:15:26 +0000337 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
338 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
Nate Begeman96fc6812005-03-31 02:05:53 +0000339 SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff);
Nate Begeman74d73452005-03-31 00:15:26 +0000340 MemOps.push_back(Load);
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000341 args_to_use.push_back(Load);
342 --GPR_remaining;
Nate Begeman74d73452005-03-31 00:15:26 +0000343 }
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000344 } else {
345 // If we have any FPRs remaining, we may also have GPRs remaining.
346 // Args passed in FPRs consume either 1 (f32) or 2 (f64) available
347 // GPRs.
348 if (GPR_remaining > 0) {
349 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
350 --GPR_remaining;
351 }
352 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
353 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
354 --GPR_remaining;
355 }
Nate Begeman74d73452005-03-31 00:15:26 +0000356 }
Nate Begeman307e7442005-03-26 01:28:53 +0000357 } else {
Nate Begeman74d73452005-03-31 00:15:26 +0000358 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
359 Args[i].first, PtrOff));
Nate Begeman307e7442005-03-26 01:28:53 +0000360 }
Nate Begemanf7e43382005-03-26 07:46:36 +0000361 ArgOffset += (ArgVT == MVT::f32) ? 4 : 8;
Nate Begeman307e7442005-03-26 01:28:53 +0000362 break;
363 }
Nate Begemana9795f82005-03-24 04:41:43 +0000364 }
Nate Begeman74d73452005-03-31 00:15:26 +0000365 if (!MemOps.empty())
366 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps);
Nate Begemana9795f82005-03-24 04:41:43 +0000367 }
368
369 std::vector<MVT::ValueType> RetVals;
370 MVT::ValueType RetTyVT = getValueType(RetTy);
371 if (RetTyVT != MVT::isVoid)
372 RetVals.push_back(RetTyVT);
373 RetVals.push_back(MVT::Other);
374
375 SDOperand TheCall = SDOperand(DAG.getCall(RetVals,
376 Chain, Callee, args_to_use), 0);
377 Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
378 Chain = DAG.getNode(ISD::ADJCALLSTACKUP, MVT::Other, Chain,
379 DAG.getConstant(NumBytes, getPointerTy()));
380 return std::make_pair(TheCall, Chain);
381}
382
383std::pair<SDOperand, SDOperand>
384PPC32TargetLowering::LowerVAStart(SDOperand Chain, SelectionDAG &DAG) {
385 //vastart just returns the address of the VarArgsFrameIndex slot.
386 return std::make_pair(DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32), Chain);
387}
388
389std::pair<SDOperand,SDOperand> PPC32TargetLowering::
390LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
391 const Type *ArgTy, SelectionDAG &DAG) {
Nate Begemanc7b09f12005-03-25 08:34:25 +0000392 MVT::ValueType ArgVT = getValueType(ArgTy);
393 SDOperand Result;
394 if (!isVANext) {
395 Result = DAG.getLoad(ArgVT, DAG.getEntryNode(), VAList);
396 } else {
397 unsigned Amt;
398 if (ArgVT == MVT::i32 || ArgVT == MVT::f32)
399 Amt = 4;
400 else {
401 assert((ArgVT == MVT::i64 || ArgVT == MVT::f64) &&
402 "Other types should have been promoted for varargs!");
403 Amt = 8;
404 }
405 Result = DAG.getNode(ISD::ADD, VAList.getValueType(), VAList,
406 DAG.getConstant(Amt, VAList.getValueType()));
407 }
408 return std::make_pair(Result, Chain);
Nate Begemana9795f82005-03-24 04:41:43 +0000409}
410
411
412std::pair<SDOperand, SDOperand> PPC32TargetLowering::
413LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
414 SelectionDAG &DAG) {
Nate Begeman01d05262005-03-30 01:45:43 +0000415 assert(0 && "LowerFrameReturnAddress unimplemented");
Nate Begemana9795f82005-03-24 04:41:43 +0000416 abort();
417}
418
419namespace {
Nate Begemanaa73a9f2005-04-03 11:20:20 +0000420Statistic<>NotLogic("ppc-codegen", "Number of inverted logical ops");
Nate Begemana9795f82005-03-24 04:41:43 +0000421//===--------------------------------------------------------------------===//
422/// ISel - PPC32 specific code to select PPC32 machine instructions for
423/// SelectionDAG operations.
424//===--------------------------------------------------------------------===//
425class ISel : public SelectionDAGISel {
426
427 /// Comment Here.
428 PPC32TargetLowering PPC32Lowering;
429
430 /// ExprMap - As shared expressions are codegen'd, we keep track of which
431 /// vreg the value is produced in, so we only emit one copy of each compiled
432 /// tree.
433 std::map<SDOperand, unsigned> ExprMap;
Nate Begemanc7b09f12005-03-25 08:34:25 +0000434
435 unsigned GlobalBaseReg;
436 bool GlobalBaseInitialized;
Nate Begemana9795f82005-03-24 04:41:43 +0000437
438public:
439 ISel(TargetMachine &TM) : SelectionDAGISel(PPC32Lowering), PPC32Lowering(TM)
440 {}
441
Nate Begemanc7b09f12005-03-25 08:34:25 +0000442 /// runOnFunction - Override this function in order to reset our per-function
443 /// variables.
444 virtual bool runOnFunction(Function &Fn) {
445 // Make sure we re-emit a set of the global base reg if necessary
446 GlobalBaseInitialized = false;
447 return SelectionDAGISel::runOnFunction(Fn);
448 }
449
Nate Begemana9795f82005-03-24 04:41:43 +0000450 /// InstructionSelectBasicBlock - This callback is invoked by
451 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
452 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG) {
453 DEBUG(BB->dump());
454 // Codegen the basic block.
455 Select(DAG.getRoot());
456
457 // Clear state used for selection.
458 ExprMap.clear();
459 }
460
Nate Begemandffcfcc2005-04-01 00:32:34 +0000461 unsigned getGlobalBaseReg();
Nate Begeman6b559972005-04-01 02:59:27 +0000462 unsigned getConstDouble(double floatVal, unsigned Result);
Nate Begemandffcfcc2005-04-01 00:32:34 +0000463 unsigned SelectSetCR0(SDOperand CC);
Nate Begemana9795f82005-03-24 04:41:43 +0000464 unsigned SelectExpr(SDOperand N);
465 unsigned SelectExprFP(SDOperand N, unsigned Result);
466 void Select(SDOperand N);
467
Nate Begeman04730362005-04-01 04:45:11 +0000468 bool SelectAddr(SDOperand N, unsigned& Reg, int& offset);
Nate Begemana9795f82005-03-24 04:41:43 +0000469 void SelectBranchCC(SDOperand N);
470};
471
472/// canUseAsImmediateForOpcode - This method returns a value indicating whether
473/// the ConstantSDNode N can be used as an immediate to Opcode. The return
474/// values are either 0, 1 or 2. 0 indicates that either N is not a
475/// ConstantSDNode, or is not suitable for use by that opcode. A return value
476/// of 1 indicates that the constant may be used in normal immediate form. A
477/// return value of 2 indicates that the constant may be used in shifted
478/// immediate form. If the return value is nonzero, the constant value is
479/// placed in Imm.
480///
481static unsigned canUseAsImmediateForOpcode(SDOperand N, unsigned Opcode,
Nate Begeman3e897162005-03-31 23:55:40 +0000482 unsigned& Imm, bool U = false) {
Nate Begemana9795f82005-03-24 04:41:43 +0000483 if (N.getOpcode() != ISD::Constant) return 0;
484
485 int v = (int)cast<ConstantSDNode>(N)->getSignExtended();
486
487 switch(Opcode) {
488 default: return 0;
489 case ISD::ADD:
490 if (v <= 32767 && v >= -32768) { Imm = v & 0xFFFF; return 1; }
491 if ((v & 0x0000FFFF) == 0) { Imm = v >> 16; return 2; }
492 break;
493 case ISD::AND:
494 case ISD::XOR:
495 case ISD::OR:
496 if (v >= 0 && v <= 65535) { Imm = v & 0xFFFF; return 1; }
497 if ((v & 0x0000FFFF) == 0) { Imm = v >> 16; return 2; }
498 break;
Nate Begeman307e7442005-03-26 01:28:53 +0000499 case ISD::MUL:
Nate Begeman27523a12005-04-02 00:42:16 +0000500 case ISD::SUB:
Nate Begeman307e7442005-03-26 01:28:53 +0000501 if (v <= 32767 && v >= -32768) { Imm = v & 0xFFFF; return 1; }
502 break;
Nate Begeman3e897162005-03-31 23:55:40 +0000503 case ISD::SETCC:
504 if (U && (v >= 0 && v <= 65535)) { Imm = v & 0xFFFF; return 1; }
505 if (!U && (v <= 32767 && v >= -32768)) { Imm = v & 0xFFFF; return 1; }
506 break;
Nate Begemana9795f82005-03-24 04:41:43 +0000507 }
508 return 0;
509}
Nate Begeman3e897162005-03-31 23:55:40 +0000510
511/// getBCCForSetCC - Returns the PowerPC condition branch mnemonic corresponding
512/// to Condition. If the Condition is unordered or unsigned, the bool argument
513/// U is set to true, otherwise it is set to false.
514static unsigned getBCCForSetCC(unsigned Condition, bool& U) {
515 U = false;
516 switch (Condition) {
517 default: assert(0 && "Unknown condition!"); abort();
518 case ISD::SETEQ: return PPC::BEQ;
519 case ISD::SETNE: return PPC::BNE;
520 case ISD::SETULT: U = true;
521 case ISD::SETLT: return PPC::BLT;
522 case ISD::SETULE: U = true;
523 case ISD::SETLE: return PPC::BLE;
524 case ISD::SETUGT: U = true;
525 case ISD::SETGT: return PPC::BGT;
526 case ISD::SETUGE: U = true;
527 case ISD::SETGE: return PPC::BGE;
528 }
Nate Begeman04730362005-04-01 04:45:11 +0000529 return 0;
530}
531
532/// IndexedOpForOp - Return the indexed variant for each of the PowerPC load
533/// and store immediate instructions.
534static unsigned IndexedOpForOp(unsigned Opcode) {
535 switch(Opcode) {
536 default: assert(0 && "Unknown opcode!"); abort();
537 case PPC::LBZ: return PPC::LBZX; case PPC::STB: return PPC::STBX;
538 case PPC::LHZ: return PPC::LHZX; case PPC::STH: return PPC::STHX;
539 case PPC::LHA: return PPC::LHAX; case PPC::STW: return PPC::STWX;
540 case PPC::LWZ: return PPC::LWZX; case PPC::STFS: return PPC::STFSX;
541 case PPC::LFS: return PPC::LFSX; case PPC::STFD: return PPC::STFDX;
542 case PPC::LFD: return PPC::LFDX;
543 }
544 return 0;
Nate Begeman3e897162005-03-31 23:55:40 +0000545}
Nate Begemana9795f82005-03-24 04:41:43 +0000546}
547
Nate Begemanc7b09f12005-03-25 08:34:25 +0000548/// getGlobalBaseReg - Output the instructions required to put the
549/// base address to use for accessing globals into a register.
550///
551unsigned ISel::getGlobalBaseReg() {
552 if (!GlobalBaseInitialized) {
553 // Insert the set of GlobalBaseReg into the first MBB of the function
554 MachineBasicBlock &FirstMBB = BB->getParent()->front();
555 MachineBasicBlock::iterator MBBI = FirstMBB.begin();
556 GlobalBaseReg = MakeReg(MVT::i32);
557 BuildMI(FirstMBB, MBBI, PPC::MovePCtoLR, 0, PPC::LR);
558 BuildMI(FirstMBB, MBBI, PPC::MFLR, 1, GlobalBaseReg).addReg(PPC::LR);
559 GlobalBaseInitialized = true;
560 }
561 return GlobalBaseReg;
562}
563
Nate Begeman6b559972005-04-01 02:59:27 +0000564/// getConstDouble - Loads a floating point value into a register, via the
565/// Constant Pool. Optionally takes a register in which to load the value.
566unsigned ISel::getConstDouble(double doubleVal, unsigned Result=0) {
567 unsigned Tmp1 = MakeReg(MVT::i32);
568 if (0 == Result) Result = MakeReg(MVT::f64);
569 MachineConstantPool *CP = BB->getParent()->getConstantPool();
570 ConstantFP *CFP = ConstantFP::get(Type::DoubleTy, doubleVal);
571 unsigned CPI = CP->getConstantPoolIndex(CFP);
572 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp1).addReg(getGlobalBaseReg())
573 .addConstantPoolIndex(CPI);
574 BuildMI(BB, PPC::LFD, 2, Result).addConstantPoolIndex(CPI).addReg(Tmp1);
575 return Result;
576}
577
Nate Begemandffcfcc2005-04-01 00:32:34 +0000578unsigned ISel::SelectSetCR0(SDOperand CC) {
579 unsigned Opc, Tmp1, Tmp2;
580 static const unsigned CompareOpcodes[] =
581 { PPC::FCMPU, PPC::FCMPU, PPC::CMPW, PPC::CMPLW };
582
583 // If the first operand to the select is a SETCC node, then we can fold it
584 // into the branch that selects which value to return.
585 SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(CC.Val);
586 if (SetCC && CC.getOpcode() == ISD::SETCC) {
587 bool U;
588 Opc = getBCCForSetCC(SetCC->getCondition(), U);
589 Tmp1 = SelectExpr(SetCC->getOperand(0));
590
591 // Pass the optional argument U to canUseAsImmediateForOpcode for SETCC,
592 // so that it knows whether the SETCC immediate range is signed or not.
593 if (1 == canUseAsImmediateForOpcode(SetCC->getOperand(1), ISD::SETCC,
594 Tmp2, U)) {
595 if (U)
596 BuildMI(BB, PPC::CMPLWI, 2, PPC::CR0).addReg(Tmp1).addImm(Tmp2);
597 else
598 BuildMI(BB, PPC::CMPWI, 2, PPC::CR0).addReg(Tmp1).addSImm(Tmp2);
599 } else {
600 bool IsInteger = MVT::isInteger(SetCC->getOperand(0).getValueType());
601 unsigned CompareOpc = CompareOpcodes[2 * IsInteger + U];
602 Tmp2 = SelectExpr(SetCC->getOperand(1));
603 BuildMI(BB, CompareOpc, 2, PPC::CR0).addReg(Tmp1).addReg(Tmp2);
604 }
605 } else {
606 Tmp1 = SelectExpr(CC);
607 BuildMI(BB, PPC::CMPLWI, 2, PPC::CR0).addReg(Tmp1).addImm(0);
608 Opc = PPC::BNE;
609 }
610 return Opc;
611}
612
613/// Check to see if the load is a constant offset from a base register
Nate Begeman04730362005-04-01 04:45:11 +0000614bool ISel::SelectAddr(SDOperand N, unsigned& Reg, int& offset)
Nate Begemana9795f82005-03-24 04:41:43 +0000615{
Nate Begeman96fc6812005-03-31 02:05:53 +0000616 unsigned imm = 0, opcode = N.getOpcode();
Nate Begeman04730362005-04-01 04:45:11 +0000617 if (N.getOpcode() == ISD::ADD) {
618 Reg = SelectExpr(N.getOperand(0));
Nate Begeman96fc6812005-03-31 02:05:53 +0000619 if (1 == canUseAsImmediateForOpcode(N.getOperand(1), opcode, imm)) {
Nate Begeman96fc6812005-03-31 02:05:53 +0000620 offset = imm;
Nate Begeman04730362005-04-01 04:45:11 +0000621 return false;
622 }
623 offset = SelectExpr(N.getOperand(1));
624 return true;
625 }
Nate Begemana9795f82005-03-24 04:41:43 +0000626 Reg = SelectExpr(N);
627 offset = 0;
Nate Begeman04730362005-04-01 04:45:11 +0000628 return false;
Nate Begemana9795f82005-03-24 04:41:43 +0000629}
630
631void ISel::SelectBranchCC(SDOperand N)
632{
633 assert(N.getOpcode() == ISD::BRCOND && "Not a BranchCC???");
634 MachineBasicBlock *Dest =
635 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
Nate Begeman3e897162005-03-31 23:55:40 +0000636
Nate Begemana9795f82005-03-24 04:41:43 +0000637 Select(N.getOperand(0)); //chain
Nate Begemandffcfcc2005-04-01 00:32:34 +0000638 unsigned Opc = SelectSetCR0(N.getOperand(1));
Nate Begeman3e897162005-03-31 23:55:40 +0000639 BuildMI(BB, Opc, 2).addReg(PPC::CR0).addMBB(Dest);
Nate Begemana9795f82005-03-24 04:41:43 +0000640 return;
641}
642
643unsigned ISel::SelectExprFP(SDOperand N, unsigned Result)
644{
645 unsigned Tmp1, Tmp2, Tmp3;
646 unsigned Opc = 0;
647 SDNode *Node = N.Val;
648 MVT::ValueType DestType = N.getValueType();
649 unsigned opcode = N.getOpcode();
650
651 switch (opcode) {
652 default:
653 Node->dump();
654 assert(0 && "Node not handled!\n");
655
Nate Begeman23afcfb2005-03-29 22:48:55 +0000656 case ISD::SELECT: {
Nate Begeman3e897162005-03-31 23:55:40 +0000657 // Attempt to generate FSEL. We can do this whenever we have an FP result,
658 // and an FP comparison in the SetCC node.
659 SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(N.getOperand(0).Val);
660 if (SetCC && N.getOperand(0).getOpcode() == ISD::SETCC &&
661 !MVT::isInteger(SetCC->getOperand(0).getValueType()) &&
662 SetCC->getCondition() != ISD::SETEQ &&
663 SetCC->getCondition() != ISD::SETNE) {
664 MVT::ValueType VT = SetCC->getOperand(0).getValueType();
665 Tmp1 = SelectExpr(SetCC->getOperand(0)); // Val to compare against
666 unsigned TV = SelectExpr(N.getOperand(1)); // Use if TRUE
667 unsigned FV = SelectExpr(N.getOperand(2)); // Use if FALSE
668
669 ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(1));
670 if (CN && (CN->isExactlyValue(-0.0) || CN->isExactlyValue(0.0))) {
671 switch(SetCC->getCondition()) {
672 default: assert(0 && "Invalid FSEL condition"); abort();
673 case ISD::SETULT:
674 case ISD::SETLT:
675 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp1).addReg(FV).addReg(TV);
676 return Result;
677 case ISD::SETUGE:
678 case ISD::SETGE:
679 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp1).addReg(TV).addReg(FV);
680 return Result;
681 case ISD::SETUGT:
682 case ISD::SETGT: {
683 Tmp2 = MakeReg(VT);
684 BuildMI(BB, PPC::FNEG, 1, Tmp2).addReg(Tmp1);
685 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp2).addReg(FV).addReg(TV);
686 return Result;
687 }
688 case ISD::SETULE:
689 case ISD::SETLE: {
690 Tmp2 = MakeReg(VT);
691 BuildMI(BB, PPC::FNEG, 1, Tmp2).addReg(Tmp1);
692 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp2).addReg(TV).addReg(FV);
693 return Result;
694 }
695 }
696 } else {
697 Opc = (MVT::f64 == VT) ? PPC::FSUB : PPC::FSUBS;
698 Tmp2 = SelectExpr(SetCC->getOperand(1));
699 Tmp3 = MakeReg(VT);
700 switch(SetCC->getCondition()) {
701 default: assert(0 && "Invalid FSEL condition"); abort();
702 case ISD::SETULT:
703 case ISD::SETLT:
704 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
705 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(FV).addReg(TV);
706 return Result;
707 case ISD::SETUGE:
708 case ISD::SETGE:
709 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
710 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(TV).addReg(FV);
711 return Result;
712 case ISD::SETUGT:
713 case ISD::SETGT:
714 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp2).addReg(Tmp1);
715 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(FV).addReg(TV);
716 return Result;
717 case ISD::SETULE:
718 case ISD::SETLE:
719 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp2).addReg(Tmp1);
720 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(TV).addReg(FV);
721 return Result;
722 }
723 }
724 assert(0 && "Should never get here");
725 return 0;
726 }
727
Nate Begeman31318e42005-04-01 07:21:30 +0000728 unsigned TrueValue = SelectExpr(N.getOperand(1)); //Use if TRUE
729 unsigned FalseValue = SelectExpr(N.getOperand(2)); //Use if FALSE
Nate Begeman6cb2e1b2005-04-01 08:57:43 +0000730 Opc = SelectSetCR0(N.getOperand(0));
Nate Begeman31318e42005-04-01 07:21:30 +0000731
Nate Begeman23afcfb2005-03-29 22:48:55 +0000732 // Create an iterator with which to insert the MBB for copying the false
733 // value and the MBB to hold the PHI instruction for this SetCC.
734 MachineBasicBlock *thisMBB = BB;
735 const BasicBlock *LLVM_BB = BB->getBasicBlock();
736 ilist<MachineBasicBlock>::iterator It = BB;
737 ++It;
738
739 // thisMBB:
740 // ...
741 // TrueVal = ...
742 // cmpTY cr0, r1, r2
743 // bCC copy1MBB
744 // fallthrough --> copy0MBB
Nate Begeman23afcfb2005-03-29 22:48:55 +0000745 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
746 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
Nate Begeman6cb2e1b2005-04-01 08:57:43 +0000747 BuildMI(BB, Opc, 2).addReg(PPC::CR0).addMBB(sinkMBB);
Nate Begeman23afcfb2005-03-29 22:48:55 +0000748 MachineFunction *F = BB->getParent();
749 F->getBasicBlockList().insert(It, copy0MBB);
750 F->getBasicBlockList().insert(It, sinkMBB);
751 // Update machine-CFG edges
752 BB->addSuccessor(copy0MBB);
753 BB->addSuccessor(sinkMBB);
754
755 // copy0MBB:
756 // %FalseValue = ...
757 // # fallthrough to sinkMBB
758 BB = copy0MBB;
Nate Begeman23afcfb2005-03-29 22:48:55 +0000759 // Update machine-CFG edges
760 BB->addSuccessor(sinkMBB);
761
762 // sinkMBB:
763 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
764 // ...
765 BB = sinkMBB;
766 BuildMI(BB, PPC::PHI, 4, Result).addReg(FalseValue)
767 .addMBB(copy0MBB).addReg(TrueValue).addMBB(thisMBB);
768 return Result;
769 }
Nate Begeman27eeb002005-04-02 05:59:34 +0000770
771 case ISD::FNEG:
772 if (ISD::FABS == N.getOperand(0).getOpcode()) {
773 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
774 BuildMI(BB, PPC::FNABS, 1, Result).addReg(Tmp1);
775 } else {
776 Tmp1 = SelectExpr(N.getOperand(0));
777 BuildMI(BB, PPC::FNEG, 1, Result).addReg(Tmp1);
778 }
779 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +0000780
Nate Begeman27eeb002005-04-02 05:59:34 +0000781 case ISD::FABS:
782 Tmp1 = SelectExpr(N.getOperand(0));
783 BuildMI(BB, PPC::FABS, 1, Result).addReg(Tmp1);
784 return Result;
785
Nate Begemana9795f82005-03-24 04:41:43 +0000786 case ISD::FP_ROUND:
787 assert (DestType == MVT::f32 &&
788 N.getOperand(0).getValueType() == MVT::f64 &&
789 "only f64 to f32 conversion supported here");
790 Tmp1 = SelectExpr(N.getOperand(0));
791 BuildMI(BB, PPC::FRSP, 1, Result).addReg(Tmp1);
792 return Result;
793
794 case ISD::FP_EXTEND:
795 assert (DestType == MVT::f64 &&
796 N.getOperand(0).getValueType() == MVT::f32 &&
797 "only f32 to f64 conversion supported here");
798 Tmp1 = SelectExpr(N.getOperand(0));
799 BuildMI(BB, PPC::FMR, 1, Result).addReg(Tmp1);
800 return Result;
801
802 case ISD::CopyFromReg:
Nate Begemanf2622612005-03-26 02:17:46 +0000803 if (Result == 1)
804 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
805 Tmp1 = dyn_cast<RegSDNode>(Node)->getReg();
806 BuildMI(BB, PPC::FMR, 1, Result).addReg(Tmp1);
807 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +0000808
Nate Begeman6d369cc2005-04-01 01:08:07 +0000809 case ISD::ConstantFP: {
Nate Begeman6d369cc2005-04-01 01:08:07 +0000810 ConstantFPSDNode *CN = cast<ConstantFPSDNode>(N);
Nate Begeman6b559972005-04-01 02:59:27 +0000811 Result = getConstDouble(CN->getValue(), Result);
Nate Begeman6d369cc2005-04-01 01:08:07 +0000812 return Result;
813 }
Nate Begemana9795f82005-03-24 04:41:43 +0000814
815 case ISD::MUL:
816 case ISD::ADD:
817 case ISD::SUB:
818 case ISD::SDIV:
819 switch( opcode ) {
820 case ISD::MUL: Opc = DestType == MVT::f64 ? PPC::FMUL : PPC::FMULS; break;
821 case ISD::ADD: Opc = DestType == MVT::f64 ? PPC::FADD : PPC::FADDS; break;
822 case ISD::SUB: Opc = DestType == MVT::f64 ? PPC::FSUB : PPC::FSUBS; break;
823 case ISD::SDIV: Opc = DestType == MVT::f64 ? PPC::FDIV : PPC::FDIVS; break;
824 };
Nate Begemana9795f82005-03-24 04:41:43 +0000825 Tmp1 = SelectExpr(N.getOperand(0));
826 Tmp2 = SelectExpr(N.getOperand(1));
827 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
828 return Result;
829
Nate Begemana9795f82005-03-24 04:41:43 +0000830 case ISD::UINT_TO_FP:
Nate Begemanfdcf3412005-03-30 19:38:35 +0000831 case ISD::SINT_TO_FP: {
832 assert (N.getOperand(0).getValueType() == MVT::i32
833 && "int to float must operate on i32");
834 bool IsUnsigned = (ISD::UINT_TO_FP == opcode);
835 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
836 Tmp2 = MakeReg(MVT::f64); // temp reg to load the integer value into
837 Tmp3 = MakeReg(MVT::i32); // temp reg to hold the conversion constant
838 unsigned ConstF = MakeReg(MVT::f64); // temp reg to hold the fp constant
839
840 int FrameIdx = BB->getParent()->getFrameInfo()->CreateStackObject(8, 8);
841 MachineConstantPool *CP = BB->getParent()->getConstantPool();
842
843 // FIXME: pull this FP constant generation stuff out into something like
844 // the simple ISel's getReg.
845 if (IsUnsigned) {
846 ConstantFP *CFP = ConstantFP::get(Type::DoubleTy, 0x1.000000p52);
847 unsigned CPI = CP->getConstantPoolIndex(CFP);
848 // Load constant fp value
849 unsigned Tmp4 = MakeReg(MVT::i32);
850 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp4).addReg(getGlobalBaseReg())
851 .addConstantPoolIndex(CPI);
852 BuildMI(BB, PPC::LFD, 2, ConstF).addConstantPoolIndex(CPI).addReg(Tmp4);
853 // Store the hi & low halves of the fp value, currently in int regs
854 BuildMI(BB, PPC::LIS, 1, Tmp3).addSImm(0x4330);
855 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(Tmp3), FrameIdx);
856 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(Tmp1), FrameIdx, 4);
857 addFrameReference(BuildMI(BB, PPC::LFD, 2, Tmp2), FrameIdx);
858 // Generate the return value with a subtract
859 BuildMI(BB, PPC::FSUB, 2, Result).addReg(Tmp2).addReg(ConstF);
860 } else {
861 ConstantFP *CFP = ConstantFP::get(Type::DoubleTy, 0x1.000008p52);
862 unsigned CPI = CP->getConstantPoolIndex(CFP);
863 // Load constant fp value
864 unsigned Tmp4 = MakeReg(MVT::i32);
865 unsigned TmpL = MakeReg(MVT::i32);
866 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp4).addReg(getGlobalBaseReg())
867 .addConstantPoolIndex(CPI);
868 BuildMI(BB, PPC::LFD, 2, ConstF).addConstantPoolIndex(CPI).addReg(Tmp4);
869 // Store the hi & low halves of the fp value, currently in int regs
870 BuildMI(BB, PPC::LIS, 1, Tmp3).addSImm(0x4330);
871 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(Tmp3), FrameIdx);
872 BuildMI(BB, PPC::XORIS, 2, TmpL).addReg(Tmp1).addImm(0x8000);
873 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(TmpL), FrameIdx, 4);
874 addFrameReference(BuildMI(BB, PPC::LFD, 2, Tmp2), FrameIdx);
875 // Generate the return value with a subtract
876 BuildMI(BB, PPC::FSUB, 2, Result).addReg(Tmp2).addReg(ConstF);
877 }
878 return Result;
879 }
Nate Begemana9795f82005-03-24 04:41:43 +0000880 }
Nate Begeman6b559972005-04-01 02:59:27 +0000881 assert(0 && "Should never get here");
Nate Begemana9795f82005-03-24 04:41:43 +0000882 return 0;
883}
884
885unsigned ISel::SelectExpr(SDOperand N) {
886 unsigned Result;
887 unsigned Tmp1, Tmp2, Tmp3;
888 unsigned Opc = 0;
889 unsigned opcode = N.getOpcode();
890
891 SDNode *Node = N.Val;
892 MVT::ValueType DestType = N.getValueType();
893
894 unsigned &Reg = ExprMap[N];
895 if (Reg) return Reg;
896
Nate Begeman27eeb002005-04-02 05:59:34 +0000897 switch (N.getOpcode()) {
898 default:
Nate Begemana9795f82005-03-24 04:41:43 +0000899 Reg = Result = (N.getValueType() != MVT::Other) ?
Nate Begeman27eeb002005-04-02 05:59:34 +0000900 MakeReg(N.getValueType()) : 1;
901 break;
902 case ISD::CALL:
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000903 // If this is a call instruction, make sure to prepare ALL of the result
904 // values as well as the chain.
Nate Begeman27eeb002005-04-02 05:59:34 +0000905 if (Node->getNumValues() == 1)
906 Reg = Result = 1; // Void call, just a chain.
907 else {
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000908 Result = MakeReg(Node->getValueType(0));
909 ExprMap[N.getValue(0)] = Result;
Nate Begeman27eeb002005-04-02 05:59:34 +0000910 for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000911 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
Nate Begeman27eeb002005-04-02 05:59:34 +0000912 ExprMap[SDOperand(Node, Node->getNumValues()-1)] = 1;
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000913 }
Nate Begeman27eeb002005-04-02 05:59:34 +0000914 break;
915 case ISD::ADD_PARTS:
916 case ISD::SUB_PARTS:
917 case ISD::SHL_PARTS:
918 case ISD::SRL_PARTS:
919 case ISD::SRA_PARTS:
920 Result = MakeReg(Node->getValueType(0));
921 ExprMap[N.getValue(0)] = Result;
922 for (unsigned i = 1, e = N.Val->getNumValues(); i != e; ++i)
923 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
924 break;
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000925 }
926
927 if (DestType == MVT::f64 || DestType == MVT::f32)
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000928 if (ISD::LOAD != opcode && ISD::EXTLOAD != opcode && ISD::UNDEF != opcode)
Nate Begeman74d73452005-03-31 00:15:26 +0000929 return SelectExprFP(N, Result);
Nate Begemana9795f82005-03-24 04:41:43 +0000930
931 switch (opcode) {
932 default:
933 Node->dump();
934 assert(0 && "Node not handled!\n");
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000935 case ISD::UNDEF:
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000936 BuildMI(BB, PPC::IMPLICIT_DEF, 0, Result);
937 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +0000938 case ISD::DYNAMIC_STACKALLOC:
Nate Begeman5e966612005-03-24 06:28:42 +0000939 // Generate both result values. FIXME: Need a better commment here?
940 if (Result != 1)
941 ExprMap[N.getValue(1)] = 1;
942 else
943 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
944
945 // FIXME: We are currently ignoring the requested alignment for handling
946 // greater than the stack alignment. This will need to be revisited at some
947 // point. Align = N.getOperand(2);
948 if (!isa<ConstantSDNode>(N.getOperand(2)) ||
949 cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) {
950 std::cerr << "Cannot allocate stack object with greater alignment than"
951 << " the stack alignment yet!";
952 abort();
953 }
954 Select(N.getOperand(0));
955 Tmp1 = SelectExpr(N.getOperand(1));
956 // Subtract size from stack pointer, thereby allocating some space.
957 BuildMI(BB, PPC::SUBF, 2, PPC::R1).addReg(Tmp1).addReg(PPC::R1);
958 // Put a pointer to the space into the result register by copying the SP
959 BuildMI(BB, PPC::OR, 2, Result).addReg(PPC::R1).addReg(PPC::R1);
960 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +0000961
962 case ISD::ConstantPool:
Nate Begemanca12a2b2005-03-28 22:28:37 +0000963 Tmp1 = cast<ConstantPoolSDNode>(N)->getIndex();
964 Tmp2 = MakeReg(MVT::i32);
965 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp2).addReg(getGlobalBaseReg())
966 .addConstantPoolIndex(Tmp1);
967 BuildMI(BB, PPC::LA, 2, Result).addReg(Tmp2).addConstantPoolIndex(Tmp1);
968 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +0000969
970 case ISD::FrameIndex:
Nate Begemanf3d08f32005-03-29 00:03:27 +0000971 Tmp1 = cast<FrameIndexSDNode>(N)->getIndex();
Nate Begeman58f718c2005-03-30 02:23:08 +0000972 addFrameReference(BuildMI(BB, PPC::ADDI, 2, Result), (int)Tmp1, 0, false);
Nate Begemanf3d08f32005-03-29 00:03:27 +0000973 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +0000974
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000975 case ISD::GlobalAddress: {
976 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
Nate Begemanca12a2b2005-03-28 22:28:37 +0000977 Tmp1 = MakeReg(MVT::i32);
Nate Begemanc7b09f12005-03-25 08:34:25 +0000978 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp1).addReg(getGlobalBaseReg())
979 .addGlobalAddress(GV);
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000980 if (GV->hasWeakLinkage() || GV->isExternal()) {
981 BuildMI(BB, PPC::LWZ, 2, Result).addGlobalAddress(GV).addReg(Tmp1);
982 } else {
983 BuildMI(BB, PPC::LA, 2, Result).addReg(Tmp1).addGlobalAddress(GV);
984 }
985 return Result;
986 }
987
Nate Begeman5e966612005-03-24 06:28:42 +0000988 case ISD::LOAD:
Nate Begemana9795f82005-03-24 04:41:43 +0000989 case ISD::EXTLOAD:
990 case ISD::ZEXTLOAD:
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000991 case ISD::SEXTLOAD: {
Nate Begeman9db505c2005-03-28 19:36:43 +0000992 MVT::ValueType TypeBeingLoaded = (ISD::LOAD == opcode) ?
993 Node->getValueType(0) : cast<MVTSDNode>(Node)->getExtraValueType();
Nate Begeman74d73452005-03-31 00:15:26 +0000994 bool sext = (ISD::SEXTLOAD == opcode);
995 bool byte = (MVT::i8 == TypeBeingLoaded);
996
Nate Begeman5e966612005-03-24 06:28:42 +0000997 // Make sure we generate both values.
998 if (Result != 1)
999 ExprMap[N.getValue(1)] = 1; // Generate the token
1000 else
1001 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1002
1003 SDOperand Chain = N.getOperand(0);
1004 SDOperand Address = N.getOperand(1);
1005 Select(Chain);
1006
Nate Begeman9db505c2005-03-28 19:36:43 +00001007 switch (TypeBeingLoaded) {
Nate Begeman74d73452005-03-31 00:15:26 +00001008 default: Node->dump(); assert(0 && "Cannot load this type!");
Nate Begeman9db505c2005-03-28 19:36:43 +00001009 case MVT::i1: Opc = PPC::LBZ; break;
1010 case MVT::i8: Opc = PPC::LBZ; break;
1011 case MVT::i16: Opc = sext ? PPC::LHA : PPC::LHZ; break;
1012 case MVT::i32: Opc = PPC::LWZ; break;
Nate Begeman74d73452005-03-31 00:15:26 +00001013 case MVT::f32: Opc = PPC::LFS; break;
1014 case MVT::f64: Opc = PPC::LFD; break;
Nate Begeman5e966612005-03-24 06:28:42 +00001015 }
1016
Nate Begeman74d73452005-03-31 00:15:26 +00001017 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Address)) {
1018 Tmp1 = MakeReg(MVT::i32);
1019 int CPI = CP->getIndex();
1020 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp1).addReg(getGlobalBaseReg())
1021 .addConstantPoolIndex(CPI);
1022 BuildMI(BB, Opc, 2, Result).addConstantPoolIndex(CPI).addReg(Tmp1);
Nate Begeman9db505c2005-03-28 19:36:43 +00001023 }
Nate Begeman74d73452005-03-31 00:15:26 +00001024 else if(Address.getOpcode() == ISD::FrameIndex) {
Nate Begeman58f718c2005-03-30 02:23:08 +00001025 Tmp1 = cast<FrameIndexSDNode>(Address)->getIndex();
1026 addFrameReference(BuildMI(BB, Opc, 2, Result), (int)Tmp1);
Nate Begeman5e966612005-03-24 06:28:42 +00001027 } else {
1028 int offset;
Nate Begeman04730362005-04-01 04:45:11 +00001029 bool idx = SelectAddr(Address, Tmp1, offset);
1030 if (idx) {
1031 Opc = IndexedOpForOp(Opc);
1032 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(offset);
1033 } else {
1034 BuildMI(BB, Opc, 2, Result).addSImm(offset).addReg(Tmp1);
1035 }
Nate Begeman5e966612005-03-24 06:28:42 +00001036 }
1037 return Result;
1038 }
1039
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001040 case ISD::CALL: {
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001041 unsigned GPR_idx = 0, FPR_idx = 0;
1042 static const unsigned GPR[] = {
1043 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
1044 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
1045 };
1046 static const unsigned FPR[] = {
1047 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
1048 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
1049 };
1050
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001051 // Lower the chain for this call.
1052 Select(N.getOperand(0));
1053 ExprMap[N.getValue(Node->getNumValues()-1)] = 1;
Nate Begeman74d73452005-03-31 00:15:26 +00001054
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001055 // Load the register args to virtual regs
1056 std::vector<unsigned> ArgVR;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001057 for(int i = 2, e = Node->getNumOperands(); i < e; ++i)
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001058 ArgVR.push_back(SelectExpr(N.getOperand(i)));
1059
1060 // Copy the virtual registers into the appropriate argument register
1061 for(int i = 0, e = ArgVR.size(); i < e; ++i) {
1062 switch(N.getOperand(i+2).getValueType()) {
1063 default: Node->dump(); assert(0 && "Unknown value type for call");
1064 case MVT::i1:
1065 case MVT::i8:
1066 case MVT::i16:
1067 case MVT::i32:
1068 assert(GPR_idx < 8 && "Too many int args");
1069 if (N.getOperand(i+2).getOpcode() != ISD::UNDEF)
1070 BuildMI(BB, PPC::OR,2,GPR[GPR_idx]).addReg(ArgVR[i]).addReg(ArgVR[i]);
1071 ++GPR_idx;
1072 break;
1073 case MVT::f64:
1074 case MVT::f32:
1075 assert(FPR_idx < 13 && "Too many fp args");
1076 BuildMI(BB, PPC::FMR, 1, FPR[FPR_idx]).addReg(ArgVR[i]);
1077 ++FPR_idx;
1078 break;
1079 }
1080 }
1081
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001082 // Emit the correct call instruction based on the type of symbol called.
1083 if (GlobalAddressSDNode *GASD =
1084 dyn_cast<GlobalAddressSDNode>(N.getOperand(1))) {
1085 BuildMI(BB, PPC::CALLpcrel, 1).addGlobalAddress(GASD->getGlobal(), true);
1086 } else if (ExternalSymbolSDNode *ESSDN =
1087 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1))) {
1088 BuildMI(BB, PPC::CALLpcrel, 1).addExternalSymbol(ESSDN->getSymbol(), true);
1089 } else {
1090 Tmp1 = SelectExpr(N.getOperand(1));
1091 BuildMI(BB, PPC::OR, 2, PPC::R12).addReg(Tmp1).addReg(Tmp1);
1092 BuildMI(BB, PPC::MTCTR, 1).addReg(PPC::R12);
1093 BuildMI(BB, PPC::CALLindirect, 3).addImm(20).addImm(0).addReg(PPC::R12);
1094 }
1095
1096 switch (Node->getValueType(0)) {
1097 default: assert(0 && "Unknown value type for call result!");
1098 case MVT::Other: return 1;
1099 case MVT::i1:
1100 case MVT::i8:
1101 case MVT::i16:
1102 case MVT::i32:
Nate Begemanc7b09f12005-03-25 08:34:25 +00001103 BuildMI(BB, PPC::OR, 2, Result).addReg(PPC::R3).addReg(PPC::R3);
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001104 if (Node->getValueType(1) == MVT::i32)
Nate Begemanc7b09f12005-03-25 08:34:25 +00001105 BuildMI(BB, PPC::OR, 2, Result+1).addReg(PPC::R4).addReg(PPC::R4);
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001106 break;
1107 case MVT::f32:
1108 case MVT::f64:
1109 BuildMI(BB, PPC::FMR, 1, Result).addReg(PPC::F1);
1110 break;
1111 }
1112 return Result+N.ResNo;
1113 }
Nate Begemana9795f82005-03-24 04:41:43 +00001114
1115 case ISD::SIGN_EXTEND:
1116 case ISD::SIGN_EXTEND_INREG:
1117 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman9db505c2005-03-28 19:36:43 +00001118 switch(cast<MVTSDNode>(Node)->getExtraValueType()) {
1119 default: Node->dump(); assert(0 && "Unhandled SIGN_EXTEND type"); break;
1120 case MVT::i16:
1121 BuildMI(BB, PPC::EXTSH, 1, Result).addReg(Tmp1);
1122 break;
1123 case MVT::i8:
1124 BuildMI(BB, PPC::EXTSB, 1, Result).addReg(Tmp1);
1125 break;
Nate Begeman74747862005-03-29 22:24:51 +00001126 case MVT::i1:
1127 BuildMI(BB, PPC::SUBFIC, 2, Result).addReg(Tmp1).addSImm(0);
1128 break;
Nate Begeman9db505c2005-03-28 19:36:43 +00001129 }
Nate Begemana9795f82005-03-24 04:41:43 +00001130 return Result;
1131
1132 case ISD::ZERO_EXTEND_INREG:
1133 Tmp1 = SelectExpr(N.getOperand(0));
1134 switch(cast<MVTSDNode>(Node)->getExtraValueType()) {
Nate Begeman9db505c2005-03-28 19:36:43 +00001135 default: Node->dump(); assert(0 && "Unhandled ZERO_EXTEND type"); break;
Nate Begemana9795f82005-03-24 04:41:43 +00001136 case MVT::i16: Tmp2 = 16; break;
1137 case MVT::i8: Tmp2 = 24; break;
1138 case MVT::i1: Tmp2 = 31; break;
1139 }
Nate Begeman33162522005-03-29 21:54:38 +00001140 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(0).addImm(Tmp2)
1141 .addImm(31);
Nate Begemana9795f82005-03-24 04:41:43 +00001142 return Result;
1143
Nate Begemana9795f82005-03-24 04:41:43 +00001144 case ISD::CopyFromReg:
1145 if (Result == 1)
1146 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1147 Tmp1 = dyn_cast<RegSDNode>(Node)->getReg();
1148 BuildMI(BB, PPC::OR, 2, Result).addReg(Tmp1).addReg(Tmp1);
1149 return Result;
1150
1151 case ISD::SHL:
Nate Begeman5e966612005-03-24 06:28:42 +00001152 Tmp1 = SelectExpr(N.getOperand(0));
1153 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1154 Tmp2 = CN->getValue() & 0x1F;
Nate Begeman33162522005-03-29 21:54:38 +00001155 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(Tmp2).addImm(0)
Nate Begeman5e966612005-03-24 06:28:42 +00001156 .addImm(31-Tmp2);
1157 } else {
1158 Tmp2 = SelectExpr(N.getOperand(1));
1159 BuildMI(BB, PPC::SLW, 2, Result).addReg(Tmp1).addReg(Tmp2);
1160 }
1161 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001162
Nate Begeman5e966612005-03-24 06:28:42 +00001163 case ISD::SRL:
1164 Tmp1 = SelectExpr(N.getOperand(0));
1165 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1166 Tmp2 = CN->getValue() & 0x1F;
Nate Begeman33162522005-03-29 21:54:38 +00001167 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(32-Tmp2)
Nate Begeman5e966612005-03-24 06:28:42 +00001168 .addImm(Tmp2).addImm(31);
1169 } else {
1170 Tmp2 = SelectExpr(N.getOperand(1));
1171 BuildMI(BB, PPC::SRW, 2, Result).addReg(Tmp1).addReg(Tmp2);
1172 }
1173 return Result;
1174
1175 case ISD::SRA:
1176 Tmp1 = SelectExpr(N.getOperand(0));
1177 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1178 Tmp2 = CN->getValue() & 0x1F;
1179 BuildMI(BB, PPC::SRAWI, 2, Result).addReg(Tmp1).addImm(Tmp2);
1180 } else {
1181 Tmp2 = SelectExpr(N.getOperand(1));
1182 BuildMI(BB, PPC::SRAW, 2, Result).addReg(Tmp1).addReg(Tmp2);
1183 }
1184 return Result;
1185
Nate Begemana9795f82005-03-24 04:41:43 +00001186 case ISD::ADD:
1187 assert (DestType == MVT::i32 && "Only do arithmetic on i32s!");
1188 Tmp1 = SelectExpr(N.getOperand(0));
1189 switch(canUseAsImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
1190 default: assert(0 && "unhandled result code");
1191 case 0: // No immediate
1192 Tmp2 = SelectExpr(N.getOperand(1));
1193 BuildMI(BB, PPC::ADD, 2, Result).addReg(Tmp1).addReg(Tmp2);
1194 break;
1195 case 1: // Low immediate
1196 BuildMI(BB, PPC::ADDI, 2, Result).addReg(Tmp1).addSImm(Tmp2);
1197 break;
1198 case 2: // Shifted immediate
1199 BuildMI(BB, PPC::ADDIS, 2, Result).addReg(Tmp1).addSImm(Tmp2);
1200 break;
1201 }
1202 return Result;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001203
Nate Begemana9795f82005-03-24 04:41:43 +00001204 case ISD::AND:
1205 case ISD::OR:
Nate Begemana9795f82005-03-24 04:41:43 +00001206 Tmp1 = SelectExpr(N.getOperand(0));
1207 switch(canUseAsImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
1208 default: assert(0 && "unhandled result code");
1209 case 0: // No immediate
1210 Tmp2 = SelectExpr(N.getOperand(1));
1211 switch (opcode) {
Nate Begeman5e966612005-03-24 06:28:42 +00001212 case ISD::AND: Opc = PPC::AND; break;
1213 case ISD::OR: Opc = PPC::OR; break;
Nate Begemana9795f82005-03-24 04:41:43 +00001214 }
Nate Begeman5e966612005-03-24 06:28:42 +00001215 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +00001216 break;
1217 case 1: // Low immediate
1218 switch (opcode) {
Nate Begeman5e966612005-03-24 06:28:42 +00001219 case ISD::AND: Opc = PPC::ANDIo; break;
1220 case ISD::OR: Opc = PPC::ORI; break;
Nate Begemana9795f82005-03-24 04:41:43 +00001221 }
Nate Begeman5e966612005-03-24 06:28:42 +00001222 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +00001223 break;
1224 case 2: // Shifted immediate
1225 switch (opcode) {
Nate Begeman5e966612005-03-24 06:28:42 +00001226 case ISD::AND: Opc = PPC::ANDISo; break;
1227 case ISD::OR: Opc = PPC::ORIS; break;
Nate Begemana9795f82005-03-24 04:41:43 +00001228 }
Nate Begeman5e966612005-03-24 06:28:42 +00001229 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +00001230 break;
1231 }
1232 return Result;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001233
Nate Begemanaa73a9f2005-04-03 11:20:20 +00001234 case ISD::XOR: {
1235 // Check for EQV: xor, (xor a, -1), b
1236 if (N.getOperand(0).getOpcode() == ISD::XOR &&
1237 N.getOperand(0).getOperand(1).getOpcode() == ISD::Constant &&
1238 cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->isAllOnesValue()) {
1239 ++NotLogic;
1240 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1241 Tmp2 = SelectExpr(N.getOperand(1));
1242 BuildMI(BB, PPC::EQV, 2, Result).addReg(Tmp1).addReg(Tmp2);
1243 return Result;
1244 }
1245 // Check for NOT, NOR, and NAND: xor (copy, or, and), -1
1246 if (N.getOperand(1).getOpcode() == ISD::Constant &&
1247 cast<ConstantSDNode>(N.getOperand(1))->isAllOnesValue()) {
1248 ++NotLogic;
1249 switch(N.getOperand(0).getOpcode()) {
1250 case ISD::OR:
1251 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1252 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1253 BuildMI(BB, PPC::NOR, 2, Result).addReg(Tmp1).addReg(Tmp2);
1254 break;
1255 case ISD::AND:
1256 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1257 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1258 BuildMI(BB, PPC::NAND, 2, Result).addReg(Tmp1).addReg(Tmp2);
1259 break;
1260 default:
1261 Tmp1 = SelectExpr(N.getOperand(0));
1262 BuildMI(BB, PPC::NOR, 2, Result).addReg(Tmp1).addReg(Tmp1);
1263 break;
1264 }
1265 return Result;
1266 }
1267 Tmp1 = SelectExpr(N.getOperand(0));
1268 switch(canUseAsImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
1269 default: assert(0 && "unhandled result code");
1270 case 0: // No immediate
1271 Tmp2 = SelectExpr(N.getOperand(1));
1272 BuildMI(BB, PPC::XOR, 2, Result).addReg(Tmp1).addReg(Tmp2);
1273 break;
1274 case 1: // Low immediate
1275 BuildMI(BB, PPC::XORI, 2, Result).addReg(Tmp1).addImm(Tmp2);
1276 break;
1277 case 2: // Shifted immediate
1278 BuildMI(BB, PPC::XORIS, 2, Result).addReg(Tmp1).addImm(Tmp2);
1279 break;
1280 }
1281 return Result;
1282 }
1283
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001284 case ISD::SUB:
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001285 Tmp2 = SelectExpr(N.getOperand(1));
Nate Begeman27523a12005-04-02 00:42:16 +00001286 if (1 == canUseAsImmediateForOpcode(N.getOperand(0), opcode, Tmp1))
1287 BuildMI(BB, PPC::SUBFIC, 2, Result).addReg(Tmp2).addSImm(Tmp1);
1288 else {
1289 Tmp1 = SelectExpr(N.getOperand(0));
1290 BuildMI(BB, PPC::SUBF, 2, Result).addReg(Tmp2).addReg(Tmp1);
1291 }
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001292 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001293
Nate Begeman5e966612005-03-24 06:28:42 +00001294 case ISD::MUL:
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001295 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman307e7442005-03-26 01:28:53 +00001296 if (1 == canUseAsImmediateForOpcode(N.getOperand(1), opcode, Tmp2))
1297 BuildMI(BB, PPC::MULLI, 2, Result).addReg(Tmp1).addSImm(Tmp2);
1298 else {
1299 Tmp2 = SelectExpr(N.getOperand(1));
1300 BuildMI(BB, PPC::MULLW, 2, Result).addReg(Tmp1).addReg(Tmp2);
1301 }
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001302 return Result;
1303
Nate Begemanf3d08f32005-03-29 00:03:27 +00001304 case ISD::SDIV:
1305 case ISD::UDIV:
Nate Begemanf3d08f32005-03-29 00:03:27 +00001306 Tmp1 = SelectExpr(N.getOperand(0));
1307 Tmp2 = SelectExpr(N.getOperand(1));
1308 Opc = (ISD::UDIV == opcode) ? PPC::DIVWU : PPC::DIVW;
1309 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1310 return Result;
1311
1312 case ISD::UREM:
1313 case ISD::SREM: {
Nate Begemanf3d08f32005-03-29 00:03:27 +00001314 Tmp1 = SelectExpr(N.getOperand(0));
1315 Tmp2 = SelectExpr(N.getOperand(1));
1316 Tmp3 = MakeReg(MVT::i32);
1317 unsigned Tmp4 = MakeReg(MVT::i32);
1318 Opc = (ISD::UREM == opcode) ? PPC::DIVWU : PPC::DIVW;
1319 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
1320 BuildMI(BB, PPC::MULLW, 2, Tmp4).addReg(Tmp3).addReg(Tmp2);
1321 BuildMI(BB, PPC::SUBF, 2, Result).addReg(Tmp4).addReg(Tmp1);
1322 return Result;
1323 }
1324
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001325 case ISD::ADD_PARTS:
Nate Begemanca12a2b2005-03-28 22:28:37 +00001326 case ISD::SUB_PARTS: {
1327 assert(N.getNumOperands() == 4 && N.getValueType() == MVT::i32 &&
1328 "Not an i64 add/sub!");
1329 // Emit all of the operands.
1330 std::vector<unsigned> InVals;
1331 for (unsigned i = 0, e = N.getNumOperands(); i != e; ++i)
1332 InVals.push_back(SelectExpr(N.getOperand(i)));
1333 if (N.getOpcode() == ISD::ADD_PARTS) {
Nate Begeman27eeb002005-04-02 05:59:34 +00001334 BuildMI(BB, PPC::ADDC, 2, Result).addReg(InVals[0]).addReg(InVals[2]);
1335 BuildMI(BB, PPC::ADDE, 2, Result+1).addReg(InVals[1]).addReg(InVals[3]);
Nate Begemanca12a2b2005-03-28 22:28:37 +00001336 } else {
Nate Begeman27eeb002005-04-02 05:59:34 +00001337 BuildMI(BB, PPC::SUBFC, 2, Result).addReg(InVals[2]).addReg(InVals[0]);
1338 BuildMI(BB, PPC::SUBFE, 2, Result+1).addReg(InVals[3]).addReg(InVals[1]);
1339 }
1340 return Result+N.ResNo;
1341 }
1342
1343 case ISD::SHL_PARTS:
1344 case ISD::SRA_PARTS:
1345 case ISD::SRL_PARTS: {
1346 assert(N.getNumOperands() == 3 && N.getValueType() == MVT::i32 &&
1347 "Not an i64 shift!");
1348 unsigned ShiftOpLo = SelectExpr(N.getOperand(0));
1349 unsigned ShiftOpHi = SelectExpr(N.getOperand(1));
1350 unsigned SHReg = SelectExpr(N.getOperand(2));
1351 Tmp1 = MakeReg(MVT::i32);
1352 Tmp2 = MakeReg(MVT::i32);
1353 Tmp3 = MakeReg(MVT::i32);
1354 unsigned Tmp4 = MakeReg(MVT::i32);
1355 unsigned Tmp5 = MakeReg(MVT::i32);
1356 unsigned Tmp6 = MakeReg(MVT::i32);
1357 BuildMI(BB, PPC::SUBFIC, 2, Tmp1).addReg(SHReg).addSImm(32);
1358 if (ISD::SHL_PARTS == opcode) {
1359 BuildMI(BB, PPC::SLW, 2, Tmp2).addReg(ShiftOpHi).addReg(SHReg);
1360 BuildMI(BB, PPC::SRW, 2, Tmp3).addReg(ShiftOpLo).addReg(Tmp1);
1361 BuildMI(BB, PPC::OR, 2, Tmp4).addReg(Tmp2).addReg(Tmp3);
1362 BuildMI(BB, PPC::ADDI, 2, Tmp5).addReg(SHReg).addSImm(-32);
Nate Begemanfa554702005-04-03 22:13:27 +00001363 BuildMI(BB, PPC::SLW, 2, Tmp6).addReg(ShiftOpLo).addReg(Tmp5);
Nate Begeman27eeb002005-04-02 05:59:34 +00001364 BuildMI(BB, PPC::OR, 2, Result+1).addReg(Tmp4).addReg(Tmp6);
1365 BuildMI(BB, PPC::SLW, 2, Result).addReg(ShiftOpLo).addReg(SHReg);
1366 } else if (ISD::SRL_PARTS == opcode) {
1367 BuildMI(BB, PPC::SRW, 2, Tmp2).addReg(ShiftOpLo).addReg(SHReg);
1368 BuildMI(BB, PPC::SLW, 2, Tmp3).addReg(ShiftOpHi).addReg(Tmp1);
1369 BuildMI(BB, PPC::OR, 2, Tmp4).addReg(Tmp2).addReg(Tmp3);
1370 BuildMI(BB, PPC::ADDI, 2, Tmp5).addReg(SHReg).addSImm(-32);
1371 BuildMI(BB, PPC::SRW, 2, Tmp6).addReg(ShiftOpHi).addReg(Tmp5);
1372 BuildMI(BB, PPC::OR, 2, Result).addReg(Tmp4).addReg(Tmp6);
1373 BuildMI(BB, PPC::SRW, 2, Result+1).addReg(ShiftOpHi).addReg(SHReg);
1374 } else {
1375 MachineBasicBlock *TmpMBB = new MachineBasicBlock(BB->getBasicBlock());
1376 MachineBasicBlock *PhiMBB = new MachineBasicBlock(BB->getBasicBlock());
1377 MachineBasicBlock *OldMBB = BB;
1378 MachineFunction *F = BB->getParent();
1379 ilist<MachineBasicBlock>::iterator It = BB; ++It;
1380 F->getBasicBlockList().insert(It, TmpMBB);
1381 F->getBasicBlockList().insert(It, PhiMBB);
1382 BB->addSuccessor(TmpMBB);
1383 BB->addSuccessor(PhiMBB);
1384 BuildMI(BB, PPC::SRW, 2, Tmp2).addReg(ShiftOpLo).addReg(SHReg);
1385 BuildMI(BB, PPC::SLW, 2, Tmp3).addReg(ShiftOpHi).addReg(Tmp1);
1386 BuildMI(BB, PPC::OR, 2, Tmp4).addReg(Tmp2).addReg(Tmp3);
1387 BuildMI(BB, PPC::ADDICo, 2, Tmp5).addReg(SHReg).addSImm(-32);
1388 BuildMI(BB, PPC::SRAW, 2, Tmp6).addReg(ShiftOpHi).addReg(Tmp5);
1389 BuildMI(BB, PPC::SRAW, 2, Result+1).addReg(ShiftOpHi).addReg(SHReg);
1390 BuildMI(BB, PPC::BLE, 2).addReg(PPC::CR0).addMBB(PhiMBB);
1391 // Select correct least significant half if the shift amount > 32
1392 BB = TmpMBB;
1393 unsigned Tmp7 = MakeReg(MVT::i32);
1394 BuildMI(BB, PPC::OR, 2, Tmp7).addReg(Tmp6).addReg(Tmp6);
1395 TmpMBB->addSuccessor(PhiMBB);
1396 BB = PhiMBB;
1397 BuildMI(BB, PPC::PHI, 4, Result).addReg(Tmp4).addMBB(OldMBB)
1398 .addReg(Tmp7).addMBB(TmpMBB);
Nate Begemanca12a2b2005-03-28 22:28:37 +00001399 }
1400 return Result+N.ResNo;
1401 }
1402
Nate Begemana9795f82005-03-24 04:41:43 +00001403 case ISD::FP_TO_UINT:
Nate Begeman6b559972005-04-01 02:59:27 +00001404 case ISD::FP_TO_SINT: {
1405 bool U = (ISD::FP_TO_UINT == opcode);
1406 Tmp1 = SelectExpr(N.getOperand(0));
1407 if (!U) {
1408 Tmp2 = MakeReg(MVT::f64);
1409 BuildMI(BB, PPC::FCTIWZ, 1, Tmp2).addReg(Tmp1);
1410 int FrameIdx = BB->getParent()->getFrameInfo()->CreateStackObject(8, 8);
1411 addFrameReference(BuildMI(BB, PPC::STFD, 3).addReg(Tmp2), FrameIdx);
1412 addFrameReference(BuildMI(BB, PPC::LWZ, 2, Result), FrameIdx, 4);
1413 return Result;
1414 } else {
1415 unsigned Zero = getConstDouble(0.0);
1416 unsigned MaxInt = getConstDouble((1LL << 32) - 1);
1417 unsigned Border = getConstDouble(1LL << 31);
1418 unsigned UseZero = MakeReg(MVT::f64);
1419 unsigned UseMaxInt = MakeReg(MVT::f64);
1420 unsigned UseChoice = MakeReg(MVT::f64);
1421 unsigned TmpReg = MakeReg(MVT::f64);
1422 unsigned TmpReg2 = MakeReg(MVT::f64);
1423 unsigned ConvReg = MakeReg(MVT::f64);
1424 unsigned IntTmp = MakeReg(MVT::i32);
1425 unsigned XorReg = MakeReg(MVT::i32);
1426 MachineFunction *F = BB->getParent();
1427 int FrameIdx = F->getFrameInfo()->CreateStackObject(8, 8);
1428 // Update machine-CFG edges
1429 MachineBasicBlock *XorMBB = new MachineBasicBlock(BB->getBasicBlock());
1430 MachineBasicBlock *PhiMBB = new MachineBasicBlock(BB->getBasicBlock());
1431 MachineBasicBlock *OldMBB = BB;
1432 ilist<MachineBasicBlock>::iterator It = BB; ++It;
1433 F->getBasicBlockList().insert(It, XorMBB);
1434 F->getBasicBlockList().insert(It, PhiMBB);
1435 BB->addSuccessor(XorMBB);
1436 BB->addSuccessor(PhiMBB);
1437 // Convert from floating point to unsigned 32-bit value
1438 // Use 0 if incoming value is < 0.0
1439 BuildMI(BB, PPC::FSEL, 3, UseZero).addReg(Tmp1).addReg(Tmp1).addReg(Zero);
1440 // Use 2**32 - 1 if incoming value is >= 2**32
1441 BuildMI(BB, PPC::FSUB, 2, UseMaxInt).addReg(MaxInt).addReg(Tmp1);
1442 BuildMI(BB, PPC::FSEL, 3, UseChoice).addReg(UseMaxInt).addReg(UseZero)
1443 .addReg(MaxInt);
1444 // Subtract 2**31
1445 BuildMI(BB, PPC::FSUB, 2, TmpReg).addReg(UseChoice).addReg(Border);
1446 // Use difference if >= 2**31
1447 BuildMI(BB, PPC::FCMPU, 2, PPC::CR0).addReg(UseChoice).addReg(Border);
1448 BuildMI(BB, PPC::FSEL, 3, TmpReg2).addReg(TmpReg).addReg(TmpReg)
1449 .addReg(UseChoice);
1450 // Convert to integer
1451 BuildMI(BB, PPC::FCTIWZ, 1, ConvReg).addReg(TmpReg2);
1452 addFrameReference(BuildMI(BB, PPC::STFD, 3).addReg(ConvReg), FrameIdx);
1453 addFrameReference(BuildMI(BB, PPC::LWZ, 2, IntTmp), FrameIdx, 4);
1454 BuildMI(BB, PPC::BLT, 2).addReg(PPC::CR0).addMBB(PhiMBB);
1455 BuildMI(BB, PPC::B, 1).addMBB(XorMBB);
1456
1457 // XorMBB:
1458 // add 2**31 if input was >= 2**31
1459 BB = XorMBB;
1460 BuildMI(BB, PPC::XORIS, 2, XorReg).addReg(IntTmp).addImm(0x8000);
1461 XorMBB->addSuccessor(PhiMBB);
1462
1463 // PhiMBB:
1464 // DestReg = phi [ IntTmp, OldMBB ], [ XorReg, XorMBB ]
1465 BB = PhiMBB;
1466 BuildMI(BB, PPC::PHI, 4, Result).addReg(IntTmp).addMBB(OldMBB)
1467 .addReg(XorReg).addMBB(XorMBB);
1468 return Result;
1469 }
1470 assert(0 && "Should never get here");
1471 return 0;
1472 }
Nate Begemana9795f82005-03-24 04:41:43 +00001473
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001474 case ISD::SETCC:
Nate Begeman33162522005-03-29 21:54:38 +00001475 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Node)) {
Nate Begemandffcfcc2005-04-01 00:32:34 +00001476 Opc = SelectSetCR0(N);
Nate Begeman33162522005-03-29 21:54:38 +00001477
Nate Begeman31318e42005-04-01 07:21:30 +00001478 unsigned TrueValue = MakeReg(MVT::i32);
1479 BuildMI(BB, PPC::LI, 1, TrueValue).addSImm(1);
1480 unsigned FalseValue = MakeReg(MVT::i32);
1481 BuildMI(BB, PPC::LI, 1, FalseValue).addSImm(0);
1482
Nate Begeman33162522005-03-29 21:54:38 +00001483 // Create an iterator with which to insert the MBB for copying the false
1484 // value and the MBB to hold the PHI instruction for this SetCC.
1485 MachineBasicBlock *thisMBB = BB;
1486 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1487 ilist<MachineBasicBlock>::iterator It = BB;
1488 ++It;
1489
1490 // thisMBB:
1491 // ...
1492 // cmpTY cr0, r1, r2
1493 // %TrueValue = li 1
1494 // bCC sinkMBB
Nate Begeman33162522005-03-29 21:54:38 +00001495 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
1496 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
1497 BuildMI(BB, Opc, 2).addReg(PPC::CR0).addMBB(sinkMBB);
1498 MachineFunction *F = BB->getParent();
1499 F->getBasicBlockList().insert(It, copy0MBB);
1500 F->getBasicBlockList().insert(It, sinkMBB);
1501 // Update machine-CFG edges
1502 BB->addSuccessor(copy0MBB);
1503 BB->addSuccessor(sinkMBB);
1504
1505 // copy0MBB:
1506 // %FalseValue = li 0
1507 // fallthrough
1508 BB = copy0MBB;
Nate Begeman33162522005-03-29 21:54:38 +00001509 // Update machine-CFG edges
1510 BB->addSuccessor(sinkMBB);
1511
1512 // sinkMBB:
1513 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1514 // ...
1515 BB = sinkMBB;
1516 BuildMI(BB, PPC::PHI, 4, Result).addReg(FalseValue)
1517 .addMBB(copy0MBB).addReg(TrueValue).addMBB(thisMBB);
1518 return Result;
1519 }
1520 assert(0 && "Is this legal?");
1521 return 0;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001522
Nate Begeman74747862005-03-29 22:24:51 +00001523 case ISD::SELECT: {
Chris Lattner30710192005-04-01 07:10:02 +00001524 unsigned TrueValue = SelectExpr(N.getOperand(1)); //Use if TRUE
1525 unsigned FalseValue = SelectExpr(N.getOperand(2)); //Use if FALSE
Nate Begeman6cb2e1b2005-04-01 08:57:43 +00001526 Opc = SelectSetCR0(N.getOperand(0));
Chris Lattner30710192005-04-01 07:10:02 +00001527
Nate Begeman74747862005-03-29 22:24:51 +00001528 // Create an iterator with which to insert the MBB for copying the false
1529 // value and the MBB to hold the PHI instruction for this SetCC.
1530 MachineBasicBlock *thisMBB = BB;
1531 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1532 ilist<MachineBasicBlock>::iterator It = BB;
1533 ++It;
1534
1535 // thisMBB:
1536 // ...
1537 // TrueVal = ...
1538 // cmpTY cr0, r1, r2
1539 // bCC copy1MBB
1540 // fallthrough --> copy0MBB
Nate Begeman74747862005-03-29 22:24:51 +00001541 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
1542 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
Nate Begeman3e897162005-03-31 23:55:40 +00001543 BuildMI(BB, Opc, 2).addReg(PPC::CR0).addMBB(sinkMBB);
Nate Begeman74747862005-03-29 22:24:51 +00001544 MachineFunction *F = BB->getParent();
1545 F->getBasicBlockList().insert(It, copy0MBB);
1546 F->getBasicBlockList().insert(It, sinkMBB);
1547 // Update machine-CFG edges
1548 BB->addSuccessor(copy0MBB);
1549 BB->addSuccessor(sinkMBB);
1550
1551 // copy0MBB:
1552 // %FalseValue = ...
1553 // # fallthrough to sinkMBB
1554 BB = copy0MBB;
Nate Begeman74747862005-03-29 22:24:51 +00001555 // Update machine-CFG edges
1556 BB->addSuccessor(sinkMBB);
1557
1558 // sinkMBB:
1559 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1560 // ...
1561 BB = sinkMBB;
1562 BuildMI(BB, PPC::PHI, 4, Result).addReg(FalseValue)
1563 .addMBB(copy0MBB).addReg(TrueValue).addMBB(thisMBB);
1564
1565 // FIXME: Select i64?
1566 return Result;
1567 }
Nate Begemana9795f82005-03-24 04:41:43 +00001568
1569 case ISD::Constant:
1570 switch (N.getValueType()) {
1571 default: assert(0 && "Cannot use constants of this type!");
1572 case MVT::i1:
1573 BuildMI(BB, PPC::LI, 1, Result)
1574 .addSImm(!cast<ConstantSDNode>(N)->isNullValue());
1575 break;
1576 case MVT::i32:
1577 {
1578 int v = (int)cast<ConstantSDNode>(N)->getSignExtended();
1579 if (v < 32768 && v >= -32768) {
1580 BuildMI(BB, PPC::LI, 1, Result).addSImm(v);
1581 } else {
Nate Begeman5e966612005-03-24 06:28:42 +00001582 Tmp1 = MakeReg(MVT::i32);
1583 BuildMI(BB, PPC::LIS, 1, Tmp1).addSImm(v >> 16);
1584 BuildMI(BB, PPC::ORI, 2, Result).addReg(Tmp1).addImm(v & 0xFFFF);
Nate Begemana9795f82005-03-24 04:41:43 +00001585 }
1586 }
1587 }
1588 return Result;
1589 }
1590
1591 return 0;
1592}
1593
1594void ISel::Select(SDOperand N) {
1595 unsigned Tmp1, Tmp2, Opc;
1596 unsigned opcode = N.getOpcode();
1597
1598 if (!ExprMap.insert(std::make_pair(N, 1)).second)
1599 return; // Already selected.
1600
1601 SDNode *Node = N.Val;
1602
1603 switch (Node->getOpcode()) {
1604 default:
1605 Node->dump(); std::cerr << "\n";
1606 assert(0 && "Node not handled yet!");
1607 case ISD::EntryToken: return; // Noop
1608 case ISD::TokenFactor:
1609 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1610 Select(Node->getOperand(i));
1611 return;
1612 case ISD::ADJCALLSTACKDOWN:
1613 case ISD::ADJCALLSTACKUP:
1614 Select(N.getOperand(0));
1615 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1616 Opc = N.getOpcode() == ISD::ADJCALLSTACKDOWN ? PPC::ADJCALLSTACKDOWN :
1617 PPC::ADJCALLSTACKUP;
1618 BuildMI(BB, Opc, 1).addImm(Tmp1);
1619 return;
1620 case ISD::BR: {
1621 MachineBasicBlock *Dest =
1622 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
Nate Begemana9795f82005-03-24 04:41:43 +00001623 Select(N.getOperand(0));
1624 BuildMI(BB, PPC::B, 1).addMBB(Dest);
1625 return;
1626 }
1627 case ISD::BRCOND:
1628 SelectBranchCC(N);
1629 return;
1630 case ISD::CopyToReg:
1631 Select(N.getOperand(0));
1632 Tmp1 = SelectExpr(N.getOperand(1));
1633 Tmp2 = cast<RegSDNode>(N)->getReg();
1634
1635 if (Tmp1 != Tmp2) {
1636 if (N.getOperand(1).getValueType() == MVT::f64 ||
1637 N.getOperand(1).getValueType() == MVT::f32)
1638 BuildMI(BB, PPC::FMR, 1, Tmp2).addReg(Tmp1);
1639 else
1640 BuildMI(BB, PPC::OR, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
1641 }
1642 return;
1643 case ISD::ImplicitDef:
1644 Select(N.getOperand(0));
1645 BuildMI(BB, PPC::IMPLICIT_DEF, 0, cast<RegSDNode>(N)->getReg());
1646 return;
1647 case ISD::RET:
1648 switch (N.getNumOperands()) {
1649 default:
1650 assert(0 && "Unknown return instruction!");
1651 case 3:
1652 assert(N.getOperand(1).getValueType() == MVT::i32 &&
1653 N.getOperand(2).getValueType() == MVT::i32 &&
1654 "Unknown two-register value!");
1655 Select(N.getOperand(0));
1656 Tmp1 = SelectExpr(N.getOperand(1));
1657 Tmp2 = SelectExpr(N.getOperand(2));
Nate Begeman27523a12005-04-02 00:42:16 +00001658 BuildMI(BB, PPC::OR, 2, PPC::R3).addReg(Tmp2).addReg(Tmp2);
1659 BuildMI(BB, PPC::OR, 2, PPC::R4).addReg(Tmp1).addReg(Tmp1);
Nate Begemana9795f82005-03-24 04:41:43 +00001660 break;
1661 case 2:
1662 Select(N.getOperand(0));
1663 Tmp1 = SelectExpr(N.getOperand(1));
1664 switch (N.getOperand(1).getValueType()) {
1665 default:
1666 assert(0 && "Unknown return type!");
1667 case MVT::f64:
1668 case MVT::f32:
1669 BuildMI(BB, PPC::FMR, 1, PPC::F1).addReg(Tmp1);
1670 break;
1671 case MVT::i32:
1672 BuildMI(BB, PPC::OR, 2, PPC::R3).addReg(Tmp1).addReg(Tmp1);
1673 break;
1674 }
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001675 case 1:
1676 Select(N.getOperand(0));
1677 break;
Nate Begemana9795f82005-03-24 04:41:43 +00001678 }
1679 BuildMI(BB, PPC::BLR, 0); // Just emit a 'ret' instruction
1680 return;
Nate Begemana9795f82005-03-24 04:41:43 +00001681 case ISD::TRUNCSTORE:
1682 case ISD::STORE:
1683 {
1684 SDOperand Chain = N.getOperand(0);
1685 SDOperand Value = N.getOperand(1);
1686 SDOperand Address = N.getOperand(2);
1687 Select(Chain);
1688
1689 Tmp1 = SelectExpr(Value); //value
1690
1691 if (opcode == ISD::STORE) {
1692 switch(Value.getValueType()) {
1693 default: assert(0 && "unknown Type in store");
1694 case MVT::i32: Opc = PPC::STW; break;
1695 case MVT::f64: Opc = PPC::STFD; break;
1696 case MVT::f32: Opc = PPC::STFS; break;
1697 }
1698 } else { //ISD::TRUNCSTORE
1699 switch(cast<MVTSDNode>(Node)->getExtraValueType()) {
1700 default: assert(0 && "unknown Type in store");
1701 case MVT::i1: //FIXME: DAG does not promote this load
1702 case MVT::i8: Opc = PPC::STB; break;
1703 case MVT::i16: Opc = PPC::STH; break;
1704 }
1705 }
1706
Nate Begemana7e11a42005-04-01 05:57:17 +00001707 if(Address.getOpcode() == ISD::FrameIndex)
Nate Begemana9795f82005-03-24 04:41:43 +00001708 {
Nate Begeman58f718c2005-03-30 02:23:08 +00001709 Tmp2 = cast<FrameIndexSDNode>(Address)->getIndex();
1710 addFrameReference(BuildMI(BB, Opc, 3).addReg(Tmp1), (int)Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +00001711 }
1712 else
1713 {
1714 int offset;
Nate Begeman04730362005-04-01 04:45:11 +00001715 bool idx = SelectAddr(Address, Tmp2, offset);
1716 if (idx) {
1717 Opc = IndexedOpForOp(Opc);
1718 BuildMI(BB, Opc, 3).addReg(Tmp1).addReg(Tmp2).addReg(offset);
1719 } else {
1720 BuildMI(BB, Opc, 3).addReg(Tmp1).addImm(offset).addReg(Tmp2);
1721 }
Nate Begemana9795f82005-03-24 04:41:43 +00001722 }
1723 return;
1724 }
1725 case ISD::EXTLOAD:
1726 case ISD::SEXTLOAD:
1727 case ISD::ZEXTLOAD:
1728 case ISD::LOAD:
1729 case ISD::CopyFromReg:
1730 case ISD::CALL:
1731 case ISD::DYNAMIC_STACKALLOC:
1732 ExprMap.erase(N);
1733 SelectExpr(N);
1734 return;
1735 }
1736 assert(0 && "Should not be reached!");
1737}
1738
1739
1740/// createPPC32PatternInstructionSelector - This pass converts an LLVM function
1741/// into a machine code representation using pattern matching and a machine
1742/// description file.
1743///
1744FunctionPass *llvm::createPPC32ISelPattern(TargetMachine &TM) {
1745 return new ISel(TM);
Chris Lattner246fa632005-03-24 06:16:18 +00001746}
1747