blob: fd4d89db2f33d3e683d2c0f84421d07656dff825 [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 Begemana9795f82005-03-24 04:41:43 +0000763 case ISD::ConstantFP:
Nate Begemanca12a2b2005-03-28 22:28:37 +0000764 assert(0 && "ISD::ConstantFP Unimplemented");
Nate Begemana9795f82005-03-24 04:41:43 +0000765 abort();
766
767 case ISD::MUL:
768 case ISD::ADD:
769 case ISD::SUB:
770 case ISD::SDIV:
771 switch( opcode ) {
772 case ISD::MUL: Opc = DestType == MVT::f64 ? PPC::FMUL : PPC::FMULS; break;
773 case ISD::ADD: Opc = DestType == MVT::f64 ? PPC::FADD : PPC::FADDS; break;
774 case ISD::SUB: Opc = DestType == MVT::f64 ? PPC::FSUB : PPC::FSUBS; break;
775 case ISD::SDIV: Opc = DestType == MVT::f64 ? PPC::FDIV : PPC::FDIVS; break;
776 };
Nate Begemana9795f82005-03-24 04:41:43 +0000777 Tmp1 = SelectExpr(N.getOperand(0));
778 Tmp2 = SelectExpr(N.getOperand(1));
779 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
780 return Result;
781
Nate Begemana9795f82005-03-24 04:41:43 +0000782 case ISD::UINT_TO_FP:
Nate Begemanfdcf3412005-03-30 19:38:35 +0000783 case ISD::SINT_TO_FP: {
784 assert (N.getOperand(0).getValueType() == MVT::i32
785 && "int to float must operate on i32");
786 bool IsUnsigned = (ISD::UINT_TO_FP == opcode);
787 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
788 Tmp2 = MakeReg(MVT::f64); // temp reg to load the integer value into
789 Tmp3 = MakeReg(MVT::i32); // temp reg to hold the conversion constant
790 unsigned ConstF = MakeReg(MVT::f64); // temp reg to hold the fp constant
791
792 int FrameIdx = BB->getParent()->getFrameInfo()->CreateStackObject(8, 8);
793 MachineConstantPool *CP = BB->getParent()->getConstantPool();
794
795 // FIXME: pull this FP constant generation stuff out into something like
796 // the simple ISel's getReg.
797 if (IsUnsigned) {
798 ConstantFP *CFP = ConstantFP::get(Type::DoubleTy, 0x1.000000p52);
799 unsigned CPI = CP->getConstantPoolIndex(CFP);
800 // Load constant fp value
801 unsigned Tmp4 = MakeReg(MVT::i32);
802 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp4).addReg(getGlobalBaseReg())
803 .addConstantPoolIndex(CPI);
804 BuildMI(BB, PPC::LFD, 2, ConstF).addConstantPoolIndex(CPI).addReg(Tmp4);
805 // Store the hi & low halves of the fp value, currently in int regs
806 BuildMI(BB, PPC::LIS, 1, Tmp3).addSImm(0x4330);
807 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(Tmp3), FrameIdx);
808 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(Tmp1), FrameIdx, 4);
809 addFrameReference(BuildMI(BB, PPC::LFD, 2, Tmp2), FrameIdx);
810 // Generate the return value with a subtract
811 BuildMI(BB, PPC::FSUB, 2, Result).addReg(Tmp2).addReg(ConstF);
812 } else {
813 ConstantFP *CFP = ConstantFP::get(Type::DoubleTy, 0x1.000008p52);
814 unsigned CPI = CP->getConstantPoolIndex(CFP);
815 // Load constant fp value
816 unsigned Tmp4 = MakeReg(MVT::i32);
817 unsigned TmpL = MakeReg(MVT::i32);
818 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp4).addReg(getGlobalBaseReg())
819 .addConstantPoolIndex(CPI);
820 BuildMI(BB, PPC::LFD, 2, ConstF).addConstantPoolIndex(CPI).addReg(Tmp4);
821 // Store the hi & low halves of the fp value, currently in int regs
822 BuildMI(BB, PPC::LIS, 1, Tmp3).addSImm(0x4330);
823 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(Tmp3), FrameIdx);
824 BuildMI(BB, PPC::XORIS, 2, TmpL).addReg(Tmp1).addImm(0x8000);
825 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(TmpL), FrameIdx, 4);
826 addFrameReference(BuildMI(BB, PPC::LFD, 2, Tmp2), FrameIdx);
827 // Generate the return value with a subtract
828 BuildMI(BB, PPC::FSUB, 2, Result).addReg(Tmp2).addReg(ConstF);
829 }
830 return Result;
831 }
Nate Begemana9795f82005-03-24 04:41:43 +0000832 }
833 assert(0 && "should not get here");
834 return 0;
835}
836
837unsigned ISel::SelectExpr(SDOperand N) {
838 unsigned Result;
839 unsigned Tmp1, Tmp2, Tmp3;
840 unsigned Opc = 0;
841 unsigned opcode = N.getOpcode();
842
843 SDNode *Node = N.Val;
844 MVT::ValueType DestType = N.getValueType();
845
846 unsigned &Reg = ExprMap[N];
847 if (Reg) return Reg;
848
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000849 if (N.getOpcode() != ISD::CALL && N.getOpcode() != ISD::ADD_PARTS &&
850 N.getOpcode() != ISD::SUB_PARTS)
Nate Begemana9795f82005-03-24 04:41:43 +0000851 Reg = Result = (N.getValueType() != MVT::Other) ?
852 MakeReg(N.getValueType()) : 1;
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000853 else {
854 // If this is a call instruction, make sure to prepare ALL of the result
855 // values as well as the chain.
856 if (N.getOpcode() == ISD::CALL) {
857 if (Node->getNumValues() == 1)
858 Reg = Result = 1; // Void call, just a chain.
859 else {
860 Result = MakeReg(Node->getValueType(0));
861 ExprMap[N.getValue(0)] = Result;
862 for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
863 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
864 ExprMap[SDOperand(Node, Node->getNumValues()-1)] = 1;
865 }
866 } else {
867 Result = MakeReg(Node->getValueType(0));
868 ExprMap[N.getValue(0)] = Result;
869 for (unsigned i = 1, e = N.Val->getNumValues(); i != e; ++i)
870 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
871 }
872 }
873
874 if (DestType == MVT::f64 || DestType == MVT::f32)
Nate Begeman74d73452005-03-31 00:15:26 +0000875 if (ISD::LOAD != opcode && ISD::EXTLOAD != opcode)
876 return SelectExprFP(N, Result);
Nate Begemana9795f82005-03-24 04:41:43 +0000877
878 switch (opcode) {
879 default:
880 Node->dump();
881 assert(0 && "Node not handled!\n");
882
883 case ISD::DYNAMIC_STACKALLOC:
Nate Begeman5e966612005-03-24 06:28:42 +0000884 // Generate both result values. FIXME: Need a better commment here?
885 if (Result != 1)
886 ExprMap[N.getValue(1)] = 1;
887 else
888 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
889
890 // FIXME: We are currently ignoring the requested alignment for handling
891 // greater than the stack alignment. This will need to be revisited at some
892 // point. Align = N.getOperand(2);
893 if (!isa<ConstantSDNode>(N.getOperand(2)) ||
894 cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) {
895 std::cerr << "Cannot allocate stack object with greater alignment than"
896 << " the stack alignment yet!";
897 abort();
898 }
899 Select(N.getOperand(0));
900 Tmp1 = SelectExpr(N.getOperand(1));
901 // Subtract size from stack pointer, thereby allocating some space.
902 BuildMI(BB, PPC::SUBF, 2, PPC::R1).addReg(Tmp1).addReg(PPC::R1);
903 // Put a pointer to the space into the result register by copying the SP
904 BuildMI(BB, PPC::OR, 2, Result).addReg(PPC::R1).addReg(PPC::R1);
905 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +0000906
907 case ISD::ConstantPool:
Nate Begemanca12a2b2005-03-28 22:28:37 +0000908 Tmp1 = cast<ConstantPoolSDNode>(N)->getIndex();
909 Tmp2 = MakeReg(MVT::i32);
910 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp2).addReg(getGlobalBaseReg())
911 .addConstantPoolIndex(Tmp1);
912 BuildMI(BB, PPC::LA, 2, Result).addReg(Tmp2).addConstantPoolIndex(Tmp1);
913 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +0000914
915 case ISD::FrameIndex:
Nate Begemanf3d08f32005-03-29 00:03:27 +0000916 Tmp1 = cast<FrameIndexSDNode>(N)->getIndex();
Nate Begeman58f718c2005-03-30 02:23:08 +0000917 addFrameReference(BuildMI(BB, PPC::ADDI, 2, Result), (int)Tmp1, 0, false);
Nate Begemanf3d08f32005-03-29 00:03:27 +0000918 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +0000919
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000920 case ISD::GlobalAddress: {
921 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
Nate Begemanca12a2b2005-03-28 22:28:37 +0000922 Tmp1 = MakeReg(MVT::i32);
Nate Begemanc7b09f12005-03-25 08:34:25 +0000923 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp1).addReg(getGlobalBaseReg())
924 .addGlobalAddress(GV);
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000925 if (GV->hasWeakLinkage() || GV->isExternal()) {
926 BuildMI(BB, PPC::LWZ, 2, Result).addGlobalAddress(GV).addReg(Tmp1);
927 } else {
928 BuildMI(BB, PPC::LA, 2, Result).addReg(Tmp1).addGlobalAddress(GV);
929 }
930 return Result;
931 }
932
Nate Begeman5e966612005-03-24 06:28:42 +0000933 case ISD::LOAD:
Nate Begemana9795f82005-03-24 04:41:43 +0000934 case ISD::EXTLOAD:
935 case ISD::ZEXTLOAD:
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000936 case ISD::SEXTLOAD: {
Nate Begeman9db505c2005-03-28 19:36:43 +0000937 MVT::ValueType TypeBeingLoaded = (ISD::LOAD == opcode) ?
938 Node->getValueType(0) : cast<MVTSDNode>(Node)->getExtraValueType();
Nate Begeman74d73452005-03-31 00:15:26 +0000939 bool sext = (ISD::SEXTLOAD == opcode);
940 bool byte = (MVT::i8 == TypeBeingLoaded);
941
Nate Begeman5e966612005-03-24 06:28:42 +0000942 // Make sure we generate both values.
943 if (Result != 1)
944 ExprMap[N.getValue(1)] = 1; // Generate the token
945 else
946 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
947
948 SDOperand Chain = N.getOperand(0);
949 SDOperand Address = N.getOperand(1);
950 Select(Chain);
951
Nate Begeman9db505c2005-03-28 19:36:43 +0000952 switch (TypeBeingLoaded) {
Nate Begeman74d73452005-03-31 00:15:26 +0000953 default: Node->dump(); assert(0 && "Cannot load this type!");
Nate Begeman9db505c2005-03-28 19:36:43 +0000954 case MVT::i1: Opc = PPC::LBZ; break;
955 case MVT::i8: Opc = PPC::LBZ; break;
956 case MVT::i16: Opc = sext ? PPC::LHA : PPC::LHZ; break;
957 case MVT::i32: Opc = PPC::LWZ; break;
Nate Begeman74d73452005-03-31 00:15:26 +0000958 case MVT::f32: Opc = PPC::LFS; break;
959 case MVT::f64: Opc = PPC::LFD; break;
Nate Begeman5e966612005-03-24 06:28:42 +0000960 }
961
Nate Begeman74d73452005-03-31 00:15:26 +0000962 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Address)) {
963 Tmp1 = MakeReg(MVT::i32);
964 int CPI = CP->getIndex();
965 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp1).addReg(getGlobalBaseReg())
966 .addConstantPoolIndex(CPI);
967 BuildMI(BB, Opc, 2, Result).addConstantPoolIndex(CPI).addReg(Tmp1);
Nate Begeman9db505c2005-03-28 19:36:43 +0000968 }
Nate Begeman74d73452005-03-31 00:15:26 +0000969 else if(Address.getOpcode() == ISD::FrameIndex) {
Nate Begeman58f718c2005-03-30 02:23:08 +0000970 Tmp1 = cast<FrameIndexSDNode>(Address)->getIndex();
971 addFrameReference(BuildMI(BB, Opc, 2, Result), (int)Tmp1);
Nate Begeman5e966612005-03-24 06:28:42 +0000972 } else {
973 int offset;
974 SelectAddr(Address, Tmp1, offset);
975 BuildMI(BB, Opc, 2, Result).addSImm(offset).addReg(Tmp1);
976 }
977 return Result;
978 }
979
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000980 case ISD::CALL: {
981 // Lower the chain for this call.
982 Select(N.getOperand(0));
983 ExprMap[N.getValue(Node->getNumValues()-1)] = 1;
Nate Begeman74d73452005-03-31 00:15:26 +0000984
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000985 for(int i = 2, e = Node->getNumOperands(); i < e; ++i)
Nate Begeman74d73452005-03-31 00:15:26 +0000986 Select(N.getOperand(i));
987
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000988 // Emit the correct call instruction based on the type of symbol called.
989 if (GlobalAddressSDNode *GASD =
990 dyn_cast<GlobalAddressSDNode>(N.getOperand(1))) {
991 BuildMI(BB, PPC::CALLpcrel, 1).addGlobalAddress(GASD->getGlobal(), true);
992 } else if (ExternalSymbolSDNode *ESSDN =
993 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1))) {
994 BuildMI(BB, PPC::CALLpcrel, 1).addExternalSymbol(ESSDN->getSymbol(), true);
995 } else {
996 Tmp1 = SelectExpr(N.getOperand(1));
997 BuildMI(BB, PPC::OR, 2, PPC::R12).addReg(Tmp1).addReg(Tmp1);
998 BuildMI(BB, PPC::MTCTR, 1).addReg(PPC::R12);
999 BuildMI(BB, PPC::CALLindirect, 3).addImm(20).addImm(0).addReg(PPC::R12);
1000 }
1001
1002 switch (Node->getValueType(0)) {
1003 default: assert(0 && "Unknown value type for call result!");
1004 case MVT::Other: return 1;
1005 case MVT::i1:
1006 case MVT::i8:
1007 case MVT::i16:
1008 case MVT::i32:
Nate Begemanc7b09f12005-03-25 08:34:25 +00001009 BuildMI(BB, PPC::OR, 2, Result).addReg(PPC::R3).addReg(PPC::R3);
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001010 if (Node->getValueType(1) == MVT::i32)
Nate Begemanc7b09f12005-03-25 08:34:25 +00001011 BuildMI(BB, PPC::OR, 2, Result+1).addReg(PPC::R4).addReg(PPC::R4);
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001012 break;
1013 case MVT::f32:
1014 case MVT::f64:
1015 BuildMI(BB, PPC::FMR, 1, Result).addReg(PPC::F1);
1016 break;
1017 }
1018 return Result+N.ResNo;
1019 }
Nate Begemana9795f82005-03-24 04:41:43 +00001020
1021 case ISD::SIGN_EXTEND:
1022 case ISD::SIGN_EXTEND_INREG:
1023 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman9db505c2005-03-28 19:36:43 +00001024 switch(cast<MVTSDNode>(Node)->getExtraValueType()) {
1025 default: Node->dump(); assert(0 && "Unhandled SIGN_EXTEND type"); break;
1026 case MVT::i16:
1027 BuildMI(BB, PPC::EXTSH, 1, Result).addReg(Tmp1);
1028 break;
1029 case MVT::i8:
1030 BuildMI(BB, PPC::EXTSB, 1, Result).addReg(Tmp1);
1031 break;
Nate Begeman74747862005-03-29 22:24:51 +00001032 case MVT::i1:
1033 BuildMI(BB, PPC::SUBFIC, 2, Result).addReg(Tmp1).addSImm(0);
1034 break;
Nate Begeman9db505c2005-03-28 19:36:43 +00001035 }
Nate Begemana9795f82005-03-24 04:41:43 +00001036 return Result;
1037
1038 case ISD::ZERO_EXTEND_INREG:
1039 Tmp1 = SelectExpr(N.getOperand(0));
1040 switch(cast<MVTSDNode>(Node)->getExtraValueType()) {
Nate Begeman9db505c2005-03-28 19:36:43 +00001041 default: Node->dump(); assert(0 && "Unhandled ZERO_EXTEND type"); break;
Nate Begemana9795f82005-03-24 04:41:43 +00001042 case MVT::i16: Tmp2 = 16; break;
1043 case MVT::i8: Tmp2 = 24; break;
1044 case MVT::i1: Tmp2 = 31; break;
1045 }
Nate Begeman33162522005-03-29 21:54:38 +00001046 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(0).addImm(Tmp2)
1047 .addImm(31);
Nate Begemana9795f82005-03-24 04:41:43 +00001048 return Result;
1049
Nate Begemana9795f82005-03-24 04:41:43 +00001050 case ISD::CopyFromReg:
1051 if (Result == 1)
1052 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1053 Tmp1 = dyn_cast<RegSDNode>(Node)->getReg();
1054 BuildMI(BB, PPC::OR, 2, Result).addReg(Tmp1).addReg(Tmp1);
1055 return Result;
1056
1057 case ISD::SHL:
Nate Begeman5e966612005-03-24 06:28:42 +00001058 Tmp1 = SelectExpr(N.getOperand(0));
1059 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1060 Tmp2 = CN->getValue() & 0x1F;
Nate Begeman33162522005-03-29 21:54:38 +00001061 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(Tmp2).addImm(0)
Nate Begeman5e966612005-03-24 06:28:42 +00001062 .addImm(31-Tmp2);
1063 } else {
1064 Tmp2 = SelectExpr(N.getOperand(1));
1065 BuildMI(BB, PPC::SLW, 2, Result).addReg(Tmp1).addReg(Tmp2);
1066 }
1067 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001068
Nate Begeman5e966612005-03-24 06:28:42 +00001069 case ISD::SRL:
1070 Tmp1 = SelectExpr(N.getOperand(0));
1071 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1072 Tmp2 = CN->getValue() & 0x1F;
Nate Begeman33162522005-03-29 21:54:38 +00001073 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(32-Tmp2)
Nate Begeman5e966612005-03-24 06:28:42 +00001074 .addImm(Tmp2).addImm(31);
1075 } else {
1076 Tmp2 = SelectExpr(N.getOperand(1));
1077 BuildMI(BB, PPC::SRW, 2, Result).addReg(Tmp1).addReg(Tmp2);
1078 }
1079 return Result;
1080
1081 case ISD::SRA:
1082 Tmp1 = SelectExpr(N.getOperand(0));
1083 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1084 Tmp2 = CN->getValue() & 0x1F;
1085 BuildMI(BB, PPC::SRAWI, 2, Result).addReg(Tmp1).addImm(Tmp2);
1086 } else {
1087 Tmp2 = SelectExpr(N.getOperand(1));
1088 BuildMI(BB, PPC::SRAW, 2, Result).addReg(Tmp1).addReg(Tmp2);
1089 }
1090 return Result;
1091
Nate Begemana9795f82005-03-24 04:41:43 +00001092 case ISD::ADD:
1093 assert (DestType == MVT::i32 && "Only do arithmetic on i32s!");
1094 Tmp1 = SelectExpr(N.getOperand(0));
1095 switch(canUseAsImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
1096 default: assert(0 && "unhandled result code");
1097 case 0: // No immediate
1098 Tmp2 = SelectExpr(N.getOperand(1));
1099 BuildMI(BB, PPC::ADD, 2, Result).addReg(Tmp1).addReg(Tmp2);
1100 break;
1101 case 1: // Low immediate
1102 BuildMI(BB, PPC::ADDI, 2, Result).addReg(Tmp1).addSImm(Tmp2);
1103 break;
1104 case 2: // Shifted immediate
1105 BuildMI(BB, PPC::ADDIS, 2, Result).addReg(Tmp1).addSImm(Tmp2);
1106 break;
1107 }
1108 return Result;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001109
Nate Begemana9795f82005-03-24 04:41:43 +00001110 case ISD::AND:
1111 case ISD::OR:
1112 case ISD::XOR:
1113 assert (DestType == MVT::i32 && "Only do arithmetic on i32s!");
1114 Tmp1 = SelectExpr(N.getOperand(0));
1115 switch(canUseAsImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
1116 default: assert(0 && "unhandled result code");
1117 case 0: // No immediate
1118 Tmp2 = SelectExpr(N.getOperand(1));
1119 switch (opcode) {
Nate Begeman5e966612005-03-24 06:28:42 +00001120 case ISD::AND: Opc = PPC::AND; break;
1121 case ISD::OR: Opc = PPC::OR; break;
1122 case ISD::XOR: Opc = PPC::XOR; break;
Nate Begemana9795f82005-03-24 04:41:43 +00001123 }
Nate Begeman5e966612005-03-24 06:28:42 +00001124 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +00001125 break;
1126 case 1: // Low immediate
1127 switch (opcode) {
Nate Begeman5e966612005-03-24 06:28:42 +00001128 case ISD::AND: Opc = PPC::ANDIo; break;
1129 case ISD::OR: Opc = PPC::ORI; break;
1130 case ISD::XOR: Opc = PPC::XORI; break;
Nate Begemana9795f82005-03-24 04:41:43 +00001131 }
Nate Begeman5e966612005-03-24 06:28:42 +00001132 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +00001133 break;
1134 case 2: // Shifted immediate
1135 switch (opcode) {
Nate Begeman5e966612005-03-24 06:28:42 +00001136 case ISD::AND: Opc = PPC::ANDISo; break;
1137 case ISD::OR: Opc = PPC::ORIS; break;
1138 case ISD::XOR: Opc = PPC::XORIS; 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 }
1143 return Result;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001144
1145 case ISD::SUB:
1146 assert (DestType == MVT::i32 && "Only do arithmetic on i32s!");
1147 Tmp1 = SelectExpr(N.getOperand(0));
1148 Tmp2 = SelectExpr(N.getOperand(1));
1149 BuildMI(BB, PPC::SUBF, 2, Result).addReg(Tmp2).addReg(Tmp1);
1150 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001151
Nate Begeman5e966612005-03-24 06:28:42 +00001152 case ISD::MUL:
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001153 assert (DestType == MVT::i32 && "Only do arithmetic on i32s!");
1154 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman307e7442005-03-26 01:28:53 +00001155 if (1 == canUseAsImmediateForOpcode(N.getOperand(1), opcode, Tmp2))
1156 BuildMI(BB, PPC::MULLI, 2, Result).addReg(Tmp1).addSImm(Tmp2);
1157 else {
1158 Tmp2 = SelectExpr(N.getOperand(1));
1159 BuildMI(BB, PPC::MULLW, 2, Result).addReg(Tmp1).addReg(Tmp2);
1160 }
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001161 return Result;
1162
Nate Begemanf3d08f32005-03-29 00:03:27 +00001163 case ISD::SDIV:
1164 case ISD::UDIV:
1165 assert (DestType == MVT::i32 && "Only do arithmetic on i32s!");
1166 Tmp1 = SelectExpr(N.getOperand(0));
1167 Tmp2 = SelectExpr(N.getOperand(1));
1168 Opc = (ISD::UDIV == opcode) ? PPC::DIVWU : PPC::DIVW;
1169 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1170 return Result;
1171
1172 case ISD::UREM:
1173 case ISD::SREM: {
1174 assert (DestType == MVT::i32 && "Only do arithmetic on i32s!");
1175 Tmp1 = SelectExpr(N.getOperand(0));
1176 Tmp2 = SelectExpr(N.getOperand(1));
1177 Tmp3 = MakeReg(MVT::i32);
1178 unsigned Tmp4 = MakeReg(MVT::i32);
1179 Opc = (ISD::UREM == opcode) ? PPC::DIVWU : PPC::DIVW;
1180 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
1181 BuildMI(BB, PPC::MULLW, 2, Tmp4).addReg(Tmp3).addReg(Tmp2);
1182 BuildMI(BB, PPC::SUBF, 2, Result).addReg(Tmp4).addReg(Tmp1);
1183 return Result;
1184 }
1185
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001186 case ISD::ADD_PARTS:
Nate Begemanca12a2b2005-03-28 22:28:37 +00001187 case ISD::SUB_PARTS: {
1188 assert(N.getNumOperands() == 4 && N.getValueType() == MVT::i32 &&
1189 "Not an i64 add/sub!");
1190 // Emit all of the operands.
1191 std::vector<unsigned> InVals;
1192 for (unsigned i = 0, e = N.getNumOperands(); i != e; ++i)
1193 InVals.push_back(SelectExpr(N.getOperand(i)));
1194 if (N.getOpcode() == ISD::ADD_PARTS) {
Nate Begemanf70b5762005-03-28 23:08:54 +00001195 BuildMI(BB, PPC::ADDC, 2, Result+1).addReg(InVals[0]).addReg(InVals[2]);
1196 BuildMI(BB, PPC::ADDE, 2, Result).addReg(InVals[1]).addReg(InVals[3]);
Nate Begemanca12a2b2005-03-28 22:28:37 +00001197 } else {
Nate Begemanf70b5762005-03-28 23:08:54 +00001198 BuildMI(BB, PPC::SUBFC, 2, Result+1).addReg(InVals[2]).addReg(InVals[0]);
1199 BuildMI(BB, PPC::SUBFE, 2, Result).addReg(InVals[3]).addReg(InVals[1]);
Nate Begemanca12a2b2005-03-28 22:28:37 +00001200 }
1201 return Result+N.ResNo;
1202 }
1203
Nate Begemana9795f82005-03-24 04:41:43 +00001204 case ISD::FP_TO_UINT:
1205 case ISD::FP_TO_SINT:
Nate Begeman01d05262005-03-30 01:45:43 +00001206 assert(0 && "FP_TO_S/UINT unimplemented");
Nate Begemana9795f82005-03-24 04:41:43 +00001207 abort();
1208
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001209 case ISD::SETCC:
Nate Begeman33162522005-03-29 21:54:38 +00001210 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Node)) {
Nate Begemandffcfcc2005-04-01 00:32:34 +00001211 Opc = SelectSetCR0(N);
Nate Begeman33162522005-03-29 21:54:38 +00001212
1213 // Create an iterator with which to insert the MBB for copying the false
1214 // value and the MBB to hold the PHI instruction for this SetCC.
1215 MachineBasicBlock *thisMBB = BB;
1216 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1217 ilist<MachineBasicBlock>::iterator It = BB;
1218 ++It;
1219
1220 // thisMBB:
1221 // ...
1222 // cmpTY cr0, r1, r2
1223 // %TrueValue = li 1
1224 // bCC sinkMBB
Nate Begeman33162522005-03-29 21:54:38 +00001225 unsigned TrueValue = MakeReg(MVT::i32);
1226 BuildMI(BB, PPC::LI, 1, TrueValue).addSImm(1);
1227 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
1228 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
1229 BuildMI(BB, Opc, 2).addReg(PPC::CR0).addMBB(sinkMBB);
1230 MachineFunction *F = BB->getParent();
1231 F->getBasicBlockList().insert(It, copy0MBB);
1232 F->getBasicBlockList().insert(It, sinkMBB);
1233 // Update machine-CFG edges
1234 BB->addSuccessor(copy0MBB);
1235 BB->addSuccessor(sinkMBB);
1236
1237 // copy0MBB:
1238 // %FalseValue = li 0
1239 // fallthrough
1240 BB = copy0MBB;
1241 unsigned FalseValue = MakeReg(MVT::i32);
1242 BuildMI(BB, PPC::LI, 1, FalseValue).addSImm(0);
1243 // Update machine-CFG edges
1244 BB->addSuccessor(sinkMBB);
1245
1246 // sinkMBB:
1247 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1248 // ...
1249 BB = sinkMBB;
1250 BuildMI(BB, PPC::PHI, 4, Result).addReg(FalseValue)
1251 .addMBB(copy0MBB).addReg(TrueValue).addMBB(thisMBB);
1252 return Result;
1253 }
1254 assert(0 && "Is this legal?");
1255 return 0;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001256
Nate Begeman74747862005-03-29 22:24:51 +00001257 case ISD::SELECT: {
Nate Begemandffcfcc2005-04-01 00:32:34 +00001258 Opc = SelectSetCR0(N.getOperand(0));
1259
Nate Begeman74747862005-03-29 22:24:51 +00001260 // Create an iterator with which to insert the MBB for copying the false
1261 // value and the MBB to hold the PHI instruction for this SetCC.
1262 MachineBasicBlock *thisMBB = BB;
1263 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1264 ilist<MachineBasicBlock>::iterator It = BB;
1265 ++It;
1266
1267 // thisMBB:
1268 // ...
1269 // TrueVal = ...
1270 // cmpTY cr0, r1, r2
1271 // bCC copy1MBB
1272 // fallthrough --> copy0MBB
Nate Begeman74747862005-03-29 22:24:51 +00001273 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
1274 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
1275 unsigned TrueValue = SelectExpr(N.getOperand(1)); //Use if TRUE
Nate Begeman3e897162005-03-31 23:55:40 +00001276 BuildMI(BB, Opc, 2).addReg(PPC::CR0).addMBB(sinkMBB);
Nate Begeman74747862005-03-29 22:24:51 +00001277 MachineFunction *F = BB->getParent();
1278 F->getBasicBlockList().insert(It, copy0MBB);
1279 F->getBasicBlockList().insert(It, sinkMBB);
1280 // Update machine-CFG edges
1281 BB->addSuccessor(copy0MBB);
1282 BB->addSuccessor(sinkMBB);
1283
1284 // copy0MBB:
1285 // %FalseValue = ...
1286 // # fallthrough to sinkMBB
1287 BB = copy0MBB;
1288 unsigned FalseValue = SelectExpr(N.getOperand(2)); //Use if FALSE
1289 // Update machine-CFG edges
1290 BB->addSuccessor(sinkMBB);
1291
1292 // sinkMBB:
1293 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1294 // ...
1295 BB = sinkMBB;
1296 BuildMI(BB, PPC::PHI, 4, Result).addReg(FalseValue)
1297 .addMBB(copy0MBB).addReg(TrueValue).addMBB(thisMBB);
1298
1299 // FIXME: Select i64?
1300 return Result;
1301 }
Nate Begemana9795f82005-03-24 04:41:43 +00001302
1303 case ISD::Constant:
1304 switch (N.getValueType()) {
1305 default: assert(0 && "Cannot use constants of this type!");
1306 case MVT::i1:
1307 BuildMI(BB, PPC::LI, 1, Result)
1308 .addSImm(!cast<ConstantSDNode>(N)->isNullValue());
1309 break;
1310 case MVT::i32:
1311 {
1312 int v = (int)cast<ConstantSDNode>(N)->getSignExtended();
1313 if (v < 32768 && v >= -32768) {
1314 BuildMI(BB, PPC::LI, 1, Result).addSImm(v);
1315 } else {
Nate Begeman5e966612005-03-24 06:28:42 +00001316 Tmp1 = MakeReg(MVT::i32);
1317 BuildMI(BB, PPC::LIS, 1, Tmp1).addSImm(v >> 16);
1318 BuildMI(BB, PPC::ORI, 2, Result).addReg(Tmp1).addImm(v & 0xFFFF);
Nate Begemana9795f82005-03-24 04:41:43 +00001319 }
1320 }
1321 }
1322 return Result;
1323 }
1324
1325 return 0;
1326}
1327
1328void ISel::Select(SDOperand N) {
1329 unsigned Tmp1, Tmp2, Opc;
1330 unsigned opcode = N.getOpcode();
1331
1332 if (!ExprMap.insert(std::make_pair(N, 1)).second)
1333 return; // Already selected.
1334
1335 SDNode *Node = N.Val;
1336
1337 switch (Node->getOpcode()) {
1338 default:
1339 Node->dump(); std::cerr << "\n";
1340 assert(0 && "Node not handled yet!");
1341 case ISD::EntryToken: return; // Noop
1342 case ISD::TokenFactor:
1343 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1344 Select(Node->getOperand(i));
1345 return;
1346 case ISD::ADJCALLSTACKDOWN:
1347 case ISD::ADJCALLSTACKUP:
1348 Select(N.getOperand(0));
1349 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1350 Opc = N.getOpcode() == ISD::ADJCALLSTACKDOWN ? PPC::ADJCALLSTACKDOWN :
1351 PPC::ADJCALLSTACKUP;
1352 BuildMI(BB, Opc, 1).addImm(Tmp1);
1353 return;
1354 case ISD::BR: {
1355 MachineBasicBlock *Dest =
1356 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
Nate Begemana9795f82005-03-24 04:41:43 +00001357 Select(N.getOperand(0));
1358 BuildMI(BB, PPC::B, 1).addMBB(Dest);
1359 return;
1360 }
1361 case ISD::BRCOND:
1362 SelectBranchCC(N);
1363 return;
1364 case ISD::CopyToReg:
1365 Select(N.getOperand(0));
1366 Tmp1 = SelectExpr(N.getOperand(1));
1367 Tmp2 = cast<RegSDNode>(N)->getReg();
1368
1369 if (Tmp1 != Tmp2) {
1370 if (N.getOperand(1).getValueType() == MVT::f64 ||
1371 N.getOperand(1).getValueType() == MVT::f32)
1372 BuildMI(BB, PPC::FMR, 1, Tmp2).addReg(Tmp1);
1373 else
1374 BuildMI(BB, PPC::OR, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
1375 }
1376 return;
1377 case ISD::ImplicitDef:
1378 Select(N.getOperand(0));
1379 BuildMI(BB, PPC::IMPLICIT_DEF, 0, cast<RegSDNode>(N)->getReg());
1380 return;
1381 case ISD::RET:
1382 switch (N.getNumOperands()) {
1383 default:
1384 assert(0 && "Unknown return instruction!");
1385 case 3:
1386 assert(N.getOperand(1).getValueType() == MVT::i32 &&
1387 N.getOperand(2).getValueType() == MVT::i32 &&
1388 "Unknown two-register value!");
1389 Select(N.getOperand(0));
1390 Tmp1 = SelectExpr(N.getOperand(1));
1391 Tmp2 = SelectExpr(N.getOperand(2));
1392 BuildMI(BB, PPC::OR, 2, PPC::R3).addReg(Tmp1).addReg(Tmp1);
1393 BuildMI(BB, PPC::OR, 2, PPC::R4).addReg(Tmp2).addReg(Tmp2);
1394 break;
1395 case 2:
1396 Select(N.getOperand(0));
1397 Tmp1 = SelectExpr(N.getOperand(1));
1398 switch (N.getOperand(1).getValueType()) {
1399 default:
1400 assert(0 && "Unknown return type!");
1401 case MVT::f64:
1402 case MVT::f32:
1403 BuildMI(BB, PPC::FMR, 1, PPC::F1).addReg(Tmp1);
1404 break;
1405 case MVT::i32:
1406 BuildMI(BB, PPC::OR, 2, PPC::R3).addReg(Tmp1).addReg(Tmp1);
1407 break;
1408 }
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001409 case 1:
1410 Select(N.getOperand(0));
1411 break;
Nate Begemana9795f82005-03-24 04:41:43 +00001412 }
1413 BuildMI(BB, PPC::BLR, 0); // Just emit a 'ret' instruction
1414 return;
Nate Begemana9795f82005-03-24 04:41:43 +00001415 case ISD::TRUNCSTORE:
1416 case ISD::STORE:
1417 {
1418 SDOperand Chain = N.getOperand(0);
1419 SDOperand Value = N.getOperand(1);
1420 SDOperand Address = N.getOperand(2);
1421 Select(Chain);
1422
1423 Tmp1 = SelectExpr(Value); //value
1424
1425 if (opcode == ISD::STORE) {
1426 switch(Value.getValueType()) {
1427 default: assert(0 && "unknown Type in store");
1428 case MVT::i32: Opc = PPC::STW; break;
1429 case MVT::f64: Opc = PPC::STFD; break;
1430 case MVT::f32: Opc = PPC::STFS; break;
1431 }
1432 } else { //ISD::TRUNCSTORE
1433 switch(cast<MVTSDNode>(Node)->getExtraValueType()) {
1434 default: assert(0 && "unknown Type in store");
1435 case MVT::i1: //FIXME: DAG does not promote this load
1436 case MVT::i8: Opc = PPC::STB; break;
1437 case MVT::i16: Opc = PPC::STH; break;
1438 }
1439 }
1440
1441 if (Address.getOpcode() == ISD::GlobalAddress)
1442 {
1443 BuildMI(BB, Opc, 2).addReg(Tmp1)
1444 .addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
1445 }
1446 else if(Address.getOpcode() == ISD::FrameIndex)
1447 {
Nate Begeman58f718c2005-03-30 02:23:08 +00001448 Tmp2 = cast<FrameIndexSDNode>(Address)->getIndex();
1449 addFrameReference(BuildMI(BB, Opc, 3).addReg(Tmp1), (int)Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +00001450 }
1451 else
1452 {
1453 int offset;
1454 SelectAddr(Address, Tmp2, offset);
1455 BuildMI(BB, Opc, 3).addReg(Tmp1).addImm(offset).addReg(Tmp2);
1456 }
1457 return;
1458 }
1459 case ISD::EXTLOAD:
1460 case ISD::SEXTLOAD:
1461 case ISD::ZEXTLOAD:
1462 case ISD::LOAD:
1463 case ISD::CopyFromReg:
1464 case ISD::CALL:
1465 case ISD::DYNAMIC_STACKALLOC:
1466 ExprMap.erase(N);
1467 SelectExpr(N);
1468 return;
1469 }
1470 assert(0 && "Should not be reached!");
1471}
1472
1473
1474/// createPPC32PatternInstructionSelector - This pass converts an LLVM function
1475/// into a machine code representation using pattern matching and a machine
1476/// description file.
1477///
1478FunctionPass *llvm::createPPC32ISelPattern(TargetMachine &TM) {
1479 return new ISel(TM);
Chris Lattner246fa632005-03-24 06:16:18 +00001480}
1481