blob: 9788d67cf1cac842a85f613548847042ff07fad5 [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.
Nate Begeman815d6da2005-04-06 00:25:27 +000011// Magic number generation for integer divide from the PowerPC Compiler Writer's
12// Guide, section 3.2.3.5
Nate Begemana9795f82005-03-24 04:41:43 +000013//
14//===----------------------------------------------------------------------===//
15
16#include "PowerPC.h"
17#include "PowerPCInstrBuilder.h"
18#include "PowerPCInstrInfo.h"
Nate Begemancd08e4c2005-04-09 20:09:12 +000019#include "PPC32TargetMachine.h"
Nate Begemana9795f82005-03-24 04:41:43 +000020#include "llvm/Constants.h" // FIXME: REMOVE
21#include "llvm/Function.h"
22#include "llvm/CodeGen/MachineConstantPool.h" // FIXME: REMOVE
23#include "llvm/CodeGen/MachineFunction.h"
24#include "llvm/CodeGen/MachineFrameInfo.h"
25#include "llvm/CodeGen/SelectionDAG.h"
26#include "llvm/CodeGen/SelectionDAGISel.h"
27#include "llvm/CodeGen/SSARegMap.h"
28#include "llvm/Target/TargetData.h"
29#include "llvm/Target/TargetLowering.h"
Nate Begeman93075ec2005-04-04 23:40:36 +000030#include "llvm/Target/TargetOptions.h"
Nate Begemana9795f82005-03-24 04:41:43 +000031#include "llvm/Support/Debug.h"
32#include "llvm/Support/MathExtras.h"
33#include "llvm/ADT/Statistic.h"
34#include <set>
35#include <algorithm>
36using namespace llvm;
37
38//===----------------------------------------------------------------------===//
39// PPC32TargetLowering - PPC32 Implementation of the TargetLowering interface
40namespace {
41 class PPC32TargetLowering : public TargetLowering {
42 int VarArgsFrameIndex; // FrameIndex for start of varargs area.
43 int ReturnAddrIndex; // FrameIndex for return slot.
44 public:
45 PPC32TargetLowering(TargetMachine &TM) : TargetLowering(TM) {
Nate Begemana9795f82005-03-24 04:41:43 +000046 // Set up the register classes.
47 addRegisterClass(MVT::i32, PPC32::GPRCRegisterClass);
Nate Begeman7532e2f2005-03-26 08:25:22 +000048 addRegisterClass(MVT::f32, PPC32::FPRCRegisterClass);
Nate Begemana9795f82005-03-24 04:41:43 +000049 addRegisterClass(MVT::f64, PPC32::FPRCRegisterClass);
50
Nate Begeman74d73452005-03-31 00:15:26 +000051 // PowerPC has no intrinsics for these particular operations
Nate Begeman01d05262005-03-30 01:45:43 +000052 setOperationAction(ISD::MEMMOVE, MVT::Other, Expand);
53 setOperationAction(ISD::MEMSET, MVT::Other, Expand);
54 setOperationAction(ISD::MEMCPY, MVT::Other, Expand);
55
Nate Begeman74d73452005-03-31 00:15:26 +000056 // PowerPC has an i16 but no i8 (or i1) SEXTLOAD
57 setOperationAction(ISD::SEXTLOAD, MVT::i1, Expand);
58 setOperationAction(ISD::SEXTLOAD, MVT::i8, Expand);
Nate Begeman815d6da2005-04-06 00:25:27 +000059
60 // PowerPC has no SREM/UREM instructions
61 setOperationAction(ISD::SREM, MVT::i32, Expand);
62 setOperationAction(ISD::UREM, MVT::i32, Expand);
Chris Lattner43fdea02005-04-02 05:03:24 +000063
Nate Begeman27eeb002005-04-02 05:59:34 +000064 setShiftAmountFlavor(Extend); // shl X, 32 == 0
Chris Lattnercbd06fc2005-04-07 19:41:49 +000065 setSetCCResultContents(ZeroOrOneSetCCResult);
Nate Begeman3e897162005-03-31 23:55:40 +000066 addLegalFPImmediate(+0.0); // Necessary for FSEL
67 addLegalFPImmediate(-0.0); //
68
Nate Begemana9795f82005-03-24 04:41:43 +000069 computeRegisterProperties();
70 }
71
72 /// LowerArguments - This hook must be implemented to indicate how we should
73 /// lower the arguments for the specified function, into the specified DAG.
74 virtual std::vector<SDOperand>
75 LowerArguments(Function &F, SelectionDAG &DAG);
76
77 /// LowerCallTo - This hook lowers an abstract call to a function into an
78 /// actual call.
79 virtual std::pair<SDOperand, SDOperand>
Nate Begeman307e7442005-03-26 01:28:53 +000080 LowerCallTo(SDOperand Chain, const Type *RetTy, bool isVarArg,
81 SDOperand Callee, ArgListTy &Args, SelectionDAG &DAG);
Nate Begemana9795f82005-03-24 04:41:43 +000082
83 virtual std::pair<SDOperand, SDOperand>
84 LowerVAStart(SDOperand Chain, SelectionDAG &DAG);
85
86 virtual std::pair<SDOperand,SDOperand>
87 LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
88 const Type *ArgTy, SelectionDAG &DAG);
89
90 virtual std::pair<SDOperand, SDOperand>
91 LowerFrameReturnAddress(bool isFrameAddr, SDOperand Chain, unsigned Depth,
92 SelectionDAG &DAG);
93 };
94}
95
96
97std::vector<SDOperand>
98PPC32TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
99 //
100 // add beautiful description of PPC stack frame format, or at least some docs
101 //
102 MachineFunction &MF = DAG.getMachineFunction();
103 MachineFrameInfo *MFI = MF.getFrameInfo();
104 MachineBasicBlock& BB = MF.front();
105 std::vector<SDOperand> ArgValues;
106
107 // Due to the rather complicated nature of the PowerPC ABI, rather than a
108 // fixed size array of physical args, for the sake of simplicity let the STL
109 // handle tracking them for us.
110 std::vector<unsigned> argVR, argPR, argOp;
111 unsigned ArgOffset = 24;
112 unsigned GPR_remaining = 8;
113 unsigned FPR_remaining = 13;
114 unsigned GPR_idx = 0, FPR_idx = 0;
115 static const unsigned GPR[] = {
116 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
117 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
118 };
119 static const unsigned FPR[] = {
120 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
121 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
122 };
123
124 // Add DAG nodes to load the arguments... On entry to a function on PPC,
125 // the arguments start at offset 24, although they are likely to be passed
126 // in registers.
127 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
128 SDOperand newroot, argt;
129 unsigned ObjSize;
130 bool needsLoad = false;
Nate Begemancd08e4c2005-04-09 20:09:12 +0000131 bool ArgLive = !I->use_empty();
Nate Begemana9795f82005-03-24 04:41:43 +0000132 MVT::ValueType ObjectVT = getValueType(I->getType());
133
134 switch (ObjectVT) {
135 default: assert(0 && "Unhandled argument type!");
136 case MVT::i1:
137 case MVT::i8:
138 case MVT::i16:
139 case MVT::i32:
140 ObjSize = 4;
Nate Begemancd08e4c2005-04-09 20:09:12 +0000141 if (!ArgLive) break;
Nate Begemana9795f82005-03-24 04:41:43 +0000142 if (GPR_remaining > 0) {
Nate Begemancd08e4c2005-04-09 20:09:12 +0000143 MF.addLiveIn(GPR[GPR_idx]);
Nate Begemanf70b5762005-03-28 23:08:54 +0000144 argt = newroot = DAG.getCopyFromReg(GPR[GPR_idx], MVT::i32,
145 DAG.getRoot());
Nate Begemana9795f82005-03-24 04:41:43 +0000146 if (ObjectVT != MVT::i32)
147 argt = DAG.getNode(ISD::TRUNCATE, ObjectVT, newroot);
Nate Begemana9795f82005-03-24 04:41:43 +0000148 } else {
149 needsLoad = true;
150 }
151 break;
Nate Begemanf7e43382005-03-26 07:46:36 +0000152 case MVT::i64: ObjSize = 8;
Nate Begemancd08e4c2005-04-09 20:09:12 +0000153 if (!ArgLive) break;
Nate Begemanc5b1cd22005-04-10 05:53:14 +0000154 if (GPR_remaining > 0) {
155 SDOperand argHi, argLo;
Nate Begemancd08e4c2005-04-09 20:09:12 +0000156 MF.addLiveIn(GPR[GPR_idx]);
Nate Begemanc5b1cd22005-04-10 05:53:14 +0000157 argHi = DAG.getCopyFromReg(GPR[GPR_idx], MVT::i32, DAG.getRoot());
158 // If we have two or more remaining argument registers, then both halves
159 // of the i64 can be sourced from there. Otherwise, the lower half will
160 // have to come off the stack. This can happen when an i64 is preceded
161 // by 28 bytes of arguments.
162 if (GPR_remaining > 1) {
163 MF.addLiveIn(GPR[GPR_idx+1]);
164 argLo = DAG.getCopyFromReg(GPR[GPR_idx+1], MVT::i32, argHi);
165 } else {
166 int FI = MFI->CreateFixedObject(4, ArgOffset+4);
167 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
168 argLo = DAG.getLoad(MVT::i32, DAG.getEntryNode(), FIN);
169 }
Nate Begemanca12a2b2005-03-28 22:28:37 +0000170 // Build the outgoing arg thingy
Nate Begemanf70b5762005-03-28 23:08:54 +0000171 argt = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, argLo, argHi);
172 newroot = argLo;
Nate Begemana9795f82005-03-24 04:41:43 +0000173 } else {
174 needsLoad = true;
175 }
176 break;
Nate Begemancd08e4c2005-04-09 20:09:12 +0000177 case MVT::f32:
178 case MVT::f64:
179 ObjSize = (ObjectVT == MVT::f64) ? 8 : 4;
180 if (!ArgLive) break;
Nate Begemana9795f82005-03-24 04:41:43 +0000181 if (FPR_remaining > 0) {
Nate Begemancd08e4c2005-04-09 20:09:12 +0000182 MF.addLiveIn(FPR[FPR_idx]);
Nate Begemanf70b5762005-03-28 23:08:54 +0000183 argt = newroot = DAG.getCopyFromReg(FPR[FPR_idx], ObjectVT,
184 DAG.getRoot());
Nate Begemana9795f82005-03-24 04:41:43 +0000185 --FPR_remaining;
186 ++FPR_idx;
187 } else {
188 needsLoad = true;
189 }
190 break;
191 }
192
193 // We need to load the argument to a virtual register if we determined above
194 // that we ran out of physical registers of the appropriate type
195 if (needsLoad) {
Nate Begemane5846682005-04-04 06:52:38 +0000196 unsigned SubregOffset = 0;
Nate Begemanc3e2db42005-04-04 09:09:00 +0000197 if (ObjectVT == MVT::i8 || ObjectVT == MVT::i1) SubregOffset = 3;
Nate Begemane5846682005-04-04 06:52:38 +0000198 if (ObjectVT == MVT::i16) SubregOffset = 2;
Nate Begemana9795f82005-03-24 04:41:43 +0000199 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
200 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
Nate Begemane5846682005-04-04 06:52:38 +0000201 FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN,
202 DAG.getConstant(SubregOffset, MVT::i32));
Nate Begemana9795f82005-03-24 04:41:43 +0000203 argt = newroot = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN);
204 }
205
206 // Every 4 bytes of argument space consumes one of the GPRs available for
207 // argument passing.
208 if (GPR_remaining > 0) {
209 unsigned delta = (GPR_remaining > 1 && ObjSize == 8) ? 2 : 1;
210 GPR_remaining -= delta;
211 GPR_idx += delta;
212 }
213 ArgOffset += ObjSize;
Chris Lattner91277ea2005-04-09 21:23:24 +0000214 if (newroot.Val)
215 DAG.setRoot(newroot.getValue(1));
Nate Begemana9795f82005-03-24 04:41:43 +0000216
Nate Begemana9795f82005-03-24 04:41:43 +0000217 ArgValues.push_back(argt);
218 }
219
Nate Begemana9795f82005-03-24 04:41:43 +0000220 // If the function takes variable number of arguments, make a frame index for
221 // the start of the first vararg value... for expansion of llvm.va_start.
Nate Begemanfa554702005-04-03 22:13:27 +0000222 if (F.isVarArg()) {
Nate Begemana9795f82005-03-24 04:41:43 +0000223 VarArgsFrameIndex = MFI->CreateFixedObject(4, ArgOffset);
Nate Begemanfa554702005-04-03 22:13:27 +0000224 SDOperand FIN = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
Nate Begeman6644d4c2005-04-03 23:11:17 +0000225 // If this function is vararg, store any remaining integer argument regs
226 // to their spots on the stack so that they may be loaded by deferencing the
227 // result of va_next.
228 std::vector<SDOperand> MemOps;
229 for (; GPR_remaining > 0; --GPR_remaining, ++GPR_idx) {
Nate Begemancd08e4c2005-04-09 20:09:12 +0000230 MF.addLiveIn(GPR[GPR_idx]);
Nate Begeman6644d4c2005-04-03 23:11:17 +0000231 SDOperand Val = DAG.getCopyFromReg(GPR[GPR_idx], MVT::i32, DAG.getRoot());
232 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Val.getValue(1),
233 Val, FIN);
234 MemOps.push_back(Store);
235 // Increment the address by four for the next argument to store
236 SDOperand PtrOff = DAG.getConstant(4, getPointerTy());
237 FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN, PtrOff);
238 }
239 DAG.setRoot(DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps));
Nate Begemanfa554702005-04-03 22:13:27 +0000240 }
Nate Begemana9795f82005-03-24 04:41:43 +0000241
Nate Begemancd08e4c2005-04-09 20:09:12 +0000242 // Finally, inform the code generator which regs we return values in.
243 switch (getValueType(F.getReturnType())) {
244 default: assert(0 && "Unknown type!");
245 case MVT::isVoid: break;
246 case MVT::i1:
247 case MVT::i8:
248 case MVT::i16:
249 case MVT::i32:
250 MF.addLiveOut(PPC::R3);
251 break;
252 case MVT::i64:
253 MF.addLiveOut(PPC::R3);
254 MF.addLiveOut(PPC::R4);
255 break;
256 case MVT::f32:
257 case MVT::f64:
258 MF.addLiveOut(PPC::F1);
259 break;
260 }
261
Nate Begemana9795f82005-03-24 04:41:43 +0000262 return ArgValues;
263}
264
265std::pair<SDOperand, SDOperand>
266PPC32TargetLowering::LowerCallTo(SDOperand Chain,
Nate Begeman307e7442005-03-26 01:28:53 +0000267 const Type *RetTy, bool isVarArg,
268 SDOperand Callee, ArgListTy &Args, SelectionDAG &DAG) {
269 // args_to_use will accumulate outgoing args for the ISD::CALL case in
270 // SelectExpr to use to put the arguments in the appropriate registers.
Nate Begemana9795f82005-03-24 04:41:43 +0000271 std::vector<SDOperand> args_to_use;
Nate Begeman307e7442005-03-26 01:28:53 +0000272
273 // Count how many bytes are to be pushed on the stack, including the linkage
274 // area, and parameter passing area.
275 unsigned NumBytes = 24;
276
277 if (Args.empty()) {
Nate Begemana7e11a42005-04-01 05:57:17 +0000278 Chain = DAG.getNode(ISD::ADJCALLSTACKDOWN, MVT::Other, Chain,
279 DAG.getConstant(NumBytes, getPointerTy()));
Nate Begeman307e7442005-03-26 01:28:53 +0000280 } else {
281 for (unsigned i = 0, e = Args.size(); i != e; ++i)
282 switch (getValueType(Args[i].second)) {
283 default: assert(0 && "Unknown value type!");
284 case MVT::i1:
285 case MVT::i8:
286 case MVT::i16:
287 case MVT::i32:
288 case MVT::f32:
289 NumBytes += 4;
290 break;
291 case MVT::i64:
292 case MVT::f64:
293 NumBytes += 8;
294 break;
295 }
296
297 // Just to be safe, we'll always reserve the full 24 bytes of linkage area
298 // plus 32 bytes of argument space in case any called code gets funky on us.
299 if (NumBytes < 56) NumBytes = 56;
300
301 // Adjust the stack pointer for the new arguments...
302 // These operations are automatically eliminated by the prolog/epilog pass
303 Chain = DAG.getNode(ISD::ADJCALLSTACKDOWN, MVT::Other, Chain,
304 DAG.getConstant(NumBytes, getPointerTy()));
305
306 // Set up a copy of the stack pointer for use loading and storing any
307 // arguments that may not fit in the registers available for argument
308 // passing.
309 SDOperand StackPtr = DAG.getCopyFromReg(PPC::R1, MVT::i32,
310 DAG.getEntryNode());
311
312 // Figure out which arguments are going to go in registers, and which in
313 // memory. Also, if this is a vararg function, floating point operations
314 // must be stored to our stack, and loaded into integer regs as well, if
315 // any integer regs are available for argument passing.
316 unsigned ArgOffset = 24;
317 unsigned GPR_remaining = 8;
318 unsigned FPR_remaining = 13;
Nate Begeman74d73452005-03-31 00:15:26 +0000319
320 std::vector<SDOperand> MemOps;
Nate Begeman307e7442005-03-26 01:28:53 +0000321 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
322 // PtrOff will be used to store the current argument to the stack if a
323 // register cannot be found for it.
324 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
325 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
Nate Begemanf7e43382005-03-26 07:46:36 +0000326 MVT::ValueType ArgVT = getValueType(Args[i].second);
Nate Begeman307e7442005-03-26 01:28:53 +0000327
Nate Begemanf7e43382005-03-26 07:46:36 +0000328 switch (ArgVT) {
Nate Begeman307e7442005-03-26 01:28:53 +0000329 default: assert(0 && "Unexpected ValueType for argument!");
330 case MVT::i1:
331 case MVT::i8:
332 case MVT::i16:
333 // Promote the integer to 32 bits. If the input type is signed use a
334 // sign extend, otherwise use a zero extend.
335 if (Args[i].second->isSigned())
336 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
337 else
338 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
339 // FALL THROUGH
340 case MVT::i32:
341 if (GPR_remaining > 0) {
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000342 args_to_use.push_back(Args[i].first);
Nate Begeman307e7442005-03-26 01:28:53 +0000343 --GPR_remaining;
344 } else {
Nate Begeman74d73452005-03-31 00:15:26 +0000345 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
346 Args[i].first, PtrOff));
Nate Begeman307e7442005-03-26 01:28:53 +0000347 }
348 ArgOffset += 4;
349 break;
350 case MVT::i64:
Nate Begemanf7e43382005-03-26 07:46:36 +0000351 // If we have one free GPR left, we can place the upper half of the i64
352 // in it, and store the other half to the stack. If we have two or more
353 // free GPRs, then we can pass both halves of the i64 in registers.
354 if (GPR_remaining > 0) {
Nate Begemanf2622612005-03-26 02:17:46 +0000355 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
356 Args[i].first, DAG.getConstant(1, MVT::i32));
357 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
358 Args[i].first, DAG.getConstant(0, MVT::i32));
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000359 args_to_use.push_back(Hi);
Nate Begeman74d73452005-03-31 00:15:26 +0000360 --GPR_remaining;
Nate Begeman74d73452005-03-31 00:15:26 +0000361 if (GPR_remaining > 0) {
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000362 args_to_use.push_back(Lo);
Nate Begeman74d73452005-03-31 00:15:26 +0000363 --GPR_remaining;
Nate Begemanf7e43382005-03-26 07:46:36 +0000364 } else {
365 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
366 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
Nate Begeman74d73452005-03-31 00:15:26 +0000367 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
368 Lo, PtrOff));
Nate Begemanf7e43382005-03-26 07:46:36 +0000369 }
Nate Begeman307e7442005-03-26 01:28:53 +0000370 } else {
Nate Begeman74d73452005-03-31 00:15:26 +0000371 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
372 Args[i].first, PtrOff));
Nate Begeman307e7442005-03-26 01:28:53 +0000373 }
374 ArgOffset += 8;
375 break;
376 case MVT::f32:
Nate Begeman307e7442005-03-26 01:28:53 +0000377 case MVT::f64:
Nate Begemanf7e43382005-03-26 07:46:36 +0000378 if (FPR_remaining > 0) {
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000379 args_to_use.push_back(Args[i].first);
380 --FPR_remaining;
Nate Begemanf7e43382005-03-26 07:46:36 +0000381 if (isVarArg) {
Nate Begeman96fc6812005-03-31 02:05:53 +0000382 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Chain,
383 Args[i].first, PtrOff);
384 MemOps.push_back(Store);
Nate Begeman74d73452005-03-31 00:15:26 +0000385 // Float varargs are always shadowed in available integer registers
386 if (GPR_remaining > 0) {
Nate Begeman96fc6812005-03-31 02:05:53 +0000387 SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff);
Nate Begeman74d73452005-03-31 00:15:26 +0000388 MemOps.push_back(Load);
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000389 args_to_use.push_back(Load);
390 --GPR_remaining;
Nate Begeman74d73452005-03-31 00:15:26 +0000391 }
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000392 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
Nate Begeman74d73452005-03-31 00:15:26 +0000393 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
394 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
Nate Begeman96fc6812005-03-31 02:05:53 +0000395 SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff);
Nate Begeman74d73452005-03-31 00:15:26 +0000396 MemOps.push_back(Load);
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000397 args_to_use.push_back(Load);
398 --GPR_remaining;
Nate Begeman74d73452005-03-31 00:15:26 +0000399 }
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000400 } else {
401 // If we have any FPRs remaining, we may also have GPRs remaining.
402 // Args passed in FPRs consume either 1 (f32) or 2 (f64) available
403 // GPRs.
404 if (GPR_remaining > 0) {
405 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
406 --GPR_remaining;
407 }
408 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
409 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
410 --GPR_remaining;
411 }
Nate Begeman74d73452005-03-31 00:15:26 +0000412 }
Nate Begeman307e7442005-03-26 01:28:53 +0000413 } else {
Nate Begeman74d73452005-03-31 00:15:26 +0000414 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
415 Args[i].first, PtrOff));
Nate Begeman307e7442005-03-26 01:28:53 +0000416 }
Nate Begemanf7e43382005-03-26 07:46:36 +0000417 ArgOffset += (ArgVT == MVT::f32) ? 4 : 8;
Nate Begeman307e7442005-03-26 01:28:53 +0000418 break;
419 }
Nate Begemana9795f82005-03-24 04:41:43 +0000420 }
Nate Begeman74d73452005-03-31 00:15:26 +0000421 if (!MemOps.empty())
422 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps);
Nate Begemana9795f82005-03-24 04:41:43 +0000423 }
424
425 std::vector<MVT::ValueType> RetVals;
426 MVT::ValueType RetTyVT = getValueType(RetTy);
427 if (RetTyVT != MVT::isVoid)
428 RetVals.push_back(RetTyVT);
429 RetVals.push_back(MVT::Other);
430
431 SDOperand TheCall = SDOperand(DAG.getCall(RetVals,
432 Chain, Callee, args_to_use), 0);
433 Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
434 Chain = DAG.getNode(ISD::ADJCALLSTACKUP, MVT::Other, Chain,
435 DAG.getConstant(NumBytes, getPointerTy()));
436 return std::make_pair(TheCall, Chain);
437}
438
439std::pair<SDOperand, SDOperand>
440PPC32TargetLowering::LowerVAStart(SDOperand Chain, SelectionDAG &DAG) {
441 //vastart just returns the address of the VarArgsFrameIndex slot.
442 return std::make_pair(DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32), Chain);
443}
444
445std::pair<SDOperand,SDOperand> PPC32TargetLowering::
446LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
447 const Type *ArgTy, SelectionDAG &DAG) {
Nate Begemanc7b09f12005-03-25 08:34:25 +0000448 MVT::ValueType ArgVT = getValueType(ArgTy);
449 SDOperand Result;
450 if (!isVANext) {
451 Result = DAG.getLoad(ArgVT, DAG.getEntryNode(), VAList);
452 } else {
453 unsigned Amt;
454 if (ArgVT == MVT::i32 || ArgVT == MVT::f32)
455 Amt = 4;
456 else {
457 assert((ArgVT == MVT::i64 || ArgVT == MVT::f64) &&
458 "Other types should have been promoted for varargs!");
459 Amt = 8;
460 }
461 Result = DAG.getNode(ISD::ADD, VAList.getValueType(), VAList,
462 DAG.getConstant(Amt, VAList.getValueType()));
463 }
464 return std::make_pair(Result, Chain);
Nate Begemana9795f82005-03-24 04:41:43 +0000465}
466
467
468std::pair<SDOperand, SDOperand> PPC32TargetLowering::
469LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
470 SelectionDAG &DAG) {
Nate Begeman01d05262005-03-30 01:45:43 +0000471 assert(0 && "LowerFrameReturnAddress unimplemented");
Nate Begemana9795f82005-03-24 04:41:43 +0000472 abort();
473}
474
475namespace {
Nate Begemanc7bd4822005-04-11 06:34:10 +0000476Statistic<>Recorded("ppc-codegen", "Number of recording ops emitted");
Nate Begeman93075ec2005-04-04 23:40:36 +0000477Statistic<>FusedFP("ppc-codegen", "Number of fused fp operations");
Nate Begemana9795f82005-03-24 04:41:43 +0000478//===--------------------------------------------------------------------===//
479/// ISel - PPC32 specific code to select PPC32 machine instructions for
480/// SelectionDAG operations.
481//===--------------------------------------------------------------------===//
482class ISel : public SelectionDAGISel {
Nate Begemana9795f82005-03-24 04:41:43 +0000483 PPC32TargetLowering PPC32Lowering;
Nate Begeman815d6da2005-04-06 00:25:27 +0000484 SelectionDAG *ISelDAG; // Hack to support us having a dag->dag transform
485 // for sdiv and udiv until it is put into the future
486 // dag combiner.
Nate Begemana9795f82005-03-24 04:41:43 +0000487
488 /// ExprMap - As shared expressions are codegen'd, we keep track of which
489 /// vreg the value is produced in, so we only emit one copy of each compiled
490 /// tree.
491 std::map<SDOperand, unsigned> ExprMap;
Nate Begemanc7b09f12005-03-25 08:34:25 +0000492
493 unsigned GlobalBaseReg;
494 bool GlobalBaseInitialized;
Nate Begemanc7bd4822005-04-11 06:34:10 +0000495 bool RecordSuccess;
Nate Begemana9795f82005-03-24 04:41:43 +0000496public:
Nate Begeman815d6da2005-04-06 00:25:27 +0000497 ISel(TargetMachine &TM) : SelectionDAGISel(PPC32Lowering), PPC32Lowering(TM),
498 ISelDAG(0) {}
Nate Begemana9795f82005-03-24 04:41:43 +0000499
Nate Begemanc7b09f12005-03-25 08:34:25 +0000500 /// runOnFunction - Override this function in order to reset our per-function
501 /// variables.
502 virtual bool runOnFunction(Function &Fn) {
503 // Make sure we re-emit a set of the global base reg if necessary
504 GlobalBaseInitialized = false;
505 return SelectionDAGISel::runOnFunction(Fn);
506 }
507
Nate Begemana9795f82005-03-24 04:41:43 +0000508 /// InstructionSelectBasicBlock - This callback is invoked by
509 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
510 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG) {
511 DEBUG(BB->dump());
512 // Codegen the basic block.
Nate Begeman815d6da2005-04-06 00:25:27 +0000513 ISelDAG = &DAG;
Nate Begemana9795f82005-03-24 04:41:43 +0000514 Select(DAG.getRoot());
515
516 // Clear state used for selection.
517 ExprMap.clear();
Nate Begeman815d6da2005-04-06 00:25:27 +0000518 ISelDAG = 0;
Nate Begemana9795f82005-03-24 04:41:43 +0000519 }
Nate Begeman815d6da2005-04-06 00:25:27 +0000520
521 // dag -> dag expanders for integer divide by constant
522 SDOperand BuildSDIVSequence(SDOperand N);
523 SDOperand BuildUDIVSequence(SDOperand N);
Nate Begemana9795f82005-03-24 04:41:43 +0000524
Nate Begemandffcfcc2005-04-01 00:32:34 +0000525 unsigned getGlobalBaseReg();
Nate Begeman6b559972005-04-01 02:59:27 +0000526 unsigned getConstDouble(double floatVal, unsigned Result);
Nate Begeman7ddecb42005-04-06 23:51:40 +0000527 bool SelectBitfieldInsert(SDOperand OR, unsigned Result);
Nate Begemandffcfcc2005-04-01 00:32:34 +0000528 unsigned SelectSetCR0(SDOperand CC);
Nate Begemanc7bd4822005-04-11 06:34:10 +0000529 unsigned SelectExpr(SDOperand N, bool Recording=false);
Nate Begemana9795f82005-03-24 04:41:43 +0000530 unsigned SelectExprFP(SDOperand N, unsigned Result);
531 void Select(SDOperand N);
532
Nate Begeman04730362005-04-01 04:45:11 +0000533 bool SelectAddr(SDOperand N, unsigned& Reg, int& offset);
Nate Begemana9795f82005-03-24 04:41:43 +0000534 void SelectBranchCC(SDOperand N);
535};
536
Nate Begeman80196b12005-04-05 00:15:08 +0000537/// ExactLog2 - This function solves for (Val == 1 << (N-1)) and returns N. It
538/// returns zero when the input is not exactly a power of two.
539static unsigned ExactLog2(unsigned Val) {
540 if (Val == 0 || (Val & (Val-1))) return 0;
541 unsigned Count = 0;
542 while (Val != 1) {
543 Val >>= 1;
544 ++Count;
545 }
546 return Count;
547}
548
Nate Begeman7ddecb42005-04-06 23:51:40 +0000549// IsRunOfOnes - returns true if Val consists of one contiguous run of 1's with
550// any number of 0's on either side. the 1's are allowed to wrap from LSB to
551// MSB. so 0x000FFF0, 0x0000FFFF, and 0xFF0000FF are all runs. 0x0F0F0000 is
552// not, since all 1's are not contiguous.
553static bool IsRunOfOnes(unsigned Val, unsigned &MB, unsigned &ME) {
554 bool isRun = true;
555 MB = 0;
556 ME = 0;
557
558 // look for first set bit
559 int i = 0;
560 for (; i < 32; i++) {
561 if ((Val & (1 << (31 - i))) != 0) {
562 MB = i;
563 ME = i;
564 break;
565 }
566 }
567
568 // look for last set bit
569 for (; i < 32; i++) {
570 if ((Val & (1 << (31 - i))) == 0)
571 break;
572 ME = i;
573 }
574
575 // look for next set bit
576 for (; i < 32; i++) {
577 if ((Val & (1 << (31 - i))) != 0)
578 break;
579 }
580
581 // if we exhausted all the bits, we found a match at this point for 0*1*0*
582 if (i == 32)
583 return true;
584
585 // since we just encountered more 1's, if it doesn't wrap around to the
586 // most significant bit of the word, then we did not find a match to 1*0*1* so
587 // exit.
588 if (MB != 0)
589 return false;
590
591 // look for last set bit
592 for (MB = i; i < 32; i++) {
593 if ((Val & (1 << (31 - i))) == 0)
594 break;
595 }
596
597 // if we exhausted all the bits, then we found a match for 1*0*1*, otherwise,
598 // the value is not a run of ones.
599 if (i == 32)
600 return true;
601 return false;
602}
603
Nate Begeman439b4442005-04-05 04:22:58 +0000604/// getImmediateForOpcode - This method returns a value indicating whether
Nate Begemana9795f82005-03-24 04:41:43 +0000605/// the ConstantSDNode N can be used as an immediate to Opcode. The return
606/// values are either 0, 1 or 2. 0 indicates that either N is not a
Nate Begeman9f833d32005-04-12 00:10:02 +0000607/// ConstantSDNode, or is not suitable for use by that opcode.
608/// Return value codes for turning into an enum someday:
609/// 1: constant may be used in normal immediate form.
610/// 2: constant may be used in shifted immediate form.
611/// 3: log base 2 of the constant may be used.
612/// 4: constant is suitable for integer division conversion
613/// 5: constant is a bitfield mask
Nate Begemana9795f82005-03-24 04:41:43 +0000614///
Nate Begeman439b4442005-04-05 04:22:58 +0000615static unsigned getImmediateForOpcode(SDOperand N, unsigned Opcode,
616 unsigned& Imm, bool U = false) {
Nate Begemana9795f82005-03-24 04:41:43 +0000617 if (N.getOpcode() != ISD::Constant) return 0;
618
619 int v = (int)cast<ConstantSDNode>(N)->getSignExtended();
620
621 switch(Opcode) {
622 default: return 0;
623 case ISD::ADD:
624 if (v <= 32767 && v >= -32768) { Imm = v & 0xFFFF; return 1; }
625 if ((v & 0x0000FFFF) == 0) { Imm = v >> 16; return 2; }
626 break;
Nate Begeman9f833d32005-04-12 00:10:02 +0000627 case ISD::AND: {
628 unsigned MB, ME;
629 if (IsRunOfOnes(v, MB, ME)) { Imm = MB << 16 | ME & 0xFFFF; return 5; }
630 if (v >= 0 && v <= 65535) { Imm = v & 0xFFFF; return 1; }
631 if ((v & 0x0000FFFF) == 0) { Imm = v >> 16; return 2; }
632 break;
633 }
Nate Begemana9795f82005-03-24 04:41:43 +0000634 case ISD::XOR:
635 case ISD::OR:
636 if (v >= 0 && v <= 65535) { Imm = v & 0xFFFF; return 1; }
637 if ((v & 0x0000FFFF) == 0) { Imm = v >> 16; return 2; }
638 break;
Nate Begeman307e7442005-03-26 01:28:53 +0000639 case ISD::MUL:
Nate Begeman27523a12005-04-02 00:42:16 +0000640 case ISD::SUB:
Nate Begeman307e7442005-03-26 01:28:53 +0000641 if (v <= 32767 && v >= -32768) { Imm = v & 0xFFFF; return 1; }
642 break;
Nate Begeman3e897162005-03-31 23:55:40 +0000643 case ISD::SETCC:
644 if (U && (v >= 0 && v <= 65535)) { Imm = v & 0xFFFF; return 1; }
645 if (!U && (v <= 32767 && v >= -32768)) { Imm = v & 0xFFFF; return 1; }
646 break;
Nate Begeman80196b12005-04-05 00:15:08 +0000647 case ISD::SDIV:
Nate Begeman439b4442005-04-05 04:22:58 +0000648 if ((Imm = ExactLog2(v))) { return 3; }
Nate Begeman9f833d32005-04-12 00:10:02 +0000649 if ((Imm = ExactLog2(-v))) { Imm = -Imm; return 3; }
Nate Begeman815d6da2005-04-06 00:25:27 +0000650 if (v <= -2 || v >= 2) { return 4; }
651 break;
652 case ISD::UDIV:
Nate Begeman27b4c232005-04-06 06:44:57 +0000653 if (v > 1) { return 4; }
Nate Begeman80196b12005-04-05 00:15:08 +0000654 break;
Nate Begemana9795f82005-03-24 04:41:43 +0000655 }
656 return 0;
657}
Nate Begeman3e897162005-03-31 23:55:40 +0000658
Nate Begemanc7bd4822005-04-11 06:34:10 +0000659/// NodeHasRecordingVariant - If SelectExpr can always produce code for
660/// NodeOpcode that also sets CR0 as a side effect, return true. Otherwise,
661/// return false.
662static bool NodeHasRecordingVariant(unsigned NodeOpcode) {
663 switch(NodeOpcode) {
664 default: return false;
665 case ISD::AND:
Nate Begeman9765c252005-04-12 21:22:28 +0000666 case ISD::OR:
667 case ISD::ZERO_EXTEND_INREG: return true;
Nate Begemanc7bd4822005-04-11 06:34:10 +0000668 }
669}
670
Nate Begeman3e897162005-03-31 23:55:40 +0000671/// getBCCForSetCC - Returns the PowerPC condition branch mnemonic corresponding
672/// to Condition. If the Condition is unordered or unsigned, the bool argument
673/// U is set to true, otherwise it is set to false.
674static unsigned getBCCForSetCC(unsigned Condition, bool& U) {
675 U = false;
676 switch (Condition) {
677 default: assert(0 && "Unknown condition!"); abort();
678 case ISD::SETEQ: return PPC::BEQ;
679 case ISD::SETNE: return PPC::BNE;
680 case ISD::SETULT: U = true;
681 case ISD::SETLT: return PPC::BLT;
682 case ISD::SETULE: U = true;
683 case ISD::SETLE: return PPC::BLE;
684 case ISD::SETUGT: U = true;
685 case ISD::SETGT: return PPC::BGT;
686 case ISD::SETUGE: U = true;
687 case ISD::SETGE: return PPC::BGE;
688 }
Nate Begeman04730362005-04-01 04:45:11 +0000689 return 0;
690}
691
692/// IndexedOpForOp - Return the indexed variant for each of the PowerPC load
693/// and store immediate instructions.
694static unsigned IndexedOpForOp(unsigned Opcode) {
695 switch(Opcode) {
696 default: assert(0 && "Unknown opcode!"); abort();
697 case PPC::LBZ: return PPC::LBZX; case PPC::STB: return PPC::STBX;
698 case PPC::LHZ: return PPC::LHZX; case PPC::STH: return PPC::STHX;
699 case PPC::LHA: return PPC::LHAX; case PPC::STW: return PPC::STWX;
700 case PPC::LWZ: return PPC::LWZX; case PPC::STFS: return PPC::STFSX;
701 case PPC::LFS: return PPC::LFSX; case PPC::STFD: return PPC::STFDX;
702 case PPC::LFD: return PPC::LFDX;
703 }
704 return 0;
Nate Begeman3e897162005-03-31 23:55:40 +0000705}
Nate Begeman815d6da2005-04-06 00:25:27 +0000706
707// Structure used to return the necessary information to codegen an SDIV as
708// a multiply.
709struct ms {
710 int m; // magic number
711 int s; // shift amount
712};
713
714struct mu {
715 unsigned int m; // magic number
716 int a; // add indicator
717 int s; // shift amount
718};
719
720/// magic - calculate the magic numbers required to codegen an integer sdiv as
721/// a sequence of multiply and shifts. Requires that the divisor not be 0, 1,
722/// or -1.
723static struct ms magic(int d) {
724 int p;
725 unsigned int ad, anc, delta, q1, r1, q2, r2, t;
726 const unsigned int two31 = 2147483648U; // 2^31
727 struct ms mag;
728
729 ad = abs(d);
730 t = two31 + ((unsigned int)d >> 31);
731 anc = t - 1 - t%ad; // absolute value of nc
732 p = 31; // initialize p
733 q1 = two31/anc; // initialize q1 = 2p/abs(nc)
734 r1 = two31 - q1*anc; // initialize r1 = rem(2p,abs(nc))
735 q2 = two31/ad; // initialize q2 = 2p/abs(d)
736 r2 = two31 - q2*ad; // initialize r2 = rem(2p,abs(d))
737 do {
738 p = p + 1;
739 q1 = 2*q1; // update q1 = 2p/abs(nc)
740 r1 = 2*r1; // update r1 = rem(2p/abs(nc))
741 if (r1 >= anc) { // must be unsigned comparison
742 q1 = q1 + 1;
743 r1 = r1 - anc;
744 }
745 q2 = 2*q2; // update q2 = 2p/abs(d)
746 r2 = 2*r2; // update r2 = rem(2p/abs(d))
747 if (r2 >= ad) { // must be unsigned comparison
748 q2 = q2 + 1;
749 r2 = r2 - ad;
750 }
751 delta = ad - r2;
752 } while (q1 < delta || (q1 == delta && r1 == 0));
753
754 mag.m = q2 + 1;
755 if (d < 0) mag.m = -mag.m; // resulting magic number
756 mag.s = p - 32; // resulting shift
757 return mag;
758}
759
760/// magicu - calculate the magic numbers required to codegen an integer udiv as
761/// a sequence of multiply, add and shifts. Requires that the divisor not be 0.
762static struct mu magicu(unsigned d)
763{
764 int p;
765 unsigned int nc, delta, q1, r1, q2, r2;
766 struct mu magu;
767 magu.a = 0; // initialize "add" indicator
768 nc = - 1 - (-d)%d;
769 p = 31; // initialize p
770 q1 = 0x80000000/nc; // initialize q1 = 2p/nc
771 r1 = 0x80000000 - q1*nc; // initialize r1 = rem(2p,nc)
772 q2 = 0x7FFFFFFF/d; // initialize q2 = (2p-1)/d
773 r2 = 0x7FFFFFFF - q2*d; // initialize r2 = rem((2p-1),d)
774 do {
775 p = p + 1;
776 if (r1 >= nc - r1 ) {
777 q1 = 2*q1 + 1; // update q1
778 r1 = 2*r1 - nc; // update r1
779 }
780 else {
781 q1 = 2*q1; // update q1
782 r1 = 2*r1; // update r1
783 }
784 if (r2 + 1 >= d - r2) {
785 if (q2 >= 0x7FFFFFFF) magu.a = 1;
786 q2 = 2*q2 + 1; // update q2
787 r2 = 2*r2 + 1 - d; // update r2
788 }
789 else {
790 if (q2 >= 0x80000000) magu.a = 1;
791 q2 = 2*q2; // update q2
792 r2 = 2*r2 + 1; // update r2
793 }
794 delta = d - 1 - r2;
795 } while (p < 64 && (q1 < delta || (q1 == delta && r1 == 0)));
796 magu.m = q2 + 1; // resulting magic number
797 magu.s = p - 32; // resulting shift
798 return magu;
799}
800}
801
802/// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
803/// return a DAG expression to select that will generate the same value by
804/// multiplying by a magic number. See:
805/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
806SDOperand ISel::BuildSDIVSequence(SDOperand N) {
807 int d = (int)cast<ConstantSDNode>(N.getOperand(1))->getSignExtended();
808 ms magics = magic(d);
809 // Multiply the numerator (operand 0) by the magic value
810 SDOperand Q = ISelDAG->getNode(ISD::MULHS, MVT::i32, N.getOperand(0),
811 ISelDAG->getConstant(magics.m, MVT::i32));
812 // If d > 0 and m < 0, add the numerator
813 if (d > 0 && magics.m < 0)
814 Q = ISelDAG->getNode(ISD::ADD, MVT::i32, Q, N.getOperand(0));
815 // If d < 0 and m > 0, subtract the numerator.
816 if (d < 0 && magics.m > 0)
817 Q = ISelDAG->getNode(ISD::SUB, MVT::i32, Q, N.getOperand(0));
818 // Shift right algebraic if shift value is nonzero
819 if (magics.s > 0)
820 Q = ISelDAG->getNode(ISD::SRA, MVT::i32, Q,
821 ISelDAG->getConstant(magics.s, MVT::i32));
822 // Extract the sign bit and add it to the quotient
823 SDOperand T =
824 ISelDAG->getNode(ISD::SRL, MVT::i32, Q, ISelDAG->getConstant(31, MVT::i32));
Nate Begeman27b4c232005-04-06 06:44:57 +0000825 return ISelDAG->getNode(ISD::ADD, MVT::i32, Q, T);
Nate Begeman815d6da2005-04-06 00:25:27 +0000826}
827
828/// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
829/// return a DAG expression to select that will generate the same value by
830/// multiplying by a magic number. See:
831/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
832SDOperand ISel::BuildUDIVSequence(SDOperand N) {
833 unsigned d =
834 (unsigned)cast<ConstantSDNode>(N.getOperand(1))->getSignExtended();
835 mu magics = magicu(d);
836 // Multiply the numerator (operand 0) by the magic value
837 SDOperand Q = ISelDAG->getNode(ISD::MULHU, MVT::i32, N.getOperand(0),
838 ISelDAG->getConstant(magics.m, MVT::i32));
839 if (magics.a == 0) {
840 Q = ISelDAG->getNode(ISD::SRL, MVT::i32, Q,
841 ISelDAG->getConstant(magics.s, MVT::i32));
842 } else {
843 SDOperand NPQ = ISelDAG->getNode(ISD::SUB, MVT::i32, N.getOperand(0), Q);
844 NPQ = ISelDAG->getNode(ISD::SRL, MVT::i32, NPQ,
845 ISelDAG->getConstant(1, MVT::i32));
846 NPQ = ISelDAG->getNode(ISD::ADD, MVT::i32, NPQ, Q);
847 Q = ISelDAG->getNode(ISD::SRL, MVT::i32, NPQ,
848 ISelDAG->getConstant(magics.s-1, MVT::i32));
849 }
Nate Begeman27b4c232005-04-06 06:44:57 +0000850 return Q;
Nate Begemana9795f82005-03-24 04:41:43 +0000851}
852
Nate Begemanc7b09f12005-03-25 08:34:25 +0000853/// getGlobalBaseReg - Output the instructions required to put the
854/// base address to use for accessing globals into a register.
855///
856unsigned ISel::getGlobalBaseReg() {
857 if (!GlobalBaseInitialized) {
858 // Insert the set of GlobalBaseReg into the first MBB of the function
859 MachineBasicBlock &FirstMBB = BB->getParent()->front();
860 MachineBasicBlock::iterator MBBI = FirstMBB.begin();
861 GlobalBaseReg = MakeReg(MVT::i32);
862 BuildMI(FirstMBB, MBBI, PPC::MovePCtoLR, 0, PPC::LR);
863 BuildMI(FirstMBB, MBBI, PPC::MFLR, 1, GlobalBaseReg).addReg(PPC::LR);
864 GlobalBaseInitialized = true;
865 }
866 return GlobalBaseReg;
867}
868
Nate Begeman6b559972005-04-01 02:59:27 +0000869/// getConstDouble - Loads a floating point value into a register, via the
870/// Constant Pool. Optionally takes a register in which to load the value.
871unsigned ISel::getConstDouble(double doubleVal, unsigned Result=0) {
872 unsigned Tmp1 = MakeReg(MVT::i32);
873 if (0 == Result) Result = MakeReg(MVT::f64);
874 MachineConstantPool *CP = BB->getParent()->getConstantPool();
875 ConstantFP *CFP = ConstantFP::get(Type::DoubleTy, doubleVal);
876 unsigned CPI = CP->getConstantPoolIndex(CFP);
877 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp1).addReg(getGlobalBaseReg())
878 .addConstantPoolIndex(CPI);
879 BuildMI(BB, PPC::LFD, 2, Result).addConstantPoolIndex(CPI).addReg(Tmp1);
880 return Result;
881}
882
Nate Begeman7ddecb42005-04-06 23:51:40 +0000883/// SelectBitfieldInsert - turn an or of two masked values into
884/// the rotate left word immediate then mask insert (rlwimi) instruction.
885/// Returns true on success, false if the caller still needs to select OR.
886///
887/// Patterns matched:
888/// 1. or shl, and 5. or and, and
889/// 2. or and, shl 6. or shl, shr
890/// 3. or shr, and 7. or shr, shl
891/// 4. or and, shr
892bool ISel::SelectBitfieldInsert(SDOperand OR, unsigned Result) {
Nate Begemancd08e4c2005-04-09 20:09:12 +0000893 bool IsRotate = false;
Nate Begeman7ddecb42005-04-06 23:51:40 +0000894 unsigned TgtMask = 0xFFFFFFFF, InsMask = 0xFFFFFFFF, Amount = 0;
895 unsigned Op0Opc = OR.getOperand(0).getOpcode();
896 unsigned Op1Opc = OR.getOperand(1).getOpcode();
897
898 // Verify that we have the correct opcodes
899 if (ISD::SHL != Op0Opc && ISD::SRL != Op0Opc && ISD::AND != Op0Opc)
900 return false;
901 if (ISD::SHL != Op1Opc && ISD::SRL != Op1Opc && ISD::AND != Op1Opc)
902 return false;
903
904 // Generate Mask value for Target
905 if (ConstantSDNode *CN =
906 dyn_cast<ConstantSDNode>(OR.getOperand(0).getOperand(1).Val)) {
907 switch(Op0Opc) {
908 case ISD::SHL: TgtMask <<= (unsigned)CN->getValue(); break;
909 case ISD::SRL: TgtMask >>= (unsigned)CN->getValue(); break;
910 case ISD::AND: TgtMask &= (unsigned)CN->getValue(); break;
911 }
912 } else {
913 return false;
914 }
915
916 // Generate Mask value for Insert
917 if (ConstantSDNode *CN =
918 dyn_cast<ConstantSDNode>(OR.getOperand(1).getOperand(1).Val)) {
919 switch(Op1Opc) {
920 case ISD::SHL:
921 Amount = CN->getValue();
Nate Begemancd08e4c2005-04-09 20:09:12 +0000922 InsMask <<= Amount;
923 if (Op0Opc == ISD::SRL) IsRotate = true;
Nate Begeman7ddecb42005-04-06 23:51:40 +0000924 break;
925 case ISD::SRL:
926 Amount = CN->getValue();
927 InsMask >>= Amount;
928 Amount = 32-Amount;
Nate Begemancd08e4c2005-04-09 20:09:12 +0000929 if (Op0Opc == ISD::SHL) IsRotate = true;
Nate Begeman7ddecb42005-04-06 23:51:40 +0000930 break;
931 case ISD::AND:
932 InsMask &= (unsigned)CN->getValue();
933 break;
934 }
935 } else {
936 return false;
937 }
938
939 // Verify that the Target mask and Insert mask together form a full word mask
940 // and that the Insert mask is a run of set bits (which implies both are runs
941 // of set bits). Given that, Select the arguments and generate the rlwimi
942 // instruction.
943 unsigned MB, ME;
944 if (((TgtMask ^ InsMask) == 0xFFFFFFFF) && IsRunOfOnes(InsMask, MB, ME)) {
945 unsigned Tmp1, Tmp2;
Nate Begemancd08e4c2005-04-09 20:09:12 +0000946 // Check for rotlwi / rotrwi here, a special case of bitfield insert
947 // where both bitfield halves are sourced from the same value.
948 if (IsRotate &&
949 OR.getOperand(0).getOperand(0) == OR.getOperand(1).getOperand(0)) {
Nate Begemancd08e4c2005-04-09 20:09:12 +0000950 Tmp1 = SelectExpr(OR.getOperand(0).getOperand(0));
951 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(Amount)
952 .addImm(0).addImm(31);
953 return true;
954 }
Nate Begeman7ddecb42005-04-06 23:51:40 +0000955 if (Op0Opc == ISD::AND)
956 Tmp1 = SelectExpr(OR.getOperand(0).getOperand(0));
957 else
958 Tmp1 = SelectExpr(OR.getOperand(0));
959 Tmp2 = SelectExpr(OR.getOperand(1).getOperand(0));
960 BuildMI(BB, PPC::RLWIMI, 5, Result).addReg(Tmp1).addReg(Tmp2)
961 .addImm(Amount).addImm(MB).addImm(ME);
962 return true;
963 }
964 return false;
965}
966
Nate Begemandffcfcc2005-04-01 00:32:34 +0000967unsigned ISel::SelectSetCR0(SDOperand CC) {
968 unsigned Opc, Tmp1, Tmp2;
Nate Begeman9765c252005-04-12 21:22:28 +0000969 bool AlreadySelected = false;
Nate Begemandffcfcc2005-04-01 00:32:34 +0000970 static const unsigned CompareOpcodes[] =
971 { PPC::FCMPU, PPC::FCMPU, PPC::CMPW, PPC::CMPLW };
972
973 // If the first operand to the select is a SETCC node, then we can fold it
974 // into the branch that selects which value to return.
975 SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(CC.Val);
976 if (SetCC && CC.getOpcode() == ISD::SETCC) {
977 bool U;
978 Opc = getBCCForSetCC(SetCC->getCondition(), U);
Nate Begemandffcfcc2005-04-01 00:32:34 +0000979
Nate Begeman439b4442005-04-05 04:22:58 +0000980 // Pass the optional argument U to getImmediateForOpcode for SETCC,
Nate Begemandffcfcc2005-04-01 00:32:34 +0000981 // so that it knows whether the SETCC immediate range is signed or not.
Nate Begeman439b4442005-04-05 04:22:58 +0000982 if (1 == getImmediateForOpcode(SetCC->getOperand(1), ISD::SETCC,
983 Tmp2, U)) {
Nate Begemanc7bd4822005-04-11 06:34:10 +0000984 // For comparisons against zero, we can implicity set CR0 if a recording
985 // variant (e.g. 'or.' instead of 'or') of the instruction that defines
986 // operand zero of the SetCC node is available.
987 if (0 == Tmp2 &&
Nate Begeman9765c252005-04-12 21:22:28 +0000988 NodeHasRecordingVariant(SetCC->getOperand(0).getOpcode()) &&
989 SetCC->getOperand(0).Val->hasOneUse()) {
Nate Begemanc7bd4822005-04-11 06:34:10 +0000990 RecordSuccess = false;
991 Tmp1 = SelectExpr(SetCC->getOperand(0), true);
992 if (RecordSuccess) {
993 ++Recorded;
994 return Opc;
995 }
996 AlreadySelected = true;
997 }
998 // If we could not implicitly set CR0, then emit a compare immediate
999 // instead.
1000 if (!AlreadySelected) Tmp1 = SelectExpr(SetCC->getOperand(0));
Nate Begemandffcfcc2005-04-01 00:32:34 +00001001 if (U)
1002 BuildMI(BB, PPC::CMPLWI, 2, PPC::CR0).addReg(Tmp1).addImm(Tmp2);
1003 else
1004 BuildMI(BB, PPC::CMPWI, 2, PPC::CR0).addReg(Tmp1).addSImm(Tmp2);
1005 } else {
1006 bool IsInteger = MVT::isInteger(SetCC->getOperand(0).getValueType());
1007 unsigned CompareOpc = CompareOpcodes[2 * IsInteger + U];
Nate Begemanc7bd4822005-04-11 06:34:10 +00001008 Tmp1 = SelectExpr(SetCC->getOperand(0));
Nate Begemandffcfcc2005-04-01 00:32:34 +00001009 Tmp2 = SelectExpr(SetCC->getOperand(1));
1010 BuildMI(BB, CompareOpc, 2, PPC::CR0).addReg(Tmp1).addReg(Tmp2);
1011 }
1012 } else {
Nate Begeman9765c252005-04-12 21:22:28 +00001013 Opc = PPC::BNE;
Nate Begemandffcfcc2005-04-01 00:32:34 +00001014 Tmp1 = SelectExpr(CC);
1015 BuildMI(BB, PPC::CMPLWI, 2, PPC::CR0).addReg(Tmp1).addImm(0);
Nate Begemandffcfcc2005-04-01 00:32:34 +00001016 }
1017 return Opc;
1018}
1019
1020/// Check to see if the load is a constant offset from a base register
Nate Begeman04730362005-04-01 04:45:11 +00001021bool ISel::SelectAddr(SDOperand N, unsigned& Reg, int& offset)
Nate Begemana9795f82005-03-24 04:41:43 +00001022{
Nate Begeman96fc6812005-03-31 02:05:53 +00001023 unsigned imm = 0, opcode = N.getOpcode();
Nate Begeman04730362005-04-01 04:45:11 +00001024 if (N.getOpcode() == ISD::ADD) {
1025 Reg = SelectExpr(N.getOperand(0));
Nate Begeman439b4442005-04-05 04:22:58 +00001026 if (1 == getImmediateForOpcode(N.getOperand(1), opcode, imm)) {
Nate Begeman96fc6812005-03-31 02:05:53 +00001027 offset = imm;
Nate Begeman04730362005-04-01 04:45:11 +00001028 return false;
1029 }
1030 offset = SelectExpr(N.getOperand(1));
1031 return true;
1032 }
Nate Begemana9795f82005-03-24 04:41:43 +00001033 Reg = SelectExpr(N);
1034 offset = 0;
Nate Begeman04730362005-04-01 04:45:11 +00001035 return false;
Nate Begemana9795f82005-03-24 04:41:43 +00001036}
1037
1038void ISel::SelectBranchCC(SDOperand N)
1039{
Nate Begemana9795f82005-03-24 04:41:43 +00001040 MachineBasicBlock *Dest =
1041 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
Nate Begeman3e897162005-03-31 23:55:40 +00001042
Nate Begemana9795f82005-03-24 04:41:43 +00001043 Select(N.getOperand(0)); //chain
Nate Begemandffcfcc2005-04-01 00:32:34 +00001044 unsigned Opc = SelectSetCR0(N.getOperand(1));
Nate Begemancd08e4c2005-04-09 20:09:12 +00001045
1046 // Iterate to the next basic block, unless we're already at the end of the
1047 ilist<MachineBasicBlock>::iterator It = BB, E = BB->getParent()->end();
Nate Begeman706471e2005-04-09 23:35:05 +00001048 if (++It == E) It = BB;
Nate Begemancd08e4c2005-04-09 20:09:12 +00001049
1050 // If this is a two way branch, then grab the fallthrough basic block argument
1051 // and build a PowerPC branch pseudo-op, suitable for long branch conversion
1052 // if necessary by the branch selection pass. Otherwise, emit a standard
1053 // conditional branch.
1054 if (N.getOpcode() == ISD::BRCONDTWOWAY) {
1055 MachineBasicBlock *Fallthrough =
1056 cast<BasicBlockSDNode>(N.getOperand(3))->getBasicBlock();
1057 if (Dest != It) {
1058 BuildMI(BB, PPC::COND_BRANCH, 4).addReg(PPC::CR0).addImm(Opc)
1059 .addMBB(Dest).addMBB(Fallthrough);
1060 if (Fallthrough != It)
1061 BuildMI(BB, PPC::B, 1).addMBB(Fallthrough);
1062 } else {
1063 if (Fallthrough != It) {
1064 Opc = PPC32InstrInfo::invertPPCBranchOpcode(Opc);
1065 BuildMI(BB, PPC::COND_BRANCH, 4).addReg(PPC::CR0).addImm(Opc)
1066 .addMBB(Fallthrough).addMBB(Dest);
1067 }
1068 }
1069 } else {
Nate Begeman27499e32005-04-10 01:48:29 +00001070 BuildMI(BB, PPC::COND_BRANCH, 4).addReg(PPC::CR0).addImm(Opc)
1071 .addMBB(Dest).addMBB(It);
1072 //BuildMI(BB, Opc, 2).addReg(PPC::CR0).addMBB(Dest);
Nate Begemancd08e4c2005-04-09 20:09:12 +00001073 }
Nate Begemana9795f82005-03-24 04:41:43 +00001074 return;
1075}
1076
1077unsigned ISel::SelectExprFP(SDOperand N, unsigned Result)
1078{
1079 unsigned Tmp1, Tmp2, Tmp3;
1080 unsigned Opc = 0;
1081 SDNode *Node = N.Val;
1082 MVT::ValueType DestType = N.getValueType();
1083 unsigned opcode = N.getOpcode();
1084
1085 switch (opcode) {
1086 default:
1087 Node->dump();
1088 assert(0 && "Node not handled!\n");
1089
Nate Begeman23afcfb2005-03-29 22:48:55 +00001090 case ISD::SELECT: {
Nate Begeman3e897162005-03-31 23:55:40 +00001091 // Attempt to generate FSEL. We can do this whenever we have an FP result,
1092 // and an FP comparison in the SetCC node.
1093 SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(N.getOperand(0).Val);
1094 if (SetCC && N.getOperand(0).getOpcode() == ISD::SETCC &&
1095 !MVT::isInteger(SetCC->getOperand(0).getValueType()) &&
1096 SetCC->getCondition() != ISD::SETEQ &&
1097 SetCC->getCondition() != ISD::SETNE) {
1098 MVT::ValueType VT = SetCC->getOperand(0).getValueType();
Nate Begeman3e897162005-03-31 23:55:40 +00001099 unsigned TV = SelectExpr(N.getOperand(1)); // Use if TRUE
1100 unsigned FV = SelectExpr(N.getOperand(2)); // Use if FALSE
1101
1102 ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(1));
1103 if (CN && (CN->isExactlyValue(-0.0) || CN->isExactlyValue(0.0))) {
1104 switch(SetCC->getCondition()) {
1105 default: assert(0 && "Invalid FSEL condition"); abort();
1106 case ISD::SETULT:
1107 case ISD::SETLT:
Nate Begemanaf4ab1b2005-04-09 09:33:07 +00001108 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
Nate Begeman3e897162005-03-31 23:55:40 +00001109 case ISD::SETUGE:
1110 case ISD::SETGE:
Nate Begemanaf4ab1b2005-04-09 09:33:07 +00001111 Tmp1 = SelectExpr(SetCC->getOperand(0)); // Val to compare against
Nate Begeman3e897162005-03-31 23:55:40 +00001112 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp1).addReg(TV).addReg(FV);
1113 return Result;
1114 case ISD::SETUGT:
Nate Begemanaf4ab1b2005-04-09 09:33:07 +00001115 case ISD::SETGT:
1116 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
Nate Begeman3e897162005-03-31 23:55:40 +00001117 case ISD::SETULE:
1118 case ISD::SETLE: {
Nate Begemanaf4ab1b2005-04-09 09:33:07 +00001119 if (SetCC->getOperand(0).getOpcode() == ISD::FNEG) {
1120 Tmp2 = SelectExpr(SetCC->getOperand(0).getOperand(0));
1121 } else {
1122 Tmp2 = MakeReg(VT);
1123 Tmp1 = SelectExpr(SetCC->getOperand(0)); // Val to compare against
1124 BuildMI(BB, PPC::FNEG, 1, Tmp2).addReg(Tmp1);
1125 }
Nate Begeman3e897162005-03-31 23:55:40 +00001126 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp2).addReg(TV).addReg(FV);
1127 return Result;
1128 }
1129 }
1130 } else {
1131 Opc = (MVT::f64 == VT) ? PPC::FSUB : PPC::FSUBS;
Nate Begemanaf4ab1b2005-04-09 09:33:07 +00001132 Tmp1 = SelectExpr(SetCC->getOperand(0)); // Val to compare against
Nate Begeman3e897162005-03-31 23:55:40 +00001133 Tmp2 = SelectExpr(SetCC->getOperand(1));
1134 Tmp3 = MakeReg(VT);
1135 switch(SetCC->getCondition()) {
1136 default: assert(0 && "Invalid FSEL condition"); abort();
1137 case ISD::SETULT:
1138 case ISD::SETLT:
1139 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
1140 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(FV).addReg(TV);
1141 return Result;
1142 case ISD::SETUGE:
1143 case ISD::SETGE:
1144 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
1145 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(TV).addReg(FV);
1146 return Result;
1147 case ISD::SETUGT:
1148 case ISD::SETGT:
1149 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp2).addReg(Tmp1);
1150 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(FV).addReg(TV);
1151 return Result;
1152 case ISD::SETULE:
1153 case ISD::SETLE:
1154 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp2).addReg(Tmp1);
1155 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(TV).addReg(FV);
1156 return Result;
1157 }
1158 }
1159 assert(0 && "Should never get here");
1160 return 0;
1161 }
1162
Nate Begeman31318e42005-04-01 07:21:30 +00001163 unsigned TrueValue = SelectExpr(N.getOperand(1)); //Use if TRUE
1164 unsigned FalseValue = SelectExpr(N.getOperand(2)); //Use if FALSE
Nate Begeman6cb2e1b2005-04-01 08:57:43 +00001165 Opc = SelectSetCR0(N.getOperand(0));
Nate Begeman31318e42005-04-01 07:21:30 +00001166
Nate Begeman23afcfb2005-03-29 22:48:55 +00001167 // Create an iterator with which to insert the MBB for copying the false
1168 // value and the MBB to hold the PHI instruction for this SetCC.
1169 MachineBasicBlock *thisMBB = BB;
1170 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1171 ilist<MachineBasicBlock>::iterator It = BB;
1172 ++It;
1173
1174 // thisMBB:
1175 // ...
1176 // TrueVal = ...
1177 // cmpTY cr0, r1, r2
1178 // bCC copy1MBB
1179 // fallthrough --> copy0MBB
Nate Begeman23afcfb2005-03-29 22:48:55 +00001180 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
1181 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
Nate Begeman6cb2e1b2005-04-01 08:57:43 +00001182 BuildMI(BB, Opc, 2).addReg(PPC::CR0).addMBB(sinkMBB);
Nate Begeman23afcfb2005-03-29 22:48:55 +00001183 MachineFunction *F = BB->getParent();
1184 F->getBasicBlockList().insert(It, copy0MBB);
1185 F->getBasicBlockList().insert(It, sinkMBB);
1186 // Update machine-CFG edges
1187 BB->addSuccessor(copy0MBB);
1188 BB->addSuccessor(sinkMBB);
1189
1190 // copy0MBB:
1191 // %FalseValue = ...
1192 // # fallthrough to sinkMBB
1193 BB = copy0MBB;
Nate Begeman23afcfb2005-03-29 22:48:55 +00001194 // Update machine-CFG edges
1195 BB->addSuccessor(sinkMBB);
1196
1197 // sinkMBB:
1198 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1199 // ...
1200 BB = sinkMBB;
1201 BuildMI(BB, PPC::PHI, 4, Result).addReg(FalseValue)
1202 .addMBB(copy0MBB).addReg(TrueValue).addMBB(thisMBB);
1203 return Result;
1204 }
Nate Begeman27eeb002005-04-02 05:59:34 +00001205
1206 case ISD::FNEG:
Nate Begeman93075ec2005-04-04 23:40:36 +00001207 if (!NoExcessFPPrecision &&
1208 ISD::ADD == N.getOperand(0).getOpcode() &&
1209 N.getOperand(0).Val->hasOneUse() &&
1210 ISD::MUL == N.getOperand(0).getOperand(0).getOpcode() &&
1211 N.getOperand(0).getOperand(0).Val->hasOneUse()) {
Nate Begeman80196b12005-04-05 00:15:08 +00001212 ++FusedFP; // Statistic
Nate Begeman93075ec2005-04-04 23:40:36 +00001213 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(0));
1214 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(1));
1215 Tmp3 = SelectExpr(N.getOperand(0).getOperand(1));
1216 Opc = DestType == MVT::f64 ? PPC::FNMADD : PPC::FNMADDS;
1217 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1218 } else if (!NoExcessFPPrecision &&
Nate Begemane88aa5b2005-04-09 03:05:51 +00001219 ISD::ADD == N.getOperand(0).getOpcode() &&
Nate Begeman93075ec2005-04-04 23:40:36 +00001220 N.getOperand(0).Val->hasOneUse() &&
Nate Begemane88aa5b2005-04-09 03:05:51 +00001221 ISD::MUL == N.getOperand(0).getOperand(1).getOpcode() &&
1222 N.getOperand(0).getOperand(1).Val->hasOneUse()) {
Nate Begeman80196b12005-04-05 00:15:08 +00001223 ++FusedFP; // Statistic
Nate Begemane88aa5b2005-04-09 03:05:51 +00001224 Tmp1 = SelectExpr(N.getOperand(0).getOperand(1).getOperand(0));
1225 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1).getOperand(1));
1226 Tmp3 = SelectExpr(N.getOperand(0).getOperand(0));
1227 Opc = DestType == MVT::f64 ? PPC::FNMADD : PPC::FNMADDS;
Nate Begeman93075ec2005-04-04 23:40:36 +00001228 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1229 } else if (ISD::FABS == N.getOperand(0).getOpcode()) {
Nate Begeman27eeb002005-04-02 05:59:34 +00001230 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1231 BuildMI(BB, PPC::FNABS, 1, Result).addReg(Tmp1);
1232 } else {
1233 Tmp1 = SelectExpr(N.getOperand(0));
1234 BuildMI(BB, PPC::FNEG, 1, Result).addReg(Tmp1);
1235 }
1236 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001237
Nate Begeman27eeb002005-04-02 05:59:34 +00001238 case ISD::FABS:
1239 Tmp1 = SelectExpr(N.getOperand(0));
1240 BuildMI(BB, PPC::FABS, 1, Result).addReg(Tmp1);
1241 return Result;
1242
Nate Begemana9795f82005-03-24 04:41:43 +00001243 case ISD::FP_ROUND:
1244 assert (DestType == MVT::f32 &&
1245 N.getOperand(0).getValueType() == MVT::f64 &&
1246 "only f64 to f32 conversion supported here");
1247 Tmp1 = SelectExpr(N.getOperand(0));
1248 BuildMI(BB, PPC::FRSP, 1, Result).addReg(Tmp1);
1249 return Result;
1250
1251 case ISD::FP_EXTEND:
1252 assert (DestType == MVT::f64 &&
1253 N.getOperand(0).getValueType() == MVT::f32 &&
1254 "only f32 to f64 conversion supported here");
1255 Tmp1 = SelectExpr(N.getOperand(0));
1256 BuildMI(BB, PPC::FMR, 1, Result).addReg(Tmp1);
1257 return Result;
1258
1259 case ISD::CopyFromReg:
Nate Begemanf2622612005-03-26 02:17:46 +00001260 if (Result == 1)
1261 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1262 Tmp1 = dyn_cast<RegSDNode>(Node)->getReg();
1263 BuildMI(BB, PPC::FMR, 1, Result).addReg(Tmp1);
1264 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001265
Nate Begeman6d369cc2005-04-01 01:08:07 +00001266 case ISD::ConstantFP: {
Nate Begeman6d369cc2005-04-01 01:08:07 +00001267 ConstantFPSDNode *CN = cast<ConstantFPSDNode>(N);
Nate Begeman6b559972005-04-01 02:59:27 +00001268 Result = getConstDouble(CN->getValue(), Result);
Nate Begeman6d369cc2005-04-01 01:08:07 +00001269 return Result;
1270 }
Nate Begemana9795f82005-03-24 04:41:43 +00001271
Nate Begemana9795f82005-03-24 04:41:43 +00001272 case ISD::ADD:
Nate Begeman93075ec2005-04-04 23:40:36 +00001273 if (!NoExcessFPPrecision && N.getOperand(0).getOpcode() == ISD::MUL &&
1274 N.getOperand(0).Val->hasOneUse()) {
1275 ++FusedFP; // Statistic
1276 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1277 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1278 Tmp3 = SelectExpr(N.getOperand(1));
1279 Opc = DestType == MVT::f64 ? PPC::FMADD : PPC::FMADDS;
1280 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1281 return Result;
1282 }
Nate Begemane88aa5b2005-04-09 03:05:51 +00001283 if (!NoExcessFPPrecision && N.getOperand(1).getOpcode() == ISD::MUL &&
1284 N.getOperand(1).Val->hasOneUse()) {
1285 ++FusedFP; // Statistic
1286 Tmp1 = SelectExpr(N.getOperand(1).getOperand(0));
1287 Tmp2 = SelectExpr(N.getOperand(1).getOperand(1));
1288 Tmp3 = SelectExpr(N.getOperand(0));
1289 Opc = DestType == MVT::f64 ? PPC::FMADD : PPC::FMADDS;
1290 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1291 return Result;
1292 }
Nate Begeman93075ec2005-04-04 23:40:36 +00001293 Opc = DestType == MVT::f64 ? PPC::FADD : PPC::FADDS;
1294 Tmp1 = SelectExpr(N.getOperand(0));
1295 Tmp2 = SelectExpr(N.getOperand(1));
1296 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1297 return Result;
1298
Nate Begemana9795f82005-03-24 04:41:43 +00001299 case ISD::SUB:
Nate Begeman93075ec2005-04-04 23:40:36 +00001300 if (!NoExcessFPPrecision && N.getOperand(0).getOpcode() == ISD::MUL &&
1301 N.getOperand(0).Val->hasOneUse()) {
1302 ++FusedFP; // Statistic
1303 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1304 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1305 Tmp3 = SelectExpr(N.getOperand(1));
1306 Opc = DestType == MVT::f64 ? PPC::FMSUB : PPC::FMSUBS;
1307 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1308 return Result;
1309 }
Nate Begemane88aa5b2005-04-09 03:05:51 +00001310 if (!NoExcessFPPrecision && N.getOperand(1).getOpcode() == ISD::MUL &&
1311 N.getOperand(1).Val->hasOneUse()) {
1312 ++FusedFP; // Statistic
1313 Tmp1 = SelectExpr(N.getOperand(1).getOperand(0));
1314 Tmp2 = SelectExpr(N.getOperand(1).getOperand(1));
1315 Tmp3 = SelectExpr(N.getOperand(0));
1316 Opc = DestType == MVT::f64 ? PPC::FNMSUB : PPC::FNMSUBS;
1317 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1318 return Result;
1319 }
Nate Begeman93075ec2005-04-04 23:40:36 +00001320 Opc = DestType == MVT::f64 ? PPC::FSUB : PPC::FSUBS;
1321 Tmp1 = SelectExpr(N.getOperand(0));
1322 Tmp2 = SelectExpr(N.getOperand(1));
1323 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1324 return Result;
1325
1326 case ISD::MUL:
Nate Begemana9795f82005-03-24 04:41:43 +00001327 case ISD::SDIV:
1328 switch( opcode ) {
1329 case ISD::MUL: Opc = DestType == MVT::f64 ? PPC::FMUL : PPC::FMULS; break;
Nate Begemana9795f82005-03-24 04:41:43 +00001330 case ISD::SDIV: Opc = DestType == MVT::f64 ? PPC::FDIV : PPC::FDIVS; break;
1331 };
Nate Begemana9795f82005-03-24 04:41:43 +00001332 Tmp1 = SelectExpr(N.getOperand(0));
1333 Tmp2 = SelectExpr(N.getOperand(1));
1334 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1335 return Result;
1336
Nate Begemana9795f82005-03-24 04:41:43 +00001337 case ISD::UINT_TO_FP:
Nate Begemanfdcf3412005-03-30 19:38:35 +00001338 case ISD::SINT_TO_FP: {
1339 assert (N.getOperand(0).getValueType() == MVT::i32
1340 && "int to float must operate on i32");
1341 bool IsUnsigned = (ISD::UINT_TO_FP == opcode);
1342 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
1343 Tmp2 = MakeReg(MVT::f64); // temp reg to load the integer value into
1344 Tmp3 = MakeReg(MVT::i32); // temp reg to hold the conversion constant
Nate Begemanfdcf3412005-03-30 19:38:35 +00001345
1346 int FrameIdx = BB->getParent()->getFrameInfo()->CreateStackObject(8, 8);
1347 MachineConstantPool *CP = BB->getParent()->getConstantPool();
1348
Nate Begemanfdcf3412005-03-30 19:38:35 +00001349 if (IsUnsigned) {
Nate Begeman709c8062005-04-10 06:06:10 +00001350 unsigned ConstF = getConstDouble(0x1.000000p52);
Nate Begemanfdcf3412005-03-30 19:38:35 +00001351 // Store the hi & low halves of the fp value, currently in int regs
1352 BuildMI(BB, PPC::LIS, 1, Tmp3).addSImm(0x4330);
1353 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(Tmp3), FrameIdx);
1354 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(Tmp1), FrameIdx, 4);
1355 addFrameReference(BuildMI(BB, PPC::LFD, 2, Tmp2), FrameIdx);
1356 // Generate the return value with a subtract
1357 BuildMI(BB, PPC::FSUB, 2, Result).addReg(Tmp2).addReg(ConstF);
1358 } else {
Nate Begeman709c8062005-04-10 06:06:10 +00001359 unsigned ConstF = getConstDouble(0x1.000008p52);
Nate Begemanfdcf3412005-03-30 19:38:35 +00001360 unsigned TmpL = MakeReg(MVT::i32);
Nate Begemanfdcf3412005-03-30 19:38:35 +00001361 // Store the hi & low halves of the fp value, currently in int regs
1362 BuildMI(BB, PPC::LIS, 1, Tmp3).addSImm(0x4330);
1363 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(Tmp3), FrameIdx);
1364 BuildMI(BB, PPC::XORIS, 2, TmpL).addReg(Tmp1).addImm(0x8000);
1365 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(TmpL), FrameIdx, 4);
1366 addFrameReference(BuildMI(BB, PPC::LFD, 2, Tmp2), FrameIdx);
1367 // Generate the return value with a subtract
1368 BuildMI(BB, PPC::FSUB, 2, Result).addReg(Tmp2).addReg(ConstF);
1369 }
1370 return Result;
1371 }
Nate Begemana9795f82005-03-24 04:41:43 +00001372 }
Nate Begeman6b559972005-04-01 02:59:27 +00001373 assert(0 && "Should never get here");
Nate Begemana9795f82005-03-24 04:41:43 +00001374 return 0;
1375}
1376
Nate Begemanc7bd4822005-04-11 06:34:10 +00001377unsigned ISel::SelectExpr(SDOperand N, bool Recording) {
Nate Begemana9795f82005-03-24 04:41:43 +00001378 unsigned Result;
1379 unsigned Tmp1, Tmp2, Tmp3;
1380 unsigned Opc = 0;
1381 unsigned opcode = N.getOpcode();
1382
1383 SDNode *Node = N.Val;
1384 MVT::ValueType DestType = N.getValueType();
1385
1386 unsigned &Reg = ExprMap[N];
1387 if (Reg) return Reg;
1388
Nate Begeman27eeb002005-04-02 05:59:34 +00001389 switch (N.getOpcode()) {
1390 default:
Nate Begemana9795f82005-03-24 04:41:43 +00001391 Reg = Result = (N.getValueType() != MVT::Other) ?
Nate Begeman27eeb002005-04-02 05:59:34 +00001392 MakeReg(N.getValueType()) : 1;
1393 break;
1394 case ISD::CALL:
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001395 // If this is a call instruction, make sure to prepare ALL of the result
1396 // values as well as the chain.
Nate Begeman27eeb002005-04-02 05:59:34 +00001397 if (Node->getNumValues() == 1)
1398 Reg = Result = 1; // Void call, just a chain.
1399 else {
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001400 Result = MakeReg(Node->getValueType(0));
1401 ExprMap[N.getValue(0)] = Result;
Nate Begeman27eeb002005-04-02 05:59:34 +00001402 for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001403 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
Nate Begeman27eeb002005-04-02 05:59:34 +00001404 ExprMap[SDOperand(Node, Node->getNumValues()-1)] = 1;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001405 }
Nate Begeman27eeb002005-04-02 05:59:34 +00001406 break;
1407 case ISD::ADD_PARTS:
1408 case ISD::SUB_PARTS:
1409 case ISD::SHL_PARTS:
1410 case ISD::SRL_PARTS:
1411 case ISD::SRA_PARTS:
1412 Result = MakeReg(Node->getValueType(0));
1413 ExprMap[N.getValue(0)] = Result;
1414 for (unsigned i = 1, e = N.Val->getNumValues(); i != e; ++i)
1415 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
1416 break;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001417 }
1418
Nate Begemane5846682005-04-04 06:52:38 +00001419 if (ISD::CopyFromReg == opcode)
1420 DestType = N.getValue(0).getValueType();
1421
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001422 if (DestType == MVT::f64 || DestType == MVT::f32)
Nate Begemana0e3e942005-04-10 01:14:13 +00001423 if (ISD::LOAD != opcode && ISD::EXTLOAD != opcode &&
1424 ISD::UNDEF != opcode && ISD::CALL != opcode)
Nate Begeman74d73452005-03-31 00:15:26 +00001425 return SelectExprFP(N, Result);
Nate Begemana9795f82005-03-24 04:41:43 +00001426
1427 switch (opcode) {
1428 default:
1429 Node->dump();
1430 assert(0 && "Node not handled!\n");
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001431 case ISD::UNDEF:
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001432 BuildMI(BB, PPC::IMPLICIT_DEF, 0, Result);
1433 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001434 case ISD::DYNAMIC_STACKALLOC:
Nate Begeman5e966612005-03-24 06:28:42 +00001435 // Generate both result values. FIXME: Need a better commment here?
1436 if (Result != 1)
1437 ExprMap[N.getValue(1)] = 1;
1438 else
1439 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1440
1441 // FIXME: We are currently ignoring the requested alignment for handling
1442 // greater than the stack alignment. This will need to be revisited at some
1443 // point. Align = N.getOperand(2);
1444 if (!isa<ConstantSDNode>(N.getOperand(2)) ||
1445 cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) {
1446 std::cerr << "Cannot allocate stack object with greater alignment than"
1447 << " the stack alignment yet!";
1448 abort();
1449 }
1450 Select(N.getOperand(0));
1451 Tmp1 = SelectExpr(N.getOperand(1));
1452 // Subtract size from stack pointer, thereby allocating some space.
1453 BuildMI(BB, PPC::SUBF, 2, PPC::R1).addReg(Tmp1).addReg(PPC::R1);
1454 // Put a pointer to the space into the result register by copying the SP
1455 BuildMI(BB, PPC::OR, 2, Result).addReg(PPC::R1).addReg(PPC::R1);
1456 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001457
1458 case ISD::ConstantPool:
Nate Begemanca12a2b2005-03-28 22:28:37 +00001459 Tmp1 = cast<ConstantPoolSDNode>(N)->getIndex();
1460 Tmp2 = MakeReg(MVT::i32);
1461 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp2).addReg(getGlobalBaseReg())
1462 .addConstantPoolIndex(Tmp1);
1463 BuildMI(BB, PPC::LA, 2, Result).addReg(Tmp2).addConstantPoolIndex(Tmp1);
1464 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001465
1466 case ISD::FrameIndex:
Nate Begemanf3d08f32005-03-29 00:03:27 +00001467 Tmp1 = cast<FrameIndexSDNode>(N)->getIndex();
Nate Begeman58f718c2005-03-30 02:23:08 +00001468 addFrameReference(BuildMI(BB, PPC::ADDI, 2, Result), (int)Tmp1, 0, false);
Nate Begemanf3d08f32005-03-29 00:03:27 +00001469 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001470
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001471 case ISD::GlobalAddress: {
1472 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
Nate Begemanca12a2b2005-03-28 22:28:37 +00001473 Tmp1 = MakeReg(MVT::i32);
Nate Begemanc7b09f12005-03-25 08:34:25 +00001474 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp1).addReg(getGlobalBaseReg())
1475 .addGlobalAddress(GV);
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001476 if (GV->hasWeakLinkage() || GV->isExternal()) {
1477 BuildMI(BB, PPC::LWZ, 2, Result).addGlobalAddress(GV).addReg(Tmp1);
1478 } else {
1479 BuildMI(BB, PPC::LA, 2, Result).addReg(Tmp1).addGlobalAddress(GV);
1480 }
1481 return Result;
1482 }
1483
Nate Begeman5e966612005-03-24 06:28:42 +00001484 case ISD::LOAD:
Nate Begemana9795f82005-03-24 04:41:43 +00001485 case ISD::EXTLOAD:
1486 case ISD::ZEXTLOAD:
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001487 case ISD::SEXTLOAD: {
Nate Begeman9db505c2005-03-28 19:36:43 +00001488 MVT::ValueType TypeBeingLoaded = (ISD::LOAD == opcode) ?
1489 Node->getValueType(0) : cast<MVTSDNode>(Node)->getExtraValueType();
Nate Begeman74d73452005-03-31 00:15:26 +00001490 bool sext = (ISD::SEXTLOAD == opcode);
Nate Begeman74d73452005-03-31 00:15:26 +00001491
Nate Begeman5e966612005-03-24 06:28:42 +00001492 // Make sure we generate both values.
1493 if (Result != 1)
1494 ExprMap[N.getValue(1)] = 1; // Generate the token
1495 else
1496 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1497
1498 SDOperand Chain = N.getOperand(0);
1499 SDOperand Address = N.getOperand(1);
1500 Select(Chain);
1501
Nate Begeman9db505c2005-03-28 19:36:43 +00001502 switch (TypeBeingLoaded) {
Nate Begeman74d73452005-03-31 00:15:26 +00001503 default: Node->dump(); assert(0 && "Cannot load this type!");
Nate Begeman9db505c2005-03-28 19:36:43 +00001504 case MVT::i1: Opc = PPC::LBZ; break;
1505 case MVT::i8: Opc = PPC::LBZ; break;
1506 case MVT::i16: Opc = sext ? PPC::LHA : PPC::LHZ; break;
1507 case MVT::i32: Opc = PPC::LWZ; break;
Nate Begeman74d73452005-03-31 00:15:26 +00001508 case MVT::f32: Opc = PPC::LFS; break;
1509 case MVT::f64: Opc = PPC::LFD; break;
Nate Begeman5e966612005-03-24 06:28:42 +00001510 }
1511
Nate Begeman74d73452005-03-31 00:15:26 +00001512 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Address)) {
1513 Tmp1 = MakeReg(MVT::i32);
1514 int CPI = CP->getIndex();
1515 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp1).addReg(getGlobalBaseReg())
1516 .addConstantPoolIndex(CPI);
1517 BuildMI(BB, Opc, 2, Result).addConstantPoolIndex(CPI).addReg(Tmp1);
Nate Begeman9db505c2005-03-28 19:36:43 +00001518 }
Nate Begeman74d73452005-03-31 00:15:26 +00001519 else if(Address.getOpcode() == ISD::FrameIndex) {
Nate Begeman58f718c2005-03-30 02:23:08 +00001520 Tmp1 = cast<FrameIndexSDNode>(Address)->getIndex();
1521 addFrameReference(BuildMI(BB, Opc, 2, Result), (int)Tmp1);
Nate Begeman5e966612005-03-24 06:28:42 +00001522 } else {
1523 int offset;
Nate Begeman04730362005-04-01 04:45:11 +00001524 bool idx = SelectAddr(Address, Tmp1, offset);
1525 if (idx) {
1526 Opc = IndexedOpForOp(Opc);
1527 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(offset);
1528 } else {
1529 BuildMI(BB, Opc, 2, Result).addSImm(offset).addReg(Tmp1);
1530 }
Nate Begeman5e966612005-03-24 06:28:42 +00001531 }
1532 return Result;
1533 }
1534
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001535 case ISD::CALL: {
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001536 unsigned GPR_idx = 0, FPR_idx = 0;
1537 static const unsigned GPR[] = {
1538 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
1539 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
1540 };
1541 static const unsigned FPR[] = {
1542 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
1543 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
1544 };
1545
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001546 // Lower the chain for this call.
1547 Select(N.getOperand(0));
1548 ExprMap[N.getValue(Node->getNumValues()-1)] = 1;
Nate Begeman74d73452005-03-31 00:15:26 +00001549
Nate Begemand860aa62005-04-04 22:17:48 +00001550 MachineInstr *CallMI;
1551 // Emit the correct call instruction based on the type of symbol called.
1552 if (GlobalAddressSDNode *GASD =
1553 dyn_cast<GlobalAddressSDNode>(N.getOperand(1))) {
1554 CallMI = BuildMI(PPC::CALLpcrel, 1).addGlobalAddress(GASD->getGlobal(),
1555 true);
1556 } else if (ExternalSymbolSDNode *ESSDN =
1557 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1))) {
1558 CallMI = BuildMI(PPC::CALLpcrel, 1).addExternalSymbol(ESSDN->getSymbol(),
1559 true);
1560 } else {
1561 Tmp1 = SelectExpr(N.getOperand(1));
1562 BuildMI(BB, PPC::OR, 2, PPC::R12).addReg(Tmp1).addReg(Tmp1);
1563 BuildMI(BB, PPC::MTCTR, 1).addReg(PPC::R12);
1564 CallMI = BuildMI(PPC::CALLindirect, 3).addImm(20).addImm(0)
1565 .addReg(PPC::R12);
1566 }
1567
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001568 // Load the register args to virtual regs
1569 std::vector<unsigned> ArgVR;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001570 for(int i = 2, e = Node->getNumOperands(); i < e; ++i)
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001571 ArgVR.push_back(SelectExpr(N.getOperand(i)));
1572
1573 // Copy the virtual registers into the appropriate argument register
1574 for(int i = 0, e = ArgVR.size(); i < e; ++i) {
1575 switch(N.getOperand(i+2).getValueType()) {
1576 default: Node->dump(); assert(0 && "Unknown value type for call");
1577 case MVT::i1:
1578 case MVT::i8:
1579 case MVT::i16:
1580 case MVT::i32:
1581 assert(GPR_idx < 8 && "Too many int args");
Nate Begemand860aa62005-04-04 22:17:48 +00001582 if (N.getOperand(i+2).getOpcode() != ISD::UNDEF) {
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001583 BuildMI(BB, PPC::OR,2,GPR[GPR_idx]).addReg(ArgVR[i]).addReg(ArgVR[i]);
Nate Begemand860aa62005-04-04 22:17:48 +00001584 CallMI->addRegOperand(GPR[GPR_idx], MachineOperand::Use);
1585 }
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001586 ++GPR_idx;
1587 break;
1588 case MVT::f64:
1589 case MVT::f32:
1590 assert(FPR_idx < 13 && "Too many fp args");
1591 BuildMI(BB, PPC::FMR, 1, FPR[FPR_idx]).addReg(ArgVR[i]);
Nate Begemand860aa62005-04-04 22:17:48 +00001592 CallMI->addRegOperand(FPR[FPR_idx], MachineOperand::Use);
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001593 ++FPR_idx;
1594 break;
1595 }
1596 }
Nate Begemand860aa62005-04-04 22:17:48 +00001597
1598 // Put the call instruction in the correct place in the MachineBasicBlock
1599 BB->push_back(CallMI);
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001600
1601 switch (Node->getValueType(0)) {
1602 default: assert(0 && "Unknown value type for call result!");
1603 case MVT::Other: return 1;
1604 case MVT::i1:
1605 case MVT::i8:
1606 case MVT::i16:
1607 case MVT::i32:
Nate Begemane5846682005-04-04 06:52:38 +00001608 if (Node->getValueType(1) == MVT::i32) {
1609 BuildMI(BB, PPC::OR, 2, Result+1).addReg(PPC::R3).addReg(PPC::R3);
1610 BuildMI(BB, PPC::OR, 2, Result).addReg(PPC::R4).addReg(PPC::R4);
1611 } else {
1612 BuildMI(BB, PPC::OR, 2, Result).addReg(PPC::R3).addReg(PPC::R3);
1613 }
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001614 break;
1615 case MVT::f32:
1616 case MVT::f64:
1617 BuildMI(BB, PPC::FMR, 1, Result).addReg(PPC::F1);
1618 break;
1619 }
1620 return Result+N.ResNo;
1621 }
Nate Begemana9795f82005-03-24 04:41:43 +00001622
1623 case ISD::SIGN_EXTEND:
1624 case ISD::SIGN_EXTEND_INREG:
1625 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman9db505c2005-03-28 19:36:43 +00001626 switch(cast<MVTSDNode>(Node)->getExtraValueType()) {
1627 default: Node->dump(); assert(0 && "Unhandled SIGN_EXTEND type"); break;
Nate Begemanc7bd4822005-04-11 06:34:10 +00001628 case MVT::i16:
Nate Begeman9db505c2005-03-28 19:36:43 +00001629 BuildMI(BB, PPC::EXTSH, 1, Result).addReg(Tmp1);
1630 break;
Nate Begemanc7bd4822005-04-11 06:34:10 +00001631 case MVT::i8:
Nate Begeman9db505c2005-03-28 19:36:43 +00001632 BuildMI(BB, PPC::EXTSB, 1, Result).addReg(Tmp1);
1633 break;
Nate Begeman74747862005-03-29 22:24:51 +00001634 case MVT::i1:
1635 BuildMI(BB, PPC::SUBFIC, 2, Result).addReg(Tmp1).addSImm(0);
1636 break;
Nate Begeman9db505c2005-03-28 19:36:43 +00001637 }
Nate Begemana9795f82005-03-24 04:41:43 +00001638 return Result;
1639
1640 case ISD::ZERO_EXTEND_INREG:
1641 Tmp1 = SelectExpr(N.getOperand(0));
1642 switch(cast<MVTSDNode>(Node)->getExtraValueType()) {
Nate Begeman9db505c2005-03-28 19:36:43 +00001643 default: Node->dump(); assert(0 && "Unhandled ZERO_EXTEND type"); break;
Nate Begemana9795f82005-03-24 04:41:43 +00001644 case MVT::i16: Tmp2 = 16; break;
1645 case MVT::i8: Tmp2 = 24; break;
1646 case MVT::i1: Tmp2 = 31; break;
1647 }
Nate Begeman9765c252005-04-12 21:22:28 +00001648 Opc = Recording ? PPC::RLWINMo : PPC::RLWINM;
1649 RecordSuccess = true;
1650 BuildMI(BB, Opc, 4, Result).addReg(Tmp1).addImm(0).addImm(Tmp2).addImm(31);
Nate Begemana9795f82005-03-24 04:41:43 +00001651 return Result;
1652
Nate Begemana9795f82005-03-24 04:41:43 +00001653 case ISD::CopyFromReg:
1654 if (Result == 1)
1655 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1656 Tmp1 = dyn_cast<RegSDNode>(Node)->getReg();
1657 BuildMI(BB, PPC::OR, 2, Result).addReg(Tmp1).addReg(Tmp1);
1658 return Result;
1659
1660 case ISD::SHL:
Nate Begeman5e966612005-03-24 06:28:42 +00001661 Tmp1 = SelectExpr(N.getOperand(0));
1662 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1663 Tmp2 = CN->getValue() & 0x1F;
Nate Begeman33162522005-03-29 21:54:38 +00001664 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(Tmp2).addImm(0)
Nate Begeman5e966612005-03-24 06:28:42 +00001665 .addImm(31-Tmp2);
1666 } else {
1667 Tmp2 = SelectExpr(N.getOperand(1));
1668 BuildMI(BB, PPC::SLW, 2, Result).addReg(Tmp1).addReg(Tmp2);
1669 }
1670 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001671
Nate Begeman5e966612005-03-24 06:28:42 +00001672 case ISD::SRL:
1673 Tmp1 = SelectExpr(N.getOperand(0));
1674 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1675 Tmp2 = CN->getValue() & 0x1F;
Nate Begeman33162522005-03-29 21:54:38 +00001676 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(32-Tmp2)
Nate Begeman5e966612005-03-24 06:28:42 +00001677 .addImm(Tmp2).addImm(31);
1678 } else {
1679 Tmp2 = SelectExpr(N.getOperand(1));
1680 BuildMI(BB, PPC::SRW, 2, Result).addReg(Tmp1).addReg(Tmp2);
1681 }
1682 return Result;
1683
1684 case ISD::SRA:
1685 Tmp1 = SelectExpr(N.getOperand(0));
1686 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1687 Tmp2 = CN->getValue() & 0x1F;
1688 BuildMI(BB, PPC::SRAWI, 2, Result).addReg(Tmp1).addImm(Tmp2);
1689 } else {
1690 Tmp2 = SelectExpr(N.getOperand(1));
1691 BuildMI(BB, PPC::SRAW, 2, Result).addReg(Tmp1).addReg(Tmp2);
1692 }
1693 return Result;
1694
Nate Begemana9795f82005-03-24 04:41:43 +00001695 case ISD::ADD:
1696 assert (DestType == MVT::i32 && "Only do arithmetic on i32s!");
1697 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman439b4442005-04-05 04:22:58 +00001698 switch(getImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
Nate Begemana9795f82005-03-24 04:41:43 +00001699 default: assert(0 && "unhandled result code");
1700 case 0: // No immediate
1701 Tmp2 = SelectExpr(N.getOperand(1));
1702 BuildMI(BB, PPC::ADD, 2, Result).addReg(Tmp1).addReg(Tmp2);
1703 break;
1704 case 1: // Low immediate
1705 BuildMI(BB, PPC::ADDI, 2, Result).addReg(Tmp1).addSImm(Tmp2);
1706 break;
1707 case 2: // Shifted immediate
1708 BuildMI(BB, PPC::ADDIS, 2, Result).addReg(Tmp1).addSImm(Tmp2);
1709 break;
1710 }
1711 return Result;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001712
Nate Begemana9795f82005-03-24 04:41:43 +00001713 case ISD::AND:
Nate Begeman7ddecb42005-04-06 23:51:40 +00001714 Tmp1 = SelectExpr(N.getOperand(0));
1715 // FIXME: should add check in getImmediateForOpcode to return a value
1716 // indicating the immediate is a run of set bits so we can emit a bitfield
1717 // clear with RLWINM instead.
1718 switch(getImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
1719 default: assert(0 && "unhandled result code");
1720 case 0: // No immediate
1721 Tmp2 = SelectExpr(N.getOperand(1));
Nate Begemanc7bd4822005-04-11 06:34:10 +00001722 Opc = Recording ? PPC::ANDo : PPC::AND;
1723 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
Nate Begeman7ddecb42005-04-06 23:51:40 +00001724 break;
1725 case 1: // Low immediate
1726 BuildMI(BB, PPC::ANDIo, 2, Result).addReg(Tmp1).addImm(Tmp2);
1727 break;
1728 case 2: // Shifted immediate
1729 BuildMI(BB, PPC::ANDISo, 2, Result).addReg(Tmp1).addImm(Tmp2);
1730 break;
Nate Begeman9f833d32005-04-12 00:10:02 +00001731 case 5: // Bitfield mask
1732 Opc = Recording ? PPC::RLWINMo : PPC::RLWINM;
1733 Tmp3 = Tmp2 >> 16; // MB
1734 Tmp2 &= 0xFFFF; // ME
1735 BuildMI(BB, Opc, 4, Result).addReg(Tmp1).addImm(0)
1736 .addImm(Tmp3).addImm(Tmp2);
1737 break;
Nate Begeman7ddecb42005-04-06 23:51:40 +00001738 }
Nate Begemanc7bd4822005-04-11 06:34:10 +00001739 RecordSuccess = true;
Nate Begeman7ddecb42005-04-06 23:51:40 +00001740 return Result;
1741
Nate Begemana9795f82005-03-24 04:41:43 +00001742 case ISD::OR:
Nate Begeman7ddecb42005-04-06 23:51:40 +00001743 if (SelectBitfieldInsert(N, Result))
1744 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001745 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman439b4442005-04-05 04:22:58 +00001746 switch(getImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
Nate Begemana9795f82005-03-24 04:41:43 +00001747 default: assert(0 && "unhandled result code");
1748 case 0: // No immediate
1749 Tmp2 = SelectExpr(N.getOperand(1));
Nate Begemanc7bd4822005-04-11 06:34:10 +00001750 Opc = Recording ? PPC::ORo : PPC::OR;
1751 RecordSuccess = true;
1752 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +00001753 break;
1754 case 1: // Low immediate
Nate Begeman7ddecb42005-04-06 23:51:40 +00001755 BuildMI(BB, PPC::ORI, 2, Result).addReg(Tmp1).addImm(Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +00001756 break;
1757 case 2: // Shifted immediate
Nate Begeman7ddecb42005-04-06 23:51:40 +00001758 BuildMI(BB, PPC::ORIS, 2, Result).addReg(Tmp1).addImm(Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +00001759 break;
1760 }
1761 return Result;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001762
Nate Begemanaa73a9f2005-04-03 11:20:20 +00001763 case ISD::XOR: {
1764 // Check for EQV: xor, (xor a, -1), b
1765 if (N.getOperand(0).getOpcode() == ISD::XOR &&
1766 N.getOperand(0).getOperand(1).getOpcode() == ISD::Constant &&
1767 cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->isAllOnesValue()) {
Nate Begemanaa73a9f2005-04-03 11:20:20 +00001768 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1769 Tmp2 = SelectExpr(N.getOperand(1));
1770 BuildMI(BB, PPC::EQV, 2, Result).addReg(Tmp1).addReg(Tmp2);
1771 return Result;
1772 }
1773 // Check for NOT, NOR, and NAND: xor (copy, or, and), -1
1774 if (N.getOperand(1).getOpcode() == ISD::Constant &&
1775 cast<ConstantSDNode>(N.getOperand(1))->isAllOnesValue()) {
Nate Begemanaa73a9f2005-04-03 11:20:20 +00001776 switch(N.getOperand(0).getOpcode()) {
1777 case ISD::OR:
1778 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1779 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1780 BuildMI(BB, PPC::NOR, 2, Result).addReg(Tmp1).addReg(Tmp2);
1781 break;
1782 case ISD::AND:
1783 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1784 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1785 BuildMI(BB, PPC::NAND, 2, Result).addReg(Tmp1).addReg(Tmp2);
1786 break;
1787 default:
1788 Tmp1 = SelectExpr(N.getOperand(0));
1789 BuildMI(BB, PPC::NOR, 2, Result).addReg(Tmp1).addReg(Tmp1);
1790 break;
1791 }
1792 return Result;
1793 }
1794 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman439b4442005-04-05 04:22:58 +00001795 switch(getImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
Nate Begemanaa73a9f2005-04-03 11:20:20 +00001796 default: assert(0 && "unhandled result code");
1797 case 0: // No immediate
1798 Tmp2 = SelectExpr(N.getOperand(1));
1799 BuildMI(BB, PPC::XOR, 2, Result).addReg(Tmp1).addReg(Tmp2);
1800 break;
1801 case 1: // Low immediate
1802 BuildMI(BB, PPC::XORI, 2, Result).addReg(Tmp1).addImm(Tmp2);
1803 break;
1804 case 2: // Shifted immediate
1805 BuildMI(BB, PPC::XORIS, 2, Result).addReg(Tmp1).addImm(Tmp2);
1806 break;
1807 }
1808 return Result;
1809 }
1810
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001811 case ISD::SUB:
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001812 Tmp2 = SelectExpr(N.getOperand(1));
Nate Begeman439b4442005-04-05 04:22:58 +00001813 if (1 == getImmediateForOpcode(N.getOperand(0), opcode, Tmp1))
Nate Begeman27523a12005-04-02 00:42:16 +00001814 BuildMI(BB, PPC::SUBFIC, 2, Result).addReg(Tmp2).addSImm(Tmp1);
1815 else {
1816 Tmp1 = SelectExpr(N.getOperand(0));
1817 BuildMI(BB, PPC::SUBF, 2, Result).addReg(Tmp2).addReg(Tmp1);
1818 }
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001819 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001820
Nate Begeman5e966612005-03-24 06:28:42 +00001821 case ISD::MUL:
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001822 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman439b4442005-04-05 04:22:58 +00001823 if (1 == getImmediateForOpcode(N.getOperand(1), opcode, Tmp2))
Nate Begeman307e7442005-03-26 01:28:53 +00001824 BuildMI(BB, PPC::MULLI, 2, Result).addReg(Tmp1).addSImm(Tmp2);
1825 else {
1826 Tmp2 = SelectExpr(N.getOperand(1));
1827 BuildMI(BB, PPC::MULLW, 2, Result).addReg(Tmp1).addReg(Tmp2);
1828 }
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001829 return Result;
1830
Nate Begeman815d6da2005-04-06 00:25:27 +00001831 case ISD::MULHS:
1832 case ISD::MULHU:
1833 Tmp1 = SelectExpr(N.getOperand(0));
1834 Tmp2 = SelectExpr(N.getOperand(1));
1835 Opc = (ISD::MULHU == opcode) ? PPC::MULHWU : PPC::MULHW;
1836 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1837 return Result;
1838
Nate Begemanf3d08f32005-03-29 00:03:27 +00001839 case ISD::SDIV:
1840 case ISD::UDIV:
Nate Begeman815d6da2005-04-06 00:25:27 +00001841 switch (getImmediateForOpcode(N.getOperand(1), opcode, Tmp3)) {
1842 default: break;
1843 // If this is an sdiv by a power of two, we can use an srawi/addze pair.
1844 case 3:
Nate Begeman80196b12005-04-05 00:15:08 +00001845 Tmp1 = MakeReg(MVT::i32);
1846 Tmp2 = SelectExpr(N.getOperand(0));
Nate Begeman9f833d32005-04-12 00:10:02 +00001847 if ((int)Tmp3 < 0) {
1848 unsigned Tmp4 = MakeReg(MVT::i32);
1849 BuildMI(BB, PPC::SRAWI, 2, Tmp1).addReg(Tmp2).addImm(-Tmp3);
1850 BuildMI(BB, PPC::ADDZE, 1, Tmp4).addReg(Tmp1);
1851 BuildMI(BB, PPC::NEG, 1, Result).addReg(Tmp4);
1852 } else {
1853 BuildMI(BB, PPC::SRAWI, 2, Tmp1).addReg(Tmp2).addImm(Tmp3);
1854 BuildMI(BB, PPC::ADDZE, 1, Result).addReg(Tmp1);
1855 }
Nate Begeman80196b12005-04-05 00:15:08 +00001856 return Result;
Nate Begeman815d6da2005-04-06 00:25:27 +00001857 // If this is a divide by constant, we can emit code using some magic
1858 // constants to implement it as a multiply instead.
Nate Begeman27b4c232005-04-06 06:44:57 +00001859 case 4:
1860 ExprMap.erase(N);
1861 if (opcode == ISD::SDIV)
1862 return SelectExpr(BuildSDIVSequence(N));
1863 else
1864 return SelectExpr(BuildUDIVSequence(N));
Nate Begeman80196b12005-04-05 00:15:08 +00001865 }
Nate Begemanf3d08f32005-03-29 00:03:27 +00001866 Tmp1 = SelectExpr(N.getOperand(0));
1867 Tmp2 = SelectExpr(N.getOperand(1));
1868 Opc = (ISD::UDIV == opcode) ? PPC::DIVWU : PPC::DIVW;
1869 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1870 return Result;
1871
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001872 case ISD::ADD_PARTS:
Nate Begemanca12a2b2005-03-28 22:28:37 +00001873 case ISD::SUB_PARTS: {
1874 assert(N.getNumOperands() == 4 && N.getValueType() == MVT::i32 &&
1875 "Not an i64 add/sub!");
1876 // Emit all of the operands.
1877 std::vector<unsigned> InVals;
1878 for (unsigned i = 0, e = N.getNumOperands(); i != e; ++i)
1879 InVals.push_back(SelectExpr(N.getOperand(i)));
1880 if (N.getOpcode() == ISD::ADD_PARTS) {
Nate Begeman27eeb002005-04-02 05:59:34 +00001881 BuildMI(BB, PPC::ADDC, 2, Result).addReg(InVals[0]).addReg(InVals[2]);
1882 BuildMI(BB, PPC::ADDE, 2, Result+1).addReg(InVals[1]).addReg(InVals[3]);
Nate Begemanca12a2b2005-03-28 22:28:37 +00001883 } else {
Nate Begeman27eeb002005-04-02 05:59:34 +00001884 BuildMI(BB, PPC::SUBFC, 2, Result).addReg(InVals[2]).addReg(InVals[0]);
1885 BuildMI(BB, PPC::SUBFE, 2, Result+1).addReg(InVals[3]).addReg(InVals[1]);
1886 }
1887 return Result+N.ResNo;
1888 }
1889
1890 case ISD::SHL_PARTS:
1891 case ISD::SRA_PARTS:
1892 case ISD::SRL_PARTS: {
1893 assert(N.getNumOperands() == 3 && N.getValueType() == MVT::i32 &&
1894 "Not an i64 shift!");
1895 unsigned ShiftOpLo = SelectExpr(N.getOperand(0));
1896 unsigned ShiftOpHi = SelectExpr(N.getOperand(1));
1897 unsigned SHReg = SelectExpr(N.getOperand(2));
1898 Tmp1 = MakeReg(MVT::i32);
1899 Tmp2 = MakeReg(MVT::i32);
1900 Tmp3 = MakeReg(MVT::i32);
1901 unsigned Tmp4 = MakeReg(MVT::i32);
1902 unsigned Tmp5 = MakeReg(MVT::i32);
1903 unsigned Tmp6 = MakeReg(MVT::i32);
1904 BuildMI(BB, PPC::SUBFIC, 2, Tmp1).addReg(SHReg).addSImm(32);
1905 if (ISD::SHL_PARTS == opcode) {
1906 BuildMI(BB, PPC::SLW, 2, Tmp2).addReg(ShiftOpHi).addReg(SHReg);
1907 BuildMI(BB, PPC::SRW, 2, Tmp3).addReg(ShiftOpLo).addReg(Tmp1);
1908 BuildMI(BB, PPC::OR, 2, Tmp4).addReg(Tmp2).addReg(Tmp3);
1909 BuildMI(BB, PPC::ADDI, 2, Tmp5).addReg(SHReg).addSImm(-32);
Nate Begemanfa554702005-04-03 22:13:27 +00001910 BuildMI(BB, PPC::SLW, 2, Tmp6).addReg(ShiftOpLo).addReg(Tmp5);
Nate Begeman27eeb002005-04-02 05:59:34 +00001911 BuildMI(BB, PPC::OR, 2, Result+1).addReg(Tmp4).addReg(Tmp6);
1912 BuildMI(BB, PPC::SLW, 2, Result).addReg(ShiftOpLo).addReg(SHReg);
1913 } else if (ISD::SRL_PARTS == opcode) {
1914 BuildMI(BB, PPC::SRW, 2, Tmp2).addReg(ShiftOpLo).addReg(SHReg);
1915 BuildMI(BB, PPC::SLW, 2, Tmp3).addReg(ShiftOpHi).addReg(Tmp1);
1916 BuildMI(BB, PPC::OR, 2, Tmp4).addReg(Tmp2).addReg(Tmp3);
1917 BuildMI(BB, PPC::ADDI, 2, Tmp5).addReg(SHReg).addSImm(-32);
1918 BuildMI(BB, PPC::SRW, 2, Tmp6).addReg(ShiftOpHi).addReg(Tmp5);
1919 BuildMI(BB, PPC::OR, 2, Result).addReg(Tmp4).addReg(Tmp6);
1920 BuildMI(BB, PPC::SRW, 2, Result+1).addReg(ShiftOpHi).addReg(SHReg);
1921 } else {
1922 MachineBasicBlock *TmpMBB = new MachineBasicBlock(BB->getBasicBlock());
1923 MachineBasicBlock *PhiMBB = new MachineBasicBlock(BB->getBasicBlock());
1924 MachineBasicBlock *OldMBB = BB;
1925 MachineFunction *F = BB->getParent();
1926 ilist<MachineBasicBlock>::iterator It = BB; ++It;
1927 F->getBasicBlockList().insert(It, TmpMBB);
1928 F->getBasicBlockList().insert(It, PhiMBB);
1929 BB->addSuccessor(TmpMBB);
1930 BB->addSuccessor(PhiMBB);
1931 BuildMI(BB, PPC::SRW, 2, Tmp2).addReg(ShiftOpLo).addReg(SHReg);
1932 BuildMI(BB, PPC::SLW, 2, Tmp3).addReg(ShiftOpHi).addReg(Tmp1);
1933 BuildMI(BB, PPC::OR, 2, Tmp4).addReg(Tmp2).addReg(Tmp3);
1934 BuildMI(BB, PPC::ADDICo, 2, Tmp5).addReg(SHReg).addSImm(-32);
1935 BuildMI(BB, PPC::SRAW, 2, Tmp6).addReg(ShiftOpHi).addReg(Tmp5);
1936 BuildMI(BB, PPC::SRAW, 2, Result+1).addReg(ShiftOpHi).addReg(SHReg);
1937 BuildMI(BB, PPC::BLE, 2).addReg(PPC::CR0).addMBB(PhiMBB);
1938 // Select correct least significant half if the shift amount > 32
1939 BB = TmpMBB;
1940 unsigned Tmp7 = MakeReg(MVT::i32);
1941 BuildMI(BB, PPC::OR, 2, Tmp7).addReg(Tmp6).addReg(Tmp6);
1942 TmpMBB->addSuccessor(PhiMBB);
1943 BB = PhiMBB;
1944 BuildMI(BB, PPC::PHI, 4, Result).addReg(Tmp4).addMBB(OldMBB)
1945 .addReg(Tmp7).addMBB(TmpMBB);
Nate Begemanca12a2b2005-03-28 22:28:37 +00001946 }
1947 return Result+N.ResNo;
1948 }
1949
Nate Begemana9795f82005-03-24 04:41:43 +00001950 case ISD::FP_TO_UINT:
Nate Begeman6b559972005-04-01 02:59:27 +00001951 case ISD::FP_TO_SINT: {
1952 bool U = (ISD::FP_TO_UINT == opcode);
1953 Tmp1 = SelectExpr(N.getOperand(0));
1954 if (!U) {
1955 Tmp2 = MakeReg(MVT::f64);
1956 BuildMI(BB, PPC::FCTIWZ, 1, Tmp2).addReg(Tmp1);
1957 int FrameIdx = BB->getParent()->getFrameInfo()->CreateStackObject(8, 8);
1958 addFrameReference(BuildMI(BB, PPC::STFD, 3).addReg(Tmp2), FrameIdx);
1959 addFrameReference(BuildMI(BB, PPC::LWZ, 2, Result), FrameIdx, 4);
1960 return Result;
1961 } else {
1962 unsigned Zero = getConstDouble(0.0);
1963 unsigned MaxInt = getConstDouble((1LL << 32) - 1);
1964 unsigned Border = getConstDouble(1LL << 31);
1965 unsigned UseZero = MakeReg(MVT::f64);
1966 unsigned UseMaxInt = MakeReg(MVT::f64);
1967 unsigned UseChoice = MakeReg(MVT::f64);
1968 unsigned TmpReg = MakeReg(MVT::f64);
1969 unsigned TmpReg2 = MakeReg(MVT::f64);
1970 unsigned ConvReg = MakeReg(MVT::f64);
1971 unsigned IntTmp = MakeReg(MVT::i32);
1972 unsigned XorReg = MakeReg(MVT::i32);
1973 MachineFunction *F = BB->getParent();
1974 int FrameIdx = F->getFrameInfo()->CreateStackObject(8, 8);
1975 // Update machine-CFG edges
1976 MachineBasicBlock *XorMBB = new MachineBasicBlock(BB->getBasicBlock());
1977 MachineBasicBlock *PhiMBB = new MachineBasicBlock(BB->getBasicBlock());
1978 MachineBasicBlock *OldMBB = BB;
1979 ilist<MachineBasicBlock>::iterator It = BB; ++It;
1980 F->getBasicBlockList().insert(It, XorMBB);
1981 F->getBasicBlockList().insert(It, PhiMBB);
1982 BB->addSuccessor(XorMBB);
1983 BB->addSuccessor(PhiMBB);
1984 // Convert from floating point to unsigned 32-bit value
1985 // Use 0 if incoming value is < 0.0
1986 BuildMI(BB, PPC::FSEL, 3, UseZero).addReg(Tmp1).addReg(Tmp1).addReg(Zero);
1987 // Use 2**32 - 1 if incoming value is >= 2**32
1988 BuildMI(BB, PPC::FSUB, 2, UseMaxInt).addReg(MaxInt).addReg(Tmp1);
1989 BuildMI(BB, PPC::FSEL, 3, UseChoice).addReg(UseMaxInt).addReg(UseZero)
1990 .addReg(MaxInt);
1991 // Subtract 2**31
1992 BuildMI(BB, PPC::FSUB, 2, TmpReg).addReg(UseChoice).addReg(Border);
1993 // Use difference if >= 2**31
1994 BuildMI(BB, PPC::FCMPU, 2, PPC::CR0).addReg(UseChoice).addReg(Border);
1995 BuildMI(BB, PPC::FSEL, 3, TmpReg2).addReg(TmpReg).addReg(TmpReg)
1996 .addReg(UseChoice);
1997 // Convert to integer
1998 BuildMI(BB, PPC::FCTIWZ, 1, ConvReg).addReg(TmpReg2);
1999 addFrameReference(BuildMI(BB, PPC::STFD, 3).addReg(ConvReg), FrameIdx);
2000 addFrameReference(BuildMI(BB, PPC::LWZ, 2, IntTmp), FrameIdx, 4);
2001 BuildMI(BB, PPC::BLT, 2).addReg(PPC::CR0).addMBB(PhiMBB);
2002 BuildMI(BB, PPC::B, 1).addMBB(XorMBB);
2003
2004 // XorMBB:
2005 // add 2**31 if input was >= 2**31
2006 BB = XorMBB;
2007 BuildMI(BB, PPC::XORIS, 2, XorReg).addReg(IntTmp).addImm(0x8000);
2008 XorMBB->addSuccessor(PhiMBB);
2009
2010 // PhiMBB:
2011 // DestReg = phi [ IntTmp, OldMBB ], [ XorReg, XorMBB ]
2012 BB = PhiMBB;
2013 BuildMI(BB, PPC::PHI, 4, Result).addReg(IntTmp).addMBB(OldMBB)
2014 .addReg(XorReg).addMBB(XorMBB);
2015 return Result;
2016 }
2017 assert(0 && "Should never get here");
2018 return 0;
2019 }
Nate Begemana9795f82005-03-24 04:41:43 +00002020
Nate Begeman9e3e1b52005-03-24 23:35:30 +00002021 case ISD::SETCC:
Nate Begeman33162522005-03-29 21:54:38 +00002022 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Node)) {
Nate Begeman7e7fadd2005-04-07 20:30:01 +00002023 if (ConstantSDNode *CN =
2024 dyn_cast<ConstantSDNode>(SetCC->getOperand(1).Val)) {
Nate Begeman9765c252005-04-12 21:22:28 +00002025 // We can codegen setcc op, imm very efficiently compared to a brcond.
2026 // Check for those cases here.
2027 // setcc op, 0
Nate Begeman7e7fadd2005-04-07 20:30:01 +00002028 if (CN->getValue() == 0) {
2029 Tmp1 = SelectExpr(SetCC->getOperand(0));
2030 switch (SetCC->getCondition()) {
2031 default: assert(0 && "Unhandled SetCC condition"); abort();
2032 case ISD::SETEQ:
Nate Begeman7e7fadd2005-04-07 20:30:01 +00002033 Tmp2 = MakeReg(MVT::i32);
2034 BuildMI(BB, PPC::CNTLZW, 1, Tmp2).addReg(Tmp1);
2035 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp2).addImm(27)
2036 .addImm(5).addImm(31);
2037 break;
2038 case ISD::SETNE:
Nate Begeman7e7fadd2005-04-07 20:30:01 +00002039 Tmp2 = MakeReg(MVT::i32);
2040 BuildMI(BB, PPC::ADDIC, 2, Tmp2).addReg(Tmp1).addSImm(-1);
2041 BuildMI(BB, PPC::SUBFE, 2, Result).addReg(Tmp2).addReg(Tmp1);
2042 break;
Nate Begeman7e7fadd2005-04-07 20:30:01 +00002043 case ISD::SETLT:
2044 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(1)
2045 .addImm(31).addImm(31);
2046 break;
Nate Begeman7e7fadd2005-04-07 20:30:01 +00002047 case ISD::SETGT:
2048 Tmp2 = MakeReg(MVT::i32);
2049 Tmp3 = MakeReg(MVT::i32);
2050 BuildMI(BB, PPC::NEG, 2, Tmp2).addReg(Tmp1);
2051 BuildMI(BB, PPC::ANDC, 2, Tmp3).addReg(Tmp2).addReg(Tmp1);
2052 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp3).addImm(1)
2053 .addImm(31).addImm(31);
2054 break;
Nate Begeman9765c252005-04-12 21:22:28 +00002055 }
2056 return Result;
2057 }
2058 // setcc op, -1
2059 if (CN->isAllOnesValue()) {
2060 Tmp1 = SelectExpr(SetCC->getOperand(0));
2061 switch (SetCC->getCondition()) {
2062 default: assert(0 && "Unhandled SetCC condition"); abort();
2063 case ISD::SETEQ:
2064 Tmp2 = MakeReg(MVT::i32);
2065 Tmp3 = MakeReg(MVT::i32);
2066 BuildMI(BB, PPC::ADDIC, 2, Tmp2).addReg(Tmp1).addSImm(1);
2067 BuildMI(BB, PPC::LI, 1, Tmp3).addSImm(0);
2068 BuildMI(BB, PPC::ADDZE, 1, Result).addReg(Tmp3);
Nate Begeman7e7fadd2005-04-07 20:30:01 +00002069 break;
Nate Begeman9765c252005-04-12 21:22:28 +00002070 case ISD::SETNE:
2071 Tmp2 = MakeReg(MVT::i32);
2072 Tmp3 = MakeReg(MVT::i32);
2073 BuildMI(BB, PPC::NOR, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
2074 BuildMI(BB, PPC::ADDIC, 2, Tmp3).addReg(Tmp2).addSImm(-1);
2075 BuildMI(BB, PPC::SUBFE, 2, Result).addReg(Tmp3).addReg(Tmp2);
2076 break;
2077 case ISD::SETLT:
2078 Tmp2 = MakeReg(MVT::i32);
2079 Tmp3 = MakeReg(MVT::i32);
2080 BuildMI(BB, PPC::ADDI, 2, Tmp2).addReg(Tmp1).addSImm(1);
2081 BuildMI(BB, PPC::AND, 2, Tmp3).addReg(Tmp2).addReg(Tmp1);
2082 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp3).addImm(1)
2083 .addImm(31).addImm(31);
2084 break;
2085 case ISD::SETGT:
2086 Tmp2 = MakeReg(MVT::i32);
Nate Begeman7e7fadd2005-04-07 20:30:01 +00002087 BuildMI(BB, PPC::RLWINM, 4, Tmp2).addReg(Tmp1).addImm(1)
2088 .addImm(31).addImm(31);
2089 BuildMI(BB, PPC::XORI, 2, Result).addReg(Tmp2).addImm(1);
2090 break;
2091 }
2092 return Result;
2093 }
2094 }
2095
Nate Begemandffcfcc2005-04-01 00:32:34 +00002096 Opc = SelectSetCR0(N);
Nate Begeman31318e42005-04-01 07:21:30 +00002097 unsigned TrueValue = MakeReg(MVT::i32);
2098 BuildMI(BB, PPC::LI, 1, TrueValue).addSImm(1);
2099 unsigned FalseValue = MakeReg(MVT::i32);
2100 BuildMI(BB, PPC::LI, 1, FalseValue).addSImm(0);
2101
Nate Begeman33162522005-03-29 21:54:38 +00002102 // Create an iterator with which to insert the MBB for copying the false
2103 // value and the MBB to hold the PHI instruction for this SetCC.
2104 MachineBasicBlock *thisMBB = BB;
2105 const BasicBlock *LLVM_BB = BB->getBasicBlock();
2106 ilist<MachineBasicBlock>::iterator It = BB;
2107 ++It;
2108
2109 // thisMBB:
2110 // ...
2111 // cmpTY cr0, r1, r2
2112 // %TrueValue = li 1
2113 // bCC sinkMBB
Nate Begeman33162522005-03-29 21:54:38 +00002114 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
2115 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
2116 BuildMI(BB, Opc, 2).addReg(PPC::CR0).addMBB(sinkMBB);
2117 MachineFunction *F = BB->getParent();
2118 F->getBasicBlockList().insert(It, copy0MBB);
2119 F->getBasicBlockList().insert(It, sinkMBB);
2120 // Update machine-CFG edges
2121 BB->addSuccessor(copy0MBB);
2122 BB->addSuccessor(sinkMBB);
2123
2124 // copy0MBB:
2125 // %FalseValue = li 0
2126 // fallthrough
2127 BB = copy0MBB;
Nate Begeman33162522005-03-29 21:54:38 +00002128 // Update machine-CFG edges
2129 BB->addSuccessor(sinkMBB);
2130
2131 // sinkMBB:
2132 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
2133 // ...
2134 BB = sinkMBB;
2135 BuildMI(BB, PPC::PHI, 4, Result).addReg(FalseValue)
2136 .addMBB(copy0MBB).addReg(TrueValue).addMBB(thisMBB);
2137 return Result;
2138 }
2139 assert(0 && "Is this legal?");
2140 return 0;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00002141
Nate Begeman74747862005-03-29 22:24:51 +00002142 case ISD::SELECT: {
Nate Begeman7e7fadd2005-04-07 20:30:01 +00002143 // We can codegen select (a < 0) ? b : 0 very efficiently compared to a
2144 // conditional branch. Check for that here.
2145 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(N.getOperand(0).Val)) {
2146 if (ConstantSDNode *CN =
2147 dyn_cast<ConstantSDNode>(SetCC->getOperand(1).Val)) {
2148 if (ConstantSDNode *CNF =
2149 dyn_cast<ConstantSDNode>(N.getOperand(2).Val)) {
2150 if (CN->getValue() == 0 && CNF->getValue() == 0 &&
2151 SetCC->getCondition() == ISD::SETLT) {
2152 Tmp1 = SelectExpr(N.getOperand(1)); // TRUE value
2153 Tmp2 = SelectExpr(SetCC->getOperand(0));
2154 Tmp3 = MakeReg(MVT::i32);
2155 BuildMI(BB, PPC::SRAWI, 2, Tmp3).addReg(Tmp2).addImm(31);
2156 BuildMI(BB, PPC::AND, 2, Result).addReg(Tmp1).addReg(Tmp3);
2157 return Result;
2158 }
2159 }
2160 }
2161 }
Chris Lattner30710192005-04-01 07:10:02 +00002162 unsigned TrueValue = SelectExpr(N.getOperand(1)); //Use if TRUE
2163 unsigned FalseValue = SelectExpr(N.getOperand(2)); //Use if FALSE
Nate Begeman6cb2e1b2005-04-01 08:57:43 +00002164 Opc = SelectSetCR0(N.getOperand(0));
Chris Lattner30710192005-04-01 07:10:02 +00002165
Nate Begeman74747862005-03-29 22:24:51 +00002166 // Create an iterator with which to insert the MBB for copying the false
2167 // value and the MBB to hold the PHI instruction for this SetCC.
2168 MachineBasicBlock *thisMBB = BB;
2169 const BasicBlock *LLVM_BB = BB->getBasicBlock();
2170 ilist<MachineBasicBlock>::iterator It = BB;
2171 ++It;
2172
2173 // thisMBB:
2174 // ...
2175 // TrueVal = ...
2176 // cmpTY cr0, r1, r2
2177 // bCC copy1MBB
2178 // fallthrough --> copy0MBB
Nate Begeman74747862005-03-29 22:24:51 +00002179 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
2180 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
Nate Begeman3e897162005-03-31 23:55:40 +00002181 BuildMI(BB, Opc, 2).addReg(PPC::CR0).addMBB(sinkMBB);
Nate Begeman74747862005-03-29 22:24:51 +00002182 MachineFunction *F = BB->getParent();
2183 F->getBasicBlockList().insert(It, copy0MBB);
2184 F->getBasicBlockList().insert(It, sinkMBB);
2185 // Update machine-CFG edges
2186 BB->addSuccessor(copy0MBB);
2187 BB->addSuccessor(sinkMBB);
2188
2189 // copy0MBB:
2190 // %FalseValue = ...
2191 // # fallthrough to sinkMBB
2192 BB = copy0MBB;
Nate Begeman74747862005-03-29 22:24:51 +00002193 // Update machine-CFG edges
2194 BB->addSuccessor(sinkMBB);
2195
2196 // sinkMBB:
2197 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
2198 // ...
2199 BB = sinkMBB;
2200 BuildMI(BB, PPC::PHI, 4, Result).addReg(FalseValue)
2201 .addMBB(copy0MBB).addReg(TrueValue).addMBB(thisMBB);
Nate Begeman74747862005-03-29 22:24:51 +00002202 return Result;
2203 }
Nate Begemana9795f82005-03-24 04:41:43 +00002204
2205 case ISD::Constant:
2206 switch (N.getValueType()) {
2207 default: assert(0 && "Cannot use constants of this type!");
2208 case MVT::i1:
2209 BuildMI(BB, PPC::LI, 1, Result)
2210 .addSImm(!cast<ConstantSDNode>(N)->isNullValue());
2211 break;
2212 case MVT::i32:
2213 {
2214 int v = (int)cast<ConstantSDNode>(N)->getSignExtended();
2215 if (v < 32768 && v >= -32768) {
2216 BuildMI(BB, PPC::LI, 1, Result).addSImm(v);
2217 } else {
Nate Begeman5e966612005-03-24 06:28:42 +00002218 Tmp1 = MakeReg(MVT::i32);
2219 BuildMI(BB, PPC::LIS, 1, Tmp1).addSImm(v >> 16);
2220 BuildMI(BB, PPC::ORI, 2, Result).addReg(Tmp1).addImm(v & 0xFFFF);
Nate Begemana9795f82005-03-24 04:41:43 +00002221 }
2222 }
2223 }
2224 return Result;
2225 }
2226
2227 return 0;
2228}
2229
2230void ISel::Select(SDOperand N) {
2231 unsigned Tmp1, Tmp2, Opc;
2232 unsigned opcode = N.getOpcode();
2233
2234 if (!ExprMap.insert(std::make_pair(N, 1)).second)
2235 return; // Already selected.
2236
2237 SDNode *Node = N.Val;
2238
2239 switch (Node->getOpcode()) {
2240 default:
2241 Node->dump(); std::cerr << "\n";
2242 assert(0 && "Node not handled yet!");
2243 case ISD::EntryToken: return; // Noop
2244 case ISD::TokenFactor:
2245 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
2246 Select(Node->getOperand(i));
2247 return;
2248 case ISD::ADJCALLSTACKDOWN:
2249 case ISD::ADJCALLSTACKUP:
2250 Select(N.getOperand(0));
2251 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
2252 Opc = N.getOpcode() == ISD::ADJCALLSTACKDOWN ? PPC::ADJCALLSTACKDOWN :
2253 PPC::ADJCALLSTACKUP;
2254 BuildMI(BB, Opc, 1).addImm(Tmp1);
2255 return;
2256 case ISD::BR: {
2257 MachineBasicBlock *Dest =
2258 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
Nate Begemana9795f82005-03-24 04:41:43 +00002259 Select(N.getOperand(0));
2260 BuildMI(BB, PPC::B, 1).addMBB(Dest);
2261 return;
2262 }
2263 case ISD::BRCOND:
Nate Begemancd08e4c2005-04-09 20:09:12 +00002264 case ISD::BRCONDTWOWAY:
Nate Begemana9795f82005-03-24 04:41:43 +00002265 SelectBranchCC(N);
2266 return;
2267 case ISD::CopyToReg:
2268 Select(N.getOperand(0));
2269 Tmp1 = SelectExpr(N.getOperand(1));
2270 Tmp2 = cast<RegSDNode>(N)->getReg();
2271
2272 if (Tmp1 != Tmp2) {
2273 if (N.getOperand(1).getValueType() == MVT::f64 ||
2274 N.getOperand(1).getValueType() == MVT::f32)
2275 BuildMI(BB, PPC::FMR, 1, Tmp2).addReg(Tmp1);
2276 else
2277 BuildMI(BB, PPC::OR, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
2278 }
2279 return;
2280 case ISD::ImplicitDef:
2281 Select(N.getOperand(0));
2282 BuildMI(BB, PPC::IMPLICIT_DEF, 0, cast<RegSDNode>(N)->getReg());
2283 return;
2284 case ISD::RET:
2285 switch (N.getNumOperands()) {
2286 default:
2287 assert(0 && "Unknown return instruction!");
2288 case 3:
2289 assert(N.getOperand(1).getValueType() == MVT::i32 &&
2290 N.getOperand(2).getValueType() == MVT::i32 &&
2291 "Unknown two-register value!");
2292 Select(N.getOperand(0));
2293 Tmp1 = SelectExpr(N.getOperand(1));
2294 Tmp2 = SelectExpr(N.getOperand(2));
Nate Begeman27523a12005-04-02 00:42:16 +00002295 BuildMI(BB, PPC::OR, 2, PPC::R3).addReg(Tmp2).addReg(Tmp2);
2296 BuildMI(BB, PPC::OR, 2, PPC::R4).addReg(Tmp1).addReg(Tmp1);
Nate Begemana9795f82005-03-24 04:41:43 +00002297 break;
2298 case 2:
2299 Select(N.getOperand(0));
2300 Tmp1 = SelectExpr(N.getOperand(1));
2301 switch (N.getOperand(1).getValueType()) {
2302 default:
2303 assert(0 && "Unknown return type!");
2304 case MVT::f64:
2305 case MVT::f32:
2306 BuildMI(BB, PPC::FMR, 1, PPC::F1).addReg(Tmp1);
2307 break;
2308 case MVT::i32:
2309 BuildMI(BB, PPC::OR, 2, PPC::R3).addReg(Tmp1).addReg(Tmp1);
2310 break;
2311 }
Nate Begeman9e3e1b52005-03-24 23:35:30 +00002312 case 1:
2313 Select(N.getOperand(0));
2314 break;
Nate Begemana9795f82005-03-24 04:41:43 +00002315 }
2316 BuildMI(BB, PPC::BLR, 0); // Just emit a 'ret' instruction
2317 return;
Nate Begemana9795f82005-03-24 04:41:43 +00002318 case ISD::TRUNCSTORE:
2319 case ISD::STORE:
2320 {
2321 SDOperand Chain = N.getOperand(0);
2322 SDOperand Value = N.getOperand(1);
2323 SDOperand Address = N.getOperand(2);
2324 Select(Chain);
2325
2326 Tmp1 = SelectExpr(Value); //value
2327
2328 if (opcode == ISD::STORE) {
2329 switch(Value.getValueType()) {
2330 default: assert(0 && "unknown Type in store");
2331 case MVT::i32: Opc = PPC::STW; break;
2332 case MVT::f64: Opc = PPC::STFD; break;
2333 case MVT::f32: Opc = PPC::STFS; break;
2334 }
2335 } else { //ISD::TRUNCSTORE
2336 switch(cast<MVTSDNode>(Node)->getExtraValueType()) {
2337 default: assert(0 && "unknown Type in store");
Nate Begeman7e7fadd2005-04-07 20:30:01 +00002338 case MVT::i1:
Nate Begemana9795f82005-03-24 04:41:43 +00002339 case MVT::i8: Opc = PPC::STB; break;
2340 case MVT::i16: Opc = PPC::STH; break;
2341 }
2342 }
2343
Nate Begemana7e11a42005-04-01 05:57:17 +00002344 if(Address.getOpcode() == ISD::FrameIndex)
Nate Begemana9795f82005-03-24 04:41:43 +00002345 {
Nate Begeman58f718c2005-03-30 02:23:08 +00002346 Tmp2 = cast<FrameIndexSDNode>(Address)->getIndex();
2347 addFrameReference(BuildMI(BB, Opc, 3).addReg(Tmp1), (int)Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +00002348 }
2349 else
2350 {
2351 int offset;
Nate Begeman04730362005-04-01 04:45:11 +00002352 bool idx = SelectAddr(Address, Tmp2, offset);
2353 if (idx) {
2354 Opc = IndexedOpForOp(Opc);
2355 BuildMI(BB, Opc, 3).addReg(Tmp1).addReg(Tmp2).addReg(offset);
2356 } else {
2357 BuildMI(BB, Opc, 3).addReg(Tmp1).addImm(offset).addReg(Tmp2);
2358 }
Nate Begemana9795f82005-03-24 04:41:43 +00002359 }
2360 return;
2361 }
2362 case ISD::EXTLOAD:
2363 case ISD::SEXTLOAD:
2364 case ISD::ZEXTLOAD:
2365 case ISD::LOAD:
2366 case ISD::CopyFromReg:
2367 case ISD::CALL:
2368 case ISD::DYNAMIC_STACKALLOC:
2369 ExprMap.erase(N);
2370 SelectExpr(N);
2371 return;
2372 }
2373 assert(0 && "Should not be reached!");
2374}
2375
2376
2377/// createPPC32PatternInstructionSelector - This pass converts an LLVM function
2378/// into a machine code representation using pattern matching and a machine
2379/// description file.
2380///
2381FunctionPass *llvm::createPPC32ISelPattern(TargetMachine &TM) {
2382 return new ISel(TM);
Chris Lattner246fa632005-03-24 06:16:18 +00002383}
2384