blob: 3acd5db0c6a209552c04405b472366c18e16a166 [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
Nate Begeman04730362005-04-01 04:45:11 +0000472 bool SelectAddr(SDOperand N, unsigned& Reg, int& offset);
Nate Begemana9795f82005-03-24 04:41:43 +0000473 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 }
Nate Begeman04730362005-04-01 04:45:11 +0000532 return 0;
533}
534
535/// IndexedOpForOp - Return the indexed variant for each of the PowerPC load
536/// and store immediate instructions.
537static unsigned IndexedOpForOp(unsigned Opcode) {
538 switch(Opcode) {
539 default: assert(0 && "Unknown opcode!"); abort();
540 case PPC::LBZ: return PPC::LBZX; case PPC::STB: return PPC::STBX;
541 case PPC::LHZ: return PPC::LHZX; case PPC::STH: return PPC::STHX;
542 case PPC::LHA: return PPC::LHAX; case PPC::STW: return PPC::STWX;
543 case PPC::LWZ: return PPC::LWZX; case PPC::STFS: return PPC::STFSX;
544 case PPC::LFS: return PPC::LFSX; case PPC::STFD: return PPC::STFDX;
545 case PPC::LFD: return PPC::LFDX;
546 }
547 return 0;
Nate Begeman3e897162005-03-31 23:55:40 +0000548}
Nate Begemana9795f82005-03-24 04:41:43 +0000549}
550
Nate Begemanc7b09f12005-03-25 08:34:25 +0000551/// getGlobalBaseReg - Output the instructions required to put the
552/// base address to use for accessing globals into a register.
553///
554unsigned ISel::getGlobalBaseReg() {
555 if (!GlobalBaseInitialized) {
556 // Insert the set of GlobalBaseReg into the first MBB of the function
557 MachineBasicBlock &FirstMBB = BB->getParent()->front();
558 MachineBasicBlock::iterator MBBI = FirstMBB.begin();
559 GlobalBaseReg = MakeReg(MVT::i32);
560 BuildMI(FirstMBB, MBBI, PPC::MovePCtoLR, 0, PPC::LR);
561 BuildMI(FirstMBB, MBBI, PPC::MFLR, 1, GlobalBaseReg).addReg(PPC::LR);
562 GlobalBaseInitialized = true;
563 }
564 return GlobalBaseReg;
565}
566
Nate Begeman6b559972005-04-01 02:59:27 +0000567/// getConstDouble - Loads a floating point value into a register, via the
568/// Constant Pool. Optionally takes a register in which to load the value.
569unsigned ISel::getConstDouble(double doubleVal, unsigned Result=0) {
570 unsigned Tmp1 = MakeReg(MVT::i32);
571 if (0 == Result) Result = MakeReg(MVT::f64);
572 MachineConstantPool *CP = BB->getParent()->getConstantPool();
573 ConstantFP *CFP = ConstantFP::get(Type::DoubleTy, doubleVal);
574 unsigned CPI = CP->getConstantPoolIndex(CFP);
575 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp1).addReg(getGlobalBaseReg())
576 .addConstantPoolIndex(CPI);
577 BuildMI(BB, PPC::LFD, 2, Result).addConstantPoolIndex(CPI).addReg(Tmp1);
578 return Result;
579}
580
Nate Begemandffcfcc2005-04-01 00:32:34 +0000581unsigned ISel::SelectSetCR0(SDOperand CC) {
582 unsigned Opc, Tmp1, Tmp2;
583 static const unsigned CompareOpcodes[] =
584 { PPC::FCMPU, PPC::FCMPU, PPC::CMPW, PPC::CMPLW };
585
586 // If the first operand to the select is a SETCC node, then we can fold it
587 // into the branch that selects which value to return.
588 SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(CC.Val);
589 if (SetCC && CC.getOpcode() == ISD::SETCC) {
590 bool U;
591 Opc = getBCCForSetCC(SetCC->getCondition(), U);
592 Tmp1 = SelectExpr(SetCC->getOperand(0));
593
594 // Pass the optional argument U to canUseAsImmediateForOpcode for SETCC,
595 // so that it knows whether the SETCC immediate range is signed or not.
596 if (1 == canUseAsImmediateForOpcode(SetCC->getOperand(1), ISD::SETCC,
597 Tmp2, U)) {
598 if (U)
599 BuildMI(BB, PPC::CMPLWI, 2, PPC::CR0).addReg(Tmp1).addImm(Tmp2);
600 else
601 BuildMI(BB, PPC::CMPWI, 2, PPC::CR0).addReg(Tmp1).addSImm(Tmp2);
602 } else {
603 bool IsInteger = MVT::isInteger(SetCC->getOperand(0).getValueType());
604 unsigned CompareOpc = CompareOpcodes[2 * IsInteger + U];
605 Tmp2 = SelectExpr(SetCC->getOperand(1));
606 BuildMI(BB, CompareOpc, 2, PPC::CR0).addReg(Tmp1).addReg(Tmp2);
607 }
608 } else {
609 Tmp1 = SelectExpr(CC);
610 BuildMI(BB, PPC::CMPLWI, 2, PPC::CR0).addReg(Tmp1).addImm(0);
611 Opc = PPC::BNE;
612 }
613 return Opc;
614}
615
616/// Check to see if the load is a constant offset from a base register
Nate Begeman04730362005-04-01 04:45:11 +0000617bool ISel::SelectAddr(SDOperand N, unsigned& Reg, int& offset)
Nate Begemana9795f82005-03-24 04:41:43 +0000618{
Nate Begeman96fc6812005-03-31 02:05:53 +0000619 unsigned imm = 0, opcode = N.getOpcode();
Nate Begeman04730362005-04-01 04:45:11 +0000620 if (N.getOpcode() == ISD::ADD) {
621 Reg = SelectExpr(N.getOperand(0));
Nate Begeman96fc6812005-03-31 02:05:53 +0000622 if (1 == canUseAsImmediateForOpcode(N.getOperand(1), opcode, imm)) {
Nate Begeman96fc6812005-03-31 02:05:53 +0000623 offset = imm;
Nate Begeman04730362005-04-01 04:45:11 +0000624 return false;
625 }
626 offset = SelectExpr(N.getOperand(1));
627 return true;
628 }
Nate Begemana9795f82005-03-24 04:41:43 +0000629 Reg = SelectExpr(N);
630 offset = 0;
Nate Begeman04730362005-04-01 04:45:11 +0000631 return false;
Nate Begemana9795f82005-03-24 04:41:43 +0000632}
633
634void ISel::SelectBranchCC(SDOperand N)
635{
636 assert(N.getOpcode() == ISD::BRCOND && "Not a BranchCC???");
637 MachineBasicBlock *Dest =
638 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
Nate Begeman3e897162005-03-31 23:55:40 +0000639
Nate Begemana9795f82005-03-24 04:41:43 +0000640 Select(N.getOperand(0)); //chain
Nate Begemandffcfcc2005-04-01 00:32:34 +0000641 unsigned Opc = SelectSetCR0(N.getOperand(1));
Nate Begeman3e897162005-03-31 23:55:40 +0000642 BuildMI(BB, Opc, 2).addReg(PPC::CR0).addMBB(Dest);
Nate Begemana9795f82005-03-24 04:41:43 +0000643 return;
644}
645
646unsigned ISel::SelectExprFP(SDOperand N, unsigned Result)
647{
648 unsigned Tmp1, Tmp2, Tmp3;
649 unsigned Opc = 0;
650 SDNode *Node = N.Val;
651 MVT::ValueType DestType = N.getValueType();
652 unsigned opcode = N.getOpcode();
653
654 switch (opcode) {
655 default:
656 Node->dump();
657 assert(0 && "Node not handled!\n");
658
Nate Begeman23afcfb2005-03-29 22:48:55 +0000659 case ISD::SELECT: {
Nate Begeman3e897162005-03-31 23:55:40 +0000660 // Attempt to generate FSEL. We can do this whenever we have an FP result,
661 // and an FP comparison in the SetCC node.
662 SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(N.getOperand(0).Val);
663 if (SetCC && N.getOperand(0).getOpcode() == ISD::SETCC &&
664 !MVT::isInteger(SetCC->getOperand(0).getValueType()) &&
665 SetCC->getCondition() != ISD::SETEQ &&
666 SetCC->getCondition() != ISD::SETNE) {
667 MVT::ValueType VT = SetCC->getOperand(0).getValueType();
668 Tmp1 = SelectExpr(SetCC->getOperand(0)); // Val to compare against
669 unsigned TV = SelectExpr(N.getOperand(1)); // Use if TRUE
670 unsigned FV = SelectExpr(N.getOperand(2)); // Use if FALSE
671
672 ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(1));
673 if (CN && (CN->isExactlyValue(-0.0) || CN->isExactlyValue(0.0))) {
674 switch(SetCC->getCondition()) {
675 default: assert(0 && "Invalid FSEL condition"); abort();
676 case ISD::SETULT:
677 case ISD::SETLT:
678 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp1).addReg(FV).addReg(TV);
679 return Result;
680 case ISD::SETUGE:
681 case ISD::SETGE:
682 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp1).addReg(TV).addReg(FV);
683 return Result;
684 case ISD::SETUGT:
685 case ISD::SETGT: {
686 Tmp2 = MakeReg(VT);
687 BuildMI(BB, PPC::FNEG, 1, Tmp2).addReg(Tmp1);
688 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp2).addReg(FV).addReg(TV);
689 return Result;
690 }
691 case ISD::SETULE:
692 case ISD::SETLE: {
693 Tmp2 = MakeReg(VT);
694 BuildMI(BB, PPC::FNEG, 1, Tmp2).addReg(Tmp1);
695 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp2).addReg(TV).addReg(FV);
696 return Result;
697 }
698 }
699 } else {
700 Opc = (MVT::f64 == VT) ? PPC::FSUB : PPC::FSUBS;
701 Tmp2 = SelectExpr(SetCC->getOperand(1));
702 Tmp3 = MakeReg(VT);
703 switch(SetCC->getCondition()) {
704 default: assert(0 && "Invalid FSEL condition"); abort();
705 case ISD::SETULT:
706 case ISD::SETLT:
707 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
708 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(FV).addReg(TV);
709 return Result;
710 case ISD::SETUGE:
711 case ISD::SETGE:
712 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
713 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(TV).addReg(FV);
714 return Result;
715 case ISD::SETUGT:
716 case ISD::SETGT:
717 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp2).addReg(Tmp1);
718 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(FV).addReg(TV);
719 return Result;
720 case ISD::SETULE:
721 case ISD::SETLE:
722 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp2).addReg(Tmp1);
723 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(TV).addReg(FV);
724 return Result;
725 }
726 }
727 assert(0 && "Should never get here");
728 return 0;
729 }
730
Nate Begeman23afcfb2005-03-29 22:48:55 +0000731 // Create an iterator with which to insert the MBB for copying the false
732 // value and the MBB to hold the PHI instruction for this SetCC.
733 MachineBasicBlock *thisMBB = BB;
734 const BasicBlock *LLVM_BB = BB->getBasicBlock();
735 ilist<MachineBasicBlock>::iterator It = BB;
736 ++It;
737
738 // thisMBB:
739 // ...
740 // TrueVal = ...
741 // cmpTY cr0, r1, r2
742 // bCC copy1MBB
743 // fallthrough --> copy0MBB
Nate Begeman3e897162005-03-31 23:55:40 +0000744 Tmp1 = SelectExpr(N.getOperand(0)); //Cond
Nate Begeman23afcfb2005-03-29 22:48:55 +0000745 BuildMI(BB, PPC::CMPLWI, 2, PPC::CR0).addReg(Tmp1).addImm(0);
746 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
747 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
748 unsigned TrueValue = SelectExpr(N.getOperand(1)); //Use if TRUE
749 BuildMI(BB, PPC::BNE, 2).addReg(PPC::CR0).addMBB(sinkMBB);
750 MachineFunction *F = BB->getParent();
751 F->getBasicBlockList().insert(It, copy0MBB);
752 F->getBasicBlockList().insert(It, sinkMBB);
753 // Update machine-CFG edges
754 BB->addSuccessor(copy0MBB);
755 BB->addSuccessor(sinkMBB);
756
757 // copy0MBB:
758 // %FalseValue = ...
759 // # fallthrough to sinkMBB
760 BB = copy0MBB;
761 unsigned FalseValue = SelectExpr(N.getOperand(2)); //Use if FALSE
762 // Update machine-CFG edges
763 BB->addSuccessor(sinkMBB);
764
765 // sinkMBB:
766 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
767 // ...
768 BB = sinkMBB;
769 BuildMI(BB, PPC::PHI, 4, Result).addReg(FalseValue)
770 .addMBB(copy0MBB).addReg(TrueValue).addMBB(thisMBB);
771 return Result;
772 }
Nate Begemana9795f82005-03-24 04:41:43 +0000773
774 case ISD::FP_ROUND:
775 assert (DestType == MVT::f32 &&
776 N.getOperand(0).getValueType() == MVT::f64 &&
777 "only f64 to f32 conversion supported here");
778 Tmp1 = SelectExpr(N.getOperand(0));
779 BuildMI(BB, PPC::FRSP, 1, Result).addReg(Tmp1);
780 return Result;
781
782 case ISD::FP_EXTEND:
783 assert (DestType == MVT::f64 &&
784 N.getOperand(0).getValueType() == MVT::f32 &&
785 "only f32 to f64 conversion supported here");
786 Tmp1 = SelectExpr(N.getOperand(0));
787 BuildMI(BB, PPC::FMR, 1, Result).addReg(Tmp1);
788 return Result;
789
790 case ISD::CopyFromReg:
Nate Begemanf2622612005-03-26 02:17:46 +0000791 if (Result == 1)
792 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
793 Tmp1 = dyn_cast<RegSDNode>(Node)->getReg();
794 BuildMI(BB, PPC::FMR, 1, Result).addReg(Tmp1);
795 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +0000796
Nate Begeman6d369cc2005-04-01 01:08:07 +0000797 case ISD::ConstantFP: {
Nate Begeman6d369cc2005-04-01 01:08:07 +0000798 ConstantFPSDNode *CN = cast<ConstantFPSDNode>(N);
Nate Begeman6b559972005-04-01 02:59:27 +0000799 Result = getConstDouble(CN->getValue(), Result);
Nate Begeman6d369cc2005-04-01 01:08:07 +0000800 return Result;
801 }
Nate Begemana9795f82005-03-24 04:41:43 +0000802
803 case ISD::MUL:
804 case ISD::ADD:
805 case ISD::SUB:
806 case ISD::SDIV:
807 switch( opcode ) {
808 case ISD::MUL: Opc = DestType == MVT::f64 ? PPC::FMUL : PPC::FMULS; break;
809 case ISD::ADD: Opc = DestType == MVT::f64 ? PPC::FADD : PPC::FADDS; break;
810 case ISD::SUB: Opc = DestType == MVT::f64 ? PPC::FSUB : PPC::FSUBS; break;
811 case ISD::SDIV: Opc = DestType == MVT::f64 ? PPC::FDIV : PPC::FDIVS; break;
812 };
Nate Begemana9795f82005-03-24 04:41:43 +0000813 Tmp1 = SelectExpr(N.getOperand(0));
814 Tmp2 = SelectExpr(N.getOperand(1));
815 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
816 return Result;
817
Nate Begemana9795f82005-03-24 04:41:43 +0000818 case ISD::UINT_TO_FP:
Nate Begemanfdcf3412005-03-30 19:38:35 +0000819 case ISD::SINT_TO_FP: {
820 assert (N.getOperand(0).getValueType() == MVT::i32
821 && "int to float must operate on i32");
822 bool IsUnsigned = (ISD::UINT_TO_FP == opcode);
823 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
824 Tmp2 = MakeReg(MVT::f64); // temp reg to load the integer value into
825 Tmp3 = MakeReg(MVT::i32); // temp reg to hold the conversion constant
826 unsigned ConstF = MakeReg(MVT::f64); // temp reg to hold the fp constant
827
828 int FrameIdx = BB->getParent()->getFrameInfo()->CreateStackObject(8, 8);
829 MachineConstantPool *CP = BB->getParent()->getConstantPool();
830
831 // FIXME: pull this FP constant generation stuff out into something like
832 // the simple ISel's getReg.
833 if (IsUnsigned) {
834 ConstantFP *CFP = ConstantFP::get(Type::DoubleTy, 0x1.000000p52);
835 unsigned CPI = CP->getConstantPoolIndex(CFP);
836 // Load constant fp value
837 unsigned Tmp4 = MakeReg(MVT::i32);
838 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp4).addReg(getGlobalBaseReg())
839 .addConstantPoolIndex(CPI);
840 BuildMI(BB, PPC::LFD, 2, ConstF).addConstantPoolIndex(CPI).addReg(Tmp4);
841 // Store the hi & low halves of the fp value, currently in int regs
842 BuildMI(BB, PPC::LIS, 1, Tmp3).addSImm(0x4330);
843 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(Tmp3), FrameIdx);
844 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(Tmp1), FrameIdx, 4);
845 addFrameReference(BuildMI(BB, PPC::LFD, 2, Tmp2), FrameIdx);
846 // Generate the return value with a subtract
847 BuildMI(BB, PPC::FSUB, 2, Result).addReg(Tmp2).addReg(ConstF);
848 } else {
849 ConstantFP *CFP = ConstantFP::get(Type::DoubleTy, 0x1.000008p52);
850 unsigned CPI = CP->getConstantPoolIndex(CFP);
851 // Load constant fp value
852 unsigned Tmp4 = MakeReg(MVT::i32);
853 unsigned TmpL = MakeReg(MVT::i32);
854 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp4).addReg(getGlobalBaseReg())
855 .addConstantPoolIndex(CPI);
856 BuildMI(BB, PPC::LFD, 2, ConstF).addConstantPoolIndex(CPI).addReg(Tmp4);
857 // Store the hi & low halves of the fp value, currently in int regs
858 BuildMI(BB, PPC::LIS, 1, Tmp3).addSImm(0x4330);
859 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(Tmp3), FrameIdx);
860 BuildMI(BB, PPC::XORIS, 2, TmpL).addReg(Tmp1).addImm(0x8000);
861 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(TmpL), FrameIdx, 4);
862 addFrameReference(BuildMI(BB, PPC::LFD, 2, Tmp2), FrameIdx);
863 // Generate the return value with a subtract
864 BuildMI(BB, PPC::FSUB, 2, Result).addReg(Tmp2).addReg(ConstF);
865 }
866 return Result;
867 }
Nate Begemana9795f82005-03-24 04:41:43 +0000868 }
Nate Begeman6b559972005-04-01 02:59:27 +0000869 assert(0 && "Should never get here");
Nate Begemana9795f82005-03-24 04:41:43 +0000870 return 0;
871}
872
873unsigned ISel::SelectExpr(SDOperand N) {
874 unsigned Result;
875 unsigned Tmp1, Tmp2, Tmp3;
876 unsigned Opc = 0;
877 unsigned opcode = N.getOpcode();
878
879 SDNode *Node = N.Val;
880 MVT::ValueType DestType = N.getValueType();
881
882 unsigned &Reg = ExprMap[N];
883 if (Reg) return Reg;
884
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000885 if (N.getOpcode() != ISD::CALL && N.getOpcode() != ISD::ADD_PARTS &&
886 N.getOpcode() != ISD::SUB_PARTS)
Nate Begemana9795f82005-03-24 04:41:43 +0000887 Reg = Result = (N.getValueType() != MVT::Other) ?
888 MakeReg(N.getValueType()) : 1;
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000889 else {
890 // If this is a call instruction, make sure to prepare ALL of the result
891 // values as well as the chain.
892 if (N.getOpcode() == ISD::CALL) {
893 if (Node->getNumValues() == 1)
894 Reg = Result = 1; // Void call, just a chain.
895 else {
896 Result = MakeReg(Node->getValueType(0));
897 ExprMap[N.getValue(0)] = Result;
898 for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
899 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
900 ExprMap[SDOperand(Node, Node->getNumValues()-1)] = 1;
901 }
902 } else {
903 Result = MakeReg(Node->getValueType(0));
904 ExprMap[N.getValue(0)] = Result;
905 for (unsigned i = 1, e = N.Val->getNumValues(); i != e; ++i)
906 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
907 }
908 }
909
910 if (DestType == MVT::f64 || DestType == MVT::f32)
Nate Begeman74d73452005-03-31 00:15:26 +0000911 if (ISD::LOAD != opcode && ISD::EXTLOAD != opcode)
912 return SelectExprFP(N, Result);
Nate Begemana9795f82005-03-24 04:41:43 +0000913
914 switch (opcode) {
915 default:
916 Node->dump();
917 assert(0 && "Node not handled!\n");
918
919 case ISD::DYNAMIC_STACKALLOC:
Nate Begeman5e966612005-03-24 06:28:42 +0000920 // Generate both result values. FIXME: Need a better commment here?
921 if (Result != 1)
922 ExprMap[N.getValue(1)] = 1;
923 else
924 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
925
926 // FIXME: We are currently ignoring the requested alignment for handling
927 // greater than the stack alignment. This will need to be revisited at some
928 // point. Align = N.getOperand(2);
929 if (!isa<ConstantSDNode>(N.getOperand(2)) ||
930 cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) {
931 std::cerr << "Cannot allocate stack object with greater alignment than"
932 << " the stack alignment yet!";
933 abort();
934 }
935 Select(N.getOperand(0));
936 Tmp1 = SelectExpr(N.getOperand(1));
937 // Subtract size from stack pointer, thereby allocating some space.
938 BuildMI(BB, PPC::SUBF, 2, PPC::R1).addReg(Tmp1).addReg(PPC::R1);
939 // Put a pointer to the space into the result register by copying the SP
940 BuildMI(BB, PPC::OR, 2, Result).addReg(PPC::R1).addReg(PPC::R1);
941 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +0000942
943 case ISD::ConstantPool:
Nate Begemanca12a2b2005-03-28 22:28:37 +0000944 Tmp1 = cast<ConstantPoolSDNode>(N)->getIndex();
945 Tmp2 = MakeReg(MVT::i32);
946 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp2).addReg(getGlobalBaseReg())
947 .addConstantPoolIndex(Tmp1);
948 BuildMI(BB, PPC::LA, 2, Result).addReg(Tmp2).addConstantPoolIndex(Tmp1);
949 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +0000950
951 case ISD::FrameIndex:
Nate Begemanf3d08f32005-03-29 00:03:27 +0000952 Tmp1 = cast<FrameIndexSDNode>(N)->getIndex();
Nate Begeman58f718c2005-03-30 02:23:08 +0000953 addFrameReference(BuildMI(BB, PPC::ADDI, 2, Result), (int)Tmp1, 0, false);
Nate Begemanf3d08f32005-03-29 00:03:27 +0000954 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +0000955
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000956 case ISD::GlobalAddress: {
957 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
Nate Begemanca12a2b2005-03-28 22:28:37 +0000958 Tmp1 = MakeReg(MVT::i32);
Nate Begemanc7b09f12005-03-25 08:34:25 +0000959 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp1).addReg(getGlobalBaseReg())
960 .addGlobalAddress(GV);
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000961 if (GV->hasWeakLinkage() || GV->isExternal()) {
962 BuildMI(BB, PPC::LWZ, 2, Result).addGlobalAddress(GV).addReg(Tmp1);
963 } else {
964 BuildMI(BB, PPC::LA, 2, Result).addReg(Tmp1).addGlobalAddress(GV);
965 }
966 return Result;
967 }
968
Nate Begeman5e966612005-03-24 06:28:42 +0000969 case ISD::LOAD:
Nate Begemana9795f82005-03-24 04:41:43 +0000970 case ISD::EXTLOAD:
971 case ISD::ZEXTLOAD:
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000972 case ISD::SEXTLOAD: {
Nate Begeman9db505c2005-03-28 19:36:43 +0000973 MVT::ValueType TypeBeingLoaded = (ISD::LOAD == opcode) ?
974 Node->getValueType(0) : cast<MVTSDNode>(Node)->getExtraValueType();
Nate Begeman74d73452005-03-31 00:15:26 +0000975 bool sext = (ISD::SEXTLOAD == opcode);
976 bool byte = (MVT::i8 == TypeBeingLoaded);
977
Nate Begeman5e966612005-03-24 06:28:42 +0000978 // Make sure we generate both values.
979 if (Result != 1)
980 ExprMap[N.getValue(1)] = 1; // Generate the token
981 else
982 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
983
984 SDOperand Chain = N.getOperand(0);
985 SDOperand Address = N.getOperand(1);
986 Select(Chain);
987
Nate Begeman9db505c2005-03-28 19:36:43 +0000988 switch (TypeBeingLoaded) {
Nate Begeman74d73452005-03-31 00:15:26 +0000989 default: Node->dump(); assert(0 && "Cannot load this type!");
Nate Begeman9db505c2005-03-28 19:36:43 +0000990 case MVT::i1: Opc = PPC::LBZ; break;
991 case MVT::i8: Opc = PPC::LBZ; break;
992 case MVT::i16: Opc = sext ? PPC::LHA : PPC::LHZ; break;
993 case MVT::i32: Opc = PPC::LWZ; break;
Nate Begeman74d73452005-03-31 00:15:26 +0000994 case MVT::f32: Opc = PPC::LFS; break;
995 case MVT::f64: Opc = PPC::LFD; break;
Nate Begeman5e966612005-03-24 06:28:42 +0000996 }
997
Nate Begeman74d73452005-03-31 00:15:26 +0000998 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Address)) {
999 Tmp1 = MakeReg(MVT::i32);
1000 int CPI = CP->getIndex();
1001 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp1).addReg(getGlobalBaseReg())
1002 .addConstantPoolIndex(CPI);
1003 BuildMI(BB, Opc, 2, Result).addConstantPoolIndex(CPI).addReg(Tmp1);
Nate Begeman9db505c2005-03-28 19:36:43 +00001004 }
Nate Begeman74d73452005-03-31 00:15:26 +00001005 else if(Address.getOpcode() == ISD::FrameIndex) {
Nate Begeman58f718c2005-03-30 02:23:08 +00001006 Tmp1 = cast<FrameIndexSDNode>(Address)->getIndex();
1007 addFrameReference(BuildMI(BB, Opc, 2, Result), (int)Tmp1);
Nate Begeman5e966612005-03-24 06:28:42 +00001008 } else {
1009 int offset;
Nate Begeman04730362005-04-01 04:45:11 +00001010 bool idx = SelectAddr(Address, Tmp1, offset);
1011 if (idx) {
1012 Opc = IndexedOpForOp(Opc);
1013 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(offset);
1014 } else {
1015 BuildMI(BB, Opc, 2, Result).addSImm(offset).addReg(Tmp1);
1016 }
Nate Begeman5e966612005-03-24 06:28:42 +00001017 }
1018 return Result;
1019 }
1020
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001021 case ISD::CALL: {
1022 // Lower the chain for this call.
1023 Select(N.getOperand(0));
1024 ExprMap[N.getValue(Node->getNumValues()-1)] = 1;
Nate Begeman74d73452005-03-31 00:15:26 +00001025
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001026 for(int i = 2, e = Node->getNumOperands(); i < e; ++i)
Nate Begeman74d73452005-03-31 00:15:26 +00001027 Select(N.getOperand(i));
1028
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001029 // Emit the correct call instruction based on the type of symbol called.
1030 if (GlobalAddressSDNode *GASD =
1031 dyn_cast<GlobalAddressSDNode>(N.getOperand(1))) {
1032 BuildMI(BB, PPC::CALLpcrel, 1).addGlobalAddress(GASD->getGlobal(), true);
1033 } else if (ExternalSymbolSDNode *ESSDN =
1034 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1))) {
1035 BuildMI(BB, PPC::CALLpcrel, 1).addExternalSymbol(ESSDN->getSymbol(), true);
1036 } else {
1037 Tmp1 = SelectExpr(N.getOperand(1));
1038 BuildMI(BB, PPC::OR, 2, PPC::R12).addReg(Tmp1).addReg(Tmp1);
1039 BuildMI(BB, PPC::MTCTR, 1).addReg(PPC::R12);
1040 BuildMI(BB, PPC::CALLindirect, 3).addImm(20).addImm(0).addReg(PPC::R12);
1041 }
1042
1043 switch (Node->getValueType(0)) {
1044 default: assert(0 && "Unknown value type for call result!");
1045 case MVT::Other: return 1;
1046 case MVT::i1:
1047 case MVT::i8:
1048 case MVT::i16:
1049 case MVT::i32:
Nate Begemanc7b09f12005-03-25 08:34:25 +00001050 BuildMI(BB, PPC::OR, 2, Result).addReg(PPC::R3).addReg(PPC::R3);
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001051 if (Node->getValueType(1) == MVT::i32)
Nate Begemanc7b09f12005-03-25 08:34:25 +00001052 BuildMI(BB, PPC::OR, 2, Result+1).addReg(PPC::R4).addReg(PPC::R4);
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001053 break;
1054 case MVT::f32:
1055 case MVT::f64:
1056 BuildMI(BB, PPC::FMR, 1, Result).addReg(PPC::F1);
1057 break;
1058 }
1059 return Result+N.ResNo;
1060 }
Nate Begemana9795f82005-03-24 04:41:43 +00001061
1062 case ISD::SIGN_EXTEND:
1063 case ISD::SIGN_EXTEND_INREG:
1064 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman9db505c2005-03-28 19:36:43 +00001065 switch(cast<MVTSDNode>(Node)->getExtraValueType()) {
1066 default: Node->dump(); assert(0 && "Unhandled SIGN_EXTEND type"); break;
1067 case MVT::i16:
1068 BuildMI(BB, PPC::EXTSH, 1, Result).addReg(Tmp1);
1069 break;
1070 case MVT::i8:
1071 BuildMI(BB, PPC::EXTSB, 1, Result).addReg(Tmp1);
1072 break;
Nate Begeman74747862005-03-29 22:24:51 +00001073 case MVT::i1:
1074 BuildMI(BB, PPC::SUBFIC, 2, Result).addReg(Tmp1).addSImm(0);
1075 break;
Nate Begeman9db505c2005-03-28 19:36:43 +00001076 }
Nate Begemana9795f82005-03-24 04:41:43 +00001077 return Result;
1078
1079 case ISD::ZERO_EXTEND_INREG:
1080 Tmp1 = SelectExpr(N.getOperand(0));
1081 switch(cast<MVTSDNode>(Node)->getExtraValueType()) {
Nate Begeman9db505c2005-03-28 19:36:43 +00001082 default: Node->dump(); assert(0 && "Unhandled ZERO_EXTEND type"); break;
Nate Begemana9795f82005-03-24 04:41:43 +00001083 case MVT::i16: Tmp2 = 16; break;
1084 case MVT::i8: Tmp2 = 24; break;
1085 case MVT::i1: Tmp2 = 31; break;
1086 }
Nate Begeman33162522005-03-29 21:54:38 +00001087 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(0).addImm(Tmp2)
1088 .addImm(31);
Nate Begemana9795f82005-03-24 04:41:43 +00001089 return Result;
1090
Nate Begemana9795f82005-03-24 04:41:43 +00001091 case ISD::CopyFromReg:
1092 if (Result == 1)
1093 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1094 Tmp1 = dyn_cast<RegSDNode>(Node)->getReg();
1095 BuildMI(BB, PPC::OR, 2, Result).addReg(Tmp1).addReg(Tmp1);
1096 return Result;
1097
1098 case ISD::SHL:
Nate Begeman5e966612005-03-24 06:28:42 +00001099 Tmp1 = SelectExpr(N.getOperand(0));
1100 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1101 Tmp2 = CN->getValue() & 0x1F;
Nate Begeman33162522005-03-29 21:54:38 +00001102 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(Tmp2).addImm(0)
Nate Begeman5e966612005-03-24 06:28:42 +00001103 .addImm(31-Tmp2);
1104 } else {
1105 Tmp2 = SelectExpr(N.getOperand(1));
1106 BuildMI(BB, PPC::SLW, 2, Result).addReg(Tmp1).addReg(Tmp2);
1107 }
1108 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001109
Nate Begeman5e966612005-03-24 06:28:42 +00001110 case ISD::SRL:
1111 Tmp1 = SelectExpr(N.getOperand(0));
1112 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1113 Tmp2 = CN->getValue() & 0x1F;
Nate Begeman33162522005-03-29 21:54:38 +00001114 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(32-Tmp2)
Nate Begeman5e966612005-03-24 06:28:42 +00001115 .addImm(Tmp2).addImm(31);
1116 } else {
1117 Tmp2 = SelectExpr(N.getOperand(1));
1118 BuildMI(BB, PPC::SRW, 2, Result).addReg(Tmp1).addReg(Tmp2);
1119 }
1120 return Result;
1121
1122 case ISD::SRA:
1123 Tmp1 = SelectExpr(N.getOperand(0));
1124 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1125 Tmp2 = CN->getValue() & 0x1F;
1126 BuildMI(BB, PPC::SRAWI, 2, Result).addReg(Tmp1).addImm(Tmp2);
1127 } else {
1128 Tmp2 = SelectExpr(N.getOperand(1));
1129 BuildMI(BB, PPC::SRAW, 2, Result).addReg(Tmp1).addReg(Tmp2);
1130 }
1131 return Result;
1132
Nate Begemana9795f82005-03-24 04:41:43 +00001133 case ISD::ADD:
1134 assert (DestType == MVT::i32 && "Only do arithmetic on i32s!");
1135 Tmp1 = SelectExpr(N.getOperand(0));
1136 switch(canUseAsImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
1137 default: assert(0 && "unhandled result code");
1138 case 0: // No immediate
1139 Tmp2 = SelectExpr(N.getOperand(1));
1140 BuildMI(BB, PPC::ADD, 2, Result).addReg(Tmp1).addReg(Tmp2);
1141 break;
1142 case 1: // Low immediate
1143 BuildMI(BB, PPC::ADDI, 2, Result).addReg(Tmp1).addSImm(Tmp2);
1144 break;
1145 case 2: // Shifted immediate
1146 BuildMI(BB, PPC::ADDIS, 2, Result).addReg(Tmp1).addSImm(Tmp2);
1147 break;
1148 }
1149 return Result;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001150
Nate Begemana9795f82005-03-24 04:41:43 +00001151 case ISD::AND:
1152 case ISD::OR:
1153 case ISD::XOR:
1154 assert (DestType == MVT::i32 && "Only do arithmetic on i32s!");
1155 Tmp1 = SelectExpr(N.getOperand(0));
1156 switch(canUseAsImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
1157 default: assert(0 && "unhandled result code");
1158 case 0: // No immediate
1159 Tmp2 = SelectExpr(N.getOperand(1));
1160 switch (opcode) {
Nate Begeman5e966612005-03-24 06:28:42 +00001161 case ISD::AND: Opc = PPC::AND; break;
1162 case ISD::OR: Opc = PPC::OR; break;
1163 case ISD::XOR: Opc = PPC::XOR; break;
Nate Begemana9795f82005-03-24 04:41:43 +00001164 }
Nate Begeman5e966612005-03-24 06:28:42 +00001165 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +00001166 break;
1167 case 1: // Low immediate
1168 switch (opcode) {
Nate Begeman5e966612005-03-24 06:28:42 +00001169 case ISD::AND: Opc = PPC::ANDIo; break;
1170 case ISD::OR: Opc = PPC::ORI; break;
1171 case ISD::XOR: Opc = PPC::XORI; break;
Nate Begemana9795f82005-03-24 04:41:43 +00001172 }
Nate Begeman5e966612005-03-24 06:28:42 +00001173 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +00001174 break;
1175 case 2: // Shifted immediate
1176 switch (opcode) {
Nate Begeman5e966612005-03-24 06:28:42 +00001177 case ISD::AND: Opc = PPC::ANDISo; break;
1178 case ISD::OR: Opc = PPC::ORIS; break;
1179 case ISD::XOR: Opc = PPC::XORIS; break;
Nate Begemana9795f82005-03-24 04:41:43 +00001180 }
Nate Begeman5e966612005-03-24 06:28:42 +00001181 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +00001182 break;
1183 }
1184 return Result;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001185
1186 case ISD::SUB:
1187 assert (DestType == MVT::i32 && "Only do arithmetic on i32s!");
1188 Tmp1 = SelectExpr(N.getOperand(0));
1189 Tmp2 = SelectExpr(N.getOperand(1));
1190 BuildMI(BB, PPC::SUBF, 2, Result).addReg(Tmp2).addReg(Tmp1);
1191 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001192
Nate Begeman5e966612005-03-24 06:28:42 +00001193 case ISD::MUL:
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001194 assert (DestType == MVT::i32 && "Only do arithmetic on i32s!");
1195 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman307e7442005-03-26 01:28:53 +00001196 if (1 == canUseAsImmediateForOpcode(N.getOperand(1), opcode, Tmp2))
1197 BuildMI(BB, PPC::MULLI, 2, Result).addReg(Tmp1).addSImm(Tmp2);
1198 else {
1199 Tmp2 = SelectExpr(N.getOperand(1));
1200 BuildMI(BB, PPC::MULLW, 2, Result).addReg(Tmp1).addReg(Tmp2);
1201 }
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001202 return Result;
1203
Nate Begemanf3d08f32005-03-29 00:03:27 +00001204 case ISD::SDIV:
1205 case ISD::UDIV:
1206 assert (DestType == MVT::i32 && "Only do arithmetic on i32s!");
1207 Tmp1 = SelectExpr(N.getOperand(0));
1208 Tmp2 = SelectExpr(N.getOperand(1));
1209 Opc = (ISD::UDIV == opcode) ? PPC::DIVWU : PPC::DIVW;
1210 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1211 return Result;
1212
1213 case ISD::UREM:
1214 case ISD::SREM: {
1215 assert (DestType == MVT::i32 && "Only do arithmetic on i32s!");
1216 Tmp1 = SelectExpr(N.getOperand(0));
1217 Tmp2 = SelectExpr(N.getOperand(1));
1218 Tmp3 = MakeReg(MVT::i32);
1219 unsigned Tmp4 = MakeReg(MVT::i32);
1220 Opc = (ISD::UREM == opcode) ? PPC::DIVWU : PPC::DIVW;
1221 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
1222 BuildMI(BB, PPC::MULLW, 2, Tmp4).addReg(Tmp3).addReg(Tmp2);
1223 BuildMI(BB, PPC::SUBF, 2, Result).addReg(Tmp4).addReg(Tmp1);
1224 return Result;
1225 }
1226
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001227 case ISD::ADD_PARTS:
Nate Begemanca12a2b2005-03-28 22:28:37 +00001228 case ISD::SUB_PARTS: {
1229 assert(N.getNumOperands() == 4 && N.getValueType() == MVT::i32 &&
1230 "Not an i64 add/sub!");
1231 // Emit all of the operands.
1232 std::vector<unsigned> InVals;
1233 for (unsigned i = 0, e = N.getNumOperands(); i != e; ++i)
1234 InVals.push_back(SelectExpr(N.getOperand(i)));
1235 if (N.getOpcode() == ISD::ADD_PARTS) {
Nate Begemanf70b5762005-03-28 23:08:54 +00001236 BuildMI(BB, PPC::ADDC, 2, Result+1).addReg(InVals[0]).addReg(InVals[2]);
1237 BuildMI(BB, PPC::ADDE, 2, Result).addReg(InVals[1]).addReg(InVals[3]);
Nate Begemanca12a2b2005-03-28 22:28:37 +00001238 } else {
Nate Begemanf70b5762005-03-28 23:08:54 +00001239 BuildMI(BB, PPC::SUBFC, 2, Result+1).addReg(InVals[2]).addReg(InVals[0]);
1240 BuildMI(BB, PPC::SUBFE, 2, Result).addReg(InVals[3]).addReg(InVals[1]);
Nate Begemanca12a2b2005-03-28 22:28:37 +00001241 }
1242 return Result+N.ResNo;
1243 }
1244
Nate Begemana9795f82005-03-24 04:41:43 +00001245 case ISD::FP_TO_UINT:
Nate Begeman6b559972005-04-01 02:59:27 +00001246 case ISD::FP_TO_SINT: {
1247 bool U = (ISD::FP_TO_UINT == opcode);
1248 Tmp1 = SelectExpr(N.getOperand(0));
1249 if (!U) {
1250 Tmp2 = MakeReg(MVT::f64);
1251 BuildMI(BB, PPC::FCTIWZ, 1, Tmp2).addReg(Tmp1);
1252 int FrameIdx = BB->getParent()->getFrameInfo()->CreateStackObject(8, 8);
1253 addFrameReference(BuildMI(BB, PPC::STFD, 3).addReg(Tmp2), FrameIdx);
1254 addFrameReference(BuildMI(BB, PPC::LWZ, 2, Result), FrameIdx, 4);
1255 return Result;
1256 } else {
1257 unsigned Zero = getConstDouble(0.0);
1258 unsigned MaxInt = getConstDouble((1LL << 32) - 1);
1259 unsigned Border = getConstDouble(1LL << 31);
1260 unsigned UseZero = MakeReg(MVT::f64);
1261 unsigned UseMaxInt = MakeReg(MVT::f64);
1262 unsigned UseChoice = MakeReg(MVT::f64);
1263 unsigned TmpReg = MakeReg(MVT::f64);
1264 unsigned TmpReg2 = MakeReg(MVT::f64);
1265 unsigned ConvReg = MakeReg(MVT::f64);
1266 unsigned IntTmp = MakeReg(MVT::i32);
1267 unsigned XorReg = MakeReg(MVT::i32);
1268 MachineFunction *F = BB->getParent();
1269 int FrameIdx = F->getFrameInfo()->CreateStackObject(8, 8);
1270 // Update machine-CFG edges
1271 MachineBasicBlock *XorMBB = new MachineBasicBlock(BB->getBasicBlock());
1272 MachineBasicBlock *PhiMBB = new MachineBasicBlock(BB->getBasicBlock());
1273 MachineBasicBlock *OldMBB = BB;
1274 ilist<MachineBasicBlock>::iterator It = BB; ++It;
1275 F->getBasicBlockList().insert(It, XorMBB);
1276 F->getBasicBlockList().insert(It, PhiMBB);
1277 BB->addSuccessor(XorMBB);
1278 BB->addSuccessor(PhiMBB);
1279 // Convert from floating point to unsigned 32-bit value
1280 // Use 0 if incoming value is < 0.0
1281 BuildMI(BB, PPC::FSEL, 3, UseZero).addReg(Tmp1).addReg(Tmp1).addReg(Zero);
1282 // Use 2**32 - 1 if incoming value is >= 2**32
1283 BuildMI(BB, PPC::FSUB, 2, UseMaxInt).addReg(MaxInt).addReg(Tmp1);
1284 BuildMI(BB, PPC::FSEL, 3, UseChoice).addReg(UseMaxInt).addReg(UseZero)
1285 .addReg(MaxInt);
1286 // Subtract 2**31
1287 BuildMI(BB, PPC::FSUB, 2, TmpReg).addReg(UseChoice).addReg(Border);
1288 // Use difference if >= 2**31
1289 BuildMI(BB, PPC::FCMPU, 2, PPC::CR0).addReg(UseChoice).addReg(Border);
1290 BuildMI(BB, PPC::FSEL, 3, TmpReg2).addReg(TmpReg).addReg(TmpReg)
1291 .addReg(UseChoice);
1292 // Convert to integer
1293 BuildMI(BB, PPC::FCTIWZ, 1, ConvReg).addReg(TmpReg2);
1294 addFrameReference(BuildMI(BB, PPC::STFD, 3).addReg(ConvReg), FrameIdx);
1295 addFrameReference(BuildMI(BB, PPC::LWZ, 2, IntTmp), FrameIdx, 4);
1296 BuildMI(BB, PPC::BLT, 2).addReg(PPC::CR0).addMBB(PhiMBB);
1297 BuildMI(BB, PPC::B, 1).addMBB(XorMBB);
1298
1299 // XorMBB:
1300 // add 2**31 if input was >= 2**31
1301 BB = XorMBB;
1302 BuildMI(BB, PPC::XORIS, 2, XorReg).addReg(IntTmp).addImm(0x8000);
1303 XorMBB->addSuccessor(PhiMBB);
1304
1305 // PhiMBB:
1306 // DestReg = phi [ IntTmp, OldMBB ], [ XorReg, XorMBB ]
1307 BB = PhiMBB;
1308 BuildMI(BB, PPC::PHI, 4, Result).addReg(IntTmp).addMBB(OldMBB)
1309 .addReg(XorReg).addMBB(XorMBB);
1310 return Result;
1311 }
1312 assert(0 && "Should never get here");
1313 return 0;
1314 }
Nate Begemana9795f82005-03-24 04:41:43 +00001315
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001316 case ISD::SETCC:
Nate Begeman33162522005-03-29 21:54:38 +00001317 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Node)) {
Nate Begemandffcfcc2005-04-01 00:32:34 +00001318 Opc = SelectSetCR0(N);
Nate Begeman33162522005-03-29 21:54:38 +00001319
1320 // Create an iterator with which to insert the MBB for copying the false
1321 // value and the MBB to hold the PHI instruction for this SetCC.
1322 MachineBasicBlock *thisMBB = BB;
1323 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1324 ilist<MachineBasicBlock>::iterator It = BB;
1325 ++It;
1326
1327 // thisMBB:
1328 // ...
1329 // cmpTY cr0, r1, r2
1330 // %TrueValue = li 1
1331 // bCC sinkMBB
Nate Begeman33162522005-03-29 21:54:38 +00001332 unsigned TrueValue = MakeReg(MVT::i32);
1333 BuildMI(BB, PPC::LI, 1, TrueValue).addSImm(1);
1334 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
1335 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
1336 BuildMI(BB, Opc, 2).addReg(PPC::CR0).addMBB(sinkMBB);
1337 MachineFunction *F = BB->getParent();
1338 F->getBasicBlockList().insert(It, copy0MBB);
1339 F->getBasicBlockList().insert(It, sinkMBB);
1340 // Update machine-CFG edges
1341 BB->addSuccessor(copy0MBB);
1342 BB->addSuccessor(sinkMBB);
1343
1344 // copy0MBB:
1345 // %FalseValue = li 0
1346 // fallthrough
1347 BB = copy0MBB;
1348 unsigned FalseValue = MakeReg(MVT::i32);
1349 BuildMI(BB, PPC::LI, 1, FalseValue).addSImm(0);
1350 // Update machine-CFG edges
1351 BB->addSuccessor(sinkMBB);
1352
1353 // sinkMBB:
1354 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1355 // ...
1356 BB = sinkMBB;
1357 BuildMI(BB, PPC::PHI, 4, Result).addReg(FalseValue)
1358 .addMBB(copy0MBB).addReg(TrueValue).addMBB(thisMBB);
1359 return Result;
1360 }
1361 assert(0 && "Is this legal?");
1362 return 0;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001363
Nate Begeman74747862005-03-29 22:24:51 +00001364 case ISD::SELECT: {
Nate Begemandffcfcc2005-04-01 00:32:34 +00001365 Opc = SelectSetCR0(N.getOperand(0));
1366
Nate Begeman74747862005-03-29 22:24:51 +00001367 // Create an iterator with which to insert the MBB for copying the false
1368 // value and the MBB to hold the PHI instruction for this SetCC.
1369 MachineBasicBlock *thisMBB = BB;
1370 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1371 ilist<MachineBasicBlock>::iterator It = BB;
1372 ++It;
1373
1374 // thisMBB:
1375 // ...
1376 // TrueVal = ...
1377 // cmpTY cr0, r1, r2
1378 // bCC copy1MBB
1379 // fallthrough --> copy0MBB
Nate Begeman74747862005-03-29 22:24:51 +00001380 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
1381 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
1382 unsigned TrueValue = SelectExpr(N.getOperand(1)); //Use if TRUE
Nate Begeman3e897162005-03-31 23:55:40 +00001383 BuildMI(BB, Opc, 2).addReg(PPC::CR0).addMBB(sinkMBB);
Nate Begeman74747862005-03-29 22:24:51 +00001384 MachineFunction *F = BB->getParent();
1385 F->getBasicBlockList().insert(It, copy0MBB);
1386 F->getBasicBlockList().insert(It, sinkMBB);
1387 // Update machine-CFG edges
1388 BB->addSuccessor(copy0MBB);
1389 BB->addSuccessor(sinkMBB);
1390
1391 // copy0MBB:
1392 // %FalseValue = ...
1393 // # fallthrough to sinkMBB
1394 BB = copy0MBB;
1395 unsigned FalseValue = SelectExpr(N.getOperand(2)); //Use if FALSE
1396 // Update machine-CFG edges
1397 BB->addSuccessor(sinkMBB);
1398
1399 // sinkMBB:
1400 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1401 // ...
1402 BB = sinkMBB;
1403 BuildMI(BB, PPC::PHI, 4, Result).addReg(FalseValue)
1404 .addMBB(copy0MBB).addReg(TrueValue).addMBB(thisMBB);
1405
1406 // FIXME: Select i64?
1407 return Result;
1408 }
Nate Begemana9795f82005-03-24 04:41:43 +00001409
1410 case ISD::Constant:
1411 switch (N.getValueType()) {
1412 default: assert(0 && "Cannot use constants of this type!");
1413 case MVT::i1:
1414 BuildMI(BB, PPC::LI, 1, Result)
1415 .addSImm(!cast<ConstantSDNode>(N)->isNullValue());
1416 break;
1417 case MVT::i32:
1418 {
1419 int v = (int)cast<ConstantSDNode>(N)->getSignExtended();
1420 if (v < 32768 && v >= -32768) {
1421 BuildMI(BB, PPC::LI, 1, Result).addSImm(v);
1422 } else {
Nate Begeman5e966612005-03-24 06:28:42 +00001423 Tmp1 = MakeReg(MVT::i32);
1424 BuildMI(BB, PPC::LIS, 1, Tmp1).addSImm(v >> 16);
1425 BuildMI(BB, PPC::ORI, 2, Result).addReg(Tmp1).addImm(v & 0xFFFF);
Nate Begemana9795f82005-03-24 04:41:43 +00001426 }
1427 }
1428 }
1429 return Result;
1430 }
1431
1432 return 0;
1433}
1434
1435void ISel::Select(SDOperand N) {
1436 unsigned Tmp1, Tmp2, Opc;
1437 unsigned opcode = N.getOpcode();
1438
1439 if (!ExprMap.insert(std::make_pair(N, 1)).second)
1440 return; // Already selected.
1441
1442 SDNode *Node = N.Val;
1443
1444 switch (Node->getOpcode()) {
1445 default:
1446 Node->dump(); std::cerr << "\n";
1447 assert(0 && "Node not handled yet!");
1448 case ISD::EntryToken: return; // Noop
1449 case ISD::TokenFactor:
1450 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1451 Select(Node->getOperand(i));
1452 return;
1453 case ISD::ADJCALLSTACKDOWN:
1454 case ISD::ADJCALLSTACKUP:
1455 Select(N.getOperand(0));
1456 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1457 Opc = N.getOpcode() == ISD::ADJCALLSTACKDOWN ? PPC::ADJCALLSTACKDOWN :
1458 PPC::ADJCALLSTACKUP;
1459 BuildMI(BB, Opc, 1).addImm(Tmp1);
1460 return;
1461 case ISD::BR: {
1462 MachineBasicBlock *Dest =
1463 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
Nate Begemana9795f82005-03-24 04:41:43 +00001464 Select(N.getOperand(0));
1465 BuildMI(BB, PPC::B, 1).addMBB(Dest);
1466 return;
1467 }
1468 case ISD::BRCOND:
1469 SelectBranchCC(N);
1470 return;
1471 case ISD::CopyToReg:
1472 Select(N.getOperand(0));
1473 Tmp1 = SelectExpr(N.getOperand(1));
1474 Tmp2 = cast<RegSDNode>(N)->getReg();
1475
1476 if (Tmp1 != Tmp2) {
1477 if (N.getOperand(1).getValueType() == MVT::f64 ||
1478 N.getOperand(1).getValueType() == MVT::f32)
1479 BuildMI(BB, PPC::FMR, 1, Tmp2).addReg(Tmp1);
1480 else
1481 BuildMI(BB, PPC::OR, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
1482 }
1483 return;
1484 case ISD::ImplicitDef:
1485 Select(N.getOperand(0));
1486 BuildMI(BB, PPC::IMPLICIT_DEF, 0, cast<RegSDNode>(N)->getReg());
1487 return;
1488 case ISD::RET:
1489 switch (N.getNumOperands()) {
1490 default:
1491 assert(0 && "Unknown return instruction!");
1492 case 3:
1493 assert(N.getOperand(1).getValueType() == MVT::i32 &&
1494 N.getOperand(2).getValueType() == MVT::i32 &&
1495 "Unknown two-register value!");
1496 Select(N.getOperand(0));
1497 Tmp1 = SelectExpr(N.getOperand(1));
1498 Tmp2 = SelectExpr(N.getOperand(2));
1499 BuildMI(BB, PPC::OR, 2, PPC::R3).addReg(Tmp1).addReg(Tmp1);
1500 BuildMI(BB, PPC::OR, 2, PPC::R4).addReg(Tmp2).addReg(Tmp2);
1501 break;
1502 case 2:
1503 Select(N.getOperand(0));
1504 Tmp1 = SelectExpr(N.getOperand(1));
1505 switch (N.getOperand(1).getValueType()) {
1506 default:
1507 assert(0 && "Unknown return type!");
1508 case MVT::f64:
1509 case MVT::f32:
1510 BuildMI(BB, PPC::FMR, 1, PPC::F1).addReg(Tmp1);
1511 break;
1512 case MVT::i32:
1513 BuildMI(BB, PPC::OR, 2, PPC::R3).addReg(Tmp1).addReg(Tmp1);
1514 break;
1515 }
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001516 case 1:
1517 Select(N.getOperand(0));
1518 break;
Nate Begemana9795f82005-03-24 04:41:43 +00001519 }
1520 BuildMI(BB, PPC::BLR, 0); // Just emit a 'ret' instruction
1521 return;
Nate Begemana9795f82005-03-24 04:41:43 +00001522 case ISD::TRUNCSTORE:
1523 case ISD::STORE:
1524 {
1525 SDOperand Chain = N.getOperand(0);
1526 SDOperand Value = N.getOperand(1);
1527 SDOperand Address = N.getOperand(2);
1528 Select(Chain);
1529
1530 Tmp1 = SelectExpr(Value); //value
1531
1532 if (opcode == ISD::STORE) {
1533 switch(Value.getValueType()) {
1534 default: assert(0 && "unknown Type in store");
1535 case MVT::i32: Opc = PPC::STW; break;
1536 case MVT::f64: Opc = PPC::STFD; break;
1537 case MVT::f32: Opc = PPC::STFS; break;
1538 }
1539 } else { //ISD::TRUNCSTORE
1540 switch(cast<MVTSDNode>(Node)->getExtraValueType()) {
1541 default: assert(0 && "unknown Type in store");
1542 case MVT::i1: //FIXME: DAG does not promote this load
1543 case MVT::i8: Opc = PPC::STB; break;
1544 case MVT::i16: Opc = PPC::STH; break;
1545 }
1546 }
1547
1548 if (Address.getOpcode() == ISD::GlobalAddress)
1549 {
1550 BuildMI(BB, Opc, 2).addReg(Tmp1)
1551 .addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
1552 }
1553 else if(Address.getOpcode() == ISD::FrameIndex)
1554 {
Nate Begeman58f718c2005-03-30 02:23:08 +00001555 Tmp2 = cast<FrameIndexSDNode>(Address)->getIndex();
1556 addFrameReference(BuildMI(BB, Opc, 3).addReg(Tmp1), (int)Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +00001557 }
1558 else
1559 {
1560 int offset;
Nate Begeman04730362005-04-01 04:45:11 +00001561 bool idx = SelectAddr(Address, Tmp2, offset);
1562 if (idx) {
1563 Opc = IndexedOpForOp(Opc);
1564 BuildMI(BB, Opc, 3).addReg(Tmp1).addReg(Tmp2).addReg(offset);
1565 } else {
1566 BuildMI(BB, Opc, 3).addReg(Tmp1).addImm(offset).addReg(Tmp2);
1567 }
Nate Begemana9795f82005-03-24 04:41:43 +00001568 }
1569 return;
1570 }
1571 case ISD::EXTLOAD:
1572 case ISD::SEXTLOAD:
1573 case ISD::ZEXTLOAD:
1574 case ISD::LOAD:
1575 case ISD::CopyFromReg:
1576 case ISD::CALL:
1577 case ISD::DYNAMIC_STACKALLOC:
1578 ExprMap.erase(N);
1579 SelectExpr(N);
1580 return;
1581 }
1582 assert(0 && "Should not be reached!");
1583}
1584
1585
1586/// createPPC32PatternInstructionSelector - This pass converts an LLVM function
1587/// into a machine code representation using pattern matching and a machine
1588/// description file.
1589///
1590FunctionPass *llvm::createPPC32ISelPattern(TargetMachine &TM) {
1591 return new ISel(TM);
Chris Lattner246fa632005-03-24 06:16:18 +00001592}
1593