blob: a6ebbadf569d6042daccc83f4c9c297fa67646ff [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:
Nate Begeman27523a12005-04-02 00:42:16 +0000491 case ISD::SUB:
Nate Begeman307e7442005-03-26 01:28:53 +0000492 if (v <= 32767 && v >= -32768) { Imm = v & 0xFFFF; return 1; }
493 break;
Nate Begeman3e897162005-03-31 23:55:40 +0000494 case ISD::SETCC:
495 if (U && (v >= 0 && v <= 65535)) { Imm = v & 0xFFFF; return 1; }
496 if (!U && (v <= 32767 && v >= -32768)) { Imm = v & 0xFFFF; return 1; }
497 break;
Nate Begemana9795f82005-03-24 04:41:43 +0000498 }
499 return 0;
500}
Nate Begeman3e897162005-03-31 23:55:40 +0000501
502/// getBCCForSetCC - Returns the PowerPC condition branch mnemonic corresponding
503/// to Condition. If the Condition is unordered or unsigned, the bool argument
504/// U is set to true, otherwise it is set to false.
505static unsigned getBCCForSetCC(unsigned Condition, bool& U) {
506 U = false;
507 switch (Condition) {
508 default: assert(0 && "Unknown condition!"); abort();
509 case ISD::SETEQ: return PPC::BEQ;
510 case ISD::SETNE: return PPC::BNE;
511 case ISD::SETULT: U = true;
512 case ISD::SETLT: return PPC::BLT;
513 case ISD::SETULE: U = true;
514 case ISD::SETLE: return PPC::BLE;
515 case ISD::SETUGT: U = true;
516 case ISD::SETGT: return PPC::BGT;
517 case ISD::SETUGE: U = true;
518 case ISD::SETGE: return PPC::BGE;
519 }
Nate Begeman04730362005-04-01 04:45:11 +0000520 return 0;
521}
522
523/// IndexedOpForOp - Return the indexed variant for each of the PowerPC load
524/// and store immediate instructions.
525static unsigned IndexedOpForOp(unsigned Opcode) {
526 switch(Opcode) {
527 default: assert(0 && "Unknown opcode!"); abort();
528 case PPC::LBZ: return PPC::LBZX; case PPC::STB: return PPC::STBX;
529 case PPC::LHZ: return PPC::LHZX; case PPC::STH: return PPC::STHX;
530 case PPC::LHA: return PPC::LHAX; case PPC::STW: return PPC::STWX;
531 case PPC::LWZ: return PPC::LWZX; case PPC::STFS: return PPC::STFSX;
532 case PPC::LFS: return PPC::LFSX; case PPC::STFD: return PPC::STFDX;
533 case PPC::LFD: return PPC::LFDX;
534 }
535 return 0;
Nate Begeman3e897162005-03-31 23:55:40 +0000536}
Nate Begemana9795f82005-03-24 04:41:43 +0000537}
538
Nate Begemanc7b09f12005-03-25 08:34:25 +0000539/// getGlobalBaseReg - Output the instructions required to put the
540/// base address to use for accessing globals into a register.
541///
542unsigned ISel::getGlobalBaseReg() {
543 if (!GlobalBaseInitialized) {
544 // Insert the set of GlobalBaseReg into the first MBB of the function
545 MachineBasicBlock &FirstMBB = BB->getParent()->front();
546 MachineBasicBlock::iterator MBBI = FirstMBB.begin();
547 GlobalBaseReg = MakeReg(MVT::i32);
548 BuildMI(FirstMBB, MBBI, PPC::MovePCtoLR, 0, PPC::LR);
549 BuildMI(FirstMBB, MBBI, PPC::MFLR, 1, GlobalBaseReg).addReg(PPC::LR);
550 GlobalBaseInitialized = true;
551 }
552 return GlobalBaseReg;
553}
554
Nate Begeman6b559972005-04-01 02:59:27 +0000555/// getConstDouble - Loads a floating point value into a register, via the
556/// Constant Pool. Optionally takes a register in which to load the value.
557unsigned ISel::getConstDouble(double doubleVal, unsigned Result=0) {
558 unsigned Tmp1 = MakeReg(MVT::i32);
559 if (0 == Result) Result = MakeReg(MVT::f64);
560 MachineConstantPool *CP = BB->getParent()->getConstantPool();
561 ConstantFP *CFP = ConstantFP::get(Type::DoubleTy, doubleVal);
562 unsigned CPI = CP->getConstantPoolIndex(CFP);
563 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp1).addReg(getGlobalBaseReg())
564 .addConstantPoolIndex(CPI);
565 BuildMI(BB, PPC::LFD, 2, Result).addConstantPoolIndex(CPI).addReg(Tmp1);
566 return Result;
567}
568
Nate Begemandffcfcc2005-04-01 00:32:34 +0000569unsigned ISel::SelectSetCR0(SDOperand CC) {
570 unsigned Opc, Tmp1, Tmp2;
571 static const unsigned CompareOpcodes[] =
572 { PPC::FCMPU, PPC::FCMPU, PPC::CMPW, PPC::CMPLW };
573
574 // If the first operand to the select is a SETCC node, then we can fold it
575 // into the branch that selects which value to return.
576 SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(CC.Val);
577 if (SetCC && CC.getOpcode() == ISD::SETCC) {
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 bool IsInteger = MVT::isInteger(SetCC->getOperand(0).getValueType());
592 unsigned CompareOpc = CompareOpcodes[2 * IsInteger + U];
593 Tmp2 = SelectExpr(SetCC->getOperand(1));
594 BuildMI(BB, CompareOpc, 2, PPC::CR0).addReg(Tmp1).addReg(Tmp2);
595 }
596 } else {
597 Tmp1 = SelectExpr(CC);
598 BuildMI(BB, PPC::CMPLWI, 2, PPC::CR0).addReg(Tmp1).addImm(0);
599 Opc = PPC::BNE;
600 }
601 return Opc;
602}
603
604/// Check to see if the load is a constant offset from a base register
Nate Begeman04730362005-04-01 04:45:11 +0000605bool ISel::SelectAddr(SDOperand N, unsigned& Reg, int& offset)
Nate Begemana9795f82005-03-24 04:41:43 +0000606{
Nate Begeman96fc6812005-03-31 02:05:53 +0000607 unsigned imm = 0, opcode = N.getOpcode();
Nate Begeman04730362005-04-01 04:45:11 +0000608 if (N.getOpcode() == ISD::ADD) {
609 Reg = SelectExpr(N.getOperand(0));
Nate Begeman96fc6812005-03-31 02:05:53 +0000610 if (1 == canUseAsImmediateForOpcode(N.getOperand(1), opcode, imm)) {
Nate Begeman96fc6812005-03-31 02:05:53 +0000611 offset = imm;
Nate Begeman04730362005-04-01 04:45:11 +0000612 return false;
613 }
614 offset = SelectExpr(N.getOperand(1));
615 return true;
616 }
Nate Begemana9795f82005-03-24 04:41:43 +0000617 Reg = SelectExpr(N);
618 offset = 0;
Nate Begeman04730362005-04-01 04:45:11 +0000619 return false;
Nate Begemana9795f82005-03-24 04:41:43 +0000620}
621
622void ISel::SelectBranchCC(SDOperand N)
623{
624 assert(N.getOpcode() == ISD::BRCOND && "Not a BranchCC???");
625 MachineBasicBlock *Dest =
626 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
Nate Begeman3e897162005-03-31 23:55:40 +0000627
Nate Begemana9795f82005-03-24 04:41:43 +0000628 Select(N.getOperand(0)); //chain
Nate Begemandffcfcc2005-04-01 00:32:34 +0000629 unsigned Opc = SelectSetCR0(N.getOperand(1));
Nate Begeman3e897162005-03-31 23:55:40 +0000630 BuildMI(BB, Opc, 2).addReg(PPC::CR0).addMBB(Dest);
Nate Begemana9795f82005-03-24 04:41:43 +0000631 return;
632}
633
634unsigned ISel::SelectExprFP(SDOperand N, unsigned Result)
635{
636 unsigned Tmp1, Tmp2, Tmp3;
637 unsigned Opc = 0;
638 SDNode *Node = N.Val;
639 MVT::ValueType DestType = N.getValueType();
640 unsigned opcode = N.getOpcode();
641
642 switch (opcode) {
643 default:
644 Node->dump();
645 assert(0 && "Node not handled!\n");
646
Nate Begeman23afcfb2005-03-29 22:48:55 +0000647 case ISD::SELECT: {
Nate Begeman3e897162005-03-31 23:55:40 +0000648 // Attempt to generate FSEL. We can do this whenever we have an FP result,
649 // and an FP comparison in the SetCC node.
650 SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(N.getOperand(0).Val);
651 if (SetCC && N.getOperand(0).getOpcode() == ISD::SETCC &&
652 !MVT::isInteger(SetCC->getOperand(0).getValueType()) &&
653 SetCC->getCondition() != ISD::SETEQ &&
654 SetCC->getCondition() != ISD::SETNE) {
655 MVT::ValueType VT = SetCC->getOperand(0).getValueType();
656 Tmp1 = SelectExpr(SetCC->getOperand(0)); // Val to compare against
657 unsigned TV = SelectExpr(N.getOperand(1)); // Use if TRUE
658 unsigned FV = SelectExpr(N.getOperand(2)); // Use if FALSE
659
660 ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(1));
661 if (CN && (CN->isExactlyValue(-0.0) || CN->isExactlyValue(0.0))) {
662 switch(SetCC->getCondition()) {
663 default: assert(0 && "Invalid FSEL condition"); abort();
664 case ISD::SETULT:
665 case ISD::SETLT:
666 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp1).addReg(FV).addReg(TV);
667 return Result;
668 case ISD::SETUGE:
669 case ISD::SETGE:
670 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp1).addReg(TV).addReg(FV);
671 return Result;
672 case ISD::SETUGT:
673 case ISD::SETGT: {
674 Tmp2 = MakeReg(VT);
675 BuildMI(BB, PPC::FNEG, 1, Tmp2).addReg(Tmp1);
676 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp2).addReg(FV).addReg(TV);
677 return Result;
678 }
679 case ISD::SETULE:
680 case ISD::SETLE: {
681 Tmp2 = MakeReg(VT);
682 BuildMI(BB, PPC::FNEG, 1, Tmp2).addReg(Tmp1);
683 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp2).addReg(TV).addReg(FV);
684 return Result;
685 }
686 }
687 } else {
688 Opc = (MVT::f64 == VT) ? PPC::FSUB : PPC::FSUBS;
689 Tmp2 = SelectExpr(SetCC->getOperand(1));
690 Tmp3 = MakeReg(VT);
691 switch(SetCC->getCondition()) {
692 default: assert(0 && "Invalid FSEL condition"); abort();
693 case ISD::SETULT:
694 case ISD::SETLT:
695 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
696 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(FV).addReg(TV);
697 return Result;
698 case ISD::SETUGE:
699 case ISD::SETGE:
700 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
701 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(TV).addReg(FV);
702 return Result;
703 case ISD::SETUGT:
704 case ISD::SETGT:
705 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp2).addReg(Tmp1);
706 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(FV).addReg(TV);
707 return Result;
708 case ISD::SETULE:
709 case ISD::SETLE:
710 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp2).addReg(Tmp1);
711 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(TV).addReg(FV);
712 return Result;
713 }
714 }
715 assert(0 && "Should never get here");
716 return 0;
717 }
718
Nate Begeman31318e42005-04-01 07:21:30 +0000719 unsigned TrueValue = SelectExpr(N.getOperand(1)); //Use if TRUE
720 unsigned FalseValue = SelectExpr(N.getOperand(2)); //Use if FALSE
Nate Begeman6cb2e1b2005-04-01 08:57:43 +0000721 Opc = SelectSetCR0(N.getOperand(0));
Nate Begeman31318e42005-04-01 07:21:30 +0000722
Nate Begeman23afcfb2005-03-29 22:48:55 +0000723 // Create an iterator with which to insert the MBB for copying the false
724 // value and the MBB to hold the PHI instruction for this SetCC.
725 MachineBasicBlock *thisMBB = BB;
726 const BasicBlock *LLVM_BB = BB->getBasicBlock();
727 ilist<MachineBasicBlock>::iterator It = BB;
728 ++It;
729
730 // thisMBB:
731 // ...
732 // TrueVal = ...
733 // cmpTY cr0, r1, r2
734 // bCC copy1MBB
735 // fallthrough --> copy0MBB
Nate Begeman23afcfb2005-03-29 22:48:55 +0000736 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
737 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
Nate Begeman6cb2e1b2005-04-01 08:57:43 +0000738 BuildMI(BB, Opc, 2).addReg(PPC::CR0).addMBB(sinkMBB);
Nate Begeman23afcfb2005-03-29 22:48:55 +0000739 MachineFunction *F = BB->getParent();
740 F->getBasicBlockList().insert(It, copy0MBB);
741 F->getBasicBlockList().insert(It, sinkMBB);
742 // Update machine-CFG edges
743 BB->addSuccessor(copy0MBB);
744 BB->addSuccessor(sinkMBB);
745
746 // copy0MBB:
747 // %FalseValue = ...
748 // # fallthrough to sinkMBB
749 BB = copy0MBB;
Nate Begeman23afcfb2005-03-29 22:48:55 +0000750 // Update machine-CFG edges
751 BB->addSuccessor(sinkMBB);
752
753 // sinkMBB:
754 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
755 // ...
756 BB = sinkMBB;
757 BuildMI(BB, PPC::PHI, 4, Result).addReg(FalseValue)
758 .addMBB(copy0MBB).addReg(TrueValue).addMBB(thisMBB);
759 return Result;
760 }
Nate Begemana9795f82005-03-24 04:41:43 +0000761
762 case ISD::FP_ROUND:
763 assert (DestType == MVT::f32 &&
764 N.getOperand(0).getValueType() == MVT::f64 &&
765 "only f64 to f32 conversion supported here");
766 Tmp1 = SelectExpr(N.getOperand(0));
767 BuildMI(BB, PPC::FRSP, 1, Result).addReg(Tmp1);
768 return Result;
769
770 case ISD::FP_EXTEND:
771 assert (DestType == MVT::f64 &&
772 N.getOperand(0).getValueType() == MVT::f32 &&
773 "only f32 to f64 conversion supported here");
774 Tmp1 = SelectExpr(N.getOperand(0));
775 BuildMI(BB, PPC::FMR, 1, Result).addReg(Tmp1);
776 return Result;
777
778 case ISD::CopyFromReg:
Nate Begemanf2622612005-03-26 02:17:46 +0000779 if (Result == 1)
780 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
781 Tmp1 = dyn_cast<RegSDNode>(Node)->getReg();
782 BuildMI(BB, PPC::FMR, 1, Result).addReg(Tmp1);
783 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +0000784
Nate Begeman6d369cc2005-04-01 01:08:07 +0000785 case ISD::ConstantFP: {
Nate Begeman6d369cc2005-04-01 01:08:07 +0000786 ConstantFPSDNode *CN = cast<ConstantFPSDNode>(N);
Nate Begeman6b559972005-04-01 02:59:27 +0000787 Result = getConstDouble(CN->getValue(), Result);
Nate Begeman6d369cc2005-04-01 01:08:07 +0000788 return Result;
789 }
Nate Begemana9795f82005-03-24 04:41:43 +0000790
791 case ISD::MUL:
792 case ISD::ADD:
793 case ISD::SUB:
794 case ISD::SDIV:
795 switch( opcode ) {
796 case ISD::MUL: Opc = DestType == MVT::f64 ? PPC::FMUL : PPC::FMULS; break;
797 case ISD::ADD: Opc = DestType == MVT::f64 ? PPC::FADD : PPC::FADDS; break;
798 case ISD::SUB: Opc = DestType == MVT::f64 ? PPC::FSUB : PPC::FSUBS; break;
799 case ISD::SDIV: Opc = DestType == MVT::f64 ? PPC::FDIV : PPC::FDIVS; break;
800 };
Nate Begemana9795f82005-03-24 04:41:43 +0000801 Tmp1 = SelectExpr(N.getOperand(0));
802 Tmp2 = SelectExpr(N.getOperand(1));
803 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
804 return Result;
805
Nate Begemana9795f82005-03-24 04:41:43 +0000806 case ISD::UINT_TO_FP:
Nate Begemanfdcf3412005-03-30 19:38:35 +0000807 case ISD::SINT_TO_FP: {
808 assert (N.getOperand(0).getValueType() == MVT::i32
809 && "int to float must operate on i32");
810 bool IsUnsigned = (ISD::UINT_TO_FP == opcode);
811 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
812 Tmp2 = MakeReg(MVT::f64); // temp reg to load the integer value into
813 Tmp3 = MakeReg(MVT::i32); // temp reg to hold the conversion constant
814 unsigned ConstF = MakeReg(MVT::f64); // temp reg to hold the fp constant
815
816 int FrameIdx = BB->getParent()->getFrameInfo()->CreateStackObject(8, 8);
817 MachineConstantPool *CP = BB->getParent()->getConstantPool();
818
819 // FIXME: pull this FP constant generation stuff out into something like
820 // the simple ISel's getReg.
821 if (IsUnsigned) {
822 ConstantFP *CFP = ConstantFP::get(Type::DoubleTy, 0x1.000000p52);
823 unsigned CPI = CP->getConstantPoolIndex(CFP);
824 // Load constant fp value
825 unsigned Tmp4 = MakeReg(MVT::i32);
826 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp4).addReg(getGlobalBaseReg())
827 .addConstantPoolIndex(CPI);
828 BuildMI(BB, PPC::LFD, 2, ConstF).addConstantPoolIndex(CPI).addReg(Tmp4);
829 // Store the hi & low halves of the fp value, currently in int regs
830 BuildMI(BB, PPC::LIS, 1, Tmp3).addSImm(0x4330);
831 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(Tmp3), FrameIdx);
832 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(Tmp1), FrameIdx, 4);
833 addFrameReference(BuildMI(BB, PPC::LFD, 2, Tmp2), FrameIdx);
834 // Generate the return value with a subtract
835 BuildMI(BB, PPC::FSUB, 2, Result).addReg(Tmp2).addReg(ConstF);
836 } else {
837 ConstantFP *CFP = ConstantFP::get(Type::DoubleTy, 0x1.000008p52);
838 unsigned CPI = CP->getConstantPoolIndex(CFP);
839 // Load constant fp value
840 unsigned Tmp4 = MakeReg(MVT::i32);
841 unsigned TmpL = MakeReg(MVT::i32);
842 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp4).addReg(getGlobalBaseReg())
843 .addConstantPoolIndex(CPI);
844 BuildMI(BB, PPC::LFD, 2, ConstF).addConstantPoolIndex(CPI).addReg(Tmp4);
845 // Store the hi & low halves of the fp value, currently in int regs
846 BuildMI(BB, PPC::LIS, 1, Tmp3).addSImm(0x4330);
847 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(Tmp3), FrameIdx);
848 BuildMI(BB, PPC::XORIS, 2, TmpL).addReg(Tmp1).addImm(0x8000);
849 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(TmpL), FrameIdx, 4);
850 addFrameReference(BuildMI(BB, PPC::LFD, 2, Tmp2), FrameIdx);
851 // Generate the return value with a subtract
852 BuildMI(BB, PPC::FSUB, 2, Result).addReg(Tmp2).addReg(ConstF);
853 }
854 return Result;
855 }
Nate Begemana9795f82005-03-24 04:41:43 +0000856 }
Nate Begeman6b559972005-04-01 02:59:27 +0000857 assert(0 && "Should never get here");
Nate Begemana9795f82005-03-24 04:41:43 +0000858 return 0;
859}
860
861unsigned ISel::SelectExpr(SDOperand N) {
862 unsigned Result;
863 unsigned Tmp1, Tmp2, Tmp3;
864 unsigned Opc = 0;
865 unsigned opcode = N.getOpcode();
866
867 SDNode *Node = N.Val;
868 MVT::ValueType DestType = N.getValueType();
869
870 unsigned &Reg = ExprMap[N];
871 if (Reg) return Reg;
872
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000873 if (N.getOpcode() != ISD::CALL && N.getOpcode() != ISD::ADD_PARTS &&
874 N.getOpcode() != ISD::SUB_PARTS)
Nate Begemana9795f82005-03-24 04:41:43 +0000875 Reg = Result = (N.getValueType() != MVT::Other) ?
876 MakeReg(N.getValueType()) : 1;
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000877 else {
878 // If this is a call instruction, make sure to prepare ALL of the result
879 // values as well as the chain.
880 if (N.getOpcode() == ISD::CALL) {
881 if (Node->getNumValues() == 1)
882 Reg = Result = 1; // Void call, just a chain.
883 else {
884 Result = MakeReg(Node->getValueType(0));
885 ExprMap[N.getValue(0)] = Result;
886 for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
887 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
888 ExprMap[SDOperand(Node, Node->getNumValues()-1)] = 1;
889 }
890 } else {
891 Result = MakeReg(Node->getValueType(0));
892 ExprMap[N.getValue(0)] = Result;
893 for (unsigned i = 1, e = N.Val->getNumValues(); i != e; ++i)
894 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
895 }
896 }
897
898 if (DestType == MVT::f64 || DestType == MVT::f32)
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000899 if (ISD::LOAD != opcode && ISD::EXTLOAD != opcode && ISD::UNDEF != opcode)
Nate Begeman74d73452005-03-31 00:15:26 +0000900 return SelectExprFP(N, Result);
Nate Begemana9795f82005-03-24 04:41:43 +0000901
902 switch (opcode) {
903 default:
904 Node->dump();
905 assert(0 && "Node not handled!\n");
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000906 case ISD::UNDEF:
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000907 BuildMI(BB, PPC::IMPLICIT_DEF, 0, Result);
908 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +0000909 case ISD::DYNAMIC_STACKALLOC:
Nate Begeman5e966612005-03-24 06:28:42 +0000910 // Generate both result values. FIXME: Need a better commment here?
911 if (Result != 1)
912 ExprMap[N.getValue(1)] = 1;
913 else
914 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
915
916 // FIXME: We are currently ignoring the requested alignment for handling
917 // greater than the stack alignment. This will need to be revisited at some
918 // point. Align = N.getOperand(2);
919 if (!isa<ConstantSDNode>(N.getOperand(2)) ||
920 cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) {
921 std::cerr << "Cannot allocate stack object with greater alignment than"
922 << " the stack alignment yet!";
923 abort();
924 }
925 Select(N.getOperand(0));
926 Tmp1 = SelectExpr(N.getOperand(1));
927 // Subtract size from stack pointer, thereby allocating some space.
928 BuildMI(BB, PPC::SUBF, 2, PPC::R1).addReg(Tmp1).addReg(PPC::R1);
929 // Put a pointer to the space into the result register by copying the SP
930 BuildMI(BB, PPC::OR, 2, Result).addReg(PPC::R1).addReg(PPC::R1);
931 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +0000932
933 case ISD::ConstantPool:
Nate Begemanca12a2b2005-03-28 22:28:37 +0000934 Tmp1 = cast<ConstantPoolSDNode>(N)->getIndex();
935 Tmp2 = MakeReg(MVT::i32);
936 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp2).addReg(getGlobalBaseReg())
937 .addConstantPoolIndex(Tmp1);
938 BuildMI(BB, PPC::LA, 2, Result).addReg(Tmp2).addConstantPoolIndex(Tmp1);
939 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +0000940
941 case ISD::FrameIndex:
Nate Begemanf3d08f32005-03-29 00:03:27 +0000942 Tmp1 = cast<FrameIndexSDNode>(N)->getIndex();
Nate Begeman58f718c2005-03-30 02:23:08 +0000943 addFrameReference(BuildMI(BB, PPC::ADDI, 2, Result), (int)Tmp1, 0, false);
Nate Begemanf3d08f32005-03-29 00:03:27 +0000944 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +0000945
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000946 case ISD::GlobalAddress: {
947 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
Nate Begemanca12a2b2005-03-28 22:28:37 +0000948 Tmp1 = MakeReg(MVT::i32);
Nate Begemanc7b09f12005-03-25 08:34:25 +0000949 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp1).addReg(getGlobalBaseReg())
950 .addGlobalAddress(GV);
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000951 if (GV->hasWeakLinkage() || GV->isExternal()) {
952 BuildMI(BB, PPC::LWZ, 2, Result).addGlobalAddress(GV).addReg(Tmp1);
953 } else {
954 BuildMI(BB, PPC::LA, 2, Result).addReg(Tmp1).addGlobalAddress(GV);
955 }
956 return Result;
957 }
958
Nate Begeman5e966612005-03-24 06:28:42 +0000959 case ISD::LOAD:
Nate Begemana9795f82005-03-24 04:41:43 +0000960 case ISD::EXTLOAD:
961 case ISD::ZEXTLOAD:
Nate Begeman9e3e1b52005-03-24 23:35:30 +0000962 case ISD::SEXTLOAD: {
Nate Begeman9db505c2005-03-28 19:36:43 +0000963 MVT::ValueType TypeBeingLoaded = (ISD::LOAD == opcode) ?
964 Node->getValueType(0) : cast<MVTSDNode>(Node)->getExtraValueType();
Nate Begeman74d73452005-03-31 00:15:26 +0000965 bool sext = (ISD::SEXTLOAD == opcode);
966 bool byte = (MVT::i8 == TypeBeingLoaded);
967
Nate Begeman5e966612005-03-24 06:28:42 +0000968 // Make sure we generate both values.
969 if (Result != 1)
970 ExprMap[N.getValue(1)] = 1; // Generate the token
971 else
972 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
973
974 SDOperand Chain = N.getOperand(0);
975 SDOperand Address = N.getOperand(1);
976 Select(Chain);
977
Nate Begeman9db505c2005-03-28 19:36:43 +0000978 switch (TypeBeingLoaded) {
Nate Begeman74d73452005-03-31 00:15:26 +0000979 default: Node->dump(); assert(0 && "Cannot load this type!");
Nate Begeman9db505c2005-03-28 19:36:43 +0000980 case MVT::i1: Opc = PPC::LBZ; break;
981 case MVT::i8: Opc = PPC::LBZ; break;
982 case MVT::i16: Opc = sext ? PPC::LHA : PPC::LHZ; break;
983 case MVT::i32: Opc = PPC::LWZ; break;
Nate Begeman74d73452005-03-31 00:15:26 +0000984 case MVT::f32: Opc = PPC::LFS; break;
985 case MVT::f64: Opc = PPC::LFD; break;
Nate Begeman5e966612005-03-24 06:28:42 +0000986 }
987
Nate Begeman74d73452005-03-31 00:15:26 +0000988 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Address)) {
989 Tmp1 = MakeReg(MVT::i32);
990 int CPI = CP->getIndex();
991 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp1).addReg(getGlobalBaseReg())
992 .addConstantPoolIndex(CPI);
993 BuildMI(BB, Opc, 2, Result).addConstantPoolIndex(CPI).addReg(Tmp1);
Nate Begeman9db505c2005-03-28 19:36:43 +0000994 }
Nate Begeman74d73452005-03-31 00:15:26 +0000995 else if(Address.getOpcode() == ISD::FrameIndex) {
Nate Begeman58f718c2005-03-30 02:23:08 +0000996 Tmp1 = cast<FrameIndexSDNode>(Address)->getIndex();
997 addFrameReference(BuildMI(BB, Opc, 2, Result), (int)Tmp1);
Nate Begeman5e966612005-03-24 06:28:42 +0000998 } else {
999 int offset;
Nate Begeman04730362005-04-01 04:45:11 +00001000 bool idx = SelectAddr(Address, Tmp1, offset);
1001 if (idx) {
1002 Opc = IndexedOpForOp(Opc);
1003 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(offset);
1004 } else {
1005 BuildMI(BB, Opc, 2, Result).addSImm(offset).addReg(Tmp1);
1006 }
Nate Begeman5e966612005-03-24 06:28:42 +00001007 }
1008 return Result;
1009 }
1010
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001011 case ISD::CALL: {
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001012 unsigned GPR_idx = 0, FPR_idx = 0;
1013 static const unsigned GPR[] = {
1014 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
1015 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
1016 };
1017 static const unsigned FPR[] = {
1018 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
1019 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
1020 };
1021
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001022 // Lower the chain for this call.
1023 Select(N.getOperand(0));
1024 ExprMap[N.getValue(Node->getNumValues()-1)] = 1;
Nate Begeman74d73452005-03-31 00:15:26 +00001025
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001026 // Load the register args to virtual regs
1027 std::vector<unsigned> ArgVR;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001028 for(int i = 2, e = Node->getNumOperands(); i < e; ++i)
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001029 ArgVR.push_back(SelectExpr(N.getOperand(i)));
1030
1031 // Copy the virtual registers into the appropriate argument register
1032 for(int i = 0, e = ArgVR.size(); i < e; ++i) {
1033 switch(N.getOperand(i+2).getValueType()) {
1034 default: Node->dump(); assert(0 && "Unknown value type for call");
1035 case MVT::i1:
1036 case MVT::i8:
1037 case MVT::i16:
1038 case MVT::i32:
1039 assert(GPR_idx < 8 && "Too many int args");
1040 if (N.getOperand(i+2).getOpcode() != ISD::UNDEF)
1041 BuildMI(BB, PPC::OR,2,GPR[GPR_idx]).addReg(ArgVR[i]).addReg(ArgVR[i]);
1042 ++GPR_idx;
1043 break;
1044 case MVT::f64:
1045 case MVT::f32:
1046 assert(FPR_idx < 13 && "Too many fp args");
1047 BuildMI(BB, PPC::FMR, 1, FPR[FPR_idx]).addReg(ArgVR[i]);
1048 ++FPR_idx;
1049 break;
1050 }
1051 }
1052
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001053 // Emit the correct call instruction based on the type of symbol called.
1054 if (GlobalAddressSDNode *GASD =
1055 dyn_cast<GlobalAddressSDNode>(N.getOperand(1))) {
1056 BuildMI(BB, PPC::CALLpcrel, 1).addGlobalAddress(GASD->getGlobal(), true);
1057 } else if (ExternalSymbolSDNode *ESSDN =
1058 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1))) {
1059 BuildMI(BB, PPC::CALLpcrel, 1).addExternalSymbol(ESSDN->getSymbol(), true);
1060 } else {
1061 Tmp1 = SelectExpr(N.getOperand(1));
1062 BuildMI(BB, PPC::OR, 2, PPC::R12).addReg(Tmp1).addReg(Tmp1);
1063 BuildMI(BB, PPC::MTCTR, 1).addReg(PPC::R12);
1064 BuildMI(BB, PPC::CALLindirect, 3).addImm(20).addImm(0).addReg(PPC::R12);
1065 }
1066
1067 switch (Node->getValueType(0)) {
1068 default: assert(0 && "Unknown value type for call result!");
1069 case MVT::Other: return 1;
1070 case MVT::i1:
1071 case MVT::i8:
1072 case MVT::i16:
1073 case MVT::i32:
Nate Begemanc7b09f12005-03-25 08:34:25 +00001074 BuildMI(BB, PPC::OR, 2, Result).addReg(PPC::R3).addReg(PPC::R3);
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001075 if (Node->getValueType(1) == MVT::i32)
Nate Begemanc7b09f12005-03-25 08:34:25 +00001076 BuildMI(BB, PPC::OR, 2, Result+1).addReg(PPC::R4).addReg(PPC::R4);
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001077 break;
1078 case MVT::f32:
1079 case MVT::f64:
1080 BuildMI(BB, PPC::FMR, 1, Result).addReg(PPC::F1);
1081 break;
1082 }
1083 return Result+N.ResNo;
1084 }
Nate Begemana9795f82005-03-24 04:41:43 +00001085
1086 case ISD::SIGN_EXTEND:
1087 case ISD::SIGN_EXTEND_INREG:
1088 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman9db505c2005-03-28 19:36:43 +00001089 switch(cast<MVTSDNode>(Node)->getExtraValueType()) {
1090 default: Node->dump(); assert(0 && "Unhandled SIGN_EXTEND type"); break;
1091 case MVT::i16:
1092 BuildMI(BB, PPC::EXTSH, 1, Result).addReg(Tmp1);
1093 break;
1094 case MVT::i8:
1095 BuildMI(BB, PPC::EXTSB, 1, Result).addReg(Tmp1);
1096 break;
Nate Begeman74747862005-03-29 22:24:51 +00001097 case MVT::i1:
1098 BuildMI(BB, PPC::SUBFIC, 2, Result).addReg(Tmp1).addSImm(0);
1099 break;
Nate Begeman9db505c2005-03-28 19:36:43 +00001100 }
Nate Begemana9795f82005-03-24 04:41:43 +00001101 return Result;
1102
1103 case ISD::ZERO_EXTEND_INREG:
1104 Tmp1 = SelectExpr(N.getOperand(0));
1105 switch(cast<MVTSDNode>(Node)->getExtraValueType()) {
Nate Begeman9db505c2005-03-28 19:36:43 +00001106 default: Node->dump(); assert(0 && "Unhandled ZERO_EXTEND type"); break;
Nate Begemana9795f82005-03-24 04:41:43 +00001107 case MVT::i16: Tmp2 = 16; break;
1108 case MVT::i8: Tmp2 = 24; break;
1109 case MVT::i1: Tmp2 = 31; break;
1110 }
Nate Begeman33162522005-03-29 21:54:38 +00001111 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(0).addImm(Tmp2)
1112 .addImm(31);
Nate Begemana9795f82005-03-24 04:41:43 +00001113 return Result;
1114
Nate Begemana9795f82005-03-24 04:41:43 +00001115 case ISD::CopyFromReg:
1116 if (Result == 1)
1117 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1118 Tmp1 = dyn_cast<RegSDNode>(Node)->getReg();
1119 BuildMI(BB, PPC::OR, 2, Result).addReg(Tmp1).addReg(Tmp1);
1120 return Result;
1121
1122 case ISD::SHL:
Nate Begeman5e966612005-03-24 06:28:42 +00001123 Tmp1 = SelectExpr(N.getOperand(0));
1124 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1125 Tmp2 = CN->getValue() & 0x1F;
Nate Begeman33162522005-03-29 21:54:38 +00001126 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(Tmp2).addImm(0)
Nate Begeman5e966612005-03-24 06:28:42 +00001127 .addImm(31-Tmp2);
1128 } else {
1129 Tmp2 = SelectExpr(N.getOperand(1));
1130 BuildMI(BB, PPC::SLW, 2, Result).addReg(Tmp1).addReg(Tmp2);
1131 }
1132 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001133
Nate Begeman5e966612005-03-24 06:28:42 +00001134 case ISD::SRL:
1135 Tmp1 = SelectExpr(N.getOperand(0));
1136 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1137 Tmp2 = CN->getValue() & 0x1F;
Nate Begeman33162522005-03-29 21:54:38 +00001138 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(32-Tmp2)
Nate Begeman5e966612005-03-24 06:28:42 +00001139 .addImm(Tmp2).addImm(31);
1140 } else {
1141 Tmp2 = SelectExpr(N.getOperand(1));
1142 BuildMI(BB, PPC::SRW, 2, Result).addReg(Tmp1).addReg(Tmp2);
1143 }
1144 return Result;
1145
1146 case ISD::SRA:
1147 Tmp1 = SelectExpr(N.getOperand(0));
1148 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1149 Tmp2 = CN->getValue() & 0x1F;
1150 BuildMI(BB, PPC::SRAWI, 2, Result).addReg(Tmp1).addImm(Tmp2);
1151 } else {
1152 Tmp2 = SelectExpr(N.getOperand(1));
1153 BuildMI(BB, PPC::SRAW, 2, Result).addReg(Tmp1).addReg(Tmp2);
1154 }
1155 return Result;
1156
Nate Begemana9795f82005-03-24 04:41:43 +00001157 case ISD::ADD:
1158 assert (DestType == MVT::i32 && "Only do arithmetic on i32s!");
1159 Tmp1 = SelectExpr(N.getOperand(0));
1160 switch(canUseAsImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
1161 default: assert(0 && "unhandled result code");
1162 case 0: // No immediate
1163 Tmp2 = SelectExpr(N.getOperand(1));
1164 BuildMI(BB, PPC::ADD, 2, Result).addReg(Tmp1).addReg(Tmp2);
1165 break;
1166 case 1: // Low immediate
1167 BuildMI(BB, PPC::ADDI, 2, Result).addReg(Tmp1).addSImm(Tmp2);
1168 break;
1169 case 2: // Shifted immediate
1170 BuildMI(BB, PPC::ADDIS, 2, Result).addReg(Tmp1).addSImm(Tmp2);
1171 break;
1172 }
1173 return Result;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001174
Nate Begemana9795f82005-03-24 04:41:43 +00001175 case ISD::AND:
1176 case ISD::OR:
1177 case ISD::XOR:
1178 assert (DestType == MVT::i32 && "Only do arithmetic on i32s!");
1179 Tmp1 = SelectExpr(N.getOperand(0));
1180 switch(canUseAsImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
1181 default: assert(0 && "unhandled result code");
1182 case 0: // No immediate
1183 Tmp2 = SelectExpr(N.getOperand(1));
1184 switch (opcode) {
Nate Begeman5e966612005-03-24 06:28:42 +00001185 case ISD::AND: Opc = PPC::AND; break;
1186 case ISD::OR: Opc = PPC::OR; break;
1187 case ISD::XOR: Opc = PPC::XOR; break;
Nate Begemana9795f82005-03-24 04:41:43 +00001188 }
Nate Begeman5e966612005-03-24 06:28:42 +00001189 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +00001190 break;
1191 case 1: // Low immediate
1192 switch (opcode) {
Nate Begeman5e966612005-03-24 06:28:42 +00001193 case ISD::AND: Opc = PPC::ANDIo; break;
1194 case ISD::OR: Opc = PPC::ORI; break;
1195 case ISD::XOR: Opc = PPC::XORI; break;
Nate Begemana9795f82005-03-24 04:41:43 +00001196 }
Nate Begeman5e966612005-03-24 06:28:42 +00001197 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +00001198 break;
1199 case 2: // Shifted immediate
1200 switch (opcode) {
Nate Begeman5e966612005-03-24 06:28:42 +00001201 case ISD::AND: Opc = PPC::ANDISo; break;
1202 case ISD::OR: Opc = PPC::ORIS; break;
1203 case ISD::XOR: Opc = PPC::XORIS; break;
Nate Begemana9795f82005-03-24 04:41:43 +00001204 }
Nate Begeman5e966612005-03-24 06:28:42 +00001205 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +00001206 break;
1207 }
1208 return Result;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001209
1210 case ISD::SUB:
1211 assert (DestType == MVT::i32 && "Only do arithmetic on i32s!");
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001212 Tmp2 = SelectExpr(N.getOperand(1));
Nate Begeman27523a12005-04-02 00:42:16 +00001213 if (1 == canUseAsImmediateForOpcode(N.getOperand(0), opcode, Tmp1))
1214 BuildMI(BB, PPC::SUBFIC, 2, Result).addReg(Tmp2).addSImm(Tmp1);
1215 else {
1216 Tmp1 = SelectExpr(N.getOperand(0));
1217 BuildMI(BB, PPC::SUBF, 2, Result).addReg(Tmp2).addReg(Tmp1);
1218 }
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001219 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001220
Nate Begeman5e966612005-03-24 06:28:42 +00001221 case ISD::MUL:
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001222 assert (DestType == MVT::i32 && "Only do arithmetic on i32s!");
1223 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman307e7442005-03-26 01:28:53 +00001224 if (1 == canUseAsImmediateForOpcode(N.getOperand(1), opcode, Tmp2))
1225 BuildMI(BB, PPC::MULLI, 2, Result).addReg(Tmp1).addSImm(Tmp2);
1226 else {
1227 Tmp2 = SelectExpr(N.getOperand(1));
1228 BuildMI(BB, PPC::MULLW, 2, Result).addReg(Tmp1).addReg(Tmp2);
1229 }
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001230 return Result;
1231
Nate Begemanf3d08f32005-03-29 00:03:27 +00001232 case ISD::SDIV:
1233 case ISD::UDIV:
1234 assert (DestType == MVT::i32 && "Only do arithmetic on i32s!");
1235 Tmp1 = SelectExpr(N.getOperand(0));
1236 Tmp2 = SelectExpr(N.getOperand(1));
1237 Opc = (ISD::UDIV == opcode) ? PPC::DIVWU : PPC::DIVW;
1238 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1239 return Result;
1240
1241 case ISD::UREM:
1242 case ISD::SREM: {
1243 assert (DestType == MVT::i32 && "Only do arithmetic on i32s!");
1244 Tmp1 = SelectExpr(N.getOperand(0));
1245 Tmp2 = SelectExpr(N.getOperand(1));
1246 Tmp3 = MakeReg(MVT::i32);
1247 unsigned Tmp4 = MakeReg(MVT::i32);
1248 Opc = (ISD::UREM == opcode) ? PPC::DIVWU : PPC::DIVW;
1249 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
1250 BuildMI(BB, PPC::MULLW, 2, Tmp4).addReg(Tmp3).addReg(Tmp2);
1251 BuildMI(BB, PPC::SUBF, 2, Result).addReg(Tmp4).addReg(Tmp1);
1252 return Result;
1253 }
1254
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001255 case ISD::ADD_PARTS:
Nate Begemanca12a2b2005-03-28 22:28:37 +00001256 case ISD::SUB_PARTS: {
1257 assert(N.getNumOperands() == 4 && N.getValueType() == MVT::i32 &&
1258 "Not an i64 add/sub!");
1259 // Emit all of the operands.
1260 std::vector<unsigned> InVals;
1261 for (unsigned i = 0, e = N.getNumOperands(); i != e; ++i)
1262 InVals.push_back(SelectExpr(N.getOperand(i)));
1263 if (N.getOpcode() == ISD::ADD_PARTS) {
Nate Begemanf70b5762005-03-28 23:08:54 +00001264 BuildMI(BB, PPC::ADDC, 2, Result+1).addReg(InVals[0]).addReg(InVals[2]);
1265 BuildMI(BB, PPC::ADDE, 2, Result).addReg(InVals[1]).addReg(InVals[3]);
Nate Begemanca12a2b2005-03-28 22:28:37 +00001266 } else {
Nate Begemanf70b5762005-03-28 23:08:54 +00001267 BuildMI(BB, PPC::SUBFC, 2, Result+1).addReg(InVals[2]).addReg(InVals[0]);
1268 BuildMI(BB, PPC::SUBFE, 2, Result).addReg(InVals[3]).addReg(InVals[1]);
Nate Begemanca12a2b2005-03-28 22:28:37 +00001269 }
1270 return Result+N.ResNo;
1271 }
1272
Nate Begemana9795f82005-03-24 04:41:43 +00001273 case ISD::FP_TO_UINT:
Nate Begeman6b559972005-04-01 02:59:27 +00001274 case ISD::FP_TO_SINT: {
1275 bool U = (ISD::FP_TO_UINT == opcode);
1276 Tmp1 = SelectExpr(N.getOperand(0));
1277 if (!U) {
1278 Tmp2 = MakeReg(MVT::f64);
1279 BuildMI(BB, PPC::FCTIWZ, 1, Tmp2).addReg(Tmp1);
1280 int FrameIdx = BB->getParent()->getFrameInfo()->CreateStackObject(8, 8);
1281 addFrameReference(BuildMI(BB, PPC::STFD, 3).addReg(Tmp2), FrameIdx);
1282 addFrameReference(BuildMI(BB, PPC::LWZ, 2, Result), FrameIdx, 4);
1283 return Result;
1284 } else {
1285 unsigned Zero = getConstDouble(0.0);
1286 unsigned MaxInt = getConstDouble((1LL << 32) - 1);
1287 unsigned Border = getConstDouble(1LL << 31);
1288 unsigned UseZero = MakeReg(MVT::f64);
1289 unsigned UseMaxInt = MakeReg(MVT::f64);
1290 unsigned UseChoice = MakeReg(MVT::f64);
1291 unsigned TmpReg = MakeReg(MVT::f64);
1292 unsigned TmpReg2 = MakeReg(MVT::f64);
1293 unsigned ConvReg = MakeReg(MVT::f64);
1294 unsigned IntTmp = MakeReg(MVT::i32);
1295 unsigned XorReg = MakeReg(MVT::i32);
1296 MachineFunction *F = BB->getParent();
1297 int FrameIdx = F->getFrameInfo()->CreateStackObject(8, 8);
1298 // Update machine-CFG edges
1299 MachineBasicBlock *XorMBB = new MachineBasicBlock(BB->getBasicBlock());
1300 MachineBasicBlock *PhiMBB = new MachineBasicBlock(BB->getBasicBlock());
1301 MachineBasicBlock *OldMBB = BB;
1302 ilist<MachineBasicBlock>::iterator It = BB; ++It;
1303 F->getBasicBlockList().insert(It, XorMBB);
1304 F->getBasicBlockList().insert(It, PhiMBB);
1305 BB->addSuccessor(XorMBB);
1306 BB->addSuccessor(PhiMBB);
1307 // Convert from floating point to unsigned 32-bit value
1308 // Use 0 if incoming value is < 0.0
1309 BuildMI(BB, PPC::FSEL, 3, UseZero).addReg(Tmp1).addReg(Tmp1).addReg(Zero);
1310 // Use 2**32 - 1 if incoming value is >= 2**32
1311 BuildMI(BB, PPC::FSUB, 2, UseMaxInt).addReg(MaxInt).addReg(Tmp1);
1312 BuildMI(BB, PPC::FSEL, 3, UseChoice).addReg(UseMaxInt).addReg(UseZero)
1313 .addReg(MaxInt);
1314 // Subtract 2**31
1315 BuildMI(BB, PPC::FSUB, 2, TmpReg).addReg(UseChoice).addReg(Border);
1316 // Use difference if >= 2**31
1317 BuildMI(BB, PPC::FCMPU, 2, PPC::CR0).addReg(UseChoice).addReg(Border);
1318 BuildMI(BB, PPC::FSEL, 3, TmpReg2).addReg(TmpReg).addReg(TmpReg)
1319 .addReg(UseChoice);
1320 // Convert to integer
1321 BuildMI(BB, PPC::FCTIWZ, 1, ConvReg).addReg(TmpReg2);
1322 addFrameReference(BuildMI(BB, PPC::STFD, 3).addReg(ConvReg), FrameIdx);
1323 addFrameReference(BuildMI(BB, PPC::LWZ, 2, IntTmp), FrameIdx, 4);
1324 BuildMI(BB, PPC::BLT, 2).addReg(PPC::CR0).addMBB(PhiMBB);
1325 BuildMI(BB, PPC::B, 1).addMBB(XorMBB);
1326
1327 // XorMBB:
1328 // add 2**31 if input was >= 2**31
1329 BB = XorMBB;
1330 BuildMI(BB, PPC::XORIS, 2, XorReg).addReg(IntTmp).addImm(0x8000);
1331 XorMBB->addSuccessor(PhiMBB);
1332
1333 // PhiMBB:
1334 // DestReg = phi [ IntTmp, OldMBB ], [ XorReg, XorMBB ]
1335 BB = PhiMBB;
1336 BuildMI(BB, PPC::PHI, 4, Result).addReg(IntTmp).addMBB(OldMBB)
1337 .addReg(XorReg).addMBB(XorMBB);
1338 return Result;
1339 }
1340 assert(0 && "Should never get here");
1341 return 0;
1342 }
Nate Begemana9795f82005-03-24 04:41:43 +00001343
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001344 case ISD::SETCC:
Nate Begeman33162522005-03-29 21:54:38 +00001345 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Node)) {
Nate Begemandffcfcc2005-04-01 00:32:34 +00001346 Opc = SelectSetCR0(N);
Nate Begeman33162522005-03-29 21:54:38 +00001347
Nate Begeman31318e42005-04-01 07:21:30 +00001348 unsigned TrueValue = MakeReg(MVT::i32);
1349 BuildMI(BB, PPC::LI, 1, TrueValue).addSImm(1);
1350 unsigned FalseValue = MakeReg(MVT::i32);
1351 BuildMI(BB, PPC::LI, 1, FalseValue).addSImm(0);
1352
Nate Begeman33162522005-03-29 21:54:38 +00001353 // Create an iterator with which to insert the MBB for copying the false
1354 // value and the MBB to hold the PHI instruction for this SetCC.
1355 MachineBasicBlock *thisMBB = BB;
1356 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1357 ilist<MachineBasicBlock>::iterator It = BB;
1358 ++It;
1359
1360 // thisMBB:
1361 // ...
1362 // cmpTY cr0, r1, r2
1363 // %TrueValue = li 1
1364 // bCC sinkMBB
Nate Begeman33162522005-03-29 21:54:38 +00001365 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
1366 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
1367 BuildMI(BB, Opc, 2).addReg(PPC::CR0).addMBB(sinkMBB);
1368 MachineFunction *F = BB->getParent();
1369 F->getBasicBlockList().insert(It, copy0MBB);
1370 F->getBasicBlockList().insert(It, sinkMBB);
1371 // Update machine-CFG edges
1372 BB->addSuccessor(copy0MBB);
1373 BB->addSuccessor(sinkMBB);
1374
1375 // copy0MBB:
1376 // %FalseValue = li 0
1377 // fallthrough
1378 BB = copy0MBB;
Nate Begeman33162522005-03-29 21:54:38 +00001379 // Update machine-CFG edges
1380 BB->addSuccessor(sinkMBB);
1381
1382 // sinkMBB:
1383 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1384 // ...
1385 BB = sinkMBB;
1386 BuildMI(BB, PPC::PHI, 4, Result).addReg(FalseValue)
1387 .addMBB(copy0MBB).addReg(TrueValue).addMBB(thisMBB);
1388 return Result;
1389 }
1390 assert(0 && "Is this legal?");
1391 return 0;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001392
Nate Begeman74747862005-03-29 22:24:51 +00001393 case ISD::SELECT: {
Chris Lattner30710192005-04-01 07:10:02 +00001394 unsigned TrueValue = SelectExpr(N.getOperand(1)); //Use if TRUE
1395 unsigned FalseValue = SelectExpr(N.getOperand(2)); //Use if FALSE
Nate Begeman6cb2e1b2005-04-01 08:57:43 +00001396 Opc = SelectSetCR0(N.getOperand(0));
Chris Lattner30710192005-04-01 07:10:02 +00001397
Nate Begeman74747862005-03-29 22:24:51 +00001398 // Create an iterator with which to insert the MBB for copying the false
1399 // value and the MBB to hold the PHI instruction for this SetCC.
1400 MachineBasicBlock *thisMBB = BB;
1401 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1402 ilist<MachineBasicBlock>::iterator It = BB;
1403 ++It;
1404
1405 // thisMBB:
1406 // ...
1407 // TrueVal = ...
1408 // cmpTY cr0, r1, r2
1409 // bCC copy1MBB
1410 // fallthrough --> copy0MBB
Nate Begeman74747862005-03-29 22:24:51 +00001411 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
1412 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
Nate Begeman3e897162005-03-31 23:55:40 +00001413 BuildMI(BB, Opc, 2).addReg(PPC::CR0).addMBB(sinkMBB);
Nate Begeman74747862005-03-29 22:24:51 +00001414 MachineFunction *F = BB->getParent();
1415 F->getBasicBlockList().insert(It, copy0MBB);
1416 F->getBasicBlockList().insert(It, sinkMBB);
1417 // Update machine-CFG edges
1418 BB->addSuccessor(copy0MBB);
1419 BB->addSuccessor(sinkMBB);
1420
1421 // copy0MBB:
1422 // %FalseValue = ...
1423 // # fallthrough to sinkMBB
1424 BB = copy0MBB;
Nate Begeman74747862005-03-29 22:24:51 +00001425 // Update machine-CFG edges
1426 BB->addSuccessor(sinkMBB);
1427
1428 // sinkMBB:
1429 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1430 // ...
1431 BB = sinkMBB;
1432 BuildMI(BB, PPC::PHI, 4, Result).addReg(FalseValue)
1433 .addMBB(copy0MBB).addReg(TrueValue).addMBB(thisMBB);
1434
1435 // FIXME: Select i64?
1436 return Result;
1437 }
Nate Begemana9795f82005-03-24 04:41:43 +00001438
1439 case ISD::Constant:
1440 switch (N.getValueType()) {
1441 default: assert(0 && "Cannot use constants of this type!");
1442 case MVT::i1:
1443 BuildMI(BB, PPC::LI, 1, Result)
1444 .addSImm(!cast<ConstantSDNode>(N)->isNullValue());
1445 break;
1446 case MVT::i32:
1447 {
1448 int v = (int)cast<ConstantSDNode>(N)->getSignExtended();
1449 if (v < 32768 && v >= -32768) {
1450 BuildMI(BB, PPC::LI, 1, Result).addSImm(v);
1451 } else {
Nate Begeman5e966612005-03-24 06:28:42 +00001452 Tmp1 = MakeReg(MVT::i32);
1453 BuildMI(BB, PPC::LIS, 1, Tmp1).addSImm(v >> 16);
1454 BuildMI(BB, PPC::ORI, 2, Result).addReg(Tmp1).addImm(v & 0xFFFF);
Nate Begemana9795f82005-03-24 04:41:43 +00001455 }
1456 }
1457 }
1458 return Result;
1459 }
1460
1461 return 0;
1462}
1463
1464void ISel::Select(SDOperand N) {
1465 unsigned Tmp1, Tmp2, Opc;
1466 unsigned opcode = N.getOpcode();
1467
1468 if (!ExprMap.insert(std::make_pair(N, 1)).second)
1469 return; // Already selected.
1470
1471 SDNode *Node = N.Val;
1472
1473 switch (Node->getOpcode()) {
1474 default:
1475 Node->dump(); std::cerr << "\n";
1476 assert(0 && "Node not handled yet!");
1477 case ISD::EntryToken: return; // Noop
1478 case ISD::TokenFactor:
1479 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1480 Select(Node->getOperand(i));
1481 return;
1482 case ISD::ADJCALLSTACKDOWN:
1483 case ISD::ADJCALLSTACKUP:
1484 Select(N.getOperand(0));
1485 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1486 Opc = N.getOpcode() == ISD::ADJCALLSTACKDOWN ? PPC::ADJCALLSTACKDOWN :
1487 PPC::ADJCALLSTACKUP;
1488 BuildMI(BB, Opc, 1).addImm(Tmp1);
1489 return;
1490 case ISD::BR: {
1491 MachineBasicBlock *Dest =
1492 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
Nate Begemana9795f82005-03-24 04:41:43 +00001493 Select(N.getOperand(0));
1494 BuildMI(BB, PPC::B, 1).addMBB(Dest);
1495 return;
1496 }
1497 case ISD::BRCOND:
1498 SelectBranchCC(N);
1499 return;
1500 case ISD::CopyToReg:
1501 Select(N.getOperand(0));
1502 Tmp1 = SelectExpr(N.getOperand(1));
1503 Tmp2 = cast<RegSDNode>(N)->getReg();
1504
1505 if (Tmp1 != Tmp2) {
1506 if (N.getOperand(1).getValueType() == MVT::f64 ||
1507 N.getOperand(1).getValueType() == MVT::f32)
1508 BuildMI(BB, PPC::FMR, 1, Tmp2).addReg(Tmp1);
1509 else
1510 BuildMI(BB, PPC::OR, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
1511 }
1512 return;
1513 case ISD::ImplicitDef:
1514 Select(N.getOperand(0));
1515 BuildMI(BB, PPC::IMPLICIT_DEF, 0, cast<RegSDNode>(N)->getReg());
1516 return;
1517 case ISD::RET:
1518 switch (N.getNumOperands()) {
1519 default:
1520 assert(0 && "Unknown return instruction!");
1521 case 3:
1522 assert(N.getOperand(1).getValueType() == MVT::i32 &&
1523 N.getOperand(2).getValueType() == MVT::i32 &&
1524 "Unknown two-register value!");
1525 Select(N.getOperand(0));
1526 Tmp1 = SelectExpr(N.getOperand(1));
1527 Tmp2 = SelectExpr(N.getOperand(2));
Nate Begeman27523a12005-04-02 00:42:16 +00001528 BuildMI(BB, PPC::OR, 2, PPC::R3).addReg(Tmp2).addReg(Tmp2);
1529 BuildMI(BB, PPC::OR, 2, PPC::R4).addReg(Tmp1).addReg(Tmp1);
Nate Begemana9795f82005-03-24 04:41:43 +00001530 break;
1531 case 2:
1532 Select(N.getOperand(0));
1533 Tmp1 = SelectExpr(N.getOperand(1));
1534 switch (N.getOperand(1).getValueType()) {
1535 default:
1536 assert(0 && "Unknown return type!");
1537 case MVT::f64:
1538 case MVT::f32:
1539 BuildMI(BB, PPC::FMR, 1, PPC::F1).addReg(Tmp1);
1540 break;
1541 case MVT::i32:
1542 BuildMI(BB, PPC::OR, 2, PPC::R3).addReg(Tmp1).addReg(Tmp1);
1543 break;
1544 }
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001545 case 1:
1546 Select(N.getOperand(0));
1547 break;
Nate Begemana9795f82005-03-24 04:41:43 +00001548 }
1549 BuildMI(BB, PPC::BLR, 0); // Just emit a 'ret' instruction
1550 return;
Nate Begemana9795f82005-03-24 04:41:43 +00001551 case ISD::TRUNCSTORE:
1552 case ISD::STORE:
1553 {
1554 SDOperand Chain = N.getOperand(0);
1555 SDOperand Value = N.getOperand(1);
1556 SDOperand Address = N.getOperand(2);
1557 Select(Chain);
1558
1559 Tmp1 = SelectExpr(Value); //value
1560
1561 if (opcode == ISD::STORE) {
1562 switch(Value.getValueType()) {
1563 default: assert(0 && "unknown Type in store");
1564 case MVT::i32: Opc = PPC::STW; break;
1565 case MVT::f64: Opc = PPC::STFD; break;
1566 case MVT::f32: Opc = PPC::STFS; break;
1567 }
1568 } else { //ISD::TRUNCSTORE
1569 switch(cast<MVTSDNode>(Node)->getExtraValueType()) {
1570 default: assert(0 && "unknown Type in store");
1571 case MVT::i1: //FIXME: DAG does not promote this load
1572 case MVT::i8: Opc = PPC::STB; break;
1573 case MVT::i16: Opc = PPC::STH; break;
1574 }
1575 }
1576
Nate Begemana7e11a42005-04-01 05:57:17 +00001577 if(Address.getOpcode() == ISD::FrameIndex)
Nate Begemana9795f82005-03-24 04:41:43 +00001578 {
Nate Begeman58f718c2005-03-30 02:23:08 +00001579 Tmp2 = cast<FrameIndexSDNode>(Address)->getIndex();
1580 addFrameReference(BuildMI(BB, Opc, 3).addReg(Tmp1), (int)Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +00001581 }
1582 else
1583 {
1584 int offset;
Nate Begeman04730362005-04-01 04:45:11 +00001585 bool idx = SelectAddr(Address, Tmp2, offset);
1586 if (idx) {
1587 Opc = IndexedOpForOp(Opc);
1588 BuildMI(BB, Opc, 3).addReg(Tmp1).addReg(Tmp2).addReg(offset);
1589 } else {
1590 BuildMI(BB, Opc, 3).addReg(Tmp1).addImm(offset).addReg(Tmp2);
1591 }
Nate Begemana9795f82005-03-24 04:41:43 +00001592 }
1593 return;
1594 }
1595 case ISD::EXTLOAD:
1596 case ISD::SEXTLOAD:
1597 case ISD::ZEXTLOAD:
1598 case ISD::LOAD:
1599 case ISD::CopyFromReg:
1600 case ISD::CALL:
1601 case ISD::DYNAMIC_STACKALLOC:
1602 ExprMap.erase(N);
1603 SelectExpr(N);
1604 return;
1605 }
1606 assert(0 && "Should not be reached!");
1607}
1608
1609
1610/// createPPC32PatternInstructionSelector - This pass converts an LLVM function
1611/// into a machine code representation using pattern matching and a machine
1612/// description file.
1613///
1614FunctionPass *llvm::createPPC32ISelPattern(TargetMachine &TM) {
1615 return new ISel(TM);
Chris Lattner246fa632005-03-24 06:16:18 +00001616}
1617