blob: 558232669e7f58e2fba5a92ed9ad16233d9dac04 [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();
466 unsigned SelectSetCR0(SDOperand CC);
Nate Begemana9795f82005-03-24 04:41:43 +0000467 unsigned SelectExpr(SDOperand N);
468 unsigned SelectExprFP(SDOperand N, unsigned Result);
469 void Select(SDOperand N);
470
471 void SelectAddr(SDOperand N, unsigned& Reg, int& offset);
472 void SelectBranchCC(SDOperand N);
473};
474
475/// canUseAsImmediateForOpcode - This method returns a value indicating whether
476/// the ConstantSDNode N can be used as an immediate to Opcode. The return
477/// values are either 0, 1 or 2. 0 indicates that either N is not a
478/// ConstantSDNode, or is not suitable for use by that opcode. A return value
479/// of 1 indicates that the constant may be used in normal immediate form. A
480/// return value of 2 indicates that the constant may be used in shifted
481/// immediate form. If the return value is nonzero, the constant value is
482/// placed in Imm.
483///
484static unsigned canUseAsImmediateForOpcode(SDOperand N, unsigned Opcode,
Nate Begeman3e897162005-03-31 23:55:40 +0000485 unsigned& Imm, bool U = false) {
Nate Begemana9795f82005-03-24 04:41:43 +0000486 if (N.getOpcode() != ISD::Constant) return 0;
487
488 int v = (int)cast<ConstantSDNode>(N)->getSignExtended();
489
490 switch(Opcode) {
491 default: return 0;
492 case ISD::ADD:
493 if (v <= 32767 && v >= -32768) { Imm = v & 0xFFFF; return 1; }
494 if ((v & 0x0000FFFF) == 0) { Imm = v >> 16; return 2; }
495 break;
496 case ISD::AND:
497 case ISD::XOR:
498 case ISD::OR:
499 if (v >= 0 && v <= 65535) { Imm = v & 0xFFFF; return 1; }
500 if ((v & 0x0000FFFF) == 0) { Imm = v >> 16; return 2; }
501 break;
Nate Begeman307e7442005-03-26 01:28:53 +0000502 case ISD::MUL:
503 if (v <= 32767 && v >= -32768) { Imm = v & 0xFFFF; return 1; }
504 break;
Nate Begeman3e897162005-03-31 23:55:40 +0000505 case ISD::SETCC:
506 if (U && (v >= 0 && v <= 65535)) { Imm = v & 0xFFFF; return 1; }
507 if (!U && (v <= 32767 && v >= -32768)) { Imm = v & 0xFFFF; return 1; }
508 break;
Nate Begemana9795f82005-03-24 04:41:43 +0000509 }
510 return 0;
511}
Nate Begeman3e897162005-03-31 23:55:40 +0000512
513/// getBCCForSetCC - Returns the PowerPC condition branch mnemonic corresponding
514/// to Condition. If the Condition is unordered or unsigned, the bool argument
515/// U is set to true, otherwise it is set to false.
516static unsigned getBCCForSetCC(unsigned Condition, bool& U) {
517 U = false;
518 switch (Condition) {
519 default: assert(0 && "Unknown condition!"); abort();
520 case ISD::SETEQ: return PPC::BEQ;
521 case ISD::SETNE: return PPC::BNE;
522 case ISD::SETULT: U = true;
523 case ISD::SETLT: return PPC::BLT;
524 case ISD::SETULE: U = true;
525 case ISD::SETLE: return PPC::BLE;
526 case ISD::SETUGT: U = true;
527 case ISD::SETGT: return PPC::BGT;
528 case ISD::SETUGE: U = true;
529 case ISD::SETGE: return PPC::BGE;
530 }
531}
Nate Begemana9795f82005-03-24 04:41:43 +0000532}
533
Nate Begemanc7b09f12005-03-25 08:34:25 +0000534/// getGlobalBaseReg - Output the instructions required to put the
535/// base address to use for accessing globals into a register.
536///
537unsigned ISel::getGlobalBaseReg() {
538 if (!GlobalBaseInitialized) {
539 // Insert the set of GlobalBaseReg into the first MBB of the function
540 MachineBasicBlock &FirstMBB = BB->getParent()->front();
541 MachineBasicBlock::iterator MBBI = FirstMBB.begin();
542 GlobalBaseReg = MakeReg(MVT::i32);
543 BuildMI(FirstMBB, MBBI, PPC::MovePCtoLR, 0, PPC::LR);
544 BuildMI(FirstMBB, MBBI, PPC::MFLR, 1, GlobalBaseReg).addReg(PPC::LR);
545 GlobalBaseInitialized = true;
546 }
547 return GlobalBaseReg;
548}
549
Nate Begemandffcfcc2005-04-01 00:32:34 +0000550unsigned ISel::SelectSetCR0(SDOperand CC) {
551 unsigned Opc, Tmp1, Tmp2;
552 static const unsigned CompareOpcodes[] =
553 { PPC::FCMPU, PPC::FCMPU, PPC::CMPW, PPC::CMPLW };
554
555 // If the first operand to the select is a SETCC node, then we can fold it
556 // into the branch that selects which value to return.
557 SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(CC.Val);
558 if (SetCC && CC.getOpcode() == ISD::SETCC) {
559 bool U;
560 Opc = getBCCForSetCC(SetCC->getCondition(), U);
561 Tmp1 = SelectExpr(SetCC->getOperand(0));
562
563 // Pass the optional argument U to canUseAsImmediateForOpcode for SETCC,
564 // so that it knows whether the SETCC immediate range is signed or not.
565 if (1 == canUseAsImmediateForOpcode(SetCC->getOperand(1), ISD::SETCC,
566 Tmp2, U)) {
567 if (U)
568 BuildMI(BB, PPC::CMPLWI, 2, PPC::CR0).addReg(Tmp1).addImm(Tmp2);
569 else
570 BuildMI(BB, PPC::CMPWI, 2, PPC::CR0).addReg(Tmp1).addSImm(Tmp2);
571 } else {
572 bool IsInteger = MVT::isInteger(SetCC->getOperand(0).getValueType());
573 unsigned CompareOpc = CompareOpcodes[2 * IsInteger + U];
574 Tmp2 = SelectExpr(SetCC->getOperand(1));
575 BuildMI(BB, CompareOpc, 2, PPC::CR0).addReg(Tmp1).addReg(Tmp2);
576 }
577 } else {
578 Tmp1 = SelectExpr(CC);
579 BuildMI(BB, PPC::CMPLWI, 2, PPC::CR0).addReg(Tmp1).addImm(0);
580 Opc = PPC::BNE;
581 }
582 return Opc;
583}
584
585/// Check to see if the load is a constant offset from a base register
Nate Begemana9795f82005-03-24 04:41:43 +0000586void ISel::SelectAddr(SDOperand N, unsigned& Reg, int& offset)
587{
Nate Begeman96fc6812005-03-31 02:05:53 +0000588 unsigned imm = 0, opcode = N.getOpcode();
589 if (N.getOpcode() == ISD::ADD)
590 if (1 == canUseAsImmediateForOpcode(N.getOperand(1), opcode, imm)) {
591 Reg = SelectExpr(N.getOperand(0));
592 offset = imm;
593 return;
594 }
Nate Begemana9795f82005-03-24 04:41:43 +0000595 Reg = SelectExpr(N);
596 offset = 0;
597 return;
598}
599
600void ISel::SelectBranchCC(SDOperand N)
601{
602 assert(N.getOpcode() == ISD::BRCOND && "Not a BranchCC???");
603 MachineBasicBlock *Dest =
604 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
Nate Begeman3e897162005-03-31 23:55:40 +0000605
Nate Begemana9795f82005-03-24 04:41:43 +0000606 Select(N.getOperand(0)); //chain
Nate Begemandffcfcc2005-04-01 00:32:34 +0000607 unsigned Opc = SelectSetCR0(N.getOperand(1));
Nate Begeman3e897162005-03-31 23:55:40 +0000608 BuildMI(BB, Opc, 2).addReg(PPC::CR0).addMBB(Dest);
Nate Begemana9795f82005-03-24 04:41:43 +0000609 return;
610}
611
612unsigned ISel::SelectExprFP(SDOperand N, unsigned Result)
613{
614 unsigned Tmp1, Tmp2, Tmp3;
615 unsigned Opc = 0;
616 SDNode *Node = N.Val;
617 MVT::ValueType DestType = N.getValueType();
618 unsigned opcode = N.getOpcode();
619
620 switch (opcode) {
621 default:
622 Node->dump();
623 assert(0 && "Node not handled!\n");
624
Nate Begeman23afcfb2005-03-29 22:48:55 +0000625 case ISD::SELECT: {
Nate Begeman3e897162005-03-31 23:55:40 +0000626 // Attempt to generate FSEL. We can do this whenever we have an FP result,
627 // and an FP comparison in the SetCC node.
628 SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(N.getOperand(0).Val);
629 if (SetCC && N.getOperand(0).getOpcode() == ISD::SETCC &&
630 !MVT::isInteger(SetCC->getOperand(0).getValueType()) &&
631 SetCC->getCondition() != ISD::SETEQ &&
632 SetCC->getCondition() != ISD::SETNE) {
633 MVT::ValueType VT = SetCC->getOperand(0).getValueType();
634 Tmp1 = SelectExpr(SetCC->getOperand(0)); // Val to compare against
635 unsigned TV = SelectExpr(N.getOperand(1)); // Use if TRUE
636 unsigned FV = SelectExpr(N.getOperand(2)); // Use if FALSE
637
638 ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(1));
639 if (CN && (CN->isExactlyValue(-0.0) || CN->isExactlyValue(0.0))) {
640 switch(SetCC->getCondition()) {
641 default: assert(0 && "Invalid FSEL condition"); abort();
642 case ISD::SETULT:
643 case ISD::SETLT:
644 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp1).addReg(FV).addReg(TV);
645 return Result;
646 case ISD::SETUGE:
647 case ISD::SETGE:
648 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp1).addReg(TV).addReg(FV);
649 return Result;
650 case ISD::SETUGT:
651 case ISD::SETGT: {
652 Tmp2 = MakeReg(VT);
653 BuildMI(BB, PPC::FNEG, 1, Tmp2).addReg(Tmp1);
654 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp2).addReg(FV).addReg(TV);
655 return Result;
656 }
657 case ISD::SETULE:
658 case ISD::SETLE: {
659 Tmp2 = MakeReg(VT);
660 BuildMI(BB, PPC::FNEG, 1, Tmp2).addReg(Tmp1);
661 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp2).addReg(TV).addReg(FV);
662 return Result;
663 }
664 }
665 } else {
666 Opc = (MVT::f64 == VT) ? PPC::FSUB : PPC::FSUBS;
667 Tmp2 = SelectExpr(SetCC->getOperand(1));
668 Tmp3 = MakeReg(VT);
669 switch(SetCC->getCondition()) {
670 default: assert(0 && "Invalid FSEL condition"); abort();
671 case ISD::SETULT:
672 case ISD::SETLT:
673 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
674 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(FV).addReg(TV);
675 return Result;
676 case ISD::SETUGE:
677 case ISD::SETGE:
678 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
679 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(TV).addReg(FV);
680 return Result;
681 case ISD::SETUGT:
682 case ISD::SETGT:
683 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp2).addReg(Tmp1);
684 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(FV).addReg(TV);
685 return Result;
686 case ISD::SETULE:
687 case ISD::SETLE:
688 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp2).addReg(Tmp1);
689 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(TV).addReg(FV);
690 return Result;
691 }
692 }
693 assert(0 && "Should never get here");
694 return 0;
695 }
696
Nate Begeman23afcfb2005-03-29 22:48:55 +0000697 // Create an iterator with which to insert the MBB for copying the false
698 // value and the MBB to hold the PHI instruction for this SetCC.
699 MachineBasicBlock *thisMBB = BB;
700 const BasicBlock *LLVM_BB = BB->getBasicBlock();
701 ilist<MachineBasicBlock>::iterator It = BB;
702 ++It;
703
704 // thisMBB:
705 // ...
706 // TrueVal = ...
707 // cmpTY cr0, r1, r2
708 // bCC copy1MBB
709 // fallthrough --> copy0MBB
Nate Begeman3e897162005-03-31 23:55:40 +0000710 Tmp1 = SelectExpr(N.getOperand(0)); //Cond
Nate Begeman23afcfb2005-03-29 22:48:55 +0000711 BuildMI(BB, PPC::CMPLWI, 2, PPC::CR0).addReg(Tmp1).addImm(0);
712 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
713 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
714 unsigned TrueValue = SelectExpr(N.getOperand(1)); //Use if TRUE
715 BuildMI(BB, PPC::BNE, 2).addReg(PPC::CR0).addMBB(sinkMBB);
716 MachineFunction *F = BB->getParent();
717 F->getBasicBlockList().insert(It, copy0MBB);
718 F->getBasicBlockList().insert(It, sinkMBB);
719 // Update machine-CFG edges
720 BB->addSuccessor(copy0MBB);
721 BB->addSuccessor(sinkMBB);
722
723 // copy0MBB:
724 // %FalseValue = ...
725 // # fallthrough to sinkMBB
726 BB = copy0MBB;
727 unsigned FalseValue = SelectExpr(N.getOperand(2)); //Use if FALSE
728 // Update machine-CFG edges
729 BB->addSuccessor(sinkMBB);
730
731 // sinkMBB:
732 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
733 // ...
734 BB = sinkMBB;
735 BuildMI(BB, PPC::PHI, 4, Result).addReg(FalseValue)
736 .addMBB(copy0MBB).addReg(TrueValue).addMBB(thisMBB);
737 return Result;
738 }
Nate Begemana9795f82005-03-24 04:41:43 +0000739
740 case ISD::FP_ROUND:
741 assert (DestType == MVT::f32 &&
742 N.getOperand(0).getValueType() == MVT::f64 &&
743 "only f64 to f32 conversion supported here");
744 Tmp1 = SelectExpr(N.getOperand(0));
745 BuildMI(BB, PPC::FRSP, 1, Result).addReg(Tmp1);
746 return Result;
747
748 case ISD::FP_EXTEND:
749 assert (DestType == MVT::f64 &&
750 N.getOperand(0).getValueType() == MVT::f32 &&
751 "only f32 to f64 conversion supported here");
752 Tmp1 = SelectExpr(N.getOperand(0));
753 BuildMI(BB, PPC::FMR, 1, Result).addReg(Tmp1);
754 return Result;
755
756 case ISD::CopyFromReg:
Nate Begemanf2622612005-03-26 02:17:46 +0000757 if (Result == 1)
758 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
759 Tmp1 = dyn_cast<RegSDNode>(Node)->getReg();
760 BuildMI(BB, PPC::FMR, 1, Result).addReg(Tmp1);
761 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +0000762
Nate Begeman6d369cc2005-04-01 01:08:07 +0000763 case ISD::ConstantFP: {
764 Tmp1 = MakeReg(MVT::i32);
765 ConstantFPSDNode *CN = cast<ConstantFPSDNode>(N);
766 MachineConstantPool *CP = BB->getParent()->getConstantPool();
767 ConstantFP *CFP = ConstantFP::get(Type::DoubleTy, CN->getValue());
768 unsigned CPI = CP->getConstantPoolIndex(CFP);
769 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp1).addReg(getGlobalBaseReg())
770 .addConstantPoolIndex(CPI);
771 BuildMI(BB, PPC::LFD, 2, Result).addConstantPoolIndex(CPI).addReg(Tmp1);
772 return Result;
773 }
Nate Begemana9795f82005-03-24 04:41:43 +0000774
775 case ISD::MUL:
776 case ISD::ADD:
777 case ISD::SUB:
778 case ISD::SDIV:
779 switch( opcode ) {
780 case ISD::MUL: Opc = DestType == MVT::f64 ? PPC::FMUL : PPC::FMULS; break;
781 case ISD::ADD: Opc = DestType == MVT::f64 ? PPC::FADD : PPC::FADDS; break;
782 case ISD::SUB: Opc = DestType == MVT::f64 ? PPC::FSUB : PPC::FSUBS; break;
783 case ISD::SDIV: Opc = DestType == MVT::f64 ? PPC::FDIV : PPC::FDIVS; break;
784 };
Nate Begemana9795f82005-03-24 04:41:43 +0000785 Tmp1 = SelectExpr(N.getOperand(0));
786 Tmp2 = SelectExpr(N.getOperand(1));
787 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
788 return Result;
789
Nate Begemana9795f82005-03-24 04:41:43 +0000790 case ISD::UINT_TO_FP:
Nate Begemanfdcf3412005-03-30 19:38:35 +0000791 case ISD::SINT_TO_FP: {
792 assert (N.getOperand(0).getValueType() == MVT::i32
793 && "int to float must operate on i32");
794 bool IsUnsigned = (ISD::UINT_TO_FP == opcode);
795 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
796 Tmp2 = MakeReg(MVT::f64); // temp reg to load the integer value into
797 Tmp3 = MakeReg(MVT::i32); // temp reg to hold the conversion constant
798 unsigned ConstF = MakeReg(MVT::f64); // temp reg to hold the fp constant
799
800 int FrameIdx = BB->getParent()->getFrameInfo()->CreateStackObject(8, 8);
801 MachineConstantPool *CP = BB->getParent()->getConstantPool();
802
803 // FIXME: pull this FP constant generation stuff out into something like
804 // the simple ISel's getReg.
805 if (IsUnsigned) {
806 ConstantFP *CFP = ConstantFP::get(Type::DoubleTy, 0x1.000000p52);
807 unsigned CPI = CP->getConstantPoolIndex(CFP);
808 // Load constant fp value
809 unsigned Tmp4 = MakeReg(MVT::i32);
810 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp4).addReg(getGlobalBaseReg())
811 .addConstantPoolIndex(CPI);
812 BuildMI(BB, PPC::LFD, 2, ConstF).addConstantPoolIndex(CPI).addReg(Tmp4);
813 // Store the hi & low halves of the fp value, currently in int regs
814 BuildMI(BB, PPC::LIS, 1, Tmp3).addSImm(0x4330);
815 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(Tmp3), FrameIdx);
816 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(Tmp1), FrameIdx, 4);
817 addFrameReference(BuildMI(BB, PPC::LFD, 2, Tmp2), FrameIdx);
818 // Generate the return value with a subtract
819 BuildMI(BB, PPC::FSUB, 2, Result).addReg(Tmp2).addReg(ConstF);
820 } else {
821 ConstantFP *CFP = ConstantFP::get(Type::DoubleTy, 0x1.000008p52);
822 unsigned CPI = CP->getConstantPoolIndex(CFP);
823 // Load constant fp value
824 unsigned Tmp4 = MakeReg(MVT::i32);
825 unsigned TmpL = MakeReg(MVT::i32);
826 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp4).addReg(getGlobalBaseReg())
827 .addConstantPoolIndex(CPI);
828 BuildMI(BB, PPC::LFD, 2, ConstF).addConstantPoolIndex(CPI).addReg(Tmp4);
829 // Store the hi & low halves of the fp value, currently in int regs
830 BuildMI(BB, PPC::LIS, 1, Tmp3).addSImm(0x4330);
831 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(Tmp3), FrameIdx);
832 BuildMI(BB, PPC::XORIS, 2, TmpL).addReg(Tmp1).addImm(0x8000);
833 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(TmpL), FrameIdx, 4);
834 addFrameReference(BuildMI(BB, PPC::LFD, 2, Tmp2), FrameIdx);
835 // Generate the return value with a subtract
836 BuildMI(BB, PPC::FSUB, 2, Result).addReg(Tmp2).addReg(ConstF);
837 }
838 return Result;
839 }
Nate Begemana9795f82005-03-24 04:41:43 +0000840 }
841 assert(0 && "should not get here");
842 return 0;
843}
844
845unsigned ISel::SelectExpr(SDOperand N) {
846 unsigned Result;
847 unsigned Tmp1, Tmp2, Tmp3;
848 unsigned Opc = 0;
849 unsigned opcode = N.getOpcode();
850
851 SDNode *Node = N.Val;
852 MVT::ValueType DestType = N.getValueType();
853
854 unsigned &Reg = ExprMap[N];
855 if (Reg) return Reg;
856
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000857 if (N.getOpcode() != ISD::CALL && N.getOpcode() != ISD::ADD_PARTS &&
858 N.getOpcode() != ISD::SUB_PARTS)
Nate Begemana9795f82005-03-24 04:41:43 +0000859 Reg = Result = (N.getValueType() != MVT::Other) ?
860 MakeReg(N.getValueType()) : 1;
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000861 else {
862 // If this is a call instruction, make sure to prepare ALL of the result
863 // values as well as the chain.
864 if (N.getOpcode() == ISD::CALL) {
865 if (Node->getNumValues() == 1)
866 Reg = Result = 1; // Void call, just a chain.
867 else {
868 Result = MakeReg(Node->getValueType(0));
869 ExprMap[N.getValue(0)] = Result;
870 for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
871 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
872 ExprMap[SDOperand(Node, Node->getNumValues()-1)] = 1;
873 }
874 } else {
875 Result = MakeReg(Node->getValueType(0));
876 ExprMap[N.getValue(0)] = Result;
877 for (unsigned i = 1, e = N.Val->getNumValues(); i != e; ++i)
878 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
879 }
880 }
881
882 if (DestType == MVT::f64 || DestType == MVT::f32)
Nate Begeman74d73452005-03-31 00:15:26 +0000883 if (ISD::LOAD != opcode && ISD::EXTLOAD != opcode)
884 return SelectExprFP(N, Result);
Nate Begemana9795f82005-03-24 04:41:43 +0000885
886 switch (opcode) {
887 default:
888 Node->dump();
889 assert(0 && "Node not handled!\n");
890
891 case ISD::DYNAMIC_STACKALLOC:
Nate Begeman5e966612005-03-24 06:28:42 +0000892 // Generate both result values. FIXME: Need a better commment here?
893 if (Result != 1)
894 ExprMap[N.getValue(1)] = 1;
895 else
896 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
897
898 // FIXME: We are currently ignoring the requested alignment for handling
899 // greater than the stack alignment. This will need to be revisited at some
900 // point. Align = N.getOperand(2);
901 if (!isa<ConstantSDNode>(N.getOperand(2)) ||
902 cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) {
903 std::cerr << "Cannot allocate stack object with greater alignment than"
904 << " the stack alignment yet!";
905 abort();
906 }
907 Select(N.getOperand(0));
908 Tmp1 = SelectExpr(N.getOperand(1));
909 // Subtract size from stack pointer, thereby allocating some space.
910 BuildMI(BB, PPC::SUBF, 2, PPC::R1).addReg(Tmp1).addReg(PPC::R1);
911 // Put a pointer to the space into the result register by copying the SP
912 BuildMI(BB, PPC::OR, 2, Result).addReg(PPC::R1).addReg(PPC::R1);
913 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +0000914
915 case ISD::ConstantPool:
Nate Begemanca12a2b2005-03-28 22:28:37 +0000916 Tmp1 = cast<ConstantPoolSDNode>(N)->getIndex();
917 Tmp2 = MakeReg(MVT::i32);
918 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp2).addReg(getGlobalBaseReg())
919 .addConstantPoolIndex(Tmp1);
920 BuildMI(BB, PPC::LA, 2, Result).addReg(Tmp2).addConstantPoolIndex(Tmp1);
921 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +0000922
923 case ISD::FrameIndex:
Nate Begemanf3d08f32005-03-29 00:03:27 +0000924 Tmp1 = cast<FrameIndexSDNode>(N)->getIndex();
Nate Begeman58f718c2005-03-30 02:23:08 +0000925 addFrameReference(BuildMI(BB, PPC::ADDI, 2, Result), (int)Tmp1, 0, false);
Nate Begemanf3d08f32005-03-29 00:03:27 +0000926 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +0000927
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000928 case ISD::GlobalAddress: {
929 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
Nate Begemanca12a2b2005-03-28 22:28:37 +0000930 Tmp1 = MakeReg(MVT::i32);
Nate Begemanc7b09f12005-03-25 08:34:25 +0000931 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp1).addReg(getGlobalBaseReg())
932 .addGlobalAddress(GV);
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000933 if (GV->hasWeakLinkage() || GV->isExternal()) {
934 BuildMI(BB, PPC::LWZ, 2, Result).addGlobalAddress(GV).addReg(Tmp1);
935 } else {
936 BuildMI(BB, PPC::LA, 2, Result).addReg(Tmp1).addGlobalAddress(GV);
937 }
938 return Result;
939 }
940
Nate Begeman5e966612005-03-24 06:28:42 +0000941 case ISD::LOAD:
Nate Begemana9795f82005-03-24 04:41:43 +0000942 case ISD::EXTLOAD:
943 case ISD::ZEXTLOAD:
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000944 case ISD::SEXTLOAD: {
Nate Begeman9db505c2005-03-28 19:36:43 +0000945 MVT::ValueType TypeBeingLoaded = (ISD::LOAD == opcode) ?
946 Node->getValueType(0) : cast<MVTSDNode>(Node)->getExtraValueType();
Nate Begeman74d73452005-03-31 00:15:26 +0000947 bool sext = (ISD::SEXTLOAD == opcode);
948 bool byte = (MVT::i8 == TypeBeingLoaded);
949
Nate Begeman5e966612005-03-24 06:28:42 +0000950 // Make sure we generate both values.
951 if (Result != 1)
952 ExprMap[N.getValue(1)] = 1; // Generate the token
953 else
954 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
955
956 SDOperand Chain = N.getOperand(0);
957 SDOperand Address = N.getOperand(1);
958 Select(Chain);
959
Nate Begeman9db505c2005-03-28 19:36:43 +0000960 switch (TypeBeingLoaded) {
Nate Begeman74d73452005-03-31 00:15:26 +0000961 default: Node->dump(); assert(0 && "Cannot load this type!");
Nate Begeman9db505c2005-03-28 19:36:43 +0000962 case MVT::i1: Opc = PPC::LBZ; break;
963 case MVT::i8: Opc = PPC::LBZ; break;
964 case MVT::i16: Opc = sext ? PPC::LHA : PPC::LHZ; break;
965 case MVT::i32: Opc = PPC::LWZ; break;
Nate Begeman74d73452005-03-31 00:15:26 +0000966 case MVT::f32: Opc = PPC::LFS; break;
967 case MVT::f64: Opc = PPC::LFD; break;
Nate Begeman5e966612005-03-24 06:28:42 +0000968 }
969
Nate Begeman74d73452005-03-31 00:15:26 +0000970 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Address)) {
971 Tmp1 = MakeReg(MVT::i32);
972 int CPI = CP->getIndex();
973 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp1).addReg(getGlobalBaseReg())
974 .addConstantPoolIndex(CPI);
975 BuildMI(BB, Opc, 2, Result).addConstantPoolIndex(CPI).addReg(Tmp1);
Nate Begeman9db505c2005-03-28 19:36:43 +0000976 }
Nate Begeman74d73452005-03-31 00:15:26 +0000977 else if(Address.getOpcode() == ISD::FrameIndex) {
Nate Begeman58f718c2005-03-30 02:23:08 +0000978 Tmp1 = cast<FrameIndexSDNode>(Address)->getIndex();
979 addFrameReference(BuildMI(BB, Opc, 2, Result), (int)Tmp1);
Nate Begeman5e966612005-03-24 06:28:42 +0000980 } else {
981 int offset;
982 SelectAddr(Address, Tmp1, offset);
983 BuildMI(BB, Opc, 2, Result).addSImm(offset).addReg(Tmp1);
984 }
985 return Result;
986 }
987
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000988 case ISD::CALL: {
989 // Lower the chain for this call.
990 Select(N.getOperand(0));
991 ExprMap[N.getValue(Node->getNumValues()-1)] = 1;
Nate Begeman74d73452005-03-31 00:15:26 +0000992
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000993 for(int i = 2, e = Node->getNumOperands(); i < e; ++i)
Nate Begeman74d73452005-03-31 00:15:26 +0000994 Select(N.getOperand(i));
995
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000996 // Emit the correct call instruction based on the type of symbol called.
997 if (GlobalAddressSDNode *GASD =
998 dyn_cast<GlobalAddressSDNode>(N.getOperand(1))) {
999 BuildMI(BB, PPC::CALLpcrel, 1).addGlobalAddress(GASD->getGlobal(), true);
1000 } else if (ExternalSymbolSDNode *ESSDN =
1001 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1))) {
1002 BuildMI(BB, PPC::CALLpcrel, 1).addExternalSymbol(ESSDN->getSymbol(), true);
1003 } else {
1004 Tmp1 = SelectExpr(N.getOperand(1));
1005 BuildMI(BB, PPC::OR, 2, PPC::R12).addReg(Tmp1).addReg(Tmp1);
1006 BuildMI(BB, PPC::MTCTR, 1).addReg(PPC::R12);
1007 BuildMI(BB, PPC::CALLindirect, 3).addImm(20).addImm(0).addReg(PPC::R12);
1008 }
1009
1010 switch (Node->getValueType(0)) {
1011 default: assert(0 && "Unknown value type for call result!");
1012 case MVT::Other: return 1;
1013 case MVT::i1:
1014 case MVT::i8:
1015 case MVT::i16:
1016 case MVT::i32:
Nate Begemanc7b09f12005-03-25 08:34:25 +00001017 BuildMI(BB, PPC::OR, 2, Result).addReg(PPC::R3).addReg(PPC::R3);
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001018 if (Node->getValueType(1) == MVT::i32)
Nate Begemanc7b09f12005-03-25 08:34:25 +00001019 BuildMI(BB, PPC::OR, 2, Result+1).addReg(PPC::R4).addReg(PPC::R4);
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001020 break;
1021 case MVT::f32:
1022 case MVT::f64:
1023 BuildMI(BB, PPC::FMR, 1, Result).addReg(PPC::F1);
1024 break;
1025 }
1026 return Result+N.ResNo;
1027 }
Nate Begemana9795f82005-03-24 04:41:43 +00001028
1029 case ISD::SIGN_EXTEND:
1030 case ISD::SIGN_EXTEND_INREG:
1031 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman9db505c2005-03-28 19:36:43 +00001032 switch(cast<MVTSDNode>(Node)->getExtraValueType()) {
1033 default: Node->dump(); assert(0 && "Unhandled SIGN_EXTEND type"); break;
1034 case MVT::i16:
1035 BuildMI(BB, PPC::EXTSH, 1, Result).addReg(Tmp1);
1036 break;
1037 case MVT::i8:
1038 BuildMI(BB, PPC::EXTSB, 1, Result).addReg(Tmp1);
1039 break;
Nate Begeman74747862005-03-29 22:24:51 +00001040 case MVT::i1:
1041 BuildMI(BB, PPC::SUBFIC, 2, Result).addReg(Tmp1).addSImm(0);
1042 break;
Nate Begeman9db505c2005-03-28 19:36:43 +00001043 }
Nate Begemana9795f82005-03-24 04:41:43 +00001044 return Result;
1045
1046 case ISD::ZERO_EXTEND_INREG:
1047 Tmp1 = SelectExpr(N.getOperand(0));
1048 switch(cast<MVTSDNode>(Node)->getExtraValueType()) {
Nate Begeman9db505c2005-03-28 19:36:43 +00001049 default: Node->dump(); assert(0 && "Unhandled ZERO_EXTEND type"); break;
Nate Begemana9795f82005-03-24 04:41:43 +00001050 case MVT::i16: Tmp2 = 16; break;
1051 case MVT::i8: Tmp2 = 24; break;
1052 case MVT::i1: Tmp2 = 31; break;
1053 }
Nate Begeman33162522005-03-29 21:54:38 +00001054 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(0).addImm(Tmp2)
1055 .addImm(31);
Nate Begemana9795f82005-03-24 04:41:43 +00001056 return Result;
1057
Nate Begemana9795f82005-03-24 04:41:43 +00001058 case ISD::CopyFromReg:
1059 if (Result == 1)
1060 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1061 Tmp1 = dyn_cast<RegSDNode>(Node)->getReg();
1062 BuildMI(BB, PPC::OR, 2, Result).addReg(Tmp1).addReg(Tmp1);
1063 return Result;
1064
1065 case ISD::SHL:
Nate Begeman5e966612005-03-24 06:28:42 +00001066 Tmp1 = SelectExpr(N.getOperand(0));
1067 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1068 Tmp2 = CN->getValue() & 0x1F;
Nate Begeman33162522005-03-29 21:54:38 +00001069 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(Tmp2).addImm(0)
Nate Begeman5e966612005-03-24 06:28:42 +00001070 .addImm(31-Tmp2);
1071 } else {
1072 Tmp2 = SelectExpr(N.getOperand(1));
1073 BuildMI(BB, PPC::SLW, 2, Result).addReg(Tmp1).addReg(Tmp2);
1074 }
1075 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001076
Nate Begeman5e966612005-03-24 06:28:42 +00001077 case ISD::SRL:
1078 Tmp1 = SelectExpr(N.getOperand(0));
1079 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1080 Tmp2 = CN->getValue() & 0x1F;
Nate Begeman33162522005-03-29 21:54:38 +00001081 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(32-Tmp2)
Nate Begeman5e966612005-03-24 06:28:42 +00001082 .addImm(Tmp2).addImm(31);
1083 } else {
1084 Tmp2 = SelectExpr(N.getOperand(1));
1085 BuildMI(BB, PPC::SRW, 2, Result).addReg(Tmp1).addReg(Tmp2);
1086 }
1087 return Result;
1088
1089 case ISD::SRA:
1090 Tmp1 = SelectExpr(N.getOperand(0));
1091 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1092 Tmp2 = CN->getValue() & 0x1F;
1093 BuildMI(BB, PPC::SRAWI, 2, Result).addReg(Tmp1).addImm(Tmp2);
1094 } else {
1095 Tmp2 = SelectExpr(N.getOperand(1));
1096 BuildMI(BB, PPC::SRAW, 2, Result).addReg(Tmp1).addReg(Tmp2);
1097 }
1098 return Result;
1099
Nate Begemana9795f82005-03-24 04:41:43 +00001100 case ISD::ADD:
1101 assert (DestType == MVT::i32 && "Only do arithmetic on i32s!");
1102 Tmp1 = SelectExpr(N.getOperand(0));
1103 switch(canUseAsImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
1104 default: assert(0 && "unhandled result code");
1105 case 0: // No immediate
1106 Tmp2 = SelectExpr(N.getOperand(1));
1107 BuildMI(BB, PPC::ADD, 2, Result).addReg(Tmp1).addReg(Tmp2);
1108 break;
1109 case 1: // Low immediate
1110 BuildMI(BB, PPC::ADDI, 2, Result).addReg(Tmp1).addSImm(Tmp2);
1111 break;
1112 case 2: // Shifted immediate
1113 BuildMI(BB, PPC::ADDIS, 2, Result).addReg(Tmp1).addSImm(Tmp2);
1114 break;
1115 }
1116 return Result;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001117
Nate Begemana9795f82005-03-24 04:41:43 +00001118 case ISD::AND:
1119 case ISD::OR:
1120 case ISD::XOR:
1121 assert (DestType == MVT::i32 && "Only do arithmetic on i32s!");
1122 Tmp1 = SelectExpr(N.getOperand(0));
1123 switch(canUseAsImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
1124 default: assert(0 && "unhandled result code");
1125 case 0: // No immediate
1126 Tmp2 = SelectExpr(N.getOperand(1));
1127 switch (opcode) {
Nate Begeman5e966612005-03-24 06:28:42 +00001128 case ISD::AND: Opc = PPC::AND; break;
1129 case ISD::OR: Opc = PPC::OR; break;
1130 case ISD::XOR: Opc = PPC::XOR; break;
Nate Begemana9795f82005-03-24 04:41:43 +00001131 }
Nate Begeman5e966612005-03-24 06:28:42 +00001132 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +00001133 break;
1134 case 1: // Low immediate
1135 switch (opcode) {
Nate Begeman5e966612005-03-24 06:28:42 +00001136 case ISD::AND: Opc = PPC::ANDIo; break;
1137 case ISD::OR: Opc = PPC::ORI; break;
1138 case ISD::XOR: Opc = PPC::XORI; break;
Nate Begemana9795f82005-03-24 04:41:43 +00001139 }
Nate Begeman5e966612005-03-24 06:28:42 +00001140 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +00001141 break;
1142 case 2: // Shifted immediate
1143 switch (opcode) {
Nate Begeman5e966612005-03-24 06:28:42 +00001144 case ISD::AND: Opc = PPC::ANDISo; break;
1145 case ISD::OR: Opc = PPC::ORIS; break;
1146 case ISD::XOR: Opc = PPC::XORIS; break;
Nate Begemana9795f82005-03-24 04:41:43 +00001147 }
Nate Begeman5e966612005-03-24 06:28:42 +00001148 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +00001149 break;
1150 }
1151 return Result;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001152
1153 case ISD::SUB:
1154 assert (DestType == MVT::i32 && "Only do arithmetic on i32s!");
1155 Tmp1 = SelectExpr(N.getOperand(0));
1156 Tmp2 = SelectExpr(N.getOperand(1));
1157 BuildMI(BB, PPC::SUBF, 2, Result).addReg(Tmp2).addReg(Tmp1);
1158 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001159
Nate Begeman5e966612005-03-24 06:28:42 +00001160 case ISD::MUL:
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001161 assert (DestType == MVT::i32 && "Only do arithmetic on i32s!");
1162 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman307e7442005-03-26 01:28:53 +00001163 if (1 == canUseAsImmediateForOpcode(N.getOperand(1), opcode, Tmp2))
1164 BuildMI(BB, PPC::MULLI, 2, Result).addReg(Tmp1).addSImm(Tmp2);
1165 else {
1166 Tmp2 = SelectExpr(N.getOperand(1));
1167 BuildMI(BB, PPC::MULLW, 2, Result).addReg(Tmp1).addReg(Tmp2);
1168 }
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001169 return Result;
1170
Nate Begemanf3d08f32005-03-29 00:03:27 +00001171 case ISD::SDIV:
1172 case ISD::UDIV:
1173 assert (DestType == MVT::i32 && "Only do arithmetic on i32s!");
1174 Tmp1 = SelectExpr(N.getOperand(0));
1175 Tmp2 = SelectExpr(N.getOperand(1));
1176 Opc = (ISD::UDIV == opcode) ? PPC::DIVWU : PPC::DIVW;
1177 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1178 return Result;
1179
1180 case ISD::UREM:
1181 case ISD::SREM: {
1182 assert (DestType == MVT::i32 && "Only do arithmetic on i32s!");
1183 Tmp1 = SelectExpr(N.getOperand(0));
1184 Tmp2 = SelectExpr(N.getOperand(1));
1185 Tmp3 = MakeReg(MVT::i32);
1186 unsigned Tmp4 = MakeReg(MVT::i32);
1187 Opc = (ISD::UREM == opcode) ? PPC::DIVWU : PPC::DIVW;
1188 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
1189 BuildMI(BB, PPC::MULLW, 2, Tmp4).addReg(Tmp3).addReg(Tmp2);
1190 BuildMI(BB, PPC::SUBF, 2, Result).addReg(Tmp4).addReg(Tmp1);
1191 return Result;
1192 }
1193
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001194 case ISD::ADD_PARTS:
Nate Begemanca12a2b2005-03-28 22:28:37 +00001195 case ISD::SUB_PARTS: {
1196 assert(N.getNumOperands() == 4 && N.getValueType() == MVT::i32 &&
1197 "Not an i64 add/sub!");
1198 // Emit all of the operands.
1199 std::vector<unsigned> InVals;
1200 for (unsigned i = 0, e = N.getNumOperands(); i != e; ++i)
1201 InVals.push_back(SelectExpr(N.getOperand(i)));
1202 if (N.getOpcode() == ISD::ADD_PARTS) {
Nate Begemanf70b5762005-03-28 23:08:54 +00001203 BuildMI(BB, PPC::ADDC, 2, Result+1).addReg(InVals[0]).addReg(InVals[2]);
1204 BuildMI(BB, PPC::ADDE, 2, Result).addReg(InVals[1]).addReg(InVals[3]);
Nate Begemanca12a2b2005-03-28 22:28:37 +00001205 } else {
Nate Begemanf70b5762005-03-28 23:08:54 +00001206 BuildMI(BB, PPC::SUBFC, 2, Result+1).addReg(InVals[2]).addReg(InVals[0]);
1207 BuildMI(BB, PPC::SUBFE, 2, Result).addReg(InVals[3]).addReg(InVals[1]);
Nate Begemanca12a2b2005-03-28 22:28:37 +00001208 }
1209 return Result+N.ResNo;
1210 }
1211
Nate Begemana9795f82005-03-24 04:41:43 +00001212 case ISD::FP_TO_UINT:
1213 case ISD::FP_TO_SINT:
Nate Begeman01d05262005-03-30 01:45:43 +00001214 assert(0 && "FP_TO_S/UINT unimplemented");
Nate Begemana9795f82005-03-24 04:41:43 +00001215 abort();
1216
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001217 case ISD::SETCC:
Nate Begeman33162522005-03-29 21:54:38 +00001218 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Node)) {
Nate Begemandffcfcc2005-04-01 00:32:34 +00001219 Opc = SelectSetCR0(N);
Nate Begeman33162522005-03-29 21:54:38 +00001220
1221 // Create an iterator with which to insert the MBB for copying the false
1222 // value and the MBB to hold the PHI instruction for this SetCC.
1223 MachineBasicBlock *thisMBB = BB;
1224 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1225 ilist<MachineBasicBlock>::iterator It = BB;
1226 ++It;
1227
1228 // thisMBB:
1229 // ...
1230 // cmpTY cr0, r1, r2
1231 // %TrueValue = li 1
1232 // bCC sinkMBB
Nate Begeman33162522005-03-29 21:54:38 +00001233 unsigned TrueValue = MakeReg(MVT::i32);
1234 BuildMI(BB, PPC::LI, 1, TrueValue).addSImm(1);
1235 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
1236 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
1237 BuildMI(BB, Opc, 2).addReg(PPC::CR0).addMBB(sinkMBB);
1238 MachineFunction *F = BB->getParent();
1239 F->getBasicBlockList().insert(It, copy0MBB);
1240 F->getBasicBlockList().insert(It, sinkMBB);
1241 // Update machine-CFG edges
1242 BB->addSuccessor(copy0MBB);
1243 BB->addSuccessor(sinkMBB);
1244
1245 // copy0MBB:
1246 // %FalseValue = li 0
1247 // fallthrough
1248 BB = copy0MBB;
1249 unsigned FalseValue = MakeReg(MVT::i32);
1250 BuildMI(BB, PPC::LI, 1, FalseValue).addSImm(0);
1251 // Update machine-CFG edges
1252 BB->addSuccessor(sinkMBB);
1253
1254 // sinkMBB:
1255 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1256 // ...
1257 BB = sinkMBB;
1258 BuildMI(BB, PPC::PHI, 4, Result).addReg(FalseValue)
1259 .addMBB(copy0MBB).addReg(TrueValue).addMBB(thisMBB);
1260 return Result;
1261 }
1262 assert(0 && "Is this legal?");
1263 return 0;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001264
Nate Begeman74747862005-03-29 22:24:51 +00001265 case ISD::SELECT: {
Nate Begemandffcfcc2005-04-01 00:32:34 +00001266 Opc = SelectSetCR0(N.getOperand(0));
1267
Nate Begeman74747862005-03-29 22:24:51 +00001268 // Create an iterator with which to insert the MBB for copying the false
1269 // value and the MBB to hold the PHI instruction for this SetCC.
1270 MachineBasicBlock *thisMBB = BB;
1271 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1272 ilist<MachineBasicBlock>::iterator It = BB;
1273 ++It;
1274
1275 // thisMBB:
1276 // ...
1277 // TrueVal = ...
1278 // cmpTY cr0, r1, r2
1279 // bCC copy1MBB
1280 // fallthrough --> copy0MBB
Nate Begeman74747862005-03-29 22:24:51 +00001281 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
1282 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
1283 unsigned TrueValue = SelectExpr(N.getOperand(1)); //Use if TRUE
Nate Begeman3e897162005-03-31 23:55:40 +00001284 BuildMI(BB, Opc, 2).addReg(PPC::CR0).addMBB(sinkMBB);
Nate Begeman74747862005-03-29 22:24:51 +00001285 MachineFunction *F = BB->getParent();
1286 F->getBasicBlockList().insert(It, copy0MBB);
1287 F->getBasicBlockList().insert(It, sinkMBB);
1288 // Update machine-CFG edges
1289 BB->addSuccessor(copy0MBB);
1290 BB->addSuccessor(sinkMBB);
1291
1292 // copy0MBB:
1293 // %FalseValue = ...
1294 // # fallthrough to sinkMBB
1295 BB = copy0MBB;
1296 unsigned FalseValue = SelectExpr(N.getOperand(2)); //Use if FALSE
1297 // Update machine-CFG edges
1298 BB->addSuccessor(sinkMBB);
1299
1300 // sinkMBB:
1301 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1302 // ...
1303 BB = sinkMBB;
1304 BuildMI(BB, PPC::PHI, 4, Result).addReg(FalseValue)
1305 .addMBB(copy0MBB).addReg(TrueValue).addMBB(thisMBB);
1306
1307 // FIXME: Select i64?
1308 return Result;
1309 }
Nate Begemana9795f82005-03-24 04:41:43 +00001310
1311 case ISD::Constant:
1312 switch (N.getValueType()) {
1313 default: assert(0 && "Cannot use constants of this type!");
1314 case MVT::i1:
1315 BuildMI(BB, PPC::LI, 1, Result)
1316 .addSImm(!cast<ConstantSDNode>(N)->isNullValue());
1317 break;
1318 case MVT::i32:
1319 {
1320 int v = (int)cast<ConstantSDNode>(N)->getSignExtended();
1321 if (v < 32768 && v >= -32768) {
1322 BuildMI(BB, PPC::LI, 1, Result).addSImm(v);
1323 } else {
Nate Begeman5e966612005-03-24 06:28:42 +00001324 Tmp1 = MakeReg(MVT::i32);
1325 BuildMI(BB, PPC::LIS, 1, Tmp1).addSImm(v >> 16);
1326 BuildMI(BB, PPC::ORI, 2, Result).addReg(Tmp1).addImm(v & 0xFFFF);
Nate Begemana9795f82005-03-24 04:41:43 +00001327 }
1328 }
1329 }
1330 return Result;
1331 }
1332
1333 return 0;
1334}
1335
1336void ISel::Select(SDOperand N) {
1337 unsigned Tmp1, Tmp2, Opc;
1338 unsigned opcode = N.getOpcode();
1339
1340 if (!ExprMap.insert(std::make_pair(N, 1)).second)
1341 return; // Already selected.
1342
1343 SDNode *Node = N.Val;
1344
1345 switch (Node->getOpcode()) {
1346 default:
1347 Node->dump(); std::cerr << "\n";
1348 assert(0 && "Node not handled yet!");
1349 case ISD::EntryToken: return; // Noop
1350 case ISD::TokenFactor:
1351 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1352 Select(Node->getOperand(i));
1353 return;
1354 case ISD::ADJCALLSTACKDOWN:
1355 case ISD::ADJCALLSTACKUP:
1356 Select(N.getOperand(0));
1357 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1358 Opc = N.getOpcode() == ISD::ADJCALLSTACKDOWN ? PPC::ADJCALLSTACKDOWN :
1359 PPC::ADJCALLSTACKUP;
1360 BuildMI(BB, Opc, 1).addImm(Tmp1);
1361 return;
1362 case ISD::BR: {
1363 MachineBasicBlock *Dest =
1364 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
Nate Begemana9795f82005-03-24 04:41:43 +00001365 Select(N.getOperand(0));
1366 BuildMI(BB, PPC::B, 1).addMBB(Dest);
1367 return;
1368 }
1369 case ISD::BRCOND:
1370 SelectBranchCC(N);
1371 return;
1372 case ISD::CopyToReg:
1373 Select(N.getOperand(0));
1374 Tmp1 = SelectExpr(N.getOperand(1));
1375 Tmp2 = cast<RegSDNode>(N)->getReg();
1376
1377 if (Tmp1 != Tmp2) {
1378 if (N.getOperand(1).getValueType() == MVT::f64 ||
1379 N.getOperand(1).getValueType() == MVT::f32)
1380 BuildMI(BB, PPC::FMR, 1, Tmp2).addReg(Tmp1);
1381 else
1382 BuildMI(BB, PPC::OR, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
1383 }
1384 return;
1385 case ISD::ImplicitDef:
1386 Select(N.getOperand(0));
1387 BuildMI(BB, PPC::IMPLICIT_DEF, 0, cast<RegSDNode>(N)->getReg());
1388 return;
1389 case ISD::RET:
1390 switch (N.getNumOperands()) {
1391 default:
1392 assert(0 && "Unknown return instruction!");
1393 case 3:
1394 assert(N.getOperand(1).getValueType() == MVT::i32 &&
1395 N.getOperand(2).getValueType() == MVT::i32 &&
1396 "Unknown two-register value!");
1397 Select(N.getOperand(0));
1398 Tmp1 = SelectExpr(N.getOperand(1));
1399 Tmp2 = SelectExpr(N.getOperand(2));
1400 BuildMI(BB, PPC::OR, 2, PPC::R3).addReg(Tmp1).addReg(Tmp1);
1401 BuildMI(BB, PPC::OR, 2, PPC::R4).addReg(Tmp2).addReg(Tmp2);
1402 break;
1403 case 2:
1404 Select(N.getOperand(0));
1405 Tmp1 = SelectExpr(N.getOperand(1));
1406 switch (N.getOperand(1).getValueType()) {
1407 default:
1408 assert(0 && "Unknown return type!");
1409 case MVT::f64:
1410 case MVT::f32:
1411 BuildMI(BB, PPC::FMR, 1, PPC::F1).addReg(Tmp1);
1412 break;
1413 case MVT::i32:
1414 BuildMI(BB, PPC::OR, 2, PPC::R3).addReg(Tmp1).addReg(Tmp1);
1415 break;
1416 }
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001417 case 1:
1418 Select(N.getOperand(0));
1419 break;
Nate Begemana9795f82005-03-24 04:41:43 +00001420 }
1421 BuildMI(BB, PPC::BLR, 0); // Just emit a 'ret' instruction
1422 return;
Nate Begemana9795f82005-03-24 04:41:43 +00001423 case ISD::TRUNCSTORE:
1424 case ISD::STORE:
1425 {
1426 SDOperand Chain = N.getOperand(0);
1427 SDOperand Value = N.getOperand(1);
1428 SDOperand Address = N.getOperand(2);
1429 Select(Chain);
1430
1431 Tmp1 = SelectExpr(Value); //value
1432
1433 if (opcode == ISD::STORE) {
1434 switch(Value.getValueType()) {
1435 default: assert(0 && "unknown Type in store");
1436 case MVT::i32: Opc = PPC::STW; break;
1437 case MVT::f64: Opc = PPC::STFD; break;
1438 case MVT::f32: Opc = PPC::STFS; break;
1439 }
1440 } else { //ISD::TRUNCSTORE
1441 switch(cast<MVTSDNode>(Node)->getExtraValueType()) {
1442 default: assert(0 && "unknown Type in store");
1443 case MVT::i1: //FIXME: DAG does not promote this load
1444 case MVT::i8: Opc = PPC::STB; break;
1445 case MVT::i16: Opc = PPC::STH; break;
1446 }
1447 }
1448
1449 if (Address.getOpcode() == ISD::GlobalAddress)
1450 {
1451 BuildMI(BB, Opc, 2).addReg(Tmp1)
1452 .addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
1453 }
1454 else if(Address.getOpcode() == ISD::FrameIndex)
1455 {
Nate Begeman58f718c2005-03-30 02:23:08 +00001456 Tmp2 = cast<FrameIndexSDNode>(Address)->getIndex();
1457 addFrameReference(BuildMI(BB, Opc, 3).addReg(Tmp1), (int)Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +00001458 }
1459 else
1460 {
1461 int offset;
1462 SelectAddr(Address, Tmp2, offset);
1463 BuildMI(BB, Opc, 3).addReg(Tmp1).addImm(offset).addReg(Tmp2);
1464 }
1465 return;
1466 }
1467 case ISD::EXTLOAD:
1468 case ISD::SEXTLOAD:
1469 case ISD::ZEXTLOAD:
1470 case ISD::LOAD:
1471 case ISD::CopyFromReg:
1472 case ISD::CALL:
1473 case ISD::DYNAMIC_STACKALLOC:
1474 ExprMap.erase(N);
1475 SelectExpr(N);
1476 return;
1477 }
1478 assert(0 && "Should not be reached!");
1479}
1480
1481
1482/// createPPC32PatternInstructionSelector - This pass converts an LLVM function
1483/// into a machine code representation using pattern matching and a machine
1484/// description file.
1485///
1486FunctionPass *llvm::createPPC32ISelPattern(TargetMachine &TM) {
1487 return new ISel(TM);
Chris Lattner246fa632005-03-24 06:16:18 +00001488}
1489