blob: be50248f555e0864e5d2ad806ca9207ff5a8c48e [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
Chris Lattnercbd06fc2005-04-07 19:41:49 +000064 setSetCCResultContents(ZeroOrOneSetCCResult);
Nate Begeman3e897162005-03-31 23:55:40 +000065 addLegalFPImmediate(+0.0); // Necessary for FSEL
66 addLegalFPImmediate(-0.0); //
67
Nate Begemana9795f82005-03-24 04:41:43 +000068 computeRegisterProperties();
69 }
70
71 /// LowerArguments - This hook must be implemented to indicate how we should
72 /// lower the arguments for the specified function, into the specified DAG.
73 virtual std::vector<SDOperand>
74 LowerArguments(Function &F, SelectionDAG &DAG);
75
76 /// LowerCallTo - This hook lowers an abstract call to a function into an
77 /// actual call.
78 virtual std::pair<SDOperand, SDOperand>
Nate Begeman307e7442005-03-26 01:28:53 +000079 LowerCallTo(SDOperand Chain, const Type *RetTy, bool isVarArg,
80 SDOperand Callee, ArgListTy &Args, SelectionDAG &DAG);
Nate Begemana9795f82005-03-24 04:41:43 +000081
82 virtual std::pair<SDOperand, SDOperand>
83 LowerVAStart(SDOperand Chain, SelectionDAG &DAG);
84
85 virtual std::pair<SDOperand,SDOperand>
86 LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
87 const Type *ArgTy, SelectionDAG &DAG);
88
89 virtual std::pair<SDOperand, SDOperand>
90 LowerFrameReturnAddress(bool isFrameAddr, SDOperand Chain, unsigned Depth,
91 SelectionDAG &DAG);
92 };
93}
94
95
96std::vector<SDOperand>
97PPC32TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
98 //
99 // add beautiful description of PPC stack frame format, or at least some docs
100 //
101 MachineFunction &MF = DAG.getMachineFunction();
102 MachineFrameInfo *MFI = MF.getFrameInfo();
103 MachineBasicBlock& BB = MF.front();
104 std::vector<SDOperand> ArgValues;
105
106 // Due to the rather complicated nature of the PowerPC ABI, rather than a
107 // fixed size array of physical args, for the sake of simplicity let the STL
108 // handle tracking them for us.
109 std::vector<unsigned> argVR, argPR, argOp;
110 unsigned ArgOffset = 24;
111 unsigned GPR_remaining = 8;
112 unsigned FPR_remaining = 13;
113 unsigned GPR_idx = 0, FPR_idx = 0;
114 static const unsigned GPR[] = {
115 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
116 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
117 };
118 static const unsigned FPR[] = {
119 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
120 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
121 };
122
123 // Add DAG nodes to load the arguments... On entry to a function on PPC,
124 // the arguments start at offset 24, although they are likely to be passed
125 // in registers.
126 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
127 SDOperand newroot, argt;
128 unsigned ObjSize;
129 bool needsLoad = false;
Nate Begemancd08e4c2005-04-09 20:09:12 +0000130 bool ArgLive = !I->use_empty();
Nate Begemana9795f82005-03-24 04:41:43 +0000131 MVT::ValueType ObjectVT = getValueType(I->getType());
132
133 switch (ObjectVT) {
134 default: assert(0 && "Unhandled argument type!");
135 case MVT::i1:
136 case MVT::i8:
137 case MVT::i16:
138 case MVT::i32:
139 ObjSize = 4;
Nate Begemancd08e4c2005-04-09 20:09:12 +0000140 if (!ArgLive) break;
Nate Begemana9795f82005-03-24 04:41:43 +0000141 if (GPR_remaining > 0) {
Nate Begemancd08e4c2005-04-09 20:09:12 +0000142 MF.addLiveIn(GPR[GPR_idx]);
Nate Begemanf70b5762005-03-28 23:08:54 +0000143 argt = newroot = DAG.getCopyFromReg(GPR[GPR_idx], MVT::i32,
144 DAG.getRoot());
Nate Begemana9795f82005-03-24 04:41:43 +0000145 if (ObjectVT != MVT::i32)
146 argt = DAG.getNode(ISD::TRUNCATE, ObjectVT, newroot);
Nate Begemana9795f82005-03-24 04:41:43 +0000147 } else {
148 needsLoad = true;
149 }
150 break;
Nate Begemanf7e43382005-03-26 07:46:36 +0000151 case MVT::i64: ObjSize = 8;
Nate Begemancd08e4c2005-04-09 20:09:12 +0000152 if (!ArgLive) break;
Nate Begemanc5b1cd22005-04-10 05:53:14 +0000153 if (GPR_remaining > 0) {
154 SDOperand argHi, argLo;
Nate Begemancd08e4c2005-04-09 20:09:12 +0000155 MF.addLiveIn(GPR[GPR_idx]);
Nate Begemanc5b1cd22005-04-10 05:53:14 +0000156 argHi = DAG.getCopyFromReg(GPR[GPR_idx], MVT::i32, DAG.getRoot());
157 // If we have two or more remaining argument registers, then both halves
158 // of the i64 can be sourced from there. Otherwise, the lower half will
159 // have to come off the stack. This can happen when an i64 is preceded
160 // by 28 bytes of arguments.
161 if (GPR_remaining > 1) {
162 MF.addLiveIn(GPR[GPR_idx+1]);
163 argLo = DAG.getCopyFromReg(GPR[GPR_idx+1], MVT::i32, argHi);
164 } else {
165 int FI = MFI->CreateFixedObject(4, ArgOffset+4);
166 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
167 argLo = DAG.getLoad(MVT::i32, DAG.getEntryNode(), FIN);
168 }
Nate Begemanca12a2b2005-03-28 22:28:37 +0000169 // Build the outgoing arg thingy
Nate Begemanf70b5762005-03-28 23:08:54 +0000170 argt = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, argLo, argHi);
171 newroot = argLo;
Nate Begemana9795f82005-03-24 04:41:43 +0000172 } else {
173 needsLoad = true;
174 }
175 break;
Nate Begemancd08e4c2005-04-09 20:09:12 +0000176 case MVT::f32:
177 case MVT::f64:
178 ObjSize = (ObjectVT == MVT::f64) ? 8 : 4;
179 if (!ArgLive) break;
Nate Begemana9795f82005-03-24 04:41:43 +0000180 if (FPR_remaining > 0) {
Nate Begemancd08e4c2005-04-09 20:09:12 +0000181 MF.addLiveIn(FPR[FPR_idx]);
Nate Begemanf70b5762005-03-28 23:08:54 +0000182 argt = newroot = DAG.getCopyFromReg(FPR[FPR_idx], ObjectVT,
183 DAG.getRoot());
Nate Begemana9795f82005-03-24 04:41:43 +0000184 --FPR_remaining;
185 ++FPR_idx;
186 } else {
187 needsLoad = true;
188 }
189 break;
190 }
191
192 // We need to load the argument to a virtual register if we determined above
193 // that we ran out of physical registers of the appropriate type
194 if (needsLoad) {
Nate Begemane5846682005-04-04 06:52:38 +0000195 unsigned SubregOffset = 0;
Nate Begemanc3e2db42005-04-04 09:09:00 +0000196 if (ObjectVT == MVT::i8 || ObjectVT == MVT::i1) SubregOffset = 3;
Nate Begemane5846682005-04-04 06:52:38 +0000197 if (ObjectVT == MVT::i16) SubregOffset = 2;
Nate Begemana9795f82005-03-24 04:41:43 +0000198 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
199 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
Nate Begemane5846682005-04-04 06:52:38 +0000200 FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN,
201 DAG.getConstant(SubregOffset, MVT::i32));
Nate Begemana9795f82005-03-24 04:41:43 +0000202 argt = newroot = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN);
203 }
204
205 // Every 4 bytes of argument space consumes one of the GPRs available for
206 // argument passing.
207 if (GPR_remaining > 0) {
208 unsigned delta = (GPR_remaining > 1 && ObjSize == 8) ? 2 : 1;
209 GPR_remaining -= delta;
210 GPR_idx += delta;
211 }
212 ArgOffset += ObjSize;
Chris Lattner91277ea2005-04-09 21:23:24 +0000213 if (newroot.Val)
214 DAG.setRoot(newroot.getValue(1));
Nate Begemana9795f82005-03-24 04:41:43 +0000215
Nate Begemana9795f82005-03-24 04:41:43 +0000216 ArgValues.push_back(argt);
217 }
218
Nate Begemana9795f82005-03-24 04:41:43 +0000219 // If the function takes variable number of arguments, make a frame index for
220 // the start of the first vararg value... for expansion of llvm.va_start.
Nate Begemanfa554702005-04-03 22:13:27 +0000221 if (F.isVarArg()) {
Nate Begemana9795f82005-03-24 04:41:43 +0000222 VarArgsFrameIndex = MFI->CreateFixedObject(4, ArgOffset);
Nate Begemanfa554702005-04-03 22:13:27 +0000223 SDOperand FIN = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
Nate Begeman6644d4c2005-04-03 23:11:17 +0000224 // If this function is vararg, store any remaining integer argument regs
225 // to their spots on the stack so that they may be loaded by deferencing the
226 // result of va_next.
227 std::vector<SDOperand> MemOps;
228 for (; GPR_remaining > 0; --GPR_remaining, ++GPR_idx) {
Nate Begemancd08e4c2005-04-09 20:09:12 +0000229 MF.addLiveIn(GPR[GPR_idx]);
Nate Begeman6644d4c2005-04-03 23:11:17 +0000230 SDOperand Val = DAG.getCopyFromReg(GPR[GPR_idx], MVT::i32, DAG.getRoot());
231 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Val.getValue(1),
232 Val, FIN);
233 MemOps.push_back(Store);
234 // Increment the address by four for the next argument to store
235 SDOperand PtrOff = DAG.getConstant(4, getPointerTy());
236 FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN, PtrOff);
237 }
238 DAG.setRoot(DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps));
Nate Begemanfa554702005-04-03 22:13:27 +0000239 }
Nate Begemana9795f82005-03-24 04:41:43 +0000240
Nate Begemancd08e4c2005-04-09 20:09:12 +0000241 // Finally, inform the code generator which regs we return values in.
242 switch (getValueType(F.getReturnType())) {
243 default: assert(0 && "Unknown type!");
244 case MVT::isVoid: break;
245 case MVT::i1:
246 case MVT::i8:
247 case MVT::i16:
248 case MVT::i32:
249 MF.addLiveOut(PPC::R3);
250 break;
251 case MVT::i64:
252 MF.addLiveOut(PPC::R3);
253 MF.addLiveOut(PPC::R4);
254 break;
255 case MVT::f32:
256 case MVT::f64:
257 MF.addLiveOut(PPC::F1);
258 break;
259 }
260
Nate Begemana9795f82005-03-24 04:41:43 +0000261 return ArgValues;
262}
263
264std::pair<SDOperand, SDOperand>
265PPC32TargetLowering::LowerCallTo(SDOperand Chain,
Nate Begeman307e7442005-03-26 01:28:53 +0000266 const Type *RetTy, bool isVarArg,
267 SDOperand Callee, ArgListTy &Args, SelectionDAG &DAG) {
268 // args_to_use will accumulate outgoing args for the ISD::CALL case in
269 // SelectExpr to use to put the arguments in the appropriate registers.
Nate Begemana9795f82005-03-24 04:41:43 +0000270 std::vector<SDOperand> args_to_use;
Nate Begeman307e7442005-03-26 01:28:53 +0000271
272 // Count how many bytes are to be pushed on the stack, including the linkage
273 // area, and parameter passing area.
274 unsigned NumBytes = 24;
275
276 if (Args.empty()) {
Nate Begemana7e11a42005-04-01 05:57:17 +0000277 Chain = DAG.getNode(ISD::ADJCALLSTACKDOWN, MVT::Other, Chain,
278 DAG.getConstant(NumBytes, getPointerTy()));
Nate Begeman307e7442005-03-26 01:28:53 +0000279 } else {
280 for (unsigned i = 0, e = Args.size(); i != e; ++i)
281 switch (getValueType(Args[i].second)) {
282 default: assert(0 && "Unknown value type!");
283 case MVT::i1:
284 case MVT::i8:
285 case MVT::i16:
286 case MVT::i32:
287 case MVT::f32:
288 NumBytes += 4;
289 break;
290 case MVT::i64:
291 case MVT::f64:
292 NumBytes += 8;
293 break;
294 }
295
296 // Just to be safe, we'll always reserve the full 24 bytes of linkage area
297 // plus 32 bytes of argument space in case any called code gets funky on us.
298 if (NumBytes < 56) NumBytes = 56;
299
300 // Adjust the stack pointer for the new arguments...
301 // These operations are automatically eliminated by the prolog/epilog pass
302 Chain = DAG.getNode(ISD::ADJCALLSTACKDOWN, MVT::Other, Chain,
303 DAG.getConstant(NumBytes, getPointerTy()));
304
305 // Set up a copy of the stack pointer for use loading and storing any
306 // arguments that may not fit in the registers available for argument
307 // passing.
308 SDOperand StackPtr = DAG.getCopyFromReg(PPC::R1, MVT::i32,
309 DAG.getEntryNode());
310
311 // Figure out which arguments are going to go in registers, and which in
312 // memory. Also, if this is a vararg function, floating point operations
313 // must be stored to our stack, and loaded into integer regs as well, if
314 // any integer regs are available for argument passing.
315 unsigned ArgOffset = 24;
316 unsigned GPR_remaining = 8;
317 unsigned FPR_remaining = 13;
Nate Begeman74d73452005-03-31 00:15:26 +0000318
319 std::vector<SDOperand> MemOps;
Nate Begeman307e7442005-03-26 01:28:53 +0000320 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
321 // PtrOff will be used to store the current argument to the stack if a
322 // register cannot be found for it.
323 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
324 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
Nate Begemanf7e43382005-03-26 07:46:36 +0000325 MVT::ValueType ArgVT = getValueType(Args[i].second);
Nate Begeman307e7442005-03-26 01:28:53 +0000326
Nate Begemanf7e43382005-03-26 07:46:36 +0000327 switch (ArgVT) {
Nate Begeman307e7442005-03-26 01:28:53 +0000328 default: assert(0 && "Unexpected ValueType for argument!");
329 case MVT::i1:
330 case MVT::i8:
331 case MVT::i16:
332 // Promote the integer to 32 bits. If the input type is signed use a
333 // sign extend, otherwise use a zero extend.
334 if (Args[i].second->isSigned())
335 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
336 else
337 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
338 // FALL THROUGH
339 case MVT::i32:
340 if (GPR_remaining > 0) {
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000341 args_to_use.push_back(Args[i].first);
Nate Begeman307e7442005-03-26 01:28:53 +0000342 --GPR_remaining;
343 } else {
Nate Begeman74d73452005-03-31 00:15:26 +0000344 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
345 Args[i].first, PtrOff));
Nate Begeman307e7442005-03-26 01:28:53 +0000346 }
347 ArgOffset += 4;
348 break;
349 case MVT::i64:
Nate Begemanf7e43382005-03-26 07:46:36 +0000350 // If we have one free GPR left, we can place the upper half of the i64
351 // in it, and store the other half to the stack. If we have two or more
352 // free GPRs, then we can pass both halves of the i64 in registers.
353 if (GPR_remaining > 0) {
Nate Begemanf2622612005-03-26 02:17:46 +0000354 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
355 Args[i].first, DAG.getConstant(1, MVT::i32));
356 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
357 Args[i].first, DAG.getConstant(0, MVT::i32));
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000358 args_to_use.push_back(Hi);
Nate Begeman74d73452005-03-31 00:15:26 +0000359 --GPR_remaining;
Nate Begeman74d73452005-03-31 00:15:26 +0000360 if (GPR_remaining > 0) {
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000361 args_to_use.push_back(Lo);
Nate Begeman74d73452005-03-31 00:15:26 +0000362 --GPR_remaining;
Nate Begemanf7e43382005-03-26 07:46:36 +0000363 } else {
364 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
365 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
Nate Begeman74d73452005-03-31 00:15:26 +0000366 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
367 Lo, PtrOff));
Nate Begemanf7e43382005-03-26 07:46:36 +0000368 }
Nate Begeman307e7442005-03-26 01:28:53 +0000369 } else {
Nate Begeman74d73452005-03-31 00:15:26 +0000370 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
371 Args[i].first, PtrOff));
Nate Begeman307e7442005-03-26 01:28:53 +0000372 }
373 ArgOffset += 8;
374 break;
375 case MVT::f32:
Nate Begeman307e7442005-03-26 01:28:53 +0000376 case MVT::f64:
Nate Begemanf7e43382005-03-26 07:46:36 +0000377 if (FPR_remaining > 0) {
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000378 args_to_use.push_back(Args[i].first);
379 --FPR_remaining;
Nate Begemanf7e43382005-03-26 07:46:36 +0000380 if (isVarArg) {
Nate Begeman96fc6812005-03-31 02:05:53 +0000381 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Chain,
382 Args[i].first, PtrOff);
383 MemOps.push_back(Store);
Nate Begeman74d73452005-03-31 00:15:26 +0000384 // Float varargs are always shadowed in available integer registers
385 if (GPR_remaining > 0) {
Nate Begeman96fc6812005-03-31 02:05:53 +0000386 SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff);
Nate Begeman74d73452005-03-31 00:15:26 +0000387 MemOps.push_back(Load);
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000388 args_to_use.push_back(Load);
389 --GPR_remaining;
Nate Begeman74d73452005-03-31 00:15:26 +0000390 }
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000391 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
Nate Begeman74d73452005-03-31 00:15:26 +0000392 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
393 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
Nate Begeman96fc6812005-03-31 02:05:53 +0000394 SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff);
Nate Begeman74d73452005-03-31 00:15:26 +0000395 MemOps.push_back(Load);
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000396 args_to_use.push_back(Load);
397 --GPR_remaining;
Nate Begeman74d73452005-03-31 00:15:26 +0000398 }
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000399 } else {
400 // If we have any FPRs remaining, we may also have GPRs remaining.
401 // Args passed in FPRs consume either 1 (f32) or 2 (f64) available
402 // GPRs.
403 if (GPR_remaining > 0) {
404 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
405 --GPR_remaining;
406 }
407 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
408 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
409 --GPR_remaining;
410 }
Nate Begeman74d73452005-03-31 00:15:26 +0000411 }
Nate Begeman307e7442005-03-26 01:28:53 +0000412 } else {
Nate Begeman74d73452005-03-31 00:15:26 +0000413 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
414 Args[i].first, PtrOff));
Nate Begeman307e7442005-03-26 01:28:53 +0000415 }
Nate Begemanf7e43382005-03-26 07:46:36 +0000416 ArgOffset += (ArgVT == MVT::f32) ? 4 : 8;
Nate Begeman307e7442005-03-26 01:28:53 +0000417 break;
418 }
Nate Begemana9795f82005-03-24 04:41:43 +0000419 }
Nate Begeman74d73452005-03-31 00:15:26 +0000420 if (!MemOps.empty())
421 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps);
Nate Begemana9795f82005-03-24 04:41:43 +0000422 }
423
424 std::vector<MVT::ValueType> RetVals;
425 MVT::ValueType RetTyVT = getValueType(RetTy);
426 if (RetTyVT != MVT::isVoid)
427 RetVals.push_back(RetTyVT);
428 RetVals.push_back(MVT::Other);
429
430 SDOperand TheCall = SDOperand(DAG.getCall(RetVals,
431 Chain, Callee, args_to_use), 0);
432 Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
433 Chain = DAG.getNode(ISD::ADJCALLSTACKUP, MVT::Other, Chain,
434 DAG.getConstant(NumBytes, getPointerTy()));
435 return std::make_pair(TheCall, Chain);
436}
437
438std::pair<SDOperand, SDOperand>
439PPC32TargetLowering::LowerVAStart(SDOperand Chain, SelectionDAG &DAG) {
440 //vastart just returns the address of the VarArgsFrameIndex slot.
441 return std::make_pair(DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32), Chain);
442}
443
444std::pair<SDOperand,SDOperand> PPC32TargetLowering::
445LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
446 const Type *ArgTy, SelectionDAG &DAG) {
Nate Begemanc7b09f12005-03-25 08:34:25 +0000447 MVT::ValueType ArgVT = getValueType(ArgTy);
448 SDOperand Result;
449 if (!isVANext) {
450 Result = DAG.getLoad(ArgVT, DAG.getEntryNode(), VAList);
451 } else {
452 unsigned Amt;
453 if (ArgVT == MVT::i32 || ArgVT == MVT::f32)
454 Amt = 4;
455 else {
456 assert((ArgVT == MVT::i64 || ArgVT == MVT::f64) &&
457 "Other types should have been promoted for varargs!");
458 Amt = 8;
459 }
460 Result = DAG.getNode(ISD::ADD, VAList.getValueType(), VAList,
461 DAG.getConstant(Amt, VAList.getValueType()));
462 }
463 return std::make_pair(Result, Chain);
Nate Begemana9795f82005-03-24 04:41:43 +0000464}
465
466
467std::pair<SDOperand, SDOperand> PPC32TargetLowering::
468LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
469 SelectionDAG &DAG) {
Nate Begeman01d05262005-03-30 01:45:43 +0000470 assert(0 && "LowerFrameReturnAddress unimplemented");
Nate Begemana9795f82005-03-24 04:41:43 +0000471 abort();
472}
473
474namespace {
Nate Begemanc7bd4822005-04-11 06:34:10 +0000475Statistic<>Recorded("ppc-codegen", "Number of recording ops emitted");
Nate Begeman93075ec2005-04-04 23:40:36 +0000476Statistic<>FusedFP("ppc-codegen", "Number of fused fp operations");
Nate Begemana9795f82005-03-24 04:41:43 +0000477//===--------------------------------------------------------------------===//
478/// ISel - PPC32 specific code to select PPC32 machine instructions for
479/// SelectionDAG operations.
480//===--------------------------------------------------------------------===//
481class ISel : public SelectionDAGISel {
Nate Begemana9795f82005-03-24 04:41:43 +0000482 PPC32TargetLowering PPC32Lowering;
Nate Begeman815d6da2005-04-06 00:25:27 +0000483 SelectionDAG *ISelDAG; // Hack to support us having a dag->dag transform
484 // for sdiv and udiv until it is put into the future
485 // dag combiner.
Nate Begemana9795f82005-03-24 04:41:43 +0000486
487 /// ExprMap - As shared expressions are codegen'd, we keep track of which
488 /// vreg the value is produced in, so we only emit one copy of each compiled
489 /// tree.
490 std::map<SDOperand, unsigned> ExprMap;
Nate Begemanc7b09f12005-03-25 08:34:25 +0000491
492 unsigned GlobalBaseReg;
493 bool GlobalBaseInitialized;
Nate Begemanc7bd4822005-04-11 06:34:10 +0000494 bool RecordSuccess;
Nate Begemana9795f82005-03-24 04:41:43 +0000495public:
Nate Begeman815d6da2005-04-06 00:25:27 +0000496 ISel(TargetMachine &TM) : SelectionDAGISel(PPC32Lowering), PPC32Lowering(TM),
497 ISelDAG(0) {}
Nate Begemana9795f82005-03-24 04:41:43 +0000498
Nate Begemanc7b09f12005-03-25 08:34:25 +0000499 /// runOnFunction - Override this function in order to reset our per-function
500 /// variables.
501 virtual bool runOnFunction(Function &Fn) {
502 // Make sure we re-emit a set of the global base reg if necessary
503 GlobalBaseInitialized = false;
504 return SelectionDAGISel::runOnFunction(Fn);
505 }
506
Nate Begemana9795f82005-03-24 04:41:43 +0000507 /// InstructionSelectBasicBlock - This callback is invoked by
508 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
509 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG) {
510 DEBUG(BB->dump());
511 // Codegen the basic block.
Nate Begeman815d6da2005-04-06 00:25:27 +0000512 ISelDAG = &DAG;
Nate Begemana9795f82005-03-24 04:41:43 +0000513 Select(DAG.getRoot());
514
515 // Clear state used for selection.
516 ExprMap.clear();
Nate Begeman815d6da2005-04-06 00:25:27 +0000517 ISelDAG = 0;
Nate Begemana9795f82005-03-24 04:41:43 +0000518 }
Nate Begeman815d6da2005-04-06 00:25:27 +0000519
520 // dag -> dag expanders for integer divide by constant
521 SDOperand BuildSDIVSequence(SDOperand N);
522 SDOperand BuildUDIVSequence(SDOperand N);
Nate Begemana9795f82005-03-24 04:41:43 +0000523
Nate Begemandffcfcc2005-04-01 00:32:34 +0000524 unsigned getGlobalBaseReg();
Nate Begeman6b559972005-04-01 02:59:27 +0000525 unsigned getConstDouble(double floatVal, unsigned Result);
Nate Begeman7ddecb42005-04-06 23:51:40 +0000526 bool SelectBitfieldInsert(SDOperand OR, unsigned Result);
Nate Begeman3664cef2005-04-13 22:14:14 +0000527 unsigned FoldIfWideZeroExtend(SDOperand N);
Nate Begeman1b7f7fb2005-04-13 23:15:44 +0000528 unsigned SelectCC(SDOperand CC, unsigned &Opc);
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:
Chris Lattner519f40b2005-04-13 02:46:17 +0000667 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 Begeman3664cef2005-04-13 22:14:14 +0000967/// FoldIfWideZeroExtend - 32 bit PowerPC implicit masks shift amounts to the
968/// low six bits. If the shift amount is an ISD::AND node with a mask that is
969/// wider than the implicit mask, then we can get rid of the AND and let the
970/// shift do the mask.
971unsigned ISel::FoldIfWideZeroExtend(SDOperand N) {
972 unsigned C;
973 if (N.getOpcode() == ISD::AND &&
974 5 == getImmediateForOpcode(N.getOperand(1), ISD::AND, C) && // isMask
975 31 == (C & 0xFFFF) && // ME
976 26 >= (C >> 16)) // MB
977 return SelectExpr(N.getOperand(0));
978 else
979 return SelectExpr(N);
980}
981
Nate Begeman1b7f7fb2005-04-13 23:15:44 +0000982unsigned ISel::SelectCC(SDOperand CC, unsigned &Opc) {
983 unsigned Result, Tmp1, Tmp2;
Nate Begeman9765c252005-04-12 21:22:28 +0000984 bool AlreadySelected = false;
Nate Begemandffcfcc2005-04-01 00:32:34 +0000985 static const unsigned CompareOpcodes[] =
986 { PPC::FCMPU, PPC::FCMPU, PPC::CMPW, PPC::CMPLW };
987
Nate Begeman1b7f7fb2005-04-13 23:15:44 +0000988 // Allocate a condition register for this expression
989 Result = RegMap->createVirtualRegister(PPC32::CRRCRegisterClass);
990
Nate Begemandffcfcc2005-04-01 00:32:34 +0000991 // If the first operand to the select is a SETCC node, then we can fold it
992 // into the branch that selects which value to return.
993 SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(CC.Val);
994 if (SetCC && CC.getOpcode() == ISD::SETCC) {
995 bool U;
996 Opc = getBCCForSetCC(SetCC->getCondition(), U);
Nate Begemandffcfcc2005-04-01 00:32:34 +0000997
Nate Begeman439b4442005-04-05 04:22:58 +0000998 // Pass the optional argument U to getImmediateForOpcode for SETCC,
Nate Begemandffcfcc2005-04-01 00:32:34 +0000999 // so that it knows whether the SETCC immediate range is signed or not.
Nate Begeman439b4442005-04-05 04:22:58 +00001000 if (1 == getImmediateForOpcode(SetCC->getOperand(1), ISD::SETCC,
1001 Tmp2, U)) {
Nate Begemanc7bd4822005-04-11 06:34:10 +00001002 // For comparisons against zero, we can implicity set CR0 if a recording
1003 // variant (e.g. 'or.' instead of 'or') of the instruction that defines
1004 // operand zero of the SetCC node is available.
1005 if (0 == Tmp2 &&
Nate Begeman9765c252005-04-12 21:22:28 +00001006 NodeHasRecordingVariant(SetCC->getOperand(0).getOpcode()) &&
1007 SetCC->getOperand(0).Val->hasOneUse()) {
Nate Begemanc7bd4822005-04-11 06:34:10 +00001008 RecordSuccess = false;
1009 Tmp1 = SelectExpr(SetCC->getOperand(0), true);
1010 if (RecordSuccess) {
1011 ++Recorded;
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001012 return PPC::CR0;
Nate Begemanc7bd4822005-04-11 06:34:10 +00001013 }
1014 AlreadySelected = true;
1015 }
1016 // If we could not implicitly set CR0, then emit a compare immediate
1017 // instead.
1018 if (!AlreadySelected) Tmp1 = SelectExpr(SetCC->getOperand(0));
Nate Begemandffcfcc2005-04-01 00:32:34 +00001019 if (U)
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001020 BuildMI(BB, PPC::CMPLWI, 2, Result).addReg(Tmp1).addImm(Tmp2);
Nate Begemandffcfcc2005-04-01 00:32:34 +00001021 else
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001022 BuildMI(BB, PPC::CMPWI, 2, Result).addReg(Tmp1).addSImm(Tmp2);
Nate Begemandffcfcc2005-04-01 00:32:34 +00001023 } else {
1024 bool IsInteger = MVT::isInteger(SetCC->getOperand(0).getValueType());
1025 unsigned CompareOpc = CompareOpcodes[2 * IsInteger + U];
Nate Begemanc7bd4822005-04-11 06:34:10 +00001026 Tmp1 = SelectExpr(SetCC->getOperand(0));
Nate Begemandffcfcc2005-04-01 00:32:34 +00001027 Tmp2 = SelectExpr(SetCC->getOperand(1));
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001028 BuildMI(BB, CompareOpc, 2, Result).addReg(Tmp1).addReg(Tmp2);
Nate Begemandffcfcc2005-04-01 00:32:34 +00001029 }
1030 } else {
Nate Begeman9765c252005-04-12 21:22:28 +00001031 Opc = PPC::BNE;
Nate Begemandffcfcc2005-04-01 00:32:34 +00001032 Tmp1 = SelectExpr(CC);
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001033 BuildMI(BB, PPC::CMPLWI, 2, Result).addReg(Tmp1).addImm(0);
Nate Begemandffcfcc2005-04-01 00:32:34 +00001034 }
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001035 return Result;
Nate Begemandffcfcc2005-04-01 00:32:34 +00001036}
1037
1038/// Check to see if the load is a constant offset from a base register
Nate Begeman04730362005-04-01 04:45:11 +00001039bool ISel::SelectAddr(SDOperand N, unsigned& Reg, int& offset)
Nate Begemana9795f82005-03-24 04:41:43 +00001040{
Nate Begeman96fc6812005-03-31 02:05:53 +00001041 unsigned imm = 0, opcode = N.getOpcode();
Nate Begeman04730362005-04-01 04:45:11 +00001042 if (N.getOpcode() == ISD::ADD) {
1043 Reg = SelectExpr(N.getOperand(0));
Nate Begeman439b4442005-04-05 04:22:58 +00001044 if (1 == getImmediateForOpcode(N.getOperand(1), opcode, imm)) {
Nate Begeman96fc6812005-03-31 02:05:53 +00001045 offset = imm;
Nate Begeman04730362005-04-01 04:45:11 +00001046 return false;
1047 }
1048 offset = SelectExpr(N.getOperand(1));
1049 return true;
1050 }
Nate Begemana9795f82005-03-24 04:41:43 +00001051 Reg = SelectExpr(N);
1052 offset = 0;
Nate Begeman04730362005-04-01 04:45:11 +00001053 return false;
Nate Begemana9795f82005-03-24 04:41:43 +00001054}
1055
1056void ISel::SelectBranchCC(SDOperand N)
1057{
Nate Begemana9795f82005-03-24 04:41:43 +00001058 MachineBasicBlock *Dest =
1059 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
Nate Begeman3e897162005-03-31 23:55:40 +00001060
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001061 unsigned Opc, CCReg;
Nate Begemana9795f82005-03-24 04:41:43 +00001062 Select(N.getOperand(0)); //chain
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001063 CCReg = SelectCC(N.getOperand(1), Opc);
Nate Begemancd08e4c2005-04-09 20:09:12 +00001064
1065 // Iterate to the next basic block, unless we're already at the end of the
1066 ilist<MachineBasicBlock>::iterator It = BB, E = BB->getParent()->end();
Nate Begeman706471e2005-04-09 23:35:05 +00001067 if (++It == E) It = BB;
Nate Begemancd08e4c2005-04-09 20:09:12 +00001068
1069 // If this is a two way branch, then grab the fallthrough basic block argument
1070 // and build a PowerPC branch pseudo-op, suitable for long branch conversion
1071 // if necessary by the branch selection pass. Otherwise, emit a standard
1072 // conditional branch.
1073 if (N.getOpcode() == ISD::BRCONDTWOWAY) {
1074 MachineBasicBlock *Fallthrough =
1075 cast<BasicBlockSDNode>(N.getOperand(3))->getBasicBlock();
1076 if (Dest != It) {
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001077 BuildMI(BB, PPC::COND_BRANCH, 4).addReg(CCReg).addImm(Opc)
Nate Begemancd08e4c2005-04-09 20:09:12 +00001078 .addMBB(Dest).addMBB(Fallthrough);
1079 if (Fallthrough != It)
1080 BuildMI(BB, PPC::B, 1).addMBB(Fallthrough);
1081 } else {
1082 if (Fallthrough != It) {
1083 Opc = PPC32InstrInfo::invertPPCBranchOpcode(Opc);
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001084 BuildMI(BB, PPC::COND_BRANCH, 4).addReg(CCReg).addImm(Opc)
Nate Begemancd08e4c2005-04-09 20:09:12 +00001085 .addMBB(Fallthrough).addMBB(Dest);
1086 }
1087 }
1088 } else {
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001089 BuildMI(BB, PPC::COND_BRANCH, 4).addReg(CCReg).addImm(Opc)
Nate Begeman27499e32005-04-10 01:48:29 +00001090 .addMBB(Dest).addMBB(It);
Nate Begemancd08e4c2005-04-09 20:09:12 +00001091 }
Nate Begemana9795f82005-03-24 04:41:43 +00001092 return;
1093}
1094
1095unsigned ISel::SelectExprFP(SDOperand N, unsigned Result)
1096{
1097 unsigned Tmp1, Tmp2, Tmp3;
1098 unsigned Opc = 0;
1099 SDNode *Node = N.Val;
1100 MVT::ValueType DestType = N.getValueType();
1101 unsigned opcode = N.getOpcode();
1102
1103 switch (opcode) {
1104 default:
1105 Node->dump();
1106 assert(0 && "Node not handled!\n");
1107
Nate Begeman23afcfb2005-03-29 22:48:55 +00001108 case ISD::SELECT: {
Nate Begeman3e897162005-03-31 23:55:40 +00001109 // Attempt to generate FSEL. We can do this whenever we have an FP result,
1110 // and an FP comparison in the SetCC node.
1111 SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(N.getOperand(0).Val);
1112 if (SetCC && N.getOperand(0).getOpcode() == ISD::SETCC &&
1113 !MVT::isInteger(SetCC->getOperand(0).getValueType()) &&
1114 SetCC->getCondition() != ISD::SETEQ &&
1115 SetCC->getCondition() != ISD::SETNE) {
1116 MVT::ValueType VT = SetCC->getOperand(0).getValueType();
Nate Begeman3e897162005-03-31 23:55:40 +00001117 unsigned TV = SelectExpr(N.getOperand(1)); // Use if TRUE
1118 unsigned FV = SelectExpr(N.getOperand(2)); // Use if FALSE
1119
1120 ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(1));
1121 if (CN && (CN->isExactlyValue(-0.0) || CN->isExactlyValue(0.0))) {
1122 switch(SetCC->getCondition()) {
1123 default: assert(0 && "Invalid FSEL condition"); abort();
1124 case ISD::SETULT:
1125 case ISD::SETLT:
Nate Begemanaf4ab1b2005-04-09 09:33:07 +00001126 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
Nate Begeman3e897162005-03-31 23:55:40 +00001127 case ISD::SETUGE:
1128 case ISD::SETGE:
Nate Begemanaf4ab1b2005-04-09 09:33:07 +00001129 Tmp1 = SelectExpr(SetCC->getOperand(0)); // Val to compare against
Nate Begeman3e897162005-03-31 23:55:40 +00001130 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp1).addReg(TV).addReg(FV);
1131 return Result;
1132 case ISD::SETUGT:
Nate Begemanaf4ab1b2005-04-09 09:33:07 +00001133 case ISD::SETGT:
1134 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
Nate Begeman3e897162005-03-31 23:55:40 +00001135 case ISD::SETULE:
1136 case ISD::SETLE: {
Nate Begemanaf4ab1b2005-04-09 09:33:07 +00001137 if (SetCC->getOperand(0).getOpcode() == ISD::FNEG) {
1138 Tmp2 = SelectExpr(SetCC->getOperand(0).getOperand(0));
1139 } else {
1140 Tmp2 = MakeReg(VT);
1141 Tmp1 = SelectExpr(SetCC->getOperand(0)); // Val to compare against
1142 BuildMI(BB, PPC::FNEG, 1, Tmp2).addReg(Tmp1);
1143 }
Nate Begeman3e897162005-03-31 23:55:40 +00001144 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp2).addReg(TV).addReg(FV);
1145 return Result;
1146 }
1147 }
1148 } else {
1149 Opc = (MVT::f64 == VT) ? PPC::FSUB : PPC::FSUBS;
Nate Begemanaf4ab1b2005-04-09 09:33:07 +00001150 Tmp1 = SelectExpr(SetCC->getOperand(0)); // Val to compare against
Nate Begeman3e897162005-03-31 23:55:40 +00001151 Tmp2 = SelectExpr(SetCC->getOperand(1));
1152 Tmp3 = MakeReg(VT);
1153 switch(SetCC->getCondition()) {
1154 default: assert(0 && "Invalid FSEL condition"); abort();
1155 case ISD::SETULT:
1156 case ISD::SETLT:
1157 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
1158 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(FV).addReg(TV);
1159 return Result;
1160 case ISD::SETUGE:
1161 case ISD::SETGE:
1162 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
1163 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(TV).addReg(FV);
1164 return Result;
1165 case ISD::SETUGT:
1166 case ISD::SETGT:
1167 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp2).addReg(Tmp1);
1168 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(FV).addReg(TV);
1169 return Result;
1170 case ISD::SETULE:
1171 case ISD::SETLE:
1172 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp2).addReg(Tmp1);
1173 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(TV).addReg(FV);
1174 return Result;
1175 }
1176 }
1177 assert(0 && "Should never get here");
1178 return 0;
1179 }
1180
Nate Begeman31318e42005-04-01 07:21:30 +00001181 unsigned TrueValue = SelectExpr(N.getOperand(1)); //Use if TRUE
1182 unsigned FalseValue = SelectExpr(N.getOperand(2)); //Use if FALSE
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001183 unsigned CCReg = SelectCC(N.getOperand(0), Opc);
Nate Begeman31318e42005-04-01 07:21:30 +00001184
Nate Begeman23afcfb2005-03-29 22:48:55 +00001185 // Create an iterator with which to insert the MBB for copying the false
1186 // value and the MBB to hold the PHI instruction for this SetCC.
1187 MachineBasicBlock *thisMBB = BB;
1188 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1189 ilist<MachineBasicBlock>::iterator It = BB;
1190 ++It;
1191
1192 // thisMBB:
1193 // ...
1194 // TrueVal = ...
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001195 // cmpTY ccX, r1, r2
Nate Begeman23afcfb2005-03-29 22:48:55 +00001196 // bCC copy1MBB
1197 // fallthrough --> copy0MBB
Nate Begeman23afcfb2005-03-29 22:48:55 +00001198 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
1199 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001200 BuildMI(BB, Opc, 2).addReg(CCReg).addMBB(sinkMBB);
Nate Begeman23afcfb2005-03-29 22:48:55 +00001201 MachineFunction *F = BB->getParent();
1202 F->getBasicBlockList().insert(It, copy0MBB);
1203 F->getBasicBlockList().insert(It, sinkMBB);
1204 // Update machine-CFG edges
1205 BB->addSuccessor(copy0MBB);
1206 BB->addSuccessor(sinkMBB);
1207
1208 // copy0MBB:
1209 // %FalseValue = ...
1210 // # fallthrough to sinkMBB
1211 BB = copy0MBB;
Nate Begeman23afcfb2005-03-29 22:48:55 +00001212 // Update machine-CFG edges
1213 BB->addSuccessor(sinkMBB);
1214
1215 // sinkMBB:
1216 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1217 // ...
1218 BB = sinkMBB;
1219 BuildMI(BB, PPC::PHI, 4, Result).addReg(FalseValue)
1220 .addMBB(copy0MBB).addReg(TrueValue).addMBB(thisMBB);
1221 return Result;
1222 }
Nate Begeman27eeb002005-04-02 05:59:34 +00001223
1224 case ISD::FNEG:
Nate Begeman93075ec2005-04-04 23:40:36 +00001225 if (!NoExcessFPPrecision &&
1226 ISD::ADD == N.getOperand(0).getOpcode() &&
1227 N.getOperand(0).Val->hasOneUse() &&
1228 ISD::MUL == N.getOperand(0).getOperand(0).getOpcode() &&
1229 N.getOperand(0).getOperand(0).Val->hasOneUse()) {
Nate Begeman80196b12005-04-05 00:15:08 +00001230 ++FusedFP; // Statistic
Nate Begeman93075ec2005-04-04 23:40:36 +00001231 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(0));
1232 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(1));
1233 Tmp3 = SelectExpr(N.getOperand(0).getOperand(1));
1234 Opc = DestType == MVT::f64 ? PPC::FNMADD : PPC::FNMADDS;
1235 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1236 } else if (!NoExcessFPPrecision &&
Nate Begemane88aa5b2005-04-09 03:05:51 +00001237 ISD::ADD == N.getOperand(0).getOpcode() &&
Nate Begeman93075ec2005-04-04 23:40:36 +00001238 N.getOperand(0).Val->hasOneUse() &&
Nate Begemane88aa5b2005-04-09 03:05:51 +00001239 ISD::MUL == N.getOperand(0).getOperand(1).getOpcode() &&
1240 N.getOperand(0).getOperand(1).Val->hasOneUse()) {
Nate Begeman80196b12005-04-05 00:15:08 +00001241 ++FusedFP; // Statistic
Nate Begemane88aa5b2005-04-09 03:05:51 +00001242 Tmp1 = SelectExpr(N.getOperand(0).getOperand(1).getOperand(0));
1243 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1).getOperand(1));
1244 Tmp3 = SelectExpr(N.getOperand(0).getOperand(0));
1245 Opc = DestType == MVT::f64 ? PPC::FNMADD : PPC::FNMADDS;
Nate Begeman93075ec2005-04-04 23:40:36 +00001246 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1247 } else if (ISD::FABS == N.getOperand(0).getOpcode()) {
Nate Begeman27eeb002005-04-02 05:59:34 +00001248 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1249 BuildMI(BB, PPC::FNABS, 1, Result).addReg(Tmp1);
1250 } else {
1251 Tmp1 = SelectExpr(N.getOperand(0));
1252 BuildMI(BB, PPC::FNEG, 1, Result).addReg(Tmp1);
1253 }
1254 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001255
Nate Begeman27eeb002005-04-02 05:59:34 +00001256 case ISD::FABS:
1257 Tmp1 = SelectExpr(N.getOperand(0));
1258 BuildMI(BB, PPC::FABS, 1, Result).addReg(Tmp1);
1259 return Result;
1260
Nate Begemana9795f82005-03-24 04:41:43 +00001261 case ISD::FP_ROUND:
1262 assert (DestType == MVT::f32 &&
1263 N.getOperand(0).getValueType() == MVT::f64 &&
1264 "only f64 to f32 conversion supported here");
1265 Tmp1 = SelectExpr(N.getOperand(0));
1266 BuildMI(BB, PPC::FRSP, 1, Result).addReg(Tmp1);
1267 return Result;
1268
1269 case ISD::FP_EXTEND:
1270 assert (DestType == MVT::f64 &&
1271 N.getOperand(0).getValueType() == MVT::f32 &&
1272 "only f32 to f64 conversion supported here");
1273 Tmp1 = SelectExpr(N.getOperand(0));
1274 BuildMI(BB, PPC::FMR, 1, Result).addReg(Tmp1);
1275 return Result;
1276
1277 case ISD::CopyFromReg:
Nate Begemanf2622612005-03-26 02:17:46 +00001278 if (Result == 1)
1279 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1280 Tmp1 = dyn_cast<RegSDNode>(Node)->getReg();
1281 BuildMI(BB, PPC::FMR, 1, Result).addReg(Tmp1);
1282 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001283
Nate Begeman6d369cc2005-04-01 01:08:07 +00001284 case ISD::ConstantFP: {
Nate Begeman6d369cc2005-04-01 01:08:07 +00001285 ConstantFPSDNode *CN = cast<ConstantFPSDNode>(N);
Nate Begeman6b559972005-04-01 02:59:27 +00001286 Result = getConstDouble(CN->getValue(), Result);
Nate Begeman6d369cc2005-04-01 01:08:07 +00001287 return Result;
1288 }
Nate Begemana9795f82005-03-24 04:41:43 +00001289
Nate Begemana9795f82005-03-24 04:41:43 +00001290 case ISD::ADD:
Nate Begeman93075ec2005-04-04 23:40:36 +00001291 if (!NoExcessFPPrecision && N.getOperand(0).getOpcode() == ISD::MUL &&
1292 N.getOperand(0).Val->hasOneUse()) {
1293 ++FusedFP; // Statistic
1294 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1295 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1296 Tmp3 = SelectExpr(N.getOperand(1));
1297 Opc = DestType == MVT::f64 ? PPC::FMADD : PPC::FMADDS;
1298 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1299 return Result;
1300 }
Nate Begemane88aa5b2005-04-09 03:05:51 +00001301 if (!NoExcessFPPrecision && N.getOperand(1).getOpcode() == ISD::MUL &&
1302 N.getOperand(1).Val->hasOneUse()) {
1303 ++FusedFP; // Statistic
1304 Tmp1 = SelectExpr(N.getOperand(1).getOperand(0));
1305 Tmp2 = SelectExpr(N.getOperand(1).getOperand(1));
1306 Tmp3 = SelectExpr(N.getOperand(0));
1307 Opc = DestType == MVT::f64 ? PPC::FMADD : PPC::FMADDS;
1308 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1309 return Result;
1310 }
Nate Begeman93075ec2005-04-04 23:40:36 +00001311 Opc = DestType == MVT::f64 ? PPC::FADD : PPC::FADDS;
1312 Tmp1 = SelectExpr(N.getOperand(0));
1313 Tmp2 = SelectExpr(N.getOperand(1));
1314 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1315 return Result;
1316
Nate Begemana9795f82005-03-24 04:41:43 +00001317 case ISD::SUB:
Nate Begeman93075ec2005-04-04 23:40:36 +00001318 if (!NoExcessFPPrecision && N.getOperand(0).getOpcode() == ISD::MUL &&
1319 N.getOperand(0).Val->hasOneUse()) {
1320 ++FusedFP; // Statistic
1321 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1322 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1323 Tmp3 = SelectExpr(N.getOperand(1));
1324 Opc = DestType == MVT::f64 ? PPC::FMSUB : PPC::FMSUBS;
1325 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1326 return Result;
1327 }
Nate Begemane88aa5b2005-04-09 03:05:51 +00001328 if (!NoExcessFPPrecision && N.getOperand(1).getOpcode() == ISD::MUL &&
1329 N.getOperand(1).Val->hasOneUse()) {
1330 ++FusedFP; // Statistic
1331 Tmp1 = SelectExpr(N.getOperand(1).getOperand(0));
1332 Tmp2 = SelectExpr(N.getOperand(1).getOperand(1));
1333 Tmp3 = SelectExpr(N.getOperand(0));
1334 Opc = DestType == MVT::f64 ? PPC::FNMSUB : PPC::FNMSUBS;
1335 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1336 return Result;
1337 }
Nate Begeman93075ec2005-04-04 23:40:36 +00001338 Opc = DestType == MVT::f64 ? PPC::FSUB : PPC::FSUBS;
1339 Tmp1 = SelectExpr(N.getOperand(0));
1340 Tmp2 = SelectExpr(N.getOperand(1));
1341 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1342 return Result;
1343
1344 case ISD::MUL:
Nate Begemana9795f82005-03-24 04:41:43 +00001345 case ISD::SDIV:
1346 switch( opcode ) {
1347 case ISD::MUL: Opc = DestType == MVT::f64 ? PPC::FMUL : PPC::FMULS; break;
Nate Begemana9795f82005-03-24 04:41:43 +00001348 case ISD::SDIV: Opc = DestType == MVT::f64 ? PPC::FDIV : PPC::FDIVS; break;
1349 };
Nate Begemana9795f82005-03-24 04:41:43 +00001350 Tmp1 = SelectExpr(N.getOperand(0));
1351 Tmp2 = SelectExpr(N.getOperand(1));
1352 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1353 return Result;
1354
Nate Begemana9795f82005-03-24 04:41:43 +00001355 case ISD::UINT_TO_FP:
Nate Begemanfdcf3412005-03-30 19:38:35 +00001356 case ISD::SINT_TO_FP: {
1357 assert (N.getOperand(0).getValueType() == MVT::i32
1358 && "int to float must operate on i32");
1359 bool IsUnsigned = (ISD::UINT_TO_FP == opcode);
1360 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
1361 Tmp2 = MakeReg(MVT::f64); // temp reg to load the integer value into
1362 Tmp3 = MakeReg(MVT::i32); // temp reg to hold the conversion constant
Nate Begemanfdcf3412005-03-30 19:38:35 +00001363
1364 int FrameIdx = BB->getParent()->getFrameInfo()->CreateStackObject(8, 8);
1365 MachineConstantPool *CP = BB->getParent()->getConstantPool();
1366
Nate Begemanfdcf3412005-03-30 19:38:35 +00001367 if (IsUnsigned) {
Nate Begeman709c8062005-04-10 06:06:10 +00001368 unsigned ConstF = getConstDouble(0x1.000000p52);
Nate Begemanfdcf3412005-03-30 19:38:35 +00001369 // Store the hi & low halves of the fp value, currently in int regs
1370 BuildMI(BB, PPC::LIS, 1, Tmp3).addSImm(0x4330);
1371 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(Tmp3), FrameIdx);
1372 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(Tmp1), FrameIdx, 4);
1373 addFrameReference(BuildMI(BB, PPC::LFD, 2, Tmp2), FrameIdx);
1374 // Generate the return value with a subtract
1375 BuildMI(BB, PPC::FSUB, 2, Result).addReg(Tmp2).addReg(ConstF);
1376 } else {
Nate Begeman709c8062005-04-10 06:06:10 +00001377 unsigned ConstF = getConstDouble(0x1.000008p52);
Nate Begemanfdcf3412005-03-30 19:38:35 +00001378 unsigned TmpL = MakeReg(MVT::i32);
Nate Begemanfdcf3412005-03-30 19:38:35 +00001379 // Store the hi & low halves of the fp value, currently in int regs
1380 BuildMI(BB, PPC::LIS, 1, Tmp3).addSImm(0x4330);
1381 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(Tmp3), FrameIdx);
1382 BuildMI(BB, PPC::XORIS, 2, TmpL).addReg(Tmp1).addImm(0x8000);
1383 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(TmpL), FrameIdx, 4);
1384 addFrameReference(BuildMI(BB, PPC::LFD, 2, Tmp2), FrameIdx);
1385 // Generate the return value with a subtract
1386 BuildMI(BB, PPC::FSUB, 2, Result).addReg(Tmp2).addReg(ConstF);
1387 }
1388 return Result;
1389 }
Nate Begemana9795f82005-03-24 04:41:43 +00001390 }
Nate Begeman6b559972005-04-01 02:59:27 +00001391 assert(0 && "Should never get here");
Nate Begemana9795f82005-03-24 04:41:43 +00001392 return 0;
1393}
1394
Nate Begemanc7bd4822005-04-11 06:34:10 +00001395unsigned ISel::SelectExpr(SDOperand N, bool Recording) {
Nate Begemana9795f82005-03-24 04:41:43 +00001396 unsigned Result;
1397 unsigned Tmp1, Tmp2, Tmp3;
1398 unsigned Opc = 0;
1399 unsigned opcode = N.getOpcode();
1400
1401 SDNode *Node = N.Val;
1402 MVT::ValueType DestType = N.getValueType();
1403
1404 unsigned &Reg = ExprMap[N];
1405 if (Reg) return Reg;
1406
Nate Begeman27eeb002005-04-02 05:59:34 +00001407 switch (N.getOpcode()) {
1408 default:
Nate Begemana9795f82005-03-24 04:41:43 +00001409 Reg = Result = (N.getValueType() != MVT::Other) ?
Nate Begeman27eeb002005-04-02 05:59:34 +00001410 MakeReg(N.getValueType()) : 1;
1411 break;
1412 case ISD::CALL:
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001413 // If this is a call instruction, make sure to prepare ALL of the result
1414 // values as well as the chain.
Nate Begeman27eeb002005-04-02 05:59:34 +00001415 if (Node->getNumValues() == 1)
1416 Reg = Result = 1; // Void call, just a chain.
1417 else {
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001418 Result = MakeReg(Node->getValueType(0));
1419 ExprMap[N.getValue(0)] = Result;
Nate Begeman27eeb002005-04-02 05:59:34 +00001420 for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001421 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
Nate Begeman27eeb002005-04-02 05:59:34 +00001422 ExprMap[SDOperand(Node, Node->getNumValues()-1)] = 1;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001423 }
Nate Begeman27eeb002005-04-02 05:59:34 +00001424 break;
1425 case ISD::ADD_PARTS:
1426 case ISD::SUB_PARTS:
1427 case ISD::SHL_PARTS:
1428 case ISD::SRL_PARTS:
1429 case ISD::SRA_PARTS:
1430 Result = MakeReg(Node->getValueType(0));
1431 ExprMap[N.getValue(0)] = Result;
1432 for (unsigned i = 1, e = N.Val->getNumValues(); i != e; ++i)
1433 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
1434 break;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001435 }
1436
Nate Begemane5846682005-04-04 06:52:38 +00001437 if (ISD::CopyFromReg == opcode)
1438 DestType = N.getValue(0).getValueType();
1439
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001440 if (DestType == MVT::f64 || DestType == MVT::f32)
Nate Begemana0e3e942005-04-10 01:14:13 +00001441 if (ISD::LOAD != opcode && ISD::EXTLOAD != opcode &&
1442 ISD::UNDEF != opcode && ISD::CALL != opcode)
Nate Begeman74d73452005-03-31 00:15:26 +00001443 return SelectExprFP(N, Result);
Nate Begemana9795f82005-03-24 04:41:43 +00001444
1445 switch (opcode) {
1446 default:
1447 Node->dump();
1448 assert(0 && "Node not handled!\n");
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001449 case ISD::UNDEF:
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001450 BuildMI(BB, PPC::IMPLICIT_DEF, 0, Result);
1451 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001452 case ISD::DYNAMIC_STACKALLOC:
Nate Begeman5e966612005-03-24 06:28:42 +00001453 // Generate both result values. FIXME: Need a better commment here?
1454 if (Result != 1)
1455 ExprMap[N.getValue(1)] = 1;
1456 else
1457 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1458
1459 // FIXME: We are currently ignoring the requested alignment for handling
1460 // greater than the stack alignment. This will need to be revisited at some
1461 // point. Align = N.getOperand(2);
1462 if (!isa<ConstantSDNode>(N.getOperand(2)) ||
1463 cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) {
1464 std::cerr << "Cannot allocate stack object with greater alignment than"
1465 << " the stack alignment yet!";
1466 abort();
1467 }
1468 Select(N.getOperand(0));
1469 Tmp1 = SelectExpr(N.getOperand(1));
1470 // Subtract size from stack pointer, thereby allocating some space.
1471 BuildMI(BB, PPC::SUBF, 2, PPC::R1).addReg(Tmp1).addReg(PPC::R1);
1472 // Put a pointer to the space into the result register by copying the SP
1473 BuildMI(BB, PPC::OR, 2, Result).addReg(PPC::R1).addReg(PPC::R1);
1474 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001475
1476 case ISD::ConstantPool:
Nate Begemanca12a2b2005-03-28 22:28:37 +00001477 Tmp1 = cast<ConstantPoolSDNode>(N)->getIndex();
1478 Tmp2 = MakeReg(MVT::i32);
1479 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp2).addReg(getGlobalBaseReg())
1480 .addConstantPoolIndex(Tmp1);
1481 BuildMI(BB, PPC::LA, 2, Result).addReg(Tmp2).addConstantPoolIndex(Tmp1);
1482 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001483
1484 case ISD::FrameIndex:
Nate Begemanf3d08f32005-03-29 00:03:27 +00001485 Tmp1 = cast<FrameIndexSDNode>(N)->getIndex();
Nate Begeman58f718c2005-03-30 02:23:08 +00001486 addFrameReference(BuildMI(BB, PPC::ADDI, 2, Result), (int)Tmp1, 0, false);
Nate Begemanf3d08f32005-03-29 00:03:27 +00001487 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001488
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001489 case ISD::GlobalAddress: {
1490 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
Nate Begemanca12a2b2005-03-28 22:28:37 +00001491 Tmp1 = MakeReg(MVT::i32);
Nate Begemanc7b09f12005-03-25 08:34:25 +00001492 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp1).addReg(getGlobalBaseReg())
1493 .addGlobalAddress(GV);
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001494 if (GV->hasWeakLinkage() || GV->isExternal()) {
1495 BuildMI(BB, PPC::LWZ, 2, Result).addGlobalAddress(GV).addReg(Tmp1);
1496 } else {
1497 BuildMI(BB, PPC::LA, 2, Result).addReg(Tmp1).addGlobalAddress(GV);
1498 }
1499 return Result;
1500 }
1501
Nate Begeman5e966612005-03-24 06:28:42 +00001502 case ISD::LOAD:
Nate Begemana9795f82005-03-24 04:41:43 +00001503 case ISD::EXTLOAD:
1504 case ISD::ZEXTLOAD:
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001505 case ISD::SEXTLOAD: {
Nate Begeman9db505c2005-03-28 19:36:43 +00001506 MVT::ValueType TypeBeingLoaded = (ISD::LOAD == opcode) ?
1507 Node->getValueType(0) : cast<MVTSDNode>(Node)->getExtraValueType();
Nate Begeman74d73452005-03-31 00:15:26 +00001508 bool sext = (ISD::SEXTLOAD == opcode);
Nate Begeman74d73452005-03-31 00:15:26 +00001509
Nate Begeman5e966612005-03-24 06:28:42 +00001510 // Make sure we generate both values.
1511 if (Result != 1)
1512 ExprMap[N.getValue(1)] = 1; // Generate the token
1513 else
1514 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1515
1516 SDOperand Chain = N.getOperand(0);
1517 SDOperand Address = N.getOperand(1);
1518 Select(Chain);
1519
Nate Begeman9db505c2005-03-28 19:36:43 +00001520 switch (TypeBeingLoaded) {
Nate Begeman74d73452005-03-31 00:15:26 +00001521 default: Node->dump(); assert(0 && "Cannot load this type!");
Nate Begeman9db505c2005-03-28 19:36:43 +00001522 case MVT::i1: Opc = PPC::LBZ; break;
1523 case MVT::i8: Opc = PPC::LBZ; break;
1524 case MVT::i16: Opc = sext ? PPC::LHA : PPC::LHZ; break;
1525 case MVT::i32: Opc = PPC::LWZ; break;
Nate Begeman74d73452005-03-31 00:15:26 +00001526 case MVT::f32: Opc = PPC::LFS; break;
1527 case MVT::f64: Opc = PPC::LFD; break;
Nate Begeman5e966612005-03-24 06:28:42 +00001528 }
1529
Nate Begeman74d73452005-03-31 00:15:26 +00001530 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Address)) {
1531 Tmp1 = MakeReg(MVT::i32);
1532 int CPI = CP->getIndex();
1533 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp1).addReg(getGlobalBaseReg())
1534 .addConstantPoolIndex(CPI);
1535 BuildMI(BB, Opc, 2, Result).addConstantPoolIndex(CPI).addReg(Tmp1);
Nate Begeman9db505c2005-03-28 19:36:43 +00001536 }
Nate Begeman74d73452005-03-31 00:15:26 +00001537 else if(Address.getOpcode() == ISD::FrameIndex) {
Nate Begeman58f718c2005-03-30 02:23:08 +00001538 Tmp1 = cast<FrameIndexSDNode>(Address)->getIndex();
1539 addFrameReference(BuildMI(BB, Opc, 2, Result), (int)Tmp1);
Nate Begeman5e966612005-03-24 06:28:42 +00001540 } else {
1541 int offset;
Nate Begeman04730362005-04-01 04:45:11 +00001542 bool idx = SelectAddr(Address, Tmp1, offset);
1543 if (idx) {
1544 Opc = IndexedOpForOp(Opc);
1545 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(offset);
1546 } else {
1547 BuildMI(BB, Opc, 2, Result).addSImm(offset).addReg(Tmp1);
1548 }
Nate Begeman5e966612005-03-24 06:28:42 +00001549 }
1550 return Result;
1551 }
1552
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001553 case ISD::CALL: {
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001554 unsigned GPR_idx = 0, FPR_idx = 0;
1555 static const unsigned GPR[] = {
1556 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
1557 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
1558 };
1559 static const unsigned FPR[] = {
1560 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
1561 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
1562 };
1563
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001564 // Lower the chain for this call.
1565 Select(N.getOperand(0));
1566 ExprMap[N.getValue(Node->getNumValues()-1)] = 1;
Nate Begeman74d73452005-03-31 00:15:26 +00001567
Nate Begemand860aa62005-04-04 22:17:48 +00001568 MachineInstr *CallMI;
1569 // Emit the correct call instruction based on the type of symbol called.
1570 if (GlobalAddressSDNode *GASD =
1571 dyn_cast<GlobalAddressSDNode>(N.getOperand(1))) {
1572 CallMI = BuildMI(PPC::CALLpcrel, 1).addGlobalAddress(GASD->getGlobal(),
1573 true);
1574 } else if (ExternalSymbolSDNode *ESSDN =
1575 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1))) {
1576 CallMI = BuildMI(PPC::CALLpcrel, 1).addExternalSymbol(ESSDN->getSymbol(),
1577 true);
1578 } else {
1579 Tmp1 = SelectExpr(N.getOperand(1));
1580 BuildMI(BB, PPC::OR, 2, PPC::R12).addReg(Tmp1).addReg(Tmp1);
1581 BuildMI(BB, PPC::MTCTR, 1).addReg(PPC::R12);
1582 CallMI = BuildMI(PPC::CALLindirect, 3).addImm(20).addImm(0)
1583 .addReg(PPC::R12);
1584 }
1585
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001586 // Load the register args to virtual regs
1587 std::vector<unsigned> ArgVR;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001588 for(int i = 2, e = Node->getNumOperands(); i < e; ++i)
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001589 ArgVR.push_back(SelectExpr(N.getOperand(i)));
1590
1591 // Copy the virtual registers into the appropriate argument register
1592 for(int i = 0, e = ArgVR.size(); i < e; ++i) {
1593 switch(N.getOperand(i+2).getValueType()) {
1594 default: Node->dump(); assert(0 && "Unknown value type for call");
1595 case MVT::i1:
1596 case MVT::i8:
1597 case MVT::i16:
1598 case MVT::i32:
1599 assert(GPR_idx < 8 && "Too many int args");
Nate Begemand860aa62005-04-04 22:17:48 +00001600 if (N.getOperand(i+2).getOpcode() != ISD::UNDEF) {
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001601 BuildMI(BB, PPC::OR,2,GPR[GPR_idx]).addReg(ArgVR[i]).addReg(ArgVR[i]);
Nate Begemand860aa62005-04-04 22:17:48 +00001602 CallMI->addRegOperand(GPR[GPR_idx], MachineOperand::Use);
1603 }
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001604 ++GPR_idx;
1605 break;
1606 case MVT::f64:
1607 case MVT::f32:
1608 assert(FPR_idx < 13 && "Too many fp args");
1609 BuildMI(BB, PPC::FMR, 1, FPR[FPR_idx]).addReg(ArgVR[i]);
Nate Begemand860aa62005-04-04 22:17:48 +00001610 CallMI->addRegOperand(FPR[FPR_idx], MachineOperand::Use);
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001611 ++FPR_idx;
1612 break;
1613 }
1614 }
Nate Begemand860aa62005-04-04 22:17:48 +00001615
1616 // Put the call instruction in the correct place in the MachineBasicBlock
1617 BB->push_back(CallMI);
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001618
1619 switch (Node->getValueType(0)) {
1620 default: assert(0 && "Unknown value type for call result!");
1621 case MVT::Other: return 1;
1622 case MVT::i1:
1623 case MVT::i8:
1624 case MVT::i16:
1625 case MVT::i32:
Nate Begemane5846682005-04-04 06:52:38 +00001626 if (Node->getValueType(1) == MVT::i32) {
1627 BuildMI(BB, PPC::OR, 2, Result+1).addReg(PPC::R3).addReg(PPC::R3);
1628 BuildMI(BB, PPC::OR, 2, Result).addReg(PPC::R4).addReg(PPC::R4);
1629 } else {
1630 BuildMI(BB, PPC::OR, 2, Result).addReg(PPC::R3).addReg(PPC::R3);
1631 }
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001632 break;
1633 case MVT::f32:
1634 case MVT::f64:
1635 BuildMI(BB, PPC::FMR, 1, Result).addReg(PPC::F1);
1636 break;
1637 }
1638 return Result+N.ResNo;
1639 }
Nate Begemana9795f82005-03-24 04:41:43 +00001640
1641 case ISD::SIGN_EXTEND:
1642 case ISD::SIGN_EXTEND_INREG:
1643 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman9db505c2005-03-28 19:36:43 +00001644 switch(cast<MVTSDNode>(Node)->getExtraValueType()) {
1645 default: Node->dump(); assert(0 && "Unhandled SIGN_EXTEND type"); break;
Nate Begemanc7bd4822005-04-11 06:34:10 +00001646 case MVT::i16:
Nate Begeman9db505c2005-03-28 19:36:43 +00001647 BuildMI(BB, PPC::EXTSH, 1, Result).addReg(Tmp1);
1648 break;
Nate Begemanc7bd4822005-04-11 06:34:10 +00001649 case MVT::i8:
Nate Begeman9db505c2005-03-28 19:36:43 +00001650 BuildMI(BB, PPC::EXTSB, 1, Result).addReg(Tmp1);
1651 break;
Nate Begeman74747862005-03-29 22:24:51 +00001652 case MVT::i1:
1653 BuildMI(BB, PPC::SUBFIC, 2, Result).addReg(Tmp1).addSImm(0);
1654 break;
Nate Begeman9db505c2005-03-28 19:36:43 +00001655 }
Nate Begemana9795f82005-03-24 04:41:43 +00001656 return Result;
1657
Nate Begemana9795f82005-03-24 04:41:43 +00001658 case ISD::CopyFromReg:
1659 if (Result == 1)
1660 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1661 Tmp1 = dyn_cast<RegSDNode>(Node)->getReg();
1662 BuildMI(BB, PPC::OR, 2, Result).addReg(Tmp1).addReg(Tmp1);
1663 return Result;
1664
1665 case ISD::SHL:
Nate Begeman5e966612005-03-24 06:28:42 +00001666 Tmp1 = SelectExpr(N.getOperand(0));
1667 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1668 Tmp2 = CN->getValue() & 0x1F;
Nate Begeman33162522005-03-29 21:54:38 +00001669 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(Tmp2).addImm(0)
Nate Begeman5e966612005-03-24 06:28:42 +00001670 .addImm(31-Tmp2);
1671 } else {
Nate Begeman3664cef2005-04-13 22:14:14 +00001672 Tmp2 = FoldIfWideZeroExtend(N.getOperand(1));
Nate Begeman5e966612005-03-24 06:28:42 +00001673 BuildMI(BB, PPC::SLW, 2, Result).addReg(Tmp1).addReg(Tmp2);
1674 }
1675 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001676
Nate Begeman5e966612005-03-24 06:28:42 +00001677 case ISD::SRL:
1678 Tmp1 = SelectExpr(N.getOperand(0));
1679 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1680 Tmp2 = CN->getValue() & 0x1F;
Nate Begeman33162522005-03-29 21:54:38 +00001681 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(32-Tmp2)
Nate Begeman5e966612005-03-24 06:28:42 +00001682 .addImm(Tmp2).addImm(31);
1683 } else {
Nate Begeman3664cef2005-04-13 22:14:14 +00001684 Tmp2 = FoldIfWideZeroExtend(N.getOperand(1));
Nate Begeman5e966612005-03-24 06:28:42 +00001685 BuildMI(BB, PPC::SRW, 2, Result).addReg(Tmp1).addReg(Tmp2);
1686 }
1687 return Result;
1688
1689 case ISD::SRA:
1690 Tmp1 = SelectExpr(N.getOperand(0));
1691 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1692 Tmp2 = CN->getValue() & 0x1F;
1693 BuildMI(BB, PPC::SRAWI, 2, Result).addReg(Tmp1).addImm(Tmp2);
1694 } else {
Nate Begeman3664cef2005-04-13 22:14:14 +00001695 Tmp2 = FoldIfWideZeroExtend(N.getOperand(1));
Nate Begeman5e966612005-03-24 06:28:42 +00001696 BuildMI(BB, PPC::SRAW, 2, Result).addReg(Tmp1).addReg(Tmp2);
1697 }
1698 return Result;
1699
Nate Begemana9795f82005-03-24 04:41:43 +00001700 case ISD::ADD:
1701 assert (DestType == MVT::i32 && "Only do arithmetic on i32s!");
1702 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman439b4442005-04-05 04:22:58 +00001703 switch(getImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
Nate Begemana9795f82005-03-24 04:41:43 +00001704 default: assert(0 && "unhandled result code");
1705 case 0: // No immediate
1706 Tmp2 = SelectExpr(N.getOperand(1));
1707 BuildMI(BB, PPC::ADD, 2, Result).addReg(Tmp1).addReg(Tmp2);
1708 break;
1709 case 1: // Low immediate
1710 BuildMI(BB, PPC::ADDI, 2, Result).addReg(Tmp1).addSImm(Tmp2);
1711 break;
1712 case 2: // Shifted immediate
1713 BuildMI(BB, PPC::ADDIS, 2, Result).addReg(Tmp1).addSImm(Tmp2);
1714 break;
1715 }
1716 return Result;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001717
Nate Begemana9795f82005-03-24 04:41:43 +00001718 case ISD::AND:
Nate Begeman7ddecb42005-04-06 23:51:40 +00001719 Tmp1 = SelectExpr(N.getOperand(0));
1720 // FIXME: should add check in getImmediateForOpcode to return a value
1721 // indicating the immediate is a run of set bits so we can emit a bitfield
1722 // clear with RLWINM instead.
1723 switch(getImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
1724 default: assert(0 && "unhandled result code");
1725 case 0: // No immediate
1726 Tmp2 = SelectExpr(N.getOperand(1));
Nate Begemanc7bd4822005-04-11 06:34:10 +00001727 Opc = Recording ? PPC::ANDo : PPC::AND;
1728 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
Nate Begeman7ddecb42005-04-06 23:51:40 +00001729 break;
1730 case 1: // Low immediate
1731 BuildMI(BB, PPC::ANDIo, 2, Result).addReg(Tmp1).addImm(Tmp2);
1732 break;
1733 case 2: // Shifted immediate
1734 BuildMI(BB, PPC::ANDISo, 2, Result).addReg(Tmp1).addImm(Tmp2);
1735 break;
Nate Begeman9f833d32005-04-12 00:10:02 +00001736 case 5: // Bitfield mask
1737 Opc = Recording ? PPC::RLWINMo : PPC::RLWINM;
1738 Tmp3 = Tmp2 >> 16; // MB
1739 Tmp2 &= 0xFFFF; // ME
1740 BuildMI(BB, Opc, 4, Result).addReg(Tmp1).addImm(0)
1741 .addImm(Tmp3).addImm(Tmp2);
1742 break;
Nate Begeman7ddecb42005-04-06 23:51:40 +00001743 }
Nate Begemanc7bd4822005-04-11 06:34:10 +00001744 RecordSuccess = true;
Nate Begeman7ddecb42005-04-06 23:51:40 +00001745 return Result;
1746
Nate Begemana9795f82005-03-24 04:41:43 +00001747 case ISD::OR:
Nate Begeman7ddecb42005-04-06 23:51:40 +00001748 if (SelectBitfieldInsert(N, Result))
1749 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001750 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman439b4442005-04-05 04:22:58 +00001751 switch(getImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
Nate Begemana9795f82005-03-24 04:41:43 +00001752 default: assert(0 && "unhandled result code");
1753 case 0: // No immediate
1754 Tmp2 = SelectExpr(N.getOperand(1));
Nate Begemanc7bd4822005-04-11 06:34:10 +00001755 Opc = Recording ? PPC::ORo : PPC::OR;
1756 RecordSuccess = true;
1757 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +00001758 break;
1759 case 1: // Low immediate
Nate Begeman7ddecb42005-04-06 23:51:40 +00001760 BuildMI(BB, PPC::ORI, 2, Result).addReg(Tmp1).addImm(Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +00001761 break;
1762 case 2: // Shifted immediate
Nate Begeman7ddecb42005-04-06 23:51:40 +00001763 BuildMI(BB, PPC::ORIS, 2, Result).addReg(Tmp1).addImm(Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +00001764 break;
1765 }
1766 return Result;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001767
Nate Begemanaa73a9f2005-04-03 11:20:20 +00001768 case ISD::XOR: {
1769 // Check for EQV: xor, (xor a, -1), b
1770 if (N.getOperand(0).getOpcode() == ISD::XOR &&
1771 N.getOperand(0).getOperand(1).getOpcode() == ISD::Constant &&
1772 cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->isAllOnesValue()) {
Nate Begemanaa73a9f2005-04-03 11:20:20 +00001773 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1774 Tmp2 = SelectExpr(N.getOperand(1));
1775 BuildMI(BB, PPC::EQV, 2, Result).addReg(Tmp1).addReg(Tmp2);
1776 return Result;
1777 }
1778 // Check for NOT, NOR, and NAND: xor (copy, or, and), -1
1779 if (N.getOperand(1).getOpcode() == ISD::Constant &&
1780 cast<ConstantSDNode>(N.getOperand(1))->isAllOnesValue()) {
Nate Begemanaa73a9f2005-04-03 11:20:20 +00001781 switch(N.getOperand(0).getOpcode()) {
1782 case ISD::OR:
1783 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1784 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1785 BuildMI(BB, PPC::NOR, 2, Result).addReg(Tmp1).addReg(Tmp2);
1786 break;
1787 case ISD::AND:
1788 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1789 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1790 BuildMI(BB, PPC::NAND, 2, Result).addReg(Tmp1).addReg(Tmp2);
1791 break;
1792 default:
1793 Tmp1 = SelectExpr(N.getOperand(0));
1794 BuildMI(BB, PPC::NOR, 2, Result).addReg(Tmp1).addReg(Tmp1);
1795 break;
1796 }
1797 return Result;
1798 }
1799 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman439b4442005-04-05 04:22:58 +00001800 switch(getImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
Nate Begemanaa73a9f2005-04-03 11:20:20 +00001801 default: assert(0 && "unhandled result code");
1802 case 0: // No immediate
1803 Tmp2 = SelectExpr(N.getOperand(1));
1804 BuildMI(BB, PPC::XOR, 2, Result).addReg(Tmp1).addReg(Tmp2);
1805 break;
1806 case 1: // Low immediate
1807 BuildMI(BB, PPC::XORI, 2, Result).addReg(Tmp1).addImm(Tmp2);
1808 break;
1809 case 2: // Shifted immediate
1810 BuildMI(BB, PPC::XORIS, 2, Result).addReg(Tmp1).addImm(Tmp2);
1811 break;
1812 }
1813 return Result;
1814 }
1815
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001816 case ISD::SUB:
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001817 Tmp2 = SelectExpr(N.getOperand(1));
Nate Begeman439b4442005-04-05 04:22:58 +00001818 if (1 == getImmediateForOpcode(N.getOperand(0), opcode, Tmp1))
Nate Begeman27523a12005-04-02 00:42:16 +00001819 BuildMI(BB, PPC::SUBFIC, 2, Result).addReg(Tmp2).addSImm(Tmp1);
1820 else {
1821 Tmp1 = SelectExpr(N.getOperand(0));
1822 BuildMI(BB, PPC::SUBF, 2, Result).addReg(Tmp2).addReg(Tmp1);
1823 }
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001824 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001825
Nate Begeman5e966612005-03-24 06:28:42 +00001826 case ISD::MUL:
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001827 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman439b4442005-04-05 04:22:58 +00001828 if (1 == getImmediateForOpcode(N.getOperand(1), opcode, Tmp2))
Nate Begeman307e7442005-03-26 01:28:53 +00001829 BuildMI(BB, PPC::MULLI, 2, Result).addReg(Tmp1).addSImm(Tmp2);
1830 else {
1831 Tmp2 = SelectExpr(N.getOperand(1));
1832 BuildMI(BB, PPC::MULLW, 2, Result).addReg(Tmp1).addReg(Tmp2);
1833 }
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001834 return Result;
1835
Nate Begeman815d6da2005-04-06 00:25:27 +00001836 case ISD::MULHS:
1837 case ISD::MULHU:
1838 Tmp1 = SelectExpr(N.getOperand(0));
1839 Tmp2 = SelectExpr(N.getOperand(1));
1840 Opc = (ISD::MULHU == opcode) ? PPC::MULHWU : PPC::MULHW;
1841 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1842 return Result;
1843
Nate Begemanf3d08f32005-03-29 00:03:27 +00001844 case ISD::SDIV:
1845 case ISD::UDIV:
Nate Begeman815d6da2005-04-06 00:25:27 +00001846 switch (getImmediateForOpcode(N.getOperand(1), opcode, Tmp3)) {
1847 default: break;
1848 // If this is an sdiv by a power of two, we can use an srawi/addze pair.
1849 case 3:
Nate Begeman80196b12005-04-05 00:15:08 +00001850 Tmp1 = MakeReg(MVT::i32);
1851 Tmp2 = SelectExpr(N.getOperand(0));
Nate Begeman9f833d32005-04-12 00:10:02 +00001852 if ((int)Tmp3 < 0) {
1853 unsigned Tmp4 = MakeReg(MVT::i32);
1854 BuildMI(BB, PPC::SRAWI, 2, Tmp1).addReg(Tmp2).addImm(-Tmp3);
1855 BuildMI(BB, PPC::ADDZE, 1, Tmp4).addReg(Tmp1);
1856 BuildMI(BB, PPC::NEG, 1, Result).addReg(Tmp4);
1857 } else {
1858 BuildMI(BB, PPC::SRAWI, 2, Tmp1).addReg(Tmp2).addImm(Tmp3);
1859 BuildMI(BB, PPC::ADDZE, 1, Result).addReg(Tmp1);
1860 }
Nate Begeman80196b12005-04-05 00:15:08 +00001861 return Result;
Nate Begeman815d6da2005-04-06 00:25:27 +00001862 // If this is a divide by constant, we can emit code using some magic
1863 // constants to implement it as a multiply instead.
Nate Begeman27b4c232005-04-06 06:44:57 +00001864 case 4:
1865 ExprMap.erase(N);
1866 if (opcode == ISD::SDIV)
1867 return SelectExpr(BuildSDIVSequence(N));
1868 else
1869 return SelectExpr(BuildUDIVSequence(N));
Nate Begeman80196b12005-04-05 00:15:08 +00001870 }
Nate Begemanf3d08f32005-03-29 00:03:27 +00001871 Tmp1 = SelectExpr(N.getOperand(0));
1872 Tmp2 = SelectExpr(N.getOperand(1));
1873 Opc = (ISD::UDIV == opcode) ? PPC::DIVWU : PPC::DIVW;
1874 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1875 return Result;
1876
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001877 case ISD::ADD_PARTS:
Nate Begemanca12a2b2005-03-28 22:28:37 +00001878 case ISD::SUB_PARTS: {
1879 assert(N.getNumOperands() == 4 && N.getValueType() == MVT::i32 &&
1880 "Not an i64 add/sub!");
1881 // Emit all of the operands.
1882 std::vector<unsigned> InVals;
1883 for (unsigned i = 0, e = N.getNumOperands(); i != e; ++i)
1884 InVals.push_back(SelectExpr(N.getOperand(i)));
1885 if (N.getOpcode() == ISD::ADD_PARTS) {
Nate Begeman27eeb002005-04-02 05:59:34 +00001886 BuildMI(BB, PPC::ADDC, 2, Result).addReg(InVals[0]).addReg(InVals[2]);
1887 BuildMI(BB, PPC::ADDE, 2, Result+1).addReg(InVals[1]).addReg(InVals[3]);
Nate Begemanca12a2b2005-03-28 22:28:37 +00001888 } else {
Nate Begeman27eeb002005-04-02 05:59:34 +00001889 BuildMI(BB, PPC::SUBFC, 2, Result).addReg(InVals[2]).addReg(InVals[0]);
1890 BuildMI(BB, PPC::SUBFE, 2, Result+1).addReg(InVals[3]).addReg(InVals[1]);
1891 }
1892 return Result+N.ResNo;
1893 }
1894
1895 case ISD::SHL_PARTS:
1896 case ISD::SRA_PARTS:
1897 case ISD::SRL_PARTS: {
1898 assert(N.getNumOperands() == 3 && N.getValueType() == MVT::i32 &&
1899 "Not an i64 shift!");
1900 unsigned ShiftOpLo = SelectExpr(N.getOperand(0));
1901 unsigned ShiftOpHi = SelectExpr(N.getOperand(1));
Nate Begeman3664cef2005-04-13 22:14:14 +00001902 unsigned SHReg = FoldIfWideZeroExtend(N.getOperand(2));
1903 Tmp1 = MakeReg(MVT::i32);
1904 Tmp2 = MakeReg(MVT::i32);
Nate Begeman27eeb002005-04-02 05:59:34 +00001905 Tmp3 = MakeReg(MVT::i32);
1906 unsigned Tmp4 = MakeReg(MVT::i32);
1907 unsigned Tmp5 = MakeReg(MVT::i32);
1908 unsigned Tmp6 = MakeReg(MVT::i32);
1909 BuildMI(BB, PPC::SUBFIC, 2, Tmp1).addReg(SHReg).addSImm(32);
1910 if (ISD::SHL_PARTS == opcode) {
1911 BuildMI(BB, PPC::SLW, 2, Tmp2).addReg(ShiftOpHi).addReg(SHReg);
1912 BuildMI(BB, PPC::SRW, 2, Tmp3).addReg(ShiftOpLo).addReg(Tmp1);
1913 BuildMI(BB, PPC::OR, 2, Tmp4).addReg(Tmp2).addReg(Tmp3);
1914 BuildMI(BB, PPC::ADDI, 2, Tmp5).addReg(SHReg).addSImm(-32);
Nate Begemanfa554702005-04-03 22:13:27 +00001915 BuildMI(BB, PPC::SLW, 2, Tmp6).addReg(ShiftOpLo).addReg(Tmp5);
Nate Begeman27eeb002005-04-02 05:59:34 +00001916 BuildMI(BB, PPC::OR, 2, Result+1).addReg(Tmp4).addReg(Tmp6);
1917 BuildMI(BB, PPC::SLW, 2, Result).addReg(ShiftOpLo).addReg(SHReg);
1918 } else if (ISD::SRL_PARTS == opcode) {
1919 BuildMI(BB, PPC::SRW, 2, Tmp2).addReg(ShiftOpLo).addReg(SHReg);
1920 BuildMI(BB, PPC::SLW, 2, Tmp3).addReg(ShiftOpHi).addReg(Tmp1);
1921 BuildMI(BB, PPC::OR, 2, Tmp4).addReg(Tmp2).addReg(Tmp3);
1922 BuildMI(BB, PPC::ADDI, 2, Tmp5).addReg(SHReg).addSImm(-32);
1923 BuildMI(BB, PPC::SRW, 2, Tmp6).addReg(ShiftOpHi).addReg(Tmp5);
1924 BuildMI(BB, PPC::OR, 2, Result).addReg(Tmp4).addReg(Tmp6);
1925 BuildMI(BB, PPC::SRW, 2, Result+1).addReg(ShiftOpHi).addReg(SHReg);
1926 } else {
1927 MachineBasicBlock *TmpMBB = new MachineBasicBlock(BB->getBasicBlock());
1928 MachineBasicBlock *PhiMBB = new MachineBasicBlock(BB->getBasicBlock());
1929 MachineBasicBlock *OldMBB = BB;
1930 MachineFunction *F = BB->getParent();
1931 ilist<MachineBasicBlock>::iterator It = BB; ++It;
1932 F->getBasicBlockList().insert(It, TmpMBB);
1933 F->getBasicBlockList().insert(It, PhiMBB);
1934 BB->addSuccessor(TmpMBB);
1935 BB->addSuccessor(PhiMBB);
1936 BuildMI(BB, PPC::SRW, 2, Tmp2).addReg(ShiftOpLo).addReg(SHReg);
1937 BuildMI(BB, PPC::SLW, 2, Tmp3).addReg(ShiftOpHi).addReg(Tmp1);
1938 BuildMI(BB, PPC::OR, 2, Tmp4).addReg(Tmp2).addReg(Tmp3);
1939 BuildMI(BB, PPC::ADDICo, 2, Tmp5).addReg(SHReg).addSImm(-32);
1940 BuildMI(BB, PPC::SRAW, 2, Tmp6).addReg(ShiftOpHi).addReg(Tmp5);
1941 BuildMI(BB, PPC::SRAW, 2, Result+1).addReg(ShiftOpHi).addReg(SHReg);
1942 BuildMI(BB, PPC::BLE, 2).addReg(PPC::CR0).addMBB(PhiMBB);
1943 // Select correct least significant half if the shift amount > 32
1944 BB = TmpMBB;
1945 unsigned Tmp7 = MakeReg(MVT::i32);
1946 BuildMI(BB, PPC::OR, 2, Tmp7).addReg(Tmp6).addReg(Tmp6);
1947 TmpMBB->addSuccessor(PhiMBB);
1948 BB = PhiMBB;
1949 BuildMI(BB, PPC::PHI, 4, Result).addReg(Tmp4).addMBB(OldMBB)
1950 .addReg(Tmp7).addMBB(TmpMBB);
Nate Begemanca12a2b2005-03-28 22:28:37 +00001951 }
1952 return Result+N.ResNo;
1953 }
1954
Nate Begemana9795f82005-03-24 04:41:43 +00001955 case ISD::FP_TO_UINT:
Nate Begeman6b559972005-04-01 02:59:27 +00001956 case ISD::FP_TO_SINT: {
1957 bool U = (ISD::FP_TO_UINT == opcode);
1958 Tmp1 = SelectExpr(N.getOperand(0));
1959 if (!U) {
1960 Tmp2 = MakeReg(MVT::f64);
1961 BuildMI(BB, PPC::FCTIWZ, 1, Tmp2).addReg(Tmp1);
1962 int FrameIdx = BB->getParent()->getFrameInfo()->CreateStackObject(8, 8);
1963 addFrameReference(BuildMI(BB, PPC::STFD, 3).addReg(Tmp2), FrameIdx);
1964 addFrameReference(BuildMI(BB, PPC::LWZ, 2, Result), FrameIdx, 4);
1965 return Result;
1966 } else {
1967 unsigned Zero = getConstDouble(0.0);
1968 unsigned MaxInt = getConstDouble((1LL << 32) - 1);
1969 unsigned Border = getConstDouble(1LL << 31);
1970 unsigned UseZero = MakeReg(MVT::f64);
1971 unsigned UseMaxInt = MakeReg(MVT::f64);
1972 unsigned UseChoice = MakeReg(MVT::f64);
1973 unsigned TmpReg = MakeReg(MVT::f64);
1974 unsigned TmpReg2 = MakeReg(MVT::f64);
1975 unsigned ConvReg = MakeReg(MVT::f64);
1976 unsigned IntTmp = MakeReg(MVT::i32);
1977 unsigned XorReg = MakeReg(MVT::i32);
1978 MachineFunction *F = BB->getParent();
1979 int FrameIdx = F->getFrameInfo()->CreateStackObject(8, 8);
1980 // Update machine-CFG edges
1981 MachineBasicBlock *XorMBB = new MachineBasicBlock(BB->getBasicBlock());
1982 MachineBasicBlock *PhiMBB = new MachineBasicBlock(BB->getBasicBlock());
1983 MachineBasicBlock *OldMBB = BB;
1984 ilist<MachineBasicBlock>::iterator It = BB; ++It;
1985 F->getBasicBlockList().insert(It, XorMBB);
1986 F->getBasicBlockList().insert(It, PhiMBB);
1987 BB->addSuccessor(XorMBB);
1988 BB->addSuccessor(PhiMBB);
1989 // Convert from floating point to unsigned 32-bit value
1990 // Use 0 if incoming value is < 0.0
1991 BuildMI(BB, PPC::FSEL, 3, UseZero).addReg(Tmp1).addReg(Tmp1).addReg(Zero);
1992 // Use 2**32 - 1 if incoming value is >= 2**32
1993 BuildMI(BB, PPC::FSUB, 2, UseMaxInt).addReg(MaxInt).addReg(Tmp1);
1994 BuildMI(BB, PPC::FSEL, 3, UseChoice).addReg(UseMaxInt).addReg(UseZero)
1995 .addReg(MaxInt);
1996 // Subtract 2**31
1997 BuildMI(BB, PPC::FSUB, 2, TmpReg).addReg(UseChoice).addReg(Border);
1998 // Use difference if >= 2**31
1999 BuildMI(BB, PPC::FCMPU, 2, PPC::CR0).addReg(UseChoice).addReg(Border);
2000 BuildMI(BB, PPC::FSEL, 3, TmpReg2).addReg(TmpReg).addReg(TmpReg)
2001 .addReg(UseChoice);
2002 // Convert to integer
2003 BuildMI(BB, PPC::FCTIWZ, 1, ConvReg).addReg(TmpReg2);
2004 addFrameReference(BuildMI(BB, PPC::STFD, 3).addReg(ConvReg), FrameIdx);
2005 addFrameReference(BuildMI(BB, PPC::LWZ, 2, IntTmp), FrameIdx, 4);
2006 BuildMI(BB, PPC::BLT, 2).addReg(PPC::CR0).addMBB(PhiMBB);
2007 BuildMI(BB, PPC::B, 1).addMBB(XorMBB);
2008
2009 // XorMBB:
2010 // add 2**31 if input was >= 2**31
2011 BB = XorMBB;
2012 BuildMI(BB, PPC::XORIS, 2, XorReg).addReg(IntTmp).addImm(0x8000);
2013 XorMBB->addSuccessor(PhiMBB);
2014
2015 // PhiMBB:
2016 // DestReg = phi [ IntTmp, OldMBB ], [ XorReg, XorMBB ]
2017 BB = PhiMBB;
2018 BuildMI(BB, PPC::PHI, 4, Result).addReg(IntTmp).addMBB(OldMBB)
2019 .addReg(XorReg).addMBB(XorMBB);
2020 return Result;
2021 }
2022 assert(0 && "Should never get here");
2023 return 0;
2024 }
Nate Begemana9795f82005-03-24 04:41:43 +00002025
Nate Begeman9e3e1b52005-03-24 23:35:30 +00002026 case ISD::SETCC:
Nate Begeman33162522005-03-29 21:54:38 +00002027 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Node)) {
Nate Begeman7e7fadd2005-04-07 20:30:01 +00002028 if (ConstantSDNode *CN =
2029 dyn_cast<ConstantSDNode>(SetCC->getOperand(1).Val)) {
Nate Begeman9765c252005-04-12 21:22:28 +00002030 // We can codegen setcc op, imm very efficiently compared to a brcond.
2031 // Check for those cases here.
2032 // setcc op, 0
Nate Begeman7e7fadd2005-04-07 20:30:01 +00002033 if (CN->getValue() == 0) {
2034 Tmp1 = SelectExpr(SetCC->getOperand(0));
2035 switch (SetCC->getCondition()) {
2036 default: assert(0 && "Unhandled SetCC condition"); abort();
2037 case ISD::SETEQ:
Nate Begeman7e7fadd2005-04-07 20:30:01 +00002038 Tmp2 = MakeReg(MVT::i32);
2039 BuildMI(BB, PPC::CNTLZW, 1, Tmp2).addReg(Tmp1);
2040 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp2).addImm(27)
2041 .addImm(5).addImm(31);
2042 break;
2043 case ISD::SETNE:
Nate Begeman7e7fadd2005-04-07 20:30:01 +00002044 Tmp2 = MakeReg(MVT::i32);
2045 BuildMI(BB, PPC::ADDIC, 2, Tmp2).addReg(Tmp1).addSImm(-1);
2046 BuildMI(BB, PPC::SUBFE, 2, Result).addReg(Tmp2).addReg(Tmp1);
2047 break;
Nate Begeman7e7fadd2005-04-07 20:30:01 +00002048 case ISD::SETLT:
2049 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(1)
2050 .addImm(31).addImm(31);
2051 break;
Nate Begeman7e7fadd2005-04-07 20:30:01 +00002052 case ISD::SETGT:
2053 Tmp2 = MakeReg(MVT::i32);
2054 Tmp3 = MakeReg(MVT::i32);
2055 BuildMI(BB, PPC::NEG, 2, Tmp2).addReg(Tmp1);
2056 BuildMI(BB, PPC::ANDC, 2, Tmp3).addReg(Tmp2).addReg(Tmp1);
2057 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp3).addImm(1)
2058 .addImm(31).addImm(31);
2059 break;
Nate Begeman9765c252005-04-12 21:22:28 +00002060 }
2061 return Result;
2062 }
2063 // setcc op, -1
2064 if (CN->isAllOnesValue()) {
2065 Tmp1 = SelectExpr(SetCC->getOperand(0));
2066 switch (SetCC->getCondition()) {
2067 default: assert(0 && "Unhandled SetCC condition"); abort();
2068 case ISD::SETEQ:
2069 Tmp2 = MakeReg(MVT::i32);
2070 Tmp3 = MakeReg(MVT::i32);
2071 BuildMI(BB, PPC::ADDIC, 2, Tmp2).addReg(Tmp1).addSImm(1);
2072 BuildMI(BB, PPC::LI, 1, Tmp3).addSImm(0);
2073 BuildMI(BB, PPC::ADDZE, 1, Result).addReg(Tmp3);
Nate Begeman7e7fadd2005-04-07 20:30:01 +00002074 break;
Nate Begeman9765c252005-04-12 21:22:28 +00002075 case ISD::SETNE:
2076 Tmp2 = MakeReg(MVT::i32);
2077 Tmp3 = MakeReg(MVT::i32);
2078 BuildMI(BB, PPC::NOR, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
2079 BuildMI(BB, PPC::ADDIC, 2, Tmp3).addReg(Tmp2).addSImm(-1);
2080 BuildMI(BB, PPC::SUBFE, 2, Result).addReg(Tmp3).addReg(Tmp2);
2081 break;
2082 case ISD::SETLT:
2083 Tmp2 = MakeReg(MVT::i32);
2084 Tmp3 = MakeReg(MVT::i32);
2085 BuildMI(BB, PPC::ADDI, 2, Tmp2).addReg(Tmp1).addSImm(1);
2086 BuildMI(BB, PPC::AND, 2, Tmp3).addReg(Tmp2).addReg(Tmp1);
2087 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp3).addImm(1)
2088 .addImm(31).addImm(31);
2089 break;
2090 case ISD::SETGT:
2091 Tmp2 = MakeReg(MVT::i32);
Nate Begeman7e7fadd2005-04-07 20:30:01 +00002092 BuildMI(BB, PPC::RLWINM, 4, Tmp2).addReg(Tmp1).addImm(1)
2093 .addImm(31).addImm(31);
2094 BuildMI(BB, PPC::XORI, 2, Result).addReg(Tmp2).addImm(1);
2095 break;
2096 }
2097 return Result;
2098 }
2099 }
2100
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00002101 unsigned CCReg = SelectCC(N, Opc);
Nate Begeman31318e42005-04-01 07:21:30 +00002102 unsigned TrueValue = MakeReg(MVT::i32);
2103 BuildMI(BB, PPC::LI, 1, TrueValue).addSImm(1);
2104 unsigned FalseValue = MakeReg(MVT::i32);
2105 BuildMI(BB, PPC::LI, 1, FalseValue).addSImm(0);
2106
Nate Begeman33162522005-03-29 21:54:38 +00002107 // Create an iterator with which to insert the MBB for copying the false
2108 // value and the MBB to hold the PHI instruction for this SetCC.
2109 MachineBasicBlock *thisMBB = BB;
2110 const BasicBlock *LLVM_BB = BB->getBasicBlock();
2111 ilist<MachineBasicBlock>::iterator It = BB;
2112 ++It;
2113
2114 // thisMBB:
2115 // ...
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00002116 // cmpTY ccX, r1, r2
Nate Begeman33162522005-03-29 21:54:38 +00002117 // %TrueValue = li 1
2118 // bCC sinkMBB
Nate Begeman33162522005-03-29 21:54:38 +00002119 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
2120 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00002121 BuildMI(BB, Opc, 2).addReg(CCReg).addMBB(sinkMBB);
Nate Begeman33162522005-03-29 21:54:38 +00002122 MachineFunction *F = BB->getParent();
2123 F->getBasicBlockList().insert(It, copy0MBB);
2124 F->getBasicBlockList().insert(It, sinkMBB);
2125 // Update machine-CFG edges
2126 BB->addSuccessor(copy0MBB);
2127 BB->addSuccessor(sinkMBB);
2128
2129 // copy0MBB:
2130 // %FalseValue = li 0
2131 // fallthrough
2132 BB = copy0MBB;
Nate Begeman33162522005-03-29 21:54:38 +00002133 // Update machine-CFG edges
2134 BB->addSuccessor(sinkMBB);
2135
2136 // sinkMBB:
2137 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
2138 // ...
2139 BB = sinkMBB;
2140 BuildMI(BB, PPC::PHI, 4, Result).addReg(FalseValue)
2141 .addMBB(copy0MBB).addReg(TrueValue).addMBB(thisMBB);
2142 return Result;
2143 }
2144 assert(0 && "Is this legal?");
2145 return 0;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00002146
Nate Begeman74747862005-03-29 22:24:51 +00002147 case ISD::SELECT: {
Chris Lattner30710192005-04-01 07:10:02 +00002148 unsigned TrueValue = SelectExpr(N.getOperand(1)); //Use if TRUE
2149 unsigned FalseValue = SelectExpr(N.getOperand(2)); //Use if FALSE
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00002150 unsigned CCReg = SelectCC(N.getOperand(0), Opc);
Chris Lattner30710192005-04-01 07:10:02 +00002151
Nate Begeman74747862005-03-29 22:24:51 +00002152 // Create an iterator with which to insert the MBB for copying the false
2153 // value and the MBB to hold the PHI instruction for this SetCC.
2154 MachineBasicBlock *thisMBB = BB;
2155 const BasicBlock *LLVM_BB = BB->getBasicBlock();
2156 ilist<MachineBasicBlock>::iterator It = BB;
2157 ++It;
2158
2159 // thisMBB:
2160 // ...
2161 // TrueVal = ...
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00002162 // cmpTY ccX, r1, r2
Nate Begeman74747862005-03-29 22:24:51 +00002163 // bCC copy1MBB
2164 // fallthrough --> copy0MBB
Nate Begeman74747862005-03-29 22:24:51 +00002165 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
2166 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00002167 BuildMI(BB, Opc, 2).addReg(CCReg).addMBB(sinkMBB);
Nate Begeman74747862005-03-29 22:24:51 +00002168 MachineFunction *F = BB->getParent();
2169 F->getBasicBlockList().insert(It, copy0MBB);
2170 F->getBasicBlockList().insert(It, sinkMBB);
2171 // Update machine-CFG edges
2172 BB->addSuccessor(copy0MBB);
2173 BB->addSuccessor(sinkMBB);
2174
2175 // copy0MBB:
2176 // %FalseValue = ...
2177 // # fallthrough to sinkMBB
2178 BB = copy0MBB;
Nate Begeman74747862005-03-29 22:24:51 +00002179 // Update machine-CFG edges
2180 BB->addSuccessor(sinkMBB);
2181
2182 // sinkMBB:
2183 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
2184 // ...
2185 BB = sinkMBB;
2186 BuildMI(BB, PPC::PHI, 4, Result).addReg(FalseValue)
2187 .addMBB(copy0MBB).addReg(TrueValue).addMBB(thisMBB);
Nate Begeman74747862005-03-29 22:24:51 +00002188 return Result;
2189 }
Nate Begemana9795f82005-03-24 04:41:43 +00002190
2191 case ISD::Constant:
2192 switch (N.getValueType()) {
2193 default: assert(0 && "Cannot use constants of this type!");
2194 case MVT::i1:
2195 BuildMI(BB, PPC::LI, 1, Result)
2196 .addSImm(!cast<ConstantSDNode>(N)->isNullValue());
2197 break;
2198 case MVT::i32:
2199 {
2200 int v = (int)cast<ConstantSDNode>(N)->getSignExtended();
2201 if (v < 32768 && v >= -32768) {
2202 BuildMI(BB, PPC::LI, 1, Result).addSImm(v);
2203 } else {
Nate Begeman5e966612005-03-24 06:28:42 +00002204 Tmp1 = MakeReg(MVT::i32);
2205 BuildMI(BB, PPC::LIS, 1, Tmp1).addSImm(v >> 16);
2206 BuildMI(BB, PPC::ORI, 2, Result).addReg(Tmp1).addImm(v & 0xFFFF);
Nate Begemana9795f82005-03-24 04:41:43 +00002207 }
2208 }
2209 }
2210 return Result;
2211 }
2212
2213 return 0;
2214}
2215
2216void ISel::Select(SDOperand N) {
2217 unsigned Tmp1, Tmp2, Opc;
2218 unsigned opcode = N.getOpcode();
2219
2220 if (!ExprMap.insert(std::make_pair(N, 1)).second)
2221 return; // Already selected.
2222
2223 SDNode *Node = N.Val;
2224
2225 switch (Node->getOpcode()) {
2226 default:
2227 Node->dump(); std::cerr << "\n";
2228 assert(0 && "Node not handled yet!");
2229 case ISD::EntryToken: return; // Noop
2230 case ISD::TokenFactor:
2231 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
2232 Select(Node->getOperand(i));
2233 return;
2234 case ISD::ADJCALLSTACKDOWN:
2235 case ISD::ADJCALLSTACKUP:
2236 Select(N.getOperand(0));
2237 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
2238 Opc = N.getOpcode() == ISD::ADJCALLSTACKDOWN ? PPC::ADJCALLSTACKDOWN :
2239 PPC::ADJCALLSTACKUP;
2240 BuildMI(BB, Opc, 1).addImm(Tmp1);
2241 return;
2242 case ISD::BR: {
2243 MachineBasicBlock *Dest =
2244 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
Nate Begemana9795f82005-03-24 04:41:43 +00002245 Select(N.getOperand(0));
2246 BuildMI(BB, PPC::B, 1).addMBB(Dest);
2247 return;
2248 }
2249 case ISD::BRCOND:
Nate Begemancd08e4c2005-04-09 20:09:12 +00002250 case ISD::BRCONDTWOWAY:
Nate Begemana9795f82005-03-24 04:41:43 +00002251 SelectBranchCC(N);
2252 return;
2253 case ISD::CopyToReg:
2254 Select(N.getOperand(0));
2255 Tmp1 = SelectExpr(N.getOperand(1));
2256 Tmp2 = cast<RegSDNode>(N)->getReg();
2257
2258 if (Tmp1 != Tmp2) {
2259 if (N.getOperand(1).getValueType() == MVT::f64 ||
2260 N.getOperand(1).getValueType() == MVT::f32)
2261 BuildMI(BB, PPC::FMR, 1, Tmp2).addReg(Tmp1);
2262 else
2263 BuildMI(BB, PPC::OR, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
2264 }
2265 return;
2266 case ISD::ImplicitDef:
2267 Select(N.getOperand(0));
2268 BuildMI(BB, PPC::IMPLICIT_DEF, 0, cast<RegSDNode>(N)->getReg());
2269 return;
2270 case ISD::RET:
2271 switch (N.getNumOperands()) {
2272 default:
2273 assert(0 && "Unknown return instruction!");
2274 case 3:
2275 assert(N.getOperand(1).getValueType() == MVT::i32 &&
2276 N.getOperand(2).getValueType() == MVT::i32 &&
2277 "Unknown two-register value!");
2278 Select(N.getOperand(0));
2279 Tmp1 = SelectExpr(N.getOperand(1));
2280 Tmp2 = SelectExpr(N.getOperand(2));
Nate Begeman27523a12005-04-02 00:42:16 +00002281 BuildMI(BB, PPC::OR, 2, PPC::R3).addReg(Tmp2).addReg(Tmp2);
2282 BuildMI(BB, PPC::OR, 2, PPC::R4).addReg(Tmp1).addReg(Tmp1);
Nate Begemana9795f82005-03-24 04:41:43 +00002283 break;
2284 case 2:
2285 Select(N.getOperand(0));
2286 Tmp1 = SelectExpr(N.getOperand(1));
2287 switch (N.getOperand(1).getValueType()) {
2288 default:
2289 assert(0 && "Unknown return type!");
2290 case MVT::f64:
2291 case MVT::f32:
2292 BuildMI(BB, PPC::FMR, 1, PPC::F1).addReg(Tmp1);
2293 break;
2294 case MVT::i32:
2295 BuildMI(BB, PPC::OR, 2, PPC::R3).addReg(Tmp1).addReg(Tmp1);
2296 break;
2297 }
Nate Begeman9e3e1b52005-03-24 23:35:30 +00002298 case 1:
2299 Select(N.getOperand(0));
2300 break;
Nate Begemana9795f82005-03-24 04:41:43 +00002301 }
2302 BuildMI(BB, PPC::BLR, 0); // Just emit a 'ret' instruction
2303 return;
Nate Begemana9795f82005-03-24 04:41:43 +00002304 case ISD::TRUNCSTORE:
2305 case ISD::STORE:
2306 {
2307 SDOperand Chain = N.getOperand(0);
2308 SDOperand Value = N.getOperand(1);
2309 SDOperand Address = N.getOperand(2);
2310 Select(Chain);
2311
2312 Tmp1 = SelectExpr(Value); //value
2313
2314 if (opcode == ISD::STORE) {
2315 switch(Value.getValueType()) {
2316 default: assert(0 && "unknown Type in store");
2317 case MVT::i32: Opc = PPC::STW; break;
2318 case MVT::f64: Opc = PPC::STFD; break;
2319 case MVT::f32: Opc = PPC::STFS; break;
2320 }
2321 } else { //ISD::TRUNCSTORE
2322 switch(cast<MVTSDNode>(Node)->getExtraValueType()) {
2323 default: assert(0 && "unknown Type in store");
Nate Begeman7e7fadd2005-04-07 20:30:01 +00002324 case MVT::i1:
Nate Begemana9795f82005-03-24 04:41:43 +00002325 case MVT::i8: Opc = PPC::STB; break;
2326 case MVT::i16: Opc = PPC::STH; break;
2327 }
2328 }
2329
Nate Begemana7e11a42005-04-01 05:57:17 +00002330 if(Address.getOpcode() == ISD::FrameIndex)
Nate Begemana9795f82005-03-24 04:41:43 +00002331 {
Nate Begeman58f718c2005-03-30 02:23:08 +00002332 Tmp2 = cast<FrameIndexSDNode>(Address)->getIndex();
2333 addFrameReference(BuildMI(BB, Opc, 3).addReg(Tmp1), (int)Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +00002334 }
2335 else
2336 {
2337 int offset;
Nate Begeman04730362005-04-01 04:45:11 +00002338 bool idx = SelectAddr(Address, Tmp2, offset);
2339 if (idx) {
2340 Opc = IndexedOpForOp(Opc);
2341 BuildMI(BB, Opc, 3).addReg(Tmp1).addReg(Tmp2).addReg(offset);
2342 } else {
2343 BuildMI(BB, Opc, 3).addReg(Tmp1).addImm(offset).addReg(Tmp2);
2344 }
Nate Begemana9795f82005-03-24 04:41:43 +00002345 }
2346 return;
2347 }
2348 case ISD::EXTLOAD:
2349 case ISD::SEXTLOAD:
2350 case ISD::ZEXTLOAD:
2351 case ISD::LOAD:
2352 case ISD::CopyFromReg:
2353 case ISD::CALL:
2354 case ISD::DYNAMIC_STACKALLOC:
2355 ExprMap.erase(N);
2356 SelectExpr(N);
2357 return;
2358 }
2359 assert(0 && "Should not be reached!");
2360}
2361
2362
2363/// createPPC32PatternInstructionSelector - This pass converts an LLVM function
2364/// into a machine code representation using pattern matching and a machine
2365/// description file.
2366///
2367FunctionPass *llvm::createPPC32ISelPattern(TargetMachine &TM) {
2368 return new ISel(TM);
Chris Lattner246fa632005-03-24 06:16:18 +00002369}
2370