blob: 8430970e67d544e519f8f6fca0fd3878b45b3f51 [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
Chris Lattnercbd06fc2005-04-07 19:41:49 +000064 setSetCCResultContents(ZeroOrOneSetCCResult);
Nate Begeman3e897162005-03-31 23:55:40 +000065 addLegalFPImmediate(+0.0); // Necessary for FSEL
66 addLegalFPImmediate(-0.0); //
67
Nate Begemana9795f82005-03-24 04:41:43 +000068 computeRegisterProperties();
69 }
70
71 /// LowerArguments - This hook must be implemented to indicate how we should
72 /// lower the arguments for the specified function, into the specified DAG.
73 virtual std::vector<SDOperand>
74 LowerArguments(Function &F, SelectionDAG &DAG);
75
76 /// LowerCallTo - This hook lowers an abstract call to a function into an
77 /// actual call.
78 virtual std::pair<SDOperand, SDOperand>
Nate Begeman307e7442005-03-26 01:28:53 +000079 LowerCallTo(SDOperand Chain, const Type *RetTy, bool isVarArg,
80 SDOperand Callee, ArgListTy &Args, SelectionDAG &DAG);
Nate Begemana9795f82005-03-24 04:41:43 +000081
82 virtual std::pair<SDOperand, SDOperand>
83 LowerVAStart(SDOperand Chain, SelectionDAG &DAG);
84
85 virtual std::pair<SDOperand,SDOperand>
86 LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
87 const Type *ArgTy, SelectionDAG &DAG);
88
89 virtual std::pair<SDOperand, SDOperand>
90 LowerFrameReturnAddress(bool isFrameAddr, SDOperand Chain, unsigned Depth,
91 SelectionDAG &DAG);
92 };
93}
94
95
96std::vector<SDOperand>
97PPC32TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
98 //
99 // add beautiful description of PPC stack frame format, or at least some docs
100 //
101 MachineFunction &MF = DAG.getMachineFunction();
102 MachineFrameInfo *MFI = MF.getFrameInfo();
103 MachineBasicBlock& BB = MF.front();
104 std::vector<SDOperand> ArgValues;
105
106 // Due to the rather complicated nature of the PowerPC ABI, rather than a
107 // fixed size array of physical args, for the sake of simplicity let the STL
108 // handle tracking them for us.
109 std::vector<unsigned> argVR, argPR, argOp;
110 unsigned ArgOffset = 24;
111 unsigned GPR_remaining = 8;
112 unsigned FPR_remaining = 13;
113 unsigned GPR_idx = 0, FPR_idx = 0;
114 static const unsigned GPR[] = {
115 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
116 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
117 };
118 static const unsigned FPR[] = {
119 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
120 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
121 };
122
123 // Add DAG nodes to load the arguments... On entry to a function on PPC,
124 // the arguments start at offset 24, although they are likely to be passed
125 // in registers.
126 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
127 SDOperand newroot, argt;
128 unsigned ObjSize;
129 bool needsLoad = false;
Nate Begemancd08e4c2005-04-09 20:09:12 +0000130 bool ArgLive = !I->use_empty();
Nate Begemana9795f82005-03-24 04:41:43 +0000131 MVT::ValueType ObjectVT = getValueType(I->getType());
132
133 switch (ObjectVT) {
134 default: assert(0 && "Unhandled argument type!");
135 case MVT::i1:
136 case MVT::i8:
137 case MVT::i16:
138 case MVT::i32:
139 ObjSize = 4;
Nate Begemancd08e4c2005-04-09 20:09:12 +0000140 if (!ArgLive) break;
Nate Begemana9795f82005-03-24 04:41:43 +0000141 if (GPR_remaining > 0) {
Nate Begemancd08e4c2005-04-09 20:09:12 +0000142 MF.addLiveIn(GPR[GPR_idx]);
Nate Begemanf70b5762005-03-28 23:08:54 +0000143 argt = newroot = DAG.getCopyFromReg(GPR[GPR_idx], MVT::i32,
144 DAG.getRoot());
Nate Begemana9795f82005-03-24 04:41:43 +0000145 if (ObjectVT != MVT::i32)
146 argt = DAG.getNode(ISD::TRUNCATE, ObjectVT, newroot);
Nate Begemana9795f82005-03-24 04:41:43 +0000147 } else {
148 needsLoad = true;
149 }
150 break;
Nate Begemanf7e43382005-03-26 07:46:36 +0000151 case MVT::i64: ObjSize = 8;
Nate Begemancd08e4c2005-04-09 20:09:12 +0000152 if (!ArgLive) break;
Nate Begemanc5b1cd22005-04-10 05:53:14 +0000153 if (GPR_remaining > 0) {
154 SDOperand argHi, argLo;
Nate Begemancd08e4c2005-04-09 20:09:12 +0000155 MF.addLiveIn(GPR[GPR_idx]);
Nate Begemanc5b1cd22005-04-10 05:53:14 +0000156 argHi = DAG.getCopyFromReg(GPR[GPR_idx], MVT::i32, DAG.getRoot());
157 // If we have two or more remaining argument registers, then both halves
158 // of the i64 can be sourced from there. Otherwise, the lower half will
159 // have to come off the stack. This can happen when an i64 is preceded
160 // by 28 bytes of arguments.
161 if (GPR_remaining > 1) {
162 MF.addLiveIn(GPR[GPR_idx+1]);
163 argLo = DAG.getCopyFromReg(GPR[GPR_idx+1], MVT::i32, argHi);
164 } else {
165 int FI = MFI->CreateFixedObject(4, ArgOffset+4);
166 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
167 argLo = DAG.getLoad(MVT::i32, DAG.getEntryNode(), FIN);
168 }
Nate Begemanca12a2b2005-03-28 22:28:37 +0000169 // Build the outgoing arg thingy
Nate Begemanf70b5762005-03-28 23:08:54 +0000170 argt = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, argLo, argHi);
171 newroot = argLo;
Nate Begemana9795f82005-03-24 04:41:43 +0000172 } else {
173 needsLoad = true;
174 }
175 break;
Nate Begemancd08e4c2005-04-09 20:09:12 +0000176 case MVT::f32:
177 case MVT::f64:
178 ObjSize = (ObjectVT == MVT::f64) ? 8 : 4;
179 if (!ArgLive) break;
Nate Begemana9795f82005-03-24 04:41:43 +0000180 if (FPR_remaining > 0) {
Nate Begemancd08e4c2005-04-09 20:09:12 +0000181 MF.addLiveIn(FPR[FPR_idx]);
Nate Begemanf70b5762005-03-28 23:08:54 +0000182 argt = newroot = DAG.getCopyFromReg(FPR[FPR_idx], ObjectVT,
183 DAG.getRoot());
Nate Begemana9795f82005-03-24 04:41:43 +0000184 --FPR_remaining;
185 ++FPR_idx;
186 } else {
187 needsLoad = true;
188 }
189 break;
190 }
191
192 // We need to load the argument to a virtual register if we determined above
193 // that we ran out of physical registers of the appropriate type
194 if (needsLoad) {
Nate Begemane5846682005-04-04 06:52:38 +0000195 unsigned SubregOffset = 0;
Nate Begemanc3e2db42005-04-04 09:09:00 +0000196 if (ObjectVT == MVT::i8 || ObjectVT == MVT::i1) SubregOffset = 3;
Nate Begemane5846682005-04-04 06:52:38 +0000197 if (ObjectVT == MVT::i16) SubregOffset = 2;
Nate Begemana9795f82005-03-24 04:41:43 +0000198 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
199 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
Nate Begemane5846682005-04-04 06:52:38 +0000200 FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN,
201 DAG.getConstant(SubregOffset, MVT::i32));
Nate Begemana9795f82005-03-24 04:41:43 +0000202 argt = newroot = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN);
203 }
204
205 // Every 4 bytes of argument space consumes one of the GPRs available for
206 // argument passing.
207 if (GPR_remaining > 0) {
208 unsigned delta = (GPR_remaining > 1 && ObjSize == 8) ? 2 : 1;
209 GPR_remaining -= delta;
210 GPR_idx += delta;
211 }
212 ArgOffset += ObjSize;
Chris Lattner91277ea2005-04-09 21:23:24 +0000213 if (newroot.Val)
214 DAG.setRoot(newroot.getValue(1));
Nate Begemana9795f82005-03-24 04:41:43 +0000215
Nate Begemana9795f82005-03-24 04:41:43 +0000216 ArgValues.push_back(argt);
217 }
218
Nate Begemana9795f82005-03-24 04:41:43 +0000219 // If the function takes variable number of arguments, make a frame index for
220 // the start of the first vararg value... for expansion of llvm.va_start.
Nate Begemanfa554702005-04-03 22:13:27 +0000221 if (F.isVarArg()) {
Nate Begemana9795f82005-03-24 04:41:43 +0000222 VarArgsFrameIndex = MFI->CreateFixedObject(4, ArgOffset);
Nate Begemanfa554702005-04-03 22:13:27 +0000223 SDOperand FIN = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
Nate Begeman6644d4c2005-04-03 23:11:17 +0000224 // If this function is vararg, store any remaining integer argument regs
225 // to their spots on the stack so that they may be loaded by deferencing the
226 // result of va_next.
227 std::vector<SDOperand> MemOps;
228 for (; GPR_remaining > 0; --GPR_remaining, ++GPR_idx) {
Nate Begemancd08e4c2005-04-09 20:09:12 +0000229 MF.addLiveIn(GPR[GPR_idx]);
Nate Begeman6644d4c2005-04-03 23:11:17 +0000230 SDOperand Val = DAG.getCopyFromReg(GPR[GPR_idx], MVT::i32, DAG.getRoot());
231 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Val.getValue(1),
232 Val, FIN);
233 MemOps.push_back(Store);
234 // Increment the address by four for the next argument to store
235 SDOperand PtrOff = DAG.getConstant(4, getPointerTy());
236 FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN, PtrOff);
237 }
238 DAG.setRoot(DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps));
Nate Begemanfa554702005-04-03 22:13:27 +0000239 }
Nate Begemana9795f82005-03-24 04:41:43 +0000240
Nate Begemancd08e4c2005-04-09 20:09:12 +0000241 // Finally, inform the code generator which regs we return values in.
242 switch (getValueType(F.getReturnType())) {
243 default: assert(0 && "Unknown type!");
244 case MVT::isVoid: break;
245 case MVT::i1:
246 case MVT::i8:
247 case MVT::i16:
248 case MVT::i32:
249 MF.addLiveOut(PPC::R3);
250 break;
251 case MVT::i64:
252 MF.addLiveOut(PPC::R3);
253 MF.addLiveOut(PPC::R4);
254 break;
255 case MVT::f32:
256 case MVT::f64:
257 MF.addLiveOut(PPC::F1);
258 break;
259 }
260
Nate Begemana9795f82005-03-24 04:41:43 +0000261 return ArgValues;
262}
263
264std::pair<SDOperand, SDOperand>
265PPC32TargetLowering::LowerCallTo(SDOperand Chain,
Nate Begeman307e7442005-03-26 01:28:53 +0000266 const Type *RetTy, bool isVarArg,
267 SDOperand Callee, ArgListTy &Args, SelectionDAG &DAG) {
268 // args_to_use will accumulate outgoing args for the ISD::CALL case in
269 // SelectExpr to use to put the arguments in the appropriate registers.
Nate Begemana9795f82005-03-24 04:41:43 +0000270 std::vector<SDOperand> args_to_use;
Nate Begeman307e7442005-03-26 01:28:53 +0000271
272 // Count how many bytes are to be pushed on the stack, including the linkage
273 // area, and parameter passing area.
274 unsigned NumBytes = 24;
275
276 if (Args.empty()) {
Nate Begemana7e11a42005-04-01 05:57:17 +0000277 Chain = DAG.getNode(ISD::ADJCALLSTACKDOWN, MVT::Other, Chain,
278 DAG.getConstant(NumBytes, getPointerTy()));
Nate Begeman307e7442005-03-26 01:28:53 +0000279 } else {
280 for (unsigned i = 0, e = Args.size(); i != e; ++i)
281 switch (getValueType(Args[i].second)) {
282 default: assert(0 && "Unknown value type!");
283 case MVT::i1:
284 case MVT::i8:
285 case MVT::i16:
286 case MVT::i32:
287 case MVT::f32:
288 NumBytes += 4;
289 break;
290 case MVT::i64:
291 case MVT::f64:
292 NumBytes += 8;
293 break;
294 }
295
296 // Just to be safe, we'll always reserve the full 24 bytes of linkage area
297 // plus 32 bytes of argument space in case any called code gets funky on us.
298 if (NumBytes < 56) NumBytes = 56;
299
300 // Adjust the stack pointer for the new arguments...
301 // These operations are automatically eliminated by the prolog/epilog pass
302 Chain = DAG.getNode(ISD::ADJCALLSTACKDOWN, MVT::Other, Chain,
303 DAG.getConstant(NumBytes, getPointerTy()));
304
305 // Set up a copy of the stack pointer for use loading and storing any
306 // arguments that may not fit in the registers available for argument
307 // passing.
308 SDOperand StackPtr = DAG.getCopyFromReg(PPC::R1, MVT::i32,
309 DAG.getEntryNode());
310
311 // Figure out which arguments are going to go in registers, and which in
312 // memory. Also, if this is a vararg function, floating point operations
313 // must be stored to our stack, and loaded into integer regs as well, if
314 // any integer regs are available for argument passing.
315 unsigned ArgOffset = 24;
316 unsigned GPR_remaining = 8;
317 unsigned FPR_remaining = 13;
Nate Begeman74d73452005-03-31 00:15:26 +0000318
319 std::vector<SDOperand> MemOps;
Nate Begeman307e7442005-03-26 01:28:53 +0000320 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
321 // PtrOff will be used to store the current argument to the stack if a
322 // register cannot be found for it.
323 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
324 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
Nate Begemanf7e43382005-03-26 07:46:36 +0000325 MVT::ValueType ArgVT = getValueType(Args[i].second);
Nate Begeman307e7442005-03-26 01:28:53 +0000326
Nate Begemanf7e43382005-03-26 07:46:36 +0000327 switch (ArgVT) {
Nate Begeman307e7442005-03-26 01:28:53 +0000328 default: assert(0 && "Unexpected ValueType for argument!");
329 case MVT::i1:
330 case MVT::i8:
331 case MVT::i16:
332 // Promote the integer to 32 bits. If the input type is signed use a
333 // sign extend, otherwise use a zero extend.
334 if (Args[i].second->isSigned())
335 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
336 else
337 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
338 // FALL THROUGH
339 case MVT::i32:
340 if (GPR_remaining > 0) {
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000341 args_to_use.push_back(Args[i].first);
Nate Begeman307e7442005-03-26 01:28:53 +0000342 --GPR_remaining;
343 } else {
Nate Begeman74d73452005-03-31 00:15:26 +0000344 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
345 Args[i].first, PtrOff));
Nate Begeman307e7442005-03-26 01:28:53 +0000346 }
347 ArgOffset += 4;
348 break;
349 case MVT::i64:
Nate Begemanf7e43382005-03-26 07:46:36 +0000350 // If we have one free GPR left, we can place the upper half of the i64
351 // in it, and store the other half to the stack. If we have two or more
352 // free GPRs, then we can pass both halves of the i64 in registers.
353 if (GPR_remaining > 0) {
Nate Begemanf2622612005-03-26 02:17:46 +0000354 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
355 Args[i].first, DAG.getConstant(1, MVT::i32));
356 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
357 Args[i].first, DAG.getConstant(0, MVT::i32));
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000358 args_to_use.push_back(Hi);
Nate Begeman74d73452005-03-31 00:15:26 +0000359 --GPR_remaining;
Nate Begeman74d73452005-03-31 00:15:26 +0000360 if (GPR_remaining > 0) {
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000361 args_to_use.push_back(Lo);
Nate Begeman74d73452005-03-31 00:15:26 +0000362 --GPR_remaining;
Nate Begemanf7e43382005-03-26 07:46:36 +0000363 } else {
364 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
365 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
Nate Begeman74d73452005-03-31 00:15:26 +0000366 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
367 Lo, PtrOff));
Nate Begemanf7e43382005-03-26 07:46:36 +0000368 }
Nate Begeman307e7442005-03-26 01:28:53 +0000369 } else {
Nate Begeman74d73452005-03-31 00:15:26 +0000370 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
371 Args[i].first, PtrOff));
Nate Begeman307e7442005-03-26 01:28:53 +0000372 }
373 ArgOffset += 8;
374 break;
375 case MVT::f32:
Nate Begeman307e7442005-03-26 01:28:53 +0000376 case MVT::f64:
Nate Begemanf7e43382005-03-26 07:46:36 +0000377 if (FPR_remaining > 0) {
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000378 args_to_use.push_back(Args[i].first);
379 --FPR_remaining;
Nate Begemanf7e43382005-03-26 07:46:36 +0000380 if (isVarArg) {
Nate Begeman96fc6812005-03-31 02:05:53 +0000381 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Chain,
382 Args[i].first, PtrOff);
383 MemOps.push_back(Store);
Nate Begeman74d73452005-03-31 00:15:26 +0000384 // Float varargs are always shadowed in available integer registers
385 if (GPR_remaining > 0) {
Nate Begeman96fc6812005-03-31 02:05:53 +0000386 SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff);
Nate Begeman74d73452005-03-31 00:15:26 +0000387 MemOps.push_back(Load);
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000388 args_to_use.push_back(Load);
389 --GPR_remaining;
Nate Begeman74d73452005-03-31 00:15:26 +0000390 }
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000391 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
Nate Begeman74d73452005-03-31 00:15:26 +0000392 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
393 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
Nate Begeman96fc6812005-03-31 02:05:53 +0000394 SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff);
Nate Begeman74d73452005-03-31 00:15:26 +0000395 MemOps.push_back(Load);
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000396 args_to_use.push_back(Load);
397 --GPR_remaining;
Nate Begeman74d73452005-03-31 00:15:26 +0000398 }
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000399 } else {
400 // If we have any FPRs remaining, we may also have GPRs remaining.
401 // Args passed in FPRs consume either 1 (f32) or 2 (f64) available
402 // GPRs.
403 if (GPR_remaining > 0) {
404 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
405 --GPR_remaining;
406 }
407 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
408 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
409 --GPR_remaining;
410 }
Nate Begeman74d73452005-03-31 00:15:26 +0000411 }
Nate Begeman307e7442005-03-26 01:28:53 +0000412 } else {
Nate Begeman74d73452005-03-31 00:15:26 +0000413 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
414 Args[i].first, PtrOff));
Nate Begeman307e7442005-03-26 01:28:53 +0000415 }
Nate Begemanf7e43382005-03-26 07:46:36 +0000416 ArgOffset += (ArgVT == MVT::f32) ? 4 : 8;
Nate Begeman307e7442005-03-26 01:28:53 +0000417 break;
418 }
Nate Begemana9795f82005-03-24 04:41:43 +0000419 }
Nate Begeman74d73452005-03-31 00:15:26 +0000420 if (!MemOps.empty())
421 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps);
Nate Begemana9795f82005-03-24 04:41:43 +0000422 }
423
424 std::vector<MVT::ValueType> RetVals;
425 MVT::ValueType RetTyVT = getValueType(RetTy);
426 if (RetTyVT != MVT::isVoid)
427 RetVals.push_back(RetTyVT);
428 RetVals.push_back(MVT::Other);
429
430 SDOperand TheCall = SDOperand(DAG.getCall(RetVals,
431 Chain, Callee, args_to_use), 0);
432 Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
433 Chain = DAG.getNode(ISD::ADJCALLSTACKUP, MVT::Other, Chain,
434 DAG.getConstant(NumBytes, getPointerTy()));
435 return std::make_pair(TheCall, Chain);
436}
437
438std::pair<SDOperand, SDOperand>
439PPC32TargetLowering::LowerVAStart(SDOperand Chain, SelectionDAG &DAG) {
440 //vastart just returns the address of the VarArgsFrameIndex slot.
441 return std::make_pair(DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32), Chain);
442}
443
444std::pair<SDOperand,SDOperand> PPC32TargetLowering::
445LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
446 const Type *ArgTy, SelectionDAG &DAG) {
Nate Begemanc7b09f12005-03-25 08:34:25 +0000447 MVT::ValueType ArgVT = getValueType(ArgTy);
448 SDOperand Result;
449 if (!isVANext) {
450 Result = DAG.getLoad(ArgVT, DAG.getEntryNode(), VAList);
451 } else {
452 unsigned Amt;
453 if (ArgVT == MVT::i32 || ArgVT == MVT::f32)
454 Amt = 4;
455 else {
456 assert((ArgVT == MVT::i64 || ArgVT == MVT::f64) &&
457 "Other types should have been promoted for varargs!");
458 Amt = 8;
459 }
460 Result = DAG.getNode(ISD::ADD, VAList.getValueType(), VAList,
461 DAG.getConstant(Amt, VAList.getValueType()));
462 }
463 return std::make_pair(Result, Chain);
Nate Begemana9795f82005-03-24 04:41:43 +0000464}
465
466
467std::pair<SDOperand, SDOperand> PPC32TargetLowering::
468LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
469 SelectionDAG &DAG) {
Nate Begeman01d05262005-03-30 01:45:43 +0000470 assert(0 && "LowerFrameReturnAddress unimplemented");
Nate Begemana9795f82005-03-24 04:41:43 +0000471 abort();
472}
473
474namespace {
Nate Begemanc7bd4822005-04-11 06:34:10 +0000475Statistic<>Recorded("ppc-codegen", "Number of recording ops emitted");
Nate Begeman93075ec2005-04-04 23:40:36 +0000476Statistic<>FusedFP("ppc-codegen", "Number of fused fp operations");
Nate Begeman7bfba7d2005-04-14 09:45:08 +0000477Statistic<>MultiBranch("ppc-codegen", "Number of setcc logical ops collapsed");
Nate Begemana9795f82005-03-24 04:41:43 +0000478//===--------------------------------------------------------------------===//
479/// ISel - PPC32 specific code to select PPC32 machine instructions for
480/// SelectionDAG operations.
481//===--------------------------------------------------------------------===//
482class ISel : public SelectionDAGISel {
Nate Begemana9795f82005-03-24 04:41:43 +0000483 PPC32TargetLowering PPC32Lowering;
Nate Begeman815d6da2005-04-06 00:25:27 +0000484 SelectionDAG *ISelDAG; // Hack to support us having a dag->dag transform
485 // for sdiv and udiv until it is put into the future
486 // dag combiner.
Nate Begemana9795f82005-03-24 04:41:43 +0000487
488 /// ExprMap - As shared expressions are codegen'd, we keep track of which
489 /// vreg the value is produced in, so we only emit one copy of each compiled
490 /// tree.
491 std::map<SDOperand, unsigned> ExprMap;
Nate Begemanc7b09f12005-03-25 08:34:25 +0000492
493 unsigned GlobalBaseReg;
494 bool GlobalBaseInitialized;
Nate Begemanc7bd4822005-04-11 06:34:10 +0000495 bool RecordSuccess;
Nate Begemana9795f82005-03-24 04:41:43 +0000496public:
Nate Begeman815d6da2005-04-06 00:25:27 +0000497 ISel(TargetMachine &TM) : SelectionDAGISel(PPC32Lowering), PPC32Lowering(TM),
498 ISelDAG(0) {}
Nate Begemana9795f82005-03-24 04:41:43 +0000499
Nate Begemanc7b09f12005-03-25 08:34:25 +0000500 /// runOnFunction - Override this function in order to reset our per-function
501 /// variables.
502 virtual bool runOnFunction(Function &Fn) {
503 // Make sure we re-emit a set of the global base reg if necessary
504 GlobalBaseInitialized = false;
505 return SelectionDAGISel::runOnFunction(Fn);
506 }
507
Nate Begemana9795f82005-03-24 04:41:43 +0000508 /// InstructionSelectBasicBlock - This callback is invoked by
509 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
510 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG) {
511 DEBUG(BB->dump());
512 // Codegen the basic block.
Nate Begeman815d6da2005-04-06 00:25:27 +0000513 ISelDAG = &DAG;
Nate Begemana9795f82005-03-24 04:41:43 +0000514 Select(DAG.getRoot());
515
516 // Clear state used for selection.
517 ExprMap.clear();
Nate Begeman815d6da2005-04-06 00:25:27 +0000518 ISelDAG = 0;
Nate Begemana9795f82005-03-24 04:41:43 +0000519 }
Nate Begeman815d6da2005-04-06 00:25:27 +0000520
521 // dag -> dag expanders for integer divide by constant
522 SDOperand BuildSDIVSequence(SDOperand N);
523 SDOperand BuildUDIVSequence(SDOperand N);
Nate Begemana9795f82005-03-24 04:41:43 +0000524
Nate Begemandffcfcc2005-04-01 00:32:34 +0000525 unsigned getGlobalBaseReg();
Nate Begeman6b559972005-04-01 02:59:27 +0000526 unsigned getConstDouble(double floatVal, unsigned Result);
Nate Begeman7ddecb42005-04-06 23:51:40 +0000527 bool SelectBitfieldInsert(SDOperand OR, unsigned Result);
Nate Begeman3664cef2005-04-13 22:14:14 +0000528 unsigned FoldIfWideZeroExtend(SDOperand N);
Nate Begeman1b7f7fb2005-04-13 23:15:44 +0000529 unsigned SelectCC(SDOperand CC, unsigned &Opc);
Nate Begemanc7bd4822005-04-11 06:34:10 +0000530 unsigned SelectExpr(SDOperand N, bool Recording=false);
Nate Begemana9795f82005-03-24 04:41:43 +0000531 unsigned SelectExprFP(SDOperand N, unsigned Result);
532 void Select(SDOperand N);
533
Nate Begeman04730362005-04-01 04:45:11 +0000534 bool SelectAddr(SDOperand N, unsigned& Reg, int& offset);
Nate Begemana9795f82005-03-24 04:41:43 +0000535 void SelectBranchCC(SDOperand N);
536};
537
Nate Begeman80196b12005-04-05 00:15:08 +0000538/// ExactLog2 - This function solves for (Val == 1 << (N-1)) and returns N. It
539/// returns zero when the input is not exactly a power of two.
540static unsigned ExactLog2(unsigned Val) {
541 if (Val == 0 || (Val & (Val-1))) return 0;
542 unsigned Count = 0;
543 while (Val != 1) {
544 Val >>= 1;
545 ++Count;
546 }
547 return Count;
548}
549
Nate Begeman7ddecb42005-04-06 23:51:40 +0000550// IsRunOfOnes - returns true if Val consists of one contiguous run of 1's with
551// any number of 0's on either side. the 1's are allowed to wrap from LSB to
552// MSB. so 0x000FFF0, 0x0000FFFF, and 0xFF0000FF are all runs. 0x0F0F0000 is
553// not, since all 1's are not contiguous.
554static bool IsRunOfOnes(unsigned Val, unsigned &MB, unsigned &ME) {
555 bool isRun = true;
556 MB = 0;
557 ME = 0;
558
559 // look for first set bit
560 int i = 0;
561 for (; i < 32; i++) {
562 if ((Val & (1 << (31 - i))) != 0) {
563 MB = i;
564 ME = i;
565 break;
566 }
567 }
568
569 // look for last set bit
570 for (; i < 32; i++) {
571 if ((Val & (1 << (31 - i))) == 0)
572 break;
573 ME = i;
574 }
575
576 // look for next set bit
577 for (; i < 32; i++) {
578 if ((Val & (1 << (31 - i))) != 0)
579 break;
580 }
581
582 // if we exhausted all the bits, we found a match at this point for 0*1*0*
583 if (i == 32)
584 return true;
585
586 // since we just encountered more 1's, if it doesn't wrap around to the
587 // most significant bit of the word, then we did not find a match to 1*0*1* so
588 // exit.
589 if (MB != 0)
590 return false;
591
592 // look for last set bit
593 for (MB = i; i < 32; i++) {
594 if ((Val & (1 << (31 - i))) == 0)
595 break;
596 }
597
598 // if we exhausted all the bits, then we found a match for 1*0*1*, otherwise,
599 // the value is not a run of ones.
600 if (i == 32)
601 return true;
602 return false;
603}
604
Nate Begeman439b4442005-04-05 04:22:58 +0000605/// getImmediateForOpcode - This method returns a value indicating whether
Nate Begemana9795f82005-03-24 04:41:43 +0000606/// the ConstantSDNode N can be used as an immediate to Opcode. The return
607/// values are either 0, 1 or 2. 0 indicates that either N is not a
Nate Begeman9f833d32005-04-12 00:10:02 +0000608/// ConstantSDNode, or is not suitable for use by that opcode.
609/// Return value codes for turning into an enum someday:
610/// 1: constant may be used in normal immediate form.
611/// 2: constant may be used in shifted immediate form.
612/// 3: log base 2 of the constant may be used.
613/// 4: constant is suitable for integer division conversion
614/// 5: constant is a bitfield mask
Nate Begemana9795f82005-03-24 04:41:43 +0000615///
Nate Begeman439b4442005-04-05 04:22:58 +0000616static unsigned getImmediateForOpcode(SDOperand N, unsigned Opcode,
617 unsigned& Imm, bool U = false) {
Nate Begemana9795f82005-03-24 04:41:43 +0000618 if (N.getOpcode() != ISD::Constant) return 0;
619
620 int v = (int)cast<ConstantSDNode>(N)->getSignExtended();
621
622 switch(Opcode) {
623 default: return 0;
624 case ISD::ADD:
625 if (v <= 32767 && v >= -32768) { Imm = v & 0xFFFF; return 1; }
626 if ((v & 0x0000FFFF) == 0) { Imm = v >> 16; return 2; }
627 break;
Nate Begeman9f833d32005-04-12 00:10:02 +0000628 case ISD::AND: {
629 unsigned MB, ME;
630 if (IsRunOfOnes(v, MB, ME)) { Imm = MB << 16 | ME & 0xFFFF; return 5; }
631 if (v >= 0 && v <= 65535) { Imm = v & 0xFFFF; return 1; }
632 if ((v & 0x0000FFFF) == 0) { Imm = v >> 16; return 2; }
633 break;
634 }
Nate Begemana9795f82005-03-24 04:41:43 +0000635 case ISD::XOR:
636 case ISD::OR:
637 if (v >= 0 && v <= 65535) { Imm = v & 0xFFFF; return 1; }
638 if ((v & 0x0000FFFF) == 0) { Imm = v >> 16; return 2; }
639 break;
Nate Begeman307e7442005-03-26 01:28:53 +0000640 case ISD::MUL:
Nate Begeman27523a12005-04-02 00:42:16 +0000641 case ISD::SUB:
Nate Begeman307e7442005-03-26 01:28:53 +0000642 if (v <= 32767 && v >= -32768) { Imm = v & 0xFFFF; return 1; }
643 break;
Nate Begeman3e897162005-03-31 23:55:40 +0000644 case ISD::SETCC:
645 if (U && (v >= 0 && v <= 65535)) { Imm = v & 0xFFFF; return 1; }
646 if (!U && (v <= 32767 && v >= -32768)) { Imm = v & 0xFFFF; return 1; }
647 break;
Nate Begeman80196b12005-04-05 00:15:08 +0000648 case ISD::SDIV:
Nate Begeman439b4442005-04-05 04:22:58 +0000649 if ((Imm = ExactLog2(v))) { return 3; }
Nate Begeman9f833d32005-04-12 00:10:02 +0000650 if ((Imm = ExactLog2(-v))) { Imm = -Imm; return 3; }
Nate Begeman815d6da2005-04-06 00:25:27 +0000651 if (v <= -2 || v >= 2) { return 4; }
652 break;
653 case ISD::UDIV:
Nate Begeman27b4c232005-04-06 06:44:57 +0000654 if (v > 1) { return 4; }
Nate Begeman80196b12005-04-05 00:15:08 +0000655 break;
Nate Begemana9795f82005-03-24 04:41:43 +0000656 }
657 return 0;
658}
Nate Begeman3e897162005-03-31 23:55:40 +0000659
Nate Begemanc7bd4822005-04-11 06:34:10 +0000660/// NodeHasRecordingVariant - If SelectExpr can always produce code for
661/// NodeOpcode that also sets CR0 as a side effect, return true. Otherwise,
662/// return false.
663static bool NodeHasRecordingVariant(unsigned NodeOpcode) {
664 switch(NodeOpcode) {
665 default: return false;
666 case ISD::AND:
Nate Begeman9765c252005-04-12 21:22:28 +0000667 case ISD::OR:
Chris Lattner519f40b2005-04-13 02:46:17 +0000668 return true;
Nate Begemanc7bd4822005-04-11 06:34:10 +0000669 }
670}
671
Nate Begeman3e897162005-03-31 23:55:40 +0000672/// getBCCForSetCC - Returns the PowerPC condition branch mnemonic corresponding
673/// to Condition. If the Condition is unordered or unsigned, the bool argument
674/// U is set to true, otherwise it is set to false.
675static unsigned getBCCForSetCC(unsigned Condition, bool& U) {
676 U = false;
677 switch (Condition) {
678 default: assert(0 && "Unknown condition!"); abort();
679 case ISD::SETEQ: return PPC::BEQ;
680 case ISD::SETNE: return PPC::BNE;
681 case ISD::SETULT: U = true;
682 case ISD::SETLT: return PPC::BLT;
683 case ISD::SETULE: U = true;
684 case ISD::SETLE: return PPC::BLE;
685 case ISD::SETUGT: U = true;
686 case ISD::SETGT: return PPC::BGT;
687 case ISD::SETUGE: U = true;
688 case ISD::SETGE: return PPC::BGE;
689 }
Nate Begeman04730362005-04-01 04:45:11 +0000690 return 0;
691}
692
Nate Begeman7bfba7d2005-04-14 09:45:08 +0000693/// getCROpForOp - Return the condition register opcode (or inverted opcode)
694/// associated with the SelectionDAG opcode.
695static unsigned getCROpForSetCC(unsigned Opcode, bool Inv1, bool Inv2) {
696 switch (Opcode) {
697 default: assert(0 && "Unknown opcode!"); abort();
698 case ISD::AND:
699 if (Inv1 && Inv2) return PPC::CRNOR; // De Morgan's Law
700 if (!Inv1 && !Inv2) return PPC::CRAND;
701 if (Inv1 ^ Inv2) return PPC::CRANDC;
702 case ISD::OR:
703 if (Inv1 && Inv2) return PPC::CRNAND; // De Morgan's Law
704 if (!Inv1 && !Inv2) return PPC::CROR;
705 if (Inv1 ^ Inv2) return PPC::CRORC;
706 }
707 return 0;
708}
709
710/// getCRIdxForSetCC - Return the index of the condition register field
711/// associated with the SetCC condition, and whether or not the field is
712/// treated as inverted. That is, lt = 0; ge = 0 inverted.
713static unsigned getCRIdxForSetCC(unsigned Condition, bool& Inv) {
714 switch (Condition) {
715 default: assert(0 && "Unknown condition!"); abort();
716 case ISD::SETULT:
717 case ISD::SETLT: Inv = false; return 0;
718 case ISD::SETUGE:
719 case ISD::SETGE: Inv = true; return 0;
720 case ISD::SETUGT:
721 case ISD::SETGT: Inv = false; return 1;
722 case ISD::SETULE:
723 case ISD::SETLE: Inv = true; return 1;
724 case ISD::SETEQ: Inv = false; return 2;
725 case ISD::SETNE: Inv = true; return 2;
726 }
727 return 0;
728}
729
Nate Begeman04730362005-04-01 04:45:11 +0000730/// IndexedOpForOp - Return the indexed variant for each of the PowerPC load
731/// and store immediate instructions.
732static unsigned IndexedOpForOp(unsigned Opcode) {
733 switch(Opcode) {
734 default: assert(0 && "Unknown opcode!"); abort();
735 case PPC::LBZ: return PPC::LBZX; case PPC::STB: return PPC::STBX;
736 case PPC::LHZ: return PPC::LHZX; case PPC::STH: return PPC::STHX;
737 case PPC::LHA: return PPC::LHAX; case PPC::STW: return PPC::STWX;
738 case PPC::LWZ: return PPC::LWZX; case PPC::STFS: return PPC::STFSX;
739 case PPC::LFS: return PPC::LFSX; case PPC::STFD: return PPC::STFDX;
740 case PPC::LFD: return PPC::LFDX;
741 }
742 return 0;
Nate Begeman3e897162005-03-31 23:55:40 +0000743}
Nate Begeman815d6da2005-04-06 00:25:27 +0000744
745// Structure used to return the necessary information to codegen an SDIV as
746// a multiply.
747struct ms {
748 int m; // magic number
749 int s; // shift amount
750};
751
752struct mu {
753 unsigned int m; // magic number
754 int a; // add indicator
755 int s; // shift amount
756};
757
758/// magic - calculate the magic numbers required to codegen an integer sdiv as
759/// a sequence of multiply and shifts. Requires that the divisor not be 0, 1,
760/// or -1.
761static struct ms magic(int d) {
762 int p;
763 unsigned int ad, anc, delta, q1, r1, q2, r2, t;
764 const unsigned int two31 = 2147483648U; // 2^31
765 struct ms mag;
766
767 ad = abs(d);
768 t = two31 + ((unsigned int)d >> 31);
769 anc = t - 1 - t%ad; // absolute value of nc
770 p = 31; // initialize p
771 q1 = two31/anc; // initialize q1 = 2p/abs(nc)
772 r1 = two31 - q1*anc; // initialize r1 = rem(2p,abs(nc))
773 q2 = two31/ad; // initialize q2 = 2p/abs(d)
774 r2 = two31 - q2*ad; // initialize r2 = rem(2p,abs(d))
775 do {
776 p = p + 1;
777 q1 = 2*q1; // update q1 = 2p/abs(nc)
778 r1 = 2*r1; // update r1 = rem(2p/abs(nc))
779 if (r1 >= anc) { // must be unsigned comparison
780 q1 = q1 + 1;
781 r1 = r1 - anc;
782 }
783 q2 = 2*q2; // update q2 = 2p/abs(d)
784 r2 = 2*r2; // update r2 = rem(2p/abs(d))
785 if (r2 >= ad) { // must be unsigned comparison
786 q2 = q2 + 1;
787 r2 = r2 - ad;
788 }
789 delta = ad - r2;
790 } while (q1 < delta || (q1 == delta && r1 == 0));
791
792 mag.m = q2 + 1;
793 if (d < 0) mag.m = -mag.m; // resulting magic number
794 mag.s = p - 32; // resulting shift
795 return mag;
796}
797
798/// magicu - calculate the magic numbers required to codegen an integer udiv as
799/// a sequence of multiply, add and shifts. Requires that the divisor not be 0.
800static struct mu magicu(unsigned d)
801{
802 int p;
803 unsigned int nc, delta, q1, r1, q2, r2;
804 struct mu magu;
805 magu.a = 0; // initialize "add" indicator
806 nc = - 1 - (-d)%d;
807 p = 31; // initialize p
808 q1 = 0x80000000/nc; // initialize q1 = 2p/nc
809 r1 = 0x80000000 - q1*nc; // initialize r1 = rem(2p,nc)
810 q2 = 0x7FFFFFFF/d; // initialize q2 = (2p-1)/d
811 r2 = 0x7FFFFFFF - q2*d; // initialize r2 = rem((2p-1),d)
812 do {
813 p = p + 1;
814 if (r1 >= nc - r1 ) {
815 q1 = 2*q1 + 1; // update q1
816 r1 = 2*r1 - nc; // update r1
817 }
818 else {
819 q1 = 2*q1; // update q1
820 r1 = 2*r1; // update r1
821 }
822 if (r2 + 1 >= d - r2) {
823 if (q2 >= 0x7FFFFFFF) magu.a = 1;
824 q2 = 2*q2 + 1; // update q2
825 r2 = 2*r2 + 1 - d; // update r2
826 }
827 else {
828 if (q2 >= 0x80000000) magu.a = 1;
829 q2 = 2*q2; // update q2
830 r2 = 2*r2 + 1; // update r2
831 }
832 delta = d - 1 - r2;
833 } while (p < 64 && (q1 < delta || (q1 == delta && r1 == 0)));
834 magu.m = q2 + 1; // resulting magic number
835 magu.s = p - 32; // resulting shift
836 return magu;
837}
838}
839
840/// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
841/// return a DAG expression to select that will generate the same value by
842/// multiplying by a magic number. See:
843/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
844SDOperand ISel::BuildSDIVSequence(SDOperand N) {
845 int d = (int)cast<ConstantSDNode>(N.getOperand(1))->getSignExtended();
846 ms magics = magic(d);
847 // Multiply the numerator (operand 0) by the magic value
848 SDOperand Q = ISelDAG->getNode(ISD::MULHS, MVT::i32, N.getOperand(0),
849 ISelDAG->getConstant(magics.m, MVT::i32));
850 // If d > 0 and m < 0, add the numerator
851 if (d > 0 && magics.m < 0)
852 Q = ISelDAG->getNode(ISD::ADD, MVT::i32, Q, N.getOperand(0));
853 // If d < 0 and m > 0, subtract the numerator.
854 if (d < 0 && magics.m > 0)
855 Q = ISelDAG->getNode(ISD::SUB, MVT::i32, Q, N.getOperand(0));
856 // Shift right algebraic if shift value is nonzero
857 if (magics.s > 0)
858 Q = ISelDAG->getNode(ISD::SRA, MVT::i32, Q,
859 ISelDAG->getConstant(magics.s, MVT::i32));
860 // Extract the sign bit and add it to the quotient
861 SDOperand T =
862 ISelDAG->getNode(ISD::SRL, MVT::i32, Q, ISelDAG->getConstant(31, MVT::i32));
Nate Begeman27b4c232005-04-06 06:44:57 +0000863 return ISelDAG->getNode(ISD::ADD, MVT::i32, Q, T);
Nate Begeman815d6da2005-04-06 00:25:27 +0000864}
865
866/// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
867/// return a DAG expression to select that will generate the same value by
868/// multiplying by a magic number. See:
869/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
870SDOperand ISel::BuildUDIVSequence(SDOperand N) {
871 unsigned d =
872 (unsigned)cast<ConstantSDNode>(N.getOperand(1))->getSignExtended();
873 mu magics = magicu(d);
874 // Multiply the numerator (operand 0) by the magic value
875 SDOperand Q = ISelDAG->getNode(ISD::MULHU, MVT::i32, N.getOperand(0),
876 ISelDAG->getConstant(magics.m, MVT::i32));
877 if (magics.a == 0) {
878 Q = ISelDAG->getNode(ISD::SRL, MVT::i32, Q,
879 ISelDAG->getConstant(magics.s, MVT::i32));
880 } else {
881 SDOperand NPQ = ISelDAG->getNode(ISD::SUB, MVT::i32, N.getOperand(0), Q);
882 NPQ = ISelDAG->getNode(ISD::SRL, MVT::i32, NPQ,
883 ISelDAG->getConstant(1, MVT::i32));
884 NPQ = ISelDAG->getNode(ISD::ADD, MVT::i32, NPQ, Q);
885 Q = ISelDAG->getNode(ISD::SRL, MVT::i32, NPQ,
886 ISelDAG->getConstant(magics.s-1, MVT::i32));
887 }
Nate Begeman27b4c232005-04-06 06:44:57 +0000888 return Q;
Nate Begemana9795f82005-03-24 04:41:43 +0000889}
890
Nate Begemanc7b09f12005-03-25 08:34:25 +0000891/// getGlobalBaseReg - Output the instructions required to put the
892/// base address to use for accessing globals into a register.
893///
894unsigned ISel::getGlobalBaseReg() {
895 if (!GlobalBaseInitialized) {
896 // Insert the set of GlobalBaseReg into the first MBB of the function
897 MachineBasicBlock &FirstMBB = BB->getParent()->front();
898 MachineBasicBlock::iterator MBBI = FirstMBB.begin();
899 GlobalBaseReg = MakeReg(MVT::i32);
900 BuildMI(FirstMBB, MBBI, PPC::MovePCtoLR, 0, PPC::LR);
901 BuildMI(FirstMBB, MBBI, PPC::MFLR, 1, GlobalBaseReg).addReg(PPC::LR);
902 GlobalBaseInitialized = true;
903 }
904 return GlobalBaseReg;
905}
906
Nate Begeman6b559972005-04-01 02:59:27 +0000907/// getConstDouble - Loads a floating point value into a register, via the
908/// Constant Pool. Optionally takes a register in which to load the value.
909unsigned ISel::getConstDouble(double doubleVal, unsigned Result=0) {
910 unsigned Tmp1 = MakeReg(MVT::i32);
911 if (0 == Result) Result = MakeReg(MVT::f64);
912 MachineConstantPool *CP = BB->getParent()->getConstantPool();
913 ConstantFP *CFP = ConstantFP::get(Type::DoubleTy, doubleVal);
914 unsigned CPI = CP->getConstantPoolIndex(CFP);
915 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp1).addReg(getGlobalBaseReg())
916 .addConstantPoolIndex(CPI);
917 BuildMI(BB, PPC::LFD, 2, Result).addConstantPoolIndex(CPI).addReg(Tmp1);
918 return Result;
919}
920
Nate Begeman7ddecb42005-04-06 23:51:40 +0000921/// SelectBitfieldInsert - turn an or of two masked values into
922/// the rotate left word immediate then mask insert (rlwimi) instruction.
923/// Returns true on success, false if the caller still needs to select OR.
924///
925/// Patterns matched:
926/// 1. or shl, and 5. or and, and
927/// 2. or and, shl 6. or shl, shr
928/// 3. or shr, and 7. or shr, shl
929/// 4. or and, shr
930bool ISel::SelectBitfieldInsert(SDOperand OR, unsigned Result) {
Nate Begemancd08e4c2005-04-09 20:09:12 +0000931 bool IsRotate = false;
Nate Begeman7ddecb42005-04-06 23:51:40 +0000932 unsigned TgtMask = 0xFFFFFFFF, InsMask = 0xFFFFFFFF, Amount = 0;
933 unsigned Op0Opc = OR.getOperand(0).getOpcode();
934 unsigned Op1Opc = OR.getOperand(1).getOpcode();
935
936 // Verify that we have the correct opcodes
937 if (ISD::SHL != Op0Opc && ISD::SRL != Op0Opc && ISD::AND != Op0Opc)
938 return false;
939 if (ISD::SHL != Op1Opc && ISD::SRL != Op1Opc && ISD::AND != Op1Opc)
940 return false;
941
942 // Generate Mask value for Target
943 if (ConstantSDNode *CN =
944 dyn_cast<ConstantSDNode>(OR.getOperand(0).getOperand(1).Val)) {
945 switch(Op0Opc) {
946 case ISD::SHL: TgtMask <<= (unsigned)CN->getValue(); break;
947 case ISD::SRL: TgtMask >>= (unsigned)CN->getValue(); break;
948 case ISD::AND: TgtMask &= (unsigned)CN->getValue(); break;
949 }
950 } else {
951 return false;
952 }
953
954 // Generate Mask value for Insert
955 if (ConstantSDNode *CN =
956 dyn_cast<ConstantSDNode>(OR.getOperand(1).getOperand(1).Val)) {
957 switch(Op1Opc) {
958 case ISD::SHL:
959 Amount = CN->getValue();
Nate Begemancd08e4c2005-04-09 20:09:12 +0000960 InsMask <<= Amount;
961 if (Op0Opc == ISD::SRL) IsRotate = true;
Nate Begeman7ddecb42005-04-06 23:51:40 +0000962 break;
963 case ISD::SRL:
964 Amount = CN->getValue();
965 InsMask >>= Amount;
966 Amount = 32-Amount;
Nate Begemancd08e4c2005-04-09 20:09:12 +0000967 if (Op0Opc == ISD::SHL) IsRotate = true;
Nate Begeman7ddecb42005-04-06 23:51:40 +0000968 break;
969 case ISD::AND:
970 InsMask &= (unsigned)CN->getValue();
971 break;
972 }
973 } else {
974 return false;
975 }
976
977 // Verify that the Target mask and Insert mask together form a full word mask
978 // and that the Insert mask is a run of set bits (which implies both are runs
979 // of set bits). Given that, Select the arguments and generate the rlwimi
980 // instruction.
981 unsigned MB, ME;
982 if (((TgtMask ^ InsMask) == 0xFFFFFFFF) && IsRunOfOnes(InsMask, MB, ME)) {
983 unsigned Tmp1, Tmp2;
Nate Begemancd08e4c2005-04-09 20:09:12 +0000984 // Check for rotlwi / rotrwi here, a special case of bitfield insert
985 // where both bitfield halves are sourced from the same value.
986 if (IsRotate &&
987 OR.getOperand(0).getOperand(0) == OR.getOperand(1).getOperand(0)) {
Nate Begemancd08e4c2005-04-09 20:09:12 +0000988 Tmp1 = SelectExpr(OR.getOperand(0).getOperand(0));
989 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(Amount)
990 .addImm(0).addImm(31);
991 return true;
992 }
Nate Begeman7ddecb42005-04-06 23:51:40 +0000993 if (Op0Opc == ISD::AND)
994 Tmp1 = SelectExpr(OR.getOperand(0).getOperand(0));
995 else
996 Tmp1 = SelectExpr(OR.getOperand(0));
997 Tmp2 = SelectExpr(OR.getOperand(1).getOperand(0));
998 BuildMI(BB, PPC::RLWIMI, 5, Result).addReg(Tmp1).addReg(Tmp2)
999 .addImm(Amount).addImm(MB).addImm(ME);
1000 return true;
1001 }
1002 return false;
1003}
1004
Nate Begeman3664cef2005-04-13 22:14:14 +00001005/// FoldIfWideZeroExtend - 32 bit PowerPC implicit masks shift amounts to the
1006/// low six bits. If the shift amount is an ISD::AND node with a mask that is
1007/// wider than the implicit mask, then we can get rid of the AND and let the
1008/// shift do the mask.
1009unsigned ISel::FoldIfWideZeroExtend(SDOperand N) {
1010 unsigned C;
1011 if (N.getOpcode() == ISD::AND &&
1012 5 == getImmediateForOpcode(N.getOperand(1), ISD::AND, C) && // isMask
1013 31 == (C & 0xFFFF) && // ME
1014 26 >= (C >> 16)) // MB
1015 return SelectExpr(N.getOperand(0));
1016 else
1017 return SelectExpr(N);
1018}
1019
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001020unsigned ISel::SelectCC(SDOperand CC, unsigned &Opc) {
1021 unsigned Result, Tmp1, Tmp2;
Nate Begeman9765c252005-04-12 21:22:28 +00001022 bool AlreadySelected = false;
Nate Begemandffcfcc2005-04-01 00:32:34 +00001023 static const unsigned CompareOpcodes[] =
1024 { PPC::FCMPU, PPC::FCMPU, PPC::CMPW, PPC::CMPLW };
1025
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001026 // Allocate a condition register for this expression
1027 Result = RegMap->createVirtualRegister(PPC32::CRRCRegisterClass);
1028
Nate Begemandffcfcc2005-04-01 00:32:34 +00001029 // If the first operand to the select is a SETCC node, then we can fold it
1030 // into the branch that selects which value to return.
1031 SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(CC.Val);
1032 if (SetCC && CC.getOpcode() == ISD::SETCC) {
1033 bool U;
1034 Opc = getBCCForSetCC(SetCC->getCondition(), U);
Nate Begemandffcfcc2005-04-01 00:32:34 +00001035
Nate Begeman439b4442005-04-05 04:22:58 +00001036 // Pass the optional argument U to getImmediateForOpcode for SETCC,
Nate Begemandffcfcc2005-04-01 00:32:34 +00001037 // so that it knows whether the SETCC immediate range is signed or not.
Nate Begeman439b4442005-04-05 04:22:58 +00001038 if (1 == getImmediateForOpcode(SetCC->getOperand(1), ISD::SETCC,
1039 Tmp2, U)) {
Nate Begemanc7bd4822005-04-11 06:34:10 +00001040 // For comparisons against zero, we can implicity set CR0 if a recording
1041 // variant (e.g. 'or.' instead of 'or') of the instruction that defines
1042 // operand zero of the SetCC node is available.
1043 if (0 == Tmp2 &&
Nate Begeman9765c252005-04-12 21:22:28 +00001044 NodeHasRecordingVariant(SetCC->getOperand(0).getOpcode()) &&
1045 SetCC->getOperand(0).Val->hasOneUse()) {
Nate Begemanc7bd4822005-04-11 06:34:10 +00001046 RecordSuccess = false;
1047 Tmp1 = SelectExpr(SetCC->getOperand(0), true);
1048 if (RecordSuccess) {
1049 ++Recorded;
Nate Begeman7bfba7d2005-04-14 09:45:08 +00001050 BuildMI(BB, PPC::MCRF, 1, Result).addReg(PPC::CR0);
1051 return Result;
Nate Begemanc7bd4822005-04-11 06:34:10 +00001052 }
1053 AlreadySelected = true;
1054 }
1055 // If we could not implicitly set CR0, then emit a compare immediate
1056 // instead.
1057 if (!AlreadySelected) Tmp1 = SelectExpr(SetCC->getOperand(0));
Nate Begemandffcfcc2005-04-01 00:32:34 +00001058 if (U)
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001059 BuildMI(BB, PPC::CMPLWI, 2, Result).addReg(Tmp1).addImm(Tmp2);
Nate Begemandffcfcc2005-04-01 00:32:34 +00001060 else
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001061 BuildMI(BB, PPC::CMPWI, 2, Result).addReg(Tmp1).addSImm(Tmp2);
Nate Begemandffcfcc2005-04-01 00:32:34 +00001062 } else {
1063 bool IsInteger = MVT::isInteger(SetCC->getOperand(0).getValueType());
1064 unsigned CompareOpc = CompareOpcodes[2 * IsInteger + U];
Nate Begemanc7bd4822005-04-11 06:34:10 +00001065 Tmp1 = SelectExpr(SetCC->getOperand(0));
Nate Begemandffcfcc2005-04-01 00:32:34 +00001066 Tmp2 = SelectExpr(SetCC->getOperand(1));
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001067 BuildMI(BB, CompareOpc, 2, Result).addReg(Tmp1).addReg(Tmp2);
Nate Begemandffcfcc2005-04-01 00:32:34 +00001068 }
1069 } else {
Nate Begeman7bfba7d2005-04-14 09:45:08 +00001070#if 0
1071 if (CC.getOpcode() == ISD::AND || CC.getOpcode() == ISD::OR)
1072 if (CC.getOperand(0).Val->hasOneUse() &&
1073 CC.getOperand(1).Val->hasOneUse()) {
1074 SetCCSDNode* Op0CC = dyn_cast<SetCCSDNode>(CC.getOperand(0).Val);
1075 SetCCSDNode* Op1CC = dyn_cast<SetCCSDNode>(CC.getOperand(1).Val);
1076 if (Op0CC && Op1CC) {
1077 ++MultiBranch;
1078 bool Inv0, Inv1;
1079 unsigned Opc1;
1080 unsigned Idx0 = getCRIdxForSetCC(Op0CC->getCondition(), Inv0);
1081 unsigned Idx1 = getCRIdxForSetCC(Op1CC->getCondition(), Inv1);
1082 unsigned CROpc = getCROpForSetCC(CC.getOpcode(), Inv0, Inv1);
1083 Tmp1 = SelectCC(CC.getOperand(0), Opc);
1084 Tmp2 = SelectCC(CC.getOperand(1), Opc1);
1085 if (Inv0 && !Inv1) {
1086 std::swap(Tmp1, Tmp2);
1087 std::swap(Idx0, Idx1);
1088 Opc = Opc1;
1089 }
1090 if (Inv0 && Inv1) Opc = PPC32InstrInfo::invertPPCBranchOpcode(Opc);
1091 BuildMI(BB, CROpc, 5, Result).addImm(Idx0).addReg(Tmp1).addImm(Idx0)
1092 .addReg(Tmp2).addImm(Idx1);
1093 return Result;
1094 }
1095 }
1096#endif
Nate Begeman9765c252005-04-12 21:22:28 +00001097 Opc = PPC::BNE;
Nate Begemandffcfcc2005-04-01 00:32:34 +00001098 Tmp1 = SelectExpr(CC);
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001099 BuildMI(BB, PPC::CMPLWI, 2, Result).addReg(Tmp1).addImm(0);
Nate Begemandffcfcc2005-04-01 00:32:34 +00001100 }
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001101 return Result;
Nate Begemandffcfcc2005-04-01 00:32:34 +00001102}
1103
1104/// Check to see if the load is a constant offset from a base register
Nate Begeman04730362005-04-01 04:45:11 +00001105bool ISel::SelectAddr(SDOperand N, unsigned& Reg, int& offset)
Nate Begemana9795f82005-03-24 04:41:43 +00001106{
Nate Begeman96fc6812005-03-31 02:05:53 +00001107 unsigned imm = 0, opcode = N.getOpcode();
Nate Begeman04730362005-04-01 04:45:11 +00001108 if (N.getOpcode() == ISD::ADD) {
1109 Reg = SelectExpr(N.getOperand(0));
Nate Begeman439b4442005-04-05 04:22:58 +00001110 if (1 == getImmediateForOpcode(N.getOperand(1), opcode, imm)) {
Nate Begeman96fc6812005-03-31 02:05:53 +00001111 offset = imm;
Nate Begeman04730362005-04-01 04:45:11 +00001112 return false;
1113 }
1114 offset = SelectExpr(N.getOperand(1));
1115 return true;
1116 }
Nate Begemana9795f82005-03-24 04:41:43 +00001117 Reg = SelectExpr(N);
1118 offset = 0;
Nate Begeman04730362005-04-01 04:45:11 +00001119 return false;
Nate Begemana9795f82005-03-24 04:41:43 +00001120}
1121
1122void ISel::SelectBranchCC(SDOperand N)
1123{
Nate Begemana9795f82005-03-24 04:41:43 +00001124 MachineBasicBlock *Dest =
1125 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
Nate Begeman3e897162005-03-31 23:55:40 +00001126
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001127 unsigned Opc, CCReg;
Nate Begemana9795f82005-03-24 04:41:43 +00001128 Select(N.getOperand(0)); //chain
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001129 CCReg = SelectCC(N.getOperand(1), Opc);
Nate Begemancd08e4c2005-04-09 20:09:12 +00001130
1131 // Iterate to the next basic block, unless we're already at the end of the
1132 ilist<MachineBasicBlock>::iterator It = BB, E = BB->getParent()->end();
Nate Begeman706471e2005-04-09 23:35:05 +00001133 if (++It == E) It = BB;
Nate Begemancd08e4c2005-04-09 20:09:12 +00001134
1135 // If this is a two way branch, then grab the fallthrough basic block argument
1136 // and build a PowerPC branch pseudo-op, suitable for long branch conversion
1137 // if necessary by the branch selection pass. Otherwise, emit a standard
1138 // conditional branch.
1139 if (N.getOpcode() == ISD::BRCONDTWOWAY) {
1140 MachineBasicBlock *Fallthrough =
1141 cast<BasicBlockSDNode>(N.getOperand(3))->getBasicBlock();
1142 if (Dest != It) {
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001143 BuildMI(BB, PPC::COND_BRANCH, 4).addReg(CCReg).addImm(Opc)
Nate Begemancd08e4c2005-04-09 20:09:12 +00001144 .addMBB(Dest).addMBB(Fallthrough);
1145 if (Fallthrough != It)
1146 BuildMI(BB, PPC::B, 1).addMBB(Fallthrough);
1147 } else {
1148 if (Fallthrough != It) {
1149 Opc = PPC32InstrInfo::invertPPCBranchOpcode(Opc);
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001150 BuildMI(BB, PPC::COND_BRANCH, 4).addReg(CCReg).addImm(Opc)
Nate Begemancd08e4c2005-04-09 20:09:12 +00001151 .addMBB(Fallthrough).addMBB(Dest);
1152 }
1153 }
1154 } else {
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001155 BuildMI(BB, PPC::COND_BRANCH, 4).addReg(CCReg).addImm(Opc)
Nate Begeman27499e32005-04-10 01:48:29 +00001156 .addMBB(Dest).addMBB(It);
Nate Begemancd08e4c2005-04-09 20:09:12 +00001157 }
Nate Begemana9795f82005-03-24 04:41:43 +00001158 return;
1159}
1160
1161unsigned ISel::SelectExprFP(SDOperand N, unsigned Result)
1162{
1163 unsigned Tmp1, Tmp2, Tmp3;
1164 unsigned Opc = 0;
1165 SDNode *Node = N.Val;
1166 MVT::ValueType DestType = N.getValueType();
1167 unsigned opcode = N.getOpcode();
1168
1169 switch (opcode) {
1170 default:
1171 Node->dump();
1172 assert(0 && "Node not handled!\n");
1173
Nate Begeman23afcfb2005-03-29 22:48:55 +00001174 case ISD::SELECT: {
Nate Begeman3e897162005-03-31 23:55:40 +00001175 // Attempt to generate FSEL. We can do this whenever we have an FP result,
1176 // and an FP comparison in the SetCC node.
1177 SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(N.getOperand(0).Val);
1178 if (SetCC && N.getOperand(0).getOpcode() == ISD::SETCC &&
1179 !MVT::isInteger(SetCC->getOperand(0).getValueType()) &&
1180 SetCC->getCondition() != ISD::SETEQ &&
1181 SetCC->getCondition() != ISD::SETNE) {
1182 MVT::ValueType VT = SetCC->getOperand(0).getValueType();
Nate Begeman3e897162005-03-31 23:55:40 +00001183 unsigned TV = SelectExpr(N.getOperand(1)); // Use if TRUE
1184 unsigned FV = SelectExpr(N.getOperand(2)); // Use if FALSE
1185
1186 ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(1));
1187 if (CN && (CN->isExactlyValue(-0.0) || CN->isExactlyValue(0.0))) {
1188 switch(SetCC->getCondition()) {
1189 default: assert(0 && "Invalid FSEL condition"); abort();
1190 case ISD::SETULT:
1191 case ISD::SETLT:
Nate Begemanaf4ab1b2005-04-09 09:33:07 +00001192 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
Nate Begeman3e897162005-03-31 23:55:40 +00001193 case ISD::SETUGE:
1194 case ISD::SETGE:
Nate Begemanaf4ab1b2005-04-09 09:33:07 +00001195 Tmp1 = SelectExpr(SetCC->getOperand(0)); // Val to compare against
Nate Begeman3e897162005-03-31 23:55:40 +00001196 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp1).addReg(TV).addReg(FV);
1197 return Result;
1198 case ISD::SETUGT:
Nate Begemanaf4ab1b2005-04-09 09:33:07 +00001199 case ISD::SETGT:
1200 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
Nate Begeman3e897162005-03-31 23:55:40 +00001201 case ISD::SETULE:
1202 case ISD::SETLE: {
Nate Begemanaf4ab1b2005-04-09 09:33:07 +00001203 if (SetCC->getOperand(0).getOpcode() == ISD::FNEG) {
1204 Tmp2 = SelectExpr(SetCC->getOperand(0).getOperand(0));
1205 } else {
1206 Tmp2 = MakeReg(VT);
1207 Tmp1 = SelectExpr(SetCC->getOperand(0)); // Val to compare against
1208 BuildMI(BB, PPC::FNEG, 1, Tmp2).addReg(Tmp1);
1209 }
Nate Begeman3e897162005-03-31 23:55:40 +00001210 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp2).addReg(TV).addReg(FV);
1211 return Result;
1212 }
1213 }
1214 } else {
1215 Opc = (MVT::f64 == VT) ? PPC::FSUB : PPC::FSUBS;
Nate Begemanaf4ab1b2005-04-09 09:33:07 +00001216 Tmp1 = SelectExpr(SetCC->getOperand(0)); // Val to compare against
Nate Begeman3e897162005-03-31 23:55:40 +00001217 Tmp2 = SelectExpr(SetCC->getOperand(1));
1218 Tmp3 = MakeReg(VT);
1219 switch(SetCC->getCondition()) {
1220 default: assert(0 && "Invalid FSEL condition"); abort();
1221 case ISD::SETULT:
1222 case ISD::SETLT:
1223 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
1224 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(FV).addReg(TV);
1225 return Result;
1226 case ISD::SETUGE:
1227 case ISD::SETGE:
1228 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
1229 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(TV).addReg(FV);
1230 return Result;
1231 case ISD::SETUGT:
1232 case ISD::SETGT:
1233 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp2).addReg(Tmp1);
1234 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(FV).addReg(TV);
1235 return Result;
1236 case ISD::SETULE:
1237 case ISD::SETLE:
1238 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp2).addReg(Tmp1);
1239 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(TV).addReg(FV);
1240 return Result;
1241 }
1242 }
1243 assert(0 && "Should never get here");
1244 return 0;
1245 }
1246
Nate Begeman31318e42005-04-01 07:21:30 +00001247 unsigned TrueValue = SelectExpr(N.getOperand(1)); //Use if TRUE
1248 unsigned FalseValue = SelectExpr(N.getOperand(2)); //Use if FALSE
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001249 unsigned CCReg = SelectCC(N.getOperand(0), Opc);
Nate Begeman31318e42005-04-01 07:21:30 +00001250
Nate Begeman23afcfb2005-03-29 22:48:55 +00001251 // Create an iterator with which to insert the MBB for copying the false
1252 // value and the MBB to hold the PHI instruction for this SetCC.
1253 MachineBasicBlock *thisMBB = BB;
1254 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1255 ilist<MachineBasicBlock>::iterator It = BB;
1256 ++It;
1257
1258 // thisMBB:
1259 // ...
1260 // TrueVal = ...
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001261 // cmpTY ccX, r1, r2
Nate Begeman23afcfb2005-03-29 22:48:55 +00001262 // bCC copy1MBB
1263 // fallthrough --> copy0MBB
Nate Begeman23afcfb2005-03-29 22:48:55 +00001264 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
1265 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001266 BuildMI(BB, Opc, 2).addReg(CCReg).addMBB(sinkMBB);
Nate Begeman23afcfb2005-03-29 22:48:55 +00001267 MachineFunction *F = BB->getParent();
1268 F->getBasicBlockList().insert(It, copy0MBB);
1269 F->getBasicBlockList().insert(It, sinkMBB);
1270 // Update machine-CFG edges
1271 BB->addSuccessor(copy0MBB);
1272 BB->addSuccessor(sinkMBB);
1273
1274 // copy0MBB:
1275 // %FalseValue = ...
1276 // # fallthrough to sinkMBB
1277 BB = copy0MBB;
Nate Begeman23afcfb2005-03-29 22:48:55 +00001278 // Update machine-CFG edges
1279 BB->addSuccessor(sinkMBB);
1280
1281 // sinkMBB:
1282 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1283 // ...
1284 BB = sinkMBB;
1285 BuildMI(BB, PPC::PHI, 4, Result).addReg(FalseValue)
1286 .addMBB(copy0MBB).addReg(TrueValue).addMBB(thisMBB);
1287 return Result;
1288 }
Nate Begeman27eeb002005-04-02 05:59:34 +00001289
1290 case ISD::FNEG:
Nate Begeman93075ec2005-04-04 23:40:36 +00001291 if (!NoExcessFPPrecision &&
1292 ISD::ADD == N.getOperand(0).getOpcode() &&
1293 N.getOperand(0).Val->hasOneUse() &&
1294 ISD::MUL == N.getOperand(0).getOperand(0).getOpcode() &&
1295 N.getOperand(0).getOperand(0).Val->hasOneUse()) {
Nate Begeman80196b12005-04-05 00:15:08 +00001296 ++FusedFP; // Statistic
Nate Begeman93075ec2005-04-04 23:40:36 +00001297 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(0));
1298 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(1));
1299 Tmp3 = SelectExpr(N.getOperand(0).getOperand(1));
1300 Opc = DestType == MVT::f64 ? PPC::FNMADD : PPC::FNMADDS;
1301 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1302 } else if (!NoExcessFPPrecision &&
Nate Begemane88aa5b2005-04-09 03:05:51 +00001303 ISD::ADD == N.getOperand(0).getOpcode() &&
Nate Begeman93075ec2005-04-04 23:40:36 +00001304 N.getOperand(0).Val->hasOneUse() &&
Nate Begemane88aa5b2005-04-09 03:05:51 +00001305 ISD::MUL == N.getOperand(0).getOperand(1).getOpcode() &&
1306 N.getOperand(0).getOperand(1).Val->hasOneUse()) {
Nate Begeman80196b12005-04-05 00:15:08 +00001307 ++FusedFP; // Statistic
Nate Begemane88aa5b2005-04-09 03:05:51 +00001308 Tmp1 = SelectExpr(N.getOperand(0).getOperand(1).getOperand(0));
1309 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1).getOperand(1));
1310 Tmp3 = SelectExpr(N.getOperand(0).getOperand(0));
1311 Opc = DestType == MVT::f64 ? PPC::FNMADD : PPC::FNMADDS;
Nate Begeman93075ec2005-04-04 23:40:36 +00001312 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1313 } else if (ISD::FABS == N.getOperand(0).getOpcode()) {
Nate Begeman27eeb002005-04-02 05:59:34 +00001314 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1315 BuildMI(BB, PPC::FNABS, 1, Result).addReg(Tmp1);
1316 } else {
1317 Tmp1 = SelectExpr(N.getOperand(0));
1318 BuildMI(BB, PPC::FNEG, 1, Result).addReg(Tmp1);
1319 }
1320 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001321
Nate Begeman27eeb002005-04-02 05:59:34 +00001322 case ISD::FABS:
1323 Tmp1 = SelectExpr(N.getOperand(0));
1324 BuildMI(BB, PPC::FABS, 1, Result).addReg(Tmp1);
1325 return Result;
1326
Nate Begemana9795f82005-03-24 04:41:43 +00001327 case ISD::FP_ROUND:
1328 assert (DestType == MVT::f32 &&
1329 N.getOperand(0).getValueType() == MVT::f64 &&
1330 "only f64 to f32 conversion supported here");
1331 Tmp1 = SelectExpr(N.getOperand(0));
1332 BuildMI(BB, PPC::FRSP, 1, Result).addReg(Tmp1);
1333 return Result;
1334
1335 case ISD::FP_EXTEND:
1336 assert (DestType == MVT::f64 &&
1337 N.getOperand(0).getValueType() == MVT::f32 &&
1338 "only f32 to f64 conversion supported here");
1339 Tmp1 = SelectExpr(N.getOperand(0));
1340 BuildMI(BB, PPC::FMR, 1, Result).addReg(Tmp1);
1341 return Result;
1342
1343 case ISD::CopyFromReg:
Nate Begemanf2622612005-03-26 02:17:46 +00001344 if (Result == 1)
1345 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1346 Tmp1 = dyn_cast<RegSDNode>(Node)->getReg();
1347 BuildMI(BB, PPC::FMR, 1, Result).addReg(Tmp1);
1348 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001349
Nate Begeman6d369cc2005-04-01 01:08:07 +00001350 case ISD::ConstantFP: {
Nate Begeman6d369cc2005-04-01 01:08:07 +00001351 ConstantFPSDNode *CN = cast<ConstantFPSDNode>(N);
Nate Begeman6b559972005-04-01 02:59:27 +00001352 Result = getConstDouble(CN->getValue(), Result);
Nate Begeman6d369cc2005-04-01 01:08:07 +00001353 return Result;
1354 }
Nate Begemana9795f82005-03-24 04:41:43 +00001355
Nate Begemana9795f82005-03-24 04:41:43 +00001356 case ISD::ADD:
Nate Begeman93075ec2005-04-04 23:40:36 +00001357 if (!NoExcessFPPrecision && N.getOperand(0).getOpcode() == ISD::MUL &&
1358 N.getOperand(0).Val->hasOneUse()) {
1359 ++FusedFP; // Statistic
1360 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1361 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1362 Tmp3 = SelectExpr(N.getOperand(1));
1363 Opc = DestType == MVT::f64 ? PPC::FMADD : PPC::FMADDS;
1364 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1365 return Result;
1366 }
Nate Begemane88aa5b2005-04-09 03:05:51 +00001367 if (!NoExcessFPPrecision && N.getOperand(1).getOpcode() == ISD::MUL &&
1368 N.getOperand(1).Val->hasOneUse()) {
1369 ++FusedFP; // Statistic
1370 Tmp1 = SelectExpr(N.getOperand(1).getOperand(0));
1371 Tmp2 = SelectExpr(N.getOperand(1).getOperand(1));
1372 Tmp3 = SelectExpr(N.getOperand(0));
1373 Opc = DestType == MVT::f64 ? PPC::FMADD : PPC::FMADDS;
1374 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1375 return Result;
1376 }
Nate Begeman93075ec2005-04-04 23:40:36 +00001377 Opc = DestType == MVT::f64 ? PPC::FADD : PPC::FADDS;
1378 Tmp1 = SelectExpr(N.getOperand(0));
1379 Tmp2 = SelectExpr(N.getOperand(1));
1380 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1381 return Result;
1382
Nate Begemana9795f82005-03-24 04:41:43 +00001383 case ISD::SUB:
Nate Begeman93075ec2005-04-04 23:40:36 +00001384 if (!NoExcessFPPrecision && N.getOperand(0).getOpcode() == ISD::MUL &&
1385 N.getOperand(0).Val->hasOneUse()) {
1386 ++FusedFP; // Statistic
1387 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1388 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1389 Tmp3 = SelectExpr(N.getOperand(1));
1390 Opc = DestType == MVT::f64 ? PPC::FMSUB : PPC::FMSUBS;
1391 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1392 return Result;
1393 }
Nate Begemane88aa5b2005-04-09 03:05:51 +00001394 if (!NoExcessFPPrecision && N.getOperand(1).getOpcode() == ISD::MUL &&
1395 N.getOperand(1).Val->hasOneUse()) {
1396 ++FusedFP; // Statistic
1397 Tmp1 = SelectExpr(N.getOperand(1).getOperand(0));
1398 Tmp2 = SelectExpr(N.getOperand(1).getOperand(1));
1399 Tmp3 = SelectExpr(N.getOperand(0));
1400 Opc = DestType == MVT::f64 ? PPC::FNMSUB : PPC::FNMSUBS;
1401 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1402 return Result;
1403 }
Nate Begeman93075ec2005-04-04 23:40:36 +00001404 Opc = DestType == MVT::f64 ? PPC::FSUB : PPC::FSUBS;
1405 Tmp1 = SelectExpr(N.getOperand(0));
1406 Tmp2 = SelectExpr(N.getOperand(1));
1407 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1408 return Result;
1409
1410 case ISD::MUL:
Nate Begemana9795f82005-03-24 04:41:43 +00001411 case ISD::SDIV:
1412 switch( opcode ) {
1413 case ISD::MUL: Opc = DestType == MVT::f64 ? PPC::FMUL : PPC::FMULS; break;
Nate Begemana9795f82005-03-24 04:41:43 +00001414 case ISD::SDIV: Opc = DestType == MVT::f64 ? PPC::FDIV : PPC::FDIVS; break;
1415 };
Nate Begemana9795f82005-03-24 04:41:43 +00001416 Tmp1 = SelectExpr(N.getOperand(0));
1417 Tmp2 = SelectExpr(N.getOperand(1));
1418 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1419 return Result;
1420
Nate Begemana9795f82005-03-24 04:41:43 +00001421 case ISD::UINT_TO_FP:
Nate Begemanfdcf3412005-03-30 19:38:35 +00001422 case ISD::SINT_TO_FP: {
1423 assert (N.getOperand(0).getValueType() == MVT::i32
1424 && "int to float must operate on i32");
1425 bool IsUnsigned = (ISD::UINT_TO_FP == opcode);
1426 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
1427 Tmp2 = MakeReg(MVT::f64); // temp reg to load the integer value into
1428 Tmp3 = MakeReg(MVT::i32); // temp reg to hold the conversion constant
Nate Begemanfdcf3412005-03-30 19:38:35 +00001429
1430 int FrameIdx = BB->getParent()->getFrameInfo()->CreateStackObject(8, 8);
1431 MachineConstantPool *CP = BB->getParent()->getConstantPool();
1432
Nate Begemanfdcf3412005-03-30 19:38:35 +00001433 if (IsUnsigned) {
Nate Begeman709c8062005-04-10 06:06:10 +00001434 unsigned ConstF = getConstDouble(0x1.000000p52);
Nate Begemanfdcf3412005-03-30 19:38:35 +00001435 // Store the hi & low halves of the fp value, currently in int regs
1436 BuildMI(BB, PPC::LIS, 1, Tmp3).addSImm(0x4330);
1437 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(Tmp3), FrameIdx);
1438 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(Tmp1), FrameIdx, 4);
1439 addFrameReference(BuildMI(BB, PPC::LFD, 2, Tmp2), FrameIdx);
1440 // Generate the return value with a subtract
1441 BuildMI(BB, PPC::FSUB, 2, Result).addReg(Tmp2).addReg(ConstF);
1442 } else {
Nate Begeman709c8062005-04-10 06:06:10 +00001443 unsigned ConstF = getConstDouble(0x1.000008p52);
Nate Begemanfdcf3412005-03-30 19:38:35 +00001444 unsigned TmpL = MakeReg(MVT::i32);
Nate Begemanfdcf3412005-03-30 19:38:35 +00001445 // Store the hi & low halves of the fp value, currently in int regs
1446 BuildMI(BB, PPC::LIS, 1, Tmp3).addSImm(0x4330);
1447 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(Tmp3), FrameIdx);
1448 BuildMI(BB, PPC::XORIS, 2, TmpL).addReg(Tmp1).addImm(0x8000);
1449 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(TmpL), FrameIdx, 4);
1450 addFrameReference(BuildMI(BB, PPC::LFD, 2, Tmp2), FrameIdx);
1451 // Generate the return value with a subtract
1452 BuildMI(BB, PPC::FSUB, 2, Result).addReg(Tmp2).addReg(ConstF);
1453 }
1454 return Result;
1455 }
Nate Begemana9795f82005-03-24 04:41:43 +00001456 }
Nate Begeman6b559972005-04-01 02:59:27 +00001457 assert(0 && "Should never get here");
Nate Begemana9795f82005-03-24 04:41:43 +00001458 return 0;
1459}
1460
Nate Begemanc7bd4822005-04-11 06:34:10 +00001461unsigned ISel::SelectExpr(SDOperand N, bool Recording) {
Nate Begemana9795f82005-03-24 04:41:43 +00001462 unsigned Result;
1463 unsigned Tmp1, Tmp2, Tmp3;
1464 unsigned Opc = 0;
1465 unsigned opcode = N.getOpcode();
1466
1467 SDNode *Node = N.Val;
1468 MVT::ValueType DestType = N.getValueType();
1469
1470 unsigned &Reg = ExprMap[N];
1471 if (Reg) return Reg;
1472
Nate Begeman27eeb002005-04-02 05:59:34 +00001473 switch (N.getOpcode()) {
1474 default:
Nate Begemana9795f82005-03-24 04:41:43 +00001475 Reg = Result = (N.getValueType() != MVT::Other) ?
Nate Begeman27eeb002005-04-02 05:59:34 +00001476 MakeReg(N.getValueType()) : 1;
1477 break;
1478 case ISD::CALL:
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001479 // If this is a call instruction, make sure to prepare ALL of the result
1480 // values as well as the chain.
Nate Begeman27eeb002005-04-02 05:59:34 +00001481 if (Node->getNumValues() == 1)
1482 Reg = Result = 1; // Void call, just a chain.
1483 else {
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001484 Result = MakeReg(Node->getValueType(0));
1485 ExprMap[N.getValue(0)] = Result;
Nate Begeman27eeb002005-04-02 05:59:34 +00001486 for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001487 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
Nate Begeman27eeb002005-04-02 05:59:34 +00001488 ExprMap[SDOperand(Node, Node->getNumValues()-1)] = 1;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001489 }
Nate Begeman27eeb002005-04-02 05:59:34 +00001490 break;
1491 case ISD::ADD_PARTS:
1492 case ISD::SUB_PARTS:
1493 case ISD::SHL_PARTS:
1494 case ISD::SRL_PARTS:
1495 case ISD::SRA_PARTS:
1496 Result = MakeReg(Node->getValueType(0));
1497 ExprMap[N.getValue(0)] = Result;
1498 for (unsigned i = 1, e = N.Val->getNumValues(); i != e; ++i)
1499 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
1500 break;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001501 }
1502
Nate Begemane5846682005-04-04 06:52:38 +00001503 if (ISD::CopyFromReg == opcode)
1504 DestType = N.getValue(0).getValueType();
1505
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001506 if (DestType == MVT::f64 || DestType == MVT::f32)
Nate Begemana0e3e942005-04-10 01:14:13 +00001507 if (ISD::LOAD != opcode && ISD::EXTLOAD != opcode &&
1508 ISD::UNDEF != opcode && ISD::CALL != opcode)
Nate Begeman74d73452005-03-31 00:15:26 +00001509 return SelectExprFP(N, Result);
Nate Begemana9795f82005-03-24 04:41:43 +00001510
1511 switch (opcode) {
1512 default:
1513 Node->dump();
1514 assert(0 && "Node not handled!\n");
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001515 case ISD::UNDEF:
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001516 BuildMI(BB, PPC::IMPLICIT_DEF, 0, Result);
1517 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001518 case ISD::DYNAMIC_STACKALLOC:
Nate Begeman5e966612005-03-24 06:28:42 +00001519 // Generate both result values. FIXME: Need a better commment here?
1520 if (Result != 1)
1521 ExprMap[N.getValue(1)] = 1;
1522 else
1523 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1524
1525 // FIXME: We are currently ignoring the requested alignment for handling
1526 // greater than the stack alignment. This will need to be revisited at some
1527 // point. Align = N.getOperand(2);
1528 if (!isa<ConstantSDNode>(N.getOperand(2)) ||
1529 cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) {
1530 std::cerr << "Cannot allocate stack object with greater alignment than"
1531 << " the stack alignment yet!";
1532 abort();
1533 }
1534 Select(N.getOperand(0));
1535 Tmp1 = SelectExpr(N.getOperand(1));
1536 // Subtract size from stack pointer, thereby allocating some space.
1537 BuildMI(BB, PPC::SUBF, 2, PPC::R1).addReg(Tmp1).addReg(PPC::R1);
1538 // Put a pointer to the space into the result register by copying the SP
1539 BuildMI(BB, PPC::OR, 2, Result).addReg(PPC::R1).addReg(PPC::R1);
1540 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001541
1542 case ISD::ConstantPool:
Nate Begemanca12a2b2005-03-28 22:28:37 +00001543 Tmp1 = cast<ConstantPoolSDNode>(N)->getIndex();
1544 Tmp2 = MakeReg(MVT::i32);
1545 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp2).addReg(getGlobalBaseReg())
1546 .addConstantPoolIndex(Tmp1);
1547 BuildMI(BB, PPC::LA, 2, Result).addReg(Tmp2).addConstantPoolIndex(Tmp1);
1548 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001549
1550 case ISD::FrameIndex:
Nate Begemanf3d08f32005-03-29 00:03:27 +00001551 Tmp1 = cast<FrameIndexSDNode>(N)->getIndex();
Nate Begeman58f718c2005-03-30 02:23:08 +00001552 addFrameReference(BuildMI(BB, PPC::ADDI, 2, Result), (int)Tmp1, 0, false);
Nate Begemanf3d08f32005-03-29 00:03:27 +00001553 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001554
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001555 case ISD::GlobalAddress: {
1556 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
Nate Begemanca12a2b2005-03-28 22:28:37 +00001557 Tmp1 = MakeReg(MVT::i32);
Nate Begemanc7b09f12005-03-25 08:34:25 +00001558 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp1).addReg(getGlobalBaseReg())
1559 .addGlobalAddress(GV);
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001560 if (GV->hasWeakLinkage() || GV->isExternal()) {
1561 BuildMI(BB, PPC::LWZ, 2, Result).addGlobalAddress(GV).addReg(Tmp1);
1562 } else {
1563 BuildMI(BB, PPC::LA, 2, Result).addReg(Tmp1).addGlobalAddress(GV);
1564 }
1565 return Result;
1566 }
1567
Nate Begeman5e966612005-03-24 06:28:42 +00001568 case ISD::LOAD:
Nate Begemana9795f82005-03-24 04:41:43 +00001569 case ISD::EXTLOAD:
1570 case ISD::ZEXTLOAD:
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001571 case ISD::SEXTLOAD: {
Nate Begeman9db505c2005-03-28 19:36:43 +00001572 MVT::ValueType TypeBeingLoaded = (ISD::LOAD == opcode) ?
1573 Node->getValueType(0) : cast<MVTSDNode>(Node)->getExtraValueType();
Nate Begeman74d73452005-03-31 00:15:26 +00001574 bool sext = (ISD::SEXTLOAD == opcode);
Nate Begeman74d73452005-03-31 00:15:26 +00001575
Nate Begeman5e966612005-03-24 06:28:42 +00001576 // Make sure we generate both values.
1577 if (Result != 1)
1578 ExprMap[N.getValue(1)] = 1; // Generate the token
1579 else
1580 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1581
1582 SDOperand Chain = N.getOperand(0);
1583 SDOperand Address = N.getOperand(1);
1584 Select(Chain);
1585
Nate Begeman9db505c2005-03-28 19:36:43 +00001586 switch (TypeBeingLoaded) {
Nate Begeman74d73452005-03-31 00:15:26 +00001587 default: Node->dump(); assert(0 && "Cannot load this type!");
Nate Begeman9db505c2005-03-28 19:36:43 +00001588 case MVT::i1: Opc = PPC::LBZ; break;
1589 case MVT::i8: Opc = PPC::LBZ; break;
1590 case MVT::i16: Opc = sext ? PPC::LHA : PPC::LHZ; break;
1591 case MVT::i32: Opc = PPC::LWZ; break;
Nate Begeman74d73452005-03-31 00:15:26 +00001592 case MVT::f32: Opc = PPC::LFS; break;
1593 case MVT::f64: Opc = PPC::LFD; break;
Nate Begeman5e966612005-03-24 06:28:42 +00001594 }
1595
Nate Begeman74d73452005-03-31 00:15:26 +00001596 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Address)) {
1597 Tmp1 = MakeReg(MVT::i32);
1598 int CPI = CP->getIndex();
1599 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp1).addReg(getGlobalBaseReg())
1600 .addConstantPoolIndex(CPI);
1601 BuildMI(BB, Opc, 2, Result).addConstantPoolIndex(CPI).addReg(Tmp1);
Nate Begeman9db505c2005-03-28 19:36:43 +00001602 }
Nate Begeman74d73452005-03-31 00:15:26 +00001603 else if(Address.getOpcode() == ISD::FrameIndex) {
Nate Begeman58f718c2005-03-30 02:23:08 +00001604 Tmp1 = cast<FrameIndexSDNode>(Address)->getIndex();
1605 addFrameReference(BuildMI(BB, Opc, 2, Result), (int)Tmp1);
Nate Begeman5e966612005-03-24 06:28:42 +00001606 } else {
1607 int offset;
Nate Begeman04730362005-04-01 04:45:11 +00001608 bool idx = SelectAddr(Address, Tmp1, offset);
1609 if (idx) {
1610 Opc = IndexedOpForOp(Opc);
1611 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(offset);
1612 } else {
1613 BuildMI(BB, Opc, 2, Result).addSImm(offset).addReg(Tmp1);
1614 }
Nate Begeman5e966612005-03-24 06:28:42 +00001615 }
1616 return Result;
1617 }
1618
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001619 case ISD::CALL: {
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001620 unsigned GPR_idx = 0, FPR_idx = 0;
1621 static const unsigned GPR[] = {
1622 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
1623 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
1624 };
1625 static const unsigned FPR[] = {
1626 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
1627 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
1628 };
1629
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001630 // Lower the chain for this call.
1631 Select(N.getOperand(0));
1632 ExprMap[N.getValue(Node->getNumValues()-1)] = 1;
Nate Begeman74d73452005-03-31 00:15:26 +00001633
Nate Begemand860aa62005-04-04 22:17:48 +00001634 MachineInstr *CallMI;
1635 // Emit the correct call instruction based on the type of symbol called.
1636 if (GlobalAddressSDNode *GASD =
1637 dyn_cast<GlobalAddressSDNode>(N.getOperand(1))) {
1638 CallMI = BuildMI(PPC::CALLpcrel, 1).addGlobalAddress(GASD->getGlobal(),
1639 true);
1640 } else if (ExternalSymbolSDNode *ESSDN =
1641 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1))) {
1642 CallMI = BuildMI(PPC::CALLpcrel, 1).addExternalSymbol(ESSDN->getSymbol(),
1643 true);
1644 } else {
1645 Tmp1 = SelectExpr(N.getOperand(1));
1646 BuildMI(BB, PPC::OR, 2, PPC::R12).addReg(Tmp1).addReg(Tmp1);
1647 BuildMI(BB, PPC::MTCTR, 1).addReg(PPC::R12);
1648 CallMI = BuildMI(PPC::CALLindirect, 3).addImm(20).addImm(0)
1649 .addReg(PPC::R12);
1650 }
1651
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001652 // Load the register args to virtual regs
1653 std::vector<unsigned> ArgVR;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001654 for(int i = 2, e = Node->getNumOperands(); i < e; ++i)
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001655 ArgVR.push_back(SelectExpr(N.getOperand(i)));
1656
1657 // Copy the virtual registers into the appropriate argument register
1658 for(int i = 0, e = ArgVR.size(); i < e; ++i) {
1659 switch(N.getOperand(i+2).getValueType()) {
1660 default: Node->dump(); assert(0 && "Unknown value type for call");
1661 case MVT::i1:
1662 case MVT::i8:
1663 case MVT::i16:
1664 case MVT::i32:
1665 assert(GPR_idx < 8 && "Too many int args");
Nate Begemand860aa62005-04-04 22:17:48 +00001666 if (N.getOperand(i+2).getOpcode() != ISD::UNDEF) {
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001667 BuildMI(BB, PPC::OR,2,GPR[GPR_idx]).addReg(ArgVR[i]).addReg(ArgVR[i]);
Nate Begemand860aa62005-04-04 22:17:48 +00001668 CallMI->addRegOperand(GPR[GPR_idx], MachineOperand::Use);
1669 }
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001670 ++GPR_idx;
1671 break;
1672 case MVT::f64:
1673 case MVT::f32:
1674 assert(FPR_idx < 13 && "Too many fp args");
1675 BuildMI(BB, PPC::FMR, 1, FPR[FPR_idx]).addReg(ArgVR[i]);
Nate Begemand860aa62005-04-04 22:17:48 +00001676 CallMI->addRegOperand(FPR[FPR_idx], MachineOperand::Use);
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001677 ++FPR_idx;
1678 break;
1679 }
1680 }
Nate Begemand860aa62005-04-04 22:17:48 +00001681
1682 // Put the call instruction in the correct place in the MachineBasicBlock
1683 BB->push_back(CallMI);
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001684
1685 switch (Node->getValueType(0)) {
1686 default: assert(0 && "Unknown value type for call result!");
1687 case MVT::Other: return 1;
1688 case MVT::i1:
1689 case MVT::i8:
1690 case MVT::i16:
1691 case MVT::i32:
Nate Begemane5846682005-04-04 06:52:38 +00001692 if (Node->getValueType(1) == MVT::i32) {
1693 BuildMI(BB, PPC::OR, 2, Result+1).addReg(PPC::R3).addReg(PPC::R3);
1694 BuildMI(BB, PPC::OR, 2, Result).addReg(PPC::R4).addReg(PPC::R4);
1695 } else {
1696 BuildMI(BB, PPC::OR, 2, Result).addReg(PPC::R3).addReg(PPC::R3);
1697 }
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001698 break;
1699 case MVT::f32:
1700 case MVT::f64:
1701 BuildMI(BB, PPC::FMR, 1, Result).addReg(PPC::F1);
1702 break;
1703 }
1704 return Result+N.ResNo;
1705 }
Nate Begemana9795f82005-03-24 04:41:43 +00001706
1707 case ISD::SIGN_EXTEND:
1708 case ISD::SIGN_EXTEND_INREG:
1709 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman9db505c2005-03-28 19:36:43 +00001710 switch(cast<MVTSDNode>(Node)->getExtraValueType()) {
1711 default: Node->dump(); assert(0 && "Unhandled SIGN_EXTEND type"); break;
Nate Begemanc7bd4822005-04-11 06:34:10 +00001712 case MVT::i16:
Nate Begeman9db505c2005-03-28 19:36:43 +00001713 BuildMI(BB, PPC::EXTSH, 1, Result).addReg(Tmp1);
1714 break;
Nate Begemanc7bd4822005-04-11 06:34:10 +00001715 case MVT::i8:
Nate Begeman9db505c2005-03-28 19:36:43 +00001716 BuildMI(BB, PPC::EXTSB, 1, Result).addReg(Tmp1);
1717 break;
Nate Begeman74747862005-03-29 22:24:51 +00001718 case MVT::i1:
1719 BuildMI(BB, PPC::SUBFIC, 2, Result).addReg(Tmp1).addSImm(0);
1720 break;
Nate Begeman9db505c2005-03-28 19:36:43 +00001721 }
Nate Begemana9795f82005-03-24 04:41:43 +00001722 return Result;
1723
Nate Begemana9795f82005-03-24 04:41:43 +00001724 case ISD::CopyFromReg:
1725 if (Result == 1)
1726 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1727 Tmp1 = dyn_cast<RegSDNode>(Node)->getReg();
1728 BuildMI(BB, PPC::OR, 2, Result).addReg(Tmp1).addReg(Tmp1);
1729 return Result;
1730
1731 case ISD::SHL:
Nate Begeman5e966612005-03-24 06:28:42 +00001732 Tmp1 = SelectExpr(N.getOperand(0));
1733 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1734 Tmp2 = CN->getValue() & 0x1F;
Nate Begeman33162522005-03-29 21:54:38 +00001735 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(Tmp2).addImm(0)
Nate Begeman5e966612005-03-24 06:28:42 +00001736 .addImm(31-Tmp2);
1737 } else {
Nate Begeman3664cef2005-04-13 22:14:14 +00001738 Tmp2 = FoldIfWideZeroExtend(N.getOperand(1));
Nate Begeman5e966612005-03-24 06:28:42 +00001739 BuildMI(BB, PPC::SLW, 2, Result).addReg(Tmp1).addReg(Tmp2);
1740 }
1741 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001742
Nate Begeman5e966612005-03-24 06:28:42 +00001743 case ISD::SRL:
1744 Tmp1 = SelectExpr(N.getOperand(0));
1745 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1746 Tmp2 = CN->getValue() & 0x1F;
Nate Begeman33162522005-03-29 21:54:38 +00001747 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(32-Tmp2)
Nate Begeman5e966612005-03-24 06:28:42 +00001748 .addImm(Tmp2).addImm(31);
1749 } else {
Nate Begeman3664cef2005-04-13 22:14:14 +00001750 Tmp2 = FoldIfWideZeroExtend(N.getOperand(1));
Nate Begeman5e966612005-03-24 06:28:42 +00001751 BuildMI(BB, PPC::SRW, 2, Result).addReg(Tmp1).addReg(Tmp2);
1752 }
1753 return Result;
1754
1755 case ISD::SRA:
1756 Tmp1 = SelectExpr(N.getOperand(0));
1757 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1758 Tmp2 = CN->getValue() & 0x1F;
1759 BuildMI(BB, PPC::SRAWI, 2, Result).addReg(Tmp1).addImm(Tmp2);
1760 } else {
Nate Begeman3664cef2005-04-13 22:14:14 +00001761 Tmp2 = FoldIfWideZeroExtend(N.getOperand(1));
Nate Begeman5e966612005-03-24 06:28:42 +00001762 BuildMI(BB, PPC::SRAW, 2, Result).addReg(Tmp1).addReg(Tmp2);
1763 }
1764 return Result;
1765
Nate Begemana9795f82005-03-24 04:41:43 +00001766 case ISD::ADD:
1767 assert (DestType == MVT::i32 && "Only do arithmetic on i32s!");
1768 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman439b4442005-04-05 04:22:58 +00001769 switch(getImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
Nate Begemana9795f82005-03-24 04:41:43 +00001770 default: assert(0 && "unhandled result code");
1771 case 0: // No immediate
1772 Tmp2 = SelectExpr(N.getOperand(1));
1773 BuildMI(BB, PPC::ADD, 2, Result).addReg(Tmp1).addReg(Tmp2);
1774 break;
1775 case 1: // Low immediate
1776 BuildMI(BB, PPC::ADDI, 2, Result).addReg(Tmp1).addSImm(Tmp2);
1777 break;
1778 case 2: // Shifted immediate
1779 BuildMI(BB, PPC::ADDIS, 2, Result).addReg(Tmp1).addSImm(Tmp2);
1780 break;
1781 }
1782 return Result;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001783
Nate Begemana9795f82005-03-24 04:41:43 +00001784 case ISD::AND:
Nate Begeman7ddecb42005-04-06 23:51:40 +00001785 Tmp1 = SelectExpr(N.getOperand(0));
1786 // FIXME: should add check in getImmediateForOpcode to return a value
1787 // indicating the immediate is a run of set bits so we can emit a bitfield
1788 // clear with RLWINM instead.
1789 switch(getImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
1790 default: assert(0 && "unhandled result code");
1791 case 0: // No immediate
1792 Tmp2 = SelectExpr(N.getOperand(1));
Nate Begemanc7bd4822005-04-11 06:34:10 +00001793 Opc = Recording ? PPC::ANDo : PPC::AND;
1794 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
Nate Begeman7ddecb42005-04-06 23:51:40 +00001795 break;
1796 case 1: // Low immediate
1797 BuildMI(BB, PPC::ANDIo, 2, Result).addReg(Tmp1).addImm(Tmp2);
1798 break;
1799 case 2: // Shifted immediate
1800 BuildMI(BB, PPC::ANDISo, 2, Result).addReg(Tmp1).addImm(Tmp2);
1801 break;
Nate Begeman9f833d32005-04-12 00:10:02 +00001802 case 5: // Bitfield mask
1803 Opc = Recording ? PPC::RLWINMo : PPC::RLWINM;
1804 Tmp3 = Tmp2 >> 16; // MB
1805 Tmp2 &= 0xFFFF; // ME
1806 BuildMI(BB, Opc, 4, Result).addReg(Tmp1).addImm(0)
1807 .addImm(Tmp3).addImm(Tmp2);
1808 break;
Nate Begeman7ddecb42005-04-06 23:51:40 +00001809 }
Nate Begemanc7bd4822005-04-11 06:34:10 +00001810 RecordSuccess = true;
Nate Begeman7ddecb42005-04-06 23:51:40 +00001811 return Result;
1812
Nate Begemana9795f82005-03-24 04:41:43 +00001813 case ISD::OR:
Nate Begeman7ddecb42005-04-06 23:51:40 +00001814 if (SelectBitfieldInsert(N, Result))
1815 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001816 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman439b4442005-04-05 04:22:58 +00001817 switch(getImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
Nate Begemana9795f82005-03-24 04:41:43 +00001818 default: assert(0 && "unhandled result code");
1819 case 0: // No immediate
1820 Tmp2 = SelectExpr(N.getOperand(1));
Nate Begemanc7bd4822005-04-11 06:34:10 +00001821 Opc = Recording ? PPC::ORo : PPC::OR;
1822 RecordSuccess = true;
1823 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +00001824 break;
1825 case 1: // Low immediate
Nate Begeman7ddecb42005-04-06 23:51:40 +00001826 BuildMI(BB, PPC::ORI, 2, Result).addReg(Tmp1).addImm(Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +00001827 break;
1828 case 2: // Shifted immediate
Nate Begeman7ddecb42005-04-06 23:51:40 +00001829 BuildMI(BB, PPC::ORIS, 2, Result).addReg(Tmp1).addImm(Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +00001830 break;
1831 }
1832 return Result;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001833
Nate Begemanaa73a9f2005-04-03 11:20:20 +00001834 case ISD::XOR: {
1835 // Check for EQV: xor, (xor a, -1), b
1836 if (N.getOperand(0).getOpcode() == ISD::XOR &&
1837 N.getOperand(0).getOperand(1).getOpcode() == ISD::Constant &&
1838 cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->isAllOnesValue()) {
Nate Begemanaa73a9f2005-04-03 11:20:20 +00001839 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1840 Tmp2 = SelectExpr(N.getOperand(1));
1841 BuildMI(BB, PPC::EQV, 2, Result).addReg(Tmp1).addReg(Tmp2);
1842 return Result;
1843 }
1844 // Check for NOT, NOR, and NAND: xor (copy, or, and), -1
1845 if (N.getOperand(1).getOpcode() == ISD::Constant &&
1846 cast<ConstantSDNode>(N.getOperand(1))->isAllOnesValue()) {
Nate Begemanaa73a9f2005-04-03 11:20:20 +00001847 switch(N.getOperand(0).getOpcode()) {
1848 case ISD::OR:
1849 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1850 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1851 BuildMI(BB, PPC::NOR, 2, Result).addReg(Tmp1).addReg(Tmp2);
1852 break;
1853 case ISD::AND:
1854 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1855 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1856 BuildMI(BB, PPC::NAND, 2, Result).addReg(Tmp1).addReg(Tmp2);
1857 break;
1858 default:
1859 Tmp1 = SelectExpr(N.getOperand(0));
1860 BuildMI(BB, PPC::NOR, 2, Result).addReg(Tmp1).addReg(Tmp1);
1861 break;
1862 }
1863 return Result;
1864 }
1865 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman439b4442005-04-05 04:22:58 +00001866 switch(getImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
Nate Begemanaa73a9f2005-04-03 11:20:20 +00001867 default: assert(0 && "unhandled result code");
1868 case 0: // No immediate
1869 Tmp2 = SelectExpr(N.getOperand(1));
1870 BuildMI(BB, PPC::XOR, 2, Result).addReg(Tmp1).addReg(Tmp2);
1871 break;
1872 case 1: // Low immediate
1873 BuildMI(BB, PPC::XORI, 2, Result).addReg(Tmp1).addImm(Tmp2);
1874 break;
1875 case 2: // Shifted immediate
1876 BuildMI(BB, PPC::XORIS, 2, Result).addReg(Tmp1).addImm(Tmp2);
1877 break;
1878 }
1879 return Result;
1880 }
1881
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001882 case ISD::SUB:
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001883 Tmp2 = SelectExpr(N.getOperand(1));
Nate Begeman439b4442005-04-05 04:22:58 +00001884 if (1 == getImmediateForOpcode(N.getOperand(0), opcode, Tmp1))
Nate Begeman27523a12005-04-02 00:42:16 +00001885 BuildMI(BB, PPC::SUBFIC, 2, Result).addReg(Tmp2).addSImm(Tmp1);
1886 else {
1887 Tmp1 = SelectExpr(N.getOperand(0));
1888 BuildMI(BB, PPC::SUBF, 2, Result).addReg(Tmp2).addReg(Tmp1);
1889 }
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001890 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001891
Nate Begeman5e966612005-03-24 06:28:42 +00001892 case ISD::MUL:
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001893 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman439b4442005-04-05 04:22:58 +00001894 if (1 == getImmediateForOpcode(N.getOperand(1), opcode, Tmp2))
Nate Begeman307e7442005-03-26 01:28:53 +00001895 BuildMI(BB, PPC::MULLI, 2, Result).addReg(Tmp1).addSImm(Tmp2);
1896 else {
1897 Tmp2 = SelectExpr(N.getOperand(1));
1898 BuildMI(BB, PPC::MULLW, 2, Result).addReg(Tmp1).addReg(Tmp2);
1899 }
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001900 return Result;
1901
Nate Begeman815d6da2005-04-06 00:25:27 +00001902 case ISD::MULHS:
1903 case ISD::MULHU:
1904 Tmp1 = SelectExpr(N.getOperand(0));
1905 Tmp2 = SelectExpr(N.getOperand(1));
1906 Opc = (ISD::MULHU == opcode) ? PPC::MULHWU : PPC::MULHW;
1907 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1908 return Result;
1909
Nate Begemanf3d08f32005-03-29 00:03:27 +00001910 case ISD::SDIV:
1911 case ISD::UDIV:
Nate Begeman815d6da2005-04-06 00:25:27 +00001912 switch (getImmediateForOpcode(N.getOperand(1), opcode, Tmp3)) {
1913 default: break;
1914 // If this is an sdiv by a power of two, we can use an srawi/addze pair.
1915 case 3:
Nate Begeman80196b12005-04-05 00:15:08 +00001916 Tmp1 = MakeReg(MVT::i32);
1917 Tmp2 = SelectExpr(N.getOperand(0));
Nate Begeman9f833d32005-04-12 00:10:02 +00001918 if ((int)Tmp3 < 0) {
1919 unsigned Tmp4 = MakeReg(MVT::i32);
1920 BuildMI(BB, PPC::SRAWI, 2, Tmp1).addReg(Tmp2).addImm(-Tmp3);
1921 BuildMI(BB, PPC::ADDZE, 1, Tmp4).addReg(Tmp1);
1922 BuildMI(BB, PPC::NEG, 1, Result).addReg(Tmp4);
1923 } else {
1924 BuildMI(BB, PPC::SRAWI, 2, Tmp1).addReg(Tmp2).addImm(Tmp3);
1925 BuildMI(BB, PPC::ADDZE, 1, Result).addReg(Tmp1);
1926 }
Nate Begeman80196b12005-04-05 00:15:08 +00001927 return Result;
Nate Begeman815d6da2005-04-06 00:25:27 +00001928 // If this is a divide by constant, we can emit code using some magic
1929 // constants to implement it as a multiply instead.
Nate Begeman27b4c232005-04-06 06:44:57 +00001930 case 4:
1931 ExprMap.erase(N);
1932 if (opcode == ISD::SDIV)
1933 return SelectExpr(BuildSDIVSequence(N));
1934 else
1935 return SelectExpr(BuildUDIVSequence(N));
Nate Begeman80196b12005-04-05 00:15:08 +00001936 }
Nate Begemanf3d08f32005-03-29 00:03:27 +00001937 Tmp1 = SelectExpr(N.getOperand(0));
1938 Tmp2 = SelectExpr(N.getOperand(1));
1939 Opc = (ISD::UDIV == opcode) ? PPC::DIVWU : PPC::DIVW;
1940 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1941 return Result;
1942
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001943 case ISD::ADD_PARTS:
Nate Begemanca12a2b2005-03-28 22:28:37 +00001944 case ISD::SUB_PARTS: {
1945 assert(N.getNumOperands() == 4 && N.getValueType() == MVT::i32 &&
1946 "Not an i64 add/sub!");
1947 // Emit all of the operands.
1948 std::vector<unsigned> InVals;
1949 for (unsigned i = 0, e = N.getNumOperands(); i != e; ++i)
1950 InVals.push_back(SelectExpr(N.getOperand(i)));
1951 if (N.getOpcode() == ISD::ADD_PARTS) {
Nate Begeman27eeb002005-04-02 05:59:34 +00001952 BuildMI(BB, PPC::ADDC, 2, Result).addReg(InVals[0]).addReg(InVals[2]);
1953 BuildMI(BB, PPC::ADDE, 2, Result+1).addReg(InVals[1]).addReg(InVals[3]);
Nate Begemanca12a2b2005-03-28 22:28:37 +00001954 } else {
Nate Begeman27eeb002005-04-02 05:59:34 +00001955 BuildMI(BB, PPC::SUBFC, 2, Result).addReg(InVals[2]).addReg(InVals[0]);
1956 BuildMI(BB, PPC::SUBFE, 2, Result+1).addReg(InVals[3]).addReg(InVals[1]);
1957 }
1958 return Result+N.ResNo;
1959 }
1960
1961 case ISD::SHL_PARTS:
1962 case ISD::SRA_PARTS:
1963 case ISD::SRL_PARTS: {
1964 assert(N.getNumOperands() == 3 && N.getValueType() == MVT::i32 &&
1965 "Not an i64 shift!");
1966 unsigned ShiftOpLo = SelectExpr(N.getOperand(0));
1967 unsigned ShiftOpHi = SelectExpr(N.getOperand(1));
Nate Begeman3664cef2005-04-13 22:14:14 +00001968 unsigned SHReg = FoldIfWideZeroExtend(N.getOperand(2));
1969 Tmp1 = MakeReg(MVT::i32);
1970 Tmp2 = MakeReg(MVT::i32);
Nate Begeman27eeb002005-04-02 05:59:34 +00001971 Tmp3 = MakeReg(MVT::i32);
1972 unsigned Tmp4 = MakeReg(MVT::i32);
1973 unsigned Tmp5 = MakeReg(MVT::i32);
1974 unsigned Tmp6 = MakeReg(MVT::i32);
1975 BuildMI(BB, PPC::SUBFIC, 2, Tmp1).addReg(SHReg).addSImm(32);
1976 if (ISD::SHL_PARTS == opcode) {
1977 BuildMI(BB, PPC::SLW, 2, Tmp2).addReg(ShiftOpHi).addReg(SHReg);
1978 BuildMI(BB, PPC::SRW, 2, Tmp3).addReg(ShiftOpLo).addReg(Tmp1);
1979 BuildMI(BB, PPC::OR, 2, Tmp4).addReg(Tmp2).addReg(Tmp3);
1980 BuildMI(BB, PPC::ADDI, 2, Tmp5).addReg(SHReg).addSImm(-32);
Nate Begemanfa554702005-04-03 22:13:27 +00001981 BuildMI(BB, PPC::SLW, 2, Tmp6).addReg(ShiftOpLo).addReg(Tmp5);
Nate Begeman27eeb002005-04-02 05:59:34 +00001982 BuildMI(BB, PPC::OR, 2, Result+1).addReg(Tmp4).addReg(Tmp6);
1983 BuildMI(BB, PPC::SLW, 2, Result).addReg(ShiftOpLo).addReg(SHReg);
1984 } else if (ISD::SRL_PARTS == opcode) {
1985 BuildMI(BB, PPC::SRW, 2, Tmp2).addReg(ShiftOpLo).addReg(SHReg);
1986 BuildMI(BB, PPC::SLW, 2, Tmp3).addReg(ShiftOpHi).addReg(Tmp1);
1987 BuildMI(BB, PPC::OR, 2, Tmp4).addReg(Tmp2).addReg(Tmp3);
1988 BuildMI(BB, PPC::ADDI, 2, Tmp5).addReg(SHReg).addSImm(-32);
1989 BuildMI(BB, PPC::SRW, 2, Tmp6).addReg(ShiftOpHi).addReg(Tmp5);
1990 BuildMI(BB, PPC::OR, 2, Result).addReg(Tmp4).addReg(Tmp6);
1991 BuildMI(BB, PPC::SRW, 2, Result+1).addReg(ShiftOpHi).addReg(SHReg);
1992 } else {
1993 MachineBasicBlock *TmpMBB = new MachineBasicBlock(BB->getBasicBlock());
1994 MachineBasicBlock *PhiMBB = new MachineBasicBlock(BB->getBasicBlock());
1995 MachineBasicBlock *OldMBB = BB;
1996 MachineFunction *F = BB->getParent();
1997 ilist<MachineBasicBlock>::iterator It = BB; ++It;
1998 F->getBasicBlockList().insert(It, TmpMBB);
1999 F->getBasicBlockList().insert(It, PhiMBB);
2000 BB->addSuccessor(TmpMBB);
2001 BB->addSuccessor(PhiMBB);
2002 BuildMI(BB, PPC::SRW, 2, Tmp2).addReg(ShiftOpLo).addReg(SHReg);
2003 BuildMI(BB, PPC::SLW, 2, Tmp3).addReg(ShiftOpHi).addReg(Tmp1);
2004 BuildMI(BB, PPC::OR, 2, Tmp4).addReg(Tmp2).addReg(Tmp3);
2005 BuildMI(BB, PPC::ADDICo, 2, Tmp5).addReg(SHReg).addSImm(-32);
2006 BuildMI(BB, PPC::SRAW, 2, Tmp6).addReg(ShiftOpHi).addReg(Tmp5);
2007 BuildMI(BB, PPC::SRAW, 2, Result+1).addReg(ShiftOpHi).addReg(SHReg);
2008 BuildMI(BB, PPC::BLE, 2).addReg(PPC::CR0).addMBB(PhiMBB);
2009 // Select correct least significant half if the shift amount > 32
2010 BB = TmpMBB;
2011 unsigned Tmp7 = MakeReg(MVT::i32);
2012 BuildMI(BB, PPC::OR, 2, Tmp7).addReg(Tmp6).addReg(Tmp6);
2013 TmpMBB->addSuccessor(PhiMBB);
2014 BB = PhiMBB;
2015 BuildMI(BB, PPC::PHI, 4, Result).addReg(Tmp4).addMBB(OldMBB)
2016 .addReg(Tmp7).addMBB(TmpMBB);
Nate Begemanca12a2b2005-03-28 22:28:37 +00002017 }
2018 return Result+N.ResNo;
2019 }
2020
Nate Begemana9795f82005-03-24 04:41:43 +00002021 case ISD::FP_TO_UINT:
Nate Begeman6b559972005-04-01 02:59:27 +00002022 case ISD::FP_TO_SINT: {
2023 bool U = (ISD::FP_TO_UINT == opcode);
2024 Tmp1 = SelectExpr(N.getOperand(0));
2025 if (!U) {
2026 Tmp2 = MakeReg(MVT::f64);
2027 BuildMI(BB, PPC::FCTIWZ, 1, Tmp2).addReg(Tmp1);
2028 int FrameIdx = BB->getParent()->getFrameInfo()->CreateStackObject(8, 8);
2029 addFrameReference(BuildMI(BB, PPC::STFD, 3).addReg(Tmp2), FrameIdx);
2030 addFrameReference(BuildMI(BB, PPC::LWZ, 2, Result), FrameIdx, 4);
2031 return Result;
2032 } else {
2033 unsigned Zero = getConstDouble(0.0);
2034 unsigned MaxInt = getConstDouble((1LL << 32) - 1);
2035 unsigned Border = getConstDouble(1LL << 31);
2036 unsigned UseZero = MakeReg(MVT::f64);
2037 unsigned UseMaxInt = MakeReg(MVT::f64);
2038 unsigned UseChoice = MakeReg(MVT::f64);
2039 unsigned TmpReg = MakeReg(MVT::f64);
2040 unsigned TmpReg2 = MakeReg(MVT::f64);
2041 unsigned ConvReg = MakeReg(MVT::f64);
2042 unsigned IntTmp = MakeReg(MVT::i32);
2043 unsigned XorReg = MakeReg(MVT::i32);
2044 MachineFunction *F = BB->getParent();
2045 int FrameIdx = F->getFrameInfo()->CreateStackObject(8, 8);
2046 // Update machine-CFG edges
2047 MachineBasicBlock *XorMBB = new MachineBasicBlock(BB->getBasicBlock());
2048 MachineBasicBlock *PhiMBB = new MachineBasicBlock(BB->getBasicBlock());
2049 MachineBasicBlock *OldMBB = BB;
2050 ilist<MachineBasicBlock>::iterator It = BB; ++It;
2051 F->getBasicBlockList().insert(It, XorMBB);
2052 F->getBasicBlockList().insert(It, PhiMBB);
2053 BB->addSuccessor(XorMBB);
2054 BB->addSuccessor(PhiMBB);
2055 // Convert from floating point to unsigned 32-bit value
2056 // Use 0 if incoming value is < 0.0
2057 BuildMI(BB, PPC::FSEL, 3, UseZero).addReg(Tmp1).addReg(Tmp1).addReg(Zero);
2058 // Use 2**32 - 1 if incoming value is >= 2**32
2059 BuildMI(BB, PPC::FSUB, 2, UseMaxInt).addReg(MaxInt).addReg(Tmp1);
2060 BuildMI(BB, PPC::FSEL, 3, UseChoice).addReg(UseMaxInt).addReg(UseZero)
2061 .addReg(MaxInt);
2062 // Subtract 2**31
2063 BuildMI(BB, PPC::FSUB, 2, TmpReg).addReg(UseChoice).addReg(Border);
2064 // Use difference if >= 2**31
2065 BuildMI(BB, PPC::FCMPU, 2, PPC::CR0).addReg(UseChoice).addReg(Border);
2066 BuildMI(BB, PPC::FSEL, 3, TmpReg2).addReg(TmpReg).addReg(TmpReg)
2067 .addReg(UseChoice);
2068 // Convert to integer
2069 BuildMI(BB, PPC::FCTIWZ, 1, ConvReg).addReg(TmpReg2);
2070 addFrameReference(BuildMI(BB, PPC::STFD, 3).addReg(ConvReg), FrameIdx);
2071 addFrameReference(BuildMI(BB, PPC::LWZ, 2, IntTmp), FrameIdx, 4);
2072 BuildMI(BB, PPC::BLT, 2).addReg(PPC::CR0).addMBB(PhiMBB);
2073 BuildMI(BB, PPC::B, 1).addMBB(XorMBB);
2074
2075 // XorMBB:
2076 // add 2**31 if input was >= 2**31
2077 BB = XorMBB;
2078 BuildMI(BB, PPC::XORIS, 2, XorReg).addReg(IntTmp).addImm(0x8000);
2079 XorMBB->addSuccessor(PhiMBB);
2080
2081 // PhiMBB:
2082 // DestReg = phi [ IntTmp, OldMBB ], [ XorReg, XorMBB ]
2083 BB = PhiMBB;
2084 BuildMI(BB, PPC::PHI, 4, Result).addReg(IntTmp).addMBB(OldMBB)
2085 .addReg(XorReg).addMBB(XorMBB);
2086 return Result;
2087 }
2088 assert(0 && "Should never get here");
2089 return 0;
2090 }
Nate Begemana9795f82005-03-24 04:41:43 +00002091
Nate Begeman9e3e1b52005-03-24 23:35:30 +00002092 case ISD::SETCC:
Nate Begeman33162522005-03-29 21:54:38 +00002093 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Node)) {
Nate Begeman7e7fadd2005-04-07 20:30:01 +00002094 if (ConstantSDNode *CN =
2095 dyn_cast<ConstantSDNode>(SetCC->getOperand(1).Val)) {
Nate Begeman9765c252005-04-12 21:22:28 +00002096 // We can codegen setcc op, imm very efficiently compared to a brcond.
2097 // Check for those cases here.
2098 // setcc op, 0
Nate Begeman7e7fadd2005-04-07 20:30:01 +00002099 if (CN->getValue() == 0) {
2100 Tmp1 = SelectExpr(SetCC->getOperand(0));
2101 switch (SetCC->getCondition()) {
Nate Begeman7bfba7d2005-04-14 09:45:08 +00002102 default: SetCC->dump(); assert(0 && "Unhandled SetCC condition"); abort();
Nate Begeman7e7fadd2005-04-07 20:30:01 +00002103 case ISD::SETEQ:
Nate Begeman7e7fadd2005-04-07 20:30:01 +00002104 Tmp2 = MakeReg(MVT::i32);
2105 BuildMI(BB, PPC::CNTLZW, 1, Tmp2).addReg(Tmp1);
2106 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp2).addImm(27)
2107 .addImm(5).addImm(31);
2108 break;
2109 case ISD::SETNE:
Nate Begeman7e7fadd2005-04-07 20:30:01 +00002110 Tmp2 = MakeReg(MVT::i32);
2111 BuildMI(BB, PPC::ADDIC, 2, Tmp2).addReg(Tmp1).addSImm(-1);
2112 BuildMI(BB, PPC::SUBFE, 2, Result).addReg(Tmp2).addReg(Tmp1);
2113 break;
Nate Begeman7e7fadd2005-04-07 20:30:01 +00002114 case ISD::SETLT:
2115 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(1)
2116 .addImm(31).addImm(31);
2117 break;
Nate Begeman7e7fadd2005-04-07 20:30:01 +00002118 case ISD::SETGT:
2119 Tmp2 = MakeReg(MVT::i32);
2120 Tmp3 = MakeReg(MVT::i32);
2121 BuildMI(BB, PPC::NEG, 2, Tmp2).addReg(Tmp1);
2122 BuildMI(BB, PPC::ANDC, 2, Tmp3).addReg(Tmp2).addReg(Tmp1);
2123 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp3).addImm(1)
2124 .addImm(31).addImm(31);
2125 break;
Nate Begeman9765c252005-04-12 21:22:28 +00002126 }
2127 return Result;
2128 }
2129 // setcc op, -1
2130 if (CN->isAllOnesValue()) {
2131 Tmp1 = SelectExpr(SetCC->getOperand(0));
2132 switch (SetCC->getCondition()) {
2133 default: assert(0 && "Unhandled SetCC condition"); abort();
2134 case ISD::SETEQ:
2135 Tmp2 = MakeReg(MVT::i32);
2136 Tmp3 = MakeReg(MVT::i32);
2137 BuildMI(BB, PPC::ADDIC, 2, Tmp2).addReg(Tmp1).addSImm(1);
2138 BuildMI(BB, PPC::LI, 1, Tmp3).addSImm(0);
2139 BuildMI(BB, PPC::ADDZE, 1, Result).addReg(Tmp3);
Nate Begeman7e7fadd2005-04-07 20:30:01 +00002140 break;
Nate Begeman9765c252005-04-12 21:22:28 +00002141 case ISD::SETNE:
2142 Tmp2 = MakeReg(MVT::i32);
2143 Tmp3 = MakeReg(MVT::i32);
2144 BuildMI(BB, PPC::NOR, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
2145 BuildMI(BB, PPC::ADDIC, 2, Tmp3).addReg(Tmp2).addSImm(-1);
2146 BuildMI(BB, PPC::SUBFE, 2, Result).addReg(Tmp3).addReg(Tmp2);
2147 break;
2148 case ISD::SETLT:
2149 Tmp2 = MakeReg(MVT::i32);
2150 Tmp3 = MakeReg(MVT::i32);
2151 BuildMI(BB, PPC::ADDI, 2, Tmp2).addReg(Tmp1).addSImm(1);
2152 BuildMI(BB, PPC::AND, 2, Tmp3).addReg(Tmp2).addReg(Tmp1);
2153 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp3).addImm(1)
2154 .addImm(31).addImm(31);
2155 break;
2156 case ISD::SETGT:
2157 Tmp2 = MakeReg(MVT::i32);
Nate Begeman7e7fadd2005-04-07 20:30:01 +00002158 BuildMI(BB, PPC::RLWINM, 4, Tmp2).addReg(Tmp1).addImm(1)
2159 .addImm(31).addImm(31);
2160 BuildMI(BB, PPC::XORI, 2, Result).addReg(Tmp2).addImm(1);
2161 break;
2162 }
2163 return Result;
2164 }
2165 }
2166
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00002167 unsigned CCReg = SelectCC(N, Opc);
Nate Begeman31318e42005-04-01 07:21:30 +00002168 unsigned TrueValue = MakeReg(MVT::i32);
2169 BuildMI(BB, PPC::LI, 1, TrueValue).addSImm(1);
2170 unsigned FalseValue = MakeReg(MVT::i32);
2171 BuildMI(BB, PPC::LI, 1, FalseValue).addSImm(0);
2172
Nate Begeman33162522005-03-29 21:54:38 +00002173 // Create an iterator with which to insert the MBB for copying the false
2174 // value and the MBB to hold the PHI instruction for this SetCC.
2175 MachineBasicBlock *thisMBB = BB;
2176 const BasicBlock *LLVM_BB = BB->getBasicBlock();
2177 ilist<MachineBasicBlock>::iterator It = BB;
2178 ++It;
2179
2180 // thisMBB:
2181 // ...
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00002182 // cmpTY ccX, r1, r2
Nate Begeman33162522005-03-29 21:54:38 +00002183 // %TrueValue = li 1
2184 // bCC sinkMBB
Nate Begeman33162522005-03-29 21:54:38 +00002185 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
2186 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00002187 BuildMI(BB, Opc, 2).addReg(CCReg).addMBB(sinkMBB);
Nate Begeman33162522005-03-29 21:54:38 +00002188 MachineFunction *F = BB->getParent();
2189 F->getBasicBlockList().insert(It, copy0MBB);
2190 F->getBasicBlockList().insert(It, sinkMBB);
2191 // Update machine-CFG edges
2192 BB->addSuccessor(copy0MBB);
2193 BB->addSuccessor(sinkMBB);
2194
2195 // copy0MBB:
2196 // %FalseValue = li 0
2197 // fallthrough
2198 BB = copy0MBB;
Nate Begeman33162522005-03-29 21:54:38 +00002199 // Update machine-CFG edges
2200 BB->addSuccessor(sinkMBB);
2201
2202 // sinkMBB:
2203 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
2204 // ...
2205 BB = sinkMBB;
2206 BuildMI(BB, PPC::PHI, 4, Result).addReg(FalseValue)
2207 .addMBB(copy0MBB).addReg(TrueValue).addMBB(thisMBB);
2208 return Result;
2209 }
2210 assert(0 && "Is this legal?");
2211 return 0;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00002212
Nate Begeman74747862005-03-29 22:24:51 +00002213 case ISD::SELECT: {
Chris Lattner30710192005-04-01 07:10:02 +00002214 unsigned TrueValue = SelectExpr(N.getOperand(1)); //Use if TRUE
2215 unsigned FalseValue = SelectExpr(N.getOperand(2)); //Use if FALSE
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00002216 unsigned CCReg = SelectCC(N.getOperand(0), Opc);
Chris Lattner30710192005-04-01 07:10:02 +00002217
Nate Begeman74747862005-03-29 22:24:51 +00002218 // Create an iterator with which to insert the MBB for copying the false
2219 // value and the MBB to hold the PHI instruction for this SetCC.
2220 MachineBasicBlock *thisMBB = BB;
2221 const BasicBlock *LLVM_BB = BB->getBasicBlock();
2222 ilist<MachineBasicBlock>::iterator It = BB;
2223 ++It;
2224
2225 // thisMBB:
2226 // ...
2227 // TrueVal = ...
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00002228 // cmpTY ccX, r1, r2
Nate Begeman74747862005-03-29 22:24:51 +00002229 // bCC copy1MBB
2230 // fallthrough --> copy0MBB
Nate Begeman74747862005-03-29 22:24:51 +00002231 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
2232 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00002233 BuildMI(BB, Opc, 2).addReg(CCReg).addMBB(sinkMBB);
Nate Begeman74747862005-03-29 22:24:51 +00002234 MachineFunction *F = BB->getParent();
2235 F->getBasicBlockList().insert(It, copy0MBB);
2236 F->getBasicBlockList().insert(It, sinkMBB);
2237 // Update machine-CFG edges
2238 BB->addSuccessor(copy0MBB);
2239 BB->addSuccessor(sinkMBB);
2240
2241 // copy0MBB:
2242 // %FalseValue = ...
2243 // # fallthrough to sinkMBB
2244 BB = copy0MBB;
Nate Begeman74747862005-03-29 22:24:51 +00002245 // Update machine-CFG edges
2246 BB->addSuccessor(sinkMBB);
2247
2248 // sinkMBB:
2249 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
2250 // ...
2251 BB = sinkMBB;
2252 BuildMI(BB, PPC::PHI, 4, Result).addReg(FalseValue)
2253 .addMBB(copy0MBB).addReg(TrueValue).addMBB(thisMBB);
Nate Begeman74747862005-03-29 22:24:51 +00002254 return Result;
2255 }
Nate Begemana9795f82005-03-24 04:41:43 +00002256
2257 case ISD::Constant:
2258 switch (N.getValueType()) {
2259 default: assert(0 && "Cannot use constants of this type!");
2260 case MVT::i1:
2261 BuildMI(BB, PPC::LI, 1, Result)
2262 .addSImm(!cast<ConstantSDNode>(N)->isNullValue());
2263 break;
2264 case MVT::i32:
2265 {
2266 int v = (int)cast<ConstantSDNode>(N)->getSignExtended();
2267 if (v < 32768 && v >= -32768) {
2268 BuildMI(BB, PPC::LI, 1, Result).addSImm(v);
2269 } else {
Nate Begeman5e966612005-03-24 06:28:42 +00002270 Tmp1 = MakeReg(MVT::i32);
2271 BuildMI(BB, PPC::LIS, 1, Tmp1).addSImm(v >> 16);
2272 BuildMI(BB, PPC::ORI, 2, Result).addReg(Tmp1).addImm(v & 0xFFFF);
Nate Begemana9795f82005-03-24 04:41:43 +00002273 }
2274 }
2275 }
2276 return Result;
2277 }
2278
2279 return 0;
2280}
2281
2282void ISel::Select(SDOperand N) {
2283 unsigned Tmp1, Tmp2, Opc;
2284 unsigned opcode = N.getOpcode();
2285
2286 if (!ExprMap.insert(std::make_pair(N, 1)).second)
2287 return; // Already selected.
2288
2289 SDNode *Node = N.Val;
2290
2291 switch (Node->getOpcode()) {
2292 default:
2293 Node->dump(); std::cerr << "\n";
2294 assert(0 && "Node not handled yet!");
2295 case ISD::EntryToken: return; // Noop
2296 case ISD::TokenFactor:
2297 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
2298 Select(Node->getOperand(i));
2299 return;
2300 case ISD::ADJCALLSTACKDOWN:
2301 case ISD::ADJCALLSTACKUP:
2302 Select(N.getOperand(0));
2303 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
2304 Opc = N.getOpcode() == ISD::ADJCALLSTACKDOWN ? PPC::ADJCALLSTACKDOWN :
2305 PPC::ADJCALLSTACKUP;
2306 BuildMI(BB, Opc, 1).addImm(Tmp1);
2307 return;
2308 case ISD::BR: {
2309 MachineBasicBlock *Dest =
2310 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
Nate Begemana9795f82005-03-24 04:41:43 +00002311 Select(N.getOperand(0));
2312 BuildMI(BB, PPC::B, 1).addMBB(Dest);
2313 return;
2314 }
2315 case ISD::BRCOND:
Nate Begemancd08e4c2005-04-09 20:09:12 +00002316 case ISD::BRCONDTWOWAY:
Nate Begemana9795f82005-03-24 04:41:43 +00002317 SelectBranchCC(N);
2318 return;
2319 case ISD::CopyToReg:
2320 Select(N.getOperand(0));
2321 Tmp1 = SelectExpr(N.getOperand(1));
2322 Tmp2 = cast<RegSDNode>(N)->getReg();
2323
2324 if (Tmp1 != Tmp2) {
2325 if (N.getOperand(1).getValueType() == MVT::f64 ||
2326 N.getOperand(1).getValueType() == MVT::f32)
2327 BuildMI(BB, PPC::FMR, 1, Tmp2).addReg(Tmp1);
2328 else
2329 BuildMI(BB, PPC::OR, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
2330 }
2331 return;
2332 case ISD::ImplicitDef:
2333 Select(N.getOperand(0));
2334 BuildMI(BB, PPC::IMPLICIT_DEF, 0, cast<RegSDNode>(N)->getReg());
2335 return;
2336 case ISD::RET:
2337 switch (N.getNumOperands()) {
2338 default:
2339 assert(0 && "Unknown return instruction!");
2340 case 3:
2341 assert(N.getOperand(1).getValueType() == MVT::i32 &&
2342 N.getOperand(2).getValueType() == MVT::i32 &&
2343 "Unknown two-register value!");
2344 Select(N.getOperand(0));
2345 Tmp1 = SelectExpr(N.getOperand(1));
2346 Tmp2 = SelectExpr(N.getOperand(2));
Nate Begeman27523a12005-04-02 00:42:16 +00002347 BuildMI(BB, PPC::OR, 2, PPC::R3).addReg(Tmp2).addReg(Tmp2);
2348 BuildMI(BB, PPC::OR, 2, PPC::R4).addReg(Tmp1).addReg(Tmp1);
Nate Begemana9795f82005-03-24 04:41:43 +00002349 break;
2350 case 2:
2351 Select(N.getOperand(0));
2352 Tmp1 = SelectExpr(N.getOperand(1));
2353 switch (N.getOperand(1).getValueType()) {
2354 default:
2355 assert(0 && "Unknown return type!");
2356 case MVT::f64:
2357 case MVT::f32:
2358 BuildMI(BB, PPC::FMR, 1, PPC::F1).addReg(Tmp1);
2359 break;
2360 case MVT::i32:
2361 BuildMI(BB, PPC::OR, 2, PPC::R3).addReg(Tmp1).addReg(Tmp1);
2362 break;
2363 }
Nate Begeman9e3e1b52005-03-24 23:35:30 +00002364 case 1:
2365 Select(N.getOperand(0));
2366 break;
Nate Begemana9795f82005-03-24 04:41:43 +00002367 }
2368 BuildMI(BB, PPC::BLR, 0); // Just emit a 'ret' instruction
2369 return;
Nate Begemana9795f82005-03-24 04:41:43 +00002370 case ISD::TRUNCSTORE:
2371 case ISD::STORE:
2372 {
2373 SDOperand Chain = N.getOperand(0);
2374 SDOperand Value = N.getOperand(1);
2375 SDOperand Address = N.getOperand(2);
2376 Select(Chain);
2377
2378 Tmp1 = SelectExpr(Value); //value
2379
2380 if (opcode == ISD::STORE) {
2381 switch(Value.getValueType()) {
2382 default: assert(0 && "unknown Type in store");
2383 case MVT::i32: Opc = PPC::STW; break;
2384 case MVT::f64: Opc = PPC::STFD; break;
2385 case MVT::f32: Opc = PPC::STFS; break;
2386 }
2387 } else { //ISD::TRUNCSTORE
2388 switch(cast<MVTSDNode>(Node)->getExtraValueType()) {
2389 default: assert(0 && "unknown Type in store");
Nate Begeman7e7fadd2005-04-07 20:30:01 +00002390 case MVT::i1:
Nate Begemana9795f82005-03-24 04:41:43 +00002391 case MVT::i8: Opc = PPC::STB; break;
2392 case MVT::i16: Opc = PPC::STH; break;
2393 }
2394 }
2395
Nate Begemana7e11a42005-04-01 05:57:17 +00002396 if(Address.getOpcode() == ISD::FrameIndex)
Nate Begemana9795f82005-03-24 04:41:43 +00002397 {
Nate Begeman58f718c2005-03-30 02:23:08 +00002398 Tmp2 = cast<FrameIndexSDNode>(Address)->getIndex();
2399 addFrameReference(BuildMI(BB, Opc, 3).addReg(Tmp1), (int)Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +00002400 }
2401 else
2402 {
2403 int offset;
Nate Begeman04730362005-04-01 04:45:11 +00002404 bool idx = SelectAddr(Address, Tmp2, offset);
2405 if (idx) {
2406 Opc = IndexedOpForOp(Opc);
2407 BuildMI(BB, Opc, 3).addReg(Tmp1).addReg(Tmp2).addReg(offset);
2408 } else {
2409 BuildMI(BB, Opc, 3).addReg(Tmp1).addImm(offset).addReg(Tmp2);
2410 }
Nate Begemana9795f82005-03-24 04:41:43 +00002411 }
2412 return;
2413 }
2414 case ISD::EXTLOAD:
2415 case ISD::SEXTLOAD:
2416 case ISD::ZEXTLOAD:
2417 case ISD::LOAD:
2418 case ISD::CopyFromReg:
2419 case ISD::CALL:
2420 case ISD::DYNAMIC_STACKALLOC:
2421 ExprMap.erase(N);
2422 SelectExpr(N);
2423 return;
2424 }
2425 assert(0 && "Should not be reached!");
2426}
2427
2428
2429/// createPPC32PatternInstructionSelector - This pass converts an LLVM function
2430/// into a machine code representation using pattern matching and a machine
2431/// description file.
2432///
2433FunctionPass *llvm::createPPC32ISelPattern(TargetMachine &TM) {
2434 return new ISel(TM);
Chris Lattner246fa632005-03-24 06:16:18 +00002435}
2436