blob: db56384a3829522ab17643ec0e0bfaebc6d35c69 [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);
Nate Begemanfc1b1da2005-04-01 22:34:39 +000056
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()) {
Nate Begemana7e11a42005-04-01 05:57:17 +0000213 Chain = DAG.getNode(ISD::ADJCALLSTACKDOWN, MVT::Other, Chain,
214 DAG.getConstant(NumBytes, getPointerTy()));
Nate Begeman307e7442005-03-26 01:28:53 +0000215 } else {
216 for (unsigned i = 0, e = Args.size(); i != e; ++i)
217 switch (getValueType(Args[i].second)) {
218 default: assert(0 && "Unknown value type!");
219 case MVT::i1:
220 case MVT::i8:
221 case MVT::i16:
222 case MVT::i32:
223 case MVT::f32:
224 NumBytes += 4;
225 break;
226 case MVT::i64:
227 case MVT::f64:
228 NumBytes += 8;
229 break;
230 }
231
232 // Just to be safe, we'll always reserve the full 24 bytes of linkage area
233 // plus 32 bytes of argument space in case any called code gets funky on us.
234 if (NumBytes < 56) NumBytes = 56;
235
236 // Adjust the stack pointer for the new arguments...
237 // These operations are automatically eliminated by the prolog/epilog pass
238 Chain = DAG.getNode(ISD::ADJCALLSTACKDOWN, MVT::Other, Chain,
239 DAG.getConstant(NumBytes, getPointerTy()));
240
241 // Set up a copy of the stack pointer for use loading and storing any
242 // arguments that may not fit in the registers available for argument
243 // passing.
244 SDOperand StackPtr = DAG.getCopyFromReg(PPC::R1, MVT::i32,
245 DAG.getEntryNode());
246
247 // Figure out which arguments are going to go in registers, and which in
248 // memory. Also, if this is a vararg function, floating point operations
249 // must be stored to our stack, and loaded into integer regs as well, if
250 // any integer regs are available for argument passing.
251 unsigned ArgOffset = 24;
252 unsigned GPR_remaining = 8;
253 unsigned FPR_remaining = 13;
Nate Begeman74d73452005-03-31 00:15:26 +0000254
255 std::vector<SDOperand> MemOps;
Nate Begeman307e7442005-03-26 01:28:53 +0000256 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
257 // PtrOff will be used to store the current argument to the stack if a
258 // register cannot be found for it.
259 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
260 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
Nate Begemanf7e43382005-03-26 07:46:36 +0000261 MVT::ValueType ArgVT = getValueType(Args[i].second);
Nate Begeman307e7442005-03-26 01:28:53 +0000262
Nate Begemanf7e43382005-03-26 07:46:36 +0000263 switch (ArgVT) {
Nate Begeman307e7442005-03-26 01:28:53 +0000264 default: assert(0 && "Unexpected ValueType for argument!");
265 case MVT::i1:
266 case MVT::i8:
267 case MVT::i16:
268 // Promote the integer to 32 bits. If the input type is signed use a
269 // sign extend, otherwise use a zero extend.
270 if (Args[i].second->isSigned())
271 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
272 else
273 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
274 // FALL THROUGH
275 case MVT::i32:
276 if (GPR_remaining > 0) {
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000277 args_to_use.push_back(Args[i].first);
Nate Begeman307e7442005-03-26 01:28:53 +0000278 --GPR_remaining;
279 } else {
Nate Begeman74d73452005-03-31 00:15:26 +0000280 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
281 Args[i].first, PtrOff));
Nate Begeman307e7442005-03-26 01:28:53 +0000282 }
283 ArgOffset += 4;
284 break;
285 case MVT::i64:
Nate Begemanf7e43382005-03-26 07:46:36 +0000286 // If we have one free GPR left, we can place the upper half of the i64
287 // in it, and store the other half to the stack. If we have two or more
288 // free GPRs, then we can pass both halves of the i64 in registers.
289 if (GPR_remaining > 0) {
Nate Begemanf2622612005-03-26 02:17:46 +0000290 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
291 Args[i].first, DAG.getConstant(1, MVT::i32));
292 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
293 Args[i].first, DAG.getConstant(0, MVT::i32));
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000294 args_to_use.push_back(Hi);
Nate Begeman74d73452005-03-31 00:15:26 +0000295 --GPR_remaining;
Nate Begeman74d73452005-03-31 00:15:26 +0000296 if (GPR_remaining > 0) {
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000297 args_to_use.push_back(Lo);
Nate Begeman74d73452005-03-31 00:15:26 +0000298 --GPR_remaining;
Nate Begemanf7e43382005-03-26 07:46:36 +0000299 } else {
300 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
301 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
Nate Begeman74d73452005-03-31 00:15:26 +0000302 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
303 Lo, PtrOff));
Nate Begemanf7e43382005-03-26 07:46:36 +0000304 }
Nate Begeman307e7442005-03-26 01:28:53 +0000305 } else {
Nate Begeman74d73452005-03-31 00:15:26 +0000306 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
307 Args[i].first, PtrOff));
Nate Begeman307e7442005-03-26 01:28:53 +0000308 }
309 ArgOffset += 8;
310 break;
311 case MVT::f32:
Nate Begeman307e7442005-03-26 01:28:53 +0000312 case MVT::f64:
Nate Begemanf7e43382005-03-26 07:46:36 +0000313 if (FPR_remaining > 0) {
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000314 args_to_use.push_back(Args[i].first);
315 --FPR_remaining;
Nate Begemanf7e43382005-03-26 07:46:36 +0000316 if (isVarArg) {
Nate Begeman96fc6812005-03-31 02:05:53 +0000317 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Chain,
318 Args[i].first, PtrOff);
319 MemOps.push_back(Store);
Nate Begeman74d73452005-03-31 00:15:26 +0000320 // Float varargs are always shadowed in available integer registers
321 if (GPR_remaining > 0) {
Nate Begeman96fc6812005-03-31 02:05:53 +0000322 SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff);
Nate Begeman74d73452005-03-31 00:15:26 +0000323 MemOps.push_back(Load);
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000324 args_to_use.push_back(Load);
325 --GPR_remaining;
Nate Begeman74d73452005-03-31 00:15:26 +0000326 }
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000327 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
Nate Begeman74d73452005-03-31 00:15:26 +0000328 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
329 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
Nate Begeman96fc6812005-03-31 02:05:53 +0000330 SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff);
Nate Begeman74d73452005-03-31 00:15:26 +0000331 MemOps.push_back(Load);
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000332 args_to_use.push_back(Load);
333 --GPR_remaining;
Nate Begeman74d73452005-03-31 00:15:26 +0000334 }
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000335 } else {
336 // If we have any FPRs remaining, we may also have GPRs remaining.
337 // Args passed in FPRs consume either 1 (f32) or 2 (f64) available
338 // GPRs.
339 if (GPR_remaining > 0) {
340 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
341 --GPR_remaining;
342 }
343 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
344 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
345 --GPR_remaining;
346 }
Nate Begeman74d73452005-03-31 00:15:26 +0000347 }
Nate Begeman307e7442005-03-26 01:28:53 +0000348 } else {
Nate Begeman74d73452005-03-31 00:15:26 +0000349 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
350 Args[i].first, PtrOff));
Nate Begeman307e7442005-03-26 01:28:53 +0000351 }
Nate Begemanf7e43382005-03-26 07:46:36 +0000352 ArgOffset += (ArgVT == MVT::f32) ? 4 : 8;
Nate Begeman307e7442005-03-26 01:28:53 +0000353 break;
354 }
Nate Begemana9795f82005-03-24 04:41:43 +0000355 }
Nate Begeman74d73452005-03-31 00:15:26 +0000356 if (!MemOps.empty())
357 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps);
Nate Begemana9795f82005-03-24 04:41:43 +0000358 }
359
360 std::vector<MVT::ValueType> RetVals;
361 MVT::ValueType RetTyVT = getValueType(RetTy);
362 if (RetTyVT != MVT::isVoid)
363 RetVals.push_back(RetTyVT);
364 RetVals.push_back(MVT::Other);
365
366 SDOperand TheCall = SDOperand(DAG.getCall(RetVals,
367 Chain, Callee, args_to_use), 0);
368 Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
369 Chain = DAG.getNode(ISD::ADJCALLSTACKUP, MVT::Other, Chain,
370 DAG.getConstant(NumBytes, getPointerTy()));
371 return std::make_pair(TheCall, Chain);
372}
373
374std::pair<SDOperand, SDOperand>
375PPC32TargetLowering::LowerVAStart(SDOperand Chain, SelectionDAG &DAG) {
376 //vastart just returns the address of the VarArgsFrameIndex slot.
377 return std::make_pair(DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32), Chain);
378}
379
380std::pair<SDOperand,SDOperand> PPC32TargetLowering::
381LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
382 const Type *ArgTy, SelectionDAG &DAG) {
Nate Begemanc7b09f12005-03-25 08:34:25 +0000383 MVT::ValueType ArgVT = getValueType(ArgTy);
384 SDOperand Result;
385 if (!isVANext) {
386 Result = DAG.getLoad(ArgVT, DAG.getEntryNode(), VAList);
387 } else {
388 unsigned Amt;
389 if (ArgVT == MVT::i32 || ArgVT == MVT::f32)
390 Amt = 4;
391 else {
392 assert((ArgVT == MVT::i64 || ArgVT == MVT::f64) &&
393 "Other types should have been promoted for varargs!");
394 Amt = 8;
395 }
396 Result = DAG.getNode(ISD::ADD, VAList.getValueType(), VAList,
397 DAG.getConstant(Amt, VAList.getValueType()));
398 }
399 return std::make_pair(Result, Chain);
Nate Begemana9795f82005-03-24 04:41:43 +0000400}
401
402
403std::pair<SDOperand, SDOperand> PPC32TargetLowering::
404LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
405 SelectionDAG &DAG) {
Nate Begeman01d05262005-03-30 01:45:43 +0000406 assert(0 && "LowerFrameReturnAddress unimplemented");
Nate Begemana9795f82005-03-24 04:41:43 +0000407 abort();
408}
409
410namespace {
411
412//===--------------------------------------------------------------------===//
413/// ISel - PPC32 specific code to select PPC32 machine instructions for
414/// SelectionDAG operations.
415//===--------------------------------------------------------------------===//
416class ISel : public SelectionDAGISel {
417
418 /// Comment Here.
419 PPC32TargetLowering PPC32Lowering;
420
421 /// ExprMap - As shared expressions are codegen'd, we keep track of which
422 /// vreg the value is produced in, so we only emit one copy of each compiled
423 /// tree.
424 std::map<SDOperand, unsigned> ExprMap;
Nate Begemanc7b09f12005-03-25 08:34:25 +0000425
426 unsigned GlobalBaseReg;
427 bool GlobalBaseInitialized;
Nate Begemana9795f82005-03-24 04:41:43 +0000428
429public:
430 ISel(TargetMachine &TM) : SelectionDAGISel(PPC32Lowering), PPC32Lowering(TM)
431 {}
432
Nate Begemanc7b09f12005-03-25 08:34:25 +0000433 /// runOnFunction - Override this function in order to reset our per-function
434 /// variables.
435 virtual bool runOnFunction(Function &Fn) {
436 // Make sure we re-emit a set of the global base reg if necessary
437 GlobalBaseInitialized = false;
438 return SelectionDAGISel::runOnFunction(Fn);
439 }
440
Nate Begemana9795f82005-03-24 04:41:43 +0000441 /// InstructionSelectBasicBlock - This callback is invoked by
442 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
443 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG) {
444 DEBUG(BB->dump());
445 // Codegen the basic block.
446 Select(DAG.getRoot());
447
448 // Clear state used for selection.
449 ExprMap.clear();
450 }
451
Nate Begemandffcfcc2005-04-01 00:32:34 +0000452 unsigned getGlobalBaseReg();
Nate Begeman6b559972005-04-01 02:59:27 +0000453 unsigned getConstDouble(double floatVal, unsigned Result);
Nate Begemandffcfcc2005-04-01 00:32:34 +0000454 unsigned SelectSetCR0(SDOperand CC);
Nate Begemana9795f82005-03-24 04:41:43 +0000455 unsigned SelectExpr(SDOperand N);
456 unsigned SelectExprFP(SDOperand N, unsigned Result);
457 void Select(SDOperand N);
458
Nate Begeman04730362005-04-01 04:45:11 +0000459 bool SelectAddr(SDOperand N, unsigned& Reg, int& offset);
Nate Begemana9795f82005-03-24 04:41:43 +0000460 void SelectBranchCC(SDOperand N);
461};
462
463/// canUseAsImmediateForOpcode - This method returns a value indicating whether
464/// the ConstantSDNode N can be used as an immediate to Opcode. The return
465/// values are either 0, 1 or 2. 0 indicates that either N is not a
466/// ConstantSDNode, or is not suitable for use by that opcode. A return value
467/// of 1 indicates that the constant may be used in normal immediate form. A
468/// return value of 2 indicates that the constant may be used in shifted
469/// immediate form. If the return value is nonzero, the constant value is
470/// placed in Imm.
471///
472static unsigned canUseAsImmediateForOpcode(SDOperand N, unsigned Opcode,
Nate Begeman3e897162005-03-31 23:55:40 +0000473 unsigned& Imm, bool U = false) {
Nate Begemana9795f82005-03-24 04:41:43 +0000474 if (N.getOpcode() != ISD::Constant) return 0;
475
476 int v = (int)cast<ConstantSDNode>(N)->getSignExtended();
477
478 switch(Opcode) {
479 default: return 0;
480 case ISD::ADD:
481 if (v <= 32767 && v >= -32768) { Imm = v & 0xFFFF; return 1; }
482 if ((v & 0x0000FFFF) == 0) { Imm = v >> 16; return 2; }
483 break;
484 case ISD::AND:
485 case ISD::XOR:
486 case ISD::OR:
487 if (v >= 0 && v <= 65535) { Imm = v & 0xFFFF; return 1; }
488 if ((v & 0x0000FFFF) == 0) { Imm = v >> 16; return 2; }
489 break;
Nate Begeman307e7442005-03-26 01:28:53 +0000490 case ISD::MUL:
491 if (v <= 32767 && v >= -32768) { Imm = v & 0xFFFF; return 1; }
492 break;
Nate Begeman3e897162005-03-31 23:55:40 +0000493 case ISD::SETCC:
494 if (U && (v >= 0 && v <= 65535)) { Imm = v & 0xFFFF; return 1; }
495 if (!U && (v <= 32767 && v >= -32768)) { Imm = v & 0xFFFF; return 1; }
496 break;
Nate Begemana9795f82005-03-24 04:41:43 +0000497 }
498 return 0;
499}
Nate Begeman3e897162005-03-31 23:55:40 +0000500
501/// getBCCForSetCC - Returns the PowerPC condition branch mnemonic corresponding
502/// to Condition. If the Condition is unordered or unsigned, the bool argument
503/// U is set to true, otherwise it is set to false.
504static unsigned getBCCForSetCC(unsigned Condition, bool& U) {
505 U = false;
506 switch (Condition) {
507 default: assert(0 && "Unknown condition!"); abort();
508 case ISD::SETEQ: return PPC::BEQ;
509 case ISD::SETNE: return PPC::BNE;
510 case ISD::SETULT: U = true;
511 case ISD::SETLT: return PPC::BLT;
512 case ISD::SETULE: U = true;
513 case ISD::SETLE: return PPC::BLE;
514 case ISD::SETUGT: U = true;
515 case ISD::SETGT: return PPC::BGT;
516 case ISD::SETUGE: U = true;
517 case ISD::SETGE: return PPC::BGE;
518 }
Nate Begeman04730362005-04-01 04:45:11 +0000519 return 0;
520}
521
522/// IndexedOpForOp - Return the indexed variant for each of the PowerPC load
523/// and store immediate instructions.
524static unsigned IndexedOpForOp(unsigned Opcode) {
525 switch(Opcode) {
526 default: assert(0 && "Unknown opcode!"); abort();
527 case PPC::LBZ: return PPC::LBZX; case PPC::STB: return PPC::STBX;
528 case PPC::LHZ: return PPC::LHZX; case PPC::STH: return PPC::STHX;
529 case PPC::LHA: return PPC::LHAX; case PPC::STW: return PPC::STWX;
530 case PPC::LWZ: return PPC::LWZX; case PPC::STFS: return PPC::STFSX;
531 case PPC::LFS: return PPC::LFSX; case PPC::STFD: return PPC::STFDX;
532 case PPC::LFD: return PPC::LFDX;
533 }
534 return 0;
Nate Begeman3e897162005-03-31 23:55:40 +0000535}
Nate Begemana9795f82005-03-24 04:41:43 +0000536}
537
Nate Begemanc7b09f12005-03-25 08:34:25 +0000538/// getGlobalBaseReg - Output the instructions required to put the
539/// base address to use for accessing globals into a register.
540///
541unsigned ISel::getGlobalBaseReg() {
542 if (!GlobalBaseInitialized) {
543 // Insert the set of GlobalBaseReg into the first MBB of the function
544 MachineBasicBlock &FirstMBB = BB->getParent()->front();
545 MachineBasicBlock::iterator MBBI = FirstMBB.begin();
546 GlobalBaseReg = MakeReg(MVT::i32);
547 BuildMI(FirstMBB, MBBI, PPC::MovePCtoLR, 0, PPC::LR);
548 BuildMI(FirstMBB, MBBI, PPC::MFLR, 1, GlobalBaseReg).addReg(PPC::LR);
549 GlobalBaseInitialized = true;
550 }
551 return GlobalBaseReg;
552}
553
Nate Begeman6b559972005-04-01 02:59:27 +0000554/// getConstDouble - Loads a floating point value into a register, via the
555/// Constant Pool. Optionally takes a register in which to load the value.
556unsigned ISel::getConstDouble(double doubleVal, unsigned Result=0) {
557 unsigned Tmp1 = MakeReg(MVT::i32);
558 if (0 == Result) Result = MakeReg(MVT::f64);
559 MachineConstantPool *CP = BB->getParent()->getConstantPool();
560 ConstantFP *CFP = ConstantFP::get(Type::DoubleTy, doubleVal);
561 unsigned CPI = CP->getConstantPoolIndex(CFP);
562 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp1).addReg(getGlobalBaseReg())
563 .addConstantPoolIndex(CPI);
564 BuildMI(BB, PPC::LFD, 2, Result).addConstantPoolIndex(CPI).addReg(Tmp1);
565 return Result;
566}
567
Nate Begemandffcfcc2005-04-01 00:32:34 +0000568unsigned ISel::SelectSetCR0(SDOperand CC) {
569 unsigned Opc, Tmp1, Tmp2;
570 static const unsigned CompareOpcodes[] =
571 { PPC::FCMPU, PPC::FCMPU, PPC::CMPW, PPC::CMPLW };
572
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>(CC.Val);
576 if (SetCC && CC.getOpcode() == ISD::SETCC) {
577 bool U;
578 Opc = getBCCForSetCC(SetCC->getCondition(), U);
579 Tmp1 = SelectExpr(SetCC->getOperand(0));
580
581 // Pass the optional argument U to canUseAsImmediateForOpcode for SETCC,
582 // so that it knows whether the SETCC immediate range is signed or not.
583 if (1 == canUseAsImmediateForOpcode(SetCC->getOperand(1), ISD::SETCC,
584 Tmp2, U)) {
585 if (U)
586 BuildMI(BB, PPC::CMPLWI, 2, PPC::CR0).addReg(Tmp1).addImm(Tmp2);
587 else
588 BuildMI(BB, PPC::CMPWI, 2, PPC::CR0).addReg(Tmp1).addSImm(Tmp2);
589 } else {
590 bool IsInteger = MVT::isInteger(SetCC->getOperand(0).getValueType());
591 unsigned CompareOpc = CompareOpcodes[2 * IsInteger + U];
592 Tmp2 = SelectExpr(SetCC->getOperand(1));
593 BuildMI(BB, CompareOpc, 2, PPC::CR0).addReg(Tmp1).addReg(Tmp2);
594 }
595 } else {
596 Tmp1 = SelectExpr(CC);
597 BuildMI(BB, PPC::CMPLWI, 2, PPC::CR0).addReg(Tmp1).addImm(0);
598 Opc = PPC::BNE;
599 }
600 return Opc;
601}
602
603/// Check to see if the load is a constant offset from a base register
Nate Begeman04730362005-04-01 04:45:11 +0000604bool ISel::SelectAddr(SDOperand N, unsigned& Reg, int& offset)
Nate Begemana9795f82005-03-24 04:41:43 +0000605{
Nate Begeman96fc6812005-03-31 02:05:53 +0000606 unsigned imm = 0, opcode = N.getOpcode();
Nate Begeman04730362005-04-01 04:45:11 +0000607 if (N.getOpcode() == ISD::ADD) {
608 Reg = SelectExpr(N.getOperand(0));
Nate Begeman96fc6812005-03-31 02:05:53 +0000609 if (1 == canUseAsImmediateForOpcode(N.getOperand(1), opcode, imm)) {
Nate Begeman96fc6812005-03-31 02:05:53 +0000610 offset = imm;
Nate Begeman04730362005-04-01 04:45:11 +0000611 return false;
612 }
613 offset = SelectExpr(N.getOperand(1));
614 return true;
615 }
Nate Begemana9795f82005-03-24 04:41:43 +0000616 Reg = SelectExpr(N);
617 offset = 0;
Nate Begeman04730362005-04-01 04:45:11 +0000618 return false;
Nate Begemana9795f82005-03-24 04:41:43 +0000619}
620
621void ISel::SelectBranchCC(SDOperand N)
622{
623 assert(N.getOpcode() == ISD::BRCOND && "Not a BranchCC???");
624 MachineBasicBlock *Dest =
625 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
Nate Begeman3e897162005-03-31 23:55:40 +0000626
Nate Begemana9795f82005-03-24 04:41:43 +0000627 Select(N.getOperand(0)); //chain
Nate Begemandffcfcc2005-04-01 00:32:34 +0000628 unsigned Opc = SelectSetCR0(N.getOperand(1));
Nate Begeman3e897162005-03-31 23:55:40 +0000629 BuildMI(BB, Opc, 2).addReg(PPC::CR0).addMBB(Dest);
Nate Begemana9795f82005-03-24 04:41:43 +0000630 return;
631}
632
633unsigned ISel::SelectExprFP(SDOperand N, unsigned Result)
634{
635 unsigned Tmp1, Tmp2, Tmp3;
636 unsigned Opc = 0;
637 SDNode *Node = N.Val;
638 MVT::ValueType DestType = N.getValueType();
639 unsigned opcode = N.getOpcode();
640
641 switch (opcode) {
642 default:
643 Node->dump();
644 assert(0 && "Node not handled!\n");
645
Nate Begeman23afcfb2005-03-29 22:48:55 +0000646 case ISD::SELECT: {
Nate Begeman3e897162005-03-31 23:55:40 +0000647 // Attempt to generate FSEL. We can do this whenever we have an FP result,
648 // and an FP comparison in the SetCC node.
649 SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(N.getOperand(0).Val);
650 if (SetCC && N.getOperand(0).getOpcode() == ISD::SETCC &&
651 !MVT::isInteger(SetCC->getOperand(0).getValueType()) &&
652 SetCC->getCondition() != ISD::SETEQ &&
653 SetCC->getCondition() != ISD::SETNE) {
654 MVT::ValueType VT = SetCC->getOperand(0).getValueType();
655 Tmp1 = SelectExpr(SetCC->getOperand(0)); // Val to compare against
656 unsigned TV = SelectExpr(N.getOperand(1)); // Use if TRUE
657 unsigned FV = SelectExpr(N.getOperand(2)); // Use if FALSE
658
659 ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(1));
660 if (CN && (CN->isExactlyValue(-0.0) || CN->isExactlyValue(0.0))) {
661 switch(SetCC->getCondition()) {
662 default: assert(0 && "Invalid FSEL condition"); abort();
663 case ISD::SETULT:
664 case ISD::SETLT:
665 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp1).addReg(FV).addReg(TV);
666 return Result;
667 case ISD::SETUGE:
668 case ISD::SETGE:
669 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp1).addReg(TV).addReg(FV);
670 return Result;
671 case ISD::SETUGT:
672 case ISD::SETGT: {
673 Tmp2 = MakeReg(VT);
674 BuildMI(BB, PPC::FNEG, 1, Tmp2).addReg(Tmp1);
675 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp2).addReg(FV).addReg(TV);
676 return Result;
677 }
678 case ISD::SETULE:
679 case ISD::SETLE: {
680 Tmp2 = MakeReg(VT);
681 BuildMI(BB, PPC::FNEG, 1, Tmp2).addReg(Tmp1);
682 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp2).addReg(TV).addReg(FV);
683 return Result;
684 }
685 }
686 } else {
687 Opc = (MVT::f64 == VT) ? PPC::FSUB : PPC::FSUBS;
688 Tmp2 = SelectExpr(SetCC->getOperand(1));
689 Tmp3 = MakeReg(VT);
690 switch(SetCC->getCondition()) {
691 default: assert(0 && "Invalid FSEL condition"); abort();
692 case ISD::SETULT:
693 case ISD::SETLT:
694 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
695 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(FV).addReg(TV);
696 return Result;
697 case ISD::SETUGE:
698 case ISD::SETGE:
699 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
700 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(TV).addReg(FV);
701 return Result;
702 case ISD::SETUGT:
703 case ISD::SETGT:
704 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp2).addReg(Tmp1);
705 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(FV).addReg(TV);
706 return Result;
707 case ISD::SETULE:
708 case ISD::SETLE:
709 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp2).addReg(Tmp1);
710 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(TV).addReg(FV);
711 return Result;
712 }
713 }
714 assert(0 && "Should never get here");
715 return 0;
716 }
717
Nate Begeman31318e42005-04-01 07:21:30 +0000718 unsigned TrueValue = SelectExpr(N.getOperand(1)); //Use if TRUE
719 unsigned FalseValue = SelectExpr(N.getOperand(2)); //Use if FALSE
Nate Begeman6cb2e1b2005-04-01 08:57:43 +0000720 Opc = SelectSetCR0(N.getOperand(0));
Nate Begeman31318e42005-04-01 07:21:30 +0000721
Nate Begeman23afcfb2005-03-29 22:48:55 +0000722 // Create an iterator with which to insert the MBB for copying the false
723 // value and the MBB to hold the PHI instruction for this SetCC.
724 MachineBasicBlock *thisMBB = BB;
725 const BasicBlock *LLVM_BB = BB->getBasicBlock();
726 ilist<MachineBasicBlock>::iterator It = BB;
727 ++It;
728
729 // thisMBB:
730 // ...
731 // TrueVal = ...
732 // cmpTY cr0, r1, r2
733 // bCC copy1MBB
734 // fallthrough --> copy0MBB
Nate Begeman23afcfb2005-03-29 22:48:55 +0000735 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
736 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
Nate Begeman6cb2e1b2005-04-01 08:57:43 +0000737 BuildMI(BB, Opc, 2).addReg(PPC::CR0).addMBB(sinkMBB);
Nate Begeman23afcfb2005-03-29 22:48:55 +0000738 MachineFunction *F = BB->getParent();
739 F->getBasicBlockList().insert(It, copy0MBB);
740 F->getBasicBlockList().insert(It, sinkMBB);
741 // Update machine-CFG edges
742 BB->addSuccessor(copy0MBB);
743 BB->addSuccessor(sinkMBB);
744
745 // copy0MBB:
746 // %FalseValue = ...
747 // # fallthrough to sinkMBB
748 BB = copy0MBB;
Nate Begeman23afcfb2005-03-29 22:48:55 +0000749 // Update machine-CFG edges
750 BB->addSuccessor(sinkMBB);
751
752 // sinkMBB:
753 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
754 // ...
755 BB = sinkMBB;
756 BuildMI(BB, PPC::PHI, 4, Result).addReg(FalseValue)
757 .addMBB(copy0MBB).addReg(TrueValue).addMBB(thisMBB);
758 return Result;
759 }
Nate Begemana9795f82005-03-24 04:41:43 +0000760
761 case ISD::FP_ROUND:
762 assert (DestType == MVT::f32 &&
763 N.getOperand(0).getValueType() == MVT::f64 &&
764 "only f64 to f32 conversion supported here");
765 Tmp1 = SelectExpr(N.getOperand(0));
766 BuildMI(BB, PPC::FRSP, 1, Result).addReg(Tmp1);
767 return Result;
768
769 case ISD::FP_EXTEND:
770 assert (DestType == MVT::f64 &&
771 N.getOperand(0).getValueType() == MVT::f32 &&
772 "only f32 to f64 conversion supported here");
773 Tmp1 = SelectExpr(N.getOperand(0));
774 BuildMI(BB, PPC::FMR, 1, Result).addReg(Tmp1);
775 return Result;
776
777 case ISD::CopyFromReg:
Nate Begemanf2622612005-03-26 02:17:46 +0000778 if (Result == 1)
779 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
780 Tmp1 = dyn_cast<RegSDNode>(Node)->getReg();
781 BuildMI(BB, PPC::FMR, 1, Result).addReg(Tmp1);
782 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +0000783
Nate Begeman6d369cc2005-04-01 01:08:07 +0000784 case ISD::ConstantFP: {
Nate Begeman6d369cc2005-04-01 01:08:07 +0000785 ConstantFPSDNode *CN = cast<ConstantFPSDNode>(N);
Nate Begeman6b559972005-04-01 02:59:27 +0000786 Result = getConstDouble(CN->getValue(), Result);
Nate Begeman6d369cc2005-04-01 01:08:07 +0000787 return Result;
788 }
Nate Begemana9795f82005-03-24 04:41:43 +0000789
790 case ISD::MUL:
791 case ISD::ADD:
792 case ISD::SUB:
793 case ISD::SDIV:
794 switch( opcode ) {
795 case ISD::MUL: Opc = DestType == MVT::f64 ? PPC::FMUL : PPC::FMULS; break;
796 case ISD::ADD: Opc = DestType == MVT::f64 ? PPC::FADD : PPC::FADDS; break;
797 case ISD::SUB: Opc = DestType == MVT::f64 ? PPC::FSUB : PPC::FSUBS; break;
798 case ISD::SDIV: Opc = DestType == MVT::f64 ? PPC::FDIV : PPC::FDIVS; break;
799 };
Nate Begemana9795f82005-03-24 04:41:43 +0000800 Tmp1 = SelectExpr(N.getOperand(0));
801 Tmp2 = SelectExpr(N.getOperand(1));
802 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
803 return Result;
804
Nate Begemana9795f82005-03-24 04:41:43 +0000805 case ISD::UINT_TO_FP:
Nate Begemanfdcf3412005-03-30 19:38:35 +0000806 case ISD::SINT_TO_FP: {
807 assert (N.getOperand(0).getValueType() == MVT::i32
808 && "int to float must operate on i32");
809 bool IsUnsigned = (ISD::UINT_TO_FP == opcode);
810 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
811 Tmp2 = MakeReg(MVT::f64); // temp reg to load the integer value into
812 Tmp3 = MakeReg(MVT::i32); // temp reg to hold the conversion constant
813 unsigned ConstF = MakeReg(MVT::f64); // temp reg to hold the fp constant
814
815 int FrameIdx = BB->getParent()->getFrameInfo()->CreateStackObject(8, 8);
816 MachineConstantPool *CP = BB->getParent()->getConstantPool();
817
818 // FIXME: pull this FP constant generation stuff out into something like
819 // the simple ISel's getReg.
820 if (IsUnsigned) {
821 ConstantFP *CFP = ConstantFP::get(Type::DoubleTy, 0x1.000000p52);
822 unsigned CPI = CP->getConstantPoolIndex(CFP);
823 // Load constant fp value
824 unsigned Tmp4 = MakeReg(MVT::i32);
825 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp4).addReg(getGlobalBaseReg())
826 .addConstantPoolIndex(CPI);
827 BuildMI(BB, PPC::LFD, 2, ConstF).addConstantPoolIndex(CPI).addReg(Tmp4);
828 // Store the hi & low halves of the fp value, currently in int regs
829 BuildMI(BB, PPC::LIS, 1, Tmp3).addSImm(0x4330);
830 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(Tmp3), FrameIdx);
831 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(Tmp1), FrameIdx, 4);
832 addFrameReference(BuildMI(BB, PPC::LFD, 2, Tmp2), FrameIdx);
833 // Generate the return value with a subtract
834 BuildMI(BB, PPC::FSUB, 2, Result).addReg(Tmp2).addReg(ConstF);
835 } else {
836 ConstantFP *CFP = ConstantFP::get(Type::DoubleTy, 0x1.000008p52);
837 unsigned CPI = CP->getConstantPoolIndex(CFP);
838 // Load constant fp value
839 unsigned Tmp4 = MakeReg(MVT::i32);
840 unsigned TmpL = MakeReg(MVT::i32);
841 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp4).addReg(getGlobalBaseReg())
842 .addConstantPoolIndex(CPI);
843 BuildMI(BB, PPC::LFD, 2, ConstF).addConstantPoolIndex(CPI).addReg(Tmp4);
844 // Store the hi & low halves of the fp value, currently in int regs
845 BuildMI(BB, PPC::LIS, 1, Tmp3).addSImm(0x4330);
846 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(Tmp3), FrameIdx);
847 BuildMI(BB, PPC::XORIS, 2, TmpL).addReg(Tmp1).addImm(0x8000);
848 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(TmpL), FrameIdx, 4);
849 addFrameReference(BuildMI(BB, PPC::LFD, 2, Tmp2), FrameIdx);
850 // Generate the return value with a subtract
851 BuildMI(BB, PPC::FSUB, 2, Result).addReg(Tmp2).addReg(ConstF);
852 }
853 return Result;
854 }
Nate Begemana9795f82005-03-24 04:41:43 +0000855 }
Nate Begeman6b559972005-04-01 02:59:27 +0000856 assert(0 && "Should never get here");
Nate Begemana9795f82005-03-24 04:41:43 +0000857 return 0;
858}
859
860unsigned ISel::SelectExpr(SDOperand N) {
861 unsigned Result;
862 unsigned Tmp1, Tmp2, Tmp3;
863 unsigned Opc = 0;
864 unsigned opcode = N.getOpcode();
865
866 SDNode *Node = N.Val;
867 MVT::ValueType DestType = N.getValueType();
868
869 unsigned &Reg = ExprMap[N];
870 if (Reg) return Reg;
871
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000872 if (N.getOpcode() != ISD::CALL && N.getOpcode() != ISD::ADD_PARTS &&
873 N.getOpcode() != ISD::SUB_PARTS)
Nate Begemana9795f82005-03-24 04:41:43 +0000874 Reg = Result = (N.getValueType() != MVT::Other) ?
875 MakeReg(N.getValueType()) : 1;
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000876 else {
877 // If this is a call instruction, make sure to prepare ALL of the result
878 // values as well as the chain.
879 if (N.getOpcode() == ISD::CALL) {
880 if (Node->getNumValues() == 1)
881 Reg = Result = 1; // Void call, just a chain.
882 else {
883 Result = MakeReg(Node->getValueType(0));
884 ExprMap[N.getValue(0)] = Result;
885 for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
886 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
887 ExprMap[SDOperand(Node, Node->getNumValues()-1)] = 1;
888 }
889 } else {
890 Result = MakeReg(Node->getValueType(0));
891 ExprMap[N.getValue(0)] = Result;
892 for (unsigned i = 1, e = N.Val->getNumValues(); i != e; ++i)
893 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
894 }
895 }
896
897 if (DestType == MVT::f64 || DestType == MVT::f32)
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000898 if (ISD::LOAD != opcode && ISD::EXTLOAD != opcode && ISD::UNDEF != opcode)
Nate Begeman74d73452005-03-31 00:15:26 +0000899 return SelectExprFP(N, Result);
Nate Begemana9795f82005-03-24 04:41:43 +0000900
901 switch (opcode) {
902 default:
903 Node->dump();
904 assert(0 && "Node not handled!\n");
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000905 case ISD::UNDEF:
906 if (Result != 1)
907 ExprMap[N.getValue(1)] = 1;
908 else
909 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
910 BuildMI(BB, PPC::IMPLICIT_DEF, 0, Result);
911 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +0000912 case ISD::DYNAMIC_STACKALLOC:
Nate Begeman5e966612005-03-24 06:28:42 +0000913 // Generate both result values. FIXME: Need a better commment here?
914 if (Result != 1)
915 ExprMap[N.getValue(1)] = 1;
916 else
917 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
918
919 // FIXME: We are currently ignoring the requested alignment for handling
920 // greater than the stack alignment. This will need to be revisited at some
921 // point. Align = N.getOperand(2);
922 if (!isa<ConstantSDNode>(N.getOperand(2)) ||
923 cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) {
924 std::cerr << "Cannot allocate stack object with greater alignment than"
925 << " the stack alignment yet!";
926 abort();
927 }
928 Select(N.getOperand(0));
929 Tmp1 = SelectExpr(N.getOperand(1));
930 // Subtract size from stack pointer, thereby allocating some space.
931 BuildMI(BB, PPC::SUBF, 2, PPC::R1).addReg(Tmp1).addReg(PPC::R1);
932 // Put a pointer to the space into the result register by copying the SP
933 BuildMI(BB, PPC::OR, 2, Result).addReg(PPC::R1).addReg(PPC::R1);
934 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +0000935
936 case ISD::ConstantPool:
Nate Begemanca12a2b2005-03-28 22:28:37 +0000937 Tmp1 = cast<ConstantPoolSDNode>(N)->getIndex();
938 Tmp2 = MakeReg(MVT::i32);
939 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp2).addReg(getGlobalBaseReg())
940 .addConstantPoolIndex(Tmp1);
941 BuildMI(BB, PPC::LA, 2, Result).addReg(Tmp2).addConstantPoolIndex(Tmp1);
942 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +0000943
944 case ISD::FrameIndex:
Nate Begemanf3d08f32005-03-29 00:03:27 +0000945 Tmp1 = cast<FrameIndexSDNode>(N)->getIndex();
Nate Begeman58f718c2005-03-30 02:23:08 +0000946 addFrameReference(BuildMI(BB, PPC::ADDI, 2, Result), (int)Tmp1, 0, false);
Nate Begemanf3d08f32005-03-29 00:03:27 +0000947 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +0000948
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000949 case ISD::GlobalAddress: {
950 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
Nate Begemanca12a2b2005-03-28 22:28:37 +0000951 Tmp1 = MakeReg(MVT::i32);
Nate Begemanc7b09f12005-03-25 08:34:25 +0000952 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp1).addReg(getGlobalBaseReg())
953 .addGlobalAddress(GV);
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000954 if (GV->hasWeakLinkage() || GV->isExternal()) {
955 BuildMI(BB, PPC::LWZ, 2, Result).addGlobalAddress(GV).addReg(Tmp1);
956 } else {
957 BuildMI(BB, PPC::LA, 2, Result).addReg(Tmp1).addGlobalAddress(GV);
958 }
959 return Result;
960 }
961
Nate Begeman5e966612005-03-24 06:28:42 +0000962 case ISD::LOAD:
Nate Begemana9795f82005-03-24 04:41:43 +0000963 case ISD::EXTLOAD:
964 case ISD::ZEXTLOAD:
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000965 case ISD::SEXTLOAD: {
Nate Begeman9db505c2005-03-28 19:36:43 +0000966 MVT::ValueType TypeBeingLoaded = (ISD::LOAD == opcode) ?
967 Node->getValueType(0) : cast<MVTSDNode>(Node)->getExtraValueType();
Nate Begeman74d73452005-03-31 00:15:26 +0000968 bool sext = (ISD::SEXTLOAD == opcode);
969 bool byte = (MVT::i8 == TypeBeingLoaded);
970
Nate Begeman5e966612005-03-24 06:28:42 +0000971 // Make sure we generate both values.
972 if (Result != 1)
973 ExprMap[N.getValue(1)] = 1; // Generate the token
974 else
975 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
976
977 SDOperand Chain = N.getOperand(0);
978 SDOperand Address = N.getOperand(1);
979 Select(Chain);
980
Nate Begeman9db505c2005-03-28 19:36:43 +0000981 switch (TypeBeingLoaded) {
Nate Begeman74d73452005-03-31 00:15:26 +0000982 default: Node->dump(); assert(0 && "Cannot load this type!");
Nate Begeman9db505c2005-03-28 19:36:43 +0000983 case MVT::i1: Opc = PPC::LBZ; break;
984 case MVT::i8: Opc = PPC::LBZ; break;
985 case MVT::i16: Opc = sext ? PPC::LHA : PPC::LHZ; break;
986 case MVT::i32: Opc = PPC::LWZ; break;
Nate Begeman74d73452005-03-31 00:15:26 +0000987 case MVT::f32: Opc = PPC::LFS; break;
988 case MVT::f64: Opc = PPC::LFD; break;
Nate Begeman5e966612005-03-24 06:28:42 +0000989 }
990
Nate Begeman74d73452005-03-31 00:15:26 +0000991 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Address)) {
992 Tmp1 = MakeReg(MVT::i32);
993 int CPI = CP->getIndex();
994 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp1).addReg(getGlobalBaseReg())
995 .addConstantPoolIndex(CPI);
996 BuildMI(BB, Opc, 2, Result).addConstantPoolIndex(CPI).addReg(Tmp1);
Nate Begeman9db505c2005-03-28 19:36:43 +0000997 }
Nate Begeman74d73452005-03-31 00:15:26 +0000998 else if(Address.getOpcode() == ISD::FrameIndex) {
Nate Begeman58f718c2005-03-30 02:23:08 +0000999 Tmp1 = cast<FrameIndexSDNode>(Address)->getIndex();
1000 addFrameReference(BuildMI(BB, Opc, 2, Result), (int)Tmp1);
Nate Begeman5e966612005-03-24 06:28:42 +00001001 } else {
1002 int offset;
Nate Begeman04730362005-04-01 04:45:11 +00001003 bool idx = SelectAddr(Address, Tmp1, offset);
1004 if (idx) {
1005 Opc = IndexedOpForOp(Opc);
1006 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(offset);
1007 } else {
1008 BuildMI(BB, Opc, 2, Result).addSImm(offset).addReg(Tmp1);
1009 }
Nate Begeman5e966612005-03-24 06:28:42 +00001010 }
1011 return Result;
1012 }
1013
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001014 case ISD::CALL: {
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001015 unsigned GPR_idx = 0, FPR_idx = 0;
1016 static const unsigned GPR[] = {
1017 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
1018 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
1019 };
1020 static const unsigned FPR[] = {
1021 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
1022 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
1023 };
1024
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001025 // Lower the chain for this call.
1026 Select(N.getOperand(0));
1027 ExprMap[N.getValue(Node->getNumValues()-1)] = 1;
Nate Begeman74d73452005-03-31 00:15:26 +00001028
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001029 // Load the register args to virtual regs
1030 std::vector<unsigned> ArgVR;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001031 for(int i = 2, e = Node->getNumOperands(); i < e; ++i)
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001032 ArgVR.push_back(SelectExpr(N.getOperand(i)));
1033
1034 // Copy the virtual registers into the appropriate argument register
1035 for(int i = 0, e = ArgVR.size(); i < e; ++i) {
1036 switch(N.getOperand(i+2).getValueType()) {
1037 default: Node->dump(); assert(0 && "Unknown value type for call");
1038 case MVT::i1:
1039 case MVT::i8:
1040 case MVT::i16:
1041 case MVT::i32:
1042 assert(GPR_idx < 8 && "Too many int args");
1043 if (N.getOperand(i+2).getOpcode() != ISD::UNDEF)
1044 BuildMI(BB, PPC::OR,2,GPR[GPR_idx]).addReg(ArgVR[i]).addReg(ArgVR[i]);
1045 ++GPR_idx;
1046 break;
1047 case MVT::f64:
1048 case MVT::f32:
1049 assert(FPR_idx < 13 && "Too many fp args");
1050 BuildMI(BB, PPC::FMR, 1, FPR[FPR_idx]).addReg(ArgVR[i]);
1051 ++FPR_idx;
1052 break;
1053 }
1054 }
1055
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001056 // Emit the correct call instruction based on the type of symbol called.
1057 if (GlobalAddressSDNode *GASD =
1058 dyn_cast<GlobalAddressSDNode>(N.getOperand(1))) {
1059 BuildMI(BB, PPC::CALLpcrel, 1).addGlobalAddress(GASD->getGlobal(), true);
1060 } else if (ExternalSymbolSDNode *ESSDN =
1061 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1))) {
1062 BuildMI(BB, PPC::CALLpcrel, 1).addExternalSymbol(ESSDN->getSymbol(), true);
1063 } else {
1064 Tmp1 = SelectExpr(N.getOperand(1));
1065 BuildMI(BB, PPC::OR, 2, PPC::R12).addReg(Tmp1).addReg(Tmp1);
1066 BuildMI(BB, PPC::MTCTR, 1).addReg(PPC::R12);
1067 BuildMI(BB, PPC::CALLindirect, 3).addImm(20).addImm(0).addReg(PPC::R12);
1068 }
1069
1070 switch (Node->getValueType(0)) {
1071 default: assert(0 && "Unknown value type for call result!");
1072 case MVT::Other: return 1;
1073 case MVT::i1:
1074 case MVT::i8:
1075 case MVT::i16:
1076 case MVT::i32:
Nate Begemanc7b09f12005-03-25 08:34:25 +00001077 BuildMI(BB, PPC::OR, 2, Result).addReg(PPC::R3).addReg(PPC::R3);
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001078 if (Node->getValueType(1) == MVT::i32)
Nate Begemanc7b09f12005-03-25 08:34:25 +00001079 BuildMI(BB, PPC::OR, 2, Result+1).addReg(PPC::R4).addReg(PPC::R4);
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001080 break;
1081 case MVT::f32:
1082 case MVT::f64:
1083 BuildMI(BB, PPC::FMR, 1, Result).addReg(PPC::F1);
1084 break;
1085 }
1086 return Result+N.ResNo;
1087 }
Nate Begemana9795f82005-03-24 04:41:43 +00001088
1089 case ISD::SIGN_EXTEND:
1090 case ISD::SIGN_EXTEND_INREG:
1091 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman9db505c2005-03-28 19:36:43 +00001092 switch(cast<MVTSDNode>(Node)->getExtraValueType()) {
1093 default: Node->dump(); assert(0 && "Unhandled SIGN_EXTEND type"); break;
1094 case MVT::i16:
1095 BuildMI(BB, PPC::EXTSH, 1, Result).addReg(Tmp1);
1096 break;
1097 case MVT::i8:
1098 BuildMI(BB, PPC::EXTSB, 1, Result).addReg(Tmp1);
1099 break;
Nate Begeman74747862005-03-29 22:24:51 +00001100 case MVT::i1:
1101 BuildMI(BB, PPC::SUBFIC, 2, Result).addReg(Tmp1).addSImm(0);
1102 break;
Nate Begeman9db505c2005-03-28 19:36:43 +00001103 }
Nate Begemana9795f82005-03-24 04:41:43 +00001104 return Result;
1105
1106 case ISD::ZERO_EXTEND_INREG:
1107 Tmp1 = SelectExpr(N.getOperand(0));
1108 switch(cast<MVTSDNode>(Node)->getExtraValueType()) {
Nate Begeman9db505c2005-03-28 19:36:43 +00001109 default: Node->dump(); assert(0 && "Unhandled ZERO_EXTEND type"); break;
Nate Begemana9795f82005-03-24 04:41:43 +00001110 case MVT::i16: Tmp2 = 16; break;
1111 case MVT::i8: Tmp2 = 24; break;
1112 case MVT::i1: Tmp2 = 31; break;
1113 }
Nate Begeman33162522005-03-29 21:54:38 +00001114 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(0).addImm(Tmp2)
1115 .addImm(31);
Nate Begemana9795f82005-03-24 04:41:43 +00001116 return Result;
1117
Nate Begemana9795f82005-03-24 04:41:43 +00001118 case ISD::CopyFromReg:
1119 if (Result == 1)
1120 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1121 Tmp1 = dyn_cast<RegSDNode>(Node)->getReg();
1122 BuildMI(BB, PPC::OR, 2, Result).addReg(Tmp1).addReg(Tmp1);
1123 return Result;
1124
1125 case ISD::SHL:
Nate Begeman5e966612005-03-24 06:28:42 +00001126 Tmp1 = SelectExpr(N.getOperand(0));
1127 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1128 Tmp2 = CN->getValue() & 0x1F;
Nate Begeman33162522005-03-29 21:54:38 +00001129 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(Tmp2).addImm(0)
Nate Begeman5e966612005-03-24 06:28:42 +00001130 .addImm(31-Tmp2);
1131 } else {
1132 Tmp2 = SelectExpr(N.getOperand(1));
1133 BuildMI(BB, PPC::SLW, 2, Result).addReg(Tmp1).addReg(Tmp2);
1134 }
1135 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001136
Nate Begeman5e966612005-03-24 06:28:42 +00001137 case ISD::SRL:
1138 Tmp1 = SelectExpr(N.getOperand(0));
1139 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1140 Tmp2 = CN->getValue() & 0x1F;
Nate Begeman33162522005-03-29 21:54:38 +00001141 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(32-Tmp2)
Nate Begeman5e966612005-03-24 06:28:42 +00001142 .addImm(Tmp2).addImm(31);
1143 } else {
1144 Tmp2 = SelectExpr(N.getOperand(1));
1145 BuildMI(BB, PPC::SRW, 2, Result).addReg(Tmp1).addReg(Tmp2);
1146 }
1147 return Result;
1148
1149 case ISD::SRA:
1150 Tmp1 = SelectExpr(N.getOperand(0));
1151 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1152 Tmp2 = CN->getValue() & 0x1F;
1153 BuildMI(BB, PPC::SRAWI, 2, Result).addReg(Tmp1).addImm(Tmp2);
1154 } else {
1155 Tmp2 = SelectExpr(N.getOperand(1));
1156 BuildMI(BB, PPC::SRAW, 2, Result).addReg(Tmp1).addReg(Tmp2);
1157 }
1158 return Result;
1159
Nate Begemana9795f82005-03-24 04:41:43 +00001160 case ISD::ADD:
1161 assert (DestType == MVT::i32 && "Only do arithmetic on i32s!");
1162 Tmp1 = SelectExpr(N.getOperand(0));
1163 switch(canUseAsImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
1164 default: assert(0 && "unhandled result code");
1165 case 0: // No immediate
1166 Tmp2 = SelectExpr(N.getOperand(1));
1167 BuildMI(BB, PPC::ADD, 2, Result).addReg(Tmp1).addReg(Tmp2);
1168 break;
1169 case 1: // Low immediate
1170 BuildMI(BB, PPC::ADDI, 2, Result).addReg(Tmp1).addSImm(Tmp2);
1171 break;
1172 case 2: // Shifted immediate
1173 BuildMI(BB, PPC::ADDIS, 2, Result).addReg(Tmp1).addSImm(Tmp2);
1174 break;
1175 }
1176 return Result;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001177
Nate Begemana9795f82005-03-24 04:41:43 +00001178 case ISD::AND:
1179 case ISD::OR:
1180 case ISD::XOR:
1181 assert (DestType == MVT::i32 && "Only do arithmetic on i32s!");
1182 Tmp1 = SelectExpr(N.getOperand(0));
1183 switch(canUseAsImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
1184 default: assert(0 && "unhandled result code");
1185 case 0: // No immediate
1186 Tmp2 = SelectExpr(N.getOperand(1));
1187 switch (opcode) {
Nate Begeman5e966612005-03-24 06:28:42 +00001188 case ISD::AND: Opc = PPC::AND; break;
1189 case ISD::OR: Opc = PPC::OR; break;
1190 case ISD::XOR: Opc = PPC::XOR; break;
Nate Begemana9795f82005-03-24 04:41:43 +00001191 }
Nate Begeman5e966612005-03-24 06:28:42 +00001192 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +00001193 break;
1194 case 1: // Low immediate
1195 switch (opcode) {
Nate Begeman5e966612005-03-24 06:28:42 +00001196 case ISD::AND: Opc = PPC::ANDIo; break;
1197 case ISD::OR: Opc = PPC::ORI; break;
1198 case ISD::XOR: Opc = PPC::XORI; break;
Nate Begemana9795f82005-03-24 04:41:43 +00001199 }
Nate Begeman5e966612005-03-24 06:28:42 +00001200 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +00001201 break;
1202 case 2: // Shifted immediate
1203 switch (opcode) {
Nate Begeman5e966612005-03-24 06:28:42 +00001204 case ISD::AND: Opc = PPC::ANDISo; break;
1205 case ISD::OR: Opc = PPC::ORIS; break;
1206 case ISD::XOR: Opc = PPC::XORIS; break;
Nate Begemana9795f82005-03-24 04:41:43 +00001207 }
Nate Begeman5e966612005-03-24 06:28:42 +00001208 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +00001209 break;
1210 }
1211 return Result;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001212
1213 case ISD::SUB:
1214 assert (DestType == MVT::i32 && "Only do arithmetic on i32s!");
1215 Tmp1 = SelectExpr(N.getOperand(0));
1216 Tmp2 = SelectExpr(N.getOperand(1));
1217 BuildMI(BB, PPC::SUBF, 2, Result).addReg(Tmp2).addReg(Tmp1);
1218 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001219
Nate Begeman5e966612005-03-24 06:28:42 +00001220 case ISD::MUL:
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001221 assert (DestType == MVT::i32 && "Only do arithmetic on i32s!");
1222 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman307e7442005-03-26 01:28:53 +00001223 if (1 == canUseAsImmediateForOpcode(N.getOperand(1), opcode, Tmp2))
1224 BuildMI(BB, PPC::MULLI, 2, Result).addReg(Tmp1).addSImm(Tmp2);
1225 else {
1226 Tmp2 = SelectExpr(N.getOperand(1));
1227 BuildMI(BB, PPC::MULLW, 2, Result).addReg(Tmp1).addReg(Tmp2);
1228 }
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001229 return Result;
1230
Nate Begemanf3d08f32005-03-29 00:03:27 +00001231 case ISD::SDIV:
1232 case ISD::UDIV:
1233 assert (DestType == MVT::i32 && "Only do arithmetic on i32s!");
1234 Tmp1 = SelectExpr(N.getOperand(0));
1235 Tmp2 = SelectExpr(N.getOperand(1));
1236 Opc = (ISD::UDIV == opcode) ? PPC::DIVWU : PPC::DIVW;
1237 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1238 return Result;
1239
1240 case ISD::UREM:
1241 case ISD::SREM: {
1242 assert (DestType == MVT::i32 && "Only do arithmetic on i32s!");
1243 Tmp1 = SelectExpr(N.getOperand(0));
1244 Tmp2 = SelectExpr(N.getOperand(1));
1245 Tmp3 = MakeReg(MVT::i32);
1246 unsigned Tmp4 = MakeReg(MVT::i32);
1247 Opc = (ISD::UREM == opcode) ? PPC::DIVWU : PPC::DIVW;
1248 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
1249 BuildMI(BB, PPC::MULLW, 2, Tmp4).addReg(Tmp3).addReg(Tmp2);
1250 BuildMI(BB, PPC::SUBF, 2, Result).addReg(Tmp4).addReg(Tmp1);
1251 return Result;
1252 }
1253
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001254 case ISD::ADD_PARTS:
Nate Begemanca12a2b2005-03-28 22:28:37 +00001255 case ISD::SUB_PARTS: {
1256 assert(N.getNumOperands() == 4 && N.getValueType() == MVT::i32 &&
1257 "Not an i64 add/sub!");
1258 // Emit all of the operands.
1259 std::vector<unsigned> InVals;
1260 for (unsigned i = 0, e = N.getNumOperands(); i != e; ++i)
1261 InVals.push_back(SelectExpr(N.getOperand(i)));
1262 if (N.getOpcode() == ISD::ADD_PARTS) {
Nate Begemanf70b5762005-03-28 23:08:54 +00001263 BuildMI(BB, PPC::ADDC, 2, Result+1).addReg(InVals[0]).addReg(InVals[2]);
1264 BuildMI(BB, PPC::ADDE, 2, Result).addReg(InVals[1]).addReg(InVals[3]);
Nate Begemanca12a2b2005-03-28 22:28:37 +00001265 } else {
Nate Begemanf70b5762005-03-28 23:08:54 +00001266 BuildMI(BB, PPC::SUBFC, 2, Result+1).addReg(InVals[2]).addReg(InVals[0]);
1267 BuildMI(BB, PPC::SUBFE, 2, Result).addReg(InVals[3]).addReg(InVals[1]);
Nate Begemanca12a2b2005-03-28 22:28:37 +00001268 }
1269 return Result+N.ResNo;
1270 }
1271
Nate Begemana9795f82005-03-24 04:41:43 +00001272 case ISD::FP_TO_UINT:
Nate Begeman6b559972005-04-01 02:59:27 +00001273 case ISD::FP_TO_SINT: {
1274 bool U = (ISD::FP_TO_UINT == opcode);
1275 Tmp1 = SelectExpr(N.getOperand(0));
1276 if (!U) {
1277 Tmp2 = MakeReg(MVT::f64);
1278 BuildMI(BB, PPC::FCTIWZ, 1, Tmp2).addReg(Tmp1);
1279 int FrameIdx = BB->getParent()->getFrameInfo()->CreateStackObject(8, 8);
1280 addFrameReference(BuildMI(BB, PPC::STFD, 3).addReg(Tmp2), FrameIdx);
1281 addFrameReference(BuildMI(BB, PPC::LWZ, 2, Result), FrameIdx, 4);
1282 return Result;
1283 } else {
1284 unsigned Zero = getConstDouble(0.0);
1285 unsigned MaxInt = getConstDouble((1LL << 32) - 1);
1286 unsigned Border = getConstDouble(1LL << 31);
1287 unsigned UseZero = MakeReg(MVT::f64);
1288 unsigned UseMaxInt = MakeReg(MVT::f64);
1289 unsigned UseChoice = MakeReg(MVT::f64);
1290 unsigned TmpReg = MakeReg(MVT::f64);
1291 unsigned TmpReg2 = MakeReg(MVT::f64);
1292 unsigned ConvReg = MakeReg(MVT::f64);
1293 unsigned IntTmp = MakeReg(MVT::i32);
1294 unsigned XorReg = MakeReg(MVT::i32);
1295 MachineFunction *F = BB->getParent();
1296 int FrameIdx = F->getFrameInfo()->CreateStackObject(8, 8);
1297 // Update machine-CFG edges
1298 MachineBasicBlock *XorMBB = new MachineBasicBlock(BB->getBasicBlock());
1299 MachineBasicBlock *PhiMBB = new MachineBasicBlock(BB->getBasicBlock());
1300 MachineBasicBlock *OldMBB = BB;
1301 ilist<MachineBasicBlock>::iterator It = BB; ++It;
1302 F->getBasicBlockList().insert(It, XorMBB);
1303 F->getBasicBlockList().insert(It, PhiMBB);
1304 BB->addSuccessor(XorMBB);
1305 BB->addSuccessor(PhiMBB);
1306 // Convert from floating point to unsigned 32-bit value
1307 // Use 0 if incoming value is < 0.0
1308 BuildMI(BB, PPC::FSEL, 3, UseZero).addReg(Tmp1).addReg(Tmp1).addReg(Zero);
1309 // Use 2**32 - 1 if incoming value is >= 2**32
1310 BuildMI(BB, PPC::FSUB, 2, UseMaxInt).addReg(MaxInt).addReg(Tmp1);
1311 BuildMI(BB, PPC::FSEL, 3, UseChoice).addReg(UseMaxInt).addReg(UseZero)
1312 .addReg(MaxInt);
1313 // Subtract 2**31
1314 BuildMI(BB, PPC::FSUB, 2, TmpReg).addReg(UseChoice).addReg(Border);
1315 // Use difference if >= 2**31
1316 BuildMI(BB, PPC::FCMPU, 2, PPC::CR0).addReg(UseChoice).addReg(Border);
1317 BuildMI(BB, PPC::FSEL, 3, TmpReg2).addReg(TmpReg).addReg(TmpReg)
1318 .addReg(UseChoice);
1319 // Convert to integer
1320 BuildMI(BB, PPC::FCTIWZ, 1, ConvReg).addReg(TmpReg2);
1321 addFrameReference(BuildMI(BB, PPC::STFD, 3).addReg(ConvReg), FrameIdx);
1322 addFrameReference(BuildMI(BB, PPC::LWZ, 2, IntTmp), FrameIdx, 4);
1323 BuildMI(BB, PPC::BLT, 2).addReg(PPC::CR0).addMBB(PhiMBB);
1324 BuildMI(BB, PPC::B, 1).addMBB(XorMBB);
1325
1326 // XorMBB:
1327 // add 2**31 if input was >= 2**31
1328 BB = XorMBB;
1329 BuildMI(BB, PPC::XORIS, 2, XorReg).addReg(IntTmp).addImm(0x8000);
1330 XorMBB->addSuccessor(PhiMBB);
1331
1332 // PhiMBB:
1333 // DestReg = phi [ IntTmp, OldMBB ], [ XorReg, XorMBB ]
1334 BB = PhiMBB;
1335 BuildMI(BB, PPC::PHI, 4, Result).addReg(IntTmp).addMBB(OldMBB)
1336 .addReg(XorReg).addMBB(XorMBB);
1337 return Result;
1338 }
1339 assert(0 && "Should never get here");
1340 return 0;
1341 }
Nate Begemana9795f82005-03-24 04:41:43 +00001342
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001343 case ISD::SETCC:
Nate Begeman33162522005-03-29 21:54:38 +00001344 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Node)) {
Nate Begemandffcfcc2005-04-01 00:32:34 +00001345 Opc = SelectSetCR0(N);
Nate Begeman33162522005-03-29 21:54:38 +00001346
Nate Begeman31318e42005-04-01 07:21:30 +00001347 unsigned TrueValue = MakeReg(MVT::i32);
1348 BuildMI(BB, PPC::LI, 1, TrueValue).addSImm(1);
1349 unsigned FalseValue = MakeReg(MVT::i32);
1350 BuildMI(BB, PPC::LI, 1, FalseValue).addSImm(0);
1351
Nate Begeman33162522005-03-29 21:54:38 +00001352 // Create an iterator with which to insert the MBB for copying the false
1353 // value and the MBB to hold the PHI instruction for this SetCC.
1354 MachineBasicBlock *thisMBB = BB;
1355 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1356 ilist<MachineBasicBlock>::iterator It = BB;
1357 ++It;
1358
1359 // thisMBB:
1360 // ...
1361 // cmpTY cr0, r1, r2
1362 // %TrueValue = li 1
1363 // bCC sinkMBB
Nate Begeman33162522005-03-29 21:54:38 +00001364 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
1365 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
1366 BuildMI(BB, Opc, 2).addReg(PPC::CR0).addMBB(sinkMBB);
1367 MachineFunction *F = BB->getParent();
1368 F->getBasicBlockList().insert(It, copy0MBB);
1369 F->getBasicBlockList().insert(It, sinkMBB);
1370 // Update machine-CFG edges
1371 BB->addSuccessor(copy0MBB);
1372 BB->addSuccessor(sinkMBB);
1373
1374 // copy0MBB:
1375 // %FalseValue = li 0
1376 // fallthrough
1377 BB = copy0MBB;
Nate Begeman33162522005-03-29 21:54:38 +00001378 // Update machine-CFG edges
1379 BB->addSuccessor(sinkMBB);
1380
1381 // sinkMBB:
1382 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1383 // ...
1384 BB = sinkMBB;
1385 BuildMI(BB, PPC::PHI, 4, Result).addReg(FalseValue)
1386 .addMBB(copy0MBB).addReg(TrueValue).addMBB(thisMBB);
1387 return Result;
1388 }
1389 assert(0 && "Is this legal?");
1390 return 0;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001391
Nate Begeman74747862005-03-29 22:24:51 +00001392 case ISD::SELECT: {
Chris Lattner30710192005-04-01 07:10:02 +00001393 unsigned TrueValue = SelectExpr(N.getOperand(1)); //Use if TRUE
1394 unsigned FalseValue = SelectExpr(N.getOperand(2)); //Use if FALSE
Nate Begeman6cb2e1b2005-04-01 08:57:43 +00001395 Opc = SelectSetCR0(N.getOperand(0));
Chris Lattner30710192005-04-01 07:10:02 +00001396
Nate Begeman74747862005-03-29 22:24:51 +00001397 // Create an iterator with which to insert the MBB for copying the false
1398 // value and the MBB to hold the PHI instruction for this SetCC.
1399 MachineBasicBlock *thisMBB = BB;
1400 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1401 ilist<MachineBasicBlock>::iterator It = BB;
1402 ++It;
1403
1404 // thisMBB:
1405 // ...
1406 // TrueVal = ...
1407 // cmpTY cr0, r1, r2
1408 // bCC copy1MBB
1409 // fallthrough --> copy0MBB
Nate Begeman74747862005-03-29 22:24:51 +00001410 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
1411 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
Nate Begeman3e897162005-03-31 23:55:40 +00001412 BuildMI(BB, Opc, 2).addReg(PPC::CR0).addMBB(sinkMBB);
Nate Begeman74747862005-03-29 22:24:51 +00001413 MachineFunction *F = BB->getParent();
1414 F->getBasicBlockList().insert(It, copy0MBB);
1415 F->getBasicBlockList().insert(It, sinkMBB);
1416 // Update machine-CFG edges
1417 BB->addSuccessor(copy0MBB);
1418 BB->addSuccessor(sinkMBB);
1419
1420 // copy0MBB:
1421 // %FalseValue = ...
1422 // # fallthrough to sinkMBB
1423 BB = copy0MBB;
Nate Begeman74747862005-03-29 22:24:51 +00001424 // Update machine-CFG edges
1425 BB->addSuccessor(sinkMBB);
1426
1427 // sinkMBB:
1428 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1429 // ...
1430 BB = sinkMBB;
1431 BuildMI(BB, PPC::PHI, 4, Result).addReg(FalseValue)
1432 .addMBB(copy0MBB).addReg(TrueValue).addMBB(thisMBB);
1433
1434 // FIXME: Select i64?
1435 return Result;
1436 }
Nate Begemana9795f82005-03-24 04:41:43 +00001437
1438 case ISD::Constant:
1439 switch (N.getValueType()) {
1440 default: assert(0 && "Cannot use constants of this type!");
1441 case MVT::i1:
1442 BuildMI(BB, PPC::LI, 1, Result)
1443 .addSImm(!cast<ConstantSDNode>(N)->isNullValue());
1444 break;
1445 case MVT::i32:
1446 {
1447 int v = (int)cast<ConstantSDNode>(N)->getSignExtended();
1448 if (v < 32768 && v >= -32768) {
1449 BuildMI(BB, PPC::LI, 1, Result).addSImm(v);
1450 } else {
Nate Begeman5e966612005-03-24 06:28:42 +00001451 Tmp1 = MakeReg(MVT::i32);
1452 BuildMI(BB, PPC::LIS, 1, Tmp1).addSImm(v >> 16);
1453 BuildMI(BB, PPC::ORI, 2, Result).addReg(Tmp1).addImm(v & 0xFFFF);
Nate Begemana9795f82005-03-24 04:41:43 +00001454 }
1455 }
1456 }
1457 return Result;
1458 }
1459
1460 return 0;
1461}
1462
1463void ISel::Select(SDOperand N) {
1464 unsigned Tmp1, Tmp2, Opc;
1465 unsigned opcode = N.getOpcode();
1466
1467 if (!ExprMap.insert(std::make_pair(N, 1)).second)
1468 return; // Already selected.
1469
1470 SDNode *Node = N.Val;
1471
1472 switch (Node->getOpcode()) {
1473 default:
1474 Node->dump(); std::cerr << "\n";
1475 assert(0 && "Node not handled yet!");
1476 case ISD::EntryToken: return; // Noop
1477 case ISD::TokenFactor:
1478 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1479 Select(Node->getOperand(i));
1480 return;
1481 case ISD::ADJCALLSTACKDOWN:
1482 case ISD::ADJCALLSTACKUP:
1483 Select(N.getOperand(0));
1484 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1485 Opc = N.getOpcode() == ISD::ADJCALLSTACKDOWN ? PPC::ADJCALLSTACKDOWN :
1486 PPC::ADJCALLSTACKUP;
1487 BuildMI(BB, Opc, 1).addImm(Tmp1);
1488 return;
1489 case ISD::BR: {
1490 MachineBasicBlock *Dest =
1491 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
Nate Begemana9795f82005-03-24 04:41:43 +00001492 Select(N.getOperand(0));
1493 BuildMI(BB, PPC::B, 1).addMBB(Dest);
1494 return;
1495 }
1496 case ISD::BRCOND:
1497 SelectBranchCC(N);
1498 return;
1499 case ISD::CopyToReg:
1500 Select(N.getOperand(0));
1501 Tmp1 = SelectExpr(N.getOperand(1));
1502 Tmp2 = cast<RegSDNode>(N)->getReg();
1503
1504 if (Tmp1 != Tmp2) {
1505 if (N.getOperand(1).getValueType() == MVT::f64 ||
1506 N.getOperand(1).getValueType() == MVT::f32)
1507 BuildMI(BB, PPC::FMR, 1, Tmp2).addReg(Tmp1);
1508 else
1509 BuildMI(BB, PPC::OR, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
1510 }
1511 return;
1512 case ISD::ImplicitDef:
1513 Select(N.getOperand(0));
1514 BuildMI(BB, PPC::IMPLICIT_DEF, 0, cast<RegSDNode>(N)->getReg());
1515 return;
1516 case ISD::RET:
1517 switch (N.getNumOperands()) {
1518 default:
1519 assert(0 && "Unknown return instruction!");
1520 case 3:
1521 assert(N.getOperand(1).getValueType() == MVT::i32 &&
1522 N.getOperand(2).getValueType() == MVT::i32 &&
1523 "Unknown two-register value!");
1524 Select(N.getOperand(0));
1525 Tmp1 = SelectExpr(N.getOperand(1));
1526 Tmp2 = SelectExpr(N.getOperand(2));
1527 BuildMI(BB, PPC::OR, 2, PPC::R3).addReg(Tmp1).addReg(Tmp1);
1528 BuildMI(BB, PPC::OR, 2, PPC::R4).addReg(Tmp2).addReg(Tmp2);
1529 break;
1530 case 2:
1531 Select(N.getOperand(0));
1532 Tmp1 = SelectExpr(N.getOperand(1));
1533 switch (N.getOperand(1).getValueType()) {
1534 default:
1535 assert(0 && "Unknown return type!");
1536 case MVT::f64:
1537 case MVT::f32:
1538 BuildMI(BB, PPC::FMR, 1, PPC::F1).addReg(Tmp1);
1539 break;
1540 case MVT::i32:
1541 BuildMI(BB, PPC::OR, 2, PPC::R3).addReg(Tmp1).addReg(Tmp1);
1542 break;
1543 }
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001544 case 1:
1545 Select(N.getOperand(0));
1546 break;
Nate Begemana9795f82005-03-24 04:41:43 +00001547 }
1548 BuildMI(BB, PPC::BLR, 0); // Just emit a 'ret' instruction
1549 return;
Nate Begemana9795f82005-03-24 04:41:43 +00001550 case ISD::TRUNCSTORE:
1551 case ISD::STORE:
1552 {
1553 SDOperand Chain = N.getOperand(0);
1554 SDOperand Value = N.getOperand(1);
1555 SDOperand Address = N.getOperand(2);
1556 Select(Chain);
1557
1558 Tmp1 = SelectExpr(Value); //value
1559
1560 if (opcode == ISD::STORE) {
1561 switch(Value.getValueType()) {
1562 default: assert(0 && "unknown Type in store");
1563 case MVT::i32: Opc = PPC::STW; break;
1564 case MVT::f64: Opc = PPC::STFD; break;
1565 case MVT::f32: Opc = PPC::STFS; break;
1566 }
1567 } else { //ISD::TRUNCSTORE
1568 switch(cast<MVTSDNode>(Node)->getExtraValueType()) {
1569 default: assert(0 && "unknown Type in store");
1570 case MVT::i1: //FIXME: DAG does not promote this load
1571 case MVT::i8: Opc = PPC::STB; break;
1572 case MVT::i16: Opc = PPC::STH; break;
1573 }
1574 }
1575
Nate Begemana7e11a42005-04-01 05:57:17 +00001576 if(Address.getOpcode() == ISD::FrameIndex)
Nate Begemana9795f82005-03-24 04:41:43 +00001577 {
Nate Begeman58f718c2005-03-30 02:23:08 +00001578 Tmp2 = cast<FrameIndexSDNode>(Address)->getIndex();
1579 addFrameReference(BuildMI(BB, Opc, 3).addReg(Tmp1), (int)Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +00001580 }
1581 else
1582 {
1583 int offset;
Nate Begeman04730362005-04-01 04:45:11 +00001584 bool idx = SelectAddr(Address, Tmp2, offset);
1585 if (idx) {
1586 Opc = IndexedOpForOp(Opc);
1587 BuildMI(BB, Opc, 3).addReg(Tmp1).addReg(Tmp2).addReg(offset);
1588 } else {
1589 BuildMI(BB, Opc, 3).addReg(Tmp1).addImm(offset).addReg(Tmp2);
1590 }
Nate Begemana9795f82005-03-24 04:41:43 +00001591 }
1592 return;
1593 }
1594 case ISD::EXTLOAD:
1595 case ISD::SEXTLOAD:
1596 case ISD::ZEXTLOAD:
1597 case ISD::LOAD:
1598 case ISD::CopyFromReg:
1599 case ISD::CALL:
1600 case ISD::DYNAMIC_STACKALLOC:
1601 ExprMap.erase(N);
1602 SelectExpr(N);
1603 return;
1604 }
1605 assert(0 && "Should not be reached!");
1606}
1607
1608
1609/// createPPC32PatternInstructionSelector - This pass converts an LLVM function
1610/// into a machine code representation using pattern matching and a machine
1611/// description file.
1612///
1613FunctionPass *llvm::createPPC32ISelPattern(TargetMachine &TM) {
1614 return new ISel(TM);
Chris Lattner246fa632005-03-24 06:16:18 +00001615}
1616