blob: 1e4b88f5b49e128f671399c21f88da91eed53cfe [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 Begemandffcfcc2005-04-01 00:32:34 +0000527 unsigned SelectSetCR0(SDOperand CC);
Nate Begemanc7bd4822005-04-11 06:34:10 +0000528 unsigned SelectExpr(SDOperand N, bool Recording=false);
Nate Begemana9795f82005-03-24 04:41:43 +0000529 unsigned SelectExprFP(SDOperand N, unsigned Result);
530 void Select(SDOperand N);
531
Nate Begeman04730362005-04-01 04:45:11 +0000532 bool SelectAddr(SDOperand N, unsigned& Reg, int& offset);
Nate Begemana9795f82005-03-24 04:41:43 +0000533 void SelectBranchCC(SDOperand N);
534};
535
Nate Begeman80196b12005-04-05 00:15:08 +0000536/// ExactLog2 - This function solves for (Val == 1 << (N-1)) and returns N. It
537/// returns zero when the input is not exactly a power of two.
538static unsigned ExactLog2(unsigned Val) {
539 if (Val == 0 || (Val & (Val-1))) return 0;
540 unsigned Count = 0;
541 while (Val != 1) {
542 Val >>= 1;
543 ++Count;
544 }
545 return Count;
546}
547
Nate Begeman7ddecb42005-04-06 23:51:40 +0000548// IsRunOfOnes - returns true if Val consists of one contiguous run of 1's with
549// any number of 0's on either side. the 1's are allowed to wrap from LSB to
550// MSB. so 0x000FFF0, 0x0000FFFF, and 0xFF0000FF are all runs. 0x0F0F0000 is
551// not, since all 1's are not contiguous.
552static bool IsRunOfOnes(unsigned Val, unsigned &MB, unsigned &ME) {
553 bool isRun = true;
554 MB = 0;
555 ME = 0;
556
557 // look for first set bit
558 int i = 0;
559 for (; i < 32; i++) {
560 if ((Val & (1 << (31 - i))) != 0) {
561 MB = i;
562 ME = i;
563 break;
564 }
565 }
566
567 // look for last set bit
568 for (; i < 32; i++) {
569 if ((Val & (1 << (31 - i))) == 0)
570 break;
571 ME = i;
572 }
573
574 // look for next set bit
575 for (; i < 32; i++) {
576 if ((Val & (1 << (31 - i))) != 0)
577 break;
578 }
579
580 // if we exhausted all the bits, we found a match at this point for 0*1*0*
581 if (i == 32)
582 return true;
583
584 // since we just encountered more 1's, if it doesn't wrap around to the
585 // most significant bit of the word, then we did not find a match to 1*0*1* so
586 // exit.
587 if (MB != 0)
588 return false;
589
590 // look for last set bit
591 for (MB = i; i < 32; i++) {
592 if ((Val & (1 << (31 - i))) == 0)
593 break;
594 }
595
596 // if we exhausted all the bits, then we found a match for 1*0*1*, otherwise,
597 // the value is not a run of ones.
598 if (i == 32)
599 return true;
600 return false;
601}
602
Nate Begeman439b4442005-04-05 04:22:58 +0000603/// getImmediateForOpcode - This method returns a value indicating whether
Nate Begemana9795f82005-03-24 04:41:43 +0000604/// the ConstantSDNode N can be used as an immediate to Opcode. The return
605/// values are either 0, 1 or 2. 0 indicates that either N is not a
Nate Begeman9f833d32005-04-12 00:10:02 +0000606/// ConstantSDNode, or is not suitable for use by that opcode.
607/// Return value codes for turning into an enum someday:
608/// 1: constant may be used in normal immediate form.
609/// 2: constant may be used in shifted immediate form.
610/// 3: log base 2 of the constant may be used.
611/// 4: constant is suitable for integer division conversion
612/// 5: constant is a bitfield mask
Nate Begemana9795f82005-03-24 04:41:43 +0000613///
Nate Begeman439b4442005-04-05 04:22:58 +0000614static unsigned getImmediateForOpcode(SDOperand N, unsigned Opcode,
615 unsigned& Imm, bool U = false) {
Nate Begemana9795f82005-03-24 04:41:43 +0000616 if (N.getOpcode() != ISD::Constant) return 0;
617
618 int v = (int)cast<ConstantSDNode>(N)->getSignExtended();
619
620 switch(Opcode) {
621 default: return 0;
622 case ISD::ADD:
623 if (v <= 32767 && v >= -32768) { Imm = v & 0xFFFF; return 1; }
624 if ((v & 0x0000FFFF) == 0) { Imm = v >> 16; return 2; }
625 break;
Nate Begeman9f833d32005-04-12 00:10:02 +0000626 case ISD::AND: {
627 unsigned MB, ME;
628 if (IsRunOfOnes(v, MB, ME)) { Imm = MB << 16 | ME & 0xFFFF; return 5; }
629 if (v >= 0 && v <= 65535) { Imm = v & 0xFFFF; return 1; }
630 if ((v & 0x0000FFFF) == 0) { Imm = v >> 16; return 2; }
631 break;
632 }
Nate Begemana9795f82005-03-24 04:41:43 +0000633 case ISD::XOR:
634 case ISD::OR:
635 if (v >= 0 && v <= 65535) { Imm = v & 0xFFFF; return 1; }
636 if ((v & 0x0000FFFF) == 0) { Imm = v >> 16; return 2; }
637 break;
Nate Begeman307e7442005-03-26 01:28:53 +0000638 case ISD::MUL:
Nate Begeman27523a12005-04-02 00:42:16 +0000639 case ISD::SUB:
Nate Begeman307e7442005-03-26 01:28:53 +0000640 if (v <= 32767 && v >= -32768) { Imm = v & 0xFFFF; return 1; }
641 break;
Nate Begeman3e897162005-03-31 23:55:40 +0000642 case ISD::SETCC:
643 if (U && (v >= 0 && v <= 65535)) { Imm = v & 0xFFFF; return 1; }
644 if (!U && (v <= 32767 && v >= -32768)) { Imm = v & 0xFFFF; return 1; }
645 break;
Nate Begeman80196b12005-04-05 00:15:08 +0000646 case ISD::SDIV:
Nate Begeman439b4442005-04-05 04:22:58 +0000647 if ((Imm = ExactLog2(v))) { return 3; }
Nate Begeman9f833d32005-04-12 00:10:02 +0000648 if ((Imm = ExactLog2(-v))) { Imm = -Imm; return 3; }
Nate Begeman815d6da2005-04-06 00:25:27 +0000649 if (v <= -2 || v >= 2) { return 4; }
650 break;
651 case ISD::UDIV:
Nate Begeman27b4c232005-04-06 06:44:57 +0000652 if (v > 1) { return 4; }
Nate Begeman80196b12005-04-05 00:15:08 +0000653 break;
Nate Begemana9795f82005-03-24 04:41:43 +0000654 }
655 return 0;
656}
Nate Begeman3e897162005-03-31 23:55:40 +0000657
Nate Begemanc7bd4822005-04-11 06:34:10 +0000658/// NodeHasRecordingVariant - If SelectExpr can always produce code for
659/// NodeOpcode that also sets CR0 as a side effect, return true. Otherwise,
660/// return false.
661static bool NodeHasRecordingVariant(unsigned NodeOpcode) {
662 switch(NodeOpcode) {
663 default: return false;
664 case ISD::AND:
Nate Begeman9765c252005-04-12 21:22:28 +0000665 case ISD::OR:
666 case ISD::ZERO_EXTEND_INREG: return true;
Nate Begemanc7bd4822005-04-11 06:34:10 +0000667 }
668}
669
Nate Begeman3e897162005-03-31 23:55:40 +0000670/// getBCCForSetCC - Returns the PowerPC condition branch mnemonic corresponding
671/// to Condition. If the Condition is unordered or unsigned, the bool argument
672/// U is set to true, otherwise it is set to false.
673static unsigned getBCCForSetCC(unsigned Condition, bool& U) {
674 U = false;
675 switch (Condition) {
676 default: assert(0 && "Unknown condition!"); abort();
677 case ISD::SETEQ: return PPC::BEQ;
678 case ISD::SETNE: return PPC::BNE;
679 case ISD::SETULT: U = true;
680 case ISD::SETLT: return PPC::BLT;
681 case ISD::SETULE: U = true;
682 case ISD::SETLE: return PPC::BLE;
683 case ISD::SETUGT: U = true;
684 case ISD::SETGT: return PPC::BGT;
685 case ISD::SETUGE: U = true;
686 case ISD::SETGE: return PPC::BGE;
687 }
Nate Begeman04730362005-04-01 04:45:11 +0000688 return 0;
689}
690
691/// IndexedOpForOp - Return the indexed variant for each of the PowerPC load
692/// and store immediate instructions.
693static unsigned IndexedOpForOp(unsigned Opcode) {
694 switch(Opcode) {
695 default: assert(0 && "Unknown opcode!"); abort();
696 case PPC::LBZ: return PPC::LBZX; case PPC::STB: return PPC::STBX;
697 case PPC::LHZ: return PPC::LHZX; case PPC::STH: return PPC::STHX;
698 case PPC::LHA: return PPC::LHAX; case PPC::STW: return PPC::STWX;
699 case PPC::LWZ: return PPC::LWZX; case PPC::STFS: return PPC::STFSX;
700 case PPC::LFS: return PPC::LFSX; case PPC::STFD: return PPC::STFDX;
701 case PPC::LFD: return PPC::LFDX;
702 }
703 return 0;
Nate Begeman3e897162005-03-31 23:55:40 +0000704}
Nate Begeman815d6da2005-04-06 00:25:27 +0000705
706// Structure used to return the necessary information to codegen an SDIV as
707// a multiply.
708struct ms {
709 int m; // magic number
710 int s; // shift amount
711};
712
713struct mu {
714 unsigned int m; // magic number
715 int a; // add indicator
716 int s; // shift amount
717};
718
719/// magic - calculate the magic numbers required to codegen an integer sdiv as
720/// a sequence of multiply and shifts. Requires that the divisor not be 0, 1,
721/// or -1.
722static struct ms magic(int d) {
723 int p;
724 unsigned int ad, anc, delta, q1, r1, q2, r2, t;
725 const unsigned int two31 = 2147483648U; // 2^31
726 struct ms mag;
727
728 ad = abs(d);
729 t = two31 + ((unsigned int)d >> 31);
730 anc = t - 1 - t%ad; // absolute value of nc
731 p = 31; // initialize p
732 q1 = two31/anc; // initialize q1 = 2p/abs(nc)
733 r1 = two31 - q1*anc; // initialize r1 = rem(2p,abs(nc))
734 q2 = two31/ad; // initialize q2 = 2p/abs(d)
735 r2 = two31 - q2*ad; // initialize r2 = rem(2p,abs(d))
736 do {
737 p = p + 1;
738 q1 = 2*q1; // update q1 = 2p/abs(nc)
739 r1 = 2*r1; // update r1 = rem(2p/abs(nc))
740 if (r1 >= anc) { // must be unsigned comparison
741 q1 = q1 + 1;
742 r1 = r1 - anc;
743 }
744 q2 = 2*q2; // update q2 = 2p/abs(d)
745 r2 = 2*r2; // update r2 = rem(2p/abs(d))
746 if (r2 >= ad) { // must be unsigned comparison
747 q2 = q2 + 1;
748 r2 = r2 - ad;
749 }
750 delta = ad - r2;
751 } while (q1 < delta || (q1 == delta && r1 == 0));
752
753 mag.m = q2 + 1;
754 if (d < 0) mag.m = -mag.m; // resulting magic number
755 mag.s = p - 32; // resulting shift
756 return mag;
757}
758
759/// magicu - calculate the magic numbers required to codegen an integer udiv as
760/// a sequence of multiply, add and shifts. Requires that the divisor not be 0.
761static struct mu magicu(unsigned d)
762{
763 int p;
764 unsigned int nc, delta, q1, r1, q2, r2;
765 struct mu magu;
766 magu.a = 0; // initialize "add" indicator
767 nc = - 1 - (-d)%d;
768 p = 31; // initialize p
769 q1 = 0x80000000/nc; // initialize q1 = 2p/nc
770 r1 = 0x80000000 - q1*nc; // initialize r1 = rem(2p,nc)
771 q2 = 0x7FFFFFFF/d; // initialize q2 = (2p-1)/d
772 r2 = 0x7FFFFFFF - q2*d; // initialize r2 = rem((2p-1),d)
773 do {
774 p = p + 1;
775 if (r1 >= nc - r1 ) {
776 q1 = 2*q1 + 1; // update q1
777 r1 = 2*r1 - nc; // update r1
778 }
779 else {
780 q1 = 2*q1; // update q1
781 r1 = 2*r1; // update r1
782 }
783 if (r2 + 1 >= d - r2) {
784 if (q2 >= 0x7FFFFFFF) magu.a = 1;
785 q2 = 2*q2 + 1; // update q2
786 r2 = 2*r2 + 1 - d; // update r2
787 }
788 else {
789 if (q2 >= 0x80000000) magu.a = 1;
790 q2 = 2*q2; // update q2
791 r2 = 2*r2 + 1; // update r2
792 }
793 delta = d - 1 - r2;
794 } while (p < 64 && (q1 < delta || (q1 == delta && r1 == 0)));
795 magu.m = q2 + 1; // resulting magic number
796 magu.s = p - 32; // resulting shift
797 return magu;
798}
799}
800
801/// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
802/// return a DAG expression to select that will generate the same value by
803/// multiplying by a magic number. See:
804/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
805SDOperand ISel::BuildSDIVSequence(SDOperand N) {
806 int d = (int)cast<ConstantSDNode>(N.getOperand(1))->getSignExtended();
807 ms magics = magic(d);
808 // Multiply the numerator (operand 0) by the magic value
809 SDOperand Q = ISelDAG->getNode(ISD::MULHS, MVT::i32, N.getOperand(0),
810 ISelDAG->getConstant(magics.m, MVT::i32));
811 // If d > 0 and m < 0, add the numerator
812 if (d > 0 && magics.m < 0)
813 Q = ISelDAG->getNode(ISD::ADD, MVT::i32, Q, N.getOperand(0));
814 // If d < 0 and m > 0, subtract the numerator.
815 if (d < 0 && magics.m > 0)
816 Q = ISelDAG->getNode(ISD::SUB, MVT::i32, Q, N.getOperand(0));
817 // Shift right algebraic if shift value is nonzero
818 if (magics.s > 0)
819 Q = ISelDAG->getNode(ISD::SRA, MVT::i32, Q,
820 ISelDAG->getConstant(magics.s, MVT::i32));
821 // Extract the sign bit and add it to the quotient
822 SDOperand T =
823 ISelDAG->getNode(ISD::SRL, MVT::i32, Q, ISelDAG->getConstant(31, MVT::i32));
Nate Begeman27b4c232005-04-06 06:44:57 +0000824 return ISelDAG->getNode(ISD::ADD, MVT::i32, Q, T);
Nate Begeman815d6da2005-04-06 00:25:27 +0000825}
826
827/// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
828/// return a DAG expression to select that will generate the same value by
829/// multiplying by a magic number. See:
830/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
831SDOperand ISel::BuildUDIVSequence(SDOperand N) {
832 unsigned d =
833 (unsigned)cast<ConstantSDNode>(N.getOperand(1))->getSignExtended();
834 mu magics = magicu(d);
835 // Multiply the numerator (operand 0) by the magic value
836 SDOperand Q = ISelDAG->getNode(ISD::MULHU, MVT::i32, N.getOperand(0),
837 ISelDAG->getConstant(magics.m, MVT::i32));
838 if (magics.a == 0) {
839 Q = ISelDAG->getNode(ISD::SRL, MVT::i32, Q,
840 ISelDAG->getConstant(magics.s, MVT::i32));
841 } else {
842 SDOperand NPQ = ISelDAG->getNode(ISD::SUB, MVT::i32, N.getOperand(0), Q);
843 NPQ = ISelDAG->getNode(ISD::SRL, MVT::i32, NPQ,
844 ISelDAG->getConstant(1, MVT::i32));
845 NPQ = ISelDAG->getNode(ISD::ADD, MVT::i32, NPQ, Q);
846 Q = ISelDAG->getNode(ISD::SRL, MVT::i32, NPQ,
847 ISelDAG->getConstant(magics.s-1, MVT::i32));
848 }
Nate Begeman27b4c232005-04-06 06:44:57 +0000849 return Q;
Nate Begemana9795f82005-03-24 04:41:43 +0000850}
851
Nate Begemanc7b09f12005-03-25 08:34:25 +0000852/// getGlobalBaseReg - Output the instructions required to put the
853/// base address to use for accessing globals into a register.
854///
855unsigned ISel::getGlobalBaseReg() {
856 if (!GlobalBaseInitialized) {
857 // Insert the set of GlobalBaseReg into the first MBB of the function
858 MachineBasicBlock &FirstMBB = BB->getParent()->front();
859 MachineBasicBlock::iterator MBBI = FirstMBB.begin();
860 GlobalBaseReg = MakeReg(MVT::i32);
861 BuildMI(FirstMBB, MBBI, PPC::MovePCtoLR, 0, PPC::LR);
862 BuildMI(FirstMBB, MBBI, PPC::MFLR, 1, GlobalBaseReg).addReg(PPC::LR);
863 GlobalBaseInitialized = true;
864 }
865 return GlobalBaseReg;
866}
867
Nate Begeman6b559972005-04-01 02:59:27 +0000868/// getConstDouble - Loads a floating point value into a register, via the
869/// Constant Pool. Optionally takes a register in which to load the value.
870unsigned ISel::getConstDouble(double doubleVal, unsigned Result=0) {
871 unsigned Tmp1 = MakeReg(MVT::i32);
872 if (0 == Result) Result = MakeReg(MVT::f64);
873 MachineConstantPool *CP = BB->getParent()->getConstantPool();
874 ConstantFP *CFP = ConstantFP::get(Type::DoubleTy, doubleVal);
875 unsigned CPI = CP->getConstantPoolIndex(CFP);
876 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp1).addReg(getGlobalBaseReg())
877 .addConstantPoolIndex(CPI);
878 BuildMI(BB, PPC::LFD, 2, Result).addConstantPoolIndex(CPI).addReg(Tmp1);
879 return Result;
880}
881
Nate Begeman7ddecb42005-04-06 23:51:40 +0000882/// SelectBitfieldInsert - turn an or of two masked values into
883/// the rotate left word immediate then mask insert (rlwimi) instruction.
884/// Returns true on success, false if the caller still needs to select OR.
885///
886/// Patterns matched:
887/// 1. or shl, and 5. or and, and
888/// 2. or and, shl 6. or shl, shr
889/// 3. or shr, and 7. or shr, shl
890/// 4. or and, shr
891bool ISel::SelectBitfieldInsert(SDOperand OR, unsigned Result) {
Nate Begemancd08e4c2005-04-09 20:09:12 +0000892 bool IsRotate = false;
Nate Begeman7ddecb42005-04-06 23:51:40 +0000893 unsigned TgtMask = 0xFFFFFFFF, InsMask = 0xFFFFFFFF, Amount = 0;
894 unsigned Op0Opc = OR.getOperand(0).getOpcode();
895 unsigned Op1Opc = OR.getOperand(1).getOpcode();
896
897 // Verify that we have the correct opcodes
898 if (ISD::SHL != Op0Opc && ISD::SRL != Op0Opc && ISD::AND != Op0Opc)
899 return false;
900 if (ISD::SHL != Op1Opc && ISD::SRL != Op1Opc && ISD::AND != Op1Opc)
901 return false;
902
903 // Generate Mask value for Target
904 if (ConstantSDNode *CN =
905 dyn_cast<ConstantSDNode>(OR.getOperand(0).getOperand(1).Val)) {
906 switch(Op0Opc) {
907 case ISD::SHL: TgtMask <<= (unsigned)CN->getValue(); break;
908 case ISD::SRL: TgtMask >>= (unsigned)CN->getValue(); break;
909 case ISD::AND: TgtMask &= (unsigned)CN->getValue(); break;
910 }
911 } else {
912 return false;
913 }
914
915 // Generate Mask value for Insert
916 if (ConstantSDNode *CN =
917 dyn_cast<ConstantSDNode>(OR.getOperand(1).getOperand(1).Val)) {
918 switch(Op1Opc) {
919 case ISD::SHL:
920 Amount = CN->getValue();
Nate Begemancd08e4c2005-04-09 20:09:12 +0000921 InsMask <<= Amount;
922 if (Op0Opc == ISD::SRL) IsRotate = true;
Nate Begeman7ddecb42005-04-06 23:51:40 +0000923 break;
924 case ISD::SRL:
925 Amount = CN->getValue();
926 InsMask >>= Amount;
927 Amount = 32-Amount;
Nate Begemancd08e4c2005-04-09 20:09:12 +0000928 if (Op0Opc == ISD::SHL) IsRotate = true;
Nate Begeman7ddecb42005-04-06 23:51:40 +0000929 break;
930 case ISD::AND:
931 InsMask &= (unsigned)CN->getValue();
932 break;
933 }
934 } else {
935 return false;
936 }
937
938 // Verify that the Target mask and Insert mask together form a full word mask
939 // and that the Insert mask is a run of set bits (which implies both are runs
940 // of set bits). Given that, Select the arguments and generate the rlwimi
941 // instruction.
942 unsigned MB, ME;
943 if (((TgtMask ^ InsMask) == 0xFFFFFFFF) && IsRunOfOnes(InsMask, MB, ME)) {
944 unsigned Tmp1, Tmp2;
Nate Begemancd08e4c2005-04-09 20:09:12 +0000945 // Check for rotlwi / rotrwi here, a special case of bitfield insert
946 // where both bitfield halves are sourced from the same value.
947 if (IsRotate &&
948 OR.getOperand(0).getOperand(0) == OR.getOperand(1).getOperand(0)) {
Nate Begemancd08e4c2005-04-09 20:09:12 +0000949 Tmp1 = SelectExpr(OR.getOperand(0).getOperand(0));
950 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(Amount)
951 .addImm(0).addImm(31);
952 return true;
953 }
Nate Begeman7ddecb42005-04-06 23:51:40 +0000954 if (Op0Opc == ISD::AND)
955 Tmp1 = SelectExpr(OR.getOperand(0).getOperand(0));
956 else
957 Tmp1 = SelectExpr(OR.getOperand(0));
958 Tmp2 = SelectExpr(OR.getOperand(1).getOperand(0));
959 BuildMI(BB, PPC::RLWIMI, 5, Result).addReg(Tmp1).addReg(Tmp2)
960 .addImm(Amount).addImm(MB).addImm(ME);
961 return true;
962 }
963 return false;
964}
965
Nate Begemandffcfcc2005-04-01 00:32:34 +0000966unsigned ISel::SelectSetCR0(SDOperand CC) {
967 unsigned Opc, Tmp1, Tmp2;
Nate Begeman9765c252005-04-12 21:22:28 +0000968 bool AlreadySelected = false;
Nate Begemandffcfcc2005-04-01 00:32:34 +0000969 static const unsigned CompareOpcodes[] =
970 { PPC::FCMPU, PPC::FCMPU, PPC::CMPW, PPC::CMPLW };
971
972 // If the first operand to the select is a SETCC node, then we can fold it
973 // into the branch that selects which value to return.
974 SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(CC.Val);
975 if (SetCC && CC.getOpcode() == ISD::SETCC) {
976 bool U;
977 Opc = getBCCForSetCC(SetCC->getCondition(), U);
Nate Begemandffcfcc2005-04-01 00:32:34 +0000978
Nate Begeman439b4442005-04-05 04:22:58 +0000979 // Pass the optional argument U to getImmediateForOpcode for SETCC,
Nate Begemandffcfcc2005-04-01 00:32:34 +0000980 // so that it knows whether the SETCC immediate range is signed or not.
Nate Begeman439b4442005-04-05 04:22:58 +0000981 if (1 == getImmediateForOpcode(SetCC->getOperand(1), ISD::SETCC,
982 Tmp2, U)) {
Nate Begemanc7bd4822005-04-11 06:34:10 +0000983 // For comparisons against zero, we can implicity set CR0 if a recording
984 // variant (e.g. 'or.' instead of 'or') of the instruction that defines
985 // operand zero of the SetCC node is available.
986 if (0 == Tmp2 &&
Nate Begeman9765c252005-04-12 21:22:28 +0000987 NodeHasRecordingVariant(SetCC->getOperand(0).getOpcode()) &&
988 SetCC->getOperand(0).Val->hasOneUse()) {
Nate Begemanc7bd4822005-04-11 06:34:10 +0000989 RecordSuccess = false;
990 Tmp1 = SelectExpr(SetCC->getOperand(0), true);
991 if (RecordSuccess) {
992 ++Recorded;
993 return Opc;
994 }
995 AlreadySelected = true;
996 }
997 // If we could not implicitly set CR0, then emit a compare immediate
998 // instead.
999 if (!AlreadySelected) Tmp1 = SelectExpr(SetCC->getOperand(0));
Nate Begemandffcfcc2005-04-01 00:32:34 +00001000 if (U)
1001 BuildMI(BB, PPC::CMPLWI, 2, PPC::CR0).addReg(Tmp1).addImm(Tmp2);
1002 else
1003 BuildMI(BB, PPC::CMPWI, 2, PPC::CR0).addReg(Tmp1).addSImm(Tmp2);
1004 } else {
1005 bool IsInteger = MVT::isInteger(SetCC->getOperand(0).getValueType());
1006 unsigned CompareOpc = CompareOpcodes[2 * IsInteger + U];
Nate Begemanc7bd4822005-04-11 06:34:10 +00001007 Tmp1 = SelectExpr(SetCC->getOperand(0));
Nate Begemandffcfcc2005-04-01 00:32:34 +00001008 Tmp2 = SelectExpr(SetCC->getOperand(1));
1009 BuildMI(BB, CompareOpc, 2, PPC::CR0).addReg(Tmp1).addReg(Tmp2);
1010 }
1011 } else {
Nate Begeman9765c252005-04-12 21:22:28 +00001012 Opc = PPC::BNE;
Nate Begemandffcfcc2005-04-01 00:32:34 +00001013 Tmp1 = SelectExpr(CC);
1014 BuildMI(BB, PPC::CMPLWI, 2, PPC::CR0).addReg(Tmp1).addImm(0);
Nate Begemandffcfcc2005-04-01 00:32:34 +00001015 }
1016 return Opc;
1017}
1018
1019/// Check to see if the load is a constant offset from a base register
Nate Begeman04730362005-04-01 04:45:11 +00001020bool ISel::SelectAddr(SDOperand N, unsigned& Reg, int& offset)
Nate Begemana9795f82005-03-24 04:41:43 +00001021{
Nate Begeman96fc6812005-03-31 02:05:53 +00001022 unsigned imm = 0, opcode = N.getOpcode();
Nate Begeman04730362005-04-01 04:45:11 +00001023 if (N.getOpcode() == ISD::ADD) {
1024 Reg = SelectExpr(N.getOperand(0));
Nate Begeman439b4442005-04-05 04:22:58 +00001025 if (1 == getImmediateForOpcode(N.getOperand(1), opcode, imm)) {
Nate Begeman96fc6812005-03-31 02:05:53 +00001026 offset = imm;
Nate Begeman04730362005-04-01 04:45:11 +00001027 return false;
1028 }
1029 offset = SelectExpr(N.getOperand(1));
1030 return true;
1031 }
Nate Begemana9795f82005-03-24 04:41:43 +00001032 Reg = SelectExpr(N);
1033 offset = 0;
Nate Begeman04730362005-04-01 04:45:11 +00001034 return false;
Nate Begemana9795f82005-03-24 04:41:43 +00001035}
1036
1037void ISel::SelectBranchCC(SDOperand N)
1038{
Nate Begemana9795f82005-03-24 04:41:43 +00001039 MachineBasicBlock *Dest =
1040 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
Nate Begeman3e897162005-03-31 23:55:40 +00001041
Nate Begemana9795f82005-03-24 04:41:43 +00001042 Select(N.getOperand(0)); //chain
Nate Begemandffcfcc2005-04-01 00:32:34 +00001043 unsigned Opc = SelectSetCR0(N.getOperand(1));
Nate Begemancd08e4c2005-04-09 20:09:12 +00001044
1045 // Iterate to the next basic block, unless we're already at the end of the
1046 ilist<MachineBasicBlock>::iterator It = BB, E = BB->getParent()->end();
Nate Begeman706471e2005-04-09 23:35:05 +00001047 if (++It == E) It = BB;
Nate Begemancd08e4c2005-04-09 20:09:12 +00001048
1049 // If this is a two way branch, then grab the fallthrough basic block argument
1050 // and build a PowerPC branch pseudo-op, suitable for long branch conversion
1051 // if necessary by the branch selection pass. Otherwise, emit a standard
1052 // conditional branch.
1053 if (N.getOpcode() == ISD::BRCONDTWOWAY) {
1054 MachineBasicBlock *Fallthrough =
1055 cast<BasicBlockSDNode>(N.getOperand(3))->getBasicBlock();
1056 if (Dest != It) {
1057 BuildMI(BB, PPC::COND_BRANCH, 4).addReg(PPC::CR0).addImm(Opc)
1058 .addMBB(Dest).addMBB(Fallthrough);
1059 if (Fallthrough != It)
1060 BuildMI(BB, PPC::B, 1).addMBB(Fallthrough);
1061 } else {
1062 if (Fallthrough != It) {
1063 Opc = PPC32InstrInfo::invertPPCBranchOpcode(Opc);
1064 BuildMI(BB, PPC::COND_BRANCH, 4).addReg(PPC::CR0).addImm(Opc)
1065 .addMBB(Fallthrough).addMBB(Dest);
1066 }
1067 }
1068 } else {
Nate Begeman27499e32005-04-10 01:48:29 +00001069 BuildMI(BB, PPC::COND_BRANCH, 4).addReg(PPC::CR0).addImm(Opc)
1070 .addMBB(Dest).addMBB(It);
1071 //BuildMI(BB, Opc, 2).addReg(PPC::CR0).addMBB(Dest);
Nate Begemancd08e4c2005-04-09 20:09:12 +00001072 }
Nate Begemana9795f82005-03-24 04:41:43 +00001073 return;
1074}
1075
1076unsigned ISel::SelectExprFP(SDOperand N, unsigned Result)
1077{
1078 unsigned Tmp1, Tmp2, Tmp3;
1079 unsigned Opc = 0;
1080 SDNode *Node = N.Val;
1081 MVT::ValueType DestType = N.getValueType();
1082 unsigned opcode = N.getOpcode();
1083
1084 switch (opcode) {
1085 default:
1086 Node->dump();
1087 assert(0 && "Node not handled!\n");
1088
Nate Begeman23afcfb2005-03-29 22:48:55 +00001089 case ISD::SELECT: {
Nate Begeman3e897162005-03-31 23:55:40 +00001090 // Attempt to generate FSEL. We can do this whenever we have an FP result,
1091 // and an FP comparison in the SetCC node.
1092 SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(N.getOperand(0).Val);
1093 if (SetCC && N.getOperand(0).getOpcode() == ISD::SETCC &&
1094 !MVT::isInteger(SetCC->getOperand(0).getValueType()) &&
1095 SetCC->getCondition() != ISD::SETEQ &&
1096 SetCC->getCondition() != ISD::SETNE) {
1097 MVT::ValueType VT = SetCC->getOperand(0).getValueType();
Nate Begeman3e897162005-03-31 23:55:40 +00001098 unsigned TV = SelectExpr(N.getOperand(1)); // Use if TRUE
1099 unsigned FV = SelectExpr(N.getOperand(2)); // Use if FALSE
1100
1101 ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(1));
1102 if (CN && (CN->isExactlyValue(-0.0) || CN->isExactlyValue(0.0))) {
1103 switch(SetCC->getCondition()) {
1104 default: assert(0 && "Invalid FSEL condition"); abort();
1105 case ISD::SETULT:
1106 case ISD::SETLT:
Nate Begemanaf4ab1b2005-04-09 09:33:07 +00001107 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
Nate Begeman3e897162005-03-31 23:55:40 +00001108 case ISD::SETUGE:
1109 case ISD::SETGE:
Nate Begemanaf4ab1b2005-04-09 09:33:07 +00001110 Tmp1 = SelectExpr(SetCC->getOperand(0)); // Val to compare against
Nate Begeman3e897162005-03-31 23:55:40 +00001111 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp1).addReg(TV).addReg(FV);
1112 return Result;
1113 case ISD::SETUGT:
Nate Begemanaf4ab1b2005-04-09 09:33:07 +00001114 case ISD::SETGT:
1115 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
Nate Begeman3e897162005-03-31 23:55:40 +00001116 case ISD::SETULE:
1117 case ISD::SETLE: {
Nate Begemanaf4ab1b2005-04-09 09:33:07 +00001118 if (SetCC->getOperand(0).getOpcode() == ISD::FNEG) {
1119 Tmp2 = SelectExpr(SetCC->getOperand(0).getOperand(0));
1120 } else {
1121 Tmp2 = MakeReg(VT);
1122 Tmp1 = SelectExpr(SetCC->getOperand(0)); // Val to compare against
1123 BuildMI(BB, PPC::FNEG, 1, Tmp2).addReg(Tmp1);
1124 }
Nate Begeman3e897162005-03-31 23:55:40 +00001125 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp2).addReg(TV).addReg(FV);
1126 return Result;
1127 }
1128 }
1129 } else {
1130 Opc = (MVT::f64 == VT) ? PPC::FSUB : PPC::FSUBS;
Nate Begemanaf4ab1b2005-04-09 09:33:07 +00001131 Tmp1 = SelectExpr(SetCC->getOperand(0)); // Val to compare against
Nate Begeman3e897162005-03-31 23:55:40 +00001132 Tmp2 = SelectExpr(SetCC->getOperand(1));
1133 Tmp3 = MakeReg(VT);
1134 switch(SetCC->getCondition()) {
1135 default: assert(0 && "Invalid FSEL condition"); abort();
1136 case ISD::SETULT:
1137 case ISD::SETLT:
1138 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
1139 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(FV).addReg(TV);
1140 return Result;
1141 case ISD::SETUGE:
1142 case ISD::SETGE:
1143 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
1144 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(TV).addReg(FV);
1145 return Result;
1146 case ISD::SETUGT:
1147 case ISD::SETGT:
1148 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp2).addReg(Tmp1);
1149 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(FV).addReg(TV);
1150 return Result;
1151 case ISD::SETULE:
1152 case ISD::SETLE:
1153 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp2).addReg(Tmp1);
1154 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(TV).addReg(FV);
1155 return Result;
1156 }
1157 }
1158 assert(0 && "Should never get here");
1159 return 0;
1160 }
1161
Nate Begeman31318e42005-04-01 07:21:30 +00001162 unsigned TrueValue = SelectExpr(N.getOperand(1)); //Use if TRUE
1163 unsigned FalseValue = SelectExpr(N.getOperand(2)); //Use if FALSE
Nate Begeman6cb2e1b2005-04-01 08:57:43 +00001164 Opc = SelectSetCR0(N.getOperand(0));
Nate Begeman31318e42005-04-01 07:21:30 +00001165
Nate Begeman23afcfb2005-03-29 22:48:55 +00001166 // Create an iterator with which to insert the MBB for copying the false
1167 // value and the MBB to hold the PHI instruction for this SetCC.
1168 MachineBasicBlock *thisMBB = BB;
1169 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1170 ilist<MachineBasicBlock>::iterator It = BB;
1171 ++It;
1172
1173 // thisMBB:
1174 // ...
1175 // TrueVal = ...
1176 // cmpTY cr0, r1, r2
1177 // bCC copy1MBB
1178 // fallthrough --> copy0MBB
Nate Begeman23afcfb2005-03-29 22:48:55 +00001179 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
1180 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
Nate Begeman6cb2e1b2005-04-01 08:57:43 +00001181 BuildMI(BB, Opc, 2).addReg(PPC::CR0).addMBB(sinkMBB);
Nate Begeman23afcfb2005-03-29 22:48:55 +00001182 MachineFunction *F = BB->getParent();
1183 F->getBasicBlockList().insert(It, copy0MBB);
1184 F->getBasicBlockList().insert(It, sinkMBB);
1185 // Update machine-CFG edges
1186 BB->addSuccessor(copy0MBB);
1187 BB->addSuccessor(sinkMBB);
1188
1189 // copy0MBB:
1190 // %FalseValue = ...
1191 // # fallthrough to sinkMBB
1192 BB = copy0MBB;
Nate Begeman23afcfb2005-03-29 22:48:55 +00001193 // Update machine-CFG edges
1194 BB->addSuccessor(sinkMBB);
1195
1196 // sinkMBB:
1197 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1198 // ...
1199 BB = sinkMBB;
1200 BuildMI(BB, PPC::PHI, 4, Result).addReg(FalseValue)
1201 .addMBB(copy0MBB).addReg(TrueValue).addMBB(thisMBB);
1202 return Result;
1203 }
Nate Begeman27eeb002005-04-02 05:59:34 +00001204
1205 case ISD::FNEG:
Nate Begeman93075ec2005-04-04 23:40:36 +00001206 if (!NoExcessFPPrecision &&
1207 ISD::ADD == N.getOperand(0).getOpcode() &&
1208 N.getOperand(0).Val->hasOneUse() &&
1209 ISD::MUL == N.getOperand(0).getOperand(0).getOpcode() &&
1210 N.getOperand(0).getOperand(0).Val->hasOneUse()) {
Nate Begeman80196b12005-04-05 00:15:08 +00001211 ++FusedFP; // Statistic
Nate Begeman93075ec2005-04-04 23:40:36 +00001212 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(0));
1213 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(1));
1214 Tmp3 = SelectExpr(N.getOperand(0).getOperand(1));
1215 Opc = DestType == MVT::f64 ? PPC::FNMADD : PPC::FNMADDS;
1216 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1217 } else if (!NoExcessFPPrecision &&
Nate Begemane88aa5b2005-04-09 03:05:51 +00001218 ISD::ADD == N.getOperand(0).getOpcode() &&
Nate Begeman93075ec2005-04-04 23:40:36 +00001219 N.getOperand(0).Val->hasOneUse() &&
Nate Begemane88aa5b2005-04-09 03:05:51 +00001220 ISD::MUL == N.getOperand(0).getOperand(1).getOpcode() &&
1221 N.getOperand(0).getOperand(1).Val->hasOneUse()) {
Nate Begeman80196b12005-04-05 00:15:08 +00001222 ++FusedFP; // Statistic
Nate Begemane88aa5b2005-04-09 03:05:51 +00001223 Tmp1 = SelectExpr(N.getOperand(0).getOperand(1).getOperand(0));
1224 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1).getOperand(1));
1225 Tmp3 = SelectExpr(N.getOperand(0).getOperand(0));
1226 Opc = DestType == MVT::f64 ? PPC::FNMADD : PPC::FNMADDS;
Nate Begeman93075ec2005-04-04 23:40:36 +00001227 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1228 } else if (ISD::FABS == N.getOperand(0).getOpcode()) {
Nate Begeman27eeb002005-04-02 05:59:34 +00001229 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1230 BuildMI(BB, PPC::FNABS, 1, Result).addReg(Tmp1);
1231 } else {
1232 Tmp1 = SelectExpr(N.getOperand(0));
1233 BuildMI(BB, PPC::FNEG, 1, Result).addReg(Tmp1);
1234 }
1235 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001236
Nate Begeman27eeb002005-04-02 05:59:34 +00001237 case ISD::FABS:
1238 Tmp1 = SelectExpr(N.getOperand(0));
1239 BuildMI(BB, PPC::FABS, 1, Result).addReg(Tmp1);
1240 return Result;
1241
Nate Begemana9795f82005-03-24 04:41:43 +00001242 case ISD::FP_ROUND:
1243 assert (DestType == MVT::f32 &&
1244 N.getOperand(0).getValueType() == MVT::f64 &&
1245 "only f64 to f32 conversion supported here");
1246 Tmp1 = SelectExpr(N.getOperand(0));
1247 BuildMI(BB, PPC::FRSP, 1, Result).addReg(Tmp1);
1248 return Result;
1249
1250 case ISD::FP_EXTEND:
1251 assert (DestType == MVT::f64 &&
1252 N.getOperand(0).getValueType() == MVT::f32 &&
1253 "only f32 to f64 conversion supported here");
1254 Tmp1 = SelectExpr(N.getOperand(0));
1255 BuildMI(BB, PPC::FMR, 1, Result).addReg(Tmp1);
1256 return Result;
1257
1258 case ISD::CopyFromReg:
Nate Begemanf2622612005-03-26 02:17:46 +00001259 if (Result == 1)
1260 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1261 Tmp1 = dyn_cast<RegSDNode>(Node)->getReg();
1262 BuildMI(BB, PPC::FMR, 1, Result).addReg(Tmp1);
1263 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001264
Nate Begeman6d369cc2005-04-01 01:08:07 +00001265 case ISD::ConstantFP: {
Nate Begeman6d369cc2005-04-01 01:08:07 +00001266 ConstantFPSDNode *CN = cast<ConstantFPSDNode>(N);
Nate Begeman6b559972005-04-01 02:59:27 +00001267 Result = getConstDouble(CN->getValue(), Result);
Nate Begeman6d369cc2005-04-01 01:08:07 +00001268 return Result;
1269 }
Nate Begemana9795f82005-03-24 04:41:43 +00001270
Nate Begemana9795f82005-03-24 04:41:43 +00001271 case ISD::ADD:
Nate Begeman93075ec2005-04-04 23:40:36 +00001272 if (!NoExcessFPPrecision && N.getOperand(0).getOpcode() == ISD::MUL &&
1273 N.getOperand(0).Val->hasOneUse()) {
1274 ++FusedFP; // Statistic
1275 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1276 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1277 Tmp3 = SelectExpr(N.getOperand(1));
1278 Opc = DestType == MVT::f64 ? PPC::FMADD : PPC::FMADDS;
1279 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1280 return Result;
1281 }
Nate Begemane88aa5b2005-04-09 03:05:51 +00001282 if (!NoExcessFPPrecision && N.getOperand(1).getOpcode() == ISD::MUL &&
1283 N.getOperand(1).Val->hasOneUse()) {
1284 ++FusedFP; // Statistic
1285 Tmp1 = SelectExpr(N.getOperand(1).getOperand(0));
1286 Tmp2 = SelectExpr(N.getOperand(1).getOperand(1));
1287 Tmp3 = SelectExpr(N.getOperand(0));
1288 Opc = DestType == MVT::f64 ? PPC::FMADD : PPC::FMADDS;
1289 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1290 return Result;
1291 }
Nate Begeman93075ec2005-04-04 23:40:36 +00001292 Opc = DestType == MVT::f64 ? PPC::FADD : PPC::FADDS;
1293 Tmp1 = SelectExpr(N.getOperand(0));
1294 Tmp2 = SelectExpr(N.getOperand(1));
1295 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1296 return Result;
1297
Nate Begemana9795f82005-03-24 04:41:43 +00001298 case ISD::SUB:
Nate Begeman93075ec2005-04-04 23:40:36 +00001299 if (!NoExcessFPPrecision && N.getOperand(0).getOpcode() == ISD::MUL &&
1300 N.getOperand(0).Val->hasOneUse()) {
1301 ++FusedFP; // Statistic
1302 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1303 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1304 Tmp3 = SelectExpr(N.getOperand(1));
1305 Opc = DestType == MVT::f64 ? PPC::FMSUB : PPC::FMSUBS;
1306 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1307 return Result;
1308 }
Nate Begemane88aa5b2005-04-09 03:05:51 +00001309 if (!NoExcessFPPrecision && N.getOperand(1).getOpcode() == ISD::MUL &&
1310 N.getOperand(1).Val->hasOneUse()) {
1311 ++FusedFP; // Statistic
1312 Tmp1 = SelectExpr(N.getOperand(1).getOperand(0));
1313 Tmp2 = SelectExpr(N.getOperand(1).getOperand(1));
1314 Tmp3 = SelectExpr(N.getOperand(0));
1315 Opc = DestType == MVT::f64 ? PPC::FNMSUB : PPC::FNMSUBS;
1316 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1317 return Result;
1318 }
Nate Begeman93075ec2005-04-04 23:40:36 +00001319 Opc = DestType == MVT::f64 ? PPC::FSUB : PPC::FSUBS;
1320 Tmp1 = SelectExpr(N.getOperand(0));
1321 Tmp2 = SelectExpr(N.getOperand(1));
1322 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1323 return Result;
1324
1325 case ISD::MUL:
Nate Begemana9795f82005-03-24 04:41:43 +00001326 case ISD::SDIV:
1327 switch( opcode ) {
1328 case ISD::MUL: Opc = DestType == MVT::f64 ? PPC::FMUL : PPC::FMULS; break;
Nate Begemana9795f82005-03-24 04:41:43 +00001329 case ISD::SDIV: Opc = DestType == MVT::f64 ? PPC::FDIV : PPC::FDIVS; break;
1330 };
Nate Begemana9795f82005-03-24 04:41:43 +00001331 Tmp1 = SelectExpr(N.getOperand(0));
1332 Tmp2 = SelectExpr(N.getOperand(1));
1333 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1334 return Result;
1335
Nate Begemana9795f82005-03-24 04:41:43 +00001336 case ISD::UINT_TO_FP:
Nate Begemanfdcf3412005-03-30 19:38:35 +00001337 case ISD::SINT_TO_FP: {
1338 assert (N.getOperand(0).getValueType() == MVT::i32
1339 && "int to float must operate on i32");
1340 bool IsUnsigned = (ISD::UINT_TO_FP == opcode);
1341 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
1342 Tmp2 = MakeReg(MVT::f64); // temp reg to load the integer value into
1343 Tmp3 = MakeReg(MVT::i32); // temp reg to hold the conversion constant
Nate Begemanfdcf3412005-03-30 19:38:35 +00001344
1345 int FrameIdx = BB->getParent()->getFrameInfo()->CreateStackObject(8, 8);
1346 MachineConstantPool *CP = BB->getParent()->getConstantPool();
1347
Nate Begemanfdcf3412005-03-30 19:38:35 +00001348 if (IsUnsigned) {
Nate Begeman709c8062005-04-10 06:06:10 +00001349 unsigned ConstF = getConstDouble(0x1.000000p52);
Nate Begemanfdcf3412005-03-30 19:38:35 +00001350 // Store the hi & low halves of the fp value, currently in int regs
1351 BuildMI(BB, PPC::LIS, 1, Tmp3).addSImm(0x4330);
1352 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(Tmp3), FrameIdx);
1353 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(Tmp1), FrameIdx, 4);
1354 addFrameReference(BuildMI(BB, PPC::LFD, 2, Tmp2), FrameIdx);
1355 // Generate the return value with a subtract
1356 BuildMI(BB, PPC::FSUB, 2, Result).addReg(Tmp2).addReg(ConstF);
1357 } else {
Nate Begeman709c8062005-04-10 06:06:10 +00001358 unsigned ConstF = getConstDouble(0x1.000008p52);
Nate Begemanfdcf3412005-03-30 19:38:35 +00001359 unsigned TmpL = MakeReg(MVT::i32);
Nate Begemanfdcf3412005-03-30 19:38:35 +00001360 // Store the hi & low halves of the fp value, currently in int regs
1361 BuildMI(BB, PPC::LIS, 1, Tmp3).addSImm(0x4330);
1362 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(Tmp3), FrameIdx);
1363 BuildMI(BB, PPC::XORIS, 2, TmpL).addReg(Tmp1).addImm(0x8000);
1364 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(TmpL), FrameIdx, 4);
1365 addFrameReference(BuildMI(BB, PPC::LFD, 2, Tmp2), FrameIdx);
1366 // Generate the return value with a subtract
1367 BuildMI(BB, PPC::FSUB, 2, Result).addReg(Tmp2).addReg(ConstF);
1368 }
1369 return Result;
1370 }
Nate Begemana9795f82005-03-24 04:41:43 +00001371 }
Nate Begeman6b559972005-04-01 02:59:27 +00001372 assert(0 && "Should never get here");
Nate Begemana9795f82005-03-24 04:41:43 +00001373 return 0;
1374}
1375
Nate Begemanc7bd4822005-04-11 06:34:10 +00001376unsigned ISel::SelectExpr(SDOperand N, bool Recording) {
Nate Begemana9795f82005-03-24 04:41:43 +00001377 unsigned Result;
1378 unsigned Tmp1, Tmp2, Tmp3;
1379 unsigned Opc = 0;
1380 unsigned opcode = N.getOpcode();
1381
1382 SDNode *Node = N.Val;
1383 MVT::ValueType DestType = N.getValueType();
1384
1385 unsigned &Reg = ExprMap[N];
1386 if (Reg) return Reg;
1387
Nate Begeman27eeb002005-04-02 05:59:34 +00001388 switch (N.getOpcode()) {
1389 default:
Nate Begemana9795f82005-03-24 04:41:43 +00001390 Reg = Result = (N.getValueType() != MVT::Other) ?
Nate Begeman27eeb002005-04-02 05:59:34 +00001391 MakeReg(N.getValueType()) : 1;
1392 break;
1393 case ISD::CALL:
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001394 // If this is a call instruction, make sure to prepare ALL of the result
1395 // values as well as the chain.
Nate Begeman27eeb002005-04-02 05:59:34 +00001396 if (Node->getNumValues() == 1)
1397 Reg = Result = 1; // Void call, just a chain.
1398 else {
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001399 Result = MakeReg(Node->getValueType(0));
1400 ExprMap[N.getValue(0)] = Result;
Nate Begeman27eeb002005-04-02 05:59:34 +00001401 for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001402 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
Nate Begeman27eeb002005-04-02 05:59:34 +00001403 ExprMap[SDOperand(Node, Node->getNumValues()-1)] = 1;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001404 }
Nate Begeman27eeb002005-04-02 05:59:34 +00001405 break;
1406 case ISD::ADD_PARTS:
1407 case ISD::SUB_PARTS:
1408 case ISD::SHL_PARTS:
1409 case ISD::SRL_PARTS:
1410 case ISD::SRA_PARTS:
1411 Result = MakeReg(Node->getValueType(0));
1412 ExprMap[N.getValue(0)] = Result;
1413 for (unsigned i = 1, e = N.Val->getNumValues(); i != e; ++i)
1414 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
1415 break;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001416 }
1417
Nate Begemane5846682005-04-04 06:52:38 +00001418 if (ISD::CopyFromReg == opcode)
1419 DestType = N.getValue(0).getValueType();
1420
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001421 if (DestType == MVT::f64 || DestType == MVT::f32)
Nate Begemana0e3e942005-04-10 01:14:13 +00001422 if (ISD::LOAD != opcode && ISD::EXTLOAD != opcode &&
1423 ISD::UNDEF != opcode && ISD::CALL != opcode)
Nate Begeman74d73452005-03-31 00:15:26 +00001424 return SelectExprFP(N, Result);
Nate Begemana9795f82005-03-24 04:41:43 +00001425
1426 switch (opcode) {
1427 default:
1428 Node->dump();
1429 assert(0 && "Node not handled!\n");
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001430 case ISD::UNDEF:
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001431 BuildMI(BB, PPC::IMPLICIT_DEF, 0, Result);
1432 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001433 case ISD::DYNAMIC_STACKALLOC:
Nate Begeman5e966612005-03-24 06:28:42 +00001434 // Generate both result values. FIXME: Need a better commment here?
1435 if (Result != 1)
1436 ExprMap[N.getValue(1)] = 1;
1437 else
1438 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1439
1440 // FIXME: We are currently ignoring the requested alignment for handling
1441 // greater than the stack alignment. This will need to be revisited at some
1442 // point. Align = N.getOperand(2);
1443 if (!isa<ConstantSDNode>(N.getOperand(2)) ||
1444 cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) {
1445 std::cerr << "Cannot allocate stack object with greater alignment than"
1446 << " the stack alignment yet!";
1447 abort();
1448 }
1449 Select(N.getOperand(0));
1450 Tmp1 = SelectExpr(N.getOperand(1));
1451 // Subtract size from stack pointer, thereby allocating some space.
1452 BuildMI(BB, PPC::SUBF, 2, PPC::R1).addReg(Tmp1).addReg(PPC::R1);
1453 // Put a pointer to the space into the result register by copying the SP
1454 BuildMI(BB, PPC::OR, 2, Result).addReg(PPC::R1).addReg(PPC::R1);
1455 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001456
1457 case ISD::ConstantPool:
Nate Begemanca12a2b2005-03-28 22:28:37 +00001458 Tmp1 = cast<ConstantPoolSDNode>(N)->getIndex();
1459 Tmp2 = MakeReg(MVT::i32);
1460 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp2).addReg(getGlobalBaseReg())
1461 .addConstantPoolIndex(Tmp1);
1462 BuildMI(BB, PPC::LA, 2, Result).addReg(Tmp2).addConstantPoolIndex(Tmp1);
1463 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001464
1465 case ISD::FrameIndex:
Nate Begemanf3d08f32005-03-29 00:03:27 +00001466 Tmp1 = cast<FrameIndexSDNode>(N)->getIndex();
Nate Begeman58f718c2005-03-30 02:23:08 +00001467 addFrameReference(BuildMI(BB, PPC::ADDI, 2, Result), (int)Tmp1, 0, false);
Nate Begemanf3d08f32005-03-29 00:03:27 +00001468 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001469
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001470 case ISD::GlobalAddress: {
1471 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
Nate Begemanca12a2b2005-03-28 22:28:37 +00001472 Tmp1 = MakeReg(MVT::i32);
Nate Begemanc7b09f12005-03-25 08:34:25 +00001473 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp1).addReg(getGlobalBaseReg())
1474 .addGlobalAddress(GV);
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001475 if (GV->hasWeakLinkage() || GV->isExternal()) {
1476 BuildMI(BB, PPC::LWZ, 2, Result).addGlobalAddress(GV).addReg(Tmp1);
1477 } else {
1478 BuildMI(BB, PPC::LA, 2, Result).addReg(Tmp1).addGlobalAddress(GV);
1479 }
1480 return Result;
1481 }
1482
Nate Begeman5e966612005-03-24 06:28:42 +00001483 case ISD::LOAD:
Nate Begemana9795f82005-03-24 04:41:43 +00001484 case ISD::EXTLOAD:
1485 case ISD::ZEXTLOAD:
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001486 case ISD::SEXTLOAD: {
Nate Begeman9db505c2005-03-28 19:36:43 +00001487 MVT::ValueType TypeBeingLoaded = (ISD::LOAD == opcode) ?
1488 Node->getValueType(0) : cast<MVTSDNode>(Node)->getExtraValueType();
Nate Begeman74d73452005-03-31 00:15:26 +00001489 bool sext = (ISD::SEXTLOAD == opcode);
Nate Begeman74d73452005-03-31 00:15:26 +00001490
Nate Begeman5e966612005-03-24 06:28:42 +00001491 // Make sure we generate both values.
1492 if (Result != 1)
1493 ExprMap[N.getValue(1)] = 1; // Generate the token
1494 else
1495 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1496
1497 SDOperand Chain = N.getOperand(0);
1498 SDOperand Address = N.getOperand(1);
1499 Select(Chain);
1500
Nate Begeman9db505c2005-03-28 19:36:43 +00001501 switch (TypeBeingLoaded) {
Nate Begeman74d73452005-03-31 00:15:26 +00001502 default: Node->dump(); assert(0 && "Cannot load this type!");
Nate Begeman9db505c2005-03-28 19:36:43 +00001503 case MVT::i1: Opc = PPC::LBZ; break;
1504 case MVT::i8: Opc = PPC::LBZ; break;
1505 case MVT::i16: Opc = sext ? PPC::LHA : PPC::LHZ; break;
1506 case MVT::i32: Opc = PPC::LWZ; break;
Nate Begeman74d73452005-03-31 00:15:26 +00001507 case MVT::f32: Opc = PPC::LFS; break;
1508 case MVT::f64: Opc = PPC::LFD; break;
Nate Begeman5e966612005-03-24 06:28:42 +00001509 }
1510
Nate Begeman74d73452005-03-31 00:15:26 +00001511 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Address)) {
1512 Tmp1 = MakeReg(MVT::i32);
1513 int CPI = CP->getIndex();
1514 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp1).addReg(getGlobalBaseReg())
1515 .addConstantPoolIndex(CPI);
1516 BuildMI(BB, Opc, 2, Result).addConstantPoolIndex(CPI).addReg(Tmp1);
Nate Begeman9db505c2005-03-28 19:36:43 +00001517 }
Nate Begeman74d73452005-03-31 00:15:26 +00001518 else if(Address.getOpcode() == ISD::FrameIndex) {
Nate Begeman58f718c2005-03-30 02:23:08 +00001519 Tmp1 = cast<FrameIndexSDNode>(Address)->getIndex();
1520 addFrameReference(BuildMI(BB, Opc, 2, Result), (int)Tmp1);
Nate Begeman5e966612005-03-24 06:28:42 +00001521 } else {
1522 int offset;
Nate Begeman04730362005-04-01 04:45:11 +00001523 bool idx = SelectAddr(Address, Tmp1, offset);
1524 if (idx) {
1525 Opc = IndexedOpForOp(Opc);
1526 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(offset);
1527 } else {
1528 BuildMI(BB, Opc, 2, Result).addSImm(offset).addReg(Tmp1);
1529 }
Nate Begeman5e966612005-03-24 06:28:42 +00001530 }
1531 return Result;
1532 }
1533
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001534 case ISD::CALL: {
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001535 unsigned GPR_idx = 0, FPR_idx = 0;
1536 static const unsigned GPR[] = {
1537 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
1538 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
1539 };
1540 static const unsigned FPR[] = {
1541 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
1542 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
1543 };
1544
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001545 // Lower the chain for this call.
1546 Select(N.getOperand(0));
1547 ExprMap[N.getValue(Node->getNumValues()-1)] = 1;
Nate Begeman74d73452005-03-31 00:15:26 +00001548
Nate Begemand860aa62005-04-04 22:17:48 +00001549 MachineInstr *CallMI;
1550 // Emit the correct call instruction based on the type of symbol called.
1551 if (GlobalAddressSDNode *GASD =
1552 dyn_cast<GlobalAddressSDNode>(N.getOperand(1))) {
1553 CallMI = BuildMI(PPC::CALLpcrel, 1).addGlobalAddress(GASD->getGlobal(),
1554 true);
1555 } else if (ExternalSymbolSDNode *ESSDN =
1556 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1))) {
1557 CallMI = BuildMI(PPC::CALLpcrel, 1).addExternalSymbol(ESSDN->getSymbol(),
1558 true);
1559 } else {
1560 Tmp1 = SelectExpr(N.getOperand(1));
1561 BuildMI(BB, PPC::OR, 2, PPC::R12).addReg(Tmp1).addReg(Tmp1);
1562 BuildMI(BB, PPC::MTCTR, 1).addReg(PPC::R12);
1563 CallMI = BuildMI(PPC::CALLindirect, 3).addImm(20).addImm(0)
1564 .addReg(PPC::R12);
1565 }
1566
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001567 // Load the register args to virtual regs
1568 std::vector<unsigned> ArgVR;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001569 for(int i = 2, e = Node->getNumOperands(); i < e; ++i)
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001570 ArgVR.push_back(SelectExpr(N.getOperand(i)));
1571
1572 // Copy the virtual registers into the appropriate argument register
1573 for(int i = 0, e = ArgVR.size(); i < e; ++i) {
1574 switch(N.getOperand(i+2).getValueType()) {
1575 default: Node->dump(); assert(0 && "Unknown value type for call");
1576 case MVT::i1:
1577 case MVT::i8:
1578 case MVT::i16:
1579 case MVT::i32:
1580 assert(GPR_idx < 8 && "Too many int args");
Nate Begemand860aa62005-04-04 22:17:48 +00001581 if (N.getOperand(i+2).getOpcode() != ISD::UNDEF) {
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001582 BuildMI(BB, PPC::OR,2,GPR[GPR_idx]).addReg(ArgVR[i]).addReg(ArgVR[i]);
Nate Begemand860aa62005-04-04 22:17:48 +00001583 CallMI->addRegOperand(GPR[GPR_idx], MachineOperand::Use);
1584 }
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001585 ++GPR_idx;
1586 break;
1587 case MVT::f64:
1588 case MVT::f32:
1589 assert(FPR_idx < 13 && "Too many fp args");
1590 BuildMI(BB, PPC::FMR, 1, FPR[FPR_idx]).addReg(ArgVR[i]);
Nate Begemand860aa62005-04-04 22:17:48 +00001591 CallMI->addRegOperand(FPR[FPR_idx], MachineOperand::Use);
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001592 ++FPR_idx;
1593 break;
1594 }
1595 }
Nate Begemand860aa62005-04-04 22:17:48 +00001596
1597 // Put the call instruction in the correct place in the MachineBasicBlock
1598 BB->push_back(CallMI);
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001599
1600 switch (Node->getValueType(0)) {
1601 default: assert(0 && "Unknown value type for call result!");
1602 case MVT::Other: return 1;
1603 case MVT::i1:
1604 case MVT::i8:
1605 case MVT::i16:
1606 case MVT::i32:
Nate Begemane5846682005-04-04 06:52:38 +00001607 if (Node->getValueType(1) == MVT::i32) {
1608 BuildMI(BB, PPC::OR, 2, Result+1).addReg(PPC::R3).addReg(PPC::R3);
1609 BuildMI(BB, PPC::OR, 2, Result).addReg(PPC::R4).addReg(PPC::R4);
1610 } else {
1611 BuildMI(BB, PPC::OR, 2, Result).addReg(PPC::R3).addReg(PPC::R3);
1612 }
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001613 break;
1614 case MVT::f32:
1615 case MVT::f64:
1616 BuildMI(BB, PPC::FMR, 1, Result).addReg(PPC::F1);
1617 break;
1618 }
1619 return Result+N.ResNo;
1620 }
Nate Begemana9795f82005-03-24 04:41:43 +00001621
1622 case ISD::SIGN_EXTEND:
1623 case ISD::SIGN_EXTEND_INREG:
1624 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman9db505c2005-03-28 19:36:43 +00001625 switch(cast<MVTSDNode>(Node)->getExtraValueType()) {
1626 default: Node->dump(); assert(0 && "Unhandled SIGN_EXTEND type"); break;
Nate Begemanc7bd4822005-04-11 06:34:10 +00001627 case MVT::i16:
Nate Begeman9db505c2005-03-28 19:36:43 +00001628 BuildMI(BB, PPC::EXTSH, 1, Result).addReg(Tmp1);
1629 break;
Nate Begemanc7bd4822005-04-11 06:34:10 +00001630 case MVT::i8:
Nate Begeman9db505c2005-03-28 19:36:43 +00001631 BuildMI(BB, PPC::EXTSB, 1, Result).addReg(Tmp1);
1632 break;
Nate Begeman74747862005-03-29 22:24:51 +00001633 case MVT::i1:
1634 BuildMI(BB, PPC::SUBFIC, 2, Result).addReg(Tmp1).addSImm(0);
1635 break;
Nate Begeman9db505c2005-03-28 19:36:43 +00001636 }
Nate Begemana9795f82005-03-24 04:41:43 +00001637 return Result;
1638
Nate Begemana9795f82005-03-24 04:41:43 +00001639 case ISD::CopyFromReg:
1640 if (Result == 1)
1641 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1642 Tmp1 = dyn_cast<RegSDNode>(Node)->getReg();
1643 BuildMI(BB, PPC::OR, 2, Result).addReg(Tmp1).addReg(Tmp1);
1644 return Result;
1645
1646 case ISD::SHL:
Nate Begeman5e966612005-03-24 06:28:42 +00001647 Tmp1 = SelectExpr(N.getOperand(0));
1648 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1649 Tmp2 = CN->getValue() & 0x1F;
Nate Begeman33162522005-03-29 21:54:38 +00001650 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(Tmp2).addImm(0)
Nate Begeman5e966612005-03-24 06:28:42 +00001651 .addImm(31-Tmp2);
1652 } else {
1653 Tmp2 = SelectExpr(N.getOperand(1));
1654 BuildMI(BB, PPC::SLW, 2, Result).addReg(Tmp1).addReg(Tmp2);
1655 }
1656 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001657
Nate Begeman5e966612005-03-24 06:28:42 +00001658 case ISD::SRL:
1659 Tmp1 = SelectExpr(N.getOperand(0));
1660 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1661 Tmp2 = CN->getValue() & 0x1F;
Nate Begeman33162522005-03-29 21:54:38 +00001662 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(32-Tmp2)
Nate Begeman5e966612005-03-24 06:28:42 +00001663 .addImm(Tmp2).addImm(31);
1664 } else {
1665 Tmp2 = SelectExpr(N.getOperand(1));
1666 BuildMI(BB, PPC::SRW, 2, Result).addReg(Tmp1).addReg(Tmp2);
1667 }
1668 return Result;
1669
1670 case ISD::SRA:
1671 Tmp1 = SelectExpr(N.getOperand(0));
1672 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1673 Tmp2 = CN->getValue() & 0x1F;
1674 BuildMI(BB, PPC::SRAWI, 2, Result).addReg(Tmp1).addImm(Tmp2);
1675 } else {
1676 Tmp2 = SelectExpr(N.getOperand(1));
1677 BuildMI(BB, PPC::SRAW, 2, Result).addReg(Tmp1).addReg(Tmp2);
1678 }
1679 return Result;
1680
Nate Begemana9795f82005-03-24 04:41:43 +00001681 case ISD::ADD:
1682 assert (DestType == MVT::i32 && "Only do arithmetic on i32s!");
1683 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman439b4442005-04-05 04:22:58 +00001684 switch(getImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
Nate Begemana9795f82005-03-24 04:41:43 +00001685 default: assert(0 && "unhandled result code");
1686 case 0: // No immediate
1687 Tmp2 = SelectExpr(N.getOperand(1));
1688 BuildMI(BB, PPC::ADD, 2, Result).addReg(Tmp1).addReg(Tmp2);
1689 break;
1690 case 1: // Low immediate
1691 BuildMI(BB, PPC::ADDI, 2, Result).addReg(Tmp1).addSImm(Tmp2);
1692 break;
1693 case 2: // Shifted immediate
1694 BuildMI(BB, PPC::ADDIS, 2, Result).addReg(Tmp1).addSImm(Tmp2);
1695 break;
1696 }
1697 return Result;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001698
Nate Begemana9795f82005-03-24 04:41:43 +00001699 case ISD::AND:
Nate Begeman7ddecb42005-04-06 23:51:40 +00001700 Tmp1 = SelectExpr(N.getOperand(0));
1701 // FIXME: should add check in getImmediateForOpcode to return a value
1702 // indicating the immediate is a run of set bits so we can emit a bitfield
1703 // clear with RLWINM instead.
1704 switch(getImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
1705 default: assert(0 && "unhandled result code");
1706 case 0: // No immediate
1707 Tmp2 = SelectExpr(N.getOperand(1));
Nate Begemanc7bd4822005-04-11 06:34:10 +00001708 Opc = Recording ? PPC::ANDo : PPC::AND;
1709 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
Nate Begeman7ddecb42005-04-06 23:51:40 +00001710 break;
1711 case 1: // Low immediate
1712 BuildMI(BB, PPC::ANDIo, 2, Result).addReg(Tmp1).addImm(Tmp2);
1713 break;
1714 case 2: // Shifted immediate
1715 BuildMI(BB, PPC::ANDISo, 2, Result).addReg(Tmp1).addImm(Tmp2);
1716 break;
Nate Begeman9f833d32005-04-12 00:10:02 +00001717 case 5: // Bitfield mask
1718 Opc = Recording ? PPC::RLWINMo : PPC::RLWINM;
1719 Tmp3 = Tmp2 >> 16; // MB
1720 Tmp2 &= 0xFFFF; // ME
1721 BuildMI(BB, Opc, 4, Result).addReg(Tmp1).addImm(0)
1722 .addImm(Tmp3).addImm(Tmp2);
1723 break;
Nate Begeman7ddecb42005-04-06 23:51:40 +00001724 }
Nate Begemanc7bd4822005-04-11 06:34:10 +00001725 RecordSuccess = true;
Nate Begeman7ddecb42005-04-06 23:51:40 +00001726 return Result;
1727
Nate Begemana9795f82005-03-24 04:41:43 +00001728 case ISD::OR:
Nate Begeman7ddecb42005-04-06 23:51:40 +00001729 if (SelectBitfieldInsert(N, Result))
1730 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001731 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman439b4442005-04-05 04:22:58 +00001732 switch(getImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
Nate Begemana9795f82005-03-24 04:41:43 +00001733 default: assert(0 && "unhandled result code");
1734 case 0: // No immediate
1735 Tmp2 = SelectExpr(N.getOperand(1));
Nate Begemanc7bd4822005-04-11 06:34:10 +00001736 Opc = Recording ? PPC::ORo : PPC::OR;
1737 RecordSuccess = true;
1738 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +00001739 break;
1740 case 1: // Low immediate
Nate Begeman7ddecb42005-04-06 23:51:40 +00001741 BuildMI(BB, PPC::ORI, 2, Result).addReg(Tmp1).addImm(Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +00001742 break;
1743 case 2: // Shifted immediate
Nate Begeman7ddecb42005-04-06 23:51:40 +00001744 BuildMI(BB, PPC::ORIS, 2, Result).addReg(Tmp1).addImm(Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +00001745 break;
1746 }
1747 return Result;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001748
Nate Begemanaa73a9f2005-04-03 11:20:20 +00001749 case ISD::XOR: {
1750 // Check for EQV: xor, (xor a, -1), b
1751 if (N.getOperand(0).getOpcode() == ISD::XOR &&
1752 N.getOperand(0).getOperand(1).getOpcode() == ISD::Constant &&
1753 cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->isAllOnesValue()) {
Nate Begemanaa73a9f2005-04-03 11:20:20 +00001754 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1755 Tmp2 = SelectExpr(N.getOperand(1));
1756 BuildMI(BB, PPC::EQV, 2, Result).addReg(Tmp1).addReg(Tmp2);
1757 return Result;
1758 }
1759 // Check for NOT, NOR, and NAND: xor (copy, or, and), -1
1760 if (N.getOperand(1).getOpcode() == ISD::Constant &&
1761 cast<ConstantSDNode>(N.getOperand(1))->isAllOnesValue()) {
Nate Begemanaa73a9f2005-04-03 11:20:20 +00001762 switch(N.getOperand(0).getOpcode()) {
1763 case ISD::OR:
1764 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1765 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1766 BuildMI(BB, PPC::NOR, 2, Result).addReg(Tmp1).addReg(Tmp2);
1767 break;
1768 case ISD::AND:
1769 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1770 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1771 BuildMI(BB, PPC::NAND, 2, Result).addReg(Tmp1).addReg(Tmp2);
1772 break;
1773 default:
1774 Tmp1 = SelectExpr(N.getOperand(0));
1775 BuildMI(BB, PPC::NOR, 2, Result).addReg(Tmp1).addReg(Tmp1);
1776 break;
1777 }
1778 return Result;
1779 }
1780 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman439b4442005-04-05 04:22:58 +00001781 switch(getImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
Nate Begemanaa73a9f2005-04-03 11:20:20 +00001782 default: assert(0 && "unhandled result code");
1783 case 0: // No immediate
1784 Tmp2 = SelectExpr(N.getOperand(1));
1785 BuildMI(BB, PPC::XOR, 2, Result).addReg(Tmp1).addReg(Tmp2);
1786 break;
1787 case 1: // Low immediate
1788 BuildMI(BB, PPC::XORI, 2, Result).addReg(Tmp1).addImm(Tmp2);
1789 break;
1790 case 2: // Shifted immediate
1791 BuildMI(BB, PPC::XORIS, 2, Result).addReg(Tmp1).addImm(Tmp2);
1792 break;
1793 }
1794 return Result;
1795 }
1796
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001797 case ISD::SUB:
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001798 Tmp2 = SelectExpr(N.getOperand(1));
Nate Begeman439b4442005-04-05 04:22:58 +00001799 if (1 == getImmediateForOpcode(N.getOperand(0), opcode, Tmp1))
Nate Begeman27523a12005-04-02 00:42:16 +00001800 BuildMI(BB, PPC::SUBFIC, 2, Result).addReg(Tmp2).addSImm(Tmp1);
1801 else {
1802 Tmp1 = SelectExpr(N.getOperand(0));
1803 BuildMI(BB, PPC::SUBF, 2, Result).addReg(Tmp2).addReg(Tmp1);
1804 }
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001805 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001806
Nate Begeman5e966612005-03-24 06:28:42 +00001807 case ISD::MUL:
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001808 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman439b4442005-04-05 04:22:58 +00001809 if (1 == getImmediateForOpcode(N.getOperand(1), opcode, Tmp2))
Nate Begeman307e7442005-03-26 01:28:53 +00001810 BuildMI(BB, PPC::MULLI, 2, Result).addReg(Tmp1).addSImm(Tmp2);
1811 else {
1812 Tmp2 = SelectExpr(N.getOperand(1));
1813 BuildMI(BB, PPC::MULLW, 2, Result).addReg(Tmp1).addReg(Tmp2);
1814 }
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001815 return Result;
1816
Nate Begeman815d6da2005-04-06 00:25:27 +00001817 case ISD::MULHS:
1818 case ISD::MULHU:
1819 Tmp1 = SelectExpr(N.getOperand(0));
1820 Tmp2 = SelectExpr(N.getOperand(1));
1821 Opc = (ISD::MULHU == opcode) ? PPC::MULHWU : PPC::MULHW;
1822 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1823 return Result;
1824
Nate Begemanf3d08f32005-03-29 00:03:27 +00001825 case ISD::SDIV:
1826 case ISD::UDIV:
Nate Begeman815d6da2005-04-06 00:25:27 +00001827 switch (getImmediateForOpcode(N.getOperand(1), opcode, Tmp3)) {
1828 default: break;
1829 // If this is an sdiv by a power of two, we can use an srawi/addze pair.
1830 case 3:
Nate Begeman80196b12005-04-05 00:15:08 +00001831 Tmp1 = MakeReg(MVT::i32);
1832 Tmp2 = SelectExpr(N.getOperand(0));
Nate Begeman9f833d32005-04-12 00:10:02 +00001833 if ((int)Tmp3 < 0) {
1834 unsigned Tmp4 = MakeReg(MVT::i32);
1835 BuildMI(BB, PPC::SRAWI, 2, Tmp1).addReg(Tmp2).addImm(-Tmp3);
1836 BuildMI(BB, PPC::ADDZE, 1, Tmp4).addReg(Tmp1);
1837 BuildMI(BB, PPC::NEG, 1, Result).addReg(Tmp4);
1838 } else {
1839 BuildMI(BB, PPC::SRAWI, 2, Tmp1).addReg(Tmp2).addImm(Tmp3);
1840 BuildMI(BB, PPC::ADDZE, 1, Result).addReg(Tmp1);
1841 }
Nate Begeman80196b12005-04-05 00:15:08 +00001842 return Result;
Nate Begeman815d6da2005-04-06 00:25:27 +00001843 // If this is a divide by constant, we can emit code using some magic
1844 // constants to implement it as a multiply instead.
Nate Begeman27b4c232005-04-06 06:44:57 +00001845 case 4:
1846 ExprMap.erase(N);
1847 if (opcode == ISD::SDIV)
1848 return SelectExpr(BuildSDIVSequence(N));
1849 else
1850 return SelectExpr(BuildUDIVSequence(N));
Nate Begeman80196b12005-04-05 00:15:08 +00001851 }
Nate Begemanf3d08f32005-03-29 00:03:27 +00001852 Tmp1 = SelectExpr(N.getOperand(0));
1853 Tmp2 = SelectExpr(N.getOperand(1));
1854 Opc = (ISD::UDIV == opcode) ? PPC::DIVWU : PPC::DIVW;
1855 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1856 return Result;
1857
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001858 case ISD::ADD_PARTS:
Nate Begemanca12a2b2005-03-28 22:28:37 +00001859 case ISD::SUB_PARTS: {
1860 assert(N.getNumOperands() == 4 && N.getValueType() == MVT::i32 &&
1861 "Not an i64 add/sub!");
1862 // Emit all of the operands.
1863 std::vector<unsigned> InVals;
1864 for (unsigned i = 0, e = N.getNumOperands(); i != e; ++i)
1865 InVals.push_back(SelectExpr(N.getOperand(i)));
1866 if (N.getOpcode() == ISD::ADD_PARTS) {
Nate Begeman27eeb002005-04-02 05:59:34 +00001867 BuildMI(BB, PPC::ADDC, 2, Result).addReg(InVals[0]).addReg(InVals[2]);
1868 BuildMI(BB, PPC::ADDE, 2, Result+1).addReg(InVals[1]).addReg(InVals[3]);
Nate Begemanca12a2b2005-03-28 22:28:37 +00001869 } else {
Nate Begeman27eeb002005-04-02 05:59:34 +00001870 BuildMI(BB, PPC::SUBFC, 2, Result).addReg(InVals[2]).addReg(InVals[0]);
1871 BuildMI(BB, PPC::SUBFE, 2, Result+1).addReg(InVals[3]).addReg(InVals[1]);
1872 }
1873 return Result+N.ResNo;
1874 }
1875
1876 case ISD::SHL_PARTS:
1877 case ISD::SRA_PARTS:
1878 case ISD::SRL_PARTS: {
1879 assert(N.getNumOperands() == 3 && N.getValueType() == MVT::i32 &&
1880 "Not an i64 shift!");
1881 unsigned ShiftOpLo = SelectExpr(N.getOperand(0));
1882 unsigned ShiftOpHi = SelectExpr(N.getOperand(1));
1883 unsigned SHReg = SelectExpr(N.getOperand(2));
1884 Tmp1 = MakeReg(MVT::i32);
1885 Tmp2 = MakeReg(MVT::i32);
1886 Tmp3 = MakeReg(MVT::i32);
1887 unsigned Tmp4 = MakeReg(MVT::i32);
1888 unsigned Tmp5 = MakeReg(MVT::i32);
1889 unsigned Tmp6 = MakeReg(MVT::i32);
1890 BuildMI(BB, PPC::SUBFIC, 2, Tmp1).addReg(SHReg).addSImm(32);
1891 if (ISD::SHL_PARTS == opcode) {
1892 BuildMI(BB, PPC::SLW, 2, Tmp2).addReg(ShiftOpHi).addReg(SHReg);
1893 BuildMI(BB, PPC::SRW, 2, Tmp3).addReg(ShiftOpLo).addReg(Tmp1);
1894 BuildMI(BB, PPC::OR, 2, Tmp4).addReg(Tmp2).addReg(Tmp3);
1895 BuildMI(BB, PPC::ADDI, 2, Tmp5).addReg(SHReg).addSImm(-32);
Nate Begemanfa554702005-04-03 22:13:27 +00001896 BuildMI(BB, PPC::SLW, 2, Tmp6).addReg(ShiftOpLo).addReg(Tmp5);
Nate Begeman27eeb002005-04-02 05:59:34 +00001897 BuildMI(BB, PPC::OR, 2, Result+1).addReg(Tmp4).addReg(Tmp6);
1898 BuildMI(BB, PPC::SLW, 2, Result).addReg(ShiftOpLo).addReg(SHReg);
1899 } else if (ISD::SRL_PARTS == opcode) {
1900 BuildMI(BB, PPC::SRW, 2, Tmp2).addReg(ShiftOpLo).addReg(SHReg);
1901 BuildMI(BB, PPC::SLW, 2, Tmp3).addReg(ShiftOpHi).addReg(Tmp1);
1902 BuildMI(BB, PPC::OR, 2, Tmp4).addReg(Tmp2).addReg(Tmp3);
1903 BuildMI(BB, PPC::ADDI, 2, Tmp5).addReg(SHReg).addSImm(-32);
1904 BuildMI(BB, PPC::SRW, 2, Tmp6).addReg(ShiftOpHi).addReg(Tmp5);
1905 BuildMI(BB, PPC::OR, 2, Result).addReg(Tmp4).addReg(Tmp6);
1906 BuildMI(BB, PPC::SRW, 2, Result+1).addReg(ShiftOpHi).addReg(SHReg);
1907 } else {
1908 MachineBasicBlock *TmpMBB = new MachineBasicBlock(BB->getBasicBlock());
1909 MachineBasicBlock *PhiMBB = new MachineBasicBlock(BB->getBasicBlock());
1910 MachineBasicBlock *OldMBB = BB;
1911 MachineFunction *F = BB->getParent();
1912 ilist<MachineBasicBlock>::iterator It = BB; ++It;
1913 F->getBasicBlockList().insert(It, TmpMBB);
1914 F->getBasicBlockList().insert(It, PhiMBB);
1915 BB->addSuccessor(TmpMBB);
1916 BB->addSuccessor(PhiMBB);
1917 BuildMI(BB, PPC::SRW, 2, Tmp2).addReg(ShiftOpLo).addReg(SHReg);
1918 BuildMI(BB, PPC::SLW, 2, Tmp3).addReg(ShiftOpHi).addReg(Tmp1);
1919 BuildMI(BB, PPC::OR, 2, Tmp4).addReg(Tmp2).addReg(Tmp3);
1920 BuildMI(BB, PPC::ADDICo, 2, Tmp5).addReg(SHReg).addSImm(-32);
1921 BuildMI(BB, PPC::SRAW, 2, Tmp6).addReg(ShiftOpHi).addReg(Tmp5);
1922 BuildMI(BB, PPC::SRAW, 2, Result+1).addReg(ShiftOpHi).addReg(SHReg);
1923 BuildMI(BB, PPC::BLE, 2).addReg(PPC::CR0).addMBB(PhiMBB);
1924 // Select correct least significant half if the shift amount > 32
1925 BB = TmpMBB;
1926 unsigned Tmp7 = MakeReg(MVT::i32);
1927 BuildMI(BB, PPC::OR, 2, Tmp7).addReg(Tmp6).addReg(Tmp6);
1928 TmpMBB->addSuccessor(PhiMBB);
1929 BB = PhiMBB;
1930 BuildMI(BB, PPC::PHI, 4, Result).addReg(Tmp4).addMBB(OldMBB)
1931 .addReg(Tmp7).addMBB(TmpMBB);
Nate Begemanca12a2b2005-03-28 22:28:37 +00001932 }
1933 return Result+N.ResNo;
1934 }
1935
Nate Begemana9795f82005-03-24 04:41:43 +00001936 case ISD::FP_TO_UINT:
Nate Begeman6b559972005-04-01 02:59:27 +00001937 case ISD::FP_TO_SINT: {
1938 bool U = (ISD::FP_TO_UINT == opcode);
1939 Tmp1 = SelectExpr(N.getOperand(0));
1940 if (!U) {
1941 Tmp2 = MakeReg(MVT::f64);
1942 BuildMI(BB, PPC::FCTIWZ, 1, Tmp2).addReg(Tmp1);
1943 int FrameIdx = BB->getParent()->getFrameInfo()->CreateStackObject(8, 8);
1944 addFrameReference(BuildMI(BB, PPC::STFD, 3).addReg(Tmp2), FrameIdx);
1945 addFrameReference(BuildMI(BB, PPC::LWZ, 2, Result), FrameIdx, 4);
1946 return Result;
1947 } else {
1948 unsigned Zero = getConstDouble(0.0);
1949 unsigned MaxInt = getConstDouble((1LL << 32) - 1);
1950 unsigned Border = getConstDouble(1LL << 31);
1951 unsigned UseZero = MakeReg(MVT::f64);
1952 unsigned UseMaxInt = MakeReg(MVT::f64);
1953 unsigned UseChoice = MakeReg(MVT::f64);
1954 unsigned TmpReg = MakeReg(MVT::f64);
1955 unsigned TmpReg2 = MakeReg(MVT::f64);
1956 unsigned ConvReg = MakeReg(MVT::f64);
1957 unsigned IntTmp = MakeReg(MVT::i32);
1958 unsigned XorReg = MakeReg(MVT::i32);
1959 MachineFunction *F = BB->getParent();
1960 int FrameIdx = F->getFrameInfo()->CreateStackObject(8, 8);
1961 // Update machine-CFG edges
1962 MachineBasicBlock *XorMBB = new MachineBasicBlock(BB->getBasicBlock());
1963 MachineBasicBlock *PhiMBB = new MachineBasicBlock(BB->getBasicBlock());
1964 MachineBasicBlock *OldMBB = BB;
1965 ilist<MachineBasicBlock>::iterator It = BB; ++It;
1966 F->getBasicBlockList().insert(It, XorMBB);
1967 F->getBasicBlockList().insert(It, PhiMBB);
1968 BB->addSuccessor(XorMBB);
1969 BB->addSuccessor(PhiMBB);
1970 // Convert from floating point to unsigned 32-bit value
1971 // Use 0 if incoming value is < 0.0
1972 BuildMI(BB, PPC::FSEL, 3, UseZero).addReg(Tmp1).addReg(Tmp1).addReg(Zero);
1973 // Use 2**32 - 1 if incoming value is >= 2**32
1974 BuildMI(BB, PPC::FSUB, 2, UseMaxInt).addReg(MaxInt).addReg(Tmp1);
1975 BuildMI(BB, PPC::FSEL, 3, UseChoice).addReg(UseMaxInt).addReg(UseZero)
1976 .addReg(MaxInt);
1977 // Subtract 2**31
1978 BuildMI(BB, PPC::FSUB, 2, TmpReg).addReg(UseChoice).addReg(Border);
1979 // Use difference if >= 2**31
1980 BuildMI(BB, PPC::FCMPU, 2, PPC::CR0).addReg(UseChoice).addReg(Border);
1981 BuildMI(BB, PPC::FSEL, 3, TmpReg2).addReg(TmpReg).addReg(TmpReg)
1982 .addReg(UseChoice);
1983 // Convert to integer
1984 BuildMI(BB, PPC::FCTIWZ, 1, ConvReg).addReg(TmpReg2);
1985 addFrameReference(BuildMI(BB, PPC::STFD, 3).addReg(ConvReg), FrameIdx);
1986 addFrameReference(BuildMI(BB, PPC::LWZ, 2, IntTmp), FrameIdx, 4);
1987 BuildMI(BB, PPC::BLT, 2).addReg(PPC::CR0).addMBB(PhiMBB);
1988 BuildMI(BB, PPC::B, 1).addMBB(XorMBB);
1989
1990 // XorMBB:
1991 // add 2**31 if input was >= 2**31
1992 BB = XorMBB;
1993 BuildMI(BB, PPC::XORIS, 2, XorReg).addReg(IntTmp).addImm(0x8000);
1994 XorMBB->addSuccessor(PhiMBB);
1995
1996 // PhiMBB:
1997 // DestReg = phi [ IntTmp, OldMBB ], [ XorReg, XorMBB ]
1998 BB = PhiMBB;
1999 BuildMI(BB, PPC::PHI, 4, Result).addReg(IntTmp).addMBB(OldMBB)
2000 .addReg(XorReg).addMBB(XorMBB);
2001 return Result;
2002 }
2003 assert(0 && "Should never get here");
2004 return 0;
2005 }
Nate Begemana9795f82005-03-24 04:41:43 +00002006
Nate Begeman9e3e1b52005-03-24 23:35:30 +00002007 case ISD::SETCC:
Nate Begeman33162522005-03-29 21:54:38 +00002008 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Node)) {
Nate Begeman7e7fadd2005-04-07 20:30:01 +00002009 if (ConstantSDNode *CN =
2010 dyn_cast<ConstantSDNode>(SetCC->getOperand(1).Val)) {
Nate Begeman9765c252005-04-12 21:22:28 +00002011 // We can codegen setcc op, imm very efficiently compared to a brcond.
2012 // Check for those cases here.
2013 // setcc op, 0
Nate Begeman7e7fadd2005-04-07 20:30:01 +00002014 if (CN->getValue() == 0) {
2015 Tmp1 = SelectExpr(SetCC->getOperand(0));
2016 switch (SetCC->getCondition()) {
2017 default: assert(0 && "Unhandled SetCC condition"); abort();
2018 case ISD::SETEQ:
Nate Begeman7e7fadd2005-04-07 20:30:01 +00002019 Tmp2 = MakeReg(MVT::i32);
2020 BuildMI(BB, PPC::CNTLZW, 1, Tmp2).addReg(Tmp1);
2021 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp2).addImm(27)
2022 .addImm(5).addImm(31);
2023 break;
2024 case ISD::SETNE:
Nate Begeman7e7fadd2005-04-07 20:30:01 +00002025 Tmp2 = MakeReg(MVT::i32);
2026 BuildMI(BB, PPC::ADDIC, 2, Tmp2).addReg(Tmp1).addSImm(-1);
2027 BuildMI(BB, PPC::SUBFE, 2, Result).addReg(Tmp2).addReg(Tmp1);
2028 break;
Nate Begeman7e7fadd2005-04-07 20:30:01 +00002029 case ISD::SETLT:
2030 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(1)
2031 .addImm(31).addImm(31);
2032 break;
Nate Begeman7e7fadd2005-04-07 20:30:01 +00002033 case ISD::SETGT:
2034 Tmp2 = MakeReg(MVT::i32);
2035 Tmp3 = MakeReg(MVT::i32);
2036 BuildMI(BB, PPC::NEG, 2, Tmp2).addReg(Tmp1);
2037 BuildMI(BB, PPC::ANDC, 2, Tmp3).addReg(Tmp2).addReg(Tmp1);
2038 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp3).addImm(1)
2039 .addImm(31).addImm(31);
2040 break;
Nate Begeman9765c252005-04-12 21:22:28 +00002041 }
2042 return Result;
2043 }
2044 // setcc op, -1
2045 if (CN->isAllOnesValue()) {
2046 Tmp1 = SelectExpr(SetCC->getOperand(0));
2047 switch (SetCC->getCondition()) {
2048 default: assert(0 && "Unhandled SetCC condition"); abort();
2049 case ISD::SETEQ:
2050 Tmp2 = MakeReg(MVT::i32);
2051 Tmp3 = MakeReg(MVT::i32);
2052 BuildMI(BB, PPC::ADDIC, 2, Tmp2).addReg(Tmp1).addSImm(1);
2053 BuildMI(BB, PPC::LI, 1, Tmp3).addSImm(0);
2054 BuildMI(BB, PPC::ADDZE, 1, Result).addReg(Tmp3);
Nate Begeman7e7fadd2005-04-07 20:30:01 +00002055 break;
Nate Begeman9765c252005-04-12 21:22:28 +00002056 case ISD::SETNE:
2057 Tmp2 = MakeReg(MVT::i32);
2058 Tmp3 = MakeReg(MVT::i32);
2059 BuildMI(BB, PPC::NOR, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
2060 BuildMI(BB, PPC::ADDIC, 2, Tmp3).addReg(Tmp2).addSImm(-1);
2061 BuildMI(BB, PPC::SUBFE, 2, Result).addReg(Tmp3).addReg(Tmp2);
2062 break;
2063 case ISD::SETLT:
2064 Tmp2 = MakeReg(MVT::i32);
2065 Tmp3 = MakeReg(MVT::i32);
2066 BuildMI(BB, PPC::ADDI, 2, Tmp2).addReg(Tmp1).addSImm(1);
2067 BuildMI(BB, PPC::AND, 2, Tmp3).addReg(Tmp2).addReg(Tmp1);
2068 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp3).addImm(1)
2069 .addImm(31).addImm(31);
2070 break;
2071 case ISD::SETGT:
2072 Tmp2 = MakeReg(MVT::i32);
Nate Begeman7e7fadd2005-04-07 20:30:01 +00002073 BuildMI(BB, PPC::RLWINM, 4, Tmp2).addReg(Tmp1).addImm(1)
2074 .addImm(31).addImm(31);
2075 BuildMI(BB, PPC::XORI, 2, Result).addReg(Tmp2).addImm(1);
2076 break;
2077 }
2078 return Result;
2079 }
2080 }
2081
Nate Begemandffcfcc2005-04-01 00:32:34 +00002082 Opc = SelectSetCR0(N);
Nate Begeman31318e42005-04-01 07:21:30 +00002083 unsigned TrueValue = MakeReg(MVT::i32);
2084 BuildMI(BB, PPC::LI, 1, TrueValue).addSImm(1);
2085 unsigned FalseValue = MakeReg(MVT::i32);
2086 BuildMI(BB, PPC::LI, 1, FalseValue).addSImm(0);
2087
Nate Begeman33162522005-03-29 21:54:38 +00002088 // Create an iterator with which to insert the MBB for copying the false
2089 // value and the MBB to hold the PHI instruction for this SetCC.
2090 MachineBasicBlock *thisMBB = BB;
2091 const BasicBlock *LLVM_BB = BB->getBasicBlock();
2092 ilist<MachineBasicBlock>::iterator It = BB;
2093 ++It;
2094
2095 // thisMBB:
2096 // ...
2097 // cmpTY cr0, r1, r2
2098 // %TrueValue = li 1
2099 // bCC sinkMBB
Nate Begeman33162522005-03-29 21:54:38 +00002100 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
2101 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
2102 BuildMI(BB, Opc, 2).addReg(PPC::CR0).addMBB(sinkMBB);
2103 MachineFunction *F = BB->getParent();
2104 F->getBasicBlockList().insert(It, copy0MBB);
2105 F->getBasicBlockList().insert(It, sinkMBB);
2106 // Update machine-CFG edges
2107 BB->addSuccessor(copy0MBB);
2108 BB->addSuccessor(sinkMBB);
2109
2110 // copy0MBB:
2111 // %FalseValue = li 0
2112 // fallthrough
2113 BB = copy0MBB;
Nate Begeman33162522005-03-29 21:54:38 +00002114 // Update machine-CFG edges
2115 BB->addSuccessor(sinkMBB);
2116
2117 // sinkMBB:
2118 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
2119 // ...
2120 BB = sinkMBB;
2121 BuildMI(BB, PPC::PHI, 4, Result).addReg(FalseValue)
2122 .addMBB(copy0MBB).addReg(TrueValue).addMBB(thisMBB);
2123 return Result;
2124 }
2125 assert(0 && "Is this legal?");
2126 return 0;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00002127
Nate Begeman74747862005-03-29 22:24:51 +00002128 case ISD::SELECT: {
Nate Begeman7e7fadd2005-04-07 20:30:01 +00002129 // We can codegen select (a < 0) ? b : 0 very efficiently compared to a
2130 // conditional branch. Check for that here.
2131 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(N.getOperand(0).Val)) {
2132 if (ConstantSDNode *CN =
2133 dyn_cast<ConstantSDNode>(SetCC->getOperand(1).Val)) {
2134 if (ConstantSDNode *CNF =
2135 dyn_cast<ConstantSDNode>(N.getOperand(2).Val)) {
2136 if (CN->getValue() == 0 && CNF->getValue() == 0 &&
2137 SetCC->getCondition() == ISD::SETLT) {
2138 Tmp1 = SelectExpr(N.getOperand(1)); // TRUE value
2139 Tmp2 = SelectExpr(SetCC->getOperand(0));
2140 Tmp3 = MakeReg(MVT::i32);
2141 BuildMI(BB, PPC::SRAWI, 2, Tmp3).addReg(Tmp2).addImm(31);
2142 BuildMI(BB, PPC::AND, 2, Result).addReg(Tmp1).addReg(Tmp3);
2143 return Result;
2144 }
2145 }
2146 }
2147 }
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 Begeman6cb2e1b2005-04-01 08:57:43 +00002150 Opc = SelectSetCR0(N.getOperand(0));
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 = ...
2162 // cmpTY cr0, r1, r2
2163 // 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 Begeman3e897162005-03-31 23:55:40 +00002167 BuildMI(BB, Opc, 2).addReg(PPC::CR0).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