blob: f6b47fc8740d9ee054bffb3e1c51ecad1d1405ff [file] [log] [blame]
Nate Begemana9795f82005-03-24 04:41:43 +00001//===-- PPC32ISelPattern.cpp - A pattern matching inst selector for PPC32 -===//
2//
3// The LLVM Compiler Infrastructure
4//
Nate Begeman5e966612005-03-24 06:28:42 +00005// This file was developed by Nate Begeman and is distributed under
Nate Begemana9795f82005-03-24 04:41:43 +00006// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines a pattern matching instruction selector for 32 bit PowerPC.
Nate Begeman815d6da2005-04-06 00:25:27 +000011// Magic number generation for integer divide from the PowerPC Compiler Writer's
12// Guide, section 3.2.3.5
Nate Begemana9795f82005-03-24 04:41:43 +000013//
14//===----------------------------------------------------------------------===//
15
16#include "PowerPC.h"
17#include "PowerPCInstrBuilder.h"
18#include "PowerPCInstrInfo.h"
Nate Begemancd08e4c2005-04-09 20:09:12 +000019#include "PPC32TargetMachine.h"
Nate Begemana9795f82005-03-24 04:41:43 +000020#include "llvm/Constants.h" // FIXME: REMOVE
21#include "llvm/Function.h"
22#include "llvm/CodeGen/MachineConstantPool.h" // FIXME: REMOVE
23#include "llvm/CodeGen/MachineFunction.h"
24#include "llvm/CodeGen/MachineFrameInfo.h"
25#include "llvm/CodeGen/SelectionDAG.h"
26#include "llvm/CodeGen/SelectionDAGISel.h"
27#include "llvm/CodeGen/SSARegMap.h"
28#include "llvm/Target/TargetData.h"
29#include "llvm/Target/TargetLowering.h"
Nate Begeman93075ec2005-04-04 23:40:36 +000030#include "llvm/Target/TargetOptions.h"
Nate Begemana9795f82005-03-24 04:41:43 +000031#include "llvm/Support/Debug.h"
32#include "llvm/Support/MathExtras.h"
33#include "llvm/ADT/Statistic.h"
34#include <set>
35#include <algorithm>
36using namespace llvm;
37
38//===----------------------------------------------------------------------===//
39// PPC32TargetLowering - PPC32 Implementation of the TargetLowering interface
40namespace {
41 class PPC32TargetLowering : public TargetLowering {
42 int VarArgsFrameIndex; // FrameIndex for start of varargs area.
43 int ReturnAddrIndex; // FrameIndex for return slot.
44 public:
45 PPC32TargetLowering(TargetMachine &TM) : TargetLowering(TM) {
Nate Begemana9795f82005-03-24 04:41:43 +000046 // Set up the register classes.
47 addRegisterClass(MVT::i32, PPC32::GPRCRegisterClass);
Nate Begeman7532e2f2005-03-26 08:25:22 +000048 addRegisterClass(MVT::f32, PPC32::FPRCRegisterClass);
Nate Begemana9795f82005-03-24 04:41:43 +000049 addRegisterClass(MVT::f64, PPC32::FPRCRegisterClass);
50
Nate Begeman74d73452005-03-31 00:15:26 +000051 // PowerPC has no intrinsics for these particular operations
Nate Begeman01d05262005-03-30 01:45:43 +000052 setOperationAction(ISD::MEMMOVE, MVT::Other, Expand);
53 setOperationAction(ISD::MEMSET, MVT::Other, Expand);
54 setOperationAction(ISD::MEMCPY, MVT::Other, Expand);
55
Nate Begeman74d73452005-03-31 00:15:26 +000056 // PowerPC has an i16 but no i8 (or i1) SEXTLOAD
57 setOperationAction(ISD::SEXTLOAD, MVT::i1, Expand);
58 setOperationAction(ISD::SEXTLOAD, MVT::i8, Expand);
Nate Begeman815d6da2005-04-06 00:25:27 +000059
60 // PowerPC has no SREM/UREM instructions
61 setOperationAction(ISD::SREM, MVT::i32, Expand);
62 setOperationAction(ISD::UREM, MVT::i32, Expand);
Chris Lattner43fdea02005-04-02 05:03:24 +000063
Nate Begeman27eeb002005-04-02 05:59:34 +000064 setShiftAmountFlavor(Extend); // shl X, 32 == 0
Chris Lattnercbd06fc2005-04-07 19:41:49 +000065 setSetCCResultContents(ZeroOrOneSetCCResult);
Nate Begeman3e897162005-03-31 23:55:40 +000066 addLegalFPImmediate(+0.0); // Necessary for FSEL
67 addLegalFPImmediate(-0.0); //
68
Nate Begemana9795f82005-03-24 04:41:43 +000069 computeRegisterProperties();
70 }
71
72 /// LowerArguments - This hook must be implemented to indicate how we should
73 /// lower the arguments for the specified function, into the specified DAG.
74 virtual std::vector<SDOperand>
75 LowerArguments(Function &F, SelectionDAG &DAG);
76
77 /// LowerCallTo - This hook lowers an abstract call to a function into an
78 /// actual call.
79 virtual std::pair<SDOperand, SDOperand>
Nate Begeman307e7442005-03-26 01:28:53 +000080 LowerCallTo(SDOperand Chain, const Type *RetTy, bool isVarArg,
81 SDOperand Callee, ArgListTy &Args, SelectionDAG &DAG);
Nate Begemana9795f82005-03-24 04:41:43 +000082
83 virtual std::pair<SDOperand, SDOperand>
84 LowerVAStart(SDOperand Chain, SelectionDAG &DAG);
85
86 virtual std::pair<SDOperand,SDOperand>
87 LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
88 const Type *ArgTy, SelectionDAG &DAG);
89
90 virtual std::pair<SDOperand, SDOperand>
91 LowerFrameReturnAddress(bool isFrameAddr, SDOperand Chain, unsigned Depth,
92 SelectionDAG &DAG);
93 };
94}
95
96
97std::vector<SDOperand>
98PPC32TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
99 //
100 // add beautiful description of PPC stack frame format, or at least some docs
101 //
102 MachineFunction &MF = DAG.getMachineFunction();
103 MachineFrameInfo *MFI = MF.getFrameInfo();
104 MachineBasicBlock& BB = MF.front();
105 std::vector<SDOperand> ArgValues;
106
107 // Due to the rather complicated nature of the PowerPC ABI, rather than a
108 // fixed size array of physical args, for the sake of simplicity let the STL
109 // handle tracking them for us.
110 std::vector<unsigned> argVR, argPR, argOp;
111 unsigned ArgOffset = 24;
112 unsigned GPR_remaining = 8;
113 unsigned FPR_remaining = 13;
114 unsigned GPR_idx = 0, FPR_idx = 0;
115 static const unsigned GPR[] = {
116 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
117 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
118 };
119 static const unsigned FPR[] = {
120 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
121 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
122 };
123
124 // Add DAG nodes to load the arguments... On entry to a function on PPC,
125 // the arguments start at offset 24, although they are likely to be passed
126 // in registers.
127 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
128 SDOperand newroot, argt;
129 unsigned ObjSize;
130 bool needsLoad = false;
Nate Begemancd08e4c2005-04-09 20:09:12 +0000131 bool ArgLive = !I->use_empty();
Nate Begemana9795f82005-03-24 04:41:43 +0000132 MVT::ValueType ObjectVT = getValueType(I->getType());
133
134 switch (ObjectVT) {
135 default: assert(0 && "Unhandled argument type!");
136 case MVT::i1:
137 case MVT::i8:
138 case MVT::i16:
139 case MVT::i32:
140 ObjSize = 4;
Nate Begemancd08e4c2005-04-09 20:09:12 +0000141 if (!ArgLive) break;
Nate Begemana9795f82005-03-24 04:41:43 +0000142 if (GPR_remaining > 0) {
Nate Begemancd08e4c2005-04-09 20:09:12 +0000143 MF.addLiveIn(GPR[GPR_idx]);
Nate Begemanf70b5762005-03-28 23:08:54 +0000144 argt = newroot = DAG.getCopyFromReg(GPR[GPR_idx], MVT::i32,
145 DAG.getRoot());
Nate Begemana9795f82005-03-24 04:41:43 +0000146 if (ObjectVT != MVT::i32)
147 argt = DAG.getNode(ISD::TRUNCATE, ObjectVT, newroot);
Nate Begemana9795f82005-03-24 04:41:43 +0000148 } else {
149 needsLoad = true;
150 }
151 break;
Nate Begemanf7e43382005-03-26 07:46:36 +0000152 case MVT::i64: ObjSize = 8;
Nate Begemancd08e4c2005-04-09 20:09:12 +0000153 if (!ArgLive) break;
Nate Begemanf7e43382005-03-26 07:46:36 +0000154 // FIXME: can split 64b load between reg/mem if it is last arg in regs
Nate Begemana9795f82005-03-24 04:41:43 +0000155 if (GPR_remaining > 1) {
Nate Begemancd08e4c2005-04-09 20:09:12 +0000156 MF.addLiveIn(GPR[GPR_idx]);
157 MF.addLiveIn(GPR[GPR_idx+1]);
Nate Begemanca12a2b2005-03-28 22:28:37 +0000158 // Copy the extracted halves into the virtual registers
Nate Begemanf70b5762005-03-28 23:08:54 +0000159 SDOperand argHi = DAG.getCopyFromReg(GPR[GPR_idx], MVT::i32,
160 DAG.getRoot());
161 SDOperand argLo = DAG.getCopyFromReg(GPR[GPR_idx+1], MVT::i32, argHi);
Nate Begemanca12a2b2005-03-28 22:28:37 +0000162 // Build the outgoing arg thingy
Nate Begemanf70b5762005-03-28 23:08:54 +0000163 argt = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, argLo, argHi);
164 newroot = argLo;
Nate Begemana9795f82005-03-24 04:41:43 +0000165 } else {
166 needsLoad = true;
167 }
168 break;
Nate Begemancd08e4c2005-04-09 20:09:12 +0000169 case MVT::f32:
170 case MVT::f64:
171 ObjSize = (ObjectVT == MVT::f64) ? 8 : 4;
172 if (!ArgLive) break;
Nate Begemana9795f82005-03-24 04:41:43 +0000173 if (FPR_remaining > 0) {
Nate Begemancd08e4c2005-04-09 20:09:12 +0000174 MF.addLiveIn(FPR[FPR_idx]);
Nate Begemanf70b5762005-03-28 23:08:54 +0000175 argt = newroot = DAG.getCopyFromReg(FPR[FPR_idx], ObjectVT,
176 DAG.getRoot());
Nate Begemana9795f82005-03-24 04:41:43 +0000177 --FPR_remaining;
178 ++FPR_idx;
179 } else {
180 needsLoad = true;
181 }
182 break;
183 }
184
185 // We need to load the argument to a virtual register if we determined above
186 // that we ran out of physical registers of the appropriate type
187 if (needsLoad) {
Nate Begemane5846682005-04-04 06:52:38 +0000188 unsigned SubregOffset = 0;
Nate Begemanc3e2db42005-04-04 09:09:00 +0000189 if (ObjectVT == MVT::i8 || ObjectVT == MVT::i1) SubregOffset = 3;
Nate Begemane5846682005-04-04 06:52:38 +0000190 if (ObjectVT == MVT::i16) SubregOffset = 2;
Nate Begemana9795f82005-03-24 04:41:43 +0000191 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
192 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
Nate Begemane5846682005-04-04 06:52:38 +0000193 FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN,
194 DAG.getConstant(SubregOffset, MVT::i32));
Nate Begemana9795f82005-03-24 04:41:43 +0000195 argt = newroot = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN);
196 }
197
198 // Every 4 bytes of argument space consumes one of the GPRs available for
199 // argument passing.
200 if (GPR_remaining > 0) {
201 unsigned delta = (GPR_remaining > 1 && ObjSize == 8) ? 2 : 1;
202 GPR_remaining -= delta;
203 GPR_idx += delta;
204 }
205 ArgOffset += ObjSize;
Chris Lattner91277ea2005-04-09 21:23:24 +0000206 if (newroot.Val)
207 DAG.setRoot(newroot.getValue(1));
Nate Begemana9795f82005-03-24 04:41:43 +0000208
Nate Begemana9795f82005-03-24 04:41:43 +0000209 ArgValues.push_back(argt);
210 }
211
Nate Begemana9795f82005-03-24 04:41:43 +0000212 // If the function takes variable number of arguments, make a frame index for
213 // the start of the first vararg value... for expansion of llvm.va_start.
Nate Begemanfa554702005-04-03 22:13:27 +0000214 if (F.isVarArg()) {
Nate Begemana9795f82005-03-24 04:41:43 +0000215 VarArgsFrameIndex = MFI->CreateFixedObject(4, ArgOffset);
Nate Begemanfa554702005-04-03 22:13:27 +0000216 SDOperand FIN = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
Nate Begeman6644d4c2005-04-03 23:11:17 +0000217 // If this function is vararg, store any remaining integer argument regs
218 // to their spots on the stack so that they may be loaded by deferencing the
219 // result of va_next.
220 std::vector<SDOperand> MemOps;
221 for (; GPR_remaining > 0; --GPR_remaining, ++GPR_idx) {
Nate Begemancd08e4c2005-04-09 20:09:12 +0000222 MF.addLiveIn(GPR[GPR_idx]);
Nate Begeman6644d4c2005-04-03 23:11:17 +0000223 SDOperand Val = DAG.getCopyFromReg(GPR[GPR_idx], MVT::i32, DAG.getRoot());
224 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Val.getValue(1),
225 Val, FIN);
226 MemOps.push_back(Store);
227 // Increment the address by four for the next argument to store
228 SDOperand PtrOff = DAG.getConstant(4, getPointerTy());
229 FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN, PtrOff);
230 }
231 DAG.setRoot(DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps));
Nate Begemanfa554702005-04-03 22:13:27 +0000232 }
Nate Begemana9795f82005-03-24 04:41:43 +0000233
Nate Begemancd08e4c2005-04-09 20:09:12 +0000234 // Finally, inform the code generator which regs we return values in.
235 switch (getValueType(F.getReturnType())) {
236 default: assert(0 && "Unknown type!");
237 case MVT::isVoid: break;
238 case MVT::i1:
239 case MVT::i8:
240 case MVT::i16:
241 case MVT::i32:
242 MF.addLiveOut(PPC::R3);
243 break;
244 case MVT::i64:
245 MF.addLiveOut(PPC::R3);
246 MF.addLiveOut(PPC::R4);
247 break;
248 case MVT::f32:
249 case MVT::f64:
250 MF.addLiveOut(PPC::F1);
251 break;
252 }
253
Nate Begemana9795f82005-03-24 04:41:43 +0000254 return ArgValues;
255}
256
257std::pair<SDOperand, SDOperand>
258PPC32TargetLowering::LowerCallTo(SDOperand Chain,
Nate Begeman307e7442005-03-26 01:28:53 +0000259 const Type *RetTy, bool isVarArg,
260 SDOperand Callee, ArgListTy &Args, SelectionDAG &DAG) {
261 // args_to_use will accumulate outgoing args for the ISD::CALL case in
262 // SelectExpr to use to put the arguments in the appropriate registers.
Nate Begemana9795f82005-03-24 04:41:43 +0000263 std::vector<SDOperand> args_to_use;
Nate Begeman307e7442005-03-26 01:28:53 +0000264
265 // Count how many bytes are to be pushed on the stack, including the linkage
266 // area, and parameter passing area.
267 unsigned NumBytes = 24;
268
269 if (Args.empty()) {
Nate Begemana7e11a42005-04-01 05:57:17 +0000270 Chain = DAG.getNode(ISD::ADJCALLSTACKDOWN, MVT::Other, Chain,
271 DAG.getConstant(NumBytes, getPointerTy()));
Nate Begeman307e7442005-03-26 01:28:53 +0000272 } else {
273 for (unsigned i = 0, e = Args.size(); i != e; ++i)
274 switch (getValueType(Args[i].second)) {
275 default: assert(0 && "Unknown value type!");
276 case MVT::i1:
277 case MVT::i8:
278 case MVT::i16:
279 case MVT::i32:
280 case MVT::f32:
281 NumBytes += 4;
282 break;
283 case MVT::i64:
284 case MVT::f64:
285 NumBytes += 8;
286 break;
287 }
288
289 // Just to be safe, we'll always reserve the full 24 bytes of linkage area
290 // plus 32 bytes of argument space in case any called code gets funky on us.
291 if (NumBytes < 56) NumBytes = 56;
292
293 // Adjust the stack pointer for the new arguments...
294 // These operations are automatically eliminated by the prolog/epilog pass
295 Chain = DAG.getNode(ISD::ADJCALLSTACKDOWN, MVT::Other, Chain,
296 DAG.getConstant(NumBytes, getPointerTy()));
297
298 // Set up a copy of the stack pointer for use loading and storing any
299 // arguments that may not fit in the registers available for argument
300 // passing.
301 SDOperand StackPtr = DAG.getCopyFromReg(PPC::R1, MVT::i32,
302 DAG.getEntryNode());
303
304 // Figure out which arguments are going to go in registers, and which in
305 // memory. Also, if this is a vararg function, floating point operations
306 // must be stored to our stack, and loaded into integer regs as well, if
307 // any integer regs are available for argument passing.
308 unsigned ArgOffset = 24;
309 unsigned GPR_remaining = 8;
310 unsigned FPR_remaining = 13;
Nate Begeman74d73452005-03-31 00:15:26 +0000311
312 std::vector<SDOperand> MemOps;
Nate Begeman307e7442005-03-26 01:28:53 +0000313 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
314 // PtrOff will be used to store the current argument to the stack if a
315 // register cannot be found for it.
316 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
317 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
Nate Begemanf7e43382005-03-26 07:46:36 +0000318 MVT::ValueType ArgVT = getValueType(Args[i].second);
Nate Begeman307e7442005-03-26 01:28:53 +0000319
Nate Begemanf7e43382005-03-26 07:46:36 +0000320 switch (ArgVT) {
Nate Begeman307e7442005-03-26 01:28:53 +0000321 default: assert(0 && "Unexpected ValueType for argument!");
322 case MVT::i1:
323 case MVT::i8:
324 case MVT::i16:
325 // Promote the integer to 32 bits. If the input type is signed use a
326 // sign extend, otherwise use a zero extend.
327 if (Args[i].second->isSigned())
328 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
329 else
330 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
331 // FALL THROUGH
332 case MVT::i32:
333 if (GPR_remaining > 0) {
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000334 args_to_use.push_back(Args[i].first);
Nate Begeman307e7442005-03-26 01:28:53 +0000335 --GPR_remaining;
336 } else {
Nate Begeman74d73452005-03-31 00:15:26 +0000337 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
338 Args[i].first, PtrOff));
Nate Begeman307e7442005-03-26 01:28:53 +0000339 }
340 ArgOffset += 4;
341 break;
342 case MVT::i64:
Nate Begemanf7e43382005-03-26 07:46:36 +0000343 // If we have one free GPR left, we can place the upper half of the i64
344 // in it, and store the other half to the stack. If we have two or more
345 // free GPRs, then we can pass both halves of the i64 in registers.
346 if (GPR_remaining > 0) {
Nate Begemanf2622612005-03-26 02:17:46 +0000347 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
348 Args[i].first, DAG.getConstant(1, MVT::i32));
349 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
350 Args[i].first, DAG.getConstant(0, MVT::i32));
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000351 args_to_use.push_back(Hi);
Nate Begeman74d73452005-03-31 00:15:26 +0000352 --GPR_remaining;
Nate Begeman74d73452005-03-31 00:15:26 +0000353 if (GPR_remaining > 0) {
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000354 args_to_use.push_back(Lo);
Nate Begeman74d73452005-03-31 00:15:26 +0000355 --GPR_remaining;
Nate Begemanf7e43382005-03-26 07:46:36 +0000356 } else {
357 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
358 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
Nate Begeman74d73452005-03-31 00:15:26 +0000359 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
360 Lo, PtrOff));
Nate Begemanf7e43382005-03-26 07:46:36 +0000361 }
Nate Begeman307e7442005-03-26 01:28:53 +0000362 } else {
Nate Begeman74d73452005-03-31 00:15:26 +0000363 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
364 Args[i].first, PtrOff));
Nate Begeman307e7442005-03-26 01:28:53 +0000365 }
366 ArgOffset += 8;
367 break;
368 case MVT::f32:
Nate Begeman307e7442005-03-26 01:28:53 +0000369 case MVT::f64:
Nate Begemanf7e43382005-03-26 07:46:36 +0000370 if (FPR_remaining > 0) {
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000371 args_to_use.push_back(Args[i].first);
372 --FPR_remaining;
Nate Begemanf7e43382005-03-26 07:46:36 +0000373 if (isVarArg) {
Nate Begeman96fc6812005-03-31 02:05:53 +0000374 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Chain,
375 Args[i].first, PtrOff);
376 MemOps.push_back(Store);
Nate Begeman74d73452005-03-31 00:15:26 +0000377 // Float varargs are always shadowed in available integer registers
378 if (GPR_remaining > 0) {
Nate Begeman96fc6812005-03-31 02:05:53 +0000379 SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff);
Nate Begeman74d73452005-03-31 00:15:26 +0000380 MemOps.push_back(Load);
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000381 args_to_use.push_back(Load);
382 --GPR_remaining;
Nate Begeman74d73452005-03-31 00:15:26 +0000383 }
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000384 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
Nate Begeman74d73452005-03-31 00:15:26 +0000385 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
386 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
Nate Begeman96fc6812005-03-31 02:05:53 +0000387 SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff);
Nate Begeman74d73452005-03-31 00:15:26 +0000388 MemOps.push_back(Load);
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000389 args_to_use.push_back(Load);
390 --GPR_remaining;
Nate Begeman74d73452005-03-31 00:15:26 +0000391 }
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000392 } else {
393 // If we have any FPRs remaining, we may also have GPRs remaining.
394 // Args passed in FPRs consume either 1 (f32) or 2 (f64) available
395 // GPRs.
396 if (GPR_remaining > 0) {
397 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
398 --GPR_remaining;
399 }
400 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
401 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
402 --GPR_remaining;
403 }
Nate Begeman74d73452005-03-31 00:15:26 +0000404 }
Nate Begeman307e7442005-03-26 01:28:53 +0000405 } else {
Nate Begeman74d73452005-03-31 00:15:26 +0000406 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
407 Args[i].first, PtrOff));
Nate Begeman307e7442005-03-26 01:28:53 +0000408 }
Nate Begemanf7e43382005-03-26 07:46:36 +0000409 ArgOffset += (ArgVT == MVT::f32) ? 4 : 8;
Nate Begeman307e7442005-03-26 01:28:53 +0000410 break;
411 }
Nate Begemana9795f82005-03-24 04:41:43 +0000412 }
Nate Begeman74d73452005-03-31 00:15:26 +0000413 if (!MemOps.empty())
414 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps);
Nate Begemana9795f82005-03-24 04:41:43 +0000415 }
416
417 std::vector<MVT::ValueType> RetVals;
418 MVT::ValueType RetTyVT = getValueType(RetTy);
419 if (RetTyVT != MVT::isVoid)
420 RetVals.push_back(RetTyVT);
421 RetVals.push_back(MVT::Other);
422
423 SDOperand TheCall = SDOperand(DAG.getCall(RetVals,
424 Chain, Callee, args_to_use), 0);
425 Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
426 Chain = DAG.getNode(ISD::ADJCALLSTACKUP, MVT::Other, Chain,
427 DAG.getConstant(NumBytes, getPointerTy()));
428 return std::make_pair(TheCall, Chain);
429}
430
431std::pair<SDOperand, SDOperand>
432PPC32TargetLowering::LowerVAStart(SDOperand Chain, SelectionDAG &DAG) {
433 //vastart just returns the address of the VarArgsFrameIndex slot.
434 return std::make_pair(DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32), Chain);
435}
436
437std::pair<SDOperand,SDOperand> PPC32TargetLowering::
438LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
439 const Type *ArgTy, SelectionDAG &DAG) {
Nate Begemanc7b09f12005-03-25 08:34:25 +0000440 MVT::ValueType ArgVT = getValueType(ArgTy);
441 SDOperand Result;
442 if (!isVANext) {
443 Result = DAG.getLoad(ArgVT, DAG.getEntryNode(), VAList);
444 } else {
445 unsigned Amt;
446 if (ArgVT == MVT::i32 || ArgVT == MVT::f32)
447 Amt = 4;
448 else {
449 assert((ArgVT == MVT::i64 || ArgVT == MVT::f64) &&
450 "Other types should have been promoted for varargs!");
451 Amt = 8;
452 }
453 Result = DAG.getNode(ISD::ADD, VAList.getValueType(), VAList,
454 DAG.getConstant(Amt, VAList.getValueType()));
455 }
456 return std::make_pair(Result, Chain);
Nate Begemana9795f82005-03-24 04:41:43 +0000457}
458
459
460std::pair<SDOperand, SDOperand> PPC32TargetLowering::
461LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
462 SelectionDAG &DAG) {
Nate Begeman01d05262005-03-30 01:45:43 +0000463 assert(0 && "LowerFrameReturnAddress unimplemented");
Nate Begemana9795f82005-03-24 04:41:43 +0000464 abort();
465}
466
467namespace {
Nate Begemancd08e4c2005-04-09 20:09:12 +0000468Statistic<>Rotates("ppc-codegen", "Number of rotates emitted");
Nate Begeman93075ec2005-04-04 23:40:36 +0000469Statistic<>FusedFP("ppc-codegen", "Number of fused fp operations");
Nate Begemana9795f82005-03-24 04:41:43 +0000470//===--------------------------------------------------------------------===//
471/// ISel - PPC32 specific code to select PPC32 machine instructions for
472/// SelectionDAG operations.
473//===--------------------------------------------------------------------===//
474class ISel : public SelectionDAGISel {
Nate Begemana9795f82005-03-24 04:41:43 +0000475 PPC32TargetLowering PPC32Lowering;
Nate Begeman815d6da2005-04-06 00:25:27 +0000476 SelectionDAG *ISelDAG; // Hack to support us having a dag->dag transform
477 // for sdiv and udiv until it is put into the future
478 // dag combiner.
Nate Begemana9795f82005-03-24 04:41:43 +0000479
480 /// ExprMap - As shared expressions are codegen'd, we keep track of which
481 /// vreg the value is produced in, so we only emit one copy of each compiled
482 /// tree.
483 std::map<SDOperand, unsigned> ExprMap;
Nate Begemanc7b09f12005-03-25 08:34:25 +0000484
485 unsigned GlobalBaseReg;
486 bool GlobalBaseInitialized;
Nate Begemana9795f82005-03-24 04:41:43 +0000487
488public:
Nate Begeman815d6da2005-04-06 00:25:27 +0000489 ISel(TargetMachine &TM) : SelectionDAGISel(PPC32Lowering), PPC32Lowering(TM),
490 ISelDAG(0) {}
Nate Begemana9795f82005-03-24 04:41:43 +0000491
Nate Begemanc7b09f12005-03-25 08:34:25 +0000492 /// runOnFunction - Override this function in order to reset our per-function
493 /// variables.
494 virtual bool runOnFunction(Function &Fn) {
495 // Make sure we re-emit a set of the global base reg if necessary
496 GlobalBaseInitialized = false;
497 return SelectionDAGISel::runOnFunction(Fn);
498 }
499
Nate Begemana9795f82005-03-24 04:41:43 +0000500 /// InstructionSelectBasicBlock - This callback is invoked by
501 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
502 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG) {
503 DEBUG(BB->dump());
504 // Codegen the basic block.
Nate Begeman815d6da2005-04-06 00:25:27 +0000505 ISelDAG = &DAG;
Nate Begemana9795f82005-03-24 04:41:43 +0000506 Select(DAG.getRoot());
507
508 // Clear state used for selection.
509 ExprMap.clear();
Nate Begeman815d6da2005-04-06 00:25:27 +0000510 ISelDAG = 0;
Nate Begemana9795f82005-03-24 04:41:43 +0000511 }
Nate Begeman815d6da2005-04-06 00:25:27 +0000512
513 // dag -> dag expanders for integer divide by constant
514 SDOperand BuildSDIVSequence(SDOperand N);
515 SDOperand BuildUDIVSequence(SDOperand N);
Nate Begemana9795f82005-03-24 04:41:43 +0000516
Nate Begemandffcfcc2005-04-01 00:32:34 +0000517 unsigned getGlobalBaseReg();
Nate Begeman6b559972005-04-01 02:59:27 +0000518 unsigned getConstDouble(double floatVal, unsigned Result);
Nate Begeman7ddecb42005-04-06 23:51:40 +0000519 bool SelectBitfieldInsert(SDOperand OR, unsigned Result);
Nate Begemandffcfcc2005-04-01 00:32:34 +0000520 unsigned SelectSetCR0(SDOperand CC);
Nate Begemana9795f82005-03-24 04:41:43 +0000521 unsigned SelectExpr(SDOperand N);
522 unsigned SelectExprFP(SDOperand N, unsigned Result);
523 void Select(SDOperand N);
524
Nate Begeman04730362005-04-01 04:45:11 +0000525 bool SelectAddr(SDOperand N, unsigned& Reg, int& offset);
Nate Begemana9795f82005-03-24 04:41:43 +0000526 void SelectBranchCC(SDOperand N);
527};
528
Nate Begeman80196b12005-04-05 00:15:08 +0000529/// ExactLog2 - This function solves for (Val == 1 << (N-1)) and returns N. It
530/// returns zero when the input is not exactly a power of two.
531static unsigned ExactLog2(unsigned Val) {
532 if (Val == 0 || (Val & (Val-1))) return 0;
533 unsigned Count = 0;
534 while (Val != 1) {
535 Val >>= 1;
536 ++Count;
537 }
538 return Count;
539}
540
Nate Begeman7ddecb42005-04-06 23:51:40 +0000541// IsRunOfOnes - returns true if Val consists of one contiguous run of 1's with
542// any number of 0's on either side. the 1's are allowed to wrap from LSB to
543// MSB. so 0x000FFF0, 0x0000FFFF, and 0xFF0000FF are all runs. 0x0F0F0000 is
544// not, since all 1's are not contiguous.
545static bool IsRunOfOnes(unsigned Val, unsigned &MB, unsigned &ME) {
546 bool isRun = true;
547 MB = 0;
548 ME = 0;
549
550 // look for first set bit
551 int i = 0;
552 for (; i < 32; i++) {
553 if ((Val & (1 << (31 - i))) != 0) {
554 MB = i;
555 ME = i;
556 break;
557 }
558 }
559
560 // look for last set bit
561 for (; i < 32; i++) {
562 if ((Val & (1 << (31 - i))) == 0)
563 break;
564 ME = i;
565 }
566
567 // look for next set bit
568 for (; i < 32; i++) {
569 if ((Val & (1 << (31 - i))) != 0)
570 break;
571 }
572
573 // if we exhausted all the bits, we found a match at this point for 0*1*0*
574 if (i == 32)
575 return true;
576
577 // since we just encountered more 1's, if it doesn't wrap around to the
578 // most significant bit of the word, then we did not find a match to 1*0*1* so
579 // exit.
580 if (MB != 0)
581 return false;
582
583 // look for last set bit
584 for (MB = i; i < 32; i++) {
585 if ((Val & (1 << (31 - i))) == 0)
586 break;
587 }
588
589 // if we exhausted all the bits, then we found a match for 1*0*1*, otherwise,
590 // the value is not a run of ones.
591 if (i == 32)
592 return true;
593 return false;
594}
595
Nate Begeman439b4442005-04-05 04:22:58 +0000596/// getImmediateForOpcode - This method returns a value indicating whether
Nate Begemana9795f82005-03-24 04:41:43 +0000597/// the ConstantSDNode N can be used as an immediate to Opcode. The return
598/// values are either 0, 1 or 2. 0 indicates that either N is not a
599/// ConstantSDNode, or is not suitable for use by that opcode. A return value
600/// of 1 indicates that the constant may be used in normal immediate form. A
601/// return value of 2 indicates that the constant may be used in shifted
Nate Begeman439b4442005-04-05 04:22:58 +0000602/// immediate form. A return value of 3 indicates that log base 2 of the
Nate Begeman815d6da2005-04-06 00:25:27 +0000603/// constant may be used. A return value of 4 indicates that the constant is
604/// suitable for conversion into a magic number for integer division.
Nate Begemana9795f82005-03-24 04:41:43 +0000605///
Nate Begeman439b4442005-04-05 04:22:58 +0000606static unsigned getImmediateForOpcode(SDOperand N, unsigned Opcode,
607 unsigned& Imm, bool U = false) {
Nate Begemana9795f82005-03-24 04:41:43 +0000608 if (N.getOpcode() != ISD::Constant) return 0;
609
610 int v = (int)cast<ConstantSDNode>(N)->getSignExtended();
611
612 switch(Opcode) {
613 default: return 0;
614 case ISD::ADD:
615 if (v <= 32767 && v >= -32768) { Imm = v & 0xFFFF; return 1; }
616 if ((v & 0x0000FFFF) == 0) { Imm = v >> 16; return 2; }
617 break;
618 case ISD::AND:
619 case ISD::XOR:
620 case ISD::OR:
621 if (v >= 0 && v <= 65535) { Imm = v & 0xFFFF; return 1; }
622 if ((v & 0x0000FFFF) == 0) { Imm = v >> 16; return 2; }
623 break;
Nate Begeman307e7442005-03-26 01:28:53 +0000624 case ISD::MUL:
Nate Begeman27523a12005-04-02 00:42:16 +0000625 case ISD::SUB:
Nate Begeman307e7442005-03-26 01:28:53 +0000626 if (v <= 32767 && v >= -32768) { Imm = v & 0xFFFF; return 1; }
627 break;
Nate Begeman3e897162005-03-31 23:55:40 +0000628 case ISD::SETCC:
629 if (U && (v >= 0 && v <= 65535)) { Imm = v & 0xFFFF; return 1; }
630 if (!U && (v <= 32767 && v >= -32768)) { Imm = v & 0xFFFF; return 1; }
631 break;
Nate Begeman80196b12005-04-05 00:15:08 +0000632 case ISD::SDIV:
Nate Begeman439b4442005-04-05 04:22:58 +0000633 if ((Imm = ExactLog2(v))) { return 3; }
Nate Begeman815d6da2005-04-06 00:25:27 +0000634 if (v <= -2 || v >= 2) { return 4; }
635 break;
636 case ISD::UDIV:
Nate Begeman27b4c232005-04-06 06:44:57 +0000637 if (v > 1) { return 4; }
Nate Begeman80196b12005-04-05 00:15:08 +0000638 break;
Nate Begemana9795f82005-03-24 04:41:43 +0000639 }
640 return 0;
641}
Nate Begeman3e897162005-03-31 23:55:40 +0000642
643/// getBCCForSetCC - Returns the PowerPC condition branch mnemonic corresponding
644/// to Condition. If the Condition is unordered or unsigned, the bool argument
645/// U is set to true, otherwise it is set to false.
646static unsigned getBCCForSetCC(unsigned Condition, bool& U) {
647 U = false;
648 switch (Condition) {
649 default: assert(0 && "Unknown condition!"); abort();
650 case ISD::SETEQ: return PPC::BEQ;
651 case ISD::SETNE: return PPC::BNE;
652 case ISD::SETULT: U = true;
653 case ISD::SETLT: return PPC::BLT;
654 case ISD::SETULE: U = true;
655 case ISD::SETLE: return PPC::BLE;
656 case ISD::SETUGT: U = true;
657 case ISD::SETGT: return PPC::BGT;
658 case ISD::SETUGE: U = true;
659 case ISD::SETGE: return PPC::BGE;
660 }
Nate Begeman04730362005-04-01 04:45:11 +0000661 return 0;
662}
663
664/// IndexedOpForOp - Return the indexed variant for each of the PowerPC load
665/// and store immediate instructions.
666static unsigned IndexedOpForOp(unsigned Opcode) {
667 switch(Opcode) {
668 default: assert(0 && "Unknown opcode!"); abort();
669 case PPC::LBZ: return PPC::LBZX; case PPC::STB: return PPC::STBX;
670 case PPC::LHZ: return PPC::LHZX; case PPC::STH: return PPC::STHX;
671 case PPC::LHA: return PPC::LHAX; case PPC::STW: return PPC::STWX;
672 case PPC::LWZ: return PPC::LWZX; case PPC::STFS: return PPC::STFSX;
673 case PPC::LFS: return PPC::LFSX; case PPC::STFD: return PPC::STFDX;
674 case PPC::LFD: return PPC::LFDX;
675 }
676 return 0;
Nate Begeman3e897162005-03-31 23:55:40 +0000677}
Nate Begeman815d6da2005-04-06 00:25:27 +0000678
679// Structure used to return the necessary information to codegen an SDIV as
680// a multiply.
681struct ms {
682 int m; // magic number
683 int s; // shift amount
684};
685
686struct mu {
687 unsigned int m; // magic number
688 int a; // add indicator
689 int s; // shift amount
690};
691
692/// magic - calculate the magic numbers required to codegen an integer sdiv as
693/// a sequence of multiply and shifts. Requires that the divisor not be 0, 1,
694/// or -1.
695static struct ms magic(int d) {
696 int p;
697 unsigned int ad, anc, delta, q1, r1, q2, r2, t;
698 const unsigned int two31 = 2147483648U; // 2^31
699 struct ms mag;
700
701 ad = abs(d);
702 t = two31 + ((unsigned int)d >> 31);
703 anc = t - 1 - t%ad; // absolute value of nc
704 p = 31; // initialize p
705 q1 = two31/anc; // initialize q1 = 2p/abs(nc)
706 r1 = two31 - q1*anc; // initialize r1 = rem(2p,abs(nc))
707 q2 = two31/ad; // initialize q2 = 2p/abs(d)
708 r2 = two31 - q2*ad; // initialize r2 = rem(2p,abs(d))
709 do {
710 p = p + 1;
711 q1 = 2*q1; // update q1 = 2p/abs(nc)
712 r1 = 2*r1; // update r1 = rem(2p/abs(nc))
713 if (r1 >= anc) { // must be unsigned comparison
714 q1 = q1 + 1;
715 r1 = r1 - anc;
716 }
717 q2 = 2*q2; // update q2 = 2p/abs(d)
718 r2 = 2*r2; // update r2 = rem(2p/abs(d))
719 if (r2 >= ad) { // must be unsigned comparison
720 q2 = q2 + 1;
721 r2 = r2 - ad;
722 }
723 delta = ad - r2;
724 } while (q1 < delta || (q1 == delta && r1 == 0));
725
726 mag.m = q2 + 1;
727 if (d < 0) mag.m = -mag.m; // resulting magic number
728 mag.s = p - 32; // resulting shift
729 return mag;
730}
731
732/// magicu - calculate the magic numbers required to codegen an integer udiv as
733/// a sequence of multiply, add and shifts. Requires that the divisor not be 0.
734static struct mu magicu(unsigned d)
735{
736 int p;
737 unsigned int nc, delta, q1, r1, q2, r2;
738 struct mu magu;
739 magu.a = 0; // initialize "add" indicator
740 nc = - 1 - (-d)%d;
741 p = 31; // initialize p
742 q1 = 0x80000000/nc; // initialize q1 = 2p/nc
743 r1 = 0x80000000 - q1*nc; // initialize r1 = rem(2p,nc)
744 q2 = 0x7FFFFFFF/d; // initialize q2 = (2p-1)/d
745 r2 = 0x7FFFFFFF - q2*d; // initialize r2 = rem((2p-1),d)
746 do {
747 p = p + 1;
748 if (r1 >= nc - r1 ) {
749 q1 = 2*q1 + 1; // update q1
750 r1 = 2*r1 - nc; // update r1
751 }
752 else {
753 q1 = 2*q1; // update q1
754 r1 = 2*r1; // update r1
755 }
756 if (r2 + 1 >= d - r2) {
757 if (q2 >= 0x7FFFFFFF) magu.a = 1;
758 q2 = 2*q2 + 1; // update q2
759 r2 = 2*r2 + 1 - d; // update r2
760 }
761 else {
762 if (q2 >= 0x80000000) magu.a = 1;
763 q2 = 2*q2; // update q2
764 r2 = 2*r2 + 1; // update r2
765 }
766 delta = d - 1 - r2;
767 } while (p < 64 && (q1 < delta || (q1 == delta && r1 == 0)));
768 magu.m = q2 + 1; // resulting magic number
769 magu.s = p - 32; // resulting shift
770 return magu;
771}
772}
773
774/// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
775/// return a DAG expression to select that will generate the same value by
776/// multiplying by a magic number. See:
777/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
778SDOperand ISel::BuildSDIVSequence(SDOperand N) {
779 int d = (int)cast<ConstantSDNode>(N.getOperand(1))->getSignExtended();
780 ms magics = magic(d);
781 // Multiply the numerator (operand 0) by the magic value
782 SDOperand Q = ISelDAG->getNode(ISD::MULHS, MVT::i32, N.getOperand(0),
783 ISelDAG->getConstant(magics.m, MVT::i32));
784 // If d > 0 and m < 0, add the numerator
785 if (d > 0 && magics.m < 0)
786 Q = ISelDAG->getNode(ISD::ADD, MVT::i32, Q, N.getOperand(0));
787 // If d < 0 and m > 0, subtract the numerator.
788 if (d < 0 && magics.m > 0)
789 Q = ISelDAG->getNode(ISD::SUB, MVT::i32, Q, N.getOperand(0));
790 // Shift right algebraic if shift value is nonzero
791 if (magics.s > 0)
792 Q = ISelDAG->getNode(ISD::SRA, MVT::i32, Q,
793 ISelDAG->getConstant(magics.s, MVT::i32));
794 // Extract the sign bit and add it to the quotient
795 SDOperand T =
796 ISelDAG->getNode(ISD::SRL, MVT::i32, Q, ISelDAG->getConstant(31, MVT::i32));
Nate Begeman27b4c232005-04-06 06:44:57 +0000797 return ISelDAG->getNode(ISD::ADD, MVT::i32, Q, T);
Nate Begeman815d6da2005-04-06 00:25:27 +0000798}
799
800/// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
801/// return a DAG expression to select that will generate the same value by
802/// multiplying by a magic number. See:
803/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
804SDOperand ISel::BuildUDIVSequence(SDOperand N) {
805 unsigned d =
806 (unsigned)cast<ConstantSDNode>(N.getOperand(1))->getSignExtended();
807 mu magics = magicu(d);
808 // Multiply the numerator (operand 0) by the magic value
809 SDOperand Q = ISelDAG->getNode(ISD::MULHU, MVT::i32, N.getOperand(0),
810 ISelDAG->getConstant(magics.m, MVT::i32));
811 if (magics.a == 0) {
812 Q = ISelDAG->getNode(ISD::SRL, MVT::i32, Q,
813 ISelDAG->getConstant(magics.s, MVT::i32));
814 } else {
815 SDOperand NPQ = ISelDAG->getNode(ISD::SUB, MVT::i32, N.getOperand(0), Q);
816 NPQ = ISelDAG->getNode(ISD::SRL, MVT::i32, NPQ,
817 ISelDAG->getConstant(1, MVT::i32));
818 NPQ = ISelDAG->getNode(ISD::ADD, MVT::i32, NPQ, Q);
819 Q = ISelDAG->getNode(ISD::SRL, MVT::i32, NPQ,
820 ISelDAG->getConstant(magics.s-1, MVT::i32));
821 }
Nate Begeman27b4c232005-04-06 06:44:57 +0000822 return Q;
Nate Begemana9795f82005-03-24 04:41:43 +0000823}
824
Nate Begemanc7b09f12005-03-25 08:34:25 +0000825/// getGlobalBaseReg - Output the instructions required to put the
826/// base address to use for accessing globals into a register.
827///
828unsigned ISel::getGlobalBaseReg() {
829 if (!GlobalBaseInitialized) {
830 // Insert the set of GlobalBaseReg into the first MBB of the function
831 MachineBasicBlock &FirstMBB = BB->getParent()->front();
832 MachineBasicBlock::iterator MBBI = FirstMBB.begin();
833 GlobalBaseReg = MakeReg(MVT::i32);
834 BuildMI(FirstMBB, MBBI, PPC::MovePCtoLR, 0, PPC::LR);
835 BuildMI(FirstMBB, MBBI, PPC::MFLR, 1, GlobalBaseReg).addReg(PPC::LR);
836 GlobalBaseInitialized = true;
837 }
838 return GlobalBaseReg;
839}
840
Nate Begeman6b559972005-04-01 02:59:27 +0000841/// getConstDouble - Loads a floating point value into a register, via the
842/// Constant Pool. Optionally takes a register in which to load the value.
843unsigned ISel::getConstDouble(double doubleVal, unsigned Result=0) {
844 unsigned Tmp1 = MakeReg(MVT::i32);
845 if (0 == Result) Result = MakeReg(MVT::f64);
846 MachineConstantPool *CP = BB->getParent()->getConstantPool();
847 ConstantFP *CFP = ConstantFP::get(Type::DoubleTy, doubleVal);
848 unsigned CPI = CP->getConstantPoolIndex(CFP);
849 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp1).addReg(getGlobalBaseReg())
850 .addConstantPoolIndex(CPI);
851 BuildMI(BB, PPC::LFD, 2, Result).addConstantPoolIndex(CPI).addReg(Tmp1);
852 return Result;
853}
854
Nate Begeman7ddecb42005-04-06 23:51:40 +0000855/// SelectBitfieldInsert - turn an or of two masked values into
856/// the rotate left word immediate then mask insert (rlwimi) instruction.
857/// Returns true on success, false if the caller still needs to select OR.
858///
859/// Patterns matched:
860/// 1. or shl, and 5. or and, and
861/// 2. or and, shl 6. or shl, shr
862/// 3. or shr, and 7. or shr, shl
863/// 4. or and, shr
864bool ISel::SelectBitfieldInsert(SDOperand OR, unsigned Result) {
Nate Begemancd08e4c2005-04-09 20:09:12 +0000865 bool IsRotate = false;
Nate Begeman7ddecb42005-04-06 23:51:40 +0000866 unsigned TgtMask = 0xFFFFFFFF, InsMask = 0xFFFFFFFF, Amount = 0;
867 unsigned Op0Opc = OR.getOperand(0).getOpcode();
868 unsigned Op1Opc = OR.getOperand(1).getOpcode();
869
870 // Verify that we have the correct opcodes
871 if (ISD::SHL != Op0Opc && ISD::SRL != Op0Opc && ISD::AND != Op0Opc)
872 return false;
873 if (ISD::SHL != Op1Opc && ISD::SRL != Op1Opc && ISD::AND != Op1Opc)
874 return false;
875
876 // Generate Mask value for Target
877 if (ConstantSDNode *CN =
878 dyn_cast<ConstantSDNode>(OR.getOperand(0).getOperand(1).Val)) {
879 switch(Op0Opc) {
880 case ISD::SHL: TgtMask <<= (unsigned)CN->getValue(); break;
881 case ISD::SRL: TgtMask >>= (unsigned)CN->getValue(); break;
882 case ISD::AND: TgtMask &= (unsigned)CN->getValue(); break;
883 }
884 } else {
885 return false;
886 }
887
888 // Generate Mask value for Insert
889 if (ConstantSDNode *CN =
890 dyn_cast<ConstantSDNode>(OR.getOperand(1).getOperand(1).Val)) {
891 switch(Op1Opc) {
892 case ISD::SHL:
893 Amount = CN->getValue();
Nate Begemancd08e4c2005-04-09 20:09:12 +0000894 InsMask <<= Amount;
895 if (Op0Opc == ISD::SRL) IsRotate = true;
Nate Begeman7ddecb42005-04-06 23:51:40 +0000896 break;
897 case ISD::SRL:
898 Amount = CN->getValue();
899 InsMask >>= Amount;
900 Amount = 32-Amount;
Nate Begemancd08e4c2005-04-09 20:09:12 +0000901 if (Op0Opc == ISD::SHL) IsRotate = true;
Nate Begeman7ddecb42005-04-06 23:51:40 +0000902 break;
903 case ISD::AND:
904 InsMask &= (unsigned)CN->getValue();
905 break;
906 }
907 } else {
908 return false;
909 }
910
911 // Verify that the Target mask and Insert mask together form a full word mask
912 // and that the Insert mask is a run of set bits (which implies both are runs
913 // of set bits). Given that, Select the arguments and generate the rlwimi
914 // instruction.
915 unsigned MB, ME;
916 if (((TgtMask ^ InsMask) == 0xFFFFFFFF) && IsRunOfOnes(InsMask, MB, ME)) {
917 unsigned Tmp1, Tmp2;
Nate Begemancd08e4c2005-04-09 20:09:12 +0000918 // Check for rotlwi / rotrwi here, a special case of bitfield insert
919 // where both bitfield halves are sourced from the same value.
920 if (IsRotate &&
921 OR.getOperand(0).getOperand(0) == OR.getOperand(1).getOperand(0)) {
922 ++Rotates; // Statistic
923 Tmp1 = SelectExpr(OR.getOperand(0).getOperand(0));
924 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(Amount)
925 .addImm(0).addImm(31);
926 return true;
927 }
Nate Begeman7ddecb42005-04-06 23:51:40 +0000928 if (Op0Opc == ISD::AND)
929 Tmp1 = SelectExpr(OR.getOperand(0).getOperand(0));
930 else
931 Tmp1 = SelectExpr(OR.getOperand(0));
932 Tmp2 = SelectExpr(OR.getOperand(1).getOperand(0));
933 BuildMI(BB, PPC::RLWIMI, 5, Result).addReg(Tmp1).addReg(Tmp2)
934 .addImm(Amount).addImm(MB).addImm(ME);
935 return true;
936 }
937 return false;
938}
939
Nate Begemandffcfcc2005-04-01 00:32:34 +0000940unsigned ISel::SelectSetCR0(SDOperand CC) {
941 unsigned Opc, Tmp1, Tmp2;
942 static const unsigned CompareOpcodes[] =
943 { PPC::FCMPU, PPC::FCMPU, PPC::CMPW, PPC::CMPLW };
944
945 // If the first operand to the select is a SETCC node, then we can fold it
946 // into the branch that selects which value to return.
947 SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(CC.Val);
948 if (SetCC && CC.getOpcode() == ISD::SETCC) {
949 bool U;
950 Opc = getBCCForSetCC(SetCC->getCondition(), U);
951 Tmp1 = SelectExpr(SetCC->getOperand(0));
952
Nate Begeman439b4442005-04-05 04:22:58 +0000953 // Pass the optional argument U to getImmediateForOpcode for SETCC,
Nate Begemandffcfcc2005-04-01 00:32:34 +0000954 // so that it knows whether the SETCC immediate range is signed or not.
Nate Begeman439b4442005-04-05 04:22:58 +0000955 if (1 == getImmediateForOpcode(SetCC->getOperand(1), ISD::SETCC,
956 Tmp2, U)) {
Nate Begemandffcfcc2005-04-01 00:32:34 +0000957 if (U)
958 BuildMI(BB, PPC::CMPLWI, 2, PPC::CR0).addReg(Tmp1).addImm(Tmp2);
959 else
960 BuildMI(BB, PPC::CMPWI, 2, PPC::CR0).addReg(Tmp1).addSImm(Tmp2);
961 } else {
962 bool IsInteger = MVT::isInteger(SetCC->getOperand(0).getValueType());
963 unsigned CompareOpc = CompareOpcodes[2 * IsInteger + U];
964 Tmp2 = SelectExpr(SetCC->getOperand(1));
965 BuildMI(BB, CompareOpc, 2, PPC::CR0).addReg(Tmp1).addReg(Tmp2);
966 }
967 } else {
968 Tmp1 = SelectExpr(CC);
969 BuildMI(BB, PPC::CMPLWI, 2, PPC::CR0).addReg(Tmp1).addImm(0);
970 Opc = PPC::BNE;
971 }
972 return Opc;
973}
974
975/// Check to see if the load is a constant offset from a base register
Nate Begeman04730362005-04-01 04:45:11 +0000976bool ISel::SelectAddr(SDOperand N, unsigned& Reg, int& offset)
Nate Begemana9795f82005-03-24 04:41:43 +0000977{
Nate Begeman96fc6812005-03-31 02:05:53 +0000978 unsigned imm = 0, opcode = N.getOpcode();
Nate Begeman04730362005-04-01 04:45:11 +0000979 if (N.getOpcode() == ISD::ADD) {
980 Reg = SelectExpr(N.getOperand(0));
Nate Begeman439b4442005-04-05 04:22:58 +0000981 if (1 == getImmediateForOpcode(N.getOperand(1), opcode, imm)) {
Nate Begeman96fc6812005-03-31 02:05:53 +0000982 offset = imm;
Nate Begeman04730362005-04-01 04:45:11 +0000983 return false;
984 }
985 offset = SelectExpr(N.getOperand(1));
986 return true;
987 }
Nate Begemana9795f82005-03-24 04:41:43 +0000988 Reg = SelectExpr(N);
989 offset = 0;
Nate Begeman04730362005-04-01 04:45:11 +0000990 return false;
Nate Begemana9795f82005-03-24 04:41:43 +0000991}
992
993void ISel::SelectBranchCC(SDOperand N)
994{
Nate Begemana9795f82005-03-24 04:41:43 +0000995 MachineBasicBlock *Dest =
996 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
Nate Begeman3e897162005-03-31 23:55:40 +0000997
Nate Begemana9795f82005-03-24 04:41:43 +0000998 Select(N.getOperand(0)); //chain
Nate Begemandffcfcc2005-04-01 00:32:34 +0000999 unsigned Opc = SelectSetCR0(N.getOperand(1));
Nate Begemancd08e4c2005-04-09 20:09:12 +00001000
1001 // Iterate to the next basic block, unless we're already at the end of the
1002 ilist<MachineBasicBlock>::iterator It = BB, E = BB->getParent()->end();
Nate Begeman706471e2005-04-09 23:35:05 +00001003 if (++It == E) It = BB;
Nate Begemancd08e4c2005-04-09 20:09:12 +00001004
1005 // If this is a two way branch, then grab the fallthrough basic block argument
1006 // and build a PowerPC branch pseudo-op, suitable for long branch conversion
1007 // if necessary by the branch selection pass. Otherwise, emit a standard
1008 // conditional branch.
1009 if (N.getOpcode() == ISD::BRCONDTWOWAY) {
1010 MachineBasicBlock *Fallthrough =
1011 cast<BasicBlockSDNode>(N.getOperand(3))->getBasicBlock();
1012 if (Dest != It) {
1013 BuildMI(BB, PPC::COND_BRANCH, 4).addReg(PPC::CR0).addImm(Opc)
1014 .addMBB(Dest).addMBB(Fallthrough);
1015 if (Fallthrough != It)
1016 BuildMI(BB, PPC::B, 1).addMBB(Fallthrough);
1017 } else {
1018 if (Fallthrough != It) {
1019 Opc = PPC32InstrInfo::invertPPCBranchOpcode(Opc);
1020 BuildMI(BB, PPC::COND_BRANCH, 4).addReg(PPC::CR0).addImm(Opc)
1021 .addMBB(Fallthrough).addMBB(Dest);
1022 }
1023 }
1024 } else {
Nate Begeman27499e32005-04-10 01:48:29 +00001025 BuildMI(BB, PPC::COND_BRANCH, 4).addReg(PPC::CR0).addImm(Opc)
1026 .addMBB(Dest).addMBB(It);
1027 //BuildMI(BB, Opc, 2).addReg(PPC::CR0).addMBB(Dest);
Nate Begemancd08e4c2005-04-09 20:09:12 +00001028 }
Nate Begemana9795f82005-03-24 04:41:43 +00001029 return;
1030}
1031
1032unsigned ISel::SelectExprFP(SDOperand N, unsigned Result)
1033{
1034 unsigned Tmp1, Tmp2, Tmp3;
1035 unsigned Opc = 0;
1036 SDNode *Node = N.Val;
1037 MVT::ValueType DestType = N.getValueType();
1038 unsigned opcode = N.getOpcode();
1039
1040 switch (opcode) {
1041 default:
1042 Node->dump();
1043 assert(0 && "Node not handled!\n");
1044
Nate Begeman23afcfb2005-03-29 22:48:55 +00001045 case ISD::SELECT: {
Nate Begeman3e897162005-03-31 23:55:40 +00001046 // Attempt to generate FSEL. We can do this whenever we have an FP result,
1047 // and an FP comparison in the SetCC node.
1048 SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(N.getOperand(0).Val);
1049 if (SetCC && N.getOperand(0).getOpcode() == ISD::SETCC &&
1050 !MVT::isInteger(SetCC->getOperand(0).getValueType()) &&
1051 SetCC->getCondition() != ISD::SETEQ &&
1052 SetCC->getCondition() != ISD::SETNE) {
1053 MVT::ValueType VT = SetCC->getOperand(0).getValueType();
Nate Begeman3e897162005-03-31 23:55:40 +00001054 unsigned TV = SelectExpr(N.getOperand(1)); // Use if TRUE
1055 unsigned FV = SelectExpr(N.getOperand(2)); // Use if FALSE
1056
1057 ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(1));
1058 if (CN && (CN->isExactlyValue(-0.0) || CN->isExactlyValue(0.0))) {
1059 switch(SetCC->getCondition()) {
1060 default: assert(0 && "Invalid FSEL condition"); abort();
1061 case ISD::SETULT:
1062 case ISD::SETLT:
Nate Begemanaf4ab1b2005-04-09 09:33:07 +00001063 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
Nate Begeman3e897162005-03-31 23:55:40 +00001064 case ISD::SETUGE:
1065 case ISD::SETGE:
Nate Begemanaf4ab1b2005-04-09 09:33:07 +00001066 Tmp1 = SelectExpr(SetCC->getOperand(0)); // Val to compare against
Nate Begeman3e897162005-03-31 23:55:40 +00001067 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp1).addReg(TV).addReg(FV);
1068 return Result;
1069 case ISD::SETUGT:
Nate Begemanaf4ab1b2005-04-09 09:33:07 +00001070 case ISD::SETGT:
1071 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
Nate Begeman3e897162005-03-31 23:55:40 +00001072 case ISD::SETULE:
1073 case ISD::SETLE: {
Nate Begemanaf4ab1b2005-04-09 09:33:07 +00001074 if (SetCC->getOperand(0).getOpcode() == ISD::FNEG) {
1075 Tmp2 = SelectExpr(SetCC->getOperand(0).getOperand(0));
1076 } else {
1077 Tmp2 = MakeReg(VT);
1078 Tmp1 = SelectExpr(SetCC->getOperand(0)); // Val to compare against
1079 BuildMI(BB, PPC::FNEG, 1, Tmp2).addReg(Tmp1);
1080 }
Nate Begeman3e897162005-03-31 23:55:40 +00001081 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp2).addReg(TV).addReg(FV);
1082 return Result;
1083 }
1084 }
1085 } else {
1086 Opc = (MVT::f64 == VT) ? PPC::FSUB : PPC::FSUBS;
Nate Begemanaf4ab1b2005-04-09 09:33:07 +00001087 Tmp1 = SelectExpr(SetCC->getOperand(0)); // Val to compare against
Nate Begeman3e897162005-03-31 23:55:40 +00001088 Tmp2 = SelectExpr(SetCC->getOperand(1));
1089 Tmp3 = MakeReg(VT);
1090 switch(SetCC->getCondition()) {
1091 default: assert(0 && "Invalid FSEL condition"); abort();
1092 case ISD::SETULT:
1093 case ISD::SETLT:
1094 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
1095 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(FV).addReg(TV);
1096 return Result;
1097 case ISD::SETUGE:
1098 case ISD::SETGE:
1099 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
1100 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(TV).addReg(FV);
1101 return Result;
1102 case ISD::SETUGT:
1103 case ISD::SETGT:
1104 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp2).addReg(Tmp1);
1105 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(FV).addReg(TV);
1106 return Result;
1107 case ISD::SETULE:
1108 case ISD::SETLE:
1109 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp2).addReg(Tmp1);
1110 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(TV).addReg(FV);
1111 return Result;
1112 }
1113 }
1114 assert(0 && "Should never get here");
1115 return 0;
1116 }
1117
Nate Begeman31318e42005-04-01 07:21:30 +00001118 unsigned TrueValue = SelectExpr(N.getOperand(1)); //Use if TRUE
1119 unsigned FalseValue = SelectExpr(N.getOperand(2)); //Use if FALSE
Nate Begeman6cb2e1b2005-04-01 08:57:43 +00001120 Opc = SelectSetCR0(N.getOperand(0));
Nate Begeman31318e42005-04-01 07:21:30 +00001121
Nate Begeman23afcfb2005-03-29 22:48:55 +00001122 // Create an iterator with which to insert the MBB for copying the false
1123 // value and the MBB to hold the PHI instruction for this SetCC.
1124 MachineBasicBlock *thisMBB = BB;
1125 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1126 ilist<MachineBasicBlock>::iterator It = BB;
1127 ++It;
1128
1129 // thisMBB:
1130 // ...
1131 // TrueVal = ...
1132 // cmpTY cr0, r1, r2
1133 // bCC copy1MBB
1134 // fallthrough --> copy0MBB
Nate Begeman23afcfb2005-03-29 22:48:55 +00001135 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
1136 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
Nate Begeman6cb2e1b2005-04-01 08:57:43 +00001137 BuildMI(BB, Opc, 2).addReg(PPC::CR0).addMBB(sinkMBB);
Nate Begeman23afcfb2005-03-29 22:48:55 +00001138 MachineFunction *F = BB->getParent();
1139 F->getBasicBlockList().insert(It, copy0MBB);
1140 F->getBasicBlockList().insert(It, sinkMBB);
1141 // Update machine-CFG edges
1142 BB->addSuccessor(copy0MBB);
1143 BB->addSuccessor(sinkMBB);
1144
1145 // copy0MBB:
1146 // %FalseValue = ...
1147 // # fallthrough to sinkMBB
1148 BB = copy0MBB;
Nate Begeman23afcfb2005-03-29 22:48:55 +00001149 // Update machine-CFG edges
1150 BB->addSuccessor(sinkMBB);
1151
1152 // sinkMBB:
1153 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1154 // ...
1155 BB = sinkMBB;
1156 BuildMI(BB, PPC::PHI, 4, Result).addReg(FalseValue)
1157 .addMBB(copy0MBB).addReg(TrueValue).addMBB(thisMBB);
1158 return Result;
1159 }
Nate Begeman27eeb002005-04-02 05:59:34 +00001160
1161 case ISD::FNEG:
Nate Begeman93075ec2005-04-04 23:40:36 +00001162 if (!NoExcessFPPrecision &&
1163 ISD::ADD == N.getOperand(0).getOpcode() &&
1164 N.getOperand(0).Val->hasOneUse() &&
1165 ISD::MUL == N.getOperand(0).getOperand(0).getOpcode() &&
1166 N.getOperand(0).getOperand(0).Val->hasOneUse()) {
Nate Begeman80196b12005-04-05 00:15:08 +00001167 ++FusedFP; // Statistic
Nate Begeman93075ec2005-04-04 23:40:36 +00001168 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(0));
1169 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(1));
1170 Tmp3 = SelectExpr(N.getOperand(0).getOperand(1));
1171 Opc = DestType == MVT::f64 ? PPC::FNMADD : PPC::FNMADDS;
1172 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1173 } else if (!NoExcessFPPrecision &&
Nate Begemane88aa5b2005-04-09 03:05:51 +00001174 ISD::ADD == N.getOperand(0).getOpcode() &&
Nate Begeman93075ec2005-04-04 23:40:36 +00001175 N.getOperand(0).Val->hasOneUse() &&
Nate Begemane88aa5b2005-04-09 03:05:51 +00001176 ISD::MUL == N.getOperand(0).getOperand(1).getOpcode() &&
1177 N.getOperand(0).getOperand(1).Val->hasOneUse()) {
Nate Begeman80196b12005-04-05 00:15:08 +00001178 ++FusedFP; // Statistic
Nate Begemane88aa5b2005-04-09 03:05:51 +00001179 Tmp1 = SelectExpr(N.getOperand(0).getOperand(1).getOperand(0));
1180 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1).getOperand(1));
1181 Tmp3 = SelectExpr(N.getOperand(0).getOperand(0));
1182 Opc = DestType == MVT::f64 ? PPC::FNMADD : PPC::FNMADDS;
Nate Begeman93075ec2005-04-04 23:40:36 +00001183 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1184 } else if (ISD::FABS == N.getOperand(0).getOpcode()) {
Nate Begeman27eeb002005-04-02 05:59:34 +00001185 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1186 BuildMI(BB, PPC::FNABS, 1, Result).addReg(Tmp1);
1187 } else {
1188 Tmp1 = SelectExpr(N.getOperand(0));
1189 BuildMI(BB, PPC::FNEG, 1, Result).addReg(Tmp1);
1190 }
1191 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001192
Nate Begeman27eeb002005-04-02 05:59:34 +00001193 case ISD::FABS:
1194 Tmp1 = SelectExpr(N.getOperand(0));
1195 BuildMI(BB, PPC::FABS, 1, Result).addReg(Tmp1);
1196 return Result;
1197
Nate Begemana9795f82005-03-24 04:41:43 +00001198 case ISD::FP_ROUND:
1199 assert (DestType == MVT::f32 &&
1200 N.getOperand(0).getValueType() == MVT::f64 &&
1201 "only f64 to f32 conversion supported here");
1202 Tmp1 = SelectExpr(N.getOperand(0));
1203 BuildMI(BB, PPC::FRSP, 1, Result).addReg(Tmp1);
1204 return Result;
1205
1206 case ISD::FP_EXTEND:
1207 assert (DestType == MVT::f64 &&
1208 N.getOperand(0).getValueType() == MVT::f32 &&
1209 "only f32 to f64 conversion supported here");
1210 Tmp1 = SelectExpr(N.getOperand(0));
1211 BuildMI(BB, PPC::FMR, 1, Result).addReg(Tmp1);
1212 return Result;
1213
1214 case ISD::CopyFromReg:
Nate Begemanf2622612005-03-26 02:17:46 +00001215 if (Result == 1)
1216 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1217 Tmp1 = dyn_cast<RegSDNode>(Node)->getReg();
1218 BuildMI(BB, PPC::FMR, 1, Result).addReg(Tmp1);
1219 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001220
Nate Begeman6d369cc2005-04-01 01:08:07 +00001221 case ISD::ConstantFP: {
Nate Begeman6d369cc2005-04-01 01:08:07 +00001222 ConstantFPSDNode *CN = cast<ConstantFPSDNode>(N);
Nate Begeman6b559972005-04-01 02:59:27 +00001223 Result = getConstDouble(CN->getValue(), Result);
Nate Begeman6d369cc2005-04-01 01:08:07 +00001224 return Result;
1225 }
Nate Begemana9795f82005-03-24 04:41:43 +00001226
Nate Begemana9795f82005-03-24 04:41:43 +00001227 case ISD::ADD:
Nate Begeman93075ec2005-04-04 23:40:36 +00001228 if (!NoExcessFPPrecision && N.getOperand(0).getOpcode() == ISD::MUL &&
1229 N.getOperand(0).Val->hasOneUse()) {
1230 ++FusedFP; // Statistic
1231 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1232 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1233 Tmp3 = SelectExpr(N.getOperand(1));
1234 Opc = DestType == MVT::f64 ? PPC::FMADD : PPC::FMADDS;
1235 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1236 return Result;
1237 }
Nate Begemane88aa5b2005-04-09 03:05:51 +00001238 if (!NoExcessFPPrecision && N.getOperand(1).getOpcode() == ISD::MUL &&
1239 N.getOperand(1).Val->hasOneUse()) {
1240 ++FusedFP; // Statistic
1241 Tmp1 = SelectExpr(N.getOperand(1).getOperand(0));
1242 Tmp2 = SelectExpr(N.getOperand(1).getOperand(1));
1243 Tmp3 = SelectExpr(N.getOperand(0));
1244 Opc = DestType == MVT::f64 ? PPC::FMADD : PPC::FMADDS;
1245 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1246 return Result;
1247 }
Nate Begeman93075ec2005-04-04 23:40:36 +00001248 Opc = DestType == MVT::f64 ? PPC::FADD : PPC::FADDS;
1249 Tmp1 = SelectExpr(N.getOperand(0));
1250 Tmp2 = SelectExpr(N.getOperand(1));
1251 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1252 return Result;
1253
Nate Begemana9795f82005-03-24 04:41:43 +00001254 case ISD::SUB:
Nate Begeman93075ec2005-04-04 23:40:36 +00001255 if (!NoExcessFPPrecision && N.getOperand(0).getOpcode() == ISD::MUL &&
1256 N.getOperand(0).Val->hasOneUse()) {
1257 ++FusedFP; // Statistic
1258 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1259 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1260 Tmp3 = SelectExpr(N.getOperand(1));
1261 Opc = DestType == MVT::f64 ? PPC::FMSUB : PPC::FMSUBS;
1262 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1263 return Result;
1264 }
Nate Begemane88aa5b2005-04-09 03:05:51 +00001265 if (!NoExcessFPPrecision && N.getOperand(1).getOpcode() == ISD::MUL &&
1266 N.getOperand(1).Val->hasOneUse()) {
1267 ++FusedFP; // Statistic
1268 Tmp1 = SelectExpr(N.getOperand(1).getOperand(0));
1269 Tmp2 = SelectExpr(N.getOperand(1).getOperand(1));
1270 Tmp3 = SelectExpr(N.getOperand(0));
1271 Opc = DestType == MVT::f64 ? PPC::FNMSUB : PPC::FNMSUBS;
1272 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1273 return Result;
1274 }
Nate Begeman93075ec2005-04-04 23:40:36 +00001275 Opc = DestType == MVT::f64 ? PPC::FSUB : PPC::FSUBS;
1276 Tmp1 = SelectExpr(N.getOperand(0));
1277 Tmp2 = SelectExpr(N.getOperand(1));
1278 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1279 return Result;
1280
1281 case ISD::MUL:
Nate Begemana9795f82005-03-24 04:41:43 +00001282 case ISD::SDIV:
1283 switch( opcode ) {
1284 case ISD::MUL: Opc = DestType == MVT::f64 ? PPC::FMUL : PPC::FMULS; break;
Nate Begemana9795f82005-03-24 04:41:43 +00001285 case ISD::SDIV: Opc = DestType == MVT::f64 ? PPC::FDIV : PPC::FDIVS; break;
1286 };
Nate Begemana9795f82005-03-24 04:41:43 +00001287 Tmp1 = SelectExpr(N.getOperand(0));
1288 Tmp2 = SelectExpr(N.getOperand(1));
1289 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1290 return Result;
1291
Nate Begemana9795f82005-03-24 04:41:43 +00001292 case ISD::UINT_TO_FP:
Nate Begemanfdcf3412005-03-30 19:38:35 +00001293 case ISD::SINT_TO_FP: {
1294 assert (N.getOperand(0).getValueType() == MVT::i32
1295 && "int to float must operate on i32");
1296 bool IsUnsigned = (ISD::UINT_TO_FP == opcode);
1297 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
1298 Tmp2 = MakeReg(MVT::f64); // temp reg to load the integer value into
1299 Tmp3 = MakeReg(MVT::i32); // temp reg to hold the conversion constant
1300 unsigned ConstF = MakeReg(MVT::f64); // temp reg to hold the fp constant
1301
1302 int FrameIdx = BB->getParent()->getFrameInfo()->CreateStackObject(8, 8);
1303 MachineConstantPool *CP = BB->getParent()->getConstantPool();
1304
1305 // FIXME: pull this FP constant generation stuff out into something like
1306 // the simple ISel's getReg.
1307 if (IsUnsigned) {
1308 ConstantFP *CFP = ConstantFP::get(Type::DoubleTy, 0x1.000000p52);
1309 unsigned CPI = CP->getConstantPoolIndex(CFP);
1310 // Load constant fp value
1311 unsigned Tmp4 = MakeReg(MVT::i32);
1312 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp4).addReg(getGlobalBaseReg())
1313 .addConstantPoolIndex(CPI);
1314 BuildMI(BB, PPC::LFD, 2, ConstF).addConstantPoolIndex(CPI).addReg(Tmp4);
1315 // Store the hi & low halves of the fp value, currently in int regs
1316 BuildMI(BB, PPC::LIS, 1, Tmp3).addSImm(0x4330);
1317 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(Tmp3), FrameIdx);
1318 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(Tmp1), FrameIdx, 4);
1319 addFrameReference(BuildMI(BB, PPC::LFD, 2, Tmp2), FrameIdx);
1320 // Generate the return value with a subtract
1321 BuildMI(BB, PPC::FSUB, 2, Result).addReg(Tmp2).addReg(ConstF);
1322 } else {
1323 ConstantFP *CFP = ConstantFP::get(Type::DoubleTy, 0x1.000008p52);
1324 unsigned CPI = CP->getConstantPoolIndex(CFP);
1325 // Load constant fp value
1326 unsigned Tmp4 = MakeReg(MVT::i32);
1327 unsigned TmpL = MakeReg(MVT::i32);
1328 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp4).addReg(getGlobalBaseReg())
1329 .addConstantPoolIndex(CPI);
1330 BuildMI(BB, PPC::LFD, 2, ConstF).addConstantPoolIndex(CPI).addReg(Tmp4);
1331 // Store the hi & low halves of the fp value, currently in int regs
1332 BuildMI(BB, PPC::LIS, 1, Tmp3).addSImm(0x4330);
1333 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(Tmp3), FrameIdx);
1334 BuildMI(BB, PPC::XORIS, 2, TmpL).addReg(Tmp1).addImm(0x8000);
1335 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(TmpL), FrameIdx, 4);
1336 addFrameReference(BuildMI(BB, PPC::LFD, 2, Tmp2), FrameIdx);
1337 // Generate the return value with a subtract
1338 BuildMI(BB, PPC::FSUB, 2, Result).addReg(Tmp2).addReg(ConstF);
1339 }
1340 return Result;
1341 }
Nate Begemana9795f82005-03-24 04:41:43 +00001342 }
Nate Begeman6b559972005-04-01 02:59:27 +00001343 assert(0 && "Should never get here");
Nate Begemana9795f82005-03-24 04:41:43 +00001344 return 0;
1345}
1346
1347unsigned ISel::SelectExpr(SDOperand N) {
1348 unsigned Result;
1349 unsigned Tmp1, Tmp2, Tmp3;
1350 unsigned Opc = 0;
1351 unsigned opcode = N.getOpcode();
1352
1353 SDNode *Node = N.Val;
1354 MVT::ValueType DestType = N.getValueType();
1355
1356 unsigned &Reg = ExprMap[N];
1357 if (Reg) return Reg;
1358
Nate Begeman27eeb002005-04-02 05:59:34 +00001359 switch (N.getOpcode()) {
1360 default:
Nate Begemana9795f82005-03-24 04:41:43 +00001361 Reg = Result = (N.getValueType() != MVT::Other) ?
Nate Begeman27eeb002005-04-02 05:59:34 +00001362 MakeReg(N.getValueType()) : 1;
1363 break;
1364 case ISD::CALL:
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001365 // If this is a call instruction, make sure to prepare ALL of the result
1366 // values as well as the chain.
Nate Begeman27eeb002005-04-02 05:59:34 +00001367 if (Node->getNumValues() == 1)
1368 Reg = Result = 1; // Void call, just a chain.
1369 else {
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001370 Result = MakeReg(Node->getValueType(0));
1371 ExprMap[N.getValue(0)] = Result;
Nate Begeman27eeb002005-04-02 05:59:34 +00001372 for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001373 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
Nate Begeman27eeb002005-04-02 05:59:34 +00001374 ExprMap[SDOperand(Node, Node->getNumValues()-1)] = 1;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001375 }
Nate Begeman27eeb002005-04-02 05:59:34 +00001376 break;
1377 case ISD::ADD_PARTS:
1378 case ISD::SUB_PARTS:
1379 case ISD::SHL_PARTS:
1380 case ISD::SRL_PARTS:
1381 case ISD::SRA_PARTS:
1382 Result = MakeReg(Node->getValueType(0));
1383 ExprMap[N.getValue(0)] = Result;
1384 for (unsigned i = 1, e = N.Val->getNumValues(); i != e; ++i)
1385 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
1386 break;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001387 }
1388
Nate Begemane5846682005-04-04 06:52:38 +00001389 if (ISD::CopyFromReg == opcode)
1390 DestType = N.getValue(0).getValueType();
1391
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001392 if (DestType == MVT::f64 || DestType == MVT::f32)
Nate Begemana0e3e942005-04-10 01:14:13 +00001393 if (ISD::LOAD != opcode && ISD::EXTLOAD != opcode &&
1394 ISD::UNDEF != opcode && ISD::CALL != opcode)
Nate Begeman74d73452005-03-31 00:15:26 +00001395 return SelectExprFP(N, Result);
Nate Begemana9795f82005-03-24 04:41:43 +00001396
1397 switch (opcode) {
1398 default:
1399 Node->dump();
1400 assert(0 && "Node not handled!\n");
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001401 case ISD::UNDEF:
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001402 BuildMI(BB, PPC::IMPLICIT_DEF, 0, Result);
1403 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001404 case ISD::DYNAMIC_STACKALLOC:
Nate Begeman5e966612005-03-24 06:28:42 +00001405 // Generate both result values. FIXME: Need a better commment here?
1406 if (Result != 1)
1407 ExprMap[N.getValue(1)] = 1;
1408 else
1409 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1410
1411 // FIXME: We are currently ignoring the requested alignment for handling
1412 // greater than the stack alignment. This will need to be revisited at some
1413 // point. Align = N.getOperand(2);
1414 if (!isa<ConstantSDNode>(N.getOperand(2)) ||
1415 cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) {
1416 std::cerr << "Cannot allocate stack object with greater alignment than"
1417 << " the stack alignment yet!";
1418 abort();
1419 }
1420 Select(N.getOperand(0));
1421 Tmp1 = SelectExpr(N.getOperand(1));
1422 // Subtract size from stack pointer, thereby allocating some space.
1423 BuildMI(BB, PPC::SUBF, 2, PPC::R1).addReg(Tmp1).addReg(PPC::R1);
1424 // Put a pointer to the space into the result register by copying the SP
1425 BuildMI(BB, PPC::OR, 2, Result).addReg(PPC::R1).addReg(PPC::R1);
1426 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001427
1428 case ISD::ConstantPool:
Nate Begemanca12a2b2005-03-28 22:28:37 +00001429 Tmp1 = cast<ConstantPoolSDNode>(N)->getIndex();
1430 Tmp2 = MakeReg(MVT::i32);
1431 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp2).addReg(getGlobalBaseReg())
1432 .addConstantPoolIndex(Tmp1);
1433 BuildMI(BB, PPC::LA, 2, Result).addReg(Tmp2).addConstantPoolIndex(Tmp1);
1434 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001435
1436 case ISD::FrameIndex:
Nate Begemanf3d08f32005-03-29 00:03:27 +00001437 Tmp1 = cast<FrameIndexSDNode>(N)->getIndex();
Nate Begeman58f718c2005-03-30 02:23:08 +00001438 addFrameReference(BuildMI(BB, PPC::ADDI, 2, Result), (int)Tmp1, 0, false);
Nate Begemanf3d08f32005-03-29 00:03:27 +00001439 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001440
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001441 case ISD::GlobalAddress: {
1442 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
Nate Begemanca12a2b2005-03-28 22:28:37 +00001443 Tmp1 = MakeReg(MVT::i32);
Nate Begemanc7b09f12005-03-25 08:34:25 +00001444 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp1).addReg(getGlobalBaseReg())
1445 .addGlobalAddress(GV);
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001446 if (GV->hasWeakLinkage() || GV->isExternal()) {
1447 BuildMI(BB, PPC::LWZ, 2, Result).addGlobalAddress(GV).addReg(Tmp1);
1448 } else {
1449 BuildMI(BB, PPC::LA, 2, Result).addReg(Tmp1).addGlobalAddress(GV);
1450 }
1451 return Result;
1452 }
1453
Nate Begeman5e966612005-03-24 06:28:42 +00001454 case ISD::LOAD:
Nate Begemana9795f82005-03-24 04:41:43 +00001455 case ISD::EXTLOAD:
1456 case ISD::ZEXTLOAD:
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001457 case ISD::SEXTLOAD: {
Nate Begeman9db505c2005-03-28 19:36:43 +00001458 MVT::ValueType TypeBeingLoaded = (ISD::LOAD == opcode) ?
1459 Node->getValueType(0) : cast<MVTSDNode>(Node)->getExtraValueType();
Nate Begeman74d73452005-03-31 00:15:26 +00001460 bool sext = (ISD::SEXTLOAD == opcode);
Nate Begeman74d73452005-03-31 00:15:26 +00001461
Nate Begeman5e966612005-03-24 06:28:42 +00001462 // Make sure we generate both values.
1463 if (Result != 1)
1464 ExprMap[N.getValue(1)] = 1; // Generate the token
1465 else
1466 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1467
1468 SDOperand Chain = N.getOperand(0);
1469 SDOperand Address = N.getOperand(1);
1470 Select(Chain);
1471
Nate Begeman9db505c2005-03-28 19:36:43 +00001472 switch (TypeBeingLoaded) {
Nate Begeman74d73452005-03-31 00:15:26 +00001473 default: Node->dump(); assert(0 && "Cannot load this type!");
Nate Begeman9db505c2005-03-28 19:36:43 +00001474 case MVT::i1: Opc = PPC::LBZ; break;
1475 case MVT::i8: Opc = PPC::LBZ; break;
1476 case MVT::i16: Opc = sext ? PPC::LHA : PPC::LHZ; break;
1477 case MVT::i32: Opc = PPC::LWZ; break;
Nate Begeman74d73452005-03-31 00:15:26 +00001478 case MVT::f32: Opc = PPC::LFS; break;
1479 case MVT::f64: Opc = PPC::LFD; break;
Nate Begeman5e966612005-03-24 06:28:42 +00001480 }
1481
Nate Begeman74d73452005-03-31 00:15:26 +00001482 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Address)) {
1483 Tmp1 = MakeReg(MVT::i32);
1484 int CPI = CP->getIndex();
1485 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp1).addReg(getGlobalBaseReg())
1486 .addConstantPoolIndex(CPI);
1487 BuildMI(BB, Opc, 2, Result).addConstantPoolIndex(CPI).addReg(Tmp1);
Nate Begeman9db505c2005-03-28 19:36:43 +00001488 }
Nate Begeman74d73452005-03-31 00:15:26 +00001489 else if(Address.getOpcode() == ISD::FrameIndex) {
Nate Begeman58f718c2005-03-30 02:23:08 +00001490 Tmp1 = cast<FrameIndexSDNode>(Address)->getIndex();
1491 addFrameReference(BuildMI(BB, Opc, 2, Result), (int)Tmp1);
Nate Begeman5e966612005-03-24 06:28:42 +00001492 } else {
1493 int offset;
Nate Begeman04730362005-04-01 04:45:11 +00001494 bool idx = SelectAddr(Address, Tmp1, offset);
1495 if (idx) {
1496 Opc = IndexedOpForOp(Opc);
1497 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(offset);
1498 } else {
1499 BuildMI(BB, Opc, 2, Result).addSImm(offset).addReg(Tmp1);
1500 }
Nate Begeman5e966612005-03-24 06:28:42 +00001501 }
1502 return Result;
1503 }
1504
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001505 case ISD::CALL: {
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001506 unsigned GPR_idx = 0, FPR_idx = 0;
1507 static const unsigned GPR[] = {
1508 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
1509 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
1510 };
1511 static const unsigned FPR[] = {
1512 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
1513 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
1514 };
1515
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001516 // Lower the chain for this call.
1517 Select(N.getOperand(0));
1518 ExprMap[N.getValue(Node->getNumValues()-1)] = 1;
Nate Begeman74d73452005-03-31 00:15:26 +00001519
Nate Begemand860aa62005-04-04 22:17:48 +00001520 MachineInstr *CallMI;
1521 // Emit the correct call instruction based on the type of symbol called.
1522 if (GlobalAddressSDNode *GASD =
1523 dyn_cast<GlobalAddressSDNode>(N.getOperand(1))) {
1524 CallMI = BuildMI(PPC::CALLpcrel, 1).addGlobalAddress(GASD->getGlobal(),
1525 true);
1526 } else if (ExternalSymbolSDNode *ESSDN =
1527 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1))) {
1528 CallMI = BuildMI(PPC::CALLpcrel, 1).addExternalSymbol(ESSDN->getSymbol(),
1529 true);
1530 } else {
1531 Tmp1 = SelectExpr(N.getOperand(1));
1532 BuildMI(BB, PPC::OR, 2, PPC::R12).addReg(Tmp1).addReg(Tmp1);
1533 BuildMI(BB, PPC::MTCTR, 1).addReg(PPC::R12);
1534 CallMI = BuildMI(PPC::CALLindirect, 3).addImm(20).addImm(0)
1535 .addReg(PPC::R12);
1536 }
1537
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001538 // Load the register args to virtual regs
1539 std::vector<unsigned> ArgVR;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001540 for(int i = 2, e = Node->getNumOperands(); i < e; ++i)
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001541 ArgVR.push_back(SelectExpr(N.getOperand(i)));
1542
1543 // Copy the virtual registers into the appropriate argument register
1544 for(int i = 0, e = ArgVR.size(); i < e; ++i) {
1545 switch(N.getOperand(i+2).getValueType()) {
1546 default: Node->dump(); assert(0 && "Unknown value type for call");
1547 case MVT::i1:
1548 case MVT::i8:
1549 case MVT::i16:
1550 case MVT::i32:
1551 assert(GPR_idx < 8 && "Too many int args");
Nate Begemand860aa62005-04-04 22:17:48 +00001552 if (N.getOperand(i+2).getOpcode() != ISD::UNDEF) {
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001553 BuildMI(BB, PPC::OR,2,GPR[GPR_idx]).addReg(ArgVR[i]).addReg(ArgVR[i]);
Nate Begemand860aa62005-04-04 22:17:48 +00001554 CallMI->addRegOperand(GPR[GPR_idx], MachineOperand::Use);
1555 }
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001556 ++GPR_idx;
1557 break;
1558 case MVT::f64:
1559 case MVT::f32:
1560 assert(FPR_idx < 13 && "Too many fp args");
1561 BuildMI(BB, PPC::FMR, 1, FPR[FPR_idx]).addReg(ArgVR[i]);
Nate Begemand860aa62005-04-04 22:17:48 +00001562 CallMI->addRegOperand(FPR[FPR_idx], MachineOperand::Use);
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001563 ++FPR_idx;
1564 break;
1565 }
1566 }
Nate Begemand860aa62005-04-04 22:17:48 +00001567
1568 // Put the call instruction in the correct place in the MachineBasicBlock
1569 BB->push_back(CallMI);
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001570
1571 switch (Node->getValueType(0)) {
1572 default: assert(0 && "Unknown value type for call result!");
1573 case MVT::Other: return 1;
1574 case MVT::i1:
1575 case MVT::i8:
1576 case MVT::i16:
1577 case MVT::i32:
Nate Begemane5846682005-04-04 06:52:38 +00001578 if (Node->getValueType(1) == MVT::i32) {
1579 BuildMI(BB, PPC::OR, 2, Result+1).addReg(PPC::R3).addReg(PPC::R3);
1580 BuildMI(BB, PPC::OR, 2, Result).addReg(PPC::R4).addReg(PPC::R4);
1581 } else {
1582 BuildMI(BB, PPC::OR, 2, Result).addReg(PPC::R3).addReg(PPC::R3);
1583 }
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001584 break;
1585 case MVT::f32:
1586 case MVT::f64:
1587 BuildMI(BB, PPC::FMR, 1, Result).addReg(PPC::F1);
1588 break;
1589 }
1590 return Result+N.ResNo;
1591 }
Nate Begemana9795f82005-03-24 04:41:43 +00001592
1593 case ISD::SIGN_EXTEND:
1594 case ISD::SIGN_EXTEND_INREG:
1595 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman9db505c2005-03-28 19:36:43 +00001596 switch(cast<MVTSDNode>(Node)->getExtraValueType()) {
1597 default: Node->dump(); assert(0 && "Unhandled SIGN_EXTEND type"); break;
1598 case MVT::i16:
1599 BuildMI(BB, PPC::EXTSH, 1, Result).addReg(Tmp1);
1600 break;
1601 case MVT::i8:
1602 BuildMI(BB, PPC::EXTSB, 1, Result).addReg(Tmp1);
1603 break;
Nate Begeman74747862005-03-29 22:24:51 +00001604 case MVT::i1:
1605 BuildMI(BB, PPC::SUBFIC, 2, Result).addReg(Tmp1).addSImm(0);
1606 break;
Nate Begeman9db505c2005-03-28 19:36:43 +00001607 }
Nate Begemana9795f82005-03-24 04:41:43 +00001608 return Result;
1609
1610 case ISD::ZERO_EXTEND_INREG:
1611 Tmp1 = SelectExpr(N.getOperand(0));
1612 switch(cast<MVTSDNode>(Node)->getExtraValueType()) {
Nate Begeman9db505c2005-03-28 19:36:43 +00001613 default: Node->dump(); assert(0 && "Unhandled ZERO_EXTEND type"); break;
Nate Begemana9795f82005-03-24 04:41:43 +00001614 case MVT::i16: Tmp2 = 16; break;
1615 case MVT::i8: Tmp2 = 24; break;
1616 case MVT::i1: Tmp2 = 31; break;
1617 }
Nate Begeman33162522005-03-29 21:54:38 +00001618 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(0).addImm(Tmp2)
1619 .addImm(31);
Nate Begemana9795f82005-03-24 04:41:43 +00001620 return Result;
1621
Nate Begemana9795f82005-03-24 04:41:43 +00001622 case ISD::CopyFromReg:
1623 if (Result == 1)
1624 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1625 Tmp1 = dyn_cast<RegSDNode>(Node)->getReg();
1626 BuildMI(BB, PPC::OR, 2, Result).addReg(Tmp1).addReg(Tmp1);
1627 return Result;
1628
1629 case ISD::SHL:
Nate Begeman5e966612005-03-24 06:28:42 +00001630 Tmp1 = SelectExpr(N.getOperand(0));
1631 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1632 Tmp2 = CN->getValue() & 0x1F;
Nate Begeman33162522005-03-29 21:54:38 +00001633 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(Tmp2).addImm(0)
Nate Begeman5e966612005-03-24 06:28:42 +00001634 .addImm(31-Tmp2);
1635 } else {
1636 Tmp2 = SelectExpr(N.getOperand(1));
1637 BuildMI(BB, PPC::SLW, 2, Result).addReg(Tmp1).addReg(Tmp2);
1638 }
1639 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001640
Nate Begeman5e966612005-03-24 06:28:42 +00001641 case ISD::SRL:
1642 Tmp1 = SelectExpr(N.getOperand(0));
1643 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1644 Tmp2 = CN->getValue() & 0x1F;
Nate Begeman33162522005-03-29 21:54:38 +00001645 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(32-Tmp2)
Nate Begeman5e966612005-03-24 06:28:42 +00001646 .addImm(Tmp2).addImm(31);
1647 } else {
1648 Tmp2 = SelectExpr(N.getOperand(1));
1649 BuildMI(BB, PPC::SRW, 2, Result).addReg(Tmp1).addReg(Tmp2);
1650 }
1651 return Result;
1652
1653 case ISD::SRA:
1654 Tmp1 = SelectExpr(N.getOperand(0));
1655 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1656 Tmp2 = CN->getValue() & 0x1F;
1657 BuildMI(BB, PPC::SRAWI, 2, Result).addReg(Tmp1).addImm(Tmp2);
1658 } else {
1659 Tmp2 = SelectExpr(N.getOperand(1));
1660 BuildMI(BB, PPC::SRAW, 2, Result).addReg(Tmp1).addReg(Tmp2);
1661 }
1662 return Result;
1663
Nate Begemana9795f82005-03-24 04:41:43 +00001664 case ISD::ADD:
1665 assert (DestType == MVT::i32 && "Only do arithmetic on i32s!");
1666 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman439b4442005-04-05 04:22:58 +00001667 switch(getImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
Nate Begemana9795f82005-03-24 04:41:43 +00001668 default: assert(0 && "unhandled result code");
1669 case 0: // No immediate
1670 Tmp2 = SelectExpr(N.getOperand(1));
1671 BuildMI(BB, PPC::ADD, 2, Result).addReg(Tmp1).addReg(Tmp2);
1672 break;
1673 case 1: // Low immediate
1674 BuildMI(BB, PPC::ADDI, 2, Result).addReg(Tmp1).addSImm(Tmp2);
1675 break;
1676 case 2: // Shifted immediate
1677 BuildMI(BB, PPC::ADDIS, 2, Result).addReg(Tmp1).addSImm(Tmp2);
1678 break;
1679 }
1680 return Result;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001681
Nate Begemana9795f82005-03-24 04:41:43 +00001682 case ISD::AND:
Nate Begeman7ddecb42005-04-06 23:51:40 +00001683 Tmp1 = SelectExpr(N.getOperand(0));
1684 // FIXME: should add check in getImmediateForOpcode to return a value
1685 // indicating the immediate is a run of set bits so we can emit a bitfield
1686 // clear with RLWINM instead.
1687 switch(getImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
1688 default: assert(0 && "unhandled result code");
1689 case 0: // No immediate
1690 Tmp2 = SelectExpr(N.getOperand(1));
1691 BuildMI(BB, PPC::AND, 2, Result).addReg(Tmp1).addReg(Tmp2);
1692 break;
1693 case 1: // Low immediate
1694 BuildMI(BB, PPC::ANDIo, 2, Result).addReg(Tmp1).addImm(Tmp2);
1695 break;
1696 case 2: // Shifted immediate
1697 BuildMI(BB, PPC::ANDISo, 2, Result).addReg(Tmp1).addImm(Tmp2);
1698 break;
1699 }
1700 return Result;
1701
Nate Begemana9795f82005-03-24 04:41:43 +00001702 case ISD::OR:
Nate Begeman7ddecb42005-04-06 23:51:40 +00001703 if (SelectBitfieldInsert(N, Result))
1704 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001705 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman439b4442005-04-05 04:22:58 +00001706 switch(getImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
Nate Begemana9795f82005-03-24 04:41:43 +00001707 default: assert(0 && "unhandled result code");
1708 case 0: // No immediate
1709 Tmp2 = SelectExpr(N.getOperand(1));
Nate Begeman7ddecb42005-04-06 23:51:40 +00001710 BuildMI(BB, PPC::OR, 2, Result).addReg(Tmp1).addReg(Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +00001711 break;
1712 case 1: // Low immediate
Nate Begeman7ddecb42005-04-06 23:51:40 +00001713 BuildMI(BB, PPC::ORI, 2, Result).addReg(Tmp1).addImm(Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +00001714 break;
1715 case 2: // Shifted immediate
Nate Begeman7ddecb42005-04-06 23:51:40 +00001716 BuildMI(BB, PPC::ORIS, 2, Result).addReg(Tmp1).addImm(Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +00001717 break;
1718 }
1719 return Result;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001720
Nate Begemanaa73a9f2005-04-03 11:20:20 +00001721 case ISD::XOR: {
1722 // Check for EQV: xor, (xor a, -1), b
1723 if (N.getOperand(0).getOpcode() == ISD::XOR &&
1724 N.getOperand(0).getOperand(1).getOpcode() == ISD::Constant &&
1725 cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->isAllOnesValue()) {
Nate Begemanaa73a9f2005-04-03 11:20:20 +00001726 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1727 Tmp2 = SelectExpr(N.getOperand(1));
1728 BuildMI(BB, PPC::EQV, 2, Result).addReg(Tmp1).addReg(Tmp2);
1729 return Result;
1730 }
1731 // Check for NOT, NOR, and NAND: xor (copy, or, and), -1
1732 if (N.getOperand(1).getOpcode() == ISD::Constant &&
1733 cast<ConstantSDNode>(N.getOperand(1))->isAllOnesValue()) {
Nate Begemanaa73a9f2005-04-03 11:20:20 +00001734 switch(N.getOperand(0).getOpcode()) {
1735 case ISD::OR:
1736 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1737 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1738 BuildMI(BB, PPC::NOR, 2, Result).addReg(Tmp1).addReg(Tmp2);
1739 break;
1740 case ISD::AND:
1741 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1742 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1743 BuildMI(BB, PPC::NAND, 2, Result).addReg(Tmp1).addReg(Tmp2);
1744 break;
1745 default:
1746 Tmp1 = SelectExpr(N.getOperand(0));
1747 BuildMI(BB, PPC::NOR, 2, Result).addReg(Tmp1).addReg(Tmp1);
1748 break;
1749 }
1750 return Result;
1751 }
1752 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman439b4442005-04-05 04:22:58 +00001753 switch(getImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
Nate Begemanaa73a9f2005-04-03 11:20:20 +00001754 default: assert(0 && "unhandled result code");
1755 case 0: // No immediate
1756 Tmp2 = SelectExpr(N.getOperand(1));
1757 BuildMI(BB, PPC::XOR, 2, Result).addReg(Tmp1).addReg(Tmp2);
1758 break;
1759 case 1: // Low immediate
1760 BuildMI(BB, PPC::XORI, 2, Result).addReg(Tmp1).addImm(Tmp2);
1761 break;
1762 case 2: // Shifted immediate
1763 BuildMI(BB, PPC::XORIS, 2, Result).addReg(Tmp1).addImm(Tmp2);
1764 break;
1765 }
1766 return Result;
1767 }
1768
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001769 case ISD::SUB:
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001770 Tmp2 = SelectExpr(N.getOperand(1));
Nate Begeman439b4442005-04-05 04:22:58 +00001771 if (1 == getImmediateForOpcode(N.getOperand(0), opcode, Tmp1))
Nate Begeman27523a12005-04-02 00:42:16 +00001772 BuildMI(BB, PPC::SUBFIC, 2, Result).addReg(Tmp2).addSImm(Tmp1);
1773 else {
1774 Tmp1 = SelectExpr(N.getOperand(0));
1775 BuildMI(BB, PPC::SUBF, 2, Result).addReg(Tmp2).addReg(Tmp1);
1776 }
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001777 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001778
Nate Begeman5e966612005-03-24 06:28:42 +00001779 case ISD::MUL:
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001780 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman439b4442005-04-05 04:22:58 +00001781 if (1 == getImmediateForOpcode(N.getOperand(1), opcode, Tmp2))
Nate Begeman307e7442005-03-26 01:28:53 +00001782 BuildMI(BB, PPC::MULLI, 2, Result).addReg(Tmp1).addSImm(Tmp2);
1783 else {
1784 Tmp2 = SelectExpr(N.getOperand(1));
1785 BuildMI(BB, PPC::MULLW, 2, Result).addReg(Tmp1).addReg(Tmp2);
1786 }
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001787 return Result;
1788
Nate Begeman815d6da2005-04-06 00:25:27 +00001789 case ISD::MULHS:
1790 case ISD::MULHU:
1791 Tmp1 = SelectExpr(N.getOperand(0));
1792 Tmp2 = SelectExpr(N.getOperand(1));
1793 Opc = (ISD::MULHU == opcode) ? PPC::MULHWU : PPC::MULHW;
1794 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1795 return Result;
1796
Nate Begemanf3d08f32005-03-29 00:03:27 +00001797 case ISD::SDIV:
1798 case ISD::UDIV:
Nate Begeman815d6da2005-04-06 00:25:27 +00001799 switch (getImmediateForOpcode(N.getOperand(1), opcode, Tmp3)) {
1800 default: break;
1801 // If this is an sdiv by a power of two, we can use an srawi/addze pair.
1802 case 3:
Nate Begeman80196b12005-04-05 00:15:08 +00001803 Tmp1 = MakeReg(MVT::i32);
1804 Tmp2 = SelectExpr(N.getOperand(0));
1805 BuildMI(BB, PPC::SRAWI, 2, Tmp1).addReg(Tmp2).addImm(Tmp3);
1806 BuildMI(BB, PPC::ADDZE, 1, Result).addReg(Tmp1);
1807 return Result;
Nate Begeman815d6da2005-04-06 00:25:27 +00001808 // If this is a divide by constant, we can emit code using some magic
1809 // constants to implement it as a multiply instead.
Nate Begeman27b4c232005-04-06 06:44:57 +00001810 case 4:
1811 ExprMap.erase(N);
1812 if (opcode == ISD::SDIV)
1813 return SelectExpr(BuildSDIVSequence(N));
1814 else
1815 return SelectExpr(BuildUDIVSequence(N));
Nate Begeman80196b12005-04-05 00:15:08 +00001816 }
Nate Begemanf3d08f32005-03-29 00:03:27 +00001817 Tmp1 = SelectExpr(N.getOperand(0));
1818 Tmp2 = SelectExpr(N.getOperand(1));
1819 Opc = (ISD::UDIV == opcode) ? PPC::DIVWU : PPC::DIVW;
1820 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1821 return Result;
1822
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001823 case ISD::ADD_PARTS:
Nate Begemanca12a2b2005-03-28 22:28:37 +00001824 case ISD::SUB_PARTS: {
1825 assert(N.getNumOperands() == 4 && N.getValueType() == MVT::i32 &&
1826 "Not an i64 add/sub!");
1827 // Emit all of the operands.
1828 std::vector<unsigned> InVals;
1829 for (unsigned i = 0, e = N.getNumOperands(); i != e; ++i)
1830 InVals.push_back(SelectExpr(N.getOperand(i)));
1831 if (N.getOpcode() == ISD::ADD_PARTS) {
Nate Begeman27eeb002005-04-02 05:59:34 +00001832 BuildMI(BB, PPC::ADDC, 2, Result).addReg(InVals[0]).addReg(InVals[2]);
1833 BuildMI(BB, PPC::ADDE, 2, Result+1).addReg(InVals[1]).addReg(InVals[3]);
Nate Begemanca12a2b2005-03-28 22:28:37 +00001834 } else {
Nate Begeman27eeb002005-04-02 05:59:34 +00001835 BuildMI(BB, PPC::SUBFC, 2, Result).addReg(InVals[2]).addReg(InVals[0]);
1836 BuildMI(BB, PPC::SUBFE, 2, Result+1).addReg(InVals[3]).addReg(InVals[1]);
1837 }
1838 return Result+N.ResNo;
1839 }
1840
1841 case ISD::SHL_PARTS:
1842 case ISD::SRA_PARTS:
1843 case ISD::SRL_PARTS: {
1844 assert(N.getNumOperands() == 3 && N.getValueType() == MVT::i32 &&
1845 "Not an i64 shift!");
1846 unsigned ShiftOpLo = SelectExpr(N.getOperand(0));
1847 unsigned ShiftOpHi = SelectExpr(N.getOperand(1));
1848 unsigned SHReg = SelectExpr(N.getOperand(2));
1849 Tmp1 = MakeReg(MVT::i32);
1850 Tmp2 = MakeReg(MVT::i32);
1851 Tmp3 = MakeReg(MVT::i32);
1852 unsigned Tmp4 = MakeReg(MVT::i32);
1853 unsigned Tmp5 = MakeReg(MVT::i32);
1854 unsigned Tmp6 = MakeReg(MVT::i32);
1855 BuildMI(BB, PPC::SUBFIC, 2, Tmp1).addReg(SHReg).addSImm(32);
1856 if (ISD::SHL_PARTS == opcode) {
1857 BuildMI(BB, PPC::SLW, 2, Tmp2).addReg(ShiftOpHi).addReg(SHReg);
1858 BuildMI(BB, PPC::SRW, 2, Tmp3).addReg(ShiftOpLo).addReg(Tmp1);
1859 BuildMI(BB, PPC::OR, 2, Tmp4).addReg(Tmp2).addReg(Tmp3);
1860 BuildMI(BB, PPC::ADDI, 2, Tmp5).addReg(SHReg).addSImm(-32);
Nate Begemanfa554702005-04-03 22:13:27 +00001861 BuildMI(BB, PPC::SLW, 2, Tmp6).addReg(ShiftOpLo).addReg(Tmp5);
Nate Begeman27eeb002005-04-02 05:59:34 +00001862 BuildMI(BB, PPC::OR, 2, Result+1).addReg(Tmp4).addReg(Tmp6);
1863 BuildMI(BB, PPC::SLW, 2, Result).addReg(ShiftOpLo).addReg(SHReg);
1864 } else if (ISD::SRL_PARTS == opcode) {
1865 BuildMI(BB, PPC::SRW, 2, Tmp2).addReg(ShiftOpLo).addReg(SHReg);
1866 BuildMI(BB, PPC::SLW, 2, Tmp3).addReg(ShiftOpHi).addReg(Tmp1);
1867 BuildMI(BB, PPC::OR, 2, Tmp4).addReg(Tmp2).addReg(Tmp3);
1868 BuildMI(BB, PPC::ADDI, 2, Tmp5).addReg(SHReg).addSImm(-32);
1869 BuildMI(BB, PPC::SRW, 2, Tmp6).addReg(ShiftOpHi).addReg(Tmp5);
1870 BuildMI(BB, PPC::OR, 2, Result).addReg(Tmp4).addReg(Tmp6);
1871 BuildMI(BB, PPC::SRW, 2, Result+1).addReg(ShiftOpHi).addReg(SHReg);
1872 } else {
1873 MachineBasicBlock *TmpMBB = new MachineBasicBlock(BB->getBasicBlock());
1874 MachineBasicBlock *PhiMBB = new MachineBasicBlock(BB->getBasicBlock());
1875 MachineBasicBlock *OldMBB = BB;
1876 MachineFunction *F = BB->getParent();
1877 ilist<MachineBasicBlock>::iterator It = BB; ++It;
1878 F->getBasicBlockList().insert(It, TmpMBB);
1879 F->getBasicBlockList().insert(It, PhiMBB);
1880 BB->addSuccessor(TmpMBB);
1881 BB->addSuccessor(PhiMBB);
1882 BuildMI(BB, PPC::SRW, 2, Tmp2).addReg(ShiftOpLo).addReg(SHReg);
1883 BuildMI(BB, PPC::SLW, 2, Tmp3).addReg(ShiftOpHi).addReg(Tmp1);
1884 BuildMI(BB, PPC::OR, 2, Tmp4).addReg(Tmp2).addReg(Tmp3);
1885 BuildMI(BB, PPC::ADDICo, 2, Tmp5).addReg(SHReg).addSImm(-32);
1886 BuildMI(BB, PPC::SRAW, 2, Tmp6).addReg(ShiftOpHi).addReg(Tmp5);
1887 BuildMI(BB, PPC::SRAW, 2, Result+1).addReg(ShiftOpHi).addReg(SHReg);
1888 BuildMI(BB, PPC::BLE, 2).addReg(PPC::CR0).addMBB(PhiMBB);
1889 // Select correct least significant half if the shift amount > 32
1890 BB = TmpMBB;
1891 unsigned Tmp7 = MakeReg(MVT::i32);
1892 BuildMI(BB, PPC::OR, 2, Tmp7).addReg(Tmp6).addReg(Tmp6);
1893 TmpMBB->addSuccessor(PhiMBB);
1894 BB = PhiMBB;
1895 BuildMI(BB, PPC::PHI, 4, Result).addReg(Tmp4).addMBB(OldMBB)
1896 .addReg(Tmp7).addMBB(TmpMBB);
Nate Begemanca12a2b2005-03-28 22:28:37 +00001897 }
1898 return Result+N.ResNo;
1899 }
1900
Nate Begemana9795f82005-03-24 04:41:43 +00001901 case ISD::FP_TO_UINT:
Nate Begeman6b559972005-04-01 02:59:27 +00001902 case ISD::FP_TO_SINT: {
1903 bool U = (ISD::FP_TO_UINT == opcode);
1904 Tmp1 = SelectExpr(N.getOperand(0));
1905 if (!U) {
1906 Tmp2 = MakeReg(MVT::f64);
1907 BuildMI(BB, PPC::FCTIWZ, 1, Tmp2).addReg(Tmp1);
1908 int FrameIdx = BB->getParent()->getFrameInfo()->CreateStackObject(8, 8);
1909 addFrameReference(BuildMI(BB, PPC::STFD, 3).addReg(Tmp2), FrameIdx);
1910 addFrameReference(BuildMI(BB, PPC::LWZ, 2, Result), FrameIdx, 4);
1911 return Result;
1912 } else {
1913 unsigned Zero = getConstDouble(0.0);
1914 unsigned MaxInt = getConstDouble((1LL << 32) - 1);
1915 unsigned Border = getConstDouble(1LL << 31);
1916 unsigned UseZero = MakeReg(MVT::f64);
1917 unsigned UseMaxInt = MakeReg(MVT::f64);
1918 unsigned UseChoice = MakeReg(MVT::f64);
1919 unsigned TmpReg = MakeReg(MVT::f64);
1920 unsigned TmpReg2 = MakeReg(MVT::f64);
1921 unsigned ConvReg = MakeReg(MVT::f64);
1922 unsigned IntTmp = MakeReg(MVT::i32);
1923 unsigned XorReg = MakeReg(MVT::i32);
1924 MachineFunction *F = BB->getParent();
1925 int FrameIdx = F->getFrameInfo()->CreateStackObject(8, 8);
1926 // Update machine-CFG edges
1927 MachineBasicBlock *XorMBB = new MachineBasicBlock(BB->getBasicBlock());
1928 MachineBasicBlock *PhiMBB = new MachineBasicBlock(BB->getBasicBlock());
1929 MachineBasicBlock *OldMBB = BB;
1930 ilist<MachineBasicBlock>::iterator It = BB; ++It;
1931 F->getBasicBlockList().insert(It, XorMBB);
1932 F->getBasicBlockList().insert(It, PhiMBB);
1933 BB->addSuccessor(XorMBB);
1934 BB->addSuccessor(PhiMBB);
1935 // Convert from floating point to unsigned 32-bit value
1936 // Use 0 if incoming value is < 0.0
1937 BuildMI(BB, PPC::FSEL, 3, UseZero).addReg(Tmp1).addReg(Tmp1).addReg(Zero);
1938 // Use 2**32 - 1 if incoming value is >= 2**32
1939 BuildMI(BB, PPC::FSUB, 2, UseMaxInt).addReg(MaxInt).addReg(Tmp1);
1940 BuildMI(BB, PPC::FSEL, 3, UseChoice).addReg(UseMaxInt).addReg(UseZero)
1941 .addReg(MaxInt);
1942 // Subtract 2**31
1943 BuildMI(BB, PPC::FSUB, 2, TmpReg).addReg(UseChoice).addReg(Border);
1944 // Use difference if >= 2**31
1945 BuildMI(BB, PPC::FCMPU, 2, PPC::CR0).addReg(UseChoice).addReg(Border);
1946 BuildMI(BB, PPC::FSEL, 3, TmpReg2).addReg(TmpReg).addReg(TmpReg)
1947 .addReg(UseChoice);
1948 // Convert to integer
1949 BuildMI(BB, PPC::FCTIWZ, 1, ConvReg).addReg(TmpReg2);
1950 addFrameReference(BuildMI(BB, PPC::STFD, 3).addReg(ConvReg), FrameIdx);
1951 addFrameReference(BuildMI(BB, PPC::LWZ, 2, IntTmp), FrameIdx, 4);
1952 BuildMI(BB, PPC::BLT, 2).addReg(PPC::CR0).addMBB(PhiMBB);
1953 BuildMI(BB, PPC::B, 1).addMBB(XorMBB);
1954
1955 // XorMBB:
1956 // add 2**31 if input was >= 2**31
1957 BB = XorMBB;
1958 BuildMI(BB, PPC::XORIS, 2, XorReg).addReg(IntTmp).addImm(0x8000);
1959 XorMBB->addSuccessor(PhiMBB);
1960
1961 // PhiMBB:
1962 // DestReg = phi [ IntTmp, OldMBB ], [ XorReg, XorMBB ]
1963 BB = PhiMBB;
1964 BuildMI(BB, PPC::PHI, 4, Result).addReg(IntTmp).addMBB(OldMBB)
1965 .addReg(XorReg).addMBB(XorMBB);
1966 return Result;
1967 }
1968 assert(0 && "Should never get here");
1969 return 0;
1970 }
Nate Begemana9795f82005-03-24 04:41:43 +00001971
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001972 case ISD::SETCC:
Nate Begeman33162522005-03-29 21:54:38 +00001973 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Node)) {
Nate Begeman7e7fadd2005-04-07 20:30:01 +00001974 // We can codegen setcc op, 0 very efficiently compared to a conditional
1975 // branch. Check for that here.
1976 if (ConstantSDNode *CN =
1977 dyn_cast<ConstantSDNode>(SetCC->getOperand(1).Val)) {
1978 if (CN->getValue() == 0) {
1979 Tmp1 = SelectExpr(SetCC->getOperand(0));
1980 switch (SetCC->getCondition()) {
1981 default: assert(0 && "Unhandled SetCC condition"); abort();
1982 case ISD::SETEQ:
1983 case ISD::SETULE:
1984 Tmp2 = MakeReg(MVT::i32);
1985 BuildMI(BB, PPC::CNTLZW, 1, Tmp2).addReg(Tmp1);
1986 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp2).addImm(27)
1987 .addImm(5).addImm(31);
1988 break;
1989 case ISD::SETNE:
1990 case ISD::SETUGT:
1991 Tmp2 = MakeReg(MVT::i32);
1992 BuildMI(BB, PPC::ADDIC, 2, Tmp2).addReg(Tmp1).addSImm(-1);
1993 BuildMI(BB, PPC::SUBFE, 2, Result).addReg(Tmp2).addReg(Tmp1);
1994 break;
1995 case ISD::SETULT:
1996 BuildMI(BB, PPC::LI, 1, Result).addSImm(0);
1997 break;
1998 case ISD::SETLT:
1999 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(1)
2000 .addImm(31).addImm(31);
2001 break;
2002 case ISD::SETLE:
2003 Tmp2 = MakeReg(MVT::i32);
2004 Tmp3 = MakeReg(MVT::i32);
2005 BuildMI(BB, PPC::NEG, 2, Tmp2).addReg(Tmp1);
2006 BuildMI(BB, PPC::ORC, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
2007 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp3).addImm(1)
2008 .addImm(31).addImm(31);
2009 break;
2010 case ISD::SETGT:
2011 Tmp2 = MakeReg(MVT::i32);
2012 Tmp3 = MakeReg(MVT::i32);
2013 BuildMI(BB, PPC::NEG, 2, Tmp2).addReg(Tmp1);
2014 BuildMI(BB, PPC::ANDC, 2, Tmp3).addReg(Tmp2).addReg(Tmp1);
2015 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp3).addImm(1)
2016 .addImm(31).addImm(31);
2017 break;
2018 case ISD::SETUGE:
2019 BuildMI(BB, PPC::LI, 1, Result).addSImm(1);
2020 break;
2021 case ISD::SETGE:
2022 BuildMI(BB, PPC::RLWINM, 4, Tmp2).addReg(Tmp1).addImm(1)
2023 .addImm(31).addImm(31);
2024 BuildMI(BB, PPC::XORI, 2, Result).addReg(Tmp2).addImm(1);
2025 break;
2026 }
2027 return Result;
2028 }
2029 }
2030
Nate Begemandffcfcc2005-04-01 00:32:34 +00002031 Opc = SelectSetCR0(N);
Nate Begeman31318e42005-04-01 07:21:30 +00002032 unsigned TrueValue = MakeReg(MVT::i32);
2033 BuildMI(BB, PPC::LI, 1, TrueValue).addSImm(1);
2034 unsigned FalseValue = MakeReg(MVT::i32);
2035 BuildMI(BB, PPC::LI, 1, FalseValue).addSImm(0);
2036
Nate Begeman33162522005-03-29 21:54:38 +00002037 // Create an iterator with which to insert the MBB for copying the false
2038 // value and the MBB to hold the PHI instruction for this SetCC.
2039 MachineBasicBlock *thisMBB = BB;
2040 const BasicBlock *LLVM_BB = BB->getBasicBlock();
2041 ilist<MachineBasicBlock>::iterator It = BB;
2042 ++It;
2043
2044 // thisMBB:
2045 // ...
2046 // cmpTY cr0, r1, r2
2047 // %TrueValue = li 1
2048 // bCC sinkMBB
Nate Begeman33162522005-03-29 21:54:38 +00002049 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
2050 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
2051 BuildMI(BB, Opc, 2).addReg(PPC::CR0).addMBB(sinkMBB);
2052 MachineFunction *F = BB->getParent();
2053 F->getBasicBlockList().insert(It, copy0MBB);
2054 F->getBasicBlockList().insert(It, sinkMBB);
2055 // Update machine-CFG edges
2056 BB->addSuccessor(copy0MBB);
2057 BB->addSuccessor(sinkMBB);
2058
2059 // copy0MBB:
2060 // %FalseValue = li 0
2061 // fallthrough
2062 BB = copy0MBB;
Nate Begeman33162522005-03-29 21:54:38 +00002063 // Update machine-CFG edges
2064 BB->addSuccessor(sinkMBB);
2065
2066 // sinkMBB:
2067 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
2068 // ...
2069 BB = sinkMBB;
2070 BuildMI(BB, PPC::PHI, 4, Result).addReg(FalseValue)
2071 .addMBB(copy0MBB).addReg(TrueValue).addMBB(thisMBB);
2072 return Result;
2073 }
2074 assert(0 && "Is this legal?");
2075 return 0;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00002076
Nate Begeman74747862005-03-29 22:24:51 +00002077 case ISD::SELECT: {
Nate Begeman7e7fadd2005-04-07 20:30:01 +00002078 // We can codegen select (a < 0) ? b : 0 very efficiently compared to a
2079 // conditional branch. Check for that here.
2080 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(N.getOperand(0).Val)) {
2081 if (ConstantSDNode *CN =
2082 dyn_cast<ConstantSDNode>(SetCC->getOperand(1).Val)) {
2083 if (ConstantSDNode *CNF =
2084 dyn_cast<ConstantSDNode>(N.getOperand(2).Val)) {
2085 if (CN->getValue() == 0 && CNF->getValue() == 0 &&
2086 SetCC->getCondition() == ISD::SETLT) {
2087 Tmp1 = SelectExpr(N.getOperand(1)); // TRUE value
2088 Tmp2 = SelectExpr(SetCC->getOperand(0));
2089 Tmp3 = MakeReg(MVT::i32);
2090 BuildMI(BB, PPC::SRAWI, 2, Tmp3).addReg(Tmp2).addImm(31);
2091 BuildMI(BB, PPC::AND, 2, Result).addReg(Tmp1).addReg(Tmp3);
2092 return Result;
2093 }
2094 }
2095 }
2096 }
Chris Lattner30710192005-04-01 07:10:02 +00002097 unsigned TrueValue = SelectExpr(N.getOperand(1)); //Use if TRUE
2098 unsigned FalseValue = SelectExpr(N.getOperand(2)); //Use if FALSE
Nate Begeman6cb2e1b2005-04-01 08:57:43 +00002099 Opc = SelectSetCR0(N.getOperand(0));
Chris Lattner30710192005-04-01 07:10:02 +00002100
Nate Begeman74747862005-03-29 22:24:51 +00002101 // Create an iterator with which to insert the MBB for copying the false
2102 // value and the MBB to hold the PHI instruction for this SetCC.
2103 MachineBasicBlock *thisMBB = BB;
2104 const BasicBlock *LLVM_BB = BB->getBasicBlock();
2105 ilist<MachineBasicBlock>::iterator It = BB;
2106 ++It;
2107
2108 // thisMBB:
2109 // ...
2110 // TrueVal = ...
2111 // cmpTY cr0, r1, r2
2112 // bCC copy1MBB
2113 // fallthrough --> copy0MBB
Nate Begeman74747862005-03-29 22:24:51 +00002114 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
2115 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
Nate Begeman3e897162005-03-31 23:55:40 +00002116 BuildMI(BB, Opc, 2).addReg(PPC::CR0).addMBB(sinkMBB);
Nate Begeman74747862005-03-29 22:24:51 +00002117 MachineFunction *F = BB->getParent();
2118 F->getBasicBlockList().insert(It, copy0MBB);
2119 F->getBasicBlockList().insert(It, sinkMBB);
2120 // Update machine-CFG edges
2121 BB->addSuccessor(copy0MBB);
2122 BB->addSuccessor(sinkMBB);
2123
2124 // copy0MBB:
2125 // %FalseValue = ...
2126 // # fallthrough to sinkMBB
2127 BB = copy0MBB;
Nate Begeman74747862005-03-29 22:24:51 +00002128 // Update machine-CFG edges
2129 BB->addSuccessor(sinkMBB);
2130
2131 // sinkMBB:
2132 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
2133 // ...
2134 BB = sinkMBB;
2135 BuildMI(BB, PPC::PHI, 4, Result).addReg(FalseValue)
2136 .addMBB(copy0MBB).addReg(TrueValue).addMBB(thisMBB);
Nate Begeman74747862005-03-29 22:24:51 +00002137 return Result;
2138 }
Nate Begemana9795f82005-03-24 04:41:43 +00002139
2140 case ISD::Constant:
2141 switch (N.getValueType()) {
2142 default: assert(0 && "Cannot use constants of this type!");
2143 case MVT::i1:
2144 BuildMI(BB, PPC::LI, 1, Result)
2145 .addSImm(!cast<ConstantSDNode>(N)->isNullValue());
2146 break;
2147 case MVT::i32:
2148 {
2149 int v = (int)cast<ConstantSDNode>(N)->getSignExtended();
2150 if (v < 32768 && v >= -32768) {
2151 BuildMI(BB, PPC::LI, 1, Result).addSImm(v);
2152 } else {
Nate Begeman5e966612005-03-24 06:28:42 +00002153 Tmp1 = MakeReg(MVT::i32);
2154 BuildMI(BB, PPC::LIS, 1, Tmp1).addSImm(v >> 16);
2155 BuildMI(BB, PPC::ORI, 2, Result).addReg(Tmp1).addImm(v & 0xFFFF);
Nate Begemana9795f82005-03-24 04:41:43 +00002156 }
2157 }
2158 }
2159 return Result;
2160 }
2161
2162 return 0;
2163}
2164
2165void ISel::Select(SDOperand N) {
2166 unsigned Tmp1, Tmp2, Opc;
2167 unsigned opcode = N.getOpcode();
2168
2169 if (!ExprMap.insert(std::make_pair(N, 1)).second)
2170 return; // Already selected.
2171
2172 SDNode *Node = N.Val;
2173
2174 switch (Node->getOpcode()) {
2175 default:
2176 Node->dump(); std::cerr << "\n";
2177 assert(0 && "Node not handled yet!");
2178 case ISD::EntryToken: return; // Noop
2179 case ISD::TokenFactor:
2180 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
2181 Select(Node->getOperand(i));
2182 return;
2183 case ISD::ADJCALLSTACKDOWN:
2184 case ISD::ADJCALLSTACKUP:
2185 Select(N.getOperand(0));
2186 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
2187 Opc = N.getOpcode() == ISD::ADJCALLSTACKDOWN ? PPC::ADJCALLSTACKDOWN :
2188 PPC::ADJCALLSTACKUP;
2189 BuildMI(BB, Opc, 1).addImm(Tmp1);
2190 return;
2191 case ISD::BR: {
2192 MachineBasicBlock *Dest =
2193 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
Nate Begemana9795f82005-03-24 04:41:43 +00002194 Select(N.getOperand(0));
2195 BuildMI(BB, PPC::B, 1).addMBB(Dest);
2196 return;
2197 }
2198 case ISD::BRCOND:
Nate Begemancd08e4c2005-04-09 20:09:12 +00002199 case ISD::BRCONDTWOWAY:
Nate Begemana9795f82005-03-24 04:41:43 +00002200 SelectBranchCC(N);
2201 return;
2202 case ISD::CopyToReg:
2203 Select(N.getOperand(0));
2204 Tmp1 = SelectExpr(N.getOperand(1));
2205 Tmp2 = cast<RegSDNode>(N)->getReg();
2206
2207 if (Tmp1 != Tmp2) {
2208 if (N.getOperand(1).getValueType() == MVT::f64 ||
2209 N.getOperand(1).getValueType() == MVT::f32)
2210 BuildMI(BB, PPC::FMR, 1, Tmp2).addReg(Tmp1);
2211 else
2212 BuildMI(BB, PPC::OR, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
2213 }
2214 return;
2215 case ISD::ImplicitDef:
2216 Select(N.getOperand(0));
2217 BuildMI(BB, PPC::IMPLICIT_DEF, 0, cast<RegSDNode>(N)->getReg());
2218 return;
2219 case ISD::RET:
2220 switch (N.getNumOperands()) {
2221 default:
2222 assert(0 && "Unknown return instruction!");
2223 case 3:
2224 assert(N.getOperand(1).getValueType() == MVT::i32 &&
2225 N.getOperand(2).getValueType() == MVT::i32 &&
2226 "Unknown two-register value!");
2227 Select(N.getOperand(0));
2228 Tmp1 = SelectExpr(N.getOperand(1));
2229 Tmp2 = SelectExpr(N.getOperand(2));
Nate Begeman27523a12005-04-02 00:42:16 +00002230 BuildMI(BB, PPC::OR, 2, PPC::R3).addReg(Tmp2).addReg(Tmp2);
2231 BuildMI(BB, PPC::OR, 2, PPC::R4).addReg(Tmp1).addReg(Tmp1);
Nate Begemana9795f82005-03-24 04:41:43 +00002232 break;
2233 case 2:
2234 Select(N.getOperand(0));
2235 Tmp1 = SelectExpr(N.getOperand(1));
2236 switch (N.getOperand(1).getValueType()) {
2237 default:
2238 assert(0 && "Unknown return type!");
2239 case MVT::f64:
2240 case MVT::f32:
2241 BuildMI(BB, PPC::FMR, 1, PPC::F1).addReg(Tmp1);
2242 break;
2243 case MVT::i32:
2244 BuildMI(BB, PPC::OR, 2, PPC::R3).addReg(Tmp1).addReg(Tmp1);
2245 break;
2246 }
Nate Begeman9e3e1b52005-03-24 23:35:30 +00002247 case 1:
2248 Select(N.getOperand(0));
2249 break;
Nate Begemana9795f82005-03-24 04:41:43 +00002250 }
2251 BuildMI(BB, PPC::BLR, 0); // Just emit a 'ret' instruction
2252 return;
Nate Begemana9795f82005-03-24 04:41:43 +00002253 case ISD::TRUNCSTORE:
2254 case ISD::STORE:
2255 {
2256 SDOperand Chain = N.getOperand(0);
2257 SDOperand Value = N.getOperand(1);
2258 SDOperand Address = N.getOperand(2);
2259 Select(Chain);
2260
2261 Tmp1 = SelectExpr(Value); //value
2262
2263 if (opcode == ISD::STORE) {
2264 switch(Value.getValueType()) {
2265 default: assert(0 && "unknown Type in store");
2266 case MVT::i32: Opc = PPC::STW; break;
2267 case MVT::f64: Opc = PPC::STFD; break;
2268 case MVT::f32: Opc = PPC::STFS; break;
2269 }
2270 } else { //ISD::TRUNCSTORE
2271 switch(cast<MVTSDNode>(Node)->getExtraValueType()) {
2272 default: assert(0 && "unknown Type in store");
Nate Begeman7e7fadd2005-04-07 20:30:01 +00002273 case MVT::i1:
Nate Begemana9795f82005-03-24 04:41:43 +00002274 case MVT::i8: Opc = PPC::STB; break;
2275 case MVT::i16: Opc = PPC::STH; break;
2276 }
2277 }
2278
Nate Begemana7e11a42005-04-01 05:57:17 +00002279 if(Address.getOpcode() == ISD::FrameIndex)
Nate Begemana9795f82005-03-24 04:41:43 +00002280 {
Nate Begeman58f718c2005-03-30 02:23:08 +00002281 Tmp2 = cast<FrameIndexSDNode>(Address)->getIndex();
2282 addFrameReference(BuildMI(BB, Opc, 3).addReg(Tmp1), (int)Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +00002283 }
2284 else
2285 {
2286 int offset;
Nate Begeman04730362005-04-01 04:45:11 +00002287 bool idx = SelectAddr(Address, Tmp2, offset);
2288 if (idx) {
2289 Opc = IndexedOpForOp(Opc);
2290 BuildMI(BB, Opc, 3).addReg(Tmp1).addReg(Tmp2).addReg(offset);
2291 } else {
2292 BuildMI(BB, Opc, 3).addReg(Tmp1).addImm(offset).addReg(Tmp2);
2293 }
Nate Begemana9795f82005-03-24 04:41:43 +00002294 }
2295 return;
2296 }
2297 case ISD::EXTLOAD:
2298 case ISD::SEXTLOAD:
2299 case ISD::ZEXTLOAD:
2300 case ISD::LOAD:
2301 case ISD::CopyFromReg:
2302 case ISD::CALL:
2303 case ISD::DYNAMIC_STACKALLOC:
2304 ExprMap.erase(N);
2305 SelectExpr(N);
2306 return;
2307 }
2308 assert(0 && "Should not be reached!");
2309}
2310
2311
2312/// createPPC32PatternInstructionSelector - This pass converts an LLVM function
2313/// into a machine code representation using pattern matching and a machine
2314/// description file.
2315///
2316FunctionPass *llvm::createPPC32ISelPattern(TargetMachine &TM) {
2317 return new ISel(TM);
Chris Lattner246fa632005-03-24 06:16:18 +00002318}
2319