blob: aea11e4b12db29267ced17c9cc4aba3ba0ebe6b1 [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"
19#include "PPC32RegisterInfo.h"
20#include "llvm/Constants.h" // FIXME: REMOVE
21#include "llvm/Function.h"
22#include "llvm/CodeGen/MachineConstantPool.h" // FIXME: REMOVE
23#include "llvm/CodeGen/MachineFunction.h"
24#include "llvm/CodeGen/MachineFrameInfo.h"
25#include "llvm/CodeGen/SelectionDAG.h"
26#include "llvm/CodeGen/SelectionDAGISel.h"
27#include "llvm/CodeGen/SSARegMap.h"
28#include "llvm/Target/TargetData.h"
29#include "llvm/Target/TargetLowering.h"
Nate Begeman93075ec2005-04-04 23:40:36 +000030#include "llvm/Target/TargetOptions.h"
Nate Begemana9795f82005-03-24 04:41:43 +000031#include "llvm/Support/Debug.h"
32#include "llvm/Support/MathExtras.h"
33#include "llvm/ADT/Statistic.h"
34#include <set>
35#include <algorithm>
36using namespace llvm;
37
38//===----------------------------------------------------------------------===//
39// PPC32TargetLowering - PPC32 Implementation of the TargetLowering interface
40namespace {
41 class PPC32TargetLowering : public TargetLowering {
42 int VarArgsFrameIndex; // FrameIndex for start of varargs area.
43 int ReturnAddrIndex; // FrameIndex for return slot.
44 public:
45 PPC32TargetLowering(TargetMachine &TM) : TargetLowering(TM) {
Nate Begemana9795f82005-03-24 04:41:43 +000046 // Set up the register classes.
47 addRegisterClass(MVT::i32, PPC32::GPRCRegisterClass);
Nate Begeman7532e2f2005-03-26 08:25:22 +000048 addRegisterClass(MVT::f32, PPC32::FPRCRegisterClass);
Nate Begemana9795f82005-03-24 04:41:43 +000049 addRegisterClass(MVT::f64, PPC32::FPRCRegisterClass);
50
Nate Begeman74d73452005-03-31 00:15:26 +000051 // PowerPC has no intrinsics for these particular operations
Nate Begeman01d05262005-03-30 01:45:43 +000052 setOperationAction(ISD::MEMMOVE, MVT::Other, Expand);
53 setOperationAction(ISD::MEMSET, MVT::Other, Expand);
54 setOperationAction(ISD::MEMCPY, MVT::Other, Expand);
55
Nate Begeman74d73452005-03-31 00:15:26 +000056 // PowerPC has an i16 but no i8 (or i1) SEXTLOAD
57 setOperationAction(ISD::SEXTLOAD, MVT::i1, Expand);
58 setOperationAction(ISD::SEXTLOAD, MVT::i8, Expand);
Nate Begeman815d6da2005-04-06 00:25:27 +000059
60 // PowerPC has no SREM/UREM instructions
61 setOperationAction(ISD::SREM, MVT::i32, Expand);
62 setOperationAction(ISD::UREM, MVT::i32, Expand);
Chris Lattner43fdea02005-04-02 05:03:24 +000063
Nate Begeman27eeb002005-04-02 05:59:34 +000064 setShiftAmountFlavor(Extend); // shl X, 32 == 0
Chris Lattnercbd06fc2005-04-07 19:41:49 +000065 setSetCCResultContents(ZeroOrOneSetCCResult);
Nate Begeman3e897162005-03-31 23:55:40 +000066 addLegalFPImmediate(+0.0); // Necessary for FSEL
67 addLegalFPImmediate(-0.0); //
68
Nate Begemana9795f82005-03-24 04:41:43 +000069 computeRegisterProperties();
70 }
71
72 /// LowerArguments - This hook must be implemented to indicate how we should
73 /// lower the arguments for the specified function, into the specified DAG.
74 virtual std::vector<SDOperand>
75 LowerArguments(Function &F, SelectionDAG &DAG);
76
77 /// LowerCallTo - This hook lowers an abstract call to a function into an
78 /// actual call.
79 virtual std::pair<SDOperand, SDOperand>
Nate Begeman307e7442005-03-26 01:28:53 +000080 LowerCallTo(SDOperand Chain, const Type *RetTy, bool isVarArg,
81 SDOperand Callee, ArgListTy &Args, SelectionDAG &DAG);
Nate Begemana9795f82005-03-24 04:41:43 +000082
83 virtual std::pair<SDOperand, SDOperand>
84 LowerVAStart(SDOperand Chain, SelectionDAG &DAG);
85
86 virtual std::pair<SDOperand,SDOperand>
87 LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
88 const Type *ArgTy, SelectionDAG &DAG);
89
90 virtual std::pair<SDOperand, SDOperand>
91 LowerFrameReturnAddress(bool isFrameAddr, SDOperand Chain, unsigned Depth,
92 SelectionDAG &DAG);
93 };
94}
95
96
97std::vector<SDOperand>
98PPC32TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
99 //
100 // add beautiful description of PPC stack frame format, or at least some docs
101 //
102 MachineFunction &MF = DAG.getMachineFunction();
103 MachineFrameInfo *MFI = MF.getFrameInfo();
104 MachineBasicBlock& BB = MF.front();
105 std::vector<SDOperand> ArgValues;
106
107 // Due to the rather complicated nature of the PowerPC ABI, rather than a
108 // fixed size array of physical args, for the sake of simplicity let the STL
109 // handle tracking them for us.
110 std::vector<unsigned> argVR, argPR, argOp;
111 unsigned ArgOffset = 24;
112 unsigned GPR_remaining = 8;
113 unsigned FPR_remaining = 13;
114 unsigned GPR_idx = 0, FPR_idx = 0;
115 static const unsigned GPR[] = {
116 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
117 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
118 };
119 static const unsigned FPR[] = {
120 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
121 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
122 };
123
124 // Add DAG nodes to load the arguments... On entry to a function on PPC,
125 // the arguments start at offset 24, although they are likely to be passed
126 // in registers.
127 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
128 SDOperand newroot, argt;
129 unsigned ObjSize;
130 bool needsLoad = false;
131 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;
140 if (GPR_remaining > 0) {
141 BuildMI(&BB, PPC::IMPLICIT_DEF, 0, GPR[GPR_idx]);
Nate Begemanf70b5762005-03-28 23:08:54 +0000142 argt = newroot = DAG.getCopyFromReg(GPR[GPR_idx], MVT::i32,
143 DAG.getRoot());
Nate Begemana9795f82005-03-24 04:41:43 +0000144 if (ObjectVT != MVT::i32)
145 argt = DAG.getNode(ISD::TRUNCATE, ObjectVT, newroot);
Nate Begemana9795f82005-03-24 04:41:43 +0000146 } else {
147 needsLoad = true;
148 }
149 break;
Nate Begemanf7e43382005-03-26 07:46:36 +0000150 case MVT::i64: ObjSize = 8;
151 // FIXME: can split 64b load between reg/mem if it is last arg in regs
Nate Begemana9795f82005-03-24 04:41:43 +0000152 if (GPR_remaining > 1) {
153 BuildMI(&BB, PPC::IMPLICIT_DEF, 0, GPR[GPR_idx]);
154 BuildMI(&BB, PPC::IMPLICIT_DEF, 0, GPR[GPR_idx+1]);
Nate Begemanca12a2b2005-03-28 22:28:37 +0000155 // Copy the extracted halves into the virtual registers
Nate Begemanf70b5762005-03-28 23:08:54 +0000156 SDOperand argHi = DAG.getCopyFromReg(GPR[GPR_idx], MVT::i32,
157 DAG.getRoot());
158 SDOperand argLo = DAG.getCopyFromReg(GPR[GPR_idx+1], MVT::i32, argHi);
Nate Begemanca12a2b2005-03-28 22:28:37 +0000159 // Build the outgoing arg thingy
Nate Begemanf70b5762005-03-28 23:08:54 +0000160 argt = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, argLo, argHi);
161 newroot = argLo;
Nate Begemana9795f82005-03-24 04:41:43 +0000162 } else {
163 needsLoad = true;
164 }
165 break;
166 case MVT::f32: ObjSize = 4;
167 case MVT::f64: ObjSize = 8;
168 if (FPR_remaining > 0) {
169 BuildMI(&BB, PPC::IMPLICIT_DEF, 0, FPR[FPR_idx]);
Nate Begemanf70b5762005-03-28 23:08:54 +0000170 argt = newroot = DAG.getCopyFromReg(FPR[FPR_idx], ObjectVT,
171 DAG.getRoot());
Nate Begemana9795f82005-03-24 04:41:43 +0000172 --FPR_remaining;
173 ++FPR_idx;
174 } else {
175 needsLoad = true;
176 }
177 break;
178 }
179
180 // We need to load the argument to a virtual register if we determined above
181 // that we ran out of physical registers of the appropriate type
182 if (needsLoad) {
Nate Begemane5846682005-04-04 06:52:38 +0000183 unsigned SubregOffset = 0;
Nate Begemanc3e2db42005-04-04 09:09:00 +0000184 if (ObjectVT == MVT::i8 || ObjectVT == MVT::i1) SubregOffset = 3;
Nate Begemane5846682005-04-04 06:52:38 +0000185 if (ObjectVT == MVT::i16) SubregOffset = 2;
Nate Begemana9795f82005-03-24 04:41:43 +0000186 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
187 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
Nate Begemane5846682005-04-04 06:52:38 +0000188 FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN,
189 DAG.getConstant(SubregOffset, MVT::i32));
Nate Begemana9795f82005-03-24 04:41:43 +0000190 argt = newroot = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN);
191 }
192
193 // Every 4 bytes of argument space consumes one of the GPRs available for
194 // argument passing.
195 if (GPR_remaining > 0) {
196 unsigned delta = (GPR_remaining > 1 && ObjSize == 8) ? 2 : 1;
197 GPR_remaining -= delta;
198 GPR_idx += delta;
199 }
200 ArgOffset += ObjSize;
201
202 DAG.setRoot(newroot.getValue(1));
203 ArgValues.push_back(argt);
204 }
205
Nate Begemana9795f82005-03-24 04:41:43 +0000206 // If the function takes variable number of arguments, make a frame index for
207 // the start of the first vararg value... for expansion of llvm.va_start.
Nate Begemanfa554702005-04-03 22:13:27 +0000208 if (F.isVarArg()) {
Nate Begemana9795f82005-03-24 04:41:43 +0000209 VarArgsFrameIndex = MFI->CreateFixedObject(4, ArgOffset);
Nate Begemanfa554702005-04-03 22:13:27 +0000210 SDOperand FIN = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
Nate Begeman6644d4c2005-04-03 23:11:17 +0000211 // If this function is vararg, store any remaining integer argument regs
212 // to their spots on the stack so that they may be loaded by deferencing the
213 // result of va_next.
214 std::vector<SDOperand> MemOps;
215 for (; GPR_remaining > 0; --GPR_remaining, ++GPR_idx) {
216 BuildMI(&BB, PPC::IMPLICIT_DEF, 0, GPR[GPR_idx]);
217 SDOperand Val = DAG.getCopyFromReg(GPR[GPR_idx], MVT::i32, DAG.getRoot());
218 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Val.getValue(1),
219 Val, FIN);
220 MemOps.push_back(Store);
221 // Increment the address by four for the next argument to store
222 SDOperand PtrOff = DAG.getConstant(4, getPointerTy());
223 FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN, PtrOff);
224 }
225 DAG.setRoot(DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps));
Nate Begemanfa554702005-04-03 22:13:27 +0000226 }
Nate Begemana9795f82005-03-24 04:41:43 +0000227
228 return ArgValues;
229}
230
231std::pair<SDOperand, SDOperand>
232PPC32TargetLowering::LowerCallTo(SDOperand Chain,
Nate Begeman307e7442005-03-26 01:28:53 +0000233 const Type *RetTy, bool isVarArg,
234 SDOperand Callee, ArgListTy &Args, SelectionDAG &DAG) {
235 // args_to_use will accumulate outgoing args for the ISD::CALL case in
236 // SelectExpr to use to put the arguments in the appropriate registers.
Nate Begemana9795f82005-03-24 04:41:43 +0000237 std::vector<SDOperand> args_to_use;
Nate Begeman307e7442005-03-26 01:28:53 +0000238
239 // Count how many bytes are to be pushed on the stack, including the linkage
240 // area, and parameter passing area.
241 unsigned NumBytes = 24;
242
243 if (Args.empty()) {
Nate Begemana7e11a42005-04-01 05:57:17 +0000244 Chain = DAG.getNode(ISD::ADJCALLSTACKDOWN, MVT::Other, Chain,
245 DAG.getConstant(NumBytes, getPointerTy()));
Nate Begeman307e7442005-03-26 01:28:53 +0000246 } else {
247 for (unsigned i = 0, e = Args.size(); i != e; ++i)
248 switch (getValueType(Args[i].second)) {
249 default: assert(0 && "Unknown value type!");
250 case MVT::i1:
251 case MVT::i8:
252 case MVT::i16:
253 case MVT::i32:
254 case MVT::f32:
255 NumBytes += 4;
256 break;
257 case MVT::i64:
258 case MVT::f64:
259 NumBytes += 8;
260 break;
261 }
262
263 // Just to be safe, we'll always reserve the full 24 bytes of linkage area
264 // plus 32 bytes of argument space in case any called code gets funky on us.
265 if (NumBytes < 56) NumBytes = 56;
266
267 // Adjust the stack pointer for the new arguments...
268 // These operations are automatically eliminated by the prolog/epilog pass
269 Chain = DAG.getNode(ISD::ADJCALLSTACKDOWN, MVT::Other, Chain,
270 DAG.getConstant(NumBytes, getPointerTy()));
271
272 // Set up a copy of the stack pointer for use loading and storing any
273 // arguments that may not fit in the registers available for argument
274 // passing.
275 SDOperand StackPtr = DAG.getCopyFromReg(PPC::R1, MVT::i32,
276 DAG.getEntryNode());
277
278 // Figure out which arguments are going to go in registers, and which in
279 // memory. Also, if this is a vararg function, floating point operations
280 // must be stored to our stack, and loaded into integer regs as well, if
281 // any integer regs are available for argument passing.
282 unsigned ArgOffset = 24;
283 unsigned GPR_remaining = 8;
284 unsigned FPR_remaining = 13;
Nate Begeman74d73452005-03-31 00:15:26 +0000285
286 std::vector<SDOperand> MemOps;
Nate Begeman307e7442005-03-26 01:28:53 +0000287 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
288 // PtrOff will be used to store the current argument to the stack if a
289 // register cannot be found for it.
290 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
291 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
Nate Begemanf7e43382005-03-26 07:46:36 +0000292 MVT::ValueType ArgVT = getValueType(Args[i].second);
Nate Begeman307e7442005-03-26 01:28:53 +0000293
Nate Begemanf7e43382005-03-26 07:46:36 +0000294 switch (ArgVT) {
Nate Begeman307e7442005-03-26 01:28:53 +0000295 default: assert(0 && "Unexpected ValueType for argument!");
296 case MVT::i1:
297 case MVT::i8:
298 case MVT::i16:
299 // Promote the integer to 32 bits. If the input type is signed use a
300 // sign extend, otherwise use a zero extend.
301 if (Args[i].second->isSigned())
302 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
303 else
304 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
305 // FALL THROUGH
306 case MVT::i32:
307 if (GPR_remaining > 0) {
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000308 args_to_use.push_back(Args[i].first);
Nate Begeman307e7442005-03-26 01:28:53 +0000309 --GPR_remaining;
310 } else {
Nate Begeman74d73452005-03-31 00:15:26 +0000311 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
312 Args[i].first, PtrOff));
Nate Begeman307e7442005-03-26 01:28:53 +0000313 }
314 ArgOffset += 4;
315 break;
316 case MVT::i64:
Nate Begemanf7e43382005-03-26 07:46:36 +0000317 // If we have one free GPR left, we can place the upper half of the i64
318 // in it, and store the other half to the stack. If we have two or more
319 // free GPRs, then we can pass both halves of the i64 in registers.
320 if (GPR_remaining > 0) {
Nate Begemanf2622612005-03-26 02:17:46 +0000321 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
322 Args[i].first, DAG.getConstant(1, MVT::i32));
323 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
324 Args[i].first, DAG.getConstant(0, MVT::i32));
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000325 args_to_use.push_back(Hi);
Nate Begeman74d73452005-03-31 00:15:26 +0000326 --GPR_remaining;
Nate Begeman74d73452005-03-31 00:15:26 +0000327 if (GPR_remaining > 0) {
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000328 args_to_use.push_back(Lo);
Nate Begeman74d73452005-03-31 00:15:26 +0000329 --GPR_remaining;
Nate Begemanf7e43382005-03-26 07:46:36 +0000330 } else {
331 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
332 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
Nate Begeman74d73452005-03-31 00:15:26 +0000333 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
334 Lo, PtrOff));
Nate Begemanf7e43382005-03-26 07:46:36 +0000335 }
Nate Begeman307e7442005-03-26 01:28:53 +0000336 } else {
Nate Begeman74d73452005-03-31 00:15:26 +0000337 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
338 Args[i].first, PtrOff));
Nate Begeman307e7442005-03-26 01:28:53 +0000339 }
340 ArgOffset += 8;
341 break;
342 case MVT::f32:
Nate Begeman307e7442005-03-26 01:28:53 +0000343 case MVT::f64:
Nate Begemanf7e43382005-03-26 07:46:36 +0000344 if (FPR_remaining > 0) {
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000345 args_to_use.push_back(Args[i].first);
346 --FPR_remaining;
Nate Begemanf7e43382005-03-26 07:46:36 +0000347 if (isVarArg) {
Nate Begeman96fc6812005-03-31 02:05:53 +0000348 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Chain,
349 Args[i].first, PtrOff);
350 MemOps.push_back(Store);
Nate Begeman74d73452005-03-31 00:15:26 +0000351 // Float varargs are always shadowed in available integer registers
352 if (GPR_remaining > 0) {
Nate Begeman96fc6812005-03-31 02:05:53 +0000353 SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff);
Nate Begeman74d73452005-03-31 00:15:26 +0000354 MemOps.push_back(Load);
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000355 args_to_use.push_back(Load);
356 --GPR_remaining;
Nate Begeman74d73452005-03-31 00:15:26 +0000357 }
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000358 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
Nate Begeman74d73452005-03-31 00:15:26 +0000359 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
360 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
Nate Begeman96fc6812005-03-31 02:05:53 +0000361 SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff);
Nate Begeman74d73452005-03-31 00:15:26 +0000362 MemOps.push_back(Load);
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000363 args_to_use.push_back(Load);
364 --GPR_remaining;
Nate Begeman74d73452005-03-31 00:15:26 +0000365 }
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000366 } else {
367 // If we have any FPRs remaining, we may also have GPRs remaining.
368 // Args passed in FPRs consume either 1 (f32) or 2 (f64) available
369 // GPRs.
370 if (GPR_remaining > 0) {
371 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
372 --GPR_remaining;
373 }
374 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
375 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
376 --GPR_remaining;
377 }
Nate Begeman74d73452005-03-31 00:15:26 +0000378 }
Nate Begeman307e7442005-03-26 01:28:53 +0000379 } else {
Nate Begeman74d73452005-03-31 00:15:26 +0000380 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
381 Args[i].first, PtrOff));
Nate Begeman307e7442005-03-26 01:28:53 +0000382 }
Nate Begemanf7e43382005-03-26 07:46:36 +0000383 ArgOffset += (ArgVT == MVT::f32) ? 4 : 8;
Nate Begeman307e7442005-03-26 01:28:53 +0000384 break;
385 }
Nate Begemana9795f82005-03-24 04:41:43 +0000386 }
Nate Begeman74d73452005-03-31 00:15:26 +0000387 if (!MemOps.empty())
388 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps);
Nate Begemana9795f82005-03-24 04:41:43 +0000389 }
390
391 std::vector<MVT::ValueType> RetVals;
392 MVT::ValueType RetTyVT = getValueType(RetTy);
393 if (RetTyVT != MVT::isVoid)
394 RetVals.push_back(RetTyVT);
395 RetVals.push_back(MVT::Other);
396
397 SDOperand TheCall = SDOperand(DAG.getCall(RetVals,
398 Chain, Callee, args_to_use), 0);
399 Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
400 Chain = DAG.getNode(ISD::ADJCALLSTACKUP, MVT::Other, Chain,
401 DAG.getConstant(NumBytes, getPointerTy()));
402 return std::make_pair(TheCall, Chain);
403}
404
405std::pair<SDOperand, SDOperand>
406PPC32TargetLowering::LowerVAStart(SDOperand Chain, SelectionDAG &DAG) {
407 //vastart just returns the address of the VarArgsFrameIndex slot.
408 return std::make_pair(DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32), Chain);
409}
410
411std::pair<SDOperand,SDOperand> PPC32TargetLowering::
412LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
413 const Type *ArgTy, SelectionDAG &DAG) {
Nate Begemanc7b09f12005-03-25 08:34:25 +0000414 MVT::ValueType ArgVT = getValueType(ArgTy);
415 SDOperand Result;
416 if (!isVANext) {
417 Result = DAG.getLoad(ArgVT, DAG.getEntryNode(), VAList);
418 } else {
419 unsigned Amt;
420 if (ArgVT == MVT::i32 || ArgVT == MVT::f32)
421 Amt = 4;
422 else {
423 assert((ArgVT == MVT::i64 || ArgVT == MVT::f64) &&
424 "Other types should have been promoted for varargs!");
425 Amt = 8;
426 }
427 Result = DAG.getNode(ISD::ADD, VAList.getValueType(), VAList,
428 DAG.getConstant(Amt, VAList.getValueType()));
429 }
430 return std::make_pair(Result, Chain);
Nate Begemana9795f82005-03-24 04:41:43 +0000431}
432
433
434std::pair<SDOperand, SDOperand> PPC32TargetLowering::
435LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
436 SelectionDAG &DAG) {
Nate Begeman01d05262005-03-30 01:45:43 +0000437 assert(0 && "LowerFrameReturnAddress unimplemented");
Nate Begemana9795f82005-03-24 04:41:43 +0000438 abort();
439}
440
441namespace {
Nate Begemanaa73a9f2005-04-03 11:20:20 +0000442Statistic<>NotLogic("ppc-codegen", "Number of inverted logical ops");
Nate Begeman93075ec2005-04-04 23:40:36 +0000443Statistic<>FusedFP("ppc-codegen", "Number of fused fp operations");
Nate Begemana9795f82005-03-24 04:41:43 +0000444//===--------------------------------------------------------------------===//
445/// ISel - PPC32 specific code to select PPC32 machine instructions for
446/// SelectionDAG operations.
447//===--------------------------------------------------------------------===//
448class ISel : public SelectionDAGISel {
Nate Begemana9795f82005-03-24 04:41:43 +0000449 PPC32TargetLowering PPC32Lowering;
Nate Begeman815d6da2005-04-06 00:25:27 +0000450 SelectionDAG *ISelDAG; // Hack to support us having a dag->dag transform
451 // for sdiv and udiv until it is put into the future
452 // dag combiner.
Nate Begemana9795f82005-03-24 04:41:43 +0000453
454 /// ExprMap - As shared expressions are codegen'd, we keep track of which
455 /// vreg the value is produced in, so we only emit one copy of each compiled
456 /// tree.
457 std::map<SDOperand, unsigned> ExprMap;
Nate Begemanc7b09f12005-03-25 08:34:25 +0000458
459 unsigned GlobalBaseReg;
460 bool GlobalBaseInitialized;
Nate Begemana9795f82005-03-24 04:41:43 +0000461
462public:
Nate Begeman815d6da2005-04-06 00:25:27 +0000463 ISel(TargetMachine &TM) : SelectionDAGISel(PPC32Lowering), PPC32Lowering(TM),
464 ISelDAG(0) {}
Nate Begemana9795f82005-03-24 04:41:43 +0000465
Nate Begemanc7b09f12005-03-25 08:34:25 +0000466 /// runOnFunction - Override this function in order to reset our per-function
467 /// variables.
468 virtual bool runOnFunction(Function &Fn) {
469 // Make sure we re-emit a set of the global base reg if necessary
470 GlobalBaseInitialized = false;
471 return SelectionDAGISel::runOnFunction(Fn);
472 }
473
Nate Begemana9795f82005-03-24 04:41:43 +0000474 /// InstructionSelectBasicBlock - This callback is invoked by
475 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
476 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG) {
477 DEBUG(BB->dump());
478 // Codegen the basic block.
Nate Begeman815d6da2005-04-06 00:25:27 +0000479 ISelDAG = &DAG;
Nate Begemana9795f82005-03-24 04:41:43 +0000480 Select(DAG.getRoot());
481
482 // Clear state used for selection.
483 ExprMap.clear();
Nate Begeman815d6da2005-04-06 00:25:27 +0000484 ISelDAG = 0;
Nate Begemana9795f82005-03-24 04:41:43 +0000485 }
Nate Begeman815d6da2005-04-06 00:25:27 +0000486
487 // dag -> dag expanders for integer divide by constant
488 SDOperand BuildSDIVSequence(SDOperand N);
489 SDOperand BuildUDIVSequence(SDOperand N);
Nate Begemana9795f82005-03-24 04:41:43 +0000490
Nate Begemandffcfcc2005-04-01 00:32:34 +0000491 unsigned getGlobalBaseReg();
Nate Begeman6b559972005-04-01 02:59:27 +0000492 unsigned getConstDouble(double floatVal, unsigned Result);
Nate Begeman7ddecb42005-04-06 23:51:40 +0000493 bool SelectBitfieldInsert(SDOperand OR, unsigned Result);
Nate Begemandffcfcc2005-04-01 00:32:34 +0000494 unsigned SelectSetCR0(SDOperand CC);
Nate Begemana9795f82005-03-24 04:41:43 +0000495 unsigned SelectExpr(SDOperand N);
496 unsigned SelectExprFP(SDOperand N, unsigned Result);
497 void Select(SDOperand N);
498
Nate Begeman04730362005-04-01 04:45:11 +0000499 bool SelectAddr(SDOperand N, unsigned& Reg, int& offset);
Nate Begemana9795f82005-03-24 04:41:43 +0000500 void SelectBranchCC(SDOperand N);
501};
502
Nate Begeman80196b12005-04-05 00:15:08 +0000503/// ExactLog2 - This function solves for (Val == 1 << (N-1)) and returns N. It
504/// returns zero when the input is not exactly a power of two.
505static unsigned ExactLog2(unsigned Val) {
506 if (Val == 0 || (Val & (Val-1))) return 0;
507 unsigned Count = 0;
508 while (Val != 1) {
509 Val >>= 1;
510 ++Count;
511 }
512 return Count;
513}
514
Nate Begeman7ddecb42005-04-06 23:51:40 +0000515// IsRunOfOnes - returns true if Val consists of one contiguous run of 1's with
516// any number of 0's on either side. the 1's are allowed to wrap from LSB to
517// MSB. so 0x000FFF0, 0x0000FFFF, and 0xFF0000FF are all runs. 0x0F0F0000 is
518// not, since all 1's are not contiguous.
519static bool IsRunOfOnes(unsigned Val, unsigned &MB, unsigned &ME) {
520 bool isRun = true;
521 MB = 0;
522 ME = 0;
523
524 // look for first set bit
525 int i = 0;
526 for (; i < 32; i++) {
527 if ((Val & (1 << (31 - i))) != 0) {
528 MB = i;
529 ME = i;
530 break;
531 }
532 }
533
534 // look for last set bit
535 for (; i < 32; i++) {
536 if ((Val & (1 << (31 - i))) == 0)
537 break;
538 ME = i;
539 }
540
541 // look for next set bit
542 for (; i < 32; i++) {
543 if ((Val & (1 << (31 - i))) != 0)
544 break;
545 }
546
547 // if we exhausted all the bits, we found a match at this point for 0*1*0*
548 if (i == 32)
549 return true;
550
551 // since we just encountered more 1's, if it doesn't wrap around to the
552 // most significant bit of the word, then we did not find a match to 1*0*1* so
553 // exit.
554 if (MB != 0)
555 return false;
556
557 // look for last set bit
558 for (MB = i; i < 32; i++) {
559 if ((Val & (1 << (31 - i))) == 0)
560 break;
561 }
562
563 // if we exhausted all the bits, then we found a match for 1*0*1*, otherwise,
564 // the value is not a run of ones.
565 if (i == 32)
566 return true;
567 return false;
568}
569
Nate Begeman439b4442005-04-05 04:22:58 +0000570/// getImmediateForOpcode - This method returns a value indicating whether
Nate Begemana9795f82005-03-24 04:41:43 +0000571/// the ConstantSDNode N can be used as an immediate to Opcode. The return
572/// values are either 0, 1 or 2. 0 indicates that either N is not a
573/// ConstantSDNode, or is not suitable for use by that opcode. A return value
574/// of 1 indicates that the constant may be used in normal immediate form. A
575/// return value of 2 indicates that the constant may be used in shifted
Nate Begeman439b4442005-04-05 04:22:58 +0000576/// immediate form. A return value of 3 indicates that log base 2 of the
Nate Begeman815d6da2005-04-06 00:25:27 +0000577/// constant may be used. A return value of 4 indicates that the constant is
578/// suitable for conversion into a magic number for integer division.
Nate Begemana9795f82005-03-24 04:41:43 +0000579///
Nate Begeman439b4442005-04-05 04:22:58 +0000580static unsigned getImmediateForOpcode(SDOperand N, unsigned Opcode,
581 unsigned& Imm, bool U = false) {
Nate Begemana9795f82005-03-24 04:41:43 +0000582 if (N.getOpcode() != ISD::Constant) return 0;
583
584 int v = (int)cast<ConstantSDNode>(N)->getSignExtended();
585
586 switch(Opcode) {
587 default: return 0;
588 case ISD::ADD:
589 if (v <= 32767 && v >= -32768) { Imm = v & 0xFFFF; return 1; }
590 if ((v & 0x0000FFFF) == 0) { Imm = v >> 16; return 2; }
591 break;
592 case ISD::AND:
593 case ISD::XOR:
594 case ISD::OR:
595 if (v >= 0 && v <= 65535) { Imm = v & 0xFFFF; return 1; }
596 if ((v & 0x0000FFFF) == 0) { Imm = v >> 16; return 2; }
597 break;
Nate Begeman307e7442005-03-26 01:28:53 +0000598 case ISD::MUL:
Nate Begeman27523a12005-04-02 00:42:16 +0000599 case ISD::SUB:
Nate Begeman307e7442005-03-26 01:28:53 +0000600 if (v <= 32767 && v >= -32768) { Imm = v & 0xFFFF; return 1; }
601 break;
Nate Begeman3e897162005-03-31 23:55:40 +0000602 case ISD::SETCC:
603 if (U && (v >= 0 && v <= 65535)) { Imm = v & 0xFFFF; return 1; }
604 if (!U && (v <= 32767 && v >= -32768)) { Imm = v & 0xFFFF; return 1; }
605 break;
Nate Begeman80196b12005-04-05 00:15:08 +0000606 case ISD::SDIV:
Nate Begeman439b4442005-04-05 04:22:58 +0000607 if ((Imm = ExactLog2(v))) { return 3; }
Nate Begeman815d6da2005-04-06 00:25:27 +0000608 if (v <= -2 || v >= 2) { return 4; }
609 break;
610 case ISD::UDIV:
Nate Begeman27b4c232005-04-06 06:44:57 +0000611 if (v > 1) { return 4; }
Nate Begeman80196b12005-04-05 00:15:08 +0000612 break;
Nate Begemana9795f82005-03-24 04:41:43 +0000613 }
614 return 0;
615}
Nate Begeman3e897162005-03-31 23:55:40 +0000616
617/// getBCCForSetCC - Returns the PowerPC condition branch mnemonic corresponding
618/// to Condition. If the Condition is unordered or unsigned, the bool argument
619/// U is set to true, otherwise it is set to false.
620static unsigned getBCCForSetCC(unsigned Condition, bool& U) {
621 U = false;
622 switch (Condition) {
623 default: assert(0 && "Unknown condition!"); abort();
624 case ISD::SETEQ: return PPC::BEQ;
625 case ISD::SETNE: return PPC::BNE;
626 case ISD::SETULT: U = true;
627 case ISD::SETLT: return PPC::BLT;
628 case ISD::SETULE: U = true;
629 case ISD::SETLE: return PPC::BLE;
630 case ISD::SETUGT: U = true;
631 case ISD::SETGT: return PPC::BGT;
632 case ISD::SETUGE: U = true;
633 case ISD::SETGE: return PPC::BGE;
634 }
Nate Begeman04730362005-04-01 04:45:11 +0000635 return 0;
636}
637
638/// IndexedOpForOp - Return the indexed variant for each of the PowerPC load
639/// and store immediate instructions.
640static unsigned IndexedOpForOp(unsigned Opcode) {
641 switch(Opcode) {
642 default: assert(0 && "Unknown opcode!"); abort();
643 case PPC::LBZ: return PPC::LBZX; case PPC::STB: return PPC::STBX;
644 case PPC::LHZ: return PPC::LHZX; case PPC::STH: return PPC::STHX;
645 case PPC::LHA: return PPC::LHAX; case PPC::STW: return PPC::STWX;
646 case PPC::LWZ: return PPC::LWZX; case PPC::STFS: return PPC::STFSX;
647 case PPC::LFS: return PPC::LFSX; case PPC::STFD: return PPC::STFDX;
648 case PPC::LFD: return PPC::LFDX;
649 }
650 return 0;
Nate Begeman3e897162005-03-31 23:55:40 +0000651}
Nate Begeman815d6da2005-04-06 00:25:27 +0000652
653// Structure used to return the necessary information to codegen an SDIV as
654// a multiply.
655struct ms {
656 int m; // magic number
657 int s; // shift amount
658};
659
660struct mu {
661 unsigned int m; // magic number
662 int a; // add indicator
663 int s; // shift amount
664};
665
666/// magic - calculate the magic numbers required to codegen an integer sdiv as
667/// a sequence of multiply and shifts. Requires that the divisor not be 0, 1,
668/// or -1.
669static struct ms magic(int d) {
670 int p;
671 unsigned int ad, anc, delta, q1, r1, q2, r2, t;
672 const unsigned int two31 = 2147483648U; // 2^31
673 struct ms mag;
674
675 ad = abs(d);
676 t = two31 + ((unsigned int)d >> 31);
677 anc = t - 1 - t%ad; // absolute value of nc
678 p = 31; // initialize p
679 q1 = two31/anc; // initialize q1 = 2p/abs(nc)
680 r1 = two31 - q1*anc; // initialize r1 = rem(2p,abs(nc))
681 q2 = two31/ad; // initialize q2 = 2p/abs(d)
682 r2 = two31 - q2*ad; // initialize r2 = rem(2p,abs(d))
683 do {
684 p = p + 1;
685 q1 = 2*q1; // update q1 = 2p/abs(nc)
686 r1 = 2*r1; // update r1 = rem(2p/abs(nc))
687 if (r1 >= anc) { // must be unsigned comparison
688 q1 = q1 + 1;
689 r1 = r1 - anc;
690 }
691 q2 = 2*q2; // update q2 = 2p/abs(d)
692 r2 = 2*r2; // update r2 = rem(2p/abs(d))
693 if (r2 >= ad) { // must be unsigned comparison
694 q2 = q2 + 1;
695 r2 = r2 - ad;
696 }
697 delta = ad - r2;
698 } while (q1 < delta || (q1 == delta && r1 == 0));
699
700 mag.m = q2 + 1;
701 if (d < 0) mag.m = -mag.m; // resulting magic number
702 mag.s = p - 32; // resulting shift
703 return mag;
704}
705
706/// magicu - calculate the magic numbers required to codegen an integer udiv as
707/// a sequence of multiply, add and shifts. Requires that the divisor not be 0.
708static struct mu magicu(unsigned d)
709{
710 int p;
711 unsigned int nc, delta, q1, r1, q2, r2;
712 struct mu magu;
713 magu.a = 0; // initialize "add" indicator
714 nc = - 1 - (-d)%d;
715 p = 31; // initialize p
716 q1 = 0x80000000/nc; // initialize q1 = 2p/nc
717 r1 = 0x80000000 - q1*nc; // initialize r1 = rem(2p,nc)
718 q2 = 0x7FFFFFFF/d; // initialize q2 = (2p-1)/d
719 r2 = 0x7FFFFFFF - q2*d; // initialize r2 = rem((2p-1),d)
720 do {
721 p = p + 1;
722 if (r1 >= nc - r1 ) {
723 q1 = 2*q1 + 1; // update q1
724 r1 = 2*r1 - nc; // update r1
725 }
726 else {
727 q1 = 2*q1; // update q1
728 r1 = 2*r1; // update r1
729 }
730 if (r2 + 1 >= d - r2) {
731 if (q2 >= 0x7FFFFFFF) magu.a = 1;
732 q2 = 2*q2 + 1; // update q2
733 r2 = 2*r2 + 1 - d; // update r2
734 }
735 else {
736 if (q2 >= 0x80000000) magu.a = 1;
737 q2 = 2*q2; // update q2
738 r2 = 2*r2 + 1; // update r2
739 }
740 delta = d - 1 - r2;
741 } while (p < 64 && (q1 < delta || (q1 == delta && r1 == 0)));
742 magu.m = q2 + 1; // resulting magic number
743 magu.s = p - 32; // resulting shift
744 return magu;
745}
746}
747
748/// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
749/// return a DAG expression to select that will generate the same value by
750/// multiplying by a magic number. See:
751/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
752SDOperand ISel::BuildSDIVSequence(SDOperand N) {
753 int d = (int)cast<ConstantSDNode>(N.getOperand(1))->getSignExtended();
754 ms magics = magic(d);
755 // Multiply the numerator (operand 0) by the magic value
756 SDOperand Q = ISelDAG->getNode(ISD::MULHS, MVT::i32, N.getOperand(0),
757 ISelDAG->getConstant(magics.m, MVT::i32));
758 // If d > 0 and m < 0, add the numerator
759 if (d > 0 && magics.m < 0)
760 Q = ISelDAG->getNode(ISD::ADD, MVT::i32, Q, N.getOperand(0));
761 // If d < 0 and m > 0, subtract the numerator.
762 if (d < 0 && magics.m > 0)
763 Q = ISelDAG->getNode(ISD::SUB, MVT::i32, Q, N.getOperand(0));
764 // Shift right algebraic if shift value is nonzero
765 if (magics.s > 0)
766 Q = ISelDAG->getNode(ISD::SRA, MVT::i32, Q,
767 ISelDAG->getConstant(magics.s, MVT::i32));
768 // Extract the sign bit and add it to the quotient
769 SDOperand T =
770 ISelDAG->getNode(ISD::SRL, MVT::i32, Q, ISelDAG->getConstant(31, MVT::i32));
Nate Begeman27b4c232005-04-06 06:44:57 +0000771 return ISelDAG->getNode(ISD::ADD, MVT::i32, Q, T);
Nate Begeman815d6da2005-04-06 00:25:27 +0000772}
773
774/// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
775/// return a DAG expression to select that will generate the same value by
776/// multiplying by a magic number. See:
777/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
778SDOperand ISel::BuildUDIVSequence(SDOperand N) {
779 unsigned d =
780 (unsigned)cast<ConstantSDNode>(N.getOperand(1))->getSignExtended();
781 mu magics = magicu(d);
782 // Multiply the numerator (operand 0) by the magic value
783 SDOperand Q = ISelDAG->getNode(ISD::MULHU, MVT::i32, N.getOperand(0),
784 ISelDAG->getConstant(magics.m, MVT::i32));
785 if (magics.a == 0) {
786 Q = ISelDAG->getNode(ISD::SRL, MVT::i32, Q,
787 ISelDAG->getConstant(magics.s, MVT::i32));
788 } else {
789 SDOperand NPQ = ISelDAG->getNode(ISD::SUB, MVT::i32, N.getOperand(0), Q);
790 NPQ = ISelDAG->getNode(ISD::SRL, MVT::i32, NPQ,
791 ISelDAG->getConstant(1, MVT::i32));
792 NPQ = ISelDAG->getNode(ISD::ADD, MVT::i32, NPQ, Q);
793 Q = ISelDAG->getNode(ISD::SRL, MVT::i32, NPQ,
794 ISelDAG->getConstant(magics.s-1, MVT::i32));
795 }
Nate Begeman27b4c232005-04-06 06:44:57 +0000796 return Q;
Nate Begemana9795f82005-03-24 04:41:43 +0000797}
798
Nate Begemanc7b09f12005-03-25 08:34:25 +0000799/// getGlobalBaseReg - Output the instructions required to put the
800/// base address to use for accessing globals into a register.
801///
802unsigned ISel::getGlobalBaseReg() {
803 if (!GlobalBaseInitialized) {
804 // Insert the set of GlobalBaseReg into the first MBB of the function
805 MachineBasicBlock &FirstMBB = BB->getParent()->front();
806 MachineBasicBlock::iterator MBBI = FirstMBB.begin();
807 GlobalBaseReg = MakeReg(MVT::i32);
808 BuildMI(FirstMBB, MBBI, PPC::MovePCtoLR, 0, PPC::LR);
809 BuildMI(FirstMBB, MBBI, PPC::MFLR, 1, GlobalBaseReg).addReg(PPC::LR);
810 GlobalBaseInitialized = true;
811 }
812 return GlobalBaseReg;
813}
814
Nate Begeman6b559972005-04-01 02:59:27 +0000815/// getConstDouble - Loads a floating point value into a register, via the
816/// Constant Pool. Optionally takes a register in which to load the value.
817unsigned ISel::getConstDouble(double doubleVal, unsigned Result=0) {
818 unsigned Tmp1 = MakeReg(MVT::i32);
819 if (0 == Result) Result = MakeReg(MVT::f64);
820 MachineConstantPool *CP = BB->getParent()->getConstantPool();
821 ConstantFP *CFP = ConstantFP::get(Type::DoubleTy, doubleVal);
822 unsigned CPI = CP->getConstantPoolIndex(CFP);
823 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp1).addReg(getGlobalBaseReg())
824 .addConstantPoolIndex(CPI);
825 BuildMI(BB, PPC::LFD, 2, Result).addConstantPoolIndex(CPI).addReg(Tmp1);
826 return Result;
827}
828
Nate Begeman7ddecb42005-04-06 23:51:40 +0000829/// SelectBitfieldInsert - turn an or of two masked values into
830/// the rotate left word immediate then mask insert (rlwimi) instruction.
831/// Returns true on success, false if the caller still needs to select OR.
832///
833/// Patterns matched:
834/// 1. or shl, and 5. or and, and
835/// 2. or and, shl 6. or shl, shr
836/// 3. or shr, and 7. or shr, shl
837/// 4. or and, shr
838bool ISel::SelectBitfieldInsert(SDOperand OR, unsigned Result) {
839 unsigned TgtMask = 0xFFFFFFFF, InsMask = 0xFFFFFFFF, Amount = 0;
840 unsigned Op0Opc = OR.getOperand(0).getOpcode();
841 unsigned Op1Opc = OR.getOperand(1).getOpcode();
842
843 // Verify that we have the correct opcodes
844 if (ISD::SHL != Op0Opc && ISD::SRL != Op0Opc && ISD::AND != Op0Opc)
845 return false;
846 if (ISD::SHL != Op1Opc && ISD::SRL != Op1Opc && ISD::AND != Op1Opc)
847 return false;
848
849 // Generate Mask value for Target
850 if (ConstantSDNode *CN =
851 dyn_cast<ConstantSDNode>(OR.getOperand(0).getOperand(1).Val)) {
852 switch(Op0Opc) {
853 case ISD::SHL: TgtMask <<= (unsigned)CN->getValue(); break;
854 case ISD::SRL: TgtMask >>= (unsigned)CN->getValue(); break;
855 case ISD::AND: TgtMask &= (unsigned)CN->getValue(); break;
856 }
857 } else {
858 return false;
859 }
860
861 // Generate Mask value for Insert
862 if (ConstantSDNode *CN =
863 dyn_cast<ConstantSDNode>(OR.getOperand(1).getOperand(1).Val)) {
864 switch(Op1Opc) {
865 case ISD::SHL:
866 Amount = CN->getValue();
867 InsMask <<= Amount;
868 break;
869 case ISD::SRL:
870 Amount = CN->getValue();
871 InsMask >>= Amount;
872 Amount = 32-Amount;
873 break;
874 case ISD::AND:
875 InsMask &= (unsigned)CN->getValue();
876 break;
877 }
878 } else {
879 return false;
880 }
881
882 // Verify that the Target mask and Insert mask together form a full word mask
883 // and that the Insert mask is a run of set bits (which implies both are runs
884 // of set bits). Given that, Select the arguments and generate the rlwimi
885 // instruction.
886 unsigned MB, ME;
887 if (((TgtMask ^ InsMask) == 0xFFFFFFFF) && IsRunOfOnes(InsMask, MB, ME)) {
888 unsigned Tmp1, Tmp2;
889 if (Op0Opc == ISD::AND)
890 Tmp1 = SelectExpr(OR.getOperand(0).getOperand(0));
891 else
892 Tmp1 = SelectExpr(OR.getOperand(0));
893 Tmp2 = SelectExpr(OR.getOperand(1).getOperand(0));
894 BuildMI(BB, PPC::RLWIMI, 5, Result).addReg(Tmp1).addReg(Tmp2)
895 .addImm(Amount).addImm(MB).addImm(ME);
896 return true;
897 }
898 return false;
899}
900
Nate Begemandffcfcc2005-04-01 00:32:34 +0000901unsigned ISel::SelectSetCR0(SDOperand CC) {
902 unsigned Opc, Tmp1, Tmp2;
903 static const unsigned CompareOpcodes[] =
904 { PPC::FCMPU, PPC::FCMPU, PPC::CMPW, PPC::CMPLW };
905
906 // If the first operand to the select is a SETCC node, then we can fold it
907 // into the branch that selects which value to return.
908 SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(CC.Val);
909 if (SetCC && CC.getOpcode() == ISD::SETCC) {
910 bool U;
911 Opc = getBCCForSetCC(SetCC->getCondition(), U);
912 Tmp1 = SelectExpr(SetCC->getOperand(0));
913
Nate Begeman439b4442005-04-05 04:22:58 +0000914 // Pass the optional argument U to getImmediateForOpcode for SETCC,
Nate Begemandffcfcc2005-04-01 00:32:34 +0000915 // so that it knows whether the SETCC immediate range is signed or not.
Nate Begeman439b4442005-04-05 04:22:58 +0000916 if (1 == getImmediateForOpcode(SetCC->getOperand(1), ISD::SETCC,
917 Tmp2, U)) {
Nate Begemandffcfcc2005-04-01 00:32:34 +0000918 if (U)
919 BuildMI(BB, PPC::CMPLWI, 2, PPC::CR0).addReg(Tmp1).addImm(Tmp2);
920 else
921 BuildMI(BB, PPC::CMPWI, 2, PPC::CR0).addReg(Tmp1).addSImm(Tmp2);
922 } else {
923 bool IsInteger = MVT::isInteger(SetCC->getOperand(0).getValueType());
924 unsigned CompareOpc = CompareOpcodes[2 * IsInteger + U];
925 Tmp2 = SelectExpr(SetCC->getOperand(1));
926 BuildMI(BB, CompareOpc, 2, PPC::CR0).addReg(Tmp1).addReg(Tmp2);
927 }
928 } else {
929 Tmp1 = SelectExpr(CC);
930 BuildMI(BB, PPC::CMPLWI, 2, PPC::CR0).addReg(Tmp1).addImm(0);
931 Opc = PPC::BNE;
932 }
933 return Opc;
934}
935
936/// Check to see if the load is a constant offset from a base register
Nate Begeman04730362005-04-01 04:45:11 +0000937bool ISel::SelectAddr(SDOperand N, unsigned& Reg, int& offset)
Nate Begemana9795f82005-03-24 04:41:43 +0000938{
Nate Begeman96fc6812005-03-31 02:05:53 +0000939 unsigned imm = 0, opcode = N.getOpcode();
Nate Begeman04730362005-04-01 04:45:11 +0000940 if (N.getOpcode() == ISD::ADD) {
941 Reg = SelectExpr(N.getOperand(0));
Nate Begeman439b4442005-04-05 04:22:58 +0000942 if (1 == getImmediateForOpcode(N.getOperand(1), opcode, imm)) {
Nate Begeman96fc6812005-03-31 02:05:53 +0000943 offset = imm;
Nate Begeman04730362005-04-01 04:45:11 +0000944 return false;
945 }
946 offset = SelectExpr(N.getOperand(1));
947 return true;
948 }
Nate Begemana9795f82005-03-24 04:41:43 +0000949 Reg = SelectExpr(N);
950 offset = 0;
Nate Begeman04730362005-04-01 04:45:11 +0000951 return false;
Nate Begemana9795f82005-03-24 04:41:43 +0000952}
953
954void ISel::SelectBranchCC(SDOperand N)
955{
956 assert(N.getOpcode() == ISD::BRCOND && "Not a BranchCC???");
957 MachineBasicBlock *Dest =
958 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
Nate Begeman3e897162005-03-31 23:55:40 +0000959
Nate Begeman439b4442005-04-05 04:22:58 +0000960 // Get the MBB we will fall through to so that we can hand it off to the
961 // branch selection pass as an argument to the PPC::COND_BRANCH pseudo op.
Nate Begemanc8c5c8f2005-04-05 04:32:16 +0000962 //ilist<MachineBasicBlock>::iterator It = BB;
963 //MachineBasicBlock *Fallthrough = ++It;
Nate Begeman439b4442005-04-05 04:22:58 +0000964
Nate Begemana9795f82005-03-24 04:41:43 +0000965 Select(N.getOperand(0)); //chain
Nate Begemandffcfcc2005-04-01 00:32:34 +0000966 unsigned Opc = SelectSetCR0(N.getOperand(1));
Nate Begemanc8c5c8f2005-04-05 04:32:16 +0000967 // FIXME: Use this once we have something approximating two-way branches
968 // We cannot currently use this in case the ISel hands us something like
969 // BRcc MBBx
970 // BR MBBy
971 // since the fallthrough basic block for the conditional branch does not start
972 // with the unconditional branch (it is skipped over).
973 //BuildMI(BB, PPC::COND_BRANCH, 4).addReg(PPC::CR0).addImm(Opc)
974 // .addMBB(Dest).addMBB(Fallthrough);
975 BuildMI(BB, Opc, 2).addReg(PPC::CR0).addMBB(Dest);
Nate Begemana9795f82005-03-24 04:41:43 +0000976 return;
977}
978
979unsigned ISel::SelectExprFP(SDOperand N, unsigned Result)
980{
981 unsigned Tmp1, Tmp2, Tmp3;
982 unsigned Opc = 0;
983 SDNode *Node = N.Val;
984 MVT::ValueType DestType = N.getValueType();
985 unsigned opcode = N.getOpcode();
986
987 switch (opcode) {
988 default:
989 Node->dump();
990 assert(0 && "Node not handled!\n");
991
Nate Begeman23afcfb2005-03-29 22:48:55 +0000992 case ISD::SELECT: {
Nate Begeman3e897162005-03-31 23:55:40 +0000993 // Attempt to generate FSEL. We can do this whenever we have an FP result,
994 // and an FP comparison in the SetCC node.
995 SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(N.getOperand(0).Val);
996 if (SetCC && N.getOperand(0).getOpcode() == ISD::SETCC &&
997 !MVT::isInteger(SetCC->getOperand(0).getValueType()) &&
998 SetCC->getCondition() != ISD::SETEQ &&
999 SetCC->getCondition() != ISD::SETNE) {
1000 MVT::ValueType VT = SetCC->getOperand(0).getValueType();
1001 Tmp1 = SelectExpr(SetCC->getOperand(0)); // Val to compare against
1002 unsigned TV = SelectExpr(N.getOperand(1)); // Use if TRUE
1003 unsigned FV = SelectExpr(N.getOperand(2)); // Use if FALSE
1004
1005 ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(1));
1006 if (CN && (CN->isExactlyValue(-0.0) || CN->isExactlyValue(0.0))) {
1007 switch(SetCC->getCondition()) {
1008 default: assert(0 && "Invalid FSEL condition"); abort();
1009 case ISD::SETULT:
1010 case ISD::SETLT:
1011 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp1).addReg(FV).addReg(TV);
1012 return Result;
1013 case ISD::SETUGE:
1014 case ISD::SETGE:
1015 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp1).addReg(TV).addReg(FV);
1016 return Result;
1017 case ISD::SETUGT:
1018 case ISD::SETGT: {
1019 Tmp2 = MakeReg(VT);
1020 BuildMI(BB, PPC::FNEG, 1, Tmp2).addReg(Tmp1);
1021 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp2).addReg(FV).addReg(TV);
1022 return Result;
1023 }
1024 case ISD::SETULE:
1025 case ISD::SETLE: {
1026 Tmp2 = MakeReg(VT);
1027 BuildMI(BB, PPC::FNEG, 1, Tmp2).addReg(Tmp1);
1028 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp2).addReg(TV).addReg(FV);
1029 return Result;
1030 }
1031 }
1032 } else {
1033 Opc = (MVT::f64 == VT) ? PPC::FSUB : PPC::FSUBS;
1034 Tmp2 = SelectExpr(SetCC->getOperand(1));
1035 Tmp3 = MakeReg(VT);
1036 switch(SetCC->getCondition()) {
1037 default: assert(0 && "Invalid FSEL condition"); abort();
1038 case ISD::SETULT:
1039 case ISD::SETLT:
1040 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
1041 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(FV).addReg(TV);
1042 return Result;
1043 case ISD::SETUGE:
1044 case ISD::SETGE:
1045 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
1046 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(TV).addReg(FV);
1047 return Result;
1048 case ISD::SETUGT:
1049 case ISD::SETGT:
1050 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp2).addReg(Tmp1);
1051 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(FV).addReg(TV);
1052 return Result;
1053 case ISD::SETULE:
1054 case ISD::SETLE:
1055 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp2).addReg(Tmp1);
1056 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(TV).addReg(FV);
1057 return Result;
1058 }
1059 }
1060 assert(0 && "Should never get here");
1061 return 0;
1062 }
1063
Nate Begeman31318e42005-04-01 07:21:30 +00001064 unsigned TrueValue = SelectExpr(N.getOperand(1)); //Use if TRUE
1065 unsigned FalseValue = SelectExpr(N.getOperand(2)); //Use if FALSE
Nate Begeman6cb2e1b2005-04-01 08:57:43 +00001066 Opc = SelectSetCR0(N.getOperand(0));
Nate Begeman31318e42005-04-01 07:21:30 +00001067
Nate Begeman23afcfb2005-03-29 22:48:55 +00001068 // Create an iterator with which to insert the MBB for copying the false
1069 // value and the MBB to hold the PHI instruction for this SetCC.
1070 MachineBasicBlock *thisMBB = BB;
1071 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1072 ilist<MachineBasicBlock>::iterator It = BB;
1073 ++It;
1074
1075 // thisMBB:
1076 // ...
1077 // TrueVal = ...
1078 // cmpTY cr0, r1, r2
1079 // bCC copy1MBB
1080 // fallthrough --> copy0MBB
Nate Begeman23afcfb2005-03-29 22:48:55 +00001081 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
1082 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
Nate Begeman6cb2e1b2005-04-01 08:57:43 +00001083 BuildMI(BB, Opc, 2).addReg(PPC::CR0).addMBB(sinkMBB);
Nate Begeman23afcfb2005-03-29 22:48:55 +00001084 MachineFunction *F = BB->getParent();
1085 F->getBasicBlockList().insert(It, copy0MBB);
1086 F->getBasicBlockList().insert(It, sinkMBB);
1087 // Update machine-CFG edges
1088 BB->addSuccessor(copy0MBB);
1089 BB->addSuccessor(sinkMBB);
1090
1091 // copy0MBB:
1092 // %FalseValue = ...
1093 // # fallthrough to sinkMBB
1094 BB = copy0MBB;
Nate Begeman23afcfb2005-03-29 22:48:55 +00001095 // Update machine-CFG edges
1096 BB->addSuccessor(sinkMBB);
1097
1098 // sinkMBB:
1099 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1100 // ...
1101 BB = sinkMBB;
1102 BuildMI(BB, PPC::PHI, 4, Result).addReg(FalseValue)
1103 .addMBB(copy0MBB).addReg(TrueValue).addMBB(thisMBB);
1104 return Result;
1105 }
Nate Begeman27eeb002005-04-02 05:59:34 +00001106
1107 case ISD::FNEG:
Nate Begeman93075ec2005-04-04 23:40:36 +00001108 if (!NoExcessFPPrecision &&
1109 ISD::ADD == N.getOperand(0).getOpcode() &&
1110 N.getOperand(0).Val->hasOneUse() &&
1111 ISD::MUL == N.getOperand(0).getOperand(0).getOpcode() &&
1112 N.getOperand(0).getOperand(0).Val->hasOneUse()) {
Nate Begeman80196b12005-04-05 00:15:08 +00001113 ++FusedFP; // Statistic
Nate Begeman93075ec2005-04-04 23:40:36 +00001114 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(0));
1115 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(1));
1116 Tmp3 = SelectExpr(N.getOperand(0).getOperand(1));
1117 Opc = DestType == MVT::f64 ? PPC::FNMADD : PPC::FNMADDS;
1118 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1119 } else if (!NoExcessFPPrecision &&
Nate Begemane88aa5b2005-04-09 03:05:51 +00001120 ISD::ADD == N.getOperand(0).getOpcode() &&
Nate Begeman93075ec2005-04-04 23:40:36 +00001121 N.getOperand(0).Val->hasOneUse() &&
Nate Begemane88aa5b2005-04-09 03:05:51 +00001122 ISD::MUL == N.getOperand(0).getOperand(1).getOpcode() &&
1123 N.getOperand(0).getOperand(1).Val->hasOneUse()) {
Nate Begeman80196b12005-04-05 00:15:08 +00001124 ++FusedFP; // Statistic
Nate Begemane88aa5b2005-04-09 03:05:51 +00001125 Tmp1 = SelectExpr(N.getOperand(0).getOperand(1).getOperand(0));
1126 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1).getOperand(1));
1127 Tmp3 = SelectExpr(N.getOperand(0).getOperand(0));
1128 Opc = DestType == MVT::f64 ? PPC::FNMADD : PPC::FNMADDS;
Nate Begeman93075ec2005-04-04 23:40:36 +00001129 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1130 } else if (ISD::FABS == N.getOperand(0).getOpcode()) {
Nate Begeman27eeb002005-04-02 05:59:34 +00001131 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1132 BuildMI(BB, PPC::FNABS, 1, Result).addReg(Tmp1);
1133 } else {
1134 Tmp1 = SelectExpr(N.getOperand(0));
1135 BuildMI(BB, PPC::FNEG, 1, Result).addReg(Tmp1);
1136 }
1137 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001138
Nate Begeman27eeb002005-04-02 05:59:34 +00001139 case ISD::FABS:
1140 Tmp1 = SelectExpr(N.getOperand(0));
1141 BuildMI(BB, PPC::FABS, 1, Result).addReg(Tmp1);
1142 return Result;
1143
Nate Begemana9795f82005-03-24 04:41:43 +00001144 case ISD::FP_ROUND:
1145 assert (DestType == MVT::f32 &&
1146 N.getOperand(0).getValueType() == MVT::f64 &&
1147 "only f64 to f32 conversion supported here");
1148 Tmp1 = SelectExpr(N.getOperand(0));
1149 BuildMI(BB, PPC::FRSP, 1, Result).addReg(Tmp1);
1150 return Result;
1151
1152 case ISD::FP_EXTEND:
1153 assert (DestType == MVT::f64 &&
1154 N.getOperand(0).getValueType() == MVT::f32 &&
1155 "only f32 to f64 conversion supported here");
1156 Tmp1 = SelectExpr(N.getOperand(0));
1157 BuildMI(BB, PPC::FMR, 1, Result).addReg(Tmp1);
1158 return Result;
1159
1160 case ISD::CopyFromReg:
Nate Begemanf2622612005-03-26 02:17:46 +00001161 if (Result == 1)
1162 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1163 Tmp1 = dyn_cast<RegSDNode>(Node)->getReg();
1164 BuildMI(BB, PPC::FMR, 1, Result).addReg(Tmp1);
1165 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001166
Nate Begeman6d369cc2005-04-01 01:08:07 +00001167 case ISD::ConstantFP: {
Nate Begeman6d369cc2005-04-01 01:08:07 +00001168 ConstantFPSDNode *CN = cast<ConstantFPSDNode>(N);
Nate Begeman6b559972005-04-01 02:59:27 +00001169 Result = getConstDouble(CN->getValue(), Result);
Nate Begeman6d369cc2005-04-01 01:08:07 +00001170 return Result;
1171 }
Nate Begemana9795f82005-03-24 04:41:43 +00001172
Nate Begemana9795f82005-03-24 04:41:43 +00001173 case ISD::ADD:
Nate Begeman93075ec2005-04-04 23:40:36 +00001174 if (!NoExcessFPPrecision && N.getOperand(0).getOpcode() == ISD::MUL &&
1175 N.getOperand(0).Val->hasOneUse()) {
1176 ++FusedFP; // Statistic
1177 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1178 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1179 Tmp3 = SelectExpr(N.getOperand(1));
1180 Opc = DestType == MVT::f64 ? PPC::FMADD : PPC::FMADDS;
1181 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1182 return Result;
1183 }
Nate Begemane88aa5b2005-04-09 03:05:51 +00001184 if (!NoExcessFPPrecision && N.getOperand(1).getOpcode() == ISD::MUL &&
1185 N.getOperand(1).Val->hasOneUse()) {
1186 ++FusedFP; // Statistic
1187 Tmp1 = SelectExpr(N.getOperand(1).getOperand(0));
1188 Tmp2 = SelectExpr(N.getOperand(1).getOperand(1));
1189 Tmp3 = SelectExpr(N.getOperand(0));
1190 Opc = DestType == MVT::f64 ? PPC::FMADD : PPC::FMADDS;
1191 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1192 return Result;
1193 }
Nate Begeman93075ec2005-04-04 23:40:36 +00001194 Opc = DestType == MVT::f64 ? PPC::FADD : PPC::FADDS;
1195 Tmp1 = SelectExpr(N.getOperand(0));
1196 Tmp2 = SelectExpr(N.getOperand(1));
1197 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1198 return Result;
1199
Nate Begemana9795f82005-03-24 04:41:43 +00001200 case ISD::SUB:
Nate Begeman93075ec2005-04-04 23:40:36 +00001201 if (!NoExcessFPPrecision && N.getOperand(0).getOpcode() == ISD::MUL &&
1202 N.getOperand(0).Val->hasOneUse()) {
1203 ++FusedFP; // Statistic
1204 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1205 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1206 Tmp3 = SelectExpr(N.getOperand(1));
1207 Opc = DestType == MVT::f64 ? PPC::FMSUB : PPC::FMSUBS;
1208 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1209 return Result;
1210 }
Nate Begemane88aa5b2005-04-09 03:05:51 +00001211 if (!NoExcessFPPrecision && N.getOperand(1).getOpcode() == ISD::MUL &&
1212 N.getOperand(1).Val->hasOneUse()) {
1213 ++FusedFP; // Statistic
1214 Tmp1 = SelectExpr(N.getOperand(1).getOperand(0));
1215 Tmp2 = SelectExpr(N.getOperand(1).getOperand(1));
1216 Tmp3 = SelectExpr(N.getOperand(0));
1217 Opc = DestType == MVT::f64 ? PPC::FNMSUB : PPC::FNMSUBS;
1218 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1219 return Result;
1220 }
Nate Begeman93075ec2005-04-04 23:40:36 +00001221 Opc = DestType == MVT::f64 ? PPC::FSUB : PPC::FSUBS;
1222 Tmp1 = SelectExpr(N.getOperand(0));
1223 Tmp2 = SelectExpr(N.getOperand(1));
1224 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1225 return Result;
1226
1227 case ISD::MUL:
Nate Begemana9795f82005-03-24 04:41:43 +00001228 case ISD::SDIV:
1229 switch( opcode ) {
1230 case ISD::MUL: Opc = DestType == MVT::f64 ? PPC::FMUL : PPC::FMULS; break;
Nate Begemana9795f82005-03-24 04:41:43 +00001231 case ISD::SDIV: Opc = DestType == MVT::f64 ? PPC::FDIV : PPC::FDIVS; break;
1232 };
Nate Begemana9795f82005-03-24 04:41:43 +00001233 Tmp1 = SelectExpr(N.getOperand(0));
1234 Tmp2 = SelectExpr(N.getOperand(1));
1235 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1236 return Result;
1237
Nate Begemana9795f82005-03-24 04:41:43 +00001238 case ISD::UINT_TO_FP:
Nate Begemanfdcf3412005-03-30 19:38:35 +00001239 case ISD::SINT_TO_FP: {
1240 assert (N.getOperand(0).getValueType() == MVT::i32
1241 && "int to float must operate on i32");
1242 bool IsUnsigned = (ISD::UINT_TO_FP == opcode);
1243 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
1244 Tmp2 = MakeReg(MVT::f64); // temp reg to load the integer value into
1245 Tmp3 = MakeReg(MVT::i32); // temp reg to hold the conversion constant
1246 unsigned ConstF = MakeReg(MVT::f64); // temp reg to hold the fp constant
1247
1248 int FrameIdx = BB->getParent()->getFrameInfo()->CreateStackObject(8, 8);
1249 MachineConstantPool *CP = BB->getParent()->getConstantPool();
1250
1251 // FIXME: pull this FP constant generation stuff out into something like
1252 // the simple ISel's getReg.
1253 if (IsUnsigned) {
1254 ConstantFP *CFP = ConstantFP::get(Type::DoubleTy, 0x1.000000p52);
1255 unsigned CPI = CP->getConstantPoolIndex(CFP);
1256 // Load constant fp value
1257 unsigned Tmp4 = MakeReg(MVT::i32);
1258 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp4).addReg(getGlobalBaseReg())
1259 .addConstantPoolIndex(CPI);
1260 BuildMI(BB, PPC::LFD, 2, ConstF).addConstantPoolIndex(CPI).addReg(Tmp4);
1261 // Store the hi & low halves of the fp value, currently in int regs
1262 BuildMI(BB, PPC::LIS, 1, Tmp3).addSImm(0x4330);
1263 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(Tmp3), FrameIdx);
1264 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(Tmp1), FrameIdx, 4);
1265 addFrameReference(BuildMI(BB, PPC::LFD, 2, Tmp2), FrameIdx);
1266 // Generate the return value with a subtract
1267 BuildMI(BB, PPC::FSUB, 2, Result).addReg(Tmp2).addReg(ConstF);
1268 } else {
1269 ConstantFP *CFP = ConstantFP::get(Type::DoubleTy, 0x1.000008p52);
1270 unsigned CPI = CP->getConstantPoolIndex(CFP);
1271 // Load constant fp value
1272 unsigned Tmp4 = MakeReg(MVT::i32);
1273 unsigned TmpL = MakeReg(MVT::i32);
1274 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp4).addReg(getGlobalBaseReg())
1275 .addConstantPoolIndex(CPI);
1276 BuildMI(BB, PPC::LFD, 2, ConstF).addConstantPoolIndex(CPI).addReg(Tmp4);
1277 // Store the hi & low halves of the fp value, currently in int regs
1278 BuildMI(BB, PPC::LIS, 1, Tmp3).addSImm(0x4330);
1279 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(Tmp3), FrameIdx);
1280 BuildMI(BB, PPC::XORIS, 2, TmpL).addReg(Tmp1).addImm(0x8000);
1281 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(TmpL), FrameIdx, 4);
1282 addFrameReference(BuildMI(BB, PPC::LFD, 2, Tmp2), FrameIdx);
1283 // Generate the return value with a subtract
1284 BuildMI(BB, PPC::FSUB, 2, Result).addReg(Tmp2).addReg(ConstF);
1285 }
1286 return Result;
1287 }
Nate Begemana9795f82005-03-24 04:41:43 +00001288 }
Nate Begeman6b559972005-04-01 02:59:27 +00001289 assert(0 && "Should never get here");
Nate Begemana9795f82005-03-24 04:41:43 +00001290 return 0;
1291}
1292
1293unsigned ISel::SelectExpr(SDOperand N) {
1294 unsigned Result;
1295 unsigned Tmp1, Tmp2, Tmp3;
1296 unsigned Opc = 0;
1297 unsigned opcode = N.getOpcode();
1298
1299 SDNode *Node = N.Val;
1300 MVT::ValueType DestType = N.getValueType();
1301
1302 unsigned &Reg = ExprMap[N];
1303 if (Reg) return Reg;
1304
Nate Begeman27eeb002005-04-02 05:59:34 +00001305 switch (N.getOpcode()) {
1306 default:
Nate Begemana9795f82005-03-24 04:41:43 +00001307 Reg = Result = (N.getValueType() != MVT::Other) ?
Nate Begeman27eeb002005-04-02 05:59:34 +00001308 MakeReg(N.getValueType()) : 1;
1309 break;
1310 case ISD::CALL:
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001311 // If this is a call instruction, make sure to prepare ALL of the result
1312 // values as well as the chain.
Nate Begeman27eeb002005-04-02 05:59:34 +00001313 if (Node->getNumValues() == 1)
1314 Reg = Result = 1; // Void call, just a chain.
1315 else {
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001316 Result = MakeReg(Node->getValueType(0));
1317 ExprMap[N.getValue(0)] = Result;
Nate Begeman27eeb002005-04-02 05:59:34 +00001318 for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001319 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
Nate Begeman27eeb002005-04-02 05:59:34 +00001320 ExprMap[SDOperand(Node, Node->getNumValues()-1)] = 1;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001321 }
Nate Begeman27eeb002005-04-02 05:59:34 +00001322 break;
1323 case ISD::ADD_PARTS:
1324 case ISD::SUB_PARTS:
1325 case ISD::SHL_PARTS:
1326 case ISD::SRL_PARTS:
1327 case ISD::SRA_PARTS:
1328 Result = MakeReg(Node->getValueType(0));
1329 ExprMap[N.getValue(0)] = Result;
1330 for (unsigned i = 1, e = N.Val->getNumValues(); i != e; ++i)
1331 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
1332 break;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001333 }
1334
Nate Begemane5846682005-04-04 06:52:38 +00001335 if (ISD::CopyFromReg == opcode)
1336 DestType = N.getValue(0).getValueType();
1337
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001338 if (DestType == MVT::f64 || DestType == MVT::f32)
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001339 if (ISD::LOAD != opcode && ISD::EXTLOAD != opcode && ISD::UNDEF != opcode)
Nate Begeman74d73452005-03-31 00:15:26 +00001340 return SelectExprFP(N, Result);
Nate Begemana9795f82005-03-24 04:41:43 +00001341
1342 switch (opcode) {
1343 default:
1344 Node->dump();
1345 assert(0 && "Node not handled!\n");
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001346 case ISD::UNDEF:
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001347 BuildMI(BB, PPC::IMPLICIT_DEF, 0, Result);
1348 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001349 case ISD::DYNAMIC_STACKALLOC:
Nate Begeman5e966612005-03-24 06:28:42 +00001350 // Generate both result values. FIXME: Need a better commment here?
1351 if (Result != 1)
1352 ExprMap[N.getValue(1)] = 1;
1353 else
1354 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1355
1356 // FIXME: We are currently ignoring the requested alignment for handling
1357 // greater than the stack alignment. This will need to be revisited at some
1358 // point. Align = N.getOperand(2);
1359 if (!isa<ConstantSDNode>(N.getOperand(2)) ||
1360 cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) {
1361 std::cerr << "Cannot allocate stack object with greater alignment than"
1362 << " the stack alignment yet!";
1363 abort();
1364 }
1365 Select(N.getOperand(0));
1366 Tmp1 = SelectExpr(N.getOperand(1));
1367 // Subtract size from stack pointer, thereby allocating some space.
1368 BuildMI(BB, PPC::SUBF, 2, PPC::R1).addReg(Tmp1).addReg(PPC::R1);
1369 // Put a pointer to the space into the result register by copying the SP
1370 BuildMI(BB, PPC::OR, 2, Result).addReg(PPC::R1).addReg(PPC::R1);
1371 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001372
1373 case ISD::ConstantPool:
Nate Begemanca12a2b2005-03-28 22:28:37 +00001374 Tmp1 = cast<ConstantPoolSDNode>(N)->getIndex();
1375 Tmp2 = MakeReg(MVT::i32);
1376 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp2).addReg(getGlobalBaseReg())
1377 .addConstantPoolIndex(Tmp1);
1378 BuildMI(BB, PPC::LA, 2, Result).addReg(Tmp2).addConstantPoolIndex(Tmp1);
1379 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001380
1381 case ISD::FrameIndex:
Nate Begemanf3d08f32005-03-29 00:03:27 +00001382 Tmp1 = cast<FrameIndexSDNode>(N)->getIndex();
Nate Begeman58f718c2005-03-30 02:23:08 +00001383 addFrameReference(BuildMI(BB, PPC::ADDI, 2, Result), (int)Tmp1, 0, false);
Nate Begemanf3d08f32005-03-29 00:03:27 +00001384 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001385
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001386 case ISD::GlobalAddress: {
1387 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
Nate Begemanca12a2b2005-03-28 22:28:37 +00001388 Tmp1 = MakeReg(MVT::i32);
Nate Begemanc7b09f12005-03-25 08:34:25 +00001389 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp1).addReg(getGlobalBaseReg())
1390 .addGlobalAddress(GV);
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001391 if (GV->hasWeakLinkage() || GV->isExternal()) {
1392 BuildMI(BB, PPC::LWZ, 2, Result).addGlobalAddress(GV).addReg(Tmp1);
1393 } else {
1394 BuildMI(BB, PPC::LA, 2, Result).addReg(Tmp1).addGlobalAddress(GV);
1395 }
1396 return Result;
1397 }
1398
Nate Begeman5e966612005-03-24 06:28:42 +00001399 case ISD::LOAD:
Nate Begemana9795f82005-03-24 04:41:43 +00001400 case ISD::EXTLOAD:
1401 case ISD::ZEXTLOAD:
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001402 case ISD::SEXTLOAD: {
Nate Begeman9db505c2005-03-28 19:36:43 +00001403 MVT::ValueType TypeBeingLoaded = (ISD::LOAD == opcode) ?
1404 Node->getValueType(0) : cast<MVTSDNode>(Node)->getExtraValueType();
Nate Begeman74d73452005-03-31 00:15:26 +00001405 bool sext = (ISD::SEXTLOAD == opcode);
Nate Begeman74d73452005-03-31 00:15:26 +00001406
Nate Begeman5e966612005-03-24 06:28:42 +00001407 // Make sure we generate both values.
1408 if (Result != 1)
1409 ExprMap[N.getValue(1)] = 1; // Generate the token
1410 else
1411 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1412
1413 SDOperand Chain = N.getOperand(0);
1414 SDOperand Address = N.getOperand(1);
1415 Select(Chain);
1416
Nate Begeman9db505c2005-03-28 19:36:43 +00001417 switch (TypeBeingLoaded) {
Nate Begeman74d73452005-03-31 00:15:26 +00001418 default: Node->dump(); assert(0 && "Cannot load this type!");
Nate Begeman9db505c2005-03-28 19:36:43 +00001419 case MVT::i1: Opc = PPC::LBZ; break;
1420 case MVT::i8: Opc = PPC::LBZ; break;
1421 case MVT::i16: Opc = sext ? PPC::LHA : PPC::LHZ; break;
1422 case MVT::i32: Opc = PPC::LWZ; break;
Nate Begeman74d73452005-03-31 00:15:26 +00001423 case MVT::f32: Opc = PPC::LFS; break;
1424 case MVT::f64: Opc = PPC::LFD; break;
Nate Begeman5e966612005-03-24 06:28:42 +00001425 }
1426
Nate Begeman74d73452005-03-31 00:15:26 +00001427 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Address)) {
1428 Tmp1 = MakeReg(MVT::i32);
1429 int CPI = CP->getIndex();
1430 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp1).addReg(getGlobalBaseReg())
1431 .addConstantPoolIndex(CPI);
1432 BuildMI(BB, Opc, 2, Result).addConstantPoolIndex(CPI).addReg(Tmp1);
Nate Begeman9db505c2005-03-28 19:36:43 +00001433 }
Nate Begeman74d73452005-03-31 00:15:26 +00001434 else if(Address.getOpcode() == ISD::FrameIndex) {
Nate Begeman58f718c2005-03-30 02:23:08 +00001435 Tmp1 = cast<FrameIndexSDNode>(Address)->getIndex();
1436 addFrameReference(BuildMI(BB, Opc, 2, Result), (int)Tmp1);
Nate Begeman5e966612005-03-24 06:28:42 +00001437 } else {
1438 int offset;
Nate Begeman04730362005-04-01 04:45:11 +00001439 bool idx = SelectAddr(Address, Tmp1, offset);
1440 if (idx) {
1441 Opc = IndexedOpForOp(Opc);
1442 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(offset);
1443 } else {
1444 BuildMI(BB, Opc, 2, Result).addSImm(offset).addReg(Tmp1);
1445 }
Nate Begeman5e966612005-03-24 06:28:42 +00001446 }
1447 return Result;
1448 }
1449
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001450 case ISD::CALL: {
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001451 unsigned GPR_idx = 0, FPR_idx = 0;
1452 static const unsigned GPR[] = {
1453 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
1454 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
1455 };
1456 static const unsigned FPR[] = {
1457 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
1458 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
1459 };
1460
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001461 // Lower the chain for this call.
1462 Select(N.getOperand(0));
1463 ExprMap[N.getValue(Node->getNumValues()-1)] = 1;
Nate Begeman74d73452005-03-31 00:15:26 +00001464
Nate Begemand860aa62005-04-04 22:17:48 +00001465 MachineInstr *CallMI;
1466 // Emit the correct call instruction based on the type of symbol called.
1467 if (GlobalAddressSDNode *GASD =
1468 dyn_cast<GlobalAddressSDNode>(N.getOperand(1))) {
1469 CallMI = BuildMI(PPC::CALLpcrel, 1).addGlobalAddress(GASD->getGlobal(),
1470 true);
1471 } else if (ExternalSymbolSDNode *ESSDN =
1472 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1))) {
1473 CallMI = BuildMI(PPC::CALLpcrel, 1).addExternalSymbol(ESSDN->getSymbol(),
1474 true);
1475 } else {
1476 Tmp1 = SelectExpr(N.getOperand(1));
1477 BuildMI(BB, PPC::OR, 2, PPC::R12).addReg(Tmp1).addReg(Tmp1);
1478 BuildMI(BB, PPC::MTCTR, 1).addReg(PPC::R12);
1479 CallMI = BuildMI(PPC::CALLindirect, 3).addImm(20).addImm(0)
1480 .addReg(PPC::R12);
1481 }
1482
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001483 // Load the register args to virtual regs
1484 std::vector<unsigned> ArgVR;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001485 for(int i = 2, e = Node->getNumOperands(); i < e; ++i)
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001486 ArgVR.push_back(SelectExpr(N.getOperand(i)));
1487
1488 // Copy the virtual registers into the appropriate argument register
1489 for(int i = 0, e = ArgVR.size(); i < e; ++i) {
1490 switch(N.getOperand(i+2).getValueType()) {
1491 default: Node->dump(); assert(0 && "Unknown value type for call");
1492 case MVT::i1:
1493 case MVT::i8:
1494 case MVT::i16:
1495 case MVT::i32:
1496 assert(GPR_idx < 8 && "Too many int args");
Nate Begemand860aa62005-04-04 22:17:48 +00001497 if (N.getOperand(i+2).getOpcode() != ISD::UNDEF) {
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001498 BuildMI(BB, PPC::OR,2,GPR[GPR_idx]).addReg(ArgVR[i]).addReg(ArgVR[i]);
Nate Begemand860aa62005-04-04 22:17:48 +00001499 CallMI->addRegOperand(GPR[GPR_idx], MachineOperand::Use);
1500 }
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001501 ++GPR_idx;
1502 break;
1503 case MVT::f64:
1504 case MVT::f32:
1505 assert(FPR_idx < 13 && "Too many fp args");
1506 BuildMI(BB, PPC::FMR, 1, FPR[FPR_idx]).addReg(ArgVR[i]);
Nate Begemand860aa62005-04-04 22:17:48 +00001507 CallMI->addRegOperand(FPR[FPR_idx], MachineOperand::Use);
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001508 ++FPR_idx;
1509 break;
1510 }
1511 }
Nate Begemand860aa62005-04-04 22:17:48 +00001512
1513 // Put the call instruction in the correct place in the MachineBasicBlock
1514 BB->push_back(CallMI);
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001515
1516 switch (Node->getValueType(0)) {
1517 default: assert(0 && "Unknown value type for call result!");
1518 case MVT::Other: return 1;
1519 case MVT::i1:
1520 case MVT::i8:
1521 case MVT::i16:
1522 case MVT::i32:
Nate Begemane5846682005-04-04 06:52:38 +00001523 if (Node->getValueType(1) == MVT::i32) {
1524 BuildMI(BB, PPC::OR, 2, Result+1).addReg(PPC::R3).addReg(PPC::R3);
1525 BuildMI(BB, PPC::OR, 2, Result).addReg(PPC::R4).addReg(PPC::R4);
1526 } else {
1527 BuildMI(BB, PPC::OR, 2, Result).addReg(PPC::R3).addReg(PPC::R3);
1528 }
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001529 break;
1530 case MVT::f32:
1531 case MVT::f64:
1532 BuildMI(BB, PPC::FMR, 1, Result).addReg(PPC::F1);
1533 break;
1534 }
1535 return Result+N.ResNo;
1536 }
Nate Begemana9795f82005-03-24 04:41:43 +00001537
1538 case ISD::SIGN_EXTEND:
1539 case ISD::SIGN_EXTEND_INREG:
1540 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman9db505c2005-03-28 19:36:43 +00001541 switch(cast<MVTSDNode>(Node)->getExtraValueType()) {
1542 default: Node->dump(); assert(0 && "Unhandled SIGN_EXTEND type"); break;
1543 case MVT::i16:
1544 BuildMI(BB, PPC::EXTSH, 1, Result).addReg(Tmp1);
1545 break;
1546 case MVT::i8:
1547 BuildMI(BB, PPC::EXTSB, 1, Result).addReg(Tmp1);
1548 break;
Nate Begeman74747862005-03-29 22:24:51 +00001549 case MVT::i1:
1550 BuildMI(BB, PPC::SUBFIC, 2, Result).addReg(Tmp1).addSImm(0);
1551 break;
Nate Begeman9db505c2005-03-28 19:36:43 +00001552 }
Nate Begemana9795f82005-03-24 04:41:43 +00001553 return Result;
1554
1555 case ISD::ZERO_EXTEND_INREG:
1556 Tmp1 = SelectExpr(N.getOperand(0));
1557 switch(cast<MVTSDNode>(Node)->getExtraValueType()) {
Nate Begeman9db505c2005-03-28 19:36:43 +00001558 default: Node->dump(); assert(0 && "Unhandled ZERO_EXTEND type"); break;
Nate Begemana9795f82005-03-24 04:41:43 +00001559 case MVT::i16: Tmp2 = 16; break;
1560 case MVT::i8: Tmp2 = 24; break;
1561 case MVT::i1: Tmp2 = 31; break;
1562 }
Nate Begeman33162522005-03-29 21:54:38 +00001563 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(0).addImm(Tmp2)
1564 .addImm(31);
Nate Begemana9795f82005-03-24 04:41:43 +00001565 return Result;
1566
Nate Begemana9795f82005-03-24 04:41:43 +00001567 case ISD::CopyFromReg:
1568 if (Result == 1)
1569 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1570 Tmp1 = dyn_cast<RegSDNode>(Node)->getReg();
1571 BuildMI(BB, PPC::OR, 2, Result).addReg(Tmp1).addReg(Tmp1);
1572 return Result;
1573
1574 case ISD::SHL:
Nate Begeman5e966612005-03-24 06:28:42 +00001575 Tmp1 = SelectExpr(N.getOperand(0));
1576 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1577 Tmp2 = CN->getValue() & 0x1F;
Nate Begeman33162522005-03-29 21:54:38 +00001578 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(Tmp2).addImm(0)
Nate Begeman5e966612005-03-24 06:28:42 +00001579 .addImm(31-Tmp2);
1580 } else {
1581 Tmp2 = SelectExpr(N.getOperand(1));
1582 BuildMI(BB, PPC::SLW, 2, Result).addReg(Tmp1).addReg(Tmp2);
1583 }
1584 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001585
Nate Begeman5e966612005-03-24 06:28:42 +00001586 case ISD::SRL:
1587 Tmp1 = SelectExpr(N.getOperand(0));
1588 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1589 Tmp2 = CN->getValue() & 0x1F;
Nate Begeman33162522005-03-29 21:54:38 +00001590 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(32-Tmp2)
Nate Begeman5e966612005-03-24 06:28:42 +00001591 .addImm(Tmp2).addImm(31);
1592 } else {
1593 Tmp2 = SelectExpr(N.getOperand(1));
1594 BuildMI(BB, PPC::SRW, 2, Result).addReg(Tmp1).addReg(Tmp2);
1595 }
1596 return Result;
1597
1598 case ISD::SRA:
1599 Tmp1 = SelectExpr(N.getOperand(0));
1600 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1601 Tmp2 = CN->getValue() & 0x1F;
1602 BuildMI(BB, PPC::SRAWI, 2, Result).addReg(Tmp1).addImm(Tmp2);
1603 } else {
1604 Tmp2 = SelectExpr(N.getOperand(1));
1605 BuildMI(BB, PPC::SRAW, 2, Result).addReg(Tmp1).addReg(Tmp2);
1606 }
1607 return Result;
1608
Nate Begemana9795f82005-03-24 04:41:43 +00001609 case ISD::ADD:
1610 assert (DestType == MVT::i32 && "Only do arithmetic on i32s!");
1611 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman439b4442005-04-05 04:22:58 +00001612 switch(getImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
Nate Begemana9795f82005-03-24 04:41:43 +00001613 default: assert(0 && "unhandled result code");
1614 case 0: // No immediate
1615 Tmp2 = SelectExpr(N.getOperand(1));
1616 BuildMI(BB, PPC::ADD, 2, Result).addReg(Tmp1).addReg(Tmp2);
1617 break;
1618 case 1: // Low immediate
1619 BuildMI(BB, PPC::ADDI, 2, Result).addReg(Tmp1).addSImm(Tmp2);
1620 break;
1621 case 2: // Shifted immediate
1622 BuildMI(BB, PPC::ADDIS, 2, Result).addReg(Tmp1).addSImm(Tmp2);
1623 break;
1624 }
1625 return Result;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001626
Nate Begemana9795f82005-03-24 04:41:43 +00001627 case ISD::AND:
Nate Begeman7ddecb42005-04-06 23:51:40 +00001628 Tmp1 = SelectExpr(N.getOperand(0));
1629 // FIXME: should add check in getImmediateForOpcode to return a value
1630 // indicating the immediate is a run of set bits so we can emit a bitfield
1631 // clear with RLWINM instead.
1632 switch(getImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
1633 default: assert(0 && "unhandled result code");
1634 case 0: // No immediate
1635 Tmp2 = SelectExpr(N.getOperand(1));
1636 BuildMI(BB, PPC::AND, 2, Result).addReg(Tmp1).addReg(Tmp2);
1637 break;
1638 case 1: // Low immediate
1639 BuildMI(BB, PPC::ANDIo, 2, Result).addReg(Tmp1).addImm(Tmp2);
1640 break;
1641 case 2: // Shifted immediate
1642 BuildMI(BB, PPC::ANDISo, 2, Result).addReg(Tmp1).addImm(Tmp2);
1643 break;
1644 }
1645 return Result;
1646
Nate Begemana9795f82005-03-24 04:41:43 +00001647 case ISD::OR:
Nate Begeman7ddecb42005-04-06 23:51:40 +00001648 if (SelectBitfieldInsert(N, Result))
1649 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001650 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman439b4442005-04-05 04:22:58 +00001651 switch(getImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
Nate Begemana9795f82005-03-24 04:41:43 +00001652 default: assert(0 && "unhandled result code");
1653 case 0: // No immediate
1654 Tmp2 = SelectExpr(N.getOperand(1));
Nate Begeman7ddecb42005-04-06 23:51:40 +00001655 BuildMI(BB, PPC::OR, 2, Result).addReg(Tmp1).addReg(Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +00001656 break;
1657 case 1: // Low immediate
Nate Begeman7ddecb42005-04-06 23:51:40 +00001658 BuildMI(BB, PPC::ORI, 2, Result).addReg(Tmp1).addImm(Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +00001659 break;
1660 case 2: // Shifted immediate
Nate Begeman7ddecb42005-04-06 23:51:40 +00001661 BuildMI(BB, PPC::ORIS, 2, Result).addReg(Tmp1).addImm(Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +00001662 break;
1663 }
1664 return Result;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001665
Nate Begemanaa73a9f2005-04-03 11:20:20 +00001666 case ISD::XOR: {
1667 // Check for EQV: xor, (xor a, -1), b
1668 if (N.getOperand(0).getOpcode() == ISD::XOR &&
1669 N.getOperand(0).getOperand(1).getOpcode() == ISD::Constant &&
1670 cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->isAllOnesValue()) {
1671 ++NotLogic;
1672 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1673 Tmp2 = SelectExpr(N.getOperand(1));
1674 BuildMI(BB, PPC::EQV, 2, Result).addReg(Tmp1).addReg(Tmp2);
1675 return Result;
1676 }
1677 // Check for NOT, NOR, and NAND: xor (copy, or, and), -1
1678 if (N.getOperand(1).getOpcode() == ISD::Constant &&
1679 cast<ConstantSDNode>(N.getOperand(1))->isAllOnesValue()) {
1680 ++NotLogic;
1681 switch(N.getOperand(0).getOpcode()) {
1682 case ISD::OR:
1683 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1684 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1685 BuildMI(BB, PPC::NOR, 2, Result).addReg(Tmp1).addReg(Tmp2);
1686 break;
1687 case ISD::AND:
1688 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1689 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1690 BuildMI(BB, PPC::NAND, 2, Result).addReg(Tmp1).addReg(Tmp2);
1691 break;
1692 default:
1693 Tmp1 = SelectExpr(N.getOperand(0));
1694 BuildMI(BB, PPC::NOR, 2, Result).addReg(Tmp1).addReg(Tmp1);
1695 break;
1696 }
1697 return Result;
1698 }
1699 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman439b4442005-04-05 04:22:58 +00001700 switch(getImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
Nate Begemanaa73a9f2005-04-03 11:20:20 +00001701 default: assert(0 && "unhandled result code");
1702 case 0: // No immediate
1703 Tmp2 = SelectExpr(N.getOperand(1));
1704 BuildMI(BB, PPC::XOR, 2, Result).addReg(Tmp1).addReg(Tmp2);
1705 break;
1706 case 1: // Low immediate
1707 BuildMI(BB, PPC::XORI, 2, Result).addReg(Tmp1).addImm(Tmp2);
1708 break;
1709 case 2: // Shifted immediate
1710 BuildMI(BB, PPC::XORIS, 2, Result).addReg(Tmp1).addImm(Tmp2);
1711 break;
1712 }
1713 return Result;
1714 }
1715
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001716 case ISD::SUB:
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001717 Tmp2 = SelectExpr(N.getOperand(1));
Nate Begeman439b4442005-04-05 04:22:58 +00001718 if (1 == getImmediateForOpcode(N.getOperand(0), opcode, Tmp1))
Nate Begeman27523a12005-04-02 00:42:16 +00001719 BuildMI(BB, PPC::SUBFIC, 2, Result).addReg(Tmp2).addSImm(Tmp1);
1720 else {
1721 Tmp1 = SelectExpr(N.getOperand(0));
1722 BuildMI(BB, PPC::SUBF, 2, Result).addReg(Tmp2).addReg(Tmp1);
1723 }
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001724 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001725
Nate Begeman5e966612005-03-24 06:28:42 +00001726 case ISD::MUL:
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001727 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman439b4442005-04-05 04:22:58 +00001728 if (1 == getImmediateForOpcode(N.getOperand(1), opcode, Tmp2))
Nate Begeman307e7442005-03-26 01:28:53 +00001729 BuildMI(BB, PPC::MULLI, 2, Result).addReg(Tmp1).addSImm(Tmp2);
1730 else {
1731 Tmp2 = SelectExpr(N.getOperand(1));
1732 BuildMI(BB, PPC::MULLW, 2, Result).addReg(Tmp1).addReg(Tmp2);
1733 }
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001734 return Result;
1735
Nate Begeman815d6da2005-04-06 00:25:27 +00001736 case ISD::MULHS:
1737 case ISD::MULHU:
1738 Tmp1 = SelectExpr(N.getOperand(0));
1739 Tmp2 = SelectExpr(N.getOperand(1));
1740 Opc = (ISD::MULHU == opcode) ? PPC::MULHWU : PPC::MULHW;
1741 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1742 return Result;
1743
Nate Begemanf3d08f32005-03-29 00:03:27 +00001744 case ISD::SDIV:
1745 case ISD::UDIV:
Nate Begeman815d6da2005-04-06 00:25:27 +00001746 switch (getImmediateForOpcode(N.getOperand(1), opcode, Tmp3)) {
1747 default: break;
1748 // If this is an sdiv by a power of two, we can use an srawi/addze pair.
1749 case 3:
Nate Begeman80196b12005-04-05 00:15:08 +00001750 Tmp1 = MakeReg(MVT::i32);
1751 Tmp2 = SelectExpr(N.getOperand(0));
1752 BuildMI(BB, PPC::SRAWI, 2, Tmp1).addReg(Tmp2).addImm(Tmp3);
1753 BuildMI(BB, PPC::ADDZE, 1, Result).addReg(Tmp1);
1754 return Result;
Nate Begeman815d6da2005-04-06 00:25:27 +00001755 // If this is a divide by constant, we can emit code using some magic
1756 // constants to implement it as a multiply instead.
Nate Begeman27b4c232005-04-06 06:44:57 +00001757 case 4:
1758 ExprMap.erase(N);
1759 if (opcode == ISD::SDIV)
1760 return SelectExpr(BuildSDIVSequence(N));
1761 else
1762 return SelectExpr(BuildUDIVSequence(N));
Nate Begeman80196b12005-04-05 00:15:08 +00001763 }
Nate Begemanf3d08f32005-03-29 00:03:27 +00001764 Tmp1 = SelectExpr(N.getOperand(0));
1765 Tmp2 = SelectExpr(N.getOperand(1));
1766 Opc = (ISD::UDIV == opcode) ? PPC::DIVWU : PPC::DIVW;
1767 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1768 return Result;
1769
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001770 case ISD::ADD_PARTS:
Nate Begemanca12a2b2005-03-28 22:28:37 +00001771 case ISD::SUB_PARTS: {
1772 assert(N.getNumOperands() == 4 && N.getValueType() == MVT::i32 &&
1773 "Not an i64 add/sub!");
1774 // Emit all of the operands.
1775 std::vector<unsigned> InVals;
1776 for (unsigned i = 0, e = N.getNumOperands(); i != e; ++i)
1777 InVals.push_back(SelectExpr(N.getOperand(i)));
1778 if (N.getOpcode() == ISD::ADD_PARTS) {
Nate Begeman27eeb002005-04-02 05:59:34 +00001779 BuildMI(BB, PPC::ADDC, 2, Result).addReg(InVals[0]).addReg(InVals[2]);
1780 BuildMI(BB, PPC::ADDE, 2, Result+1).addReg(InVals[1]).addReg(InVals[3]);
Nate Begemanca12a2b2005-03-28 22:28:37 +00001781 } else {
Nate Begeman27eeb002005-04-02 05:59:34 +00001782 BuildMI(BB, PPC::SUBFC, 2, Result).addReg(InVals[2]).addReg(InVals[0]);
1783 BuildMI(BB, PPC::SUBFE, 2, Result+1).addReg(InVals[3]).addReg(InVals[1]);
1784 }
1785 return Result+N.ResNo;
1786 }
1787
1788 case ISD::SHL_PARTS:
1789 case ISD::SRA_PARTS:
1790 case ISD::SRL_PARTS: {
1791 assert(N.getNumOperands() == 3 && N.getValueType() == MVT::i32 &&
1792 "Not an i64 shift!");
1793 unsigned ShiftOpLo = SelectExpr(N.getOperand(0));
1794 unsigned ShiftOpHi = SelectExpr(N.getOperand(1));
1795 unsigned SHReg = SelectExpr(N.getOperand(2));
1796 Tmp1 = MakeReg(MVT::i32);
1797 Tmp2 = MakeReg(MVT::i32);
1798 Tmp3 = MakeReg(MVT::i32);
1799 unsigned Tmp4 = MakeReg(MVT::i32);
1800 unsigned Tmp5 = MakeReg(MVT::i32);
1801 unsigned Tmp6 = MakeReg(MVT::i32);
1802 BuildMI(BB, PPC::SUBFIC, 2, Tmp1).addReg(SHReg).addSImm(32);
1803 if (ISD::SHL_PARTS == opcode) {
1804 BuildMI(BB, PPC::SLW, 2, Tmp2).addReg(ShiftOpHi).addReg(SHReg);
1805 BuildMI(BB, PPC::SRW, 2, Tmp3).addReg(ShiftOpLo).addReg(Tmp1);
1806 BuildMI(BB, PPC::OR, 2, Tmp4).addReg(Tmp2).addReg(Tmp3);
1807 BuildMI(BB, PPC::ADDI, 2, Tmp5).addReg(SHReg).addSImm(-32);
Nate Begemanfa554702005-04-03 22:13:27 +00001808 BuildMI(BB, PPC::SLW, 2, Tmp6).addReg(ShiftOpLo).addReg(Tmp5);
Nate Begeman27eeb002005-04-02 05:59:34 +00001809 BuildMI(BB, PPC::OR, 2, Result+1).addReg(Tmp4).addReg(Tmp6);
1810 BuildMI(BB, PPC::SLW, 2, Result).addReg(ShiftOpLo).addReg(SHReg);
1811 } else if (ISD::SRL_PARTS == opcode) {
1812 BuildMI(BB, PPC::SRW, 2, Tmp2).addReg(ShiftOpLo).addReg(SHReg);
1813 BuildMI(BB, PPC::SLW, 2, Tmp3).addReg(ShiftOpHi).addReg(Tmp1);
1814 BuildMI(BB, PPC::OR, 2, Tmp4).addReg(Tmp2).addReg(Tmp3);
1815 BuildMI(BB, PPC::ADDI, 2, Tmp5).addReg(SHReg).addSImm(-32);
1816 BuildMI(BB, PPC::SRW, 2, Tmp6).addReg(ShiftOpHi).addReg(Tmp5);
1817 BuildMI(BB, PPC::OR, 2, Result).addReg(Tmp4).addReg(Tmp6);
1818 BuildMI(BB, PPC::SRW, 2, Result+1).addReg(ShiftOpHi).addReg(SHReg);
1819 } else {
1820 MachineBasicBlock *TmpMBB = new MachineBasicBlock(BB->getBasicBlock());
1821 MachineBasicBlock *PhiMBB = new MachineBasicBlock(BB->getBasicBlock());
1822 MachineBasicBlock *OldMBB = BB;
1823 MachineFunction *F = BB->getParent();
1824 ilist<MachineBasicBlock>::iterator It = BB; ++It;
1825 F->getBasicBlockList().insert(It, TmpMBB);
1826 F->getBasicBlockList().insert(It, PhiMBB);
1827 BB->addSuccessor(TmpMBB);
1828 BB->addSuccessor(PhiMBB);
1829 BuildMI(BB, PPC::SRW, 2, Tmp2).addReg(ShiftOpLo).addReg(SHReg);
1830 BuildMI(BB, PPC::SLW, 2, Tmp3).addReg(ShiftOpHi).addReg(Tmp1);
1831 BuildMI(BB, PPC::OR, 2, Tmp4).addReg(Tmp2).addReg(Tmp3);
1832 BuildMI(BB, PPC::ADDICo, 2, Tmp5).addReg(SHReg).addSImm(-32);
1833 BuildMI(BB, PPC::SRAW, 2, Tmp6).addReg(ShiftOpHi).addReg(Tmp5);
1834 BuildMI(BB, PPC::SRAW, 2, Result+1).addReg(ShiftOpHi).addReg(SHReg);
1835 BuildMI(BB, PPC::BLE, 2).addReg(PPC::CR0).addMBB(PhiMBB);
1836 // Select correct least significant half if the shift amount > 32
1837 BB = TmpMBB;
1838 unsigned Tmp7 = MakeReg(MVT::i32);
1839 BuildMI(BB, PPC::OR, 2, Tmp7).addReg(Tmp6).addReg(Tmp6);
1840 TmpMBB->addSuccessor(PhiMBB);
1841 BB = PhiMBB;
1842 BuildMI(BB, PPC::PHI, 4, Result).addReg(Tmp4).addMBB(OldMBB)
1843 .addReg(Tmp7).addMBB(TmpMBB);
Nate Begemanca12a2b2005-03-28 22:28:37 +00001844 }
1845 return Result+N.ResNo;
1846 }
1847
Nate Begemana9795f82005-03-24 04:41:43 +00001848 case ISD::FP_TO_UINT:
Nate Begeman6b559972005-04-01 02:59:27 +00001849 case ISD::FP_TO_SINT: {
1850 bool U = (ISD::FP_TO_UINT == opcode);
1851 Tmp1 = SelectExpr(N.getOperand(0));
1852 if (!U) {
1853 Tmp2 = MakeReg(MVT::f64);
1854 BuildMI(BB, PPC::FCTIWZ, 1, Tmp2).addReg(Tmp1);
1855 int FrameIdx = BB->getParent()->getFrameInfo()->CreateStackObject(8, 8);
1856 addFrameReference(BuildMI(BB, PPC::STFD, 3).addReg(Tmp2), FrameIdx);
1857 addFrameReference(BuildMI(BB, PPC::LWZ, 2, Result), FrameIdx, 4);
1858 return Result;
1859 } else {
1860 unsigned Zero = getConstDouble(0.0);
1861 unsigned MaxInt = getConstDouble((1LL << 32) - 1);
1862 unsigned Border = getConstDouble(1LL << 31);
1863 unsigned UseZero = MakeReg(MVT::f64);
1864 unsigned UseMaxInt = MakeReg(MVT::f64);
1865 unsigned UseChoice = MakeReg(MVT::f64);
1866 unsigned TmpReg = MakeReg(MVT::f64);
1867 unsigned TmpReg2 = MakeReg(MVT::f64);
1868 unsigned ConvReg = MakeReg(MVT::f64);
1869 unsigned IntTmp = MakeReg(MVT::i32);
1870 unsigned XorReg = MakeReg(MVT::i32);
1871 MachineFunction *F = BB->getParent();
1872 int FrameIdx = F->getFrameInfo()->CreateStackObject(8, 8);
1873 // Update machine-CFG edges
1874 MachineBasicBlock *XorMBB = new MachineBasicBlock(BB->getBasicBlock());
1875 MachineBasicBlock *PhiMBB = new MachineBasicBlock(BB->getBasicBlock());
1876 MachineBasicBlock *OldMBB = BB;
1877 ilist<MachineBasicBlock>::iterator It = BB; ++It;
1878 F->getBasicBlockList().insert(It, XorMBB);
1879 F->getBasicBlockList().insert(It, PhiMBB);
1880 BB->addSuccessor(XorMBB);
1881 BB->addSuccessor(PhiMBB);
1882 // Convert from floating point to unsigned 32-bit value
1883 // Use 0 if incoming value is < 0.0
1884 BuildMI(BB, PPC::FSEL, 3, UseZero).addReg(Tmp1).addReg(Tmp1).addReg(Zero);
1885 // Use 2**32 - 1 if incoming value is >= 2**32
1886 BuildMI(BB, PPC::FSUB, 2, UseMaxInt).addReg(MaxInt).addReg(Tmp1);
1887 BuildMI(BB, PPC::FSEL, 3, UseChoice).addReg(UseMaxInt).addReg(UseZero)
1888 .addReg(MaxInt);
1889 // Subtract 2**31
1890 BuildMI(BB, PPC::FSUB, 2, TmpReg).addReg(UseChoice).addReg(Border);
1891 // Use difference if >= 2**31
1892 BuildMI(BB, PPC::FCMPU, 2, PPC::CR0).addReg(UseChoice).addReg(Border);
1893 BuildMI(BB, PPC::FSEL, 3, TmpReg2).addReg(TmpReg).addReg(TmpReg)
1894 .addReg(UseChoice);
1895 // Convert to integer
1896 BuildMI(BB, PPC::FCTIWZ, 1, ConvReg).addReg(TmpReg2);
1897 addFrameReference(BuildMI(BB, PPC::STFD, 3).addReg(ConvReg), FrameIdx);
1898 addFrameReference(BuildMI(BB, PPC::LWZ, 2, IntTmp), FrameIdx, 4);
1899 BuildMI(BB, PPC::BLT, 2).addReg(PPC::CR0).addMBB(PhiMBB);
1900 BuildMI(BB, PPC::B, 1).addMBB(XorMBB);
1901
1902 // XorMBB:
1903 // add 2**31 if input was >= 2**31
1904 BB = XorMBB;
1905 BuildMI(BB, PPC::XORIS, 2, XorReg).addReg(IntTmp).addImm(0x8000);
1906 XorMBB->addSuccessor(PhiMBB);
1907
1908 // PhiMBB:
1909 // DestReg = phi [ IntTmp, OldMBB ], [ XorReg, XorMBB ]
1910 BB = PhiMBB;
1911 BuildMI(BB, PPC::PHI, 4, Result).addReg(IntTmp).addMBB(OldMBB)
1912 .addReg(XorReg).addMBB(XorMBB);
1913 return Result;
1914 }
1915 assert(0 && "Should never get here");
1916 return 0;
1917 }
Nate Begemana9795f82005-03-24 04:41:43 +00001918
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001919 case ISD::SETCC:
Nate Begeman33162522005-03-29 21:54:38 +00001920 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Node)) {
Nate Begeman7e7fadd2005-04-07 20:30:01 +00001921 // We can codegen setcc op, 0 very efficiently compared to a conditional
1922 // branch. Check for that here.
1923 if (ConstantSDNode *CN =
1924 dyn_cast<ConstantSDNode>(SetCC->getOperand(1).Val)) {
1925 if (CN->getValue() == 0) {
1926 Tmp1 = SelectExpr(SetCC->getOperand(0));
1927 switch (SetCC->getCondition()) {
1928 default: assert(0 && "Unhandled SetCC condition"); abort();
1929 case ISD::SETEQ:
1930 case ISD::SETULE:
1931 Tmp2 = MakeReg(MVT::i32);
1932 BuildMI(BB, PPC::CNTLZW, 1, Tmp2).addReg(Tmp1);
1933 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp2).addImm(27)
1934 .addImm(5).addImm(31);
1935 break;
1936 case ISD::SETNE:
1937 case ISD::SETUGT:
1938 Tmp2 = MakeReg(MVT::i32);
1939 BuildMI(BB, PPC::ADDIC, 2, Tmp2).addReg(Tmp1).addSImm(-1);
1940 BuildMI(BB, PPC::SUBFE, 2, Result).addReg(Tmp2).addReg(Tmp1);
1941 break;
1942 case ISD::SETULT:
1943 BuildMI(BB, PPC::LI, 1, Result).addSImm(0);
1944 break;
1945 case ISD::SETLT:
1946 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(1)
1947 .addImm(31).addImm(31);
1948 break;
1949 case ISD::SETLE:
1950 Tmp2 = MakeReg(MVT::i32);
1951 Tmp3 = MakeReg(MVT::i32);
1952 BuildMI(BB, PPC::NEG, 2, Tmp2).addReg(Tmp1);
1953 BuildMI(BB, PPC::ORC, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
1954 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp3).addImm(1)
1955 .addImm(31).addImm(31);
1956 break;
1957 case ISD::SETGT:
1958 Tmp2 = MakeReg(MVT::i32);
1959 Tmp3 = MakeReg(MVT::i32);
1960 BuildMI(BB, PPC::NEG, 2, Tmp2).addReg(Tmp1);
1961 BuildMI(BB, PPC::ANDC, 2, Tmp3).addReg(Tmp2).addReg(Tmp1);
1962 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp3).addImm(1)
1963 .addImm(31).addImm(31);
1964 break;
1965 case ISD::SETUGE:
1966 BuildMI(BB, PPC::LI, 1, Result).addSImm(1);
1967 break;
1968 case ISD::SETGE:
1969 BuildMI(BB, PPC::RLWINM, 4, Tmp2).addReg(Tmp1).addImm(1)
1970 .addImm(31).addImm(31);
1971 BuildMI(BB, PPC::XORI, 2, Result).addReg(Tmp2).addImm(1);
1972 break;
1973 }
1974 return Result;
1975 }
1976 }
1977
Nate Begemandffcfcc2005-04-01 00:32:34 +00001978 Opc = SelectSetCR0(N);
Nate Begeman31318e42005-04-01 07:21:30 +00001979 unsigned TrueValue = MakeReg(MVT::i32);
1980 BuildMI(BB, PPC::LI, 1, TrueValue).addSImm(1);
1981 unsigned FalseValue = MakeReg(MVT::i32);
1982 BuildMI(BB, PPC::LI, 1, FalseValue).addSImm(0);
1983
Nate Begeman33162522005-03-29 21:54:38 +00001984 // Create an iterator with which to insert the MBB for copying the false
1985 // value and the MBB to hold the PHI instruction for this SetCC.
1986 MachineBasicBlock *thisMBB = BB;
1987 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1988 ilist<MachineBasicBlock>::iterator It = BB;
1989 ++It;
1990
1991 // thisMBB:
1992 // ...
1993 // cmpTY cr0, r1, r2
1994 // %TrueValue = li 1
1995 // bCC sinkMBB
Nate Begeman33162522005-03-29 21:54:38 +00001996 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
1997 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
1998 BuildMI(BB, Opc, 2).addReg(PPC::CR0).addMBB(sinkMBB);
1999 MachineFunction *F = BB->getParent();
2000 F->getBasicBlockList().insert(It, copy0MBB);
2001 F->getBasicBlockList().insert(It, sinkMBB);
2002 // Update machine-CFG edges
2003 BB->addSuccessor(copy0MBB);
2004 BB->addSuccessor(sinkMBB);
2005
2006 // copy0MBB:
2007 // %FalseValue = li 0
2008 // fallthrough
2009 BB = copy0MBB;
Nate Begeman33162522005-03-29 21:54:38 +00002010 // Update machine-CFG edges
2011 BB->addSuccessor(sinkMBB);
2012
2013 // sinkMBB:
2014 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
2015 // ...
2016 BB = sinkMBB;
2017 BuildMI(BB, PPC::PHI, 4, Result).addReg(FalseValue)
2018 .addMBB(copy0MBB).addReg(TrueValue).addMBB(thisMBB);
2019 return Result;
2020 }
2021 assert(0 && "Is this legal?");
2022 return 0;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00002023
Nate Begeman74747862005-03-29 22:24:51 +00002024 case ISD::SELECT: {
Nate Begeman7e7fadd2005-04-07 20:30:01 +00002025 // We can codegen select (a < 0) ? b : 0 very efficiently compared to a
2026 // conditional branch. Check for that here.
2027 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(N.getOperand(0).Val)) {
2028 if (ConstantSDNode *CN =
2029 dyn_cast<ConstantSDNode>(SetCC->getOperand(1).Val)) {
2030 if (ConstantSDNode *CNF =
2031 dyn_cast<ConstantSDNode>(N.getOperand(2).Val)) {
2032 if (CN->getValue() == 0 && CNF->getValue() == 0 &&
2033 SetCC->getCondition() == ISD::SETLT) {
2034 Tmp1 = SelectExpr(N.getOperand(1)); // TRUE value
2035 Tmp2 = SelectExpr(SetCC->getOperand(0));
2036 Tmp3 = MakeReg(MVT::i32);
2037 BuildMI(BB, PPC::SRAWI, 2, Tmp3).addReg(Tmp2).addImm(31);
2038 BuildMI(BB, PPC::AND, 2, Result).addReg(Tmp1).addReg(Tmp3);
2039 return Result;
2040 }
2041 }
2042 }
2043 }
Chris Lattner30710192005-04-01 07:10:02 +00002044 unsigned TrueValue = SelectExpr(N.getOperand(1)); //Use if TRUE
2045 unsigned FalseValue = SelectExpr(N.getOperand(2)); //Use if FALSE
Nate Begeman6cb2e1b2005-04-01 08:57:43 +00002046 Opc = SelectSetCR0(N.getOperand(0));
Chris Lattner30710192005-04-01 07:10:02 +00002047
Nate Begeman74747862005-03-29 22:24:51 +00002048 // Create an iterator with which to insert the MBB for copying the false
2049 // value and the MBB to hold the PHI instruction for this SetCC.
2050 MachineBasicBlock *thisMBB = BB;
2051 const BasicBlock *LLVM_BB = BB->getBasicBlock();
2052 ilist<MachineBasicBlock>::iterator It = BB;
2053 ++It;
2054
2055 // thisMBB:
2056 // ...
2057 // TrueVal = ...
2058 // cmpTY cr0, r1, r2
2059 // bCC copy1MBB
2060 // fallthrough --> copy0MBB
Nate Begeman74747862005-03-29 22:24:51 +00002061 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
2062 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
Nate Begeman3e897162005-03-31 23:55:40 +00002063 BuildMI(BB, Opc, 2).addReg(PPC::CR0).addMBB(sinkMBB);
Nate Begeman74747862005-03-29 22:24:51 +00002064 MachineFunction *F = BB->getParent();
2065 F->getBasicBlockList().insert(It, copy0MBB);
2066 F->getBasicBlockList().insert(It, sinkMBB);
2067 // Update machine-CFG edges
2068 BB->addSuccessor(copy0MBB);
2069 BB->addSuccessor(sinkMBB);
2070
2071 // copy0MBB:
2072 // %FalseValue = ...
2073 // # fallthrough to sinkMBB
2074 BB = copy0MBB;
Nate Begeman74747862005-03-29 22:24:51 +00002075 // Update machine-CFG edges
2076 BB->addSuccessor(sinkMBB);
2077
2078 // sinkMBB:
2079 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
2080 // ...
2081 BB = sinkMBB;
2082 BuildMI(BB, PPC::PHI, 4, Result).addReg(FalseValue)
2083 .addMBB(copy0MBB).addReg(TrueValue).addMBB(thisMBB);
Nate Begeman74747862005-03-29 22:24:51 +00002084 return Result;
2085 }
Nate Begemana9795f82005-03-24 04:41:43 +00002086
2087 case ISD::Constant:
2088 switch (N.getValueType()) {
2089 default: assert(0 && "Cannot use constants of this type!");
2090 case MVT::i1:
2091 BuildMI(BB, PPC::LI, 1, Result)
2092 .addSImm(!cast<ConstantSDNode>(N)->isNullValue());
2093 break;
2094 case MVT::i32:
2095 {
2096 int v = (int)cast<ConstantSDNode>(N)->getSignExtended();
2097 if (v < 32768 && v >= -32768) {
2098 BuildMI(BB, PPC::LI, 1, Result).addSImm(v);
2099 } else {
Nate Begeman5e966612005-03-24 06:28:42 +00002100 Tmp1 = MakeReg(MVT::i32);
2101 BuildMI(BB, PPC::LIS, 1, Tmp1).addSImm(v >> 16);
2102 BuildMI(BB, PPC::ORI, 2, Result).addReg(Tmp1).addImm(v & 0xFFFF);
Nate Begemana9795f82005-03-24 04:41:43 +00002103 }
2104 }
2105 }
2106 return Result;
2107 }
2108
2109 return 0;
2110}
2111
2112void ISel::Select(SDOperand N) {
2113 unsigned Tmp1, Tmp2, Opc;
2114 unsigned opcode = N.getOpcode();
2115
2116 if (!ExprMap.insert(std::make_pair(N, 1)).second)
2117 return; // Already selected.
2118
2119 SDNode *Node = N.Val;
2120
2121 switch (Node->getOpcode()) {
2122 default:
2123 Node->dump(); std::cerr << "\n";
2124 assert(0 && "Node not handled yet!");
2125 case ISD::EntryToken: return; // Noop
2126 case ISD::TokenFactor:
2127 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
2128 Select(Node->getOperand(i));
2129 return;
2130 case ISD::ADJCALLSTACKDOWN:
2131 case ISD::ADJCALLSTACKUP:
2132 Select(N.getOperand(0));
2133 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
2134 Opc = N.getOpcode() == ISD::ADJCALLSTACKDOWN ? PPC::ADJCALLSTACKDOWN :
2135 PPC::ADJCALLSTACKUP;
2136 BuildMI(BB, Opc, 1).addImm(Tmp1);
2137 return;
2138 case ISD::BR: {
2139 MachineBasicBlock *Dest =
2140 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
Nate Begemana9795f82005-03-24 04:41:43 +00002141 Select(N.getOperand(0));
2142 BuildMI(BB, PPC::B, 1).addMBB(Dest);
2143 return;
2144 }
2145 case ISD::BRCOND:
2146 SelectBranchCC(N);
2147 return;
2148 case ISD::CopyToReg:
2149 Select(N.getOperand(0));
2150 Tmp1 = SelectExpr(N.getOperand(1));
2151 Tmp2 = cast<RegSDNode>(N)->getReg();
2152
2153 if (Tmp1 != Tmp2) {
2154 if (N.getOperand(1).getValueType() == MVT::f64 ||
2155 N.getOperand(1).getValueType() == MVT::f32)
2156 BuildMI(BB, PPC::FMR, 1, Tmp2).addReg(Tmp1);
2157 else
2158 BuildMI(BB, PPC::OR, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
2159 }
2160 return;
2161 case ISD::ImplicitDef:
2162 Select(N.getOperand(0));
2163 BuildMI(BB, PPC::IMPLICIT_DEF, 0, cast<RegSDNode>(N)->getReg());
2164 return;
2165 case ISD::RET:
2166 switch (N.getNumOperands()) {
2167 default:
2168 assert(0 && "Unknown return instruction!");
2169 case 3:
2170 assert(N.getOperand(1).getValueType() == MVT::i32 &&
2171 N.getOperand(2).getValueType() == MVT::i32 &&
2172 "Unknown two-register value!");
2173 Select(N.getOperand(0));
2174 Tmp1 = SelectExpr(N.getOperand(1));
2175 Tmp2 = SelectExpr(N.getOperand(2));
Nate Begeman27523a12005-04-02 00:42:16 +00002176 BuildMI(BB, PPC::OR, 2, PPC::R3).addReg(Tmp2).addReg(Tmp2);
2177 BuildMI(BB, PPC::OR, 2, PPC::R4).addReg(Tmp1).addReg(Tmp1);
Nate Begemana9795f82005-03-24 04:41:43 +00002178 break;
2179 case 2:
2180 Select(N.getOperand(0));
2181 Tmp1 = SelectExpr(N.getOperand(1));
2182 switch (N.getOperand(1).getValueType()) {
2183 default:
2184 assert(0 && "Unknown return type!");
2185 case MVT::f64:
2186 case MVT::f32:
2187 BuildMI(BB, PPC::FMR, 1, PPC::F1).addReg(Tmp1);
2188 break;
2189 case MVT::i32:
2190 BuildMI(BB, PPC::OR, 2, PPC::R3).addReg(Tmp1).addReg(Tmp1);
2191 break;
2192 }
Nate Begeman9e3e1b52005-03-24 23:35:30 +00002193 case 1:
2194 Select(N.getOperand(0));
2195 break;
Nate Begemana9795f82005-03-24 04:41:43 +00002196 }
2197 BuildMI(BB, PPC::BLR, 0); // Just emit a 'ret' instruction
2198 return;
Nate Begemana9795f82005-03-24 04:41:43 +00002199 case ISD::TRUNCSTORE:
2200 case ISD::STORE:
2201 {
2202 SDOperand Chain = N.getOperand(0);
2203 SDOperand Value = N.getOperand(1);
2204 SDOperand Address = N.getOperand(2);
2205 Select(Chain);
2206
2207 Tmp1 = SelectExpr(Value); //value
2208
2209 if (opcode == ISD::STORE) {
2210 switch(Value.getValueType()) {
2211 default: assert(0 && "unknown Type in store");
2212 case MVT::i32: Opc = PPC::STW; break;
2213 case MVT::f64: Opc = PPC::STFD; break;
2214 case MVT::f32: Opc = PPC::STFS; break;
2215 }
2216 } else { //ISD::TRUNCSTORE
2217 switch(cast<MVTSDNode>(Node)->getExtraValueType()) {
2218 default: assert(0 && "unknown Type in store");
Nate Begeman7e7fadd2005-04-07 20:30:01 +00002219 case MVT::i1:
Nate Begemana9795f82005-03-24 04:41:43 +00002220 case MVT::i8: Opc = PPC::STB; break;
2221 case MVT::i16: Opc = PPC::STH; break;
2222 }
2223 }
2224
Nate Begemana7e11a42005-04-01 05:57:17 +00002225 if(Address.getOpcode() == ISD::FrameIndex)
Nate Begemana9795f82005-03-24 04:41:43 +00002226 {
Nate Begeman58f718c2005-03-30 02:23:08 +00002227 Tmp2 = cast<FrameIndexSDNode>(Address)->getIndex();
2228 addFrameReference(BuildMI(BB, Opc, 3).addReg(Tmp1), (int)Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +00002229 }
2230 else
2231 {
2232 int offset;
Nate Begeman04730362005-04-01 04:45:11 +00002233 bool idx = SelectAddr(Address, Tmp2, offset);
2234 if (idx) {
2235 Opc = IndexedOpForOp(Opc);
2236 BuildMI(BB, Opc, 3).addReg(Tmp1).addReg(Tmp2).addReg(offset);
2237 } else {
2238 BuildMI(BB, Opc, 3).addReg(Tmp1).addImm(offset).addReg(Tmp2);
2239 }
Nate Begemana9795f82005-03-24 04:41:43 +00002240 }
2241 return;
2242 }
2243 case ISD::EXTLOAD:
2244 case ISD::SEXTLOAD:
2245 case ISD::ZEXTLOAD:
2246 case ISD::LOAD:
2247 case ISD::CopyFromReg:
2248 case ISD::CALL:
2249 case ISD::DYNAMIC_STACKALLOC:
2250 ExprMap.erase(N);
2251 SelectExpr(N);
2252 return;
2253 }
2254 assert(0 && "Should not be reached!");
2255}
2256
2257
2258/// createPPC32PatternInstructionSelector - This pass converts an LLVM function
2259/// into a machine code representation using pattern matching and a machine
2260/// description file.
2261///
2262FunctionPass *llvm::createPPC32ISelPattern(TargetMachine &TM) {
2263 return new ISel(TM);
Chris Lattner246fa632005-03-24 06:16:18 +00002264}
2265