blob: 6fe2c4ccbfe70f2a626372cd71fcee5ed2b6bacc [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 Begemanf8b02942005-04-15 22:12:16 +00001070 if (PPCCRopts)
Nate Begeman7bfba7d2005-04-14 09:45:08 +00001071 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 }
Nate Begeman9765c252005-04-12 21:22:28 +00001096 Opc = PPC::BNE;
Nate Begemandffcfcc2005-04-01 00:32:34 +00001097 Tmp1 = SelectExpr(CC);
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001098 BuildMI(BB, PPC::CMPLWI, 2, Result).addReg(Tmp1).addImm(0);
Nate Begemandffcfcc2005-04-01 00:32:34 +00001099 }
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001100 return Result;
Nate Begemandffcfcc2005-04-01 00:32:34 +00001101}
1102
1103/// Check to see if the load is a constant offset from a base register
Nate Begeman04730362005-04-01 04:45:11 +00001104bool ISel::SelectAddr(SDOperand N, unsigned& Reg, int& offset)
Nate Begemana9795f82005-03-24 04:41:43 +00001105{
Nate Begeman96fc6812005-03-31 02:05:53 +00001106 unsigned imm = 0, opcode = N.getOpcode();
Nate Begeman04730362005-04-01 04:45:11 +00001107 if (N.getOpcode() == ISD::ADD) {
1108 Reg = SelectExpr(N.getOperand(0));
Nate Begeman439b4442005-04-05 04:22:58 +00001109 if (1 == getImmediateForOpcode(N.getOperand(1), opcode, imm)) {
Nate Begeman96fc6812005-03-31 02:05:53 +00001110 offset = imm;
Nate Begeman04730362005-04-01 04:45:11 +00001111 return false;
1112 }
1113 offset = SelectExpr(N.getOperand(1));
1114 return true;
1115 }
Nate Begemana9795f82005-03-24 04:41:43 +00001116 Reg = SelectExpr(N);
1117 offset = 0;
Nate Begeman04730362005-04-01 04:45:11 +00001118 return false;
Nate Begemana9795f82005-03-24 04:41:43 +00001119}
1120
1121void ISel::SelectBranchCC(SDOperand N)
1122{
Nate Begemana9795f82005-03-24 04:41:43 +00001123 MachineBasicBlock *Dest =
1124 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
Nate Begeman3e897162005-03-31 23:55:40 +00001125
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001126 unsigned Opc, CCReg;
Nate Begemana9795f82005-03-24 04:41:43 +00001127 Select(N.getOperand(0)); //chain
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001128 CCReg = SelectCC(N.getOperand(1), Opc);
Nate Begemanf8b02942005-04-15 22:12:16 +00001129
Nate Begemancd08e4c2005-04-09 20:09:12 +00001130 // Iterate to the next basic block, unless we're already at the end of the
1131 ilist<MachineBasicBlock>::iterator It = BB, E = BB->getParent()->end();
Nate Begeman706471e2005-04-09 23:35:05 +00001132 if (++It == E) It = BB;
Nate Begemancd08e4c2005-04-09 20:09:12 +00001133
1134 // If this is a two way branch, then grab the fallthrough basic block argument
1135 // and build a PowerPC branch pseudo-op, suitable for long branch conversion
1136 // if necessary by the branch selection pass. Otherwise, emit a standard
1137 // conditional branch.
1138 if (N.getOpcode() == ISD::BRCONDTWOWAY) {
1139 MachineBasicBlock *Fallthrough =
1140 cast<BasicBlockSDNode>(N.getOperand(3))->getBasicBlock();
1141 if (Dest != It) {
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001142 BuildMI(BB, PPC::COND_BRANCH, 4).addReg(CCReg).addImm(Opc)
Nate Begemancd08e4c2005-04-09 20:09:12 +00001143 .addMBB(Dest).addMBB(Fallthrough);
1144 if (Fallthrough != It)
1145 BuildMI(BB, PPC::B, 1).addMBB(Fallthrough);
1146 } else {
1147 if (Fallthrough != It) {
1148 Opc = PPC32InstrInfo::invertPPCBranchOpcode(Opc);
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001149 BuildMI(BB, PPC::COND_BRANCH, 4).addReg(CCReg).addImm(Opc)
Nate Begemancd08e4c2005-04-09 20:09:12 +00001150 .addMBB(Fallthrough).addMBB(Dest);
1151 }
1152 }
1153 } else {
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001154 BuildMI(BB, PPC::COND_BRANCH, 4).addReg(CCReg).addImm(Opc)
Nate Begeman27499e32005-04-10 01:48:29 +00001155 .addMBB(Dest).addMBB(It);
Nate Begemancd08e4c2005-04-09 20:09:12 +00001156 }
Nate Begemana9795f82005-03-24 04:41:43 +00001157 return;
1158}
1159
1160unsigned ISel::SelectExprFP(SDOperand N, unsigned Result)
1161{
1162 unsigned Tmp1, Tmp2, Tmp3;
1163 unsigned Opc = 0;
1164 SDNode *Node = N.Val;
1165 MVT::ValueType DestType = N.getValueType();
1166 unsigned opcode = N.getOpcode();
1167
1168 switch (opcode) {
1169 default:
1170 Node->dump();
1171 assert(0 && "Node not handled!\n");
1172
Nate Begeman23afcfb2005-03-29 22:48:55 +00001173 case ISD::SELECT: {
Nate Begeman3e897162005-03-31 23:55:40 +00001174 // Attempt to generate FSEL. We can do this whenever we have an FP result,
1175 // and an FP comparison in the SetCC node.
1176 SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(N.getOperand(0).Val);
1177 if (SetCC && N.getOperand(0).getOpcode() == ISD::SETCC &&
1178 !MVT::isInteger(SetCC->getOperand(0).getValueType()) &&
1179 SetCC->getCondition() != ISD::SETEQ &&
1180 SetCC->getCondition() != ISD::SETNE) {
1181 MVT::ValueType VT = SetCC->getOperand(0).getValueType();
Nate Begeman3e897162005-03-31 23:55:40 +00001182 unsigned TV = SelectExpr(N.getOperand(1)); // Use if TRUE
1183 unsigned FV = SelectExpr(N.getOperand(2)); // Use if FALSE
1184
1185 ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(1));
1186 if (CN && (CN->isExactlyValue(-0.0) || CN->isExactlyValue(0.0))) {
1187 switch(SetCC->getCondition()) {
1188 default: assert(0 && "Invalid FSEL condition"); abort();
1189 case ISD::SETULT:
1190 case ISD::SETLT:
Nate Begemanaf4ab1b2005-04-09 09:33:07 +00001191 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
Nate Begeman3e897162005-03-31 23:55:40 +00001192 case ISD::SETUGE:
1193 case ISD::SETGE:
Nate Begemanaf4ab1b2005-04-09 09:33:07 +00001194 Tmp1 = SelectExpr(SetCC->getOperand(0)); // Val to compare against
Nate Begeman3e897162005-03-31 23:55:40 +00001195 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp1).addReg(TV).addReg(FV);
1196 return Result;
1197 case ISD::SETUGT:
Nate Begemanaf4ab1b2005-04-09 09:33:07 +00001198 case ISD::SETGT:
1199 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
Nate Begeman3e897162005-03-31 23:55:40 +00001200 case ISD::SETULE:
1201 case ISD::SETLE: {
Nate Begemanaf4ab1b2005-04-09 09:33:07 +00001202 if (SetCC->getOperand(0).getOpcode() == ISD::FNEG) {
1203 Tmp2 = SelectExpr(SetCC->getOperand(0).getOperand(0));
1204 } else {
1205 Tmp2 = MakeReg(VT);
1206 Tmp1 = SelectExpr(SetCC->getOperand(0)); // Val to compare against
1207 BuildMI(BB, PPC::FNEG, 1, Tmp2).addReg(Tmp1);
1208 }
Nate Begeman3e897162005-03-31 23:55:40 +00001209 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp2).addReg(TV).addReg(FV);
1210 return Result;
1211 }
1212 }
1213 } else {
1214 Opc = (MVT::f64 == VT) ? PPC::FSUB : PPC::FSUBS;
Nate Begemanaf4ab1b2005-04-09 09:33:07 +00001215 Tmp1 = SelectExpr(SetCC->getOperand(0)); // Val to compare against
Nate Begeman3e897162005-03-31 23:55:40 +00001216 Tmp2 = SelectExpr(SetCC->getOperand(1));
1217 Tmp3 = MakeReg(VT);
1218 switch(SetCC->getCondition()) {
1219 default: assert(0 && "Invalid FSEL condition"); abort();
1220 case ISD::SETULT:
1221 case ISD::SETLT:
1222 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
1223 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(FV).addReg(TV);
1224 return Result;
1225 case ISD::SETUGE:
1226 case ISD::SETGE:
1227 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
1228 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(TV).addReg(FV);
1229 return Result;
1230 case ISD::SETUGT:
1231 case ISD::SETGT:
1232 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp2).addReg(Tmp1);
1233 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(FV).addReg(TV);
1234 return Result;
1235 case ISD::SETULE:
1236 case ISD::SETLE:
1237 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp2).addReg(Tmp1);
1238 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(TV).addReg(FV);
1239 return Result;
1240 }
1241 }
1242 assert(0 && "Should never get here");
1243 return 0;
1244 }
1245
Nate Begeman31318e42005-04-01 07:21:30 +00001246 unsigned TrueValue = SelectExpr(N.getOperand(1)); //Use if TRUE
1247 unsigned FalseValue = SelectExpr(N.getOperand(2)); //Use if FALSE
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001248 unsigned CCReg = SelectCC(N.getOperand(0), Opc);
Nate Begeman31318e42005-04-01 07:21:30 +00001249
Nate Begeman23afcfb2005-03-29 22:48:55 +00001250 // Create an iterator with which to insert the MBB for copying the false
1251 // value and the MBB to hold the PHI instruction for this SetCC.
1252 MachineBasicBlock *thisMBB = BB;
1253 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1254 ilist<MachineBasicBlock>::iterator It = BB;
1255 ++It;
1256
1257 // thisMBB:
1258 // ...
1259 // TrueVal = ...
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001260 // cmpTY ccX, r1, r2
Nate Begeman23afcfb2005-03-29 22:48:55 +00001261 // bCC copy1MBB
1262 // fallthrough --> copy0MBB
Nate Begeman23afcfb2005-03-29 22:48:55 +00001263 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
1264 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001265 BuildMI(BB, Opc, 2).addReg(CCReg).addMBB(sinkMBB);
Nate Begeman23afcfb2005-03-29 22:48:55 +00001266 MachineFunction *F = BB->getParent();
1267 F->getBasicBlockList().insert(It, copy0MBB);
1268 F->getBasicBlockList().insert(It, sinkMBB);
1269 // Update machine-CFG edges
1270 BB->addSuccessor(copy0MBB);
1271 BB->addSuccessor(sinkMBB);
1272
1273 // copy0MBB:
1274 // %FalseValue = ...
1275 // # fallthrough to sinkMBB
1276 BB = copy0MBB;
Nate Begeman23afcfb2005-03-29 22:48:55 +00001277 // Update machine-CFG edges
1278 BB->addSuccessor(sinkMBB);
1279
1280 // sinkMBB:
1281 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1282 // ...
1283 BB = sinkMBB;
1284 BuildMI(BB, PPC::PHI, 4, Result).addReg(FalseValue)
1285 .addMBB(copy0MBB).addReg(TrueValue).addMBB(thisMBB);
1286 return Result;
1287 }
Nate Begeman27eeb002005-04-02 05:59:34 +00001288
1289 case ISD::FNEG:
Nate Begeman93075ec2005-04-04 23:40:36 +00001290 if (!NoExcessFPPrecision &&
1291 ISD::ADD == N.getOperand(0).getOpcode() &&
1292 N.getOperand(0).Val->hasOneUse() &&
1293 ISD::MUL == N.getOperand(0).getOperand(0).getOpcode() &&
1294 N.getOperand(0).getOperand(0).Val->hasOneUse()) {
Nate Begeman80196b12005-04-05 00:15:08 +00001295 ++FusedFP; // Statistic
Nate Begeman93075ec2005-04-04 23:40:36 +00001296 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(0));
1297 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(1));
1298 Tmp3 = SelectExpr(N.getOperand(0).getOperand(1));
1299 Opc = DestType == MVT::f64 ? PPC::FNMADD : PPC::FNMADDS;
1300 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1301 } else if (!NoExcessFPPrecision &&
Nate Begemane88aa5b2005-04-09 03:05:51 +00001302 ISD::ADD == N.getOperand(0).getOpcode() &&
Nate Begeman93075ec2005-04-04 23:40:36 +00001303 N.getOperand(0).Val->hasOneUse() &&
Nate Begemane88aa5b2005-04-09 03:05:51 +00001304 ISD::MUL == N.getOperand(0).getOperand(1).getOpcode() &&
1305 N.getOperand(0).getOperand(1).Val->hasOneUse()) {
Nate Begeman80196b12005-04-05 00:15:08 +00001306 ++FusedFP; // Statistic
Nate Begemane88aa5b2005-04-09 03:05:51 +00001307 Tmp1 = SelectExpr(N.getOperand(0).getOperand(1).getOperand(0));
1308 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1).getOperand(1));
1309 Tmp3 = SelectExpr(N.getOperand(0).getOperand(0));
1310 Opc = DestType == MVT::f64 ? PPC::FNMADD : PPC::FNMADDS;
Nate Begeman93075ec2005-04-04 23:40:36 +00001311 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1312 } else if (ISD::FABS == N.getOperand(0).getOpcode()) {
Nate Begeman27eeb002005-04-02 05:59:34 +00001313 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1314 BuildMI(BB, PPC::FNABS, 1, Result).addReg(Tmp1);
1315 } else {
1316 Tmp1 = SelectExpr(N.getOperand(0));
1317 BuildMI(BB, PPC::FNEG, 1, Result).addReg(Tmp1);
1318 }
1319 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001320
Nate Begeman27eeb002005-04-02 05:59:34 +00001321 case ISD::FABS:
1322 Tmp1 = SelectExpr(N.getOperand(0));
1323 BuildMI(BB, PPC::FABS, 1, Result).addReg(Tmp1);
1324 return Result;
1325
Nate Begemana9795f82005-03-24 04:41:43 +00001326 case ISD::FP_ROUND:
1327 assert (DestType == MVT::f32 &&
1328 N.getOperand(0).getValueType() == MVT::f64 &&
1329 "only f64 to f32 conversion supported here");
1330 Tmp1 = SelectExpr(N.getOperand(0));
1331 BuildMI(BB, PPC::FRSP, 1, Result).addReg(Tmp1);
1332 return Result;
1333
1334 case ISD::FP_EXTEND:
1335 assert (DestType == MVT::f64 &&
1336 N.getOperand(0).getValueType() == MVT::f32 &&
1337 "only f32 to f64 conversion supported here");
1338 Tmp1 = SelectExpr(N.getOperand(0));
1339 BuildMI(BB, PPC::FMR, 1, Result).addReg(Tmp1);
1340 return Result;
1341
1342 case ISD::CopyFromReg:
Nate Begemanf2622612005-03-26 02:17:46 +00001343 if (Result == 1)
1344 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1345 Tmp1 = dyn_cast<RegSDNode>(Node)->getReg();
1346 BuildMI(BB, PPC::FMR, 1, Result).addReg(Tmp1);
1347 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001348
Nate Begeman6d369cc2005-04-01 01:08:07 +00001349 case ISD::ConstantFP: {
Nate Begeman6d369cc2005-04-01 01:08:07 +00001350 ConstantFPSDNode *CN = cast<ConstantFPSDNode>(N);
Nate Begeman6b559972005-04-01 02:59:27 +00001351 Result = getConstDouble(CN->getValue(), Result);
Nate Begeman6d369cc2005-04-01 01:08:07 +00001352 return Result;
1353 }
Nate Begemana9795f82005-03-24 04:41:43 +00001354
Nate Begemana9795f82005-03-24 04:41:43 +00001355 case ISD::ADD:
Nate Begeman93075ec2005-04-04 23:40:36 +00001356 if (!NoExcessFPPrecision && N.getOperand(0).getOpcode() == ISD::MUL &&
1357 N.getOperand(0).Val->hasOneUse()) {
1358 ++FusedFP; // Statistic
1359 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1360 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1361 Tmp3 = SelectExpr(N.getOperand(1));
1362 Opc = DestType == MVT::f64 ? PPC::FMADD : PPC::FMADDS;
1363 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1364 return Result;
1365 }
Nate Begemane88aa5b2005-04-09 03:05:51 +00001366 if (!NoExcessFPPrecision && N.getOperand(1).getOpcode() == ISD::MUL &&
1367 N.getOperand(1).Val->hasOneUse()) {
1368 ++FusedFP; // Statistic
1369 Tmp1 = SelectExpr(N.getOperand(1).getOperand(0));
1370 Tmp2 = SelectExpr(N.getOperand(1).getOperand(1));
1371 Tmp3 = SelectExpr(N.getOperand(0));
1372 Opc = DestType == MVT::f64 ? PPC::FMADD : PPC::FMADDS;
1373 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1374 return Result;
1375 }
Nate Begeman93075ec2005-04-04 23:40:36 +00001376 Opc = DestType == MVT::f64 ? PPC::FADD : PPC::FADDS;
1377 Tmp1 = SelectExpr(N.getOperand(0));
1378 Tmp2 = SelectExpr(N.getOperand(1));
1379 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1380 return Result;
1381
Nate Begemana9795f82005-03-24 04:41:43 +00001382 case ISD::SUB:
Nate Begeman93075ec2005-04-04 23:40:36 +00001383 if (!NoExcessFPPrecision && N.getOperand(0).getOpcode() == ISD::MUL &&
1384 N.getOperand(0).Val->hasOneUse()) {
1385 ++FusedFP; // Statistic
1386 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1387 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1388 Tmp3 = SelectExpr(N.getOperand(1));
1389 Opc = DestType == MVT::f64 ? PPC::FMSUB : PPC::FMSUBS;
1390 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1391 return Result;
1392 }
Nate Begemane88aa5b2005-04-09 03:05:51 +00001393 if (!NoExcessFPPrecision && N.getOperand(1).getOpcode() == ISD::MUL &&
1394 N.getOperand(1).Val->hasOneUse()) {
1395 ++FusedFP; // Statistic
1396 Tmp1 = SelectExpr(N.getOperand(1).getOperand(0));
1397 Tmp2 = SelectExpr(N.getOperand(1).getOperand(1));
1398 Tmp3 = SelectExpr(N.getOperand(0));
1399 Opc = DestType == MVT::f64 ? PPC::FNMSUB : PPC::FNMSUBS;
1400 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1401 return Result;
1402 }
Nate Begeman93075ec2005-04-04 23:40:36 +00001403 Opc = DestType == MVT::f64 ? PPC::FSUB : PPC::FSUBS;
1404 Tmp1 = SelectExpr(N.getOperand(0));
1405 Tmp2 = SelectExpr(N.getOperand(1));
1406 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1407 return Result;
1408
1409 case ISD::MUL:
Nate Begemana9795f82005-03-24 04:41:43 +00001410 case ISD::SDIV:
1411 switch( opcode ) {
1412 case ISD::MUL: Opc = DestType == MVT::f64 ? PPC::FMUL : PPC::FMULS; break;
Nate Begemana9795f82005-03-24 04:41:43 +00001413 case ISD::SDIV: Opc = DestType == MVT::f64 ? PPC::FDIV : PPC::FDIVS; break;
1414 };
Nate Begemana9795f82005-03-24 04:41:43 +00001415 Tmp1 = SelectExpr(N.getOperand(0));
1416 Tmp2 = SelectExpr(N.getOperand(1));
1417 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1418 return Result;
1419
Nate Begemana9795f82005-03-24 04:41:43 +00001420 case ISD::UINT_TO_FP:
Nate Begemanfdcf3412005-03-30 19:38:35 +00001421 case ISD::SINT_TO_FP: {
1422 assert (N.getOperand(0).getValueType() == MVT::i32
1423 && "int to float must operate on i32");
1424 bool IsUnsigned = (ISD::UINT_TO_FP == opcode);
1425 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
1426 Tmp2 = MakeReg(MVT::f64); // temp reg to load the integer value into
1427 Tmp3 = MakeReg(MVT::i32); // temp reg to hold the conversion constant
Nate Begemanfdcf3412005-03-30 19:38:35 +00001428
1429 int FrameIdx = BB->getParent()->getFrameInfo()->CreateStackObject(8, 8);
1430 MachineConstantPool *CP = BB->getParent()->getConstantPool();
1431
Nate Begemanfdcf3412005-03-30 19:38:35 +00001432 if (IsUnsigned) {
Nate Begeman709c8062005-04-10 06:06:10 +00001433 unsigned ConstF = getConstDouble(0x1.000000p52);
Nate Begemanfdcf3412005-03-30 19:38:35 +00001434 // Store the hi & low halves of the fp value, currently in int regs
1435 BuildMI(BB, PPC::LIS, 1, Tmp3).addSImm(0x4330);
1436 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(Tmp3), FrameIdx);
1437 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(Tmp1), FrameIdx, 4);
1438 addFrameReference(BuildMI(BB, PPC::LFD, 2, Tmp2), FrameIdx);
1439 // Generate the return value with a subtract
1440 BuildMI(BB, PPC::FSUB, 2, Result).addReg(Tmp2).addReg(ConstF);
1441 } else {
Nate Begeman709c8062005-04-10 06:06:10 +00001442 unsigned ConstF = getConstDouble(0x1.000008p52);
Nate Begemanfdcf3412005-03-30 19:38:35 +00001443 unsigned TmpL = MakeReg(MVT::i32);
Nate Begemanfdcf3412005-03-30 19:38:35 +00001444 // Store the hi & low halves of the fp value, currently in int regs
1445 BuildMI(BB, PPC::LIS, 1, Tmp3).addSImm(0x4330);
1446 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(Tmp3), FrameIdx);
1447 BuildMI(BB, PPC::XORIS, 2, TmpL).addReg(Tmp1).addImm(0x8000);
1448 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(TmpL), FrameIdx, 4);
1449 addFrameReference(BuildMI(BB, PPC::LFD, 2, Tmp2), FrameIdx);
1450 // Generate the return value with a subtract
1451 BuildMI(BB, PPC::FSUB, 2, Result).addReg(Tmp2).addReg(ConstF);
1452 }
1453 return Result;
1454 }
Nate Begemana9795f82005-03-24 04:41:43 +00001455 }
Nate Begeman6b559972005-04-01 02:59:27 +00001456 assert(0 && "Should never get here");
Nate Begemana9795f82005-03-24 04:41:43 +00001457 return 0;
1458}
1459
Nate Begemanc7bd4822005-04-11 06:34:10 +00001460unsigned ISel::SelectExpr(SDOperand N, bool Recording) {
Nate Begemana9795f82005-03-24 04:41:43 +00001461 unsigned Result;
1462 unsigned Tmp1, Tmp2, Tmp3;
1463 unsigned Opc = 0;
1464 unsigned opcode = N.getOpcode();
1465
1466 SDNode *Node = N.Val;
1467 MVT::ValueType DestType = N.getValueType();
1468
1469 unsigned &Reg = ExprMap[N];
1470 if (Reg) return Reg;
1471
Nate Begeman27eeb002005-04-02 05:59:34 +00001472 switch (N.getOpcode()) {
1473 default:
Nate Begemana9795f82005-03-24 04:41:43 +00001474 Reg = Result = (N.getValueType() != MVT::Other) ?
Nate Begeman27eeb002005-04-02 05:59:34 +00001475 MakeReg(N.getValueType()) : 1;
1476 break;
1477 case ISD::CALL:
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001478 // If this is a call instruction, make sure to prepare ALL of the result
1479 // values as well as the chain.
Nate Begeman27eeb002005-04-02 05:59:34 +00001480 if (Node->getNumValues() == 1)
1481 Reg = Result = 1; // Void call, just a chain.
1482 else {
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001483 Result = MakeReg(Node->getValueType(0));
1484 ExprMap[N.getValue(0)] = Result;
Nate Begeman27eeb002005-04-02 05:59:34 +00001485 for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001486 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
Nate Begeman27eeb002005-04-02 05:59:34 +00001487 ExprMap[SDOperand(Node, Node->getNumValues()-1)] = 1;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001488 }
Nate Begeman27eeb002005-04-02 05:59:34 +00001489 break;
1490 case ISD::ADD_PARTS:
1491 case ISD::SUB_PARTS:
1492 case ISD::SHL_PARTS:
1493 case ISD::SRL_PARTS:
1494 case ISD::SRA_PARTS:
1495 Result = MakeReg(Node->getValueType(0));
1496 ExprMap[N.getValue(0)] = Result;
1497 for (unsigned i = 1, e = N.Val->getNumValues(); i != e; ++i)
1498 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
1499 break;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001500 }
1501
Nate Begemane5846682005-04-04 06:52:38 +00001502 if (ISD::CopyFromReg == opcode)
1503 DestType = N.getValue(0).getValueType();
1504
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001505 if (DestType == MVT::f64 || DestType == MVT::f32)
Nate Begemana0e3e942005-04-10 01:14:13 +00001506 if (ISD::LOAD != opcode && ISD::EXTLOAD != opcode &&
1507 ISD::UNDEF != opcode && ISD::CALL != opcode)
Nate Begeman74d73452005-03-31 00:15:26 +00001508 return SelectExprFP(N, Result);
Nate Begemana9795f82005-03-24 04:41:43 +00001509
1510 switch (opcode) {
1511 default:
1512 Node->dump();
1513 assert(0 && "Node not handled!\n");
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001514 case ISD::UNDEF:
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001515 BuildMI(BB, PPC::IMPLICIT_DEF, 0, Result);
1516 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001517 case ISD::DYNAMIC_STACKALLOC:
Nate Begeman5e966612005-03-24 06:28:42 +00001518 // Generate both result values. FIXME: Need a better commment here?
1519 if (Result != 1)
1520 ExprMap[N.getValue(1)] = 1;
1521 else
1522 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1523
1524 // FIXME: We are currently ignoring the requested alignment for handling
1525 // greater than the stack alignment. This will need to be revisited at some
1526 // point. Align = N.getOperand(2);
1527 if (!isa<ConstantSDNode>(N.getOperand(2)) ||
1528 cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) {
1529 std::cerr << "Cannot allocate stack object with greater alignment than"
1530 << " the stack alignment yet!";
1531 abort();
1532 }
1533 Select(N.getOperand(0));
1534 Tmp1 = SelectExpr(N.getOperand(1));
1535 // Subtract size from stack pointer, thereby allocating some space.
1536 BuildMI(BB, PPC::SUBF, 2, PPC::R1).addReg(Tmp1).addReg(PPC::R1);
1537 // Put a pointer to the space into the result register by copying the SP
1538 BuildMI(BB, PPC::OR, 2, Result).addReg(PPC::R1).addReg(PPC::R1);
1539 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001540
1541 case ISD::ConstantPool:
Nate Begemanca12a2b2005-03-28 22:28:37 +00001542 Tmp1 = cast<ConstantPoolSDNode>(N)->getIndex();
1543 Tmp2 = MakeReg(MVT::i32);
1544 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp2).addReg(getGlobalBaseReg())
1545 .addConstantPoolIndex(Tmp1);
1546 BuildMI(BB, PPC::LA, 2, Result).addReg(Tmp2).addConstantPoolIndex(Tmp1);
1547 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001548
1549 case ISD::FrameIndex:
Nate Begemanf3d08f32005-03-29 00:03:27 +00001550 Tmp1 = cast<FrameIndexSDNode>(N)->getIndex();
Nate Begeman58f718c2005-03-30 02:23:08 +00001551 addFrameReference(BuildMI(BB, PPC::ADDI, 2, Result), (int)Tmp1, 0, false);
Nate Begemanf3d08f32005-03-29 00:03:27 +00001552 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001553
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001554 case ISD::GlobalAddress: {
1555 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
Nate Begemanca12a2b2005-03-28 22:28:37 +00001556 Tmp1 = MakeReg(MVT::i32);
Nate Begemanc7b09f12005-03-25 08:34:25 +00001557 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp1).addReg(getGlobalBaseReg())
1558 .addGlobalAddress(GV);
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001559 if (GV->hasWeakLinkage() || GV->isExternal()) {
1560 BuildMI(BB, PPC::LWZ, 2, Result).addGlobalAddress(GV).addReg(Tmp1);
1561 } else {
1562 BuildMI(BB, PPC::LA, 2, Result).addReg(Tmp1).addGlobalAddress(GV);
1563 }
1564 return Result;
1565 }
1566
Nate Begeman5e966612005-03-24 06:28:42 +00001567 case ISD::LOAD:
Nate Begemana9795f82005-03-24 04:41:43 +00001568 case ISD::EXTLOAD:
1569 case ISD::ZEXTLOAD:
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001570 case ISD::SEXTLOAD: {
Nate Begeman9db505c2005-03-28 19:36:43 +00001571 MVT::ValueType TypeBeingLoaded = (ISD::LOAD == opcode) ?
1572 Node->getValueType(0) : cast<MVTSDNode>(Node)->getExtraValueType();
Nate Begeman74d73452005-03-31 00:15:26 +00001573 bool sext = (ISD::SEXTLOAD == opcode);
Nate Begeman74d73452005-03-31 00:15:26 +00001574
Nate Begeman5e966612005-03-24 06:28:42 +00001575 // Make sure we generate both values.
1576 if (Result != 1)
1577 ExprMap[N.getValue(1)] = 1; // Generate the token
1578 else
1579 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1580
1581 SDOperand Chain = N.getOperand(0);
1582 SDOperand Address = N.getOperand(1);
1583 Select(Chain);
1584
Nate Begeman9db505c2005-03-28 19:36:43 +00001585 switch (TypeBeingLoaded) {
Nate Begeman74d73452005-03-31 00:15:26 +00001586 default: Node->dump(); assert(0 && "Cannot load this type!");
Nate Begeman9db505c2005-03-28 19:36:43 +00001587 case MVT::i1: Opc = PPC::LBZ; break;
1588 case MVT::i8: Opc = PPC::LBZ; break;
1589 case MVT::i16: Opc = sext ? PPC::LHA : PPC::LHZ; break;
1590 case MVT::i32: Opc = PPC::LWZ; break;
Nate Begeman74d73452005-03-31 00:15:26 +00001591 case MVT::f32: Opc = PPC::LFS; break;
1592 case MVT::f64: Opc = PPC::LFD; break;
Nate Begeman5e966612005-03-24 06:28:42 +00001593 }
1594
Nate Begeman74d73452005-03-31 00:15:26 +00001595 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Address)) {
1596 Tmp1 = MakeReg(MVT::i32);
1597 int CPI = CP->getIndex();
1598 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp1).addReg(getGlobalBaseReg())
1599 .addConstantPoolIndex(CPI);
1600 BuildMI(BB, Opc, 2, Result).addConstantPoolIndex(CPI).addReg(Tmp1);
Nate Begeman9db505c2005-03-28 19:36:43 +00001601 }
Nate Begeman74d73452005-03-31 00:15:26 +00001602 else if(Address.getOpcode() == ISD::FrameIndex) {
Nate Begeman58f718c2005-03-30 02:23:08 +00001603 Tmp1 = cast<FrameIndexSDNode>(Address)->getIndex();
1604 addFrameReference(BuildMI(BB, Opc, 2, Result), (int)Tmp1);
Nate Begeman5e966612005-03-24 06:28:42 +00001605 } else {
1606 int offset;
Nate Begeman04730362005-04-01 04:45:11 +00001607 bool idx = SelectAddr(Address, Tmp1, offset);
1608 if (idx) {
1609 Opc = IndexedOpForOp(Opc);
1610 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(offset);
1611 } else {
1612 BuildMI(BB, Opc, 2, Result).addSImm(offset).addReg(Tmp1);
1613 }
Nate Begeman5e966612005-03-24 06:28:42 +00001614 }
1615 return Result;
1616 }
1617
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001618 case ISD::CALL: {
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001619 unsigned GPR_idx = 0, FPR_idx = 0;
1620 static const unsigned GPR[] = {
1621 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
1622 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
1623 };
1624 static const unsigned FPR[] = {
1625 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
1626 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
1627 };
1628
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001629 // Lower the chain for this call.
1630 Select(N.getOperand(0));
1631 ExprMap[N.getValue(Node->getNumValues()-1)] = 1;
Nate Begeman74d73452005-03-31 00:15:26 +00001632
Nate Begemand860aa62005-04-04 22:17:48 +00001633 MachineInstr *CallMI;
1634 // Emit the correct call instruction based on the type of symbol called.
1635 if (GlobalAddressSDNode *GASD =
1636 dyn_cast<GlobalAddressSDNode>(N.getOperand(1))) {
1637 CallMI = BuildMI(PPC::CALLpcrel, 1).addGlobalAddress(GASD->getGlobal(),
1638 true);
1639 } else if (ExternalSymbolSDNode *ESSDN =
1640 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1))) {
1641 CallMI = BuildMI(PPC::CALLpcrel, 1).addExternalSymbol(ESSDN->getSymbol(),
1642 true);
1643 } else {
1644 Tmp1 = SelectExpr(N.getOperand(1));
1645 BuildMI(BB, PPC::OR, 2, PPC::R12).addReg(Tmp1).addReg(Tmp1);
1646 BuildMI(BB, PPC::MTCTR, 1).addReg(PPC::R12);
1647 CallMI = BuildMI(PPC::CALLindirect, 3).addImm(20).addImm(0)
1648 .addReg(PPC::R12);
1649 }
1650
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001651 // Load the register args to virtual regs
1652 std::vector<unsigned> ArgVR;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001653 for(int i = 2, e = Node->getNumOperands(); i < e; ++i)
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001654 ArgVR.push_back(SelectExpr(N.getOperand(i)));
1655
1656 // Copy the virtual registers into the appropriate argument register
1657 for(int i = 0, e = ArgVR.size(); i < e; ++i) {
1658 switch(N.getOperand(i+2).getValueType()) {
1659 default: Node->dump(); assert(0 && "Unknown value type for call");
1660 case MVT::i1:
1661 case MVT::i8:
1662 case MVT::i16:
1663 case MVT::i32:
1664 assert(GPR_idx < 8 && "Too many int args");
Nate Begemand860aa62005-04-04 22:17:48 +00001665 if (N.getOperand(i+2).getOpcode() != ISD::UNDEF) {
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001666 BuildMI(BB, PPC::OR,2,GPR[GPR_idx]).addReg(ArgVR[i]).addReg(ArgVR[i]);
Nate Begemand860aa62005-04-04 22:17:48 +00001667 CallMI->addRegOperand(GPR[GPR_idx], MachineOperand::Use);
1668 }
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001669 ++GPR_idx;
1670 break;
1671 case MVT::f64:
1672 case MVT::f32:
1673 assert(FPR_idx < 13 && "Too many fp args");
1674 BuildMI(BB, PPC::FMR, 1, FPR[FPR_idx]).addReg(ArgVR[i]);
Nate Begemand860aa62005-04-04 22:17:48 +00001675 CallMI->addRegOperand(FPR[FPR_idx], MachineOperand::Use);
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001676 ++FPR_idx;
1677 break;
1678 }
1679 }
Nate Begemand860aa62005-04-04 22:17:48 +00001680
1681 // Put the call instruction in the correct place in the MachineBasicBlock
1682 BB->push_back(CallMI);
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001683
1684 switch (Node->getValueType(0)) {
1685 default: assert(0 && "Unknown value type for call result!");
1686 case MVT::Other: return 1;
1687 case MVT::i1:
1688 case MVT::i8:
1689 case MVT::i16:
1690 case MVT::i32:
Nate Begemane5846682005-04-04 06:52:38 +00001691 if (Node->getValueType(1) == MVT::i32) {
1692 BuildMI(BB, PPC::OR, 2, Result+1).addReg(PPC::R3).addReg(PPC::R3);
1693 BuildMI(BB, PPC::OR, 2, Result).addReg(PPC::R4).addReg(PPC::R4);
1694 } else {
1695 BuildMI(BB, PPC::OR, 2, Result).addReg(PPC::R3).addReg(PPC::R3);
1696 }
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001697 break;
1698 case MVT::f32:
1699 case MVT::f64:
1700 BuildMI(BB, PPC::FMR, 1, Result).addReg(PPC::F1);
1701 break;
1702 }
1703 return Result+N.ResNo;
1704 }
Nate Begemana9795f82005-03-24 04:41:43 +00001705
1706 case ISD::SIGN_EXTEND:
1707 case ISD::SIGN_EXTEND_INREG:
1708 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman9db505c2005-03-28 19:36:43 +00001709 switch(cast<MVTSDNode>(Node)->getExtraValueType()) {
1710 default: Node->dump(); assert(0 && "Unhandled SIGN_EXTEND type"); break;
Nate Begemanc7bd4822005-04-11 06:34:10 +00001711 case MVT::i16:
Nate Begeman9db505c2005-03-28 19:36:43 +00001712 BuildMI(BB, PPC::EXTSH, 1, Result).addReg(Tmp1);
1713 break;
Nate Begemanc7bd4822005-04-11 06:34:10 +00001714 case MVT::i8:
Nate Begeman9db505c2005-03-28 19:36:43 +00001715 BuildMI(BB, PPC::EXTSB, 1, Result).addReg(Tmp1);
1716 break;
Nate Begeman74747862005-03-29 22:24:51 +00001717 case MVT::i1:
1718 BuildMI(BB, PPC::SUBFIC, 2, Result).addReg(Tmp1).addSImm(0);
1719 break;
Nate Begeman9db505c2005-03-28 19:36:43 +00001720 }
Nate Begemana9795f82005-03-24 04:41:43 +00001721 return Result;
1722
Nate Begemana9795f82005-03-24 04:41:43 +00001723 case ISD::CopyFromReg:
1724 if (Result == 1)
1725 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1726 Tmp1 = dyn_cast<RegSDNode>(Node)->getReg();
1727 BuildMI(BB, PPC::OR, 2, Result).addReg(Tmp1).addReg(Tmp1);
1728 return Result;
1729
1730 case ISD::SHL:
Nate Begeman5e966612005-03-24 06:28:42 +00001731 Tmp1 = SelectExpr(N.getOperand(0));
1732 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1733 Tmp2 = CN->getValue() & 0x1F;
Nate Begeman33162522005-03-29 21:54:38 +00001734 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(Tmp2).addImm(0)
Nate Begeman5e966612005-03-24 06:28:42 +00001735 .addImm(31-Tmp2);
1736 } else {
Nate Begeman3664cef2005-04-13 22:14:14 +00001737 Tmp2 = FoldIfWideZeroExtend(N.getOperand(1));
Nate Begeman5e966612005-03-24 06:28:42 +00001738 BuildMI(BB, PPC::SLW, 2, Result).addReg(Tmp1).addReg(Tmp2);
1739 }
1740 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001741
Nate Begeman5e966612005-03-24 06:28:42 +00001742 case ISD::SRL:
1743 Tmp1 = SelectExpr(N.getOperand(0));
1744 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1745 Tmp2 = CN->getValue() & 0x1F;
Nate Begeman33162522005-03-29 21:54:38 +00001746 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(32-Tmp2)
Nate Begeman5e966612005-03-24 06:28:42 +00001747 .addImm(Tmp2).addImm(31);
1748 } else {
Nate Begeman3664cef2005-04-13 22:14:14 +00001749 Tmp2 = FoldIfWideZeroExtend(N.getOperand(1));
Nate Begeman5e966612005-03-24 06:28:42 +00001750 BuildMI(BB, PPC::SRW, 2, Result).addReg(Tmp1).addReg(Tmp2);
1751 }
1752 return Result;
1753
1754 case ISD::SRA:
1755 Tmp1 = SelectExpr(N.getOperand(0));
1756 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1757 Tmp2 = CN->getValue() & 0x1F;
1758 BuildMI(BB, PPC::SRAWI, 2, Result).addReg(Tmp1).addImm(Tmp2);
1759 } else {
Nate Begeman3664cef2005-04-13 22:14:14 +00001760 Tmp2 = FoldIfWideZeroExtend(N.getOperand(1));
Nate Begeman5e966612005-03-24 06:28:42 +00001761 BuildMI(BB, PPC::SRAW, 2, Result).addReg(Tmp1).addReg(Tmp2);
1762 }
1763 return Result;
1764
Nate Begemana9795f82005-03-24 04:41:43 +00001765 case ISD::ADD:
1766 assert (DestType == MVT::i32 && "Only do arithmetic on i32s!");
1767 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman439b4442005-04-05 04:22:58 +00001768 switch(getImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
Nate Begemana9795f82005-03-24 04:41:43 +00001769 default: assert(0 && "unhandled result code");
1770 case 0: // No immediate
1771 Tmp2 = SelectExpr(N.getOperand(1));
1772 BuildMI(BB, PPC::ADD, 2, Result).addReg(Tmp1).addReg(Tmp2);
1773 break;
1774 case 1: // Low immediate
1775 BuildMI(BB, PPC::ADDI, 2, Result).addReg(Tmp1).addSImm(Tmp2);
1776 break;
1777 case 2: // Shifted immediate
1778 BuildMI(BB, PPC::ADDIS, 2, Result).addReg(Tmp1).addSImm(Tmp2);
1779 break;
1780 }
1781 return Result;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001782
Nate Begemana9795f82005-03-24 04:41:43 +00001783 case ISD::AND:
Nate Begeman7ddecb42005-04-06 23:51:40 +00001784 Tmp1 = SelectExpr(N.getOperand(0));
1785 // FIXME: should add check in getImmediateForOpcode to return a value
1786 // indicating the immediate is a run of set bits so we can emit a bitfield
1787 // clear with RLWINM instead.
1788 switch(getImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
1789 default: assert(0 && "unhandled result code");
1790 case 0: // No immediate
1791 Tmp2 = SelectExpr(N.getOperand(1));
Nate Begemanc7bd4822005-04-11 06:34:10 +00001792 Opc = Recording ? PPC::ANDo : PPC::AND;
1793 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
Nate Begeman7ddecb42005-04-06 23:51:40 +00001794 break;
1795 case 1: // Low immediate
1796 BuildMI(BB, PPC::ANDIo, 2, Result).addReg(Tmp1).addImm(Tmp2);
1797 break;
1798 case 2: // Shifted immediate
1799 BuildMI(BB, PPC::ANDISo, 2, Result).addReg(Tmp1).addImm(Tmp2);
1800 break;
Nate Begeman9f833d32005-04-12 00:10:02 +00001801 case 5: // Bitfield mask
1802 Opc = Recording ? PPC::RLWINMo : PPC::RLWINM;
1803 Tmp3 = Tmp2 >> 16; // MB
1804 Tmp2 &= 0xFFFF; // ME
1805 BuildMI(BB, Opc, 4, Result).addReg(Tmp1).addImm(0)
1806 .addImm(Tmp3).addImm(Tmp2);
1807 break;
Nate Begeman7ddecb42005-04-06 23:51:40 +00001808 }
Nate Begemanc7bd4822005-04-11 06:34:10 +00001809 RecordSuccess = true;
Nate Begeman7ddecb42005-04-06 23:51:40 +00001810 return Result;
1811
Nate Begemana9795f82005-03-24 04:41:43 +00001812 case ISD::OR:
Nate Begeman7ddecb42005-04-06 23:51:40 +00001813 if (SelectBitfieldInsert(N, Result))
1814 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001815 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman439b4442005-04-05 04:22:58 +00001816 switch(getImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
Nate Begemana9795f82005-03-24 04:41:43 +00001817 default: assert(0 && "unhandled result code");
1818 case 0: // No immediate
1819 Tmp2 = SelectExpr(N.getOperand(1));
Nate Begemanc7bd4822005-04-11 06:34:10 +00001820 Opc = Recording ? PPC::ORo : PPC::OR;
1821 RecordSuccess = true;
1822 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +00001823 break;
1824 case 1: // Low immediate
Nate Begeman7ddecb42005-04-06 23:51:40 +00001825 BuildMI(BB, PPC::ORI, 2, Result).addReg(Tmp1).addImm(Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +00001826 break;
1827 case 2: // Shifted immediate
Nate Begeman7ddecb42005-04-06 23:51:40 +00001828 BuildMI(BB, PPC::ORIS, 2, Result).addReg(Tmp1).addImm(Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +00001829 break;
1830 }
1831 return Result;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001832
Nate Begemanaa73a9f2005-04-03 11:20:20 +00001833 case ISD::XOR: {
1834 // Check for EQV: xor, (xor a, -1), b
1835 if (N.getOperand(0).getOpcode() == ISD::XOR &&
1836 N.getOperand(0).getOperand(1).getOpcode() == ISD::Constant &&
1837 cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->isAllOnesValue()) {
Nate Begemanaa73a9f2005-04-03 11:20:20 +00001838 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1839 Tmp2 = SelectExpr(N.getOperand(1));
1840 BuildMI(BB, PPC::EQV, 2, Result).addReg(Tmp1).addReg(Tmp2);
1841 return Result;
1842 }
1843 // Check for NOT, NOR, and NAND: xor (copy, or, and), -1
1844 if (N.getOperand(1).getOpcode() == ISD::Constant &&
1845 cast<ConstantSDNode>(N.getOperand(1))->isAllOnesValue()) {
Nate Begemanaa73a9f2005-04-03 11:20:20 +00001846 switch(N.getOperand(0).getOpcode()) {
1847 case ISD::OR:
1848 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1849 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1850 BuildMI(BB, PPC::NOR, 2, Result).addReg(Tmp1).addReg(Tmp2);
1851 break;
1852 case ISD::AND:
1853 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1854 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1855 BuildMI(BB, PPC::NAND, 2, Result).addReg(Tmp1).addReg(Tmp2);
1856 break;
1857 default:
1858 Tmp1 = SelectExpr(N.getOperand(0));
1859 BuildMI(BB, PPC::NOR, 2, Result).addReg(Tmp1).addReg(Tmp1);
1860 break;
1861 }
1862 return Result;
1863 }
1864 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman439b4442005-04-05 04:22:58 +00001865 switch(getImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
Nate Begemanaa73a9f2005-04-03 11:20:20 +00001866 default: assert(0 && "unhandled result code");
1867 case 0: // No immediate
1868 Tmp2 = SelectExpr(N.getOperand(1));
1869 BuildMI(BB, PPC::XOR, 2, Result).addReg(Tmp1).addReg(Tmp2);
1870 break;
1871 case 1: // Low immediate
1872 BuildMI(BB, PPC::XORI, 2, Result).addReg(Tmp1).addImm(Tmp2);
1873 break;
1874 case 2: // Shifted immediate
1875 BuildMI(BB, PPC::XORIS, 2, Result).addReg(Tmp1).addImm(Tmp2);
1876 break;
1877 }
1878 return Result;
1879 }
1880
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001881 case ISD::SUB:
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001882 Tmp2 = SelectExpr(N.getOperand(1));
Nate Begeman439b4442005-04-05 04:22:58 +00001883 if (1 == getImmediateForOpcode(N.getOperand(0), opcode, Tmp1))
Nate Begeman27523a12005-04-02 00:42:16 +00001884 BuildMI(BB, PPC::SUBFIC, 2, Result).addReg(Tmp2).addSImm(Tmp1);
1885 else {
1886 Tmp1 = SelectExpr(N.getOperand(0));
1887 BuildMI(BB, PPC::SUBF, 2, Result).addReg(Tmp2).addReg(Tmp1);
1888 }
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001889 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001890
Nate Begeman5e966612005-03-24 06:28:42 +00001891 case ISD::MUL:
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001892 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman439b4442005-04-05 04:22:58 +00001893 if (1 == getImmediateForOpcode(N.getOperand(1), opcode, Tmp2))
Nate Begeman307e7442005-03-26 01:28:53 +00001894 BuildMI(BB, PPC::MULLI, 2, Result).addReg(Tmp1).addSImm(Tmp2);
1895 else {
1896 Tmp2 = SelectExpr(N.getOperand(1));
1897 BuildMI(BB, PPC::MULLW, 2, Result).addReg(Tmp1).addReg(Tmp2);
1898 }
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001899 return Result;
1900
Nate Begeman815d6da2005-04-06 00:25:27 +00001901 case ISD::MULHS:
1902 case ISD::MULHU:
1903 Tmp1 = SelectExpr(N.getOperand(0));
1904 Tmp2 = SelectExpr(N.getOperand(1));
1905 Opc = (ISD::MULHU == opcode) ? PPC::MULHWU : PPC::MULHW;
1906 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1907 return Result;
1908
Nate Begemanf3d08f32005-03-29 00:03:27 +00001909 case ISD::SDIV:
1910 case ISD::UDIV:
Nate Begeman815d6da2005-04-06 00:25:27 +00001911 switch (getImmediateForOpcode(N.getOperand(1), opcode, Tmp3)) {
1912 default: break;
1913 // If this is an sdiv by a power of two, we can use an srawi/addze pair.
1914 case 3:
Nate Begeman80196b12005-04-05 00:15:08 +00001915 Tmp1 = MakeReg(MVT::i32);
1916 Tmp2 = SelectExpr(N.getOperand(0));
Nate Begeman9f833d32005-04-12 00:10:02 +00001917 if ((int)Tmp3 < 0) {
1918 unsigned Tmp4 = MakeReg(MVT::i32);
1919 BuildMI(BB, PPC::SRAWI, 2, Tmp1).addReg(Tmp2).addImm(-Tmp3);
1920 BuildMI(BB, PPC::ADDZE, 1, Tmp4).addReg(Tmp1);
1921 BuildMI(BB, PPC::NEG, 1, Result).addReg(Tmp4);
1922 } else {
1923 BuildMI(BB, PPC::SRAWI, 2, Tmp1).addReg(Tmp2).addImm(Tmp3);
1924 BuildMI(BB, PPC::ADDZE, 1, Result).addReg(Tmp1);
1925 }
Nate Begeman80196b12005-04-05 00:15:08 +00001926 return Result;
Nate Begeman815d6da2005-04-06 00:25:27 +00001927 // If this is a divide by constant, we can emit code using some magic
1928 // constants to implement it as a multiply instead.
Nate Begeman27b4c232005-04-06 06:44:57 +00001929 case 4:
1930 ExprMap.erase(N);
1931 if (opcode == ISD::SDIV)
1932 return SelectExpr(BuildSDIVSequence(N));
1933 else
1934 return SelectExpr(BuildUDIVSequence(N));
Nate Begeman80196b12005-04-05 00:15:08 +00001935 }
Nate Begemanf3d08f32005-03-29 00:03:27 +00001936 Tmp1 = SelectExpr(N.getOperand(0));
1937 Tmp2 = SelectExpr(N.getOperand(1));
1938 Opc = (ISD::UDIV == opcode) ? PPC::DIVWU : PPC::DIVW;
1939 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1940 return Result;
1941
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001942 case ISD::ADD_PARTS:
Nate Begemanca12a2b2005-03-28 22:28:37 +00001943 case ISD::SUB_PARTS: {
1944 assert(N.getNumOperands() == 4 && N.getValueType() == MVT::i32 &&
1945 "Not an i64 add/sub!");
1946 // Emit all of the operands.
1947 std::vector<unsigned> InVals;
1948 for (unsigned i = 0, e = N.getNumOperands(); i != e; ++i)
1949 InVals.push_back(SelectExpr(N.getOperand(i)));
1950 if (N.getOpcode() == ISD::ADD_PARTS) {
Nate Begeman27eeb002005-04-02 05:59:34 +00001951 BuildMI(BB, PPC::ADDC, 2, Result).addReg(InVals[0]).addReg(InVals[2]);
1952 BuildMI(BB, PPC::ADDE, 2, Result+1).addReg(InVals[1]).addReg(InVals[3]);
Nate Begemanca12a2b2005-03-28 22:28:37 +00001953 } else {
Nate Begeman27eeb002005-04-02 05:59:34 +00001954 BuildMI(BB, PPC::SUBFC, 2, Result).addReg(InVals[2]).addReg(InVals[0]);
1955 BuildMI(BB, PPC::SUBFE, 2, Result+1).addReg(InVals[3]).addReg(InVals[1]);
1956 }
1957 return Result+N.ResNo;
1958 }
1959
1960 case ISD::SHL_PARTS:
1961 case ISD::SRA_PARTS:
1962 case ISD::SRL_PARTS: {
1963 assert(N.getNumOperands() == 3 && N.getValueType() == MVT::i32 &&
1964 "Not an i64 shift!");
1965 unsigned ShiftOpLo = SelectExpr(N.getOperand(0));
1966 unsigned ShiftOpHi = SelectExpr(N.getOperand(1));
Nate Begeman3664cef2005-04-13 22:14:14 +00001967 unsigned SHReg = FoldIfWideZeroExtend(N.getOperand(2));
1968 Tmp1 = MakeReg(MVT::i32);
1969 Tmp2 = MakeReg(MVT::i32);
Nate Begeman27eeb002005-04-02 05:59:34 +00001970 Tmp3 = MakeReg(MVT::i32);
1971 unsigned Tmp4 = MakeReg(MVT::i32);
1972 unsigned Tmp5 = MakeReg(MVT::i32);
1973 unsigned Tmp6 = MakeReg(MVT::i32);
1974 BuildMI(BB, PPC::SUBFIC, 2, Tmp1).addReg(SHReg).addSImm(32);
1975 if (ISD::SHL_PARTS == opcode) {
1976 BuildMI(BB, PPC::SLW, 2, Tmp2).addReg(ShiftOpHi).addReg(SHReg);
1977 BuildMI(BB, PPC::SRW, 2, Tmp3).addReg(ShiftOpLo).addReg(Tmp1);
1978 BuildMI(BB, PPC::OR, 2, Tmp4).addReg(Tmp2).addReg(Tmp3);
1979 BuildMI(BB, PPC::ADDI, 2, Tmp5).addReg(SHReg).addSImm(-32);
Nate Begemanfa554702005-04-03 22:13:27 +00001980 BuildMI(BB, PPC::SLW, 2, Tmp6).addReg(ShiftOpLo).addReg(Tmp5);
Nate Begeman27eeb002005-04-02 05:59:34 +00001981 BuildMI(BB, PPC::OR, 2, Result+1).addReg(Tmp4).addReg(Tmp6);
1982 BuildMI(BB, PPC::SLW, 2, Result).addReg(ShiftOpLo).addReg(SHReg);
1983 } else if (ISD::SRL_PARTS == opcode) {
1984 BuildMI(BB, PPC::SRW, 2, Tmp2).addReg(ShiftOpLo).addReg(SHReg);
1985 BuildMI(BB, PPC::SLW, 2, Tmp3).addReg(ShiftOpHi).addReg(Tmp1);
1986 BuildMI(BB, PPC::OR, 2, Tmp4).addReg(Tmp2).addReg(Tmp3);
1987 BuildMI(BB, PPC::ADDI, 2, Tmp5).addReg(SHReg).addSImm(-32);
1988 BuildMI(BB, PPC::SRW, 2, Tmp6).addReg(ShiftOpHi).addReg(Tmp5);
1989 BuildMI(BB, PPC::OR, 2, Result).addReg(Tmp4).addReg(Tmp6);
1990 BuildMI(BB, PPC::SRW, 2, Result+1).addReg(ShiftOpHi).addReg(SHReg);
1991 } else {
1992 MachineBasicBlock *TmpMBB = new MachineBasicBlock(BB->getBasicBlock());
1993 MachineBasicBlock *PhiMBB = new MachineBasicBlock(BB->getBasicBlock());
1994 MachineBasicBlock *OldMBB = BB;
1995 MachineFunction *F = BB->getParent();
1996 ilist<MachineBasicBlock>::iterator It = BB; ++It;
1997 F->getBasicBlockList().insert(It, TmpMBB);
1998 F->getBasicBlockList().insert(It, PhiMBB);
1999 BB->addSuccessor(TmpMBB);
2000 BB->addSuccessor(PhiMBB);
2001 BuildMI(BB, PPC::SRW, 2, Tmp2).addReg(ShiftOpLo).addReg(SHReg);
2002 BuildMI(BB, PPC::SLW, 2, Tmp3).addReg(ShiftOpHi).addReg(Tmp1);
2003 BuildMI(BB, PPC::OR, 2, Tmp4).addReg(Tmp2).addReg(Tmp3);
2004 BuildMI(BB, PPC::ADDICo, 2, Tmp5).addReg(SHReg).addSImm(-32);
2005 BuildMI(BB, PPC::SRAW, 2, Tmp6).addReg(ShiftOpHi).addReg(Tmp5);
2006 BuildMI(BB, PPC::SRAW, 2, Result+1).addReg(ShiftOpHi).addReg(SHReg);
2007 BuildMI(BB, PPC::BLE, 2).addReg(PPC::CR0).addMBB(PhiMBB);
2008 // Select correct least significant half if the shift amount > 32
2009 BB = TmpMBB;
2010 unsigned Tmp7 = MakeReg(MVT::i32);
2011 BuildMI(BB, PPC::OR, 2, Tmp7).addReg(Tmp6).addReg(Tmp6);
2012 TmpMBB->addSuccessor(PhiMBB);
2013 BB = PhiMBB;
2014 BuildMI(BB, PPC::PHI, 4, Result).addReg(Tmp4).addMBB(OldMBB)
2015 .addReg(Tmp7).addMBB(TmpMBB);
Nate Begemanca12a2b2005-03-28 22:28:37 +00002016 }
2017 return Result+N.ResNo;
2018 }
2019
Nate Begemana9795f82005-03-24 04:41:43 +00002020 case ISD::FP_TO_UINT:
Nate Begeman6b559972005-04-01 02:59:27 +00002021 case ISD::FP_TO_SINT: {
2022 bool U = (ISD::FP_TO_UINT == opcode);
2023 Tmp1 = SelectExpr(N.getOperand(0));
2024 if (!U) {
2025 Tmp2 = MakeReg(MVT::f64);
2026 BuildMI(BB, PPC::FCTIWZ, 1, Tmp2).addReg(Tmp1);
2027 int FrameIdx = BB->getParent()->getFrameInfo()->CreateStackObject(8, 8);
2028 addFrameReference(BuildMI(BB, PPC::STFD, 3).addReg(Tmp2), FrameIdx);
2029 addFrameReference(BuildMI(BB, PPC::LWZ, 2, Result), FrameIdx, 4);
2030 return Result;
2031 } else {
2032 unsigned Zero = getConstDouble(0.0);
2033 unsigned MaxInt = getConstDouble((1LL << 32) - 1);
2034 unsigned Border = getConstDouble(1LL << 31);
2035 unsigned UseZero = MakeReg(MVT::f64);
2036 unsigned UseMaxInt = MakeReg(MVT::f64);
2037 unsigned UseChoice = MakeReg(MVT::f64);
2038 unsigned TmpReg = MakeReg(MVT::f64);
2039 unsigned TmpReg2 = MakeReg(MVT::f64);
2040 unsigned ConvReg = MakeReg(MVT::f64);
2041 unsigned IntTmp = MakeReg(MVT::i32);
2042 unsigned XorReg = MakeReg(MVT::i32);
2043 MachineFunction *F = BB->getParent();
2044 int FrameIdx = F->getFrameInfo()->CreateStackObject(8, 8);
2045 // Update machine-CFG edges
2046 MachineBasicBlock *XorMBB = new MachineBasicBlock(BB->getBasicBlock());
2047 MachineBasicBlock *PhiMBB = new MachineBasicBlock(BB->getBasicBlock());
2048 MachineBasicBlock *OldMBB = BB;
2049 ilist<MachineBasicBlock>::iterator It = BB; ++It;
2050 F->getBasicBlockList().insert(It, XorMBB);
2051 F->getBasicBlockList().insert(It, PhiMBB);
2052 BB->addSuccessor(XorMBB);
2053 BB->addSuccessor(PhiMBB);
2054 // Convert from floating point to unsigned 32-bit value
2055 // Use 0 if incoming value is < 0.0
2056 BuildMI(BB, PPC::FSEL, 3, UseZero).addReg(Tmp1).addReg(Tmp1).addReg(Zero);
2057 // Use 2**32 - 1 if incoming value is >= 2**32
2058 BuildMI(BB, PPC::FSUB, 2, UseMaxInt).addReg(MaxInt).addReg(Tmp1);
2059 BuildMI(BB, PPC::FSEL, 3, UseChoice).addReg(UseMaxInt).addReg(UseZero)
2060 .addReg(MaxInt);
2061 // Subtract 2**31
2062 BuildMI(BB, PPC::FSUB, 2, TmpReg).addReg(UseChoice).addReg(Border);
2063 // Use difference if >= 2**31
2064 BuildMI(BB, PPC::FCMPU, 2, PPC::CR0).addReg(UseChoice).addReg(Border);
2065 BuildMI(BB, PPC::FSEL, 3, TmpReg2).addReg(TmpReg).addReg(TmpReg)
2066 .addReg(UseChoice);
2067 // Convert to integer
2068 BuildMI(BB, PPC::FCTIWZ, 1, ConvReg).addReg(TmpReg2);
2069 addFrameReference(BuildMI(BB, PPC::STFD, 3).addReg(ConvReg), FrameIdx);
2070 addFrameReference(BuildMI(BB, PPC::LWZ, 2, IntTmp), FrameIdx, 4);
2071 BuildMI(BB, PPC::BLT, 2).addReg(PPC::CR0).addMBB(PhiMBB);
2072 BuildMI(BB, PPC::B, 1).addMBB(XorMBB);
2073
2074 // XorMBB:
2075 // add 2**31 if input was >= 2**31
2076 BB = XorMBB;
2077 BuildMI(BB, PPC::XORIS, 2, XorReg).addReg(IntTmp).addImm(0x8000);
2078 XorMBB->addSuccessor(PhiMBB);
2079
2080 // PhiMBB:
2081 // DestReg = phi [ IntTmp, OldMBB ], [ XorReg, XorMBB ]
2082 BB = PhiMBB;
2083 BuildMI(BB, PPC::PHI, 4, Result).addReg(IntTmp).addMBB(OldMBB)
2084 .addReg(XorReg).addMBB(XorMBB);
2085 return Result;
2086 }
2087 assert(0 && "Should never get here");
2088 return 0;
2089 }
Nate Begemana9795f82005-03-24 04:41:43 +00002090
Nate Begeman9e3e1b52005-03-24 23:35:30 +00002091 case ISD::SETCC:
Nate Begeman33162522005-03-29 21:54:38 +00002092 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Node)) {
Nate Begeman7e7fadd2005-04-07 20:30:01 +00002093 if (ConstantSDNode *CN =
2094 dyn_cast<ConstantSDNode>(SetCC->getOperand(1).Val)) {
Nate Begeman9765c252005-04-12 21:22:28 +00002095 // We can codegen setcc op, imm very efficiently compared to a brcond.
2096 // Check for those cases here.
2097 // setcc op, 0
Nate Begeman7e7fadd2005-04-07 20:30:01 +00002098 if (CN->getValue() == 0) {
2099 Tmp1 = SelectExpr(SetCC->getOperand(0));
2100 switch (SetCC->getCondition()) {
Nate Begeman7bfba7d2005-04-14 09:45:08 +00002101 default: SetCC->dump(); assert(0 && "Unhandled SetCC condition"); abort();
Nate Begeman7e7fadd2005-04-07 20:30:01 +00002102 case ISD::SETEQ:
Nate Begeman7e7fadd2005-04-07 20:30:01 +00002103 Tmp2 = MakeReg(MVT::i32);
2104 BuildMI(BB, PPC::CNTLZW, 1, Tmp2).addReg(Tmp1);
2105 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp2).addImm(27)
2106 .addImm(5).addImm(31);
2107 break;
2108 case ISD::SETNE:
Nate Begeman7e7fadd2005-04-07 20:30:01 +00002109 Tmp2 = MakeReg(MVT::i32);
2110 BuildMI(BB, PPC::ADDIC, 2, Tmp2).addReg(Tmp1).addSImm(-1);
2111 BuildMI(BB, PPC::SUBFE, 2, Result).addReg(Tmp2).addReg(Tmp1);
2112 break;
Nate Begeman7e7fadd2005-04-07 20:30:01 +00002113 case ISD::SETLT:
2114 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(1)
2115 .addImm(31).addImm(31);
2116 break;
Nate Begeman7e7fadd2005-04-07 20:30:01 +00002117 case ISD::SETGT:
2118 Tmp2 = MakeReg(MVT::i32);
2119 Tmp3 = MakeReg(MVT::i32);
2120 BuildMI(BB, PPC::NEG, 2, Tmp2).addReg(Tmp1);
2121 BuildMI(BB, PPC::ANDC, 2, Tmp3).addReg(Tmp2).addReg(Tmp1);
2122 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp3).addImm(1)
2123 .addImm(31).addImm(31);
2124 break;
Nate Begeman9765c252005-04-12 21:22:28 +00002125 }
2126 return Result;
2127 }
2128 // setcc op, -1
2129 if (CN->isAllOnesValue()) {
2130 Tmp1 = SelectExpr(SetCC->getOperand(0));
2131 switch (SetCC->getCondition()) {
2132 default: assert(0 && "Unhandled SetCC condition"); abort();
2133 case ISD::SETEQ:
2134 Tmp2 = MakeReg(MVT::i32);
2135 Tmp3 = MakeReg(MVT::i32);
2136 BuildMI(BB, PPC::ADDIC, 2, Tmp2).addReg(Tmp1).addSImm(1);
2137 BuildMI(BB, PPC::LI, 1, Tmp3).addSImm(0);
2138 BuildMI(BB, PPC::ADDZE, 1, Result).addReg(Tmp3);
Nate Begeman7e7fadd2005-04-07 20:30:01 +00002139 break;
Nate Begeman9765c252005-04-12 21:22:28 +00002140 case ISD::SETNE:
2141 Tmp2 = MakeReg(MVT::i32);
2142 Tmp3 = MakeReg(MVT::i32);
2143 BuildMI(BB, PPC::NOR, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
2144 BuildMI(BB, PPC::ADDIC, 2, Tmp3).addReg(Tmp2).addSImm(-1);
2145 BuildMI(BB, PPC::SUBFE, 2, Result).addReg(Tmp3).addReg(Tmp2);
2146 break;
2147 case ISD::SETLT:
2148 Tmp2 = MakeReg(MVT::i32);
2149 Tmp3 = MakeReg(MVT::i32);
2150 BuildMI(BB, PPC::ADDI, 2, Tmp2).addReg(Tmp1).addSImm(1);
2151 BuildMI(BB, PPC::AND, 2, Tmp3).addReg(Tmp2).addReg(Tmp1);
2152 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp3).addImm(1)
2153 .addImm(31).addImm(31);
2154 break;
2155 case ISD::SETGT:
2156 Tmp2 = MakeReg(MVT::i32);
Nate Begeman7e7fadd2005-04-07 20:30:01 +00002157 BuildMI(BB, PPC::RLWINM, 4, Tmp2).addReg(Tmp1).addImm(1)
2158 .addImm(31).addImm(31);
2159 BuildMI(BB, PPC::XORI, 2, Result).addReg(Tmp2).addImm(1);
2160 break;
2161 }
2162 return Result;
2163 }
2164 }
2165
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00002166 unsigned CCReg = SelectCC(N, Opc);
Nate Begeman31318e42005-04-01 07:21:30 +00002167 unsigned TrueValue = MakeReg(MVT::i32);
2168 BuildMI(BB, PPC::LI, 1, TrueValue).addSImm(1);
2169 unsigned FalseValue = MakeReg(MVT::i32);
2170 BuildMI(BB, PPC::LI, 1, FalseValue).addSImm(0);
2171
Nate Begeman33162522005-03-29 21:54:38 +00002172 // Create an iterator with which to insert the MBB for copying the false
2173 // value and the MBB to hold the PHI instruction for this SetCC.
2174 MachineBasicBlock *thisMBB = BB;
2175 const BasicBlock *LLVM_BB = BB->getBasicBlock();
2176 ilist<MachineBasicBlock>::iterator It = BB;
2177 ++It;
2178
2179 // thisMBB:
2180 // ...
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00002181 // cmpTY ccX, r1, r2
Nate Begeman33162522005-03-29 21:54:38 +00002182 // %TrueValue = li 1
2183 // bCC sinkMBB
Nate Begeman33162522005-03-29 21:54:38 +00002184 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
2185 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00002186 BuildMI(BB, Opc, 2).addReg(CCReg).addMBB(sinkMBB);
Nate Begeman33162522005-03-29 21:54:38 +00002187 MachineFunction *F = BB->getParent();
2188 F->getBasicBlockList().insert(It, copy0MBB);
2189 F->getBasicBlockList().insert(It, sinkMBB);
2190 // Update machine-CFG edges
2191 BB->addSuccessor(copy0MBB);
2192 BB->addSuccessor(sinkMBB);
2193
2194 // copy0MBB:
2195 // %FalseValue = li 0
2196 // fallthrough
2197 BB = copy0MBB;
Nate Begeman33162522005-03-29 21:54:38 +00002198 // Update machine-CFG edges
2199 BB->addSuccessor(sinkMBB);
2200
2201 // sinkMBB:
2202 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
2203 // ...
2204 BB = sinkMBB;
2205 BuildMI(BB, PPC::PHI, 4, Result).addReg(FalseValue)
2206 .addMBB(copy0MBB).addReg(TrueValue).addMBB(thisMBB);
2207 return Result;
2208 }
2209 assert(0 && "Is this legal?");
2210 return 0;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00002211
Nate Begeman74747862005-03-29 22:24:51 +00002212 case ISD::SELECT: {
Chris Lattner30710192005-04-01 07:10:02 +00002213 unsigned TrueValue = SelectExpr(N.getOperand(1)); //Use if TRUE
2214 unsigned FalseValue = SelectExpr(N.getOperand(2)); //Use if FALSE
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00002215 unsigned CCReg = SelectCC(N.getOperand(0), Opc);
Chris Lattner30710192005-04-01 07:10:02 +00002216
Nate Begeman74747862005-03-29 22:24:51 +00002217 // Create an iterator with which to insert the MBB for copying the false
2218 // value and the MBB to hold the PHI instruction for this SetCC.
2219 MachineBasicBlock *thisMBB = BB;
2220 const BasicBlock *LLVM_BB = BB->getBasicBlock();
2221 ilist<MachineBasicBlock>::iterator It = BB;
2222 ++It;
2223
2224 // thisMBB:
2225 // ...
2226 // TrueVal = ...
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00002227 // cmpTY ccX, r1, r2
Nate Begeman74747862005-03-29 22:24:51 +00002228 // bCC copy1MBB
2229 // fallthrough --> copy0MBB
Nate Begeman74747862005-03-29 22:24:51 +00002230 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
2231 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00002232 BuildMI(BB, Opc, 2).addReg(CCReg).addMBB(sinkMBB);
Nate Begeman74747862005-03-29 22:24:51 +00002233 MachineFunction *F = BB->getParent();
2234 F->getBasicBlockList().insert(It, copy0MBB);
2235 F->getBasicBlockList().insert(It, sinkMBB);
2236 // Update machine-CFG edges
2237 BB->addSuccessor(copy0MBB);
2238 BB->addSuccessor(sinkMBB);
2239
2240 // copy0MBB:
2241 // %FalseValue = ...
2242 // # fallthrough to sinkMBB
2243 BB = copy0MBB;
Nate Begeman74747862005-03-29 22:24:51 +00002244 // Update machine-CFG edges
2245 BB->addSuccessor(sinkMBB);
2246
2247 // sinkMBB:
2248 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
2249 // ...
2250 BB = sinkMBB;
2251 BuildMI(BB, PPC::PHI, 4, Result).addReg(FalseValue)
2252 .addMBB(copy0MBB).addReg(TrueValue).addMBB(thisMBB);
Nate Begeman74747862005-03-29 22:24:51 +00002253 return Result;
2254 }
Nate Begemana9795f82005-03-24 04:41:43 +00002255
2256 case ISD::Constant:
2257 switch (N.getValueType()) {
2258 default: assert(0 && "Cannot use constants of this type!");
2259 case MVT::i1:
2260 BuildMI(BB, PPC::LI, 1, Result)
2261 .addSImm(!cast<ConstantSDNode>(N)->isNullValue());
2262 break;
2263 case MVT::i32:
2264 {
2265 int v = (int)cast<ConstantSDNode>(N)->getSignExtended();
2266 if (v < 32768 && v >= -32768) {
2267 BuildMI(BB, PPC::LI, 1, Result).addSImm(v);
2268 } else {
Nate Begeman5e966612005-03-24 06:28:42 +00002269 Tmp1 = MakeReg(MVT::i32);
2270 BuildMI(BB, PPC::LIS, 1, Tmp1).addSImm(v >> 16);
2271 BuildMI(BB, PPC::ORI, 2, Result).addReg(Tmp1).addImm(v & 0xFFFF);
Nate Begemana9795f82005-03-24 04:41:43 +00002272 }
2273 }
2274 }
2275 return Result;
2276 }
2277
2278 return 0;
2279}
2280
2281void ISel::Select(SDOperand N) {
2282 unsigned Tmp1, Tmp2, Opc;
2283 unsigned opcode = N.getOpcode();
2284
2285 if (!ExprMap.insert(std::make_pair(N, 1)).second)
2286 return; // Already selected.
2287
2288 SDNode *Node = N.Val;
2289
2290 switch (Node->getOpcode()) {
2291 default:
2292 Node->dump(); std::cerr << "\n";
2293 assert(0 && "Node not handled yet!");
2294 case ISD::EntryToken: return; // Noop
2295 case ISD::TokenFactor:
2296 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
2297 Select(Node->getOperand(i));
2298 return;
2299 case ISD::ADJCALLSTACKDOWN:
2300 case ISD::ADJCALLSTACKUP:
2301 Select(N.getOperand(0));
2302 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
2303 Opc = N.getOpcode() == ISD::ADJCALLSTACKDOWN ? PPC::ADJCALLSTACKDOWN :
2304 PPC::ADJCALLSTACKUP;
2305 BuildMI(BB, Opc, 1).addImm(Tmp1);
2306 return;
2307 case ISD::BR: {
2308 MachineBasicBlock *Dest =
2309 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
Nate Begemana9795f82005-03-24 04:41:43 +00002310 Select(N.getOperand(0));
2311 BuildMI(BB, PPC::B, 1).addMBB(Dest);
2312 return;
2313 }
2314 case ISD::BRCOND:
Nate Begemancd08e4c2005-04-09 20:09:12 +00002315 case ISD::BRCONDTWOWAY:
Nate Begemana9795f82005-03-24 04:41:43 +00002316 SelectBranchCC(N);
2317 return;
2318 case ISD::CopyToReg:
2319 Select(N.getOperand(0));
2320 Tmp1 = SelectExpr(N.getOperand(1));
2321 Tmp2 = cast<RegSDNode>(N)->getReg();
2322
2323 if (Tmp1 != Tmp2) {
2324 if (N.getOperand(1).getValueType() == MVT::f64 ||
2325 N.getOperand(1).getValueType() == MVT::f32)
2326 BuildMI(BB, PPC::FMR, 1, Tmp2).addReg(Tmp1);
2327 else
2328 BuildMI(BB, PPC::OR, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
2329 }
2330 return;
2331 case ISD::ImplicitDef:
2332 Select(N.getOperand(0));
2333 BuildMI(BB, PPC::IMPLICIT_DEF, 0, cast<RegSDNode>(N)->getReg());
2334 return;
2335 case ISD::RET:
2336 switch (N.getNumOperands()) {
2337 default:
2338 assert(0 && "Unknown return instruction!");
2339 case 3:
2340 assert(N.getOperand(1).getValueType() == MVT::i32 &&
2341 N.getOperand(2).getValueType() == MVT::i32 &&
2342 "Unknown two-register value!");
2343 Select(N.getOperand(0));
2344 Tmp1 = SelectExpr(N.getOperand(1));
2345 Tmp2 = SelectExpr(N.getOperand(2));
Nate Begeman27523a12005-04-02 00:42:16 +00002346 BuildMI(BB, PPC::OR, 2, PPC::R3).addReg(Tmp2).addReg(Tmp2);
2347 BuildMI(BB, PPC::OR, 2, PPC::R4).addReg(Tmp1).addReg(Tmp1);
Nate Begemana9795f82005-03-24 04:41:43 +00002348 break;
2349 case 2:
2350 Select(N.getOperand(0));
2351 Tmp1 = SelectExpr(N.getOperand(1));
2352 switch (N.getOperand(1).getValueType()) {
2353 default:
2354 assert(0 && "Unknown return type!");
2355 case MVT::f64:
2356 case MVT::f32:
2357 BuildMI(BB, PPC::FMR, 1, PPC::F1).addReg(Tmp1);
2358 break;
2359 case MVT::i32:
2360 BuildMI(BB, PPC::OR, 2, PPC::R3).addReg(Tmp1).addReg(Tmp1);
2361 break;
2362 }
Nate Begeman9e3e1b52005-03-24 23:35:30 +00002363 case 1:
2364 Select(N.getOperand(0));
2365 break;
Nate Begemana9795f82005-03-24 04:41:43 +00002366 }
2367 BuildMI(BB, PPC::BLR, 0); // Just emit a 'ret' instruction
2368 return;
Nate Begemana9795f82005-03-24 04:41:43 +00002369 case ISD::TRUNCSTORE:
2370 case ISD::STORE:
2371 {
2372 SDOperand Chain = N.getOperand(0);
2373 SDOperand Value = N.getOperand(1);
2374 SDOperand Address = N.getOperand(2);
2375 Select(Chain);
2376
2377 Tmp1 = SelectExpr(Value); //value
2378
2379 if (opcode == ISD::STORE) {
2380 switch(Value.getValueType()) {
2381 default: assert(0 && "unknown Type in store");
2382 case MVT::i32: Opc = PPC::STW; break;
2383 case MVT::f64: Opc = PPC::STFD; break;
2384 case MVT::f32: Opc = PPC::STFS; break;
2385 }
2386 } else { //ISD::TRUNCSTORE
2387 switch(cast<MVTSDNode>(Node)->getExtraValueType()) {
2388 default: assert(0 && "unknown Type in store");
Nate Begeman7e7fadd2005-04-07 20:30:01 +00002389 case MVT::i1:
Nate Begemana9795f82005-03-24 04:41:43 +00002390 case MVT::i8: Opc = PPC::STB; break;
2391 case MVT::i16: Opc = PPC::STH; break;
2392 }
2393 }
2394
Nate Begemana7e11a42005-04-01 05:57:17 +00002395 if(Address.getOpcode() == ISD::FrameIndex)
Nate Begemana9795f82005-03-24 04:41:43 +00002396 {
Nate Begeman58f718c2005-03-30 02:23:08 +00002397 Tmp2 = cast<FrameIndexSDNode>(Address)->getIndex();
2398 addFrameReference(BuildMI(BB, Opc, 3).addReg(Tmp1), (int)Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +00002399 }
2400 else
2401 {
2402 int offset;
Nate Begeman04730362005-04-01 04:45:11 +00002403 bool idx = SelectAddr(Address, Tmp2, offset);
2404 if (idx) {
2405 Opc = IndexedOpForOp(Opc);
2406 BuildMI(BB, Opc, 3).addReg(Tmp1).addReg(Tmp2).addReg(offset);
2407 } else {
2408 BuildMI(BB, Opc, 3).addReg(Tmp1).addImm(offset).addReg(Tmp2);
2409 }
Nate Begemana9795f82005-03-24 04:41:43 +00002410 }
2411 return;
2412 }
2413 case ISD::EXTLOAD:
2414 case ISD::SEXTLOAD:
2415 case ISD::ZEXTLOAD:
2416 case ISD::LOAD:
2417 case ISD::CopyFromReg:
2418 case ISD::CALL:
2419 case ISD::DYNAMIC_STACKALLOC:
2420 ExprMap.erase(N);
2421 SelectExpr(N);
2422 return;
2423 }
2424 assert(0 && "Should not be reached!");
2425}
2426
2427
2428/// createPPC32PatternInstructionSelector - This pass converts an LLVM function
2429/// into a machine code representation using pattern matching and a machine
2430/// description file.
2431///
2432FunctionPass *llvm::createPPC32ISelPattern(TargetMachine &TM) {
2433 return new ISel(TM);
Chris Lattner246fa632005-03-24 06:16:18 +00002434}
2435