blob: 410c62e661ac7b7c6cd67926bed8db44aea1ce59 [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.
Misha Brukmanb5f662f2005-04-21 23:30:14 +00007//
Nate Begemana9795f82005-03-24 04:41:43 +00008//===----------------------------------------------------------------------===//
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) {
Chris Lattner9bce0f92005-05-12 02:06:00 +000046 // Fold away setcc operations if possible.
47 setSetCCIsExpensive();
48
Nate Begemana9795f82005-03-24 04:41:43 +000049 // Set up the register classes.
50 addRegisterClass(MVT::i32, PPC32::GPRCRegisterClass);
Nate Begeman7532e2f2005-03-26 08:25:22 +000051 addRegisterClass(MVT::f32, PPC32::FPRCRegisterClass);
Nate Begemana9795f82005-03-24 04:41:43 +000052 addRegisterClass(MVT::f64, PPC32::FPRCRegisterClass);
Misha Brukmanb5f662f2005-04-21 23:30:14 +000053
Nate Begeman74d73452005-03-31 00:15:26 +000054 // PowerPC has no intrinsics for these particular operations
Nate Begeman01d05262005-03-30 01:45:43 +000055 setOperationAction(ISD::MEMMOVE, MVT::Other, Expand);
56 setOperationAction(ISD::MEMSET, MVT::Other, Expand);
57 setOperationAction(ISD::MEMCPY, MVT::Other, Expand);
58
Nate Begeman74d73452005-03-31 00:15:26 +000059 // PowerPC has an i16 but no i8 (or i1) SEXTLOAD
60 setOperationAction(ISD::SEXTLOAD, MVT::i1, Expand);
61 setOperationAction(ISD::SEXTLOAD, MVT::i8, Expand);
Misha Brukmanb5f662f2005-04-21 23:30:14 +000062
Nate Begeman815d6da2005-04-06 00:25:27 +000063 // PowerPC has no SREM/UREM instructions
64 setOperationAction(ISD::SREM, MVT::i32, Expand);
65 setOperationAction(ISD::UREM, MVT::i32, Expand);
Chris Lattner43fdea02005-04-02 05:03:24 +000066
Chris Lattner32f3cf62005-05-13 16:20:22 +000067 // We don't support sin/cos/sqrt/fmod
Chris Lattner17234b72005-04-30 04:26:06 +000068 setOperationAction(ISD::FSIN , MVT::f64, Expand);
69 setOperationAction(ISD::FCOS , MVT::f64, Expand);
70 setOperationAction(ISD::FSQRT, MVT::f64, Expand);
Chris Lattner32f3cf62005-05-13 16:20:22 +000071 setOperationAction(ISD::SREM , MVT::f64, Expand);
Chris Lattner17234b72005-04-30 04:26:06 +000072 setOperationAction(ISD::FSIN , MVT::f32, Expand);
73 setOperationAction(ISD::FCOS , MVT::f32, Expand);
74 setOperationAction(ISD::FSQRT, MVT::f32, Expand);
Chris Lattner32f3cf62005-05-13 16:20:22 +000075 setOperationAction(ISD::SREM , MVT::f32, Expand);
Chris Lattner17234b72005-04-30 04:26:06 +000076
Nate Begemand7c4a4a2005-05-11 23:43:56 +000077 //PowerPC does not have CTPOP or CTTZ
Andrew Lenharth691ef2b2005-05-03 17:19:30 +000078 setOperationAction(ISD::CTPOP, MVT::i32 , Expand);
79 setOperationAction(ISD::CTTZ , MVT::i32 , Expand);
Andrew Lenharth691ef2b2005-05-03 17:19:30 +000080
Chris Lattnercbd06fc2005-04-07 19:41:49 +000081 setSetCCResultContents(ZeroOrOneSetCCResult);
Nate Begeman3e897162005-03-31 23:55:40 +000082 addLegalFPImmediate(+0.0); // Necessary for FSEL
Misha Brukmanb5f662f2005-04-21 23:30:14 +000083 addLegalFPImmediate(-0.0); //
Nate Begeman3e897162005-03-31 23:55:40 +000084
Nate Begemana9795f82005-03-24 04:41:43 +000085 computeRegisterProperties();
86 }
87
88 /// LowerArguments - This hook must be implemented to indicate how we should
89 /// lower the arguments for the specified function, into the specified DAG.
90 virtual std::vector<SDOperand>
91 LowerArguments(Function &F, SelectionDAG &DAG);
Misha Brukmanb5f662f2005-04-21 23:30:14 +000092
Nate Begemana9795f82005-03-24 04:41:43 +000093 /// LowerCallTo - This hook lowers an abstract call to a function into an
94 /// actual call.
95 virtual std::pair<SDOperand, SDOperand>
Chris Lattnerc57f6822005-05-12 19:56:45 +000096 LowerCallTo(SDOperand Chain, const Type *RetTy, bool isVarArg, unsigned CC,
Chris Lattneradf6a962005-05-13 18:50:42 +000097 bool isTailCall, SDOperand Callee, ArgListTy &Args,
98 SelectionDAG &DAG);
Misha Brukmanb5f662f2005-04-21 23:30:14 +000099
Nate Begemana9795f82005-03-24 04:41:43 +0000100 virtual std::pair<SDOperand, SDOperand>
101 LowerVAStart(SDOperand Chain, SelectionDAG &DAG);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000102
Nate Begemana9795f82005-03-24 04:41:43 +0000103 virtual std::pair<SDOperand,SDOperand>
104 LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
105 const Type *ArgTy, SelectionDAG &DAG);
106
107 virtual std::pair<SDOperand, SDOperand>
108 LowerFrameReturnAddress(bool isFrameAddr, SDOperand Chain, unsigned Depth,
109 SelectionDAG &DAG);
110 };
111}
112
113
114std::vector<SDOperand>
115PPC32TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
116 //
117 // add beautiful description of PPC stack frame format, or at least some docs
118 //
119 MachineFunction &MF = DAG.getMachineFunction();
120 MachineFrameInfo *MFI = MF.getFrameInfo();
121 MachineBasicBlock& BB = MF.front();
122 std::vector<SDOperand> ArgValues;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000123
124 // Due to the rather complicated nature of the PowerPC ABI, rather than a
Nate Begemana9795f82005-03-24 04:41:43 +0000125 // fixed size array of physical args, for the sake of simplicity let the STL
126 // handle tracking them for us.
127 std::vector<unsigned> argVR, argPR, argOp;
128 unsigned ArgOffset = 24;
129 unsigned GPR_remaining = 8;
130 unsigned FPR_remaining = 13;
131 unsigned GPR_idx = 0, FPR_idx = 0;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000132 static const unsigned GPR[] = {
Nate Begemana9795f82005-03-24 04:41:43 +0000133 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
134 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
135 };
136 static const unsigned FPR[] = {
137 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
138 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
139 };
140
141 // Add DAG nodes to load the arguments... On entry to a function on PPC,
142 // the arguments start at offset 24, although they are likely to be passed
143 // in registers.
144 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
145 SDOperand newroot, argt;
146 unsigned ObjSize;
147 bool needsLoad = false;
Nate Begemancd08e4c2005-04-09 20:09:12 +0000148 bool ArgLive = !I->use_empty();
Nate Begemana9795f82005-03-24 04:41:43 +0000149 MVT::ValueType ObjectVT = getValueType(I->getType());
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000150
Nate Begemana9795f82005-03-24 04:41:43 +0000151 switch (ObjectVT) {
152 default: assert(0 && "Unhandled argument type!");
153 case MVT::i1:
154 case MVT::i8:
155 case MVT::i16:
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000156 case MVT::i32:
Nate Begemana9795f82005-03-24 04:41:43 +0000157 ObjSize = 4;
Nate Begemancd08e4c2005-04-09 20:09:12 +0000158 if (!ArgLive) break;
Nate Begemana9795f82005-03-24 04:41:43 +0000159 if (GPR_remaining > 0) {
Nate Begemancd08e4c2005-04-09 20:09:12 +0000160 MF.addLiveIn(GPR[GPR_idx]);
Nate Begemanf70b5762005-03-28 23:08:54 +0000161 argt = newroot = DAG.getCopyFromReg(GPR[GPR_idx], MVT::i32,
162 DAG.getRoot());
Nate Begemana9795f82005-03-24 04:41:43 +0000163 if (ObjectVT != MVT::i32)
164 argt = DAG.getNode(ISD::TRUNCATE, ObjectVT, newroot);
Nate Begemana9795f82005-03-24 04:41:43 +0000165 } else {
166 needsLoad = true;
167 }
168 break;
Nate Begemanf7e43382005-03-26 07:46:36 +0000169 case MVT::i64: ObjSize = 8;
Nate Begemancd08e4c2005-04-09 20:09:12 +0000170 if (!ArgLive) break;
Nate Begemanc5b1cd22005-04-10 05:53:14 +0000171 if (GPR_remaining > 0) {
172 SDOperand argHi, argLo;
Nate Begemancd08e4c2005-04-09 20:09:12 +0000173 MF.addLiveIn(GPR[GPR_idx]);
Nate Begemanc5b1cd22005-04-10 05:53:14 +0000174 argHi = DAG.getCopyFromReg(GPR[GPR_idx], MVT::i32, DAG.getRoot());
175 // If we have two or more remaining argument registers, then both halves
176 // of the i64 can be sourced from there. Otherwise, the lower half will
177 // have to come off the stack. This can happen when an i64 is preceded
178 // by 28 bytes of arguments.
179 if (GPR_remaining > 1) {
180 MF.addLiveIn(GPR[GPR_idx+1]);
181 argLo = DAG.getCopyFromReg(GPR[GPR_idx+1], MVT::i32, argHi);
182 } else {
183 int FI = MFI->CreateFixedObject(4, ArgOffset+4);
184 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
Chris Lattner022ed322005-05-15 19:54:37 +0000185 argLo = DAG.getLoad(MVT::i32, DAG.getEntryNode(), FIN,
186 DAG.getSrcValue(NULL));
Nate Begemanc5b1cd22005-04-10 05:53:14 +0000187 }
Nate Begemanca12a2b2005-03-28 22:28:37 +0000188 // Build the outgoing arg thingy
Nate Begemanf70b5762005-03-28 23:08:54 +0000189 argt = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, argLo, argHi);
190 newroot = argLo;
Nate Begemana9795f82005-03-24 04:41:43 +0000191 } else {
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000192 needsLoad = true;
Nate Begemana9795f82005-03-24 04:41:43 +0000193 }
194 break;
Nate Begemancd08e4c2005-04-09 20:09:12 +0000195 case MVT::f32:
196 case MVT::f64:
197 ObjSize = (ObjectVT == MVT::f64) ? 8 : 4;
198 if (!ArgLive) break;
Nate Begemana9795f82005-03-24 04:41:43 +0000199 if (FPR_remaining > 0) {
Nate Begemancd08e4c2005-04-09 20:09:12 +0000200 MF.addLiveIn(FPR[FPR_idx]);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000201 argt = newroot = DAG.getCopyFromReg(FPR[FPR_idx], ObjectVT,
Nate Begemanf70b5762005-03-28 23:08:54 +0000202 DAG.getRoot());
Nate Begemana9795f82005-03-24 04:41:43 +0000203 --FPR_remaining;
204 ++FPR_idx;
205 } else {
206 needsLoad = true;
207 }
208 break;
209 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000210
Nate Begemana9795f82005-03-24 04:41:43 +0000211 // We need to load the argument to a virtual register if we determined above
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000212 // that we ran out of physical registers of the appropriate type
Nate Begemana9795f82005-03-24 04:41:43 +0000213 if (needsLoad) {
Nate Begemane5846682005-04-04 06:52:38 +0000214 unsigned SubregOffset = 0;
Nate Begemanc3e2db42005-04-04 09:09:00 +0000215 if (ObjectVT == MVT::i8 || ObjectVT == MVT::i1) SubregOffset = 3;
Nate Begemane5846682005-04-04 06:52:38 +0000216 if (ObjectVT == MVT::i16) SubregOffset = 2;
Nate Begemana9795f82005-03-24 04:41:43 +0000217 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
218 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000219 FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN,
Nate Begemane5846682005-04-04 06:52:38 +0000220 DAG.getConstant(SubregOffset, MVT::i32));
Chris Lattner022ed322005-05-15 19:54:37 +0000221 argt = newroot = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
222 DAG.getSrcValue(NULL));
Nate Begemana9795f82005-03-24 04:41:43 +0000223 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000224
Nate Begemana9795f82005-03-24 04:41:43 +0000225 // Every 4 bytes of argument space consumes one of the GPRs available for
226 // argument passing.
227 if (GPR_remaining > 0) {
228 unsigned delta = (GPR_remaining > 1 && ObjSize == 8) ? 2 : 1;
229 GPR_remaining -= delta;
230 GPR_idx += delta;
231 }
232 ArgOffset += ObjSize;
Chris Lattner91277ea2005-04-09 21:23:24 +0000233 if (newroot.Val)
234 DAG.setRoot(newroot.getValue(1));
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000235
Nate Begemana9795f82005-03-24 04:41:43 +0000236 ArgValues.push_back(argt);
237 }
238
Nate Begemana9795f82005-03-24 04:41:43 +0000239 // If the function takes variable number of arguments, make a frame index for
240 // the start of the first vararg value... for expansion of llvm.va_start.
Nate Begemanfa554702005-04-03 22:13:27 +0000241 if (F.isVarArg()) {
Nate Begemana9795f82005-03-24 04:41:43 +0000242 VarArgsFrameIndex = MFI->CreateFixedObject(4, ArgOffset);
Nate Begemanfa554702005-04-03 22:13:27 +0000243 SDOperand FIN = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
Nate Begeman6644d4c2005-04-03 23:11:17 +0000244 // If this function is vararg, store any remaining integer argument regs
245 // to their spots on the stack so that they may be loaded by deferencing the
246 // result of va_next.
247 std::vector<SDOperand> MemOps;
248 for (; GPR_remaining > 0; --GPR_remaining, ++GPR_idx) {
Nate Begemancd08e4c2005-04-09 20:09:12 +0000249 MF.addLiveIn(GPR[GPR_idx]);
Nate Begeman6644d4c2005-04-03 23:11:17 +0000250 SDOperand Val = DAG.getCopyFromReg(GPR[GPR_idx], MVT::i32, DAG.getRoot());
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000251 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Val.getValue(1),
Andrew Lenharth2d86ea22005-04-27 20:10:01 +0000252 Val, FIN, DAG.getSrcValue(NULL));
Nate Begeman6644d4c2005-04-03 23:11:17 +0000253 MemOps.push_back(Store);
254 // Increment the address by four for the next argument to store
255 SDOperand PtrOff = DAG.getConstant(4, getPointerTy());
256 FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN, PtrOff);
257 }
258 DAG.setRoot(DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps));
Nate Begemanfa554702005-04-03 22:13:27 +0000259 }
Nate Begemana9795f82005-03-24 04:41:43 +0000260
Nate Begemancd08e4c2005-04-09 20:09:12 +0000261 // Finally, inform the code generator which regs we return values in.
262 switch (getValueType(F.getReturnType())) {
263 default: assert(0 && "Unknown type!");
264 case MVT::isVoid: break;
265 case MVT::i1:
266 case MVT::i8:
267 case MVT::i16:
268 case MVT::i32:
269 MF.addLiveOut(PPC::R3);
270 break;
271 case MVT::i64:
272 MF.addLiveOut(PPC::R3);
273 MF.addLiveOut(PPC::R4);
274 break;
275 case MVT::f32:
276 case MVT::f64:
277 MF.addLiveOut(PPC::F1);
278 break;
279 }
280
Nate Begemana9795f82005-03-24 04:41:43 +0000281 return ArgValues;
282}
283
284std::pair<SDOperand, SDOperand>
285PPC32TargetLowering::LowerCallTo(SDOperand Chain,
Misha Brukman7847fca2005-04-22 17:54:37 +0000286 const Type *RetTy, bool isVarArg,
Chris Lattneradf6a962005-05-13 18:50:42 +0000287 unsigned CallingConv, bool isTailCall,
Misha Brukman7847fca2005-04-22 17:54:37 +0000288 SDOperand Callee, ArgListTy &Args,
289 SelectionDAG &DAG) {
Nate Begeman307e7442005-03-26 01:28:53 +0000290 // args_to_use will accumulate outgoing args for the ISD::CALL case in
291 // SelectExpr to use to put the arguments in the appropriate registers.
Nate Begemana9795f82005-03-24 04:41:43 +0000292 std::vector<SDOperand> args_to_use;
Nate Begeman307e7442005-03-26 01:28:53 +0000293
294 // Count how many bytes are to be pushed on the stack, including the linkage
295 // area, and parameter passing area.
296 unsigned NumBytes = 24;
297
298 if (Args.empty()) {
Chris Lattner16cd04d2005-05-12 23:24:06 +0000299 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
Nate Begemana7e11a42005-04-01 05:57:17 +0000300 DAG.getConstant(NumBytes, getPointerTy()));
Nate Begeman307e7442005-03-26 01:28:53 +0000301 } else {
302 for (unsigned i = 0, e = Args.size(); i != e; ++i)
303 switch (getValueType(Args[i].second)) {
304 default: assert(0 && "Unknown value type!");
305 case MVT::i1:
306 case MVT::i8:
307 case MVT::i16:
308 case MVT::i32:
309 case MVT::f32:
310 NumBytes += 4;
311 break;
312 case MVT::i64:
313 case MVT::f64:
314 NumBytes += 8;
315 break;
316 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000317
318 // Just to be safe, we'll always reserve the full 24 bytes of linkage area
Nate Begeman307e7442005-03-26 01:28:53 +0000319 // plus 32 bytes of argument space in case any called code gets funky on us.
320 if (NumBytes < 56) NumBytes = 56;
321
322 // Adjust the stack pointer for the new arguments...
323 // These operations are automatically eliminated by the prolog/epilog pass
Chris Lattner16cd04d2005-05-12 23:24:06 +0000324 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
Nate Begeman307e7442005-03-26 01:28:53 +0000325 DAG.getConstant(NumBytes, getPointerTy()));
326
327 // Set up a copy of the stack pointer for use loading and storing any
328 // arguments that may not fit in the registers available for argument
329 // passing.
330 SDOperand StackPtr = DAG.getCopyFromReg(PPC::R1, MVT::i32,
331 DAG.getEntryNode());
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000332
Nate Begeman307e7442005-03-26 01:28:53 +0000333 // Figure out which arguments are going to go in registers, and which in
334 // memory. Also, if this is a vararg function, floating point operations
335 // must be stored to our stack, and loaded into integer regs as well, if
336 // any integer regs are available for argument passing.
337 unsigned ArgOffset = 24;
338 unsigned GPR_remaining = 8;
339 unsigned FPR_remaining = 13;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000340
Nate Begeman74d73452005-03-31 00:15:26 +0000341 std::vector<SDOperand> MemOps;
Nate Begeman307e7442005-03-26 01:28:53 +0000342 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
343 // PtrOff will be used to store the current argument to the stack if a
344 // register cannot be found for it.
345 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
346 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
Nate Begemanf7e43382005-03-26 07:46:36 +0000347 MVT::ValueType ArgVT = getValueType(Args[i].second);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000348
Nate Begemanf7e43382005-03-26 07:46:36 +0000349 switch (ArgVT) {
Nate Begeman307e7442005-03-26 01:28:53 +0000350 default: assert(0 && "Unexpected ValueType for argument!");
351 case MVT::i1:
352 case MVT::i8:
353 case MVT::i16:
354 // Promote the integer to 32 bits. If the input type is signed use a
355 // sign extend, otherwise use a zero extend.
356 if (Args[i].second->isSigned())
357 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
358 else
359 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
360 // FALL THROUGH
361 case MVT::i32:
362 if (GPR_remaining > 0) {
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000363 args_to_use.push_back(Args[i].first);
Nate Begeman307e7442005-03-26 01:28:53 +0000364 --GPR_remaining;
365 } else {
Nate Begeman74d73452005-03-31 00:15:26 +0000366 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
Chris Lattner022ed322005-05-15 19:54:37 +0000367 Args[i].first, PtrOff,
368 DAG.getSrcValue(NULL)));
Nate Begeman307e7442005-03-26 01:28:53 +0000369 }
370 ArgOffset += 4;
371 break;
372 case MVT::i64:
Nate Begemanf7e43382005-03-26 07:46:36 +0000373 // If we have one free GPR left, we can place the upper half of the i64
374 // in it, and store the other half to the stack. If we have two or more
375 // free GPRs, then we can pass both halves of the i64 in registers.
376 if (GPR_remaining > 0) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000377 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
Nate Begemanf2622612005-03-26 02:17:46 +0000378 Args[i].first, DAG.getConstant(1, MVT::i32));
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000379 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
Nate Begemanf2622612005-03-26 02:17:46 +0000380 Args[i].first, DAG.getConstant(0, MVT::i32));
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000381 args_to_use.push_back(Hi);
Nate Begeman74d73452005-03-31 00:15:26 +0000382 --GPR_remaining;
Nate Begeman74d73452005-03-31 00:15:26 +0000383 if (GPR_remaining > 0) {
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000384 args_to_use.push_back(Lo);
Nate Begeman74d73452005-03-31 00:15:26 +0000385 --GPR_remaining;
Nate Begemanf7e43382005-03-26 07:46:36 +0000386 } else {
387 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
388 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
Nate Begeman74d73452005-03-31 00:15:26 +0000389 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +0000390 Lo, PtrOff, DAG.getSrcValue(NULL)));
Nate Begemanf7e43382005-03-26 07:46:36 +0000391 }
Nate Begeman307e7442005-03-26 01:28:53 +0000392 } else {
Nate Begeman74d73452005-03-31 00:15:26 +0000393 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
Chris Lattner022ed322005-05-15 19:54:37 +0000394 Args[i].first, PtrOff,
395 DAG.getSrcValue(NULL)));
Nate Begeman307e7442005-03-26 01:28:53 +0000396 }
397 ArgOffset += 8;
398 break;
399 case MVT::f32:
Nate Begeman307e7442005-03-26 01:28:53 +0000400 case MVT::f64:
Nate Begemanf7e43382005-03-26 07:46:36 +0000401 if (FPR_remaining > 0) {
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000402 args_to_use.push_back(Args[i].first);
403 --FPR_remaining;
Nate Begemanf7e43382005-03-26 07:46:36 +0000404 if (isVarArg) {
Nate Begeman96fc6812005-03-31 02:05:53 +0000405 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Chain,
Chris Lattner022ed322005-05-15 19:54:37 +0000406 Args[i].first, PtrOff,
407 DAG.getSrcValue(NULL));
Nate Begeman96fc6812005-03-31 02:05:53 +0000408 MemOps.push_back(Store);
Nate Begeman74d73452005-03-31 00:15:26 +0000409 // Float varargs are always shadowed in available integer registers
410 if (GPR_remaining > 0) {
Chris Lattner022ed322005-05-15 19:54:37 +0000411 SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff,
412 DAG.getSrcValue(NULL));
Nate Begeman74d73452005-03-31 00:15:26 +0000413 MemOps.push_back(Load);
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000414 args_to_use.push_back(Load);
415 --GPR_remaining;
Nate Begeman74d73452005-03-31 00:15:26 +0000416 }
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000417 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
Nate Begeman74d73452005-03-31 00:15:26 +0000418 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
419 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
Chris Lattner022ed322005-05-15 19:54:37 +0000420 SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff,
421 DAG.getSrcValue(NULL));
Nate Begeman74d73452005-03-31 00:15:26 +0000422 MemOps.push_back(Load);
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000423 args_to_use.push_back(Load);
424 --GPR_remaining;
Nate Begeman74d73452005-03-31 00:15:26 +0000425 }
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000426 } else {
427 // If we have any FPRs remaining, we may also have GPRs remaining.
428 // Args passed in FPRs consume either 1 (f32) or 2 (f64) available
429 // GPRs.
430 if (GPR_remaining > 0) {
431 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
432 --GPR_remaining;
433 }
434 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
435 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
436 --GPR_remaining;
437 }
Nate Begeman74d73452005-03-31 00:15:26 +0000438 }
Nate Begeman307e7442005-03-26 01:28:53 +0000439 } else {
Nate Begeman74d73452005-03-31 00:15:26 +0000440 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
Chris Lattner022ed322005-05-15 19:54:37 +0000441 Args[i].first, PtrOff,
442 DAG.getSrcValue(NULL)));
Nate Begeman307e7442005-03-26 01:28:53 +0000443 }
Nate Begemanf7e43382005-03-26 07:46:36 +0000444 ArgOffset += (ArgVT == MVT::f32) ? 4 : 8;
Nate Begeman307e7442005-03-26 01:28:53 +0000445 break;
446 }
Nate Begemana9795f82005-03-24 04:41:43 +0000447 }
Nate Begeman74d73452005-03-31 00:15:26 +0000448 if (!MemOps.empty())
449 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps);
Nate Begemana9795f82005-03-24 04:41:43 +0000450 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000451
Nate Begemana9795f82005-03-24 04:41:43 +0000452 std::vector<MVT::ValueType> RetVals;
453 MVT::ValueType RetTyVT = getValueType(RetTy);
454 if (RetTyVT != MVT::isVoid)
455 RetVals.push_back(RetTyVT);
456 RetVals.push_back(MVT::Other);
457
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000458 SDOperand TheCall = SDOperand(DAG.getCall(RetVals,
Nate Begemana9795f82005-03-24 04:41:43 +0000459 Chain, Callee, args_to_use), 0);
460 Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
Chris Lattner16cd04d2005-05-12 23:24:06 +0000461 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain,
Nate Begemana9795f82005-03-24 04:41:43 +0000462 DAG.getConstant(NumBytes, getPointerTy()));
463 return std::make_pair(TheCall, Chain);
464}
465
466std::pair<SDOperand, SDOperand>
467PPC32TargetLowering::LowerVAStart(SDOperand Chain, SelectionDAG &DAG) {
468 //vastart just returns the address of the VarArgsFrameIndex slot.
469 return std::make_pair(DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32), Chain);
470}
471
472std::pair<SDOperand,SDOperand> PPC32TargetLowering::
473LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
474 const Type *ArgTy, SelectionDAG &DAG) {
Nate Begemanc7b09f12005-03-25 08:34:25 +0000475 MVT::ValueType ArgVT = getValueType(ArgTy);
476 SDOperand Result;
477 if (!isVANext) {
Chris Lattner022ed322005-05-15 19:54:37 +0000478 Result = DAG.getLoad(ArgVT, DAG.getEntryNode(), VAList,
479 DAG.getSrcValue(NULL));
Nate Begemanc7b09f12005-03-25 08:34:25 +0000480 } else {
481 unsigned Amt;
482 if (ArgVT == MVT::i32 || ArgVT == MVT::f32)
483 Amt = 4;
484 else {
485 assert((ArgVT == MVT::i64 || ArgVT == MVT::f64) &&
486 "Other types should have been promoted for varargs!");
487 Amt = 8;
488 }
489 Result = DAG.getNode(ISD::ADD, VAList.getValueType(), VAList,
490 DAG.getConstant(Amt, VAList.getValueType()));
491 }
492 return std::make_pair(Result, Chain);
Nate Begemana9795f82005-03-24 04:41:43 +0000493}
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000494
Nate Begemana9795f82005-03-24 04:41:43 +0000495
496std::pair<SDOperand, SDOperand> PPC32TargetLowering::
497LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
498 SelectionDAG &DAG) {
Nate Begeman01d05262005-03-30 01:45:43 +0000499 assert(0 && "LowerFrameReturnAddress unimplemented");
Nate Begemana9795f82005-03-24 04:41:43 +0000500 abort();
501}
502
503namespace {
Nate Begemanc7bd4822005-04-11 06:34:10 +0000504Statistic<>Recorded("ppc-codegen", "Number of recording ops emitted");
Nate Begeman93075ec2005-04-04 23:40:36 +0000505Statistic<>FusedFP("ppc-codegen", "Number of fused fp operations");
Nate Begeman7bfba7d2005-04-14 09:45:08 +0000506Statistic<>MultiBranch("ppc-codegen", "Number of setcc logical ops collapsed");
Nate Begemana9795f82005-03-24 04:41:43 +0000507//===--------------------------------------------------------------------===//
508/// ISel - PPC32 specific code to select PPC32 machine instructions for
509/// SelectionDAG operations.
510//===--------------------------------------------------------------------===//
511class ISel : public SelectionDAGISel {
Nate Begemana9795f82005-03-24 04:41:43 +0000512 PPC32TargetLowering PPC32Lowering;
Nate Begeman815d6da2005-04-06 00:25:27 +0000513 SelectionDAG *ISelDAG; // Hack to support us having a dag->dag transform
514 // for sdiv and udiv until it is put into the future
515 // dag combiner.
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000516
Nate Begemana9795f82005-03-24 04:41:43 +0000517 /// ExprMap - As shared expressions are codegen'd, we keep track of which
518 /// vreg the value is produced in, so we only emit one copy of each compiled
519 /// tree.
520 std::map<SDOperand, unsigned> ExprMap;
Nate Begemanc7b09f12005-03-25 08:34:25 +0000521
522 unsigned GlobalBaseReg;
523 bool GlobalBaseInitialized;
Nate Begemanc7bd4822005-04-11 06:34:10 +0000524 bool RecordSuccess;
Nate Begemana9795f82005-03-24 04:41:43 +0000525public:
Nate Begeman815d6da2005-04-06 00:25:27 +0000526 ISel(TargetMachine &TM) : SelectionDAGISel(PPC32Lowering), PPC32Lowering(TM),
527 ISelDAG(0) {}
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000528
Nate Begemanc7b09f12005-03-25 08:34:25 +0000529 /// runOnFunction - Override this function in order to reset our per-function
530 /// variables.
531 virtual bool runOnFunction(Function &Fn) {
532 // Make sure we re-emit a set of the global base reg if necessary
533 GlobalBaseInitialized = false;
534 return SelectionDAGISel::runOnFunction(Fn);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000535 }
536
Nate Begemana9795f82005-03-24 04:41:43 +0000537 /// InstructionSelectBasicBlock - This callback is invoked by
538 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
539 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG) {
540 DEBUG(BB->dump());
541 // Codegen the basic block.
Nate Begeman815d6da2005-04-06 00:25:27 +0000542 ISelDAG = &DAG;
Nate Begemana9795f82005-03-24 04:41:43 +0000543 Select(DAG.getRoot());
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000544
Nate Begemana9795f82005-03-24 04:41:43 +0000545 // Clear state used for selection.
546 ExprMap.clear();
Nate Begeman815d6da2005-04-06 00:25:27 +0000547 ISelDAG = 0;
Nate Begemana9795f82005-03-24 04:41:43 +0000548 }
Nate Begeman815d6da2005-04-06 00:25:27 +0000549
550 // dag -> dag expanders for integer divide by constant
551 SDOperand BuildSDIVSequence(SDOperand N);
552 SDOperand BuildUDIVSequence(SDOperand N);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000553
Nate Begemandffcfcc2005-04-01 00:32:34 +0000554 unsigned getGlobalBaseReg();
Nate Begeman6b559972005-04-01 02:59:27 +0000555 unsigned getConstDouble(double floatVal, unsigned Result);
Nate Begeman1cbf3ab2005-04-18 07:48:09 +0000556 void MoveCRtoGPR(unsigned CCReg, bool Inv, unsigned Idx, unsigned Result);
Nate Begeman7ddecb42005-04-06 23:51:40 +0000557 bool SelectBitfieldInsert(SDOperand OR, unsigned Result);
Nate Begeman3664cef2005-04-13 22:14:14 +0000558 unsigned FoldIfWideZeroExtend(SDOperand N);
Nate Begeman1cbf3ab2005-04-18 07:48:09 +0000559 unsigned SelectCC(SDOperand CC, unsigned &Opc, bool &Inv, unsigned &Idx);
560 unsigned SelectCCExpr(SDOperand N, unsigned& Opc, bool &Inv, unsigned &Idx);
Nate Begemanc7bd4822005-04-11 06:34:10 +0000561 unsigned SelectExpr(SDOperand N, bool Recording=false);
Nate Begemana9795f82005-03-24 04:41:43 +0000562 unsigned SelectExprFP(SDOperand N, unsigned Result);
563 void Select(SDOperand N);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000564
Nate Begeman04730362005-04-01 04:45:11 +0000565 bool SelectAddr(SDOperand N, unsigned& Reg, int& offset);
Nate Begemana9795f82005-03-24 04:41:43 +0000566 void SelectBranchCC(SDOperand N);
567};
568
Nate Begeman80196b12005-04-05 00:15:08 +0000569/// ExactLog2 - This function solves for (Val == 1 << (N-1)) and returns N. It
570/// returns zero when the input is not exactly a power of two.
571static unsigned ExactLog2(unsigned Val) {
572 if (Val == 0 || (Val & (Val-1))) return 0;
573 unsigned Count = 0;
574 while (Val != 1) {
575 Val >>= 1;
576 ++Count;
577 }
578 return Count;
579}
580
Nate Begeman7ddecb42005-04-06 23:51:40 +0000581// IsRunOfOnes - returns true if Val consists of one contiguous run of 1's with
582// any number of 0's on either side. the 1's are allowed to wrap from LSB to
583// MSB. so 0x000FFF0, 0x0000FFFF, and 0xFF0000FF are all runs. 0x0F0F0000 is
584// not, since all 1's are not contiguous.
585static bool IsRunOfOnes(unsigned Val, unsigned &MB, unsigned &ME) {
586 bool isRun = true;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000587 MB = 0;
Nate Begeman7ddecb42005-04-06 23:51:40 +0000588 ME = 0;
589
590 // look for first set bit
591 int i = 0;
592 for (; i < 32; i++) {
593 if ((Val & (1 << (31 - i))) != 0) {
594 MB = i;
595 ME = i;
596 break;
597 }
598 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000599
Nate Begeman7ddecb42005-04-06 23:51:40 +0000600 // look for last set bit
601 for (; i < 32; i++) {
602 if ((Val & (1 << (31 - i))) == 0)
603 break;
604 ME = i;
605 }
606
607 // look for next set bit
608 for (; i < 32; i++) {
609 if ((Val & (1 << (31 - i))) != 0)
610 break;
611 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000612
Nate Begeman7ddecb42005-04-06 23:51:40 +0000613 // if we exhausted all the bits, we found a match at this point for 0*1*0*
614 if (i == 32)
615 return true;
616
617 // since we just encountered more 1's, if it doesn't wrap around to the
618 // most significant bit of the word, then we did not find a match to 1*0*1* so
619 // exit.
620 if (MB != 0)
621 return false;
622
623 // look for last set bit
624 for (MB = i; i < 32; i++) {
625 if ((Val & (1 << (31 - i))) == 0)
626 break;
627 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000628
Nate Begeman7ddecb42005-04-06 23:51:40 +0000629 // if we exhausted all the bits, then we found a match for 1*0*1*, otherwise,
630 // the value is not a run of ones.
631 if (i == 32)
632 return true;
633 return false;
634}
635
Nate Begeman439b4442005-04-05 04:22:58 +0000636/// getImmediateForOpcode - This method returns a value indicating whether
Nate Begemana9795f82005-03-24 04:41:43 +0000637/// the ConstantSDNode N can be used as an immediate to Opcode. The return
638/// values are either 0, 1 or 2. 0 indicates that either N is not a
Nate Begeman9f833d32005-04-12 00:10:02 +0000639/// ConstantSDNode, or is not suitable for use by that opcode.
640/// Return value codes for turning into an enum someday:
641/// 1: constant may be used in normal immediate form.
642/// 2: constant may be used in shifted immediate form.
643/// 3: log base 2 of the constant may be used.
644/// 4: constant is suitable for integer division conversion
645/// 5: constant is a bitfield mask
Nate Begemana9795f82005-03-24 04:41:43 +0000646///
Nate Begeman439b4442005-04-05 04:22:58 +0000647static unsigned getImmediateForOpcode(SDOperand N, unsigned Opcode,
648 unsigned& Imm, bool U = false) {
Nate Begemana9795f82005-03-24 04:41:43 +0000649 if (N.getOpcode() != ISD::Constant) return 0;
650
651 int v = (int)cast<ConstantSDNode>(N)->getSignExtended();
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000652
Nate Begemana9795f82005-03-24 04:41:43 +0000653 switch(Opcode) {
654 default: return 0;
655 case ISD::ADD:
656 if (v <= 32767 && v >= -32768) { Imm = v & 0xFFFF; return 1; }
657 if ((v & 0x0000FFFF) == 0) { Imm = v >> 16; return 2; }
658 break;
Nate Begeman9f833d32005-04-12 00:10:02 +0000659 case ISD::AND: {
660 unsigned MB, ME;
661 if (IsRunOfOnes(v, MB, ME)) { Imm = MB << 16 | ME & 0xFFFF; return 5; }
662 if (v >= 0 && v <= 65535) { Imm = v & 0xFFFF; return 1; }
663 if ((v & 0x0000FFFF) == 0) { Imm = v >> 16; return 2; }
664 break;
665 }
Nate Begemana9795f82005-03-24 04:41:43 +0000666 case ISD::XOR:
667 case ISD::OR:
668 if (v >= 0 && v <= 65535) { Imm = v & 0xFFFF; return 1; }
669 if ((v & 0x0000FFFF) == 0) { Imm = v >> 16; return 2; }
670 break;
Nate Begeman307e7442005-03-26 01:28:53 +0000671 case ISD::MUL:
672 if (v <= 32767 && v >= -32768) { Imm = v & 0xFFFF; return 1; }
673 break;
Nate Begemand7c4a4a2005-05-11 23:43:56 +0000674 case ISD::SUB:
675 // handle subtract-from separately from subtract, since subi is really addi
676 if (U && v <= 32767 && v >= -32768) { Imm = v & 0xFFFF; return 1; }
677 if (!U && v <= 32768 && v >= -32767) { Imm = (-v) & 0xFFFF; return 1; }
678 break;
Nate Begeman3e897162005-03-31 23:55:40 +0000679 case ISD::SETCC:
680 if (U && (v >= 0 && v <= 65535)) { Imm = v & 0xFFFF; return 1; }
681 if (!U && (v <= 32767 && v >= -32768)) { Imm = v & 0xFFFF; return 1; }
682 break;
Nate Begeman80196b12005-04-05 00:15:08 +0000683 case ISD::SDIV:
Nate Begeman439b4442005-04-05 04:22:58 +0000684 if ((Imm = ExactLog2(v))) { return 3; }
Nate Begeman9f833d32005-04-12 00:10:02 +0000685 if ((Imm = ExactLog2(-v))) { Imm = -Imm; return 3; }
Nate Begeman815d6da2005-04-06 00:25:27 +0000686 if (v <= -2 || v >= 2) { return 4; }
687 break;
688 case ISD::UDIV:
Nate Begeman27b4c232005-04-06 06:44:57 +0000689 if (v > 1) { return 4; }
Nate Begeman80196b12005-04-05 00:15:08 +0000690 break;
Nate Begemana9795f82005-03-24 04:41:43 +0000691 }
692 return 0;
693}
Nate Begeman3e897162005-03-31 23:55:40 +0000694
Nate Begemanc7bd4822005-04-11 06:34:10 +0000695/// NodeHasRecordingVariant - If SelectExpr can always produce code for
696/// NodeOpcode that also sets CR0 as a side effect, return true. Otherwise,
697/// return false.
698static bool NodeHasRecordingVariant(unsigned NodeOpcode) {
699 switch(NodeOpcode) {
700 default: return false;
701 case ISD::AND:
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000702 case ISD::OR:
Chris Lattner519f40b2005-04-13 02:46:17 +0000703 return true;
Nate Begemanc7bd4822005-04-11 06:34:10 +0000704 }
705}
706
Nate Begeman3e897162005-03-31 23:55:40 +0000707/// getBCCForSetCC - Returns the PowerPC condition branch mnemonic corresponding
708/// to Condition. If the Condition is unordered or unsigned, the bool argument
709/// U is set to true, otherwise it is set to false.
710static unsigned getBCCForSetCC(unsigned Condition, bool& U) {
711 U = false;
712 switch (Condition) {
713 default: assert(0 && "Unknown condition!"); abort();
714 case ISD::SETEQ: return PPC::BEQ;
715 case ISD::SETNE: return PPC::BNE;
716 case ISD::SETULT: U = true;
717 case ISD::SETLT: return PPC::BLT;
718 case ISD::SETULE: U = true;
719 case ISD::SETLE: return PPC::BLE;
720 case ISD::SETUGT: U = true;
721 case ISD::SETGT: return PPC::BGT;
722 case ISD::SETUGE: U = true;
723 case ISD::SETGE: return PPC::BGE;
724 }
Nate Begeman04730362005-04-01 04:45:11 +0000725 return 0;
726}
727
Nate Begeman7bfba7d2005-04-14 09:45:08 +0000728/// getCROpForOp - Return the condition register opcode (or inverted opcode)
729/// associated with the SelectionDAG opcode.
730static unsigned getCROpForSetCC(unsigned Opcode, bool Inv1, bool Inv2) {
731 switch (Opcode) {
732 default: assert(0 && "Unknown opcode!"); abort();
733 case ISD::AND:
734 if (Inv1 && Inv2) return PPC::CRNOR; // De Morgan's Law
735 if (!Inv1 && !Inv2) return PPC::CRAND;
736 if (Inv1 ^ Inv2) return PPC::CRANDC;
737 case ISD::OR:
738 if (Inv1 && Inv2) return PPC::CRNAND; // De Morgan's Law
739 if (!Inv1 && !Inv2) return PPC::CROR;
740 if (Inv1 ^ Inv2) return PPC::CRORC;
741 }
742 return 0;
743}
744
745/// getCRIdxForSetCC - Return the index of the condition register field
746/// associated with the SetCC condition, and whether or not the field is
747/// treated as inverted. That is, lt = 0; ge = 0 inverted.
748static unsigned getCRIdxForSetCC(unsigned Condition, bool& Inv) {
749 switch (Condition) {
750 default: assert(0 && "Unknown condition!"); abort();
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000751 case ISD::SETULT:
Nate Begeman7bfba7d2005-04-14 09:45:08 +0000752 case ISD::SETLT: Inv = false; return 0;
753 case ISD::SETUGE:
754 case ISD::SETGE: Inv = true; return 0;
755 case ISD::SETUGT:
756 case ISD::SETGT: Inv = false; return 1;
757 case ISD::SETULE:
758 case ISD::SETLE: Inv = true; return 1;
759 case ISD::SETEQ: Inv = false; return 2;
760 case ISD::SETNE: Inv = true; return 2;
761 }
762 return 0;
763}
764
Nate Begeman04730362005-04-01 04:45:11 +0000765/// IndexedOpForOp - Return the indexed variant for each of the PowerPC load
766/// and store immediate instructions.
767static unsigned IndexedOpForOp(unsigned Opcode) {
768 switch(Opcode) {
769 default: assert(0 && "Unknown opcode!"); abort();
770 case PPC::LBZ: return PPC::LBZX; case PPC::STB: return PPC::STBX;
771 case PPC::LHZ: return PPC::LHZX; case PPC::STH: return PPC::STHX;
772 case PPC::LHA: return PPC::LHAX; case PPC::STW: return PPC::STWX;
773 case PPC::LWZ: return PPC::LWZX; case PPC::STFS: return PPC::STFSX;
774 case PPC::LFS: return PPC::LFSX; case PPC::STFD: return PPC::STFDX;
775 case PPC::LFD: return PPC::LFDX;
776 }
777 return 0;
Nate Begeman3e897162005-03-31 23:55:40 +0000778}
Nate Begeman815d6da2005-04-06 00:25:27 +0000779
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000780// Structure used to return the necessary information to codegen an SDIV as
Nate Begeman815d6da2005-04-06 00:25:27 +0000781// a multiply.
782struct ms {
783 int m; // magic number
784 int s; // shift amount
785};
786
787struct mu {
788 unsigned int m; // magic number
789 int a; // add indicator
790 int s; // shift amount
791};
792
793/// magic - calculate the magic numbers required to codegen an integer sdiv as
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000794/// a sequence of multiply and shifts. Requires that the divisor not be 0, 1,
Nate Begeman815d6da2005-04-06 00:25:27 +0000795/// or -1.
796static struct ms magic(int d) {
797 int p;
798 unsigned int ad, anc, delta, q1, r1, q2, r2, t;
799 const unsigned int two31 = 2147483648U; // 2^31
800 struct ms mag;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000801
Nate Begeman815d6da2005-04-06 00:25:27 +0000802 ad = abs(d);
803 t = two31 + ((unsigned int)d >> 31);
804 anc = t - 1 - t%ad; // absolute value of nc
805 p = 31; // initialize p
806 q1 = two31/anc; // initialize q1 = 2p/abs(nc)
807 r1 = two31 - q1*anc; // initialize r1 = rem(2p,abs(nc))
808 q2 = two31/ad; // initialize q2 = 2p/abs(d)
809 r2 = two31 - q2*ad; // initialize r2 = rem(2p,abs(d))
810 do {
811 p = p + 1;
812 q1 = 2*q1; // update q1 = 2p/abs(nc)
813 r1 = 2*r1; // update r1 = rem(2p/abs(nc))
814 if (r1 >= anc) { // must be unsigned comparison
815 q1 = q1 + 1;
816 r1 = r1 - anc;
817 }
818 q2 = 2*q2; // update q2 = 2p/abs(d)
819 r2 = 2*r2; // update r2 = rem(2p/abs(d))
820 if (r2 >= ad) { // must be unsigned comparison
821 q2 = q2 + 1;
822 r2 = r2 - ad;
823 }
824 delta = ad - r2;
825 } while (q1 < delta || (q1 == delta && r1 == 0));
826
827 mag.m = q2 + 1;
828 if (d < 0) mag.m = -mag.m; // resulting magic number
829 mag.s = p - 32; // resulting shift
830 return mag;
831}
832
833/// magicu - calculate the magic numbers required to codegen an integer udiv as
834/// a sequence of multiply, add and shifts. Requires that the divisor not be 0.
835static struct mu magicu(unsigned d)
836{
837 int p;
838 unsigned int nc, delta, q1, r1, q2, r2;
839 struct mu magu;
840 magu.a = 0; // initialize "add" indicator
841 nc = - 1 - (-d)%d;
842 p = 31; // initialize p
843 q1 = 0x80000000/nc; // initialize q1 = 2p/nc
844 r1 = 0x80000000 - q1*nc; // initialize r1 = rem(2p,nc)
845 q2 = 0x7FFFFFFF/d; // initialize q2 = (2p-1)/d
846 r2 = 0x7FFFFFFF - q2*d; // initialize r2 = rem((2p-1),d)
847 do {
848 p = p + 1;
849 if (r1 >= nc - r1 ) {
850 q1 = 2*q1 + 1; // update q1
851 r1 = 2*r1 - nc; // update r1
852 }
853 else {
854 q1 = 2*q1; // update q1
855 r1 = 2*r1; // update r1
856 }
857 if (r2 + 1 >= d - r2) {
858 if (q2 >= 0x7FFFFFFF) magu.a = 1;
859 q2 = 2*q2 + 1; // update q2
860 r2 = 2*r2 + 1 - d; // update r2
861 }
862 else {
863 if (q2 >= 0x80000000) magu.a = 1;
864 q2 = 2*q2; // update q2
865 r2 = 2*r2 + 1; // update r2
866 }
867 delta = d - 1 - r2;
868 } while (p < 64 && (q1 < delta || (q1 == delta && r1 == 0)));
869 magu.m = q2 + 1; // resulting magic number
870 magu.s = p - 32; // resulting shift
871 return magu;
872}
873}
874
875/// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
876/// return a DAG expression to select that will generate the same value by
877/// multiplying by a magic number. See:
878/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
879SDOperand ISel::BuildSDIVSequence(SDOperand N) {
880 int d = (int)cast<ConstantSDNode>(N.getOperand(1))->getSignExtended();
881 ms magics = magic(d);
882 // Multiply the numerator (operand 0) by the magic value
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000883 SDOperand Q = ISelDAG->getNode(ISD::MULHS, MVT::i32, N.getOperand(0),
Nate Begeman815d6da2005-04-06 00:25:27 +0000884 ISelDAG->getConstant(magics.m, MVT::i32));
885 // If d > 0 and m < 0, add the numerator
886 if (d > 0 && magics.m < 0)
887 Q = ISelDAG->getNode(ISD::ADD, MVT::i32, Q, N.getOperand(0));
888 // If d < 0 and m > 0, subtract the numerator.
889 if (d < 0 && magics.m > 0)
890 Q = ISelDAG->getNode(ISD::SUB, MVT::i32, Q, N.getOperand(0));
891 // Shift right algebraic if shift value is nonzero
892 if (magics.s > 0)
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000893 Q = ISelDAG->getNode(ISD::SRA, MVT::i32, Q,
Nate Begeman815d6da2005-04-06 00:25:27 +0000894 ISelDAG->getConstant(magics.s, MVT::i32));
895 // Extract the sign bit and add it to the quotient
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000896 SDOperand T =
Nate Begeman815d6da2005-04-06 00:25:27 +0000897 ISelDAG->getNode(ISD::SRL, MVT::i32, Q, ISelDAG->getConstant(31, MVT::i32));
Nate Begeman27b4c232005-04-06 06:44:57 +0000898 return ISelDAG->getNode(ISD::ADD, MVT::i32, Q, T);
Nate Begeman815d6da2005-04-06 00:25:27 +0000899}
900
901/// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
902/// return a DAG expression to select that will generate the same value by
903/// multiplying by a magic number. See:
904/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
905SDOperand ISel::BuildUDIVSequence(SDOperand N) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000906 unsigned d =
Nate Begeman815d6da2005-04-06 00:25:27 +0000907 (unsigned)cast<ConstantSDNode>(N.getOperand(1))->getSignExtended();
908 mu magics = magicu(d);
909 // Multiply the numerator (operand 0) by the magic value
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000910 SDOperand Q = ISelDAG->getNode(ISD::MULHU, MVT::i32, N.getOperand(0),
Nate Begeman815d6da2005-04-06 00:25:27 +0000911 ISelDAG->getConstant(magics.m, MVT::i32));
912 if (magics.a == 0) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000913 Q = ISelDAG->getNode(ISD::SRL, MVT::i32, Q,
Nate Begeman815d6da2005-04-06 00:25:27 +0000914 ISelDAG->getConstant(magics.s, MVT::i32));
915 } else {
916 SDOperand NPQ = ISelDAG->getNode(ISD::SUB, MVT::i32, N.getOperand(0), Q);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000917 NPQ = ISelDAG->getNode(ISD::SRL, MVT::i32, NPQ,
Nate Begeman815d6da2005-04-06 00:25:27 +0000918 ISelDAG->getConstant(1, MVT::i32));
919 NPQ = ISelDAG->getNode(ISD::ADD, MVT::i32, NPQ, Q);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000920 Q = ISelDAG->getNode(ISD::SRL, MVT::i32, NPQ,
Nate Begeman815d6da2005-04-06 00:25:27 +0000921 ISelDAG->getConstant(magics.s-1, MVT::i32));
922 }
Nate Begeman27b4c232005-04-06 06:44:57 +0000923 return Q;
Nate Begemana9795f82005-03-24 04:41:43 +0000924}
925
Nate Begemanc7b09f12005-03-25 08:34:25 +0000926/// getGlobalBaseReg - Output the instructions required to put the
927/// base address to use for accessing globals into a register.
928///
929unsigned ISel::getGlobalBaseReg() {
930 if (!GlobalBaseInitialized) {
931 // Insert the set of GlobalBaseReg into the first MBB of the function
932 MachineBasicBlock &FirstMBB = BB->getParent()->front();
933 MachineBasicBlock::iterator MBBI = FirstMBB.begin();
934 GlobalBaseReg = MakeReg(MVT::i32);
935 BuildMI(FirstMBB, MBBI, PPC::MovePCtoLR, 0, PPC::LR);
936 BuildMI(FirstMBB, MBBI, PPC::MFLR, 1, GlobalBaseReg).addReg(PPC::LR);
937 GlobalBaseInitialized = true;
938 }
939 return GlobalBaseReg;
940}
941
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000942/// getConstDouble - Loads a floating point value into a register, via the
Nate Begeman6b559972005-04-01 02:59:27 +0000943/// Constant Pool. Optionally takes a register in which to load the value.
944unsigned ISel::getConstDouble(double doubleVal, unsigned Result=0) {
945 unsigned Tmp1 = MakeReg(MVT::i32);
946 if (0 == Result) Result = MakeReg(MVT::f64);
947 MachineConstantPool *CP = BB->getParent()->getConstantPool();
948 ConstantFP *CFP = ConstantFP::get(Type::DoubleTy, doubleVal);
949 unsigned CPI = CP->getConstantPoolIndex(CFP);
950 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp1).addReg(getGlobalBaseReg())
951 .addConstantPoolIndex(CPI);
952 BuildMI(BB, PPC::LFD, 2, Result).addConstantPoolIndex(CPI).addReg(Tmp1);
953 return Result;
954}
955
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000956/// MoveCRtoGPR - Move CCReg[Idx] to the least significant bit of Result. If
Nate Begeman1cbf3ab2005-04-18 07:48:09 +0000957/// Inv is true, then invert the result.
958void ISel::MoveCRtoGPR(unsigned CCReg, bool Inv, unsigned Idx, unsigned Result){
959 unsigned IntCR = MakeReg(MVT::i32);
960 BuildMI(BB, PPC::MCRF, 1, PPC::CR7).addReg(CCReg);
961 BuildMI(BB, PPC::MFCR, 1, IntCR).addReg(PPC::CR7);
962 if (Inv) {
963 unsigned Tmp1 = MakeReg(MVT::i32);
964 BuildMI(BB, PPC::RLWINM, 4, Tmp1).addReg(IntCR).addImm(32-(3-Idx))
965 .addImm(31).addImm(31);
966 BuildMI(BB, PPC::XORI, 2, Result).addReg(Tmp1).addImm(1);
967 } else {
968 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(IntCR).addImm(32-(3-Idx))
969 .addImm(31).addImm(31);
970 }
971}
972
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000973/// SelectBitfieldInsert - turn an or of two masked values into
Nate Begeman7ddecb42005-04-06 23:51:40 +0000974/// the rotate left word immediate then mask insert (rlwimi) instruction.
975/// Returns true on success, false if the caller still needs to select OR.
976///
977/// Patterns matched:
978/// 1. or shl, and 5. or and, and
979/// 2. or and, shl 6. or shl, shr
980/// 3. or shr, and 7. or shr, shl
981/// 4. or and, shr
982bool ISel::SelectBitfieldInsert(SDOperand OR, unsigned Result) {
Nate Begemancd08e4c2005-04-09 20:09:12 +0000983 bool IsRotate = false;
Nate Begeman7ddecb42005-04-06 23:51:40 +0000984 unsigned TgtMask = 0xFFFFFFFF, InsMask = 0xFFFFFFFF, Amount = 0;
985 unsigned Op0Opc = OR.getOperand(0).getOpcode();
986 unsigned Op1Opc = OR.getOperand(1).getOpcode();
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000987
Nate Begeman7ddecb42005-04-06 23:51:40 +0000988 // Verify that we have the correct opcodes
989 if (ISD::SHL != Op0Opc && ISD::SRL != Op0Opc && ISD::AND != Op0Opc)
990 return false;
991 if (ISD::SHL != Op1Opc && ISD::SRL != Op1Opc && ISD::AND != Op1Opc)
992 return false;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000993
Nate Begeman7ddecb42005-04-06 23:51:40 +0000994 // Generate Mask value for Target
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000995 if (ConstantSDNode *CN =
Nate Begeman7ddecb42005-04-06 23:51:40 +0000996 dyn_cast<ConstantSDNode>(OR.getOperand(0).getOperand(1).Val)) {
997 switch(Op0Opc) {
998 case ISD::SHL: TgtMask <<= (unsigned)CN->getValue(); break;
999 case ISD::SRL: TgtMask >>= (unsigned)CN->getValue(); break;
1000 case ISD::AND: TgtMask &= (unsigned)CN->getValue(); break;
1001 }
1002 } else {
1003 return false;
1004 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001005
Nate Begeman7ddecb42005-04-06 23:51:40 +00001006 // Generate Mask value for Insert
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001007 if (ConstantSDNode *CN =
Nate Begeman7ddecb42005-04-06 23:51:40 +00001008 dyn_cast<ConstantSDNode>(OR.getOperand(1).getOperand(1).Val)) {
1009 switch(Op1Opc) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001010 case ISD::SHL:
1011 Amount = CN->getValue();
Nate Begemancd08e4c2005-04-09 20:09:12 +00001012 InsMask <<= Amount;
1013 if (Op0Opc == ISD::SRL) IsRotate = true;
Nate Begeman7ddecb42005-04-06 23:51:40 +00001014 break;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001015 case ISD::SRL:
1016 Amount = CN->getValue();
1017 InsMask >>= Amount;
Nate Begeman7ddecb42005-04-06 23:51:40 +00001018 Amount = 32-Amount;
Nate Begemancd08e4c2005-04-09 20:09:12 +00001019 if (Op0Opc == ISD::SHL) IsRotate = true;
Nate Begeman7ddecb42005-04-06 23:51:40 +00001020 break;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001021 case ISD::AND:
Nate Begeman7ddecb42005-04-06 23:51:40 +00001022 InsMask &= (unsigned)CN->getValue();
1023 break;
1024 }
1025 } else {
1026 return false;
1027 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001028
Nate Begeman7ddecb42005-04-06 23:51:40 +00001029 // Verify that the Target mask and Insert mask together form a full word mask
1030 // and that the Insert mask is a run of set bits (which implies both are runs
1031 // of set bits). Given that, Select the arguments and generate the rlwimi
1032 // instruction.
1033 unsigned MB, ME;
1034 if (((TgtMask ^ InsMask) == 0xFFFFFFFF) && IsRunOfOnes(InsMask, MB, ME)) {
1035 unsigned Tmp1, Tmp2;
Nate Begemancd08e4c2005-04-09 20:09:12 +00001036 // Check for rotlwi / rotrwi here, a special case of bitfield insert
1037 // where both bitfield halves are sourced from the same value.
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001038 if (IsRotate &&
Nate Begemancd08e4c2005-04-09 20:09:12 +00001039 OR.getOperand(0).getOperand(0) == OR.getOperand(1).getOperand(0)) {
Nate Begemancd08e4c2005-04-09 20:09:12 +00001040 Tmp1 = SelectExpr(OR.getOperand(0).getOperand(0));
1041 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(Amount)
1042 .addImm(0).addImm(31);
1043 return true;
1044 }
Nate Begeman7ddecb42005-04-06 23:51:40 +00001045 if (Op0Opc == ISD::AND)
1046 Tmp1 = SelectExpr(OR.getOperand(0).getOperand(0));
1047 else
1048 Tmp1 = SelectExpr(OR.getOperand(0));
1049 Tmp2 = SelectExpr(OR.getOperand(1).getOperand(0));
1050 BuildMI(BB, PPC::RLWIMI, 5, Result).addReg(Tmp1).addReg(Tmp2)
1051 .addImm(Amount).addImm(MB).addImm(ME);
1052 return true;
1053 }
1054 return false;
1055}
1056
Nate Begeman3664cef2005-04-13 22:14:14 +00001057/// FoldIfWideZeroExtend - 32 bit PowerPC implicit masks shift amounts to the
1058/// low six bits. If the shift amount is an ISD::AND node with a mask that is
1059/// wider than the implicit mask, then we can get rid of the AND and let the
1060/// shift do the mask.
1061unsigned ISel::FoldIfWideZeroExtend(SDOperand N) {
1062 unsigned C;
1063 if (N.getOpcode() == ISD::AND &&
1064 5 == getImmediateForOpcode(N.getOperand(1), ISD::AND, C) && // isMask
1065 31 == (C & 0xFFFF) && // ME
1066 26 >= (C >> 16)) // MB
1067 return SelectExpr(N.getOperand(0));
1068 else
1069 return SelectExpr(N);
1070}
1071
Nate Begeman1cbf3ab2005-04-18 07:48:09 +00001072unsigned ISel::SelectCC(SDOperand CC, unsigned& Opc, bool &Inv, unsigned& Idx) {
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001073 unsigned Result, Tmp1, Tmp2;
Nate Begeman9765c252005-04-12 21:22:28 +00001074 bool AlreadySelected = false;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001075 static const unsigned CompareOpcodes[] =
Nate Begemandffcfcc2005-04-01 00:32:34 +00001076 { PPC::FCMPU, PPC::FCMPU, PPC::CMPW, PPC::CMPLW };
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001077
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001078 // Allocate a condition register for this expression
1079 Result = RegMap->createVirtualRegister(PPC32::CRRCRegisterClass);
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001080
Nate Begemandffcfcc2005-04-01 00:32:34 +00001081 // If the first operand to the select is a SETCC node, then we can fold it
1082 // into the branch that selects which value to return.
Nate Begeman16ac7092005-04-18 02:43:24 +00001083 if (SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(CC.Val)) {
Nate Begemandffcfcc2005-04-01 00:32:34 +00001084 bool U;
1085 Opc = getBCCForSetCC(SetCC->getCondition(), U);
Nate Begeman1cbf3ab2005-04-18 07:48:09 +00001086 Idx = getCRIdxForSetCC(SetCC->getCondition(), Inv);
Nate Begemandffcfcc2005-04-01 00:32:34 +00001087
Nate Begeman439b4442005-04-05 04:22:58 +00001088 // Pass the optional argument U to getImmediateForOpcode for SETCC,
Nate Begemandffcfcc2005-04-01 00:32:34 +00001089 // so that it knows whether the SETCC immediate range is signed or not.
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001090 if (1 == getImmediateForOpcode(SetCC->getOperand(1), ISD::SETCC,
Nate Begeman439b4442005-04-05 04:22:58 +00001091 Tmp2, U)) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001092 // For comparisons against zero, we can implicity set CR0 if a recording
Nate Begemanc7bd4822005-04-11 06:34:10 +00001093 // variant (e.g. 'or.' instead of 'or') of the instruction that defines
1094 // operand zero of the SetCC node is available.
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001095 if (0 == Tmp2 &&
Nate Begeman9765c252005-04-12 21:22:28 +00001096 NodeHasRecordingVariant(SetCC->getOperand(0).getOpcode()) &&
1097 SetCC->getOperand(0).Val->hasOneUse()) {
Nate Begemanc7bd4822005-04-11 06:34:10 +00001098 RecordSuccess = false;
1099 Tmp1 = SelectExpr(SetCC->getOperand(0), true);
1100 if (RecordSuccess) {
1101 ++Recorded;
Nate Begeman7bfba7d2005-04-14 09:45:08 +00001102 BuildMI(BB, PPC::MCRF, 1, Result).addReg(PPC::CR0);
1103 return Result;
Nate Begemanc7bd4822005-04-11 06:34:10 +00001104 }
1105 AlreadySelected = true;
1106 }
1107 // If we could not implicitly set CR0, then emit a compare immediate
1108 // instead.
1109 if (!AlreadySelected) Tmp1 = SelectExpr(SetCC->getOperand(0));
Nate Begemandffcfcc2005-04-01 00:32:34 +00001110 if (U)
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001111 BuildMI(BB, PPC::CMPLWI, 2, Result).addReg(Tmp1).addImm(Tmp2);
Nate Begemandffcfcc2005-04-01 00:32:34 +00001112 else
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001113 BuildMI(BB, PPC::CMPWI, 2, Result).addReg(Tmp1).addSImm(Tmp2);
Nate Begemandffcfcc2005-04-01 00:32:34 +00001114 } else {
1115 bool IsInteger = MVT::isInteger(SetCC->getOperand(0).getValueType());
1116 unsigned CompareOpc = CompareOpcodes[2 * IsInteger + U];
Nate Begemanc7bd4822005-04-11 06:34:10 +00001117 Tmp1 = SelectExpr(SetCC->getOperand(0));
Nate Begemandffcfcc2005-04-01 00:32:34 +00001118 Tmp2 = SelectExpr(SetCC->getOperand(1));
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001119 BuildMI(BB, CompareOpc, 2, Result).addReg(Tmp1).addReg(Tmp2);
Nate Begemandffcfcc2005-04-01 00:32:34 +00001120 }
1121 } else {
Nate Begemanf8b02942005-04-15 22:12:16 +00001122 if (PPCCRopts)
Nate Begeman1cbf3ab2005-04-18 07:48:09 +00001123 return SelectCCExpr(CC, Opc, Inv, Idx);
1124 // If this isn't a SetCC, then select the value and compare it against zero,
1125 // treating it as if it were a boolean.
Nate Begeman9765c252005-04-12 21:22:28 +00001126 Opc = PPC::BNE;
Nate Begeman1cbf3ab2005-04-18 07:48:09 +00001127 Idx = getCRIdxForSetCC(ISD::SETNE, Inv);
Nate Begemandffcfcc2005-04-01 00:32:34 +00001128 Tmp1 = SelectExpr(CC);
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001129 BuildMI(BB, PPC::CMPLWI, 2, Result).addReg(Tmp1).addImm(0);
Nate Begemandffcfcc2005-04-01 00:32:34 +00001130 }
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001131 return Result;
Nate Begemandffcfcc2005-04-01 00:32:34 +00001132}
1133
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001134unsigned ISel::SelectCCExpr(SDOperand N, unsigned& Opc, bool &Inv,
Nate Begeman1cbf3ab2005-04-18 07:48:09 +00001135 unsigned &Idx) {
1136 bool Inv0, Inv1;
1137 unsigned Idx0, Idx1, CROpc, Opc1, Tmp1, Tmp2;
1138
1139 // Allocate a condition register for this expression
1140 unsigned Result = RegMap->createVirtualRegister(PPC32::CRRCRegisterClass);
1141
1142 // Check for the operations we support:
1143 switch(N.getOpcode()) {
1144 default:
1145 Opc = PPC::BNE;
1146 Idx = getCRIdxForSetCC(ISD::SETNE, Inv);
1147 Tmp1 = SelectExpr(N);
1148 BuildMI(BB, PPC::CMPLWI, 2, Result).addReg(Tmp1).addImm(0);
1149 break;
1150 case ISD::OR:
1151 case ISD::AND:
1152 ++MultiBranch;
1153 Tmp1 = SelectCCExpr(N.getOperand(0), Opc, Inv0, Idx0);
1154 Tmp2 = SelectCCExpr(N.getOperand(1), Opc1, Inv1, Idx1);
1155 CROpc = getCROpForSetCC(N.getOpcode(), Inv0, Inv1);
1156 if (Inv0 && !Inv1) {
1157 std::swap(Tmp1, Tmp2);
1158 std::swap(Idx0, Idx1);
1159 Opc = Opc1;
1160 }
1161 if (Inv0 && Inv1) Opc = PPC32InstrInfo::invertPPCBranchOpcode(Opc);
1162 BuildMI(BB, CROpc, 5, Result).addImm(Idx0).addReg(Tmp1).addImm(Idx0)
1163 .addReg(Tmp2).addImm(Idx1);
1164 Inv = false;
1165 Idx = Idx0;
1166 break;
1167 case ISD::SETCC:
1168 Tmp1 = SelectCC(N, Opc, Inv, Idx);
1169 Result = Tmp1;
1170 break;
1171 }
1172 return Result;
1173}
1174
Nate Begemandffcfcc2005-04-01 00:32:34 +00001175/// Check to see if the load is a constant offset from a base register
Nate Begeman04730362005-04-01 04:45:11 +00001176bool ISel::SelectAddr(SDOperand N, unsigned& Reg, int& offset)
Nate Begemana9795f82005-03-24 04:41:43 +00001177{
Nate Begeman96fc6812005-03-31 02:05:53 +00001178 unsigned imm = 0, opcode = N.getOpcode();
Nate Begeman04730362005-04-01 04:45:11 +00001179 if (N.getOpcode() == ISD::ADD) {
1180 Reg = SelectExpr(N.getOperand(0));
Nate Begeman439b4442005-04-05 04:22:58 +00001181 if (1 == getImmediateForOpcode(N.getOperand(1), opcode, imm)) {
Nate Begeman96fc6812005-03-31 02:05:53 +00001182 offset = imm;
Nate Begeman04730362005-04-01 04:45:11 +00001183 return false;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001184 }
Nate Begeman04730362005-04-01 04:45:11 +00001185 offset = SelectExpr(N.getOperand(1));
1186 return true;
1187 }
Nate Begemana9795f82005-03-24 04:41:43 +00001188 Reg = SelectExpr(N);
1189 offset = 0;
Nate Begeman04730362005-04-01 04:45:11 +00001190 return false;
Nate Begemana9795f82005-03-24 04:41:43 +00001191}
1192
1193void ISel::SelectBranchCC(SDOperand N)
1194{
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001195 MachineBasicBlock *Dest =
Nate Begemana9795f82005-03-24 04:41:43 +00001196 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
Nate Begeman3e897162005-03-31 23:55:40 +00001197
Nate Begeman1cbf3ab2005-04-18 07:48:09 +00001198 bool Inv;
1199 unsigned Opc, CCReg, Idx;
Nate Begemana9795f82005-03-24 04:41:43 +00001200 Select(N.getOperand(0)); //chain
Nate Begeman1cbf3ab2005-04-18 07:48:09 +00001201 CCReg = SelectCC(N.getOperand(1), Opc, Inv, Idx);
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001202
Nate Begemancd08e4c2005-04-09 20:09:12 +00001203 // Iterate to the next basic block, unless we're already at the end of the
1204 ilist<MachineBasicBlock>::iterator It = BB, E = BB->getParent()->end();
Nate Begeman706471e2005-04-09 23:35:05 +00001205 if (++It == E) It = BB;
Nate Begemancd08e4c2005-04-09 20:09:12 +00001206
1207 // If this is a two way branch, then grab the fallthrough basic block argument
1208 // and build a PowerPC branch pseudo-op, suitable for long branch conversion
1209 // if necessary by the branch selection pass. Otherwise, emit a standard
1210 // conditional branch.
1211 if (N.getOpcode() == ISD::BRCONDTWOWAY) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001212 MachineBasicBlock *Fallthrough =
Nate Begemancd08e4c2005-04-09 20:09:12 +00001213 cast<BasicBlockSDNode>(N.getOperand(3))->getBasicBlock();
1214 if (Dest != It) {
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001215 BuildMI(BB, PPC::COND_BRANCH, 4).addReg(CCReg).addImm(Opc)
Nate Begemancd08e4c2005-04-09 20:09:12 +00001216 .addMBB(Dest).addMBB(Fallthrough);
1217 if (Fallthrough != It)
1218 BuildMI(BB, PPC::B, 1).addMBB(Fallthrough);
1219 } else {
1220 if (Fallthrough != It) {
1221 Opc = PPC32InstrInfo::invertPPCBranchOpcode(Opc);
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001222 BuildMI(BB, PPC::COND_BRANCH, 4).addReg(CCReg).addImm(Opc)
Nate Begemancd08e4c2005-04-09 20:09:12 +00001223 .addMBB(Fallthrough).addMBB(Dest);
1224 }
1225 }
1226 } else {
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001227 BuildMI(BB, PPC::COND_BRANCH, 4).addReg(CCReg).addImm(Opc)
Nate Begeman27499e32005-04-10 01:48:29 +00001228 .addMBB(Dest).addMBB(It);
Nate Begemancd08e4c2005-04-09 20:09:12 +00001229 }
Nate Begemana9795f82005-03-24 04:41:43 +00001230 return;
1231}
1232
1233unsigned ISel::SelectExprFP(SDOperand N, unsigned Result)
1234{
1235 unsigned Tmp1, Tmp2, Tmp3;
1236 unsigned Opc = 0;
1237 SDNode *Node = N.Val;
1238 MVT::ValueType DestType = N.getValueType();
1239 unsigned opcode = N.getOpcode();
1240
1241 switch (opcode) {
1242 default:
1243 Node->dump();
1244 assert(0 && "Node not handled!\n");
1245
Nate Begeman23afcfb2005-03-29 22:48:55 +00001246 case ISD::SELECT: {
Nate Begeman3e897162005-03-31 23:55:40 +00001247 // Attempt to generate FSEL. We can do this whenever we have an FP result,
1248 // and an FP comparison in the SetCC node.
1249 SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(N.getOperand(0).Val);
1250 if (SetCC && N.getOperand(0).getOpcode() == ISD::SETCC &&
1251 !MVT::isInteger(SetCC->getOperand(0).getValueType()) &&
1252 SetCC->getCondition() != ISD::SETEQ &&
1253 SetCC->getCondition() != ISD::SETNE) {
1254 MVT::ValueType VT = SetCC->getOperand(0).getValueType();
Nate Begeman3e897162005-03-31 23:55:40 +00001255 unsigned TV = SelectExpr(N.getOperand(1)); // Use if TRUE
1256 unsigned FV = SelectExpr(N.getOperand(2)); // Use if FALSE
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001257
Nate Begeman3e897162005-03-31 23:55:40 +00001258 ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(1));
1259 if (CN && (CN->isExactlyValue(-0.0) || CN->isExactlyValue(0.0))) {
1260 switch(SetCC->getCondition()) {
1261 default: assert(0 && "Invalid FSEL condition"); abort();
1262 case ISD::SETULT:
1263 case ISD::SETLT:
Nate Begemanaf4ab1b2005-04-09 09:33:07 +00001264 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
Nate Begeman3e897162005-03-31 23:55:40 +00001265 case ISD::SETUGE:
1266 case ISD::SETGE:
Nate Begemanaf4ab1b2005-04-09 09:33:07 +00001267 Tmp1 = SelectExpr(SetCC->getOperand(0)); // Val to compare against
Nate Begeman3e897162005-03-31 23:55:40 +00001268 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp1).addReg(TV).addReg(FV);
1269 return Result;
1270 case ISD::SETUGT:
Nate Begemanaf4ab1b2005-04-09 09:33:07 +00001271 case ISD::SETGT:
1272 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
Nate Begeman3e897162005-03-31 23:55:40 +00001273 case ISD::SETULE:
1274 case ISD::SETLE: {
Nate Begemanaf4ab1b2005-04-09 09:33:07 +00001275 if (SetCC->getOperand(0).getOpcode() == ISD::FNEG) {
1276 Tmp2 = SelectExpr(SetCC->getOperand(0).getOperand(0));
1277 } else {
1278 Tmp2 = MakeReg(VT);
1279 Tmp1 = SelectExpr(SetCC->getOperand(0)); // Val to compare against
1280 BuildMI(BB, PPC::FNEG, 1, Tmp2).addReg(Tmp1);
1281 }
Nate Begeman3e897162005-03-31 23:55:40 +00001282 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp2).addReg(TV).addReg(FV);
1283 return Result;
1284 }
1285 }
1286 } else {
1287 Opc = (MVT::f64 == VT) ? PPC::FSUB : PPC::FSUBS;
Nate Begemanaf4ab1b2005-04-09 09:33:07 +00001288 Tmp1 = SelectExpr(SetCC->getOperand(0)); // Val to compare against
Nate Begeman3e897162005-03-31 23:55:40 +00001289 Tmp2 = SelectExpr(SetCC->getOperand(1));
1290 Tmp3 = MakeReg(VT);
1291 switch(SetCC->getCondition()) {
1292 default: assert(0 && "Invalid FSEL condition"); abort();
1293 case ISD::SETULT:
1294 case ISD::SETLT:
1295 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
1296 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(FV).addReg(TV);
1297 return Result;
1298 case ISD::SETUGE:
1299 case ISD::SETGE:
1300 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
1301 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(TV).addReg(FV);
1302 return Result;
1303 case ISD::SETUGT:
1304 case ISD::SETGT:
1305 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp2).addReg(Tmp1);
1306 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(FV).addReg(TV);
1307 return Result;
1308 case ISD::SETULE:
1309 case ISD::SETLE:
1310 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp2).addReg(Tmp1);
1311 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(TV).addReg(FV);
1312 return Result;
1313 }
1314 }
1315 assert(0 && "Should never get here");
1316 return 0;
1317 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001318
Nate Begeman1cbf3ab2005-04-18 07:48:09 +00001319 bool Inv;
Nate Begeman31318e42005-04-01 07:21:30 +00001320 unsigned TrueValue = SelectExpr(N.getOperand(1)); //Use if TRUE
1321 unsigned FalseValue = SelectExpr(N.getOperand(2)); //Use if FALSE
Nate Begeman1cbf3ab2005-04-18 07:48:09 +00001322 unsigned CCReg = SelectCC(N.getOperand(0), Opc, Inv, Tmp3);
Nate Begeman31318e42005-04-01 07:21:30 +00001323
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001324 // Create an iterator with which to insert the MBB for copying the false
Nate Begeman23afcfb2005-03-29 22:48:55 +00001325 // value and the MBB to hold the PHI instruction for this SetCC.
1326 MachineBasicBlock *thisMBB = BB;
1327 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1328 ilist<MachineBasicBlock>::iterator It = BB;
1329 ++It;
1330
1331 // thisMBB:
1332 // ...
1333 // TrueVal = ...
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001334 // cmpTY ccX, r1, r2
Nate Begeman23afcfb2005-03-29 22:48:55 +00001335 // bCC copy1MBB
1336 // fallthrough --> copy0MBB
Nate Begeman23afcfb2005-03-29 22:48:55 +00001337 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
1338 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001339 BuildMI(BB, Opc, 2).addReg(CCReg).addMBB(sinkMBB);
Nate Begeman23afcfb2005-03-29 22:48:55 +00001340 MachineFunction *F = BB->getParent();
1341 F->getBasicBlockList().insert(It, copy0MBB);
1342 F->getBasicBlockList().insert(It, sinkMBB);
1343 // Update machine-CFG edges
1344 BB->addSuccessor(copy0MBB);
1345 BB->addSuccessor(sinkMBB);
1346
1347 // copy0MBB:
1348 // %FalseValue = ...
1349 // # fallthrough to sinkMBB
1350 BB = copy0MBB;
Nate Begeman23afcfb2005-03-29 22:48:55 +00001351 // Update machine-CFG edges
1352 BB->addSuccessor(sinkMBB);
1353
1354 // sinkMBB:
1355 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1356 // ...
1357 BB = sinkMBB;
1358 BuildMI(BB, PPC::PHI, 4, Result).addReg(FalseValue)
1359 .addMBB(copy0MBB).addReg(TrueValue).addMBB(thisMBB);
1360 return Result;
1361 }
Nate Begeman27eeb002005-04-02 05:59:34 +00001362
1363 case ISD::FNEG:
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001364 if (!NoExcessFPPrecision &&
Nate Begeman93075ec2005-04-04 23:40:36 +00001365 ISD::ADD == N.getOperand(0).getOpcode() &&
1366 N.getOperand(0).Val->hasOneUse() &&
1367 ISD::MUL == N.getOperand(0).getOperand(0).getOpcode() &&
1368 N.getOperand(0).getOperand(0).Val->hasOneUse()) {
Nate Begeman80196b12005-04-05 00:15:08 +00001369 ++FusedFP; // Statistic
Nate Begeman93075ec2005-04-04 23:40:36 +00001370 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(0));
1371 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(1));
1372 Tmp3 = SelectExpr(N.getOperand(0).getOperand(1));
1373 Opc = DestType == MVT::f64 ? PPC::FNMADD : PPC::FNMADDS;
1374 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001375 } else if (!NoExcessFPPrecision &&
Nate Begemane88aa5b2005-04-09 03:05:51 +00001376 ISD::ADD == N.getOperand(0).getOpcode() &&
Nate Begeman93075ec2005-04-04 23:40:36 +00001377 N.getOperand(0).Val->hasOneUse() &&
Nate Begemane88aa5b2005-04-09 03:05:51 +00001378 ISD::MUL == N.getOperand(0).getOperand(1).getOpcode() &&
1379 N.getOperand(0).getOperand(1).Val->hasOneUse()) {
Nate Begeman80196b12005-04-05 00:15:08 +00001380 ++FusedFP; // Statistic
Nate Begemane88aa5b2005-04-09 03:05:51 +00001381 Tmp1 = SelectExpr(N.getOperand(0).getOperand(1).getOperand(0));
1382 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1).getOperand(1));
1383 Tmp3 = SelectExpr(N.getOperand(0).getOperand(0));
1384 Opc = DestType == MVT::f64 ? PPC::FNMADD : PPC::FNMADDS;
Nate Begeman93075ec2005-04-04 23:40:36 +00001385 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1386 } else if (ISD::FABS == N.getOperand(0).getOpcode()) {
Nate Begeman27eeb002005-04-02 05:59:34 +00001387 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1388 BuildMI(BB, PPC::FNABS, 1, Result).addReg(Tmp1);
1389 } else {
1390 Tmp1 = SelectExpr(N.getOperand(0));
1391 BuildMI(BB, PPC::FNEG, 1, Result).addReg(Tmp1);
1392 }
1393 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001394
Nate Begeman27eeb002005-04-02 05:59:34 +00001395 case ISD::FABS:
1396 Tmp1 = SelectExpr(N.getOperand(0));
1397 BuildMI(BB, PPC::FABS, 1, Result).addReg(Tmp1);
1398 return Result;
1399
Nate Begemana9795f82005-03-24 04:41:43 +00001400 case ISD::FP_ROUND:
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001401 assert (DestType == MVT::f32 &&
1402 N.getOperand(0).getValueType() == MVT::f64 &&
Nate Begemana9795f82005-03-24 04:41:43 +00001403 "only f64 to f32 conversion supported here");
1404 Tmp1 = SelectExpr(N.getOperand(0));
1405 BuildMI(BB, PPC::FRSP, 1, Result).addReg(Tmp1);
1406 return Result;
1407
1408 case ISD::FP_EXTEND:
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001409 assert (DestType == MVT::f64 &&
1410 N.getOperand(0).getValueType() == MVT::f32 &&
Nate Begemana9795f82005-03-24 04:41:43 +00001411 "only f32 to f64 conversion supported here");
1412 Tmp1 = SelectExpr(N.getOperand(0));
1413 BuildMI(BB, PPC::FMR, 1, Result).addReg(Tmp1);
1414 return Result;
1415
1416 case ISD::CopyFromReg:
Nate Begemanf2622612005-03-26 02:17:46 +00001417 if (Result == 1)
1418 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1419 Tmp1 = dyn_cast<RegSDNode>(Node)->getReg();
1420 BuildMI(BB, PPC::FMR, 1, Result).addReg(Tmp1);
1421 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001422
Nate Begeman6d369cc2005-04-01 01:08:07 +00001423 case ISD::ConstantFP: {
Nate Begeman6d369cc2005-04-01 01:08:07 +00001424 ConstantFPSDNode *CN = cast<ConstantFPSDNode>(N);
Nate Begeman6b559972005-04-01 02:59:27 +00001425 Result = getConstDouble(CN->getValue(), Result);
Nate Begeman6d369cc2005-04-01 01:08:07 +00001426 return Result;
1427 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001428
Nate Begemana9795f82005-03-24 04:41:43 +00001429 case ISD::ADD:
Nate Begeman93075ec2005-04-04 23:40:36 +00001430 if (!NoExcessFPPrecision && N.getOperand(0).getOpcode() == ISD::MUL &&
1431 N.getOperand(0).Val->hasOneUse()) {
1432 ++FusedFP; // Statistic
1433 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1434 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1435 Tmp3 = SelectExpr(N.getOperand(1));
1436 Opc = DestType == MVT::f64 ? PPC::FMADD : PPC::FMADDS;
1437 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1438 return Result;
1439 }
Nate Begemane88aa5b2005-04-09 03:05:51 +00001440 if (!NoExcessFPPrecision && N.getOperand(1).getOpcode() == ISD::MUL &&
1441 N.getOperand(1).Val->hasOneUse()) {
1442 ++FusedFP; // Statistic
1443 Tmp1 = SelectExpr(N.getOperand(1).getOperand(0));
1444 Tmp2 = SelectExpr(N.getOperand(1).getOperand(1));
1445 Tmp3 = SelectExpr(N.getOperand(0));
1446 Opc = DestType == MVT::f64 ? PPC::FMADD : PPC::FMADDS;
1447 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1448 return Result;
1449 }
Nate Begeman93075ec2005-04-04 23:40:36 +00001450 Opc = DestType == MVT::f64 ? PPC::FADD : PPC::FADDS;
1451 Tmp1 = SelectExpr(N.getOperand(0));
1452 Tmp2 = SelectExpr(N.getOperand(1));
1453 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1454 return Result;
1455
Nate Begemana9795f82005-03-24 04:41:43 +00001456 case ISD::SUB:
Nate Begeman93075ec2005-04-04 23:40:36 +00001457 if (!NoExcessFPPrecision && N.getOperand(0).getOpcode() == ISD::MUL &&
1458 N.getOperand(0).Val->hasOneUse()) {
1459 ++FusedFP; // Statistic
1460 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1461 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1462 Tmp3 = SelectExpr(N.getOperand(1));
1463 Opc = DestType == MVT::f64 ? PPC::FMSUB : PPC::FMSUBS;
1464 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1465 return Result;
1466 }
Nate Begemane88aa5b2005-04-09 03:05:51 +00001467 if (!NoExcessFPPrecision && N.getOperand(1).getOpcode() == ISD::MUL &&
1468 N.getOperand(1).Val->hasOneUse()) {
1469 ++FusedFP; // Statistic
1470 Tmp1 = SelectExpr(N.getOperand(1).getOperand(0));
1471 Tmp2 = SelectExpr(N.getOperand(1).getOperand(1));
1472 Tmp3 = SelectExpr(N.getOperand(0));
1473 Opc = DestType == MVT::f64 ? PPC::FNMSUB : PPC::FNMSUBS;
1474 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1475 return Result;
1476 }
Nate Begeman93075ec2005-04-04 23:40:36 +00001477 Opc = DestType == MVT::f64 ? PPC::FSUB : PPC::FSUBS;
1478 Tmp1 = SelectExpr(N.getOperand(0));
1479 Tmp2 = SelectExpr(N.getOperand(1));
1480 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1481 return Result;
1482
1483 case ISD::MUL:
Nate Begemana9795f82005-03-24 04:41:43 +00001484 case ISD::SDIV:
1485 switch( opcode ) {
1486 case ISD::MUL: Opc = DestType == MVT::f64 ? PPC::FMUL : PPC::FMULS; break;
Nate Begemana9795f82005-03-24 04:41:43 +00001487 case ISD::SDIV: Opc = DestType == MVT::f64 ? PPC::FDIV : PPC::FDIVS; break;
1488 };
Nate Begemana9795f82005-03-24 04:41:43 +00001489 Tmp1 = SelectExpr(N.getOperand(0));
1490 Tmp2 = SelectExpr(N.getOperand(1));
1491 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1492 return Result;
1493
Nate Begemana9795f82005-03-24 04:41:43 +00001494 case ISD::UINT_TO_FP:
Nate Begemanfdcf3412005-03-30 19:38:35 +00001495 case ISD::SINT_TO_FP: {
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001496 assert (N.getOperand(0).getValueType() == MVT::i32
Nate Begemanfdcf3412005-03-30 19:38:35 +00001497 && "int to float must operate on i32");
1498 bool IsUnsigned = (ISD::UINT_TO_FP == opcode);
1499 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
1500 Tmp2 = MakeReg(MVT::f64); // temp reg to load the integer value into
1501 Tmp3 = MakeReg(MVT::i32); // temp reg to hold the conversion constant
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001502
Nate Begemanfdcf3412005-03-30 19:38:35 +00001503 int FrameIdx = BB->getParent()->getFrameInfo()->CreateStackObject(8, 8);
1504 MachineConstantPool *CP = BB->getParent()->getConstantPool();
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001505
Nate Begemanfdcf3412005-03-30 19:38:35 +00001506 if (IsUnsigned) {
Nate Begeman709c8062005-04-10 06:06:10 +00001507 unsigned ConstF = getConstDouble(0x1.000000p52);
Nate Begemanfdcf3412005-03-30 19:38:35 +00001508 // Store the hi & low halves of the fp value, currently in int regs
1509 BuildMI(BB, PPC::LIS, 1, Tmp3).addSImm(0x4330);
1510 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(Tmp3), FrameIdx);
1511 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(Tmp1), FrameIdx, 4);
1512 addFrameReference(BuildMI(BB, PPC::LFD, 2, Tmp2), FrameIdx);
1513 // Generate the return value with a subtract
1514 BuildMI(BB, PPC::FSUB, 2, Result).addReg(Tmp2).addReg(ConstF);
1515 } else {
Nate Begeman709c8062005-04-10 06:06:10 +00001516 unsigned ConstF = getConstDouble(0x1.000008p52);
Nate Begemanfdcf3412005-03-30 19:38:35 +00001517 unsigned TmpL = MakeReg(MVT::i32);
Nate Begemanfdcf3412005-03-30 19:38:35 +00001518 // Store the hi & low halves of the fp value, currently in int regs
1519 BuildMI(BB, PPC::LIS, 1, Tmp3).addSImm(0x4330);
1520 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(Tmp3), FrameIdx);
1521 BuildMI(BB, PPC::XORIS, 2, TmpL).addReg(Tmp1).addImm(0x8000);
1522 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(TmpL), FrameIdx, 4);
1523 addFrameReference(BuildMI(BB, PPC::LFD, 2, Tmp2), FrameIdx);
1524 // Generate the return value with a subtract
1525 BuildMI(BB, PPC::FSUB, 2, Result).addReg(Tmp2).addReg(ConstF);
1526 }
1527 return Result;
1528 }
Nate Begemana9795f82005-03-24 04:41:43 +00001529 }
Nate Begeman6b559972005-04-01 02:59:27 +00001530 assert(0 && "Should never get here");
Nate Begemana9795f82005-03-24 04:41:43 +00001531 return 0;
1532}
1533
Nate Begemanc7bd4822005-04-11 06:34:10 +00001534unsigned ISel::SelectExpr(SDOperand N, bool Recording) {
Nate Begemana9795f82005-03-24 04:41:43 +00001535 unsigned Result;
1536 unsigned Tmp1, Tmp2, Tmp3;
1537 unsigned Opc = 0;
1538 unsigned opcode = N.getOpcode();
1539
1540 SDNode *Node = N.Val;
1541 MVT::ValueType DestType = N.getValueType();
1542
1543 unsigned &Reg = ExprMap[N];
1544 if (Reg) return Reg;
1545
Nate Begeman27eeb002005-04-02 05:59:34 +00001546 switch (N.getOpcode()) {
1547 default:
Nate Begemana9795f82005-03-24 04:41:43 +00001548 Reg = Result = (N.getValueType() != MVT::Other) ?
Nate Begeman27eeb002005-04-02 05:59:34 +00001549 MakeReg(N.getValueType()) : 1;
1550 break;
Chris Lattnerb5d8e6e2005-05-13 20:29:26 +00001551 case ISD::TAILCALL:
Nate Begeman27eeb002005-04-02 05:59:34 +00001552 case ISD::CALL:
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001553 // If this is a call instruction, make sure to prepare ALL of the result
1554 // values as well as the chain.
Nate Begeman27eeb002005-04-02 05:59:34 +00001555 if (Node->getNumValues() == 1)
1556 Reg = Result = 1; // Void call, just a chain.
1557 else {
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001558 Result = MakeReg(Node->getValueType(0));
1559 ExprMap[N.getValue(0)] = Result;
Nate Begeman27eeb002005-04-02 05:59:34 +00001560 for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001561 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
Nate Begeman27eeb002005-04-02 05:59:34 +00001562 ExprMap[SDOperand(Node, Node->getNumValues()-1)] = 1;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001563 }
Nate Begeman27eeb002005-04-02 05:59:34 +00001564 break;
1565 case ISD::ADD_PARTS:
1566 case ISD::SUB_PARTS:
1567 case ISD::SHL_PARTS:
1568 case ISD::SRL_PARTS:
1569 case ISD::SRA_PARTS:
1570 Result = MakeReg(Node->getValueType(0));
1571 ExprMap[N.getValue(0)] = Result;
1572 for (unsigned i = 1, e = N.Val->getNumValues(); i != e; ++i)
1573 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
1574 break;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001575 }
1576
Nate Begemane5846682005-04-04 06:52:38 +00001577 if (ISD::CopyFromReg == opcode)
1578 DestType = N.getValue(0).getValueType();
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001579
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001580 if (DestType == MVT::f64 || DestType == MVT::f32)
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001581 if (ISD::LOAD != opcode && ISD::EXTLOAD != opcode &&
Chris Lattnerb5d8e6e2005-05-13 20:29:26 +00001582 ISD::UNDEF != opcode && ISD::CALL != opcode && ISD::TAILCALL != opcode)
Nate Begeman74d73452005-03-31 00:15:26 +00001583 return SelectExprFP(N, Result);
Nate Begemana9795f82005-03-24 04:41:43 +00001584
1585 switch (opcode) {
1586 default:
1587 Node->dump();
1588 assert(0 && "Node not handled!\n");
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001589 case ISD::UNDEF:
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001590 BuildMI(BB, PPC::IMPLICIT_DEF, 0, Result);
1591 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001592 case ISD::DYNAMIC_STACKALLOC:
Nate Begeman5e966612005-03-24 06:28:42 +00001593 // Generate both result values. FIXME: Need a better commment here?
1594 if (Result != 1)
1595 ExprMap[N.getValue(1)] = 1;
1596 else
1597 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1598
1599 // FIXME: We are currently ignoring the requested alignment for handling
1600 // greater than the stack alignment. This will need to be revisited at some
1601 // point. Align = N.getOperand(2);
1602 if (!isa<ConstantSDNode>(N.getOperand(2)) ||
1603 cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) {
1604 std::cerr << "Cannot allocate stack object with greater alignment than"
1605 << " the stack alignment yet!";
1606 abort();
1607 }
1608 Select(N.getOperand(0));
1609 Tmp1 = SelectExpr(N.getOperand(1));
1610 // Subtract size from stack pointer, thereby allocating some space.
1611 BuildMI(BB, PPC::SUBF, 2, PPC::R1).addReg(Tmp1).addReg(PPC::R1);
1612 // Put a pointer to the space into the result register by copying the SP
1613 BuildMI(BB, PPC::OR, 2, Result).addReg(PPC::R1).addReg(PPC::R1);
1614 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001615
1616 case ISD::ConstantPool:
Nate Begemanca12a2b2005-03-28 22:28:37 +00001617 Tmp1 = cast<ConstantPoolSDNode>(N)->getIndex();
1618 Tmp2 = MakeReg(MVT::i32);
1619 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp2).addReg(getGlobalBaseReg())
1620 .addConstantPoolIndex(Tmp1);
1621 BuildMI(BB, PPC::LA, 2, Result).addReg(Tmp2).addConstantPoolIndex(Tmp1);
1622 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001623
1624 case ISD::FrameIndex:
Nate Begemanf3d08f32005-03-29 00:03:27 +00001625 Tmp1 = cast<FrameIndexSDNode>(N)->getIndex();
Nate Begeman58f718c2005-03-30 02:23:08 +00001626 addFrameReference(BuildMI(BB, PPC::ADDI, 2, Result), (int)Tmp1, 0, false);
Nate Begemanf3d08f32005-03-29 00:03:27 +00001627 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001628
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001629 case ISD::GlobalAddress: {
1630 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
Nate Begemanca12a2b2005-03-28 22:28:37 +00001631 Tmp1 = MakeReg(MVT::i32);
Nate Begemanc7b09f12005-03-25 08:34:25 +00001632 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp1).addReg(getGlobalBaseReg())
1633 .addGlobalAddress(GV);
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001634 if (GV->hasWeakLinkage() || GV->isExternal()) {
1635 BuildMI(BB, PPC::LWZ, 2, Result).addGlobalAddress(GV).addReg(Tmp1);
1636 } else {
1637 BuildMI(BB, PPC::LA, 2, Result).addReg(Tmp1).addGlobalAddress(GV);
1638 }
1639 return Result;
1640 }
1641
Nate Begeman5e966612005-03-24 06:28:42 +00001642 case ISD::LOAD:
Nate Begemana9795f82005-03-24 04:41:43 +00001643 case ISD::EXTLOAD:
1644 case ISD::ZEXTLOAD:
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001645 case ISD::SEXTLOAD: {
Nate Begeman9db505c2005-03-28 19:36:43 +00001646 MVT::ValueType TypeBeingLoaded = (ISD::LOAD == opcode) ?
1647 Node->getValueType(0) : cast<MVTSDNode>(Node)->getExtraValueType();
Nate Begeman74d73452005-03-31 00:15:26 +00001648 bool sext = (ISD::SEXTLOAD == opcode);
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001649
Nate Begeman5e966612005-03-24 06:28:42 +00001650 // Make sure we generate both values.
1651 if (Result != 1)
1652 ExprMap[N.getValue(1)] = 1; // Generate the token
1653 else
1654 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1655
1656 SDOperand Chain = N.getOperand(0);
1657 SDOperand Address = N.getOperand(1);
1658 Select(Chain);
1659
Nate Begeman9db505c2005-03-28 19:36:43 +00001660 switch (TypeBeingLoaded) {
Nate Begeman74d73452005-03-31 00:15:26 +00001661 default: Node->dump(); assert(0 && "Cannot load this type!");
Nate Begeman9db505c2005-03-28 19:36:43 +00001662 case MVT::i1: Opc = PPC::LBZ; break;
1663 case MVT::i8: Opc = PPC::LBZ; break;
1664 case MVT::i16: Opc = sext ? PPC::LHA : PPC::LHZ; break;
1665 case MVT::i32: Opc = PPC::LWZ; break;
Nate Begeman74d73452005-03-31 00:15:26 +00001666 case MVT::f32: Opc = PPC::LFS; break;
1667 case MVT::f64: Opc = PPC::LFD; break;
Nate Begeman5e966612005-03-24 06:28:42 +00001668 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001669
Nate Begeman74d73452005-03-31 00:15:26 +00001670 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Address)) {
1671 Tmp1 = MakeReg(MVT::i32);
1672 int CPI = CP->getIndex();
1673 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp1).addReg(getGlobalBaseReg())
1674 .addConstantPoolIndex(CPI);
1675 BuildMI(BB, Opc, 2, Result).addConstantPoolIndex(CPI).addReg(Tmp1);
Nate Begeman9db505c2005-03-28 19:36:43 +00001676 }
Nate Begeman74d73452005-03-31 00:15:26 +00001677 else if(Address.getOpcode() == ISD::FrameIndex) {
Nate Begeman58f718c2005-03-30 02:23:08 +00001678 Tmp1 = cast<FrameIndexSDNode>(Address)->getIndex();
1679 addFrameReference(BuildMI(BB, Opc, 2, Result), (int)Tmp1);
Nate Begeman5e966612005-03-24 06:28:42 +00001680 } else {
1681 int offset;
Nate Begeman04730362005-04-01 04:45:11 +00001682 bool idx = SelectAddr(Address, Tmp1, offset);
1683 if (idx) {
1684 Opc = IndexedOpForOp(Opc);
1685 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(offset);
1686 } else {
1687 BuildMI(BB, Opc, 2, Result).addSImm(offset).addReg(Tmp1);
1688 }
Nate Begeman5e966612005-03-24 06:28:42 +00001689 }
1690 return Result;
1691 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001692
Chris Lattnerb5d8e6e2005-05-13 20:29:26 +00001693 case ISD::TAILCALL:
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001694 case ISD::CALL: {
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001695 unsigned GPR_idx = 0, FPR_idx = 0;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001696 static const unsigned GPR[] = {
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001697 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
1698 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
1699 };
1700 static const unsigned FPR[] = {
1701 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
1702 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
1703 };
1704
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001705 // Lower the chain for this call.
1706 Select(N.getOperand(0));
1707 ExprMap[N.getValue(Node->getNumValues()-1)] = 1;
Nate Begeman74d73452005-03-31 00:15:26 +00001708
Nate Begemand860aa62005-04-04 22:17:48 +00001709 MachineInstr *CallMI;
1710 // Emit the correct call instruction based on the type of symbol called.
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001711 if (GlobalAddressSDNode *GASD =
Nate Begemand860aa62005-04-04 22:17:48 +00001712 dyn_cast<GlobalAddressSDNode>(N.getOperand(1))) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001713 CallMI = BuildMI(PPC::CALLpcrel, 1).addGlobalAddress(GASD->getGlobal(),
Nate Begemand860aa62005-04-04 22:17:48 +00001714 true);
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001715 } else if (ExternalSymbolSDNode *ESSDN =
Nate Begemand860aa62005-04-04 22:17:48 +00001716 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1))) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001717 CallMI = BuildMI(PPC::CALLpcrel, 1).addExternalSymbol(ESSDN->getSymbol(),
Nate Begemand860aa62005-04-04 22:17:48 +00001718 true);
1719 } else {
1720 Tmp1 = SelectExpr(N.getOperand(1));
1721 BuildMI(BB, PPC::OR, 2, PPC::R12).addReg(Tmp1).addReg(Tmp1);
1722 BuildMI(BB, PPC::MTCTR, 1).addReg(PPC::R12);
1723 CallMI = BuildMI(PPC::CALLindirect, 3).addImm(20).addImm(0)
1724 .addReg(PPC::R12);
1725 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001726
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001727 // Load the register args to virtual regs
1728 std::vector<unsigned> ArgVR;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001729 for(int i = 2, e = Node->getNumOperands(); i < e; ++i)
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001730 ArgVR.push_back(SelectExpr(N.getOperand(i)));
1731
1732 // Copy the virtual registers into the appropriate argument register
1733 for(int i = 0, e = ArgVR.size(); i < e; ++i) {
1734 switch(N.getOperand(i+2).getValueType()) {
1735 default: Node->dump(); assert(0 && "Unknown value type for call");
1736 case MVT::i1:
1737 case MVT::i8:
1738 case MVT::i16:
1739 case MVT::i32:
1740 assert(GPR_idx < 8 && "Too many int args");
Nate Begemand860aa62005-04-04 22:17:48 +00001741 if (N.getOperand(i+2).getOpcode() != ISD::UNDEF) {
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001742 BuildMI(BB, PPC::OR,2,GPR[GPR_idx]).addReg(ArgVR[i]).addReg(ArgVR[i]);
Nate Begemand860aa62005-04-04 22:17:48 +00001743 CallMI->addRegOperand(GPR[GPR_idx], MachineOperand::Use);
1744 }
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001745 ++GPR_idx;
1746 break;
1747 case MVT::f64:
1748 case MVT::f32:
1749 assert(FPR_idx < 13 && "Too many fp args");
1750 BuildMI(BB, PPC::FMR, 1, FPR[FPR_idx]).addReg(ArgVR[i]);
Nate Begemand860aa62005-04-04 22:17:48 +00001751 CallMI->addRegOperand(FPR[FPR_idx], MachineOperand::Use);
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001752 ++FPR_idx;
1753 break;
1754 }
1755 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001756
Nate Begemand860aa62005-04-04 22:17:48 +00001757 // Put the call instruction in the correct place in the MachineBasicBlock
1758 BB->push_back(CallMI);
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001759
1760 switch (Node->getValueType(0)) {
1761 default: assert(0 && "Unknown value type for call result!");
1762 case MVT::Other: return 1;
1763 case MVT::i1:
1764 case MVT::i8:
1765 case MVT::i16:
1766 case MVT::i32:
Nate Begemane5846682005-04-04 06:52:38 +00001767 if (Node->getValueType(1) == MVT::i32) {
1768 BuildMI(BB, PPC::OR, 2, Result+1).addReg(PPC::R3).addReg(PPC::R3);
1769 BuildMI(BB, PPC::OR, 2, Result).addReg(PPC::R4).addReg(PPC::R4);
1770 } else {
1771 BuildMI(BB, PPC::OR, 2, Result).addReg(PPC::R3).addReg(PPC::R3);
1772 }
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001773 break;
1774 case MVT::f32:
1775 case MVT::f64:
1776 BuildMI(BB, PPC::FMR, 1, Result).addReg(PPC::F1);
1777 break;
1778 }
1779 return Result+N.ResNo;
1780 }
Nate Begemana9795f82005-03-24 04:41:43 +00001781
1782 case ISD::SIGN_EXTEND:
1783 case ISD::SIGN_EXTEND_INREG:
1784 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman9db505c2005-03-28 19:36:43 +00001785 switch(cast<MVTSDNode>(Node)->getExtraValueType()) {
1786 default: Node->dump(); assert(0 && "Unhandled SIGN_EXTEND type"); break;
Nate Begemanc7bd4822005-04-11 06:34:10 +00001787 case MVT::i16:
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001788 BuildMI(BB, PPC::EXTSH, 1, Result).addReg(Tmp1);
Nate Begeman9db505c2005-03-28 19:36:43 +00001789 break;
Nate Begemanc7bd4822005-04-11 06:34:10 +00001790 case MVT::i8:
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001791 BuildMI(BB, PPC::EXTSB, 1, Result).addReg(Tmp1);
Nate Begeman9db505c2005-03-28 19:36:43 +00001792 break;
Nate Begeman74747862005-03-29 22:24:51 +00001793 case MVT::i1:
1794 BuildMI(BB, PPC::SUBFIC, 2, Result).addReg(Tmp1).addSImm(0);
1795 break;
Nate Begeman9db505c2005-03-28 19:36:43 +00001796 }
Nate Begemana9795f82005-03-24 04:41:43 +00001797 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001798
Nate Begemana9795f82005-03-24 04:41:43 +00001799 case ISD::CopyFromReg:
1800 if (Result == 1)
1801 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1802 Tmp1 = dyn_cast<RegSDNode>(Node)->getReg();
1803 BuildMI(BB, PPC::OR, 2, Result).addReg(Tmp1).addReg(Tmp1);
1804 return Result;
1805
1806 case ISD::SHL:
Nate Begeman5e966612005-03-24 06:28:42 +00001807 Tmp1 = SelectExpr(N.getOperand(0));
1808 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1809 Tmp2 = CN->getValue() & 0x1F;
Nate Begeman33162522005-03-29 21:54:38 +00001810 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(Tmp2).addImm(0)
Nate Begeman5e966612005-03-24 06:28:42 +00001811 .addImm(31-Tmp2);
1812 } else {
Nate Begeman3664cef2005-04-13 22:14:14 +00001813 Tmp2 = FoldIfWideZeroExtend(N.getOperand(1));
Nate Begeman5e966612005-03-24 06:28:42 +00001814 BuildMI(BB, PPC::SLW, 2, Result).addReg(Tmp1).addReg(Tmp2);
1815 }
1816 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001817
Nate Begeman5e966612005-03-24 06:28:42 +00001818 case ISD::SRL:
1819 Tmp1 = SelectExpr(N.getOperand(0));
1820 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1821 Tmp2 = CN->getValue() & 0x1F;
Nate Begeman33162522005-03-29 21:54:38 +00001822 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(32-Tmp2)
Nate Begeman5e966612005-03-24 06:28:42 +00001823 .addImm(Tmp2).addImm(31);
1824 } else {
Nate Begeman3664cef2005-04-13 22:14:14 +00001825 Tmp2 = FoldIfWideZeroExtend(N.getOperand(1));
Nate Begeman5e966612005-03-24 06:28:42 +00001826 BuildMI(BB, PPC::SRW, 2, Result).addReg(Tmp1).addReg(Tmp2);
1827 }
1828 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001829
Nate Begeman5e966612005-03-24 06:28:42 +00001830 case ISD::SRA:
1831 Tmp1 = SelectExpr(N.getOperand(0));
1832 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1833 Tmp2 = CN->getValue() & 0x1F;
1834 BuildMI(BB, PPC::SRAWI, 2, Result).addReg(Tmp1).addImm(Tmp2);
1835 } else {
Nate Begeman3664cef2005-04-13 22:14:14 +00001836 Tmp2 = FoldIfWideZeroExtend(N.getOperand(1));
Nate Begeman5e966612005-03-24 06:28:42 +00001837 BuildMI(BB, PPC::SRAW, 2, Result).addReg(Tmp1).addReg(Tmp2);
1838 }
1839 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001840
Nate Begemand7c4a4a2005-05-11 23:43:56 +00001841 case ISD::CTLZ:
1842 Tmp1 = SelectExpr(N.getOperand(0));
1843 BuildMI(BB, PPC::CNTLZW, 1, Result).addReg(Tmp1);
1844 return Result;
1845
Nate Begemana9795f82005-03-24 04:41:43 +00001846 case ISD::ADD:
1847 assert (DestType == MVT::i32 && "Only do arithmetic on i32s!");
1848 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman439b4442005-04-05 04:22:58 +00001849 switch(getImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
Nate Begemana9795f82005-03-24 04:41:43 +00001850 default: assert(0 && "unhandled result code");
1851 case 0: // No immediate
1852 Tmp2 = SelectExpr(N.getOperand(1));
1853 BuildMI(BB, PPC::ADD, 2, Result).addReg(Tmp1).addReg(Tmp2);
1854 break;
1855 case 1: // Low immediate
1856 BuildMI(BB, PPC::ADDI, 2, Result).addReg(Tmp1).addSImm(Tmp2);
1857 break;
1858 case 2: // Shifted immediate
1859 BuildMI(BB, PPC::ADDIS, 2, Result).addReg(Tmp1).addSImm(Tmp2);
1860 break;
1861 }
1862 return Result;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001863
Nate Begemana9795f82005-03-24 04:41:43 +00001864 case ISD::AND:
Nate Begeman1cbf3ab2005-04-18 07:48:09 +00001865 if (PPCCRopts) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001866 if (N.getOperand(0).getOpcode() == ISD::SETCC ||
Nate Begeman1cbf3ab2005-04-18 07:48:09 +00001867 N.getOperand(1).getOpcode() == ISD::SETCC) {
1868 bool Inv;
1869 Tmp1 = SelectCCExpr(N, Opc, Inv, Tmp2);
1870 MoveCRtoGPR(Tmp1, Inv, Tmp2, Result);
1871 return Result;
1872 }
1873 }
Nate Begeman7ddecb42005-04-06 23:51:40 +00001874 // FIXME: should add check in getImmediateForOpcode to return a value
1875 // indicating the immediate is a run of set bits so we can emit a bitfield
1876 // clear with RLWINM instead.
1877 switch(getImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
1878 default: assert(0 && "unhandled result code");
1879 case 0: // No immediate
Nate Begemand7c4a4a2005-05-11 23:43:56 +00001880 // Check for andc: and, (xor a, -1), b
1881 if (N.getOperand(0).getOpcode() == ISD::XOR &&
1882 N.getOperand(0).getOperand(1).getOpcode() == ISD::Constant &&
1883 cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->isAllOnesValue()) {
1884 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1885 Tmp2 = SelectExpr(N.getOperand(1));
1886 BuildMI(BB, PPC::ANDC, 2, Result).addReg(Tmp2).addReg(Tmp1);
1887 return Result;
1888 }
1889 // It wasn't and-with-complement, emit a regular and
Chris Lattnercafb67b2005-05-09 17:39:48 +00001890 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman7ddecb42005-04-06 23:51:40 +00001891 Tmp2 = SelectExpr(N.getOperand(1));
Nate Begemanc7bd4822005-04-11 06:34:10 +00001892 Opc = Recording ? PPC::ANDo : PPC::AND;
1893 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
Nate Begeman7ddecb42005-04-06 23:51:40 +00001894 break;
1895 case 1: // Low immediate
Chris Lattnercafb67b2005-05-09 17:39:48 +00001896 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman7ddecb42005-04-06 23:51:40 +00001897 BuildMI(BB, PPC::ANDIo, 2, Result).addReg(Tmp1).addImm(Tmp2);
1898 break;
1899 case 2: // Shifted immediate
Chris Lattnercafb67b2005-05-09 17:39:48 +00001900 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman7ddecb42005-04-06 23:51:40 +00001901 BuildMI(BB, PPC::ANDISo, 2, Result).addReg(Tmp1).addImm(Tmp2);
1902 break;
Nate Begeman9f833d32005-04-12 00:10:02 +00001903 case 5: // Bitfield mask
1904 Opc = Recording ? PPC::RLWINMo : PPC::RLWINM;
1905 Tmp3 = Tmp2 >> 16; // MB
1906 Tmp2 &= 0xFFFF; // ME
Chris Lattnercafb67b2005-05-09 17:39:48 +00001907
1908 if (N.getOperand(0).getOpcode() == ISD::SRL)
1909 if (ConstantSDNode *SA =
1910 dyn_cast<ConstantSDNode>(N.getOperand(0).getOperand(1))) {
1911
1912 // We can fold the RLWINM and the SRL together if the mask is
1913 // clearing the top bits which are rotated around.
1914 unsigned RotAmt = 32-(SA->getValue() & 31);
1915 if (Tmp2 <= RotAmt) {
1916 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1917 BuildMI(BB, Opc, 4, Result).addReg(Tmp1).addImm(RotAmt)
1918 .addImm(Tmp3).addImm(Tmp2);
1919 break;
1920 }
1921 }
1922
1923 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman9f833d32005-04-12 00:10:02 +00001924 BuildMI(BB, Opc, 4, Result).addReg(Tmp1).addImm(0)
1925 .addImm(Tmp3).addImm(Tmp2);
1926 break;
Nate Begeman7ddecb42005-04-06 23:51:40 +00001927 }
Nate Begemanc7bd4822005-04-11 06:34:10 +00001928 RecordSuccess = true;
Nate Begeman7ddecb42005-04-06 23:51:40 +00001929 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001930
Nate Begemana9795f82005-03-24 04:41:43 +00001931 case ISD::OR:
Nate Begeman7ddecb42005-04-06 23:51:40 +00001932 if (SelectBitfieldInsert(N, Result))
1933 return Result;
Nate Begeman1cbf3ab2005-04-18 07:48:09 +00001934 if (PPCCRopts) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001935 if (N.getOperand(0).getOpcode() == ISD::SETCC ||
Nate Begeman1cbf3ab2005-04-18 07:48:09 +00001936 N.getOperand(1).getOpcode() == ISD::SETCC) {
1937 bool Inv;
1938 Tmp1 = SelectCCExpr(N, Opc, Inv, Tmp2);
1939 MoveCRtoGPR(Tmp1, Inv, Tmp2, Result);
1940 return Result;
1941 }
1942 }
Nate Begemana9795f82005-03-24 04:41:43 +00001943 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman439b4442005-04-05 04:22:58 +00001944 switch(getImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
Nate Begemana9795f82005-03-24 04:41:43 +00001945 default: assert(0 && "unhandled result code");
1946 case 0: // No immediate
1947 Tmp2 = SelectExpr(N.getOperand(1));
Nate Begemanc7bd4822005-04-11 06:34:10 +00001948 Opc = Recording ? PPC::ORo : PPC::OR;
1949 RecordSuccess = true;
1950 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +00001951 break;
1952 case 1: // Low immediate
Nate Begeman7ddecb42005-04-06 23:51:40 +00001953 BuildMI(BB, PPC::ORI, 2, Result).addReg(Tmp1).addImm(Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +00001954 break;
1955 case 2: // Shifted immediate
Nate Begeman7ddecb42005-04-06 23:51:40 +00001956 BuildMI(BB, PPC::ORIS, 2, Result).addReg(Tmp1).addImm(Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +00001957 break;
1958 }
1959 return Result;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001960
Nate Begemanaa73a9f2005-04-03 11:20:20 +00001961 case ISD::XOR: {
1962 // Check for EQV: xor, (xor a, -1), b
1963 if (N.getOperand(0).getOpcode() == ISD::XOR &&
1964 N.getOperand(0).getOperand(1).getOpcode() == ISD::Constant &&
1965 cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->isAllOnesValue()) {
Nate Begemanaa73a9f2005-04-03 11:20:20 +00001966 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1967 Tmp2 = SelectExpr(N.getOperand(1));
1968 BuildMI(BB, PPC::EQV, 2, Result).addReg(Tmp1).addReg(Tmp2);
1969 return Result;
1970 }
Chris Lattner837a5212005-04-21 21:09:11 +00001971 // Check for NOT, NOR, EQV, and NAND: xor (copy, or, xor, and), -1
Nate Begemanaa73a9f2005-04-03 11:20:20 +00001972 if (N.getOperand(1).getOpcode() == ISD::Constant &&
1973 cast<ConstantSDNode>(N.getOperand(1))->isAllOnesValue()) {
Nate Begemanaa73a9f2005-04-03 11:20:20 +00001974 switch(N.getOperand(0).getOpcode()) {
1975 case ISD::OR:
1976 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1977 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1978 BuildMI(BB, PPC::NOR, 2, Result).addReg(Tmp1).addReg(Tmp2);
1979 break;
1980 case ISD::AND:
1981 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1982 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1983 BuildMI(BB, PPC::NAND, 2, Result).addReg(Tmp1).addReg(Tmp2);
1984 break;
Chris Lattner837a5212005-04-21 21:09:11 +00001985 case ISD::XOR:
1986 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1987 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1988 BuildMI(BB, PPC::EQV, 2, Result).addReg(Tmp1).addReg(Tmp2);
1989 break;
Nate Begemanaa73a9f2005-04-03 11:20:20 +00001990 default:
1991 Tmp1 = SelectExpr(N.getOperand(0));
1992 BuildMI(BB, PPC::NOR, 2, Result).addReg(Tmp1).addReg(Tmp1);
1993 break;
1994 }
1995 return Result;
1996 }
1997 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman439b4442005-04-05 04:22:58 +00001998 switch(getImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
Nate Begemanaa73a9f2005-04-03 11:20:20 +00001999 default: assert(0 && "unhandled result code");
2000 case 0: // No immediate
2001 Tmp2 = SelectExpr(N.getOperand(1));
2002 BuildMI(BB, PPC::XOR, 2, Result).addReg(Tmp1).addReg(Tmp2);
2003 break;
2004 case 1: // Low immediate
2005 BuildMI(BB, PPC::XORI, 2, Result).addReg(Tmp1).addImm(Tmp2);
2006 break;
2007 case 2: // Shifted immediate
2008 BuildMI(BB, PPC::XORIS, 2, Result).addReg(Tmp1).addImm(Tmp2);
2009 break;
2010 }
2011 return Result;
2012 }
2013
Nate Begeman9e3e1b52005-03-24 23:35:30 +00002014 case ISD::SUB:
Nate Begemand7c4a4a2005-05-11 23:43:56 +00002015 if (1 == getImmediateForOpcode(N.getOperand(0), opcode, Tmp1, true)) {
2016 Tmp2 = SelectExpr(N.getOperand(1));
Nate Begeman27523a12005-04-02 00:42:16 +00002017 BuildMI(BB, PPC::SUBFIC, 2, Result).addReg(Tmp2).addSImm(Tmp1);
Nate Begemand7c4a4a2005-05-11 23:43:56 +00002018 } else if (1 == getImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
Nate Begeman27523a12005-04-02 00:42:16 +00002019 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begemand7c4a4a2005-05-11 23:43:56 +00002020 BuildMI(BB, PPC::ADDI, 2, Result).addReg(Tmp1).addSImm(Tmp2);
2021 } else {
2022 Tmp1 = SelectExpr(N.getOperand(0));
2023 Tmp2 = SelectExpr(N.getOperand(1));
Nate Begeman27523a12005-04-02 00:42:16 +00002024 BuildMI(BB, PPC::SUBF, 2, Result).addReg(Tmp2).addReg(Tmp1);
2025 }
Nate Begeman9e3e1b52005-03-24 23:35:30 +00002026 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002027
Nate Begeman5e966612005-03-24 06:28:42 +00002028 case ISD::MUL:
Nate Begeman9e3e1b52005-03-24 23:35:30 +00002029 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman439b4442005-04-05 04:22:58 +00002030 if (1 == getImmediateForOpcode(N.getOperand(1), opcode, Tmp2))
Nate Begeman307e7442005-03-26 01:28:53 +00002031 BuildMI(BB, PPC::MULLI, 2, Result).addReg(Tmp1).addSImm(Tmp2);
2032 else {
2033 Tmp2 = SelectExpr(N.getOperand(1));
2034 BuildMI(BB, PPC::MULLW, 2, Result).addReg(Tmp1).addReg(Tmp2);
2035 }
Nate Begeman9e3e1b52005-03-24 23:35:30 +00002036 return Result;
2037
Nate Begeman815d6da2005-04-06 00:25:27 +00002038 case ISD::MULHS:
2039 case ISD::MULHU:
2040 Tmp1 = SelectExpr(N.getOperand(0));
2041 Tmp2 = SelectExpr(N.getOperand(1));
2042 Opc = (ISD::MULHU == opcode) ? PPC::MULHWU : PPC::MULHW;
2043 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
2044 return Result;
2045
Nate Begemanf3d08f32005-03-29 00:03:27 +00002046 case ISD::SDIV:
2047 case ISD::UDIV:
Nate Begeman815d6da2005-04-06 00:25:27 +00002048 switch (getImmediateForOpcode(N.getOperand(1), opcode, Tmp3)) {
2049 default: break;
2050 // If this is an sdiv by a power of two, we can use an srawi/addze pair.
2051 case 3:
Nate Begeman80196b12005-04-05 00:15:08 +00002052 Tmp1 = MakeReg(MVT::i32);
2053 Tmp2 = SelectExpr(N.getOperand(0));
Nate Begeman9f833d32005-04-12 00:10:02 +00002054 if ((int)Tmp3 < 0) {
2055 unsigned Tmp4 = MakeReg(MVT::i32);
2056 BuildMI(BB, PPC::SRAWI, 2, Tmp1).addReg(Tmp2).addImm(-Tmp3);
2057 BuildMI(BB, PPC::ADDZE, 1, Tmp4).addReg(Tmp1);
2058 BuildMI(BB, PPC::NEG, 1, Result).addReg(Tmp4);
2059 } else {
2060 BuildMI(BB, PPC::SRAWI, 2, Tmp1).addReg(Tmp2).addImm(Tmp3);
2061 BuildMI(BB, PPC::ADDZE, 1, Result).addReg(Tmp1);
2062 }
Nate Begeman80196b12005-04-05 00:15:08 +00002063 return Result;
Nate Begeman815d6da2005-04-06 00:25:27 +00002064 // If this is a divide by constant, we can emit code using some magic
2065 // constants to implement it as a multiply instead.
Nate Begeman27b4c232005-04-06 06:44:57 +00002066 case 4:
2067 ExprMap.erase(N);
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002068 if (opcode == ISD::SDIV)
Nate Begeman27b4c232005-04-06 06:44:57 +00002069 return SelectExpr(BuildSDIVSequence(N));
2070 else
2071 return SelectExpr(BuildUDIVSequence(N));
Nate Begeman80196b12005-04-05 00:15:08 +00002072 }
Nate Begemanf3d08f32005-03-29 00:03:27 +00002073 Tmp1 = SelectExpr(N.getOperand(0));
2074 Tmp2 = SelectExpr(N.getOperand(1));
2075 Opc = (ISD::UDIV == opcode) ? PPC::DIVWU : PPC::DIVW;
2076 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
2077 return Result;
2078
Nate Begeman9e3e1b52005-03-24 23:35:30 +00002079 case ISD::ADD_PARTS:
Nate Begemanca12a2b2005-03-28 22:28:37 +00002080 case ISD::SUB_PARTS: {
2081 assert(N.getNumOperands() == 4 && N.getValueType() == MVT::i32 &&
2082 "Not an i64 add/sub!");
2083 // Emit all of the operands.
2084 std::vector<unsigned> InVals;
2085 for (unsigned i = 0, e = N.getNumOperands(); i != e; ++i)
2086 InVals.push_back(SelectExpr(N.getOperand(i)));
2087 if (N.getOpcode() == ISD::ADD_PARTS) {
Nate Begeman27eeb002005-04-02 05:59:34 +00002088 BuildMI(BB, PPC::ADDC, 2, Result).addReg(InVals[0]).addReg(InVals[2]);
2089 BuildMI(BB, PPC::ADDE, 2, Result+1).addReg(InVals[1]).addReg(InVals[3]);
Nate Begemanca12a2b2005-03-28 22:28:37 +00002090 } else {
Nate Begeman27eeb002005-04-02 05:59:34 +00002091 BuildMI(BB, PPC::SUBFC, 2, Result).addReg(InVals[2]).addReg(InVals[0]);
2092 BuildMI(BB, PPC::SUBFE, 2, Result+1).addReg(InVals[3]).addReg(InVals[1]);
2093 }
2094 return Result+N.ResNo;
2095 }
2096
2097 case ISD::SHL_PARTS:
2098 case ISD::SRA_PARTS:
2099 case ISD::SRL_PARTS: {
2100 assert(N.getNumOperands() == 3 && N.getValueType() == MVT::i32 &&
2101 "Not an i64 shift!");
2102 unsigned ShiftOpLo = SelectExpr(N.getOperand(0));
2103 unsigned ShiftOpHi = SelectExpr(N.getOperand(1));
Nate Begeman3664cef2005-04-13 22:14:14 +00002104 unsigned SHReg = FoldIfWideZeroExtend(N.getOperand(2));
2105 Tmp1 = MakeReg(MVT::i32);
2106 Tmp2 = MakeReg(MVT::i32);
Nate Begeman27eeb002005-04-02 05:59:34 +00002107 Tmp3 = MakeReg(MVT::i32);
2108 unsigned Tmp4 = MakeReg(MVT::i32);
2109 unsigned Tmp5 = MakeReg(MVT::i32);
2110 unsigned Tmp6 = MakeReg(MVT::i32);
2111 BuildMI(BB, PPC::SUBFIC, 2, Tmp1).addReg(SHReg).addSImm(32);
2112 if (ISD::SHL_PARTS == opcode) {
2113 BuildMI(BB, PPC::SLW, 2, Tmp2).addReg(ShiftOpHi).addReg(SHReg);
2114 BuildMI(BB, PPC::SRW, 2, Tmp3).addReg(ShiftOpLo).addReg(Tmp1);
2115 BuildMI(BB, PPC::OR, 2, Tmp4).addReg(Tmp2).addReg(Tmp3);
2116 BuildMI(BB, PPC::ADDI, 2, Tmp5).addReg(SHReg).addSImm(-32);
Nate Begemanfa554702005-04-03 22:13:27 +00002117 BuildMI(BB, PPC::SLW, 2, Tmp6).addReg(ShiftOpLo).addReg(Tmp5);
Nate Begeman27eeb002005-04-02 05:59:34 +00002118 BuildMI(BB, PPC::OR, 2, Result+1).addReg(Tmp4).addReg(Tmp6);
2119 BuildMI(BB, PPC::SLW, 2, Result).addReg(ShiftOpLo).addReg(SHReg);
2120 } else if (ISD::SRL_PARTS == opcode) {
2121 BuildMI(BB, PPC::SRW, 2, Tmp2).addReg(ShiftOpLo).addReg(SHReg);
2122 BuildMI(BB, PPC::SLW, 2, Tmp3).addReg(ShiftOpHi).addReg(Tmp1);
2123 BuildMI(BB, PPC::OR, 2, Tmp4).addReg(Tmp2).addReg(Tmp3);
2124 BuildMI(BB, PPC::ADDI, 2, Tmp5).addReg(SHReg).addSImm(-32);
2125 BuildMI(BB, PPC::SRW, 2, Tmp6).addReg(ShiftOpHi).addReg(Tmp5);
2126 BuildMI(BB, PPC::OR, 2, Result).addReg(Tmp4).addReg(Tmp6);
2127 BuildMI(BB, PPC::SRW, 2, Result+1).addReg(ShiftOpHi).addReg(SHReg);
2128 } else {
2129 MachineBasicBlock *TmpMBB = new MachineBasicBlock(BB->getBasicBlock());
2130 MachineBasicBlock *PhiMBB = new MachineBasicBlock(BB->getBasicBlock());
2131 MachineBasicBlock *OldMBB = BB;
2132 MachineFunction *F = BB->getParent();
2133 ilist<MachineBasicBlock>::iterator It = BB; ++It;
2134 F->getBasicBlockList().insert(It, TmpMBB);
2135 F->getBasicBlockList().insert(It, PhiMBB);
2136 BB->addSuccessor(TmpMBB);
2137 BB->addSuccessor(PhiMBB);
2138 BuildMI(BB, PPC::SRW, 2, Tmp2).addReg(ShiftOpLo).addReg(SHReg);
2139 BuildMI(BB, PPC::SLW, 2, Tmp3).addReg(ShiftOpHi).addReg(Tmp1);
2140 BuildMI(BB, PPC::OR, 2, Tmp4).addReg(Tmp2).addReg(Tmp3);
2141 BuildMI(BB, PPC::ADDICo, 2, Tmp5).addReg(SHReg).addSImm(-32);
2142 BuildMI(BB, PPC::SRAW, 2, Tmp6).addReg(ShiftOpHi).addReg(Tmp5);
2143 BuildMI(BB, PPC::SRAW, 2, Result+1).addReg(ShiftOpHi).addReg(SHReg);
2144 BuildMI(BB, PPC::BLE, 2).addReg(PPC::CR0).addMBB(PhiMBB);
2145 // Select correct least significant half if the shift amount > 32
2146 BB = TmpMBB;
2147 unsigned Tmp7 = MakeReg(MVT::i32);
2148 BuildMI(BB, PPC::OR, 2, Tmp7).addReg(Tmp6).addReg(Tmp6);
2149 TmpMBB->addSuccessor(PhiMBB);
2150 BB = PhiMBB;
2151 BuildMI(BB, PPC::PHI, 4, Result).addReg(Tmp4).addMBB(OldMBB)
2152 .addReg(Tmp7).addMBB(TmpMBB);
Nate Begemanca12a2b2005-03-28 22:28:37 +00002153 }
2154 return Result+N.ResNo;
2155 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002156
Nate Begemana9795f82005-03-24 04:41:43 +00002157 case ISD::FP_TO_UINT:
Nate Begeman6b559972005-04-01 02:59:27 +00002158 case ISD::FP_TO_SINT: {
2159 bool U = (ISD::FP_TO_UINT == opcode);
2160 Tmp1 = SelectExpr(N.getOperand(0));
2161 if (!U) {
2162 Tmp2 = MakeReg(MVT::f64);
2163 BuildMI(BB, PPC::FCTIWZ, 1, Tmp2).addReg(Tmp1);
2164 int FrameIdx = BB->getParent()->getFrameInfo()->CreateStackObject(8, 8);
2165 addFrameReference(BuildMI(BB, PPC::STFD, 3).addReg(Tmp2), FrameIdx);
2166 addFrameReference(BuildMI(BB, PPC::LWZ, 2, Result), FrameIdx, 4);
2167 return Result;
2168 } else {
2169 unsigned Zero = getConstDouble(0.0);
2170 unsigned MaxInt = getConstDouble((1LL << 32) - 1);
2171 unsigned Border = getConstDouble(1LL << 31);
2172 unsigned UseZero = MakeReg(MVT::f64);
2173 unsigned UseMaxInt = MakeReg(MVT::f64);
2174 unsigned UseChoice = MakeReg(MVT::f64);
2175 unsigned TmpReg = MakeReg(MVT::f64);
2176 unsigned TmpReg2 = MakeReg(MVT::f64);
2177 unsigned ConvReg = MakeReg(MVT::f64);
2178 unsigned IntTmp = MakeReg(MVT::i32);
2179 unsigned XorReg = MakeReg(MVT::i32);
2180 MachineFunction *F = BB->getParent();
2181 int FrameIdx = F->getFrameInfo()->CreateStackObject(8, 8);
2182 // Update machine-CFG edges
2183 MachineBasicBlock *XorMBB = new MachineBasicBlock(BB->getBasicBlock());
2184 MachineBasicBlock *PhiMBB = new MachineBasicBlock(BB->getBasicBlock());
2185 MachineBasicBlock *OldMBB = BB;
2186 ilist<MachineBasicBlock>::iterator It = BB; ++It;
2187 F->getBasicBlockList().insert(It, XorMBB);
2188 F->getBasicBlockList().insert(It, PhiMBB);
2189 BB->addSuccessor(XorMBB);
2190 BB->addSuccessor(PhiMBB);
2191 // Convert from floating point to unsigned 32-bit value
2192 // Use 0 if incoming value is < 0.0
2193 BuildMI(BB, PPC::FSEL, 3, UseZero).addReg(Tmp1).addReg(Tmp1).addReg(Zero);
2194 // Use 2**32 - 1 if incoming value is >= 2**32
2195 BuildMI(BB, PPC::FSUB, 2, UseMaxInt).addReg(MaxInt).addReg(Tmp1);
2196 BuildMI(BB, PPC::FSEL, 3, UseChoice).addReg(UseMaxInt).addReg(UseZero)
2197 .addReg(MaxInt);
2198 // Subtract 2**31
2199 BuildMI(BB, PPC::FSUB, 2, TmpReg).addReg(UseChoice).addReg(Border);
2200 // Use difference if >= 2**31
2201 BuildMI(BB, PPC::FCMPU, 2, PPC::CR0).addReg(UseChoice).addReg(Border);
2202 BuildMI(BB, PPC::FSEL, 3, TmpReg2).addReg(TmpReg).addReg(TmpReg)
2203 .addReg(UseChoice);
2204 // Convert to integer
2205 BuildMI(BB, PPC::FCTIWZ, 1, ConvReg).addReg(TmpReg2);
2206 addFrameReference(BuildMI(BB, PPC::STFD, 3).addReg(ConvReg), FrameIdx);
2207 addFrameReference(BuildMI(BB, PPC::LWZ, 2, IntTmp), FrameIdx, 4);
2208 BuildMI(BB, PPC::BLT, 2).addReg(PPC::CR0).addMBB(PhiMBB);
2209 BuildMI(BB, PPC::B, 1).addMBB(XorMBB);
2210
2211 // XorMBB:
2212 // add 2**31 if input was >= 2**31
2213 BB = XorMBB;
2214 BuildMI(BB, PPC::XORIS, 2, XorReg).addReg(IntTmp).addImm(0x8000);
2215 XorMBB->addSuccessor(PhiMBB);
2216
2217 // PhiMBB:
2218 // DestReg = phi [ IntTmp, OldMBB ], [ XorReg, XorMBB ]
2219 BB = PhiMBB;
2220 BuildMI(BB, PPC::PHI, 4, Result).addReg(IntTmp).addMBB(OldMBB)
2221 .addReg(XorReg).addMBB(XorMBB);
2222 return Result;
2223 }
2224 assert(0 && "Should never get here");
2225 return 0;
2226 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002227
Nate Begeman9e3e1b52005-03-24 23:35:30 +00002228 case ISD::SETCC:
Nate Begeman33162522005-03-29 21:54:38 +00002229 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Node)) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002230 if (ConstantSDNode *CN =
Nate Begeman7e7fadd2005-04-07 20:30:01 +00002231 dyn_cast<ConstantSDNode>(SetCC->getOperand(1).Val)) {
Nate Begeman9765c252005-04-12 21:22:28 +00002232 // We can codegen setcc op, imm very efficiently compared to a brcond.
2233 // Check for those cases here.
2234 // setcc op, 0
Nate Begeman7e7fadd2005-04-07 20:30:01 +00002235 if (CN->getValue() == 0) {
2236 Tmp1 = SelectExpr(SetCC->getOperand(0));
2237 switch (SetCC->getCondition()) {
Nate Begeman7bfba7d2005-04-14 09:45:08 +00002238 default: SetCC->dump(); assert(0 && "Unhandled SetCC condition"); abort();
Nate Begeman7e7fadd2005-04-07 20:30:01 +00002239 case ISD::SETEQ:
Nate Begeman7e7fadd2005-04-07 20:30:01 +00002240 Tmp2 = MakeReg(MVT::i32);
2241 BuildMI(BB, PPC::CNTLZW, 1, Tmp2).addReg(Tmp1);
2242 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp2).addImm(27)
2243 .addImm(5).addImm(31);
2244 break;
2245 case ISD::SETNE:
Nate Begeman7e7fadd2005-04-07 20:30:01 +00002246 Tmp2 = MakeReg(MVT::i32);
2247 BuildMI(BB, PPC::ADDIC, 2, Tmp2).addReg(Tmp1).addSImm(-1);
2248 BuildMI(BB, PPC::SUBFE, 2, Result).addReg(Tmp2).addReg(Tmp1);
2249 break;
Nate Begeman7e7fadd2005-04-07 20:30:01 +00002250 case ISD::SETLT:
2251 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(1)
2252 .addImm(31).addImm(31);
2253 break;
Nate Begeman7e7fadd2005-04-07 20:30:01 +00002254 case ISD::SETGT:
2255 Tmp2 = MakeReg(MVT::i32);
2256 Tmp3 = MakeReg(MVT::i32);
2257 BuildMI(BB, PPC::NEG, 2, Tmp2).addReg(Tmp1);
2258 BuildMI(BB, PPC::ANDC, 2, Tmp3).addReg(Tmp2).addReg(Tmp1);
2259 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp3).addImm(1)
2260 .addImm(31).addImm(31);
2261 break;
Nate Begeman9765c252005-04-12 21:22:28 +00002262 }
2263 return Result;
2264 }
2265 // setcc op, -1
2266 if (CN->isAllOnesValue()) {
2267 Tmp1 = SelectExpr(SetCC->getOperand(0));
2268 switch (SetCC->getCondition()) {
2269 default: assert(0 && "Unhandled SetCC condition"); abort();
2270 case ISD::SETEQ:
2271 Tmp2 = MakeReg(MVT::i32);
2272 Tmp3 = MakeReg(MVT::i32);
2273 BuildMI(BB, PPC::ADDIC, 2, Tmp2).addReg(Tmp1).addSImm(1);
2274 BuildMI(BB, PPC::LI, 1, Tmp3).addSImm(0);
2275 BuildMI(BB, PPC::ADDZE, 1, Result).addReg(Tmp3);
Nate Begeman7e7fadd2005-04-07 20:30:01 +00002276 break;
Nate Begeman9765c252005-04-12 21:22:28 +00002277 case ISD::SETNE:
2278 Tmp2 = MakeReg(MVT::i32);
2279 Tmp3 = MakeReg(MVT::i32);
2280 BuildMI(BB, PPC::NOR, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
2281 BuildMI(BB, PPC::ADDIC, 2, Tmp3).addReg(Tmp2).addSImm(-1);
2282 BuildMI(BB, PPC::SUBFE, 2, Result).addReg(Tmp3).addReg(Tmp2);
2283 break;
2284 case ISD::SETLT:
2285 Tmp2 = MakeReg(MVT::i32);
2286 Tmp3 = MakeReg(MVT::i32);
2287 BuildMI(BB, PPC::ADDI, 2, Tmp2).addReg(Tmp1).addSImm(1);
2288 BuildMI(BB, PPC::AND, 2, Tmp3).addReg(Tmp2).addReg(Tmp1);
2289 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp3).addImm(1)
2290 .addImm(31).addImm(31);
2291 break;
2292 case ISD::SETGT:
2293 Tmp2 = MakeReg(MVT::i32);
Nate Begeman7e7fadd2005-04-07 20:30:01 +00002294 BuildMI(BB, PPC::RLWINM, 4, Tmp2).addReg(Tmp1).addImm(1)
2295 .addImm(31).addImm(31);
2296 BuildMI(BB, PPC::XORI, 2, Result).addReg(Tmp2).addImm(1);
2297 break;
2298 }
2299 return Result;
2300 }
2301 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002302
Nate Begeman1cbf3ab2005-04-18 07:48:09 +00002303 bool Inv;
2304 unsigned CCReg = SelectCC(N, Opc, Inv, Tmp2);
2305 MoveCRtoGPR(CCReg, Inv, Tmp2, Result);
Nate Begeman33162522005-03-29 21:54:38 +00002306 return Result;
2307 }
2308 assert(0 && "Is this legal?");
2309 return 0;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002310
Nate Begeman74747862005-03-29 22:24:51 +00002311 case ISD::SELECT: {
Nate Begeman1cbf3ab2005-04-18 07:48:09 +00002312 bool Inv;
Chris Lattner30710192005-04-01 07:10:02 +00002313 unsigned TrueValue = SelectExpr(N.getOperand(1)); //Use if TRUE
2314 unsigned FalseValue = SelectExpr(N.getOperand(2)); //Use if FALSE
Nate Begeman1cbf3ab2005-04-18 07:48:09 +00002315 unsigned CCReg = SelectCC(N.getOperand(0), Opc, Inv, Tmp3);
Chris Lattner30710192005-04-01 07:10:02 +00002316
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002317 // Create an iterator with which to insert the MBB for copying the false
Nate Begeman74747862005-03-29 22:24:51 +00002318 // value and the MBB to hold the PHI instruction for this SetCC.
2319 MachineBasicBlock *thisMBB = BB;
2320 const BasicBlock *LLVM_BB = BB->getBasicBlock();
2321 ilist<MachineBasicBlock>::iterator It = BB;
2322 ++It;
2323
2324 // thisMBB:
2325 // ...
2326 // TrueVal = ...
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00002327 // cmpTY ccX, r1, r2
Nate Begeman74747862005-03-29 22:24:51 +00002328 // bCC copy1MBB
2329 // fallthrough --> copy0MBB
Nate Begeman74747862005-03-29 22:24:51 +00002330 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
2331 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00002332 BuildMI(BB, Opc, 2).addReg(CCReg).addMBB(sinkMBB);
Nate Begeman74747862005-03-29 22:24:51 +00002333 MachineFunction *F = BB->getParent();
2334 F->getBasicBlockList().insert(It, copy0MBB);
2335 F->getBasicBlockList().insert(It, sinkMBB);
2336 // Update machine-CFG edges
2337 BB->addSuccessor(copy0MBB);
2338 BB->addSuccessor(sinkMBB);
2339
2340 // copy0MBB:
2341 // %FalseValue = ...
2342 // # fallthrough to sinkMBB
2343 BB = copy0MBB;
Nate Begeman74747862005-03-29 22:24:51 +00002344 // Update machine-CFG edges
2345 BB->addSuccessor(sinkMBB);
2346
2347 // sinkMBB:
2348 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
2349 // ...
2350 BB = sinkMBB;
2351 BuildMI(BB, PPC::PHI, 4, Result).addReg(FalseValue)
2352 .addMBB(copy0MBB).addReg(TrueValue).addMBB(thisMBB);
Nate Begeman74747862005-03-29 22:24:51 +00002353 return Result;
2354 }
Nate Begemana9795f82005-03-24 04:41:43 +00002355
2356 case ISD::Constant:
2357 switch (N.getValueType()) {
2358 default: assert(0 && "Cannot use constants of this type!");
2359 case MVT::i1:
2360 BuildMI(BB, PPC::LI, 1, Result)
2361 .addSImm(!cast<ConstantSDNode>(N)->isNullValue());
2362 break;
2363 case MVT::i32:
2364 {
2365 int v = (int)cast<ConstantSDNode>(N)->getSignExtended();
2366 if (v < 32768 && v >= -32768) {
2367 BuildMI(BB, PPC::LI, 1, Result).addSImm(v);
2368 } else {
Nate Begeman5e966612005-03-24 06:28:42 +00002369 Tmp1 = MakeReg(MVT::i32);
2370 BuildMI(BB, PPC::LIS, 1, Tmp1).addSImm(v >> 16);
2371 BuildMI(BB, PPC::ORI, 2, Result).addReg(Tmp1).addImm(v & 0xFFFF);
Nate Begemana9795f82005-03-24 04:41:43 +00002372 }
2373 }
2374 }
2375 return Result;
2376 }
2377
2378 return 0;
2379}
2380
2381void ISel::Select(SDOperand N) {
2382 unsigned Tmp1, Tmp2, Opc;
2383 unsigned opcode = N.getOpcode();
2384
2385 if (!ExprMap.insert(std::make_pair(N, 1)).second)
2386 return; // Already selected.
2387
2388 SDNode *Node = N.Val;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002389
Nate Begemana9795f82005-03-24 04:41:43 +00002390 switch (Node->getOpcode()) {
2391 default:
2392 Node->dump(); std::cerr << "\n";
2393 assert(0 && "Node not handled yet!");
2394 case ISD::EntryToken: return; // Noop
2395 case ISD::TokenFactor:
2396 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
2397 Select(Node->getOperand(i));
2398 return;
Chris Lattner16cd04d2005-05-12 23:24:06 +00002399 case ISD::CALLSEQ_START:
2400 case ISD::CALLSEQ_END:
Nate Begemana9795f82005-03-24 04:41:43 +00002401 Select(N.getOperand(0));
2402 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
Chris Lattner16cd04d2005-05-12 23:24:06 +00002403 Opc = N.getOpcode() == ISD::CALLSEQ_START ? PPC::ADJCALLSTACKDOWN :
Nate Begemana9795f82005-03-24 04:41:43 +00002404 PPC::ADJCALLSTACKUP;
2405 BuildMI(BB, Opc, 1).addImm(Tmp1);
2406 return;
2407 case ISD::BR: {
2408 MachineBasicBlock *Dest =
2409 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
Nate Begemana9795f82005-03-24 04:41:43 +00002410 Select(N.getOperand(0));
2411 BuildMI(BB, PPC::B, 1).addMBB(Dest);
2412 return;
2413 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002414 case ISD::BRCOND:
Nate Begemancd08e4c2005-04-09 20:09:12 +00002415 case ISD::BRCONDTWOWAY:
Nate Begemana9795f82005-03-24 04:41:43 +00002416 SelectBranchCC(N);
2417 return;
2418 case ISD::CopyToReg:
2419 Select(N.getOperand(0));
2420 Tmp1 = SelectExpr(N.getOperand(1));
2421 Tmp2 = cast<RegSDNode>(N)->getReg();
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002422
Nate Begemana9795f82005-03-24 04:41:43 +00002423 if (Tmp1 != Tmp2) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002424 if (N.getOperand(1).getValueType() == MVT::f64 ||
Nate Begemana9795f82005-03-24 04:41:43 +00002425 N.getOperand(1).getValueType() == MVT::f32)
2426 BuildMI(BB, PPC::FMR, 1, Tmp2).addReg(Tmp1);
2427 else
2428 BuildMI(BB, PPC::OR, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
2429 }
2430 return;
2431 case ISD::ImplicitDef:
2432 Select(N.getOperand(0));
2433 BuildMI(BB, PPC::IMPLICIT_DEF, 0, cast<RegSDNode>(N)->getReg());
2434 return;
2435 case ISD::RET:
2436 switch (N.getNumOperands()) {
2437 default:
2438 assert(0 && "Unknown return instruction!");
2439 case 3:
2440 assert(N.getOperand(1).getValueType() == MVT::i32 &&
2441 N.getOperand(2).getValueType() == MVT::i32 &&
Misha Brukman7847fca2005-04-22 17:54:37 +00002442 "Unknown two-register value!");
Nate Begemana9795f82005-03-24 04:41:43 +00002443 Select(N.getOperand(0));
2444 Tmp1 = SelectExpr(N.getOperand(1));
2445 Tmp2 = SelectExpr(N.getOperand(2));
Nate Begeman27523a12005-04-02 00:42:16 +00002446 BuildMI(BB, PPC::OR, 2, PPC::R3).addReg(Tmp2).addReg(Tmp2);
2447 BuildMI(BB, PPC::OR, 2, PPC::R4).addReg(Tmp1).addReg(Tmp1);
Nate Begemana9795f82005-03-24 04:41:43 +00002448 break;
2449 case 2:
2450 Select(N.getOperand(0));
2451 Tmp1 = SelectExpr(N.getOperand(1));
2452 switch (N.getOperand(1).getValueType()) {
2453 default:
2454 assert(0 && "Unknown return type!");
2455 case MVT::f64:
2456 case MVT::f32:
2457 BuildMI(BB, PPC::FMR, 1, PPC::F1).addReg(Tmp1);
2458 break;
2459 case MVT::i32:
2460 BuildMI(BB, PPC::OR, 2, PPC::R3).addReg(Tmp1).addReg(Tmp1);
2461 break;
2462 }
Nate Begeman9e3e1b52005-03-24 23:35:30 +00002463 case 1:
2464 Select(N.getOperand(0));
2465 break;
Nate Begemana9795f82005-03-24 04:41:43 +00002466 }
2467 BuildMI(BB, PPC::BLR, 0); // Just emit a 'ret' instruction
2468 return;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002469 case ISD::TRUNCSTORE:
2470 case ISD::STORE:
Nate Begemana9795f82005-03-24 04:41:43 +00002471 {
2472 SDOperand Chain = N.getOperand(0);
2473 SDOperand Value = N.getOperand(1);
2474 SDOperand Address = N.getOperand(2);
2475 Select(Chain);
2476
2477 Tmp1 = SelectExpr(Value); //value
2478
2479 if (opcode == ISD::STORE) {
2480 switch(Value.getValueType()) {
2481 default: assert(0 && "unknown Type in store");
2482 case MVT::i32: Opc = PPC::STW; break;
2483 case MVT::f64: Opc = PPC::STFD; break;
2484 case MVT::f32: Opc = PPC::STFS; break;
2485 }
2486 } else { //ISD::TRUNCSTORE
2487 switch(cast<MVTSDNode>(Node)->getExtraValueType()) {
2488 default: assert(0 && "unknown Type in store");
Nate Begeman7e7fadd2005-04-07 20:30:01 +00002489 case MVT::i1:
Nate Begemana9795f82005-03-24 04:41:43 +00002490 case MVT::i8: Opc = PPC::STB; break;
2491 case MVT::i16: Opc = PPC::STH; break;
2492 }
2493 }
2494
Nate Begemana7e11a42005-04-01 05:57:17 +00002495 if(Address.getOpcode() == ISD::FrameIndex)
Nate Begemana9795f82005-03-24 04:41:43 +00002496 {
Nate Begeman58f718c2005-03-30 02:23:08 +00002497 Tmp2 = cast<FrameIndexSDNode>(Address)->getIndex();
2498 addFrameReference(BuildMI(BB, Opc, 3).addReg(Tmp1), (int)Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +00002499 }
2500 else
2501 {
2502 int offset;
Nate Begeman04730362005-04-01 04:45:11 +00002503 bool idx = SelectAddr(Address, Tmp2, offset);
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002504 if (idx) {
Nate Begeman04730362005-04-01 04:45:11 +00002505 Opc = IndexedOpForOp(Opc);
2506 BuildMI(BB, Opc, 3).addReg(Tmp1).addReg(Tmp2).addReg(offset);
2507 } else {
2508 BuildMI(BB, Opc, 3).addReg(Tmp1).addImm(offset).addReg(Tmp2);
2509 }
Nate Begemana9795f82005-03-24 04:41:43 +00002510 }
2511 return;
2512 }
2513 case ISD::EXTLOAD:
2514 case ISD::SEXTLOAD:
2515 case ISD::ZEXTLOAD:
2516 case ISD::LOAD:
2517 case ISD::CopyFromReg:
Chris Lattnerb5d8e6e2005-05-13 20:29:26 +00002518 case ISD::TAILCALL:
Nate Begemana9795f82005-03-24 04:41:43 +00002519 case ISD::CALL:
2520 case ISD::DYNAMIC_STACKALLOC:
2521 ExprMap.erase(N);
2522 SelectExpr(N);
2523 return;
2524 }
2525 assert(0 && "Should not be reached!");
2526}
2527
2528
2529/// createPPC32PatternInstructionSelector - This pass converts an LLVM function
2530/// into a machine code representation using pattern matching and a machine
2531/// description file.
2532///
2533FunctionPass *llvm::createPPC32ISelPattern(TargetMachine &TM) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002534 return new ISel(TM);
Chris Lattner246fa632005-03-24 06:16:18 +00002535}
2536