blob: 586ed7ac030f3ab773eb590b84823a8195c93e67 [file] [log] [blame]
Nate Begemana9795f82005-03-24 04:41:43 +00001//===-- PPC32ISelPattern.cpp - A pattern matching inst selector for PPC32 -===//
2//
3// The LLVM Compiler Infrastructure
4//
Nate Begeman5e966612005-03-24 06:28:42 +00005// This file was developed by Nate Begeman and is distributed under
Nate Begemana9795f82005-03-24 04:41:43 +00006// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines a pattern matching instruction selector for 32 bit PowerPC.
Nate Begeman815d6da2005-04-06 00:25:27 +000011// Magic number generation for integer divide from the PowerPC Compiler Writer's
12// Guide, section 3.2.3.5
Nate Begemana9795f82005-03-24 04:41:43 +000013//
14//===----------------------------------------------------------------------===//
15
16#include "PowerPC.h"
17#include "PowerPCInstrBuilder.h"
18#include "PowerPCInstrInfo.h"
Nate Begemancd08e4c2005-04-09 20:09:12 +000019#include "PPC32TargetMachine.h"
Nate Begemana9795f82005-03-24 04:41:43 +000020#include "llvm/Constants.h" // FIXME: REMOVE
21#include "llvm/Function.h"
22#include "llvm/CodeGen/MachineConstantPool.h" // FIXME: REMOVE
23#include "llvm/CodeGen/MachineFunction.h"
24#include "llvm/CodeGen/MachineFrameInfo.h"
25#include "llvm/CodeGen/SelectionDAG.h"
26#include "llvm/CodeGen/SelectionDAGISel.h"
27#include "llvm/CodeGen/SSARegMap.h"
28#include "llvm/Target/TargetData.h"
29#include "llvm/Target/TargetLowering.h"
Nate Begeman93075ec2005-04-04 23:40:36 +000030#include "llvm/Target/TargetOptions.h"
Nate Begemana9795f82005-03-24 04:41:43 +000031#include "llvm/Support/Debug.h"
32#include "llvm/Support/MathExtras.h"
33#include "llvm/ADT/Statistic.h"
34#include <set>
35#include <algorithm>
36using namespace llvm;
37
38//===----------------------------------------------------------------------===//
39// PPC32TargetLowering - PPC32 Implementation of the TargetLowering interface
40namespace {
41 class PPC32TargetLowering : public TargetLowering {
42 int VarArgsFrameIndex; // FrameIndex for start of varargs area.
43 int ReturnAddrIndex; // FrameIndex for return slot.
44 public:
45 PPC32TargetLowering(TargetMachine &TM) : TargetLowering(TM) {
Nate Begemana9795f82005-03-24 04:41:43 +000046 // Set up the register classes.
47 addRegisterClass(MVT::i32, PPC32::GPRCRegisterClass);
Nate Begeman7532e2f2005-03-26 08:25:22 +000048 addRegisterClass(MVT::f32, PPC32::FPRCRegisterClass);
Nate Begemana9795f82005-03-24 04:41:43 +000049 addRegisterClass(MVT::f64, PPC32::FPRCRegisterClass);
50
Nate Begeman74d73452005-03-31 00:15:26 +000051 // PowerPC has no intrinsics for these particular operations
Nate Begeman01d05262005-03-30 01:45:43 +000052 setOperationAction(ISD::MEMMOVE, MVT::Other, Expand);
53 setOperationAction(ISD::MEMSET, MVT::Other, Expand);
54 setOperationAction(ISD::MEMCPY, MVT::Other, Expand);
55
Nate Begeman74d73452005-03-31 00:15:26 +000056 // PowerPC has an i16 but no i8 (or i1) SEXTLOAD
57 setOperationAction(ISD::SEXTLOAD, MVT::i1, Expand);
58 setOperationAction(ISD::SEXTLOAD, MVT::i8, Expand);
Nate Begeman815d6da2005-04-06 00:25:27 +000059
60 // PowerPC has no SREM/UREM instructions
61 setOperationAction(ISD::SREM, MVT::i32, Expand);
62 setOperationAction(ISD::UREM, MVT::i32, Expand);
Chris Lattner43fdea02005-04-02 05:03:24 +000063
Nate Begeman27eeb002005-04-02 05:59:34 +000064 setShiftAmountFlavor(Extend); // shl X, 32 == 0
Chris Lattnercbd06fc2005-04-07 19:41:49 +000065 setSetCCResultContents(ZeroOrOneSetCCResult);
Nate Begeman3e897162005-03-31 23:55:40 +000066 addLegalFPImmediate(+0.0); // Necessary for FSEL
67 addLegalFPImmediate(-0.0); //
68
Nate Begemana9795f82005-03-24 04:41:43 +000069 computeRegisterProperties();
70 }
71
72 /// LowerArguments - This hook must be implemented to indicate how we should
73 /// lower the arguments for the specified function, into the specified DAG.
74 virtual std::vector<SDOperand>
75 LowerArguments(Function &F, SelectionDAG &DAG);
76
77 /// LowerCallTo - This hook lowers an abstract call to a function into an
78 /// actual call.
79 virtual std::pair<SDOperand, SDOperand>
Nate Begeman307e7442005-03-26 01:28:53 +000080 LowerCallTo(SDOperand Chain, const Type *RetTy, bool isVarArg,
81 SDOperand Callee, ArgListTy &Args, SelectionDAG &DAG);
Nate Begemana9795f82005-03-24 04:41:43 +000082
83 virtual std::pair<SDOperand, SDOperand>
84 LowerVAStart(SDOperand Chain, SelectionDAG &DAG);
85
86 virtual std::pair<SDOperand,SDOperand>
87 LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
88 const Type *ArgTy, SelectionDAG &DAG);
89
90 virtual std::pair<SDOperand, SDOperand>
91 LowerFrameReturnAddress(bool isFrameAddr, SDOperand Chain, unsigned Depth,
92 SelectionDAG &DAG);
93 };
94}
95
96
97std::vector<SDOperand>
98PPC32TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
99 //
100 // add beautiful description of PPC stack frame format, or at least some docs
101 //
102 MachineFunction &MF = DAG.getMachineFunction();
103 MachineFrameInfo *MFI = MF.getFrameInfo();
104 MachineBasicBlock& BB = MF.front();
105 std::vector<SDOperand> ArgValues;
106
107 // Due to the rather complicated nature of the PowerPC ABI, rather than a
108 // fixed size array of physical args, for the sake of simplicity let the STL
109 // handle tracking them for us.
110 std::vector<unsigned> argVR, argPR, argOp;
111 unsigned ArgOffset = 24;
112 unsigned GPR_remaining = 8;
113 unsigned FPR_remaining = 13;
114 unsigned GPR_idx = 0, FPR_idx = 0;
115 static const unsigned GPR[] = {
116 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
117 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
118 };
119 static const unsigned FPR[] = {
120 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
121 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
122 };
123
124 // Add DAG nodes to load the arguments... On entry to a function on PPC,
125 // the arguments start at offset 24, although they are likely to be passed
126 // in registers.
127 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
128 SDOperand newroot, argt;
129 unsigned ObjSize;
130 bool needsLoad = false;
Nate Begemancd08e4c2005-04-09 20:09:12 +0000131 bool ArgLive = !I->use_empty();
Nate Begemana9795f82005-03-24 04:41:43 +0000132 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;
Nate Begemancd08e4c2005-04-09 20:09:12 +0000141 if (!ArgLive) break;
Nate Begemana9795f82005-03-24 04:41:43 +0000142 if (GPR_remaining > 0) {
Nate Begemancd08e4c2005-04-09 20:09:12 +0000143 MF.addLiveIn(GPR[GPR_idx]);
Nate Begemanf70b5762005-03-28 23:08:54 +0000144 argt = newroot = DAG.getCopyFromReg(GPR[GPR_idx], MVT::i32,
145 DAG.getRoot());
Nate Begemana9795f82005-03-24 04:41:43 +0000146 if (ObjectVT != MVT::i32)
147 argt = DAG.getNode(ISD::TRUNCATE, ObjectVT, newroot);
Nate Begemana9795f82005-03-24 04:41:43 +0000148 } else {
149 needsLoad = true;
150 }
151 break;
Nate Begemanf7e43382005-03-26 07:46:36 +0000152 case MVT::i64: ObjSize = 8;
Nate Begemancd08e4c2005-04-09 20:09:12 +0000153 if (!ArgLive) break;
Nate Begemanc5b1cd22005-04-10 05:53:14 +0000154 if (GPR_remaining > 0) {
155 SDOperand argHi, argLo;
Nate Begemancd08e4c2005-04-09 20:09:12 +0000156 MF.addLiveIn(GPR[GPR_idx]);
Nate Begemanc5b1cd22005-04-10 05:53:14 +0000157 argHi = DAG.getCopyFromReg(GPR[GPR_idx], MVT::i32, DAG.getRoot());
158 // If we have two or more remaining argument registers, then both halves
159 // of the i64 can be sourced from there. Otherwise, the lower half will
160 // have to come off the stack. This can happen when an i64 is preceded
161 // by 28 bytes of arguments.
162 if (GPR_remaining > 1) {
163 MF.addLiveIn(GPR[GPR_idx+1]);
164 argLo = DAG.getCopyFromReg(GPR[GPR_idx+1], MVT::i32, argHi);
165 } else {
166 int FI = MFI->CreateFixedObject(4, ArgOffset+4);
167 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
168 argLo = DAG.getLoad(MVT::i32, DAG.getEntryNode(), FIN);
169 }
Nate Begemanca12a2b2005-03-28 22:28:37 +0000170 // Build the outgoing arg thingy
Nate Begemanf70b5762005-03-28 23:08:54 +0000171 argt = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, argLo, argHi);
172 newroot = argLo;
Nate Begemana9795f82005-03-24 04:41:43 +0000173 } else {
174 needsLoad = true;
175 }
176 break;
Nate Begemancd08e4c2005-04-09 20:09:12 +0000177 case MVT::f32:
178 case MVT::f64:
179 ObjSize = (ObjectVT == MVT::f64) ? 8 : 4;
180 if (!ArgLive) break;
Nate Begemana9795f82005-03-24 04:41:43 +0000181 if (FPR_remaining > 0) {
Nate Begemancd08e4c2005-04-09 20:09:12 +0000182 MF.addLiveIn(FPR[FPR_idx]);
Nate Begemanf70b5762005-03-28 23:08:54 +0000183 argt = newroot = DAG.getCopyFromReg(FPR[FPR_idx], ObjectVT,
184 DAG.getRoot());
Nate Begemana9795f82005-03-24 04:41:43 +0000185 --FPR_remaining;
186 ++FPR_idx;
187 } else {
188 needsLoad = true;
189 }
190 break;
191 }
192
193 // We need to load the argument to a virtual register if we determined above
194 // that we ran out of physical registers of the appropriate type
195 if (needsLoad) {
Nate Begemane5846682005-04-04 06:52:38 +0000196 unsigned SubregOffset = 0;
Nate Begemanc3e2db42005-04-04 09:09:00 +0000197 if (ObjectVT == MVT::i8 || ObjectVT == MVT::i1) SubregOffset = 3;
Nate Begemane5846682005-04-04 06:52:38 +0000198 if (ObjectVT == MVT::i16) SubregOffset = 2;
Nate Begemana9795f82005-03-24 04:41:43 +0000199 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
200 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
Nate Begemane5846682005-04-04 06:52:38 +0000201 FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN,
202 DAG.getConstant(SubregOffset, MVT::i32));
Nate Begemana9795f82005-03-24 04:41:43 +0000203 argt = newroot = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN);
204 }
205
206 // Every 4 bytes of argument space consumes one of the GPRs available for
207 // argument passing.
208 if (GPR_remaining > 0) {
209 unsigned delta = (GPR_remaining > 1 && ObjSize == 8) ? 2 : 1;
210 GPR_remaining -= delta;
211 GPR_idx += delta;
212 }
213 ArgOffset += ObjSize;
Chris Lattner91277ea2005-04-09 21:23:24 +0000214 if (newroot.Val)
215 DAG.setRoot(newroot.getValue(1));
Nate Begemana9795f82005-03-24 04:41:43 +0000216
Nate Begemana9795f82005-03-24 04:41:43 +0000217 ArgValues.push_back(argt);
218 }
219
Nate Begemana9795f82005-03-24 04:41:43 +0000220 // If the function takes variable number of arguments, make a frame index for
221 // the start of the first vararg value... for expansion of llvm.va_start.
Nate Begemanfa554702005-04-03 22:13:27 +0000222 if (F.isVarArg()) {
Nate Begemana9795f82005-03-24 04:41:43 +0000223 VarArgsFrameIndex = MFI->CreateFixedObject(4, ArgOffset);
Nate Begemanfa554702005-04-03 22:13:27 +0000224 SDOperand FIN = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
Nate Begeman6644d4c2005-04-03 23:11:17 +0000225 // If this function is vararg, store any remaining integer argument regs
226 // to their spots on the stack so that they may be loaded by deferencing the
227 // result of va_next.
228 std::vector<SDOperand> MemOps;
229 for (; GPR_remaining > 0; --GPR_remaining, ++GPR_idx) {
Nate Begemancd08e4c2005-04-09 20:09:12 +0000230 MF.addLiveIn(GPR[GPR_idx]);
Nate Begeman6644d4c2005-04-03 23:11:17 +0000231 SDOperand Val = DAG.getCopyFromReg(GPR[GPR_idx], MVT::i32, DAG.getRoot());
232 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Val.getValue(1),
233 Val, FIN);
234 MemOps.push_back(Store);
235 // Increment the address by four for the next argument to store
236 SDOperand PtrOff = DAG.getConstant(4, getPointerTy());
237 FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN, PtrOff);
238 }
239 DAG.setRoot(DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps));
Nate Begemanfa554702005-04-03 22:13:27 +0000240 }
Nate Begemana9795f82005-03-24 04:41:43 +0000241
Nate Begemancd08e4c2005-04-09 20:09:12 +0000242 // Finally, inform the code generator which regs we return values in.
243 switch (getValueType(F.getReturnType())) {
244 default: assert(0 && "Unknown type!");
245 case MVT::isVoid: break;
246 case MVT::i1:
247 case MVT::i8:
248 case MVT::i16:
249 case MVT::i32:
250 MF.addLiveOut(PPC::R3);
251 break;
252 case MVT::i64:
253 MF.addLiveOut(PPC::R3);
254 MF.addLiveOut(PPC::R4);
255 break;
256 case MVT::f32:
257 case MVT::f64:
258 MF.addLiveOut(PPC::F1);
259 break;
260 }
261
Nate Begemana9795f82005-03-24 04:41:43 +0000262 return ArgValues;
263}
264
265std::pair<SDOperand, SDOperand>
266PPC32TargetLowering::LowerCallTo(SDOperand Chain,
Nate Begeman307e7442005-03-26 01:28:53 +0000267 const Type *RetTy, bool isVarArg,
268 SDOperand Callee, ArgListTy &Args, SelectionDAG &DAG) {
269 // args_to_use will accumulate outgoing args for the ISD::CALL case in
270 // SelectExpr to use to put the arguments in the appropriate registers.
Nate Begemana9795f82005-03-24 04:41:43 +0000271 std::vector<SDOperand> args_to_use;
Nate Begeman307e7442005-03-26 01:28:53 +0000272
273 // Count how many bytes are to be pushed on the stack, including the linkage
274 // area, and parameter passing area.
275 unsigned NumBytes = 24;
276
277 if (Args.empty()) {
Nate Begemana7e11a42005-04-01 05:57:17 +0000278 Chain = DAG.getNode(ISD::ADJCALLSTACKDOWN, MVT::Other, Chain,
279 DAG.getConstant(NumBytes, getPointerTy()));
Nate Begeman307e7442005-03-26 01:28:53 +0000280 } else {
281 for (unsigned i = 0, e = Args.size(); i != e; ++i)
282 switch (getValueType(Args[i].second)) {
283 default: assert(0 && "Unknown value type!");
284 case MVT::i1:
285 case MVT::i8:
286 case MVT::i16:
287 case MVT::i32:
288 case MVT::f32:
289 NumBytes += 4;
290 break;
291 case MVT::i64:
292 case MVT::f64:
293 NumBytes += 8;
294 break;
295 }
296
297 // Just to be safe, we'll always reserve the full 24 bytes of linkage area
298 // plus 32 bytes of argument space in case any called code gets funky on us.
299 if (NumBytes < 56) NumBytes = 56;
300
301 // Adjust the stack pointer for the new arguments...
302 // These operations are automatically eliminated by the prolog/epilog pass
303 Chain = DAG.getNode(ISD::ADJCALLSTACKDOWN, MVT::Other, Chain,
304 DAG.getConstant(NumBytes, getPointerTy()));
305
306 // Set up a copy of the stack pointer for use loading and storing any
307 // arguments that may not fit in the registers available for argument
308 // passing.
309 SDOperand StackPtr = DAG.getCopyFromReg(PPC::R1, MVT::i32,
310 DAG.getEntryNode());
311
312 // Figure out which arguments are going to go in registers, and which in
313 // memory. Also, if this is a vararg function, floating point operations
314 // must be stored to our stack, and loaded into integer regs as well, if
315 // any integer regs are available for argument passing.
316 unsigned ArgOffset = 24;
317 unsigned GPR_remaining = 8;
318 unsigned FPR_remaining = 13;
Nate Begeman74d73452005-03-31 00:15:26 +0000319
320 std::vector<SDOperand> MemOps;
Nate Begeman307e7442005-03-26 01:28:53 +0000321 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
322 // PtrOff will be used to store the current argument to the stack if a
323 // register cannot be found for it.
324 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
325 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
Nate Begemanf7e43382005-03-26 07:46:36 +0000326 MVT::ValueType ArgVT = getValueType(Args[i].second);
Nate Begeman307e7442005-03-26 01:28:53 +0000327
Nate Begemanf7e43382005-03-26 07:46:36 +0000328 switch (ArgVT) {
Nate Begeman307e7442005-03-26 01:28:53 +0000329 default: assert(0 && "Unexpected ValueType for argument!");
330 case MVT::i1:
331 case MVT::i8:
332 case MVT::i16:
333 // Promote the integer to 32 bits. If the input type is signed use a
334 // sign extend, otherwise use a zero extend.
335 if (Args[i].second->isSigned())
336 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
337 else
338 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
339 // FALL THROUGH
340 case MVT::i32:
341 if (GPR_remaining > 0) {
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000342 args_to_use.push_back(Args[i].first);
Nate Begeman307e7442005-03-26 01:28:53 +0000343 --GPR_remaining;
344 } else {
Nate Begeman74d73452005-03-31 00:15:26 +0000345 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
346 Args[i].first, PtrOff));
Nate Begeman307e7442005-03-26 01:28:53 +0000347 }
348 ArgOffset += 4;
349 break;
350 case MVT::i64:
Nate Begemanf7e43382005-03-26 07:46:36 +0000351 // If we have one free GPR left, we can place the upper half of the i64
352 // in it, and store the other half to the stack. If we have two or more
353 // free GPRs, then we can pass both halves of the i64 in registers.
354 if (GPR_remaining > 0) {
Nate Begemanf2622612005-03-26 02:17:46 +0000355 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
356 Args[i].first, DAG.getConstant(1, MVT::i32));
357 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
358 Args[i].first, DAG.getConstant(0, MVT::i32));
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000359 args_to_use.push_back(Hi);
Nate Begeman74d73452005-03-31 00:15:26 +0000360 --GPR_remaining;
Nate Begeman74d73452005-03-31 00:15:26 +0000361 if (GPR_remaining > 0) {
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000362 args_to_use.push_back(Lo);
Nate Begeman74d73452005-03-31 00:15:26 +0000363 --GPR_remaining;
Nate Begemanf7e43382005-03-26 07:46:36 +0000364 } else {
365 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
366 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
Nate Begeman74d73452005-03-31 00:15:26 +0000367 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
368 Lo, PtrOff));
Nate Begemanf7e43382005-03-26 07:46:36 +0000369 }
Nate Begeman307e7442005-03-26 01:28:53 +0000370 } else {
Nate Begeman74d73452005-03-31 00:15:26 +0000371 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
372 Args[i].first, PtrOff));
Nate Begeman307e7442005-03-26 01:28:53 +0000373 }
374 ArgOffset += 8;
375 break;
376 case MVT::f32:
Nate Begeman307e7442005-03-26 01:28:53 +0000377 case MVT::f64:
Nate Begemanf7e43382005-03-26 07:46:36 +0000378 if (FPR_remaining > 0) {
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000379 args_to_use.push_back(Args[i].first);
380 --FPR_remaining;
Nate Begemanf7e43382005-03-26 07:46:36 +0000381 if (isVarArg) {
Nate Begeman96fc6812005-03-31 02:05:53 +0000382 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Chain,
383 Args[i].first, PtrOff);
384 MemOps.push_back(Store);
Nate Begeman74d73452005-03-31 00:15:26 +0000385 // Float varargs are always shadowed in available integer registers
386 if (GPR_remaining > 0) {
Nate Begeman96fc6812005-03-31 02:05:53 +0000387 SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff);
Nate Begeman74d73452005-03-31 00:15:26 +0000388 MemOps.push_back(Load);
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000389 args_to_use.push_back(Load);
390 --GPR_remaining;
Nate Begeman74d73452005-03-31 00:15:26 +0000391 }
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000392 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
Nate Begeman74d73452005-03-31 00:15:26 +0000393 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
394 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
Nate Begeman96fc6812005-03-31 02:05:53 +0000395 SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff);
Nate Begeman74d73452005-03-31 00:15:26 +0000396 MemOps.push_back(Load);
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000397 args_to_use.push_back(Load);
398 --GPR_remaining;
Nate Begeman74d73452005-03-31 00:15:26 +0000399 }
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000400 } else {
401 // If we have any FPRs remaining, we may also have GPRs remaining.
402 // Args passed in FPRs consume either 1 (f32) or 2 (f64) available
403 // GPRs.
404 if (GPR_remaining > 0) {
405 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
406 --GPR_remaining;
407 }
408 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
409 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
410 --GPR_remaining;
411 }
Nate Begeman74d73452005-03-31 00:15:26 +0000412 }
Nate Begeman307e7442005-03-26 01:28:53 +0000413 } else {
Nate Begeman74d73452005-03-31 00:15:26 +0000414 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
415 Args[i].first, PtrOff));
Nate Begeman307e7442005-03-26 01:28:53 +0000416 }
Nate Begemanf7e43382005-03-26 07:46:36 +0000417 ArgOffset += (ArgVT == MVT::f32) ? 4 : 8;
Nate Begeman307e7442005-03-26 01:28:53 +0000418 break;
419 }
Nate Begemana9795f82005-03-24 04:41:43 +0000420 }
Nate Begeman74d73452005-03-31 00:15:26 +0000421 if (!MemOps.empty())
422 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps);
Nate Begemana9795f82005-03-24 04:41:43 +0000423 }
424
425 std::vector<MVT::ValueType> RetVals;
426 MVT::ValueType RetTyVT = getValueType(RetTy);
427 if (RetTyVT != MVT::isVoid)
428 RetVals.push_back(RetTyVT);
429 RetVals.push_back(MVT::Other);
430
431 SDOperand TheCall = SDOperand(DAG.getCall(RetVals,
432 Chain, Callee, args_to_use), 0);
433 Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
434 Chain = DAG.getNode(ISD::ADJCALLSTACKUP, MVT::Other, Chain,
435 DAG.getConstant(NumBytes, getPointerTy()));
436 return std::make_pair(TheCall, Chain);
437}
438
439std::pair<SDOperand, SDOperand>
440PPC32TargetLowering::LowerVAStart(SDOperand Chain, SelectionDAG &DAG) {
441 //vastart just returns the address of the VarArgsFrameIndex slot.
442 return std::make_pair(DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32), Chain);
443}
444
445std::pair<SDOperand,SDOperand> PPC32TargetLowering::
446LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
447 const Type *ArgTy, SelectionDAG &DAG) {
Nate Begemanc7b09f12005-03-25 08:34:25 +0000448 MVT::ValueType ArgVT = getValueType(ArgTy);
449 SDOperand Result;
450 if (!isVANext) {
451 Result = DAG.getLoad(ArgVT, DAG.getEntryNode(), VAList);
452 } else {
453 unsigned Amt;
454 if (ArgVT == MVT::i32 || ArgVT == MVT::f32)
455 Amt = 4;
456 else {
457 assert((ArgVT == MVT::i64 || ArgVT == MVT::f64) &&
458 "Other types should have been promoted for varargs!");
459 Amt = 8;
460 }
461 Result = DAG.getNode(ISD::ADD, VAList.getValueType(), VAList,
462 DAG.getConstant(Amt, VAList.getValueType()));
463 }
464 return std::make_pair(Result, Chain);
Nate Begemana9795f82005-03-24 04:41:43 +0000465}
466
467
468std::pair<SDOperand, SDOperand> PPC32TargetLowering::
469LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
470 SelectionDAG &DAG) {
Nate Begeman01d05262005-03-30 01:45:43 +0000471 assert(0 && "LowerFrameReturnAddress unimplemented");
Nate Begemana9795f82005-03-24 04:41:43 +0000472 abort();
473}
474
475namespace {
Nate Begemanc7bd4822005-04-11 06:34:10 +0000476Statistic<>Recorded("ppc-codegen", "Number of recording ops emitted");
Nate Begeman93075ec2005-04-04 23:40:36 +0000477Statistic<>FusedFP("ppc-codegen", "Number of fused fp operations");
Nate Begemana9795f82005-03-24 04:41:43 +0000478//===--------------------------------------------------------------------===//
479/// ISel - PPC32 specific code to select PPC32 machine instructions for
480/// SelectionDAG operations.
481//===--------------------------------------------------------------------===//
482class ISel : public SelectionDAGISel {
Nate Begemana9795f82005-03-24 04:41:43 +0000483 PPC32TargetLowering PPC32Lowering;
Nate Begeman815d6da2005-04-06 00:25:27 +0000484 SelectionDAG *ISelDAG; // Hack to support us having a dag->dag transform
485 // for sdiv and udiv until it is put into the future
486 // dag combiner.
Nate Begemana9795f82005-03-24 04:41:43 +0000487
488 /// ExprMap - As shared expressions are codegen'd, we keep track of which
489 /// vreg the value is produced in, so we only emit one copy of each compiled
490 /// tree.
491 std::map<SDOperand, unsigned> ExprMap;
Nate Begemanc7b09f12005-03-25 08:34:25 +0000492
493 unsigned GlobalBaseReg;
494 bool GlobalBaseInitialized;
Nate Begemanc7bd4822005-04-11 06:34:10 +0000495 bool RecordSuccess;
Nate Begemana9795f82005-03-24 04:41:43 +0000496public:
Nate Begeman815d6da2005-04-06 00:25:27 +0000497 ISel(TargetMachine &TM) : SelectionDAGISel(PPC32Lowering), PPC32Lowering(TM),
498 ISelDAG(0) {}
Nate Begemana9795f82005-03-24 04:41:43 +0000499
Nate Begemanc7b09f12005-03-25 08:34:25 +0000500 /// runOnFunction - Override this function in order to reset our per-function
501 /// variables.
502 virtual bool runOnFunction(Function &Fn) {
503 // Make sure we re-emit a set of the global base reg if necessary
504 GlobalBaseInitialized = false;
505 return SelectionDAGISel::runOnFunction(Fn);
506 }
507
Nate Begemana9795f82005-03-24 04:41:43 +0000508 /// InstructionSelectBasicBlock - This callback is invoked by
509 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
510 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG) {
511 DEBUG(BB->dump());
512 // Codegen the basic block.
Nate Begeman815d6da2005-04-06 00:25:27 +0000513 ISelDAG = &DAG;
Nate Begemana9795f82005-03-24 04:41:43 +0000514 Select(DAG.getRoot());
515
516 // Clear state used for selection.
517 ExprMap.clear();
Nate Begeman815d6da2005-04-06 00:25:27 +0000518 ISelDAG = 0;
Nate Begemana9795f82005-03-24 04:41:43 +0000519 }
Nate Begeman815d6da2005-04-06 00:25:27 +0000520
521 // dag -> dag expanders for integer divide by constant
522 SDOperand BuildSDIVSequence(SDOperand N);
523 SDOperand BuildUDIVSequence(SDOperand N);
Nate Begemana9795f82005-03-24 04:41:43 +0000524
Nate Begemandffcfcc2005-04-01 00:32:34 +0000525 unsigned getGlobalBaseReg();
Nate Begeman6b559972005-04-01 02:59:27 +0000526 unsigned getConstDouble(double floatVal, unsigned Result);
Nate Begeman7ddecb42005-04-06 23:51:40 +0000527 bool SelectBitfieldInsert(SDOperand OR, unsigned Result);
Nate Begemandffcfcc2005-04-01 00:32:34 +0000528 unsigned SelectSetCR0(SDOperand CC);
Nate Begemanc7bd4822005-04-11 06:34:10 +0000529 unsigned SelectExpr(SDOperand N, bool Recording=false);
Nate Begemana9795f82005-03-24 04:41:43 +0000530 unsigned SelectExprFP(SDOperand N, unsigned Result);
531 void Select(SDOperand N);
532
Nate Begeman04730362005-04-01 04:45:11 +0000533 bool SelectAddr(SDOperand N, unsigned& Reg, int& offset);
Nate Begemana9795f82005-03-24 04:41:43 +0000534 void SelectBranchCC(SDOperand N);
535};
536
Nate Begeman80196b12005-04-05 00:15:08 +0000537/// ExactLog2 - This function solves for (Val == 1 << (N-1)) and returns N. It
538/// returns zero when the input is not exactly a power of two.
539static unsigned ExactLog2(unsigned Val) {
540 if (Val == 0 || (Val & (Val-1))) return 0;
541 unsigned Count = 0;
542 while (Val != 1) {
543 Val >>= 1;
544 ++Count;
545 }
546 return Count;
547}
548
Nate Begeman7ddecb42005-04-06 23:51:40 +0000549// IsRunOfOnes - returns true if Val consists of one contiguous run of 1's with
550// any number of 0's on either side. the 1's are allowed to wrap from LSB to
551// MSB. so 0x000FFF0, 0x0000FFFF, and 0xFF0000FF are all runs. 0x0F0F0000 is
552// not, since all 1's are not contiguous.
553static bool IsRunOfOnes(unsigned Val, unsigned &MB, unsigned &ME) {
554 bool isRun = true;
555 MB = 0;
556 ME = 0;
557
558 // look for first set bit
559 int i = 0;
560 for (; i < 32; i++) {
561 if ((Val & (1 << (31 - i))) != 0) {
562 MB = i;
563 ME = i;
564 break;
565 }
566 }
567
568 // look for last set bit
569 for (; i < 32; i++) {
570 if ((Val & (1 << (31 - i))) == 0)
571 break;
572 ME = i;
573 }
574
575 // look for next set bit
576 for (; i < 32; i++) {
577 if ((Val & (1 << (31 - i))) != 0)
578 break;
579 }
580
581 // if we exhausted all the bits, we found a match at this point for 0*1*0*
582 if (i == 32)
583 return true;
584
585 // since we just encountered more 1's, if it doesn't wrap around to the
586 // most significant bit of the word, then we did not find a match to 1*0*1* so
587 // exit.
588 if (MB != 0)
589 return false;
590
591 // look for last set bit
592 for (MB = i; i < 32; i++) {
593 if ((Val & (1 << (31 - i))) == 0)
594 break;
595 }
596
597 // if we exhausted all the bits, then we found a match for 1*0*1*, otherwise,
598 // the value is not a run of ones.
599 if (i == 32)
600 return true;
601 return false;
602}
603
Nate Begeman439b4442005-04-05 04:22:58 +0000604/// getImmediateForOpcode - This method returns a value indicating whether
Nate Begemana9795f82005-03-24 04:41:43 +0000605/// the ConstantSDNode N can be used as an immediate to Opcode. The return
606/// values are either 0, 1 or 2. 0 indicates that either N is not a
607/// ConstantSDNode, or is not suitable for use by that opcode. A return value
608/// of 1 indicates that the constant may be used in normal immediate form. A
609/// return value of 2 indicates that the constant may be used in shifted
Nate Begeman439b4442005-04-05 04:22:58 +0000610/// immediate form. A return value of 3 indicates that log base 2 of the
Nate Begeman815d6da2005-04-06 00:25:27 +0000611/// constant may be used. A return value of 4 indicates that the constant is
612/// suitable for conversion into a magic number for integer division.
Nate Begemana9795f82005-03-24 04:41:43 +0000613///
Nate Begeman439b4442005-04-05 04:22:58 +0000614static unsigned getImmediateForOpcode(SDOperand N, unsigned Opcode,
615 unsigned& Imm, bool U = false) {
Nate Begemana9795f82005-03-24 04:41:43 +0000616 if (N.getOpcode() != ISD::Constant) return 0;
617
618 int v = (int)cast<ConstantSDNode>(N)->getSignExtended();
619
620 switch(Opcode) {
621 default: return 0;
622 case ISD::ADD:
623 if (v <= 32767 && v >= -32768) { Imm = v & 0xFFFF; return 1; }
624 if ((v & 0x0000FFFF) == 0) { Imm = v >> 16; return 2; }
625 break;
626 case ISD::AND:
627 case ISD::XOR:
628 case ISD::OR:
629 if (v >= 0 && v <= 65535) { Imm = v & 0xFFFF; return 1; }
630 if ((v & 0x0000FFFF) == 0) { Imm = v >> 16; return 2; }
631 break;
Nate Begeman307e7442005-03-26 01:28:53 +0000632 case ISD::MUL:
Nate Begeman27523a12005-04-02 00:42:16 +0000633 case ISD::SUB:
Nate Begeman307e7442005-03-26 01:28:53 +0000634 if (v <= 32767 && v >= -32768) { Imm = v & 0xFFFF; return 1; }
635 break;
Nate Begeman3e897162005-03-31 23:55:40 +0000636 case ISD::SETCC:
637 if (U && (v >= 0 && v <= 65535)) { Imm = v & 0xFFFF; return 1; }
638 if (!U && (v <= 32767 && v >= -32768)) { Imm = v & 0xFFFF; return 1; }
639 break;
Nate Begeman80196b12005-04-05 00:15:08 +0000640 case ISD::SDIV:
Nate Begeman439b4442005-04-05 04:22:58 +0000641 if ((Imm = ExactLog2(v))) { return 3; }
Nate Begeman815d6da2005-04-06 00:25:27 +0000642 if (v <= -2 || v >= 2) { return 4; }
643 break;
644 case ISD::UDIV:
Nate Begeman27b4c232005-04-06 06:44:57 +0000645 if (v > 1) { return 4; }
Nate Begeman80196b12005-04-05 00:15:08 +0000646 break;
Nate Begemana9795f82005-03-24 04:41:43 +0000647 }
648 return 0;
649}
Nate Begeman3e897162005-03-31 23:55:40 +0000650
Nate Begemanc7bd4822005-04-11 06:34:10 +0000651/// NodeHasRecordingVariant - If SelectExpr can always produce code for
652/// NodeOpcode that also sets CR0 as a side effect, return true. Otherwise,
653/// return false.
654static bool NodeHasRecordingVariant(unsigned NodeOpcode) {
655 switch(NodeOpcode) {
656 default: return false;
657 case ISD::AND:
658 case ISD::OR: return true;
659 }
660}
661
Nate Begeman3e897162005-03-31 23:55:40 +0000662/// getBCCForSetCC - Returns the PowerPC condition branch mnemonic corresponding
663/// to Condition. If the Condition is unordered or unsigned, the bool argument
664/// U is set to true, otherwise it is set to false.
665static unsigned getBCCForSetCC(unsigned Condition, bool& U) {
666 U = false;
667 switch (Condition) {
668 default: assert(0 && "Unknown condition!"); abort();
669 case ISD::SETEQ: return PPC::BEQ;
670 case ISD::SETNE: return PPC::BNE;
671 case ISD::SETULT: U = true;
672 case ISD::SETLT: return PPC::BLT;
673 case ISD::SETULE: U = true;
674 case ISD::SETLE: return PPC::BLE;
675 case ISD::SETUGT: U = true;
676 case ISD::SETGT: return PPC::BGT;
677 case ISD::SETUGE: U = true;
678 case ISD::SETGE: return PPC::BGE;
679 }
Nate Begeman04730362005-04-01 04:45:11 +0000680 return 0;
681}
682
683/// IndexedOpForOp - Return the indexed variant for each of the PowerPC load
684/// and store immediate instructions.
685static unsigned IndexedOpForOp(unsigned Opcode) {
686 switch(Opcode) {
687 default: assert(0 && "Unknown opcode!"); abort();
688 case PPC::LBZ: return PPC::LBZX; case PPC::STB: return PPC::STBX;
689 case PPC::LHZ: return PPC::LHZX; case PPC::STH: return PPC::STHX;
690 case PPC::LHA: return PPC::LHAX; case PPC::STW: return PPC::STWX;
691 case PPC::LWZ: return PPC::LWZX; case PPC::STFS: return PPC::STFSX;
692 case PPC::LFS: return PPC::LFSX; case PPC::STFD: return PPC::STFDX;
693 case PPC::LFD: return PPC::LFDX;
694 }
695 return 0;
Nate Begeman3e897162005-03-31 23:55:40 +0000696}
Nate Begeman815d6da2005-04-06 00:25:27 +0000697
Nate Begemanc7bd4822005-04-11 06:34:10 +0000698///
699
Nate Begeman815d6da2005-04-06 00:25:27 +0000700// Structure used to return the necessary information to codegen an SDIV as
701// a multiply.
702struct ms {
703 int m; // magic number
704 int s; // shift amount
705};
706
707struct mu {
708 unsigned int m; // magic number
709 int a; // add indicator
710 int s; // shift amount
711};
712
713/// magic - calculate the magic numbers required to codegen an integer sdiv as
714/// a sequence of multiply and shifts. Requires that the divisor not be 0, 1,
715/// or -1.
716static struct ms magic(int d) {
717 int p;
718 unsigned int ad, anc, delta, q1, r1, q2, r2, t;
719 const unsigned int two31 = 2147483648U; // 2^31
720 struct ms mag;
721
722 ad = abs(d);
723 t = two31 + ((unsigned int)d >> 31);
724 anc = t - 1 - t%ad; // absolute value of nc
725 p = 31; // initialize p
726 q1 = two31/anc; // initialize q1 = 2p/abs(nc)
727 r1 = two31 - q1*anc; // initialize r1 = rem(2p,abs(nc))
728 q2 = two31/ad; // initialize q2 = 2p/abs(d)
729 r2 = two31 - q2*ad; // initialize r2 = rem(2p,abs(d))
730 do {
731 p = p + 1;
732 q1 = 2*q1; // update q1 = 2p/abs(nc)
733 r1 = 2*r1; // update r1 = rem(2p/abs(nc))
734 if (r1 >= anc) { // must be unsigned comparison
735 q1 = q1 + 1;
736 r1 = r1 - anc;
737 }
738 q2 = 2*q2; // update q2 = 2p/abs(d)
739 r2 = 2*r2; // update r2 = rem(2p/abs(d))
740 if (r2 >= ad) { // must be unsigned comparison
741 q2 = q2 + 1;
742 r2 = r2 - ad;
743 }
744 delta = ad - r2;
745 } while (q1 < delta || (q1 == delta && r1 == 0));
746
747 mag.m = q2 + 1;
748 if (d < 0) mag.m = -mag.m; // resulting magic number
749 mag.s = p - 32; // resulting shift
750 return mag;
751}
752
753/// magicu - calculate the magic numbers required to codegen an integer udiv as
754/// a sequence of multiply, add and shifts. Requires that the divisor not be 0.
755static struct mu magicu(unsigned d)
756{
757 int p;
758 unsigned int nc, delta, q1, r1, q2, r2;
759 struct mu magu;
760 magu.a = 0; // initialize "add" indicator
761 nc = - 1 - (-d)%d;
762 p = 31; // initialize p
763 q1 = 0x80000000/nc; // initialize q1 = 2p/nc
764 r1 = 0x80000000 - q1*nc; // initialize r1 = rem(2p,nc)
765 q2 = 0x7FFFFFFF/d; // initialize q2 = (2p-1)/d
766 r2 = 0x7FFFFFFF - q2*d; // initialize r2 = rem((2p-1),d)
767 do {
768 p = p + 1;
769 if (r1 >= nc - r1 ) {
770 q1 = 2*q1 + 1; // update q1
771 r1 = 2*r1 - nc; // update r1
772 }
773 else {
774 q1 = 2*q1; // update q1
775 r1 = 2*r1; // update r1
776 }
777 if (r2 + 1 >= d - r2) {
778 if (q2 >= 0x7FFFFFFF) magu.a = 1;
779 q2 = 2*q2 + 1; // update q2
780 r2 = 2*r2 + 1 - d; // update r2
781 }
782 else {
783 if (q2 >= 0x80000000) magu.a = 1;
784 q2 = 2*q2; // update q2
785 r2 = 2*r2 + 1; // update r2
786 }
787 delta = d - 1 - r2;
788 } while (p < 64 && (q1 < delta || (q1 == delta && r1 == 0)));
789 magu.m = q2 + 1; // resulting magic number
790 magu.s = p - 32; // resulting shift
791 return magu;
792}
793}
794
795/// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
796/// return a DAG expression to select that will generate the same value by
797/// multiplying by a magic number. See:
798/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
799SDOperand ISel::BuildSDIVSequence(SDOperand N) {
800 int d = (int)cast<ConstantSDNode>(N.getOperand(1))->getSignExtended();
801 ms magics = magic(d);
802 // Multiply the numerator (operand 0) by the magic value
803 SDOperand Q = ISelDAG->getNode(ISD::MULHS, MVT::i32, N.getOperand(0),
804 ISelDAG->getConstant(magics.m, MVT::i32));
805 // If d > 0 and m < 0, add the numerator
806 if (d > 0 && magics.m < 0)
807 Q = ISelDAG->getNode(ISD::ADD, MVT::i32, Q, N.getOperand(0));
808 // If d < 0 and m > 0, subtract the numerator.
809 if (d < 0 && magics.m > 0)
810 Q = ISelDAG->getNode(ISD::SUB, MVT::i32, Q, N.getOperand(0));
811 // Shift right algebraic if shift value is nonzero
812 if (magics.s > 0)
813 Q = ISelDAG->getNode(ISD::SRA, MVT::i32, Q,
814 ISelDAG->getConstant(magics.s, MVT::i32));
815 // Extract the sign bit and add it to the quotient
816 SDOperand T =
817 ISelDAG->getNode(ISD::SRL, MVT::i32, Q, ISelDAG->getConstant(31, MVT::i32));
Nate Begeman27b4c232005-04-06 06:44:57 +0000818 return ISelDAG->getNode(ISD::ADD, MVT::i32, Q, T);
Nate Begeman815d6da2005-04-06 00:25:27 +0000819}
820
821/// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
822/// return a DAG expression to select that will generate the same value by
823/// multiplying by a magic number. See:
824/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
825SDOperand ISel::BuildUDIVSequence(SDOperand N) {
826 unsigned d =
827 (unsigned)cast<ConstantSDNode>(N.getOperand(1))->getSignExtended();
828 mu magics = magicu(d);
829 // Multiply the numerator (operand 0) by the magic value
830 SDOperand Q = ISelDAG->getNode(ISD::MULHU, MVT::i32, N.getOperand(0),
831 ISelDAG->getConstant(magics.m, MVT::i32));
832 if (magics.a == 0) {
833 Q = ISelDAG->getNode(ISD::SRL, MVT::i32, Q,
834 ISelDAG->getConstant(magics.s, MVT::i32));
835 } else {
836 SDOperand NPQ = ISelDAG->getNode(ISD::SUB, MVT::i32, N.getOperand(0), Q);
837 NPQ = ISelDAG->getNode(ISD::SRL, MVT::i32, NPQ,
838 ISelDAG->getConstant(1, MVT::i32));
839 NPQ = ISelDAG->getNode(ISD::ADD, MVT::i32, NPQ, Q);
840 Q = ISelDAG->getNode(ISD::SRL, MVT::i32, NPQ,
841 ISelDAG->getConstant(magics.s-1, MVT::i32));
842 }
Nate Begeman27b4c232005-04-06 06:44:57 +0000843 return Q;
Nate Begemana9795f82005-03-24 04:41:43 +0000844}
845
Nate Begemanc7b09f12005-03-25 08:34:25 +0000846/// getGlobalBaseReg - Output the instructions required to put the
847/// base address to use for accessing globals into a register.
848///
849unsigned ISel::getGlobalBaseReg() {
850 if (!GlobalBaseInitialized) {
851 // Insert the set of GlobalBaseReg into the first MBB of the function
852 MachineBasicBlock &FirstMBB = BB->getParent()->front();
853 MachineBasicBlock::iterator MBBI = FirstMBB.begin();
854 GlobalBaseReg = MakeReg(MVT::i32);
855 BuildMI(FirstMBB, MBBI, PPC::MovePCtoLR, 0, PPC::LR);
856 BuildMI(FirstMBB, MBBI, PPC::MFLR, 1, GlobalBaseReg).addReg(PPC::LR);
857 GlobalBaseInitialized = true;
858 }
859 return GlobalBaseReg;
860}
861
Nate Begeman6b559972005-04-01 02:59:27 +0000862/// getConstDouble - Loads a floating point value into a register, via the
863/// Constant Pool. Optionally takes a register in which to load the value.
864unsigned ISel::getConstDouble(double doubleVal, unsigned Result=0) {
865 unsigned Tmp1 = MakeReg(MVT::i32);
866 if (0 == Result) Result = MakeReg(MVT::f64);
867 MachineConstantPool *CP = BB->getParent()->getConstantPool();
868 ConstantFP *CFP = ConstantFP::get(Type::DoubleTy, doubleVal);
869 unsigned CPI = CP->getConstantPoolIndex(CFP);
870 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp1).addReg(getGlobalBaseReg())
871 .addConstantPoolIndex(CPI);
872 BuildMI(BB, PPC::LFD, 2, Result).addConstantPoolIndex(CPI).addReg(Tmp1);
873 return Result;
874}
875
Nate Begeman7ddecb42005-04-06 23:51:40 +0000876/// SelectBitfieldInsert - turn an or of two masked values into
877/// the rotate left word immediate then mask insert (rlwimi) instruction.
878/// Returns true on success, false if the caller still needs to select OR.
879///
880/// Patterns matched:
881/// 1. or shl, and 5. or and, and
882/// 2. or and, shl 6. or shl, shr
883/// 3. or shr, and 7. or shr, shl
884/// 4. or and, shr
885bool ISel::SelectBitfieldInsert(SDOperand OR, unsigned Result) {
Nate Begemancd08e4c2005-04-09 20:09:12 +0000886 bool IsRotate = false;
Nate Begeman7ddecb42005-04-06 23:51:40 +0000887 unsigned TgtMask = 0xFFFFFFFF, InsMask = 0xFFFFFFFF, Amount = 0;
888 unsigned Op0Opc = OR.getOperand(0).getOpcode();
889 unsigned Op1Opc = OR.getOperand(1).getOpcode();
890
891 // Verify that we have the correct opcodes
892 if (ISD::SHL != Op0Opc && ISD::SRL != Op0Opc && ISD::AND != Op0Opc)
893 return false;
894 if (ISD::SHL != Op1Opc && ISD::SRL != Op1Opc && ISD::AND != Op1Opc)
895 return false;
896
897 // Generate Mask value for Target
898 if (ConstantSDNode *CN =
899 dyn_cast<ConstantSDNode>(OR.getOperand(0).getOperand(1).Val)) {
900 switch(Op0Opc) {
901 case ISD::SHL: TgtMask <<= (unsigned)CN->getValue(); break;
902 case ISD::SRL: TgtMask >>= (unsigned)CN->getValue(); break;
903 case ISD::AND: TgtMask &= (unsigned)CN->getValue(); break;
904 }
905 } else {
906 return false;
907 }
908
909 // Generate Mask value for Insert
910 if (ConstantSDNode *CN =
911 dyn_cast<ConstantSDNode>(OR.getOperand(1).getOperand(1).Val)) {
912 switch(Op1Opc) {
913 case ISD::SHL:
914 Amount = CN->getValue();
Nate Begemancd08e4c2005-04-09 20:09:12 +0000915 InsMask <<= Amount;
916 if (Op0Opc == ISD::SRL) IsRotate = true;
Nate Begeman7ddecb42005-04-06 23:51:40 +0000917 break;
918 case ISD::SRL:
919 Amount = CN->getValue();
920 InsMask >>= Amount;
921 Amount = 32-Amount;
Nate Begemancd08e4c2005-04-09 20:09:12 +0000922 if (Op0Opc == ISD::SHL) IsRotate = true;
Nate Begeman7ddecb42005-04-06 23:51:40 +0000923 break;
924 case ISD::AND:
925 InsMask &= (unsigned)CN->getValue();
926 break;
927 }
928 } else {
929 return false;
930 }
931
932 // Verify that the Target mask and Insert mask together form a full word mask
933 // and that the Insert mask is a run of set bits (which implies both are runs
934 // of set bits). Given that, Select the arguments and generate the rlwimi
935 // instruction.
936 unsigned MB, ME;
937 if (((TgtMask ^ InsMask) == 0xFFFFFFFF) && IsRunOfOnes(InsMask, MB, ME)) {
938 unsigned Tmp1, Tmp2;
Nate Begemancd08e4c2005-04-09 20:09:12 +0000939 // Check for rotlwi / rotrwi here, a special case of bitfield insert
940 // where both bitfield halves are sourced from the same value.
941 if (IsRotate &&
942 OR.getOperand(0).getOperand(0) == OR.getOperand(1).getOperand(0)) {
Nate Begemancd08e4c2005-04-09 20:09:12 +0000943 Tmp1 = SelectExpr(OR.getOperand(0).getOperand(0));
944 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(Amount)
945 .addImm(0).addImm(31);
946 return true;
947 }
Nate Begeman7ddecb42005-04-06 23:51:40 +0000948 if (Op0Opc == ISD::AND)
949 Tmp1 = SelectExpr(OR.getOperand(0).getOperand(0));
950 else
951 Tmp1 = SelectExpr(OR.getOperand(0));
952 Tmp2 = SelectExpr(OR.getOperand(1).getOperand(0));
953 BuildMI(BB, PPC::RLWIMI, 5, Result).addReg(Tmp1).addReg(Tmp2)
954 .addImm(Amount).addImm(MB).addImm(ME);
955 return true;
956 }
957 return false;
958}
959
Nate Begemandffcfcc2005-04-01 00:32:34 +0000960unsigned ISel::SelectSetCR0(SDOperand CC) {
961 unsigned Opc, Tmp1, Tmp2;
962 static const unsigned CompareOpcodes[] =
963 { PPC::FCMPU, PPC::FCMPU, PPC::CMPW, PPC::CMPLW };
964
965 // If the first operand to the select is a SETCC node, then we can fold it
966 // into the branch that selects which value to return.
967 SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(CC.Val);
968 if (SetCC && CC.getOpcode() == ISD::SETCC) {
969 bool U;
Nate Begemanc7bd4822005-04-11 06:34:10 +0000970 bool AlreadySelected = false;
Nate Begemandffcfcc2005-04-01 00:32:34 +0000971 Opc = getBCCForSetCC(SetCC->getCondition(), U);
Nate Begemandffcfcc2005-04-01 00:32:34 +0000972
Nate Begeman439b4442005-04-05 04:22:58 +0000973 // Pass the optional argument U to getImmediateForOpcode for SETCC,
Nate Begemandffcfcc2005-04-01 00:32:34 +0000974 // so that it knows whether the SETCC immediate range is signed or not.
Nate Begeman439b4442005-04-05 04:22:58 +0000975 if (1 == getImmediateForOpcode(SetCC->getOperand(1), ISD::SETCC,
976 Tmp2, U)) {
Nate Begemanc7bd4822005-04-11 06:34:10 +0000977 // For comparisons against zero, we can implicity set CR0 if a recording
978 // variant (e.g. 'or.' instead of 'or') of the instruction that defines
979 // operand zero of the SetCC node is available.
980 if (0 == Tmp2 &&
981 NodeHasRecordingVariant(SetCC->getOperand(0).getOpcode())) {
982 RecordSuccess = false;
983 Tmp1 = SelectExpr(SetCC->getOperand(0), true);
984 if (RecordSuccess) {
985 ++Recorded;
986 return Opc;
987 }
988 AlreadySelected = true;
989 }
990 // If we could not implicitly set CR0, then emit a compare immediate
991 // instead.
992 if (!AlreadySelected) Tmp1 = SelectExpr(SetCC->getOperand(0));
Nate Begemandffcfcc2005-04-01 00:32:34 +0000993 if (U)
994 BuildMI(BB, PPC::CMPLWI, 2, PPC::CR0).addReg(Tmp1).addImm(Tmp2);
995 else
996 BuildMI(BB, PPC::CMPWI, 2, PPC::CR0).addReg(Tmp1).addSImm(Tmp2);
997 } else {
998 bool IsInteger = MVT::isInteger(SetCC->getOperand(0).getValueType());
999 unsigned CompareOpc = CompareOpcodes[2 * IsInteger + U];
Nate Begemanc7bd4822005-04-11 06:34:10 +00001000 Tmp1 = SelectExpr(SetCC->getOperand(0));
Nate Begemandffcfcc2005-04-01 00:32:34 +00001001 Tmp2 = SelectExpr(SetCC->getOperand(1));
1002 BuildMI(BB, CompareOpc, 2, PPC::CR0).addReg(Tmp1).addReg(Tmp2);
1003 }
1004 } else {
1005 Tmp1 = SelectExpr(CC);
1006 BuildMI(BB, PPC::CMPLWI, 2, PPC::CR0).addReg(Tmp1).addImm(0);
1007 Opc = PPC::BNE;
1008 }
1009 return Opc;
1010}
1011
1012/// Check to see if the load is a constant offset from a base register
Nate Begeman04730362005-04-01 04:45:11 +00001013bool ISel::SelectAddr(SDOperand N, unsigned& Reg, int& offset)
Nate Begemana9795f82005-03-24 04:41:43 +00001014{
Nate Begeman96fc6812005-03-31 02:05:53 +00001015 unsigned imm = 0, opcode = N.getOpcode();
Nate Begeman04730362005-04-01 04:45:11 +00001016 if (N.getOpcode() == ISD::ADD) {
1017 Reg = SelectExpr(N.getOperand(0));
Nate Begeman439b4442005-04-05 04:22:58 +00001018 if (1 == getImmediateForOpcode(N.getOperand(1), opcode, imm)) {
Nate Begeman96fc6812005-03-31 02:05:53 +00001019 offset = imm;
Nate Begeman04730362005-04-01 04:45:11 +00001020 return false;
1021 }
1022 offset = SelectExpr(N.getOperand(1));
1023 return true;
1024 }
Nate Begemana9795f82005-03-24 04:41:43 +00001025 Reg = SelectExpr(N);
1026 offset = 0;
Nate Begeman04730362005-04-01 04:45:11 +00001027 return false;
Nate Begemana9795f82005-03-24 04:41:43 +00001028}
1029
1030void ISel::SelectBranchCC(SDOperand N)
1031{
Nate Begemana9795f82005-03-24 04:41:43 +00001032 MachineBasicBlock *Dest =
1033 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
Nate Begeman3e897162005-03-31 23:55:40 +00001034
Nate Begemana9795f82005-03-24 04:41:43 +00001035 Select(N.getOperand(0)); //chain
Nate Begemandffcfcc2005-04-01 00:32:34 +00001036 unsigned Opc = SelectSetCR0(N.getOperand(1));
Nate Begemancd08e4c2005-04-09 20:09:12 +00001037
1038 // Iterate to the next basic block, unless we're already at the end of the
1039 ilist<MachineBasicBlock>::iterator It = BB, E = BB->getParent()->end();
Nate Begeman706471e2005-04-09 23:35:05 +00001040 if (++It == E) It = BB;
Nate Begemancd08e4c2005-04-09 20:09:12 +00001041
1042 // If this is a two way branch, then grab the fallthrough basic block argument
1043 // and build a PowerPC branch pseudo-op, suitable for long branch conversion
1044 // if necessary by the branch selection pass. Otherwise, emit a standard
1045 // conditional branch.
1046 if (N.getOpcode() == ISD::BRCONDTWOWAY) {
1047 MachineBasicBlock *Fallthrough =
1048 cast<BasicBlockSDNode>(N.getOperand(3))->getBasicBlock();
1049 if (Dest != It) {
1050 BuildMI(BB, PPC::COND_BRANCH, 4).addReg(PPC::CR0).addImm(Opc)
1051 .addMBB(Dest).addMBB(Fallthrough);
1052 if (Fallthrough != It)
1053 BuildMI(BB, PPC::B, 1).addMBB(Fallthrough);
1054 } else {
1055 if (Fallthrough != It) {
1056 Opc = PPC32InstrInfo::invertPPCBranchOpcode(Opc);
1057 BuildMI(BB, PPC::COND_BRANCH, 4).addReg(PPC::CR0).addImm(Opc)
1058 .addMBB(Fallthrough).addMBB(Dest);
1059 }
1060 }
1061 } else {
Nate Begeman27499e32005-04-10 01:48:29 +00001062 BuildMI(BB, PPC::COND_BRANCH, 4).addReg(PPC::CR0).addImm(Opc)
1063 .addMBB(Dest).addMBB(It);
1064 //BuildMI(BB, Opc, 2).addReg(PPC::CR0).addMBB(Dest);
Nate Begemancd08e4c2005-04-09 20:09:12 +00001065 }
Nate Begemana9795f82005-03-24 04:41:43 +00001066 return;
1067}
1068
1069unsigned ISel::SelectExprFP(SDOperand N, unsigned Result)
1070{
1071 unsigned Tmp1, Tmp2, Tmp3;
1072 unsigned Opc = 0;
1073 SDNode *Node = N.Val;
1074 MVT::ValueType DestType = N.getValueType();
1075 unsigned opcode = N.getOpcode();
1076
1077 switch (opcode) {
1078 default:
1079 Node->dump();
1080 assert(0 && "Node not handled!\n");
1081
Nate Begeman23afcfb2005-03-29 22:48:55 +00001082 case ISD::SELECT: {
Nate Begeman3e897162005-03-31 23:55:40 +00001083 // Attempt to generate FSEL. We can do this whenever we have an FP result,
1084 // and an FP comparison in the SetCC node.
1085 SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(N.getOperand(0).Val);
1086 if (SetCC && N.getOperand(0).getOpcode() == ISD::SETCC &&
1087 !MVT::isInteger(SetCC->getOperand(0).getValueType()) &&
1088 SetCC->getCondition() != ISD::SETEQ &&
1089 SetCC->getCondition() != ISD::SETNE) {
1090 MVT::ValueType VT = SetCC->getOperand(0).getValueType();
Nate Begeman3e897162005-03-31 23:55:40 +00001091 unsigned TV = SelectExpr(N.getOperand(1)); // Use if TRUE
1092 unsigned FV = SelectExpr(N.getOperand(2)); // Use if FALSE
1093
1094 ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(1));
1095 if (CN && (CN->isExactlyValue(-0.0) || CN->isExactlyValue(0.0))) {
1096 switch(SetCC->getCondition()) {
1097 default: assert(0 && "Invalid FSEL condition"); abort();
1098 case ISD::SETULT:
1099 case ISD::SETLT:
Nate Begemanaf4ab1b2005-04-09 09:33:07 +00001100 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
Nate Begeman3e897162005-03-31 23:55:40 +00001101 case ISD::SETUGE:
1102 case ISD::SETGE:
Nate Begemanaf4ab1b2005-04-09 09:33:07 +00001103 Tmp1 = SelectExpr(SetCC->getOperand(0)); // Val to compare against
Nate Begeman3e897162005-03-31 23:55:40 +00001104 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp1).addReg(TV).addReg(FV);
1105 return Result;
1106 case ISD::SETUGT:
Nate Begemanaf4ab1b2005-04-09 09:33:07 +00001107 case ISD::SETGT:
1108 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
Nate Begeman3e897162005-03-31 23:55:40 +00001109 case ISD::SETULE:
1110 case ISD::SETLE: {
Nate Begemanaf4ab1b2005-04-09 09:33:07 +00001111 if (SetCC->getOperand(0).getOpcode() == ISD::FNEG) {
1112 Tmp2 = SelectExpr(SetCC->getOperand(0).getOperand(0));
1113 } else {
1114 Tmp2 = MakeReg(VT);
1115 Tmp1 = SelectExpr(SetCC->getOperand(0)); // Val to compare against
1116 BuildMI(BB, PPC::FNEG, 1, Tmp2).addReg(Tmp1);
1117 }
Nate Begeman3e897162005-03-31 23:55:40 +00001118 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp2).addReg(TV).addReg(FV);
1119 return Result;
1120 }
1121 }
1122 } else {
1123 Opc = (MVT::f64 == VT) ? PPC::FSUB : PPC::FSUBS;
Nate Begemanaf4ab1b2005-04-09 09:33:07 +00001124 Tmp1 = SelectExpr(SetCC->getOperand(0)); // Val to compare against
Nate Begeman3e897162005-03-31 23:55:40 +00001125 Tmp2 = SelectExpr(SetCC->getOperand(1));
1126 Tmp3 = MakeReg(VT);
1127 switch(SetCC->getCondition()) {
1128 default: assert(0 && "Invalid FSEL condition"); abort();
1129 case ISD::SETULT:
1130 case ISD::SETLT:
1131 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
1132 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(FV).addReg(TV);
1133 return Result;
1134 case ISD::SETUGE:
1135 case ISD::SETGE:
1136 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
1137 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(TV).addReg(FV);
1138 return Result;
1139 case ISD::SETUGT:
1140 case ISD::SETGT:
1141 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp2).addReg(Tmp1);
1142 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(FV).addReg(TV);
1143 return Result;
1144 case ISD::SETULE:
1145 case ISD::SETLE:
1146 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp2).addReg(Tmp1);
1147 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(TV).addReg(FV);
1148 return Result;
1149 }
1150 }
1151 assert(0 && "Should never get here");
1152 return 0;
1153 }
1154
Nate Begeman31318e42005-04-01 07:21:30 +00001155 unsigned TrueValue = SelectExpr(N.getOperand(1)); //Use if TRUE
1156 unsigned FalseValue = SelectExpr(N.getOperand(2)); //Use if FALSE
Nate Begeman6cb2e1b2005-04-01 08:57:43 +00001157 Opc = SelectSetCR0(N.getOperand(0));
Nate Begeman31318e42005-04-01 07:21:30 +00001158
Nate Begeman23afcfb2005-03-29 22:48:55 +00001159 // Create an iterator with which to insert the MBB for copying the false
1160 // value and the MBB to hold the PHI instruction for this SetCC.
1161 MachineBasicBlock *thisMBB = BB;
1162 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1163 ilist<MachineBasicBlock>::iterator It = BB;
1164 ++It;
1165
1166 // thisMBB:
1167 // ...
1168 // TrueVal = ...
1169 // cmpTY cr0, r1, r2
1170 // bCC copy1MBB
1171 // fallthrough --> copy0MBB
Nate Begeman23afcfb2005-03-29 22:48:55 +00001172 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
1173 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
Nate Begeman6cb2e1b2005-04-01 08:57:43 +00001174 BuildMI(BB, Opc, 2).addReg(PPC::CR0).addMBB(sinkMBB);
Nate Begeman23afcfb2005-03-29 22:48:55 +00001175 MachineFunction *F = BB->getParent();
1176 F->getBasicBlockList().insert(It, copy0MBB);
1177 F->getBasicBlockList().insert(It, sinkMBB);
1178 // Update machine-CFG edges
1179 BB->addSuccessor(copy0MBB);
1180 BB->addSuccessor(sinkMBB);
1181
1182 // copy0MBB:
1183 // %FalseValue = ...
1184 // # fallthrough to sinkMBB
1185 BB = copy0MBB;
Nate Begeman23afcfb2005-03-29 22:48:55 +00001186 // Update machine-CFG edges
1187 BB->addSuccessor(sinkMBB);
1188
1189 // sinkMBB:
1190 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1191 // ...
1192 BB = sinkMBB;
1193 BuildMI(BB, PPC::PHI, 4, Result).addReg(FalseValue)
1194 .addMBB(copy0MBB).addReg(TrueValue).addMBB(thisMBB);
1195 return Result;
1196 }
Nate Begeman27eeb002005-04-02 05:59:34 +00001197
1198 case ISD::FNEG:
Nate Begeman93075ec2005-04-04 23:40:36 +00001199 if (!NoExcessFPPrecision &&
1200 ISD::ADD == N.getOperand(0).getOpcode() &&
1201 N.getOperand(0).Val->hasOneUse() &&
1202 ISD::MUL == N.getOperand(0).getOperand(0).getOpcode() &&
1203 N.getOperand(0).getOperand(0).Val->hasOneUse()) {
Nate Begeman80196b12005-04-05 00:15:08 +00001204 ++FusedFP; // Statistic
Nate Begeman93075ec2005-04-04 23:40:36 +00001205 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(0));
1206 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(1));
1207 Tmp3 = SelectExpr(N.getOperand(0).getOperand(1));
1208 Opc = DestType == MVT::f64 ? PPC::FNMADD : PPC::FNMADDS;
1209 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1210 } else if (!NoExcessFPPrecision &&
Nate Begemane88aa5b2005-04-09 03:05:51 +00001211 ISD::ADD == N.getOperand(0).getOpcode() &&
Nate Begeman93075ec2005-04-04 23:40:36 +00001212 N.getOperand(0).Val->hasOneUse() &&
Nate Begemane88aa5b2005-04-09 03:05:51 +00001213 ISD::MUL == N.getOperand(0).getOperand(1).getOpcode() &&
1214 N.getOperand(0).getOperand(1).Val->hasOneUse()) {
Nate Begeman80196b12005-04-05 00:15:08 +00001215 ++FusedFP; // Statistic
Nate Begemane88aa5b2005-04-09 03:05:51 +00001216 Tmp1 = SelectExpr(N.getOperand(0).getOperand(1).getOperand(0));
1217 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1).getOperand(1));
1218 Tmp3 = SelectExpr(N.getOperand(0).getOperand(0));
1219 Opc = DestType == MVT::f64 ? PPC::FNMADD : PPC::FNMADDS;
Nate Begeman93075ec2005-04-04 23:40:36 +00001220 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1221 } else if (ISD::FABS == N.getOperand(0).getOpcode()) {
Nate Begeman27eeb002005-04-02 05:59:34 +00001222 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1223 BuildMI(BB, PPC::FNABS, 1, Result).addReg(Tmp1);
1224 } else {
1225 Tmp1 = SelectExpr(N.getOperand(0));
1226 BuildMI(BB, PPC::FNEG, 1, Result).addReg(Tmp1);
1227 }
1228 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001229
Nate Begeman27eeb002005-04-02 05:59:34 +00001230 case ISD::FABS:
1231 Tmp1 = SelectExpr(N.getOperand(0));
1232 BuildMI(BB, PPC::FABS, 1, Result).addReg(Tmp1);
1233 return Result;
1234
Nate Begemana9795f82005-03-24 04:41:43 +00001235 case ISD::FP_ROUND:
1236 assert (DestType == MVT::f32 &&
1237 N.getOperand(0).getValueType() == MVT::f64 &&
1238 "only f64 to f32 conversion supported here");
1239 Tmp1 = SelectExpr(N.getOperand(0));
1240 BuildMI(BB, PPC::FRSP, 1, Result).addReg(Tmp1);
1241 return Result;
1242
1243 case ISD::FP_EXTEND:
1244 assert (DestType == MVT::f64 &&
1245 N.getOperand(0).getValueType() == MVT::f32 &&
1246 "only f32 to f64 conversion supported here");
1247 Tmp1 = SelectExpr(N.getOperand(0));
1248 BuildMI(BB, PPC::FMR, 1, Result).addReg(Tmp1);
1249 return Result;
1250
1251 case ISD::CopyFromReg:
Nate Begemanf2622612005-03-26 02:17:46 +00001252 if (Result == 1)
1253 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1254 Tmp1 = dyn_cast<RegSDNode>(Node)->getReg();
1255 BuildMI(BB, PPC::FMR, 1, Result).addReg(Tmp1);
1256 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001257
Nate Begeman6d369cc2005-04-01 01:08:07 +00001258 case ISD::ConstantFP: {
Nate Begeman6d369cc2005-04-01 01:08:07 +00001259 ConstantFPSDNode *CN = cast<ConstantFPSDNode>(N);
Nate Begeman6b559972005-04-01 02:59:27 +00001260 Result = getConstDouble(CN->getValue(), Result);
Nate Begeman6d369cc2005-04-01 01:08:07 +00001261 return Result;
1262 }
Nate Begemana9795f82005-03-24 04:41:43 +00001263
Nate Begemana9795f82005-03-24 04:41:43 +00001264 case ISD::ADD:
Nate Begeman93075ec2005-04-04 23:40:36 +00001265 if (!NoExcessFPPrecision && N.getOperand(0).getOpcode() == ISD::MUL &&
1266 N.getOperand(0).Val->hasOneUse()) {
1267 ++FusedFP; // Statistic
1268 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1269 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1270 Tmp3 = SelectExpr(N.getOperand(1));
1271 Opc = DestType == MVT::f64 ? PPC::FMADD : PPC::FMADDS;
1272 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1273 return Result;
1274 }
Nate Begemane88aa5b2005-04-09 03:05:51 +00001275 if (!NoExcessFPPrecision && N.getOperand(1).getOpcode() == ISD::MUL &&
1276 N.getOperand(1).Val->hasOneUse()) {
1277 ++FusedFP; // Statistic
1278 Tmp1 = SelectExpr(N.getOperand(1).getOperand(0));
1279 Tmp2 = SelectExpr(N.getOperand(1).getOperand(1));
1280 Tmp3 = SelectExpr(N.getOperand(0));
1281 Opc = DestType == MVT::f64 ? PPC::FMADD : PPC::FMADDS;
1282 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1283 return Result;
1284 }
Nate Begeman93075ec2005-04-04 23:40:36 +00001285 Opc = DestType == MVT::f64 ? PPC::FADD : PPC::FADDS;
1286 Tmp1 = SelectExpr(N.getOperand(0));
1287 Tmp2 = SelectExpr(N.getOperand(1));
1288 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1289 return Result;
1290
Nate Begemana9795f82005-03-24 04:41:43 +00001291 case ISD::SUB:
Nate Begeman93075ec2005-04-04 23:40:36 +00001292 if (!NoExcessFPPrecision && N.getOperand(0).getOpcode() == ISD::MUL &&
1293 N.getOperand(0).Val->hasOneUse()) {
1294 ++FusedFP; // Statistic
1295 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1296 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1297 Tmp3 = SelectExpr(N.getOperand(1));
1298 Opc = DestType == MVT::f64 ? PPC::FMSUB : PPC::FMSUBS;
1299 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1300 return Result;
1301 }
Nate Begemane88aa5b2005-04-09 03:05:51 +00001302 if (!NoExcessFPPrecision && N.getOperand(1).getOpcode() == ISD::MUL &&
1303 N.getOperand(1).Val->hasOneUse()) {
1304 ++FusedFP; // Statistic
1305 Tmp1 = SelectExpr(N.getOperand(1).getOperand(0));
1306 Tmp2 = SelectExpr(N.getOperand(1).getOperand(1));
1307 Tmp3 = SelectExpr(N.getOperand(0));
1308 Opc = DestType == MVT::f64 ? PPC::FNMSUB : PPC::FNMSUBS;
1309 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1310 return Result;
1311 }
Nate Begeman93075ec2005-04-04 23:40:36 +00001312 Opc = DestType == MVT::f64 ? PPC::FSUB : PPC::FSUBS;
1313 Tmp1 = SelectExpr(N.getOperand(0));
1314 Tmp2 = SelectExpr(N.getOperand(1));
1315 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1316 return Result;
1317
1318 case ISD::MUL:
Nate Begemana9795f82005-03-24 04:41:43 +00001319 case ISD::SDIV:
1320 switch( opcode ) {
1321 case ISD::MUL: Opc = DestType == MVT::f64 ? PPC::FMUL : PPC::FMULS; break;
Nate Begemana9795f82005-03-24 04:41:43 +00001322 case ISD::SDIV: Opc = DestType == MVT::f64 ? PPC::FDIV : PPC::FDIVS; break;
1323 };
Nate Begemana9795f82005-03-24 04:41:43 +00001324 Tmp1 = SelectExpr(N.getOperand(0));
1325 Tmp2 = SelectExpr(N.getOperand(1));
1326 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1327 return Result;
1328
Nate Begemana9795f82005-03-24 04:41:43 +00001329 case ISD::UINT_TO_FP:
Nate Begemanfdcf3412005-03-30 19:38:35 +00001330 case ISD::SINT_TO_FP: {
1331 assert (N.getOperand(0).getValueType() == MVT::i32
1332 && "int to float must operate on i32");
1333 bool IsUnsigned = (ISD::UINT_TO_FP == opcode);
1334 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
1335 Tmp2 = MakeReg(MVT::f64); // temp reg to load the integer value into
1336 Tmp3 = MakeReg(MVT::i32); // temp reg to hold the conversion constant
Nate Begemanfdcf3412005-03-30 19:38:35 +00001337
1338 int FrameIdx = BB->getParent()->getFrameInfo()->CreateStackObject(8, 8);
1339 MachineConstantPool *CP = BB->getParent()->getConstantPool();
1340
Nate Begemanfdcf3412005-03-30 19:38:35 +00001341 if (IsUnsigned) {
Nate Begeman709c8062005-04-10 06:06:10 +00001342 unsigned ConstF = getConstDouble(0x1.000000p52);
Nate Begemanfdcf3412005-03-30 19:38:35 +00001343 // Store the hi & low halves of the fp value, currently in int regs
1344 BuildMI(BB, PPC::LIS, 1, Tmp3).addSImm(0x4330);
1345 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(Tmp3), FrameIdx);
1346 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(Tmp1), FrameIdx, 4);
1347 addFrameReference(BuildMI(BB, PPC::LFD, 2, Tmp2), FrameIdx);
1348 // Generate the return value with a subtract
1349 BuildMI(BB, PPC::FSUB, 2, Result).addReg(Tmp2).addReg(ConstF);
1350 } else {
Nate Begeman709c8062005-04-10 06:06:10 +00001351 unsigned ConstF = getConstDouble(0x1.000008p52);
Nate Begemanfdcf3412005-03-30 19:38:35 +00001352 unsigned TmpL = MakeReg(MVT::i32);
Nate Begemanfdcf3412005-03-30 19:38:35 +00001353 // Store the hi & low halves of the fp value, currently in int regs
1354 BuildMI(BB, PPC::LIS, 1, Tmp3).addSImm(0x4330);
1355 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(Tmp3), FrameIdx);
1356 BuildMI(BB, PPC::XORIS, 2, TmpL).addReg(Tmp1).addImm(0x8000);
1357 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(TmpL), FrameIdx, 4);
1358 addFrameReference(BuildMI(BB, PPC::LFD, 2, Tmp2), FrameIdx);
1359 // Generate the return value with a subtract
1360 BuildMI(BB, PPC::FSUB, 2, Result).addReg(Tmp2).addReg(ConstF);
1361 }
1362 return Result;
1363 }
Nate Begemana9795f82005-03-24 04:41:43 +00001364 }
Nate Begeman6b559972005-04-01 02:59:27 +00001365 assert(0 && "Should never get here");
Nate Begemana9795f82005-03-24 04:41:43 +00001366 return 0;
1367}
1368
Nate Begemanc7bd4822005-04-11 06:34:10 +00001369unsigned ISel::SelectExpr(SDOperand N, bool Recording) {
Nate Begemana9795f82005-03-24 04:41:43 +00001370 unsigned Result;
1371 unsigned Tmp1, Tmp2, Tmp3;
1372 unsigned Opc = 0;
1373 unsigned opcode = N.getOpcode();
1374
1375 SDNode *Node = N.Val;
1376 MVT::ValueType DestType = N.getValueType();
1377
1378 unsigned &Reg = ExprMap[N];
1379 if (Reg) return Reg;
1380
Nate Begeman27eeb002005-04-02 05:59:34 +00001381 switch (N.getOpcode()) {
1382 default:
Nate Begemana9795f82005-03-24 04:41:43 +00001383 Reg = Result = (N.getValueType() != MVT::Other) ?
Nate Begeman27eeb002005-04-02 05:59:34 +00001384 MakeReg(N.getValueType()) : 1;
1385 break;
1386 case ISD::CALL:
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001387 // If this is a call instruction, make sure to prepare ALL of the result
1388 // values as well as the chain.
Nate Begeman27eeb002005-04-02 05:59:34 +00001389 if (Node->getNumValues() == 1)
1390 Reg = Result = 1; // Void call, just a chain.
1391 else {
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001392 Result = MakeReg(Node->getValueType(0));
1393 ExprMap[N.getValue(0)] = Result;
Nate Begeman27eeb002005-04-02 05:59:34 +00001394 for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001395 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
Nate Begeman27eeb002005-04-02 05:59:34 +00001396 ExprMap[SDOperand(Node, Node->getNumValues()-1)] = 1;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001397 }
Nate Begeman27eeb002005-04-02 05:59:34 +00001398 break;
1399 case ISD::ADD_PARTS:
1400 case ISD::SUB_PARTS:
1401 case ISD::SHL_PARTS:
1402 case ISD::SRL_PARTS:
1403 case ISD::SRA_PARTS:
1404 Result = MakeReg(Node->getValueType(0));
1405 ExprMap[N.getValue(0)] = Result;
1406 for (unsigned i = 1, e = N.Val->getNumValues(); i != e; ++i)
1407 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
1408 break;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001409 }
1410
Nate Begemane5846682005-04-04 06:52:38 +00001411 if (ISD::CopyFromReg == opcode)
1412 DestType = N.getValue(0).getValueType();
1413
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001414 if (DestType == MVT::f64 || DestType == MVT::f32)
Nate Begemana0e3e942005-04-10 01:14:13 +00001415 if (ISD::LOAD != opcode && ISD::EXTLOAD != opcode &&
1416 ISD::UNDEF != opcode && ISD::CALL != opcode)
Nate Begeman74d73452005-03-31 00:15:26 +00001417 return SelectExprFP(N, Result);
Nate Begemana9795f82005-03-24 04:41:43 +00001418
1419 switch (opcode) {
1420 default:
1421 Node->dump();
1422 assert(0 && "Node not handled!\n");
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001423 case ISD::UNDEF:
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001424 BuildMI(BB, PPC::IMPLICIT_DEF, 0, Result);
1425 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001426 case ISD::DYNAMIC_STACKALLOC:
Nate Begeman5e966612005-03-24 06:28:42 +00001427 // Generate both result values. FIXME: Need a better commment here?
1428 if (Result != 1)
1429 ExprMap[N.getValue(1)] = 1;
1430 else
1431 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1432
1433 // FIXME: We are currently ignoring the requested alignment for handling
1434 // greater than the stack alignment. This will need to be revisited at some
1435 // point. Align = N.getOperand(2);
1436 if (!isa<ConstantSDNode>(N.getOperand(2)) ||
1437 cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) {
1438 std::cerr << "Cannot allocate stack object with greater alignment than"
1439 << " the stack alignment yet!";
1440 abort();
1441 }
1442 Select(N.getOperand(0));
1443 Tmp1 = SelectExpr(N.getOperand(1));
1444 // Subtract size from stack pointer, thereby allocating some space.
1445 BuildMI(BB, PPC::SUBF, 2, PPC::R1).addReg(Tmp1).addReg(PPC::R1);
1446 // Put a pointer to the space into the result register by copying the SP
1447 BuildMI(BB, PPC::OR, 2, Result).addReg(PPC::R1).addReg(PPC::R1);
1448 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001449
1450 case ISD::ConstantPool:
Nate Begemanca12a2b2005-03-28 22:28:37 +00001451 Tmp1 = cast<ConstantPoolSDNode>(N)->getIndex();
1452 Tmp2 = MakeReg(MVT::i32);
1453 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp2).addReg(getGlobalBaseReg())
1454 .addConstantPoolIndex(Tmp1);
1455 BuildMI(BB, PPC::LA, 2, Result).addReg(Tmp2).addConstantPoolIndex(Tmp1);
1456 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001457
1458 case ISD::FrameIndex:
Nate Begemanf3d08f32005-03-29 00:03:27 +00001459 Tmp1 = cast<FrameIndexSDNode>(N)->getIndex();
Nate Begeman58f718c2005-03-30 02:23:08 +00001460 addFrameReference(BuildMI(BB, PPC::ADDI, 2, Result), (int)Tmp1, 0, false);
Nate Begemanf3d08f32005-03-29 00:03:27 +00001461 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001462
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001463 case ISD::GlobalAddress: {
1464 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
Nate Begemanca12a2b2005-03-28 22:28:37 +00001465 Tmp1 = MakeReg(MVT::i32);
Nate Begemanc7b09f12005-03-25 08:34:25 +00001466 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp1).addReg(getGlobalBaseReg())
1467 .addGlobalAddress(GV);
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001468 if (GV->hasWeakLinkage() || GV->isExternal()) {
1469 BuildMI(BB, PPC::LWZ, 2, Result).addGlobalAddress(GV).addReg(Tmp1);
1470 } else {
1471 BuildMI(BB, PPC::LA, 2, Result).addReg(Tmp1).addGlobalAddress(GV);
1472 }
1473 return Result;
1474 }
1475
Nate Begeman5e966612005-03-24 06:28:42 +00001476 case ISD::LOAD:
Nate Begemana9795f82005-03-24 04:41:43 +00001477 case ISD::EXTLOAD:
1478 case ISD::ZEXTLOAD:
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001479 case ISD::SEXTLOAD: {
Nate Begeman9db505c2005-03-28 19:36:43 +00001480 MVT::ValueType TypeBeingLoaded = (ISD::LOAD == opcode) ?
1481 Node->getValueType(0) : cast<MVTSDNode>(Node)->getExtraValueType();
Nate Begeman74d73452005-03-31 00:15:26 +00001482 bool sext = (ISD::SEXTLOAD == opcode);
Nate Begeman74d73452005-03-31 00:15:26 +00001483
Nate Begeman5e966612005-03-24 06:28:42 +00001484 // Make sure we generate both values.
1485 if (Result != 1)
1486 ExprMap[N.getValue(1)] = 1; // Generate the token
1487 else
1488 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1489
1490 SDOperand Chain = N.getOperand(0);
1491 SDOperand Address = N.getOperand(1);
1492 Select(Chain);
1493
Nate Begeman9db505c2005-03-28 19:36:43 +00001494 switch (TypeBeingLoaded) {
Nate Begeman74d73452005-03-31 00:15:26 +00001495 default: Node->dump(); assert(0 && "Cannot load this type!");
Nate Begeman9db505c2005-03-28 19:36:43 +00001496 case MVT::i1: Opc = PPC::LBZ; break;
1497 case MVT::i8: Opc = PPC::LBZ; break;
1498 case MVT::i16: Opc = sext ? PPC::LHA : PPC::LHZ; break;
1499 case MVT::i32: Opc = PPC::LWZ; break;
Nate Begeman74d73452005-03-31 00:15:26 +00001500 case MVT::f32: Opc = PPC::LFS; break;
1501 case MVT::f64: Opc = PPC::LFD; break;
Nate Begeman5e966612005-03-24 06:28:42 +00001502 }
1503
Nate Begeman74d73452005-03-31 00:15:26 +00001504 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Address)) {
1505 Tmp1 = MakeReg(MVT::i32);
1506 int CPI = CP->getIndex();
1507 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp1).addReg(getGlobalBaseReg())
1508 .addConstantPoolIndex(CPI);
1509 BuildMI(BB, Opc, 2, Result).addConstantPoolIndex(CPI).addReg(Tmp1);
Nate Begeman9db505c2005-03-28 19:36:43 +00001510 }
Nate Begeman74d73452005-03-31 00:15:26 +00001511 else if(Address.getOpcode() == ISD::FrameIndex) {
Nate Begeman58f718c2005-03-30 02:23:08 +00001512 Tmp1 = cast<FrameIndexSDNode>(Address)->getIndex();
1513 addFrameReference(BuildMI(BB, Opc, 2, Result), (int)Tmp1);
Nate Begeman5e966612005-03-24 06:28:42 +00001514 } else {
1515 int offset;
Nate Begeman04730362005-04-01 04:45:11 +00001516 bool idx = SelectAddr(Address, Tmp1, offset);
1517 if (idx) {
1518 Opc = IndexedOpForOp(Opc);
1519 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(offset);
1520 } else {
1521 BuildMI(BB, Opc, 2, Result).addSImm(offset).addReg(Tmp1);
1522 }
Nate Begeman5e966612005-03-24 06:28:42 +00001523 }
1524 return Result;
1525 }
1526
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001527 case ISD::CALL: {
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001528 unsigned GPR_idx = 0, FPR_idx = 0;
1529 static const unsigned GPR[] = {
1530 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
1531 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
1532 };
1533 static const unsigned FPR[] = {
1534 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
1535 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
1536 };
1537
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001538 // Lower the chain for this call.
1539 Select(N.getOperand(0));
1540 ExprMap[N.getValue(Node->getNumValues()-1)] = 1;
Nate Begeman74d73452005-03-31 00:15:26 +00001541
Nate Begemand860aa62005-04-04 22:17:48 +00001542 MachineInstr *CallMI;
1543 // Emit the correct call instruction based on the type of symbol called.
1544 if (GlobalAddressSDNode *GASD =
1545 dyn_cast<GlobalAddressSDNode>(N.getOperand(1))) {
1546 CallMI = BuildMI(PPC::CALLpcrel, 1).addGlobalAddress(GASD->getGlobal(),
1547 true);
1548 } else if (ExternalSymbolSDNode *ESSDN =
1549 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1))) {
1550 CallMI = BuildMI(PPC::CALLpcrel, 1).addExternalSymbol(ESSDN->getSymbol(),
1551 true);
1552 } else {
1553 Tmp1 = SelectExpr(N.getOperand(1));
1554 BuildMI(BB, PPC::OR, 2, PPC::R12).addReg(Tmp1).addReg(Tmp1);
1555 BuildMI(BB, PPC::MTCTR, 1).addReg(PPC::R12);
1556 CallMI = BuildMI(PPC::CALLindirect, 3).addImm(20).addImm(0)
1557 .addReg(PPC::R12);
1558 }
1559
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001560 // Load the register args to virtual regs
1561 std::vector<unsigned> ArgVR;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001562 for(int i = 2, e = Node->getNumOperands(); i < e; ++i)
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001563 ArgVR.push_back(SelectExpr(N.getOperand(i)));
1564
1565 // Copy the virtual registers into the appropriate argument register
1566 for(int i = 0, e = ArgVR.size(); i < e; ++i) {
1567 switch(N.getOperand(i+2).getValueType()) {
1568 default: Node->dump(); assert(0 && "Unknown value type for call");
1569 case MVT::i1:
1570 case MVT::i8:
1571 case MVT::i16:
1572 case MVT::i32:
1573 assert(GPR_idx < 8 && "Too many int args");
Nate Begemand860aa62005-04-04 22:17:48 +00001574 if (N.getOperand(i+2).getOpcode() != ISD::UNDEF) {
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001575 BuildMI(BB, PPC::OR,2,GPR[GPR_idx]).addReg(ArgVR[i]).addReg(ArgVR[i]);
Nate Begemand860aa62005-04-04 22:17:48 +00001576 CallMI->addRegOperand(GPR[GPR_idx], MachineOperand::Use);
1577 }
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001578 ++GPR_idx;
1579 break;
1580 case MVT::f64:
1581 case MVT::f32:
1582 assert(FPR_idx < 13 && "Too many fp args");
1583 BuildMI(BB, PPC::FMR, 1, FPR[FPR_idx]).addReg(ArgVR[i]);
Nate Begemand860aa62005-04-04 22:17:48 +00001584 CallMI->addRegOperand(FPR[FPR_idx], MachineOperand::Use);
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001585 ++FPR_idx;
1586 break;
1587 }
1588 }
Nate Begemand860aa62005-04-04 22:17:48 +00001589
1590 // Put the call instruction in the correct place in the MachineBasicBlock
1591 BB->push_back(CallMI);
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001592
1593 switch (Node->getValueType(0)) {
1594 default: assert(0 && "Unknown value type for call result!");
1595 case MVT::Other: return 1;
1596 case MVT::i1:
1597 case MVT::i8:
1598 case MVT::i16:
1599 case MVT::i32:
Nate Begemane5846682005-04-04 06:52:38 +00001600 if (Node->getValueType(1) == MVT::i32) {
1601 BuildMI(BB, PPC::OR, 2, Result+1).addReg(PPC::R3).addReg(PPC::R3);
1602 BuildMI(BB, PPC::OR, 2, Result).addReg(PPC::R4).addReg(PPC::R4);
1603 } else {
1604 BuildMI(BB, PPC::OR, 2, Result).addReg(PPC::R3).addReg(PPC::R3);
1605 }
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001606 break;
1607 case MVT::f32:
1608 case MVT::f64:
1609 BuildMI(BB, PPC::FMR, 1, Result).addReg(PPC::F1);
1610 break;
1611 }
1612 return Result+N.ResNo;
1613 }
Nate Begemana9795f82005-03-24 04:41:43 +00001614
1615 case ISD::SIGN_EXTEND:
1616 case ISD::SIGN_EXTEND_INREG:
1617 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman9db505c2005-03-28 19:36:43 +00001618 switch(cast<MVTSDNode>(Node)->getExtraValueType()) {
1619 default: Node->dump(); assert(0 && "Unhandled SIGN_EXTEND type"); break;
Nate Begemanc7bd4822005-04-11 06:34:10 +00001620 case MVT::i16:
Nate Begeman9db505c2005-03-28 19:36:43 +00001621 BuildMI(BB, PPC::EXTSH, 1, Result).addReg(Tmp1);
1622 break;
Nate Begemanc7bd4822005-04-11 06:34:10 +00001623 case MVT::i8:
Nate Begeman9db505c2005-03-28 19:36:43 +00001624 BuildMI(BB, PPC::EXTSB, 1, Result).addReg(Tmp1);
1625 break;
Nate Begeman74747862005-03-29 22:24:51 +00001626 case MVT::i1:
1627 BuildMI(BB, PPC::SUBFIC, 2, Result).addReg(Tmp1).addSImm(0);
1628 break;
Nate Begeman9db505c2005-03-28 19:36:43 +00001629 }
Nate Begemana9795f82005-03-24 04:41:43 +00001630 return Result;
1631
1632 case ISD::ZERO_EXTEND_INREG:
1633 Tmp1 = SelectExpr(N.getOperand(0));
1634 switch(cast<MVTSDNode>(Node)->getExtraValueType()) {
Nate Begeman9db505c2005-03-28 19:36:43 +00001635 default: Node->dump(); assert(0 && "Unhandled ZERO_EXTEND type"); break;
Nate Begemana9795f82005-03-24 04:41:43 +00001636 case MVT::i16: Tmp2 = 16; break;
1637 case MVT::i8: Tmp2 = 24; break;
1638 case MVT::i1: Tmp2 = 31; break;
1639 }
Nate Begeman33162522005-03-29 21:54:38 +00001640 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(0).addImm(Tmp2)
1641 .addImm(31);
Nate Begemana9795f82005-03-24 04:41:43 +00001642 return Result;
1643
Nate Begemana9795f82005-03-24 04:41:43 +00001644 case ISD::CopyFromReg:
1645 if (Result == 1)
1646 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1647 Tmp1 = dyn_cast<RegSDNode>(Node)->getReg();
1648 BuildMI(BB, PPC::OR, 2, Result).addReg(Tmp1).addReg(Tmp1);
1649 return Result;
1650
1651 case ISD::SHL:
Nate Begeman5e966612005-03-24 06:28:42 +00001652 Tmp1 = SelectExpr(N.getOperand(0));
1653 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1654 Tmp2 = CN->getValue() & 0x1F;
Nate Begeman33162522005-03-29 21:54:38 +00001655 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(Tmp2).addImm(0)
Nate Begeman5e966612005-03-24 06:28:42 +00001656 .addImm(31-Tmp2);
1657 } else {
1658 Tmp2 = SelectExpr(N.getOperand(1));
1659 BuildMI(BB, PPC::SLW, 2, Result).addReg(Tmp1).addReg(Tmp2);
1660 }
1661 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001662
Nate Begeman5e966612005-03-24 06:28:42 +00001663 case ISD::SRL:
1664 Tmp1 = SelectExpr(N.getOperand(0));
1665 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1666 Tmp2 = CN->getValue() & 0x1F;
Nate Begeman33162522005-03-29 21:54:38 +00001667 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(32-Tmp2)
Nate Begeman5e966612005-03-24 06:28:42 +00001668 .addImm(Tmp2).addImm(31);
1669 } else {
1670 Tmp2 = SelectExpr(N.getOperand(1));
1671 BuildMI(BB, PPC::SRW, 2, Result).addReg(Tmp1).addReg(Tmp2);
1672 }
1673 return Result;
1674
1675 case ISD::SRA:
1676 Tmp1 = SelectExpr(N.getOperand(0));
1677 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1678 Tmp2 = CN->getValue() & 0x1F;
1679 BuildMI(BB, PPC::SRAWI, 2, Result).addReg(Tmp1).addImm(Tmp2);
1680 } else {
1681 Tmp2 = SelectExpr(N.getOperand(1));
1682 BuildMI(BB, PPC::SRAW, 2, Result).addReg(Tmp1).addReg(Tmp2);
1683 }
1684 return Result;
1685
Nate Begemana9795f82005-03-24 04:41:43 +00001686 case ISD::ADD:
1687 assert (DestType == MVT::i32 && "Only do arithmetic on i32s!");
1688 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman439b4442005-04-05 04:22:58 +00001689 switch(getImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
Nate Begemana9795f82005-03-24 04:41:43 +00001690 default: assert(0 && "unhandled result code");
1691 case 0: // No immediate
1692 Tmp2 = SelectExpr(N.getOperand(1));
1693 BuildMI(BB, PPC::ADD, 2, Result).addReg(Tmp1).addReg(Tmp2);
1694 break;
1695 case 1: // Low immediate
1696 BuildMI(BB, PPC::ADDI, 2, Result).addReg(Tmp1).addSImm(Tmp2);
1697 break;
1698 case 2: // Shifted immediate
1699 BuildMI(BB, PPC::ADDIS, 2, Result).addReg(Tmp1).addSImm(Tmp2);
1700 break;
1701 }
1702 return Result;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001703
Nate Begemana9795f82005-03-24 04:41:43 +00001704 case ISD::AND:
Nate Begeman7ddecb42005-04-06 23:51:40 +00001705 Tmp1 = SelectExpr(N.getOperand(0));
1706 // FIXME: should add check in getImmediateForOpcode to return a value
1707 // indicating the immediate is a run of set bits so we can emit a bitfield
1708 // clear with RLWINM instead.
1709 switch(getImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
1710 default: assert(0 && "unhandled result code");
1711 case 0: // No immediate
1712 Tmp2 = SelectExpr(N.getOperand(1));
Nate Begemanc7bd4822005-04-11 06:34:10 +00001713 Opc = Recording ? PPC::ANDo : PPC::AND;
1714 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
Nate Begeman7ddecb42005-04-06 23:51:40 +00001715 break;
1716 case 1: // Low immediate
1717 BuildMI(BB, PPC::ANDIo, 2, Result).addReg(Tmp1).addImm(Tmp2);
1718 break;
1719 case 2: // Shifted immediate
1720 BuildMI(BB, PPC::ANDISo, 2, Result).addReg(Tmp1).addImm(Tmp2);
1721 break;
1722 }
Nate Begemanc7bd4822005-04-11 06:34:10 +00001723 RecordSuccess = true;
Nate Begeman7ddecb42005-04-06 23:51:40 +00001724 return Result;
1725
Nate Begemana9795f82005-03-24 04:41:43 +00001726 case ISD::OR:
Nate Begeman7ddecb42005-04-06 23:51:40 +00001727 if (SelectBitfieldInsert(N, Result))
1728 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001729 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman439b4442005-04-05 04:22:58 +00001730 switch(getImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
Nate Begemana9795f82005-03-24 04:41:43 +00001731 default: assert(0 && "unhandled result code");
1732 case 0: // No immediate
1733 Tmp2 = SelectExpr(N.getOperand(1));
Nate Begemanc7bd4822005-04-11 06:34:10 +00001734 Opc = Recording ? PPC::ORo : PPC::OR;
1735 RecordSuccess = true;
1736 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +00001737 break;
1738 case 1: // Low immediate
Nate Begeman7ddecb42005-04-06 23:51:40 +00001739 BuildMI(BB, PPC::ORI, 2, Result).addReg(Tmp1).addImm(Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +00001740 break;
1741 case 2: // Shifted immediate
Nate Begeman7ddecb42005-04-06 23:51:40 +00001742 BuildMI(BB, PPC::ORIS, 2, Result).addReg(Tmp1).addImm(Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +00001743 break;
1744 }
1745 return Result;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001746
Nate Begemanaa73a9f2005-04-03 11:20:20 +00001747 case ISD::XOR: {
1748 // Check for EQV: xor, (xor a, -1), b
1749 if (N.getOperand(0).getOpcode() == ISD::XOR &&
1750 N.getOperand(0).getOperand(1).getOpcode() == ISD::Constant &&
1751 cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->isAllOnesValue()) {
Nate Begemanaa73a9f2005-04-03 11:20:20 +00001752 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1753 Tmp2 = SelectExpr(N.getOperand(1));
1754 BuildMI(BB, PPC::EQV, 2, Result).addReg(Tmp1).addReg(Tmp2);
1755 return Result;
1756 }
1757 // Check for NOT, NOR, and NAND: xor (copy, or, and), -1
1758 if (N.getOperand(1).getOpcode() == ISD::Constant &&
1759 cast<ConstantSDNode>(N.getOperand(1))->isAllOnesValue()) {
Nate Begemanaa73a9f2005-04-03 11:20:20 +00001760 switch(N.getOperand(0).getOpcode()) {
1761 case ISD::OR:
1762 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1763 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1764 BuildMI(BB, PPC::NOR, 2, Result).addReg(Tmp1).addReg(Tmp2);
1765 break;
1766 case ISD::AND:
1767 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1768 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1769 BuildMI(BB, PPC::NAND, 2, Result).addReg(Tmp1).addReg(Tmp2);
1770 break;
1771 default:
1772 Tmp1 = SelectExpr(N.getOperand(0));
1773 BuildMI(BB, PPC::NOR, 2, Result).addReg(Tmp1).addReg(Tmp1);
1774 break;
1775 }
1776 return Result;
1777 }
1778 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman439b4442005-04-05 04:22:58 +00001779 switch(getImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
Nate Begemanaa73a9f2005-04-03 11:20:20 +00001780 default: assert(0 && "unhandled result code");
1781 case 0: // No immediate
1782 Tmp2 = SelectExpr(N.getOperand(1));
1783 BuildMI(BB, PPC::XOR, 2, Result).addReg(Tmp1).addReg(Tmp2);
1784 break;
1785 case 1: // Low immediate
1786 BuildMI(BB, PPC::XORI, 2, Result).addReg(Tmp1).addImm(Tmp2);
1787 break;
1788 case 2: // Shifted immediate
1789 BuildMI(BB, PPC::XORIS, 2, Result).addReg(Tmp1).addImm(Tmp2);
1790 break;
1791 }
1792 return Result;
1793 }
1794
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001795 case ISD::SUB:
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001796 Tmp2 = SelectExpr(N.getOperand(1));
Nate Begeman439b4442005-04-05 04:22:58 +00001797 if (1 == getImmediateForOpcode(N.getOperand(0), opcode, Tmp1))
Nate Begeman27523a12005-04-02 00:42:16 +00001798 BuildMI(BB, PPC::SUBFIC, 2, Result).addReg(Tmp2).addSImm(Tmp1);
1799 else {
1800 Tmp1 = SelectExpr(N.getOperand(0));
1801 BuildMI(BB, PPC::SUBF, 2, Result).addReg(Tmp2).addReg(Tmp1);
1802 }
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001803 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001804
Nate Begeman5e966612005-03-24 06:28:42 +00001805 case ISD::MUL:
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001806 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman439b4442005-04-05 04:22:58 +00001807 if (1 == getImmediateForOpcode(N.getOperand(1), opcode, Tmp2))
Nate Begeman307e7442005-03-26 01:28:53 +00001808 BuildMI(BB, PPC::MULLI, 2, Result).addReg(Tmp1).addSImm(Tmp2);
1809 else {
1810 Tmp2 = SelectExpr(N.getOperand(1));
1811 BuildMI(BB, PPC::MULLW, 2, Result).addReg(Tmp1).addReg(Tmp2);
1812 }
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001813 return Result;
1814
Nate Begeman815d6da2005-04-06 00:25:27 +00001815 case ISD::MULHS:
1816 case ISD::MULHU:
1817 Tmp1 = SelectExpr(N.getOperand(0));
1818 Tmp2 = SelectExpr(N.getOperand(1));
1819 Opc = (ISD::MULHU == opcode) ? PPC::MULHWU : PPC::MULHW;
1820 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1821 return Result;
1822
Nate Begemanf3d08f32005-03-29 00:03:27 +00001823 case ISD::SDIV:
1824 case ISD::UDIV:
Nate Begeman815d6da2005-04-06 00:25:27 +00001825 switch (getImmediateForOpcode(N.getOperand(1), opcode, Tmp3)) {
1826 default: break;
1827 // If this is an sdiv by a power of two, we can use an srawi/addze pair.
1828 case 3:
Nate Begeman80196b12005-04-05 00:15:08 +00001829 Tmp1 = MakeReg(MVT::i32);
1830 Tmp2 = SelectExpr(N.getOperand(0));
1831 BuildMI(BB, PPC::SRAWI, 2, Tmp1).addReg(Tmp2).addImm(Tmp3);
1832 BuildMI(BB, PPC::ADDZE, 1, Result).addReg(Tmp1);
1833 return Result;
Nate Begeman815d6da2005-04-06 00:25:27 +00001834 // If this is a divide by constant, we can emit code using some magic
1835 // constants to implement it as a multiply instead.
Nate Begeman27b4c232005-04-06 06:44:57 +00001836 case 4:
1837 ExprMap.erase(N);
1838 if (opcode == ISD::SDIV)
1839 return SelectExpr(BuildSDIVSequence(N));
1840 else
1841 return SelectExpr(BuildUDIVSequence(N));
Nate Begeman80196b12005-04-05 00:15:08 +00001842 }
Nate Begemanf3d08f32005-03-29 00:03:27 +00001843 Tmp1 = SelectExpr(N.getOperand(0));
1844 Tmp2 = SelectExpr(N.getOperand(1));
1845 Opc = (ISD::UDIV == opcode) ? PPC::DIVWU : PPC::DIVW;
1846 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1847 return Result;
1848
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001849 case ISD::ADD_PARTS:
Nate Begemanca12a2b2005-03-28 22:28:37 +00001850 case ISD::SUB_PARTS: {
1851 assert(N.getNumOperands() == 4 && N.getValueType() == MVT::i32 &&
1852 "Not an i64 add/sub!");
1853 // Emit all of the operands.
1854 std::vector<unsigned> InVals;
1855 for (unsigned i = 0, e = N.getNumOperands(); i != e; ++i)
1856 InVals.push_back(SelectExpr(N.getOperand(i)));
1857 if (N.getOpcode() == ISD::ADD_PARTS) {
Nate Begeman27eeb002005-04-02 05:59:34 +00001858 BuildMI(BB, PPC::ADDC, 2, Result).addReg(InVals[0]).addReg(InVals[2]);
1859 BuildMI(BB, PPC::ADDE, 2, Result+1).addReg(InVals[1]).addReg(InVals[3]);
Nate Begemanca12a2b2005-03-28 22:28:37 +00001860 } else {
Nate Begeman27eeb002005-04-02 05:59:34 +00001861 BuildMI(BB, PPC::SUBFC, 2, Result).addReg(InVals[2]).addReg(InVals[0]);
1862 BuildMI(BB, PPC::SUBFE, 2, Result+1).addReg(InVals[3]).addReg(InVals[1]);
1863 }
1864 return Result+N.ResNo;
1865 }
1866
1867 case ISD::SHL_PARTS:
1868 case ISD::SRA_PARTS:
1869 case ISD::SRL_PARTS: {
1870 assert(N.getNumOperands() == 3 && N.getValueType() == MVT::i32 &&
1871 "Not an i64 shift!");
1872 unsigned ShiftOpLo = SelectExpr(N.getOperand(0));
1873 unsigned ShiftOpHi = SelectExpr(N.getOperand(1));
1874 unsigned SHReg = SelectExpr(N.getOperand(2));
1875 Tmp1 = MakeReg(MVT::i32);
1876 Tmp2 = MakeReg(MVT::i32);
1877 Tmp3 = MakeReg(MVT::i32);
1878 unsigned Tmp4 = MakeReg(MVT::i32);
1879 unsigned Tmp5 = MakeReg(MVT::i32);
1880 unsigned Tmp6 = MakeReg(MVT::i32);
1881 BuildMI(BB, PPC::SUBFIC, 2, Tmp1).addReg(SHReg).addSImm(32);
1882 if (ISD::SHL_PARTS == opcode) {
1883 BuildMI(BB, PPC::SLW, 2, Tmp2).addReg(ShiftOpHi).addReg(SHReg);
1884 BuildMI(BB, PPC::SRW, 2, Tmp3).addReg(ShiftOpLo).addReg(Tmp1);
1885 BuildMI(BB, PPC::OR, 2, Tmp4).addReg(Tmp2).addReg(Tmp3);
1886 BuildMI(BB, PPC::ADDI, 2, Tmp5).addReg(SHReg).addSImm(-32);
Nate Begemanfa554702005-04-03 22:13:27 +00001887 BuildMI(BB, PPC::SLW, 2, Tmp6).addReg(ShiftOpLo).addReg(Tmp5);
Nate Begeman27eeb002005-04-02 05:59:34 +00001888 BuildMI(BB, PPC::OR, 2, Result+1).addReg(Tmp4).addReg(Tmp6);
1889 BuildMI(BB, PPC::SLW, 2, Result).addReg(ShiftOpLo).addReg(SHReg);
1890 } else if (ISD::SRL_PARTS == opcode) {
1891 BuildMI(BB, PPC::SRW, 2, Tmp2).addReg(ShiftOpLo).addReg(SHReg);
1892 BuildMI(BB, PPC::SLW, 2, Tmp3).addReg(ShiftOpHi).addReg(Tmp1);
1893 BuildMI(BB, PPC::OR, 2, Tmp4).addReg(Tmp2).addReg(Tmp3);
1894 BuildMI(BB, PPC::ADDI, 2, Tmp5).addReg(SHReg).addSImm(-32);
1895 BuildMI(BB, PPC::SRW, 2, Tmp6).addReg(ShiftOpHi).addReg(Tmp5);
1896 BuildMI(BB, PPC::OR, 2, Result).addReg(Tmp4).addReg(Tmp6);
1897 BuildMI(BB, PPC::SRW, 2, Result+1).addReg(ShiftOpHi).addReg(SHReg);
1898 } else {
1899 MachineBasicBlock *TmpMBB = new MachineBasicBlock(BB->getBasicBlock());
1900 MachineBasicBlock *PhiMBB = new MachineBasicBlock(BB->getBasicBlock());
1901 MachineBasicBlock *OldMBB = BB;
1902 MachineFunction *F = BB->getParent();
1903 ilist<MachineBasicBlock>::iterator It = BB; ++It;
1904 F->getBasicBlockList().insert(It, TmpMBB);
1905 F->getBasicBlockList().insert(It, PhiMBB);
1906 BB->addSuccessor(TmpMBB);
1907 BB->addSuccessor(PhiMBB);
1908 BuildMI(BB, PPC::SRW, 2, Tmp2).addReg(ShiftOpLo).addReg(SHReg);
1909 BuildMI(BB, PPC::SLW, 2, Tmp3).addReg(ShiftOpHi).addReg(Tmp1);
1910 BuildMI(BB, PPC::OR, 2, Tmp4).addReg(Tmp2).addReg(Tmp3);
1911 BuildMI(BB, PPC::ADDICo, 2, Tmp5).addReg(SHReg).addSImm(-32);
1912 BuildMI(BB, PPC::SRAW, 2, Tmp6).addReg(ShiftOpHi).addReg(Tmp5);
1913 BuildMI(BB, PPC::SRAW, 2, Result+1).addReg(ShiftOpHi).addReg(SHReg);
1914 BuildMI(BB, PPC::BLE, 2).addReg(PPC::CR0).addMBB(PhiMBB);
1915 // Select correct least significant half if the shift amount > 32
1916 BB = TmpMBB;
1917 unsigned Tmp7 = MakeReg(MVT::i32);
1918 BuildMI(BB, PPC::OR, 2, Tmp7).addReg(Tmp6).addReg(Tmp6);
1919 TmpMBB->addSuccessor(PhiMBB);
1920 BB = PhiMBB;
1921 BuildMI(BB, PPC::PHI, 4, Result).addReg(Tmp4).addMBB(OldMBB)
1922 .addReg(Tmp7).addMBB(TmpMBB);
Nate Begemanca12a2b2005-03-28 22:28:37 +00001923 }
1924 return Result+N.ResNo;
1925 }
1926
Nate Begemana9795f82005-03-24 04:41:43 +00001927 case ISD::FP_TO_UINT:
Nate Begeman6b559972005-04-01 02:59:27 +00001928 case ISD::FP_TO_SINT: {
1929 bool U = (ISD::FP_TO_UINT == opcode);
1930 Tmp1 = SelectExpr(N.getOperand(0));
1931 if (!U) {
1932 Tmp2 = MakeReg(MVT::f64);
1933 BuildMI(BB, PPC::FCTIWZ, 1, Tmp2).addReg(Tmp1);
1934 int FrameIdx = BB->getParent()->getFrameInfo()->CreateStackObject(8, 8);
1935 addFrameReference(BuildMI(BB, PPC::STFD, 3).addReg(Tmp2), FrameIdx);
1936 addFrameReference(BuildMI(BB, PPC::LWZ, 2, Result), FrameIdx, 4);
1937 return Result;
1938 } else {
1939 unsigned Zero = getConstDouble(0.0);
1940 unsigned MaxInt = getConstDouble((1LL << 32) - 1);
1941 unsigned Border = getConstDouble(1LL << 31);
1942 unsigned UseZero = MakeReg(MVT::f64);
1943 unsigned UseMaxInt = MakeReg(MVT::f64);
1944 unsigned UseChoice = MakeReg(MVT::f64);
1945 unsigned TmpReg = MakeReg(MVT::f64);
1946 unsigned TmpReg2 = MakeReg(MVT::f64);
1947 unsigned ConvReg = MakeReg(MVT::f64);
1948 unsigned IntTmp = MakeReg(MVT::i32);
1949 unsigned XorReg = MakeReg(MVT::i32);
1950 MachineFunction *F = BB->getParent();
1951 int FrameIdx = F->getFrameInfo()->CreateStackObject(8, 8);
1952 // Update machine-CFG edges
1953 MachineBasicBlock *XorMBB = new MachineBasicBlock(BB->getBasicBlock());
1954 MachineBasicBlock *PhiMBB = new MachineBasicBlock(BB->getBasicBlock());
1955 MachineBasicBlock *OldMBB = BB;
1956 ilist<MachineBasicBlock>::iterator It = BB; ++It;
1957 F->getBasicBlockList().insert(It, XorMBB);
1958 F->getBasicBlockList().insert(It, PhiMBB);
1959 BB->addSuccessor(XorMBB);
1960 BB->addSuccessor(PhiMBB);
1961 // Convert from floating point to unsigned 32-bit value
1962 // Use 0 if incoming value is < 0.0
1963 BuildMI(BB, PPC::FSEL, 3, UseZero).addReg(Tmp1).addReg(Tmp1).addReg(Zero);
1964 // Use 2**32 - 1 if incoming value is >= 2**32
1965 BuildMI(BB, PPC::FSUB, 2, UseMaxInt).addReg(MaxInt).addReg(Tmp1);
1966 BuildMI(BB, PPC::FSEL, 3, UseChoice).addReg(UseMaxInt).addReg(UseZero)
1967 .addReg(MaxInt);
1968 // Subtract 2**31
1969 BuildMI(BB, PPC::FSUB, 2, TmpReg).addReg(UseChoice).addReg(Border);
1970 // Use difference if >= 2**31
1971 BuildMI(BB, PPC::FCMPU, 2, PPC::CR0).addReg(UseChoice).addReg(Border);
1972 BuildMI(BB, PPC::FSEL, 3, TmpReg2).addReg(TmpReg).addReg(TmpReg)
1973 .addReg(UseChoice);
1974 // Convert to integer
1975 BuildMI(BB, PPC::FCTIWZ, 1, ConvReg).addReg(TmpReg2);
1976 addFrameReference(BuildMI(BB, PPC::STFD, 3).addReg(ConvReg), FrameIdx);
1977 addFrameReference(BuildMI(BB, PPC::LWZ, 2, IntTmp), FrameIdx, 4);
1978 BuildMI(BB, PPC::BLT, 2).addReg(PPC::CR0).addMBB(PhiMBB);
1979 BuildMI(BB, PPC::B, 1).addMBB(XorMBB);
1980
1981 // XorMBB:
1982 // add 2**31 if input was >= 2**31
1983 BB = XorMBB;
1984 BuildMI(BB, PPC::XORIS, 2, XorReg).addReg(IntTmp).addImm(0x8000);
1985 XorMBB->addSuccessor(PhiMBB);
1986
1987 // PhiMBB:
1988 // DestReg = phi [ IntTmp, OldMBB ], [ XorReg, XorMBB ]
1989 BB = PhiMBB;
1990 BuildMI(BB, PPC::PHI, 4, Result).addReg(IntTmp).addMBB(OldMBB)
1991 .addReg(XorReg).addMBB(XorMBB);
1992 return Result;
1993 }
1994 assert(0 && "Should never get here");
1995 return 0;
1996 }
Nate Begemana9795f82005-03-24 04:41:43 +00001997
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001998 case ISD::SETCC:
Nate Begeman33162522005-03-29 21:54:38 +00001999 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Node)) {
Nate Begeman7e7fadd2005-04-07 20:30:01 +00002000 // We can codegen setcc op, 0 very efficiently compared to a conditional
2001 // branch. Check for that here.
2002 if (ConstantSDNode *CN =
2003 dyn_cast<ConstantSDNode>(SetCC->getOperand(1).Val)) {
2004 if (CN->getValue() == 0) {
2005 Tmp1 = SelectExpr(SetCC->getOperand(0));
2006 switch (SetCC->getCondition()) {
2007 default: assert(0 && "Unhandled SetCC condition"); abort();
2008 case ISD::SETEQ:
2009 case ISD::SETULE:
2010 Tmp2 = MakeReg(MVT::i32);
2011 BuildMI(BB, PPC::CNTLZW, 1, Tmp2).addReg(Tmp1);
2012 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp2).addImm(27)
2013 .addImm(5).addImm(31);
2014 break;
2015 case ISD::SETNE:
2016 case ISD::SETUGT:
2017 Tmp2 = MakeReg(MVT::i32);
2018 BuildMI(BB, PPC::ADDIC, 2, Tmp2).addReg(Tmp1).addSImm(-1);
2019 BuildMI(BB, PPC::SUBFE, 2, Result).addReg(Tmp2).addReg(Tmp1);
2020 break;
2021 case ISD::SETULT:
2022 BuildMI(BB, PPC::LI, 1, Result).addSImm(0);
2023 break;
2024 case ISD::SETLT:
2025 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(1)
2026 .addImm(31).addImm(31);
2027 break;
2028 case ISD::SETLE:
2029 Tmp2 = MakeReg(MVT::i32);
2030 Tmp3 = MakeReg(MVT::i32);
2031 BuildMI(BB, PPC::NEG, 2, Tmp2).addReg(Tmp1);
2032 BuildMI(BB, PPC::ORC, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
2033 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp3).addImm(1)
2034 .addImm(31).addImm(31);
2035 break;
2036 case ISD::SETGT:
2037 Tmp2 = MakeReg(MVT::i32);
2038 Tmp3 = MakeReg(MVT::i32);
2039 BuildMI(BB, PPC::NEG, 2, Tmp2).addReg(Tmp1);
2040 BuildMI(BB, PPC::ANDC, 2, Tmp3).addReg(Tmp2).addReg(Tmp1);
2041 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp3).addImm(1)
2042 .addImm(31).addImm(31);
2043 break;
2044 case ISD::SETUGE:
2045 BuildMI(BB, PPC::LI, 1, Result).addSImm(1);
2046 break;
2047 case ISD::SETGE:
2048 BuildMI(BB, PPC::RLWINM, 4, Tmp2).addReg(Tmp1).addImm(1)
2049 .addImm(31).addImm(31);
2050 BuildMI(BB, PPC::XORI, 2, Result).addReg(Tmp2).addImm(1);
2051 break;
2052 }
2053 return Result;
2054 }
2055 }
2056
Nate Begemandffcfcc2005-04-01 00:32:34 +00002057 Opc = SelectSetCR0(N);
Nate Begeman31318e42005-04-01 07:21:30 +00002058 unsigned TrueValue = MakeReg(MVT::i32);
2059 BuildMI(BB, PPC::LI, 1, TrueValue).addSImm(1);
2060 unsigned FalseValue = MakeReg(MVT::i32);
2061 BuildMI(BB, PPC::LI, 1, FalseValue).addSImm(0);
2062
Nate Begeman33162522005-03-29 21:54:38 +00002063 // Create an iterator with which to insert the MBB for copying the false
2064 // value and the MBB to hold the PHI instruction for this SetCC.
2065 MachineBasicBlock *thisMBB = BB;
2066 const BasicBlock *LLVM_BB = BB->getBasicBlock();
2067 ilist<MachineBasicBlock>::iterator It = BB;
2068 ++It;
2069
2070 // thisMBB:
2071 // ...
2072 // cmpTY cr0, r1, r2
2073 // %TrueValue = li 1
2074 // bCC sinkMBB
Nate Begeman33162522005-03-29 21:54:38 +00002075 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
2076 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
2077 BuildMI(BB, Opc, 2).addReg(PPC::CR0).addMBB(sinkMBB);
2078 MachineFunction *F = BB->getParent();
2079 F->getBasicBlockList().insert(It, copy0MBB);
2080 F->getBasicBlockList().insert(It, sinkMBB);
2081 // Update machine-CFG edges
2082 BB->addSuccessor(copy0MBB);
2083 BB->addSuccessor(sinkMBB);
2084
2085 // copy0MBB:
2086 // %FalseValue = li 0
2087 // fallthrough
2088 BB = copy0MBB;
Nate Begeman33162522005-03-29 21:54:38 +00002089 // Update machine-CFG edges
2090 BB->addSuccessor(sinkMBB);
2091
2092 // sinkMBB:
2093 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
2094 // ...
2095 BB = sinkMBB;
2096 BuildMI(BB, PPC::PHI, 4, Result).addReg(FalseValue)
2097 .addMBB(copy0MBB).addReg(TrueValue).addMBB(thisMBB);
2098 return Result;
2099 }
2100 assert(0 && "Is this legal?");
2101 return 0;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00002102
Nate Begeman74747862005-03-29 22:24:51 +00002103 case ISD::SELECT: {
Nate Begeman7e7fadd2005-04-07 20:30:01 +00002104 // We can codegen select (a < 0) ? b : 0 very efficiently compared to a
2105 // conditional branch. Check for that here.
2106 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(N.getOperand(0).Val)) {
2107 if (ConstantSDNode *CN =
2108 dyn_cast<ConstantSDNode>(SetCC->getOperand(1).Val)) {
2109 if (ConstantSDNode *CNF =
2110 dyn_cast<ConstantSDNode>(N.getOperand(2).Val)) {
2111 if (CN->getValue() == 0 && CNF->getValue() == 0 &&
2112 SetCC->getCondition() == ISD::SETLT) {
2113 Tmp1 = SelectExpr(N.getOperand(1)); // TRUE value
2114 Tmp2 = SelectExpr(SetCC->getOperand(0));
2115 Tmp3 = MakeReg(MVT::i32);
2116 BuildMI(BB, PPC::SRAWI, 2, Tmp3).addReg(Tmp2).addImm(31);
2117 BuildMI(BB, PPC::AND, 2, Result).addReg(Tmp1).addReg(Tmp3);
2118 return Result;
2119 }
2120 }
2121 }
2122 }
Chris Lattner30710192005-04-01 07:10:02 +00002123 unsigned TrueValue = SelectExpr(N.getOperand(1)); //Use if TRUE
2124 unsigned FalseValue = SelectExpr(N.getOperand(2)); //Use if FALSE
Nate Begeman6cb2e1b2005-04-01 08:57:43 +00002125 Opc = SelectSetCR0(N.getOperand(0));
Chris Lattner30710192005-04-01 07:10:02 +00002126
Nate Begeman74747862005-03-29 22:24:51 +00002127 // Create an iterator with which to insert the MBB for copying the false
2128 // value and the MBB to hold the PHI instruction for this SetCC.
2129 MachineBasicBlock *thisMBB = BB;
2130 const BasicBlock *LLVM_BB = BB->getBasicBlock();
2131 ilist<MachineBasicBlock>::iterator It = BB;
2132 ++It;
2133
2134 // thisMBB:
2135 // ...
2136 // TrueVal = ...
2137 // cmpTY cr0, r1, r2
2138 // bCC copy1MBB
2139 // fallthrough --> copy0MBB
Nate Begeman74747862005-03-29 22:24:51 +00002140 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
2141 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
Nate Begeman3e897162005-03-31 23:55:40 +00002142 BuildMI(BB, Opc, 2).addReg(PPC::CR0).addMBB(sinkMBB);
Nate Begeman74747862005-03-29 22:24:51 +00002143 MachineFunction *F = BB->getParent();
2144 F->getBasicBlockList().insert(It, copy0MBB);
2145 F->getBasicBlockList().insert(It, sinkMBB);
2146 // Update machine-CFG edges
2147 BB->addSuccessor(copy0MBB);
2148 BB->addSuccessor(sinkMBB);
2149
2150 // copy0MBB:
2151 // %FalseValue = ...
2152 // # fallthrough to sinkMBB
2153 BB = copy0MBB;
Nate Begeman74747862005-03-29 22:24:51 +00002154 // Update machine-CFG edges
2155 BB->addSuccessor(sinkMBB);
2156
2157 // sinkMBB:
2158 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
2159 // ...
2160 BB = sinkMBB;
2161 BuildMI(BB, PPC::PHI, 4, Result).addReg(FalseValue)
2162 .addMBB(copy0MBB).addReg(TrueValue).addMBB(thisMBB);
Nate Begeman74747862005-03-29 22:24:51 +00002163 return Result;
2164 }
Nate Begemana9795f82005-03-24 04:41:43 +00002165
2166 case ISD::Constant:
2167 switch (N.getValueType()) {
2168 default: assert(0 && "Cannot use constants of this type!");
2169 case MVT::i1:
2170 BuildMI(BB, PPC::LI, 1, Result)
2171 .addSImm(!cast<ConstantSDNode>(N)->isNullValue());
2172 break;
2173 case MVT::i32:
2174 {
2175 int v = (int)cast<ConstantSDNode>(N)->getSignExtended();
2176 if (v < 32768 && v >= -32768) {
2177 BuildMI(BB, PPC::LI, 1, Result).addSImm(v);
2178 } else {
Nate Begeman5e966612005-03-24 06:28:42 +00002179 Tmp1 = MakeReg(MVT::i32);
2180 BuildMI(BB, PPC::LIS, 1, Tmp1).addSImm(v >> 16);
2181 BuildMI(BB, PPC::ORI, 2, Result).addReg(Tmp1).addImm(v & 0xFFFF);
Nate Begemana9795f82005-03-24 04:41:43 +00002182 }
2183 }
2184 }
2185 return Result;
2186 }
2187
2188 return 0;
2189}
2190
2191void ISel::Select(SDOperand N) {
2192 unsigned Tmp1, Tmp2, Opc;
2193 unsigned opcode = N.getOpcode();
2194
2195 if (!ExprMap.insert(std::make_pair(N, 1)).second)
2196 return; // Already selected.
2197
2198 SDNode *Node = N.Val;
2199
2200 switch (Node->getOpcode()) {
2201 default:
2202 Node->dump(); std::cerr << "\n";
2203 assert(0 && "Node not handled yet!");
2204 case ISD::EntryToken: return; // Noop
2205 case ISD::TokenFactor:
2206 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
2207 Select(Node->getOperand(i));
2208 return;
2209 case ISD::ADJCALLSTACKDOWN:
2210 case ISD::ADJCALLSTACKUP:
2211 Select(N.getOperand(0));
2212 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
2213 Opc = N.getOpcode() == ISD::ADJCALLSTACKDOWN ? PPC::ADJCALLSTACKDOWN :
2214 PPC::ADJCALLSTACKUP;
2215 BuildMI(BB, Opc, 1).addImm(Tmp1);
2216 return;
2217 case ISD::BR: {
2218 MachineBasicBlock *Dest =
2219 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
Nate Begemana9795f82005-03-24 04:41:43 +00002220 Select(N.getOperand(0));
2221 BuildMI(BB, PPC::B, 1).addMBB(Dest);
2222 return;
2223 }
2224 case ISD::BRCOND:
Nate Begemancd08e4c2005-04-09 20:09:12 +00002225 case ISD::BRCONDTWOWAY:
Nate Begemana9795f82005-03-24 04:41:43 +00002226 SelectBranchCC(N);
2227 return;
2228 case ISD::CopyToReg:
2229 Select(N.getOperand(0));
2230 Tmp1 = SelectExpr(N.getOperand(1));
2231 Tmp2 = cast<RegSDNode>(N)->getReg();
2232
2233 if (Tmp1 != Tmp2) {
2234 if (N.getOperand(1).getValueType() == MVT::f64 ||
2235 N.getOperand(1).getValueType() == MVT::f32)
2236 BuildMI(BB, PPC::FMR, 1, Tmp2).addReg(Tmp1);
2237 else
2238 BuildMI(BB, PPC::OR, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
2239 }
2240 return;
2241 case ISD::ImplicitDef:
2242 Select(N.getOperand(0));
2243 BuildMI(BB, PPC::IMPLICIT_DEF, 0, cast<RegSDNode>(N)->getReg());
2244 return;
2245 case ISD::RET:
2246 switch (N.getNumOperands()) {
2247 default:
2248 assert(0 && "Unknown return instruction!");
2249 case 3:
2250 assert(N.getOperand(1).getValueType() == MVT::i32 &&
2251 N.getOperand(2).getValueType() == MVT::i32 &&
2252 "Unknown two-register value!");
2253 Select(N.getOperand(0));
2254 Tmp1 = SelectExpr(N.getOperand(1));
2255 Tmp2 = SelectExpr(N.getOperand(2));
Nate Begeman27523a12005-04-02 00:42:16 +00002256 BuildMI(BB, PPC::OR, 2, PPC::R3).addReg(Tmp2).addReg(Tmp2);
2257 BuildMI(BB, PPC::OR, 2, PPC::R4).addReg(Tmp1).addReg(Tmp1);
Nate Begemana9795f82005-03-24 04:41:43 +00002258 break;
2259 case 2:
2260 Select(N.getOperand(0));
2261 Tmp1 = SelectExpr(N.getOperand(1));
2262 switch (N.getOperand(1).getValueType()) {
2263 default:
2264 assert(0 && "Unknown return type!");
2265 case MVT::f64:
2266 case MVT::f32:
2267 BuildMI(BB, PPC::FMR, 1, PPC::F1).addReg(Tmp1);
2268 break;
2269 case MVT::i32:
2270 BuildMI(BB, PPC::OR, 2, PPC::R3).addReg(Tmp1).addReg(Tmp1);
2271 break;
2272 }
Nate Begeman9e3e1b52005-03-24 23:35:30 +00002273 case 1:
2274 Select(N.getOperand(0));
2275 break;
Nate Begemana9795f82005-03-24 04:41:43 +00002276 }
2277 BuildMI(BB, PPC::BLR, 0); // Just emit a 'ret' instruction
2278 return;
Nate Begemana9795f82005-03-24 04:41:43 +00002279 case ISD::TRUNCSTORE:
2280 case ISD::STORE:
2281 {
2282 SDOperand Chain = N.getOperand(0);
2283 SDOperand Value = N.getOperand(1);
2284 SDOperand Address = N.getOperand(2);
2285 Select(Chain);
2286
2287 Tmp1 = SelectExpr(Value); //value
2288
2289 if (opcode == ISD::STORE) {
2290 switch(Value.getValueType()) {
2291 default: assert(0 && "unknown Type in store");
2292 case MVT::i32: Opc = PPC::STW; break;
2293 case MVT::f64: Opc = PPC::STFD; break;
2294 case MVT::f32: Opc = PPC::STFS; break;
2295 }
2296 } else { //ISD::TRUNCSTORE
2297 switch(cast<MVTSDNode>(Node)->getExtraValueType()) {
2298 default: assert(0 && "unknown Type in store");
Nate Begeman7e7fadd2005-04-07 20:30:01 +00002299 case MVT::i1:
Nate Begemana9795f82005-03-24 04:41:43 +00002300 case MVT::i8: Opc = PPC::STB; break;
2301 case MVT::i16: Opc = PPC::STH; break;
2302 }
2303 }
2304
Nate Begemana7e11a42005-04-01 05:57:17 +00002305 if(Address.getOpcode() == ISD::FrameIndex)
Nate Begemana9795f82005-03-24 04:41:43 +00002306 {
Nate Begeman58f718c2005-03-30 02:23:08 +00002307 Tmp2 = cast<FrameIndexSDNode>(Address)->getIndex();
2308 addFrameReference(BuildMI(BB, Opc, 3).addReg(Tmp1), (int)Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +00002309 }
2310 else
2311 {
2312 int offset;
Nate Begeman04730362005-04-01 04:45:11 +00002313 bool idx = SelectAddr(Address, Tmp2, offset);
2314 if (idx) {
2315 Opc = IndexedOpForOp(Opc);
2316 BuildMI(BB, Opc, 3).addReg(Tmp1).addReg(Tmp2).addReg(offset);
2317 } else {
2318 BuildMI(BB, Opc, 3).addReg(Tmp1).addImm(offset).addReg(Tmp2);
2319 }
Nate Begemana9795f82005-03-24 04:41:43 +00002320 }
2321 return;
2322 }
2323 case ISD::EXTLOAD:
2324 case ISD::SEXTLOAD:
2325 case ISD::ZEXTLOAD:
2326 case ISD::LOAD:
2327 case ISD::CopyFromReg:
2328 case ISD::CALL:
2329 case ISD::DYNAMIC_STACKALLOC:
2330 ExprMap.erase(N);
2331 SelectExpr(N);
2332 return;
2333 }
2334 assert(0 && "Should not be reached!");
2335}
2336
2337
2338/// createPPC32PatternInstructionSelector - This pass converts an LLVM function
2339/// into a machine code representation using pattern matching and a machine
2340/// description file.
2341///
2342FunctionPass *llvm::createPPC32ISelPattern(TargetMachine &TM) {
2343 return new ISel(TM);
Chris Lattner246fa632005-03-24 06:16:18 +00002344}
2345