blob: aabfed87596c4d29a570f2d1486dc8a5c45f4a9f [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
Chris Lattner644db4e2005-04-09 03:22:30 +000052 setOperationAction(ISD::BRCONDTWOWAY, MVT::Other, Expand);
Nate Begeman01d05262005-03-30 01:45:43 +000053 setOperationAction(ISD::MEMMOVE, MVT::Other, Expand);
54 setOperationAction(ISD::MEMSET, MVT::Other, Expand);
55 setOperationAction(ISD::MEMCPY, MVT::Other, Expand);
56
Nate Begeman74d73452005-03-31 00:15:26 +000057 // PowerPC has an i16 but no i8 (or i1) SEXTLOAD
58 setOperationAction(ISD::SEXTLOAD, MVT::i1, Expand);
59 setOperationAction(ISD::SEXTLOAD, MVT::i8, Expand);
Nate Begeman815d6da2005-04-06 00:25:27 +000060
61 // PowerPC has no SREM/UREM instructions
62 setOperationAction(ISD::SREM, MVT::i32, Expand);
63 setOperationAction(ISD::UREM, MVT::i32, Expand);
Chris Lattner43fdea02005-04-02 05:03:24 +000064
Nate Begeman27eeb002005-04-02 05:59:34 +000065 setShiftAmountFlavor(Extend); // shl X, 32 == 0
Chris Lattnercbd06fc2005-04-07 19:41:49 +000066 setSetCCResultContents(ZeroOrOneSetCCResult);
Nate Begeman3e897162005-03-31 23:55:40 +000067 addLegalFPImmediate(+0.0); // Necessary for FSEL
68 addLegalFPImmediate(-0.0); //
69
Nate Begemana9795f82005-03-24 04:41:43 +000070 computeRegisterProperties();
71 }
72
73 /// LowerArguments - This hook must be implemented to indicate how we should
74 /// lower the arguments for the specified function, into the specified DAG.
75 virtual std::vector<SDOperand>
76 LowerArguments(Function &F, SelectionDAG &DAG);
77
78 /// LowerCallTo - This hook lowers an abstract call to a function into an
79 /// actual call.
80 virtual std::pair<SDOperand, SDOperand>
Nate Begeman307e7442005-03-26 01:28:53 +000081 LowerCallTo(SDOperand Chain, const Type *RetTy, bool isVarArg,
82 SDOperand Callee, ArgListTy &Args, SelectionDAG &DAG);
Nate Begemana9795f82005-03-24 04:41:43 +000083
84 virtual std::pair<SDOperand, SDOperand>
85 LowerVAStart(SDOperand Chain, SelectionDAG &DAG);
86
87 virtual std::pair<SDOperand,SDOperand>
88 LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
89 const Type *ArgTy, SelectionDAG &DAG);
90
91 virtual std::pair<SDOperand, SDOperand>
92 LowerFrameReturnAddress(bool isFrameAddr, SDOperand Chain, unsigned Depth,
93 SelectionDAG &DAG);
94 };
95}
96
97
98std::vector<SDOperand>
99PPC32TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
100 //
101 // add beautiful description of PPC stack frame format, or at least some docs
102 //
103 MachineFunction &MF = DAG.getMachineFunction();
104 MachineFrameInfo *MFI = MF.getFrameInfo();
105 MachineBasicBlock& BB = MF.front();
106 std::vector<SDOperand> ArgValues;
107
108 // Due to the rather complicated nature of the PowerPC ABI, rather than a
109 // fixed size array of physical args, for the sake of simplicity let the STL
110 // handle tracking them for us.
111 std::vector<unsigned> argVR, argPR, argOp;
112 unsigned ArgOffset = 24;
113 unsigned GPR_remaining = 8;
114 unsigned FPR_remaining = 13;
115 unsigned GPR_idx = 0, FPR_idx = 0;
116 static const unsigned GPR[] = {
117 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
118 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
119 };
120 static const unsigned FPR[] = {
121 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
122 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
123 };
124
125 // Add DAG nodes to load the arguments... On entry to a function on PPC,
126 // the arguments start at offset 24, although they are likely to be passed
127 // in registers.
128 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
129 SDOperand newroot, argt;
130 unsigned ObjSize;
131 bool needsLoad = false;
132 MVT::ValueType ObjectVT = getValueType(I->getType());
133
134 switch (ObjectVT) {
135 default: assert(0 && "Unhandled argument type!");
136 case MVT::i1:
137 case MVT::i8:
138 case MVT::i16:
139 case MVT::i32:
140 ObjSize = 4;
141 if (GPR_remaining > 0) {
142 BuildMI(&BB, PPC::IMPLICIT_DEF, 0, GPR[GPR_idx]);
Nate Begemanf70b5762005-03-28 23:08:54 +0000143 argt = newroot = DAG.getCopyFromReg(GPR[GPR_idx], MVT::i32,
144 DAG.getRoot());
Nate Begemana9795f82005-03-24 04:41:43 +0000145 if (ObjectVT != MVT::i32)
146 argt = DAG.getNode(ISD::TRUNCATE, ObjectVT, newroot);
Nate Begemana9795f82005-03-24 04:41:43 +0000147 } else {
148 needsLoad = true;
149 }
150 break;
Nate Begemanf7e43382005-03-26 07:46:36 +0000151 case MVT::i64: ObjSize = 8;
152 // FIXME: can split 64b load between reg/mem if it is last arg in regs
Nate Begemana9795f82005-03-24 04:41:43 +0000153 if (GPR_remaining > 1) {
154 BuildMI(&BB, PPC::IMPLICIT_DEF, 0, GPR[GPR_idx]);
155 BuildMI(&BB, PPC::IMPLICIT_DEF, 0, GPR[GPR_idx+1]);
Nate Begemanca12a2b2005-03-28 22:28:37 +0000156 // Copy the extracted halves into the virtual registers
Nate Begemanf70b5762005-03-28 23:08:54 +0000157 SDOperand argHi = DAG.getCopyFromReg(GPR[GPR_idx], MVT::i32,
158 DAG.getRoot());
159 SDOperand argLo = DAG.getCopyFromReg(GPR[GPR_idx+1], MVT::i32, argHi);
Nate Begemanca12a2b2005-03-28 22:28:37 +0000160 // Build the outgoing arg thingy
Nate Begemanf70b5762005-03-28 23:08:54 +0000161 argt = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, argLo, argHi);
162 newroot = argLo;
Nate Begemana9795f82005-03-24 04:41:43 +0000163 } else {
164 needsLoad = true;
165 }
166 break;
167 case MVT::f32: ObjSize = 4;
168 case MVT::f64: ObjSize = 8;
169 if (FPR_remaining > 0) {
170 BuildMI(&BB, PPC::IMPLICIT_DEF, 0, FPR[FPR_idx]);
Nate Begemanf70b5762005-03-28 23:08:54 +0000171 argt = newroot = DAG.getCopyFromReg(FPR[FPR_idx], ObjectVT,
172 DAG.getRoot());
Nate Begemana9795f82005-03-24 04:41:43 +0000173 --FPR_remaining;
174 ++FPR_idx;
175 } else {
176 needsLoad = true;
177 }
178 break;
179 }
180
181 // We need to load the argument to a virtual register if we determined above
182 // that we ran out of physical registers of the appropriate type
183 if (needsLoad) {
Nate Begemane5846682005-04-04 06:52:38 +0000184 unsigned SubregOffset = 0;
Nate Begemanc3e2db42005-04-04 09:09:00 +0000185 if (ObjectVT == MVT::i8 || ObjectVT == MVT::i1) SubregOffset = 3;
Nate Begemane5846682005-04-04 06:52:38 +0000186 if (ObjectVT == MVT::i16) SubregOffset = 2;
Nate Begemana9795f82005-03-24 04:41:43 +0000187 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
188 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
Nate Begemane5846682005-04-04 06:52:38 +0000189 FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN,
190 DAG.getConstant(SubregOffset, MVT::i32));
Nate Begemana9795f82005-03-24 04:41:43 +0000191 argt = newroot = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN);
192 }
193
194 // Every 4 bytes of argument space consumes one of the GPRs available for
195 // argument passing.
196 if (GPR_remaining > 0) {
197 unsigned delta = (GPR_remaining > 1 && ObjSize == 8) ? 2 : 1;
198 GPR_remaining -= delta;
199 GPR_idx += delta;
200 }
201 ArgOffset += ObjSize;
202
203 DAG.setRoot(newroot.getValue(1));
204 ArgValues.push_back(argt);
205 }
206
Nate Begemana9795f82005-03-24 04:41:43 +0000207 // If the function takes variable number of arguments, make a frame index for
208 // the start of the first vararg value... for expansion of llvm.va_start.
Nate Begemanfa554702005-04-03 22:13:27 +0000209 if (F.isVarArg()) {
Nate Begemana9795f82005-03-24 04:41:43 +0000210 VarArgsFrameIndex = MFI->CreateFixedObject(4, ArgOffset);
Nate Begemanfa554702005-04-03 22:13:27 +0000211 SDOperand FIN = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
Nate Begeman6644d4c2005-04-03 23:11:17 +0000212 // If this function is vararg, store any remaining integer argument regs
213 // to their spots on the stack so that they may be loaded by deferencing the
214 // result of va_next.
215 std::vector<SDOperand> MemOps;
216 for (; GPR_remaining > 0; --GPR_remaining, ++GPR_idx) {
217 BuildMI(&BB, PPC::IMPLICIT_DEF, 0, GPR[GPR_idx]);
218 SDOperand Val = DAG.getCopyFromReg(GPR[GPR_idx], MVT::i32, DAG.getRoot());
219 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Val.getValue(1),
220 Val, FIN);
221 MemOps.push_back(Store);
222 // Increment the address by four for the next argument to store
223 SDOperand PtrOff = DAG.getConstant(4, getPointerTy());
224 FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN, PtrOff);
225 }
226 DAG.setRoot(DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps));
Nate Begemanfa554702005-04-03 22:13:27 +0000227 }
Nate Begemana9795f82005-03-24 04:41:43 +0000228
229 return ArgValues;
230}
231
232std::pair<SDOperand, SDOperand>
233PPC32TargetLowering::LowerCallTo(SDOperand Chain,
Nate Begeman307e7442005-03-26 01:28:53 +0000234 const Type *RetTy, bool isVarArg,
235 SDOperand Callee, ArgListTy &Args, SelectionDAG &DAG) {
236 // args_to_use will accumulate outgoing args for the ISD::CALL case in
237 // SelectExpr to use to put the arguments in the appropriate registers.
Nate Begemana9795f82005-03-24 04:41:43 +0000238 std::vector<SDOperand> args_to_use;
Nate Begeman307e7442005-03-26 01:28:53 +0000239
240 // Count how many bytes are to be pushed on the stack, including the linkage
241 // area, and parameter passing area.
242 unsigned NumBytes = 24;
243
244 if (Args.empty()) {
Nate Begemana7e11a42005-04-01 05:57:17 +0000245 Chain = DAG.getNode(ISD::ADJCALLSTACKDOWN, MVT::Other, Chain,
246 DAG.getConstant(NumBytes, getPointerTy()));
Nate Begeman307e7442005-03-26 01:28:53 +0000247 } else {
248 for (unsigned i = 0, e = Args.size(); i != e; ++i)
249 switch (getValueType(Args[i].second)) {
250 default: assert(0 && "Unknown value type!");
251 case MVT::i1:
252 case MVT::i8:
253 case MVT::i16:
254 case MVT::i32:
255 case MVT::f32:
256 NumBytes += 4;
257 break;
258 case MVT::i64:
259 case MVT::f64:
260 NumBytes += 8;
261 break;
262 }
263
264 // Just to be safe, we'll always reserve the full 24 bytes of linkage area
265 // plus 32 bytes of argument space in case any called code gets funky on us.
266 if (NumBytes < 56) NumBytes = 56;
267
268 // Adjust the stack pointer for the new arguments...
269 // These operations are automatically eliminated by the prolog/epilog pass
270 Chain = DAG.getNode(ISD::ADJCALLSTACKDOWN, MVT::Other, Chain,
271 DAG.getConstant(NumBytes, getPointerTy()));
272
273 // Set up a copy of the stack pointer for use loading and storing any
274 // arguments that may not fit in the registers available for argument
275 // passing.
276 SDOperand StackPtr = DAG.getCopyFromReg(PPC::R1, MVT::i32,
277 DAG.getEntryNode());
278
279 // Figure out which arguments are going to go in registers, and which in
280 // memory. Also, if this is a vararg function, floating point operations
281 // must be stored to our stack, and loaded into integer regs as well, if
282 // any integer regs are available for argument passing.
283 unsigned ArgOffset = 24;
284 unsigned GPR_remaining = 8;
285 unsigned FPR_remaining = 13;
Nate Begeman74d73452005-03-31 00:15:26 +0000286
287 std::vector<SDOperand> MemOps;
Nate Begeman307e7442005-03-26 01:28:53 +0000288 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
289 // PtrOff will be used to store the current argument to the stack if a
290 // register cannot be found for it.
291 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
292 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
Nate Begemanf7e43382005-03-26 07:46:36 +0000293 MVT::ValueType ArgVT = getValueType(Args[i].second);
Nate Begeman307e7442005-03-26 01:28:53 +0000294
Nate Begemanf7e43382005-03-26 07:46:36 +0000295 switch (ArgVT) {
Nate Begeman307e7442005-03-26 01:28:53 +0000296 default: assert(0 && "Unexpected ValueType for argument!");
297 case MVT::i1:
298 case MVT::i8:
299 case MVT::i16:
300 // Promote the integer to 32 bits. If the input type is signed use a
301 // sign extend, otherwise use a zero extend.
302 if (Args[i].second->isSigned())
303 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
304 else
305 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
306 // FALL THROUGH
307 case MVT::i32:
308 if (GPR_remaining > 0) {
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000309 args_to_use.push_back(Args[i].first);
Nate Begeman307e7442005-03-26 01:28:53 +0000310 --GPR_remaining;
311 } else {
Nate Begeman74d73452005-03-31 00:15:26 +0000312 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
313 Args[i].first, PtrOff));
Nate Begeman307e7442005-03-26 01:28:53 +0000314 }
315 ArgOffset += 4;
316 break;
317 case MVT::i64:
Nate Begemanf7e43382005-03-26 07:46:36 +0000318 // If we have one free GPR left, we can place the upper half of the i64
319 // in it, and store the other half to the stack. If we have two or more
320 // free GPRs, then we can pass both halves of the i64 in registers.
321 if (GPR_remaining > 0) {
Nate Begemanf2622612005-03-26 02:17:46 +0000322 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
323 Args[i].first, DAG.getConstant(1, MVT::i32));
324 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
325 Args[i].first, DAG.getConstant(0, MVT::i32));
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000326 args_to_use.push_back(Hi);
Nate Begeman74d73452005-03-31 00:15:26 +0000327 --GPR_remaining;
Nate Begeman74d73452005-03-31 00:15:26 +0000328 if (GPR_remaining > 0) {
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000329 args_to_use.push_back(Lo);
Nate Begeman74d73452005-03-31 00:15:26 +0000330 --GPR_remaining;
Nate Begemanf7e43382005-03-26 07:46:36 +0000331 } else {
332 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
333 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
Nate Begeman74d73452005-03-31 00:15:26 +0000334 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
335 Lo, PtrOff));
Nate Begemanf7e43382005-03-26 07:46:36 +0000336 }
Nate Begeman307e7442005-03-26 01:28:53 +0000337 } else {
Nate Begeman74d73452005-03-31 00:15:26 +0000338 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
339 Args[i].first, PtrOff));
Nate Begeman307e7442005-03-26 01:28:53 +0000340 }
341 ArgOffset += 8;
342 break;
343 case MVT::f32:
Nate Begeman307e7442005-03-26 01:28:53 +0000344 case MVT::f64:
Nate Begemanf7e43382005-03-26 07:46:36 +0000345 if (FPR_remaining > 0) {
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000346 args_to_use.push_back(Args[i].first);
347 --FPR_remaining;
Nate Begemanf7e43382005-03-26 07:46:36 +0000348 if (isVarArg) {
Nate Begeman96fc6812005-03-31 02:05:53 +0000349 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Chain,
350 Args[i].first, PtrOff);
351 MemOps.push_back(Store);
Nate Begeman74d73452005-03-31 00:15:26 +0000352 // Float varargs are always shadowed in available integer registers
353 if (GPR_remaining > 0) {
Nate Begeman96fc6812005-03-31 02:05:53 +0000354 SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff);
Nate Begeman74d73452005-03-31 00:15:26 +0000355 MemOps.push_back(Load);
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000356 args_to_use.push_back(Load);
357 --GPR_remaining;
Nate Begeman74d73452005-03-31 00:15:26 +0000358 }
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000359 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
Nate Begeman74d73452005-03-31 00:15:26 +0000360 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
361 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
Nate Begeman96fc6812005-03-31 02:05:53 +0000362 SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff);
Nate Begeman74d73452005-03-31 00:15:26 +0000363 MemOps.push_back(Load);
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000364 args_to_use.push_back(Load);
365 --GPR_remaining;
Nate Begeman74d73452005-03-31 00:15:26 +0000366 }
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000367 } else {
368 // If we have any FPRs remaining, we may also have GPRs remaining.
369 // Args passed in FPRs consume either 1 (f32) or 2 (f64) available
370 // GPRs.
371 if (GPR_remaining > 0) {
372 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
373 --GPR_remaining;
374 }
375 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
376 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
377 --GPR_remaining;
378 }
Nate Begeman74d73452005-03-31 00:15:26 +0000379 }
Nate Begeman307e7442005-03-26 01:28:53 +0000380 } else {
Nate Begeman74d73452005-03-31 00:15:26 +0000381 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
382 Args[i].first, PtrOff));
Nate Begeman307e7442005-03-26 01:28:53 +0000383 }
Nate Begemanf7e43382005-03-26 07:46:36 +0000384 ArgOffset += (ArgVT == MVT::f32) ? 4 : 8;
Nate Begeman307e7442005-03-26 01:28:53 +0000385 break;
386 }
Nate Begemana9795f82005-03-24 04:41:43 +0000387 }
Nate Begeman74d73452005-03-31 00:15:26 +0000388 if (!MemOps.empty())
389 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps);
Nate Begemana9795f82005-03-24 04:41:43 +0000390 }
391
392 std::vector<MVT::ValueType> RetVals;
393 MVT::ValueType RetTyVT = getValueType(RetTy);
394 if (RetTyVT != MVT::isVoid)
395 RetVals.push_back(RetTyVT);
396 RetVals.push_back(MVT::Other);
397
398 SDOperand TheCall = SDOperand(DAG.getCall(RetVals,
399 Chain, Callee, args_to_use), 0);
400 Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
401 Chain = DAG.getNode(ISD::ADJCALLSTACKUP, MVT::Other, Chain,
402 DAG.getConstant(NumBytes, getPointerTy()));
403 return std::make_pair(TheCall, Chain);
404}
405
406std::pair<SDOperand, SDOperand>
407PPC32TargetLowering::LowerVAStart(SDOperand Chain, SelectionDAG &DAG) {
408 //vastart just returns the address of the VarArgsFrameIndex slot.
409 return std::make_pair(DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32), Chain);
410}
411
412std::pair<SDOperand,SDOperand> PPC32TargetLowering::
413LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
414 const Type *ArgTy, SelectionDAG &DAG) {
Nate Begemanc7b09f12005-03-25 08:34:25 +0000415 MVT::ValueType ArgVT = getValueType(ArgTy);
416 SDOperand Result;
417 if (!isVANext) {
418 Result = DAG.getLoad(ArgVT, DAG.getEntryNode(), VAList);
419 } else {
420 unsigned Amt;
421 if (ArgVT == MVT::i32 || ArgVT == MVT::f32)
422 Amt = 4;
423 else {
424 assert((ArgVT == MVT::i64 || ArgVT == MVT::f64) &&
425 "Other types should have been promoted for varargs!");
426 Amt = 8;
427 }
428 Result = DAG.getNode(ISD::ADD, VAList.getValueType(), VAList,
429 DAG.getConstant(Amt, VAList.getValueType()));
430 }
431 return std::make_pair(Result, Chain);
Nate Begemana9795f82005-03-24 04:41:43 +0000432}
433
434
435std::pair<SDOperand, SDOperand> PPC32TargetLowering::
436LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
437 SelectionDAG &DAG) {
Nate Begeman01d05262005-03-30 01:45:43 +0000438 assert(0 && "LowerFrameReturnAddress unimplemented");
Nate Begemana9795f82005-03-24 04:41:43 +0000439 abort();
440}
441
442namespace {
Nate Begemanaa73a9f2005-04-03 11:20:20 +0000443Statistic<>NotLogic("ppc-codegen", "Number of inverted logical ops");
Nate Begeman93075ec2005-04-04 23:40:36 +0000444Statistic<>FusedFP("ppc-codegen", "Number of fused fp operations");
Nate Begemana9795f82005-03-24 04:41:43 +0000445//===--------------------------------------------------------------------===//
446/// ISel - PPC32 specific code to select PPC32 machine instructions for
447/// SelectionDAG operations.
448//===--------------------------------------------------------------------===//
449class ISel : public SelectionDAGISel {
Nate Begemana9795f82005-03-24 04:41:43 +0000450 PPC32TargetLowering PPC32Lowering;
Nate Begeman815d6da2005-04-06 00:25:27 +0000451 SelectionDAG *ISelDAG; // Hack to support us having a dag->dag transform
452 // for sdiv and udiv until it is put into the future
453 // dag combiner.
Nate Begemana9795f82005-03-24 04:41:43 +0000454
455 /// ExprMap - As shared expressions are codegen'd, we keep track of which
456 /// vreg the value is produced in, so we only emit one copy of each compiled
457 /// tree.
458 std::map<SDOperand, unsigned> ExprMap;
Nate Begemanc7b09f12005-03-25 08:34:25 +0000459
460 unsigned GlobalBaseReg;
461 bool GlobalBaseInitialized;
Nate Begemana9795f82005-03-24 04:41:43 +0000462
463public:
Nate Begeman815d6da2005-04-06 00:25:27 +0000464 ISel(TargetMachine &TM) : SelectionDAGISel(PPC32Lowering), PPC32Lowering(TM),
465 ISelDAG(0) {}
Nate Begemana9795f82005-03-24 04:41:43 +0000466
Nate Begemanc7b09f12005-03-25 08:34:25 +0000467 /// runOnFunction - Override this function in order to reset our per-function
468 /// variables.
469 virtual bool runOnFunction(Function &Fn) {
470 // Make sure we re-emit a set of the global base reg if necessary
471 GlobalBaseInitialized = false;
472 return SelectionDAGISel::runOnFunction(Fn);
473 }
474
Nate Begemana9795f82005-03-24 04:41:43 +0000475 /// InstructionSelectBasicBlock - This callback is invoked by
476 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
477 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG) {
478 DEBUG(BB->dump());
479 // Codegen the basic block.
Nate Begeman815d6da2005-04-06 00:25:27 +0000480 ISelDAG = &DAG;
Nate Begemana9795f82005-03-24 04:41:43 +0000481 Select(DAG.getRoot());
482
483 // Clear state used for selection.
484 ExprMap.clear();
Nate Begeman815d6da2005-04-06 00:25:27 +0000485 ISelDAG = 0;
Nate Begemana9795f82005-03-24 04:41:43 +0000486 }
Nate Begeman815d6da2005-04-06 00:25:27 +0000487
488 // dag -> dag expanders for integer divide by constant
489 SDOperand BuildSDIVSequence(SDOperand N);
490 SDOperand BuildUDIVSequence(SDOperand N);
Nate Begemana9795f82005-03-24 04:41:43 +0000491
Nate Begemandffcfcc2005-04-01 00:32:34 +0000492 unsigned getGlobalBaseReg();
Nate Begeman6b559972005-04-01 02:59:27 +0000493 unsigned getConstDouble(double floatVal, unsigned Result);
Nate Begeman7ddecb42005-04-06 23:51:40 +0000494 bool SelectBitfieldInsert(SDOperand OR, unsigned Result);
Nate Begemandffcfcc2005-04-01 00:32:34 +0000495 unsigned SelectSetCR0(SDOperand CC);
Nate Begemana9795f82005-03-24 04:41:43 +0000496 unsigned SelectExpr(SDOperand N);
497 unsigned SelectExprFP(SDOperand N, unsigned Result);
498 void Select(SDOperand N);
499
Nate Begeman04730362005-04-01 04:45:11 +0000500 bool SelectAddr(SDOperand N, unsigned& Reg, int& offset);
Nate Begemana9795f82005-03-24 04:41:43 +0000501 void SelectBranchCC(SDOperand N);
502};
503
Nate Begeman80196b12005-04-05 00:15:08 +0000504/// ExactLog2 - This function solves for (Val == 1 << (N-1)) and returns N. It
505/// returns zero when the input is not exactly a power of two.
506static unsigned ExactLog2(unsigned Val) {
507 if (Val == 0 || (Val & (Val-1))) return 0;
508 unsigned Count = 0;
509 while (Val != 1) {
510 Val >>= 1;
511 ++Count;
512 }
513 return Count;
514}
515
Nate Begeman7ddecb42005-04-06 23:51:40 +0000516// IsRunOfOnes - returns true if Val consists of one contiguous run of 1's with
517// any number of 0's on either side. the 1's are allowed to wrap from LSB to
518// MSB. so 0x000FFF0, 0x0000FFFF, and 0xFF0000FF are all runs. 0x0F0F0000 is
519// not, since all 1's are not contiguous.
520static bool IsRunOfOnes(unsigned Val, unsigned &MB, unsigned &ME) {
521 bool isRun = true;
522 MB = 0;
523 ME = 0;
524
525 // look for first set bit
526 int i = 0;
527 for (; i < 32; i++) {
528 if ((Val & (1 << (31 - i))) != 0) {
529 MB = i;
530 ME = i;
531 break;
532 }
533 }
534
535 // look for last set bit
536 for (; i < 32; i++) {
537 if ((Val & (1 << (31 - i))) == 0)
538 break;
539 ME = i;
540 }
541
542 // look for next set bit
543 for (; i < 32; i++) {
544 if ((Val & (1 << (31 - i))) != 0)
545 break;
546 }
547
548 // if we exhausted all the bits, we found a match at this point for 0*1*0*
549 if (i == 32)
550 return true;
551
552 // since we just encountered more 1's, if it doesn't wrap around to the
553 // most significant bit of the word, then we did not find a match to 1*0*1* so
554 // exit.
555 if (MB != 0)
556 return false;
557
558 // look for last set bit
559 for (MB = i; i < 32; i++) {
560 if ((Val & (1 << (31 - i))) == 0)
561 break;
562 }
563
564 // if we exhausted all the bits, then we found a match for 1*0*1*, otherwise,
565 // the value is not a run of ones.
566 if (i == 32)
567 return true;
568 return false;
569}
570
Nate Begeman439b4442005-04-05 04:22:58 +0000571/// getImmediateForOpcode - This method returns a value indicating whether
Nate Begemana9795f82005-03-24 04:41:43 +0000572/// the ConstantSDNode N can be used as an immediate to Opcode. The return
573/// values are either 0, 1 or 2. 0 indicates that either N is not a
574/// ConstantSDNode, or is not suitable for use by that opcode. A return value
575/// of 1 indicates that the constant may be used in normal immediate form. A
576/// return value of 2 indicates that the constant may be used in shifted
Nate Begeman439b4442005-04-05 04:22:58 +0000577/// immediate form. A return value of 3 indicates that log base 2 of the
Nate Begeman815d6da2005-04-06 00:25:27 +0000578/// constant may be used. A return value of 4 indicates that the constant is
579/// suitable for conversion into a magic number for integer division.
Nate Begemana9795f82005-03-24 04:41:43 +0000580///
Nate Begeman439b4442005-04-05 04:22:58 +0000581static unsigned getImmediateForOpcode(SDOperand N, unsigned Opcode,
582 unsigned& Imm, bool U = false) {
Nate Begemana9795f82005-03-24 04:41:43 +0000583 if (N.getOpcode() != ISD::Constant) return 0;
584
585 int v = (int)cast<ConstantSDNode>(N)->getSignExtended();
586
587 switch(Opcode) {
588 default: return 0;
589 case ISD::ADD:
590 if (v <= 32767 && v >= -32768) { Imm = v & 0xFFFF; return 1; }
591 if ((v & 0x0000FFFF) == 0) { Imm = v >> 16; return 2; }
592 break;
593 case ISD::AND:
594 case ISD::XOR:
595 case ISD::OR:
596 if (v >= 0 && v <= 65535) { Imm = v & 0xFFFF; return 1; }
597 if ((v & 0x0000FFFF) == 0) { Imm = v >> 16; return 2; }
598 break;
Nate Begeman307e7442005-03-26 01:28:53 +0000599 case ISD::MUL:
Nate Begeman27523a12005-04-02 00:42:16 +0000600 case ISD::SUB:
Nate Begeman307e7442005-03-26 01:28:53 +0000601 if (v <= 32767 && v >= -32768) { Imm = v & 0xFFFF; return 1; }
602 break;
Nate Begeman3e897162005-03-31 23:55:40 +0000603 case ISD::SETCC:
604 if (U && (v >= 0 && v <= 65535)) { Imm = v & 0xFFFF; return 1; }
605 if (!U && (v <= 32767 && v >= -32768)) { Imm = v & 0xFFFF; return 1; }
606 break;
Nate Begeman80196b12005-04-05 00:15:08 +0000607 case ISD::SDIV:
Nate Begeman439b4442005-04-05 04:22:58 +0000608 if ((Imm = ExactLog2(v))) { return 3; }
Nate Begeman815d6da2005-04-06 00:25:27 +0000609 if (v <= -2 || v >= 2) { return 4; }
610 break;
611 case ISD::UDIV:
Nate Begeman27b4c232005-04-06 06:44:57 +0000612 if (v > 1) { return 4; }
Nate Begeman80196b12005-04-05 00:15:08 +0000613 break;
Nate Begemana9795f82005-03-24 04:41:43 +0000614 }
615 return 0;
616}
Nate Begeman3e897162005-03-31 23:55:40 +0000617
618/// getBCCForSetCC - Returns the PowerPC condition branch mnemonic corresponding
619/// to Condition. If the Condition is unordered or unsigned, the bool argument
620/// U is set to true, otherwise it is set to false.
621static unsigned getBCCForSetCC(unsigned Condition, bool& U) {
622 U = false;
623 switch (Condition) {
624 default: assert(0 && "Unknown condition!"); abort();
625 case ISD::SETEQ: return PPC::BEQ;
626 case ISD::SETNE: return PPC::BNE;
627 case ISD::SETULT: U = true;
628 case ISD::SETLT: return PPC::BLT;
629 case ISD::SETULE: U = true;
630 case ISD::SETLE: return PPC::BLE;
631 case ISD::SETUGT: U = true;
632 case ISD::SETGT: return PPC::BGT;
633 case ISD::SETUGE: U = true;
634 case ISD::SETGE: return PPC::BGE;
635 }
Nate Begeman04730362005-04-01 04:45:11 +0000636 return 0;
637}
638
639/// IndexedOpForOp - Return the indexed variant for each of the PowerPC load
640/// and store immediate instructions.
641static unsigned IndexedOpForOp(unsigned Opcode) {
642 switch(Opcode) {
643 default: assert(0 && "Unknown opcode!"); abort();
644 case PPC::LBZ: return PPC::LBZX; case PPC::STB: return PPC::STBX;
645 case PPC::LHZ: return PPC::LHZX; case PPC::STH: return PPC::STHX;
646 case PPC::LHA: return PPC::LHAX; case PPC::STW: return PPC::STWX;
647 case PPC::LWZ: return PPC::LWZX; case PPC::STFS: return PPC::STFSX;
648 case PPC::LFS: return PPC::LFSX; case PPC::STFD: return PPC::STFDX;
649 case PPC::LFD: return PPC::LFDX;
650 }
651 return 0;
Nate Begeman3e897162005-03-31 23:55:40 +0000652}
Nate Begeman815d6da2005-04-06 00:25:27 +0000653
654// Structure used to return the necessary information to codegen an SDIV as
655// a multiply.
656struct ms {
657 int m; // magic number
658 int s; // shift amount
659};
660
661struct mu {
662 unsigned int m; // magic number
663 int a; // add indicator
664 int s; // shift amount
665};
666
667/// magic - calculate the magic numbers required to codegen an integer sdiv as
668/// a sequence of multiply and shifts. Requires that the divisor not be 0, 1,
669/// or -1.
670static struct ms magic(int d) {
671 int p;
672 unsigned int ad, anc, delta, q1, r1, q2, r2, t;
673 const unsigned int two31 = 2147483648U; // 2^31
674 struct ms mag;
675
676 ad = abs(d);
677 t = two31 + ((unsigned int)d >> 31);
678 anc = t - 1 - t%ad; // absolute value of nc
679 p = 31; // initialize p
680 q1 = two31/anc; // initialize q1 = 2p/abs(nc)
681 r1 = two31 - q1*anc; // initialize r1 = rem(2p,abs(nc))
682 q2 = two31/ad; // initialize q2 = 2p/abs(d)
683 r2 = two31 - q2*ad; // initialize r2 = rem(2p,abs(d))
684 do {
685 p = p + 1;
686 q1 = 2*q1; // update q1 = 2p/abs(nc)
687 r1 = 2*r1; // update r1 = rem(2p/abs(nc))
688 if (r1 >= anc) { // must be unsigned comparison
689 q1 = q1 + 1;
690 r1 = r1 - anc;
691 }
692 q2 = 2*q2; // update q2 = 2p/abs(d)
693 r2 = 2*r2; // update r2 = rem(2p/abs(d))
694 if (r2 >= ad) { // must be unsigned comparison
695 q2 = q2 + 1;
696 r2 = r2 - ad;
697 }
698 delta = ad - r2;
699 } while (q1 < delta || (q1 == delta && r1 == 0));
700
701 mag.m = q2 + 1;
702 if (d < 0) mag.m = -mag.m; // resulting magic number
703 mag.s = p - 32; // resulting shift
704 return mag;
705}
706
707/// magicu - calculate the magic numbers required to codegen an integer udiv as
708/// a sequence of multiply, add and shifts. Requires that the divisor not be 0.
709static struct mu magicu(unsigned d)
710{
711 int p;
712 unsigned int nc, delta, q1, r1, q2, r2;
713 struct mu magu;
714 magu.a = 0; // initialize "add" indicator
715 nc = - 1 - (-d)%d;
716 p = 31; // initialize p
717 q1 = 0x80000000/nc; // initialize q1 = 2p/nc
718 r1 = 0x80000000 - q1*nc; // initialize r1 = rem(2p,nc)
719 q2 = 0x7FFFFFFF/d; // initialize q2 = (2p-1)/d
720 r2 = 0x7FFFFFFF - q2*d; // initialize r2 = rem((2p-1),d)
721 do {
722 p = p + 1;
723 if (r1 >= nc - r1 ) {
724 q1 = 2*q1 + 1; // update q1
725 r1 = 2*r1 - nc; // update r1
726 }
727 else {
728 q1 = 2*q1; // update q1
729 r1 = 2*r1; // update r1
730 }
731 if (r2 + 1 >= d - r2) {
732 if (q2 >= 0x7FFFFFFF) magu.a = 1;
733 q2 = 2*q2 + 1; // update q2
734 r2 = 2*r2 + 1 - d; // update r2
735 }
736 else {
737 if (q2 >= 0x80000000) magu.a = 1;
738 q2 = 2*q2; // update q2
739 r2 = 2*r2 + 1; // update r2
740 }
741 delta = d - 1 - r2;
742 } while (p < 64 && (q1 < delta || (q1 == delta && r1 == 0)));
743 magu.m = q2 + 1; // resulting magic number
744 magu.s = p - 32; // resulting shift
745 return magu;
746}
747}
748
749/// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
750/// return a DAG expression to select that will generate the same value by
751/// multiplying by a magic number. See:
752/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
753SDOperand ISel::BuildSDIVSequence(SDOperand N) {
754 int d = (int)cast<ConstantSDNode>(N.getOperand(1))->getSignExtended();
755 ms magics = magic(d);
756 // Multiply the numerator (operand 0) by the magic value
757 SDOperand Q = ISelDAG->getNode(ISD::MULHS, MVT::i32, N.getOperand(0),
758 ISelDAG->getConstant(magics.m, MVT::i32));
759 // If d > 0 and m < 0, add the numerator
760 if (d > 0 && magics.m < 0)
761 Q = ISelDAG->getNode(ISD::ADD, MVT::i32, Q, N.getOperand(0));
762 // If d < 0 and m > 0, subtract the numerator.
763 if (d < 0 && magics.m > 0)
764 Q = ISelDAG->getNode(ISD::SUB, MVT::i32, Q, N.getOperand(0));
765 // Shift right algebraic if shift value is nonzero
766 if (magics.s > 0)
767 Q = ISelDAG->getNode(ISD::SRA, MVT::i32, Q,
768 ISelDAG->getConstant(magics.s, MVT::i32));
769 // Extract the sign bit and add it to the quotient
770 SDOperand T =
771 ISelDAG->getNode(ISD::SRL, MVT::i32, Q, ISelDAG->getConstant(31, MVT::i32));
Nate Begeman27b4c232005-04-06 06:44:57 +0000772 return ISelDAG->getNode(ISD::ADD, MVT::i32, Q, T);
Nate Begeman815d6da2005-04-06 00:25:27 +0000773}
774
775/// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
776/// return a DAG expression to select that will generate the same value by
777/// multiplying by a magic number. See:
778/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
779SDOperand ISel::BuildUDIVSequence(SDOperand N) {
780 unsigned d =
781 (unsigned)cast<ConstantSDNode>(N.getOperand(1))->getSignExtended();
782 mu magics = magicu(d);
783 // Multiply the numerator (operand 0) by the magic value
784 SDOperand Q = ISelDAG->getNode(ISD::MULHU, MVT::i32, N.getOperand(0),
785 ISelDAG->getConstant(magics.m, MVT::i32));
786 if (magics.a == 0) {
787 Q = ISelDAG->getNode(ISD::SRL, MVT::i32, Q,
788 ISelDAG->getConstant(magics.s, MVT::i32));
789 } else {
790 SDOperand NPQ = ISelDAG->getNode(ISD::SUB, MVT::i32, N.getOperand(0), Q);
791 NPQ = ISelDAG->getNode(ISD::SRL, MVT::i32, NPQ,
792 ISelDAG->getConstant(1, MVT::i32));
793 NPQ = ISelDAG->getNode(ISD::ADD, MVT::i32, NPQ, Q);
794 Q = ISelDAG->getNode(ISD::SRL, MVT::i32, NPQ,
795 ISelDAG->getConstant(magics.s-1, MVT::i32));
796 }
Nate Begeman27b4c232005-04-06 06:44:57 +0000797 return Q;
Nate Begemana9795f82005-03-24 04:41:43 +0000798}
799
Nate Begemanc7b09f12005-03-25 08:34:25 +0000800/// getGlobalBaseReg - Output the instructions required to put the
801/// base address to use for accessing globals into a register.
802///
803unsigned ISel::getGlobalBaseReg() {
804 if (!GlobalBaseInitialized) {
805 // Insert the set of GlobalBaseReg into the first MBB of the function
806 MachineBasicBlock &FirstMBB = BB->getParent()->front();
807 MachineBasicBlock::iterator MBBI = FirstMBB.begin();
808 GlobalBaseReg = MakeReg(MVT::i32);
809 BuildMI(FirstMBB, MBBI, PPC::MovePCtoLR, 0, PPC::LR);
810 BuildMI(FirstMBB, MBBI, PPC::MFLR, 1, GlobalBaseReg).addReg(PPC::LR);
811 GlobalBaseInitialized = true;
812 }
813 return GlobalBaseReg;
814}
815
Nate Begeman6b559972005-04-01 02:59:27 +0000816/// getConstDouble - Loads a floating point value into a register, via the
817/// Constant Pool. Optionally takes a register in which to load the value.
818unsigned ISel::getConstDouble(double doubleVal, unsigned Result=0) {
819 unsigned Tmp1 = MakeReg(MVT::i32);
820 if (0 == Result) Result = MakeReg(MVT::f64);
821 MachineConstantPool *CP = BB->getParent()->getConstantPool();
822 ConstantFP *CFP = ConstantFP::get(Type::DoubleTy, doubleVal);
823 unsigned CPI = CP->getConstantPoolIndex(CFP);
824 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp1).addReg(getGlobalBaseReg())
825 .addConstantPoolIndex(CPI);
826 BuildMI(BB, PPC::LFD, 2, Result).addConstantPoolIndex(CPI).addReg(Tmp1);
827 return Result;
828}
829
Nate Begeman7ddecb42005-04-06 23:51:40 +0000830/// SelectBitfieldInsert - turn an or of two masked values into
831/// the rotate left word immediate then mask insert (rlwimi) instruction.
832/// Returns true on success, false if the caller still needs to select OR.
833///
834/// Patterns matched:
835/// 1. or shl, and 5. or and, and
836/// 2. or and, shl 6. or shl, shr
837/// 3. or shr, and 7. or shr, shl
838/// 4. or and, shr
839bool ISel::SelectBitfieldInsert(SDOperand OR, unsigned Result) {
840 unsigned TgtMask = 0xFFFFFFFF, InsMask = 0xFFFFFFFF, Amount = 0;
841 unsigned Op0Opc = OR.getOperand(0).getOpcode();
842 unsigned Op1Opc = OR.getOperand(1).getOpcode();
843
844 // Verify that we have the correct opcodes
845 if (ISD::SHL != Op0Opc && ISD::SRL != Op0Opc && ISD::AND != Op0Opc)
846 return false;
847 if (ISD::SHL != Op1Opc && ISD::SRL != Op1Opc && ISD::AND != Op1Opc)
848 return false;
849
850 // Generate Mask value for Target
851 if (ConstantSDNode *CN =
852 dyn_cast<ConstantSDNode>(OR.getOperand(0).getOperand(1).Val)) {
853 switch(Op0Opc) {
854 case ISD::SHL: TgtMask <<= (unsigned)CN->getValue(); break;
855 case ISD::SRL: TgtMask >>= (unsigned)CN->getValue(); break;
856 case ISD::AND: TgtMask &= (unsigned)CN->getValue(); break;
857 }
858 } else {
859 return false;
860 }
861
862 // Generate Mask value for Insert
863 if (ConstantSDNode *CN =
864 dyn_cast<ConstantSDNode>(OR.getOperand(1).getOperand(1).Val)) {
865 switch(Op1Opc) {
866 case ISD::SHL:
867 Amount = CN->getValue();
868 InsMask <<= Amount;
869 break;
870 case ISD::SRL:
871 Amount = CN->getValue();
872 InsMask >>= Amount;
873 Amount = 32-Amount;
874 break;
875 case ISD::AND:
876 InsMask &= (unsigned)CN->getValue();
877 break;
878 }
879 } else {
880 return false;
881 }
882
883 // Verify that the Target mask and Insert mask together form a full word mask
884 // and that the Insert mask is a run of set bits (which implies both are runs
885 // of set bits). Given that, Select the arguments and generate the rlwimi
886 // instruction.
887 unsigned MB, ME;
888 if (((TgtMask ^ InsMask) == 0xFFFFFFFF) && IsRunOfOnes(InsMask, MB, ME)) {
889 unsigned Tmp1, Tmp2;
890 if (Op0Opc == ISD::AND)
891 Tmp1 = SelectExpr(OR.getOperand(0).getOperand(0));
892 else
893 Tmp1 = SelectExpr(OR.getOperand(0));
894 Tmp2 = SelectExpr(OR.getOperand(1).getOperand(0));
895 BuildMI(BB, PPC::RLWIMI, 5, Result).addReg(Tmp1).addReg(Tmp2)
896 .addImm(Amount).addImm(MB).addImm(ME);
897 return true;
898 }
899 return false;
900}
901
Nate Begemandffcfcc2005-04-01 00:32:34 +0000902unsigned ISel::SelectSetCR0(SDOperand CC) {
903 unsigned Opc, Tmp1, Tmp2;
904 static const unsigned CompareOpcodes[] =
905 { PPC::FCMPU, PPC::FCMPU, PPC::CMPW, PPC::CMPLW };
906
907 // If the first operand to the select is a SETCC node, then we can fold it
908 // into the branch that selects which value to return.
909 SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(CC.Val);
910 if (SetCC && CC.getOpcode() == ISD::SETCC) {
911 bool U;
912 Opc = getBCCForSetCC(SetCC->getCondition(), U);
913 Tmp1 = SelectExpr(SetCC->getOperand(0));
914
Nate Begeman439b4442005-04-05 04:22:58 +0000915 // Pass the optional argument U to getImmediateForOpcode for SETCC,
Nate Begemandffcfcc2005-04-01 00:32:34 +0000916 // so that it knows whether the SETCC immediate range is signed or not.
Nate Begeman439b4442005-04-05 04:22:58 +0000917 if (1 == getImmediateForOpcode(SetCC->getOperand(1), ISD::SETCC,
918 Tmp2, U)) {
Nate Begemandffcfcc2005-04-01 00:32:34 +0000919 if (U)
920 BuildMI(BB, PPC::CMPLWI, 2, PPC::CR0).addReg(Tmp1).addImm(Tmp2);
921 else
922 BuildMI(BB, PPC::CMPWI, 2, PPC::CR0).addReg(Tmp1).addSImm(Tmp2);
923 } else {
924 bool IsInteger = MVT::isInteger(SetCC->getOperand(0).getValueType());
925 unsigned CompareOpc = CompareOpcodes[2 * IsInteger + U];
926 Tmp2 = SelectExpr(SetCC->getOperand(1));
927 BuildMI(BB, CompareOpc, 2, PPC::CR0).addReg(Tmp1).addReg(Tmp2);
928 }
929 } else {
930 Tmp1 = SelectExpr(CC);
931 BuildMI(BB, PPC::CMPLWI, 2, PPC::CR0).addReg(Tmp1).addImm(0);
932 Opc = PPC::BNE;
933 }
934 return Opc;
935}
936
937/// Check to see if the load is a constant offset from a base register
Nate Begeman04730362005-04-01 04:45:11 +0000938bool ISel::SelectAddr(SDOperand N, unsigned& Reg, int& offset)
Nate Begemana9795f82005-03-24 04:41:43 +0000939{
Nate Begeman96fc6812005-03-31 02:05:53 +0000940 unsigned imm = 0, opcode = N.getOpcode();
Nate Begeman04730362005-04-01 04:45:11 +0000941 if (N.getOpcode() == ISD::ADD) {
942 Reg = SelectExpr(N.getOperand(0));
Nate Begeman439b4442005-04-05 04:22:58 +0000943 if (1 == getImmediateForOpcode(N.getOperand(1), opcode, imm)) {
Nate Begeman96fc6812005-03-31 02:05:53 +0000944 offset = imm;
Nate Begeman04730362005-04-01 04:45:11 +0000945 return false;
946 }
947 offset = SelectExpr(N.getOperand(1));
948 return true;
949 }
Nate Begemana9795f82005-03-24 04:41:43 +0000950 Reg = SelectExpr(N);
951 offset = 0;
Nate Begeman04730362005-04-01 04:45:11 +0000952 return false;
Nate Begemana9795f82005-03-24 04:41:43 +0000953}
954
955void ISel::SelectBranchCC(SDOperand N)
956{
957 assert(N.getOpcode() == ISD::BRCOND && "Not a BranchCC???");
958 MachineBasicBlock *Dest =
959 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
Nate Begeman3e897162005-03-31 23:55:40 +0000960
Nate Begeman439b4442005-04-05 04:22:58 +0000961 // Get the MBB we will fall through to so that we can hand it off to the
962 // branch selection pass as an argument to the PPC::COND_BRANCH pseudo op.
Nate Begemanc8c5c8f2005-04-05 04:32:16 +0000963 //ilist<MachineBasicBlock>::iterator It = BB;
964 //MachineBasicBlock *Fallthrough = ++It;
Nate Begeman439b4442005-04-05 04:22:58 +0000965
Nate Begemana9795f82005-03-24 04:41:43 +0000966 Select(N.getOperand(0)); //chain
Nate Begemandffcfcc2005-04-01 00:32:34 +0000967 unsigned Opc = SelectSetCR0(N.getOperand(1));
Nate Begemanc8c5c8f2005-04-05 04:32:16 +0000968 // FIXME: Use this once we have something approximating two-way branches
969 // We cannot currently use this in case the ISel hands us something like
970 // BRcc MBBx
971 // BR MBBy
972 // since the fallthrough basic block for the conditional branch does not start
973 // with the unconditional branch (it is skipped over).
974 //BuildMI(BB, PPC::COND_BRANCH, 4).addReg(PPC::CR0).addImm(Opc)
975 // .addMBB(Dest).addMBB(Fallthrough);
976 BuildMI(BB, Opc, 2).addReg(PPC::CR0).addMBB(Dest);
Nate Begemana9795f82005-03-24 04:41:43 +0000977 return;
978}
979
980unsigned ISel::SelectExprFP(SDOperand N, unsigned Result)
981{
982 unsigned Tmp1, Tmp2, Tmp3;
983 unsigned Opc = 0;
984 SDNode *Node = N.Val;
985 MVT::ValueType DestType = N.getValueType();
986 unsigned opcode = N.getOpcode();
987
988 switch (opcode) {
989 default:
990 Node->dump();
991 assert(0 && "Node not handled!\n");
992
Nate Begeman23afcfb2005-03-29 22:48:55 +0000993 case ISD::SELECT: {
Nate Begeman3e897162005-03-31 23:55:40 +0000994 // Attempt to generate FSEL. We can do this whenever we have an FP result,
995 // and an FP comparison in the SetCC node.
996 SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(N.getOperand(0).Val);
997 if (SetCC && N.getOperand(0).getOpcode() == ISD::SETCC &&
998 !MVT::isInteger(SetCC->getOperand(0).getValueType()) &&
999 SetCC->getCondition() != ISD::SETEQ &&
1000 SetCC->getCondition() != ISD::SETNE) {
1001 MVT::ValueType VT = SetCC->getOperand(0).getValueType();
Nate Begeman3e897162005-03-31 23:55:40 +00001002 unsigned TV = SelectExpr(N.getOperand(1)); // Use if TRUE
1003 unsigned FV = SelectExpr(N.getOperand(2)); // Use if FALSE
1004
1005 ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(1));
1006 if (CN && (CN->isExactlyValue(-0.0) || CN->isExactlyValue(0.0))) {
1007 switch(SetCC->getCondition()) {
1008 default: assert(0 && "Invalid FSEL condition"); abort();
1009 case ISD::SETULT:
1010 case ISD::SETLT:
Nate Begemanaf4ab1b2005-04-09 09:33:07 +00001011 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
Nate Begeman3e897162005-03-31 23:55:40 +00001012 case ISD::SETUGE:
1013 case ISD::SETGE:
Nate Begemanaf4ab1b2005-04-09 09:33:07 +00001014 Tmp1 = SelectExpr(SetCC->getOperand(0)); // Val to compare against
Nate Begeman3e897162005-03-31 23:55:40 +00001015 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp1).addReg(TV).addReg(FV);
1016 return Result;
1017 case ISD::SETUGT:
Nate Begemanaf4ab1b2005-04-09 09:33:07 +00001018 case ISD::SETGT:
1019 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
Nate Begeman3e897162005-03-31 23:55:40 +00001020 case ISD::SETULE:
1021 case ISD::SETLE: {
Nate Begemanaf4ab1b2005-04-09 09:33:07 +00001022 if (SetCC->getOperand(0).getOpcode() == ISD::FNEG) {
1023 Tmp2 = SelectExpr(SetCC->getOperand(0).getOperand(0));
1024 } else {
1025 Tmp2 = MakeReg(VT);
1026 Tmp1 = SelectExpr(SetCC->getOperand(0)); // Val to compare against
1027 BuildMI(BB, PPC::FNEG, 1, Tmp2).addReg(Tmp1);
1028 }
Nate Begeman3e897162005-03-31 23:55:40 +00001029 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp2).addReg(TV).addReg(FV);
1030 return Result;
1031 }
1032 }
1033 } else {
1034 Opc = (MVT::f64 == VT) ? PPC::FSUB : PPC::FSUBS;
Nate Begemanaf4ab1b2005-04-09 09:33:07 +00001035 Tmp1 = SelectExpr(SetCC->getOperand(0)); // Val to compare against
Nate Begeman3e897162005-03-31 23:55:40 +00001036 Tmp2 = SelectExpr(SetCC->getOperand(1));
1037 Tmp3 = MakeReg(VT);
1038 switch(SetCC->getCondition()) {
1039 default: assert(0 && "Invalid FSEL condition"); abort();
1040 case ISD::SETULT:
1041 case ISD::SETLT:
1042 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
1043 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(FV).addReg(TV);
1044 return Result;
1045 case ISD::SETUGE:
1046 case ISD::SETGE:
1047 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
1048 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(TV).addReg(FV);
1049 return Result;
1050 case ISD::SETUGT:
1051 case ISD::SETGT:
1052 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp2).addReg(Tmp1);
1053 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(FV).addReg(TV);
1054 return Result;
1055 case ISD::SETULE:
1056 case ISD::SETLE:
1057 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp2).addReg(Tmp1);
1058 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(TV).addReg(FV);
1059 return Result;
1060 }
1061 }
1062 assert(0 && "Should never get here");
1063 return 0;
1064 }
1065
Nate Begeman31318e42005-04-01 07:21:30 +00001066 unsigned TrueValue = SelectExpr(N.getOperand(1)); //Use if TRUE
1067 unsigned FalseValue = SelectExpr(N.getOperand(2)); //Use if FALSE
Nate Begeman6cb2e1b2005-04-01 08:57:43 +00001068 Opc = SelectSetCR0(N.getOperand(0));
Nate Begeman31318e42005-04-01 07:21:30 +00001069
Nate Begeman23afcfb2005-03-29 22:48:55 +00001070 // Create an iterator with which to insert the MBB for copying the false
1071 // value and the MBB to hold the PHI instruction for this SetCC.
1072 MachineBasicBlock *thisMBB = BB;
1073 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1074 ilist<MachineBasicBlock>::iterator It = BB;
1075 ++It;
1076
1077 // thisMBB:
1078 // ...
1079 // TrueVal = ...
1080 // cmpTY cr0, r1, r2
1081 // bCC copy1MBB
1082 // fallthrough --> copy0MBB
Nate Begeman23afcfb2005-03-29 22:48:55 +00001083 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
1084 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
Nate Begeman6cb2e1b2005-04-01 08:57:43 +00001085 BuildMI(BB, Opc, 2).addReg(PPC::CR0).addMBB(sinkMBB);
Nate Begeman23afcfb2005-03-29 22:48:55 +00001086 MachineFunction *F = BB->getParent();
1087 F->getBasicBlockList().insert(It, copy0MBB);
1088 F->getBasicBlockList().insert(It, sinkMBB);
1089 // Update machine-CFG edges
1090 BB->addSuccessor(copy0MBB);
1091 BB->addSuccessor(sinkMBB);
1092
1093 // copy0MBB:
1094 // %FalseValue = ...
1095 // # fallthrough to sinkMBB
1096 BB = copy0MBB;
Nate Begeman23afcfb2005-03-29 22:48:55 +00001097 // Update machine-CFG edges
1098 BB->addSuccessor(sinkMBB);
1099
1100 // sinkMBB:
1101 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1102 // ...
1103 BB = sinkMBB;
1104 BuildMI(BB, PPC::PHI, 4, Result).addReg(FalseValue)
1105 .addMBB(copy0MBB).addReg(TrueValue).addMBB(thisMBB);
1106 return Result;
1107 }
Nate Begeman27eeb002005-04-02 05:59:34 +00001108
1109 case ISD::FNEG:
Nate Begeman93075ec2005-04-04 23:40:36 +00001110 if (!NoExcessFPPrecision &&
1111 ISD::ADD == N.getOperand(0).getOpcode() &&
1112 N.getOperand(0).Val->hasOneUse() &&
1113 ISD::MUL == N.getOperand(0).getOperand(0).getOpcode() &&
1114 N.getOperand(0).getOperand(0).Val->hasOneUse()) {
Nate Begeman80196b12005-04-05 00:15:08 +00001115 ++FusedFP; // Statistic
Nate Begeman93075ec2005-04-04 23:40:36 +00001116 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(0));
1117 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(1));
1118 Tmp3 = SelectExpr(N.getOperand(0).getOperand(1));
1119 Opc = DestType == MVT::f64 ? PPC::FNMADD : PPC::FNMADDS;
1120 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1121 } else if (!NoExcessFPPrecision &&
Nate Begemane88aa5b2005-04-09 03:05:51 +00001122 ISD::ADD == N.getOperand(0).getOpcode() &&
Nate Begeman93075ec2005-04-04 23:40:36 +00001123 N.getOperand(0).Val->hasOneUse() &&
Nate Begemane88aa5b2005-04-09 03:05:51 +00001124 ISD::MUL == N.getOperand(0).getOperand(1).getOpcode() &&
1125 N.getOperand(0).getOperand(1).Val->hasOneUse()) {
Nate Begeman80196b12005-04-05 00:15:08 +00001126 ++FusedFP; // Statistic
Nate Begemane88aa5b2005-04-09 03:05:51 +00001127 Tmp1 = SelectExpr(N.getOperand(0).getOperand(1).getOperand(0));
1128 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1).getOperand(1));
1129 Tmp3 = SelectExpr(N.getOperand(0).getOperand(0));
1130 Opc = DestType == MVT::f64 ? PPC::FNMADD : PPC::FNMADDS;
Nate Begeman93075ec2005-04-04 23:40:36 +00001131 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1132 } else if (ISD::FABS == N.getOperand(0).getOpcode()) {
Nate Begeman27eeb002005-04-02 05:59:34 +00001133 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1134 BuildMI(BB, PPC::FNABS, 1, Result).addReg(Tmp1);
1135 } else {
1136 Tmp1 = SelectExpr(N.getOperand(0));
1137 BuildMI(BB, PPC::FNEG, 1, Result).addReg(Tmp1);
1138 }
1139 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001140
Nate Begeman27eeb002005-04-02 05:59:34 +00001141 case ISD::FABS:
1142 Tmp1 = SelectExpr(N.getOperand(0));
1143 BuildMI(BB, PPC::FABS, 1, Result).addReg(Tmp1);
1144 return Result;
1145
Nate Begemana9795f82005-03-24 04:41:43 +00001146 case ISD::FP_ROUND:
1147 assert (DestType == MVT::f32 &&
1148 N.getOperand(0).getValueType() == MVT::f64 &&
1149 "only f64 to f32 conversion supported here");
1150 Tmp1 = SelectExpr(N.getOperand(0));
1151 BuildMI(BB, PPC::FRSP, 1, Result).addReg(Tmp1);
1152 return Result;
1153
1154 case ISD::FP_EXTEND:
1155 assert (DestType == MVT::f64 &&
1156 N.getOperand(0).getValueType() == MVT::f32 &&
1157 "only f32 to f64 conversion supported here");
1158 Tmp1 = SelectExpr(N.getOperand(0));
1159 BuildMI(BB, PPC::FMR, 1, Result).addReg(Tmp1);
1160 return Result;
1161
1162 case ISD::CopyFromReg:
Nate Begemanf2622612005-03-26 02:17:46 +00001163 if (Result == 1)
1164 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1165 Tmp1 = dyn_cast<RegSDNode>(Node)->getReg();
1166 BuildMI(BB, PPC::FMR, 1, Result).addReg(Tmp1);
1167 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001168
Nate Begeman6d369cc2005-04-01 01:08:07 +00001169 case ISD::ConstantFP: {
Nate Begeman6d369cc2005-04-01 01:08:07 +00001170 ConstantFPSDNode *CN = cast<ConstantFPSDNode>(N);
Nate Begeman6b559972005-04-01 02:59:27 +00001171 Result = getConstDouble(CN->getValue(), Result);
Nate Begeman6d369cc2005-04-01 01:08:07 +00001172 return Result;
1173 }
Nate Begemana9795f82005-03-24 04:41:43 +00001174
Nate Begemana9795f82005-03-24 04:41:43 +00001175 case ISD::ADD:
Nate Begeman93075ec2005-04-04 23:40:36 +00001176 if (!NoExcessFPPrecision && N.getOperand(0).getOpcode() == ISD::MUL &&
1177 N.getOperand(0).Val->hasOneUse()) {
1178 ++FusedFP; // Statistic
1179 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1180 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1181 Tmp3 = SelectExpr(N.getOperand(1));
1182 Opc = DestType == MVT::f64 ? PPC::FMADD : PPC::FMADDS;
1183 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1184 return Result;
1185 }
Nate Begemane88aa5b2005-04-09 03:05:51 +00001186 if (!NoExcessFPPrecision && N.getOperand(1).getOpcode() == ISD::MUL &&
1187 N.getOperand(1).Val->hasOneUse()) {
1188 ++FusedFP; // Statistic
1189 Tmp1 = SelectExpr(N.getOperand(1).getOperand(0));
1190 Tmp2 = SelectExpr(N.getOperand(1).getOperand(1));
1191 Tmp3 = SelectExpr(N.getOperand(0));
1192 Opc = DestType == MVT::f64 ? PPC::FMADD : PPC::FMADDS;
1193 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1194 return Result;
1195 }
Nate Begeman93075ec2005-04-04 23:40:36 +00001196 Opc = DestType == MVT::f64 ? PPC::FADD : PPC::FADDS;
1197 Tmp1 = SelectExpr(N.getOperand(0));
1198 Tmp2 = SelectExpr(N.getOperand(1));
1199 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1200 return Result;
1201
Nate Begemana9795f82005-03-24 04:41:43 +00001202 case ISD::SUB:
Nate Begeman93075ec2005-04-04 23:40:36 +00001203 if (!NoExcessFPPrecision && N.getOperand(0).getOpcode() == ISD::MUL &&
1204 N.getOperand(0).Val->hasOneUse()) {
1205 ++FusedFP; // Statistic
1206 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1207 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1208 Tmp3 = SelectExpr(N.getOperand(1));
1209 Opc = DestType == MVT::f64 ? PPC::FMSUB : PPC::FMSUBS;
1210 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1211 return Result;
1212 }
Nate Begemane88aa5b2005-04-09 03:05:51 +00001213 if (!NoExcessFPPrecision && N.getOperand(1).getOpcode() == ISD::MUL &&
1214 N.getOperand(1).Val->hasOneUse()) {
1215 ++FusedFP; // Statistic
1216 Tmp1 = SelectExpr(N.getOperand(1).getOperand(0));
1217 Tmp2 = SelectExpr(N.getOperand(1).getOperand(1));
1218 Tmp3 = SelectExpr(N.getOperand(0));
1219 Opc = DestType == MVT::f64 ? PPC::FNMSUB : PPC::FNMSUBS;
1220 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1221 return Result;
1222 }
Nate Begeman93075ec2005-04-04 23:40:36 +00001223 Opc = DestType == MVT::f64 ? PPC::FSUB : PPC::FSUBS;
1224 Tmp1 = SelectExpr(N.getOperand(0));
1225 Tmp2 = SelectExpr(N.getOperand(1));
1226 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1227 return Result;
1228
1229 case ISD::MUL:
Nate Begemana9795f82005-03-24 04:41:43 +00001230 case ISD::SDIV:
1231 switch( opcode ) {
1232 case ISD::MUL: Opc = DestType == MVT::f64 ? PPC::FMUL : PPC::FMULS; break;
Nate Begemana9795f82005-03-24 04:41:43 +00001233 case ISD::SDIV: Opc = DestType == MVT::f64 ? PPC::FDIV : PPC::FDIVS; break;
1234 };
Nate Begemana9795f82005-03-24 04:41:43 +00001235 Tmp1 = SelectExpr(N.getOperand(0));
1236 Tmp2 = SelectExpr(N.getOperand(1));
1237 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1238 return Result;
1239
Nate Begemana9795f82005-03-24 04:41:43 +00001240 case ISD::UINT_TO_FP:
Nate Begemanfdcf3412005-03-30 19:38:35 +00001241 case ISD::SINT_TO_FP: {
1242 assert (N.getOperand(0).getValueType() == MVT::i32
1243 && "int to float must operate on i32");
1244 bool IsUnsigned = (ISD::UINT_TO_FP == opcode);
1245 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
1246 Tmp2 = MakeReg(MVT::f64); // temp reg to load the integer value into
1247 Tmp3 = MakeReg(MVT::i32); // temp reg to hold the conversion constant
1248 unsigned ConstF = MakeReg(MVT::f64); // temp reg to hold the fp constant
1249
1250 int FrameIdx = BB->getParent()->getFrameInfo()->CreateStackObject(8, 8);
1251 MachineConstantPool *CP = BB->getParent()->getConstantPool();
1252
1253 // FIXME: pull this FP constant generation stuff out into something like
1254 // the simple ISel's getReg.
1255 if (IsUnsigned) {
1256 ConstantFP *CFP = ConstantFP::get(Type::DoubleTy, 0x1.000000p52);
1257 unsigned CPI = CP->getConstantPoolIndex(CFP);
1258 // Load constant fp value
1259 unsigned Tmp4 = MakeReg(MVT::i32);
1260 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp4).addReg(getGlobalBaseReg())
1261 .addConstantPoolIndex(CPI);
1262 BuildMI(BB, PPC::LFD, 2, ConstF).addConstantPoolIndex(CPI).addReg(Tmp4);
1263 // Store the hi & low halves of the fp value, currently in int regs
1264 BuildMI(BB, PPC::LIS, 1, Tmp3).addSImm(0x4330);
1265 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(Tmp3), FrameIdx);
1266 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(Tmp1), FrameIdx, 4);
1267 addFrameReference(BuildMI(BB, PPC::LFD, 2, Tmp2), FrameIdx);
1268 // Generate the return value with a subtract
1269 BuildMI(BB, PPC::FSUB, 2, Result).addReg(Tmp2).addReg(ConstF);
1270 } else {
1271 ConstantFP *CFP = ConstantFP::get(Type::DoubleTy, 0x1.000008p52);
1272 unsigned CPI = CP->getConstantPoolIndex(CFP);
1273 // Load constant fp value
1274 unsigned Tmp4 = MakeReg(MVT::i32);
1275 unsigned TmpL = MakeReg(MVT::i32);
1276 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp4).addReg(getGlobalBaseReg())
1277 .addConstantPoolIndex(CPI);
1278 BuildMI(BB, PPC::LFD, 2, ConstF).addConstantPoolIndex(CPI).addReg(Tmp4);
1279 // Store the hi & low halves of the fp value, currently in int regs
1280 BuildMI(BB, PPC::LIS, 1, Tmp3).addSImm(0x4330);
1281 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(Tmp3), FrameIdx);
1282 BuildMI(BB, PPC::XORIS, 2, TmpL).addReg(Tmp1).addImm(0x8000);
1283 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(TmpL), FrameIdx, 4);
1284 addFrameReference(BuildMI(BB, PPC::LFD, 2, Tmp2), FrameIdx);
1285 // Generate the return value with a subtract
1286 BuildMI(BB, PPC::FSUB, 2, Result).addReg(Tmp2).addReg(ConstF);
1287 }
1288 return Result;
1289 }
Nate Begemana9795f82005-03-24 04:41:43 +00001290 }
Nate Begeman6b559972005-04-01 02:59:27 +00001291 assert(0 && "Should never get here");
Nate Begemana9795f82005-03-24 04:41:43 +00001292 return 0;
1293}
1294
1295unsigned ISel::SelectExpr(SDOperand N) {
1296 unsigned Result;
1297 unsigned Tmp1, Tmp2, Tmp3;
1298 unsigned Opc = 0;
1299 unsigned opcode = N.getOpcode();
1300
1301 SDNode *Node = N.Val;
1302 MVT::ValueType DestType = N.getValueType();
1303
1304 unsigned &Reg = ExprMap[N];
1305 if (Reg) return Reg;
1306
Nate Begeman27eeb002005-04-02 05:59:34 +00001307 switch (N.getOpcode()) {
1308 default:
Nate Begemana9795f82005-03-24 04:41:43 +00001309 Reg = Result = (N.getValueType() != MVT::Other) ?
Nate Begeman27eeb002005-04-02 05:59:34 +00001310 MakeReg(N.getValueType()) : 1;
1311 break;
1312 case ISD::CALL:
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001313 // If this is a call instruction, make sure to prepare ALL of the result
1314 // values as well as the chain.
Nate Begeman27eeb002005-04-02 05:59:34 +00001315 if (Node->getNumValues() == 1)
1316 Reg = Result = 1; // Void call, just a chain.
1317 else {
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001318 Result = MakeReg(Node->getValueType(0));
1319 ExprMap[N.getValue(0)] = Result;
Nate Begeman27eeb002005-04-02 05:59:34 +00001320 for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001321 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
Nate Begeman27eeb002005-04-02 05:59:34 +00001322 ExprMap[SDOperand(Node, Node->getNumValues()-1)] = 1;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001323 }
Nate Begeman27eeb002005-04-02 05:59:34 +00001324 break;
1325 case ISD::ADD_PARTS:
1326 case ISD::SUB_PARTS:
1327 case ISD::SHL_PARTS:
1328 case ISD::SRL_PARTS:
1329 case ISD::SRA_PARTS:
1330 Result = MakeReg(Node->getValueType(0));
1331 ExprMap[N.getValue(0)] = Result;
1332 for (unsigned i = 1, e = N.Val->getNumValues(); i != e; ++i)
1333 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
1334 break;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001335 }
1336
Nate Begemane5846682005-04-04 06:52:38 +00001337 if (ISD::CopyFromReg == opcode)
1338 DestType = N.getValue(0).getValueType();
1339
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001340 if (DestType == MVT::f64 || DestType == MVT::f32)
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001341 if (ISD::LOAD != opcode && ISD::EXTLOAD != opcode && ISD::UNDEF != opcode)
Nate Begeman74d73452005-03-31 00:15:26 +00001342 return SelectExprFP(N, Result);
Nate Begemana9795f82005-03-24 04:41:43 +00001343
1344 switch (opcode) {
1345 default:
1346 Node->dump();
1347 assert(0 && "Node not handled!\n");
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001348 case ISD::UNDEF:
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001349 BuildMI(BB, PPC::IMPLICIT_DEF, 0, Result);
1350 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001351 case ISD::DYNAMIC_STACKALLOC:
Nate Begeman5e966612005-03-24 06:28:42 +00001352 // Generate both result values. FIXME: Need a better commment here?
1353 if (Result != 1)
1354 ExprMap[N.getValue(1)] = 1;
1355 else
1356 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1357
1358 // FIXME: We are currently ignoring the requested alignment for handling
1359 // greater than the stack alignment. This will need to be revisited at some
1360 // point. Align = N.getOperand(2);
1361 if (!isa<ConstantSDNode>(N.getOperand(2)) ||
1362 cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) {
1363 std::cerr << "Cannot allocate stack object with greater alignment than"
1364 << " the stack alignment yet!";
1365 abort();
1366 }
1367 Select(N.getOperand(0));
1368 Tmp1 = SelectExpr(N.getOperand(1));
1369 // Subtract size from stack pointer, thereby allocating some space.
1370 BuildMI(BB, PPC::SUBF, 2, PPC::R1).addReg(Tmp1).addReg(PPC::R1);
1371 // Put a pointer to the space into the result register by copying the SP
1372 BuildMI(BB, PPC::OR, 2, Result).addReg(PPC::R1).addReg(PPC::R1);
1373 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001374
1375 case ISD::ConstantPool:
Nate Begemanca12a2b2005-03-28 22:28:37 +00001376 Tmp1 = cast<ConstantPoolSDNode>(N)->getIndex();
1377 Tmp2 = MakeReg(MVT::i32);
1378 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp2).addReg(getGlobalBaseReg())
1379 .addConstantPoolIndex(Tmp1);
1380 BuildMI(BB, PPC::LA, 2, Result).addReg(Tmp2).addConstantPoolIndex(Tmp1);
1381 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001382
1383 case ISD::FrameIndex:
Nate Begemanf3d08f32005-03-29 00:03:27 +00001384 Tmp1 = cast<FrameIndexSDNode>(N)->getIndex();
Nate Begeman58f718c2005-03-30 02:23:08 +00001385 addFrameReference(BuildMI(BB, PPC::ADDI, 2, Result), (int)Tmp1, 0, false);
Nate Begemanf3d08f32005-03-29 00:03:27 +00001386 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001387
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001388 case ISD::GlobalAddress: {
1389 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
Nate Begemanca12a2b2005-03-28 22:28:37 +00001390 Tmp1 = MakeReg(MVT::i32);
Nate Begemanc7b09f12005-03-25 08:34:25 +00001391 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp1).addReg(getGlobalBaseReg())
1392 .addGlobalAddress(GV);
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001393 if (GV->hasWeakLinkage() || GV->isExternal()) {
1394 BuildMI(BB, PPC::LWZ, 2, Result).addGlobalAddress(GV).addReg(Tmp1);
1395 } else {
1396 BuildMI(BB, PPC::LA, 2, Result).addReg(Tmp1).addGlobalAddress(GV);
1397 }
1398 return Result;
1399 }
1400
Nate Begeman5e966612005-03-24 06:28:42 +00001401 case ISD::LOAD:
Nate Begemana9795f82005-03-24 04:41:43 +00001402 case ISD::EXTLOAD:
1403 case ISD::ZEXTLOAD:
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001404 case ISD::SEXTLOAD: {
Nate Begeman9db505c2005-03-28 19:36:43 +00001405 MVT::ValueType TypeBeingLoaded = (ISD::LOAD == opcode) ?
1406 Node->getValueType(0) : cast<MVTSDNode>(Node)->getExtraValueType();
Nate Begeman74d73452005-03-31 00:15:26 +00001407 bool sext = (ISD::SEXTLOAD == opcode);
Nate Begeman74d73452005-03-31 00:15:26 +00001408
Nate Begeman5e966612005-03-24 06:28:42 +00001409 // Make sure we generate both values.
1410 if (Result != 1)
1411 ExprMap[N.getValue(1)] = 1; // Generate the token
1412 else
1413 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1414
1415 SDOperand Chain = N.getOperand(0);
1416 SDOperand Address = N.getOperand(1);
1417 Select(Chain);
1418
Nate Begeman9db505c2005-03-28 19:36:43 +00001419 switch (TypeBeingLoaded) {
Nate Begeman74d73452005-03-31 00:15:26 +00001420 default: Node->dump(); assert(0 && "Cannot load this type!");
Nate Begeman9db505c2005-03-28 19:36:43 +00001421 case MVT::i1: Opc = PPC::LBZ; break;
1422 case MVT::i8: Opc = PPC::LBZ; break;
1423 case MVT::i16: Opc = sext ? PPC::LHA : PPC::LHZ; break;
1424 case MVT::i32: Opc = PPC::LWZ; break;
Nate Begeman74d73452005-03-31 00:15:26 +00001425 case MVT::f32: Opc = PPC::LFS; break;
1426 case MVT::f64: Opc = PPC::LFD; break;
Nate Begeman5e966612005-03-24 06:28:42 +00001427 }
1428
Nate Begeman74d73452005-03-31 00:15:26 +00001429 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Address)) {
1430 Tmp1 = MakeReg(MVT::i32);
1431 int CPI = CP->getIndex();
1432 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp1).addReg(getGlobalBaseReg())
1433 .addConstantPoolIndex(CPI);
1434 BuildMI(BB, Opc, 2, Result).addConstantPoolIndex(CPI).addReg(Tmp1);
Nate Begeman9db505c2005-03-28 19:36:43 +00001435 }
Nate Begeman74d73452005-03-31 00:15:26 +00001436 else if(Address.getOpcode() == ISD::FrameIndex) {
Nate Begeman58f718c2005-03-30 02:23:08 +00001437 Tmp1 = cast<FrameIndexSDNode>(Address)->getIndex();
1438 addFrameReference(BuildMI(BB, Opc, 2, Result), (int)Tmp1);
Nate Begeman5e966612005-03-24 06:28:42 +00001439 } else {
1440 int offset;
Nate Begeman04730362005-04-01 04:45:11 +00001441 bool idx = SelectAddr(Address, Tmp1, offset);
1442 if (idx) {
1443 Opc = IndexedOpForOp(Opc);
1444 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(offset);
1445 } else {
1446 BuildMI(BB, Opc, 2, Result).addSImm(offset).addReg(Tmp1);
1447 }
Nate Begeman5e966612005-03-24 06:28:42 +00001448 }
1449 return Result;
1450 }
1451
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001452 case ISD::CALL: {
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001453 unsigned GPR_idx = 0, FPR_idx = 0;
1454 static const unsigned GPR[] = {
1455 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
1456 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
1457 };
1458 static const unsigned FPR[] = {
1459 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
1460 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
1461 };
1462
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001463 // Lower the chain for this call.
1464 Select(N.getOperand(0));
1465 ExprMap[N.getValue(Node->getNumValues()-1)] = 1;
Nate Begeman74d73452005-03-31 00:15:26 +00001466
Nate Begemand860aa62005-04-04 22:17:48 +00001467 MachineInstr *CallMI;
1468 // Emit the correct call instruction based on the type of symbol called.
1469 if (GlobalAddressSDNode *GASD =
1470 dyn_cast<GlobalAddressSDNode>(N.getOperand(1))) {
1471 CallMI = BuildMI(PPC::CALLpcrel, 1).addGlobalAddress(GASD->getGlobal(),
1472 true);
1473 } else if (ExternalSymbolSDNode *ESSDN =
1474 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1))) {
1475 CallMI = BuildMI(PPC::CALLpcrel, 1).addExternalSymbol(ESSDN->getSymbol(),
1476 true);
1477 } else {
1478 Tmp1 = SelectExpr(N.getOperand(1));
1479 BuildMI(BB, PPC::OR, 2, PPC::R12).addReg(Tmp1).addReg(Tmp1);
1480 BuildMI(BB, PPC::MTCTR, 1).addReg(PPC::R12);
1481 CallMI = BuildMI(PPC::CALLindirect, 3).addImm(20).addImm(0)
1482 .addReg(PPC::R12);
1483 }
1484
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001485 // Load the register args to virtual regs
1486 std::vector<unsigned> ArgVR;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001487 for(int i = 2, e = Node->getNumOperands(); i < e; ++i)
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001488 ArgVR.push_back(SelectExpr(N.getOperand(i)));
1489
1490 // Copy the virtual registers into the appropriate argument register
1491 for(int i = 0, e = ArgVR.size(); i < e; ++i) {
1492 switch(N.getOperand(i+2).getValueType()) {
1493 default: Node->dump(); assert(0 && "Unknown value type for call");
1494 case MVT::i1:
1495 case MVT::i8:
1496 case MVT::i16:
1497 case MVT::i32:
1498 assert(GPR_idx < 8 && "Too many int args");
Nate Begemand860aa62005-04-04 22:17:48 +00001499 if (N.getOperand(i+2).getOpcode() != ISD::UNDEF) {
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001500 BuildMI(BB, PPC::OR,2,GPR[GPR_idx]).addReg(ArgVR[i]).addReg(ArgVR[i]);
Nate Begemand860aa62005-04-04 22:17:48 +00001501 CallMI->addRegOperand(GPR[GPR_idx], MachineOperand::Use);
1502 }
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001503 ++GPR_idx;
1504 break;
1505 case MVT::f64:
1506 case MVT::f32:
1507 assert(FPR_idx < 13 && "Too many fp args");
1508 BuildMI(BB, PPC::FMR, 1, FPR[FPR_idx]).addReg(ArgVR[i]);
Nate Begemand860aa62005-04-04 22:17:48 +00001509 CallMI->addRegOperand(FPR[FPR_idx], MachineOperand::Use);
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001510 ++FPR_idx;
1511 break;
1512 }
1513 }
Nate Begemand860aa62005-04-04 22:17:48 +00001514
1515 // Put the call instruction in the correct place in the MachineBasicBlock
1516 BB->push_back(CallMI);
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001517
1518 switch (Node->getValueType(0)) {
1519 default: assert(0 && "Unknown value type for call result!");
1520 case MVT::Other: return 1;
1521 case MVT::i1:
1522 case MVT::i8:
1523 case MVT::i16:
1524 case MVT::i32:
Nate Begemane5846682005-04-04 06:52:38 +00001525 if (Node->getValueType(1) == MVT::i32) {
1526 BuildMI(BB, PPC::OR, 2, Result+1).addReg(PPC::R3).addReg(PPC::R3);
1527 BuildMI(BB, PPC::OR, 2, Result).addReg(PPC::R4).addReg(PPC::R4);
1528 } else {
1529 BuildMI(BB, PPC::OR, 2, Result).addReg(PPC::R3).addReg(PPC::R3);
1530 }
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001531 break;
1532 case MVT::f32:
1533 case MVT::f64:
1534 BuildMI(BB, PPC::FMR, 1, Result).addReg(PPC::F1);
1535 break;
1536 }
1537 return Result+N.ResNo;
1538 }
Nate Begemana9795f82005-03-24 04:41:43 +00001539
1540 case ISD::SIGN_EXTEND:
1541 case ISD::SIGN_EXTEND_INREG:
1542 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman9db505c2005-03-28 19:36:43 +00001543 switch(cast<MVTSDNode>(Node)->getExtraValueType()) {
1544 default: Node->dump(); assert(0 && "Unhandled SIGN_EXTEND type"); break;
1545 case MVT::i16:
1546 BuildMI(BB, PPC::EXTSH, 1, Result).addReg(Tmp1);
1547 break;
1548 case MVT::i8:
1549 BuildMI(BB, PPC::EXTSB, 1, Result).addReg(Tmp1);
1550 break;
Nate Begeman74747862005-03-29 22:24:51 +00001551 case MVT::i1:
1552 BuildMI(BB, PPC::SUBFIC, 2, Result).addReg(Tmp1).addSImm(0);
1553 break;
Nate Begeman9db505c2005-03-28 19:36:43 +00001554 }
Nate Begemana9795f82005-03-24 04:41:43 +00001555 return Result;
1556
1557 case ISD::ZERO_EXTEND_INREG:
1558 Tmp1 = SelectExpr(N.getOperand(0));
1559 switch(cast<MVTSDNode>(Node)->getExtraValueType()) {
Nate Begeman9db505c2005-03-28 19:36:43 +00001560 default: Node->dump(); assert(0 && "Unhandled ZERO_EXTEND type"); break;
Nate Begemana9795f82005-03-24 04:41:43 +00001561 case MVT::i16: Tmp2 = 16; break;
1562 case MVT::i8: Tmp2 = 24; break;
1563 case MVT::i1: Tmp2 = 31; break;
1564 }
Nate Begeman33162522005-03-29 21:54:38 +00001565 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(0).addImm(Tmp2)
1566 .addImm(31);
Nate Begemana9795f82005-03-24 04:41:43 +00001567 return Result;
1568
Nate Begemana9795f82005-03-24 04:41:43 +00001569 case ISD::CopyFromReg:
1570 if (Result == 1)
1571 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1572 Tmp1 = dyn_cast<RegSDNode>(Node)->getReg();
1573 BuildMI(BB, PPC::OR, 2, Result).addReg(Tmp1).addReg(Tmp1);
1574 return Result;
1575
1576 case ISD::SHL:
Nate Begeman5e966612005-03-24 06:28:42 +00001577 Tmp1 = SelectExpr(N.getOperand(0));
1578 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1579 Tmp2 = CN->getValue() & 0x1F;
Nate Begeman33162522005-03-29 21:54:38 +00001580 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(Tmp2).addImm(0)
Nate Begeman5e966612005-03-24 06:28:42 +00001581 .addImm(31-Tmp2);
1582 } else {
1583 Tmp2 = SelectExpr(N.getOperand(1));
1584 BuildMI(BB, PPC::SLW, 2, Result).addReg(Tmp1).addReg(Tmp2);
1585 }
1586 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001587
Nate Begeman5e966612005-03-24 06:28:42 +00001588 case ISD::SRL:
1589 Tmp1 = SelectExpr(N.getOperand(0));
1590 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1591 Tmp2 = CN->getValue() & 0x1F;
Nate Begeman33162522005-03-29 21:54:38 +00001592 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(32-Tmp2)
Nate Begeman5e966612005-03-24 06:28:42 +00001593 .addImm(Tmp2).addImm(31);
1594 } else {
1595 Tmp2 = SelectExpr(N.getOperand(1));
1596 BuildMI(BB, PPC::SRW, 2, Result).addReg(Tmp1).addReg(Tmp2);
1597 }
1598 return Result;
1599
1600 case ISD::SRA:
1601 Tmp1 = SelectExpr(N.getOperand(0));
1602 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1603 Tmp2 = CN->getValue() & 0x1F;
1604 BuildMI(BB, PPC::SRAWI, 2, Result).addReg(Tmp1).addImm(Tmp2);
1605 } else {
1606 Tmp2 = SelectExpr(N.getOperand(1));
1607 BuildMI(BB, PPC::SRAW, 2, Result).addReg(Tmp1).addReg(Tmp2);
1608 }
1609 return Result;
1610
Nate Begemana9795f82005-03-24 04:41:43 +00001611 case ISD::ADD:
1612 assert (DestType == MVT::i32 && "Only do arithmetic on i32s!");
1613 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman439b4442005-04-05 04:22:58 +00001614 switch(getImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
Nate Begemana9795f82005-03-24 04:41:43 +00001615 default: assert(0 && "unhandled result code");
1616 case 0: // No immediate
1617 Tmp2 = SelectExpr(N.getOperand(1));
1618 BuildMI(BB, PPC::ADD, 2, Result).addReg(Tmp1).addReg(Tmp2);
1619 break;
1620 case 1: // Low immediate
1621 BuildMI(BB, PPC::ADDI, 2, Result).addReg(Tmp1).addSImm(Tmp2);
1622 break;
1623 case 2: // Shifted immediate
1624 BuildMI(BB, PPC::ADDIS, 2, Result).addReg(Tmp1).addSImm(Tmp2);
1625 break;
1626 }
1627 return Result;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001628
Nate Begemana9795f82005-03-24 04:41:43 +00001629 case ISD::AND:
Nate Begeman7ddecb42005-04-06 23:51:40 +00001630 Tmp1 = SelectExpr(N.getOperand(0));
1631 // FIXME: should add check in getImmediateForOpcode to return a value
1632 // indicating the immediate is a run of set bits so we can emit a bitfield
1633 // clear with RLWINM instead.
1634 switch(getImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
1635 default: assert(0 && "unhandled result code");
1636 case 0: // No immediate
1637 Tmp2 = SelectExpr(N.getOperand(1));
1638 BuildMI(BB, PPC::AND, 2, Result).addReg(Tmp1).addReg(Tmp2);
1639 break;
1640 case 1: // Low immediate
1641 BuildMI(BB, PPC::ANDIo, 2, Result).addReg(Tmp1).addImm(Tmp2);
1642 break;
1643 case 2: // Shifted immediate
1644 BuildMI(BB, PPC::ANDISo, 2, Result).addReg(Tmp1).addImm(Tmp2);
1645 break;
1646 }
1647 return Result;
1648
Nate Begemana9795f82005-03-24 04:41:43 +00001649 case ISD::OR:
Nate Begeman7ddecb42005-04-06 23:51:40 +00001650 if (SelectBitfieldInsert(N, Result))
1651 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001652 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman439b4442005-04-05 04:22:58 +00001653 switch(getImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
Nate Begemana9795f82005-03-24 04:41:43 +00001654 default: assert(0 && "unhandled result code");
1655 case 0: // No immediate
1656 Tmp2 = SelectExpr(N.getOperand(1));
Nate Begeman7ddecb42005-04-06 23:51:40 +00001657 BuildMI(BB, PPC::OR, 2, Result).addReg(Tmp1).addReg(Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +00001658 break;
1659 case 1: // Low immediate
Nate Begeman7ddecb42005-04-06 23:51:40 +00001660 BuildMI(BB, PPC::ORI, 2, Result).addReg(Tmp1).addImm(Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +00001661 break;
1662 case 2: // Shifted immediate
Nate Begeman7ddecb42005-04-06 23:51:40 +00001663 BuildMI(BB, PPC::ORIS, 2, Result).addReg(Tmp1).addImm(Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +00001664 break;
1665 }
1666 return Result;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001667
Nate Begemanaa73a9f2005-04-03 11:20:20 +00001668 case ISD::XOR: {
1669 // Check for EQV: xor, (xor a, -1), b
1670 if (N.getOperand(0).getOpcode() == ISD::XOR &&
1671 N.getOperand(0).getOperand(1).getOpcode() == ISD::Constant &&
1672 cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->isAllOnesValue()) {
1673 ++NotLogic;
1674 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1675 Tmp2 = SelectExpr(N.getOperand(1));
1676 BuildMI(BB, PPC::EQV, 2, Result).addReg(Tmp1).addReg(Tmp2);
1677 return Result;
1678 }
1679 // Check for NOT, NOR, and NAND: xor (copy, or, and), -1
1680 if (N.getOperand(1).getOpcode() == ISD::Constant &&
1681 cast<ConstantSDNode>(N.getOperand(1))->isAllOnesValue()) {
1682 ++NotLogic;
1683 switch(N.getOperand(0).getOpcode()) {
1684 case ISD::OR:
1685 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1686 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1687 BuildMI(BB, PPC::NOR, 2, Result).addReg(Tmp1).addReg(Tmp2);
1688 break;
1689 case ISD::AND:
1690 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1691 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1692 BuildMI(BB, PPC::NAND, 2, Result).addReg(Tmp1).addReg(Tmp2);
1693 break;
1694 default:
1695 Tmp1 = SelectExpr(N.getOperand(0));
1696 BuildMI(BB, PPC::NOR, 2, Result).addReg(Tmp1).addReg(Tmp1);
1697 break;
1698 }
1699 return Result;
1700 }
1701 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman439b4442005-04-05 04:22:58 +00001702 switch(getImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
Nate Begemanaa73a9f2005-04-03 11:20:20 +00001703 default: assert(0 && "unhandled result code");
1704 case 0: // No immediate
1705 Tmp2 = SelectExpr(N.getOperand(1));
1706 BuildMI(BB, PPC::XOR, 2, Result).addReg(Tmp1).addReg(Tmp2);
1707 break;
1708 case 1: // Low immediate
1709 BuildMI(BB, PPC::XORI, 2, Result).addReg(Tmp1).addImm(Tmp2);
1710 break;
1711 case 2: // Shifted immediate
1712 BuildMI(BB, PPC::XORIS, 2, Result).addReg(Tmp1).addImm(Tmp2);
1713 break;
1714 }
1715 return Result;
1716 }
1717
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001718 case ISD::SUB:
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001719 Tmp2 = SelectExpr(N.getOperand(1));
Nate Begeman439b4442005-04-05 04:22:58 +00001720 if (1 == getImmediateForOpcode(N.getOperand(0), opcode, Tmp1))
Nate Begeman27523a12005-04-02 00:42:16 +00001721 BuildMI(BB, PPC::SUBFIC, 2, Result).addReg(Tmp2).addSImm(Tmp1);
1722 else {
1723 Tmp1 = SelectExpr(N.getOperand(0));
1724 BuildMI(BB, PPC::SUBF, 2, Result).addReg(Tmp2).addReg(Tmp1);
1725 }
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001726 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001727
Nate Begeman5e966612005-03-24 06:28:42 +00001728 case ISD::MUL:
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001729 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman439b4442005-04-05 04:22:58 +00001730 if (1 == getImmediateForOpcode(N.getOperand(1), opcode, Tmp2))
Nate Begeman307e7442005-03-26 01:28:53 +00001731 BuildMI(BB, PPC::MULLI, 2, Result).addReg(Tmp1).addSImm(Tmp2);
1732 else {
1733 Tmp2 = SelectExpr(N.getOperand(1));
1734 BuildMI(BB, PPC::MULLW, 2, Result).addReg(Tmp1).addReg(Tmp2);
1735 }
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001736 return Result;
1737
Nate Begeman815d6da2005-04-06 00:25:27 +00001738 case ISD::MULHS:
1739 case ISD::MULHU:
1740 Tmp1 = SelectExpr(N.getOperand(0));
1741 Tmp2 = SelectExpr(N.getOperand(1));
1742 Opc = (ISD::MULHU == opcode) ? PPC::MULHWU : PPC::MULHW;
1743 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1744 return Result;
1745
Nate Begemanf3d08f32005-03-29 00:03:27 +00001746 case ISD::SDIV:
1747 case ISD::UDIV:
Nate Begeman815d6da2005-04-06 00:25:27 +00001748 switch (getImmediateForOpcode(N.getOperand(1), opcode, Tmp3)) {
1749 default: break;
1750 // If this is an sdiv by a power of two, we can use an srawi/addze pair.
1751 case 3:
Nate Begeman80196b12005-04-05 00:15:08 +00001752 Tmp1 = MakeReg(MVT::i32);
1753 Tmp2 = SelectExpr(N.getOperand(0));
1754 BuildMI(BB, PPC::SRAWI, 2, Tmp1).addReg(Tmp2).addImm(Tmp3);
1755 BuildMI(BB, PPC::ADDZE, 1, Result).addReg(Tmp1);
1756 return Result;
Nate Begeman815d6da2005-04-06 00:25:27 +00001757 // If this is a divide by constant, we can emit code using some magic
1758 // constants to implement it as a multiply instead.
Nate Begeman27b4c232005-04-06 06:44:57 +00001759 case 4:
1760 ExprMap.erase(N);
1761 if (opcode == ISD::SDIV)
1762 return SelectExpr(BuildSDIVSequence(N));
1763 else
1764 return SelectExpr(BuildUDIVSequence(N));
Nate Begeman80196b12005-04-05 00:15:08 +00001765 }
Nate Begemanf3d08f32005-03-29 00:03:27 +00001766 Tmp1 = SelectExpr(N.getOperand(0));
1767 Tmp2 = SelectExpr(N.getOperand(1));
1768 Opc = (ISD::UDIV == opcode) ? PPC::DIVWU : PPC::DIVW;
1769 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1770 return Result;
1771
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001772 case ISD::ADD_PARTS:
Nate Begemanca12a2b2005-03-28 22:28:37 +00001773 case ISD::SUB_PARTS: {
1774 assert(N.getNumOperands() == 4 && N.getValueType() == MVT::i32 &&
1775 "Not an i64 add/sub!");
1776 // Emit all of the operands.
1777 std::vector<unsigned> InVals;
1778 for (unsigned i = 0, e = N.getNumOperands(); i != e; ++i)
1779 InVals.push_back(SelectExpr(N.getOperand(i)));
1780 if (N.getOpcode() == ISD::ADD_PARTS) {
Nate Begeman27eeb002005-04-02 05:59:34 +00001781 BuildMI(BB, PPC::ADDC, 2, Result).addReg(InVals[0]).addReg(InVals[2]);
1782 BuildMI(BB, PPC::ADDE, 2, Result+1).addReg(InVals[1]).addReg(InVals[3]);
Nate Begemanca12a2b2005-03-28 22:28:37 +00001783 } else {
Nate Begeman27eeb002005-04-02 05:59:34 +00001784 BuildMI(BB, PPC::SUBFC, 2, Result).addReg(InVals[2]).addReg(InVals[0]);
1785 BuildMI(BB, PPC::SUBFE, 2, Result+1).addReg(InVals[3]).addReg(InVals[1]);
1786 }
1787 return Result+N.ResNo;
1788 }
1789
1790 case ISD::SHL_PARTS:
1791 case ISD::SRA_PARTS:
1792 case ISD::SRL_PARTS: {
1793 assert(N.getNumOperands() == 3 && N.getValueType() == MVT::i32 &&
1794 "Not an i64 shift!");
1795 unsigned ShiftOpLo = SelectExpr(N.getOperand(0));
1796 unsigned ShiftOpHi = SelectExpr(N.getOperand(1));
1797 unsigned SHReg = SelectExpr(N.getOperand(2));
1798 Tmp1 = MakeReg(MVT::i32);
1799 Tmp2 = MakeReg(MVT::i32);
1800 Tmp3 = MakeReg(MVT::i32);
1801 unsigned Tmp4 = MakeReg(MVT::i32);
1802 unsigned Tmp5 = MakeReg(MVT::i32);
1803 unsigned Tmp6 = MakeReg(MVT::i32);
1804 BuildMI(BB, PPC::SUBFIC, 2, Tmp1).addReg(SHReg).addSImm(32);
1805 if (ISD::SHL_PARTS == opcode) {
1806 BuildMI(BB, PPC::SLW, 2, Tmp2).addReg(ShiftOpHi).addReg(SHReg);
1807 BuildMI(BB, PPC::SRW, 2, Tmp3).addReg(ShiftOpLo).addReg(Tmp1);
1808 BuildMI(BB, PPC::OR, 2, Tmp4).addReg(Tmp2).addReg(Tmp3);
1809 BuildMI(BB, PPC::ADDI, 2, Tmp5).addReg(SHReg).addSImm(-32);
Nate Begemanfa554702005-04-03 22:13:27 +00001810 BuildMI(BB, PPC::SLW, 2, Tmp6).addReg(ShiftOpLo).addReg(Tmp5);
Nate Begeman27eeb002005-04-02 05:59:34 +00001811 BuildMI(BB, PPC::OR, 2, Result+1).addReg(Tmp4).addReg(Tmp6);
1812 BuildMI(BB, PPC::SLW, 2, Result).addReg(ShiftOpLo).addReg(SHReg);
1813 } else if (ISD::SRL_PARTS == opcode) {
1814 BuildMI(BB, PPC::SRW, 2, Tmp2).addReg(ShiftOpLo).addReg(SHReg);
1815 BuildMI(BB, PPC::SLW, 2, Tmp3).addReg(ShiftOpHi).addReg(Tmp1);
1816 BuildMI(BB, PPC::OR, 2, Tmp4).addReg(Tmp2).addReg(Tmp3);
1817 BuildMI(BB, PPC::ADDI, 2, Tmp5).addReg(SHReg).addSImm(-32);
1818 BuildMI(BB, PPC::SRW, 2, Tmp6).addReg(ShiftOpHi).addReg(Tmp5);
1819 BuildMI(BB, PPC::OR, 2, Result).addReg(Tmp4).addReg(Tmp6);
1820 BuildMI(BB, PPC::SRW, 2, Result+1).addReg(ShiftOpHi).addReg(SHReg);
1821 } else {
1822 MachineBasicBlock *TmpMBB = new MachineBasicBlock(BB->getBasicBlock());
1823 MachineBasicBlock *PhiMBB = new MachineBasicBlock(BB->getBasicBlock());
1824 MachineBasicBlock *OldMBB = BB;
1825 MachineFunction *F = BB->getParent();
1826 ilist<MachineBasicBlock>::iterator It = BB; ++It;
1827 F->getBasicBlockList().insert(It, TmpMBB);
1828 F->getBasicBlockList().insert(It, PhiMBB);
1829 BB->addSuccessor(TmpMBB);
1830 BB->addSuccessor(PhiMBB);
1831 BuildMI(BB, PPC::SRW, 2, Tmp2).addReg(ShiftOpLo).addReg(SHReg);
1832 BuildMI(BB, PPC::SLW, 2, Tmp3).addReg(ShiftOpHi).addReg(Tmp1);
1833 BuildMI(BB, PPC::OR, 2, Tmp4).addReg(Tmp2).addReg(Tmp3);
1834 BuildMI(BB, PPC::ADDICo, 2, Tmp5).addReg(SHReg).addSImm(-32);
1835 BuildMI(BB, PPC::SRAW, 2, Tmp6).addReg(ShiftOpHi).addReg(Tmp5);
1836 BuildMI(BB, PPC::SRAW, 2, Result+1).addReg(ShiftOpHi).addReg(SHReg);
1837 BuildMI(BB, PPC::BLE, 2).addReg(PPC::CR0).addMBB(PhiMBB);
1838 // Select correct least significant half if the shift amount > 32
1839 BB = TmpMBB;
1840 unsigned Tmp7 = MakeReg(MVT::i32);
1841 BuildMI(BB, PPC::OR, 2, Tmp7).addReg(Tmp6).addReg(Tmp6);
1842 TmpMBB->addSuccessor(PhiMBB);
1843 BB = PhiMBB;
1844 BuildMI(BB, PPC::PHI, 4, Result).addReg(Tmp4).addMBB(OldMBB)
1845 .addReg(Tmp7).addMBB(TmpMBB);
Nate Begemanca12a2b2005-03-28 22:28:37 +00001846 }
1847 return Result+N.ResNo;
1848 }
1849
Nate Begemana9795f82005-03-24 04:41:43 +00001850 case ISD::FP_TO_UINT:
Nate Begeman6b559972005-04-01 02:59:27 +00001851 case ISD::FP_TO_SINT: {
1852 bool U = (ISD::FP_TO_UINT == opcode);
1853 Tmp1 = SelectExpr(N.getOperand(0));
1854 if (!U) {
1855 Tmp2 = MakeReg(MVT::f64);
1856 BuildMI(BB, PPC::FCTIWZ, 1, Tmp2).addReg(Tmp1);
1857 int FrameIdx = BB->getParent()->getFrameInfo()->CreateStackObject(8, 8);
1858 addFrameReference(BuildMI(BB, PPC::STFD, 3).addReg(Tmp2), FrameIdx);
1859 addFrameReference(BuildMI(BB, PPC::LWZ, 2, Result), FrameIdx, 4);
1860 return Result;
1861 } else {
1862 unsigned Zero = getConstDouble(0.0);
1863 unsigned MaxInt = getConstDouble((1LL << 32) - 1);
1864 unsigned Border = getConstDouble(1LL << 31);
1865 unsigned UseZero = MakeReg(MVT::f64);
1866 unsigned UseMaxInt = MakeReg(MVT::f64);
1867 unsigned UseChoice = MakeReg(MVT::f64);
1868 unsigned TmpReg = MakeReg(MVT::f64);
1869 unsigned TmpReg2 = MakeReg(MVT::f64);
1870 unsigned ConvReg = MakeReg(MVT::f64);
1871 unsigned IntTmp = MakeReg(MVT::i32);
1872 unsigned XorReg = MakeReg(MVT::i32);
1873 MachineFunction *F = BB->getParent();
1874 int FrameIdx = F->getFrameInfo()->CreateStackObject(8, 8);
1875 // Update machine-CFG edges
1876 MachineBasicBlock *XorMBB = new MachineBasicBlock(BB->getBasicBlock());
1877 MachineBasicBlock *PhiMBB = new MachineBasicBlock(BB->getBasicBlock());
1878 MachineBasicBlock *OldMBB = BB;
1879 ilist<MachineBasicBlock>::iterator It = BB; ++It;
1880 F->getBasicBlockList().insert(It, XorMBB);
1881 F->getBasicBlockList().insert(It, PhiMBB);
1882 BB->addSuccessor(XorMBB);
1883 BB->addSuccessor(PhiMBB);
1884 // Convert from floating point to unsigned 32-bit value
1885 // Use 0 if incoming value is < 0.0
1886 BuildMI(BB, PPC::FSEL, 3, UseZero).addReg(Tmp1).addReg(Tmp1).addReg(Zero);
1887 // Use 2**32 - 1 if incoming value is >= 2**32
1888 BuildMI(BB, PPC::FSUB, 2, UseMaxInt).addReg(MaxInt).addReg(Tmp1);
1889 BuildMI(BB, PPC::FSEL, 3, UseChoice).addReg(UseMaxInt).addReg(UseZero)
1890 .addReg(MaxInt);
1891 // Subtract 2**31
1892 BuildMI(BB, PPC::FSUB, 2, TmpReg).addReg(UseChoice).addReg(Border);
1893 // Use difference if >= 2**31
1894 BuildMI(BB, PPC::FCMPU, 2, PPC::CR0).addReg(UseChoice).addReg(Border);
1895 BuildMI(BB, PPC::FSEL, 3, TmpReg2).addReg(TmpReg).addReg(TmpReg)
1896 .addReg(UseChoice);
1897 // Convert to integer
1898 BuildMI(BB, PPC::FCTIWZ, 1, ConvReg).addReg(TmpReg2);
1899 addFrameReference(BuildMI(BB, PPC::STFD, 3).addReg(ConvReg), FrameIdx);
1900 addFrameReference(BuildMI(BB, PPC::LWZ, 2, IntTmp), FrameIdx, 4);
1901 BuildMI(BB, PPC::BLT, 2).addReg(PPC::CR0).addMBB(PhiMBB);
1902 BuildMI(BB, PPC::B, 1).addMBB(XorMBB);
1903
1904 // XorMBB:
1905 // add 2**31 if input was >= 2**31
1906 BB = XorMBB;
1907 BuildMI(BB, PPC::XORIS, 2, XorReg).addReg(IntTmp).addImm(0x8000);
1908 XorMBB->addSuccessor(PhiMBB);
1909
1910 // PhiMBB:
1911 // DestReg = phi [ IntTmp, OldMBB ], [ XorReg, XorMBB ]
1912 BB = PhiMBB;
1913 BuildMI(BB, PPC::PHI, 4, Result).addReg(IntTmp).addMBB(OldMBB)
1914 .addReg(XorReg).addMBB(XorMBB);
1915 return Result;
1916 }
1917 assert(0 && "Should never get here");
1918 return 0;
1919 }
Nate Begemana9795f82005-03-24 04:41:43 +00001920
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001921 case ISD::SETCC:
Nate Begeman33162522005-03-29 21:54:38 +00001922 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Node)) {
Nate Begeman7e7fadd2005-04-07 20:30:01 +00001923 // We can codegen setcc op, 0 very efficiently compared to a conditional
1924 // branch. Check for that here.
1925 if (ConstantSDNode *CN =
1926 dyn_cast<ConstantSDNode>(SetCC->getOperand(1).Val)) {
1927 if (CN->getValue() == 0) {
1928 Tmp1 = SelectExpr(SetCC->getOperand(0));
1929 switch (SetCC->getCondition()) {
1930 default: assert(0 && "Unhandled SetCC condition"); abort();
1931 case ISD::SETEQ:
1932 case ISD::SETULE:
1933 Tmp2 = MakeReg(MVT::i32);
1934 BuildMI(BB, PPC::CNTLZW, 1, Tmp2).addReg(Tmp1);
1935 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp2).addImm(27)
1936 .addImm(5).addImm(31);
1937 break;
1938 case ISD::SETNE:
1939 case ISD::SETUGT:
1940 Tmp2 = MakeReg(MVT::i32);
1941 BuildMI(BB, PPC::ADDIC, 2, Tmp2).addReg(Tmp1).addSImm(-1);
1942 BuildMI(BB, PPC::SUBFE, 2, Result).addReg(Tmp2).addReg(Tmp1);
1943 break;
1944 case ISD::SETULT:
1945 BuildMI(BB, PPC::LI, 1, Result).addSImm(0);
1946 break;
1947 case ISD::SETLT:
1948 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(1)
1949 .addImm(31).addImm(31);
1950 break;
1951 case ISD::SETLE:
1952 Tmp2 = MakeReg(MVT::i32);
1953 Tmp3 = MakeReg(MVT::i32);
1954 BuildMI(BB, PPC::NEG, 2, Tmp2).addReg(Tmp1);
1955 BuildMI(BB, PPC::ORC, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
1956 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp3).addImm(1)
1957 .addImm(31).addImm(31);
1958 break;
1959 case ISD::SETGT:
1960 Tmp2 = MakeReg(MVT::i32);
1961 Tmp3 = MakeReg(MVT::i32);
1962 BuildMI(BB, PPC::NEG, 2, Tmp2).addReg(Tmp1);
1963 BuildMI(BB, PPC::ANDC, 2, Tmp3).addReg(Tmp2).addReg(Tmp1);
1964 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp3).addImm(1)
1965 .addImm(31).addImm(31);
1966 break;
1967 case ISD::SETUGE:
1968 BuildMI(BB, PPC::LI, 1, Result).addSImm(1);
1969 break;
1970 case ISD::SETGE:
1971 BuildMI(BB, PPC::RLWINM, 4, Tmp2).addReg(Tmp1).addImm(1)
1972 .addImm(31).addImm(31);
1973 BuildMI(BB, PPC::XORI, 2, Result).addReg(Tmp2).addImm(1);
1974 break;
1975 }
1976 return Result;
1977 }
1978 }
1979
Nate Begemandffcfcc2005-04-01 00:32:34 +00001980 Opc = SelectSetCR0(N);
Nate Begeman31318e42005-04-01 07:21:30 +00001981 unsigned TrueValue = MakeReg(MVT::i32);
1982 BuildMI(BB, PPC::LI, 1, TrueValue).addSImm(1);
1983 unsigned FalseValue = MakeReg(MVT::i32);
1984 BuildMI(BB, PPC::LI, 1, FalseValue).addSImm(0);
1985
Nate Begeman33162522005-03-29 21:54:38 +00001986 // Create an iterator with which to insert the MBB for copying the false
1987 // value and the MBB to hold the PHI instruction for this SetCC.
1988 MachineBasicBlock *thisMBB = BB;
1989 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1990 ilist<MachineBasicBlock>::iterator It = BB;
1991 ++It;
1992
1993 // thisMBB:
1994 // ...
1995 // cmpTY cr0, r1, r2
1996 // %TrueValue = li 1
1997 // bCC sinkMBB
Nate Begeman33162522005-03-29 21:54:38 +00001998 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
1999 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
2000 BuildMI(BB, Opc, 2).addReg(PPC::CR0).addMBB(sinkMBB);
2001 MachineFunction *F = BB->getParent();
2002 F->getBasicBlockList().insert(It, copy0MBB);
2003 F->getBasicBlockList().insert(It, sinkMBB);
2004 // Update machine-CFG edges
2005 BB->addSuccessor(copy0MBB);
2006 BB->addSuccessor(sinkMBB);
2007
2008 // copy0MBB:
2009 // %FalseValue = li 0
2010 // fallthrough
2011 BB = copy0MBB;
Nate Begeman33162522005-03-29 21:54:38 +00002012 // Update machine-CFG edges
2013 BB->addSuccessor(sinkMBB);
2014
2015 // sinkMBB:
2016 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
2017 // ...
2018 BB = sinkMBB;
2019 BuildMI(BB, PPC::PHI, 4, Result).addReg(FalseValue)
2020 .addMBB(copy0MBB).addReg(TrueValue).addMBB(thisMBB);
2021 return Result;
2022 }
2023 assert(0 && "Is this legal?");
2024 return 0;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00002025
Nate Begeman74747862005-03-29 22:24:51 +00002026 case ISD::SELECT: {
Nate Begeman7e7fadd2005-04-07 20:30:01 +00002027 // We can codegen select (a < 0) ? b : 0 very efficiently compared to a
2028 // conditional branch. Check for that here.
2029 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(N.getOperand(0).Val)) {
2030 if (ConstantSDNode *CN =
2031 dyn_cast<ConstantSDNode>(SetCC->getOperand(1).Val)) {
2032 if (ConstantSDNode *CNF =
2033 dyn_cast<ConstantSDNode>(N.getOperand(2).Val)) {
2034 if (CN->getValue() == 0 && CNF->getValue() == 0 &&
2035 SetCC->getCondition() == ISD::SETLT) {
2036 Tmp1 = SelectExpr(N.getOperand(1)); // TRUE value
2037 Tmp2 = SelectExpr(SetCC->getOperand(0));
2038 Tmp3 = MakeReg(MVT::i32);
2039 BuildMI(BB, PPC::SRAWI, 2, Tmp3).addReg(Tmp2).addImm(31);
2040 BuildMI(BB, PPC::AND, 2, Result).addReg(Tmp1).addReg(Tmp3);
2041 return Result;
2042 }
2043 }
2044 }
2045 }
Chris Lattner30710192005-04-01 07:10:02 +00002046 unsigned TrueValue = SelectExpr(N.getOperand(1)); //Use if TRUE
2047 unsigned FalseValue = SelectExpr(N.getOperand(2)); //Use if FALSE
Nate Begeman6cb2e1b2005-04-01 08:57:43 +00002048 Opc = SelectSetCR0(N.getOperand(0));
Chris Lattner30710192005-04-01 07:10:02 +00002049
Nate Begeman74747862005-03-29 22:24:51 +00002050 // Create an iterator with which to insert the MBB for copying the false
2051 // value and the MBB to hold the PHI instruction for this SetCC.
2052 MachineBasicBlock *thisMBB = BB;
2053 const BasicBlock *LLVM_BB = BB->getBasicBlock();
2054 ilist<MachineBasicBlock>::iterator It = BB;
2055 ++It;
2056
2057 // thisMBB:
2058 // ...
2059 // TrueVal = ...
2060 // cmpTY cr0, r1, r2
2061 // bCC copy1MBB
2062 // fallthrough --> copy0MBB
Nate Begeman74747862005-03-29 22:24:51 +00002063 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
2064 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
Nate Begeman3e897162005-03-31 23:55:40 +00002065 BuildMI(BB, Opc, 2).addReg(PPC::CR0).addMBB(sinkMBB);
Nate Begeman74747862005-03-29 22:24:51 +00002066 MachineFunction *F = BB->getParent();
2067 F->getBasicBlockList().insert(It, copy0MBB);
2068 F->getBasicBlockList().insert(It, sinkMBB);
2069 // Update machine-CFG edges
2070 BB->addSuccessor(copy0MBB);
2071 BB->addSuccessor(sinkMBB);
2072
2073 // copy0MBB:
2074 // %FalseValue = ...
2075 // # fallthrough to sinkMBB
2076 BB = copy0MBB;
Nate Begeman74747862005-03-29 22:24:51 +00002077 // Update machine-CFG edges
2078 BB->addSuccessor(sinkMBB);
2079
2080 // sinkMBB:
2081 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
2082 // ...
2083 BB = sinkMBB;
2084 BuildMI(BB, PPC::PHI, 4, Result).addReg(FalseValue)
2085 .addMBB(copy0MBB).addReg(TrueValue).addMBB(thisMBB);
Nate Begeman74747862005-03-29 22:24:51 +00002086 return Result;
2087 }
Nate Begemana9795f82005-03-24 04:41:43 +00002088
2089 case ISD::Constant:
2090 switch (N.getValueType()) {
2091 default: assert(0 && "Cannot use constants of this type!");
2092 case MVT::i1:
2093 BuildMI(BB, PPC::LI, 1, Result)
2094 .addSImm(!cast<ConstantSDNode>(N)->isNullValue());
2095 break;
2096 case MVT::i32:
2097 {
2098 int v = (int)cast<ConstantSDNode>(N)->getSignExtended();
2099 if (v < 32768 && v >= -32768) {
2100 BuildMI(BB, PPC::LI, 1, Result).addSImm(v);
2101 } else {
Nate Begeman5e966612005-03-24 06:28:42 +00002102 Tmp1 = MakeReg(MVT::i32);
2103 BuildMI(BB, PPC::LIS, 1, Tmp1).addSImm(v >> 16);
2104 BuildMI(BB, PPC::ORI, 2, Result).addReg(Tmp1).addImm(v & 0xFFFF);
Nate Begemana9795f82005-03-24 04:41:43 +00002105 }
2106 }
2107 }
2108 return Result;
2109 }
2110
2111 return 0;
2112}
2113
2114void ISel::Select(SDOperand N) {
2115 unsigned Tmp1, Tmp2, Opc;
2116 unsigned opcode = N.getOpcode();
2117
2118 if (!ExprMap.insert(std::make_pair(N, 1)).second)
2119 return; // Already selected.
2120
2121 SDNode *Node = N.Val;
2122
2123 switch (Node->getOpcode()) {
2124 default:
2125 Node->dump(); std::cerr << "\n";
2126 assert(0 && "Node not handled yet!");
2127 case ISD::EntryToken: return; // Noop
2128 case ISD::TokenFactor:
2129 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
2130 Select(Node->getOperand(i));
2131 return;
2132 case ISD::ADJCALLSTACKDOWN:
2133 case ISD::ADJCALLSTACKUP:
2134 Select(N.getOperand(0));
2135 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
2136 Opc = N.getOpcode() == ISD::ADJCALLSTACKDOWN ? PPC::ADJCALLSTACKDOWN :
2137 PPC::ADJCALLSTACKUP;
2138 BuildMI(BB, Opc, 1).addImm(Tmp1);
2139 return;
2140 case ISD::BR: {
2141 MachineBasicBlock *Dest =
2142 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
Nate Begemana9795f82005-03-24 04:41:43 +00002143 Select(N.getOperand(0));
2144 BuildMI(BB, PPC::B, 1).addMBB(Dest);
2145 return;
2146 }
2147 case ISD::BRCOND:
2148 SelectBranchCC(N);
2149 return;
2150 case ISD::CopyToReg:
2151 Select(N.getOperand(0));
2152 Tmp1 = SelectExpr(N.getOperand(1));
2153 Tmp2 = cast<RegSDNode>(N)->getReg();
2154
2155 if (Tmp1 != Tmp2) {
2156 if (N.getOperand(1).getValueType() == MVT::f64 ||
2157 N.getOperand(1).getValueType() == MVT::f32)
2158 BuildMI(BB, PPC::FMR, 1, Tmp2).addReg(Tmp1);
2159 else
2160 BuildMI(BB, PPC::OR, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
2161 }
2162 return;
2163 case ISD::ImplicitDef:
2164 Select(N.getOperand(0));
2165 BuildMI(BB, PPC::IMPLICIT_DEF, 0, cast<RegSDNode>(N)->getReg());
2166 return;
2167 case ISD::RET:
2168 switch (N.getNumOperands()) {
2169 default:
2170 assert(0 && "Unknown return instruction!");
2171 case 3:
2172 assert(N.getOperand(1).getValueType() == MVT::i32 &&
2173 N.getOperand(2).getValueType() == MVT::i32 &&
2174 "Unknown two-register value!");
2175 Select(N.getOperand(0));
2176 Tmp1 = SelectExpr(N.getOperand(1));
2177 Tmp2 = SelectExpr(N.getOperand(2));
Nate Begeman27523a12005-04-02 00:42:16 +00002178 BuildMI(BB, PPC::OR, 2, PPC::R3).addReg(Tmp2).addReg(Tmp2);
2179 BuildMI(BB, PPC::OR, 2, PPC::R4).addReg(Tmp1).addReg(Tmp1);
Nate Begemana9795f82005-03-24 04:41:43 +00002180 break;
2181 case 2:
2182 Select(N.getOperand(0));
2183 Tmp1 = SelectExpr(N.getOperand(1));
2184 switch (N.getOperand(1).getValueType()) {
2185 default:
2186 assert(0 && "Unknown return type!");
2187 case MVT::f64:
2188 case MVT::f32:
2189 BuildMI(BB, PPC::FMR, 1, PPC::F1).addReg(Tmp1);
2190 break;
2191 case MVT::i32:
2192 BuildMI(BB, PPC::OR, 2, PPC::R3).addReg(Tmp1).addReg(Tmp1);
2193 break;
2194 }
Nate Begeman9e3e1b52005-03-24 23:35:30 +00002195 case 1:
2196 Select(N.getOperand(0));
2197 break;
Nate Begemana9795f82005-03-24 04:41:43 +00002198 }
2199 BuildMI(BB, PPC::BLR, 0); // Just emit a 'ret' instruction
2200 return;
Nate Begemana9795f82005-03-24 04:41:43 +00002201 case ISD::TRUNCSTORE:
2202 case ISD::STORE:
2203 {
2204 SDOperand Chain = N.getOperand(0);
2205 SDOperand Value = N.getOperand(1);
2206 SDOperand Address = N.getOperand(2);
2207 Select(Chain);
2208
2209 Tmp1 = SelectExpr(Value); //value
2210
2211 if (opcode == ISD::STORE) {
2212 switch(Value.getValueType()) {
2213 default: assert(0 && "unknown Type in store");
2214 case MVT::i32: Opc = PPC::STW; break;
2215 case MVT::f64: Opc = PPC::STFD; break;
2216 case MVT::f32: Opc = PPC::STFS; break;
2217 }
2218 } else { //ISD::TRUNCSTORE
2219 switch(cast<MVTSDNode>(Node)->getExtraValueType()) {
2220 default: assert(0 && "unknown Type in store");
Nate Begeman7e7fadd2005-04-07 20:30:01 +00002221 case MVT::i1:
Nate Begemana9795f82005-03-24 04:41:43 +00002222 case MVT::i8: Opc = PPC::STB; break;
2223 case MVT::i16: Opc = PPC::STH; break;
2224 }
2225 }
2226
Nate Begemana7e11a42005-04-01 05:57:17 +00002227 if(Address.getOpcode() == ISD::FrameIndex)
Nate Begemana9795f82005-03-24 04:41:43 +00002228 {
Nate Begeman58f718c2005-03-30 02:23:08 +00002229 Tmp2 = cast<FrameIndexSDNode>(Address)->getIndex();
2230 addFrameReference(BuildMI(BB, Opc, 3).addReg(Tmp1), (int)Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +00002231 }
2232 else
2233 {
2234 int offset;
Nate Begeman04730362005-04-01 04:45:11 +00002235 bool idx = SelectAddr(Address, Tmp2, offset);
2236 if (idx) {
2237 Opc = IndexedOpForOp(Opc);
2238 BuildMI(BB, Opc, 3).addReg(Tmp1).addReg(Tmp2).addReg(offset);
2239 } else {
2240 BuildMI(BB, Opc, 3).addReg(Tmp1).addImm(offset).addReg(Tmp2);
2241 }
Nate Begemana9795f82005-03-24 04:41:43 +00002242 }
2243 return;
2244 }
2245 case ISD::EXTLOAD:
2246 case ISD::SEXTLOAD:
2247 case ISD::ZEXTLOAD:
2248 case ISD::LOAD:
2249 case ISD::CopyFromReg:
2250 case ISD::CALL:
2251 case ISD::DYNAMIC_STACKALLOC:
2252 ExprMap.erase(N);
2253 SelectExpr(N);
2254 return;
2255 }
2256 assert(0 && "Should not be reached!");
2257}
2258
2259
2260/// createPPC32PatternInstructionSelector - This pass converts an LLVM function
2261/// into a machine code representation using pattern matching and a machine
2262/// description file.
2263///
2264FunctionPass *llvm::createPPC32ISelPattern(TargetMachine &TM) {
2265 return new ISel(TM);
Chris Lattner246fa632005-03-24 06:16:18 +00002266}
2267