blob: ca2acc688dea1c785ed827043bf7fbdc0d507903 [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
Nate Begeman3e897162005-03-31 23:55:40 +000065 addLegalFPImmediate(+0.0); // Necessary for FSEL
66 addLegalFPImmediate(-0.0); //
67
Nate Begemana9795f82005-03-24 04:41:43 +000068 computeRegisterProperties();
69 }
70
71 /// LowerArguments - This hook must be implemented to indicate how we should
72 /// lower the arguments for the specified function, into the specified DAG.
73 virtual std::vector<SDOperand>
74 LowerArguments(Function &F, SelectionDAG &DAG);
75
76 /// LowerCallTo - This hook lowers an abstract call to a function into an
77 /// actual call.
78 virtual std::pair<SDOperand, SDOperand>
Nate Begeman307e7442005-03-26 01:28:53 +000079 LowerCallTo(SDOperand Chain, const Type *RetTy, bool isVarArg,
80 SDOperand Callee, ArgListTy &Args, SelectionDAG &DAG);
Nate Begemana9795f82005-03-24 04:41:43 +000081
82 virtual std::pair<SDOperand, SDOperand>
83 LowerVAStart(SDOperand Chain, SelectionDAG &DAG);
84
85 virtual std::pair<SDOperand,SDOperand>
86 LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
87 const Type *ArgTy, SelectionDAG &DAG);
88
89 virtual std::pair<SDOperand, SDOperand>
90 LowerFrameReturnAddress(bool isFrameAddr, SDOperand Chain, unsigned Depth,
91 SelectionDAG &DAG);
92 };
93}
94
95
96std::vector<SDOperand>
97PPC32TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
98 //
99 // add beautiful description of PPC stack frame format, or at least some docs
100 //
101 MachineFunction &MF = DAG.getMachineFunction();
102 MachineFrameInfo *MFI = MF.getFrameInfo();
103 MachineBasicBlock& BB = MF.front();
104 std::vector<SDOperand> ArgValues;
105
106 // Due to the rather complicated nature of the PowerPC ABI, rather than a
107 // fixed size array of physical args, for the sake of simplicity let the STL
108 // handle tracking them for us.
109 std::vector<unsigned> argVR, argPR, argOp;
110 unsigned ArgOffset = 24;
111 unsigned GPR_remaining = 8;
112 unsigned FPR_remaining = 13;
113 unsigned GPR_idx = 0, FPR_idx = 0;
114 static const unsigned GPR[] = {
115 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
116 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
117 };
118 static const unsigned FPR[] = {
119 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
120 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
121 };
122
123 // Add DAG nodes to load the arguments... On entry to a function on PPC,
124 // the arguments start at offset 24, although they are likely to be passed
125 // in registers.
126 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
127 SDOperand newroot, argt;
128 unsigned ObjSize;
129 bool needsLoad = false;
130 MVT::ValueType ObjectVT = getValueType(I->getType());
131
132 switch (ObjectVT) {
133 default: assert(0 && "Unhandled argument type!");
134 case MVT::i1:
135 case MVT::i8:
136 case MVT::i16:
137 case MVT::i32:
138 ObjSize = 4;
139 if (GPR_remaining > 0) {
140 BuildMI(&BB, PPC::IMPLICIT_DEF, 0, GPR[GPR_idx]);
Nate Begemanf70b5762005-03-28 23:08:54 +0000141 argt = newroot = DAG.getCopyFromReg(GPR[GPR_idx], MVT::i32,
142 DAG.getRoot());
Nate Begemana9795f82005-03-24 04:41:43 +0000143 if (ObjectVT != MVT::i32)
144 argt = DAG.getNode(ISD::TRUNCATE, ObjectVT, newroot);
Nate Begemana9795f82005-03-24 04:41:43 +0000145 } else {
146 needsLoad = true;
147 }
148 break;
Nate Begemanf7e43382005-03-26 07:46:36 +0000149 case MVT::i64: ObjSize = 8;
150 // FIXME: can split 64b load between reg/mem if it is last arg in regs
Nate Begemana9795f82005-03-24 04:41:43 +0000151 if (GPR_remaining > 1) {
152 BuildMI(&BB, PPC::IMPLICIT_DEF, 0, GPR[GPR_idx]);
153 BuildMI(&BB, PPC::IMPLICIT_DEF, 0, GPR[GPR_idx+1]);
Nate Begemanca12a2b2005-03-28 22:28:37 +0000154 // Copy the extracted halves into the virtual registers
Nate Begemanf70b5762005-03-28 23:08:54 +0000155 SDOperand argHi = DAG.getCopyFromReg(GPR[GPR_idx], MVT::i32,
156 DAG.getRoot());
157 SDOperand argLo = DAG.getCopyFromReg(GPR[GPR_idx+1], MVT::i32, argHi);
Nate Begemanca12a2b2005-03-28 22:28:37 +0000158 // Build the outgoing arg thingy
Nate Begemanf70b5762005-03-28 23:08:54 +0000159 argt = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, argLo, argHi);
160 newroot = argLo;
Nate Begemana9795f82005-03-24 04:41:43 +0000161 } else {
162 needsLoad = true;
163 }
164 break;
165 case MVT::f32: ObjSize = 4;
166 case MVT::f64: ObjSize = 8;
167 if (FPR_remaining > 0) {
168 BuildMI(&BB, PPC::IMPLICIT_DEF, 0, FPR[FPR_idx]);
Nate Begemanf70b5762005-03-28 23:08:54 +0000169 argt = newroot = DAG.getCopyFromReg(FPR[FPR_idx], ObjectVT,
170 DAG.getRoot());
Nate Begemana9795f82005-03-24 04:41:43 +0000171 --FPR_remaining;
172 ++FPR_idx;
173 } else {
174 needsLoad = true;
175 }
176 break;
177 }
178
179 // We need to load the argument to a virtual register if we determined above
180 // that we ran out of physical registers of the appropriate type
181 if (needsLoad) {
Nate Begemane5846682005-04-04 06:52:38 +0000182 unsigned SubregOffset = 0;
Nate Begemanc3e2db42005-04-04 09:09:00 +0000183 if (ObjectVT == MVT::i8 || ObjectVT == MVT::i1) SubregOffset = 3;
Nate Begemane5846682005-04-04 06:52:38 +0000184 if (ObjectVT == MVT::i16) SubregOffset = 2;
Nate Begemana9795f82005-03-24 04:41:43 +0000185 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
186 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
Nate Begemane5846682005-04-04 06:52:38 +0000187 FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN,
188 DAG.getConstant(SubregOffset, MVT::i32));
Nate Begemana9795f82005-03-24 04:41:43 +0000189 argt = newroot = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN);
190 }
191
192 // Every 4 bytes of argument space consumes one of the GPRs available for
193 // argument passing.
194 if (GPR_remaining > 0) {
195 unsigned delta = (GPR_remaining > 1 && ObjSize == 8) ? 2 : 1;
196 GPR_remaining -= delta;
197 GPR_idx += delta;
198 }
199 ArgOffset += ObjSize;
200
201 DAG.setRoot(newroot.getValue(1));
202 ArgValues.push_back(argt);
203 }
204
Nate Begemana9795f82005-03-24 04:41:43 +0000205 // If the function takes variable number of arguments, make a frame index for
206 // the start of the first vararg value... for expansion of llvm.va_start.
Nate Begemanfa554702005-04-03 22:13:27 +0000207 if (F.isVarArg()) {
Nate Begemana9795f82005-03-24 04:41:43 +0000208 VarArgsFrameIndex = MFI->CreateFixedObject(4, ArgOffset);
Nate Begemanfa554702005-04-03 22:13:27 +0000209 SDOperand FIN = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
Nate Begeman6644d4c2005-04-03 23:11:17 +0000210 // If this function is vararg, store any remaining integer argument regs
211 // to their spots on the stack so that they may be loaded by deferencing the
212 // result of va_next.
213 std::vector<SDOperand> MemOps;
214 for (; GPR_remaining > 0; --GPR_remaining, ++GPR_idx) {
215 BuildMI(&BB, PPC::IMPLICIT_DEF, 0, GPR[GPR_idx]);
216 SDOperand Val = DAG.getCopyFromReg(GPR[GPR_idx], MVT::i32, DAG.getRoot());
217 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Val.getValue(1),
218 Val, FIN);
219 MemOps.push_back(Store);
220 // Increment the address by four for the next argument to store
221 SDOperand PtrOff = DAG.getConstant(4, getPointerTy());
222 FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN, PtrOff);
223 }
224 DAG.setRoot(DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps));
Nate Begemanfa554702005-04-03 22:13:27 +0000225 }
Nate Begemana9795f82005-03-24 04:41:43 +0000226
227 return ArgValues;
228}
229
230std::pair<SDOperand, SDOperand>
231PPC32TargetLowering::LowerCallTo(SDOperand Chain,
Nate Begeman307e7442005-03-26 01:28:53 +0000232 const Type *RetTy, bool isVarArg,
233 SDOperand Callee, ArgListTy &Args, SelectionDAG &DAG) {
234 // args_to_use will accumulate outgoing args for the ISD::CALL case in
235 // SelectExpr to use to put the arguments in the appropriate registers.
Nate Begemana9795f82005-03-24 04:41:43 +0000236 std::vector<SDOperand> args_to_use;
Nate Begeman307e7442005-03-26 01:28:53 +0000237
238 // Count how many bytes are to be pushed on the stack, including the linkage
239 // area, and parameter passing area.
240 unsigned NumBytes = 24;
241
242 if (Args.empty()) {
Nate Begemana7e11a42005-04-01 05:57:17 +0000243 Chain = DAG.getNode(ISD::ADJCALLSTACKDOWN, MVT::Other, Chain,
244 DAG.getConstant(NumBytes, getPointerTy()));
Nate Begeman307e7442005-03-26 01:28:53 +0000245 } else {
246 for (unsigned i = 0, e = Args.size(); i != e; ++i)
247 switch (getValueType(Args[i].second)) {
248 default: assert(0 && "Unknown value type!");
249 case MVT::i1:
250 case MVT::i8:
251 case MVT::i16:
252 case MVT::i32:
253 case MVT::f32:
254 NumBytes += 4;
255 break;
256 case MVT::i64:
257 case MVT::f64:
258 NumBytes += 8;
259 break;
260 }
261
262 // Just to be safe, we'll always reserve the full 24 bytes of linkage area
263 // plus 32 bytes of argument space in case any called code gets funky on us.
264 if (NumBytes < 56) NumBytes = 56;
265
266 // Adjust the stack pointer for the new arguments...
267 // These operations are automatically eliminated by the prolog/epilog pass
268 Chain = DAG.getNode(ISD::ADJCALLSTACKDOWN, MVT::Other, Chain,
269 DAG.getConstant(NumBytes, getPointerTy()));
270
271 // Set up a copy of the stack pointer for use loading and storing any
272 // arguments that may not fit in the registers available for argument
273 // passing.
274 SDOperand StackPtr = DAG.getCopyFromReg(PPC::R1, MVT::i32,
275 DAG.getEntryNode());
276
277 // Figure out which arguments are going to go in registers, and which in
278 // memory. Also, if this is a vararg function, floating point operations
279 // must be stored to our stack, and loaded into integer regs as well, if
280 // any integer regs are available for argument passing.
281 unsigned ArgOffset = 24;
282 unsigned GPR_remaining = 8;
283 unsigned FPR_remaining = 13;
Nate Begeman74d73452005-03-31 00:15:26 +0000284
285 std::vector<SDOperand> MemOps;
Nate Begeman307e7442005-03-26 01:28:53 +0000286 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
287 // PtrOff will be used to store the current argument to the stack if a
288 // register cannot be found for it.
289 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
290 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
Nate Begemanf7e43382005-03-26 07:46:36 +0000291 MVT::ValueType ArgVT = getValueType(Args[i].second);
Nate Begeman307e7442005-03-26 01:28:53 +0000292
Nate Begemanf7e43382005-03-26 07:46:36 +0000293 switch (ArgVT) {
Nate Begeman307e7442005-03-26 01:28:53 +0000294 default: assert(0 && "Unexpected ValueType for argument!");
295 case MVT::i1:
296 case MVT::i8:
297 case MVT::i16:
298 // Promote the integer to 32 bits. If the input type is signed use a
299 // sign extend, otherwise use a zero extend.
300 if (Args[i].second->isSigned())
301 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
302 else
303 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
304 // FALL THROUGH
305 case MVT::i32:
306 if (GPR_remaining > 0) {
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000307 args_to_use.push_back(Args[i].first);
Nate Begeman307e7442005-03-26 01:28:53 +0000308 --GPR_remaining;
309 } else {
Nate Begeman74d73452005-03-31 00:15:26 +0000310 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
311 Args[i].first, PtrOff));
Nate Begeman307e7442005-03-26 01:28:53 +0000312 }
313 ArgOffset += 4;
314 break;
315 case MVT::i64:
Nate Begemanf7e43382005-03-26 07:46:36 +0000316 // If we have one free GPR left, we can place the upper half of the i64
317 // in it, and store the other half to the stack. If we have two or more
318 // free GPRs, then we can pass both halves of the i64 in registers.
319 if (GPR_remaining > 0) {
Nate Begemanf2622612005-03-26 02:17:46 +0000320 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
321 Args[i].first, DAG.getConstant(1, MVT::i32));
322 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
323 Args[i].first, DAG.getConstant(0, MVT::i32));
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000324 args_to_use.push_back(Hi);
Nate Begeman74d73452005-03-31 00:15:26 +0000325 --GPR_remaining;
Nate Begeman74d73452005-03-31 00:15:26 +0000326 if (GPR_remaining > 0) {
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000327 args_to_use.push_back(Lo);
Nate Begeman74d73452005-03-31 00:15:26 +0000328 --GPR_remaining;
Nate Begemanf7e43382005-03-26 07:46:36 +0000329 } else {
330 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
331 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
Nate Begeman74d73452005-03-31 00:15:26 +0000332 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
333 Lo, PtrOff));
Nate Begemanf7e43382005-03-26 07:46:36 +0000334 }
Nate Begeman307e7442005-03-26 01:28:53 +0000335 } else {
Nate Begeman74d73452005-03-31 00:15:26 +0000336 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
337 Args[i].first, PtrOff));
Nate Begeman307e7442005-03-26 01:28:53 +0000338 }
339 ArgOffset += 8;
340 break;
341 case MVT::f32:
Nate Begeman307e7442005-03-26 01:28:53 +0000342 case MVT::f64:
Nate Begemanf7e43382005-03-26 07:46:36 +0000343 if (FPR_remaining > 0) {
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000344 args_to_use.push_back(Args[i].first);
345 --FPR_remaining;
Nate Begemanf7e43382005-03-26 07:46:36 +0000346 if (isVarArg) {
Nate Begeman96fc6812005-03-31 02:05:53 +0000347 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Chain,
348 Args[i].first, PtrOff);
349 MemOps.push_back(Store);
Nate Begeman74d73452005-03-31 00:15:26 +0000350 // Float varargs are always shadowed in available integer registers
351 if (GPR_remaining > 0) {
Nate Begeman96fc6812005-03-31 02:05:53 +0000352 SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff);
Nate Begeman74d73452005-03-31 00:15:26 +0000353 MemOps.push_back(Load);
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000354 args_to_use.push_back(Load);
355 --GPR_remaining;
Nate Begeman74d73452005-03-31 00:15:26 +0000356 }
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000357 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
Nate Begeman74d73452005-03-31 00:15:26 +0000358 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
359 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
Nate Begeman96fc6812005-03-31 02:05:53 +0000360 SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff);
Nate Begeman74d73452005-03-31 00:15:26 +0000361 MemOps.push_back(Load);
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000362 args_to_use.push_back(Load);
363 --GPR_remaining;
Nate Begeman74d73452005-03-31 00:15:26 +0000364 }
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000365 } else {
366 // If we have any FPRs remaining, we may also have GPRs remaining.
367 // Args passed in FPRs consume either 1 (f32) or 2 (f64) available
368 // GPRs.
369 if (GPR_remaining > 0) {
370 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
371 --GPR_remaining;
372 }
373 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
374 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
375 --GPR_remaining;
376 }
Nate Begeman74d73452005-03-31 00:15:26 +0000377 }
Nate Begeman307e7442005-03-26 01:28:53 +0000378 } else {
Nate Begeman74d73452005-03-31 00:15:26 +0000379 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
380 Args[i].first, PtrOff));
Nate Begeman307e7442005-03-26 01:28:53 +0000381 }
Nate Begemanf7e43382005-03-26 07:46:36 +0000382 ArgOffset += (ArgVT == MVT::f32) ? 4 : 8;
Nate Begeman307e7442005-03-26 01:28:53 +0000383 break;
384 }
Nate Begemana9795f82005-03-24 04:41:43 +0000385 }
Nate Begeman74d73452005-03-31 00:15:26 +0000386 if (!MemOps.empty())
387 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps);
Nate Begemana9795f82005-03-24 04:41:43 +0000388 }
389
390 std::vector<MVT::ValueType> RetVals;
391 MVT::ValueType RetTyVT = getValueType(RetTy);
392 if (RetTyVT != MVT::isVoid)
393 RetVals.push_back(RetTyVT);
394 RetVals.push_back(MVT::Other);
395
396 SDOperand TheCall = SDOperand(DAG.getCall(RetVals,
397 Chain, Callee, args_to_use), 0);
398 Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
399 Chain = DAG.getNode(ISD::ADJCALLSTACKUP, MVT::Other, Chain,
400 DAG.getConstant(NumBytes, getPointerTy()));
401 return std::make_pair(TheCall, Chain);
402}
403
404std::pair<SDOperand, SDOperand>
405PPC32TargetLowering::LowerVAStart(SDOperand Chain, SelectionDAG &DAG) {
406 //vastart just returns the address of the VarArgsFrameIndex slot.
407 return std::make_pair(DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32), Chain);
408}
409
410std::pair<SDOperand,SDOperand> PPC32TargetLowering::
411LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
412 const Type *ArgTy, SelectionDAG &DAG) {
Nate Begemanc7b09f12005-03-25 08:34:25 +0000413 MVT::ValueType ArgVT = getValueType(ArgTy);
414 SDOperand Result;
415 if (!isVANext) {
416 Result = DAG.getLoad(ArgVT, DAG.getEntryNode(), VAList);
417 } else {
418 unsigned Amt;
419 if (ArgVT == MVT::i32 || ArgVT == MVT::f32)
420 Amt = 4;
421 else {
422 assert((ArgVT == MVT::i64 || ArgVT == MVT::f64) &&
423 "Other types should have been promoted for varargs!");
424 Amt = 8;
425 }
426 Result = DAG.getNode(ISD::ADD, VAList.getValueType(), VAList,
427 DAG.getConstant(Amt, VAList.getValueType()));
428 }
429 return std::make_pair(Result, Chain);
Nate Begemana9795f82005-03-24 04:41:43 +0000430}
431
432
433std::pair<SDOperand, SDOperand> PPC32TargetLowering::
434LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
435 SelectionDAG &DAG) {
Nate Begeman01d05262005-03-30 01:45:43 +0000436 assert(0 && "LowerFrameReturnAddress unimplemented");
Nate Begemana9795f82005-03-24 04:41:43 +0000437 abort();
438}
439
440namespace {
Nate Begemanaa73a9f2005-04-03 11:20:20 +0000441Statistic<>NotLogic("ppc-codegen", "Number of inverted logical ops");
Nate Begeman93075ec2005-04-04 23:40:36 +0000442Statistic<>FusedFP("ppc-codegen", "Number of fused fp operations");
Nate Begemana9795f82005-03-24 04:41:43 +0000443//===--------------------------------------------------------------------===//
444/// ISel - PPC32 specific code to select PPC32 machine instructions for
445/// SelectionDAG operations.
446//===--------------------------------------------------------------------===//
447class ISel : public SelectionDAGISel {
Nate Begemana9795f82005-03-24 04:41:43 +0000448 PPC32TargetLowering PPC32Lowering;
Nate Begeman815d6da2005-04-06 00:25:27 +0000449 SelectionDAG *ISelDAG; // Hack to support us having a dag->dag transform
450 // for sdiv and udiv until it is put into the future
451 // dag combiner.
Nate Begemana9795f82005-03-24 04:41:43 +0000452
453 /// ExprMap - As shared expressions are codegen'd, we keep track of which
454 /// vreg the value is produced in, so we only emit one copy of each compiled
455 /// tree.
456 std::map<SDOperand, unsigned> ExprMap;
Nate Begemanc7b09f12005-03-25 08:34:25 +0000457
458 unsigned GlobalBaseReg;
459 bool GlobalBaseInitialized;
Nate Begemana9795f82005-03-24 04:41:43 +0000460
461public:
Nate Begeman815d6da2005-04-06 00:25:27 +0000462 ISel(TargetMachine &TM) : SelectionDAGISel(PPC32Lowering), PPC32Lowering(TM),
463 ISelDAG(0) {}
Nate Begemana9795f82005-03-24 04:41:43 +0000464
Nate Begemanc7b09f12005-03-25 08:34:25 +0000465 /// runOnFunction - Override this function in order to reset our per-function
466 /// variables.
467 virtual bool runOnFunction(Function &Fn) {
468 // Make sure we re-emit a set of the global base reg if necessary
469 GlobalBaseInitialized = false;
470 return SelectionDAGISel::runOnFunction(Fn);
471 }
472
Nate Begemana9795f82005-03-24 04:41:43 +0000473 /// InstructionSelectBasicBlock - This callback is invoked by
474 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
475 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG) {
476 DEBUG(BB->dump());
477 // Codegen the basic block.
Nate Begeman815d6da2005-04-06 00:25:27 +0000478 ISelDAG = &DAG;
Nate Begemana9795f82005-03-24 04:41:43 +0000479 Select(DAG.getRoot());
480
481 // Clear state used for selection.
482 ExprMap.clear();
Nate Begeman815d6da2005-04-06 00:25:27 +0000483 ISelDAG = 0;
Nate Begemana9795f82005-03-24 04:41:43 +0000484 }
Nate Begeman815d6da2005-04-06 00:25:27 +0000485
486 // dag -> dag expanders for integer divide by constant
487 SDOperand BuildSDIVSequence(SDOperand N);
488 SDOperand BuildUDIVSequence(SDOperand N);
Nate Begemana9795f82005-03-24 04:41:43 +0000489
Nate Begemandffcfcc2005-04-01 00:32:34 +0000490 unsigned getGlobalBaseReg();
Nate Begeman6b559972005-04-01 02:59:27 +0000491 unsigned getConstDouble(double floatVal, unsigned Result);
Nate Begeman7ddecb42005-04-06 23:51:40 +0000492 bool SelectBitfieldInsert(SDOperand OR, unsigned Result);
Nate Begemandffcfcc2005-04-01 00:32:34 +0000493 unsigned SelectSetCR0(SDOperand CC);
Nate Begemana9795f82005-03-24 04:41:43 +0000494 unsigned SelectExpr(SDOperand N);
495 unsigned SelectExprFP(SDOperand N, unsigned Result);
496 void Select(SDOperand N);
497
Nate Begeman04730362005-04-01 04:45:11 +0000498 bool SelectAddr(SDOperand N, unsigned& Reg, int& offset);
Nate Begemana9795f82005-03-24 04:41:43 +0000499 void SelectBranchCC(SDOperand N);
500};
501
Nate Begeman80196b12005-04-05 00:15:08 +0000502/// ExactLog2 - This function solves for (Val == 1 << (N-1)) and returns N. It
503/// returns zero when the input is not exactly a power of two.
504static unsigned ExactLog2(unsigned Val) {
505 if (Val == 0 || (Val & (Val-1))) return 0;
506 unsigned Count = 0;
507 while (Val != 1) {
508 Val >>= 1;
509 ++Count;
510 }
511 return Count;
512}
513
Nate Begeman7ddecb42005-04-06 23:51:40 +0000514// IsRunOfOnes - returns true if Val consists of one contiguous run of 1's with
515// any number of 0's on either side. the 1's are allowed to wrap from LSB to
516// MSB. so 0x000FFF0, 0x0000FFFF, and 0xFF0000FF are all runs. 0x0F0F0000 is
517// not, since all 1's are not contiguous.
518static bool IsRunOfOnes(unsigned Val, unsigned &MB, unsigned &ME) {
519 bool isRun = true;
520 MB = 0;
521 ME = 0;
522
523 // look for first set bit
524 int i = 0;
525 for (; i < 32; i++) {
526 if ((Val & (1 << (31 - i))) != 0) {
527 MB = i;
528 ME = i;
529 break;
530 }
531 }
532
533 // look for last set bit
534 for (; i < 32; i++) {
535 if ((Val & (1 << (31 - i))) == 0)
536 break;
537 ME = i;
538 }
539
540 // look for next set bit
541 for (; i < 32; i++) {
542 if ((Val & (1 << (31 - i))) != 0)
543 break;
544 }
545
546 // if we exhausted all the bits, we found a match at this point for 0*1*0*
547 if (i == 32)
548 return true;
549
550 // since we just encountered more 1's, if it doesn't wrap around to the
551 // most significant bit of the word, then we did not find a match to 1*0*1* so
552 // exit.
553 if (MB != 0)
554 return false;
555
556 // look for last set bit
557 for (MB = i; i < 32; i++) {
558 if ((Val & (1 << (31 - i))) == 0)
559 break;
560 }
561
562 // if we exhausted all the bits, then we found a match for 1*0*1*, otherwise,
563 // the value is not a run of ones.
564 if (i == 32)
565 return true;
566 return false;
567}
568
Nate Begeman439b4442005-04-05 04:22:58 +0000569/// getImmediateForOpcode - This method returns a value indicating whether
Nate Begemana9795f82005-03-24 04:41:43 +0000570/// the ConstantSDNode N can be used as an immediate to Opcode. The return
571/// values are either 0, 1 or 2. 0 indicates that either N is not a
572/// ConstantSDNode, or is not suitable for use by that opcode. A return value
573/// of 1 indicates that the constant may be used in normal immediate form. A
574/// return value of 2 indicates that the constant may be used in shifted
Nate Begeman439b4442005-04-05 04:22:58 +0000575/// immediate form. A return value of 3 indicates that log base 2 of the
Nate Begeman815d6da2005-04-06 00:25:27 +0000576/// constant may be used. A return value of 4 indicates that the constant is
577/// suitable for conversion into a magic number for integer division.
Nate Begemana9795f82005-03-24 04:41:43 +0000578///
Nate Begeman439b4442005-04-05 04:22:58 +0000579static unsigned getImmediateForOpcode(SDOperand N, unsigned Opcode,
580 unsigned& Imm, bool U = false) {
Nate Begemana9795f82005-03-24 04:41:43 +0000581 if (N.getOpcode() != ISD::Constant) return 0;
582
583 int v = (int)cast<ConstantSDNode>(N)->getSignExtended();
584
585 switch(Opcode) {
586 default: return 0;
587 case ISD::ADD:
588 if (v <= 32767 && v >= -32768) { Imm = v & 0xFFFF; return 1; }
589 if ((v & 0x0000FFFF) == 0) { Imm = v >> 16; return 2; }
590 break;
591 case ISD::AND:
592 case ISD::XOR:
593 case ISD::OR:
594 if (v >= 0 && v <= 65535) { Imm = v & 0xFFFF; return 1; }
595 if ((v & 0x0000FFFF) == 0) { Imm = v >> 16; return 2; }
596 break;
Nate Begeman307e7442005-03-26 01:28:53 +0000597 case ISD::MUL:
Nate Begeman27523a12005-04-02 00:42:16 +0000598 case ISD::SUB:
Nate Begeman307e7442005-03-26 01:28:53 +0000599 if (v <= 32767 && v >= -32768) { Imm = v & 0xFFFF; return 1; }
600 break;
Nate Begeman3e897162005-03-31 23:55:40 +0000601 case ISD::SETCC:
602 if (U && (v >= 0 && v <= 65535)) { Imm = v & 0xFFFF; return 1; }
603 if (!U && (v <= 32767 && v >= -32768)) { Imm = v & 0xFFFF; return 1; }
604 break;
Nate Begeman80196b12005-04-05 00:15:08 +0000605 case ISD::SDIV:
Nate Begeman439b4442005-04-05 04:22:58 +0000606 if ((Imm = ExactLog2(v))) { return 3; }
Nate Begeman815d6da2005-04-06 00:25:27 +0000607 if (v <= -2 || v >= 2) { return 4; }
608 break;
609 case ISD::UDIV:
Nate Begeman27b4c232005-04-06 06:44:57 +0000610 if (v > 1) { return 4; }
Nate Begeman80196b12005-04-05 00:15:08 +0000611 break;
Nate Begemana9795f82005-03-24 04:41:43 +0000612 }
613 return 0;
614}
Nate Begeman3e897162005-03-31 23:55:40 +0000615
616/// getBCCForSetCC - Returns the PowerPC condition branch mnemonic corresponding
617/// to Condition. If the Condition is unordered or unsigned, the bool argument
618/// U is set to true, otherwise it is set to false.
619static unsigned getBCCForSetCC(unsigned Condition, bool& U) {
620 U = false;
621 switch (Condition) {
622 default: assert(0 && "Unknown condition!"); abort();
623 case ISD::SETEQ: return PPC::BEQ;
624 case ISD::SETNE: return PPC::BNE;
625 case ISD::SETULT: U = true;
626 case ISD::SETLT: return PPC::BLT;
627 case ISD::SETULE: U = true;
628 case ISD::SETLE: return PPC::BLE;
629 case ISD::SETUGT: U = true;
630 case ISD::SETGT: return PPC::BGT;
631 case ISD::SETUGE: U = true;
632 case ISD::SETGE: return PPC::BGE;
633 }
Nate Begeman04730362005-04-01 04:45:11 +0000634 return 0;
635}
636
637/// IndexedOpForOp - Return the indexed variant for each of the PowerPC load
638/// and store immediate instructions.
639static unsigned IndexedOpForOp(unsigned Opcode) {
640 switch(Opcode) {
641 default: assert(0 && "Unknown opcode!"); abort();
642 case PPC::LBZ: return PPC::LBZX; case PPC::STB: return PPC::STBX;
643 case PPC::LHZ: return PPC::LHZX; case PPC::STH: return PPC::STHX;
644 case PPC::LHA: return PPC::LHAX; case PPC::STW: return PPC::STWX;
645 case PPC::LWZ: return PPC::LWZX; case PPC::STFS: return PPC::STFSX;
646 case PPC::LFS: return PPC::LFSX; case PPC::STFD: return PPC::STFDX;
647 case PPC::LFD: return PPC::LFDX;
648 }
649 return 0;
Nate Begeman3e897162005-03-31 23:55:40 +0000650}
Nate Begeman815d6da2005-04-06 00:25:27 +0000651
652// Structure used to return the necessary information to codegen an SDIV as
653// a multiply.
654struct ms {
655 int m; // magic number
656 int s; // shift amount
657};
658
659struct mu {
660 unsigned int m; // magic number
661 int a; // add indicator
662 int s; // shift amount
663};
664
665/// magic - calculate the magic numbers required to codegen an integer sdiv as
666/// a sequence of multiply and shifts. Requires that the divisor not be 0, 1,
667/// or -1.
668static struct ms magic(int d) {
669 int p;
670 unsigned int ad, anc, delta, q1, r1, q2, r2, t;
671 const unsigned int two31 = 2147483648U; // 2^31
672 struct ms mag;
673
674 ad = abs(d);
675 t = two31 + ((unsigned int)d >> 31);
676 anc = t - 1 - t%ad; // absolute value of nc
677 p = 31; // initialize p
678 q1 = two31/anc; // initialize q1 = 2p/abs(nc)
679 r1 = two31 - q1*anc; // initialize r1 = rem(2p,abs(nc))
680 q2 = two31/ad; // initialize q2 = 2p/abs(d)
681 r2 = two31 - q2*ad; // initialize r2 = rem(2p,abs(d))
682 do {
683 p = p + 1;
684 q1 = 2*q1; // update q1 = 2p/abs(nc)
685 r1 = 2*r1; // update r1 = rem(2p/abs(nc))
686 if (r1 >= anc) { // must be unsigned comparison
687 q1 = q1 + 1;
688 r1 = r1 - anc;
689 }
690 q2 = 2*q2; // update q2 = 2p/abs(d)
691 r2 = 2*r2; // update r2 = rem(2p/abs(d))
692 if (r2 >= ad) { // must be unsigned comparison
693 q2 = q2 + 1;
694 r2 = r2 - ad;
695 }
696 delta = ad - r2;
697 } while (q1 < delta || (q1 == delta && r1 == 0));
698
699 mag.m = q2 + 1;
700 if (d < 0) mag.m = -mag.m; // resulting magic number
701 mag.s = p - 32; // resulting shift
702 return mag;
703}
704
705/// magicu - calculate the magic numbers required to codegen an integer udiv as
706/// a sequence of multiply, add and shifts. Requires that the divisor not be 0.
707static struct mu magicu(unsigned d)
708{
709 int p;
710 unsigned int nc, delta, q1, r1, q2, r2;
711 struct mu magu;
712 magu.a = 0; // initialize "add" indicator
713 nc = - 1 - (-d)%d;
714 p = 31; // initialize p
715 q1 = 0x80000000/nc; // initialize q1 = 2p/nc
716 r1 = 0x80000000 - q1*nc; // initialize r1 = rem(2p,nc)
717 q2 = 0x7FFFFFFF/d; // initialize q2 = (2p-1)/d
718 r2 = 0x7FFFFFFF - q2*d; // initialize r2 = rem((2p-1),d)
719 do {
720 p = p + 1;
721 if (r1 >= nc - r1 ) {
722 q1 = 2*q1 + 1; // update q1
723 r1 = 2*r1 - nc; // update r1
724 }
725 else {
726 q1 = 2*q1; // update q1
727 r1 = 2*r1; // update r1
728 }
729 if (r2 + 1 >= d - r2) {
730 if (q2 >= 0x7FFFFFFF) magu.a = 1;
731 q2 = 2*q2 + 1; // update q2
732 r2 = 2*r2 + 1 - d; // update r2
733 }
734 else {
735 if (q2 >= 0x80000000) magu.a = 1;
736 q2 = 2*q2; // update q2
737 r2 = 2*r2 + 1; // update r2
738 }
739 delta = d - 1 - r2;
740 } while (p < 64 && (q1 < delta || (q1 == delta && r1 == 0)));
741 magu.m = q2 + 1; // resulting magic number
742 magu.s = p - 32; // resulting shift
743 return magu;
744}
745}
746
747/// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
748/// return a DAG expression to select that will generate the same value by
749/// multiplying by a magic number. See:
750/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
751SDOperand ISel::BuildSDIVSequence(SDOperand N) {
752 int d = (int)cast<ConstantSDNode>(N.getOperand(1))->getSignExtended();
753 ms magics = magic(d);
754 // Multiply the numerator (operand 0) by the magic value
755 SDOperand Q = ISelDAG->getNode(ISD::MULHS, MVT::i32, N.getOperand(0),
756 ISelDAG->getConstant(magics.m, MVT::i32));
757 // If d > 0 and m < 0, add the numerator
758 if (d > 0 && magics.m < 0)
759 Q = ISelDAG->getNode(ISD::ADD, MVT::i32, Q, N.getOperand(0));
760 // If d < 0 and m > 0, subtract the numerator.
761 if (d < 0 && magics.m > 0)
762 Q = ISelDAG->getNode(ISD::SUB, MVT::i32, Q, N.getOperand(0));
763 // Shift right algebraic if shift value is nonzero
764 if (magics.s > 0)
765 Q = ISelDAG->getNode(ISD::SRA, MVT::i32, Q,
766 ISelDAG->getConstant(magics.s, MVT::i32));
767 // Extract the sign bit and add it to the quotient
768 SDOperand T =
769 ISelDAG->getNode(ISD::SRL, MVT::i32, Q, ISelDAG->getConstant(31, MVT::i32));
Nate Begeman27b4c232005-04-06 06:44:57 +0000770 return ISelDAG->getNode(ISD::ADD, MVT::i32, Q, T);
Nate Begeman815d6da2005-04-06 00:25:27 +0000771}
772
773/// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
774/// return a DAG expression to select that will generate the same value by
775/// multiplying by a magic number. See:
776/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
777SDOperand ISel::BuildUDIVSequence(SDOperand N) {
778 unsigned d =
779 (unsigned)cast<ConstantSDNode>(N.getOperand(1))->getSignExtended();
780 mu magics = magicu(d);
781 // Multiply the numerator (operand 0) by the magic value
782 SDOperand Q = ISelDAG->getNode(ISD::MULHU, MVT::i32, N.getOperand(0),
783 ISelDAG->getConstant(magics.m, MVT::i32));
784 if (magics.a == 0) {
785 Q = ISelDAG->getNode(ISD::SRL, MVT::i32, Q,
786 ISelDAG->getConstant(magics.s, MVT::i32));
787 } else {
788 SDOperand NPQ = ISelDAG->getNode(ISD::SUB, MVT::i32, N.getOperand(0), Q);
789 NPQ = ISelDAG->getNode(ISD::SRL, MVT::i32, NPQ,
790 ISelDAG->getConstant(1, MVT::i32));
791 NPQ = ISelDAG->getNode(ISD::ADD, MVT::i32, NPQ, Q);
792 Q = ISelDAG->getNode(ISD::SRL, MVT::i32, NPQ,
793 ISelDAG->getConstant(magics.s-1, MVT::i32));
794 }
Nate Begeman27b4c232005-04-06 06:44:57 +0000795 return Q;
Nate Begemana9795f82005-03-24 04:41:43 +0000796}
797
Nate Begemanc7b09f12005-03-25 08:34:25 +0000798/// getGlobalBaseReg - Output the instructions required to put the
799/// base address to use for accessing globals into a register.
800///
801unsigned ISel::getGlobalBaseReg() {
802 if (!GlobalBaseInitialized) {
803 // Insert the set of GlobalBaseReg into the first MBB of the function
804 MachineBasicBlock &FirstMBB = BB->getParent()->front();
805 MachineBasicBlock::iterator MBBI = FirstMBB.begin();
806 GlobalBaseReg = MakeReg(MVT::i32);
807 BuildMI(FirstMBB, MBBI, PPC::MovePCtoLR, 0, PPC::LR);
808 BuildMI(FirstMBB, MBBI, PPC::MFLR, 1, GlobalBaseReg).addReg(PPC::LR);
809 GlobalBaseInitialized = true;
810 }
811 return GlobalBaseReg;
812}
813
Nate Begeman6b559972005-04-01 02:59:27 +0000814/// getConstDouble - Loads a floating point value into a register, via the
815/// Constant Pool. Optionally takes a register in which to load the value.
816unsigned ISel::getConstDouble(double doubleVal, unsigned Result=0) {
817 unsigned Tmp1 = MakeReg(MVT::i32);
818 if (0 == Result) Result = MakeReg(MVT::f64);
819 MachineConstantPool *CP = BB->getParent()->getConstantPool();
820 ConstantFP *CFP = ConstantFP::get(Type::DoubleTy, doubleVal);
821 unsigned CPI = CP->getConstantPoolIndex(CFP);
822 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp1).addReg(getGlobalBaseReg())
823 .addConstantPoolIndex(CPI);
824 BuildMI(BB, PPC::LFD, 2, Result).addConstantPoolIndex(CPI).addReg(Tmp1);
825 return Result;
826}
827
Nate Begeman7ddecb42005-04-06 23:51:40 +0000828/// SelectBitfieldInsert - turn an or of two masked values into
829/// the rotate left word immediate then mask insert (rlwimi) instruction.
830/// Returns true on success, false if the caller still needs to select OR.
831///
832/// Patterns matched:
833/// 1. or shl, and 5. or and, and
834/// 2. or and, shl 6. or shl, shr
835/// 3. or shr, and 7. or shr, shl
836/// 4. or and, shr
837bool ISel::SelectBitfieldInsert(SDOperand OR, unsigned Result) {
838 unsigned TgtMask = 0xFFFFFFFF, InsMask = 0xFFFFFFFF, Amount = 0;
839 unsigned Op0Opc = OR.getOperand(0).getOpcode();
840 unsigned Op1Opc = OR.getOperand(1).getOpcode();
841
842 // Verify that we have the correct opcodes
843 if (ISD::SHL != Op0Opc && ISD::SRL != Op0Opc && ISD::AND != Op0Opc)
844 return false;
845 if (ISD::SHL != Op1Opc && ISD::SRL != Op1Opc && ISD::AND != Op1Opc)
846 return false;
847
848 // Generate Mask value for Target
849 if (ConstantSDNode *CN =
850 dyn_cast<ConstantSDNode>(OR.getOperand(0).getOperand(1).Val)) {
851 switch(Op0Opc) {
852 case ISD::SHL: TgtMask <<= (unsigned)CN->getValue(); break;
853 case ISD::SRL: TgtMask >>= (unsigned)CN->getValue(); break;
854 case ISD::AND: TgtMask &= (unsigned)CN->getValue(); break;
855 }
856 } else {
857 return false;
858 }
859
860 // Generate Mask value for Insert
861 if (ConstantSDNode *CN =
862 dyn_cast<ConstantSDNode>(OR.getOperand(1).getOperand(1).Val)) {
863 switch(Op1Opc) {
864 case ISD::SHL:
865 Amount = CN->getValue();
866 InsMask <<= Amount;
867 break;
868 case ISD::SRL:
869 Amount = CN->getValue();
870 InsMask >>= Amount;
871 Amount = 32-Amount;
872 break;
873 case ISD::AND:
874 InsMask &= (unsigned)CN->getValue();
875 break;
876 }
877 } else {
878 return false;
879 }
880
881 // Verify that the Target mask and Insert mask together form a full word mask
882 // and that the Insert mask is a run of set bits (which implies both are runs
883 // of set bits). Given that, Select the arguments and generate the rlwimi
884 // instruction.
885 unsigned MB, ME;
886 if (((TgtMask ^ InsMask) == 0xFFFFFFFF) && IsRunOfOnes(InsMask, MB, ME)) {
887 unsigned Tmp1, Tmp2;
888 if (Op0Opc == ISD::AND)
889 Tmp1 = SelectExpr(OR.getOperand(0).getOperand(0));
890 else
891 Tmp1 = SelectExpr(OR.getOperand(0));
892 Tmp2 = SelectExpr(OR.getOperand(1).getOperand(0));
893 BuildMI(BB, PPC::RLWIMI, 5, Result).addReg(Tmp1).addReg(Tmp2)
894 .addImm(Amount).addImm(MB).addImm(ME);
895 return true;
896 }
897 return false;
898}
899
Nate Begemandffcfcc2005-04-01 00:32:34 +0000900unsigned ISel::SelectSetCR0(SDOperand CC) {
901 unsigned Opc, Tmp1, Tmp2;
902 static const unsigned CompareOpcodes[] =
903 { PPC::FCMPU, PPC::FCMPU, PPC::CMPW, PPC::CMPLW };
904
905 // If the first operand to the select is a SETCC node, then we can fold it
906 // into the branch that selects which value to return.
907 SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(CC.Val);
908 if (SetCC && CC.getOpcode() == ISD::SETCC) {
909 bool U;
910 Opc = getBCCForSetCC(SetCC->getCondition(), U);
911 Tmp1 = SelectExpr(SetCC->getOperand(0));
912
Nate Begeman439b4442005-04-05 04:22:58 +0000913 // Pass the optional argument U to getImmediateForOpcode for SETCC,
Nate Begemandffcfcc2005-04-01 00:32:34 +0000914 // so that it knows whether the SETCC immediate range is signed or not.
Nate Begeman439b4442005-04-05 04:22:58 +0000915 if (1 == getImmediateForOpcode(SetCC->getOperand(1), ISD::SETCC,
916 Tmp2, U)) {
Nate Begemandffcfcc2005-04-01 00:32:34 +0000917 if (U)
918 BuildMI(BB, PPC::CMPLWI, 2, PPC::CR0).addReg(Tmp1).addImm(Tmp2);
919 else
920 BuildMI(BB, PPC::CMPWI, 2, PPC::CR0).addReg(Tmp1).addSImm(Tmp2);
921 } else {
922 bool IsInteger = MVT::isInteger(SetCC->getOperand(0).getValueType());
923 unsigned CompareOpc = CompareOpcodes[2 * IsInteger + U];
924 Tmp2 = SelectExpr(SetCC->getOperand(1));
925 BuildMI(BB, CompareOpc, 2, PPC::CR0).addReg(Tmp1).addReg(Tmp2);
926 }
927 } else {
928 Tmp1 = SelectExpr(CC);
929 BuildMI(BB, PPC::CMPLWI, 2, PPC::CR0).addReg(Tmp1).addImm(0);
930 Opc = PPC::BNE;
931 }
932 return Opc;
933}
934
935/// Check to see if the load is a constant offset from a base register
Nate Begeman04730362005-04-01 04:45:11 +0000936bool ISel::SelectAddr(SDOperand N, unsigned& Reg, int& offset)
Nate Begemana9795f82005-03-24 04:41:43 +0000937{
Nate Begeman96fc6812005-03-31 02:05:53 +0000938 unsigned imm = 0, opcode = N.getOpcode();
Nate Begeman04730362005-04-01 04:45:11 +0000939 if (N.getOpcode() == ISD::ADD) {
940 Reg = SelectExpr(N.getOperand(0));
Nate Begeman439b4442005-04-05 04:22:58 +0000941 if (1 == getImmediateForOpcode(N.getOperand(1), opcode, imm)) {
Nate Begeman96fc6812005-03-31 02:05:53 +0000942 offset = imm;
Nate Begeman04730362005-04-01 04:45:11 +0000943 return false;
944 }
945 offset = SelectExpr(N.getOperand(1));
946 return true;
947 }
Nate Begemana9795f82005-03-24 04:41:43 +0000948 Reg = SelectExpr(N);
949 offset = 0;
Nate Begeman04730362005-04-01 04:45:11 +0000950 return false;
Nate Begemana9795f82005-03-24 04:41:43 +0000951}
952
953void ISel::SelectBranchCC(SDOperand N)
954{
955 assert(N.getOpcode() == ISD::BRCOND && "Not a BranchCC???");
956 MachineBasicBlock *Dest =
957 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
Nate Begeman3e897162005-03-31 23:55:40 +0000958
Nate Begeman439b4442005-04-05 04:22:58 +0000959 // Get the MBB we will fall through to so that we can hand it off to the
960 // branch selection pass as an argument to the PPC::COND_BRANCH pseudo op.
Nate Begemanc8c5c8f2005-04-05 04:32:16 +0000961 //ilist<MachineBasicBlock>::iterator It = BB;
962 //MachineBasicBlock *Fallthrough = ++It;
Nate Begeman439b4442005-04-05 04:22:58 +0000963
Nate Begemana9795f82005-03-24 04:41:43 +0000964 Select(N.getOperand(0)); //chain
Nate Begemandffcfcc2005-04-01 00:32:34 +0000965 unsigned Opc = SelectSetCR0(N.getOperand(1));
Nate Begemanc8c5c8f2005-04-05 04:32:16 +0000966 // FIXME: Use this once we have something approximating two-way branches
967 // We cannot currently use this in case the ISel hands us something like
968 // BRcc MBBx
969 // BR MBBy
970 // since the fallthrough basic block for the conditional branch does not start
971 // with the unconditional branch (it is skipped over).
972 //BuildMI(BB, PPC::COND_BRANCH, 4).addReg(PPC::CR0).addImm(Opc)
973 // .addMBB(Dest).addMBB(Fallthrough);
974 BuildMI(BB, Opc, 2).addReg(PPC::CR0).addMBB(Dest);
Nate Begemana9795f82005-03-24 04:41:43 +0000975 return;
976}
977
978unsigned ISel::SelectExprFP(SDOperand N, unsigned Result)
979{
980 unsigned Tmp1, Tmp2, Tmp3;
981 unsigned Opc = 0;
982 SDNode *Node = N.Val;
983 MVT::ValueType DestType = N.getValueType();
984 unsigned opcode = N.getOpcode();
985
986 switch (opcode) {
987 default:
988 Node->dump();
989 assert(0 && "Node not handled!\n");
990
Nate Begeman23afcfb2005-03-29 22:48:55 +0000991 case ISD::SELECT: {
Nate Begeman3e897162005-03-31 23:55:40 +0000992 // Attempt to generate FSEL. We can do this whenever we have an FP result,
993 // and an FP comparison in the SetCC node.
994 SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(N.getOperand(0).Val);
995 if (SetCC && N.getOperand(0).getOpcode() == ISD::SETCC &&
996 !MVT::isInteger(SetCC->getOperand(0).getValueType()) &&
997 SetCC->getCondition() != ISD::SETEQ &&
998 SetCC->getCondition() != ISD::SETNE) {
999 MVT::ValueType VT = SetCC->getOperand(0).getValueType();
1000 Tmp1 = SelectExpr(SetCC->getOperand(0)); // Val to compare against
1001 unsigned TV = SelectExpr(N.getOperand(1)); // Use if TRUE
1002 unsigned FV = SelectExpr(N.getOperand(2)); // Use if FALSE
1003
1004 ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(1));
1005 if (CN && (CN->isExactlyValue(-0.0) || CN->isExactlyValue(0.0))) {
1006 switch(SetCC->getCondition()) {
1007 default: assert(0 && "Invalid FSEL condition"); abort();
1008 case ISD::SETULT:
1009 case ISD::SETLT:
1010 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp1).addReg(FV).addReg(TV);
1011 return Result;
1012 case ISD::SETUGE:
1013 case ISD::SETGE:
1014 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp1).addReg(TV).addReg(FV);
1015 return Result;
1016 case ISD::SETUGT:
1017 case ISD::SETGT: {
1018 Tmp2 = MakeReg(VT);
1019 BuildMI(BB, PPC::FNEG, 1, Tmp2).addReg(Tmp1);
1020 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp2).addReg(FV).addReg(TV);
1021 return Result;
1022 }
1023 case ISD::SETULE:
1024 case ISD::SETLE: {
1025 Tmp2 = MakeReg(VT);
1026 BuildMI(BB, PPC::FNEG, 1, Tmp2).addReg(Tmp1);
1027 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp2).addReg(TV).addReg(FV);
1028 return Result;
1029 }
1030 }
1031 } else {
1032 Opc = (MVT::f64 == VT) ? PPC::FSUB : PPC::FSUBS;
1033 Tmp2 = SelectExpr(SetCC->getOperand(1));
1034 Tmp3 = MakeReg(VT);
1035 switch(SetCC->getCondition()) {
1036 default: assert(0 && "Invalid FSEL condition"); abort();
1037 case ISD::SETULT:
1038 case ISD::SETLT:
1039 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
1040 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(FV).addReg(TV);
1041 return Result;
1042 case ISD::SETUGE:
1043 case ISD::SETGE:
1044 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
1045 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(TV).addReg(FV);
1046 return Result;
1047 case ISD::SETUGT:
1048 case ISD::SETGT:
1049 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp2).addReg(Tmp1);
1050 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(FV).addReg(TV);
1051 return Result;
1052 case ISD::SETULE:
1053 case ISD::SETLE:
1054 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp2).addReg(Tmp1);
1055 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(TV).addReg(FV);
1056 return Result;
1057 }
1058 }
1059 assert(0 && "Should never get here");
1060 return 0;
1061 }
1062
Nate Begeman31318e42005-04-01 07:21:30 +00001063 unsigned TrueValue = SelectExpr(N.getOperand(1)); //Use if TRUE
1064 unsigned FalseValue = SelectExpr(N.getOperand(2)); //Use if FALSE
Nate Begeman6cb2e1b2005-04-01 08:57:43 +00001065 Opc = SelectSetCR0(N.getOperand(0));
Nate Begeman31318e42005-04-01 07:21:30 +00001066
Nate Begeman23afcfb2005-03-29 22:48:55 +00001067 // Create an iterator with which to insert the MBB for copying the false
1068 // value and the MBB to hold the PHI instruction for this SetCC.
1069 MachineBasicBlock *thisMBB = BB;
1070 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1071 ilist<MachineBasicBlock>::iterator It = BB;
1072 ++It;
1073
1074 // thisMBB:
1075 // ...
1076 // TrueVal = ...
1077 // cmpTY cr0, r1, r2
1078 // bCC copy1MBB
1079 // fallthrough --> copy0MBB
Nate Begeman23afcfb2005-03-29 22:48:55 +00001080 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
1081 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
Nate Begeman6cb2e1b2005-04-01 08:57:43 +00001082 BuildMI(BB, Opc, 2).addReg(PPC::CR0).addMBB(sinkMBB);
Nate Begeman23afcfb2005-03-29 22:48:55 +00001083 MachineFunction *F = BB->getParent();
1084 F->getBasicBlockList().insert(It, copy0MBB);
1085 F->getBasicBlockList().insert(It, sinkMBB);
1086 // Update machine-CFG edges
1087 BB->addSuccessor(copy0MBB);
1088 BB->addSuccessor(sinkMBB);
1089
1090 // copy0MBB:
1091 // %FalseValue = ...
1092 // # fallthrough to sinkMBB
1093 BB = copy0MBB;
Nate Begeman23afcfb2005-03-29 22:48:55 +00001094 // Update machine-CFG edges
1095 BB->addSuccessor(sinkMBB);
1096
1097 // sinkMBB:
1098 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1099 // ...
1100 BB = sinkMBB;
1101 BuildMI(BB, PPC::PHI, 4, Result).addReg(FalseValue)
1102 .addMBB(copy0MBB).addReg(TrueValue).addMBB(thisMBB);
1103 return Result;
1104 }
Nate Begeman27eeb002005-04-02 05:59:34 +00001105
1106 case ISD::FNEG:
Nate Begeman93075ec2005-04-04 23:40:36 +00001107 if (!NoExcessFPPrecision &&
1108 ISD::ADD == N.getOperand(0).getOpcode() &&
1109 N.getOperand(0).Val->hasOneUse() &&
1110 ISD::MUL == N.getOperand(0).getOperand(0).getOpcode() &&
1111 N.getOperand(0).getOperand(0).Val->hasOneUse()) {
Nate Begeman80196b12005-04-05 00:15:08 +00001112 ++FusedFP; // Statistic
Nate Begeman93075ec2005-04-04 23:40:36 +00001113 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(0));
1114 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(1));
1115 Tmp3 = SelectExpr(N.getOperand(0).getOperand(1));
1116 Opc = DestType == MVT::f64 ? PPC::FNMADD : PPC::FNMADDS;
1117 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1118 } else if (!NoExcessFPPrecision &&
1119 ISD::SUB == N.getOperand(0).getOpcode() &&
1120 N.getOperand(0).Val->hasOneUse() &&
1121 ISD::MUL == N.getOperand(0).getOperand(0).getOpcode() &&
1122 N.getOperand(0).getOperand(0).Val->hasOneUse()) {
Nate Begeman80196b12005-04-05 00:15:08 +00001123 ++FusedFP; // Statistic
Nate Begeman93075ec2005-04-04 23:40:36 +00001124 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(0));
1125 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(1));
1126 Tmp3 = SelectExpr(N.getOperand(0).getOperand(1));
1127 Opc = DestType == MVT::f64 ? PPC::FNMSUB : PPC::FNMSUBS;
1128 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1129 } else if (ISD::FABS == N.getOperand(0).getOpcode()) {
Nate Begeman27eeb002005-04-02 05:59:34 +00001130 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1131 BuildMI(BB, PPC::FNABS, 1, Result).addReg(Tmp1);
1132 } else {
1133 Tmp1 = SelectExpr(N.getOperand(0));
1134 BuildMI(BB, PPC::FNEG, 1, Result).addReg(Tmp1);
1135 }
1136 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001137
Nate Begeman27eeb002005-04-02 05:59:34 +00001138 case ISD::FABS:
1139 Tmp1 = SelectExpr(N.getOperand(0));
1140 BuildMI(BB, PPC::FABS, 1, Result).addReg(Tmp1);
1141 return Result;
1142
Nate Begemana9795f82005-03-24 04:41:43 +00001143 case ISD::FP_ROUND:
1144 assert (DestType == MVT::f32 &&
1145 N.getOperand(0).getValueType() == MVT::f64 &&
1146 "only f64 to f32 conversion supported here");
1147 Tmp1 = SelectExpr(N.getOperand(0));
1148 BuildMI(BB, PPC::FRSP, 1, Result).addReg(Tmp1);
1149 return Result;
1150
1151 case ISD::FP_EXTEND:
1152 assert (DestType == MVT::f64 &&
1153 N.getOperand(0).getValueType() == MVT::f32 &&
1154 "only f32 to f64 conversion supported here");
1155 Tmp1 = SelectExpr(N.getOperand(0));
1156 BuildMI(BB, PPC::FMR, 1, Result).addReg(Tmp1);
1157 return Result;
1158
1159 case ISD::CopyFromReg:
Nate Begemanf2622612005-03-26 02:17:46 +00001160 if (Result == 1)
1161 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1162 Tmp1 = dyn_cast<RegSDNode>(Node)->getReg();
1163 BuildMI(BB, PPC::FMR, 1, Result).addReg(Tmp1);
1164 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001165
Nate Begeman6d369cc2005-04-01 01:08:07 +00001166 case ISD::ConstantFP: {
Nate Begeman6d369cc2005-04-01 01:08:07 +00001167 ConstantFPSDNode *CN = cast<ConstantFPSDNode>(N);
Nate Begeman6b559972005-04-01 02:59:27 +00001168 Result = getConstDouble(CN->getValue(), Result);
Nate Begeman6d369cc2005-04-01 01:08:07 +00001169 return Result;
1170 }
Nate Begemana9795f82005-03-24 04:41:43 +00001171
Nate Begemana9795f82005-03-24 04:41:43 +00001172 case ISD::ADD:
Nate Begeman93075ec2005-04-04 23:40:36 +00001173 if (!NoExcessFPPrecision && N.getOperand(0).getOpcode() == ISD::MUL &&
1174 N.getOperand(0).Val->hasOneUse()) {
1175 ++FusedFP; // Statistic
1176 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1177 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1178 Tmp3 = SelectExpr(N.getOperand(1));
1179 Opc = DestType == MVT::f64 ? PPC::FMADD : PPC::FMADDS;
1180 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1181 return Result;
1182 }
1183 Opc = DestType == MVT::f64 ? PPC::FADD : PPC::FADDS;
1184 Tmp1 = SelectExpr(N.getOperand(0));
1185 Tmp2 = SelectExpr(N.getOperand(1));
1186 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1187 return Result;
1188
Nate Begemana9795f82005-03-24 04:41:43 +00001189 case ISD::SUB:
Nate Begeman93075ec2005-04-04 23:40:36 +00001190 if (!NoExcessFPPrecision && N.getOperand(0).getOpcode() == ISD::MUL &&
1191 N.getOperand(0).Val->hasOneUse()) {
1192 ++FusedFP; // Statistic
1193 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1194 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1195 Tmp3 = SelectExpr(N.getOperand(1));
1196 Opc = DestType == MVT::f64 ? PPC::FMSUB : PPC::FMSUBS;
1197 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1198 return Result;
1199 }
1200 Opc = DestType == MVT::f64 ? PPC::FSUB : PPC::FSUBS;
1201 Tmp1 = SelectExpr(N.getOperand(0));
1202 Tmp2 = SelectExpr(N.getOperand(1));
1203 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1204 return Result;
1205
1206 case ISD::MUL:
Nate Begemana9795f82005-03-24 04:41:43 +00001207 case ISD::SDIV:
1208 switch( opcode ) {
1209 case ISD::MUL: Opc = DestType == MVT::f64 ? PPC::FMUL : PPC::FMULS; break;
Nate Begemana9795f82005-03-24 04:41:43 +00001210 case ISD::SDIV: Opc = DestType == MVT::f64 ? PPC::FDIV : PPC::FDIVS; break;
1211 };
Nate Begemana9795f82005-03-24 04:41:43 +00001212 Tmp1 = SelectExpr(N.getOperand(0));
1213 Tmp2 = SelectExpr(N.getOperand(1));
1214 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1215 return Result;
1216
Nate Begemana9795f82005-03-24 04:41:43 +00001217 case ISD::UINT_TO_FP:
Nate Begemanfdcf3412005-03-30 19:38:35 +00001218 case ISD::SINT_TO_FP: {
1219 assert (N.getOperand(0).getValueType() == MVT::i32
1220 && "int to float must operate on i32");
1221 bool IsUnsigned = (ISD::UINT_TO_FP == opcode);
1222 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
1223 Tmp2 = MakeReg(MVT::f64); // temp reg to load the integer value into
1224 Tmp3 = MakeReg(MVT::i32); // temp reg to hold the conversion constant
1225 unsigned ConstF = MakeReg(MVT::f64); // temp reg to hold the fp constant
1226
1227 int FrameIdx = BB->getParent()->getFrameInfo()->CreateStackObject(8, 8);
1228 MachineConstantPool *CP = BB->getParent()->getConstantPool();
1229
1230 // FIXME: pull this FP constant generation stuff out into something like
1231 // the simple ISel's getReg.
1232 if (IsUnsigned) {
1233 ConstantFP *CFP = ConstantFP::get(Type::DoubleTy, 0x1.000000p52);
1234 unsigned CPI = CP->getConstantPoolIndex(CFP);
1235 // Load constant fp value
1236 unsigned Tmp4 = MakeReg(MVT::i32);
1237 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp4).addReg(getGlobalBaseReg())
1238 .addConstantPoolIndex(CPI);
1239 BuildMI(BB, PPC::LFD, 2, ConstF).addConstantPoolIndex(CPI).addReg(Tmp4);
1240 // Store the hi & low halves of the fp value, currently in int regs
1241 BuildMI(BB, PPC::LIS, 1, Tmp3).addSImm(0x4330);
1242 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(Tmp3), FrameIdx);
1243 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(Tmp1), FrameIdx, 4);
1244 addFrameReference(BuildMI(BB, PPC::LFD, 2, Tmp2), FrameIdx);
1245 // Generate the return value with a subtract
1246 BuildMI(BB, PPC::FSUB, 2, Result).addReg(Tmp2).addReg(ConstF);
1247 } else {
1248 ConstantFP *CFP = ConstantFP::get(Type::DoubleTy, 0x1.000008p52);
1249 unsigned CPI = CP->getConstantPoolIndex(CFP);
1250 // Load constant fp value
1251 unsigned Tmp4 = MakeReg(MVT::i32);
1252 unsigned TmpL = MakeReg(MVT::i32);
1253 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp4).addReg(getGlobalBaseReg())
1254 .addConstantPoolIndex(CPI);
1255 BuildMI(BB, PPC::LFD, 2, ConstF).addConstantPoolIndex(CPI).addReg(Tmp4);
1256 // Store the hi & low halves of the fp value, currently in int regs
1257 BuildMI(BB, PPC::LIS, 1, Tmp3).addSImm(0x4330);
1258 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(Tmp3), FrameIdx);
1259 BuildMI(BB, PPC::XORIS, 2, TmpL).addReg(Tmp1).addImm(0x8000);
1260 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(TmpL), FrameIdx, 4);
1261 addFrameReference(BuildMI(BB, PPC::LFD, 2, Tmp2), FrameIdx);
1262 // Generate the return value with a subtract
1263 BuildMI(BB, PPC::FSUB, 2, Result).addReg(Tmp2).addReg(ConstF);
1264 }
1265 return Result;
1266 }
Nate Begemana9795f82005-03-24 04:41:43 +00001267 }
Nate Begeman6b559972005-04-01 02:59:27 +00001268 assert(0 && "Should never get here");
Nate Begemana9795f82005-03-24 04:41:43 +00001269 return 0;
1270}
1271
1272unsigned ISel::SelectExpr(SDOperand N) {
1273 unsigned Result;
1274 unsigned Tmp1, Tmp2, Tmp3;
1275 unsigned Opc = 0;
1276 unsigned opcode = N.getOpcode();
1277
1278 SDNode *Node = N.Val;
1279 MVT::ValueType DestType = N.getValueType();
1280
1281 unsigned &Reg = ExprMap[N];
1282 if (Reg) return Reg;
1283
Nate Begeman27eeb002005-04-02 05:59:34 +00001284 switch (N.getOpcode()) {
1285 default:
Nate Begemana9795f82005-03-24 04:41:43 +00001286 Reg = Result = (N.getValueType() != MVT::Other) ?
Nate Begeman27eeb002005-04-02 05:59:34 +00001287 MakeReg(N.getValueType()) : 1;
1288 break;
1289 case ISD::CALL:
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001290 // If this is a call instruction, make sure to prepare ALL of the result
1291 // values as well as the chain.
Nate Begeman27eeb002005-04-02 05:59:34 +00001292 if (Node->getNumValues() == 1)
1293 Reg = Result = 1; // Void call, just a chain.
1294 else {
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001295 Result = MakeReg(Node->getValueType(0));
1296 ExprMap[N.getValue(0)] = Result;
Nate Begeman27eeb002005-04-02 05:59:34 +00001297 for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001298 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
Nate Begeman27eeb002005-04-02 05:59:34 +00001299 ExprMap[SDOperand(Node, Node->getNumValues()-1)] = 1;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001300 }
Nate Begeman27eeb002005-04-02 05:59:34 +00001301 break;
1302 case ISD::ADD_PARTS:
1303 case ISD::SUB_PARTS:
1304 case ISD::SHL_PARTS:
1305 case ISD::SRL_PARTS:
1306 case ISD::SRA_PARTS:
1307 Result = MakeReg(Node->getValueType(0));
1308 ExprMap[N.getValue(0)] = Result;
1309 for (unsigned i = 1, e = N.Val->getNumValues(); i != e; ++i)
1310 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
1311 break;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001312 }
1313
Nate Begemane5846682005-04-04 06:52:38 +00001314 if (ISD::CopyFromReg == opcode)
1315 DestType = N.getValue(0).getValueType();
1316
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001317 if (DestType == MVT::f64 || DestType == MVT::f32)
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001318 if (ISD::LOAD != opcode && ISD::EXTLOAD != opcode && ISD::UNDEF != opcode)
Nate Begeman74d73452005-03-31 00:15:26 +00001319 return SelectExprFP(N, Result);
Nate Begemana9795f82005-03-24 04:41:43 +00001320
1321 switch (opcode) {
1322 default:
1323 Node->dump();
1324 assert(0 && "Node not handled!\n");
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001325 case ISD::UNDEF:
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001326 BuildMI(BB, PPC::IMPLICIT_DEF, 0, Result);
1327 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001328 case ISD::DYNAMIC_STACKALLOC:
Nate Begeman5e966612005-03-24 06:28:42 +00001329 // Generate both result values. FIXME: Need a better commment here?
1330 if (Result != 1)
1331 ExprMap[N.getValue(1)] = 1;
1332 else
1333 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1334
1335 // FIXME: We are currently ignoring the requested alignment for handling
1336 // greater than the stack alignment. This will need to be revisited at some
1337 // point. Align = N.getOperand(2);
1338 if (!isa<ConstantSDNode>(N.getOperand(2)) ||
1339 cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) {
1340 std::cerr << "Cannot allocate stack object with greater alignment than"
1341 << " the stack alignment yet!";
1342 abort();
1343 }
1344 Select(N.getOperand(0));
1345 Tmp1 = SelectExpr(N.getOperand(1));
1346 // Subtract size from stack pointer, thereby allocating some space.
1347 BuildMI(BB, PPC::SUBF, 2, PPC::R1).addReg(Tmp1).addReg(PPC::R1);
1348 // Put a pointer to the space into the result register by copying the SP
1349 BuildMI(BB, PPC::OR, 2, Result).addReg(PPC::R1).addReg(PPC::R1);
1350 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001351
1352 case ISD::ConstantPool:
Nate Begemanca12a2b2005-03-28 22:28:37 +00001353 Tmp1 = cast<ConstantPoolSDNode>(N)->getIndex();
1354 Tmp2 = MakeReg(MVT::i32);
1355 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp2).addReg(getGlobalBaseReg())
1356 .addConstantPoolIndex(Tmp1);
1357 BuildMI(BB, PPC::LA, 2, Result).addReg(Tmp2).addConstantPoolIndex(Tmp1);
1358 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001359
1360 case ISD::FrameIndex:
Nate Begemanf3d08f32005-03-29 00:03:27 +00001361 Tmp1 = cast<FrameIndexSDNode>(N)->getIndex();
Nate Begeman58f718c2005-03-30 02:23:08 +00001362 addFrameReference(BuildMI(BB, PPC::ADDI, 2, Result), (int)Tmp1, 0, false);
Nate Begemanf3d08f32005-03-29 00:03:27 +00001363 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001364
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001365 case ISD::GlobalAddress: {
1366 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
Nate Begemanca12a2b2005-03-28 22:28:37 +00001367 Tmp1 = MakeReg(MVT::i32);
Nate Begemanc7b09f12005-03-25 08:34:25 +00001368 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp1).addReg(getGlobalBaseReg())
1369 .addGlobalAddress(GV);
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001370 if (GV->hasWeakLinkage() || GV->isExternal()) {
1371 BuildMI(BB, PPC::LWZ, 2, Result).addGlobalAddress(GV).addReg(Tmp1);
1372 } else {
1373 BuildMI(BB, PPC::LA, 2, Result).addReg(Tmp1).addGlobalAddress(GV);
1374 }
1375 return Result;
1376 }
1377
Nate Begeman5e966612005-03-24 06:28:42 +00001378 case ISD::LOAD:
Nate Begemana9795f82005-03-24 04:41:43 +00001379 case ISD::EXTLOAD:
1380 case ISD::ZEXTLOAD:
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001381 case ISD::SEXTLOAD: {
Nate Begeman9db505c2005-03-28 19:36:43 +00001382 MVT::ValueType TypeBeingLoaded = (ISD::LOAD == opcode) ?
1383 Node->getValueType(0) : cast<MVTSDNode>(Node)->getExtraValueType();
Nate Begeman74d73452005-03-31 00:15:26 +00001384 bool sext = (ISD::SEXTLOAD == opcode);
Nate Begeman74d73452005-03-31 00:15:26 +00001385
Nate Begeman5e966612005-03-24 06:28:42 +00001386 // Make sure we generate both values.
1387 if (Result != 1)
1388 ExprMap[N.getValue(1)] = 1; // Generate the token
1389 else
1390 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1391
1392 SDOperand Chain = N.getOperand(0);
1393 SDOperand Address = N.getOperand(1);
1394 Select(Chain);
1395
Nate Begeman9db505c2005-03-28 19:36:43 +00001396 switch (TypeBeingLoaded) {
Nate Begeman74d73452005-03-31 00:15:26 +00001397 default: Node->dump(); assert(0 && "Cannot load this type!");
Nate Begeman9db505c2005-03-28 19:36:43 +00001398 case MVT::i1: Opc = PPC::LBZ; break;
1399 case MVT::i8: Opc = PPC::LBZ; break;
1400 case MVT::i16: Opc = sext ? PPC::LHA : PPC::LHZ; break;
1401 case MVT::i32: Opc = PPC::LWZ; break;
Nate Begeman74d73452005-03-31 00:15:26 +00001402 case MVT::f32: Opc = PPC::LFS; break;
1403 case MVT::f64: Opc = PPC::LFD; break;
Nate Begeman5e966612005-03-24 06:28:42 +00001404 }
1405
Nate Begeman74d73452005-03-31 00:15:26 +00001406 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Address)) {
1407 Tmp1 = MakeReg(MVT::i32);
1408 int CPI = CP->getIndex();
1409 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp1).addReg(getGlobalBaseReg())
1410 .addConstantPoolIndex(CPI);
1411 BuildMI(BB, Opc, 2, Result).addConstantPoolIndex(CPI).addReg(Tmp1);
Nate Begeman9db505c2005-03-28 19:36:43 +00001412 }
Nate Begeman74d73452005-03-31 00:15:26 +00001413 else if(Address.getOpcode() == ISD::FrameIndex) {
Nate Begeman58f718c2005-03-30 02:23:08 +00001414 Tmp1 = cast<FrameIndexSDNode>(Address)->getIndex();
1415 addFrameReference(BuildMI(BB, Opc, 2, Result), (int)Tmp1);
Nate Begeman5e966612005-03-24 06:28:42 +00001416 } else {
1417 int offset;
Nate Begeman04730362005-04-01 04:45:11 +00001418 bool idx = SelectAddr(Address, Tmp1, offset);
1419 if (idx) {
1420 Opc = IndexedOpForOp(Opc);
1421 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(offset);
1422 } else {
1423 BuildMI(BB, Opc, 2, Result).addSImm(offset).addReg(Tmp1);
1424 }
Nate Begeman5e966612005-03-24 06:28:42 +00001425 }
1426 return Result;
1427 }
1428
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001429 case ISD::CALL: {
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001430 unsigned GPR_idx = 0, FPR_idx = 0;
1431 static const unsigned GPR[] = {
1432 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
1433 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
1434 };
1435 static const unsigned FPR[] = {
1436 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
1437 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
1438 };
1439
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001440 // Lower the chain for this call.
1441 Select(N.getOperand(0));
1442 ExprMap[N.getValue(Node->getNumValues()-1)] = 1;
Nate Begeman74d73452005-03-31 00:15:26 +00001443
Nate Begemand860aa62005-04-04 22:17:48 +00001444 MachineInstr *CallMI;
1445 // Emit the correct call instruction based on the type of symbol called.
1446 if (GlobalAddressSDNode *GASD =
1447 dyn_cast<GlobalAddressSDNode>(N.getOperand(1))) {
1448 CallMI = BuildMI(PPC::CALLpcrel, 1).addGlobalAddress(GASD->getGlobal(),
1449 true);
1450 } else if (ExternalSymbolSDNode *ESSDN =
1451 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1))) {
1452 CallMI = BuildMI(PPC::CALLpcrel, 1).addExternalSymbol(ESSDN->getSymbol(),
1453 true);
1454 } else {
1455 Tmp1 = SelectExpr(N.getOperand(1));
1456 BuildMI(BB, PPC::OR, 2, PPC::R12).addReg(Tmp1).addReg(Tmp1);
1457 BuildMI(BB, PPC::MTCTR, 1).addReg(PPC::R12);
1458 CallMI = BuildMI(PPC::CALLindirect, 3).addImm(20).addImm(0)
1459 .addReg(PPC::R12);
1460 }
1461
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001462 // Load the register args to virtual regs
1463 std::vector<unsigned> ArgVR;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001464 for(int i = 2, e = Node->getNumOperands(); i < e; ++i)
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001465 ArgVR.push_back(SelectExpr(N.getOperand(i)));
1466
1467 // Copy the virtual registers into the appropriate argument register
1468 for(int i = 0, e = ArgVR.size(); i < e; ++i) {
1469 switch(N.getOperand(i+2).getValueType()) {
1470 default: Node->dump(); assert(0 && "Unknown value type for call");
1471 case MVT::i1:
1472 case MVT::i8:
1473 case MVT::i16:
1474 case MVT::i32:
1475 assert(GPR_idx < 8 && "Too many int args");
Nate Begemand860aa62005-04-04 22:17:48 +00001476 if (N.getOperand(i+2).getOpcode() != ISD::UNDEF) {
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001477 BuildMI(BB, PPC::OR,2,GPR[GPR_idx]).addReg(ArgVR[i]).addReg(ArgVR[i]);
Nate Begemand860aa62005-04-04 22:17:48 +00001478 CallMI->addRegOperand(GPR[GPR_idx], MachineOperand::Use);
1479 }
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001480 ++GPR_idx;
1481 break;
1482 case MVT::f64:
1483 case MVT::f32:
1484 assert(FPR_idx < 13 && "Too many fp args");
1485 BuildMI(BB, PPC::FMR, 1, FPR[FPR_idx]).addReg(ArgVR[i]);
Nate Begemand860aa62005-04-04 22:17:48 +00001486 CallMI->addRegOperand(FPR[FPR_idx], MachineOperand::Use);
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001487 ++FPR_idx;
1488 break;
1489 }
1490 }
Nate Begemand860aa62005-04-04 22:17:48 +00001491
1492 // Put the call instruction in the correct place in the MachineBasicBlock
1493 BB->push_back(CallMI);
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001494
1495 switch (Node->getValueType(0)) {
1496 default: assert(0 && "Unknown value type for call result!");
1497 case MVT::Other: return 1;
1498 case MVT::i1:
1499 case MVT::i8:
1500 case MVT::i16:
1501 case MVT::i32:
Nate Begemane5846682005-04-04 06:52:38 +00001502 if (Node->getValueType(1) == MVT::i32) {
1503 BuildMI(BB, PPC::OR, 2, Result+1).addReg(PPC::R3).addReg(PPC::R3);
1504 BuildMI(BB, PPC::OR, 2, Result).addReg(PPC::R4).addReg(PPC::R4);
1505 } else {
1506 BuildMI(BB, PPC::OR, 2, Result).addReg(PPC::R3).addReg(PPC::R3);
1507 }
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001508 break;
1509 case MVT::f32:
1510 case MVT::f64:
1511 BuildMI(BB, PPC::FMR, 1, Result).addReg(PPC::F1);
1512 break;
1513 }
1514 return Result+N.ResNo;
1515 }
Nate Begemana9795f82005-03-24 04:41:43 +00001516
1517 case ISD::SIGN_EXTEND:
1518 case ISD::SIGN_EXTEND_INREG:
1519 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman9db505c2005-03-28 19:36:43 +00001520 switch(cast<MVTSDNode>(Node)->getExtraValueType()) {
1521 default: Node->dump(); assert(0 && "Unhandled SIGN_EXTEND type"); break;
1522 case MVT::i16:
1523 BuildMI(BB, PPC::EXTSH, 1, Result).addReg(Tmp1);
1524 break;
1525 case MVT::i8:
1526 BuildMI(BB, PPC::EXTSB, 1, Result).addReg(Tmp1);
1527 break;
Nate Begeman74747862005-03-29 22:24:51 +00001528 case MVT::i1:
1529 BuildMI(BB, PPC::SUBFIC, 2, Result).addReg(Tmp1).addSImm(0);
1530 break;
Nate Begeman9db505c2005-03-28 19:36:43 +00001531 }
Nate Begemana9795f82005-03-24 04:41:43 +00001532 return Result;
1533
1534 case ISD::ZERO_EXTEND_INREG:
1535 Tmp1 = SelectExpr(N.getOperand(0));
1536 switch(cast<MVTSDNode>(Node)->getExtraValueType()) {
Nate Begeman9db505c2005-03-28 19:36:43 +00001537 default: Node->dump(); assert(0 && "Unhandled ZERO_EXTEND type"); break;
Nate Begemana9795f82005-03-24 04:41:43 +00001538 case MVT::i16: Tmp2 = 16; break;
1539 case MVT::i8: Tmp2 = 24; break;
1540 case MVT::i1: Tmp2 = 31; break;
1541 }
Nate Begeman33162522005-03-29 21:54:38 +00001542 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(0).addImm(Tmp2)
1543 .addImm(31);
Nate Begemana9795f82005-03-24 04:41:43 +00001544 return Result;
1545
Nate Begemana9795f82005-03-24 04:41:43 +00001546 case ISD::CopyFromReg:
1547 if (Result == 1)
1548 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1549 Tmp1 = dyn_cast<RegSDNode>(Node)->getReg();
1550 BuildMI(BB, PPC::OR, 2, Result).addReg(Tmp1).addReg(Tmp1);
1551 return Result;
1552
1553 case ISD::SHL:
Nate Begeman5e966612005-03-24 06:28:42 +00001554 Tmp1 = SelectExpr(N.getOperand(0));
1555 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1556 Tmp2 = CN->getValue() & 0x1F;
Nate Begeman33162522005-03-29 21:54:38 +00001557 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(Tmp2).addImm(0)
Nate Begeman5e966612005-03-24 06:28:42 +00001558 .addImm(31-Tmp2);
1559 } else {
1560 Tmp2 = SelectExpr(N.getOperand(1));
1561 BuildMI(BB, PPC::SLW, 2, Result).addReg(Tmp1).addReg(Tmp2);
1562 }
1563 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001564
Nate Begeman5e966612005-03-24 06:28:42 +00001565 case ISD::SRL:
1566 Tmp1 = SelectExpr(N.getOperand(0));
1567 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1568 Tmp2 = CN->getValue() & 0x1F;
Nate Begeman33162522005-03-29 21:54:38 +00001569 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(32-Tmp2)
Nate Begeman5e966612005-03-24 06:28:42 +00001570 .addImm(Tmp2).addImm(31);
1571 } else {
1572 Tmp2 = SelectExpr(N.getOperand(1));
1573 BuildMI(BB, PPC::SRW, 2, Result).addReg(Tmp1).addReg(Tmp2);
1574 }
1575 return Result;
1576
1577 case ISD::SRA:
1578 Tmp1 = SelectExpr(N.getOperand(0));
1579 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1580 Tmp2 = CN->getValue() & 0x1F;
1581 BuildMI(BB, PPC::SRAWI, 2, Result).addReg(Tmp1).addImm(Tmp2);
1582 } else {
1583 Tmp2 = SelectExpr(N.getOperand(1));
1584 BuildMI(BB, PPC::SRAW, 2, Result).addReg(Tmp1).addReg(Tmp2);
1585 }
1586 return Result;
1587
Nate Begemana9795f82005-03-24 04:41:43 +00001588 case ISD::ADD:
1589 assert (DestType == MVT::i32 && "Only do arithmetic on i32s!");
1590 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman439b4442005-04-05 04:22:58 +00001591 switch(getImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
Nate Begemana9795f82005-03-24 04:41:43 +00001592 default: assert(0 && "unhandled result code");
1593 case 0: // No immediate
1594 Tmp2 = SelectExpr(N.getOperand(1));
1595 BuildMI(BB, PPC::ADD, 2, Result).addReg(Tmp1).addReg(Tmp2);
1596 break;
1597 case 1: // Low immediate
1598 BuildMI(BB, PPC::ADDI, 2, Result).addReg(Tmp1).addSImm(Tmp2);
1599 break;
1600 case 2: // Shifted immediate
1601 BuildMI(BB, PPC::ADDIS, 2, Result).addReg(Tmp1).addSImm(Tmp2);
1602 break;
1603 }
1604 return Result;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001605
Nate Begemana9795f82005-03-24 04:41:43 +00001606 case ISD::AND:
Nate Begeman7ddecb42005-04-06 23:51:40 +00001607 Tmp1 = SelectExpr(N.getOperand(0));
1608 // FIXME: should add check in getImmediateForOpcode to return a value
1609 // indicating the immediate is a run of set bits so we can emit a bitfield
1610 // clear with RLWINM instead.
1611 switch(getImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
1612 default: assert(0 && "unhandled result code");
1613 case 0: // No immediate
1614 Tmp2 = SelectExpr(N.getOperand(1));
1615 BuildMI(BB, PPC::AND, 2, Result).addReg(Tmp1).addReg(Tmp2);
1616 break;
1617 case 1: // Low immediate
1618 BuildMI(BB, PPC::ANDIo, 2, Result).addReg(Tmp1).addImm(Tmp2);
1619 break;
1620 case 2: // Shifted immediate
1621 BuildMI(BB, PPC::ANDISo, 2, Result).addReg(Tmp1).addImm(Tmp2);
1622 break;
1623 }
1624 return Result;
1625
Nate Begemana9795f82005-03-24 04:41:43 +00001626 case ISD::OR:
Nate Begeman7ddecb42005-04-06 23:51:40 +00001627 if (SelectBitfieldInsert(N, Result))
1628 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001629 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman439b4442005-04-05 04:22:58 +00001630 switch(getImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
Nate Begemana9795f82005-03-24 04:41:43 +00001631 default: assert(0 && "unhandled result code");
1632 case 0: // No immediate
1633 Tmp2 = SelectExpr(N.getOperand(1));
Nate Begeman7ddecb42005-04-06 23:51:40 +00001634 BuildMI(BB, PPC::OR, 2, Result).addReg(Tmp1).addReg(Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +00001635 break;
1636 case 1: // Low immediate
Nate Begeman7ddecb42005-04-06 23:51:40 +00001637 BuildMI(BB, PPC::ORI, 2, Result).addReg(Tmp1).addImm(Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +00001638 break;
1639 case 2: // Shifted immediate
Nate Begeman7ddecb42005-04-06 23:51:40 +00001640 BuildMI(BB, PPC::ORIS, 2, Result).addReg(Tmp1).addImm(Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +00001641 break;
1642 }
1643 return Result;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001644
Nate Begemanaa73a9f2005-04-03 11:20:20 +00001645 case ISD::XOR: {
1646 // Check for EQV: xor, (xor a, -1), b
1647 if (N.getOperand(0).getOpcode() == ISD::XOR &&
1648 N.getOperand(0).getOperand(1).getOpcode() == ISD::Constant &&
1649 cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->isAllOnesValue()) {
1650 ++NotLogic;
1651 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1652 Tmp2 = SelectExpr(N.getOperand(1));
1653 BuildMI(BB, PPC::EQV, 2, Result).addReg(Tmp1).addReg(Tmp2);
1654 return Result;
1655 }
1656 // Check for NOT, NOR, and NAND: xor (copy, or, and), -1
1657 if (N.getOperand(1).getOpcode() == ISD::Constant &&
1658 cast<ConstantSDNode>(N.getOperand(1))->isAllOnesValue()) {
1659 ++NotLogic;
1660 switch(N.getOperand(0).getOpcode()) {
1661 case ISD::OR:
1662 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1663 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1664 BuildMI(BB, PPC::NOR, 2, Result).addReg(Tmp1).addReg(Tmp2);
1665 break;
1666 case ISD::AND:
1667 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1668 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1669 BuildMI(BB, PPC::NAND, 2, Result).addReg(Tmp1).addReg(Tmp2);
1670 break;
1671 default:
1672 Tmp1 = SelectExpr(N.getOperand(0));
1673 BuildMI(BB, PPC::NOR, 2, Result).addReg(Tmp1).addReg(Tmp1);
1674 break;
1675 }
1676 return Result;
1677 }
1678 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman439b4442005-04-05 04:22:58 +00001679 switch(getImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
Nate Begemanaa73a9f2005-04-03 11:20:20 +00001680 default: assert(0 && "unhandled result code");
1681 case 0: // No immediate
1682 Tmp2 = SelectExpr(N.getOperand(1));
1683 BuildMI(BB, PPC::XOR, 2, Result).addReg(Tmp1).addReg(Tmp2);
1684 break;
1685 case 1: // Low immediate
1686 BuildMI(BB, PPC::XORI, 2, Result).addReg(Tmp1).addImm(Tmp2);
1687 break;
1688 case 2: // Shifted immediate
1689 BuildMI(BB, PPC::XORIS, 2, Result).addReg(Tmp1).addImm(Tmp2);
1690 break;
1691 }
1692 return Result;
1693 }
1694
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001695 case ISD::SUB:
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001696 Tmp2 = SelectExpr(N.getOperand(1));
Nate Begeman439b4442005-04-05 04:22:58 +00001697 if (1 == getImmediateForOpcode(N.getOperand(0), opcode, Tmp1))
Nate Begeman27523a12005-04-02 00:42:16 +00001698 BuildMI(BB, PPC::SUBFIC, 2, Result).addReg(Tmp2).addSImm(Tmp1);
1699 else {
1700 Tmp1 = SelectExpr(N.getOperand(0));
1701 BuildMI(BB, PPC::SUBF, 2, Result).addReg(Tmp2).addReg(Tmp1);
1702 }
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001703 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001704
Nate Begeman5e966612005-03-24 06:28:42 +00001705 case ISD::MUL:
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001706 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman439b4442005-04-05 04:22:58 +00001707 if (1 == getImmediateForOpcode(N.getOperand(1), opcode, Tmp2))
Nate Begeman307e7442005-03-26 01:28:53 +00001708 BuildMI(BB, PPC::MULLI, 2, Result).addReg(Tmp1).addSImm(Tmp2);
1709 else {
1710 Tmp2 = SelectExpr(N.getOperand(1));
1711 BuildMI(BB, PPC::MULLW, 2, Result).addReg(Tmp1).addReg(Tmp2);
1712 }
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001713 return Result;
1714
Nate Begeman815d6da2005-04-06 00:25:27 +00001715 case ISD::MULHS:
1716 case ISD::MULHU:
1717 Tmp1 = SelectExpr(N.getOperand(0));
1718 Tmp2 = SelectExpr(N.getOperand(1));
1719 Opc = (ISD::MULHU == opcode) ? PPC::MULHWU : PPC::MULHW;
1720 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1721 return Result;
1722
Nate Begemanf3d08f32005-03-29 00:03:27 +00001723 case ISD::SDIV:
1724 case ISD::UDIV:
Nate Begeman815d6da2005-04-06 00:25:27 +00001725 switch (getImmediateForOpcode(N.getOperand(1), opcode, Tmp3)) {
1726 default: break;
1727 // If this is an sdiv by a power of two, we can use an srawi/addze pair.
1728 case 3:
Nate Begeman80196b12005-04-05 00:15:08 +00001729 Tmp1 = MakeReg(MVT::i32);
1730 Tmp2 = SelectExpr(N.getOperand(0));
1731 BuildMI(BB, PPC::SRAWI, 2, Tmp1).addReg(Tmp2).addImm(Tmp3);
1732 BuildMI(BB, PPC::ADDZE, 1, Result).addReg(Tmp1);
1733 return Result;
Nate Begeman815d6da2005-04-06 00:25:27 +00001734 // If this is a divide by constant, we can emit code using some magic
1735 // constants to implement it as a multiply instead.
Nate Begeman27b4c232005-04-06 06:44:57 +00001736 case 4:
1737 ExprMap.erase(N);
1738 if (opcode == ISD::SDIV)
1739 return SelectExpr(BuildSDIVSequence(N));
1740 else
1741 return SelectExpr(BuildUDIVSequence(N));
Nate Begeman80196b12005-04-05 00:15:08 +00001742 }
Nate Begemanf3d08f32005-03-29 00:03:27 +00001743 Tmp1 = SelectExpr(N.getOperand(0));
1744 Tmp2 = SelectExpr(N.getOperand(1));
1745 Opc = (ISD::UDIV == opcode) ? PPC::DIVWU : PPC::DIVW;
1746 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1747 return Result;
1748
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001749 case ISD::ADD_PARTS:
Nate Begemanca12a2b2005-03-28 22:28:37 +00001750 case ISD::SUB_PARTS: {
1751 assert(N.getNumOperands() == 4 && N.getValueType() == MVT::i32 &&
1752 "Not an i64 add/sub!");
1753 // Emit all of the operands.
1754 std::vector<unsigned> InVals;
1755 for (unsigned i = 0, e = N.getNumOperands(); i != e; ++i)
1756 InVals.push_back(SelectExpr(N.getOperand(i)));
1757 if (N.getOpcode() == ISD::ADD_PARTS) {
Nate Begeman27eeb002005-04-02 05:59:34 +00001758 BuildMI(BB, PPC::ADDC, 2, Result).addReg(InVals[0]).addReg(InVals[2]);
1759 BuildMI(BB, PPC::ADDE, 2, Result+1).addReg(InVals[1]).addReg(InVals[3]);
Nate Begemanca12a2b2005-03-28 22:28:37 +00001760 } else {
Nate Begeman27eeb002005-04-02 05:59:34 +00001761 BuildMI(BB, PPC::SUBFC, 2, Result).addReg(InVals[2]).addReg(InVals[0]);
1762 BuildMI(BB, PPC::SUBFE, 2, Result+1).addReg(InVals[3]).addReg(InVals[1]);
1763 }
1764 return Result+N.ResNo;
1765 }
1766
1767 case ISD::SHL_PARTS:
1768 case ISD::SRA_PARTS:
1769 case ISD::SRL_PARTS: {
1770 assert(N.getNumOperands() == 3 && N.getValueType() == MVT::i32 &&
1771 "Not an i64 shift!");
1772 unsigned ShiftOpLo = SelectExpr(N.getOperand(0));
1773 unsigned ShiftOpHi = SelectExpr(N.getOperand(1));
1774 unsigned SHReg = SelectExpr(N.getOperand(2));
1775 Tmp1 = MakeReg(MVT::i32);
1776 Tmp2 = MakeReg(MVT::i32);
1777 Tmp3 = MakeReg(MVT::i32);
1778 unsigned Tmp4 = MakeReg(MVT::i32);
1779 unsigned Tmp5 = MakeReg(MVT::i32);
1780 unsigned Tmp6 = MakeReg(MVT::i32);
1781 BuildMI(BB, PPC::SUBFIC, 2, Tmp1).addReg(SHReg).addSImm(32);
1782 if (ISD::SHL_PARTS == opcode) {
1783 BuildMI(BB, PPC::SLW, 2, Tmp2).addReg(ShiftOpHi).addReg(SHReg);
1784 BuildMI(BB, PPC::SRW, 2, Tmp3).addReg(ShiftOpLo).addReg(Tmp1);
1785 BuildMI(BB, PPC::OR, 2, Tmp4).addReg(Tmp2).addReg(Tmp3);
1786 BuildMI(BB, PPC::ADDI, 2, Tmp5).addReg(SHReg).addSImm(-32);
Nate Begemanfa554702005-04-03 22:13:27 +00001787 BuildMI(BB, PPC::SLW, 2, Tmp6).addReg(ShiftOpLo).addReg(Tmp5);
Nate Begeman27eeb002005-04-02 05:59:34 +00001788 BuildMI(BB, PPC::OR, 2, Result+1).addReg(Tmp4).addReg(Tmp6);
1789 BuildMI(BB, PPC::SLW, 2, Result).addReg(ShiftOpLo).addReg(SHReg);
1790 } else if (ISD::SRL_PARTS == opcode) {
1791 BuildMI(BB, PPC::SRW, 2, Tmp2).addReg(ShiftOpLo).addReg(SHReg);
1792 BuildMI(BB, PPC::SLW, 2, Tmp3).addReg(ShiftOpHi).addReg(Tmp1);
1793 BuildMI(BB, PPC::OR, 2, Tmp4).addReg(Tmp2).addReg(Tmp3);
1794 BuildMI(BB, PPC::ADDI, 2, Tmp5).addReg(SHReg).addSImm(-32);
1795 BuildMI(BB, PPC::SRW, 2, Tmp6).addReg(ShiftOpHi).addReg(Tmp5);
1796 BuildMI(BB, PPC::OR, 2, Result).addReg(Tmp4).addReg(Tmp6);
1797 BuildMI(BB, PPC::SRW, 2, Result+1).addReg(ShiftOpHi).addReg(SHReg);
1798 } else {
1799 MachineBasicBlock *TmpMBB = new MachineBasicBlock(BB->getBasicBlock());
1800 MachineBasicBlock *PhiMBB = new MachineBasicBlock(BB->getBasicBlock());
1801 MachineBasicBlock *OldMBB = BB;
1802 MachineFunction *F = BB->getParent();
1803 ilist<MachineBasicBlock>::iterator It = BB; ++It;
1804 F->getBasicBlockList().insert(It, TmpMBB);
1805 F->getBasicBlockList().insert(It, PhiMBB);
1806 BB->addSuccessor(TmpMBB);
1807 BB->addSuccessor(PhiMBB);
1808 BuildMI(BB, PPC::SRW, 2, Tmp2).addReg(ShiftOpLo).addReg(SHReg);
1809 BuildMI(BB, PPC::SLW, 2, Tmp3).addReg(ShiftOpHi).addReg(Tmp1);
1810 BuildMI(BB, PPC::OR, 2, Tmp4).addReg(Tmp2).addReg(Tmp3);
1811 BuildMI(BB, PPC::ADDICo, 2, Tmp5).addReg(SHReg).addSImm(-32);
1812 BuildMI(BB, PPC::SRAW, 2, Tmp6).addReg(ShiftOpHi).addReg(Tmp5);
1813 BuildMI(BB, PPC::SRAW, 2, Result+1).addReg(ShiftOpHi).addReg(SHReg);
1814 BuildMI(BB, PPC::BLE, 2).addReg(PPC::CR0).addMBB(PhiMBB);
1815 // Select correct least significant half if the shift amount > 32
1816 BB = TmpMBB;
1817 unsigned Tmp7 = MakeReg(MVT::i32);
1818 BuildMI(BB, PPC::OR, 2, Tmp7).addReg(Tmp6).addReg(Tmp6);
1819 TmpMBB->addSuccessor(PhiMBB);
1820 BB = PhiMBB;
1821 BuildMI(BB, PPC::PHI, 4, Result).addReg(Tmp4).addMBB(OldMBB)
1822 .addReg(Tmp7).addMBB(TmpMBB);
Nate Begemanca12a2b2005-03-28 22:28:37 +00001823 }
1824 return Result+N.ResNo;
1825 }
1826
Nate Begemana9795f82005-03-24 04:41:43 +00001827 case ISD::FP_TO_UINT:
Nate Begeman6b559972005-04-01 02:59:27 +00001828 case ISD::FP_TO_SINT: {
1829 bool U = (ISD::FP_TO_UINT == opcode);
1830 Tmp1 = SelectExpr(N.getOperand(0));
1831 if (!U) {
1832 Tmp2 = MakeReg(MVT::f64);
1833 BuildMI(BB, PPC::FCTIWZ, 1, Tmp2).addReg(Tmp1);
1834 int FrameIdx = BB->getParent()->getFrameInfo()->CreateStackObject(8, 8);
1835 addFrameReference(BuildMI(BB, PPC::STFD, 3).addReg(Tmp2), FrameIdx);
1836 addFrameReference(BuildMI(BB, PPC::LWZ, 2, Result), FrameIdx, 4);
1837 return Result;
1838 } else {
1839 unsigned Zero = getConstDouble(0.0);
1840 unsigned MaxInt = getConstDouble((1LL << 32) - 1);
1841 unsigned Border = getConstDouble(1LL << 31);
1842 unsigned UseZero = MakeReg(MVT::f64);
1843 unsigned UseMaxInt = MakeReg(MVT::f64);
1844 unsigned UseChoice = MakeReg(MVT::f64);
1845 unsigned TmpReg = MakeReg(MVT::f64);
1846 unsigned TmpReg2 = MakeReg(MVT::f64);
1847 unsigned ConvReg = MakeReg(MVT::f64);
1848 unsigned IntTmp = MakeReg(MVT::i32);
1849 unsigned XorReg = MakeReg(MVT::i32);
1850 MachineFunction *F = BB->getParent();
1851 int FrameIdx = F->getFrameInfo()->CreateStackObject(8, 8);
1852 // Update machine-CFG edges
1853 MachineBasicBlock *XorMBB = new MachineBasicBlock(BB->getBasicBlock());
1854 MachineBasicBlock *PhiMBB = new MachineBasicBlock(BB->getBasicBlock());
1855 MachineBasicBlock *OldMBB = BB;
1856 ilist<MachineBasicBlock>::iterator It = BB; ++It;
1857 F->getBasicBlockList().insert(It, XorMBB);
1858 F->getBasicBlockList().insert(It, PhiMBB);
1859 BB->addSuccessor(XorMBB);
1860 BB->addSuccessor(PhiMBB);
1861 // Convert from floating point to unsigned 32-bit value
1862 // Use 0 if incoming value is < 0.0
1863 BuildMI(BB, PPC::FSEL, 3, UseZero).addReg(Tmp1).addReg(Tmp1).addReg(Zero);
1864 // Use 2**32 - 1 if incoming value is >= 2**32
1865 BuildMI(BB, PPC::FSUB, 2, UseMaxInt).addReg(MaxInt).addReg(Tmp1);
1866 BuildMI(BB, PPC::FSEL, 3, UseChoice).addReg(UseMaxInt).addReg(UseZero)
1867 .addReg(MaxInt);
1868 // Subtract 2**31
1869 BuildMI(BB, PPC::FSUB, 2, TmpReg).addReg(UseChoice).addReg(Border);
1870 // Use difference if >= 2**31
1871 BuildMI(BB, PPC::FCMPU, 2, PPC::CR0).addReg(UseChoice).addReg(Border);
1872 BuildMI(BB, PPC::FSEL, 3, TmpReg2).addReg(TmpReg).addReg(TmpReg)
1873 .addReg(UseChoice);
1874 // Convert to integer
1875 BuildMI(BB, PPC::FCTIWZ, 1, ConvReg).addReg(TmpReg2);
1876 addFrameReference(BuildMI(BB, PPC::STFD, 3).addReg(ConvReg), FrameIdx);
1877 addFrameReference(BuildMI(BB, PPC::LWZ, 2, IntTmp), FrameIdx, 4);
1878 BuildMI(BB, PPC::BLT, 2).addReg(PPC::CR0).addMBB(PhiMBB);
1879 BuildMI(BB, PPC::B, 1).addMBB(XorMBB);
1880
1881 // XorMBB:
1882 // add 2**31 if input was >= 2**31
1883 BB = XorMBB;
1884 BuildMI(BB, PPC::XORIS, 2, XorReg).addReg(IntTmp).addImm(0x8000);
1885 XorMBB->addSuccessor(PhiMBB);
1886
1887 // PhiMBB:
1888 // DestReg = phi [ IntTmp, OldMBB ], [ XorReg, XorMBB ]
1889 BB = PhiMBB;
1890 BuildMI(BB, PPC::PHI, 4, Result).addReg(IntTmp).addMBB(OldMBB)
1891 .addReg(XorReg).addMBB(XorMBB);
1892 return Result;
1893 }
1894 assert(0 && "Should never get here");
1895 return 0;
1896 }
Nate Begemana9795f82005-03-24 04:41:43 +00001897
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001898 case ISD::SETCC:
Nate Begeman33162522005-03-29 21:54:38 +00001899 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Node)) {
Nate Begemandffcfcc2005-04-01 00:32:34 +00001900 Opc = SelectSetCR0(N);
Nate Begeman33162522005-03-29 21:54:38 +00001901
Nate Begeman31318e42005-04-01 07:21:30 +00001902 unsigned TrueValue = MakeReg(MVT::i32);
1903 BuildMI(BB, PPC::LI, 1, TrueValue).addSImm(1);
1904 unsigned FalseValue = MakeReg(MVT::i32);
1905 BuildMI(BB, PPC::LI, 1, FalseValue).addSImm(0);
1906
Nate Begeman33162522005-03-29 21:54:38 +00001907 // Create an iterator with which to insert the MBB for copying the false
1908 // value and the MBB to hold the PHI instruction for this SetCC.
1909 MachineBasicBlock *thisMBB = BB;
1910 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1911 ilist<MachineBasicBlock>::iterator It = BB;
1912 ++It;
1913
1914 // thisMBB:
1915 // ...
1916 // cmpTY cr0, r1, r2
1917 // %TrueValue = li 1
1918 // bCC sinkMBB
Nate Begeman33162522005-03-29 21:54:38 +00001919 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
1920 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
1921 BuildMI(BB, Opc, 2).addReg(PPC::CR0).addMBB(sinkMBB);
1922 MachineFunction *F = BB->getParent();
1923 F->getBasicBlockList().insert(It, copy0MBB);
1924 F->getBasicBlockList().insert(It, sinkMBB);
1925 // Update machine-CFG edges
1926 BB->addSuccessor(copy0MBB);
1927 BB->addSuccessor(sinkMBB);
1928
1929 // copy0MBB:
1930 // %FalseValue = li 0
1931 // fallthrough
1932 BB = copy0MBB;
Nate Begeman33162522005-03-29 21:54:38 +00001933 // Update machine-CFG edges
1934 BB->addSuccessor(sinkMBB);
1935
1936 // sinkMBB:
1937 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1938 // ...
1939 BB = sinkMBB;
1940 BuildMI(BB, PPC::PHI, 4, Result).addReg(FalseValue)
1941 .addMBB(copy0MBB).addReg(TrueValue).addMBB(thisMBB);
1942 return Result;
1943 }
1944 assert(0 && "Is this legal?");
1945 return 0;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001946
Nate Begeman74747862005-03-29 22:24:51 +00001947 case ISD::SELECT: {
Chris Lattner30710192005-04-01 07:10:02 +00001948 unsigned TrueValue = SelectExpr(N.getOperand(1)); //Use if TRUE
1949 unsigned FalseValue = SelectExpr(N.getOperand(2)); //Use if FALSE
Nate Begeman6cb2e1b2005-04-01 08:57:43 +00001950 Opc = SelectSetCR0(N.getOperand(0));
Chris Lattner30710192005-04-01 07:10:02 +00001951
Nate Begeman74747862005-03-29 22:24:51 +00001952 // Create an iterator with which to insert the MBB for copying the false
1953 // value and the MBB to hold the PHI instruction for this SetCC.
1954 MachineBasicBlock *thisMBB = BB;
1955 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1956 ilist<MachineBasicBlock>::iterator It = BB;
1957 ++It;
1958
1959 // thisMBB:
1960 // ...
1961 // TrueVal = ...
1962 // cmpTY cr0, r1, r2
1963 // bCC copy1MBB
1964 // fallthrough --> copy0MBB
Nate Begeman74747862005-03-29 22:24:51 +00001965 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
1966 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
Nate Begeman3e897162005-03-31 23:55:40 +00001967 BuildMI(BB, Opc, 2).addReg(PPC::CR0).addMBB(sinkMBB);
Nate Begeman74747862005-03-29 22:24:51 +00001968 MachineFunction *F = BB->getParent();
1969 F->getBasicBlockList().insert(It, copy0MBB);
1970 F->getBasicBlockList().insert(It, sinkMBB);
1971 // Update machine-CFG edges
1972 BB->addSuccessor(copy0MBB);
1973 BB->addSuccessor(sinkMBB);
1974
1975 // copy0MBB:
1976 // %FalseValue = ...
1977 // # fallthrough to sinkMBB
1978 BB = copy0MBB;
Nate Begeman74747862005-03-29 22:24:51 +00001979 // Update machine-CFG edges
1980 BB->addSuccessor(sinkMBB);
1981
1982 // sinkMBB:
1983 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1984 // ...
1985 BB = sinkMBB;
1986 BuildMI(BB, PPC::PHI, 4, Result).addReg(FalseValue)
1987 .addMBB(copy0MBB).addReg(TrueValue).addMBB(thisMBB);
1988
1989 // FIXME: Select i64?
1990 return Result;
1991 }
Nate Begemana9795f82005-03-24 04:41:43 +00001992
1993 case ISD::Constant:
1994 switch (N.getValueType()) {
1995 default: assert(0 && "Cannot use constants of this type!");
1996 case MVT::i1:
1997 BuildMI(BB, PPC::LI, 1, Result)
1998 .addSImm(!cast<ConstantSDNode>(N)->isNullValue());
1999 break;
2000 case MVT::i32:
2001 {
2002 int v = (int)cast<ConstantSDNode>(N)->getSignExtended();
2003 if (v < 32768 && v >= -32768) {
2004 BuildMI(BB, PPC::LI, 1, Result).addSImm(v);
2005 } else {
Nate Begeman5e966612005-03-24 06:28:42 +00002006 Tmp1 = MakeReg(MVT::i32);
2007 BuildMI(BB, PPC::LIS, 1, Tmp1).addSImm(v >> 16);
2008 BuildMI(BB, PPC::ORI, 2, Result).addReg(Tmp1).addImm(v & 0xFFFF);
Nate Begemana9795f82005-03-24 04:41:43 +00002009 }
2010 }
2011 }
2012 return Result;
2013 }
2014
2015 return 0;
2016}
2017
2018void ISel::Select(SDOperand N) {
2019 unsigned Tmp1, Tmp2, Opc;
2020 unsigned opcode = N.getOpcode();
2021
2022 if (!ExprMap.insert(std::make_pair(N, 1)).second)
2023 return; // Already selected.
2024
2025 SDNode *Node = N.Val;
2026
2027 switch (Node->getOpcode()) {
2028 default:
2029 Node->dump(); std::cerr << "\n";
2030 assert(0 && "Node not handled yet!");
2031 case ISD::EntryToken: return; // Noop
2032 case ISD::TokenFactor:
2033 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
2034 Select(Node->getOperand(i));
2035 return;
2036 case ISD::ADJCALLSTACKDOWN:
2037 case ISD::ADJCALLSTACKUP:
2038 Select(N.getOperand(0));
2039 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
2040 Opc = N.getOpcode() == ISD::ADJCALLSTACKDOWN ? PPC::ADJCALLSTACKDOWN :
2041 PPC::ADJCALLSTACKUP;
2042 BuildMI(BB, Opc, 1).addImm(Tmp1);
2043 return;
2044 case ISD::BR: {
2045 MachineBasicBlock *Dest =
2046 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
Nate Begemana9795f82005-03-24 04:41:43 +00002047 Select(N.getOperand(0));
2048 BuildMI(BB, PPC::B, 1).addMBB(Dest);
2049 return;
2050 }
2051 case ISD::BRCOND:
2052 SelectBranchCC(N);
2053 return;
2054 case ISD::CopyToReg:
2055 Select(N.getOperand(0));
2056 Tmp1 = SelectExpr(N.getOperand(1));
2057 Tmp2 = cast<RegSDNode>(N)->getReg();
2058
2059 if (Tmp1 != Tmp2) {
2060 if (N.getOperand(1).getValueType() == MVT::f64 ||
2061 N.getOperand(1).getValueType() == MVT::f32)
2062 BuildMI(BB, PPC::FMR, 1, Tmp2).addReg(Tmp1);
2063 else
2064 BuildMI(BB, PPC::OR, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
2065 }
2066 return;
2067 case ISD::ImplicitDef:
2068 Select(N.getOperand(0));
2069 BuildMI(BB, PPC::IMPLICIT_DEF, 0, cast<RegSDNode>(N)->getReg());
2070 return;
2071 case ISD::RET:
2072 switch (N.getNumOperands()) {
2073 default:
2074 assert(0 && "Unknown return instruction!");
2075 case 3:
2076 assert(N.getOperand(1).getValueType() == MVT::i32 &&
2077 N.getOperand(2).getValueType() == MVT::i32 &&
2078 "Unknown two-register value!");
2079 Select(N.getOperand(0));
2080 Tmp1 = SelectExpr(N.getOperand(1));
2081 Tmp2 = SelectExpr(N.getOperand(2));
Nate Begeman27523a12005-04-02 00:42:16 +00002082 BuildMI(BB, PPC::OR, 2, PPC::R3).addReg(Tmp2).addReg(Tmp2);
2083 BuildMI(BB, PPC::OR, 2, PPC::R4).addReg(Tmp1).addReg(Tmp1);
Nate Begemana9795f82005-03-24 04:41:43 +00002084 break;
2085 case 2:
2086 Select(N.getOperand(0));
2087 Tmp1 = SelectExpr(N.getOperand(1));
2088 switch (N.getOperand(1).getValueType()) {
2089 default:
2090 assert(0 && "Unknown return type!");
2091 case MVT::f64:
2092 case MVT::f32:
2093 BuildMI(BB, PPC::FMR, 1, PPC::F1).addReg(Tmp1);
2094 break;
2095 case MVT::i32:
2096 BuildMI(BB, PPC::OR, 2, PPC::R3).addReg(Tmp1).addReg(Tmp1);
2097 break;
2098 }
Nate Begeman9e3e1b52005-03-24 23:35:30 +00002099 case 1:
2100 Select(N.getOperand(0));
2101 break;
Nate Begemana9795f82005-03-24 04:41:43 +00002102 }
2103 BuildMI(BB, PPC::BLR, 0); // Just emit a 'ret' instruction
2104 return;
Nate Begemana9795f82005-03-24 04:41:43 +00002105 case ISD::TRUNCSTORE:
2106 case ISD::STORE:
2107 {
2108 SDOperand Chain = N.getOperand(0);
2109 SDOperand Value = N.getOperand(1);
2110 SDOperand Address = N.getOperand(2);
2111 Select(Chain);
2112
2113 Tmp1 = SelectExpr(Value); //value
2114
2115 if (opcode == ISD::STORE) {
2116 switch(Value.getValueType()) {
2117 default: assert(0 && "unknown Type in store");
2118 case MVT::i32: Opc = PPC::STW; break;
2119 case MVT::f64: Opc = PPC::STFD; break;
2120 case MVT::f32: Opc = PPC::STFS; break;
2121 }
2122 } else { //ISD::TRUNCSTORE
2123 switch(cast<MVTSDNode>(Node)->getExtraValueType()) {
2124 default: assert(0 && "unknown Type in store");
2125 case MVT::i1: //FIXME: DAG does not promote this load
2126 case MVT::i8: Opc = PPC::STB; break;
2127 case MVT::i16: Opc = PPC::STH; break;
2128 }
2129 }
2130
Nate Begemana7e11a42005-04-01 05:57:17 +00002131 if(Address.getOpcode() == ISD::FrameIndex)
Nate Begemana9795f82005-03-24 04:41:43 +00002132 {
Nate Begeman58f718c2005-03-30 02:23:08 +00002133 Tmp2 = cast<FrameIndexSDNode>(Address)->getIndex();
2134 addFrameReference(BuildMI(BB, Opc, 3).addReg(Tmp1), (int)Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +00002135 }
2136 else
2137 {
2138 int offset;
Nate Begeman04730362005-04-01 04:45:11 +00002139 bool idx = SelectAddr(Address, Tmp2, offset);
2140 if (idx) {
2141 Opc = IndexedOpForOp(Opc);
2142 BuildMI(BB, Opc, 3).addReg(Tmp1).addReg(Tmp2).addReg(offset);
2143 } else {
2144 BuildMI(BB, Opc, 3).addReg(Tmp1).addImm(offset).addReg(Tmp2);
2145 }
Nate Begemana9795f82005-03-24 04:41:43 +00002146 }
2147 return;
2148 }
2149 case ISD::EXTLOAD:
2150 case ISD::SEXTLOAD:
2151 case ISD::ZEXTLOAD:
2152 case ISD::LOAD:
2153 case ISD::CopyFromReg:
2154 case ISD::CALL:
2155 case ISD::DYNAMIC_STACKALLOC:
2156 ExprMap.erase(N);
2157 SelectExpr(N);
2158 return;
2159 }
2160 assert(0 && "Should not be reached!");
2161}
2162
2163
2164/// createPPC32PatternInstructionSelector - This pass converts an LLVM function
2165/// into a machine code representation using pattern matching and a machine
2166/// description file.
2167///
2168FunctionPass *llvm::createPPC32ISelPattern(TargetMachine &TM) {
2169 return new ISel(TM);
Chris Lattner246fa632005-03-24 06:16:18 +00002170}
2171