blob: b8529ba8cca4a56482c189bbe65336585c7fa38b [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 Begemancd08e4c2005-04-09 20:09:12 +0000476Statistic<>Rotates("ppc-codegen", "Number of rotates 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 Begemana9795f82005-03-24 04:41:43 +0000495
496public:
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 Begemana9795f82005-03-24 04:41:43 +0000529 unsigned SelectExpr(SDOperand N);
530 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
651/// getBCCForSetCC - Returns the PowerPC condition branch mnemonic corresponding
652/// to Condition. If the Condition is unordered or unsigned, the bool argument
653/// U is set to true, otherwise it is set to false.
654static unsigned getBCCForSetCC(unsigned Condition, bool& U) {
655 U = false;
656 switch (Condition) {
657 default: assert(0 && "Unknown condition!"); abort();
658 case ISD::SETEQ: return PPC::BEQ;
659 case ISD::SETNE: return PPC::BNE;
660 case ISD::SETULT: U = true;
661 case ISD::SETLT: return PPC::BLT;
662 case ISD::SETULE: U = true;
663 case ISD::SETLE: return PPC::BLE;
664 case ISD::SETUGT: U = true;
665 case ISD::SETGT: return PPC::BGT;
666 case ISD::SETUGE: U = true;
667 case ISD::SETGE: return PPC::BGE;
668 }
Nate Begeman04730362005-04-01 04:45:11 +0000669 return 0;
670}
671
672/// IndexedOpForOp - Return the indexed variant for each of the PowerPC load
673/// and store immediate instructions.
674static unsigned IndexedOpForOp(unsigned Opcode) {
675 switch(Opcode) {
676 default: assert(0 && "Unknown opcode!"); abort();
677 case PPC::LBZ: return PPC::LBZX; case PPC::STB: return PPC::STBX;
678 case PPC::LHZ: return PPC::LHZX; case PPC::STH: return PPC::STHX;
679 case PPC::LHA: return PPC::LHAX; case PPC::STW: return PPC::STWX;
680 case PPC::LWZ: return PPC::LWZX; case PPC::STFS: return PPC::STFSX;
681 case PPC::LFS: return PPC::LFSX; case PPC::STFD: return PPC::STFDX;
682 case PPC::LFD: return PPC::LFDX;
683 }
684 return 0;
Nate Begeman3e897162005-03-31 23:55:40 +0000685}
Nate Begeman815d6da2005-04-06 00:25:27 +0000686
687// Structure used to return the necessary information to codegen an SDIV as
688// a multiply.
689struct ms {
690 int m; // magic number
691 int s; // shift amount
692};
693
694struct mu {
695 unsigned int m; // magic number
696 int a; // add indicator
697 int s; // shift amount
698};
699
700/// magic - calculate the magic numbers required to codegen an integer sdiv as
701/// a sequence of multiply and shifts. Requires that the divisor not be 0, 1,
702/// or -1.
703static struct ms magic(int d) {
704 int p;
705 unsigned int ad, anc, delta, q1, r1, q2, r2, t;
706 const unsigned int two31 = 2147483648U; // 2^31
707 struct ms mag;
708
709 ad = abs(d);
710 t = two31 + ((unsigned int)d >> 31);
711 anc = t - 1 - t%ad; // absolute value of nc
712 p = 31; // initialize p
713 q1 = two31/anc; // initialize q1 = 2p/abs(nc)
714 r1 = two31 - q1*anc; // initialize r1 = rem(2p,abs(nc))
715 q2 = two31/ad; // initialize q2 = 2p/abs(d)
716 r2 = two31 - q2*ad; // initialize r2 = rem(2p,abs(d))
717 do {
718 p = p + 1;
719 q1 = 2*q1; // update q1 = 2p/abs(nc)
720 r1 = 2*r1; // update r1 = rem(2p/abs(nc))
721 if (r1 >= anc) { // must be unsigned comparison
722 q1 = q1 + 1;
723 r1 = r1 - anc;
724 }
725 q2 = 2*q2; // update q2 = 2p/abs(d)
726 r2 = 2*r2; // update r2 = rem(2p/abs(d))
727 if (r2 >= ad) { // must be unsigned comparison
728 q2 = q2 + 1;
729 r2 = r2 - ad;
730 }
731 delta = ad - r2;
732 } while (q1 < delta || (q1 == delta && r1 == 0));
733
734 mag.m = q2 + 1;
735 if (d < 0) mag.m = -mag.m; // resulting magic number
736 mag.s = p - 32; // resulting shift
737 return mag;
738}
739
740/// magicu - calculate the magic numbers required to codegen an integer udiv as
741/// a sequence of multiply, add and shifts. Requires that the divisor not be 0.
742static struct mu magicu(unsigned d)
743{
744 int p;
745 unsigned int nc, delta, q1, r1, q2, r2;
746 struct mu magu;
747 magu.a = 0; // initialize "add" indicator
748 nc = - 1 - (-d)%d;
749 p = 31; // initialize p
750 q1 = 0x80000000/nc; // initialize q1 = 2p/nc
751 r1 = 0x80000000 - q1*nc; // initialize r1 = rem(2p,nc)
752 q2 = 0x7FFFFFFF/d; // initialize q2 = (2p-1)/d
753 r2 = 0x7FFFFFFF - q2*d; // initialize r2 = rem((2p-1),d)
754 do {
755 p = p + 1;
756 if (r1 >= nc - r1 ) {
757 q1 = 2*q1 + 1; // update q1
758 r1 = 2*r1 - nc; // update r1
759 }
760 else {
761 q1 = 2*q1; // update q1
762 r1 = 2*r1; // update r1
763 }
764 if (r2 + 1 >= d - r2) {
765 if (q2 >= 0x7FFFFFFF) magu.a = 1;
766 q2 = 2*q2 + 1; // update q2
767 r2 = 2*r2 + 1 - d; // update r2
768 }
769 else {
770 if (q2 >= 0x80000000) magu.a = 1;
771 q2 = 2*q2; // update q2
772 r2 = 2*r2 + 1; // update r2
773 }
774 delta = d - 1 - r2;
775 } while (p < 64 && (q1 < delta || (q1 == delta && r1 == 0)));
776 magu.m = q2 + 1; // resulting magic number
777 magu.s = p - 32; // resulting shift
778 return magu;
779}
780}
781
782/// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
783/// return a DAG expression to select that will generate the same value by
784/// multiplying by a magic number. See:
785/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
786SDOperand ISel::BuildSDIVSequence(SDOperand N) {
787 int d = (int)cast<ConstantSDNode>(N.getOperand(1))->getSignExtended();
788 ms magics = magic(d);
789 // Multiply the numerator (operand 0) by the magic value
790 SDOperand Q = ISelDAG->getNode(ISD::MULHS, MVT::i32, N.getOperand(0),
791 ISelDAG->getConstant(magics.m, MVT::i32));
792 // If d > 0 and m < 0, add the numerator
793 if (d > 0 && magics.m < 0)
794 Q = ISelDAG->getNode(ISD::ADD, MVT::i32, Q, N.getOperand(0));
795 // If d < 0 and m > 0, subtract the numerator.
796 if (d < 0 && magics.m > 0)
797 Q = ISelDAG->getNode(ISD::SUB, MVT::i32, Q, N.getOperand(0));
798 // Shift right algebraic if shift value is nonzero
799 if (magics.s > 0)
800 Q = ISelDAG->getNode(ISD::SRA, MVT::i32, Q,
801 ISelDAG->getConstant(magics.s, MVT::i32));
802 // Extract the sign bit and add it to the quotient
803 SDOperand T =
804 ISelDAG->getNode(ISD::SRL, MVT::i32, Q, ISelDAG->getConstant(31, MVT::i32));
Nate Begeman27b4c232005-04-06 06:44:57 +0000805 return ISelDAG->getNode(ISD::ADD, MVT::i32, Q, T);
Nate Begeman815d6da2005-04-06 00:25:27 +0000806}
807
808/// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
809/// return a DAG expression to select that will generate the same value by
810/// multiplying by a magic number. See:
811/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
812SDOperand ISel::BuildUDIVSequence(SDOperand N) {
813 unsigned d =
814 (unsigned)cast<ConstantSDNode>(N.getOperand(1))->getSignExtended();
815 mu magics = magicu(d);
816 // Multiply the numerator (operand 0) by the magic value
817 SDOperand Q = ISelDAG->getNode(ISD::MULHU, MVT::i32, N.getOperand(0),
818 ISelDAG->getConstant(magics.m, MVT::i32));
819 if (magics.a == 0) {
820 Q = ISelDAG->getNode(ISD::SRL, MVT::i32, Q,
821 ISelDAG->getConstant(magics.s, MVT::i32));
822 } else {
823 SDOperand NPQ = ISelDAG->getNode(ISD::SUB, MVT::i32, N.getOperand(0), Q);
824 NPQ = ISelDAG->getNode(ISD::SRL, MVT::i32, NPQ,
825 ISelDAG->getConstant(1, MVT::i32));
826 NPQ = ISelDAG->getNode(ISD::ADD, MVT::i32, NPQ, Q);
827 Q = ISelDAG->getNode(ISD::SRL, MVT::i32, NPQ,
828 ISelDAG->getConstant(magics.s-1, MVT::i32));
829 }
Nate Begeman27b4c232005-04-06 06:44:57 +0000830 return Q;
Nate Begemana9795f82005-03-24 04:41:43 +0000831}
832
Nate Begemanc7b09f12005-03-25 08:34:25 +0000833/// getGlobalBaseReg - Output the instructions required to put the
834/// base address to use for accessing globals into a register.
835///
836unsigned ISel::getGlobalBaseReg() {
837 if (!GlobalBaseInitialized) {
838 // Insert the set of GlobalBaseReg into the first MBB of the function
839 MachineBasicBlock &FirstMBB = BB->getParent()->front();
840 MachineBasicBlock::iterator MBBI = FirstMBB.begin();
841 GlobalBaseReg = MakeReg(MVT::i32);
842 BuildMI(FirstMBB, MBBI, PPC::MovePCtoLR, 0, PPC::LR);
843 BuildMI(FirstMBB, MBBI, PPC::MFLR, 1, GlobalBaseReg).addReg(PPC::LR);
844 GlobalBaseInitialized = true;
845 }
846 return GlobalBaseReg;
847}
848
Nate Begeman6b559972005-04-01 02:59:27 +0000849/// getConstDouble - Loads a floating point value into a register, via the
850/// Constant Pool. Optionally takes a register in which to load the value.
851unsigned ISel::getConstDouble(double doubleVal, unsigned Result=0) {
852 unsigned Tmp1 = MakeReg(MVT::i32);
853 if (0 == Result) Result = MakeReg(MVT::f64);
854 MachineConstantPool *CP = BB->getParent()->getConstantPool();
855 ConstantFP *CFP = ConstantFP::get(Type::DoubleTy, doubleVal);
856 unsigned CPI = CP->getConstantPoolIndex(CFP);
857 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp1).addReg(getGlobalBaseReg())
858 .addConstantPoolIndex(CPI);
859 BuildMI(BB, PPC::LFD, 2, Result).addConstantPoolIndex(CPI).addReg(Tmp1);
860 return Result;
861}
862
Nate Begeman7ddecb42005-04-06 23:51:40 +0000863/// SelectBitfieldInsert - turn an or of two masked values into
864/// the rotate left word immediate then mask insert (rlwimi) instruction.
865/// Returns true on success, false if the caller still needs to select OR.
866///
867/// Patterns matched:
868/// 1. or shl, and 5. or and, and
869/// 2. or and, shl 6. or shl, shr
870/// 3. or shr, and 7. or shr, shl
871/// 4. or and, shr
872bool ISel::SelectBitfieldInsert(SDOperand OR, unsigned Result) {
Nate Begemancd08e4c2005-04-09 20:09:12 +0000873 bool IsRotate = false;
Nate Begeman7ddecb42005-04-06 23:51:40 +0000874 unsigned TgtMask = 0xFFFFFFFF, InsMask = 0xFFFFFFFF, Amount = 0;
875 unsigned Op0Opc = OR.getOperand(0).getOpcode();
876 unsigned Op1Opc = OR.getOperand(1).getOpcode();
877
878 // Verify that we have the correct opcodes
879 if (ISD::SHL != Op0Opc && ISD::SRL != Op0Opc && ISD::AND != Op0Opc)
880 return false;
881 if (ISD::SHL != Op1Opc && ISD::SRL != Op1Opc && ISD::AND != Op1Opc)
882 return false;
883
884 // Generate Mask value for Target
885 if (ConstantSDNode *CN =
886 dyn_cast<ConstantSDNode>(OR.getOperand(0).getOperand(1).Val)) {
887 switch(Op0Opc) {
888 case ISD::SHL: TgtMask <<= (unsigned)CN->getValue(); break;
889 case ISD::SRL: TgtMask >>= (unsigned)CN->getValue(); break;
890 case ISD::AND: TgtMask &= (unsigned)CN->getValue(); break;
891 }
892 } else {
893 return false;
894 }
895
896 // Generate Mask value for Insert
897 if (ConstantSDNode *CN =
898 dyn_cast<ConstantSDNode>(OR.getOperand(1).getOperand(1).Val)) {
899 switch(Op1Opc) {
900 case ISD::SHL:
901 Amount = CN->getValue();
Nate Begemancd08e4c2005-04-09 20:09:12 +0000902 InsMask <<= Amount;
903 if (Op0Opc == ISD::SRL) IsRotate = true;
Nate Begeman7ddecb42005-04-06 23:51:40 +0000904 break;
905 case ISD::SRL:
906 Amount = CN->getValue();
907 InsMask >>= Amount;
908 Amount = 32-Amount;
Nate Begemancd08e4c2005-04-09 20:09:12 +0000909 if (Op0Opc == ISD::SHL) IsRotate = true;
Nate Begeman7ddecb42005-04-06 23:51:40 +0000910 break;
911 case ISD::AND:
912 InsMask &= (unsigned)CN->getValue();
913 break;
914 }
915 } else {
916 return false;
917 }
918
919 // Verify that the Target mask and Insert mask together form a full word mask
920 // and that the Insert mask is a run of set bits (which implies both are runs
921 // of set bits). Given that, Select the arguments and generate the rlwimi
922 // instruction.
923 unsigned MB, ME;
924 if (((TgtMask ^ InsMask) == 0xFFFFFFFF) && IsRunOfOnes(InsMask, MB, ME)) {
925 unsigned Tmp1, Tmp2;
Nate Begemancd08e4c2005-04-09 20:09:12 +0000926 // Check for rotlwi / rotrwi here, a special case of bitfield insert
927 // where both bitfield halves are sourced from the same value.
928 if (IsRotate &&
929 OR.getOperand(0).getOperand(0) == OR.getOperand(1).getOperand(0)) {
930 ++Rotates; // Statistic
931 Tmp1 = SelectExpr(OR.getOperand(0).getOperand(0));
932 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(Amount)
933 .addImm(0).addImm(31);
934 return true;
935 }
Nate Begeman7ddecb42005-04-06 23:51:40 +0000936 if (Op0Opc == ISD::AND)
937 Tmp1 = SelectExpr(OR.getOperand(0).getOperand(0));
938 else
939 Tmp1 = SelectExpr(OR.getOperand(0));
940 Tmp2 = SelectExpr(OR.getOperand(1).getOperand(0));
941 BuildMI(BB, PPC::RLWIMI, 5, Result).addReg(Tmp1).addReg(Tmp2)
942 .addImm(Amount).addImm(MB).addImm(ME);
943 return true;
944 }
945 return false;
946}
947
Nate Begemandffcfcc2005-04-01 00:32:34 +0000948unsigned ISel::SelectSetCR0(SDOperand CC) {
949 unsigned Opc, Tmp1, Tmp2;
950 static const unsigned CompareOpcodes[] =
951 { PPC::FCMPU, PPC::FCMPU, PPC::CMPW, PPC::CMPLW };
952
953 // If the first operand to the select is a SETCC node, then we can fold it
954 // into the branch that selects which value to return.
955 SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(CC.Val);
956 if (SetCC && CC.getOpcode() == ISD::SETCC) {
957 bool U;
958 Opc = getBCCForSetCC(SetCC->getCondition(), U);
959 Tmp1 = SelectExpr(SetCC->getOperand(0));
960
Nate Begeman439b4442005-04-05 04:22:58 +0000961 // Pass the optional argument U to getImmediateForOpcode for SETCC,
Nate Begemandffcfcc2005-04-01 00:32:34 +0000962 // so that it knows whether the SETCC immediate range is signed or not.
Nate Begeman439b4442005-04-05 04:22:58 +0000963 if (1 == getImmediateForOpcode(SetCC->getOperand(1), ISD::SETCC,
964 Tmp2, U)) {
Nate Begemandffcfcc2005-04-01 00:32:34 +0000965 if (U)
966 BuildMI(BB, PPC::CMPLWI, 2, PPC::CR0).addReg(Tmp1).addImm(Tmp2);
967 else
968 BuildMI(BB, PPC::CMPWI, 2, PPC::CR0).addReg(Tmp1).addSImm(Tmp2);
969 } else {
970 bool IsInteger = MVT::isInteger(SetCC->getOperand(0).getValueType());
971 unsigned CompareOpc = CompareOpcodes[2 * IsInteger + U];
972 Tmp2 = SelectExpr(SetCC->getOperand(1));
973 BuildMI(BB, CompareOpc, 2, PPC::CR0).addReg(Tmp1).addReg(Tmp2);
974 }
975 } else {
976 Tmp1 = SelectExpr(CC);
977 BuildMI(BB, PPC::CMPLWI, 2, PPC::CR0).addReg(Tmp1).addImm(0);
978 Opc = PPC::BNE;
979 }
980 return Opc;
981}
982
983/// Check to see if the load is a constant offset from a base register
Nate Begeman04730362005-04-01 04:45:11 +0000984bool ISel::SelectAddr(SDOperand N, unsigned& Reg, int& offset)
Nate Begemana9795f82005-03-24 04:41:43 +0000985{
Nate Begeman96fc6812005-03-31 02:05:53 +0000986 unsigned imm = 0, opcode = N.getOpcode();
Nate Begeman04730362005-04-01 04:45:11 +0000987 if (N.getOpcode() == ISD::ADD) {
988 Reg = SelectExpr(N.getOperand(0));
Nate Begeman439b4442005-04-05 04:22:58 +0000989 if (1 == getImmediateForOpcode(N.getOperand(1), opcode, imm)) {
Nate Begeman96fc6812005-03-31 02:05:53 +0000990 offset = imm;
Nate Begeman04730362005-04-01 04:45:11 +0000991 return false;
992 }
993 offset = SelectExpr(N.getOperand(1));
994 return true;
995 }
Nate Begemana9795f82005-03-24 04:41:43 +0000996 Reg = SelectExpr(N);
997 offset = 0;
Nate Begeman04730362005-04-01 04:45:11 +0000998 return false;
Nate Begemana9795f82005-03-24 04:41:43 +0000999}
1000
1001void ISel::SelectBranchCC(SDOperand N)
1002{
Nate Begemana9795f82005-03-24 04:41:43 +00001003 MachineBasicBlock *Dest =
1004 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
Nate Begeman3e897162005-03-31 23:55:40 +00001005
Nate Begemana9795f82005-03-24 04:41:43 +00001006 Select(N.getOperand(0)); //chain
Nate Begemandffcfcc2005-04-01 00:32:34 +00001007 unsigned Opc = SelectSetCR0(N.getOperand(1));
Nate Begemancd08e4c2005-04-09 20:09:12 +00001008
1009 // Iterate to the next basic block, unless we're already at the end of the
1010 ilist<MachineBasicBlock>::iterator It = BB, E = BB->getParent()->end();
Nate Begeman706471e2005-04-09 23:35:05 +00001011 if (++It == E) It = BB;
Nate Begemancd08e4c2005-04-09 20:09:12 +00001012
1013 // If this is a two way branch, then grab the fallthrough basic block argument
1014 // and build a PowerPC branch pseudo-op, suitable for long branch conversion
1015 // if necessary by the branch selection pass. Otherwise, emit a standard
1016 // conditional branch.
1017 if (N.getOpcode() == ISD::BRCONDTWOWAY) {
1018 MachineBasicBlock *Fallthrough =
1019 cast<BasicBlockSDNode>(N.getOperand(3))->getBasicBlock();
1020 if (Dest != It) {
1021 BuildMI(BB, PPC::COND_BRANCH, 4).addReg(PPC::CR0).addImm(Opc)
1022 .addMBB(Dest).addMBB(Fallthrough);
1023 if (Fallthrough != It)
1024 BuildMI(BB, PPC::B, 1).addMBB(Fallthrough);
1025 } else {
1026 if (Fallthrough != It) {
1027 Opc = PPC32InstrInfo::invertPPCBranchOpcode(Opc);
1028 BuildMI(BB, PPC::COND_BRANCH, 4).addReg(PPC::CR0).addImm(Opc)
1029 .addMBB(Fallthrough).addMBB(Dest);
1030 }
1031 }
1032 } else {
Nate Begeman27499e32005-04-10 01:48:29 +00001033 BuildMI(BB, PPC::COND_BRANCH, 4).addReg(PPC::CR0).addImm(Opc)
1034 .addMBB(Dest).addMBB(It);
1035 //BuildMI(BB, Opc, 2).addReg(PPC::CR0).addMBB(Dest);
Nate Begemancd08e4c2005-04-09 20:09:12 +00001036 }
Nate Begemana9795f82005-03-24 04:41:43 +00001037 return;
1038}
1039
1040unsigned ISel::SelectExprFP(SDOperand N, unsigned Result)
1041{
1042 unsigned Tmp1, Tmp2, Tmp3;
1043 unsigned Opc = 0;
1044 SDNode *Node = N.Val;
1045 MVT::ValueType DestType = N.getValueType();
1046 unsigned opcode = N.getOpcode();
1047
1048 switch (opcode) {
1049 default:
1050 Node->dump();
1051 assert(0 && "Node not handled!\n");
1052
Nate Begeman23afcfb2005-03-29 22:48:55 +00001053 case ISD::SELECT: {
Nate Begeman3e897162005-03-31 23:55:40 +00001054 // Attempt to generate FSEL. We can do this whenever we have an FP result,
1055 // and an FP comparison in the SetCC node.
1056 SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(N.getOperand(0).Val);
1057 if (SetCC && N.getOperand(0).getOpcode() == ISD::SETCC &&
1058 !MVT::isInteger(SetCC->getOperand(0).getValueType()) &&
1059 SetCC->getCondition() != ISD::SETEQ &&
1060 SetCC->getCondition() != ISD::SETNE) {
1061 MVT::ValueType VT = SetCC->getOperand(0).getValueType();
Nate Begeman3e897162005-03-31 23:55:40 +00001062 unsigned TV = SelectExpr(N.getOperand(1)); // Use if TRUE
1063 unsigned FV = SelectExpr(N.getOperand(2)); // Use if FALSE
1064
1065 ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(1));
1066 if (CN && (CN->isExactlyValue(-0.0) || CN->isExactlyValue(0.0))) {
1067 switch(SetCC->getCondition()) {
1068 default: assert(0 && "Invalid FSEL condition"); abort();
1069 case ISD::SETULT:
1070 case ISD::SETLT:
Nate Begemanaf4ab1b2005-04-09 09:33:07 +00001071 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
Nate Begeman3e897162005-03-31 23:55:40 +00001072 case ISD::SETUGE:
1073 case ISD::SETGE:
Nate Begemanaf4ab1b2005-04-09 09:33:07 +00001074 Tmp1 = SelectExpr(SetCC->getOperand(0)); // Val to compare against
Nate Begeman3e897162005-03-31 23:55:40 +00001075 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp1).addReg(TV).addReg(FV);
1076 return Result;
1077 case ISD::SETUGT:
Nate Begemanaf4ab1b2005-04-09 09:33:07 +00001078 case ISD::SETGT:
1079 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
Nate Begeman3e897162005-03-31 23:55:40 +00001080 case ISD::SETULE:
1081 case ISD::SETLE: {
Nate Begemanaf4ab1b2005-04-09 09:33:07 +00001082 if (SetCC->getOperand(0).getOpcode() == ISD::FNEG) {
1083 Tmp2 = SelectExpr(SetCC->getOperand(0).getOperand(0));
1084 } else {
1085 Tmp2 = MakeReg(VT);
1086 Tmp1 = SelectExpr(SetCC->getOperand(0)); // Val to compare against
1087 BuildMI(BB, PPC::FNEG, 1, Tmp2).addReg(Tmp1);
1088 }
Nate Begeman3e897162005-03-31 23:55:40 +00001089 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp2).addReg(TV).addReg(FV);
1090 return Result;
1091 }
1092 }
1093 } else {
1094 Opc = (MVT::f64 == VT) ? PPC::FSUB : PPC::FSUBS;
Nate Begemanaf4ab1b2005-04-09 09:33:07 +00001095 Tmp1 = SelectExpr(SetCC->getOperand(0)); // Val to compare against
Nate Begeman3e897162005-03-31 23:55:40 +00001096 Tmp2 = SelectExpr(SetCC->getOperand(1));
1097 Tmp3 = MakeReg(VT);
1098 switch(SetCC->getCondition()) {
1099 default: assert(0 && "Invalid FSEL condition"); abort();
1100 case ISD::SETULT:
1101 case ISD::SETLT:
1102 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
1103 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(FV).addReg(TV);
1104 return Result;
1105 case ISD::SETUGE:
1106 case ISD::SETGE:
1107 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
1108 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(TV).addReg(FV);
1109 return Result;
1110 case ISD::SETUGT:
1111 case ISD::SETGT:
1112 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp2).addReg(Tmp1);
1113 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(FV).addReg(TV);
1114 return Result;
1115 case ISD::SETULE:
1116 case ISD::SETLE:
1117 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp2).addReg(Tmp1);
1118 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(TV).addReg(FV);
1119 return Result;
1120 }
1121 }
1122 assert(0 && "Should never get here");
1123 return 0;
1124 }
1125
Nate Begeman31318e42005-04-01 07:21:30 +00001126 unsigned TrueValue = SelectExpr(N.getOperand(1)); //Use if TRUE
1127 unsigned FalseValue = SelectExpr(N.getOperand(2)); //Use if FALSE
Nate Begeman6cb2e1b2005-04-01 08:57:43 +00001128 Opc = SelectSetCR0(N.getOperand(0));
Nate Begeman31318e42005-04-01 07:21:30 +00001129
Nate Begeman23afcfb2005-03-29 22:48:55 +00001130 // Create an iterator with which to insert the MBB for copying the false
1131 // value and the MBB to hold the PHI instruction for this SetCC.
1132 MachineBasicBlock *thisMBB = BB;
1133 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1134 ilist<MachineBasicBlock>::iterator It = BB;
1135 ++It;
1136
1137 // thisMBB:
1138 // ...
1139 // TrueVal = ...
1140 // cmpTY cr0, r1, r2
1141 // bCC copy1MBB
1142 // fallthrough --> copy0MBB
Nate Begeman23afcfb2005-03-29 22:48:55 +00001143 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
1144 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
Nate Begeman6cb2e1b2005-04-01 08:57:43 +00001145 BuildMI(BB, Opc, 2).addReg(PPC::CR0).addMBB(sinkMBB);
Nate Begeman23afcfb2005-03-29 22:48:55 +00001146 MachineFunction *F = BB->getParent();
1147 F->getBasicBlockList().insert(It, copy0MBB);
1148 F->getBasicBlockList().insert(It, sinkMBB);
1149 // Update machine-CFG edges
1150 BB->addSuccessor(copy0MBB);
1151 BB->addSuccessor(sinkMBB);
1152
1153 // copy0MBB:
1154 // %FalseValue = ...
1155 // # fallthrough to sinkMBB
1156 BB = copy0MBB;
Nate Begeman23afcfb2005-03-29 22:48:55 +00001157 // Update machine-CFG edges
1158 BB->addSuccessor(sinkMBB);
1159
1160 // sinkMBB:
1161 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1162 // ...
1163 BB = sinkMBB;
1164 BuildMI(BB, PPC::PHI, 4, Result).addReg(FalseValue)
1165 .addMBB(copy0MBB).addReg(TrueValue).addMBB(thisMBB);
1166 return Result;
1167 }
Nate Begeman27eeb002005-04-02 05:59:34 +00001168
1169 case ISD::FNEG:
Nate Begeman93075ec2005-04-04 23:40:36 +00001170 if (!NoExcessFPPrecision &&
1171 ISD::ADD == N.getOperand(0).getOpcode() &&
1172 N.getOperand(0).Val->hasOneUse() &&
1173 ISD::MUL == N.getOperand(0).getOperand(0).getOpcode() &&
1174 N.getOperand(0).getOperand(0).Val->hasOneUse()) {
Nate Begeman80196b12005-04-05 00:15:08 +00001175 ++FusedFP; // Statistic
Nate Begeman93075ec2005-04-04 23:40:36 +00001176 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(0));
1177 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(1));
1178 Tmp3 = SelectExpr(N.getOperand(0).getOperand(1));
1179 Opc = DestType == MVT::f64 ? PPC::FNMADD : PPC::FNMADDS;
1180 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1181 } else if (!NoExcessFPPrecision &&
Nate Begemane88aa5b2005-04-09 03:05:51 +00001182 ISD::ADD == N.getOperand(0).getOpcode() &&
Nate Begeman93075ec2005-04-04 23:40:36 +00001183 N.getOperand(0).Val->hasOneUse() &&
Nate Begemane88aa5b2005-04-09 03:05:51 +00001184 ISD::MUL == N.getOperand(0).getOperand(1).getOpcode() &&
1185 N.getOperand(0).getOperand(1).Val->hasOneUse()) {
Nate Begeman80196b12005-04-05 00:15:08 +00001186 ++FusedFP; // Statistic
Nate Begemane88aa5b2005-04-09 03:05:51 +00001187 Tmp1 = SelectExpr(N.getOperand(0).getOperand(1).getOperand(0));
1188 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1).getOperand(1));
1189 Tmp3 = SelectExpr(N.getOperand(0).getOperand(0));
1190 Opc = DestType == MVT::f64 ? PPC::FNMADD : PPC::FNMADDS;
Nate Begeman93075ec2005-04-04 23:40:36 +00001191 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1192 } else if (ISD::FABS == N.getOperand(0).getOpcode()) {
Nate Begeman27eeb002005-04-02 05:59:34 +00001193 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1194 BuildMI(BB, PPC::FNABS, 1, Result).addReg(Tmp1);
1195 } else {
1196 Tmp1 = SelectExpr(N.getOperand(0));
1197 BuildMI(BB, PPC::FNEG, 1, Result).addReg(Tmp1);
1198 }
1199 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001200
Nate Begeman27eeb002005-04-02 05:59:34 +00001201 case ISD::FABS:
1202 Tmp1 = SelectExpr(N.getOperand(0));
1203 BuildMI(BB, PPC::FABS, 1, Result).addReg(Tmp1);
1204 return Result;
1205
Nate Begemana9795f82005-03-24 04:41:43 +00001206 case ISD::FP_ROUND:
1207 assert (DestType == MVT::f32 &&
1208 N.getOperand(0).getValueType() == MVT::f64 &&
1209 "only f64 to f32 conversion supported here");
1210 Tmp1 = SelectExpr(N.getOperand(0));
1211 BuildMI(BB, PPC::FRSP, 1, Result).addReg(Tmp1);
1212 return Result;
1213
1214 case ISD::FP_EXTEND:
1215 assert (DestType == MVT::f64 &&
1216 N.getOperand(0).getValueType() == MVT::f32 &&
1217 "only f32 to f64 conversion supported here");
1218 Tmp1 = SelectExpr(N.getOperand(0));
1219 BuildMI(BB, PPC::FMR, 1, Result).addReg(Tmp1);
1220 return Result;
1221
1222 case ISD::CopyFromReg:
Nate Begemanf2622612005-03-26 02:17:46 +00001223 if (Result == 1)
1224 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1225 Tmp1 = dyn_cast<RegSDNode>(Node)->getReg();
1226 BuildMI(BB, PPC::FMR, 1, Result).addReg(Tmp1);
1227 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001228
Nate Begeman6d369cc2005-04-01 01:08:07 +00001229 case ISD::ConstantFP: {
Nate Begeman6d369cc2005-04-01 01:08:07 +00001230 ConstantFPSDNode *CN = cast<ConstantFPSDNode>(N);
Nate Begeman6b559972005-04-01 02:59:27 +00001231 Result = getConstDouble(CN->getValue(), Result);
Nate Begeman6d369cc2005-04-01 01:08:07 +00001232 return Result;
1233 }
Nate Begemana9795f82005-03-24 04:41:43 +00001234
Nate Begemana9795f82005-03-24 04:41:43 +00001235 case ISD::ADD:
Nate Begeman93075ec2005-04-04 23:40:36 +00001236 if (!NoExcessFPPrecision && N.getOperand(0).getOpcode() == ISD::MUL &&
1237 N.getOperand(0).Val->hasOneUse()) {
1238 ++FusedFP; // Statistic
1239 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1240 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1241 Tmp3 = SelectExpr(N.getOperand(1));
1242 Opc = DestType == MVT::f64 ? PPC::FMADD : PPC::FMADDS;
1243 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1244 return Result;
1245 }
Nate Begemane88aa5b2005-04-09 03:05:51 +00001246 if (!NoExcessFPPrecision && N.getOperand(1).getOpcode() == ISD::MUL &&
1247 N.getOperand(1).Val->hasOneUse()) {
1248 ++FusedFP; // Statistic
1249 Tmp1 = SelectExpr(N.getOperand(1).getOperand(0));
1250 Tmp2 = SelectExpr(N.getOperand(1).getOperand(1));
1251 Tmp3 = SelectExpr(N.getOperand(0));
1252 Opc = DestType == MVT::f64 ? PPC::FMADD : PPC::FMADDS;
1253 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1254 return Result;
1255 }
Nate Begeman93075ec2005-04-04 23:40:36 +00001256 Opc = DestType == MVT::f64 ? PPC::FADD : PPC::FADDS;
1257 Tmp1 = SelectExpr(N.getOperand(0));
1258 Tmp2 = SelectExpr(N.getOperand(1));
1259 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1260 return Result;
1261
Nate Begemana9795f82005-03-24 04:41:43 +00001262 case ISD::SUB:
Nate Begeman93075ec2005-04-04 23:40:36 +00001263 if (!NoExcessFPPrecision && N.getOperand(0).getOpcode() == ISD::MUL &&
1264 N.getOperand(0).Val->hasOneUse()) {
1265 ++FusedFP; // Statistic
1266 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1267 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1268 Tmp3 = SelectExpr(N.getOperand(1));
1269 Opc = DestType == MVT::f64 ? PPC::FMSUB : PPC::FMSUBS;
1270 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1271 return Result;
1272 }
Nate Begemane88aa5b2005-04-09 03:05:51 +00001273 if (!NoExcessFPPrecision && N.getOperand(1).getOpcode() == ISD::MUL &&
1274 N.getOperand(1).Val->hasOneUse()) {
1275 ++FusedFP; // Statistic
1276 Tmp1 = SelectExpr(N.getOperand(1).getOperand(0));
1277 Tmp2 = SelectExpr(N.getOperand(1).getOperand(1));
1278 Tmp3 = SelectExpr(N.getOperand(0));
1279 Opc = DestType == MVT::f64 ? PPC::FNMSUB : PPC::FNMSUBS;
1280 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1281 return Result;
1282 }
Nate Begeman93075ec2005-04-04 23:40:36 +00001283 Opc = DestType == MVT::f64 ? PPC::FSUB : PPC::FSUBS;
1284 Tmp1 = SelectExpr(N.getOperand(0));
1285 Tmp2 = SelectExpr(N.getOperand(1));
1286 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1287 return Result;
1288
1289 case ISD::MUL:
Nate Begemana9795f82005-03-24 04:41:43 +00001290 case ISD::SDIV:
1291 switch( opcode ) {
1292 case ISD::MUL: Opc = DestType == MVT::f64 ? PPC::FMUL : PPC::FMULS; break;
Nate Begemana9795f82005-03-24 04:41:43 +00001293 case ISD::SDIV: Opc = DestType == MVT::f64 ? PPC::FDIV : PPC::FDIVS; break;
1294 };
Nate Begemana9795f82005-03-24 04:41:43 +00001295 Tmp1 = SelectExpr(N.getOperand(0));
1296 Tmp2 = SelectExpr(N.getOperand(1));
1297 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1298 return Result;
1299
Nate Begemana9795f82005-03-24 04:41:43 +00001300 case ISD::UINT_TO_FP:
Nate Begemanfdcf3412005-03-30 19:38:35 +00001301 case ISD::SINT_TO_FP: {
1302 assert (N.getOperand(0).getValueType() == MVT::i32
1303 && "int to float must operate on i32");
1304 bool IsUnsigned = (ISD::UINT_TO_FP == opcode);
1305 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
1306 Tmp2 = MakeReg(MVT::f64); // temp reg to load the integer value into
1307 Tmp3 = MakeReg(MVT::i32); // temp reg to hold the conversion constant
1308 unsigned ConstF = MakeReg(MVT::f64); // temp reg to hold the fp constant
1309
1310 int FrameIdx = BB->getParent()->getFrameInfo()->CreateStackObject(8, 8);
1311 MachineConstantPool *CP = BB->getParent()->getConstantPool();
1312
1313 // FIXME: pull this FP constant generation stuff out into something like
1314 // the simple ISel's getReg.
1315 if (IsUnsigned) {
1316 ConstantFP *CFP = ConstantFP::get(Type::DoubleTy, 0x1.000000p52);
1317 unsigned CPI = CP->getConstantPoolIndex(CFP);
1318 // Load constant fp value
1319 unsigned Tmp4 = MakeReg(MVT::i32);
1320 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp4).addReg(getGlobalBaseReg())
1321 .addConstantPoolIndex(CPI);
1322 BuildMI(BB, PPC::LFD, 2, ConstF).addConstantPoolIndex(CPI).addReg(Tmp4);
1323 // Store the hi & low halves of the fp value, currently in int regs
1324 BuildMI(BB, PPC::LIS, 1, Tmp3).addSImm(0x4330);
1325 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(Tmp3), FrameIdx);
1326 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(Tmp1), FrameIdx, 4);
1327 addFrameReference(BuildMI(BB, PPC::LFD, 2, Tmp2), FrameIdx);
1328 // Generate the return value with a subtract
1329 BuildMI(BB, PPC::FSUB, 2, Result).addReg(Tmp2).addReg(ConstF);
1330 } else {
1331 ConstantFP *CFP = ConstantFP::get(Type::DoubleTy, 0x1.000008p52);
1332 unsigned CPI = CP->getConstantPoolIndex(CFP);
1333 // Load constant fp value
1334 unsigned Tmp4 = MakeReg(MVT::i32);
1335 unsigned TmpL = MakeReg(MVT::i32);
1336 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp4).addReg(getGlobalBaseReg())
1337 .addConstantPoolIndex(CPI);
1338 BuildMI(BB, PPC::LFD, 2, ConstF).addConstantPoolIndex(CPI).addReg(Tmp4);
1339 // Store the hi & low halves of the fp value, currently in int regs
1340 BuildMI(BB, PPC::LIS, 1, Tmp3).addSImm(0x4330);
1341 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(Tmp3), FrameIdx);
1342 BuildMI(BB, PPC::XORIS, 2, TmpL).addReg(Tmp1).addImm(0x8000);
1343 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(TmpL), FrameIdx, 4);
1344 addFrameReference(BuildMI(BB, PPC::LFD, 2, Tmp2), FrameIdx);
1345 // Generate the return value with a subtract
1346 BuildMI(BB, PPC::FSUB, 2, Result).addReg(Tmp2).addReg(ConstF);
1347 }
1348 return Result;
1349 }
Nate Begemana9795f82005-03-24 04:41:43 +00001350 }
Nate Begeman6b559972005-04-01 02:59:27 +00001351 assert(0 && "Should never get here");
Nate Begemana9795f82005-03-24 04:41:43 +00001352 return 0;
1353}
1354
1355unsigned ISel::SelectExpr(SDOperand N) {
1356 unsigned Result;
1357 unsigned Tmp1, Tmp2, Tmp3;
1358 unsigned Opc = 0;
1359 unsigned opcode = N.getOpcode();
1360
1361 SDNode *Node = N.Val;
1362 MVT::ValueType DestType = N.getValueType();
1363
1364 unsigned &Reg = ExprMap[N];
1365 if (Reg) return Reg;
1366
Nate Begeman27eeb002005-04-02 05:59:34 +00001367 switch (N.getOpcode()) {
1368 default:
Nate Begemana9795f82005-03-24 04:41:43 +00001369 Reg = Result = (N.getValueType() != MVT::Other) ?
Nate Begeman27eeb002005-04-02 05:59:34 +00001370 MakeReg(N.getValueType()) : 1;
1371 break;
1372 case ISD::CALL:
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001373 // If this is a call instruction, make sure to prepare ALL of the result
1374 // values as well as the chain.
Nate Begeman27eeb002005-04-02 05:59:34 +00001375 if (Node->getNumValues() == 1)
1376 Reg = Result = 1; // Void call, just a chain.
1377 else {
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001378 Result = MakeReg(Node->getValueType(0));
1379 ExprMap[N.getValue(0)] = Result;
Nate Begeman27eeb002005-04-02 05:59:34 +00001380 for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001381 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
Nate Begeman27eeb002005-04-02 05:59:34 +00001382 ExprMap[SDOperand(Node, Node->getNumValues()-1)] = 1;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001383 }
Nate Begeman27eeb002005-04-02 05:59:34 +00001384 break;
1385 case ISD::ADD_PARTS:
1386 case ISD::SUB_PARTS:
1387 case ISD::SHL_PARTS:
1388 case ISD::SRL_PARTS:
1389 case ISD::SRA_PARTS:
1390 Result = MakeReg(Node->getValueType(0));
1391 ExprMap[N.getValue(0)] = Result;
1392 for (unsigned i = 1, e = N.Val->getNumValues(); i != e; ++i)
1393 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
1394 break;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001395 }
1396
Nate Begemane5846682005-04-04 06:52:38 +00001397 if (ISD::CopyFromReg == opcode)
1398 DestType = N.getValue(0).getValueType();
1399
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001400 if (DestType == MVT::f64 || DestType == MVT::f32)
Nate Begemana0e3e942005-04-10 01:14:13 +00001401 if (ISD::LOAD != opcode && ISD::EXTLOAD != opcode &&
1402 ISD::UNDEF != opcode && ISD::CALL != opcode)
Nate Begeman74d73452005-03-31 00:15:26 +00001403 return SelectExprFP(N, Result);
Nate Begemana9795f82005-03-24 04:41:43 +00001404
1405 switch (opcode) {
1406 default:
1407 Node->dump();
1408 assert(0 && "Node not handled!\n");
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001409 case ISD::UNDEF:
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001410 BuildMI(BB, PPC::IMPLICIT_DEF, 0, Result);
1411 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001412 case ISD::DYNAMIC_STACKALLOC:
Nate Begeman5e966612005-03-24 06:28:42 +00001413 // Generate both result values. FIXME: Need a better commment here?
1414 if (Result != 1)
1415 ExprMap[N.getValue(1)] = 1;
1416 else
1417 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1418
1419 // FIXME: We are currently ignoring the requested alignment for handling
1420 // greater than the stack alignment. This will need to be revisited at some
1421 // point. Align = N.getOperand(2);
1422 if (!isa<ConstantSDNode>(N.getOperand(2)) ||
1423 cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) {
1424 std::cerr << "Cannot allocate stack object with greater alignment than"
1425 << " the stack alignment yet!";
1426 abort();
1427 }
1428 Select(N.getOperand(0));
1429 Tmp1 = SelectExpr(N.getOperand(1));
1430 // Subtract size from stack pointer, thereby allocating some space.
1431 BuildMI(BB, PPC::SUBF, 2, PPC::R1).addReg(Tmp1).addReg(PPC::R1);
1432 // Put a pointer to the space into the result register by copying the SP
1433 BuildMI(BB, PPC::OR, 2, Result).addReg(PPC::R1).addReg(PPC::R1);
1434 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001435
1436 case ISD::ConstantPool:
Nate Begemanca12a2b2005-03-28 22:28:37 +00001437 Tmp1 = cast<ConstantPoolSDNode>(N)->getIndex();
1438 Tmp2 = MakeReg(MVT::i32);
1439 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp2).addReg(getGlobalBaseReg())
1440 .addConstantPoolIndex(Tmp1);
1441 BuildMI(BB, PPC::LA, 2, Result).addReg(Tmp2).addConstantPoolIndex(Tmp1);
1442 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001443
1444 case ISD::FrameIndex:
Nate Begemanf3d08f32005-03-29 00:03:27 +00001445 Tmp1 = cast<FrameIndexSDNode>(N)->getIndex();
Nate Begeman58f718c2005-03-30 02:23:08 +00001446 addFrameReference(BuildMI(BB, PPC::ADDI, 2, Result), (int)Tmp1, 0, false);
Nate Begemanf3d08f32005-03-29 00:03:27 +00001447 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001448
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001449 case ISD::GlobalAddress: {
1450 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
Nate Begemanca12a2b2005-03-28 22:28:37 +00001451 Tmp1 = MakeReg(MVT::i32);
Nate Begemanc7b09f12005-03-25 08:34:25 +00001452 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp1).addReg(getGlobalBaseReg())
1453 .addGlobalAddress(GV);
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001454 if (GV->hasWeakLinkage() || GV->isExternal()) {
1455 BuildMI(BB, PPC::LWZ, 2, Result).addGlobalAddress(GV).addReg(Tmp1);
1456 } else {
1457 BuildMI(BB, PPC::LA, 2, Result).addReg(Tmp1).addGlobalAddress(GV);
1458 }
1459 return Result;
1460 }
1461
Nate Begeman5e966612005-03-24 06:28:42 +00001462 case ISD::LOAD:
Nate Begemana9795f82005-03-24 04:41:43 +00001463 case ISD::EXTLOAD:
1464 case ISD::ZEXTLOAD:
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001465 case ISD::SEXTLOAD: {
Nate Begeman9db505c2005-03-28 19:36:43 +00001466 MVT::ValueType TypeBeingLoaded = (ISD::LOAD == opcode) ?
1467 Node->getValueType(0) : cast<MVTSDNode>(Node)->getExtraValueType();
Nate Begeman74d73452005-03-31 00:15:26 +00001468 bool sext = (ISD::SEXTLOAD == opcode);
Nate Begeman74d73452005-03-31 00:15:26 +00001469
Nate Begeman5e966612005-03-24 06:28:42 +00001470 // Make sure we generate both values.
1471 if (Result != 1)
1472 ExprMap[N.getValue(1)] = 1; // Generate the token
1473 else
1474 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1475
1476 SDOperand Chain = N.getOperand(0);
1477 SDOperand Address = N.getOperand(1);
1478 Select(Chain);
1479
Nate Begeman9db505c2005-03-28 19:36:43 +00001480 switch (TypeBeingLoaded) {
Nate Begeman74d73452005-03-31 00:15:26 +00001481 default: Node->dump(); assert(0 && "Cannot load this type!");
Nate Begeman9db505c2005-03-28 19:36:43 +00001482 case MVT::i1: Opc = PPC::LBZ; break;
1483 case MVT::i8: Opc = PPC::LBZ; break;
1484 case MVT::i16: Opc = sext ? PPC::LHA : PPC::LHZ; break;
1485 case MVT::i32: Opc = PPC::LWZ; break;
Nate Begeman74d73452005-03-31 00:15:26 +00001486 case MVT::f32: Opc = PPC::LFS; break;
1487 case MVT::f64: Opc = PPC::LFD; break;
Nate Begeman5e966612005-03-24 06:28:42 +00001488 }
1489
Nate Begeman74d73452005-03-31 00:15:26 +00001490 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Address)) {
1491 Tmp1 = MakeReg(MVT::i32);
1492 int CPI = CP->getIndex();
1493 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp1).addReg(getGlobalBaseReg())
1494 .addConstantPoolIndex(CPI);
1495 BuildMI(BB, Opc, 2, Result).addConstantPoolIndex(CPI).addReg(Tmp1);
Nate Begeman9db505c2005-03-28 19:36:43 +00001496 }
Nate Begeman74d73452005-03-31 00:15:26 +00001497 else if(Address.getOpcode() == ISD::FrameIndex) {
Nate Begeman58f718c2005-03-30 02:23:08 +00001498 Tmp1 = cast<FrameIndexSDNode>(Address)->getIndex();
1499 addFrameReference(BuildMI(BB, Opc, 2, Result), (int)Tmp1);
Nate Begeman5e966612005-03-24 06:28:42 +00001500 } else {
1501 int offset;
Nate Begeman04730362005-04-01 04:45:11 +00001502 bool idx = SelectAddr(Address, Tmp1, offset);
1503 if (idx) {
1504 Opc = IndexedOpForOp(Opc);
1505 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(offset);
1506 } else {
1507 BuildMI(BB, Opc, 2, Result).addSImm(offset).addReg(Tmp1);
1508 }
Nate Begeman5e966612005-03-24 06:28:42 +00001509 }
1510 return Result;
1511 }
1512
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001513 case ISD::CALL: {
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001514 unsigned GPR_idx = 0, FPR_idx = 0;
1515 static const unsigned GPR[] = {
1516 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
1517 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
1518 };
1519 static const unsigned FPR[] = {
1520 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
1521 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
1522 };
1523
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001524 // Lower the chain for this call.
1525 Select(N.getOperand(0));
1526 ExprMap[N.getValue(Node->getNumValues()-1)] = 1;
Nate Begeman74d73452005-03-31 00:15:26 +00001527
Nate Begemand860aa62005-04-04 22:17:48 +00001528 MachineInstr *CallMI;
1529 // Emit the correct call instruction based on the type of symbol called.
1530 if (GlobalAddressSDNode *GASD =
1531 dyn_cast<GlobalAddressSDNode>(N.getOperand(1))) {
1532 CallMI = BuildMI(PPC::CALLpcrel, 1).addGlobalAddress(GASD->getGlobal(),
1533 true);
1534 } else if (ExternalSymbolSDNode *ESSDN =
1535 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1))) {
1536 CallMI = BuildMI(PPC::CALLpcrel, 1).addExternalSymbol(ESSDN->getSymbol(),
1537 true);
1538 } else {
1539 Tmp1 = SelectExpr(N.getOperand(1));
1540 BuildMI(BB, PPC::OR, 2, PPC::R12).addReg(Tmp1).addReg(Tmp1);
1541 BuildMI(BB, PPC::MTCTR, 1).addReg(PPC::R12);
1542 CallMI = BuildMI(PPC::CALLindirect, 3).addImm(20).addImm(0)
1543 .addReg(PPC::R12);
1544 }
1545
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001546 // Load the register args to virtual regs
1547 std::vector<unsigned> ArgVR;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001548 for(int i = 2, e = Node->getNumOperands(); i < e; ++i)
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001549 ArgVR.push_back(SelectExpr(N.getOperand(i)));
1550
1551 // Copy the virtual registers into the appropriate argument register
1552 for(int i = 0, e = ArgVR.size(); i < e; ++i) {
1553 switch(N.getOperand(i+2).getValueType()) {
1554 default: Node->dump(); assert(0 && "Unknown value type for call");
1555 case MVT::i1:
1556 case MVT::i8:
1557 case MVT::i16:
1558 case MVT::i32:
1559 assert(GPR_idx < 8 && "Too many int args");
Nate Begemand860aa62005-04-04 22:17:48 +00001560 if (N.getOperand(i+2).getOpcode() != ISD::UNDEF) {
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001561 BuildMI(BB, PPC::OR,2,GPR[GPR_idx]).addReg(ArgVR[i]).addReg(ArgVR[i]);
Nate Begemand860aa62005-04-04 22:17:48 +00001562 CallMI->addRegOperand(GPR[GPR_idx], MachineOperand::Use);
1563 }
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001564 ++GPR_idx;
1565 break;
1566 case MVT::f64:
1567 case MVT::f32:
1568 assert(FPR_idx < 13 && "Too many fp args");
1569 BuildMI(BB, PPC::FMR, 1, FPR[FPR_idx]).addReg(ArgVR[i]);
Nate Begemand860aa62005-04-04 22:17:48 +00001570 CallMI->addRegOperand(FPR[FPR_idx], MachineOperand::Use);
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001571 ++FPR_idx;
1572 break;
1573 }
1574 }
Nate Begemand860aa62005-04-04 22:17:48 +00001575
1576 // Put the call instruction in the correct place in the MachineBasicBlock
1577 BB->push_back(CallMI);
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001578
1579 switch (Node->getValueType(0)) {
1580 default: assert(0 && "Unknown value type for call result!");
1581 case MVT::Other: return 1;
1582 case MVT::i1:
1583 case MVT::i8:
1584 case MVT::i16:
1585 case MVT::i32:
Nate Begemane5846682005-04-04 06:52:38 +00001586 if (Node->getValueType(1) == MVT::i32) {
1587 BuildMI(BB, PPC::OR, 2, Result+1).addReg(PPC::R3).addReg(PPC::R3);
1588 BuildMI(BB, PPC::OR, 2, Result).addReg(PPC::R4).addReg(PPC::R4);
1589 } else {
1590 BuildMI(BB, PPC::OR, 2, Result).addReg(PPC::R3).addReg(PPC::R3);
1591 }
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001592 break;
1593 case MVT::f32:
1594 case MVT::f64:
1595 BuildMI(BB, PPC::FMR, 1, Result).addReg(PPC::F1);
1596 break;
1597 }
1598 return Result+N.ResNo;
1599 }
Nate Begemana9795f82005-03-24 04:41:43 +00001600
1601 case ISD::SIGN_EXTEND:
1602 case ISD::SIGN_EXTEND_INREG:
1603 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman9db505c2005-03-28 19:36:43 +00001604 switch(cast<MVTSDNode>(Node)->getExtraValueType()) {
1605 default: Node->dump(); assert(0 && "Unhandled SIGN_EXTEND type"); break;
1606 case MVT::i16:
1607 BuildMI(BB, PPC::EXTSH, 1, Result).addReg(Tmp1);
1608 break;
1609 case MVT::i8:
1610 BuildMI(BB, PPC::EXTSB, 1, Result).addReg(Tmp1);
1611 break;
Nate Begeman74747862005-03-29 22:24:51 +00001612 case MVT::i1:
1613 BuildMI(BB, PPC::SUBFIC, 2, Result).addReg(Tmp1).addSImm(0);
1614 break;
Nate Begeman9db505c2005-03-28 19:36:43 +00001615 }
Nate Begemana9795f82005-03-24 04:41:43 +00001616 return Result;
1617
1618 case ISD::ZERO_EXTEND_INREG:
1619 Tmp1 = SelectExpr(N.getOperand(0));
1620 switch(cast<MVTSDNode>(Node)->getExtraValueType()) {
Nate Begeman9db505c2005-03-28 19:36:43 +00001621 default: Node->dump(); assert(0 && "Unhandled ZERO_EXTEND type"); break;
Nate Begemana9795f82005-03-24 04:41:43 +00001622 case MVT::i16: Tmp2 = 16; break;
1623 case MVT::i8: Tmp2 = 24; break;
1624 case MVT::i1: Tmp2 = 31; break;
1625 }
Nate Begeman33162522005-03-29 21:54:38 +00001626 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(0).addImm(Tmp2)
1627 .addImm(31);
Nate Begemana9795f82005-03-24 04:41:43 +00001628 return Result;
1629
Nate Begemana9795f82005-03-24 04:41:43 +00001630 case ISD::CopyFromReg:
1631 if (Result == 1)
1632 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1633 Tmp1 = dyn_cast<RegSDNode>(Node)->getReg();
1634 BuildMI(BB, PPC::OR, 2, Result).addReg(Tmp1).addReg(Tmp1);
1635 return Result;
1636
1637 case ISD::SHL:
Nate Begeman5e966612005-03-24 06:28:42 +00001638 Tmp1 = SelectExpr(N.getOperand(0));
1639 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1640 Tmp2 = CN->getValue() & 0x1F;
Nate Begeman33162522005-03-29 21:54:38 +00001641 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(Tmp2).addImm(0)
Nate Begeman5e966612005-03-24 06:28:42 +00001642 .addImm(31-Tmp2);
1643 } else {
1644 Tmp2 = SelectExpr(N.getOperand(1));
1645 BuildMI(BB, PPC::SLW, 2, Result).addReg(Tmp1).addReg(Tmp2);
1646 }
1647 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001648
Nate Begeman5e966612005-03-24 06:28:42 +00001649 case ISD::SRL:
1650 Tmp1 = SelectExpr(N.getOperand(0));
1651 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1652 Tmp2 = CN->getValue() & 0x1F;
Nate Begeman33162522005-03-29 21:54:38 +00001653 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(32-Tmp2)
Nate Begeman5e966612005-03-24 06:28:42 +00001654 .addImm(Tmp2).addImm(31);
1655 } else {
1656 Tmp2 = SelectExpr(N.getOperand(1));
1657 BuildMI(BB, PPC::SRW, 2, Result).addReg(Tmp1).addReg(Tmp2);
1658 }
1659 return Result;
1660
1661 case ISD::SRA:
1662 Tmp1 = SelectExpr(N.getOperand(0));
1663 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1664 Tmp2 = CN->getValue() & 0x1F;
1665 BuildMI(BB, PPC::SRAWI, 2, Result).addReg(Tmp1).addImm(Tmp2);
1666 } else {
1667 Tmp2 = SelectExpr(N.getOperand(1));
1668 BuildMI(BB, PPC::SRAW, 2, Result).addReg(Tmp1).addReg(Tmp2);
1669 }
1670 return Result;
1671
Nate Begemana9795f82005-03-24 04:41:43 +00001672 case ISD::ADD:
1673 assert (DestType == MVT::i32 && "Only do arithmetic on i32s!");
1674 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman439b4442005-04-05 04:22:58 +00001675 switch(getImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
Nate Begemana9795f82005-03-24 04:41:43 +00001676 default: assert(0 && "unhandled result code");
1677 case 0: // No immediate
1678 Tmp2 = SelectExpr(N.getOperand(1));
1679 BuildMI(BB, PPC::ADD, 2, Result).addReg(Tmp1).addReg(Tmp2);
1680 break;
1681 case 1: // Low immediate
1682 BuildMI(BB, PPC::ADDI, 2, Result).addReg(Tmp1).addSImm(Tmp2);
1683 break;
1684 case 2: // Shifted immediate
1685 BuildMI(BB, PPC::ADDIS, 2, Result).addReg(Tmp1).addSImm(Tmp2);
1686 break;
1687 }
1688 return Result;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001689
Nate Begemana9795f82005-03-24 04:41:43 +00001690 case ISD::AND:
Nate Begeman7ddecb42005-04-06 23:51:40 +00001691 Tmp1 = SelectExpr(N.getOperand(0));
1692 // FIXME: should add check in getImmediateForOpcode to return a value
1693 // indicating the immediate is a run of set bits so we can emit a bitfield
1694 // clear with RLWINM instead.
1695 switch(getImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
1696 default: assert(0 && "unhandled result code");
1697 case 0: // No immediate
1698 Tmp2 = SelectExpr(N.getOperand(1));
1699 BuildMI(BB, PPC::AND, 2, Result).addReg(Tmp1).addReg(Tmp2);
1700 break;
1701 case 1: // Low immediate
1702 BuildMI(BB, PPC::ANDIo, 2, Result).addReg(Tmp1).addImm(Tmp2);
1703 break;
1704 case 2: // Shifted immediate
1705 BuildMI(BB, PPC::ANDISo, 2, Result).addReg(Tmp1).addImm(Tmp2);
1706 break;
1707 }
1708 return Result;
1709
Nate Begemana9795f82005-03-24 04:41:43 +00001710 case ISD::OR:
Nate Begeman7ddecb42005-04-06 23:51:40 +00001711 if (SelectBitfieldInsert(N, Result))
1712 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001713 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman439b4442005-04-05 04:22:58 +00001714 switch(getImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
Nate Begemana9795f82005-03-24 04:41:43 +00001715 default: assert(0 && "unhandled result code");
1716 case 0: // No immediate
1717 Tmp2 = SelectExpr(N.getOperand(1));
Nate Begeman7ddecb42005-04-06 23:51:40 +00001718 BuildMI(BB, PPC::OR, 2, Result).addReg(Tmp1).addReg(Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +00001719 break;
1720 case 1: // Low immediate
Nate Begeman7ddecb42005-04-06 23:51:40 +00001721 BuildMI(BB, PPC::ORI, 2, Result).addReg(Tmp1).addImm(Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +00001722 break;
1723 case 2: // Shifted immediate
Nate Begeman7ddecb42005-04-06 23:51:40 +00001724 BuildMI(BB, PPC::ORIS, 2, Result).addReg(Tmp1).addImm(Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +00001725 break;
1726 }
1727 return Result;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001728
Nate Begemanaa73a9f2005-04-03 11:20:20 +00001729 case ISD::XOR: {
1730 // Check for EQV: xor, (xor a, -1), b
1731 if (N.getOperand(0).getOpcode() == ISD::XOR &&
1732 N.getOperand(0).getOperand(1).getOpcode() == ISD::Constant &&
1733 cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->isAllOnesValue()) {
Nate Begemanaa73a9f2005-04-03 11:20:20 +00001734 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1735 Tmp2 = SelectExpr(N.getOperand(1));
1736 BuildMI(BB, PPC::EQV, 2, Result).addReg(Tmp1).addReg(Tmp2);
1737 return Result;
1738 }
1739 // Check for NOT, NOR, and NAND: xor (copy, or, and), -1
1740 if (N.getOperand(1).getOpcode() == ISD::Constant &&
1741 cast<ConstantSDNode>(N.getOperand(1))->isAllOnesValue()) {
Nate Begemanaa73a9f2005-04-03 11:20:20 +00001742 switch(N.getOperand(0).getOpcode()) {
1743 case ISD::OR:
1744 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1745 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1746 BuildMI(BB, PPC::NOR, 2, Result).addReg(Tmp1).addReg(Tmp2);
1747 break;
1748 case ISD::AND:
1749 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1750 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1751 BuildMI(BB, PPC::NAND, 2, Result).addReg(Tmp1).addReg(Tmp2);
1752 break;
1753 default:
1754 Tmp1 = SelectExpr(N.getOperand(0));
1755 BuildMI(BB, PPC::NOR, 2, Result).addReg(Tmp1).addReg(Tmp1);
1756 break;
1757 }
1758 return Result;
1759 }
1760 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman439b4442005-04-05 04:22:58 +00001761 switch(getImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
Nate Begemanaa73a9f2005-04-03 11:20:20 +00001762 default: assert(0 && "unhandled result code");
1763 case 0: // No immediate
1764 Tmp2 = SelectExpr(N.getOperand(1));
1765 BuildMI(BB, PPC::XOR, 2, Result).addReg(Tmp1).addReg(Tmp2);
1766 break;
1767 case 1: // Low immediate
1768 BuildMI(BB, PPC::XORI, 2, Result).addReg(Tmp1).addImm(Tmp2);
1769 break;
1770 case 2: // Shifted immediate
1771 BuildMI(BB, PPC::XORIS, 2, Result).addReg(Tmp1).addImm(Tmp2);
1772 break;
1773 }
1774 return Result;
1775 }
1776
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001777 case ISD::SUB:
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001778 Tmp2 = SelectExpr(N.getOperand(1));
Nate Begeman439b4442005-04-05 04:22:58 +00001779 if (1 == getImmediateForOpcode(N.getOperand(0), opcode, Tmp1))
Nate Begeman27523a12005-04-02 00:42:16 +00001780 BuildMI(BB, PPC::SUBFIC, 2, Result).addReg(Tmp2).addSImm(Tmp1);
1781 else {
1782 Tmp1 = SelectExpr(N.getOperand(0));
1783 BuildMI(BB, PPC::SUBF, 2, Result).addReg(Tmp2).addReg(Tmp1);
1784 }
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001785 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001786
Nate Begeman5e966612005-03-24 06:28:42 +00001787 case ISD::MUL:
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001788 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman439b4442005-04-05 04:22:58 +00001789 if (1 == getImmediateForOpcode(N.getOperand(1), opcode, Tmp2))
Nate Begeman307e7442005-03-26 01:28:53 +00001790 BuildMI(BB, PPC::MULLI, 2, Result).addReg(Tmp1).addSImm(Tmp2);
1791 else {
1792 Tmp2 = SelectExpr(N.getOperand(1));
1793 BuildMI(BB, PPC::MULLW, 2, Result).addReg(Tmp1).addReg(Tmp2);
1794 }
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001795 return Result;
1796
Nate Begeman815d6da2005-04-06 00:25:27 +00001797 case ISD::MULHS:
1798 case ISD::MULHU:
1799 Tmp1 = SelectExpr(N.getOperand(0));
1800 Tmp2 = SelectExpr(N.getOperand(1));
1801 Opc = (ISD::MULHU == opcode) ? PPC::MULHWU : PPC::MULHW;
1802 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1803 return Result;
1804
Nate Begemanf3d08f32005-03-29 00:03:27 +00001805 case ISD::SDIV:
1806 case ISD::UDIV:
Nate Begeman815d6da2005-04-06 00:25:27 +00001807 switch (getImmediateForOpcode(N.getOperand(1), opcode, Tmp3)) {
1808 default: break;
1809 // If this is an sdiv by a power of two, we can use an srawi/addze pair.
1810 case 3:
Nate Begeman80196b12005-04-05 00:15:08 +00001811 Tmp1 = MakeReg(MVT::i32);
1812 Tmp2 = SelectExpr(N.getOperand(0));
1813 BuildMI(BB, PPC::SRAWI, 2, Tmp1).addReg(Tmp2).addImm(Tmp3);
1814 BuildMI(BB, PPC::ADDZE, 1, Result).addReg(Tmp1);
1815 return Result;
Nate Begeman815d6da2005-04-06 00:25:27 +00001816 // If this is a divide by constant, we can emit code using some magic
1817 // constants to implement it as a multiply instead.
Nate Begeman27b4c232005-04-06 06:44:57 +00001818 case 4:
1819 ExprMap.erase(N);
1820 if (opcode == ISD::SDIV)
1821 return SelectExpr(BuildSDIVSequence(N));
1822 else
1823 return SelectExpr(BuildUDIVSequence(N));
Nate Begeman80196b12005-04-05 00:15:08 +00001824 }
Nate Begemanf3d08f32005-03-29 00:03:27 +00001825 Tmp1 = SelectExpr(N.getOperand(0));
1826 Tmp2 = SelectExpr(N.getOperand(1));
1827 Opc = (ISD::UDIV == opcode) ? PPC::DIVWU : PPC::DIVW;
1828 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1829 return Result;
1830
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001831 case ISD::ADD_PARTS:
Nate Begemanca12a2b2005-03-28 22:28:37 +00001832 case ISD::SUB_PARTS: {
1833 assert(N.getNumOperands() == 4 && N.getValueType() == MVT::i32 &&
1834 "Not an i64 add/sub!");
1835 // Emit all of the operands.
1836 std::vector<unsigned> InVals;
1837 for (unsigned i = 0, e = N.getNumOperands(); i != e; ++i)
1838 InVals.push_back(SelectExpr(N.getOperand(i)));
1839 if (N.getOpcode() == ISD::ADD_PARTS) {
Nate Begeman27eeb002005-04-02 05:59:34 +00001840 BuildMI(BB, PPC::ADDC, 2, Result).addReg(InVals[0]).addReg(InVals[2]);
1841 BuildMI(BB, PPC::ADDE, 2, Result+1).addReg(InVals[1]).addReg(InVals[3]);
Nate Begemanca12a2b2005-03-28 22:28:37 +00001842 } else {
Nate Begeman27eeb002005-04-02 05:59:34 +00001843 BuildMI(BB, PPC::SUBFC, 2, Result).addReg(InVals[2]).addReg(InVals[0]);
1844 BuildMI(BB, PPC::SUBFE, 2, Result+1).addReg(InVals[3]).addReg(InVals[1]);
1845 }
1846 return Result+N.ResNo;
1847 }
1848
1849 case ISD::SHL_PARTS:
1850 case ISD::SRA_PARTS:
1851 case ISD::SRL_PARTS: {
1852 assert(N.getNumOperands() == 3 && N.getValueType() == MVT::i32 &&
1853 "Not an i64 shift!");
1854 unsigned ShiftOpLo = SelectExpr(N.getOperand(0));
1855 unsigned ShiftOpHi = SelectExpr(N.getOperand(1));
1856 unsigned SHReg = SelectExpr(N.getOperand(2));
1857 Tmp1 = MakeReg(MVT::i32);
1858 Tmp2 = MakeReg(MVT::i32);
1859 Tmp3 = MakeReg(MVT::i32);
1860 unsigned Tmp4 = MakeReg(MVT::i32);
1861 unsigned Tmp5 = MakeReg(MVT::i32);
1862 unsigned Tmp6 = MakeReg(MVT::i32);
1863 BuildMI(BB, PPC::SUBFIC, 2, Tmp1).addReg(SHReg).addSImm(32);
1864 if (ISD::SHL_PARTS == opcode) {
1865 BuildMI(BB, PPC::SLW, 2, Tmp2).addReg(ShiftOpHi).addReg(SHReg);
1866 BuildMI(BB, PPC::SRW, 2, Tmp3).addReg(ShiftOpLo).addReg(Tmp1);
1867 BuildMI(BB, PPC::OR, 2, Tmp4).addReg(Tmp2).addReg(Tmp3);
1868 BuildMI(BB, PPC::ADDI, 2, Tmp5).addReg(SHReg).addSImm(-32);
Nate Begemanfa554702005-04-03 22:13:27 +00001869 BuildMI(BB, PPC::SLW, 2, Tmp6).addReg(ShiftOpLo).addReg(Tmp5);
Nate Begeman27eeb002005-04-02 05:59:34 +00001870 BuildMI(BB, PPC::OR, 2, Result+1).addReg(Tmp4).addReg(Tmp6);
1871 BuildMI(BB, PPC::SLW, 2, Result).addReg(ShiftOpLo).addReg(SHReg);
1872 } else if (ISD::SRL_PARTS == opcode) {
1873 BuildMI(BB, PPC::SRW, 2, Tmp2).addReg(ShiftOpLo).addReg(SHReg);
1874 BuildMI(BB, PPC::SLW, 2, Tmp3).addReg(ShiftOpHi).addReg(Tmp1);
1875 BuildMI(BB, PPC::OR, 2, Tmp4).addReg(Tmp2).addReg(Tmp3);
1876 BuildMI(BB, PPC::ADDI, 2, Tmp5).addReg(SHReg).addSImm(-32);
1877 BuildMI(BB, PPC::SRW, 2, Tmp6).addReg(ShiftOpHi).addReg(Tmp5);
1878 BuildMI(BB, PPC::OR, 2, Result).addReg(Tmp4).addReg(Tmp6);
1879 BuildMI(BB, PPC::SRW, 2, Result+1).addReg(ShiftOpHi).addReg(SHReg);
1880 } else {
1881 MachineBasicBlock *TmpMBB = new MachineBasicBlock(BB->getBasicBlock());
1882 MachineBasicBlock *PhiMBB = new MachineBasicBlock(BB->getBasicBlock());
1883 MachineBasicBlock *OldMBB = BB;
1884 MachineFunction *F = BB->getParent();
1885 ilist<MachineBasicBlock>::iterator It = BB; ++It;
1886 F->getBasicBlockList().insert(It, TmpMBB);
1887 F->getBasicBlockList().insert(It, PhiMBB);
1888 BB->addSuccessor(TmpMBB);
1889 BB->addSuccessor(PhiMBB);
1890 BuildMI(BB, PPC::SRW, 2, Tmp2).addReg(ShiftOpLo).addReg(SHReg);
1891 BuildMI(BB, PPC::SLW, 2, Tmp3).addReg(ShiftOpHi).addReg(Tmp1);
1892 BuildMI(BB, PPC::OR, 2, Tmp4).addReg(Tmp2).addReg(Tmp3);
1893 BuildMI(BB, PPC::ADDICo, 2, Tmp5).addReg(SHReg).addSImm(-32);
1894 BuildMI(BB, PPC::SRAW, 2, Tmp6).addReg(ShiftOpHi).addReg(Tmp5);
1895 BuildMI(BB, PPC::SRAW, 2, Result+1).addReg(ShiftOpHi).addReg(SHReg);
1896 BuildMI(BB, PPC::BLE, 2).addReg(PPC::CR0).addMBB(PhiMBB);
1897 // Select correct least significant half if the shift amount > 32
1898 BB = TmpMBB;
1899 unsigned Tmp7 = MakeReg(MVT::i32);
1900 BuildMI(BB, PPC::OR, 2, Tmp7).addReg(Tmp6).addReg(Tmp6);
1901 TmpMBB->addSuccessor(PhiMBB);
1902 BB = PhiMBB;
1903 BuildMI(BB, PPC::PHI, 4, Result).addReg(Tmp4).addMBB(OldMBB)
1904 .addReg(Tmp7).addMBB(TmpMBB);
Nate Begemanca12a2b2005-03-28 22:28:37 +00001905 }
1906 return Result+N.ResNo;
1907 }
1908
Nate Begemana9795f82005-03-24 04:41:43 +00001909 case ISD::FP_TO_UINT:
Nate Begeman6b559972005-04-01 02:59:27 +00001910 case ISD::FP_TO_SINT: {
1911 bool U = (ISD::FP_TO_UINT == opcode);
1912 Tmp1 = SelectExpr(N.getOperand(0));
1913 if (!U) {
1914 Tmp2 = MakeReg(MVT::f64);
1915 BuildMI(BB, PPC::FCTIWZ, 1, Tmp2).addReg(Tmp1);
1916 int FrameIdx = BB->getParent()->getFrameInfo()->CreateStackObject(8, 8);
1917 addFrameReference(BuildMI(BB, PPC::STFD, 3).addReg(Tmp2), FrameIdx);
1918 addFrameReference(BuildMI(BB, PPC::LWZ, 2, Result), FrameIdx, 4);
1919 return Result;
1920 } else {
1921 unsigned Zero = getConstDouble(0.0);
1922 unsigned MaxInt = getConstDouble((1LL << 32) - 1);
1923 unsigned Border = getConstDouble(1LL << 31);
1924 unsigned UseZero = MakeReg(MVT::f64);
1925 unsigned UseMaxInt = MakeReg(MVT::f64);
1926 unsigned UseChoice = MakeReg(MVT::f64);
1927 unsigned TmpReg = MakeReg(MVT::f64);
1928 unsigned TmpReg2 = MakeReg(MVT::f64);
1929 unsigned ConvReg = MakeReg(MVT::f64);
1930 unsigned IntTmp = MakeReg(MVT::i32);
1931 unsigned XorReg = MakeReg(MVT::i32);
1932 MachineFunction *F = BB->getParent();
1933 int FrameIdx = F->getFrameInfo()->CreateStackObject(8, 8);
1934 // Update machine-CFG edges
1935 MachineBasicBlock *XorMBB = new MachineBasicBlock(BB->getBasicBlock());
1936 MachineBasicBlock *PhiMBB = new MachineBasicBlock(BB->getBasicBlock());
1937 MachineBasicBlock *OldMBB = BB;
1938 ilist<MachineBasicBlock>::iterator It = BB; ++It;
1939 F->getBasicBlockList().insert(It, XorMBB);
1940 F->getBasicBlockList().insert(It, PhiMBB);
1941 BB->addSuccessor(XorMBB);
1942 BB->addSuccessor(PhiMBB);
1943 // Convert from floating point to unsigned 32-bit value
1944 // Use 0 if incoming value is < 0.0
1945 BuildMI(BB, PPC::FSEL, 3, UseZero).addReg(Tmp1).addReg(Tmp1).addReg(Zero);
1946 // Use 2**32 - 1 if incoming value is >= 2**32
1947 BuildMI(BB, PPC::FSUB, 2, UseMaxInt).addReg(MaxInt).addReg(Tmp1);
1948 BuildMI(BB, PPC::FSEL, 3, UseChoice).addReg(UseMaxInt).addReg(UseZero)
1949 .addReg(MaxInt);
1950 // Subtract 2**31
1951 BuildMI(BB, PPC::FSUB, 2, TmpReg).addReg(UseChoice).addReg(Border);
1952 // Use difference if >= 2**31
1953 BuildMI(BB, PPC::FCMPU, 2, PPC::CR0).addReg(UseChoice).addReg(Border);
1954 BuildMI(BB, PPC::FSEL, 3, TmpReg2).addReg(TmpReg).addReg(TmpReg)
1955 .addReg(UseChoice);
1956 // Convert to integer
1957 BuildMI(BB, PPC::FCTIWZ, 1, ConvReg).addReg(TmpReg2);
1958 addFrameReference(BuildMI(BB, PPC::STFD, 3).addReg(ConvReg), FrameIdx);
1959 addFrameReference(BuildMI(BB, PPC::LWZ, 2, IntTmp), FrameIdx, 4);
1960 BuildMI(BB, PPC::BLT, 2).addReg(PPC::CR0).addMBB(PhiMBB);
1961 BuildMI(BB, PPC::B, 1).addMBB(XorMBB);
1962
1963 // XorMBB:
1964 // add 2**31 if input was >= 2**31
1965 BB = XorMBB;
1966 BuildMI(BB, PPC::XORIS, 2, XorReg).addReg(IntTmp).addImm(0x8000);
1967 XorMBB->addSuccessor(PhiMBB);
1968
1969 // PhiMBB:
1970 // DestReg = phi [ IntTmp, OldMBB ], [ XorReg, XorMBB ]
1971 BB = PhiMBB;
1972 BuildMI(BB, PPC::PHI, 4, Result).addReg(IntTmp).addMBB(OldMBB)
1973 .addReg(XorReg).addMBB(XorMBB);
1974 return Result;
1975 }
1976 assert(0 && "Should never get here");
1977 return 0;
1978 }
Nate Begemana9795f82005-03-24 04:41:43 +00001979
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001980 case ISD::SETCC:
Nate Begeman33162522005-03-29 21:54:38 +00001981 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Node)) {
Nate Begeman7e7fadd2005-04-07 20:30:01 +00001982 // We can codegen setcc op, 0 very efficiently compared to a conditional
1983 // branch. Check for that here.
1984 if (ConstantSDNode *CN =
1985 dyn_cast<ConstantSDNode>(SetCC->getOperand(1).Val)) {
1986 if (CN->getValue() == 0) {
1987 Tmp1 = SelectExpr(SetCC->getOperand(0));
1988 switch (SetCC->getCondition()) {
1989 default: assert(0 && "Unhandled SetCC condition"); abort();
1990 case ISD::SETEQ:
1991 case ISD::SETULE:
1992 Tmp2 = MakeReg(MVT::i32);
1993 BuildMI(BB, PPC::CNTLZW, 1, Tmp2).addReg(Tmp1);
1994 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp2).addImm(27)
1995 .addImm(5).addImm(31);
1996 break;
1997 case ISD::SETNE:
1998 case ISD::SETUGT:
1999 Tmp2 = MakeReg(MVT::i32);
2000 BuildMI(BB, PPC::ADDIC, 2, Tmp2).addReg(Tmp1).addSImm(-1);
2001 BuildMI(BB, PPC::SUBFE, 2, Result).addReg(Tmp2).addReg(Tmp1);
2002 break;
2003 case ISD::SETULT:
2004 BuildMI(BB, PPC::LI, 1, Result).addSImm(0);
2005 break;
2006 case ISD::SETLT:
2007 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(1)
2008 .addImm(31).addImm(31);
2009 break;
2010 case ISD::SETLE:
2011 Tmp2 = MakeReg(MVT::i32);
2012 Tmp3 = MakeReg(MVT::i32);
2013 BuildMI(BB, PPC::NEG, 2, Tmp2).addReg(Tmp1);
2014 BuildMI(BB, PPC::ORC, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
2015 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp3).addImm(1)
2016 .addImm(31).addImm(31);
2017 break;
2018 case ISD::SETGT:
2019 Tmp2 = MakeReg(MVT::i32);
2020 Tmp3 = MakeReg(MVT::i32);
2021 BuildMI(BB, PPC::NEG, 2, Tmp2).addReg(Tmp1);
2022 BuildMI(BB, PPC::ANDC, 2, Tmp3).addReg(Tmp2).addReg(Tmp1);
2023 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp3).addImm(1)
2024 .addImm(31).addImm(31);
2025 break;
2026 case ISD::SETUGE:
2027 BuildMI(BB, PPC::LI, 1, Result).addSImm(1);
2028 break;
2029 case ISD::SETGE:
2030 BuildMI(BB, PPC::RLWINM, 4, Tmp2).addReg(Tmp1).addImm(1)
2031 .addImm(31).addImm(31);
2032 BuildMI(BB, PPC::XORI, 2, Result).addReg(Tmp2).addImm(1);
2033 break;
2034 }
2035 return Result;
2036 }
2037 }
2038
Nate Begemandffcfcc2005-04-01 00:32:34 +00002039 Opc = SelectSetCR0(N);
Nate Begeman31318e42005-04-01 07:21:30 +00002040 unsigned TrueValue = MakeReg(MVT::i32);
2041 BuildMI(BB, PPC::LI, 1, TrueValue).addSImm(1);
2042 unsigned FalseValue = MakeReg(MVT::i32);
2043 BuildMI(BB, PPC::LI, 1, FalseValue).addSImm(0);
2044
Nate Begeman33162522005-03-29 21:54:38 +00002045 // Create an iterator with which to insert the MBB for copying the false
2046 // value and the MBB to hold the PHI instruction for this SetCC.
2047 MachineBasicBlock *thisMBB = BB;
2048 const BasicBlock *LLVM_BB = BB->getBasicBlock();
2049 ilist<MachineBasicBlock>::iterator It = BB;
2050 ++It;
2051
2052 // thisMBB:
2053 // ...
2054 // cmpTY cr0, r1, r2
2055 // %TrueValue = li 1
2056 // bCC sinkMBB
Nate Begeman33162522005-03-29 21:54:38 +00002057 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
2058 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
2059 BuildMI(BB, Opc, 2).addReg(PPC::CR0).addMBB(sinkMBB);
2060 MachineFunction *F = BB->getParent();
2061 F->getBasicBlockList().insert(It, copy0MBB);
2062 F->getBasicBlockList().insert(It, sinkMBB);
2063 // Update machine-CFG edges
2064 BB->addSuccessor(copy0MBB);
2065 BB->addSuccessor(sinkMBB);
2066
2067 // copy0MBB:
2068 // %FalseValue = li 0
2069 // fallthrough
2070 BB = copy0MBB;
Nate Begeman33162522005-03-29 21:54:38 +00002071 // Update machine-CFG edges
2072 BB->addSuccessor(sinkMBB);
2073
2074 // sinkMBB:
2075 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
2076 // ...
2077 BB = sinkMBB;
2078 BuildMI(BB, PPC::PHI, 4, Result).addReg(FalseValue)
2079 .addMBB(copy0MBB).addReg(TrueValue).addMBB(thisMBB);
2080 return Result;
2081 }
2082 assert(0 && "Is this legal?");
2083 return 0;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00002084
Nate Begeman74747862005-03-29 22:24:51 +00002085 case ISD::SELECT: {
Nate Begeman7e7fadd2005-04-07 20:30:01 +00002086 // We can codegen select (a < 0) ? b : 0 very efficiently compared to a
2087 // conditional branch. Check for that here.
2088 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(N.getOperand(0).Val)) {
2089 if (ConstantSDNode *CN =
2090 dyn_cast<ConstantSDNode>(SetCC->getOperand(1).Val)) {
2091 if (ConstantSDNode *CNF =
2092 dyn_cast<ConstantSDNode>(N.getOperand(2).Val)) {
2093 if (CN->getValue() == 0 && CNF->getValue() == 0 &&
2094 SetCC->getCondition() == ISD::SETLT) {
2095 Tmp1 = SelectExpr(N.getOperand(1)); // TRUE value
2096 Tmp2 = SelectExpr(SetCC->getOperand(0));
2097 Tmp3 = MakeReg(MVT::i32);
2098 BuildMI(BB, PPC::SRAWI, 2, Tmp3).addReg(Tmp2).addImm(31);
2099 BuildMI(BB, PPC::AND, 2, Result).addReg(Tmp1).addReg(Tmp3);
2100 return Result;
2101 }
2102 }
2103 }
2104 }
Chris Lattner30710192005-04-01 07:10:02 +00002105 unsigned TrueValue = SelectExpr(N.getOperand(1)); //Use if TRUE
2106 unsigned FalseValue = SelectExpr(N.getOperand(2)); //Use if FALSE
Nate Begeman6cb2e1b2005-04-01 08:57:43 +00002107 Opc = SelectSetCR0(N.getOperand(0));
Chris Lattner30710192005-04-01 07:10:02 +00002108
Nate Begeman74747862005-03-29 22:24:51 +00002109 // Create an iterator with which to insert the MBB for copying the false
2110 // value and the MBB to hold the PHI instruction for this SetCC.
2111 MachineBasicBlock *thisMBB = BB;
2112 const BasicBlock *LLVM_BB = BB->getBasicBlock();
2113 ilist<MachineBasicBlock>::iterator It = BB;
2114 ++It;
2115
2116 // thisMBB:
2117 // ...
2118 // TrueVal = ...
2119 // cmpTY cr0, r1, r2
2120 // bCC copy1MBB
2121 // fallthrough --> copy0MBB
Nate Begeman74747862005-03-29 22:24:51 +00002122 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
2123 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
Nate Begeman3e897162005-03-31 23:55:40 +00002124 BuildMI(BB, Opc, 2).addReg(PPC::CR0).addMBB(sinkMBB);
Nate Begeman74747862005-03-29 22:24:51 +00002125 MachineFunction *F = BB->getParent();
2126 F->getBasicBlockList().insert(It, copy0MBB);
2127 F->getBasicBlockList().insert(It, sinkMBB);
2128 // Update machine-CFG edges
2129 BB->addSuccessor(copy0MBB);
2130 BB->addSuccessor(sinkMBB);
2131
2132 // copy0MBB:
2133 // %FalseValue = ...
2134 // # fallthrough to sinkMBB
2135 BB = copy0MBB;
Nate Begeman74747862005-03-29 22:24:51 +00002136 // Update machine-CFG edges
2137 BB->addSuccessor(sinkMBB);
2138
2139 // sinkMBB:
2140 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
2141 // ...
2142 BB = sinkMBB;
2143 BuildMI(BB, PPC::PHI, 4, Result).addReg(FalseValue)
2144 .addMBB(copy0MBB).addReg(TrueValue).addMBB(thisMBB);
Nate Begeman74747862005-03-29 22:24:51 +00002145 return Result;
2146 }
Nate Begemana9795f82005-03-24 04:41:43 +00002147
2148 case ISD::Constant:
2149 switch (N.getValueType()) {
2150 default: assert(0 && "Cannot use constants of this type!");
2151 case MVT::i1:
2152 BuildMI(BB, PPC::LI, 1, Result)
2153 .addSImm(!cast<ConstantSDNode>(N)->isNullValue());
2154 break;
2155 case MVT::i32:
2156 {
2157 int v = (int)cast<ConstantSDNode>(N)->getSignExtended();
2158 if (v < 32768 && v >= -32768) {
2159 BuildMI(BB, PPC::LI, 1, Result).addSImm(v);
2160 } else {
Nate Begeman5e966612005-03-24 06:28:42 +00002161 Tmp1 = MakeReg(MVT::i32);
2162 BuildMI(BB, PPC::LIS, 1, Tmp1).addSImm(v >> 16);
2163 BuildMI(BB, PPC::ORI, 2, Result).addReg(Tmp1).addImm(v & 0xFFFF);
Nate Begemana9795f82005-03-24 04:41:43 +00002164 }
2165 }
2166 }
2167 return Result;
2168 }
2169
2170 return 0;
2171}
2172
2173void ISel::Select(SDOperand N) {
2174 unsigned Tmp1, Tmp2, Opc;
2175 unsigned opcode = N.getOpcode();
2176
2177 if (!ExprMap.insert(std::make_pair(N, 1)).second)
2178 return; // Already selected.
2179
2180 SDNode *Node = N.Val;
2181
2182 switch (Node->getOpcode()) {
2183 default:
2184 Node->dump(); std::cerr << "\n";
2185 assert(0 && "Node not handled yet!");
2186 case ISD::EntryToken: return; // Noop
2187 case ISD::TokenFactor:
2188 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
2189 Select(Node->getOperand(i));
2190 return;
2191 case ISD::ADJCALLSTACKDOWN:
2192 case ISD::ADJCALLSTACKUP:
2193 Select(N.getOperand(0));
2194 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
2195 Opc = N.getOpcode() == ISD::ADJCALLSTACKDOWN ? PPC::ADJCALLSTACKDOWN :
2196 PPC::ADJCALLSTACKUP;
2197 BuildMI(BB, Opc, 1).addImm(Tmp1);
2198 return;
2199 case ISD::BR: {
2200 MachineBasicBlock *Dest =
2201 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
Nate Begemana9795f82005-03-24 04:41:43 +00002202 Select(N.getOperand(0));
2203 BuildMI(BB, PPC::B, 1).addMBB(Dest);
2204 return;
2205 }
2206 case ISD::BRCOND:
Nate Begemancd08e4c2005-04-09 20:09:12 +00002207 case ISD::BRCONDTWOWAY:
Nate Begemana9795f82005-03-24 04:41:43 +00002208 SelectBranchCC(N);
2209 return;
2210 case ISD::CopyToReg:
2211 Select(N.getOperand(0));
2212 Tmp1 = SelectExpr(N.getOperand(1));
2213 Tmp2 = cast<RegSDNode>(N)->getReg();
2214
2215 if (Tmp1 != Tmp2) {
2216 if (N.getOperand(1).getValueType() == MVT::f64 ||
2217 N.getOperand(1).getValueType() == MVT::f32)
2218 BuildMI(BB, PPC::FMR, 1, Tmp2).addReg(Tmp1);
2219 else
2220 BuildMI(BB, PPC::OR, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
2221 }
2222 return;
2223 case ISD::ImplicitDef:
2224 Select(N.getOperand(0));
2225 BuildMI(BB, PPC::IMPLICIT_DEF, 0, cast<RegSDNode>(N)->getReg());
2226 return;
2227 case ISD::RET:
2228 switch (N.getNumOperands()) {
2229 default:
2230 assert(0 && "Unknown return instruction!");
2231 case 3:
2232 assert(N.getOperand(1).getValueType() == MVT::i32 &&
2233 N.getOperand(2).getValueType() == MVT::i32 &&
2234 "Unknown two-register value!");
2235 Select(N.getOperand(0));
2236 Tmp1 = SelectExpr(N.getOperand(1));
2237 Tmp2 = SelectExpr(N.getOperand(2));
Nate Begeman27523a12005-04-02 00:42:16 +00002238 BuildMI(BB, PPC::OR, 2, PPC::R3).addReg(Tmp2).addReg(Tmp2);
2239 BuildMI(BB, PPC::OR, 2, PPC::R4).addReg(Tmp1).addReg(Tmp1);
Nate Begemana9795f82005-03-24 04:41:43 +00002240 break;
2241 case 2:
2242 Select(N.getOperand(0));
2243 Tmp1 = SelectExpr(N.getOperand(1));
2244 switch (N.getOperand(1).getValueType()) {
2245 default:
2246 assert(0 && "Unknown return type!");
2247 case MVT::f64:
2248 case MVT::f32:
2249 BuildMI(BB, PPC::FMR, 1, PPC::F1).addReg(Tmp1);
2250 break;
2251 case MVT::i32:
2252 BuildMI(BB, PPC::OR, 2, PPC::R3).addReg(Tmp1).addReg(Tmp1);
2253 break;
2254 }
Nate Begeman9e3e1b52005-03-24 23:35:30 +00002255 case 1:
2256 Select(N.getOperand(0));
2257 break;
Nate Begemana9795f82005-03-24 04:41:43 +00002258 }
2259 BuildMI(BB, PPC::BLR, 0); // Just emit a 'ret' instruction
2260 return;
Nate Begemana9795f82005-03-24 04:41:43 +00002261 case ISD::TRUNCSTORE:
2262 case ISD::STORE:
2263 {
2264 SDOperand Chain = N.getOperand(0);
2265 SDOperand Value = N.getOperand(1);
2266 SDOperand Address = N.getOperand(2);
2267 Select(Chain);
2268
2269 Tmp1 = SelectExpr(Value); //value
2270
2271 if (opcode == ISD::STORE) {
2272 switch(Value.getValueType()) {
2273 default: assert(0 && "unknown Type in store");
2274 case MVT::i32: Opc = PPC::STW; break;
2275 case MVT::f64: Opc = PPC::STFD; break;
2276 case MVT::f32: Opc = PPC::STFS; break;
2277 }
2278 } else { //ISD::TRUNCSTORE
2279 switch(cast<MVTSDNode>(Node)->getExtraValueType()) {
2280 default: assert(0 && "unknown Type in store");
Nate Begeman7e7fadd2005-04-07 20:30:01 +00002281 case MVT::i1:
Nate Begemana9795f82005-03-24 04:41:43 +00002282 case MVT::i8: Opc = PPC::STB; break;
2283 case MVT::i16: Opc = PPC::STH; break;
2284 }
2285 }
2286
Nate Begemana7e11a42005-04-01 05:57:17 +00002287 if(Address.getOpcode() == ISD::FrameIndex)
Nate Begemana9795f82005-03-24 04:41:43 +00002288 {
Nate Begeman58f718c2005-03-30 02:23:08 +00002289 Tmp2 = cast<FrameIndexSDNode>(Address)->getIndex();
2290 addFrameReference(BuildMI(BB, Opc, 3).addReg(Tmp1), (int)Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +00002291 }
2292 else
2293 {
2294 int offset;
Nate Begeman04730362005-04-01 04:45:11 +00002295 bool idx = SelectAddr(Address, Tmp2, offset);
2296 if (idx) {
2297 Opc = IndexedOpForOp(Opc);
2298 BuildMI(BB, Opc, 3).addReg(Tmp1).addReg(Tmp2).addReg(offset);
2299 } else {
2300 BuildMI(BB, Opc, 3).addReg(Tmp1).addImm(offset).addReg(Tmp2);
2301 }
Nate Begemana9795f82005-03-24 04:41:43 +00002302 }
2303 return;
2304 }
2305 case ISD::EXTLOAD:
2306 case ISD::SEXTLOAD:
2307 case ISD::ZEXTLOAD:
2308 case ISD::LOAD:
2309 case ISD::CopyFromReg:
2310 case ISD::CALL:
2311 case ISD::DYNAMIC_STACKALLOC:
2312 ExprMap.erase(N);
2313 SelectExpr(N);
2314 return;
2315 }
2316 assert(0 && "Should not be reached!");
2317}
2318
2319
2320/// createPPC32PatternInstructionSelector - This pass converts an LLVM function
2321/// into a machine code representation using pattern matching and a machine
2322/// description file.
2323///
2324FunctionPass *llvm::createPPC32ISelPattern(TargetMachine &TM) {
2325 return new ISel(TM);
Chris Lattner246fa632005-03-24 06:16:18 +00002326}
2327