blob: 35be0a07259b7e6e01ef47dab28683a80e4c1e8e [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 SDOperand FIN = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
Nate Begeman6644d4c2005-04-03 23:11:17 +0000198 // If this function is vararg, store any remaining integer argument regs
199 // to their spots on the stack so that they may be loaded by deferencing the
200 // result of va_next.
201 std::vector<SDOperand> MemOps;
202 for (; GPR_remaining > 0; --GPR_remaining, ++GPR_idx) {
203 BuildMI(&BB, PPC::IMPLICIT_DEF, 0, GPR[GPR_idx]);
204 SDOperand Val = DAG.getCopyFromReg(GPR[GPR_idx], MVT::i32, DAG.getRoot());
205 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Val.getValue(1),
206 Val, FIN);
207 MemOps.push_back(Store);
208 // Increment the address by four for the next argument to store
209 SDOperand PtrOff = DAG.getConstant(4, getPointerTy());
210 FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN, PtrOff);
211 }
212 DAG.setRoot(DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps));
Nate Begemanfa554702005-04-03 22:13:27 +0000213 }
Nate Begemana9795f82005-03-24 04:41:43 +0000214
215 return ArgValues;
216}
217
218std::pair<SDOperand, SDOperand>
219PPC32TargetLowering::LowerCallTo(SDOperand Chain,
Nate Begeman307e7442005-03-26 01:28:53 +0000220 const Type *RetTy, bool isVarArg,
221 SDOperand Callee, ArgListTy &Args, SelectionDAG &DAG) {
222 // args_to_use will accumulate outgoing args for the ISD::CALL case in
223 // SelectExpr to use to put the arguments in the appropriate registers.
Nate Begemana9795f82005-03-24 04:41:43 +0000224 std::vector<SDOperand> args_to_use;
Nate Begeman307e7442005-03-26 01:28:53 +0000225
226 // Count how many bytes are to be pushed on the stack, including the linkage
227 // area, and parameter passing area.
228 unsigned NumBytes = 24;
229
230 if (Args.empty()) {
Nate Begemana7e11a42005-04-01 05:57:17 +0000231 Chain = DAG.getNode(ISD::ADJCALLSTACKDOWN, MVT::Other, Chain,
232 DAG.getConstant(NumBytes, getPointerTy()));
Nate Begeman307e7442005-03-26 01:28:53 +0000233 } else {
234 for (unsigned i = 0, e = Args.size(); i != e; ++i)
235 switch (getValueType(Args[i].second)) {
236 default: assert(0 && "Unknown value type!");
237 case MVT::i1:
238 case MVT::i8:
239 case MVT::i16:
240 case MVT::i32:
241 case MVT::f32:
242 NumBytes += 4;
243 break;
244 case MVT::i64:
245 case MVT::f64:
246 NumBytes += 8;
247 break;
248 }
249
250 // Just to be safe, we'll always reserve the full 24 bytes of linkage area
251 // plus 32 bytes of argument space in case any called code gets funky on us.
252 if (NumBytes < 56) NumBytes = 56;
253
254 // Adjust the stack pointer for the new arguments...
255 // These operations are automatically eliminated by the prolog/epilog pass
256 Chain = DAG.getNode(ISD::ADJCALLSTACKDOWN, MVT::Other, Chain,
257 DAG.getConstant(NumBytes, getPointerTy()));
258
259 // Set up a copy of the stack pointer for use loading and storing any
260 // arguments that may not fit in the registers available for argument
261 // passing.
262 SDOperand StackPtr = DAG.getCopyFromReg(PPC::R1, MVT::i32,
263 DAG.getEntryNode());
264
265 // Figure out which arguments are going to go in registers, and which in
266 // memory. Also, if this is a vararg function, floating point operations
267 // must be stored to our stack, and loaded into integer regs as well, if
268 // any integer regs are available for argument passing.
269 unsigned ArgOffset = 24;
270 unsigned GPR_remaining = 8;
271 unsigned FPR_remaining = 13;
Nate Begeman74d73452005-03-31 00:15:26 +0000272
273 std::vector<SDOperand> MemOps;
Nate Begeman307e7442005-03-26 01:28:53 +0000274 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
275 // PtrOff will be used to store the current argument to the stack if a
276 // register cannot be found for it.
277 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
278 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
Nate Begemanf7e43382005-03-26 07:46:36 +0000279 MVT::ValueType ArgVT = getValueType(Args[i].second);
Nate Begeman307e7442005-03-26 01:28:53 +0000280
Nate Begemanf7e43382005-03-26 07:46:36 +0000281 switch (ArgVT) {
Nate Begeman307e7442005-03-26 01:28:53 +0000282 default: assert(0 && "Unexpected ValueType for argument!");
283 case MVT::i1:
284 case MVT::i8:
285 case MVT::i16:
286 // Promote the integer to 32 bits. If the input type is signed use a
287 // sign extend, otherwise use a zero extend.
288 if (Args[i].second->isSigned())
289 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
290 else
291 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
292 // FALL THROUGH
293 case MVT::i32:
294 if (GPR_remaining > 0) {
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000295 args_to_use.push_back(Args[i].first);
Nate Begeman307e7442005-03-26 01:28:53 +0000296 --GPR_remaining;
297 } else {
Nate Begeman74d73452005-03-31 00:15:26 +0000298 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
299 Args[i].first, PtrOff));
Nate Begeman307e7442005-03-26 01:28:53 +0000300 }
301 ArgOffset += 4;
302 break;
303 case MVT::i64:
Nate Begemanf7e43382005-03-26 07:46:36 +0000304 // If we have one free GPR left, we can place the upper half of the i64
305 // in it, and store the other half to the stack. If we have two or more
306 // free GPRs, then we can pass both halves of the i64 in registers.
307 if (GPR_remaining > 0) {
Nate Begemanf2622612005-03-26 02:17:46 +0000308 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
309 Args[i].first, DAG.getConstant(1, MVT::i32));
310 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
311 Args[i].first, DAG.getConstant(0, MVT::i32));
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000312 args_to_use.push_back(Hi);
Nate Begeman74d73452005-03-31 00:15:26 +0000313 --GPR_remaining;
Nate Begeman74d73452005-03-31 00:15:26 +0000314 if (GPR_remaining > 0) {
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000315 args_to_use.push_back(Lo);
Nate Begeman74d73452005-03-31 00:15:26 +0000316 --GPR_remaining;
Nate Begemanf7e43382005-03-26 07:46:36 +0000317 } else {
318 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
319 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
Nate Begeman74d73452005-03-31 00:15:26 +0000320 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
321 Lo, PtrOff));
Nate Begemanf7e43382005-03-26 07:46:36 +0000322 }
Nate Begeman307e7442005-03-26 01:28:53 +0000323 } else {
Nate Begeman74d73452005-03-31 00:15:26 +0000324 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
325 Args[i].first, PtrOff));
Nate Begeman307e7442005-03-26 01:28:53 +0000326 }
327 ArgOffset += 8;
328 break;
329 case MVT::f32:
Nate Begeman307e7442005-03-26 01:28:53 +0000330 case MVT::f64:
Nate Begemanf7e43382005-03-26 07:46:36 +0000331 if (FPR_remaining > 0) {
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000332 args_to_use.push_back(Args[i].first);
333 --FPR_remaining;
Nate Begemanf7e43382005-03-26 07:46:36 +0000334 if (isVarArg) {
Nate Begeman96fc6812005-03-31 02:05:53 +0000335 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Chain,
336 Args[i].first, PtrOff);
337 MemOps.push_back(Store);
Nate Begeman74d73452005-03-31 00:15:26 +0000338 // Float varargs are always shadowed in available integer registers
339 if (GPR_remaining > 0) {
Nate Begeman96fc6812005-03-31 02:05:53 +0000340 SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff);
Nate Begeman74d73452005-03-31 00:15:26 +0000341 MemOps.push_back(Load);
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000342 args_to_use.push_back(Load);
343 --GPR_remaining;
Nate Begeman74d73452005-03-31 00:15:26 +0000344 }
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000345 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
Nate Begeman74d73452005-03-31 00:15:26 +0000346 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
347 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
Nate Begeman96fc6812005-03-31 02:05:53 +0000348 SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff);
Nate Begeman74d73452005-03-31 00:15:26 +0000349 MemOps.push_back(Load);
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000350 args_to_use.push_back(Load);
351 --GPR_remaining;
Nate Begeman74d73452005-03-31 00:15:26 +0000352 }
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000353 } else {
354 // If we have any FPRs remaining, we may also have GPRs remaining.
355 // Args passed in FPRs consume either 1 (f32) or 2 (f64) available
356 // GPRs.
357 if (GPR_remaining > 0) {
358 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
359 --GPR_remaining;
360 }
361 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
362 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
363 --GPR_remaining;
364 }
Nate Begeman74d73452005-03-31 00:15:26 +0000365 }
Nate Begeman307e7442005-03-26 01:28:53 +0000366 } else {
Nate Begeman74d73452005-03-31 00:15:26 +0000367 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
368 Args[i].first, PtrOff));
Nate Begeman307e7442005-03-26 01:28:53 +0000369 }
Nate Begemanf7e43382005-03-26 07:46:36 +0000370 ArgOffset += (ArgVT == MVT::f32) ? 4 : 8;
Nate Begeman307e7442005-03-26 01:28:53 +0000371 break;
372 }
Nate Begemana9795f82005-03-24 04:41:43 +0000373 }
Nate Begeman74d73452005-03-31 00:15:26 +0000374 if (!MemOps.empty())
375 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps);
Nate Begemana9795f82005-03-24 04:41:43 +0000376 }
377
378 std::vector<MVT::ValueType> RetVals;
379 MVT::ValueType RetTyVT = getValueType(RetTy);
380 if (RetTyVT != MVT::isVoid)
381 RetVals.push_back(RetTyVT);
382 RetVals.push_back(MVT::Other);
383
384 SDOperand TheCall = SDOperand(DAG.getCall(RetVals,
385 Chain, Callee, args_to_use), 0);
386 Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
387 Chain = DAG.getNode(ISD::ADJCALLSTACKUP, MVT::Other, Chain,
388 DAG.getConstant(NumBytes, getPointerTy()));
389 return std::make_pair(TheCall, Chain);
390}
391
392std::pair<SDOperand, SDOperand>
393PPC32TargetLowering::LowerVAStart(SDOperand Chain, SelectionDAG &DAG) {
394 //vastart just returns the address of the VarArgsFrameIndex slot.
395 return std::make_pair(DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32), Chain);
396}
397
398std::pair<SDOperand,SDOperand> PPC32TargetLowering::
399LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
400 const Type *ArgTy, SelectionDAG &DAG) {
Nate Begemanc7b09f12005-03-25 08:34:25 +0000401 MVT::ValueType ArgVT = getValueType(ArgTy);
402 SDOperand Result;
403 if (!isVANext) {
404 Result = DAG.getLoad(ArgVT, DAG.getEntryNode(), VAList);
405 } else {
406 unsigned Amt;
407 if (ArgVT == MVT::i32 || ArgVT == MVT::f32)
408 Amt = 4;
409 else {
410 assert((ArgVT == MVT::i64 || ArgVT == MVT::f64) &&
411 "Other types should have been promoted for varargs!");
412 Amt = 8;
413 }
414 Result = DAG.getNode(ISD::ADD, VAList.getValueType(), VAList,
415 DAG.getConstant(Amt, VAList.getValueType()));
416 }
417 return std::make_pair(Result, Chain);
Nate Begemana9795f82005-03-24 04:41:43 +0000418}
419
420
421std::pair<SDOperand, SDOperand> PPC32TargetLowering::
422LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
423 SelectionDAG &DAG) {
Nate Begeman01d05262005-03-30 01:45:43 +0000424 assert(0 && "LowerFrameReturnAddress unimplemented");
Nate Begemana9795f82005-03-24 04:41:43 +0000425 abort();
426}
427
428namespace {
Nate Begemanaa73a9f2005-04-03 11:20:20 +0000429Statistic<>NotLogic("ppc-codegen", "Number of inverted logical ops");
Nate Begemana9795f82005-03-24 04:41:43 +0000430//===--------------------------------------------------------------------===//
431/// ISel - PPC32 specific code to select PPC32 machine instructions for
432/// SelectionDAG operations.
433//===--------------------------------------------------------------------===//
434class ISel : public SelectionDAGISel {
435
436 /// Comment Here.
437 PPC32TargetLowering PPC32Lowering;
438
439 /// ExprMap - As shared expressions are codegen'd, we keep track of which
440 /// vreg the value is produced in, so we only emit one copy of each compiled
441 /// tree.
442 std::map<SDOperand, unsigned> ExprMap;
Nate Begemanc7b09f12005-03-25 08:34:25 +0000443
444 unsigned GlobalBaseReg;
445 bool GlobalBaseInitialized;
Nate Begemana9795f82005-03-24 04:41:43 +0000446
447public:
448 ISel(TargetMachine &TM) : SelectionDAGISel(PPC32Lowering), PPC32Lowering(TM)
449 {}
450
Nate Begemanc7b09f12005-03-25 08:34:25 +0000451 /// runOnFunction - Override this function in order to reset our per-function
452 /// variables.
453 virtual bool runOnFunction(Function &Fn) {
454 // Make sure we re-emit a set of the global base reg if necessary
455 GlobalBaseInitialized = false;
456 return SelectionDAGISel::runOnFunction(Fn);
457 }
458
Nate Begemana9795f82005-03-24 04:41:43 +0000459 /// InstructionSelectBasicBlock - This callback is invoked by
460 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
461 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG) {
462 DEBUG(BB->dump());
463 // Codegen the basic block.
464 Select(DAG.getRoot());
465
466 // Clear state used for selection.
467 ExprMap.clear();
468 }
469
Nate Begemandffcfcc2005-04-01 00:32:34 +0000470 unsigned getGlobalBaseReg();
Nate Begeman6b559972005-04-01 02:59:27 +0000471 unsigned getConstDouble(double floatVal, unsigned Result);
Nate Begemandffcfcc2005-04-01 00:32:34 +0000472 unsigned SelectSetCR0(SDOperand CC);
Nate Begemana9795f82005-03-24 04:41:43 +0000473 unsigned SelectExpr(SDOperand N);
474 unsigned SelectExprFP(SDOperand N, unsigned Result);
475 void Select(SDOperand N);
476
Nate Begeman04730362005-04-01 04:45:11 +0000477 bool SelectAddr(SDOperand N, unsigned& Reg, int& offset);
Nate Begemana9795f82005-03-24 04:41:43 +0000478 void SelectBranchCC(SDOperand N);
479};
480
481/// canUseAsImmediateForOpcode - This method returns a value indicating whether
482/// the ConstantSDNode N can be used as an immediate to Opcode. The return
483/// values are either 0, 1 or 2. 0 indicates that either N is not a
484/// ConstantSDNode, or is not suitable for use by that opcode. A return value
485/// of 1 indicates that the constant may be used in normal immediate form. A
486/// return value of 2 indicates that the constant may be used in shifted
487/// immediate form. If the return value is nonzero, the constant value is
488/// placed in Imm.
489///
490static unsigned canUseAsImmediateForOpcode(SDOperand N, unsigned Opcode,
Nate Begeman3e897162005-03-31 23:55:40 +0000491 unsigned& Imm, bool U = false) {
Nate Begemana9795f82005-03-24 04:41:43 +0000492 if (N.getOpcode() != ISD::Constant) return 0;
493
494 int v = (int)cast<ConstantSDNode>(N)->getSignExtended();
495
496 switch(Opcode) {
497 default: return 0;
498 case ISD::ADD:
499 if (v <= 32767 && v >= -32768) { Imm = v & 0xFFFF; return 1; }
500 if ((v & 0x0000FFFF) == 0) { Imm = v >> 16; return 2; }
501 break;
502 case ISD::AND:
503 case ISD::XOR:
504 case ISD::OR:
505 if (v >= 0 && v <= 65535) { Imm = v & 0xFFFF; return 1; }
506 if ((v & 0x0000FFFF) == 0) { Imm = v >> 16; return 2; }
507 break;
Nate Begeman307e7442005-03-26 01:28:53 +0000508 case ISD::MUL:
Nate Begeman27523a12005-04-02 00:42:16 +0000509 case ISD::SUB:
Nate Begeman307e7442005-03-26 01:28:53 +0000510 if (v <= 32767 && v >= -32768) { Imm = v & 0xFFFF; return 1; }
511 break;
Nate Begeman3e897162005-03-31 23:55:40 +0000512 case ISD::SETCC:
513 if (U && (v >= 0 && v <= 65535)) { Imm = v & 0xFFFF; return 1; }
514 if (!U && (v <= 32767 && v >= -32768)) { Imm = v & 0xFFFF; return 1; }
515 break;
Nate Begemana9795f82005-03-24 04:41:43 +0000516 }
517 return 0;
518}
Nate Begeman3e897162005-03-31 23:55:40 +0000519
520/// getBCCForSetCC - Returns the PowerPC condition branch mnemonic corresponding
521/// to Condition. If the Condition is unordered or unsigned, the bool argument
522/// U is set to true, otherwise it is set to false.
523static unsigned getBCCForSetCC(unsigned Condition, bool& U) {
524 U = false;
525 switch (Condition) {
526 default: assert(0 && "Unknown condition!"); abort();
527 case ISD::SETEQ: return PPC::BEQ;
528 case ISD::SETNE: return PPC::BNE;
529 case ISD::SETULT: U = true;
530 case ISD::SETLT: return PPC::BLT;
531 case ISD::SETULE: U = true;
532 case ISD::SETLE: return PPC::BLE;
533 case ISD::SETUGT: U = true;
534 case ISD::SETGT: return PPC::BGT;
535 case ISD::SETUGE: U = true;
536 case ISD::SETGE: return PPC::BGE;
537 }
Nate Begeman04730362005-04-01 04:45:11 +0000538 return 0;
539}
540
541/// IndexedOpForOp - Return the indexed variant for each of the PowerPC load
542/// and store immediate instructions.
543static unsigned IndexedOpForOp(unsigned Opcode) {
544 switch(Opcode) {
545 default: assert(0 && "Unknown opcode!"); abort();
546 case PPC::LBZ: return PPC::LBZX; case PPC::STB: return PPC::STBX;
547 case PPC::LHZ: return PPC::LHZX; case PPC::STH: return PPC::STHX;
548 case PPC::LHA: return PPC::LHAX; case PPC::STW: return PPC::STWX;
549 case PPC::LWZ: return PPC::LWZX; case PPC::STFS: return PPC::STFSX;
550 case PPC::LFS: return PPC::LFSX; case PPC::STFD: return PPC::STFDX;
551 case PPC::LFD: return PPC::LFDX;
552 }
553 return 0;
Nate Begeman3e897162005-03-31 23:55:40 +0000554}
Nate Begemana9795f82005-03-24 04:41:43 +0000555}
556
Nate Begemanc7b09f12005-03-25 08:34:25 +0000557/// getGlobalBaseReg - Output the instructions required to put the
558/// base address to use for accessing globals into a register.
559///
560unsigned ISel::getGlobalBaseReg() {
561 if (!GlobalBaseInitialized) {
562 // Insert the set of GlobalBaseReg into the first MBB of the function
563 MachineBasicBlock &FirstMBB = BB->getParent()->front();
564 MachineBasicBlock::iterator MBBI = FirstMBB.begin();
565 GlobalBaseReg = MakeReg(MVT::i32);
566 BuildMI(FirstMBB, MBBI, PPC::MovePCtoLR, 0, PPC::LR);
567 BuildMI(FirstMBB, MBBI, PPC::MFLR, 1, GlobalBaseReg).addReg(PPC::LR);
568 GlobalBaseInitialized = true;
569 }
570 return GlobalBaseReg;
571}
572
Nate Begeman6b559972005-04-01 02:59:27 +0000573/// getConstDouble - Loads a floating point value into a register, via the
574/// Constant Pool. Optionally takes a register in which to load the value.
575unsigned ISel::getConstDouble(double doubleVal, unsigned Result=0) {
576 unsigned Tmp1 = MakeReg(MVT::i32);
577 if (0 == Result) Result = MakeReg(MVT::f64);
578 MachineConstantPool *CP = BB->getParent()->getConstantPool();
579 ConstantFP *CFP = ConstantFP::get(Type::DoubleTy, doubleVal);
580 unsigned CPI = CP->getConstantPoolIndex(CFP);
581 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp1).addReg(getGlobalBaseReg())
582 .addConstantPoolIndex(CPI);
583 BuildMI(BB, PPC::LFD, 2, Result).addConstantPoolIndex(CPI).addReg(Tmp1);
584 return Result;
585}
586
Nate Begemandffcfcc2005-04-01 00:32:34 +0000587unsigned ISel::SelectSetCR0(SDOperand CC) {
588 unsigned Opc, Tmp1, Tmp2;
589 static const unsigned CompareOpcodes[] =
590 { PPC::FCMPU, PPC::FCMPU, PPC::CMPW, PPC::CMPLW };
591
592 // If the first operand to the select is a SETCC node, then we can fold it
593 // into the branch that selects which value to return.
594 SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(CC.Val);
595 if (SetCC && CC.getOpcode() == ISD::SETCC) {
596 bool U;
597 Opc = getBCCForSetCC(SetCC->getCondition(), U);
598 Tmp1 = SelectExpr(SetCC->getOperand(0));
599
600 // Pass the optional argument U to canUseAsImmediateForOpcode for SETCC,
601 // so that it knows whether the SETCC immediate range is signed or not.
602 if (1 == canUseAsImmediateForOpcode(SetCC->getOperand(1), ISD::SETCC,
603 Tmp2, U)) {
604 if (U)
605 BuildMI(BB, PPC::CMPLWI, 2, PPC::CR0).addReg(Tmp1).addImm(Tmp2);
606 else
607 BuildMI(BB, PPC::CMPWI, 2, PPC::CR0).addReg(Tmp1).addSImm(Tmp2);
608 } else {
609 bool IsInteger = MVT::isInteger(SetCC->getOperand(0).getValueType());
610 unsigned CompareOpc = CompareOpcodes[2 * IsInteger + U];
611 Tmp2 = SelectExpr(SetCC->getOperand(1));
612 BuildMI(BB, CompareOpc, 2, PPC::CR0).addReg(Tmp1).addReg(Tmp2);
613 }
614 } else {
615 Tmp1 = SelectExpr(CC);
616 BuildMI(BB, PPC::CMPLWI, 2, PPC::CR0).addReg(Tmp1).addImm(0);
617 Opc = PPC::BNE;
618 }
619 return Opc;
620}
621
622/// Check to see if the load is a constant offset from a base register
Nate Begeman04730362005-04-01 04:45:11 +0000623bool ISel::SelectAddr(SDOperand N, unsigned& Reg, int& offset)
Nate Begemana9795f82005-03-24 04:41:43 +0000624{
Nate Begeman96fc6812005-03-31 02:05:53 +0000625 unsigned imm = 0, opcode = N.getOpcode();
Nate Begeman04730362005-04-01 04:45:11 +0000626 if (N.getOpcode() == ISD::ADD) {
627 Reg = SelectExpr(N.getOperand(0));
Nate Begeman96fc6812005-03-31 02:05:53 +0000628 if (1 == canUseAsImmediateForOpcode(N.getOperand(1), opcode, imm)) {
Nate Begeman96fc6812005-03-31 02:05:53 +0000629 offset = imm;
Nate Begeman04730362005-04-01 04:45:11 +0000630 return false;
631 }
632 offset = SelectExpr(N.getOperand(1));
633 return true;
634 }
Nate Begemana9795f82005-03-24 04:41:43 +0000635 Reg = SelectExpr(N);
636 offset = 0;
Nate Begeman04730362005-04-01 04:45:11 +0000637 return false;
Nate Begemana9795f82005-03-24 04:41:43 +0000638}
639
640void ISel::SelectBranchCC(SDOperand N)
641{
642 assert(N.getOpcode() == ISD::BRCOND && "Not a BranchCC???");
643 MachineBasicBlock *Dest =
644 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
Nate Begeman3e897162005-03-31 23:55:40 +0000645
Nate Begemana9795f82005-03-24 04:41:43 +0000646 Select(N.getOperand(0)); //chain
Nate Begemandffcfcc2005-04-01 00:32:34 +0000647 unsigned Opc = SelectSetCR0(N.getOperand(1));
Nate Begeman3e897162005-03-31 23:55:40 +0000648 BuildMI(BB, Opc, 2).addReg(PPC::CR0).addMBB(Dest);
Nate Begemana9795f82005-03-24 04:41:43 +0000649 return;
650}
651
652unsigned ISel::SelectExprFP(SDOperand N, unsigned Result)
653{
654 unsigned Tmp1, Tmp2, Tmp3;
655 unsigned Opc = 0;
656 SDNode *Node = N.Val;
657 MVT::ValueType DestType = N.getValueType();
658 unsigned opcode = N.getOpcode();
659
660 switch (opcode) {
661 default:
662 Node->dump();
663 assert(0 && "Node not handled!\n");
664
Nate Begeman23afcfb2005-03-29 22:48:55 +0000665 case ISD::SELECT: {
Nate Begeman3e897162005-03-31 23:55:40 +0000666 // Attempt to generate FSEL. We can do this whenever we have an FP result,
667 // and an FP comparison in the SetCC node.
668 SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(N.getOperand(0).Val);
669 if (SetCC && N.getOperand(0).getOpcode() == ISD::SETCC &&
670 !MVT::isInteger(SetCC->getOperand(0).getValueType()) &&
671 SetCC->getCondition() != ISD::SETEQ &&
672 SetCC->getCondition() != ISD::SETNE) {
673 MVT::ValueType VT = SetCC->getOperand(0).getValueType();
674 Tmp1 = SelectExpr(SetCC->getOperand(0)); // Val to compare against
675 unsigned TV = SelectExpr(N.getOperand(1)); // Use if TRUE
676 unsigned FV = SelectExpr(N.getOperand(2)); // Use if FALSE
677
678 ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(1));
679 if (CN && (CN->isExactlyValue(-0.0) || CN->isExactlyValue(0.0))) {
680 switch(SetCC->getCondition()) {
681 default: assert(0 && "Invalid FSEL condition"); abort();
682 case ISD::SETULT:
683 case ISD::SETLT:
684 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp1).addReg(FV).addReg(TV);
685 return Result;
686 case ISD::SETUGE:
687 case ISD::SETGE:
688 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp1).addReg(TV).addReg(FV);
689 return Result;
690 case ISD::SETUGT:
691 case ISD::SETGT: {
692 Tmp2 = MakeReg(VT);
693 BuildMI(BB, PPC::FNEG, 1, Tmp2).addReg(Tmp1);
694 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp2).addReg(FV).addReg(TV);
695 return Result;
696 }
697 case ISD::SETULE:
698 case ISD::SETLE: {
699 Tmp2 = MakeReg(VT);
700 BuildMI(BB, PPC::FNEG, 1, Tmp2).addReg(Tmp1);
701 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp2).addReg(TV).addReg(FV);
702 return Result;
703 }
704 }
705 } else {
706 Opc = (MVT::f64 == VT) ? PPC::FSUB : PPC::FSUBS;
707 Tmp2 = SelectExpr(SetCC->getOperand(1));
708 Tmp3 = MakeReg(VT);
709 switch(SetCC->getCondition()) {
710 default: assert(0 && "Invalid FSEL condition"); abort();
711 case ISD::SETULT:
712 case ISD::SETLT:
713 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
714 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(FV).addReg(TV);
715 return Result;
716 case ISD::SETUGE:
717 case ISD::SETGE:
718 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
719 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(TV).addReg(FV);
720 return Result;
721 case ISD::SETUGT:
722 case ISD::SETGT:
723 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp2).addReg(Tmp1);
724 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(FV).addReg(TV);
725 return Result;
726 case ISD::SETULE:
727 case ISD::SETLE:
728 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp2).addReg(Tmp1);
729 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(TV).addReg(FV);
730 return Result;
731 }
732 }
733 assert(0 && "Should never get here");
734 return 0;
735 }
736
Nate Begeman31318e42005-04-01 07:21:30 +0000737 unsigned TrueValue = SelectExpr(N.getOperand(1)); //Use if TRUE
738 unsigned FalseValue = SelectExpr(N.getOperand(2)); //Use if FALSE
Nate Begeman6cb2e1b2005-04-01 08:57:43 +0000739 Opc = SelectSetCR0(N.getOperand(0));
Nate Begeman31318e42005-04-01 07:21:30 +0000740
Nate Begeman23afcfb2005-03-29 22:48:55 +0000741 // Create an iterator with which to insert the MBB for copying the false
742 // value and the MBB to hold the PHI instruction for this SetCC.
743 MachineBasicBlock *thisMBB = BB;
744 const BasicBlock *LLVM_BB = BB->getBasicBlock();
745 ilist<MachineBasicBlock>::iterator It = BB;
746 ++It;
747
748 // thisMBB:
749 // ...
750 // TrueVal = ...
751 // cmpTY cr0, r1, r2
752 // bCC copy1MBB
753 // fallthrough --> copy0MBB
Nate Begeman23afcfb2005-03-29 22:48:55 +0000754 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
755 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
Nate Begeman6cb2e1b2005-04-01 08:57:43 +0000756 BuildMI(BB, Opc, 2).addReg(PPC::CR0).addMBB(sinkMBB);
Nate Begeman23afcfb2005-03-29 22:48:55 +0000757 MachineFunction *F = BB->getParent();
758 F->getBasicBlockList().insert(It, copy0MBB);
759 F->getBasicBlockList().insert(It, sinkMBB);
760 // Update machine-CFG edges
761 BB->addSuccessor(copy0MBB);
762 BB->addSuccessor(sinkMBB);
763
764 // copy0MBB:
765 // %FalseValue = ...
766 // # fallthrough to sinkMBB
767 BB = copy0MBB;
Nate Begeman23afcfb2005-03-29 22:48:55 +0000768 // Update machine-CFG edges
769 BB->addSuccessor(sinkMBB);
770
771 // sinkMBB:
772 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
773 // ...
774 BB = sinkMBB;
775 BuildMI(BB, PPC::PHI, 4, Result).addReg(FalseValue)
776 .addMBB(copy0MBB).addReg(TrueValue).addMBB(thisMBB);
777 return Result;
778 }
Nate Begeman27eeb002005-04-02 05:59:34 +0000779
780 case ISD::FNEG:
781 if (ISD::FABS == N.getOperand(0).getOpcode()) {
782 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
783 BuildMI(BB, PPC::FNABS, 1, Result).addReg(Tmp1);
784 } else {
785 Tmp1 = SelectExpr(N.getOperand(0));
786 BuildMI(BB, PPC::FNEG, 1, Result).addReg(Tmp1);
787 }
788 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +0000789
Nate Begeman27eeb002005-04-02 05:59:34 +0000790 case ISD::FABS:
791 Tmp1 = SelectExpr(N.getOperand(0));
792 BuildMI(BB, PPC::FABS, 1, Result).addReg(Tmp1);
793 return Result;
794
Nate Begemana9795f82005-03-24 04:41:43 +0000795 case ISD::FP_ROUND:
796 assert (DestType == MVT::f32 &&
797 N.getOperand(0).getValueType() == MVT::f64 &&
798 "only f64 to f32 conversion supported here");
799 Tmp1 = SelectExpr(N.getOperand(0));
800 BuildMI(BB, PPC::FRSP, 1, Result).addReg(Tmp1);
801 return Result;
802
803 case ISD::FP_EXTEND:
804 assert (DestType == MVT::f64 &&
805 N.getOperand(0).getValueType() == MVT::f32 &&
806 "only f32 to f64 conversion supported here");
807 Tmp1 = SelectExpr(N.getOperand(0));
808 BuildMI(BB, PPC::FMR, 1, Result).addReg(Tmp1);
809 return Result;
810
811 case ISD::CopyFromReg:
Nate Begemanf2622612005-03-26 02:17:46 +0000812 if (Result == 1)
813 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
814 Tmp1 = dyn_cast<RegSDNode>(Node)->getReg();
815 BuildMI(BB, PPC::FMR, 1, Result).addReg(Tmp1);
816 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +0000817
Nate Begeman6d369cc2005-04-01 01:08:07 +0000818 case ISD::ConstantFP: {
Nate Begeman6d369cc2005-04-01 01:08:07 +0000819 ConstantFPSDNode *CN = cast<ConstantFPSDNode>(N);
Nate Begeman6b559972005-04-01 02:59:27 +0000820 Result = getConstDouble(CN->getValue(), Result);
Nate Begeman6d369cc2005-04-01 01:08:07 +0000821 return Result;
822 }
Nate Begemana9795f82005-03-24 04:41:43 +0000823
824 case ISD::MUL:
825 case ISD::ADD:
826 case ISD::SUB:
827 case ISD::SDIV:
828 switch( opcode ) {
829 case ISD::MUL: Opc = DestType == MVT::f64 ? PPC::FMUL : PPC::FMULS; break;
830 case ISD::ADD: Opc = DestType == MVT::f64 ? PPC::FADD : PPC::FADDS; break;
831 case ISD::SUB: Opc = DestType == MVT::f64 ? PPC::FSUB : PPC::FSUBS; break;
832 case ISD::SDIV: Opc = DestType == MVT::f64 ? PPC::FDIV : PPC::FDIVS; break;
833 };
Nate Begemana9795f82005-03-24 04:41:43 +0000834 Tmp1 = SelectExpr(N.getOperand(0));
835 Tmp2 = SelectExpr(N.getOperand(1));
836 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
837 return Result;
838
Nate Begemana9795f82005-03-24 04:41:43 +0000839 case ISD::UINT_TO_FP:
Nate Begemanfdcf3412005-03-30 19:38:35 +0000840 case ISD::SINT_TO_FP: {
841 assert (N.getOperand(0).getValueType() == MVT::i32
842 && "int to float must operate on i32");
843 bool IsUnsigned = (ISD::UINT_TO_FP == opcode);
844 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
845 Tmp2 = MakeReg(MVT::f64); // temp reg to load the integer value into
846 Tmp3 = MakeReg(MVT::i32); // temp reg to hold the conversion constant
847 unsigned ConstF = MakeReg(MVT::f64); // temp reg to hold the fp constant
848
849 int FrameIdx = BB->getParent()->getFrameInfo()->CreateStackObject(8, 8);
850 MachineConstantPool *CP = BB->getParent()->getConstantPool();
851
852 // FIXME: pull this FP constant generation stuff out into something like
853 // the simple ISel's getReg.
854 if (IsUnsigned) {
855 ConstantFP *CFP = ConstantFP::get(Type::DoubleTy, 0x1.000000p52);
856 unsigned CPI = CP->getConstantPoolIndex(CFP);
857 // Load constant fp value
858 unsigned Tmp4 = MakeReg(MVT::i32);
859 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp4).addReg(getGlobalBaseReg())
860 .addConstantPoolIndex(CPI);
861 BuildMI(BB, PPC::LFD, 2, ConstF).addConstantPoolIndex(CPI).addReg(Tmp4);
862 // Store the hi & low halves of the fp value, currently in int regs
863 BuildMI(BB, PPC::LIS, 1, Tmp3).addSImm(0x4330);
864 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(Tmp3), FrameIdx);
865 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(Tmp1), 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 } else {
870 ConstantFP *CFP = ConstantFP::get(Type::DoubleTy, 0x1.000008p52);
871 unsigned CPI = CP->getConstantPoolIndex(CFP);
872 // Load constant fp value
873 unsigned Tmp4 = MakeReg(MVT::i32);
874 unsigned TmpL = MakeReg(MVT::i32);
875 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp4).addReg(getGlobalBaseReg())
876 .addConstantPoolIndex(CPI);
877 BuildMI(BB, PPC::LFD, 2, ConstF).addConstantPoolIndex(CPI).addReg(Tmp4);
878 // Store the hi & low halves of the fp value, currently in int regs
879 BuildMI(BB, PPC::LIS, 1, Tmp3).addSImm(0x4330);
880 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(Tmp3), FrameIdx);
881 BuildMI(BB, PPC::XORIS, 2, TmpL).addReg(Tmp1).addImm(0x8000);
882 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(TmpL), FrameIdx, 4);
883 addFrameReference(BuildMI(BB, PPC::LFD, 2, Tmp2), FrameIdx);
884 // Generate the return value with a subtract
885 BuildMI(BB, PPC::FSUB, 2, Result).addReg(Tmp2).addReg(ConstF);
886 }
887 return Result;
888 }
Nate Begemana9795f82005-03-24 04:41:43 +0000889 }
Nate Begeman6b559972005-04-01 02:59:27 +0000890 assert(0 && "Should never get here");
Nate Begemana9795f82005-03-24 04:41:43 +0000891 return 0;
892}
893
894unsigned ISel::SelectExpr(SDOperand N) {
895 unsigned Result;
896 unsigned Tmp1, Tmp2, Tmp3;
897 unsigned Opc = 0;
898 unsigned opcode = N.getOpcode();
899
900 SDNode *Node = N.Val;
901 MVT::ValueType DestType = N.getValueType();
902
903 unsigned &Reg = ExprMap[N];
904 if (Reg) return Reg;
905
Nate Begeman27eeb002005-04-02 05:59:34 +0000906 switch (N.getOpcode()) {
907 default:
Nate Begemana9795f82005-03-24 04:41:43 +0000908 Reg = Result = (N.getValueType() != MVT::Other) ?
Nate Begeman27eeb002005-04-02 05:59:34 +0000909 MakeReg(N.getValueType()) : 1;
910 break;
911 case ISD::CALL:
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000912 // If this is a call instruction, make sure to prepare ALL of the result
913 // values as well as the chain.
Nate Begeman27eeb002005-04-02 05:59:34 +0000914 if (Node->getNumValues() == 1)
915 Reg = Result = 1; // Void call, just a chain.
916 else {
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000917 Result = MakeReg(Node->getValueType(0));
918 ExprMap[N.getValue(0)] = Result;
Nate Begeman27eeb002005-04-02 05:59:34 +0000919 for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000920 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
Nate Begeman27eeb002005-04-02 05:59:34 +0000921 ExprMap[SDOperand(Node, Node->getNumValues()-1)] = 1;
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000922 }
Nate Begeman27eeb002005-04-02 05:59:34 +0000923 break;
924 case ISD::ADD_PARTS:
925 case ISD::SUB_PARTS:
926 case ISD::SHL_PARTS:
927 case ISD::SRL_PARTS:
928 case ISD::SRA_PARTS:
929 Result = MakeReg(Node->getValueType(0));
930 ExprMap[N.getValue(0)] = Result;
931 for (unsigned i = 1, e = N.Val->getNumValues(); i != e; ++i)
932 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
933 break;
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000934 }
935
936 if (DestType == MVT::f64 || DestType == MVT::f32)
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000937 if (ISD::LOAD != opcode && ISD::EXTLOAD != opcode && ISD::UNDEF != opcode)
Nate Begeman74d73452005-03-31 00:15:26 +0000938 return SelectExprFP(N, Result);
Nate Begemana9795f82005-03-24 04:41:43 +0000939
940 switch (opcode) {
941 default:
942 Node->dump();
943 assert(0 && "Node not handled!\n");
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000944 case ISD::UNDEF:
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000945 BuildMI(BB, PPC::IMPLICIT_DEF, 0, Result);
946 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +0000947 case ISD::DYNAMIC_STACKALLOC:
Nate Begeman5e966612005-03-24 06:28:42 +0000948 // Generate both result values. FIXME: Need a better commment here?
949 if (Result != 1)
950 ExprMap[N.getValue(1)] = 1;
951 else
952 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
953
954 // FIXME: We are currently ignoring the requested alignment for handling
955 // greater than the stack alignment. This will need to be revisited at some
956 // point. Align = N.getOperand(2);
957 if (!isa<ConstantSDNode>(N.getOperand(2)) ||
958 cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) {
959 std::cerr << "Cannot allocate stack object with greater alignment than"
960 << " the stack alignment yet!";
961 abort();
962 }
963 Select(N.getOperand(0));
964 Tmp1 = SelectExpr(N.getOperand(1));
965 // Subtract size from stack pointer, thereby allocating some space.
966 BuildMI(BB, PPC::SUBF, 2, PPC::R1).addReg(Tmp1).addReg(PPC::R1);
967 // Put a pointer to the space into the result register by copying the SP
968 BuildMI(BB, PPC::OR, 2, Result).addReg(PPC::R1).addReg(PPC::R1);
969 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +0000970
971 case ISD::ConstantPool:
Nate Begemanca12a2b2005-03-28 22:28:37 +0000972 Tmp1 = cast<ConstantPoolSDNode>(N)->getIndex();
973 Tmp2 = MakeReg(MVT::i32);
974 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp2).addReg(getGlobalBaseReg())
975 .addConstantPoolIndex(Tmp1);
976 BuildMI(BB, PPC::LA, 2, Result).addReg(Tmp2).addConstantPoolIndex(Tmp1);
977 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +0000978
979 case ISD::FrameIndex:
Nate Begemanf3d08f32005-03-29 00:03:27 +0000980 Tmp1 = cast<FrameIndexSDNode>(N)->getIndex();
Nate Begeman58f718c2005-03-30 02:23:08 +0000981 addFrameReference(BuildMI(BB, PPC::ADDI, 2, Result), (int)Tmp1, 0, false);
Nate Begemanf3d08f32005-03-29 00:03:27 +0000982 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +0000983
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000984 case ISD::GlobalAddress: {
985 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
Nate Begemanca12a2b2005-03-28 22:28:37 +0000986 Tmp1 = MakeReg(MVT::i32);
Nate Begemanc7b09f12005-03-25 08:34:25 +0000987 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp1).addReg(getGlobalBaseReg())
988 .addGlobalAddress(GV);
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000989 if (GV->hasWeakLinkage() || GV->isExternal()) {
990 BuildMI(BB, PPC::LWZ, 2, Result).addGlobalAddress(GV).addReg(Tmp1);
991 } else {
992 BuildMI(BB, PPC::LA, 2, Result).addReg(Tmp1).addGlobalAddress(GV);
993 }
994 return Result;
995 }
996
Nate Begeman5e966612005-03-24 06:28:42 +0000997 case ISD::LOAD:
Nate Begemana9795f82005-03-24 04:41:43 +0000998 case ISD::EXTLOAD:
999 case ISD::ZEXTLOAD:
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001000 case ISD::SEXTLOAD: {
Nate Begeman9db505c2005-03-28 19:36:43 +00001001 MVT::ValueType TypeBeingLoaded = (ISD::LOAD == opcode) ?
1002 Node->getValueType(0) : cast<MVTSDNode>(Node)->getExtraValueType();
Nate Begeman74d73452005-03-31 00:15:26 +00001003 bool sext = (ISD::SEXTLOAD == opcode);
1004 bool byte = (MVT::i8 == TypeBeingLoaded);
1005
Nate Begeman5e966612005-03-24 06:28:42 +00001006 // Make sure we generate both values.
1007 if (Result != 1)
1008 ExprMap[N.getValue(1)] = 1; // Generate the token
1009 else
1010 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1011
1012 SDOperand Chain = N.getOperand(0);
1013 SDOperand Address = N.getOperand(1);
1014 Select(Chain);
1015
Nate Begeman9db505c2005-03-28 19:36:43 +00001016 switch (TypeBeingLoaded) {
Nate Begeman74d73452005-03-31 00:15:26 +00001017 default: Node->dump(); assert(0 && "Cannot load this type!");
Nate Begeman9db505c2005-03-28 19:36:43 +00001018 case MVT::i1: Opc = PPC::LBZ; break;
1019 case MVT::i8: Opc = PPC::LBZ; break;
1020 case MVT::i16: Opc = sext ? PPC::LHA : PPC::LHZ; break;
1021 case MVT::i32: Opc = PPC::LWZ; break;
Nate Begeman74d73452005-03-31 00:15:26 +00001022 case MVT::f32: Opc = PPC::LFS; break;
1023 case MVT::f64: Opc = PPC::LFD; break;
Nate Begeman5e966612005-03-24 06:28:42 +00001024 }
1025
Nate Begeman74d73452005-03-31 00:15:26 +00001026 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Address)) {
1027 Tmp1 = MakeReg(MVT::i32);
1028 int CPI = CP->getIndex();
1029 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp1).addReg(getGlobalBaseReg())
1030 .addConstantPoolIndex(CPI);
1031 BuildMI(BB, Opc, 2, Result).addConstantPoolIndex(CPI).addReg(Tmp1);
Nate Begeman9db505c2005-03-28 19:36:43 +00001032 }
Nate Begeman74d73452005-03-31 00:15:26 +00001033 else if(Address.getOpcode() == ISD::FrameIndex) {
Nate Begeman58f718c2005-03-30 02:23:08 +00001034 Tmp1 = cast<FrameIndexSDNode>(Address)->getIndex();
1035 addFrameReference(BuildMI(BB, Opc, 2, Result), (int)Tmp1);
Nate Begeman5e966612005-03-24 06:28:42 +00001036 } else {
1037 int offset;
Nate Begeman04730362005-04-01 04:45:11 +00001038 bool idx = SelectAddr(Address, Tmp1, offset);
1039 if (idx) {
1040 Opc = IndexedOpForOp(Opc);
1041 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(offset);
1042 } else {
1043 BuildMI(BB, Opc, 2, Result).addSImm(offset).addReg(Tmp1);
1044 }
Nate Begeman5e966612005-03-24 06:28:42 +00001045 }
1046 return Result;
1047 }
1048
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001049 case ISD::CALL: {
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001050 unsigned GPR_idx = 0, FPR_idx = 0;
1051 static const unsigned GPR[] = {
1052 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
1053 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
1054 };
1055 static const unsigned FPR[] = {
1056 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
1057 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
1058 };
1059
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001060 // Lower the chain for this call.
1061 Select(N.getOperand(0));
1062 ExprMap[N.getValue(Node->getNumValues()-1)] = 1;
Nate Begeman74d73452005-03-31 00:15:26 +00001063
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001064 // Load the register args to virtual regs
1065 std::vector<unsigned> ArgVR;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001066 for(int i = 2, e = Node->getNumOperands(); i < e; ++i)
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001067 ArgVR.push_back(SelectExpr(N.getOperand(i)));
1068
1069 // Copy the virtual registers into the appropriate argument register
1070 for(int i = 0, e = ArgVR.size(); i < e; ++i) {
1071 switch(N.getOperand(i+2).getValueType()) {
1072 default: Node->dump(); assert(0 && "Unknown value type for call");
1073 case MVT::i1:
1074 case MVT::i8:
1075 case MVT::i16:
1076 case MVT::i32:
1077 assert(GPR_idx < 8 && "Too many int args");
1078 if (N.getOperand(i+2).getOpcode() != ISD::UNDEF)
1079 BuildMI(BB, PPC::OR,2,GPR[GPR_idx]).addReg(ArgVR[i]).addReg(ArgVR[i]);
1080 ++GPR_idx;
1081 break;
1082 case MVT::f64:
1083 case MVT::f32:
1084 assert(FPR_idx < 13 && "Too many fp args");
1085 BuildMI(BB, PPC::FMR, 1, FPR[FPR_idx]).addReg(ArgVR[i]);
1086 ++FPR_idx;
1087 break;
1088 }
1089 }
1090
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001091 // Emit the correct call instruction based on the type of symbol called.
1092 if (GlobalAddressSDNode *GASD =
1093 dyn_cast<GlobalAddressSDNode>(N.getOperand(1))) {
1094 BuildMI(BB, PPC::CALLpcrel, 1).addGlobalAddress(GASD->getGlobal(), true);
1095 } else if (ExternalSymbolSDNode *ESSDN =
1096 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1))) {
1097 BuildMI(BB, PPC::CALLpcrel, 1).addExternalSymbol(ESSDN->getSymbol(), true);
1098 } else {
1099 Tmp1 = SelectExpr(N.getOperand(1));
1100 BuildMI(BB, PPC::OR, 2, PPC::R12).addReg(Tmp1).addReg(Tmp1);
1101 BuildMI(BB, PPC::MTCTR, 1).addReg(PPC::R12);
1102 BuildMI(BB, PPC::CALLindirect, 3).addImm(20).addImm(0).addReg(PPC::R12);
1103 }
1104
1105 switch (Node->getValueType(0)) {
1106 default: assert(0 && "Unknown value type for call result!");
1107 case MVT::Other: return 1;
1108 case MVT::i1:
1109 case MVT::i8:
1110 case MVT::i16:
1111 case MVT::i32:
Nate Begemanc7b09f12005-03-25 08:34:25 +00001112 BuildMI(BB, PPC::OR, 2, Result).addReg(PPC::R3).addReg(PPC::R3);
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001113 if (Node->getValueType(1) == MVT::i32)
Nate Begemanc7b09f12005-03-25 08:34:25 +00001114 BuildMI(BB, PPC::OR, 2, Result+1).addReg(PPC::R4).addReg(PPC::R4);
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001115 break;
1116 case MVT::f32:
1117 case MVT::f64:
1118 BuildMI(BB, PPC::FMR, 1, Result).addReg(PPC::F1);
1119 break;
1120 }
1121 return Result+N.ResNo;
1122 }
Nate Begemana9795f82005-03-24 04:41:43 +00001123
1124 case ISD::SIGN_EXTEND:
1125 case ISD::SIGN_EXTEND_INREG:
1126 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman9db505c2005-03-28 19:36:43 +00001127 switch(cast<MVTSDNode>(Node)->getExtraValueType()) {
1128 default: Node->dump(); assert(0 && "Unhandled SIGN_EXTEND type"); break;
1129 case MVT::i16:
1130 BuildMI(BB, PPC::EXTSH, 1, Result).addReg(Tmp1);
1131 break;
1132 case MVT::i8:
1133 BuildMI(BB, PPC::EXTSB, 1, Result).addReg(Tmp1);
1134 break;
Nate Begeman74747862005-03-29 22:24:51 +00001135 case MVT::i1:
1136 BuildMI(BB, PPC::SUBFIC, 2, Result).addReg(Tmp1).addSImm(0);
1137 break;
Nate Begeman9db505c2005-03-28 19:36:43 +00001138 }
Nate Begemana9795f82005-03-24 04:41:43 +00001139 return Result;
1140
1141 case ISD::ZERO_EXTEND_INREG:
1142 Tmp1 = SelectExpr(N.getOperand(0));
1143 switch(cast<MVTSDNode>(Node)->getExtraValueType()) {
Nate Begeman9db505c2005-03-28 19:36:43 +00001144 default: Node->dump(); assert(0 && "Unhandled ZERO_EXTEND type"); break;
Nate Begemana9795f82005-03-24 04:41:43 +00001145 case MVT::i16: Tmp2 = 16; break;
1146 case MVT::i8: Tmp2 = 24; break;
1147 case MVT::i1: Tmp2 = 31; break;
1148 }
Nate Begeman33162522005-03-29 21:54:38 +00001149 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(0).addImm(Tmp2)
1150 .addImm(31);
Nate Begemana9795f82005-03-24 04:41:43 +00001151 return Result;
1152
Nate Begemana9795f82005-03-24 04:41:43 +00001153 case ISD::CopyFromReg:
1154 if (Result == 1)
1155 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1156 Tmp1 = dyn_cast<RegSDNode>(Node)->getReg();
1157 BuildMI(BB, PPC::OR, 2, Result).addReg(Tmp1).addReg(Tmp1);
1158 return Result;
1159
1160 case ISD::SHL:
Nate Begeman5e966612005-03-24 06:28:42 +00001161 Tmp1 = SelectExpr(N.getOperand(0));
1162 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1163 Tmp2 = CN->getValue() & 0x1F;
Nate Begeman33162522005-03-29 21:54:38 +00001164 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(Tmp2).addImm(0)
Nate Begeman5e966612005-03-24 06:28:42 +00001165 .addImm(31-Tmp2);
1166 } else {
1167 Tmp2 = SelectExpr(N.getOperand(1));
1168 BuildMI(BB, PPC::SLW, 2, Result).addReg(Tmp1).addReg(Tmp2);
1169 }
1170 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001171
Nate Begeman5e966612005-03-24 06:28:42 +00001172 case ISD::SRL:
1173 Tmp1 = SelectExpr(N.getOperand(0));
1174 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1175 Tmp2 = CN->getValue() & 0x1F;
Nate Begeman33162522005-03-29 21:54:38 +00001176 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(32-Tmp2)
Nate Begeman5e966612005-03-24 06:28:42 +00001177 .addImm(Tmp2).addImm(31);
1178 } else {
1179 Tmp2 = SelectExpr(N.getOperand(1));
1180 BuildMI(BB, PPC::SRW, 2, Result).addReg(Tmp1).addReg(Tmp2);
1181 }
1182 return Result;
1183
1184 case ISD::SRA:
1185 Tmp1 = SelectExpr(N.getOperand(0));
1186 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1187 Tmp2 = CN->getValue() & 0x1F;
1188 BuildMI(BB, PPC::SRAWI, 2, Result).addReg(Tmp1).addImm(Tmp2);
1189 } else {
1190 Tmp2 = SelectExpr(N.getOperand(1));
1191 BuildMI(BB, PPC::SRAW, 2, Result).addReg(Tmp1).addReg(Tmp2);
1192 }
1193 return Result;
1194
Nate Begemana9795f82005-03-24 04:41:43 +00001195 case ISD::ADD:
1196 assert (DestType == MVT::i32 && "Only do arithmetic on i32s!");
1197 Tmp1 = SelectExpr(N.getOperand(0));
1198 switch(canUseAsImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
1199 default: assert(0 && "unhandled result code");
1200 case 0: // No immediate
1201 Tmp2 = SelectExpr(N.getOperand(1));
1202 BuildMI(BB, PPC::ADD, 2, Result).addReg(Tmp1).addReg(Tmp2);
1203 break;
1204 case 1: // Low immediate
1205 BuildMI(BB, PPC::ADDI, 2, Result).addReg(Tmp1).addSImm(Tmp2);
1206 break;
1207 case 2: // Shifted immediate
1208 BuildMI(BB, PPC::ADDIS, 2, Result).addReg(Tmp1).addSImm(Tmp2);
1209 break;
1210 }
1211 return Result;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001212
Nate Begemana9795f82005-03-24 04:41:43 +00001213 case ISD::AND:
1214 case ISD::OR:
Nate Begemana9795f82005-03-24 04:41:43 +00001215 Tmp1 = SelectExpr(N.getOperand(0));
1216 switch(canUseAsImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
1217 default: assert(0 && "unhandled result code");
1218 case 0: // No immediate
1219 Tmp2 = SelectExpr(N.getOperand(1));
1220 switch (opcode) {
Nate Begeman5e966612005-03-24 06:28:42 +00001221 case ISD::AND: Opc = PPC::AND; break;
1222 case ISD::OR: Opc = PPC::OR; break;
Nate Begemana9795f82005-03-24 04:41:43 +00001223 }
Nate Begeman5e966612005-03-24 06:28:42 +00001224 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +00001225 break;
1226 case 1: // Low immediate
1227 switch (opcode) {
Nate Begeman5e966612005-03-24 06:28:42 +00001228 case ISD::AND: Opc = PPC::ANDIo; break;
1229 case ISD::OR: Opc = PPC::ORI; break;
Nate Begemana9795f82005-03-24 04:41:43 +00001230 }
Nate Begeman5e966612005-03-24 06:28:42 +00001231 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +00001232 break;
1233 case 2: // Shifted immediate
1234 switch (opcode) {
Nate Begeman5e966612005-03-24 06:28:42 +00001235 case ISD::AND: Opc = PPC::ANDISo; break;
1236 case ISD::OR: Opc = PPC::ORIS; break;
Nate Begemana9795f82005-03-24 04:41:43 +00001237 }
Nate Begeman5e966612005-03-24 06:28:42 +00001238 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +00001239 break;
1240 }
1241 return Result;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001242
Nate Begemanaa73a9f2005-04-03 11:20:20 +00001243 case ISD::XOR: {
1244 // Check for EQV: xor, (xor a, -1), b
1245 if (N.getOperand(0).getOpcode() == ISD::XOR &&
1246 N.getOperand(0).getOperand(1).getOpcode() == ISD::Constant &&
1247 cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->isAllOnesValue()) {
1248 ++NotLogic;
1249 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1250 Tmp2 = SelectExpr(N.getOperand(1));
1251 BuildMI(BB, PPC::EQV, 2, Result).addReg(Tmp1).addReg(Tmp2);
1252 return Result;
1253 }
1254 // Check for NOT, NOR, and NAND: xor (copy, or, and), -1
1255 if (N.getOperand(1).getOpcode() == ISD::Constant &&
1256 cast<ConstantSDNode>(N.getOperand(1))->isAllOnesValue()) {
1257 ++NotLogic;
1258 switch(N.getOperand(0).getOpcode()) {
1259 case ISD::OR:
1260 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1261 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1262 BuildMI(BB, PPC::NOR, 2, Result).addReg(Tmp1).addReg(Tmp2);
1263 break;
1264 case ISD::AND:
1265 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1266 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1267 BuildMI(BB, PPC::NAND, 2, Result).addReg(Tmp1).addReg(Tmp2);
1268 break;
1269 default:
1270 Tmp1 = SelectExpr(N.getOperand(0));
1271 BuildMI(BB, PPC::NOR, 2, Result).addReg(Tmp1).addReg(Tmp1);
1272 break;
1273 }
1274 return Result;
1275 }
1276 Tmp1 = SelectExpr(N.getOperand(0));
1277 switch(canUseAsImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
1278 default: assert(0 && "unhandled result code");
1279 case 0: // No immediate
1280 Tmp2 = SelectExpr(N.getOperand(1));
1281 BuildMI(BB, PPC::XOR, 2, Result).addReg(Tmp1).addReg(Tmp2);
1282 break;
1283 case 1: // Low immediate
1284 BuildMI(BB, PPC::XORI, 2, Result).addReg(Tmp1).addImm(Tmp2);
1285 break;
1286 case 2: // Shifted immediate
1287 BuildMI(BB, PPC::XORIS, 2, Result).addReg(Tmp1).addImm(Tmp2);
1288 break;
1289 }
1290 return Result;
1291 }
1292
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001293 case ISD::SUB:
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001294 Tmp2 = SelectExpr(N.getOperand(1));
Nate Begeman27523a12005-04-02 00:42:16 +00001295 if (1 == canUseAsImmediateForOpcode(N.getOperand(0), opcode, Tmp1))
1296 BuildMI(BB, PPC::SUBFIC, 2, Result).addReg(Tmp2).addSImm(Tmp1);
1297 else {
1298 Tmp1 = SelectExpr(N.getOperand(0));
1299 BuildMI(BB, PPC::SUBF, 2, Result).addReg(Tmp2).addReg(Tmp1);
1300 }
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001301 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001302
Nate Begeman5e966612005-03-24 06:28:42 +00001303 case ISD::MUL:
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001304 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman307e7442005-03-26 01:28:53 +00001305 if (1 == canUseAsImmediateForOpcode(N.getOperand(1), opcode, Tmp2))
1306 BuildMI(BB, PPC::MULLI, 2, Result).addReg(Tmp1).addSImm(Tmp2);
1307 else {
1308 Tmp2 = SelectExpr(N.getOperand(1));
1309 BuildMI(BB, PPC::MULLW, 2, Result).addReg(Tmp1).addReg(Tmp2);
1310 }
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001311 return Result;
1312
Nate Begemanf3d08f32005-03-29 00:03:27 +00001313 case ISD::SDIV:
1314 case ISD::UDIV:
Nate Begemanf3d08f32005-03-29 00:03:27 +00001315 Tmp1 = SelectExpr(N.getOperand(0));
1316 Tmp2 = SelectExpr(N.getOperand(1));
1317 Opc = (ISD::UDIV == opcode) ? PPC::DIVWU : PPC::DIVW;
1318 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1319 return Result;
1320
1321 case ISD::UREM:
1322 case ISD::SREM: {
Nate Begemanf3d08f32005-03-29 00:03:27 +00001323 Tmp1 = SelectExpr(N.getOperand(0));
1324 Tmp2 = SelectExpr(N.getOperand(1));
1325 Tmp3 = MakeReg(MVT::i32);
1326 unsigned Tmp4 = MakeReg(MVT::i32);
1327 Opc = (ISD::UREM == opcode) ? PPC::DIVWU : PPC::DIVW;
1328 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
1329 BuildMI(BB, PPC::MULLW, 2, Tmp4).addReg(Tmp3).addReg(Tmp2);
1330 BuildMI(BB, PPC::SUBF, 2, Result).addReg(Tmp4).addReg(Tmp1);
1331 return Result;
1332 }
1333
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001334 case ISD::ADD_PARTS:
Nate Begemanca12a2b2005-03-28 22:28:37 +00001335 case ISD::SUB_PARTS: {
1336 assert(N.getNumOperands() == 4 && N.getValueType() == MVT::i32 &&
1337 "Not an i64 add/sub!");
1338 // Emit all of the operands.
1339 std::vector<unsigned> InVals;
1340 for (unsigned i = 0, e = N.getNumOperands(); i != e; ++i)
1341 InVals.push_back(SelectExpr(N.getOperand(i)));
1342 if (N.getOpcode() == ISD::ADD_PARTS) {
Nate Begeman27eeb002005-04-02 05:59:34 +00001343 BuildMI(BB, PPC::ADDC, 2, Result).addReg(InVals[0]).addReg(InVals[2]);
1344 BuildMI(BB, PPC::ADDE, 2, Result+1).addReg(InVals[1]).addReg(InVals[3]);
Nate Begemanca12a2b2005-03-28 22:28:37 +00001345 } else {
Nate Begeman27eeb002005-04-02 05:59:34 +00001346 BuildMI(BB, PPC::SUBFC, 2, Result).addReg(InVals[2]).addReg(InVals[0]);
1347 BuildMI(BB, PPC::SUBFE, 2, Result+1).addReg(InVals[3]).addReg(InVals[1]);
1348 }
1349 return Result+N.ResNo;
1350 }
1351
1352 case ISD::SHL_PARTS:
1353 case ISD::SRA_PARTS:
1354 case ISD::SRL_PARTS: {
1355 assert(N.getNumOperands() == 3 && N.getValueType() == MVT::i32 &&
1356 "Not an i64 shift!");
1357 unsigned ShiftOpLo = SelectExpr(N.getOperand(0));
1358 unsigned ShiftOpHi = SelectExpr(N.getOperand(1));
1359 unsigned SHReg = SelectExpr(N.getOperand(2));
1360 Tmp1 = MakeReg(MVT::i32);
1361 Tmp2 = MakeReg(MVT::i32);
1362 Tmp3 = MakeReg(MVT::i32);
1363 unsigned Tmp4 = MakeReg(MVT::i32);
1364 unsigned Tmp5 = MakeReg(MVT::i32);
1365 unsigned Tmp6 = MakeReg(MVT::i32);
1366 BuildMI(BB, PPC::SUBFIC, 2, Tmp1).addReg(SHReg).addSImm(32);
1367 if (ISD::SHL_PARTS == opcode) {
1368 BuildMI(BB, PPC::SLW, 2, Tmp2).addReg(ShiftOpHi).addReg(SHReg);
1369 BuildMI(BB, PPC::SRW, 2, Tmp3).addReg(ShiftOpLo).addReg(Tmp1);
1370 BuildMI(BB, PPC::OR, 2, Tmp4).addReg(Tmp2).addReg(Tmp3);
1371 BuildMI(BB, PPC::ADDI, 2, Tmp5).addReg(SHReg).addSImm(-32);
Nate Begemanfa554702005-04-03 22:13:27 +00001372 BuildMI(BB, PPC::SLW, 2, Tmp6).addReg(ShiftOpLo).addReg(Tmp5);
Nate Begeman27eeb002005-04-02 05:59:34 +00001373 BuildMI(BB, PPC::OR, 2, Result+1).addReg(Tmp4).addReg(Tmp6);
1374 BuildMI(BB, PPC::SLW, 2, Result).addReg(ShiftOpLo).addReg(SHReg);
1375 } else if (ISD::SRL_PARTS == opcode) {
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::ADDI, 2, Tmp5).addReg(SHReg).addSImm(-32);
1380 BuildMI(BB, PPC::SRW, 2, Tmp6).addReg(ShiftOpHi).addReg(Tmp5);
1381 BuildMI(BB, PPC::OR, 2, Result).addReg(Tmp4).addReg(Tmp6);
1382 BuildMI(BB, PPC::SRW, 2, Result+1).addReg(ShiftOpHi).addReg(SHReg);
1383 } else {
1384 MachineBasicBlock *TmpMBB = new MachineBasicBlock(BB->getBasicBlock());
1385 MachineBasicBlock *PhiMBB = new MachineBasicBlock(BB->getBasicBlock());
1386 MachineBasicBlock *OldMBB = BB;
1387 MachineFunction *F = BB->getParent();
1388 ilist<MachineBasicBlock>::iterator It = BB; ++It;
1389 F->getBasicBlockList().insert(It, TmpMBB);
1390 F->getBasicBlockList().insert(It, PhiMBB);
1391 BB->addSuccessor(TmpMBB);
1392 BB->addSuccessor(PhiMBB);
1393 BuildMI(BB, PPC::SRW, 2, Tmp2).addReg(ShiftOpLo).addReg(SHReg);
1394 BuildMI(BB, PPC::SLW, 2, Tmp3).addReg(ShiftOpHi).addReg(Tmp1);
1395 BuildMI(BB, PPC::OR, 2, Tmp4).addReg(Tmp2).addReg(Tmp3);
1396 BuildMI(BB, PPC::ADDICo, 2, Tmp5).addReg(SHReg).addSImm(-32);
1397 BuildMI(BB, PPC::SRAW, 2, Tmp6).addReg(ShiftOpHi).addReg(Tmp5);
1398 BuildMI(BB, PPC::SRAW, 2, Result+1).addReg(ShiftOpHi).addReg(SHReg);
1399 BuildMI(BB, PPC::BLE, 2).addReg(PPC::CR0).addMBB(PhiMBB);
1400 // Select correct least significant half if the shift amount > 32
1401 BB = TmpMBB;
1402 unsigned Tmp7 = MakeReg(MVT::i32);
1403 BuildMI(BB, PPC::OR, 2, Tmp7).addReg(Tmp6).addReg(Tmp6);
1404 TmpMBB->addSuccessor(PhiMBB);
1405 BB = PhiMBB;
1406 BuildMI(BB, PPC::PHI, 4, Result).addReg(Tmp4).addMBB(OldMBB)
1407 .addReg(Tmp7).addMBB(TmpMBB);
Nate Begemanca12a2b2005-03-28 22:28:37 +00001408 }
1409 return Result+N.ResNo;
1410 }
1411
Nate Begemana9795f82005-03-24 04:41:43 +00001412 case ISD::FP_TO_UINT:
Nate Begeman6b559972005-04-01 02:59:27 +00001413 case ISD::FP_TO_SINT: {
1414 bool U = (ISD::FP_TO_UINT == opcode);
1415 Tmp1 = SelectExpr(N.getOperand(0));
1416 if (!U) {
1417 Tmp2 = MakeReg(MVT::f64);
1418 BuildMI(BB, PPC::FCTIWZ, 1, Tmp2).addReg(Tmp1);
1419 int FrameIdx = BB->getParent()->getFrameInfo()->CreateStackObject(8, 8);
1420 addFrameReference(BuildMI(BB, PPC::STFD, 3).addReg(Tmp2), FrameIdx);
1421 addFrameReference(BuildMI(BB, PPC::LWZ, 2, Result), FrameIdx, 4);
1422 return Result;
1423 } else {
1424 unsigned Zero = getConstDouble(0.0);
1425 unsigned MaxInt = getConstDouble((1LL << 32) - 1);
1426 unsigned Border = getConstDouble(1LL << 31);
1427 unsigned UseZero = MakeReg(MVT::f64);
1428 unsigned UseMaxInt = MakeReg(MVT::f64);
1429 unsigned UseChoice = MakeReg(MVT::f64);
1430 unsigned TmpReg = MakeReg(MVT::f64);
1431 unsigned TmpReg2 = MakeReg(MVT::f64);
1432 unsigned ConvReg = MakeReg(MVT::f64);
1433 unsigned IntTmp = MakeReg(MVT::i32);
1434 unsigned XorReg = MakeReg(MVT::i32);
1435 MachineFunction *F = BB->getParent();
1436 int FrameIdx = F->getFrameInfo()->CreateStackObject(8, 8);
1437 // Update machine-CFG edges
1438 MachineBasicBlock *XorMBB = new MachineBasicBlock(BB->getBasicBlock());
1439 MachineBasicBlock *PhiMBB = new MachineBasicBlock(BB->getBasicBlock());
1440 MachineBasicBlock *OldMBB = BB;
1441 ilist<MachineBasicBlock>::iterator It = BB; ++It;
1442 F->getBasicBlockList().insert(It, XorMBB);
1443 F->getBasicBlockList().insert(It, PhiMBB);
1444 BB->addSuccessor(XorMBB);
1445 BB->addSuccessor(PhiMBB);
1446 // Convert from floating point to unsigned 32-bit value
1447 // Use 0 if incoming value is < 0.0
1448 BuildMI(BB, PPC::FSEL, 3, UseZero).addReg(Tmp1).addReg(Tmp1).addReg(Zero);
1449 // Use 2**32 - 1 if incoming value is >= 2**32
1450 BuildMI(BB, PPC::FSUB, 2, UseMaxInt).addReg(MaxInt).addReg(Tmp1);
1451 BuildMI(BB, PPC::FSEL, 3, UseChoice).addReg(UseMaxInt).addReg(UseZero)
1452 .addReg(MaxInt);
1453 // Subtract 2**31
1454 BuildMI(BB, PPC::FSUB, 2, TmpReg).addReg(UseChoice).addReg(Border);
1455 // Use difference if >= 2**31
1456 BuildMI(BB, PPC::FCMPU, 2, PPC::CR0).addReg(UseChoice).addReg(Border);
1457 BuildMI(BB, PPC::FSEL, 3, TmpReg2).addReg(TmpReg).addReg(TmpReg)
1458 .addReg(UseChoice);
1459 // Convert to integer
1460 BuildMI(BB, PPC::FCTIWZ, 1, ConvReg).addReg(TmpReg2);
1461 addFrameReference(BuildMI(BB, PPC::STFD, 3).addReg(ConvReg), FrameIdx);
1462 addFrameReference(BuildMI(BB, PPC::LWZ, 2, IntTmp), FrameIdx, 4);
1463 BuildMI(BB, PPC::BLT, 2).addReg(PPC::CR0).addMBB(PhiMBB);
1464 BuildMI(BB, PPC::B, 1).addMBB(XorMBB);
1465
1466 // XorMBB:
1467 // add 2**31 if input was >= 2**31
1468 BB = XorMBB;
1469 BuildMI(BB, PPC::XORIS, 2, XorReg).addReg(IntTmp).addImm(0x8000);
1470 XorMBB->addSuccessor(PhiMBB);
1471
1472 // PhiMBB:
1473 // DestReg = phi [ IntTmp, OldMBB ], [ XorReg, XorMBB ]
1474 BB = PhiMBB;
1475 BuildMI(BB, PPC::PHI, 4, Result).addReg(IntTmp).addMBB(OldMBB)
1476 .addReg(XorReg).addMBB(XorMBB);
1477 return Result;
1478 }
1479 assert(0 && "Should never get here");
1480 return 0;
1481 }
Nate Begemana9795f82005-03-24 04:41:43 +00001482
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001483 case ISD::SETCC:
Nate Begeman33162522005-03-29 21:54:38 +00001484 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Node)) {
Nate Begemandffcfcc2005-04-01 00:32:34 +00001485 Opc = SelectSetCR0(N);
Nate Begeman33162522005-03-29 21:54:38 +00001486
Nate Begeman31318e42005-04-01 07:21:30 +00001487 unsigned TrueValue = MakeReg(MVT::i32);
1488 BuildMI(BB, PPC::LI, 1, TrueValue).addSImm(1);
1489 unsigned FalseValue = MakeReg(MVT::i32);
1490 BuildMI(BB, PPC::LI, 1, FalseValue).addSImm(0);
1491
Nate Begeman33162522005-03-29 21:54:38 +00001492 // Create an iterator with which to insert the MBB for copying the false
1493 // value and the MBB to hold the PHI instruction for this SetCC.
1494 MachineBasicBlock *thisMBB = BB;
1495 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1496 ilist<MachineBasicBlock>::iterator It = BB;
1497 ++It;
1498
1499 // thisMBB:
1500 // ...
1501 // cmpTY cr0, r1, r2
1502 // %TrueValue = li 1
1503 // bCC sinkMBB
Nate Begeman33162522005-03-29 21:54:38 +00001504 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
1505 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
1506 BuildMI(BB, Opc, 2).addReg(PPC::CR0).addMBB(sinkMBB);
1507 MachineFunction *F = BB->getParent();
1508 F->getBasicBlockList().insert(It, copy0MBB);
1509 F->getBasicBlockList().insert(It, sinkMBB);
1510 // Update machine-CFG edges
1511 BB->addSuccessor(copy0MBB);
1512 BB->addSuccessor(sinkMBB);
1513
1514 // copy0MBB:
1515 // %FalseValue = li 0
1516 // fallthrough
1517 BB = copy0MBB;
Nate Begeman33162522005-03-29 21:54:38 +00001518 // Update machine-CFG edges
1519 BB->addSuccessor(sinkMBB);
1520
1521 // sinkMBB:
1522 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1523 // ...
1524 BB = sinkMBB;
1525 BuildMI(BB, PPC::PHI, 4, Result).addReg(FalseValue)
1526 .addMBB(copy0MBB).addReg(TrueValue).addMBB(thisMBB);
1527 return Result;
1528 }
1529 assert(0 && "Is this legal?");
1530 return 0;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001531
Nate Begeman74747862005-03-29 22:24:51 +00001532 case ISD::SELECT: {
Chris Lattner30710192005-04-01 07:10:02 +00001533 unsigned TrueValue = SelectExpr(N.getOperand(1)); //Use if TRUE
1534 unsigned FalseValue = SelectExpr(N.getOperand(2)); //Use if FALSE
Nate Begeman6cb2e1b2005-04-01 08:57:43 +00001535 Opc = SelectSetCR0(N.getOperand(0));
Chris Lattner30710192005-04-01 07:10:02 +00001536
Nate Begeman74747862005-03-29 22:24:51 +00001537 // Create an iterator with which to insert the MBB for copying the false
1538 // value and the MBB to hold the PHI instruction for this SetCC.
1539 MachineBasicBlock *thisMBB = BB;
1540 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1541 ilist<MachineBasicBlock>::iterator It = BB;
1542 ++It;
1543
1544 // thisMBB:
1545 // ...
1546 // TrueVal = ...
1547 // cmpTY cr0, r1, r2
1548 // bCC copy1MBB
1549 // fallthrough --> copy0MBB
Nate Begeman74747862005-03-29 22:24:51 +00001550 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
1551 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
Nate Begeman3e897162005-03-31 23:55:40 +00001552 BuildMI(BB, Opc, 2).addReg(PPC::CR0).addMBB(sinkMBB);
Nate Begeman74747862005-03-29 22:24:51 +00001553 MachineFunction *F = BB->getParent();
1554 F->getBasicBlockList().insert(It, copy0MBB);
1555 F->getBasicBlockList().insert(It, sinkMBB);
1556 // Update machine-CFG edges
1557 BB->addSuccessor(copy0MBB);
1558 BB->addSuccessor(sinkMBB);
1559
1560 // copy0MBB:
1561 // %FalseValue = ...
1562 // # fallthrough to sinkMBB
1563 BB = copy0MBB;
Nate Begeman74747862005-03-29 22:24:51 +00001564 // Update machine-CFG edges
1565 BB->addSuccessor(sinkMBB);
1566
1567 // sinkMBB:
1568 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1569 // ...
1570 BB = sinkMBB;
1571 BuildMI(BB, PPC::PHI, 4, Result).addReg(FalseValue)
1572 .addMBB(copy0MBB).addReg(TrueValue).addMBB(thisMBB);
1573
1574 // FIXME: Select i64?
1575 return Result;
1576 }
Nate Begemana9795f82005-03-24 04:41:43 +00001577
1578 case ISD::Constant:
1579 switch (N.getValueType()) {
1580 default: assert(0 && "Cannot use constants of this type!");
1581 case MVT::i1:
1582 BuildMI(BB, PPC::LI, 1, Result)
1583 .addSImm(!cast<ConstantSDNode>(N)->isNullValue());
1584 break;
1585 case MVT::i32:
1586 {
1587 int v = (int)cast<ConstantSDNode>(N)->getSignExtended();
1588 if (v < 32768 && v >= -32768) {
1589 BuildMI(BB, PPC::LI, 1, Result).addSImm(v);
1590 } else {
Nate Begeman5e966612005-03-24 06:28:42 +00001591 Tmp1 = MakeReg(MVT::i32);
1592 BuildMI(BB, PPC::LIS, 1, Tmp1).addSImm(v >> 16);
1593 BuildMI(BB, PPC::ORI, 2, Result).addReg(Tmp1).addImm(v & 0xFFFF);
Nate Begemana9795f82005-03-24 04:41:43 +00001594 }
1595 }
1596 }
1597 return Result;
1598 }
1599
1600 return 0;
1601}
1602
1603void ISel::Select(SDOperand N) {
1604 unsigned Tmp1, Tmp2, Opc;
1605 unsigned opcode = N.getOpcode();
1606
1607 if (!ExprMap.insert(std::make_pair(N, 1)).second)
1608 return; // Already selected.
1609
1610 SDNode *Node = N.Val;
1611
1612 switch (Node->getOpcode()) {
1613 default:
1614 Node->dump(); std::cerr << "\n";
1615 assert(0 && "Node not handled yet!");
1616 case ISD::EntryToken: return; // Noop
1617 case ISD::TokenFactor:
1618 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1619 Select(Node->getOperand(i));
1620 return;
1621 case ISD::ADJCALLSTACKDOWN:
1622 case ISD::ADJCALLSTACKUP:
1623 Select(N.getOperand(0));
1624 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1625 Opc = N.getOpcode() == ISD::ADJCALLSTACKDOWN ? PPC::ADJCALLSTACKDOWN :
1626 PPC::ADJCALLSTACKUP;
1627 BuildMI(BB, Opc, 1).addImm(Tmp1);
1628 return;
1629 case ISD::BR: {
1630 MachineBasicBlock *Dest =
1631 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
Nate Begemana9795f82005-03-24 04:41:43 +00001632 Select(N.getOperand(0));
1633 BuildMI(BB, PPC::B, 1).addMBB(Dest);
1634 return;
1635 }
1636 case ISD::BRCOND:
1637 SelectBranchCC(N);
1638 return;
1639 case ISD::CopyToReg:
1640 Select(N.getOperand(0));
1641 Tmp1 = SelectExpr(N.getOperand(1));
1642 Tmp2 = cast<RegSDNode>(N)->getReg();
1643
1644 if (Tmp1 != Tmp2) {
1645 if (N.getOperand(1).getValueType() == MVT::f64 ||
1646 N.getOperand(1).getValueType() == MVT::f32)
1647 BuildMI(BB, PPC::FMR, 1, Tmp2).addReg(Tmp1);
1648 else
1649 BuildMI(BB, PPC::OR, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
1650 }
1651 return;
1652 case ISD::ImplicitDef:
1653 Select(N.getOperand(0));
1654 BuildMI(BB, PPC::IMPLICIT_DEF, 0, cast<RegSDNode>(N)->getReg());
1655 return;
1656 case ISD::RET:
1657 switch (N.getNumOperands()) {
1658 default:
1659 assert(0 && "Unknown return instruction!");
1660 case 3:
1661 assert(N.getOperand(1).getValueType() == MVT::i32 &&
1662 N.getOperand(2).getValueType() == MVT::i32 &&
1663 "Unknown two-register value!");
1664 Select(N.getOperand(0));
1665 Tmp1 = SelectExpr(N.getOperand(1));
1666 Tmp2 = SelectExpr(N.getOperand(2));
Nate Begeman27523a12005-04-02 00:42:16 +00001667 BuildMI(BB, PPC::OR, 2, PPC::R3).addReg(Tmp2).addReg(Tmp2);
1668 BuildMI(BB, PPC::OR, 2, PPC::R4).addReg(Tmp1).addReg(Tmp1);
Nate Begemana9795f82005-03-24 04:41:43 +00001669 break;
1670 case 2:
1671 Select(N.getOperand(0));
1672 Tmp1 = SelectExpr(N.getOperand(1));
1673 switch (N.getOperand(1).getValueType()) {
1674 default:
1675 assert(0 && "Unknown return type!");
1676 case MVT::f64:
1677 case MVT::f32:
1678 BuildMI(BB, PPC::FMR, 1, PPC::F1).addReg(Tmp1);
1679 break;
1680 case MVT::i32:
1681 BuildMI(BB, PPC::OR, 2, PPC::R3).addReg(Tmp1).addReg(Tmp1);
1682 break;
1683 }
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001684 case 1:
1685 Select(N.getOperand(0));
1686 break;
Nate Begemana9795f82005-03-24 04:41:43 +00001687 }
1688 BuildMI(BB, PPC::BLR, 0); // Just emit a 'ret' instruction
1689 return;
Nate Begemana9795f82005-03-24 04:41:43 +00001690 case ISD::TRUNCSTORE:
1691 case ISD::STORE:
1692 {
1693 SDOperand Chain = N.getOperand(0);
1694 SDOperand Value = N.getOperand(1);
1695 SDOperand Address = N.getOperand(2);
1696 Select(Chain);
1697
1698 Tmp1 = SelectExpr(Value); //value
1699
1700 if (opcode == ISD::STORE) {
1701 switch(Value.getValueType()) {
1702 default: assert(0 && "unknown Type in store");
1703 case MVT::i32: Opc = PPC::STW; break;
1704 case MVT::f64: Opc = PPC::STFD; break;
1705 case MVT::f32: Opc = PPC::STFS; break;
1706 }
1707 } else { //ISD::TRUNCSTORE
1708 switch(cast<MVTSDNode>(Node)->getExtraValueType()) {
1709 default: assert(0 && "unknown Type in store");
1710 case MVT::i1: //FIXME: DAG does not promote this load
1711 case MVT::i8: Opc = PPC::STB; break;
1712 case MVT::i16: Opc = PPC::STH; break;
1713 }
1714 }
1715
Nate Begemana7e11a42005-04-01 05:57:17 +00001716 if(Address.getOpcode() == ISD::FrameIndex)
Nate Begemana9795f82005-03-24 04:41:43 +00001717 {
Nate Begeman58f718c2005-03-30 02:23:08 +00001718 Tmp2 = cast<FrameIndexSDNode>(Address)->getIndex();
1719 addFrameReference(BuildMI(BB, Opc, 3).addReg(Tmp1), (int)Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +00001720 }
1721 else
1722 {
1723 int offset;
Nate Begeman04730362005-04-01 04:45:11 +00001724 bool idx = SelectAddr(Address, Tmp2, offset);
1725 if (idx) {
1726 Opc = IndexedOpForOp(Opc);
1727 BuildMI(BB, Opc, 3).addReg(Tmp1).addReg(Tmp2).addReg(offset);
1728 } else {
1729 BuildMI(BB, Opc, 3).addReg(Tmp1).addImm(offset).addReg(Tmp2);
1730 }
Nate Begemana9795f82005-03-24 04:41:43 +00001731 }
1732 return;
1733 }
1734 case ISD::EXTLOAD:
1735 case ISD::SEXTLOAD:
1736 case ISD::ZEXTLOAD:
1737 case ISD::LOAD:
1738 case ISD::CopyFromReg:
1739 case ISD::CALL:
1740 case ISD::DYNAMIC_STACKALLOC:
1741 ExprMap.erase(N);
1742 SelectExpr(N);
1743 return;
1744 }
1745 assert(0 && "Should not be reached!");
1746}
1747
1748
1749/// createPPC32PatternInstructionSelector - This pass converts an LLVM function
1750/// into a machine code representation using pattern matching and a machine
1751/// description file.
1752///
1753FunctionPass *llvm::createPPC32ISelPattern(TargetMachine &TM) {
1754 return new ISel(TM);
Chris Lattner246fa632005-03-24 06:16:18 +00001755}
1756