blob: 22b3276bb5cd74dd87aaf0a052c8b3bfd9e317ac [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 Begemanc7b09f12005-03-25 08:34:25 +0000465 unsigned ISel::getGlobalBaseReg();
Nate Begemana9795f82005-03-24 04:41:43 +0000466 unsigned SelectExpr(SDOperand N);
467 unsigned SelectExprFP(SDOperand N, unsigned Result);
468 void Select(SDOperand N);
469
470 void SelectAddr(SDOperand N, unsigned& Reg, int& offset);
471 void SelectBranchCC(SDOperand N);
472};
473
474/// canUseAsImmediateForOpcode - This method returns a value indicating whether
475/// the ConstantSDNode N can be used as an immediate to Opcode. The return
476/// values are either 0, 1 or 2. 0 indicates that either N is not a
477/// ConstantSDNode, or is not suitable for use by that opcode. A return value
478/// of 1 indicates that the constant may be used in normal immediate form. A
479/// return value of 2 indicates that the constant may be used in shifted
480/// immediate form. If the return value is nonzero, the constant value is
481/// placed in Imm.
482///
483static unsigned canUseAsImmediateForOpcode(SDOperand N, unsigned Opcode,
Nate Begeman3e897162005-03-31 23:55:40 +0000484 unsigned& Imm, bool U = false) {
Nate Begemana9795f82005-03-24 04:41:43 +0000485 if (N.getOpcode() != ISD::Constant) return 0;
486
487 int v = (int)cast<ConstantSDNode>(N)->getSignExtended();
488
489 switch(Opcode) {
490 default: return 0;
491 case ISD::ADD:
492 if (v <= 32767 && v >= -32768) { Imm = v & 0xFFFF; return 1; }
493 if ((v & 0x0000FFFF) == 0) { Imm = v >> 16; return 2; }
494 break;
495 case ISD::AND:
496 case ISD::XOR:
497 case ISD::OR:
498 if (v >= 0 && v <= 65535) { Imm = v & 0xFFFF; return 1; }
499 if ((v & 0x0000FFFF) == 0) { Imm = v >> 16; return 2; }
500 break;
Nate Begeman307e7442005-03-26 01:28:53 +0000501 case ISD::MUL:
502 if (v <= 32767 && v >= -32768) { Imm = v & 0xFFFF; return 1; }
503 break;
Nate Begeman3e897162005-03-31 23:55:40 +0000504 case ISD::SETCC:
505 if (U && (v >= 0 && v <= 65535)) { Imm = v & 0xFFFF; return 1; }
506 if (!U && (v <= 32767 && v >= -32768)) { Imm = v & 0xFFFF; return 1; }
507 break;
Nate Begemana9795f82005-03-24 04:41:43 +0000508 }
509 return 0;
510}
Nate Begeman3e897162005-03-31 23:55:40 +0000511
512/// getBCCForSetCC - Returns the PowerPC condition branch mnemonic corresponding
513/// to Condition. If the Condition is unordered or unsigned, the bool argument
514/// U is set to true, otherwise it is set to false.
515static unsigned getBCCForSetCC(unsigned Condition, bool& U) {
516 U = false;
517 switch (Condition) {
518 default: assert(0 && "Unknown condition!"); abort();
519 case ISD::SETEQ: return PPC::BEQ;
520 case ISD::SETNE: return PPC::BNE;
521 case ISD::SETULT: U = true;
522 case ISD::SETLT: return PPC::BLT;
523 case ISD::SETULE: U = true;
524 case ISD::SETLE: return PPC::BLE;
525 case ISD::SETUGT: U = true;
526 case ISD::SETGT: return PPC::BGT;
527 case ISD::SETUGE: U = true;
528 case ISD::SETGE: return PPC::BGE;
529 }
530}
Nate Begemana9795f82005-03-24 04:41:43 +0000531}
532
Nate Begemanc7b09f12005-03-25 08:34:25 +0000533/// getGlobalBaseReg - Output the instructions required to put the
534/// base address to use for accessing globals into a register.
535///
536unsigned ISel::getGlobalBaseReg() {
537 if (!GlobalBaseInitialized) {
538 // Insert the set of GlobalBaseReg into the first MBB of the function
539 MachineBasicBlock &FirstMBB = BB->getParent()->front();
540 MachineBasicBlock::iterator MBBI = FirstMBB.begin();
541 GlobalBaseReg = MakeReg(MVT::i32);
542 BuildMI(FirstMBB, MBBI, PPC::MovePCtoLR, 0, PPC::LR);
543 BuildMI(FirstMBB, MBBI, PPC::MFLR, 1, GlobalBaseReg).addReg(PPC::LR);
544 GlobalBaseInitialized = true;
545 }
546 return GlobalBaseReg;
547}
548
Nate Begemana9795f82005-03-24 04:41:43 +0000549//Check to see if the load is a constant offset from a base register
550void ISel::SelectAddr(SDOperand N, unsigned& Reg, int& offset)
551{
Nate Begeman96fc6812005-03-31 02:05:53 +0000552 unsigned imm = 0, opcode = N.getOpcode();
553 if (N.getOpcode() == ISD::ADD)
554 if (1 == canUseAsImmediateForOpcode(N.getOperand(1), opcode, imm)) {
555 Reg = SelectExpr(N.getOperand(0));
556 offset = imm;
557 return;
558 }
Nate Begemana9795f82005-03-24 04:41:43 +0000559 Reg = SelectExpr(N);
560 offset = 0;
561 return;
562}
563
564void ISel::SelectBranchCC(SDOperand N)
565{
566 assert(N.getOpcode() == ISD::BRCOND && "Not a BranchCC???");
567 MachineBasicBlock *Dest =
568 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
Nate Begeman3e897162005-03-31 23:55:40 +0000569
570 unsigned Opc, Tmp1, Tmp2;
Nate Begemana9795f82005-03-24 04:41:43 +0000571 Select(N.getOperand(0)); //chain
Nate Begeman3e897162005-03-31 23:55:40 +0000572
573 // If the first operand to the select is a SETCC node, then we can fold it
574 // into the branch that selects which value to return.
575 SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(N.getOperand(1).Val);
576 if (SetCC && N.getOperand(1).getOpcode() == ISD::SETCC &&
577 MVT::isInteger(SetCC->getOperand(0).getValueType())) {
578 bool U;
579 Opc = getBCCForSetCC(SetCC->getCondition(), U);
580 Tmp1 = SelectExpr(SetCC->getOperand(0));
581
582 // Pass the optional argument U to canUseAsImmediateForOpcode for SETCC,
583 // so that it knows whether the SETCC immediate range is signed or not.
584 if (1 == canUseAsImmediateForOpcode(SetCC->getOperand(1), ISD::SETCC,
585 Tmp2, U)) {
586 if (U)
587 BuildMI(BB, PPC::CMPLWI, 2, PPC::CR0).addReg(Tmp1).addImm(Tmp2);
588 else
589 BuildMI(BB, PPC::CMPWI, 2, PPC::CR0).addReg(Tmp1).addSImm(Tmp2);
590 } else {
591 Tmp2 = SelectExpr(SetCC->getOperand(1));
592 BuildMI(BB, U ? PPC::CMPLW : PPC::CMPW, 2, PPC::CR0).addReg(Tmp1)
593 .addReg(Tmp2);
594 }
595 } else {
596 Tmp1 = SelectExpr(N.getOperand(1));
597 BuildMI(BB, PPC::CMPLWI, 2, PPC::CR0).addReg(Tmp1).addImm(0);
598 Opc = PPC::BNE;
599 }
600
601 BuildMI(BB, Opc, 2).addReg(PPC::CR0).addMBB(Dest);
Nate Begemana9795f82005-03-24 04:41:43 +0000602 return;
603}
604
605unsigned ISel::SelectExprFP(SDOperand N, unsigned Result)
606{
607 unsigned Tmp1, Tmp2, Tmp3;
608 unsigned Opc = 0;
609 SDNode *Node = N.Val;
610 MVT::ValueType DestType = N.getValueType();
611 unsigned opcode = N.getOpcode();
612
613 switch (opcode) {
614 default:
615 Node->dump();
616 assert(0 && "Node not handled!\n");
617
Nate Begeman23afcfb2005-03-29 22:48:55 +0000618 case ISD::SELECT: {
Nate Begeman3e897162005-03-31 23:55:40 +0000619 // Attempt to generate FSEL. We can do this whenever we have an FP result,
620 // and an FP comparison in the SetCC node.
621 SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(N.getOperand(0).Val);
622 if (SetCC && N.getOperand(0).getOpcode() == ISD::SETCC &&
623 !MVT::isInteger(SetCC->getOperand(0).getValueType()) &&
624 SetCC->getCondition() != ISD::SETEQ &&
625 SetCC->getCondition() != ISD::SETNE) {
626 MVT::ValueType VT = SetCC->getOperand(0).getValueType();
627 Tmp1 = SelectExpr(SetCC->getOperand(0)); // Val to compare against
628 unsigned TV = SelectExpr(N.getOperand(1)); // Use if TRUE
629 unsigned FV = SelectExpr(N.getOperand(2)); // Use if FALSE
630
631 ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(1));
632 if (CN && (CN->isExactlyValue(-0.0) || CN->isExactlyValue(0.0))) {
633 switch(SetCC->getCondition()) {
634 default: assert(0 && "Invalid FSEL condition"); abort();
635 case ISD::SETULT:
636 case ISD::SETLT:
637 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp1).addReg(FV).addReg(TV);
638 return Result;
639 case ISD::SETUGE:
640 case ISD::SETGE:
641 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp1).addReg(TV).addReg(FV);
642 return Result;
643 case ISD::SETUGT:
644 case ISD::SETGT: {
645 Tmp2 = MakeReg(VT);
646 BuildMI(BB, PPC::FNEG, 1, Tmp2).addReg(Tmp1);
647 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp2).addReg(FV).addReg(TV);
648 return Result;
649 }
650 case ISD::SETULE:
651 case ISD::SETLE: {
652 Tmp2 = MakeReg(VT);
653 BuildMI(BB, PPC::FNEG, 1, Tmp2).addReg(Tmp1);
654 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp2).addReg(TV).addReg(FV);
655 return Result;
656 }
657 }
658 } else {
659 Opc = (MVT::f64 == VT) ? PPC::FSUB : PPC::FSUBS;
660 Tmp2 = SelectExpr(SetCC->getOperand(1));
661 Tmp3 = MakeReg(VT);
662 switch(SetCC->getCondition()) {
663 default: assert(0 && "Invalid FSEL condition"); abort();
664 case ISD::SETULT:
665 case ISD::SETLT:
666 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
667 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(FV).addReg(TV);
668 return Result;
669 case ISD::SETUGE:
670 case ISD::SETGE:
671 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
672 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(TV).addReg(FV);
673 return Result;
674 case ISD::SETUGT:
675 case ISD::SETGT:
676 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp2).addReg(Tmp1);
677 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(FV).addReg(TV);
678 return Result;
679 case ISD::SETULE:
680 case ISD::SETLE:
681 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp2).addReg(Tmp1);
682 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(TV).addReg(FV);
683 return Result;
684 }
685 }
686 assert(0 && "Should never get here");
687 return 0;
688 }
689
Nate Begeman23afcfb2005-03-29 22:48:55 +0000690 // Create an iterator with which to insert the MBB for copying the false
691 // value and the MBB to hold the PHI instruction for this SetCC.
692 MachineBasicBlock *thisMBB = BB;
693 const BasicBlock *LLVM_BB = BB->getBasicBlock();
694 ilist<MachineBasicBlock>::iterator It = BB;
695 ++It;
696
697 // thisMBB:
698 // ...
699 // TrueVal = ...
700 // cmpTY cr0, r1, r2
701 // bCC copy1MBB
702 // fallthrough --> copy0MBB
Nate Begeman3e897162005-03-31 23:55:40 +0000703 Tmp1 = SelectExpr(N.getOperand(0)); //Cond
Nate Begeman23afcfb2005-03-29 22:48:55 +0000704 BuildMI(BB, PPC::CMPLWI, 2, PPC::CR0).addReg(Tmp1).addImm(0);
705 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
706 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
707 unsigned TrueValue = SelectExpr(N.getOperand(1)); //Use if TRUE
708 BuildMI(BB, PPC::BNE, 2).addReg(PPC::CR0).addMBB(sinkMBB);
709 MachineFunction *F = BB->getParent();
710 F->getBasicBlockList().insert(It, copy0MBB);
711 F->getBasicBlockList().insert(It, sinkMBB);
712 // Update machine-CFG edges
713 BB->addSuccessor(copy0MBB);
714 BB->addSuccessor(sinkMBB);
715
716 // copy0MBB:
717 // %FalseValue = ...
718 // # fallthrough to sinkMBB
719 BB = copy0MBB;
720 unsigned FalseValue = SelectExpr(N.getOperand(2)); //Use if FALSE
721 // Update machine-CFG edges
722 BB->addSuccessor(sinkMBB);
723
724 // sinkMBB:
725 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
726 // ...
727 BB = sinkMBB;
728 BuildMI(BB, PPC::PHI, 4, Result).addReg(FalseValue)
729 .addMBB(copy0MBB).addReg(TrueValue).addMBB(thisMBB);
730 return Result;
731 }
Nate Begemana9795f82005-03-24 04:41:43 +0000732
733 case ISD::FP_ROUND:
734 assert (DestType == MVT::f32 &&
735 N.getOperand(0).getValueType() == MVT::f64 &&
736 "only f64 to f32 conversion supported here");
737 Tmp1 = SelectExpr(N.getOperand(0));
738 BuildMI(BB, PPC::FRSP, 1, Result).addReg(Tmp1);
739 return Result;
740
741 case ISD::FP_EXTEND:
742 assert (DestType == MVT::f64 &&
743 N.getOperand(0).getValueType() == MVT::f32 &&
744 "only f32 to f64 conversion supported here");
745 Tmp1 = SelectExpr(N.getOperand(0));
746 BuildMI(BB, PPC::FMR, 1, Result).addReg(Tmp1);
747 return Result;
748
749 case ISD::CopyFromReg:
Nate Begemanf2622612005-03-26 02:17:46 +0000750 if (Result == 1)
751 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
752 Tmp1 = dyn_cast<RegSDNode>(Node)->getReg();
753 BuildMI(BB, PPC::FMR, 1, Result).addReg(Tmp1);
754 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +0000755
Nate Begemana9795f82005-03-24 04:41:43 +0000756 case ISD::ConstantFP:
Nate Begemanca12a2b2005-03-28 22:28:37 +0000757 assert(0 && "ISD::ConstantFP Unimplemented");
Nate Begemana9795f82005-03-24 04:41:43 +0000758 abort();
759
760 case ISD::MUL:
761 case ISD::ADD:
762 case ISD::SUB:
763 case ISD::SDIV:
764 switch( opcode ) {
765 case ISD::MUL: Opc = DestType == MVT::f64 ? PPC::FMUL : PPC::FMULS; break;
766 case ISD::ADD: Opc = DestType == MVT::f64 ? PPC::FADD : PPC::FADDS; break;
767 case ISD::SUB: Opc = DestType == MVT::f64 ? PPC::FSUB : PPC::FSUBS; break;
768 case ISD::SDIV: Opc = DestType == MVT::f64 ? PPC::FDIV : PPC::FDIVS; break;
769 };
Nate Begemana9795f82005-03-24 04:41:43 +0000770 Tmp1 = SelectExpr(N.getOperand(0));
771 Tmp2 = SelectExpr(N.getOperand(1));
772 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
773 return Result;
774
Nate Begemana9795f82005-03-24 04:41:43 +0000775 case ISD::UINT_TO_FP:
Nate Begemanfdcf3412005-03-30 19:38:35 +0000776 case ISD::SINT_TO_FP: {
777 assert (N.getOperand(0).getValueType() == MVT::i32
778 && "int to float must operate on i32");
779 bool IsUnsigned = (ISD::UINT_TO_FP == opcode);
780 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
781 Tmp2 = MakeReg(MVT::f64); // temp reg to load the integer value into
782 Tmp3 = MakeReg(MVT::i32); // temp reg to hold the conversion constant
783 unsigned ConstF = MakeReg(MVT::f64); // temp reg to hold the fp constant
784
785 int FrameIdx = BB->getParent()->getFrameInfo()->CreateStackObject(8, 8);
786 MachineConstantPool *CP = BB->getParent()->getConstantPool();
787
788 // FIXME: pull this FP constant generation stuff out into something like
789 // the simple ISel's getReg.
790 if (IsUnsigned) {
791 ConstantFP *CFP = ConstantFP::get(Type::DoubleTy, 0x1.000000p52);
792 unsigned CPI = CP->getConstantPoolIndex(CFP);
793 // Load constant fp value
794 unsigned Tmp4 = MakeReg(MVT::i32);
795 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp4).addReg(getGlobalBaseReg())
796 .addConstantPoolIndex(CPI);
797 BuildMI(BB, PPC::LFD, 2, ConstF).addConstantPoolIndex(CPI).addReg(Tmp4);
798 // Store the hi & low halves of the fp value, currently in int regs
799 BuildMI(BB, PPC::LIS, 1, Tmp3).addSImm(0x4330);
800 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(Tmp3), FrameIdx);
801 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(Tmp1), FrameIdx, 4);
802 addFrameReference(BuildMI(BB, PPC::LFD, 2, Tmp2), FrameIdx);
803 // Generate the return value with a subtract
804 BuildMI(BB, PPC::FSUB, 2, Result).addReg(Tmp2).addReg(ConstF);
805 } else {
806 ConstantFP *CFP = ConstantFP::get(Type::DoubleTy, 0x1.000008p52);
807 unsigned CPI = CP->getConstantPoolIndex(CFP);
808 // Load constant fp value
809 unsigned Tmp4 = MakeReg(MVT::i32);
810 unsigned TmpL = MakeReg(MVT::i32);
811 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp4).addReg(getGlobalBaseReg())
812 .addConstantPoolIndex(CPI);
813 BuildMI(BB, PPC::LFD, 2, ConstF).addConstantPoolIndex(CPI).addReg(Tmp4);
814 // Store the hi & low halves of the fp value, currently in int regs
815 BuildMI(BB, PPC::LIS, 1, Tmp3).addSImm(0x4330);
816 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(Tmp3), FrameIdx);
817 BuildMI(BB, PPC::XORIS, 2, TmpL).addReg(Tmp1).addImm(0x8000);
818 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(TmpL), FrameIdx, 4);
819 addFrameReference(BuildMI(BB, PPC::LFD, 2, Tmp2), FrameIdx);
820 // Generate the return value with a subtract
821 BuildMI(BB, PPC::FSUB, 2, Result).addReg(Tmp2).addReg(ConstF);
822 }
823 return Result;
824 }
Nate Begemana9795f82005-03-24 04:41:43 +0000825 }
826 assert(0 && "should not get here");
827 return 0;
828}
829
830unsigned ISel::SelectExpr(SDOperand N) {
831 unsigned Result;
832 unsigned Tmp1, Tmp2, Tmp3;
833 unsigned Opc = 0;
834 unsigned opcode = N.getOpcode();
835
836 SDNode *Node = N.Val;
837 MVT::ValueType DestType = N.getValueType();
838
839 unsigned &Reg = ExprMap[N];
840 if (Reg) return Reg;
841
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000842 if (N.getOpcode() != ISD::CALL && N.getOpcode() != ISD::ADD_PARTS &&
843 N.getOpcode() != ISD::SUB_PARTS)
Nate Begemana9795f82005-03-24 04:41:43 +0000844 Reg = Result = (N.getValueType() != MVT::Other) ?
845 MakeReg(N.getValueType()) : 1;
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000846 else {
847 // If this is a call instruction, make sure to prepare ALL of the result
848 // values as well as the chain.
849 if (N.getOpcode() == ISD::CALL) {
850 if (Node->getNumValues() == 1)
851 Reg = Result = 1; // Void call, just a chain.
852 else {
853 Result = MakeReg(Node->getValueType(0));
854 ExprMap[N.getValue(0)] = Result;
855 for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
856 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
857 ExprMap[SDOperand(Node, Node->getNumValues()-1)] = 1;
858 }
859 } else {
860 Result = MakeReg(Node->getValueType(0));
861 ExprMap[N.getValue(0)] = Result;
862 for (unsigned i = 1, e = N.Val->getNumValues(); i != e; ++i)
863 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
864 }
865 }
866
867 if (DestType == MVT::f64 || DestType == MVT::f32)
Nate Begeman74d73452005-03-31 00:15:26 +0000868 if (ISD::LOAD != opcode && ISD::EXTLOAD != opcode)
869 return SelectExprFP(N, Result);
Nate Begemana9795f82005-03-24 04:41:43 +0000870
871 switch (opcode) {
872 default:
873 Node->dump();
874 assert(0 && "Node not handled!\n");
875
876 case ISD::DYNAMIC_STACKALLOC:
Nate Begeman5e966612005-03-24 06:28:42 +0000877 // Generate both result values. FIXME: Need a better commment here?
878 if (Result != 1)
879 ExprMap[N.getValue(1)] = 1;
880 else
881 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
882
883 // FIXME: We are currently ignoring the requested alignment for handling
884 // greater than the stack alignment. This will need to be revisited at some
885 // point. Align = N.getOperand(2);
886 if (!isa<ConstantSDNode>(N.getOperand(2)) ||
887 cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) {
888 std::cerr << "Cannot allocate stack object with greater alignment than"
889 << " the stack alignment yet!";
890 abort();
891 }
892 Select(N.getOperand(0));
893 Tmp1 = SelectExpr(N.getOperand(1));
894 // Subtract size from stack pointer, thereby allocating some space.
895 BuildMI(BB, PPC::SUBF, 2, PPC::R1).addReg(Tmp1).addReg(PPC::R1);
896 // Put a pointer to the space into the result register by copying the SP
897 BuildMI(BB, PPC::OR, 2, Result).addReg(PPC::R1).addReg(PPC::R1);
898 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +0000899
900 case ISD::ConstantPool:
Nate Begemanca12a2b2005-03-28 22:28:37 +0000901 Tmp1 = cast<ConstantPoolSDNode>(N)->getIndex();
902 Tmp2 = MakeReg(MVT::i32);
903 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp2).addReg(getGlobalBaseReg())
904 .addConstantPoolIndex(Tmp1);
905 BuildMI(BB, PPC::LA, 2, Result).addReg(Tmp2).addConstantPoolIndex(Tmp1);
906 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +0000907
908 case ISD::FrameIndex:
Nate Begemanf3d08f32005-03-29 00:03:27 +0000909 Tmp1 = cast<FrameIndexSDNode>(N)->getIndex();
Nate Begeman58f718c2005-03-30 02:23:08 +0000910 addFrameReference(BuildMI(BB, PPC::ADDI, 2, Result), (int)Tmp1, 0, false);
Nate Begemanf3d08f32005-03-29 00:03:27 +0000911 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +0000912
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000913 case ISD::GlobalAddress: {
914 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
Nate Begemanca12a2b2005-03-28 22:28:37 +0000915 Tmp1 = MakeReg(MVT::i32);
Nate Begemanc7b09f12005-03-25 08:34:25 +0000916 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp1).addReg(getGlobalBaseReg())
917 .addGlobalAddress(GV);
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000918 if (GV->hasWeakLinkage() || GV->isExternal()) {
919 BuildMI(BB, PPC::LWZ, 2, Result).addGlobalAddress(GV).addReg(Tmp1);
920 } else {
921 BuildMI(BB, PPC::LA, 2, Result).addReg(Tmp1).addGlobalAddress(GV);
922 }
923 return Result;
924 }
925
Nate Begeman5e966612005-03-24 06:28:42 +0000926 case ISD::LOAD:
Nate Begemana9795f82005-03-24 04:41:43 +0000927 case ISD::EXTLOAD:
928 case ISD::ZEXTLOAD:
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000929 case ISD::SEXTLOAD: {
Nate Begeman9db505c2005-03-28 19:36:43 +0000930 MVT::ValueType TypeBeingLoaded = (ISD::LOAD == opcode) ?
931 Node->getValueType(0) : cast<MVTSDNode>(Node)->getExtraValueType();
Nate Begeman74d73452005-03-31 00:15:26 +0000932 bool sext = (ISD::SEXTLOAD == opcode);
933 bool byte = (MVT::i8 == TypeBeingLoaded);
934
Nate Begeman5e966612005-03-24 06:28:42 +0000935 // Make sure we generate both values.
936 if (Result != 1)
937 ExprMap[N.getValue(1)] = 1; // Generate the token
938 else
939 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
940
941 SDOperand Chain = N.getOperand(0);
942 SDOperand Address = N.getOperand(1);
943 Select(Chain);
944
Nate Begeman9db505c2005-03-28 19:36:43 +0000945 switch (TypeBeingLoaded) {
Nate Begeman74d73452005-03-31 00:15:26 +0000946 default: Node->dump(); assert(0 && "Cannot load this type!");
Nate Begeman9db505c2005-03-28 19:36:43 +0000947 case MVT::i1: Opc = PPC::LBZ; break;
948 case MVT::i8: Opc = PPC::LBZ; break;
949 case MVT::i16: Opc = sext ? PPC::LHA : PPC::LHZ; break;
950 case MVT::i32: Opc = PPC::LWZ; break;
Nate Begeman74d73452005-03-31 00:15:26 +0000951 case MVT::f32: Opc = PPC::LFS; break;
952 case MVT::f64: Opc = PPC::LFD; break;
Nate Begeman5e966612005-03-24 06:28:42 +0000953 }
954
Nate Begeman74d73452005-03-31 00:15:26 +0000955 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Address)) {
956 Tmp1 = MakeReg(MVT::i32);
957 int CPI = CP->getIndex();
958 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp1).addReg(getGlobalBaseReg())
959 .addConstantPoolIndex(CPI);
960 BuildMI(BB, Opc, 2, Result).addConstantPoolIndex(CPI).addReg(Tmp1);
Nate Begeman9db505c2005-03-28 19:36:43 +0000961 }
Nate Begeman74d73452005-03-31 00:15:26 +0000962 else if(Address.getOpcode() == ISD::FrameIndex) {
Nate Begeman58f718c2005-03-30 02:23:08 +0000963 Tmp1 = cast<FrameIndexSDNode>(Address)->getIndex();
964 addFrameReference(BuildMI(BB, Opc, 2, Result), (int)Tmp1);
Nate Begeman5e966612005-03-24 06:28:42 +0000965 } else {
966 int offset;
967 SelectAddr(Address, Tmp1, offset);
968 BuildMI(BB, Opc, 2, Result).addSImm(offset).addReg(Tmp1);
969 }
970 return Result;
971 }
972
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000973 case ISD::CALL: {
974 // Lower the chain for this call.
975 Select(N.getOperand(0));
976 ExprMap[N.getValue(Node->getNumValues()-1)] = 1;
Nate Begeman74d73452005-03-31 00:15:26 +0000977
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000978 for(int i = 2, e = Node->getNumOperands(); i < e; ++i)
Nate Begeman74d73452005-03-31 00:15:26 +0000979 Select(N.getOperand(i));
980
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000981 // Emit the correct call instruction based on the type of symbol called.
982 if (GlobalAddressSDNode *GASD =
983 dyn_cast<GlobalAddressSDNode>(N.getOperand(1))) {
984 BuildMI(BB, PPC::CALLpcrel, 1).addGlobalAddress(GASD->getGlobal(), true);
985 } else if (ExternalSymbolSDNode *ESSDN =
986 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1))) {
987 BuildMI(BB, PPC::CALLpcrel, 1).addExternalSymbol(ESSDN->getSymbol(), true);
988 } else {
989 Tmp1 = SelectExpr(N.getOperand(1));
990 BuildMI(BB, PPC::OR, 2, PPC::R12).addReg(Tmp1).addReg(Tmp1);
991 BuildMI(BB, PPC::MTCTR, 1).addReg(PPC::R12);
992 BuildMI(BB, PPC::CALLindirect, 3).addImm(20).addImm(0).addReg(PPC::R12);
993 }
994
995 switch (Node->getValueType(0)) {
996 default: assert(0 && "Unknown value type for call result!");
997 case MVT::Other: return 1;
998 case MVT::i1:
999 case MVT::i8:
1000 case MVT::i16:
1001 case MVT::i32:
Nate Begemanc7b09f12005-03-25 08:34:25 +00001002 BuildMI(BB, PPC::OR, 2, Result).addReg(PPC::R3).addReg(PPC::R3);
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001003 if (Node->getValueType(1) == MVT::i32)
Nate Begemanc7b09f12005-03-25 08:34:25 +00001004 BuildMI(BB, PPC::OR, 2, Result+1).addReg(PPC::R4).addReg(PPC::R4);
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001005 break;
1006 case MVT::f32:
1007 case MVT::f64:
1008 BuildMI(BB, PPC::FMR, 1, Result).addReg(PPC::F1);
1009 break;
1010 }
1011 return Result+N.ResNo;
1012 }
Nate Begemana9795f82005-03-24 04:41:43 +00001013
1014 case ISD::SIGN_EXTEND:
1015 case ISD::SIGN_EXTEND_INREG:
1016 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman9db505c2005-03-28 19:36:43 +00001017 switch(cast<MVTSDNode>(Node)->getExtraValueType()) {
1018 default: Node->dump(); assert(0 && "Unhandled SIGN_EXTEND type"); break;
1019 case MVT::i16:
1020 BuildMI(BB, PPC::EXTSH, 1, Result).addReg(Tmp1);
1021 break;
1022 case MVT::i8:
1023 BuildMI(BB, PPC::EXTSB, 1, Result).addReg(Tmp1);
1024 break;
Nate Begeman74747862005-03-29 22:24:51 +00001025 case MVT::i1:
1026 BuildMI(BB, PPC::SUBFIC, 2, Result).addReg(Tmp1).addSImm(0);
1027 break;
Nate Begeman9db505c2005-03-28 19:36:43 +00001028 }
Nate Begemana9795f82005-03-24 04:41:43 +00001029 return Result;
1030
1031 case ISD::ZERO_EXTEND_INREG:
1032 Tmp1 = SelectExpr(N.getOperand(0));
1033 switch(cast<MVTSDNode>(Node)->getExtraValueType()) {
Nate Begeman9db505c2005-03-28 19:36:43 +00001034 default: Node->dump(); assert(0 && "Unhandled ZERO_EXTEND type"); break;
Nate Begemana9795f82005-03-24 04:41:43 +00001035 case MVT::i16: Tmp2 = 16; break;
1036 case MVT::i8: Tmp2 = 24; break;
1037 case MVT::i1: Tmp2 = 31; break;
1038 }
Nate Begeman33162522005-03-29 21:54:38 +00001039 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(0).addImm(Tmp2)
1040 .addImm(31);
Nate Begemana9795f82005-03-24 04:41:43 +00001041 return Result;
1042
Nate Begemana9795f82005-03-24 04:41:43 +00001043 case ISD::CopyFromReg:
1044 if (Result == 1)
1045 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1046 Tmp1 = dyn_cast<RegSDNode>(Node)->getReg();
1047 BuildMI(BB, PPC::OR, 2, Result).addReg(Tmp1).addReg(Tmp1);
1048 return Result;
1049
1050 case ISD::SHL:
Nate Begeman5e966612005-03-24 06:28:42 +00001051 Tmp1 = SelectExpr(N.getOperand(0));
1052 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1053 Tmp2 = CN->getValue() & 0x1F;
Nate Begeman33162522005-03-29 21:54:38 +00001054 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(Tmp2).addImm(0)
Nate Begeman5e966612005-03-24 06:28:42 +00001055 .addImm(31-Tmp2);
1056 } else {
1057 Tmp2 = SelectExpr(N.getOperand(1));
1058 BuildMI(BB, PPC::SLW, 2, Result).addReg(Tmp1).addReg(Tmp2);
1059 }
1060 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001061
Nate Begeman5e966612005-03-24 06:28:42 +00001062 case ISD::SRL:
1063 Tmp1 = SelectExpr(N.getOperand(0));
1064 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1065 Tmp2 = CN->getValue() & 0x1F;
Nate Begeman33162522005-03-29 21:54:38 +00001066 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(32-Tmp2)
Nate Begeman5e966612005-03-24 06:28:42 +00001067 .addImm(Tmp2).addImm(31);
1068 } else {
1069 Tmp2 = SelectExpr(N.getOperand(1));
1070 BuildMI(BB, PPC::SRW, 2, Result).addReg(Tmp1).addReg(Tmp2);
1071 }
1072 return Result;
1073
1074 case ISD::SRA:
1075 Tmp1 = SelectExpr(N.getOperand(0));
1076 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1077 Tmp2 = CN->getValue() & 0x1F;
1078 BuildMI(BB, PPC::SRAWI, 2, Result).addReg(Tmp1).addImm(Tmp2);
1079 } else {
1080 Tmp2 = SelectExpr(N.getOperand(1));
1081 BuildMI(BB, PPC::SRAW, 2, Result).addReg(Tmp1).addReg(Tmp2);
1082 }
1083 return Result;
1084
Nate Begemana9795f82005-03-24 04:41:43 +00001085 case ISD::ADD:
1086 assert (DestType == MVT::i32 && "Only do arithmetic on i32s!");
1087 Tmp1 = SelectExpr(N.getOperand(0));
1088 switch(canUseAsImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
1089 default: assert(0 && "unhandled result code");
1090 case 0: // No immediate
1091 Tmp2 = SelectExpr(N.getOperand(1));
1092 BuildMI(BB, PPC::ADD, 2, Result).addReg(Tmp1).addReg(Tmp2);
1093 break;
1094 case 1: // Low immediate
1095 BuildMI(BB, PPC::ADDI, 2, Result).addReg(Tmp1).addSImm(Tmp2);
1096 break;
1097 case 2: // Shifted immediate
1098 BuildMI(BB, PPC::ADDIS, 2, Result).addReg(Tmp1).addSImm(Tmp2);
1099 break;
1100 }
1101 return Result;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001102
Nate Begemana9795f82005-03-24 04:41:43 +00001103 case ISD::AND:
1104 case ISD::OR:
1105 case ISD::XOR:
1106 assert (DestType == MVT::i32 && "Only do arithmetic on i32s!");
1107 Tmp1 = SelectExpr(N.getOperand(0));
1108 switch(canUseAsImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
1109 default: assert(0 && "unhandled result code");
1110 case 0: // No immediate
1111 Tmp2 = SelectExpr(N.getOperand(1));
1112 switch (opcode) {
Nate Begeman5e966612005-03-24 06:28:42 +00001113 case ISD::AND: Opc = PPC::AND; break;
1114 case ISD::OR: Opc = PPC::OR; break;
1115 case ISD::XOR: Opc = PPC::XOR; break;
Nate Begemana9795f82005-03-24 04:41:43 +00001116 }
Nate Begeman5e966612005-03-24 06:28:42 +00001117 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +00001118 break;
1119 case 1: // Low immediate
1120 switch (opcode) {
Nate Begeman5e966612005-03-24 06:28:42 +00001121 case ISD::AND: Opc = PPC::ANDIo; break;
1122 case ISD::OR: Opc = PPC::ORI; break;
1123 case ISD::XOR: Opc = PPC::XORI; break;
Nate Begemana9795f82005-03-24 04:41:43 +00001124 }
Nate Begeman5e966612005-03-24 06:28:42 +00001125 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +00001126 break;
1127 case 2: // Shifted immediate
1128 switch (opcode) {
Nate Begeman5e966612005-03-24 06:28:42 +00001129 case ISD::AND: Opc = PPC::ANDISo; break;
1130 case ISD::OR: Opc = PPC::ORIS; break;
1131 case ISD::XOR: Opc = PPC::XORIS; break;
Nate Begemana9795f82005-03-24 04:41:43 +00001132 }
Nate Begeman5e966612005-03-24 06:28:42 +00001133 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +00001134 break;
1135 }
1136 return Result;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001137
1138 case ISD::SUB:
1139 assert (DestType == MVT::i32 && "Only do arithmetic on i32s!");
1140 Tmp1 = SelectExpr(N.getOperand(0));
1141 Tmp2 = SelectExpr(N.getOperand(1));
1142 BuildMI(BB, PPC::SUBF, 2, Result).addReg(Tmp2).addReg(Tmp1);
1143 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001144
Nate Begeman5e966612005-03-24 06:28:42 +00001145 case ISD::MUL:
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001146 assert (DestType == MVT::i32 && "Only do arithmetic on i32s!");
1147 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman307e7442005-03-26 01:28:53 +00001148 if (1 == canUseAsImmediateForOpcode(N.getOperand(1), opcode, Tmp2))
1149 BuildMI(BB, PPC::MULLI, 2, Result).addReg(Tmp1).addSImm(Tmp2);
1150 else {
1151 Tmp2 = SelectExpr(N.getOperand(1));
1152 BuildMI(BB, PPC::MULLW, 2, Result).addReg(Tmp1).addReg(Tmp2);
1153 }
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001154 return Result;
1155
Nate Begemanf3d08f32005-03-29 00:03:27 +00001156 case ISD::SDIV:
1157 case ISD::UDIV:
1158 assert (DestType == MVT::i32 && "Only do arithmetic on i32s!");
1159 Tmp1 = SelectExpr(N.getOperand(0));
1160 Tmp2 = SelectExpr(N.getOperand(1));
1161 Opc = (ISD::UDIV == opcode) ? PPC::DIVWU : PPC::DIVW;
1162 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1163 return Result;
1164
1165 case ISD::UREM:
1166 case ISD::SREM: {
1167 assert (DestType == MVT::i32 && "Only do arithmetic on i32s!");
1168 Tmp1 = SelectExpr(N.getOperand(0));
1169 Tmp2 = SelectExpr(N.getOperand(1));
1170 Tmp3 = MakeReg(MVT::i32);
1171 unsigned Tmp4 = MakeReg(MVT::i32);
1172 Opc = (ISD::UREM == opcode) ? PPC::DIVWU : PPC::DIVW;
1173 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
1174 BuildMI(BB, PPC::MULLW, 2, Tmp4).addReg(Tmp3).addReg(Tmp2);
1175 BuildMI(BB, PPC::SUBF, 2, Result).addReg(Tmp4).addReg(Tmp1);
1176 return Result;
1177 }
1178
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001179 case ISD::ADD_PARTS:
Nate Begemanca12a2b2005-03-28 22:28:37 +00001180 case ISD::SUB_PARTS: {
1181 assert(N.getNumOperands() == 4 && N.getValueType() == MVT::i32 &&
1182 "Not an i64 add/sub!");
1183 // Emit all of the operands.
1184 std::vector<unsigned> InVals;
1185 for (unsigned i = 0, e = N.getNumOperands(); i != e; ++i)
1186 InVals.push_back(SelectExpr(N.getOperand(i)));
1187 if (N.getOpcode() == ISD::ADD_PARTS) {
Nate Begemanf70b5762005-03-28 23:08:54 +00001188 BuildMI(BB, PPC::ADDC, 2, Result+1).addReg(InVals[0]).addReg(InVals[2]);
1189 BuildMI(BB, PPC::ADDE, 2, Result).addReg(InVals[1]).addReg(InVals[3]);
Nate Begemanca12a2b2005-03-28 22:28:37 +00001190 } else {
Nate Begemanf70b5762005-03-28 23:08:54 +00001191 BuildMI(BB, PPC::SUBFC, 2, Result+1).addReg(InVals[2]).addReg(InVals[0]);
1192 BuildMI(BB, PPC::SUBFE, 2, Result).addReg(InVals[3]).addReg(InVals[1]);
Nate Begemanca12a2b2005-03-28 22:28:37 +00001193 }
1194 return Result+N.ResNo;
1195 }
1196
Nate Begemana9795f82005-03-24 04:41:43 +00001197 case ISD::FP_TO_UINT:
1198 case ISD::FP_TO_SINT:
Nate Begeman01d05262005-03-30 01:45:43 +00001199 assert(0 && "FP_TO_S/UINT unimplemented");
Nate Begemana9795f82005-03-24 04:41:43 +00001200 abort();
1201
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001202 case ISD::SETCC:
Nate Begeman33162522005-03-29 21:54:38 +00001203 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Node)) {
1204 bool U = false;
1205 bool IsInteger = MVT::isInteger(SetCC->getOperand(0).getValueType());
1206
Nate Begeman33162522005-03-29 21:54:38 +00001207 // FIXME: Is there a situation in which we would ever need to emit fcmpo?
1208 static const unsigned CompareOpcodes[] =
1209 { PPC::FCMPU, PPC::FCMPU, PPC::CMPW, PPC::CMPLW };
Nate Begeman3e897162005-03-31 23:55:40 +00001210
1211 // Set the branch opcode to use below
1212 Opc = getBCCForSetCC(SetCC->getCondition(), U);
1213
1214 // Try and use an integer compare with immediate, if applicable.
1215 // Normal setcc uses the sign-extended immediate range, unsigned setcc
1216 // uses the zero extended immediate range.
1217 if (IsInteger &&
1218 1 == canUseAsImmediateForOpcode(N.getOperand(1), opcode, Tmp2, U)) {
1219 Tmp1 = SelectExpr(N.getOperand(0));
1220 if (U)
1221 BuildMI(BB, PPC::CMPLWI, 2, PPC::CR0).addReg(Tmp1).addImm(Tmp2);
1222 else
1223 BuildMI(BB, PPC::CMPWI, 2, PPC::CR0).addReg(Tmp1).addSImm(Tmp2);
1224 } else {
1225 Tmp1 = SelectExpr(N.getOperand(0));
1226 Tmp2 = SelectExpr(N.getOperand(1));
1227 unsigned CompareOpc = CompareOpcodes[2 * IsInteger + U];
1228 BuildMI(BB, CompareOpc, 2, PPC::CR0).addReg(Tmp1).addReg(Tmp2);
1229 }
Nate Begeman33162522005-03-29 21:54:38 +00001230
1231 // Create an iterator with which to insert the MBB for copying the false
1232 // value and the MBB to hold the PHI instruction for this SetCC.
1233 MachineBasicBlock *thisMBB = BB;
1234 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1235 ilist<MachineBasicBlock>::iterator It = BB;
1236 ++It;
1237
1238 // thisMBB:
1239 // ...
1240 // cmpTY cr0, r1, r2
1241 // %TrueValue = li 1
1242 // bCC sinkMBB
Nate Begeman33162522005-03-29 21:54:38 +00001243 unsigned TrueValue = MakeReg(MVT::i32);
1244 BuildMI(BB, PPC::LI, 1, TrueValue).addSImm(1);
1245 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
1246 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
1247 BuildMI(BB, Opc, 2).addReg(PPC::CR0).addMBB(sinkMBB);
1248 MachineFunction *F = BB->getParent();
1249 F->getBasicBlockList().insert(It, copy0MBB);
1250 F->getBasicBlockList().insert(It, sinkMBB);
1251 // Update machine-CFG edges
1252 BB->addSuccessor(copy0MBB);
1253 BB->addSuccessor(sinkMBB);
1254
1255 // copy0MBB:
1256 // %FalseValue = li 0
1257 // fallthrough
1258 BB = copy0MBB;
1259 unsigned FalseValue = MakeReg(MVT::i32);
1260 BuildMI(BB, PPC::LI, 1, FalseValue).addSImm(0);
1261 // Update machine-CFG edges
1262 BB->addSuccessor(sinkMBB);
1263
1264 // sinkMBB:
1265 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1266 // ...
1267 BB = sinkMBB;
1268 BuildMI(BB, PPC::PHI, 4, Result).addReg(FalseValue)
1269 .addMBB(copy0MBB).addReg(TrueValue).addMBB(thisMBB);
1270 return Result;
1271 }
1272 assert(0 && "Is this legal?");
1273 return 0;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001274
Nate Begeman74747862005-03-29 22:24:51 +00001275 case ISD::SELECT: {
Nate Begeman74747862005-03-29 22:24:51 +00001276 // Create an iterator with which to insert the MBB for copying the false
1277 // value and the MBB to hold the PHI instruction for this SetCC.
1278 MachineBasicBlock *thisMBB = BB;
1279 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1280 ilist<MachineBasicBlock>::iterator It = BB;
1281 ++It;
1282
Nate Begeman3e897162005-03-31 23:55:40 +00001283 // If the first operand to the select is a SETCC node, then we can fold it
1284 // into the branch that selects which value to return.
1285 SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(N.getOperand(0).Val);
1286 if (SetCC && N.getOperand(0).getOpcode() == ISD::SETCC &&
1287 MVT::isInteger(SetCC->getOperand(0).getValueType())) {
1288 bool U;
1289 Opc = getBCCForSetCC(SetCC->getCondition(), U);
1290 Tmp1 = SelectExpr(SetCC->getOperand(0));
1291
1292 // Pass the optional argument U to canUseAsImmediateForOpcode for SETCC,
1293 // so that it knows whether the SETCC immediate range is signed or not.
1294 if (1 == canUseAsImmediateForOpcode(SetCC->getOperand(1), ISD::SETCC,
1295 Tmp2, U)) {
1296 if (U)
1297 BuildMI(BB, PPC::CMPLWI, 2, PPC::CR0).addReg(Tmp1).addImm(Tmp2);
1298 else
1299 BuildMI(BB, PPC::CMPWI, 2, PPC::CR0).addReg(Tmp1).addSImm(Tmp2);
1300 } else {
1301 Tmp2 = SelectExpr(SetCC->getOperand(1));
1302 BuildMI(BB, U ? PPC::CMPLW : PPC::CMPW, 2, PPC::CR0).addReg(Tmp1)
1303 .addReg(Tmp2);
1304 }
1305 } else {
1306 Tmp1 = SelectExpr(N.getOperand(0)); //Cond
1307 BuildMI(BB, PPC::CMPLWI, 2, PPC::CR0).addReg(Tmp1).addImm(0);
1308 Opc = PPC::BNE;
1309 }
1310
Nate Begeman74747862005-03-29 22:24:51 +00001311 // thisMBB:
1312 // ...
1313 // TrueVal = ...
1314 // cmpTY cr0, r1, r2
1315 // bCC copy1MBB
1316 // fallthrough --> copy0MBB
Nate Begeman74747862005-03-29 22:24:51 +00001317 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
1318 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
1319 unsigned TrueValue = SelectExpr(N.getOperand(1)); //Use if TRUE
Nate Begeman3e897162005-03-31 23:55:40 +00001320 BuildMI(BB, Opc, 2).addReg(PPC::CR0).addMBB(sinkMBB);
Nate Begeman74747862005-03-29 22:24:51 +00001321 MachineFunction *F = BB->getParent();
1322 F->getBasicBlockList().insert(It, copy0MBB);
1323 F->getBasicBlockList().insert(It, sinkMBB);
1324 // Update machine-CFG edges
1325 BB->addSuccessor(copy0MBB);
1326 BB->addSuccessor(sinkMBB);
1327
1328 // copy0MBB:
1329 // %FalseValue = ...
1330 // # fallthrough to sinkMBB
1331 BB = copy0MBB;
1332 unsigned FalseValue = SelectExpr(N.getOperand(2)); //Use if FALSE
1333 // Update machine-CFG edges
1334 BB->addSuccessor(sinkMBB);
1335
1336 // sinkMBB:
1337 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1338 // ...
1339 BB = sinkMBB;
1340 BuildMI(BB, PPC::PHI, 4, Result).addReg(FalseValue)
1341 .addMBB(copy0MBB).addReg(TrueValue).addMBB(thisMBB);
1342
1343 // FIXME: Select i64?
1344 return Result;
1345 }
Nate Begemana9795f82005-03-24 04:41:43 +00001346
1347 case ISD::Constant:
1348 switch (N.getValueType()) {
1349 default: assert(0 && "Cannot use constants of this type!");
1350 case MVT::i1:
1351 BuildMI(BB, PPC::LI, 1, Result)
1352 .addSImm(!cast<ConstantSDNode>(N)->isNullValue());
1353 break;
1354 case MVT::i32:
1355 {
1356 int v = (int)cast<ConstantSDNode>(N)->getSignExtended();
1357 if (v < 32768 && v >= -32768) {
1358 BuildMI(BB, PPC::LI, 1, Result).addSImm(v);
1359 } else {
Nate Begeman5e966612005-03-24 06:28:42 +00001360 Tmp1 = MakeReg(MVT::i32);
1361 BuildMI(BB, PPC::LIS, 1, Tmp1).addSImm(v >> 16);
1362 BuildMI(BB, PPC::ORI, 2, Result).addReg(Tmp1).addImm(v & 0xFFFF);
Nate Begemana9795f82005-03-24 04:41:43 +00001363 }
1364 }
1365 }
1366 return Result;
1367 }
1368
1369 return 0;
1370}
1371
1372void ISel::Select(SDOperand N) {
1373 unsigned Tmp1, Tmp2, Opc;
1374 unsigned opcode = N.getOpcode();
1375
1376 if (!ExprMap.insert(std::make_pair(N, 1)).second)
1377 return; // Already selected.
1378
1379 SDNode *Node = N.Val;
1380
1381 switch (Node->getOpcode()) {
1382 default:
1383 Node->dump(); std::cerr << "\n";
1384 assert(0 && "Node not handled yet!");
1385 case ISD::EntryToken: return; // Noop
1386 case ISD::TokenFactor:
1387 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1388 Select(Node->getOperand(i));
1389 return;
1390 case ISD::ADJCALLSTACKDOWN:
1391 case ISD::ADJCALLSTACKUP:
1392 Select(N.getOperand(0));
1393 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1394 Opc = N.getOpcode() == ISD::ADJCALLSTACKDOWN ? PPC::ADJCALLSTACKDOWN :
1395 PPC::ADJCALLSTACKUP;
1396 BuildMI(BB, Opc, 1).addImm(Tmp1);
1397 return;
1398 case ISD::BR: {
1399 MachineBasicBlock *Dest =
1400 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
Nate Begemana9795f82005-03-24 04:41:43 +00001401 Select(N.getOperand(0));
1402 BuildMI(BB, PPC::B, 1).addMBB(Dest);
1403 return;
1404 }
1405 case ISD::BRCOND:
1406 SelectBranchCC(N);
1407 return;
1408 case ISD::CopyToReg:
1409 Select(N.getOperand(0));
1410 Tmp1 = SelectExpr(N.getOperand(1));
1411 Tmp2 = cast<RegSDNode>(N)->getReg();
1412
1413 if (Tmp1 != Tmp2) {
1414 if (N.getOperand(1).getValueType() == MVT::f64 ||
1415 N.getOperand(1).getValueType() == MVT::f32)
1416 BuildMI(BB, PPC::FMR, 1, Tmp2).addReg(Tmp1);
1417 else
1418 BuildMI(BB, PPC::OR, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
1419 }
1420 return;
1421 case ISD::ImplicitDef:
1422 Select(N.getOperand(0));
1423 BuildMI(BB, PPC::IMPLICIT_DEF, 0, cast<RegSDNode>(N)->getReg());
1424 return;
1425 case ISD::RET:
1426 switch (N.getNumOperands()) {
1427 default:
1428 assert(0 && "Unknown return instruction!");
1429 case 3:
1430 assert(N.getOperand(1).getValueType() == MVT::i32 &&
1431 N.getOperand(2).getValueType() == MVT::i32 &&
1432 "Unknown two-register value!");
1433 Select(N.getOperand(0));
1434 Tmp1 = SelectExpr(N.getOperand(1));
1435 Tmp2 = SelectExpr(N.getOperand(2));
1436 BuildMI(BB, PPC::OR, 2, PPC::R3).addReg(Tmp1).addReg(Tmp1);
1437 BuildMI(BB, PPC::OR, 2, PPC::R4).addReg(Tmp2).addReg(Tmp2);
1438 break;
1439 case 2:
1440 Select(N.getOperand(0));
1441 Tmp1 = SelectExpr(N.getOperand(1));
1442 switch (N.getOperand(1).getValueType()) {
1443 default:
1444 assert(0 && "Unknown return type!");
1445 case MVT::f64:
1446 case MVT::f32:
1447 BuildMI(BB, PPC::FMR, 1, PPC::F1).addReg(Tmp1);
1448 break;
1449 case MVT::i32:
1450 BuildMI(BB, PPC::OR, 2, PPC::R3).addReg(Tmp1).addReg(Tmp1);
1451 break;
1452 }
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001453 case 1:
1454 Select(N.getOperand(0));
1455 break;
Nate Begemana9795f82005-03-24 04:41:43 +00001456 }
1457 BuildMI(BB, PPC::BLR, 0); // Just emit a 'ret' instruction
1458 return;
Nate Begemana9795f82005-03-24 04:41:43 +00001459 case ISD::TRUNCSTORE:
1460 case ISD::STORE:
1461 {
1462 SDOperand Chain = N.getOperand(0);
1463 SDOperand Value = N.getOperand(1);
1464 SDOperand Address = N.getOperand(2);
1465 Select(Chain);
1466
1467 Tmp1 = SelectExpr(Value); //value
1468
1469 if (opcode == ISD::STORE) {
1470 switch(Value.getValueType()) {
1471 default: assert(0 && "unknown Type in store");
1472 case MVT::i32: Opc = PPC::STW; break;
1473 case MVT::f64: Opc = PPC::STFD; break;
1474 case MVT::f32: Opc = PPC::STFS; break;
1475 }
1476 } else { //ISD::TRUNCSTORE
1477 switch(cast<MVTSDNode>(Node)->getExtraValueType()) {
1478 default: assert(0 && "unknown Type in store");
1479 case MVT::i1: //FIXME: DAG does not promote this load
1480 case MVT::i8: Opc = PPC::STB; break;
1481 case MVT::i16: Opc = PPC::STH; break;
1482 }
1483 }
1484
1485 if (Address.getOpcode() == ISD::GlobalAddress)
1486 {
1487 BuildMI(BB, Opc, 2).addReg(Tmp1)
1488 .addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
1489 }
1490 else if(Address.getOpcode() == ISD::FrameIndex)
1491 {
Nate Begeman58f718c2005-03-30 02:23:08 +00001492 Tmp2 = cast<FrameIndexSDNode>(Address)->getIndex();
1493 addFrameReference(BuildMI(BB, Opc, 3).addReg(Tmp1), (int)Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +00001494 }
1495 else
1496 {
1497 int offset;
1498 SelectAddr(Address, Tmp2, offset);
1499 BuildMI(BB, Opc, 3).addReg(Tmp1).addImm(offset).addReg(Tmp2);
1500 }
1501 return;
1502 }
1503 case ISD::EXTLOAD:
1504 case ISD::SEXTLOAD:
1505 case ISD::ZEXTLOAD:
1506 case ISD::LOAD:
1507 case ISD::CopyFromReg:
1508 case ISD::CALL:
1509 case ISD::DYNAMIC_STACKALLOC:
1510 ExprMap.erase(N);
1511 SelectExpr(N);
1512 return;
1513 }
1514 assert(0 && "Should not be reached!");
1515}
1516
1517
1518/// createPPC32PatternInstructionSelector - This pass converts an LLVM function
1519/// into a machine code representation using pattern matching and a machine
1520/// description file.
1521///
1522FunctionPass *llvm::createPPC32ISelPattern(TargetMachine &TM) {
1523 return new ISel(TM);
Chris Lattner246fa632005-03-24 06:16:18 +00001524}
1525