blob: 26639435350fee804fa8f62752e1d89141776b5d [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);
56
Nate Begeman3e897162005-03-31 23:55:40 +000057 addLegalFPImmediate(+0.0); // Necessary for FSEL
58 addLegalFPImmediate(-0.0); //
59
Nate Begemana9795f82005-03-24 04:41:43 +000060 computeRegisterProperties();
61 }
62
63 /// LowerArguments - This hook must be implemented to indicate how we should
64 /// lower the arguments for the specified function, into the specified DAG.
65 virtual std::vector<SDOperand>
66 LowerArguments(Function &F, SelectionDAG &DAG);
67
68 /// LowerCallTo - This hook lowers an abstract call to a function into an
69 /// actual call.
70 virtual std::pair<SDOperand, SDOperand>
Nate Begeman307e7442005-03-26 01:28:53 +000071 LowerCallTo(SDOperand Chain, const Type *RetTy, bool isVarArg,
72 SDOperand Callee, ArgListTy &Args, SelectionDAG &DAG);
Nate Begemana9795f82005-03-24 04:41:43 +000073
74 virtual std::pair<SDOperand, SDOperand>
75 LowerVAStart(SDOperand Chain, SelectionDAG &DAG);
76
77 virtual std::pair<SDOperand,SDOperand>
78 LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
79 const Type *ArgTy, SelectionDAG &DAG);
80
81 virtual std::pair<SDOperand, SDOperand>
82 LowerFrameReturnAddress(bool isFrameAddr, SDOperand Chain, unsigned Depth,
83 SelectionDAG &DAG);
84 };
85}
86
87
88std::vector<SDOperand>
89PPC32TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
90 //
91 // add beautiful description of PPC stack frame format, or at least some docs
92 //
93 MachineFunction &MF = DAG.getMachineFunction();
94 MachineFrameInfo *MFI = MF.getFrameInfo();
95 MachineBasicBlock& BB = MF.front();
96 std::vector<SDOperand> ArgValues;
97
98 // Due to the rather complicated nature of the PowerPC ABI, rather than a
99 // fixed size array of physical args, for the sake of simplicity let the STL
100 // handle tracking them for us.
101 std::vector<unsigned> argVR, argPR, argOp;
102 unsigned ArgOffset = 24;
103 unsigned GPR_remaining = 8;
104 unsigned FPR_remaining = 13;
105 unsigned GPR_idx = 0, FPR_idx = 0;
106 static const unsigned GPR[] = {
107 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
108 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
109 };
110 static const unsigned FPR[] = {
111 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
112 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
113 };
114
115 // Add DAG nodes to load the arguments... On entry to a function on PPC,
116 // the arguments start at offset 24, although they are likely to be passed
117 // in registers.
118 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
119 SDOperand newroot, argt;
120 unsigned ObjSize;
121 bool needsLoad = false;
122 MVT::ValueType ObjectVT = getValueType(I->getType());
123
124 switch (ObjectVT) {
125 default: assert(0 && "Unhandled argument type!");
126 case MVT::i1:
127 case MVT::i8:
128 case MVT::i16:
129 case MVT::i32:
130 ObjSize = 4;
131 if (GPR_remaining > 0) {
132 BuildMI(&BB, PPC::IMPLICIT_DEF, 0, GPR[GPR_idx]);
Nate Begemanf70b5762005-03-28 23:08:54 +0000133 argt = newroot = DAG.getCopyFromReg(GPR[GPR_idx], MVT::i32,
134 DAG.getRoot());
Nate Begemana9795f82005-03-24 04:41:43 +0000135 if (ObjectVT != MVT::i32)
136 argt = DAG.getNode(ISD::TRUNCATE, ObjectVT, newroot);
Nate Begemana9795f82005-03-24 04:41:43 +0000137 } else {
138 needsLoad = true;
139 }
140 break;
Nate Begemanf7e43382005-03-26 07:46:36 +0000141 case MVT::i64: ObjSize = 8;
142 // FIXME: can split 64b load between reg/mem if it is last arg in regs
Nate Begemana9795f82005-03-24 04:41:43 +0000143 if (GPR_remaining > 1) {
144 BuildMI(&BB, PPC::IMPLICIT_DEF, 0, GPR[GPR_idx]);
145 BuildMI(&BB, PPC::IMPLICIT_DEF, 0, GPR[GPR_idx+1]);
Nate Begemanca12a2b2005-03-28 22:28:37 +0000146 // Copy the extracted halves into the virtual registers
Nate Begemanf70b5762005-03-28 23:08:54 +0000147 SDOperand argHi = DAG.getCopyFromReg(GPR[GPR_idx], MVT::i32,
148 DAG.getRoot());
149 SDOperand argLo = DAG.getCopyFromReg(GPR[GPR_idx+1], MVT::i32, argHi);
Nate Begemanca12a2b2005-03-28 22:28:37 +0000150 // Build the outgoing arg thingy
Nate Begemanf70b5762005-03-28 23:08:54 +0000151 argt = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, argLo, argHi);
152 newroot = argLo;
Nate Begemana9795f82005-03-24 04:41:43 +0000153 } else {
154 needsLoad = true;
155 }
156 break;
157 case MVT::f32: ObjSize = 4;
158 case MVT::f64: ObjSize = 8;
159 if (FPR_remaining > 0) {
160 BuildMI(&BB, PPC::IMPLICIT_DEF, 0, FPR[FPR_idx]);
Nate Begemanf70b5762005-03-28 23:08:54 +0000161 argt = newroot = DAG.getCopyFromReg(FPR[FPR_idx], ObjectVT,
162 DAG.getRoot());
Nate Begemana9795f82005-03-24 04:41:43 +0000163 --FPR_remaining;
164 ++FPR_idx;
165 } else {
166 needsLoad = true;
167 }
168 break;
169 }
170
171 // We need to load the argument to a virtual register if we determined above
172 // that we ran out of physical registers of the appropriate type
173 if (needsLoad) {
174 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
175 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
176 argt = newroot = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN);
177 }
178
179 // Every 4 bytes of argument space consumes one of the GPRs available for
180 // argument passing.
181 if (GPR_remaining > 0) {
182 unsigned delta = (GPR_remaining > 1 && ObjSize == 8) ? 2 : 1;
183 GPR_remaining -= delta;
184 GPR_idx += delta;
185 }
186 ArgOffset += ObjSize;
187
188 DAG.setRoot(newroot.getValue(1));
189 ArgValues.push_back(argt);
190 }
191
Nate Begemana9795f82005-03-24 04:41:43 +0000192 // If the function takes variable number of arguments, make a frame index for
193 // the start of the first vararg value... for expansion of llvm.va_start.
194 if (F.isVarArg())
195 VarArgsFrameIndex = MFI->CreateFixedObject(4, ArgOffset);
196
197 return ArgValues;
198}
199
200std::pair<SDOperand, SDOperand>
201PPC32TargetLowering::LowerCallTo(SDOperand Chain,
Nate Begeman307e7442005-03-26 01:28:53 +0000202 const Type *RetTy, bool isVarArg,
203 SDOperand Callee, ArgListTy &Args, SelectionDAG &DAG) {
204 // args_to_use will accumulate outgoing args for the ISD::CALL case in
205 // SelectExpr to use to put the arguments in the appropriate registers.
Nate Begemana9795f82005-03-24 04:41:43 +0000206 std::vector<SDOperand> args_to_use;
Nate Begeman307e7442005-03-26 01:28:53 +0000207
208 // Count how many bytes are to be pushed on the stack, including the linkage
209 // area, and parameter passing area.
210 unsigned NumBytes = 24;
211
212 if (Args.empty()) {
213 NumBytes = 0; // Save zero bytes.
214 } else {
215 for (unsigned i = 0, e = Args.size(); i != e; ++i)
216 switch (getValueType(Args[i].second)) {
217 default: assert(0 && "Unknown value type!");
218 case MVT::i1:
219 case MVT::i8:
220 case MVT::i16:
221 case MVT::i32:
222 case MVT::f32:
223 NumBytes += 4;
224 break;
225 case MVT::i64:
226 case MVT::f64:
227 NumBytes += 8;
228 break;
229 }
230
231 // Just to be safe, we'll always reserve the full 24 bytes of linkage area
232 // plus 32 bytes of argument space in case any called code gets funky on us.
233 if (NumBytes < 56) NumBytes = 56;
234
235 // Adjust the stack pointer for the new arguments...
236 // These operations are automatically eliminated by the prolog/epilog pass
237 Chain = DAG.getNode(ISD::ADJCALLSTACKDOWN, MVT::Other, Chain,
238 DAG.getConstant(NumBytes, getPointerTy()));
239
240 // Set up a copy of the stack pointer for use loading and storing any
241 // arguments that may not fit in the registers available for argument
242 // passing.
243 SDOperand StackPtr = DAG.getCopyFromReg(PPC::R1, MVT::i32,
244 DAG.getEntryNode());
245
246 // Figure out which arguments are going to go in registers, and which in
247 // memory. Also, if this is a vararg function, floating point operations
248 // must be stored to our stack, and loaded into integer regs as well, if
249 // any integer regs are available for argument passing.
250 unsigned ArgOffset = 24;
251 unsigned GPR_remaining = 8;
252 unsigned FPR_remaining = 13;
Nate Begeman74d73452005-03-31 00:15:26 +0000253 unsigned GPR_idx = 0, FPR_idx = 0;
254 static const unsigned GPR[] = {
255 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
256 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
257 };
258 static const unsigned FPR[] = {
259 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
260 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
261 };
262
263 std::vector<SDOperand> MemOps;
Nate Begeman307e7442005-03-26 01:28:53 +0000264 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
265 // PtrOff will be used to store the current argument to the stack if a
266 // register cannot be found for it.
267 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
268 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
Nate Begemanf7e43382005-03-26 07:46:36 +0000269 MVT::ValueType ArgVT = getValueType(Args[i].second);
Nate Begeman307e7442005-03-26 01:28:53 +0000270
Nate Begemanf7e43382005-03-26 07:46:36 +0000271 switch (ArgVT) {
Nate Begeman307e7442005-03-26 01:28:53 +0000272 default: assert(0 && "Unexpected ValueType for argument!");
273 case MVT::i1:
274 case MVT::i8:
275 case MVT::i16:
276 // Promote the integer to 32 bits. If the input type is signed use a
277 // sign extend, otherwise use a zero extend.
278 if (Args[i].second->isSigned())
279 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
280 else
281 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
282 // FALL THROUGH
283 case MVT::i32:
284 if (GPR_remaining > 0) {
Nate Begeman74d73452005-03-31 00:15:26 +0000285 args_to_use.push_back(DAG.getCopyToReg(Chain, Args[i].first,
286 GPR[GPR_idx]));
Nate Begeman307e7442005-03-26 01:28:53 +0000287 --GPR_remaining;
Nate Begeman74d73452005-03-31 00:15:26 +0000288 ++GPR_idx;
Nate Begeman307e7442005-03-26 01:28:53 +0000289 } else {
Nate Begeman74d73452005-03-31 00:15:26 +0000290 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
291 Args[i].first, PtrOff));
Nate Begeman307e7442005-03-26 01:28:53 +0000292 }
293 ArgOffset += 4;
294 break;
295 case MVT::i64:
Nate Begemanf7e43382005-03-26 07:46:36 +0000296 // If we have one free GPR left, we can place the upper half of the i64
297 // in it, and store the other half to the stack. If we have two or more
298 // free GPRs, then we can pass both halves of the i64 in registers.
299 if (GPR_remaining > 0) {
Nate Begemanf2622612005-03-26 02:17:46 +0000300 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
301 Args[i].first, DAG.getConstant(1, MVT::i32));
302 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
303 Args[i].first, DAG.getConstant(0, MVT::i32));
Nate Begeman74d73452005-03-31 00:15:26 +0000304 args_to_use.push_back(DAG.getCopyToReg(Chain, Hi, GPR[GPR_idx]));
305 --GPR_remaining;
306 ++GPR_idx;
307 if (GPR_remaining > 0) {
308 args_to_use.push_back(DAG.getCopyToReg(Chain, Lo, GPR[GPR_idx]));
309 --GPR_remaining;
310 ++GPR_idx;
Nate Begemanf7e43382005-03-26 07:46:36 +0000311 } else {
312 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
313 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
Nate Begeman74d73452005-03-31 00:15:26 +0000314 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
315 Lo, PtrOff));
Nate Begemanf7e43382005-03-26 07:46:36 +0000316 }
Nate Begeman307e7442005-03-26 01:28:53 +0000317 } else {
Nate Begeman74d73452005-03-31 00:15:26 +0000318 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
319 Args[i].first, PtrOff));
Nate Begeman307e7442005-03-26 01:28:53 +0000320 }
321 ArgOffset += 8;
322 break;
323 case MVT::f32:
Nate Begeman307e7442005-03-26 01:28:53 +0000324 case MVT::f64:
Nate Begemanf7e43382005-03-26 07:46:36 +0000325 if (FPR_remaining > 0) {
326 if (isVarArg) {
Nate Begeman96fc6812005-03-31 02:05:53 +0000327 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Chain,
328 Args[i].first, PtrOff);
329 MemOps.push_back(Store);
Nate Begeman74d73452005-03-31 00:15:26 +0000330 // Float varargs are always shadowed in available integer registers
331 if (GPR_remaining > 0) {
Nate Begeman96fc6812005-03-31 02:05:53 +0000332 SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff);
Nate Begeman74d73452005-03-31 00:15:26 +0000333 MemOps.push_back(Load);
Nate Begeman96fc6812005-03-31 02:05:53 +0000334 args_to_use.push_back(DAG.getCopyToReg(Load, Load,
Nate Begeman74d73452005-03-31 00:15:26 +0000335 GPR[GPR_idx]));
336 }
337 if (GPR_remaining > 1 && MVT::f64 == ArgVT) {
338 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
339 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
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 Begeman96fc6812005-03-31 02:05:53 +0000342 args_to_use.push_back(DAG.getCopyToReg(Load, Load,
Nate Begeman74d73452005-03-31 00:15:26 +0000343 GPR[GPR_idx+1]));
344 }
Nate Begemanf7e43382005-03-26 07:46:36 +0000345 }
Nate Begeman74d73452005-03-31 00:15:26 +0000346 args_to_use.push_back(DAG.getCopyToReg(Chain, Args[i].first,
347 FPR[FPR_idx]));
Nate Begeman307e7442005-03-26 01:28:53 +0000348 --FPR_remaining;
Nate Begeman74d73452005-03-31 00:15:26 +0000349 ++FPR_idx;
Nate Begemanf7e43382005-03-26 07:46:36 +0000350 // If we have any FPRs remaining, we may also have GPRs remaining.
351 // Args passed in FPRs consume either 1 (f32) or 2 (f64) available
352 // GPRs.
Nate Begeman74d73452005-03-31 00:15:26 +0000353 if (GPR_remaining > 0) {
354 --GPR_remaining;
355 ++GPR_idx;
356 }
357 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
358 --GPR_remaining;
359 ++GPR_idx;
360 }
Nate Begeman307e7442005-03-26 01:28:53 +0000361 } else {
Nate Begeman74d73452005-03-31 00:15:26 +0000362 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
363 Args[i].first, PtrOff));
Nate Begeman307e7442005-03-26 01:28:53 +0000364 }
Nate Begemanf7e43382005-03-26 07:46:36 +0000365 ArgOffset += (ArgVT == MVT::f32) ? 4 : 8;
Nate Begeman307e7442005-03-26 01:28:53 +0000366 break;
367 }
Nate Begemana9795f82005-03-24 04:41:43 +0000368 }
Nate Begeman74d73452005-03-31 00:15:26 +0000369 if (!MemOps.empty())
370 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps);
Nate Begemana9795f82005-03-24 04:41:43 +0000371 }
372
373 std::vector<MVT::ValueType> RetVals;
374 MVT::ValueType RetTyVT = getValueType(RetTy);
375 if (RetTyVT != MVT::isVoid)
376 RetVals.push_back(RetTyVT);
377 RetVals.push_back(MVT::Other);
378
379 SDOperand TheCall = SDOperand(DAG.getCall(RetVals,
380 Chain, Callee, args_to_use), 0);
381 Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
382 Chain = DAG.getNode(ISD::ADJCALLSTACKUP, MVT::Other, Chain,
383 DAG.getConstant(NumBytes, getPointerTy()));
384 return std::make_pair(TheCall, Chain);
385}
386
387std::pair<SDOperand, SDOperand>
388PPC32TargetLowering::LowerVAStart(SDOperand Chain, SelectionDAG &DAG) {
389 //vastart just returns the address of the VarArgsFrameIndex slot.
390 return std::make_pair(DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32), Chain);
391}
392
393std::pair<SDOperand,SDOperand> PPC32TargetLowering::
394LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
395 const Type *ArgTy, SelectionDAG &DAG) {
Nate Begemanc7b09f12005-03-25 08:34:25 +0000396 MVT::ValueType ArgVT = getValueType(ArgTy);
397 SDOperand Result;
398 if (!isVANext) {
399 Result = DAG.getLoad(ArgVT, DAG.getEntryNode(), VAList);
400 } else {
401 unsigned Amt;
402 if (ArgVT == MVT::i32 || ArgVT == MVT::f32)
403 Amt = 4;
404 else {
405 assert((ArgVT == MVT::i64 || ArgVT == MVT::f64) &&
406 "Other types should have been promoted for varargs!");
407 Amt = 8;
408 }
409 Result = DAG.getNode(ISD::ADD, VAList.getValueType(), VAList,
410 DAG.getConstant(Amt, VAList.getValueType()));
411 }
412 return std::make_pair(Result, Chain);
Nate Begemana9795f82005-03-24 04:41:43 +0000413}
414
415
416std::pair<SDOperand, SDOperand> PPC32TargetLowering::
417LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
418 SelectionDAG &DAG) {
Nate Begeman01d05262005-03-30 01:45:43 +0000419 assert(0 && "LowerFrameReturnAddress unimplemented");
Nate Begemana9795f82005-03-24 04:41:43 +0000420 abort();
421}
422
423namespace {
424
425//===--------------------------------------------------------------------===//
426/// ISel - PPC32 specific code to select PPC32 machine instructions for
427/// SelectionDAG operations.
428//===--------------------------------------------------------------------===//
429class ISel : public SelectionDAGISel {
430
431 /// Comment Here.
432 PPC32TargetLowering PPC32Lowering;
433
434 /// ExprMap - As shared expressions are codegen'd, we keep track of which
435 /// vreg the value is produced in, so we only emit one copy of each compiled
436 /// tree.
437 std::map<SDOperand, unsigned> ExprMap;
Nate Begemanc7b09f12005-03-25 08:34:25 +0000438
439 unsigned GlobalBaseReg;
440 bool GlobalBaseInitialized;
Nate Begemana9795f82005-03-24 04:41:43 +0000441
442public:
443 ISel(TargetMachine &TM) : SelectionDAGISel(PPC32Lowering), PPC32Lowering(TM)
444 {}
445
Nate Begemanc7b09f12005-03-25 08:34:25 +0000446 /// runOnFunction - Override this function in order to reset our per-function
447 /// variables.
448 virtual bool runOnFunction(Function &Fn) {
449 // Make sure we re-emit a set of the global base reg if necessary
450 GlobalBaseInitialized = false;
451 return SelectionDAGISel::runOnFunction(Fn);
452 }
453
Nate Begemana9795f82005-03-24 04:41:43 +0000454 /// InstructionSelectBasicBlock - This callback is invoked by
455 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
456 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG) {
457 DEBUG(BB->dump());
458 // Codegen the basic block.
459 Select(DAG.getRoot());
460
461 // Clear state used for selection.
462 ExprMap.clear();
463 }
464
Nate Begemandffcfcc2005-04-01 00:32:34 +0000465 unsigned getGlobalBaseReg();
Nate Begeman6b559972005-04-01 02:59:27 +0000466 unsigned getConstDouble(double floatVal, unsigned Result);
Nate Begemandffcfcc2005-04-01 00:32:34 +0000467 unsigned SelectSetCR0(SDOperand CC);
Nate Begemana9795f82005-03-24 04:41:43 +0000468 unsigned SelectExpr(SDOperand N);
469 unsigned SelectExprFP(SDOperand N, unsigned Result);
470 void Select(SDOperand N);
471
472 void SelectAddr(SDOperand N, unsigned& Reg, int& offset);
473 void SelectBranchCC(SDOperand N);
474};
475
476/// canUseAsImmediateForOpcode - This method returns a value indicating whether
477/// the ConstantSDNode N can be used as an immediate to Opcode. The return
478/// values are either 0, 1 or 2. 0 indicates that either N is not a
479/// ConstantSDNode, or is not suitable for use by that opcode. A return value
480/// of 1 indicates that the constant may be used in normal immediate form. A
481/// return value of 2 indicates that the constant may be used in shifted
482/// immediate form. If the return value is nonzero, the constant value is
483/// placed in Imm.
484///
485static unsigned canUseAsImmediateForOpcode(SDOperand N, unsigned Opcode,
Nate Begeman3e897162005-03-31 23:55:40 +0000486 unsigned& Imm, bool U = false) {
Nate Begemana9795f82005-03-24 04:41:43 +0000487 if (N.getOpcode() != ISD::Constant) return 0;
488
489 int v = (int)cast<ConstantSDNode>(N)->getSignExtended();
490
491 switch(Opcode) {
492 default: return 0;
493 case ISD::ADD:
494 if (v <= 32767 && v >= -32768) { Imm = v & 0xFFFF; return 1; }
495 if ((v & 0x0000FFFF) == 0) { Imm = v >> 16; return 2; }
496 break;
497 case ISD::AND:
498 case ISD::XOR:
499 case ISD::OR:
500 if (v >= 0 && v <= 65535) { Imm = v & 0xFFFF; return 1; }
501 if ((v & 0x0000FFFF) == 0) { Imm = v >> 16; return 2; }
502 break;
Nate Begeman307e7442005-03-26 01:28:53 +0000503 case ISD::MUL:
504 if (v <= 32767 && v >= -32768) { Imm = v & 0xFFFF; return 1; }
505 break;
Nate Begeman3e897162005-03-31 23:55:40 +0000506 case ISD::SETCC:
507 if (U && (v >= 0 && v <= 65535)) { Imm = v & 0xFFFF; return 1; }
508 if (!U && (v <= 32767 && v >= -32768)) { Imm = v & 0xFFFF; return 1; }
509 break;
Nate Begemana9795f82005-03-24 04:41:43 +0000510 }
511 return 0;
512}
Nate Begeman3e897162005-03-31 23:55:40 +0000513
514/// getBCCForSetCC - Returns the PowerPC condition branch mnemonic corresponding
515/// to Condition. If the Condition is unordered or unsigned, the bool argument
516/// U is set to true, otherwise it is set to false.
517static unsigned getBCCForSetCC(unsigned Condition, bool& U) {
518 U = false;
519 switch (Condition) {
520 default: assert(0 && "Unknown condition!"); abort();
521 case ISD::SETEQ: return PPC::BEQ;
522 case ISD::SETNE: return PPC::BNE;
523 case ISD::SETULT: U = true;
524 case ISD::SETLT: return PPC::BLT;
525 case ISD::SETULE: U = true;
526 case ISD::SETLE: return PPC::BLE;
527 case ISD::SETUGT: U = true;
528 case ISD::SETGT: return PPC::BGT;
529 case ISD::SETUGE: U = true;
530 case ISD::SETGE: return PPC::BGE;
531 }
532}
Nate Begemana9795f82005-03-24 04:41:43 +0000533}
534
Nate Begemanc7b09f12005-03-25 08:34:25 +0000535/// getGlobalBaseReg - Output the instructions required to put the
536/// base address to use for accessing globals into a register.
537///
538unsigned ISel::getGlobalBaseReg() {
539 if (!GlobalBaseInitialized) {
540 // Insert the set of GlobalBaseReg into the first MBB of the function
541 MachineBasicBlock &FirstMBB = BB->getParent()->front();
542 MachineBasicBlock::iterator MBBI = FirstMBB.begin();
543 GlobalBaseReg = MakeReg(MVT::i32);
544 BuildMI(FirstMBB, MBBI, PPC::MovePCtoLR, 0, PPC::LR);
545 BuildMI(FirstMBB, MBBI, PPC::MFLR, 1, GlobalBaseReg).addReg(PPC::LR);
546 GlobalBaseInitialized = true;
547 }
548 return GlobalBaseReg;
549}
550
Nate Begeman6b559972005-04-01 02:59:27 +0000551/// getConstDouble - Loads a floating point value into a register, via the
552/// Constant Pool. Optionally takes a register in which to load the value.
553unsigned ISel::getConstDouble(double doubleVal, unsigned Result=0) {
554 unsigned Tmp1 = MakeReg(MVT::i32);
555 if (0 == Result) Result = MakeReg(MVT::f64);
556 MachineConstantPool *CP = BB->getParent()->getConstantPool();
557 ConstantFP *CFP = ConstantFP::get(Type::DoubleTy, doubleVal);
558 unsigned CPI = CP->getConstantPoolIndex(CFP);
559 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp1).addReg(getGlobalBaseReg())
560 .addConstantPoolIndex(CPI);
561 BuildMI(BB, PPC::LFD, 2, Result).addConstantPoolIndex(CPI).addReg(Tmp1);
562 return Result;
563}
564
Nate Begemandffcfcc2005-04-01 00:32:34 +0000565unsigned ISel::SelectSetCR0(SDOperand CC) {
566 unsigned Opc, Tmp1, Tmp2;
567 static const unsigned CompareOpcodes[] =
568 { PPC::FCMPU, PPC::FCMPU, PPC::CMPW, PPC::CMPLW };
569
570 // If the first operand to the select is a SETCC node, then we can fold it
571 // into the branch that selects which value to return.
572 SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(CC.Val);
573 if (SetCC && CC.getOpcode() == ISD::SETCC) {
574 bool U;
575 Opc = getBCCForSetCC(SetCC->getCondition(), U);
576 Tmp1 = SelectExpr(SetCC->getOperand(0));
577
578 // Pass the optional argument U to canUseAsImmediateForOpcode for SETCC,
579 // so that it knows whether the SETCC immediate range is signed or not.
580 if (1 == canUseAsImmediateForOpcode(SetCC->getOperand(1), ISD::SETCC,
581 Tmp2, U)) {
582 if (U)
583 BuildMI(BB, PPC::CMPLWI, 2, PPC::CR0).addReg(Tmp1).addImm(Tmp2);
584 else
585 BuildMI(BB, PPC::CMPWI, 2, PPC::CR0).addReg(Tmp1).addSImm(Tmp2);
586 } else {
587 bool IsInteger = MVT::isInteger(SetCC->getOperand(0).getValueType());
588 unsigned CompareOpc = CompareOpcodes[2 * IsInteger + U];
589 Tmp2 = SelectExpr(SetCC->getOperand(1));
590 BuildMI(BB, CompareOpc, 2, PPC::CR0).addReg(Tmp1).addReg(Tmp2);
591 }
592 } else {
593 Tmp1 = SelectExpr(CC);
594 BuildMI(BB, PPC::CMPLWI, 2, PPC::CR0).addReg(Tmp1).addImm(0);
595 Opc = PPC::BNE;
596 }
597 return Opc;
598}
599
600/// Check to see if the load is a constant offset from a base register
Nate Begemana9795f82005-03-24 04:41:43 +0000601void ISel::SelectAddr(SDOperand N, unsigned& Reg, int& offset)
602{
Nate Begeman96fc6812005-03-31 02:05:53 +0000603 unsigned imm = 0, opcode = N.getOpcode();
604 if (N.getOpcode() == ISD::ADD)
605 if (1 == canUseAsImmediateForOpcode(N.getOperand(1), opcode, imm)) {
606 Reg = SelectExpr(N.getOperand(0));
607 offset = imm;
608 return;
609 }
Nate Begemana9795f82005-03-24 04:41:43 +0000610 Reg = SelectExpr(N);
611 offset = 0;
612 return;
613}
614
615void ISel::SelectBranchCC(SDOperand N)
616{
617 assert(N.getOpcode() == ISD::BRCOND && "Not a BranchCC???");
618 MachineBasicBlock *Dest =
619 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
Nate Begeman3e897162005-03-31 23:55:40 +0000620
Nate Begemana9795f82005-03-24 04:41:43 +0000621 Select(N.getOperand(0)); //chain
Nate Begemandffcfcc2005-04-01 00:32:34 +0000622 unsigned Opc = SelectSetCR0(N.getOperand(1));
Nate Begeman3e897162005-03-31 23:55:40 +0000623 BuildMI(BB, Opc, 2).addReg(PPC::CR0).addMBB(Dest);
Nate Begemana9795f82005-03-24 04:41:43 +0000624 return;
625}
626
627unsigned ISel::SelectExprFP(SDOperand N, unsigned Result)
628{
629 unsigned Tmp1, Tmp2, Tmp3;
630 unsigned Opc = 0;
631 SDNode *Node = N.Val;
632 MVT::ValueType DestType = N.getValueType();
633 unsigned opcode = N.getOpcode();
634
635 switch (opcode) {
636 default:
637 Node->dump();
638 assert(0 && "Node not handled!\n");
639
Nate Begeman23afcfb2005-03-29 22:48:55 +0000640 case ISD::SELECT: {
Nate Begeman3e897162005-03-31 23:55:40 +0000641 // Attempt to generate FSEL. We can do this whenever we have an FP result,
642 // and an FP comparison in the SetCC node.
643 SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(N.getOperand(0).Val);
644 if (SetCC && N.getOperand(0).getOpcode() == ISD::SETCC &&
645 !MVT::isInteger(SetCC->getOperand(0).getValueType()) &&
646 SetCC->getCondition() != ISD::SETEQ &&
647 SetCC->getCondition() != ISD::SETNE) {
648 MVT::ValueType VT = SetCC->getOperand(0).getValueType();
649 Tmp1 = SelectExpr(SetCC->getOperand(0)); // Val to compare against
650 unsigned TV = SelectExpr(N.getOperand(1)); // Use if TRUE
651 unsigned FV = SelectExpr(N.getOperand(2)); // Use if FALSE
652
653 ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(1));
654 if (CN && (CN->isExactlyValue(-0.0) || CN->isExactlyValue(0.0))) {
655 switch(SetCC->getCondition()) {
656 default: assert(0 && "Invalid FSEL condition"); abort();
657 case ISD::SETULT:
658 case ISD::SETLT:
659 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp1).addReg(FV).addReg(TV);
660 return Result;
661 case ISD::SETUGE:
662 case ISD::SETGE:
663 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp1).addReg(TV).addReg(FV);
664 return Result;
665 case ISD::SETUGT:
666 case ISD::SETGT: {
667 Tmp2 = MakeReg(VT);
668 BuildMI(BB, PPC::FNEG, 1, Tmp2).addReg(Tmp1);
669 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp2).addReg(FV).addReg(TV);
670 return Result;
671 }
672 case ISD::SETULE:
673 case ISD::SETLE: {
674 Tmp2 = MakeReg(VT);
675 BuildMI(BB, PPC::FNEG, 1, Tmp2).addReg(Tmp1);
676 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp2).addReg(TV).addReg(FV);
677 return Result;
678 }
679 }
680 } else {
681 Opc = (MVT::f64 == VT) ? PPC::FSUB : PPC::FSUBS;
682 Tmp2 = SelectExpr(SetCC->getOperand(1));
683 Tmp3 = MakeReg(VT);
684 switch(SetCC->getCondition()) {
685 default: assert(0 && "Invalid FSEL condition"); abort();
686 case ISD::SETULT:
687 case ISD::SETLT:
688 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
689 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(FV).addReg(TV);
690 return Result;
691 case ISD::SETUGE:
692 case ISD::SETGE:
693 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
694 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(TV).addReg(FV);
695 return Result;
696 case ISD::SETUGT:
697 case ISD::SETGT:
698 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp2).addReg(Tmp1);
699 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(FV).addReg(TV);
700 return Result;
701 case ISD::SETULE:
702 case ISD::SETLE:
703 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp2).addReg(Tmp1);
704 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(TV).addReg(FV);
705 return Result;
706 }
707 }
708 assert(0 && "Should never get here");
709 return 0;
710 }
711
Nate Begeman23afcfb2005-03-29 22:48:55 +0000712 // Create an iterator with which to insert the MBB for copying the false
713 // value and the MBB to hold the PHI instruction for this SetCC.
714 MachineBasicBlock *thisMBB = BB;
715 const BasicBlock *LLVM_BB = BB->getBasicBlock();
716 ilist<MachineBasicBlock>::iterator It = BB;
717 ++It;
718
719 // thisMBB:
720 // ...
721 // TrueVal = ...
722 // cmpTY cr0, r1, r2
723 // bCC copy1MBB
724 // fallthrough --> copy0MBB
Nate Begeman3e897162005-03-31 23:55:40 +0000725 Tmp1 = SelectExpr(N.getOperand(0)); //Cond
Nate Begeman23afcfb2005-03-29 22:48:55 +0000726 BuildMI(BB, PPC::CMPLWI, 2, PPC::CR0).addReg(Tmp1).addImm(0);
727 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
728 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
729 unsigned TrueValue = SelectExpr(N.getOperand(1)); //Use if TRUE
730 BuildMI(BB, PPC::BNE, 2).addReg(PPC::CR0).addMBB(sinkMBB);
731 MachineFunction *F = BB->getParent();
732 F->getBasicBlockList().insert(It, copy0MBB);
733 F->getBasicBlockList().insert(It, sinkMBB);
734 // Update machine-CFG edges
735 BB->addSuccessor(copy0MBB);
736 BB->addSuccessor(sinkMBB);
737
738 // copy0MBB:
739 // %FalseValue = ...
740 // # fallthrough to sinkMBB
741 BB = copy0MBB;
742 unsigned FalseValue = SelectExpr(N.getOperand(2)); //Use if FALSE
743 // Update machine-CFG edges
744 BB->addSuccessor(sinkMBB);
745
746 // sinkMBB:
747 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
748 // ...
749 BB = sinkMBB;
750 BuildMI(BB, PPC::PHI, 4, Result).addReg(FalseValue)
751 .addMBB(copy0MBB).addReg(TrueValue).addMBB(thisMBB);
752 return Result;
753 }
Nate Begemana9795f82005-03-24 04:41:43 +0000754
755 case ISD::FP_ROUND:
756 assert (DestType == MVT::f32 &&
757 N.getOperand(0).getValueType() == MVT::f64 &&
758 "only f64 to f32 conversion supported here");
759 Tmp1 = SelectExpr(N.getOperand(0));
760 BuildMI(BB, PPC::FRSP, 1, Result).addReg(Tmp1);
761 return Result;
762
763 case ISD::FP_EXTEND:
764 assert (DestType == MVT::f64 &&
765 N.getOperand(0).getValueType() == MVT::f32 &&
766 "only f32 to f64 conversion supported here");
767 Tmp1 = SelectExpr(N.getOperand(0));
768 BuildMI(BB, PPC::FMR, 1, Result).addReg(Tmp1);
769 return Result;
770
771 case ISD::CopyFromReg:
Nate Begemanf2622612005-03-26 02:17:46 +0000772 if (Result == 1)
773 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
774 Tmp1 = dyn_cast<RegSDNode>(Node)->getReg();
775 BuildMI(BB, PPC::FMR, 1, Result).addReg(Tmp1);
776 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +0000777
Nate Begeman6d369cc2005-04-01 01:08:07 +0000778 case ISD::ConstantFP: {
Nate Begeman6d369cc2005-04-01 01:08:07 +0000779 ConstantFPSDNode *CN = cast<ConstantFPSDNode>(N);
Nate Begeman6b559972005-04-01 02:59:27 +0000780 Result = getConstDouble(CN->getValue(), Result);
Nate Begeman6d369cc2005-04-01 01:08:07 +0000781 return Result;
782 }
Nate Begemana9795f82005-03-24 04:41:43 +0000783
784 case ISD::MUL:
785 case ISD::ADD:
786 case ISD::SUB:
787 case ISD::SDIV:
788 switch( opcode ) {
789 case ISD::MUL: Opc = DestType == MVT::f64 ? PPC::FMUL : PPC::FMULS; break;
790 case ISD::ADD: Opc = DestType == MVT::f64 ? PPC::FADD : PPC::FADDS; break;
791 case ISD::SUB: Opc = DestType == MVT::f64 ? PPC::FSUB : PPC::FSUBS; break;
792 case ISD::SDIV: Opc = DestType == MVT::f64 ? PPC::FDIV : PPC::FDIVS; break;
793 };
Nate Begemana9795f82005-03-24 04:41:43 +0000794 Tmp1 = SelectExpr(N.getOperand(0));
795 Tmp2 = SelectExpr(N.getOperand(1));
796 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
797 return Result;
798
Nate Begemana9795f82005-03-24 04:41:43 +0000799 case ISD::UINT_TO_FP:
Nate Begemanfdcf3412005-03-30 19:38:35 +0000800 case ISD::SINT_TO_FP: {
801 assert (N.getOperand(0).getValueType() == MVT::i32
802 && "int to float must operate on i32");
803 bool IsUnsigned = (ISD::UINT_TO_FP == opcode);
804 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
805 Tmp2 = MakeReg(MVT::f64); // temp reg to load the integer value into
806 Tmp3 = MakeReg(MVT::i32); // temp reg to hold the conversion constant
807 unsigned ConstF = MakeReg(MVT::f64); // temp reg to hold the fp constant
808
809 int FrameIdx = BB->getParent()->getFrameInfo()->CreateStackObject(8, 8);
810 MachineConstantPool *CP = BB->getParent()->getConstantPool();
811
812 // FIXME: pull this FP constant generation stuff out into something like
813 // the simple ISel's getReg.
814 if (IsUnsigned) {
815 ConstantFP *CFP = ConstantFP::get(Type::DoubleTy, 0x1.000000p52);
816 unsigned CPI = CP->getConstantPoolIndex(CFP);
817 // Load constant fp value
818 unsigned Tmp4 = MakeReg(MVT::i32);
819 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp4).addReg(getGlobalBaseReg())
820 .addConstantPoolIndex(CPI);
821 BuildMI(BB, PPC::LFD, 2, ConstF).addConstantPoolIndex(CPI).addReg(Tmp4);
822 // Store the hi & low halves of the fp value, currently in int regs
823 BuildMI(BB, PPC::LIS, 1, Tmp3).addSImm(0x4330);
824 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(Tmp3), FrameIdx);
825 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(Tmp1), FrameIdx, 4);
826 addFrameReference(BuildMI(BB, PPC::LFD, 2, Tmp2), FrameIdx);
827 // Generate the return value with a subtract
828 BuildMI(BB, PPC::FSUB, 2, Result).addReg(Tmp2).addReg(ConstF);
829 } else {
830 ConstantFP *CFP = ConstantFP::get(Type::DoubleTy, 0x1.000008p52);
831 unsigned CPI = CP->getConstantPoolIndex(CFP);
832 // Load constant fp value
833 unsigned Tmp4 = MakeReg(MVT::i32);
834 unsigned TmpL = MakeReg(MVT::i32);
835 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp4).addReg(getGlobalBaseReg())
836 .addConstantPoolIndex(CPI);
837 BuildMI(BB, PPC::LFD, 2, ConstF).addConstantPoolIndex(CPI).addReg(Tmp4);
838 // Store the hi & low halves of the fp value, currently in int regs
839 BuildMI(BB, PPC::LIS, 1, Tmp3).addSImm(0x4330);
840 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(Tmp3), FrameIdx);
841 BuildMI(BB, PPC::XORIS, 2, TmpL).addReg(Tmp1).addImm(0x8000);
842 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(TmpL), FrameIdx, 4);
843 addFrameReference(BuildMI(BB, PPC::LFD, 2, Tmp2), FrameIdx);
844 // Generate the return value with a subtract
845 BuildMI(BB, PPC::FSUB, 2, Result).addReg(Tmp2).addReg(ConstF);
846 }
847 return Result;
848 }
Nate Begemana9795f82005-03-24 04:41:43 +0000849 }
Nate Begeman6b559972005-04-01 02:59:27 +0000850 assert(0 && "Should never get here");
Nate Begemana9795f82005-03-24 04:41:43 +0000851 return 0;
852}
853
854unsigned ISel::SelectExpr(SDOperand N) {
855 unsigned Result;
856 unsigned Tmp1, Tmp2, Tmp3;
857 unsigned Opc = 0;
858 unsigned opcode = N.getOpcode();
859
860 SDNode *Node = N.Val;
861 MVT::ValueType DestType = N.getValueType();
862
863 unsigned &Reg = ExprMap[N];
864 if (Reg) return Reg;
865
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000866 if (N.getOpcode() != ISD::CALL && N.getOpcode() != ISD::ADD_PARTS &&
867 N.getOpcode() != ISD::SUB_PARTS)
Nate Begemana9795f82005-03-24 04:41:43 +0000868 Reg = Result = (N.getValueType() != MVT::Other) ?
869 MakeReg(N.getValueType()) : 1;
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000870 else {
871 // If this is a call instruction, make sure to prepare ALL of the result
872 // values as well as the chain.
873 if (N.getOpcode() == ISD::CALL) {
874 if (Node->getNumValues() == 1)
875 Reg = Result = 1; // Void call, just a chain.
876 else {
877 Result = MakeReg(Node->getValueType(0));
878 ExprMap[N.getValue(0)] = Result;
879 for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
880 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
881 ExprMap[SDOperand(Node, Node->getNumValues()-1)] = 1;
882 }
883 } else {
884 Result = MakeReg(Node->getValueType(0));
885 ExprMap[N.getValue(0)] = Result;
886 for (unsigned i = 1, e = N.Val->getNumValues(); i != e; ++i)
887 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
888 }
889 }
890
891 if (DestType == MVT::f64 || DestType == MVT::f32)
Nate Begeman74d73452005-03-31 00:15:26 +0000892 if (ISD::LOAD != opcode && ISD::EXTLOAD != opcode)
893 return SelectExprFP(N, Result);
Nate Begemana9795f82005-03-24 04:41:43 +0000894
895 switch (opcode) {
896 default:
897 Node->dump();
898 assert(0 && "Node not handled!\n");
899
900 case ISD::DYNAMIC_STACKALLOC:
Nate Begeman5e966612005-03-24 06:28:42 +0000901 // Generate both result values. FIXME: Need a better commment here?
902 if (Result != 1)
903 ExprMap[N.getValue(1)] = 1;
904 else
905 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
906
907 // FIXME: We are currently ignoring the requested alignment for handling
908 // greater than the stack alignment. This will need to be revisited at some
909 // point. Align = N.getOperand(2);
910 if (!isa<ConstantSDNode>(N.getOperand(2)) ||
911 cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) {
912 std::cerr << "Cannot allocate stack object with greater alignment than"
913 << " the stack alignment yet!";
914 abort();
915 }
916 Select(N.getOperand(0));
917 Tmp1 = SelectExpr(N.getOperand(1));
918 // Subtract size from stack pointer, thereby allocating some space.
919 BuildMI(BB, PPC::SUBF, 2, PPC::R1).addReg(Tmp1).addReg(PPC::R1);
920 // Put a pointer to the space into the result register by copying the SP
921 BuildMI(BB, PPC::OR, 2, Result).addReg(PPC::R1).addReg(PPC::R1);
922 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +0000923
924 case ISD::ConstantPool:
Nate Begemanca12a2b2005-03-28 22:28:37 +0000925 Tmp1 = cast<ConstantPoolSDNode>(N)->getIndex();
926 Tmp2 = MakeReg(MVT::i32);
927 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp2).addReg(getGlobalBaseReg())
928 .addConstantPoolIndex(Tmp1);
929 BuildMI(BB, PPC::LA, 2, Result).addReg(Tmp2).addConstantPoolIndex(Tmp1);
930 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +0000931
932 case ISD::FrameIndex:
Nate Begemanf3d08f32005-03-29 00:03:27 +0000933 Tmp1 = cast<FrameIndexSDNode>(N)->getIndex();
Nate Begeman58f718c2005-03-30 02:23:08 +0000934 addFrameReference(BuildMI(BB, PPC::ADDI, 2, Result), (int)Tmp1, 0, false);
Nate Begemanf3d08f32005-03-29 00:03:27 +0000935 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +0000936
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000937 case ISD::GlobalAddress: {
938 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
Nate Begemanca12a2b2005-03-28 22:28:37 +0000939 Tmp1 = MakeReg(MVT::i32);
Nate Begemanc7b09f12005-03-25 08:34:25 +0000940 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp1).addReg(getGlobalBaseReg())
941 .addGlobalAddress(GV);
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000942 if (GV->hasWeakLinkage() || GV->isExternal()) {
943 BuildMI(BB, PPC::LWZ, 2, Result).addGlobalAddress(GV).addReg(Tmp1);
944 } else {
945 BuildMI(BB, PPC::LA, 2, Result).addReg(Tmp1).addGlobalAddress(GV);
946 }
947 return Result;
948 }
949
Nate Begeman5e966612005-03-24 06:28:42 +0000950 case ISD::LOAD:
Nate Begemana9795f82005-03-24 04:41:43 +0000951 case ISD::EXTLOAD:
952 case ISD::ZEXTLOAD:
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000953 case ISD::SEXTLOAD: {
Nate Begeman9db505c2005-03-28 19:36:43 +0000954 MVT::ValueType TypeBeingLoaded = (ISD::LOAD == opcode) ?
955 Node->getValueType(0) : cast<MVTSDNode>(Node)->getExtraValueType();
Nate Begeman74d73452005-03-31 00:15:26 +0000956 bool sext = (ISD::SEXTLOAD == opcode);
957 bool byte = (MVT::i8 == TypeBeingLoaded);
958
Nate Begeman5e966612005-03-24 06:28:42 +0000959 // Make sure we generate both values.
960 if (Result != 1)
961 ExprMap[N.getValue(1)] = 1; // Generate the token
962 else
963 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
964
965 SDOperand Chain = N.getOperand(0);
966 SDOperand Address = N.getOperand(1);
967 Select(Chain);
968
Nate Begeman9db505c2005-03-28 19:36:43 +0000969 switch (TypeBeingLoaded) {
Nate Begeman74d73452005-03-31 00:15:26 +0000970 default: Node->dump(); assert(0 && "Cannot load this type!");
Nate Begeman9db505c2005-03-28 19:36:43 +0000971 case MVT::i1: Opc = PPC::LBZ; break;
972 case MVT::i8: Opc = PPC::LBZ; break;
973 case MVT::i16: Opc = sext ? PPC::LHA : PPC::LHZ; break;
974 case MVT::i32: Opc = PPC::LWZ; break;
Nate Begeman74d73452005-03-31 00:15:26 +0000975 case MVT::f32: Opc = PPC::LFS; break;
976 case MVT::f64: Opc = PPC::LFD; break;
Nate Begeman5e966612005-03-24 06:28:42 +0000977 }
978
Nate Begeman74d73452005-03-31 00:15:26 +0000979 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Address)) {
980 Tmp1 = MakeReg(MVT::i32);
981 int CPI = CP->getIndex();
982 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp1).addReg(getGlobalBaseReg())
983 .addConstantPoolIndex(CPI);
984 BuildMI(BB, Opc, 2, Result).addConstantPoolIndex(CPI).addReg(Tmp1);
Nate Begeman9db505c2005-03-28 19:36:43 +0000985 }
Nate Begeman74d73452005-03-31 00:15:26 +0000986 else if(Address.getOpcode() == ISD::FrameIndex) {
Nate Begeman58f718c2005-03-30 02:23:08 +0000987 Tmp1 = cast<FrameIndexSDNode>(Address)->getIndex();
988 addFrameReference(BuildMI(BB, Opc, 2, Result), (int)Tmp1);
Nate Begeman5e966612005-03-24 06:28:42 +0000989 } else {
990 int offset;
991 SelectAddr(Address, Tmp1, offset);
992 BuildMI(BB, Opc, 2, Result).addSImm(offset).addReg(Tmp1);
993 }
994 return Result;
995 }
996
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000997 case ISD::CALL: {
998 // Lower the chain for this call.
999 Select(N.getOperand(0));
1000 ExprMap[N.getValue(Node->getNumValues()-1)] = 1;
Nate Begeman74d73452005-03-31 00:15:26 +00001001
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001002 for(int i = 2, e = Node->getNumOperands(); i < e; ++i)
Nate Begeman74d73452005-03-31 00:15:26 +00001003 Select(N.getOperand(i));
1004
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001005 // Emit the correct call instruction based on the type of symbol called.
1006 if (GlobalAddressSDNode *GASD =
1007 dyn_cast<GlobalAddressSDNode>(N.getOperand(1))) {
1008 BuildMI(BB, PPC::CALLpcrel, 1).addGlobalAddress(GASD->getGlobal(), true);
1009 } else if (ExternalSymbolSDNode *ESSDN =
1010 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1))) {
1011 BuildMI(BB, PPC::CALLpcrel, 1).addExternalSymbol(ESSDN->getSymbol(), true);
1012 } else {
1013 Tmp1 = SelectExpr(N.getOperand(1));
1014 BuildMI(BB, PPC::OR, 2, PPC::R12).addReg(Tmp1).addReg(Tmp1);
1015 BuildMI(BB, PPC::MTCTR, 1).addReg(PPC::R12);
1016 BuildMI(BB, PPC::CALLindirect, 3).addImm(20).addImm(0).addReg(PPC::R12);
1017 }
1018
1019 switch (Node->getValueType(0)) {
1020 default: assert(0 && "Unknown value type for call result!");
1021 case MVT::Other: return 1;
1022 case MVT::i1:
1023 case MVT::i8:
1024 case MVT::i16:
1025 case MVT::i32:
Nate Begemanc7b09f12005-03-25 08:34:25 +00001026 BuildMI(BB, PPC::OR, 2, Result).addReg(PPC::R3).addReg(PPC::R3);
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001027 if (Node->getValueType(1) == MVT::i32)
Nate Begemanc7b09f12005-03-25 08:34:25 +00001028 BuildMI(BB, PPC::OR, 2, Result+1).addReg(PPC::R4).addReg(PPC::R4);
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001029 break;
1030 case MVT::f32:
1031 case MVT::f64:
1032 BuildMI(BB, PPC::FMR, 1, Result).addReg(PPC::F1);
1033 break;
1034 }
1035 return Result+N.ResNo;
1036 }
Nate Begemana9795f82005-03-24 04:41:43 +00001037
1038 case ISD::SIGN_EXTEND:
1039 case ISD::SIGN_EXTEND_INREG:
1040 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman9db505c2005-03-28 19:36:43 +00001041 switch(cast<MVTSDNode>(Node)->getExtraValueType()) {
1042 default: Node->dump(); assert(0 && "Unhandled SIGN_EXTEND type"); break;
1043 case MVT::i16:
1044 BuildMI(BB, PPC::EXTSH, 1, Result).addReg(Tmp1);
1045 break;
1046 case MVT::i8:
1047 BuildMI(BB, PPC::EXTSB, 1, Result).addReg(Tmp1);
1048 break;
Nate Begeman74747862005-03-29 22:24:51 +00001049 case MVT::i1:
1050 BuildMI(BB, PPC::SUBFIC, 2, Result).addReg(Tmp1).addSImm(0);
1051 break;
Nate Begeman9db505c2005-03-28 19:36:43 +00001052 }
Nate Begemana9795f82005-03-24 04:41:43 +00001053 return Result;
1054
1055 case ISD::ZERO_EXTEND_INREG:
1056 Tmp1 = SelectExpr(N.getOperand(0));
1057 switch(cast<MVTSDNode>(Node)->getExtraValueType()) {
Nate Begeman9db505c2005-03-28 19:36:43 +00001058 default: Node->dump(); assert(0 && "Unhandled ZERO_EXTEND type"); break;
Nate Begemana9795f82005-03-24 04:41:43 +00001059 case MVT::i16: Tmp2 = 16; break;
1060 case MVT::i8: Tmp2 = 24; break;
1061 case MVT::i1: Tmp2 = 31; break;
1062 }
Nate Begeman33162522005-03-29 21:54:38 +00001063 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(0).addImm(Tmp2)
1064 .addImm(31);
Nate Begemana9795f82005-03-24 04:41:43 +00001065 return Result;
1066
Nate Begemana9795f82005-03-24 04:41:43 +00001067 case ISD::CopyFromReg:
1068 if (Result == 1)
1069 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1070 Tmp1 = dyn_cast<RegSDNode>(Node)->getReg();
1071 BuildMI(BB, PPC::OR, 2, Result).addReg(Tmp1).addReg(Tmp1);
1072 return Result;
1073
1074 case ISD::SHL:
Nate Begeman5e966612005-03-24 06:28:42 +00001075 Tmp1 = SelectExpr(N.getOperand(0));
1076 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1077 Tmp2 = CN->getValue() & 0x1F;
Nate Begeman33162522005-03-29 21:54:38 +00001078 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(Tmp2).addImm(0)
Nate Begeman5e966612005-03-24 06:28:42 +00001079 .addImm(31-Tmp2);
1080 } else {
1081 Tmp2 = SelectExpr(N.getOperand(1));
1082 BuildMI(BB, PPC::SLW, 2, Result).addReg(Tmp1).addReg(Tmp2);
1083 }
1084 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001085
Nate Begeman5e966612005-03-24 06:28:42 +00001086 case ISD::SRL:
1087 Tmp1 = SelectExpr(N.getOperand(0));
1088 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1089 Tmp2 = CN->getValue() & 0x1F;
Nate Begeman33162522005-03-29 21:54:38 +00001090 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(32-Tmp2)
Nate Begeman5e966612005-03-24 06:28:42 +00001091 .addImm(Tmp2).addImm(31);
1092 } else {
1093 Tmp2 = SelectExpr(N.getOperand(1));
1094 BuildMI(BB, PPC::SRW, 2, Result).addReg(Tmp1).addReg(Tmp2);
1095 }
1096 return Result;
1097
1098 case ISD::SRA:
1099 Tmp1 = SelectExpr(N.getOperand(0));
1100 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1101 Tmp2 = CN->getValue() & 0x1F;
1102 BuildMI(BB, PPC::SRAWI, 2, Result).addReg(Tmp1).addImm(Tmp2);
1103 } else {
1104 Tmp2 = SelectExpr(N.getOperand(1));
1105 BuildMI(BB, PPC::SRAW, 2, Result).addReg(Tmp1).addReg(Tmp2);
1106 }
1107 return Result;
1108
Nate Begemana9795f82005-03-24 04:41:43 +00001109 case ISD::ADD:
1110 assert (DestType == MVT::i32 && "Only do arithmetic on i32s!");
1111 Tmp1 = SelectExpr(N.getOperand(0));
1112 switch(canUseAsImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
1113 default: assert(0 && "unhandled result code");
1114 case 0: // No immediate
1115 Tmp2 = SelectExpr(N.getOperand(1));
1116 BuildMI(BB, PPC::ADD, 2, Result).addReg(Tmp1).addReg(Tmp2);
1117 break;
1118 case 1: // Low immediate
1119 BuildMI(BB, PPC::ADDI, 2, Result).addReg(Tmp1).addSImm(Tmp2);
1120 break;
1121 case 2: // Shifted immediate
1122 BuildMI(BB, PPC::ADDIS, 2, Result).addReg(Tmp1).addSImm(Tmp2);
1123 break;
1124 }
1125 return Result;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001126
Nate Begemana9795f82005-03-24 04:41:43 +00001127 case ISD::AND:
1128 case ISD::OR:
1129 case ISD::XOR:
1130 assert (DestType == MVT::i32 && "Only do arithmetic on i32s!");
1131 Tmp1 = SelectExpr(N.getOperand(0));
1132 switch(canUseAsImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
1133 default: assert(0 && "unhandled result code");
1134 case 0: // No immediate
1135 Tmp2 = SelectExpr(N.getOperand(1));
1136 switch (opcode) {
Nate Begeman5e966612005-03-24 06:28:42 +00001137 case ISD::AND: Opc = PPC::AND; break;
1138 case ISD::OR: Opc = PPC::OR; break;
1139 case ISD::XOR: Opc = PPC::XOR; break;
Nate Begemana9795f82005-03-24 04:41:43 +00001140 }
Nate Begeman5e966612005-03-24 06:28:42 +00001141 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +00001142 break;
1143 case 1: // Low immediate
1144 switch (opcode) {
Nate Begeman5e966612005-03-24 06:28:42 +00001145 case ISD::AND: Opc = PPC::ANDIo; break;
1146 case ISD::OR: Opc = PPC::ORI; break;
1147 case ISD::XOR: Opc = PPC::XORI; break;
Nate Begemana9795f82005-03-24 04:41:43 +00001148 }
Nate Begeman5e966612005-03-24 06:28:42 +00001149 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +00001150 break;
1151 case 2: // Shifted immediate
1152 switch (opcode) {
Nate Begeman5e966612005-03-24 06:28:42 +00001153 case ISD::AND: Opc = PPC::ANDISo; break;
1154 case ISD::OR: Opc = PPC::ORIS; break;
1155 case ISD::XOR: Opc = PPC::XORIS; break;
Nate Begemana9795f82005-03-24 04:41:43 +00001156 }
Nate Begeman5e966612005-03-24 06:28:42 +00001157 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +00001158 break;
1159 }
1160 return Result;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001161
1162 case ISD::SUB:
1163 assert (DestType == MVT::i32 && "Only do arithmetic on i32s!");
1164 Tmp1 = SelectExpr(N.getOperand(0));
1165 Tmp2 = SelectExpr(N.getOperand(1));
1166 BuildMI(BB, PPC::SUBF, 2, Result).addReg(Tmp2).addReg(Tmp1);
1167 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001168
Nate Begeman5e966612005-03-24 06:28:42 +00001169 case ISD::MUL:
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001170 assert (DestType == MVT::i32 && "Only do arithmetic on i32s!");
1171 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman307e7442005-03-26 01:28:53 +00001172 if (1 == canUseAsImmediateForOpcode(N.getOperand(1), opcode, Tmp2))
1173 BuildMI(BB, PPC::MULLI, 2, Result).addReg(Tmp1).addSImm(Tmp2);
1174 else {
1175 Tmp2 = SelectExpr(N.getOperand(1));
1176 BuildMI(BB, PPC::MULLW, 2, Result).addReg(Tmp1).addReg(Tmp2);
1177 }
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001178 return Result;
1179
Nate Begemanf3d08f32005-03-29 00:03:27 +00001180 case ISD::SDIV:
1181 case ISD::UDIV:
1182 assert (DestType == MVT::i32 && "Only do arithmetic on i32s!");
1183 Tmp1 = SelectExpr(N.getOperand(0));
1184 Tmp2 = SelectExpr(N.getOperand(1));
1185 Opc = (ISD::UDIV == opcode) ? PPC::DIVWU : PPC::DIVW;
1186 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1187 return Result;
1188
1189 case ISD::UREM:
1190 case ISD::SREM: {
1191 assert (DestType == MVT::i32 && "Only do arithmetic on i32s!");
1192 Tmp1 = SelectExpr(N.getOperand(0));
1193 Tmp2 = SelectExpr(N.getOperand(1));
1194 Tmp3 = MakeReg(MVT::i32);
1195 unsigned Tmp4 = MakeReg(MVT::i32);
1196 Opc = (ISD::UREM == opcode) ? PPC::DIVWU : PPC::DIVW;
1197 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
1198 BuildMI(BB, PPC::MULLW, 2, Tmp4).addReg(Tmp3).addReg(Tmp2);
1199 BuildMI(BB, PPC::SUBF, 2, Result).addReg(Tmp4).addReg(Tmp1);
1200 return Result;
1201 }
1202
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001203 case ISD::ADD_PARTS:
Nate Begemanca12a2b2005-03-28 22:28:37 +00001204 case ISD::SUB_PARTS: {
1205 assert(N.getNumOperands() == 4 && N.getValueType() == MVT::i32 &&
1206 "Not an i64 add/sub!");
1207 // Emit all of the operands.
1208 std::vector<unsigned> InVals;
1209 for (unsigned i = 0, e = N.getNumOperands(); i != e; ++i)
1210 InVals.push_back(SelectExpr(N.getOperand(i)));
1211 if (N.getOpcode() == ISD::ADD_PARTS) {
Nate Begemanf70b5762005-03-28 23:08:54 +00001212 BuildMI(BB, PPC::ADDC, 2, Result+1).addReg(InVals[0]).addReg(InVals[2]);
1213 BuildMI(BB, PPC::ADDE, 2, Result).addReg(InVals[1]).addReg(InVals[3]);
Nate Begemanca12a2b2005-03-28 22:28:37 +00001214 } else {
Nate Begemanf70b5762005-03-28 23:08:54 +00001215 BuildMI(BB, PPC::SUBFC, 2, Result+1).addReg(InVals[2]).addReg(InVals[0]);
1216 BuildMI(BB, PPC::SUBFE, 2, Result).addReg(InVals[3]).addReg(InVals[1]);
Nate Begemanca12a2b2005-03-28 22:28:37 +00001217 }
1218 return Result+N.ResNo;
1219 }
1220
Nate Begemana9795f82005-03-24 04:41:43 +00001221 case ISD::FP_TO_UINT:
Nate Begeman6b559972005-04-01 02:59:27 +00001222 case ISD::FP_TO_SINT: {
1223 bool U = (ISD::FP_TO_UINT == opcode);
1224 Tmp1 = SelectExpr(N.getOperand(0));
1225 if (!U) {
1226 Tmp2 = MakeReg(MVT::f64);
1227 BuildMI(BB, PPC::FCTIWZ, 1, Tmp2).addReg(Tmp1);
1228 int FrameIdx = BB->getParent()->getFrameInfo()->CreateStackObject(8, 8);
1229 addFrameReference(BuildMI(BB, PPC::STFD, 3).addReg(Tmp2), FrameIdx);
1230 addFrameReference(BuildMI(BB, PPC::LWZ, 2, Result), FrameIdx, 4);
1231 return Result;
1232 } else {
1233 unsigned Zero = getConstDouble(0.0);
1234 unsigned MaxInt = getConstDouble((1LL << 32) - 1);
1235 unsigned Border = getConstDouble(1LL << 31);
1236 unsigned UseZero = MakeReg(MVT::f64);
1237 unsigned UseMaxInt = MakeReg(MVT::f64);
1238 unsigned UseChoice = MakeReg(MVT::f64);
1239 unsigned TmpReg = MakeReg(MVT::f64);
1240 unsigned TmpReg2 = MakeReg(MVT::f64);
1241 unsigned ConvReg = MakeReg(MVT::f64);
1242 unsigned IntTmp = MakeReg(MVT::i32);
1243 unsigned XorReg = MakeReg(MVT::i32);
1244 MachineFunction *F = BB->getParent();
1245 int FrameIdx = F->getFrameInfo()->CreateStackObject(8, 8);
1246 // Update machine-CFG edges
1247 MachineBasicBlock *XorMBB = new MachineBasicBlock(BB->getBasicBlock());
1248 MachineBasicBlock *PhiMBB = new MachineBasicBlock(BB->getBasicBlock());
1249 MachineBasicBlock *OldMBB = BB;
1250 ilist<MachineBasicBlock>::iterator It = BB; ++It;
1251 F->getBasicBlockList().insert(It, XorMBB);
1252 F->getBasicBlockList().insert(It, PhiMBB);
1253 BB->addSuccessor(XorMBB);
1254 BB->addSuccessor(PhiMBB);
1255 // Convert from floating point to unsigned 32-bit value
1256 // Use 0 if incoming value is < 0.0
1257 BuildMI(BB, PPC::FSEL, 3, UseZero).addReg(Tmp1).addReg(Tmp1).addReg(Zero);
1258 // Use 2**32 - 1 if incoming value is >= 2**32
1259 BuildMI(BB, PPC::FSUB, 2, UseMaxInt).addReg(MaxInt).addReg(Tmp1);
1260 BuildMI(BB, PPC::FSEL, 3, UseChoice).addReg(UseMaxInt).addReg(UseZero)
1261 .addReg(MaxInt);
1262 // Subtract 2**31
1263 BuildMI(BB, PPC::FSUB, 2, TmpReg).addReg(UseChoice).addReg(Border);
1264 // Use difference if >= 2**31
1265 BuildMI(BB, PPC::FCMPU, 2, PPC::CR0).addReg(UseChoice).addReg(Border);
1266 BuildMI(BB, PPC::FSEL, 3, TmpReg2).addReg(TmpReg).addReg(TmpReg)
1267 .addReg(UseChoice);
1268 // Convert to integer
1269 BuildMI(BB, PPC::FCTIWZ, 1, ConvReg).addReg(TmpReg2);
1270 addFrameReference(BuildMI(BB, PPC::STFD, 3).addReg(ConvReg), FrameIdx);
1271 addFrameReference(BuildMI(BB, PPC::LWZ, 2, IntTmp), FrameIdx, 4);
1272 BuildMI(BB, PPC::BLT, 2).addReg(PPC::CR0).addMBB(PhiMBB);
1273 BuildMI(BB, PPC::B, 1).addMBB(XorMBB);
1274
1275 // XorMBB:
1276 // add 2**31 if input was >= 2**31
1277 BB = XorMBB;
1278 BuildMI(BB, PPC::XORIS, 2, XorReg).addReg(IntTmp).addImm(0x8000);
1279 XorMBB->addSuccessor(PhiMBB);
1280
1281 // PhiMBB:
1282 // DestReg = phi [ IntTmp, OldMBB ], [ XorReg, XorMBB ]
1283 BB = PhiMBB;
1284 BuildMI(BB, PPC::PHI, 4, Result).addReg(IntTmp).addMBB(OldMBB)
1285 .addReg(XorReg).addMBB(XorMBB);
1286 return Result;
1287 }
1288 assert(0 && "Should never get here");
1289 return 0;
1290 }
Nate Begemana9795f82005-03-24 04:41:43 +00001291
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001292 case ISD::SETCC:
Nate Begeman33162522005-03-29 21:54:38 +00001293 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Node)) {
Nate Begemandffcfcc2005-04-01 00:32:34 +00001294 Opc = SelectSetCR0(N);
Nate Begeman33162522005-03-29 21:54:38 +00001295
1296 // Create an iterator with which to insert the MBB for copying the false
1297 // value and the MBB to hold the PHI instruction for this SetCC.
1298 MachineBasicBlock *thisMBB = BB;
1299 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1300 ilist<MachineBasicBlock>::iterator It = BB;
1301 ++It;
1302
1303 // thisMBB:
1304 // ...
1305 // cmpTY cr0, r1, r2
1306 // %TrueValue = li 1
1307 // bCC sinkMBB
Nate Begeman33162522005-03-29 21:54:38 +00001308 unsigned TrueValue = MakeReg(MVT::i32);
1309 BuildMI(BB, PPC::LI, 1, TrueValue).addSImm(1);
1310 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
1311 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
1312 BuildMI(BB, Opc, 2).addReg(PPC::CR0).addMBB(sinkMBB);
1313 MachineFunction *F = BB->getParent();
1314 F->getBasicBlockList().insert(It, copy0MBB);
1315 F->getBasicBlockList().insert(It, sinkMBB);
1316 // Update machine-CFG edges
1317 BB->addSuccessor(copy0MBB);
1318 BB->addSuccessor(sinkMBB);
1319
1320 // copy0MBB:
1321 // %FalseValue = li 0
1322 // fallthrough
1323 BB = copy0MBB;
1324 unsigned FalseValue = MakeReg(MVT::i32);
1325 BuildMI(BB, PPC::LI, 1, FalseValue).addSImm(0);
1326 // Update machine-CFG edges
1327 BB->addSuccessor(sinkMBB);
1328
1329 // sinkMBB:
1330 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1331 // ...
1332 BB = sinkMBB;
1333 BuildMI(BB, PPC::PHI, 4, Result).addReg(FalseValue)
1334 .addMBB(copy0MBB).addReg(TrueValue).addMBB(thisMBB);
1335 return Result;
1336 }
1337 assert(0 && "Is this legal?");
1338 return 0;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001339
Nate Begeman74747862005-03-29 22:24:51 +00001340 case ISD::SELECT: {
Nate Begemandffcfcc2005-04-01 00:32:34 +00001341 Opc = SelectSetCR0(N.getOperand(0));
1342
Nate Begeman74747862005-03-29 22:24:51 +00001343 // Create an iterator with which to insert the MBB for copying the false
1344 // value and the MBB to hold the PHI instruction for this SetCC.
1345 MachineBasicBlock *thisMBB = BB;
1346 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1347 ilist<MachineBasicBlock>::iterator It = BB;
1348 ++It;
1349
1350 // thisMBB:
1351 // ...
1352 // TrueVal = ...
1353 // cmpTY cr0, r1, r2
1354 // bCC copy1MBB
1355 // fallthrough --> copy0MBB
Nate Begeman74747862005-03-29 22:24:51 +00001356 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
1357 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
1358 unsigned TrueValue = SelectExpr(N.getOperand(1)); //Use if TRUE
Nate Begeman3e897162005-03-31 23:55:40 +00001359 BuildMI(BB, Opc, 2).addReg(PPC::CR0).addMBB(sinkMBB);
Nate Begeman74747862005-03-29 22:24:51 +00001360 MachineFunction *F = BB->getParent();
1361 F->getBasicBlockList().insert(It, copy0MBB);
1362 F->getBasicBlockList().insert(It, sinkMBB);
1363 // Update machine-CFG edges
1364 BB->addSuccessor(copy0MBB);
1365 BB->addSuccessor(sinkMBB);
1366
1367 // copy0MBB:
1368 // %FalseValue = ...
1369 // # fallthrough to sinkMBB
1370 BB = copy0MBB;
1371 unsigned FalseValue = SelectExpr(N.getOperand(2)); //Use if FALSE
1372 // Update machine-CFG edges
1373 BB->addSuccessor(sinkMBB);
1374
1375 // sinkMBB:
1376 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1377 // ...
1378 BB = sinkMBB;
1379 BuildMI(BB, PPC::PHI, 4, Result).addReg(FalseValue)
1380 .addMBB(copy0MBB).addReg(TrueValue).addMBB(thisMBB);
1381
1382 // FIXME: Select i64?
1383 return Result;
1384 }
Nate Begemana9795f82005-03-24 04:41:43 +00001385
1386 case ISD::Constant:
1387 switch (N.getValueType()) {
1388 default: assert(0 && "Cannot use constants of this type!");
1389 case MVT::i1:
1390 BuildMI(BB, PPC::LI, 1, Result)
1391 .addSImm(!cast<ConstantSDNode>(N)->isNullValue());
1392 break;
1393 case MVT::i32:
1394 {
1395 int v = (int)cast<ConstantSDNode>(N)->getSignExtended();
1396 if (v < 32768 && v >= -32768) {
1397 BuildMI(BB, PPC::LI, 1, Result).addSImm(v);
1398 } else {
Nate Begeman5e966612005-03-24 06:28:42 +00001399 Tmp1 = MakeReg(MVT::i32);
1400 BuildMI(BB, PPC::LIS, 1, Tmp1).addSImm(v >> 16);
1401 BuildMI(BB, PPC::ORI, 2, Result).addReg(Tmp1).addImm(v & 0xFFFF);
Nate Begemana9795f82005-03-24 04:41:43 +00001402 }
1403 }
1404 }
1405 return Result;
1406 }
1407
1408 return 0;
1409}
1410
1411void ISel::Select(SDOperand N) {
1412 unsigned Tmp1, Tmp2, Opc;
1413 unsigned opcode = N.getOpcode();
1414
1415 if (!ExprMap.insert(std::make_pair(N, 1)).second)
1416 return; // Already selected.
1417
1418 SDNode *Node = N.Val;
1419
1420 switch (Node->getOpcode()) {
1421 default:
1422 Node->dump(); std::cerr << "\n";
1423 assert(0 && "Node not handled yet!");
1424 case ISD::EntryToken: return; // Noop
1425 case ISD::TokenFactor:
1426 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1427 Select(Node->getOperand(i));
1428 return;
1429 case ISD::ADJCALLSTACKDOWN:
1430 case ISD::ADJCALLSTACKUP:
1431 Select(N.getOperand(0));
1432 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1433 Opc = N.getOpcode() == ISD::ADJCALLSTACKDOWN ? PPC::ADJCALLSTACKDOWN :
1434 PPC::ADJCALLSTACKUP;
1435 BuildMI(BB, Opc, 1).addImm(Tmp1);
1436 return;
1437 case ISD::BR: {
1438 MachineBasicBlock *Dest =
1439 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
Nate Begemana9795f82005-03-24 04:41:43 +00001440 Select(N.getOperand(0));
1441 BuildMI(BB, PPC::B, 1).addMBB(Dest);
1442 return;
1443 }
1444 case ISD::BRCOND:
1445 SelectBranchCC(N);
1446 return;
1447 case ISD::CopyToReg:
1448 Select(N.getOperand(0));
1449 Tmp1 = SelectExpr(N.getOperand(1));
1450 Tmp2 = cast<RegSDNode>(N)->getReg();
1451
1452 if (Tmp1 != Tmp2) {
1453 if (N.getOperand(1).getValueType() == MVT::f64 ||
1454 N.getOperand(1).getValueType() == MVT::f32)
1455 BuildMI(BB, PPC::FMR, 1, Tmp2).addReg(Tmp1);
1456 else
1457 BuildMI(BB, PPC::OR, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
1458 }
1459 return;
1460 case ISD::ImplicitDef:
1461 Select(N.getOperand(0));
1462 BuildMI(BB, PPC::IMPLICIT_DEF, 0, cast<RegSDNode>(N)->getReg());
1463 return;
1464 case ISD::RET:
1465 switch (N.getNumOperands()) {
1466 default:
1467 assert(0 && "Unknown return instruction!");
1468 case 3:
1469 assert(N.getOperand(1).getValueType() == MVT::i32 &&
1470 N.getOperand(2).getValueType() == MVT::i32 &&
1471 "Unknown two-register value!");
1472 Select(N.getOperand(0));
1473 Tmp1 = SelectExpr(N.getOperand(1));
1474 Tmp2 = SelectExpr(N.getOperand(2));
1475 BuildMI(BB, PPC::OR, 2, PPC::R3).addReg(Tmp1).addReg(Tmp1);
1476 BuildMI(BB, PPC::OR, 2, PPC::R4).addReg(Tmp2).addReg(Tmp2);
1477 break;
1478 case 2:
1479 Select(N.getOperand(0));
1480 Tmp1 = SelectExpr(N.getOperand(1));
1481 switch (N.getOperand(1).getValueType()) {
1482 default:
1483 assert(0 && "Unknown return type!");
1484 case MVT::f64:
1485 case MVT::f32:
1486 BuildMI(BB, PPC::FMR, 1, PPC::F1).addReg(Tmp1);
1487 break;
1488 case MVT::i32:
1489 BuildMI(BB, PPC::OR, 2, PPC::R3).addReg(Tmp1).addReg(Tmp1);
1490 break;
1491 }
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001492 case 1:
1493 Select(N.getOperand(0));
1494 break;
Nate Begemana9795f82005-03-24 04:41:43 +00001495 }
1496 BuildMI(BB, PPC::BLR, 0); // Just emit a 'ret' instruction
1497 return;
Nate Begemana9795f82005-03-24 04:41:43 +00001498 case ISD::TRUNCSTORE:
1499 case ISD::STORE:
1500 {
1501 SDOperand Chain = N.getOperand(0);
1502 SDOperand Value = N.getOperand(1);
1503 SDOperand Address = N.getOperand(2);
1504 Select(Chain);
1505
1506 Tmp1 = SelectExpr(Value); //value
1507
1508 if (opcode == ISD::STORE) {
1509 switch(Value.getValueType()) {
1510 default: assert(0 && "unknown Type in store");
1511 case MVT::i32: Opc = PPC::STW; break;
1512 case MVT::f64: Opc = PPC::STFD; break;
1513 case MVT::f32: Opc = PPC::STFS; break;
1514 }
1515 } else { //ISD::TRUNCSTORE
1516 switch(cast<MVTSDNode>(Node)->getExtraValueType()) {
1517 default: assert(0 && "unknown Type in store");
1518 case MVT::i1: //FIXME: DAG does not promote this load
1519 case MVT::i8: Opc = PPC::STB; break;
1520 case MVT::i16: Opc = PPC::STH; break;
1521 }
1522 }
1523
1524 if (Address.getOpcode() == ISD::GlobalAddress)
1525 {
1526 BuildMI(BB, Opc, 2).addReg(Tmp1)
1527 .addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
1528 }
1529 else if(Address.getOpcode() == ISD::FrameIndex)
1530 {
Nate Begeman58f718c2005-03-30 02:23:08 +00001531 Tmp2 = cast<FrameIndexSDNode>(Address)->getIndex();
1532 addFrameReference(BuildMI(BB, Opc, 3).addReg(Tmp1), (int)Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +00001533 }
1534 else
1535 {
1536 int offset;
1537 SelectAddr(Address, Tmp2, offset);
1538 BuildMI(BB, Opc, 3).addReg(Tmp1).addImm(offset).addReg(Tmp2);
1539 }
1540 return;
1541 }
1542 case ISD::EXTLOAD:
1543 case ISD::SEXTLOAD:
1544 case ISD::ZEXTLOAD:
1545 case ISD::LOAD:
1546 case ISD::CopyFromReg:
1547 case ISD::CALL:
1548 case ISD::DYNAMIC_STACKALLOC:
1549 ExprMap.erase(N);
1550 SelectExpr(N);
1551 return;
1552 }
1553 assert(0 && "Should not be reached!");
1554}
1555
1556
1557/// createPPC32PatternInstructionSelector - This pass converts an LLVM function
1558/// into a machine code representation using pattern matching and a machine
1559/// description file.
1560///
1561FunctionPass *llvm::createPPC32ISelPattern(TargetMachine &TM) {
1562 return new ISel(TM);
Chris Lattner246fa632005-03-24 06:16:18 +00001563}
1564