blob: 5388eebb6bcb2085f912c72df957421b522cada6 [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
Chris Lattnere0fe2252005-07-05 19:58:54 +0000100 virtual SDOperand LowerVAStart(SDOperand Chain, SDOperand VAListP,
101 Value *VAListV, SelectionDAG &DAG);
102
Nate Begemana9795f82005-03-24 04:41:43 +0000103 virtual std::pair<SDOperand,SDOperand>
Chris Lattnere0fe2252005-07-05 19:58:54 +0000104 LowerVAArg(SDOperand Chain, SDOperand VAListP, Value *VAListV,
105 const Type *ArgTy, SelectionDAG &DAG);
106
Nate Begemana9795f82005-03-24 04:41:43 +0000107 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
Chris Lattnere0fe2252005-07-05 19:58:54 +0000466SDOperand PPC32TargetLowering::LowerVAStart(SDOperand Chain, SDOperand VAListP,
467 Value *VAListV, SelectionDAG &DAG) {
Chris Lattnerf84a2ac2005-07-05 17:48:31 +0000468 // vastart just stores the address of the VarArgsFrameIndex slot into the
469 // memory location argument.
470 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
Chris Lattnere0fe2252005-07-05 19:58:54 +0000471 return DAG.getNode(ISD::STORE, MVT::Other, Chain, FR, VAListP,
472 DAG.getSrcValue(VAListV));
Nate Begemana9795f82005-03-24 04:41:43 +0000473}
474
Chris Lattnere0fe2252005-07-05 19:58:54 +0000475std::pair<SDOperand,SDOperand>
476PPC32TargetLowering::LowerVAArg(SDOperand Chain,
477 SDOperand VAListP, Value *VAListV,
478 const Type *ArgTy, SelectionDAG &DAG) {
Nate Begemanc7b09f12005-03-25 08:34:25 +0000479 MVT::ValueType ArgVT = getValueType(ArgTy);
Chris Lattnerf84a2ac2005-07-05 17:48:31 +0000480
481 SDOperand VAList =
Chris Lattnere0fe2252005-07-05 19:58:54 +0000482 DAG.getLoad(MVT::i32, Chain, VAListP, DAG.getSrcValue(VAListV));
483 SDOperand Result = DAG.getLoad(ArgVT, Chain, VAList, DAG.getSrcValue(NULL));
Chris Lattnerf84a2ac2005-07-05 17:48:31 +0000484 unsigned Amt;
485 if (ArgVT == MVT::i32 || ArgVT == MVT::f32)
486 Amt = 4;
487 else {
488 assert((ArgVT == MVT::i64 || ArgVT == MVT::f64) &&
489 "Other types should have been promoted for varargs!");
490 Amt = 8;
Nate Begemanc7b09f12005-03-25 08:34:25 +0000491 }
Chris Lattnerf84a2ac2005-07-05 17:48:31 +0000492 VAList = DAG.getNode(ISD::ADD, VAList.getValueType(), VAList,
493 DAG.getConstant(Amt, VAList.getValueType()));
494 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain,
Chris Lattnere0fe2252005-07-05 19:58:54 +0000495 VAList, VAListP, DAG.getSrcValue(VAListV));
Nate Begemanc7b09f12005-03-25 08:34:25 +0000496 return std::make_pair(Result, Chain);
Nate Begemana9795f82005-03-24 04:41:43 +0000497}
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000498
Nate Begemana9795f82005-03-24 04:41:43 +0000499
500std::pair<SDOperand, SDOperand> PPC32TargetLowering::
501LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
502 SelectionDAG &DAG) {
Nate Begeman01d05262005-03-30 01:45:43 +0000503 assert(0 && "LowerFrameReturnAddress unimplemented");
Nate Begemana9795f82005-03-24 04:41:43 +0000504 abort();
505}
506
507namespace {
Nate Begemanc7bd4822005-04-11 06:34:10 +0000508Statistic<>Recorded("ppc-codegen", "Number of recording ops emitted");
Nate Begeman93075ec2005-04-04 23:40:36 +0000509Statistic<>FusedFP("ppc-codegen", "Number of fused fp operations");
Nate Begeman7bfba7d2005-04-14 09:45:08 +0000510Statistic<>MultiBranch("ppc-codegen", "Number of setcc logical ops collapsed");
Nate Begemana9795f82005-03-24 04:41:43 +0000511//===--------------------------------------------------------------------===//
512/// ISel - PPC32 specific code to select PPC32 machine instructions for
513/// SelectionDAG operations.
514//===--------------------------------------------------------------------===//
515class ISel : public SelectionDAGISel {
Nate Begemana9795f82005-03-24 04:41:43 +0000516 PPC32TargetLowering PPC32Lowering;
Nate Begeman815d6da2005-04-06 00:25:27 +0000517 SelectionDAG *ISelDAG; // Hack to support us having a dag->dag transform
518 // for sdiv and udiv until it is put into the future
519 // dag combiner.
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000520
Nate Begemana9795f82005-03-24 04:41:43 +0000521 /// ExprMap - As shared expressions are codegen'd, we keep track of which
522 /// vreg the value is produced in, so we only emit one copy of each compiled
523 /// tree.
524 std::map<SDOperand, unsigned> ExprMap;
Nate Begemanc7b09f12005-03-25 08:34:25 +0000525
526 unsigned GlobalBaseReg;
527 bool GlobalBaseInitialized;
Nate Begemanc7bd4822005-04-11 06:34:10 +0000528 bool RecordSuccess;
Nate Begemana9795f82005-03-24 04:41:43 +0000529public:
Nate Begeman815d6da2005-04-06 00:25:27 +0000530 ISel(TargetMachine &TM) : SelectionDAGISel(PPC32Lowering), PPC32Lowering(TM),
531 ISelDAG(0) {}
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000532
Nate Begemanc7b09f12005-03-25 08:34:25 +0000533 /// runOnFunction - Override this function in order to reset our per-function
534 /// variables.
535 virtual bool runOnFunction(Function &Fn) {
536 // Make sure we re-emit a set of the global base reg if necessary
537 GlobalBaseInitialized = false;
538 return SelectionDAGISel::runOnFunction(Fn);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000539 }
540
Nate Begemana9795f82005-03-24 04:41:43 +0000541 /// InstructionSelectBasicBlock - This callback is invoked by
542 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
543 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG) {
544 DEBUG(BB->dump());
545 // Codegen the basic block.
Nate Begeman815d6da2005-04-06 00:25:27 +0000546 ISelDAG = &DAG;
Nate Begemana9795f82005-03-24 04:41:43 +0000547 Select(DAG.getRoot());
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000548
Nate Begemana9795f82005-03-24 04:41:43 +0000549 // Clear state used for selection.
550 ExprMap.clear();
Nate Begeman815d6da2005-04-06 00:25:27 +0000551 ISelDAG = 0;
Nate Begemana9795f82005-03-24 04:41:43 +0000552 }
Nate Begeman815d6da2005-04-06 00:25:27 +0000553
554 // dag -> dag expanders for integer divide by constant
555 SDOperand BuildSDIVSequence(SDOperand N);
556 SDOperand BuildUDIVSequence(SDOperand N);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000557
Nate Begemandffcfcc2005-04-01 00:32:34 +0000558 unsigned getGlobalBaseReg();
Nate Begeman6b559972005-04-01 02:59:27 +0000559 unsigned getConstDouble(double floatVal, unsigned Result);
Nate Begeman1cbf3ab2005-04-18 07:48:09 +0000560 void MoveCRtoGPR(unsigned CCReg, bool Inv, unsigned Idx, unsigned Result);
Nate Begeman7ddecb42005-04-06 23:51:40 +0000561 bool SelectBitfieldInsert(SDOperand OR, unsigned Result);
Nate Begeman3664cef2005-04-13 22:14:14 +0000562 unsigned FoldIfWideZeroExtend(SDOperand N);
Nate Begeman1cbf3ab2005-04-18 07:48:09 +0000563 unsigned SelectCC(SDOperand CC, unsigned &Opc, bool &Inv, unsigned &Idx);
564 unsigned SelectCCExpr(SDOperand N, unsigned& Opc, bool &Inv, unsigned &Idx);
Nate Begemanc7bd4822005-04-11 06:34:10 +0000565 unsigned SelectExpr(SDOperand N, bool Recording=false);
Nate Begemana9795f82005-03-24 04:41:43 +0000566 unsigned SelectExprFP(SDOperand N, unsigned Result);
567 void Select(SDOperand N);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000568
Nate Begeman04730362005-04-01 04:45:11 +0000569 bool SelectAddr(SDOperand N, unsigned& Reg, int& offset);
Nate Begemana9795f82005-03-24 04:41:43 +0000570 void SelectBranchCC(SDOperand N);
571};
572
Nate Begeman80196b12005-04-05 00:15:08 +0000573/// ExactLog2 - This function solves for (Val == 1 << (N-1)) and returns N. It
574/// returns zero when the input is not exactly a power of two.
575static unsigned ExactLog2(unsigned Val) {
576 if (Val == 0 || (Val & (Val-1))) return 0;
577 unsigned Count = 0;
578 while (Val != 1) {
579 Val >>= 1;
580 ++Count;
581 }
582 return Count;
583}
584
Nate Begeman7ddecb42005-04-06 23:51:40 +0000585// IsRunOfOnes - returns true if Val consists of one contiguous run of 1's with
586// any number of 0's on either side. the 1's are allowed to wrap from LSB to
587// MSB. so 0x000FFF0, 0x0000FFFF, and 0xFF0000FF are all runs. 0x0F0F0000 is
588// not, since all 1's are not contiguous.
589static bool IsRunOfOnes(unsigned Val, unsigned &MB, unsigned &ME) {
590 bool isRun = true;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000591 MB = 0;
Nate Begeman7ddecb42005-04-06 23:51:40 +0000592 ME = 0;
593
594 // look for first set bit
595 int i = 0;
596 for (; i < 32; i++) {
597 if ((Val & (1 << (31 - i))) != 0) {
598 MB = i;
599 ME = i;
600 break;
601 }
602 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000603
Nate Begeman7ddecb42005-04-06 23:51:40 +0000604 // look for last set bit
605 for (; i < 32; i++) {
606 if ((Val & (1 << (31 - i))) == 0)
607 break;
608 ME = i;
609 }
610
611 // look for next set bit
612 for (; i < 32; i++) {
613 if ((Val & (1 << (31 - i))) != 0)
614 break;
615 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000616
Nate Begeman7ddecb42005-04-06 23:51:40 +0000617 // if we exhausted all the bits, we found a match at this point for 0*1*0*
618 if (i == 32)
619 return true;
620
621 // since we just encountered more 1's, if it doesn't wrap around to the
622 // most significant bit of the word, then we did not find a match to 1*0*1* so
623 // exit.
624 if (MB != 0)
625 return false;
626
627 // look for last set bit
628 for (MB = i; i < 32; i++) {
629 if ((Val & (1 << (31 - i))) == 0)
630 break;
631 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000632
Nate Begeman7ddecb42005-04-06 23:51:40 +0000633 // if we exhausted all the bits, then we found a match for 1*0*1*, otherwise,
634 // the value is not a run of ones.
635 if (i == 32)
636 return true;
637 return false;
638}
639
Nate Begeman439b4442005-04-05 04:22:58 +0000640/// getImmediateForOpcode - This method returns a value indicating whether
Nate Begemana9795f82005-03-24 04:41:43 +0000641/// the ConstantSDNode N can be used as an immediate to Opcode. The return
642/// values are either 0, 1 or 2. 0 indicates that either N is not a
Nate Begeman9f833d32005-04-12 00:10:02 +0000643/// ConstantSDNode, or is not suitable for use by that opcode.
644/// Return value codes for turning into an enum someday:
645/// 1: constant may be used in normal immediate form.
646/// 2: constant may be used in shifted immediate form.
647/// 3: log base 2 of the constant may be used.
648/// 4: constant is suitable for integer division conversion
649/// 5: constant is a bitfield mask
Nate Begemana9795f82005-03-24 04:41:43 +0000650///
Nate Begeman439b4442005-04-05 04:22:58 +0000651static unsigned getImmediateForOpcode(SDOperand N, unsigned Opcode,
652 unsigned& Imm, bool U = false) {
Nate Begemana9795f82005-03-24 04:41:43 +0000653 if (N.getOpcode() != ISD::Constant) return 0;
654
655 int v = (int)cast<ConstantSDNode>(N)->getSignExtended();
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000656
Nate Begemana9795f82005-03-24 04:41:43 +0000657 switch(Opcode) {
658 default: return 0;
659 case ISD::ADD:
660 if (v <= 32767 && v >= -32768) { Imm = v & 0xFFFF; return 1; }
661 if ((v & 0x0000FFFF) == 0) { Imm = v >> 16; return 2; }
662 break;
Nate Begeman9f833d32005-04-12 00:10:02 +0000663 case ISD::AND: {
664 unsigned MB, ME;
665 if (IsRunOfOnes(v, MB, ME)) { Imm = MB << 16 | ME & 0xFFFF; return 5; }
666 if (v >= 0 && v <= 65535) { Imm = v & 0xFFFF; return 1; }
667 if ((v & 0x0000FFFF) == 0) { Imm = v >> 16; return 2; }
668 break;
669 }
Nate Begemana9795f82005-03-24 04:41:43 +0000670 case ISD::XOR:
671 case ISD::OR:
672 if (v >= 0 && v <= 65535) { Imm = v & 0xFFFF; return 1; }
673 if ((v & 0x0000FFFF) == 0) { Imm = v >> 16; return 2; }
674 break;
Nate Begeman307e7442005-03-26 01:28:53 +0000675 case ISD::MUL:
676 if (v <= 32767 && v >= -32768) { Imm = v & 0xFFFF; return 1; }
677 break;
Nate Begemand7c4a4a2005-05-11 23:43:56 +0000678 case ISD::SUB:
679 // handle subtract-from separately from subtract, since subi is really addi
680 if (U && v <= 32767 && v >= -32768) { Imm = v & 0xFFFF; return 1; }
681 if (!U && v <= 32768 && v >= -32767) { Imm = (-v) & 0xFFFF; return 1; }
682 break;
Nate Begeman3e897162005-03-31 23:55:40 +0000683 case ISD::SETCC:
684 if (U && (v >= 0 && v <= 65535)) { Imm = v & 0xFFFF; return 1; }
685 if (!U && (v <= 32767 && v >= -32768)) { Imm = v & 0xFFFF; return 1; }
686 break;
Nate Begeman80196b12005-04-05 00:15:08 +0000687 case ISD::SDIV:
Nate Begeman439b4442005-04-05 04:22:58 +0000688 if ((Imm = ExactLog2(v))) { return 3; }
Nate Begeman9f833d32005-04-12 00:10:02 +0000689 if ((Imm = ExactLog2(-v))) { Imm = -Imm; return 3; }
Nate Begeman815d6da2005-04-06 00:25:27 +0000690 if (v <= -2 || v >= 2) { return 4; }
691 break;
692 case ISD::UDIV:
Nate Begeman27b4c232005-04-06 06:44:57 +0000693 if (v > 1) { return 4; }
Nate Begeman80196b12005-04-05 00:15:08 +0000694 break;
Nate Begemana9795f82005-03-24 04:41:43 +0000695 }
696 return 0;
697}
Nate Begeman3e897162005-03-31 23:55:40 +0000698
Nate Begemanc7bd4822005-04-11 06:34:10 +0000699/// NodeHasRecordingVariant - If SelectExpr can always produce code for
700/// NodeOpcode that also sets CR0 as a side effect, return true. Otherwise,
701/// return false.
702static bool NodeHasRecordingVariant(unsigned NodeOpcode) {
703 switch(NodeOpcode) {
704 default: return false;
705 case ISD::AND:
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000706 case ISD::OR:
Chris Lattner519f40b2005-04-13 02:46:17 +0000707 return true;
Nate Begemanc7bd4822005-04-11 06:34:10 +0000708 }
709}
710
Nate Begeman3e897162005-03-31 23:55:40 +0000711/// getBCCForSetCC - Returns the PowerPC condition branch mnemonic corresponding
712/// to Condition. If the Condition is unordered or unsigned, the bool argument
713/// U is set to true, otherwise it is set to false.
714static unsigned getBCCForSetCC(unsigned Condition, bool& U) {
715 U = false;
716 switch (Condition) {
717 default: assert(0 && "Unknown condition!"); abort();
718 case ISD::SETEQ: return PPC::BEQ;
719 case ISD::SETNE: return PPC::BNE;
720 case ISD::SETULT: U = true;
721 case ISD::SETLT: return PPC::BLT;
722 case ISD::SETULE: U = true;
723 case ISD::SETLE: return PPC::BLE;
724 case ISD::SETUGT: U = true;
725 case ISD::SETGT: return PPC::BGT;
726 case ISD::SETUGE: U = true;
727 case ISD::SETGE: return PPC::BGE;
728 }
Nate Begeman04730362005-04-01 04:45:11 +0000729 return 0;
730}
731
Nate Begeman7bfba7d2005-04-14 09:45:08 +0000732/// getCROpForOp - Return the condition register opcode (or inverted opcode)
733/// associated with the SelectionDAG opcode.
734static unsigned getCROpForSetCC(unsigned Opcode, bool Inv1, bool Inv2) {
735 switch (Opcode) {
736 default: assert(0 && "Unknown opcode!"); abort();
737 case ISD::AND:
738 if (Inv1 && Inv2) return PPC::CRNOR; // De Morgan's Law
739 if (!Inv1 && !Inv2) return PPC::CRAND;
740 if (Inv1 ^ Inv2) return PPC::CRANDC;
741 case ISD::OR:
742 if (Inv1 && Inv2) return PPC::CRNAND; // De Morgan's Law
743 if (!Inv1 && !Inv2) return PPC::CROR;
744 if (Inv1 ^ Inv2) return PPC::CRORC;
745 }
746 return 0;
747}
748
749/// getCRIdxForSetCC - Return the index of the condition register field
750/// associated with the SetCC condition, and whether or not the field is
751/// treated as inverted. That is, lt = 0; ge = 0 inverted.
752static unsigned getCRIdxForSetCC(unsigned Condition, bool& Inv) {
753 switch (Condition) {
754 default: assert(0 && "Unknown condition!"); abort();
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000755 case ISD::SETULT:
Nate Begeman7bfba7d2005-04-14 09:45:08 +0000756 case ISD::SETLT: Inv = false; return 0;
757 case ISD::SETUGE:
758 case ISD::SETGE: Inv = true; return 0;
759 case ISD::SETUGT:
760 case ISD::SETGT: Inv = false; return 1;
761 case ISD::SETULE:
762 case ISD::SETLE: Inv = true; return 1;
763 case ISD::SETEQ: Inv = false; return 2;
764 case ISD::SETNE: Inv = true; return 2;
765 }
766 return 0;
767}
768
Nate Begeman04730362005-04-01 04:45:11 +0000769/// IndexedOpForOp - Return the indexed variant for each of the PowerPC load
770/// and store immediate instructions.
771static unsigned IndexedOpForOp(unsigned Opcode) {
772 switch(Opcode) {
773 default: assert(0 && "Unknown opcode!"); abort();
774 case PPC::LBZ: return PPC::LBZX; case PPC::STB: return PPC::STBX;
775 case PPC::LHZ: return PPC::LHZX; case PPC::STH: return PPC::STHX;
776 case PPC::LHA: return PPC::LHAX; case PPC::STW: return PPC::STWX;
777 case PPC::LWZ: return PPC::LWZX; case PPC::STFS: return PPC::STFSX;
778 case PPC::LFS: return PPC::LFSX; case PPC::STFD: return PPC::STFDX;
779 case PPC::LFD: return PPC::LFDX;
780 }
781 return 0;
Nate Begeman3e897162005-03-31 23:55:40 +0000782}
Nate Begeman815d6da2005-04-06 00:25:27 +0000783
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000784// Structure used to return the necessary information to codegen an SDIV as
Nate Begeman815d6da2005-04-06 00:25:27 +0000785// a multiply.
786struct ms {
787 int m; // magic number
788 int s; // shift amount
789};
790
791struct mu {
792 unsigned int m; // magic number
793 int a; // add indicator
794 int s; // shift amount
795};
796
797/// magic - calculate the magic numbers required to codegen an integer sdiv as
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000798/// a sequence of multiply and shifts. Requires that the divisor not be 0, 1,
Nate Begeman815d6da2005-04-06 00:25:27 +0000799/// or -1.
800static struct ms magic(int d) {
801 int p;
802 unsigned int ad, anc, delta, q1, r1, q2, r2, t;
803 const unsigned int two31 = 2147483648U; // 2^31
804 struct ms mag;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000805
Nate Begeman815d6da2005-04-06 00:25:27 +0000806 ad = abs(d);
807 t = two31 + ((unsigned int)d >> 31);
808 anc = t - 1 - t%ad; // absolute value of nc
809 p = 31; // initialize p
810 q1 = two31/anc; // initialize q1 = 2p/abs(nc)
811 r1 = two31 - q1*anc; // initialize r1 = rem(2p,abs(nc))
812 q2 = two31/ad; // initialize q2 = 2p/abs(d)
813 r2 = two31 - q2*ad; // initialize r2 = rem(2p,abs(d))
814 do {
815 p = p + 1;
816 q1 = 2*q1; // update q1 = 2p/abs(nc)
817 r1 = 2*r1; // update r1 = rem(2p/abs(nc))
818 if (r1 >= anc) { // must be unsigned comparison
819 q1 = q1 + 1;
820 r1 = r1 - anc;
821 }
822 q2 = 2*q2; // update q2 = 2p/abs(d)
823 r2 = 2*r2; // update r2 = rem(2p/abs(d))
824 if (r2 >= ad) { // must be unsigned comparison
825 q2 = q2 + 1;
826 r2 = r2 - ad;
827 }
828 delta = ad - r2;
829 } while (q1 < delta || (q1 == delta && r1 == 0));
830
831 mag.m = q2 + 1;
832 if (d < 0) mag.m = -mag.m; // resulting magic number
833 mag.s = p - 32; // resulting shift
834 return mag;
835}
836
837/// magicu - calculate the magic numbers required to codegen an integer udiv as
838/// a sequence of multiply, add and shifts. Requires that the divisor not be 0.
839static struct mu magicu(unsigned d)
840{
841 int p;
842 unsigned int nc, delta, q1, r1, q2, r2;
843 struct mu magu;
844 magu.a = 0; // initialize "add" indicator
845 nc = - 1 - (-d)%d;
846 p = 31; // initialize p
847 q1 = 0x80000000/nc; // initialize q1 = 2p/nc
848 r1 = 0x80000000 - q1*nc; // initialize r1 = rem(2p,nc)
849 q2 = 0x7FFFFFFF/d; // initialize q2 = (2p-1)/d
850 r2 = 0x7FFFFFFF - q2*d; // initialize r2 = rem((2p-1),d)
851 do {
852 p = p + 1;
853 if (r1 >= nc - r1 ) {
854 q1 = 2*q1 + 1; // update q1
855 r1 = 2*r1 - nc; // update r1
856 }
857 else {
858 q1 = 2*q1; // update q1
859 r1 = 2*r1; // update r1
860 }
861 if (r2 + 1 >= d - r2) {
862 if (q2 >= 0x7FFFFFFF) magu.a = 1;
863 q2 = 2*q2 + 1; // update q2
864 r2 = 2*r2 + 1 - d; // update r2
865 }
866 else {
867 if (q2 >= 0x80000000) magu.a = 1;
868 q2 = 2*q2; // update q2
869 r2 = 2*r2 + 1; // update r2
870 }
871 delta = d - 1 - r2;
872 } while (p < 64 && (q1 < delta || (q1 == delta && r1 == 0)));
873 magu.m = q2 + 1; // resulting magic number
874 magu.s = p - 32; // resulting shift
875 return magu;
876}
877}
878
879/// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
880/// return a DAG expression to select that will generate the same value by
881/// multiplying by a magic number. See:
882/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
883SDOperand ISel::BuildSDIVSequence(SDOperand N) {
884 int d = (int)cast<ConstantSDNode>(N.getOperand(1))->getSignExtended();
885 ms magics = magic(d);
886 // Multiply the numerator (operand 0) by the magic value
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000887 SDOperand Q = ISelDAG->getNode(ISD::MULHS, MVT::i32, N.getOperand(0),
Nate Begeman815d6da2005-04-06 00:25:27 +0000888 ISelDAG->getConstant(magics.m, MVT::i32));
889 // If d > 0 and m < 0, add the numerator
890 if (d > 0 && magics.m < 0)
891 Q = ISelDAG->getNode(ISD::ADD, MVT::i32, Q, N.getOperand(0));
892 // If d < 0 and m > 0, subtract the numerator.
893 if (d < 0 && magics.m > 0)
894 Q = ISelDAG->getNode(ISD::SUB, MVT::i32, Q, N.getOperand(0));
895 // Shift right algebraic if shift value is nonzero
896 if (magics.s > 0)
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000897 Q = ISelDAG->getNode(ISD::SRA, MVT::i32, Q,
Nate Begeman815d6da2005-04-06 00:25:27 +0000898 ISelDAG->getConstant(magics.s, MVT::i32));
899 // Extract the sign bit and add it to the quotient
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000900 SDOperand T =
Nate Begeman815d6da2005-04-06 00:25:27 +0000901 ISelDAG->getNode(ISD::SRL, MVT::i32, Q, ISelDAG->getConstant(31, MVT::i32));
Nate Begeman27b4c232005-04-06 06:44:57 +0000902 return ISelDAG->getNode(ISD::ADD, MVT::i32, Q, T);
Nate Begeman815d6da2005-04-06 00:25:27 +0000903}
904
905/// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
906/// return a DAG expression to select that will generate the same value by
907/// multiplying by a magic number. See:
908/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
909SDOperand ISel::BuildUDIVSequence(SDOperand N) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000910 unsigned d =
Nate Begeman815d6da2005-04-06 00:25:27 +0000911 (unsigned)cast<ConstantSDNode>(N.getOperand(1))->getSignExtended();
912 mu magics = magicu(d);
913 // Multiply the numerator (operand 0) by the magic value
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000914 SDOperand Q = ISelDAG->getNode(ISD::MULHU, MVT::i32, N.getOperand(0),
Nate Begeman815d6da2005-04-06 00:25:27 +0000915 ISelDAG->getConstant(magics.m, MVT::i32));
916 if (magics.a == 0) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000917 Q = ISelDAG->getNode(ISD::SRL, MVT::i32, Q,
Nate Begeman815d6da2005-04-06 00:25:27 +0000918 ISelDAG->getConstant(magics.s, MVT::i32));
919 } else {
920 SDOperand NPQ = ISelDAG->getNode(ISD::SUB, MVT::i32, N.getOperand(0), Q);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000921 NPQ = ISelDAG->getNode(ISD::SRL, MVT::i32, NPQ,
Nate Begeman815d6da2005-04-06 00:25:27 +0000922 ISelDAG->getConstant(1, MVT::i32));
923 NPQ = ISelDAG->getNode(ISD::ADD, MVT::i32, NPQ, Q);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000924 Q = ISelDAG->getNode(ISD::SRL, MVT::i32, NPQ,
Nate Begeman815d6da2005-04-06 00:25:27 +0000925 ISelDAG->getConstant(magics.s-1, MVT::i32));
926 }
Nate Begeman27b4c232005-04-06 06:44:57 +0000927 return Q;
Nate Begemana9795f82005-03-24 04:41:43 +0000928}
929
Nate Begemanc7b09f12005-03-25 08:34:25 +0000930/// getGlobalBaseReg - Output the instructions required to put the
931/// base address to use for accessing globals into a register.
932///
933unsigned ISel::getGlobalBaseReg() {
934 if (!GlobalBaseInitialized) {
935 // Insert the set of GlobalBaseReg into the first MBB of the function
936 MachineBasicBlock &FirstMBB = BB->getParent()->front();
937 MachineBasicBlock::iterator MBBI = FirstMBB.begin();
938 GlobalBaseReg = MakeReg(MVT::i32);
939 BuildMI(FirstMBB, MBBI, PPC::MovePCtoLR, 0, PPC::LR);
940 BuildMI(FirstMBB, MBBI, PPC::MFLR, 1, GlobalBaseReg).addReg(PPC::LR);
941 GlobalBaseInitialized = true;
942 }
943 return GlobalBaseReg;
944}
945
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000946/// getConstDouble - Loads a floating point value into a register, via the
Nate Begeman6b559972005-04-01 02:59:27 +0000947/// Constant Pool. Optionally takes a register in which to load the value.
948unsigned ISel::getConstDouble(double doubleVal, unsigned Result=0) {
949 unsigned Tmp1 = MakeReg(MVT::i32);
950 if (0 == Result) Result = MakeReg(MVT::f64);
951 MachineConstantPool *CP = BB->getParent()->getConstantPool();
952 ConstantFP *CFP = ConstantFP::get(Type::DoubleTy, doubleVal);
953 unsigned CPI = CP->getConstantPoolIndex(CFP);
954 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp1).addReg(getGlobalBaseReg())
955 .addConstantPoolIndex(CPI);
956 BuildMI(BB, PPC::LFD, 2, Result).addConstantPoolIndex(CPI).addReg(Tmp1);
957 return Result;
958}
959
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000960/// MoveCRtoGPR - Move CCReg[Idx] to the least significant bit of Result. If
Nate Begeman1cbf3ab2005-04-18 07:48:09 +0000961/// Inv is true, then invert the result.
962void ISel::MoveCRtoGPR(unsigned CCReg, bool Inv, unsigned Idx, unsigned Result){
963 unsigned IntCR = MakeReg(MVT::i32);
964 BuildMI(BB, PPC::MCRF, 1, PPC::CR7).addReg(CCReg);
965 BuildMI(BB, PPC::MFCR, 1, IntCR).addReg(PPC::CR7);
966 if (Inv) {
967 unsigned Tmp1 = MakeReg(MVT::i32);
968 BuildMI(BB, PPC::RLWINM, 4, Tmp1).addReg(IntCR).addImm(32-(3-Idx))
969 .addImm(31).addImm(31);
970 BuildMI(BB, PPC::XORI, 2, Result).addReg(Tmp1).addImm(1);
971 } else {
972 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(IntCR).addImm(32-(3-Idx))
973 .addImm(31).addImm(31);
974 }
975}
976
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000977/// SelectBitfieldInsert - turn an or of two masked values into
Nate Begeman7ddecb42005-04-06 23:51:40 +0000978/// the rotate left word immediate then mask insert (rlwimi) instruction.
979/// Returns true on success, false if the caller still needs to select OR.
980///
981/// Patterns matched:
982/// 1. or shl, and 5. or and, and
983/// 2. or and, shl 6. or shl, shr
984/// 3. or shr, and 7. or shr, shl
985/// 4. or and, shr
986bool ISel::SelectBitfieldInsert(SDOperand OR, unsigned Result) {
Nate Begemancd08e4c2005-04-09 20:09:12 +0000987 bool IsRotate = false;
Nate Begeman7ddecb42005-04-06 23:51:40 +0000988 unsigned TgtMask = 0xFFFFFFFF, InsMask = 0xFFFFFFFF, Amount = 0;
Nate Begemanb2c4bf32005-06-08 04:14:27 +0000989
990 SDOperand Op0 = OR.getOperand(0);
991 SDOperand Op1 = OR.getOperand(1);
992
993 unsigned Op0Opc = Op0.getOpcode();
994 unsigned Op1Opc = Op1.getOpcode();
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000995
Nate Begeman7ddecb42005-04-06 23:51:40 +0000996 // Verify that we have the correct opcodes
997 if (ISD::SHL != Op0Opc && ISD::SRL != Op0Opc && ISD::AND != Op0Opc)
998 return false;
999 if (ISD::SHL != Op1Opc && ISD::SRL != Op1Opc && ISD::AND != Op1Opc)
1000 return false;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001001
Nate Begeman7ddecb42005-04-06 23:51:40 +00001002 // Generate Mask value for Target
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001003 if (ConstantSDNode *CN =
Nate Begemanb2c4bf32005-06-08 04:14:27 +00001004 dyn_cast<ConstantSDNode>(Op0.getOperand(1).Val)) {
Nate Begeman7ddecb42005-04-06 23:51:40 +00001005 switch(Op0Opc) {
1006 case ISD::SHL: TgtMask <<= (unsigned)CN->getValue(); break;
1007 case ISD::SRL: TgtMask >>= (unsigned)CN->getValue(); break;
1008 case ISD::AND: TgtMask &= (unsigned)CN->getValue(); break;
1009 }
1010 } else {
1011 return false;
1012 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001013
Nate Begeman7ddecb42005-04-06 23:51:40 +00001014 // Generate Mask value for Insert
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001015 if (ConstantSDNode *CN =
Nate Begemanb2c4bf32005-06-08 04:14:27 +00001016 dyn_cast<ConstantSDNode>(Op1.getOperand(1).Val)) {
Nate Begeman7ddecb42005-04-06 23:51:40 +00001017 switch(Op1Opc) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001018 case ISD::SHL:
1019 Amount = CN->getValue();
Nate Begemancd08e4c2005-04-09 20:09:12 +00001020 InsMask <<= Amount;
1021 if (Op0Opc == ISD::SRL) IsRotate = true;
Nate Begeman7ddecb42005-04-06 23:51:40 +00001022 break;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001023 case ISD::SRL:
1024 Amount = CN->getValue();
1025 InsMask >>= Amount;
Nate Begeman7ddecb42005-04-06 23:51:40 +00001026 Amount = 32-Amount;
Nate Begemancd08e4c2005-04-09 20:09:12 +00001027 if (Op0Opc == ISD::SHL) IsRotate = true;
Nate Begeman7ddecb42005-04-06 23:51:40 +00001028 break;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001029 case ISD::AND:
Nate Begeman7ddecb42005-04-06 23:51:40 +00001030 InsMask &= (unsigned)CN->getValue();
1031 break;
1032 }
1033 } else {
1034 return false;
1035 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001036
Nate Begemanb2c4bf32005-06-08 04:14:27 +00001037 unsigned Tmp3 = 0;
1038
1039 // If both of the inputs are ANDs and one of them has a logical shift by
1040 // constant as its input, make that the inserted value so that we can combine
1041 // the shift into the rotate part of the rlwimi instruction
1042 if (Op0Opc == ISD::AND && Op1Opc == ISD::AND) {
1043 if (Op1.getOperand(0).getOpcode() == ISD::SHL ||
1044 Op1.getOperand(0).getOpcode() == ISD::SRL) {
1045 if (ConstantSDNode *CN =
1046 dyn_cast<ConstantSDNode>(Op1.getOperand(0).getOperand(1).Val)) {
1047 Amount = Op1.getOperand(0).getOpcode() == ISD::SHL ?
1048 CN->getValue() : 32 - CN->getValue();
1049 Tmp3 = SelectExpr(Op1.getOperand(0).getOperand(0));
1050 }
1051 } else if (Op0.getOperand(0).getOpcode() == ISD::SHL ||
1052 Op0.getOperand(0).getOpcode() == ISD::SRL) {
1053 if (ConstantSDNode *CN =
1054 dyn_cast<ConstantSDNode>(Op0.getOperand(0).getOperand(1).Val)) {
1055 std::swap(Op0, Op1);
1056 std::swap(TgtMask, InsMask);
1057 Amount = Op1.getOperand(0).getOpcode() == ISD::SHL ?
1058 CN->getValue() : 32 - CN->getValue();
1059 Tmp3 = SelectExpr(Op1.getOperand(0).getOperand(0));
1060 }
1061 }
1062 }
1063
Nate Begeman7ddecb42005-04-06 23:51:40 +00001064 // Verify that the Target mask and Insert mask together form a full word mask
1065 // and that the Insert mask is a run of set bits (which implies both are runs
1066 // of set bits). Given that, Select the arguments and generate the rlwimi
1067 // instruction.
1068 unsigned MB, ME;
Nate Begemanb2c4bf32005-06-08 04:14:27 +00001069 if (((TgtMask & InsMask) == 0) && IsRunOfOnes(InsMask, MB, ME)) {
Nate Begeman7ddecb42005-04-06 23:51:40 +00001070 unsigned Tmp1, Tmp2;
Nate Begemanb2c4bf32005-06-08 04:14:27 +00001071 bool fullMask = (TgtMask ^ InsMask) == 0xFFFFFFFF;
Nate Begemancd08e4c2005-04-09 20:09:12 +00001072 // Check for rotlwi / rotrwi here, a special case of bitfield insert
1073 // where both bitfield halves are sourced from the same value.
Nate Begemanb2c4bf32005-06-08 04:14:27 +00001074 if (IsRotate && fullMask &&
Nate Begemancd08e4c2005-04-09 20:09:12 +00001075 OR.getOperand(0).getOperand(0) == OR.getOperand(1).getOperand(0)) {
Nate Begemancd08e4c2005-04-09 20:09:12 +00001076 Tmp1 = SelectExpr(OR.getOperand(0).getOperand(0));
1077 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(Amount)
1078 .addImm(0).addImm(31);
1079 return true;
1080 }
Nate Begemanb2c4bf32005-06-08 04:14:27 +00001081 if (Op0Opc == ISD::AND && fullMask)
1082 Tmp1 = SelectExpr(Op0.getOperand(0));
Nate Begeman7ddecb42005-04-06 23:51:40 +00001083 else
Nate Begemanb2c4bf32005-06-08 04:14:27 +00001084 Tmp1 = SelectExpr(Op0);
1085 Tmp2 = Tmp3 ? Tmp3 : SelectExpr(Op1.getOperand(0));
Nate Begeman7ddecb42005-04-06 23:51:40 +00001086 BuildMI(BB, PPC::RLWIMI, 5, Result).addReg(Tmp1).addReg(Tmp2)
1087 .addImm(Amount).addImm(MB).addImm(ME);
1088 return true;
1089 }
1090 return false;
1091}
1092
Nate Begeman3664cef2005-04-13 22:14:14 +00001093/// FoldIfWideZeroExtend - 32 bit PowerPC implicit masks shift amounts to the
1094/// low six bits. If the shift amount is an ISD::AND node with a mask that is
1095/// wider than the implicit mask, then we can get rid of the AND and let the
1096/// shift do the mask.
1097unsigned ISel::FoldIfWideZeroExtend(SDOperand N) {
1098 unsigned C;
1099 if (N.getOpcode() == ISD::AND &&
1100 5 == getImmediateForOpcode(N.getOperand(1), ISD::AND, C) && // isMask
1101 31 == (C & 0xFFFF) && // ME
1102 26 >= (C >> 16)) // MB
1103 return SelectExpr(N.getOperand(0));
1104 else
1105 return SelectExpr(N);
1106}
1107
Nate Begeman1cbf3ab2005-04-18 07:48:09 +00001108unsigned ISel::SelectCC(SDOperand CC, unsigned& Opc, bool &Inv, unsigned& Idx) {
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001109 unsigned Result, Tmp1, Tmp2;
Nate Begeman9765c252005-04-12 21:22:28 +00001110 bool AlreadySelected = false;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001111 static const unsigned CompareOpcodes[] =
Nate Begemandffcfcc2005-04-01 00:32:34 +00001112 { PPC::FCMPU, PPC::FCMPU, PPC::CMPW, PPC::CMPLW };
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001113
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001114 // Allocate a condition register for this expression
1115 Result = RegMap->createVirtualRegister(PPC32::CRRCRegisterClass);
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001116
Nate Begemandffcfcc2005-04-01 00:32:34 +00001117 // If the first operand to the select is a SETCC node, then we can fold it
1118 // into the branch that selects which value to return.
Nate Begeman16ac7092005-04-18 02:43:24 +00001119 if (SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(CC.Val)) {
Nate Begemandffcfcc2005-04-01 00:32:34 +00001120 bool U;
1121 Opc = getBCCForSetCC(SetCC->getCondition(), U);
Nate Begeman1cbf3ab2005-04-18 07:48:09 +00001122 Idx = getCRIdxForSetCC(SetCC->getCondition(), Inv);
Nate Begemandffcfcc2005-04-01 00:32:34 +00001123
Nate Begeman439b4442005-04-05 04:22:58 +00001124 // Pass the optional argument U to getImmediateForOpcode for SETCC,
Nate Begemandffcfcc2005-04-01 00:32:34 +00001125 // so that it knows whether the SETCC immediate range is signed or not.
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001126 if (1 == getImmediateForOpcode(SetCC->getOperand(1), ISD::SETCC,
Nate Begeman439b4442005-04-05 04:22:58 +00001127 Tmp2, U)) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001128 // For comparisons against zero, we can implicity set CR0 if a recording
Nate Begemanc7bd4822005-04-11 06:34:10 +00001129 // variant (e.g. 'or.' instead of 'or') of the instruction that defines
1130 // operand zero of the SetCC node is available.
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001131 if (0 == Tmp2 &&
Nate Begeman9765c252005-04-12 21:22:28 +00001132 NodeHasRecordingVariant(SetCC->getOperand(0).getOpcode()) &&
1133 SetCC->getOperand(0).Val->hasOneUse()) {
Nate Begemanc7bd4822005-04-11 06:34:10 +00001134 RecordSuccess = false;
1135 Tmp1 = SelectExpr(SetCC->getOperand(0), true);
1136 if (RecordSuccess) {
1137 ++Recorded;
Nate Begeman7bfba7d2005-04-14 09:45:08 +00001138 BuildMI(BB, PPC::MCRF, 1, Result).addReg(PPC::CR0);
1139 return Result;
Nate Begemanc7bd4822005-04-11 06:34:10 +00001140 }
1141 AlreadySelected = true;
1142 }
1143 // If we could not implicitly set CR0, then emit a compare immediate
1144 // instead.
1145 if (!AlreadySelected) Tmp1 = SelectExpr(SetCC->getOperand(0));
Nate Begemandffcfcc2005-04-01 00:32:34 +00001146 if (U)
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001147 BuildMI(BB, PPC::CMPLWI, 2, Result).addReg(Tmp1).addImm(Tmp2);
Nate Begemandffcfcc2005-04-01 00:32:34 +00001148 else
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001149 BuildMI(BB, PPC::CMPWI, 2, Result).addReg(Tmp1).addSImm(Tmp2);
Nate Begemandffcfcc2005-04-01 00:32:34 +00001150 } else {
1151 bool IsInteger = MVT::isInteger(SetCC->getOperand(0).getValueType());
1152 unsigned CompareOpc = CompareOpcodes[2 * IsInteger + U];
Nate Begemanc7bd4822005-04-11 06:34:10 +00001153 Tmp1 = SelectExpr(SetCC->getOperand(0));
Nate Begemandffcfcc2005-04-01 00:32:34 +00001154 Tmp2 = SelectExpr(SetCC->getOperand(1));
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001155 BuildMI(BB, CompareOpc, 2, Result).addReg(Tmp1).addReg(Tmp2);
Nate Begemandffcfcc2005-04-01 00:32:34 +00001156 }
1157 } else {
Nate Begemanf8b02942005-04-15 22:12:16 +00001158 if (PPCCRopts)
Nate Begeman1cbf3ab2005-04-18 07:48:09 +00001159 return SelectCCExpr(CC, Opc, Inv, Idx);
1160 // If this isn't a SetCC, then select the value and compare it against zero,
1161 // treating it as if it were a boolean.
Nate Begeman9765c252005-04-12 21:22:28 +00001162 Opc = PPC::BNE;
Nate Begeman1cbf3ab2005-04-18 07:48:09 +00001163 Idx = getCRIdxForSetCC(ISD::SETNE, Inv);
Nate Begemandffcfcc2005-04-01 00:32:34 +00001164 Tmp1 = SelectExpr(CC);
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001165 BuildMI(BB, PPC::CMPLWI, 2, Result).addReg(Tmp1).addImm(0);
Nate Begemandffcfcc2005-04-01 00:32:34 +00001166 }
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001167 return Result;
Nate Begemandffcfcc2005-04-01 00:32:34 +00001168}
1169
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001170unsigned ISel::SelectCCExpr(SDOperand N, unsigned& Opc, bool &Inv,
Nate Begeman1cbf3ab2005-04-18 07:48:09 +00001171 unsigned &Idx) {
1172 bool Inv0, Inv1;
1173 unsigned Idx0, Idx1, CROpc, Opc1, Tmp1, Tmp2;
1174
1175 // Allocate a condition register for this expression
1176 unsigned Result = RegMap->createVirtualRegister(PPC32::CRRCRegisterClass);
1177
1178 // Check for the operations we support:
1179 switch(N.getOpcode()) {
1180 default:
1181 Opc = PPC::BNE;
1182 Idx = getCRIdxForSetCC(ISD::SETNE, Inv);
1183 Tmp1 = SelectExpr(N);
1184 BuildMI(BB, PPC::CMPLWI, 2, Result).addReg(Tmp1).addImm(0);
1185 break;
1186 case ISD::OR:
1187 case ISD::AND:
1188 ++MultiBranch;
1189 Tmp1 = SelectCCExpr(N.getOperand(0), Opc, Inv0, Idx0);
1190 Tmp2 = SelectCCExpr(N.getOperand(1), Opc1, Inv1, Idx1);
1191 CROpc = getCROpForSetCC(N.getOpcode(), Inv0, Inv1);
1192 if (Inv0 && !Inv1) {
1193 std::swap(Tmp1, Tmp2);
1194 std::swap(Idx0, Idx1);
1195 Opc = Opc1;
1196 }
1197 if (Inv0 && Inv1) Opc = PPC32InstrInfo::invertPPCBranchOpcode(Opc);
1198 BuildMI(BB, CROpc, 5, Result).addImm(Idx0).addReg(Tmp1).addImm(Idx0)
1199 .addReg(Tmp2).addImm(Idx1);
1200 Inv = false;
1201 Idx = Idx0;
1202 break;
1203 case ISD::SETCC:
1204 Tmp1 = SelectCC(N, Opc, Inv, Idx);
1205 Result = Tmp1;
1206 break;
1207 }
1208 return Result;
1209}
1210
Nate Begemandffcfcc2005-04-01 00:32:34 +00001211/// Check to see if the load is a constant offset from a base register
Nate Begeman04730362005-04-01 04:45:11 +00001212bool ISel::SelectAddr(SDOperand N, unsigned& Reg, int& offset)
Nate Begemana9795f82005-03-24 04:41:43 +00001213{
Nate Begeman96fc6812005-03-31 02:05:53 +00001214 unsigned imm = 0, opcode = N.getOpcode();
Nate Begeman04730362005-04-01 04:45:11 +00001215 if (N.getOpcode() == ISD::ADD) {
1216 Reg = SelectExpr(N.getOperand(0));
Nate Begeman439b4442005-04-05 04:22:58 +00001217 if (1 == getImmediateForOpcode(N.getOperand(1), opcode, imm)) {
Nate Begeman96fc6812005-03-31 02:05:53 +00001218 offset = imm;
Nate Begeman04730362005-04-01 04:45:11 +00001219 return false;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001220 }
Nate Begeman04730362005-04-01 04:45:11 +00001221 offset = SelectExpr(N.getOperand(1));
1222 return true;
1223 }
Nate Begemana9795f82005-03-24 04:41:43 +00001224 Reg = SelectExpr(N);
1225 offset = 0;
Nate Begeman04730362005-04-01 04:45:11 +00001226 return false;
Nate Begemana9795f82005-03-24 04:41:43 +00001227}
1228
1229void ISel::SelectBranchCC(SDOperand N)
1230{
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001231 MachineBasicBlock *Dest =
Nate Begemana9795f82005-03-24 04:41:43 +00001232 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
Nate Begeman3e897162005-03-31 23:55:40 +00001233
Nate Begeman1cbf3ab2005-04-18 07:48:09 +00001234 bool Inv;
1235 unsigned Opc, CCReg, Idx;
Nate Begemana9795f82005-03-24 04:41:43 +00001236 Select(N.getOperand(0)); //chain
Nate Begeman1cbf3ab2005-04-18 07:48:09 +00001237 CCReg = SelectCC(N.getOperand(1), Opc, Inv, Idx);
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001238
Nate Begeman439009c2005-06-15 18:22:43 +00001239 // Iterate to the next basic block
1240 ilist<MachineBasicBlock>::iterator It = BB;
1241 ++It;
Nate Begemancd08e4c2005-04-09 20:09:12 +00001242
1243 // If this is a two way branch, then grab the fallthrough basic block argument
1244 // and build a PowerPC branch pseudo-op, suitable for long branch conversion
1245 // if necessary by the branch selection pass. Otherwise, emit a standard
1246 // conditional branch.
1247 if (N.getOpcode() == ISD::BRCONDTWOWAY) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001248 MachineBasicBlock *Fallthrough =
Nate Begemancd08e4c2005-04-09 20:09:12 +00001249 cast<BasicBlockSDNode>(N.getOperand(3))->getBasicBlock();
1250 if (Dest != It) {
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001251 BuildMI(BB, PPC::COND_BRANCH, 4).addReg(CCReg).addImm(Opc)
Nate Begemancd08e4c2005-04-09 20:09:12 +00001252 .addMBB(Dest).addMBB(Fallthrough);
1253 if (Fallthrough != It)
1254 BuildMI(BB, PPC::B, 1).addMBB(Fallthrough);
1255 } else {
1256 if (Fallthrough != It) {
1257 Opc = PPC32InstrInfo::invertPPCBranchOpcode(Opc);
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001258 BuildMI(BB, PPC::COND_BRANCH, 4).addReg(CCReg).addImm(Opc)
Nate Begemancd08e4c2005-04-09 20:09:12 +00001259 .addMBB(Fallthrough).addMBB(Dest);
1260 }
1261 }
1262 } else {
Nate Begeman439009c2005-06-15 18:22:43 +00001263 // If the fallthrough path is off the end of the function, which would be
1264 // undefined behavior, set it to be the same as the current block because
1265 // we have nothing better to set it to, and leaving it alone will cause the
1266 // PowerPC Branch Selection pass to crash.
1267 if (It == BB->getParent()->end()) It = Dest;
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001268 BuildMI(BB, PPC::COND_BRANCH, 4).addReg(CCReg).addImm(Opc)
Nate Begeman27499e32005-04-10 01:48:29 +00001269 .addMBB(Dest).addMBB(It);
Nate Begemancd08e4c2005-04-09 20:09:12 +00001270 }
Nate Begemana9795f82005-03-24 04:41:43 +00001271 return;
1272}
1273
1274unsigned ISel::SelectExprFP(SDOperand N, unsigned Result)
1275{
1276 unsigned Tmp1, Tmp2, Tmp3;
1277 unsigned Opc = 0;
1278 SDNode *Node = N.Val;
1279 MVT::ValueType DestType = N.getValueType();
1280 unsigned opcode = N.getOpcode();
1281
1282 switch (opcode) {
1283 default:
1284 Node->dump();
1285 assert(0 && "Node not handled!\n");
1286
Nate Begeman23afcfb2005-03-29 22:48:55 +00001287 case ISD::SELECT: {
Nate Begeman3e897162005-03-31 23:55:40 +00001288 // Attempt to generate FSEL. We can do this whenever we have an FP result,
1289 // and an FP comparison in the SetCC node.
1290 SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(N.getOperand(0).Val);
1291 if (SetCC && N.getOperand(0).getOpcode() == ISD::SETCC &&
1292 !MVT::isInteger(SetCC->getOperand(0).getValueType()) &&
1293 SetCC->getCondition() != ISD::SETEQ &&
1294 SetCC->getCondition() != ISD::SETNE) {
1295 MVT::ValueType VT = SetCC->getOperand(0).getValueType();
Nate Begeman3e897162005-03-31 23:55:40 +00001296 unsigned TV = SelectExpr(N.getOperand(1)); // Use if TRUE
1297 unsigned FV = SelectExpr(N.getOperand(2)); // Use if FALSE
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001298
Nate Begeman3e897162005-03-31 23:55:40 +00001299 ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(1));
1300 if (CN && (CN->isExactlyValue(-0.0) || CN->isExactlyValue(0.0))) {
1301 switch(SetCC->getCondition()) {
1302 default: assert(0 && "Invalid FSEL condition"); abort();
1303 case ISD::SETULT:
1304 case ISD::SETLT:
Nate Begemanaf4ab1b2005-04-09 09:33:07 +00001305 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
Nate Begeman3e897162005-03-31 23:55:40 +00001306 case ISD::SETUGE:
1307 case ISD::SETGE:
Nate Begemanaf4ab1b2005-04-09 09:33:07 +00001308 Tmp1 = SelectExpr(SetCC->getOperand(0)); // Val to compare against
Nate Begeman3e897162005-03-31 23:55:40 +00001309 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp1).addReg(TV).addReg(FV);
1310 return Result;
1311 case ISD::SETUGT:
Nate Begemanaf4ab1b2005-04-09 09:33:07 +00001312 case ISD::SETGT:
1313 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
Nate Begeman3e897162005-03-31 23:55:40 +00001314 case ISD::SETULE:
1315 case ISD::SETLE: {
Nate Begemanaf4ab1b2005-04-09 09:33:07 +00001316 if (SetCC->getOperand(0).getOpcode() == ISD::FNEG) {
1317 Tmp2 = SelectExpr(SetCC->getOperand(0).getOperand(0));
1318 } else {
1319 Tmp2 = MakeReg(VT);
1320 Tmp1 = SelectExpr(SetCC->getOperand(0)); // Val to compare against
1321 BuildMI(BB, PPC::FNEG, 1, Tmp2).addReg(Tmp1);
1322 }
Nate Begeman3e897162005-03-31 23:55:40 +00001323 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp2).addReg(TV).addReg(FV);
1324 return Result;
1325 }
1326 }
1327 } else {
1328 Opc = (MVT::f64 == VT) ? PPC::FSUB : PPC::FSUBS;
Nate Begemanaf4ab1b2005-04-09 09:33:07 +00001329 Tmp1 = SelectExpr(SetCC->getOperand(0)); // Val to compare against
Nate Begeman3e897162005-03-31 23:55:40 +00001330 Tmp2 = SelectExpr(SetCC->getOperand(1));
1331 Tmp3 = MakeReg(VT);
1332 switch(SetCC->getCondition()) {
1333 default: assert(0 && "Invalid FSEL condition"); abort();
1334 case ISD::SETULT:
1335 case ISD::SETLT:
1336 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
1337 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(FV).addReg(TV);
1338 return Result;
1339 case ISD::SETUGE:
1340 case ISD::SETGE:
1341 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
1342 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(TV).addReg(FV);
1343 return Result;
1344 case ISD::SETUGT:
1345 case ISD::SETGT:
1346 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp2).addReg(Tmp1);
1347 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(FV).addReg(TV);
1348 return Result;
1349 case ISD::SETULE:
1350 case ISD::SETLE:
1351 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp2).addReg(Tmp1);
1352 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(TV).addReg(FV);
1353 return Result;
1354 }
1355 }
1356 assert(0 && "Should never get here");
1357 return 0;
1358 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001359
Nate Begeman1cbf3ab2005-04-18 07:48:09 +00001360 bool Inv;
Nate Begeman31318e42005-04-01 07:21:30 +00001361 unsigned TrueValue = SelectExpr(N.getOperand(1)); //Use if TRUE
1362 unsigned FalseValue = SelectExpr(N.getOperand(2)); //Use if FALSE
Nate Begeman1cbf3ab2005-04-18 07:48:09 +00001363 unsigned CCReg = SelectCC(N.getOperand(0), Opc, Inv, Tmp3);
Nate Begeman31318e42005-04-01 07:21:30 +00001364
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001365 // Create an iterator with which to insert the MBB for copying the false
Nate Begeman23afcfb2005-03-29 22:48:55 +00001366 // value and the MBB to hold the PHI instruction for this SetCC.
1367 MachineBasicBlock *thisMBB = BB;
1368 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1369 ilist<MachineBasicBlock>::iterator It = BB;
1370 ++It;
1371
1372 // thisMBB:
1373 // ...
1374 // TrueVal = ...
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001375 // cmpTY ccX, r1, r2
Nate Begeman23afcfb2005-03-29 22:48:55 +00001376 // bCC copy1MBB
1377 // fallthrough --> copy0MBB
Nate Begeman23afcfb2005-03-29 22:48:55 +00001378 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
1379 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001380 BuildMI(BB, Opc, 2).addReg(CCReg).addMBB(sinkMBB);
Nate Begeman23afcfb2005-03-29 22:48:55 +00001381 MachineFunction *F = BB->getParent();
1382 F->getBasicBlockList().insert(It, copy0MBB);
1383 F->getBasicBlockList().insert(It, sinkMBB);
1384 // Update machine-CFG edges
1385 BB->addSuccessor(copy0MBB);
1386 BB->addSuccessor(sinkMBB);
1387
1388 // copy0MBB:
1389 // %FalseValue = ...
1390 // # fallthrough to sinkMBB
1391 BB = copy0MBB;
Nate Begeman23afcfb2005-03-29 22:48:55 +00001392 // Update machine-CFG edges
1393 BB->addSuccessor(sinkMBB);
1394
1395 // sinkMBB:
1396 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1397 // ...
1398 BB = sinkMBB;
1399 BuildMI(BB, PPC::PHI, 4, Result).addReg(FalseValue)
1400 .addMBB(copy0MBB).addReg(TrueValue).addMBB(thisMBB);
1401 return Result;
1402 }
Nate Begeman27eeb002005-04-02 05:59:34 +00001403
1404 case ISD::FNEG:
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001405 if (!NoExcessFPPrecision &&
Nate Begeman93075ec2005-04-04 23:40:36 +00001406 ISD::ADD == N.getOperand(0).getOpcode() &&
1407 N.getOperand(0).Val->hasOneUse() &&
1408 ISD::MUL == N.getOperand(0).getOperand(0).getOpcode() &&
1409 N.getOperand(0).getOperand(0).Val->hasOneUse()) {
Nate Begeman80196b12005-04-05 00:15:08 +00001410 ++FusedFP; // Statistic
Nate Begeman93075ec2005-04-04 23:40:36 +00001411 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(0));
1412 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(1));
1413 Tmp3 = SelectExpr(N.getOperand(0).getOperand(1));
1414 Opc = DestType == MVT::f64 ? PPC::FNMADD : PPC::FNMADDS;
1415 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001416 } else if (!NoExcessFPPrecision &&
Nate Begemane88aa5b2005-04-09 03:05:51 +00001417 ISD::ADD == N.getOperand(0).getOpcode() &&
Nate Begeman93075ec2005-04-04 23:40:36 +00001418 N.getOperand(0).Val->hasOneUse() &&
Nate Begemane88aa5b2005-04-09 03:05:51 +00001419 ISD::MUL == N.getOperand(0).getOperand(1).getOpcode() &&
1420 N.getOperand(0).getOperand(1).Val->hasOneUse()) {
Nate Begeman80196b12005-04-05 00:15:08 +00001421 ++FusedFP; // Statistic
Nate Begemane88aa5b2005-04-09 03:05:51 +00001422 Tmp1 = SelectExpr(N.getOperand(0).getOperand(1).getOperand(0));
1423 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1).getOperand(1));
1424 Tmp3 = SelectExpr(N.getOperand(0).getOperand(0));
1425 Opc = DestType == MVT::f64 ? PPC::FNMADD : PPC::FNMADDS;
Nate Begeman93075ec2005-04-04 23:40:36 +00001426 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1427 } else if (ISD::FABS == N.getOperand(0).getOpcode()) {
Nate Begeman27eeb002005-04-02 05:59:34 +00001428 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1429 BuildMI(BB, PPC::FNABS, 1, Result).addReg(Tmp1);
1430 } else {
1431 Tmp1 = SelectExpr(N.getOperand(0));
1432 BuildMI(BB, PPC::FNEG, 1, Result).addReg(Tmp1);
1433 }
1434 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001435
Nate Begeman27eeb002005-04-02 05:59:34 +00001436 case ISD::FABS:
1437 Tmp1 = SelectExpr(N.getOperand(0));
1438 BuildMI(BB, PPC::FABS, 1, Result).addReg(Tmp1);
1439 return Result;
1440
Nate Begemana9795f82005-03-24 04:41:43 +00001441 case ISD::FP_ROUND:
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001442 assert (DestType == MVT::f32 &&
1443 N.getOperand(0).getValueType() == MVT::f64 &&
Nate Begemana9795f82005-03-24 04:41:43 +00001444 "only f64 to f32 conversion supported here");
1445 Tmp1 = SelectExpr(N.getOperand(0));
1446 BuildMI(BB, PPC::FRSP, 1, Result).addReg(Tmp1);
1447 return Result;
1448
1449 case ISD::FP_EXTEND:
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001450 assert (DestType == MVT::f64 &&
1451 N.getOperand(0).getValueType() == MVT::f32 &&
Nate Begemana9795f82005-03-24 04:41:43 +00001452 "only f32 to f64 conversion supported here");
1453 Tmp1 = SelectExpr(N.getOperand(0));
1454 BuildMI(BB, PPC::FMR, 1, Result).addReg(Tmp1);
1455 return Result;
1456
1457 case ISD::CopyFromReg:
Nate Begemanf2622612005-03-26 02:17:46 +00001458 if (Result == 1)
1459 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1460 Tmp1 = dyn_cast<RegSDNode>(Node)->getReg();
1461 BuildMI(BB, PPC::FMR, 1, Result).addReg(Tmp1);
1462 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001463
Nate Begeman6d369cc2005-04-01 01:08:07 +00001464 case ISD::ConstantFP: {
Nate Begeman6d369cc2005-04-01 01:08:07 +00001465 ConstantFPSDNode *CN = cast<ConstantFPSDNode>(N);
Nate Begeman6b559972005-04-01 02:59:27 +00001466 Result = getConstDouble(CN->getValue(), Result);
Nate Begeman6d369cc2005-04-01 01:08:07 +00001467 return Result;
1468 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001469
Nate Begemana9795f82005-03-24 04:41:43 +00001470 case ISD::ADD:
Nate Begeman93075ec2005-04-04 23:40:36 +00001471 if (!NoExcessFPPrecision && N.getOperand(0).getOpcode() == ISD::MUL &&
1472 N.getOperand(0).Val->hasOneUse()) {
1473 ++FusedFP; // Statistic
1474 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1475 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1476 Tmp3 = SelectExpr(N.getOperand(1));
1477 Opc = DestType == MVT::f64 ? PPC::FMADD : PPC::FMADDS;
1478 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1479 return Result;
1480 }
Nate Begemane88aa5b2005-04-09 03:05:51 +00001481 if (!NoExcessFPPrecision && N.getOperand(1).getOpcode() == ISD::MUL &&
1482 N.getOperand(1).Val->hasOneUse()) {
1483 ++FusedFP; // Statistic
1484 Tmp1 = SelectExpr(N.getOperand(1).getOperand(0));
1485 Tmp2 = SelectExpr(N.getOperand(1).getOperand(1));
1486 Tmp3 = SelectExpr(N.getOperand(0));
1487 Opc = DestType == MVT::f64 ? PPC::FMADD : PPC::FMADDS;
1488 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1489 return Result;
1490 }
Nate Begeman93075ec2005-04-04 23:40:36 +00001491 Opc = DestType == MVT::f64 ? PPC::FADD : PPC::FADDS;
1492 Tmp1 = SelectExpr(N.getOperand(0));
1493 Tmp2 = SelectExpr(N.getOperand(1));
1494 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1495 return Result;
1496
Nate Begemana9795f82005-03-24 04:41:43 +00001497 case ISD::SUB:
Nate Begeman93075ec2005-04-04 23:40:36 +00001498 if (!NoExcessFPPrecision && N.getOperand(0).getOpcode() == ISD::MUL &&
1499 N.getOperand(0).Val->hasOneUse()) {
1500 ++FusedFP; // Statistic
1501 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1502 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1503 Tmp3 = SelectExpr(N.getOperand(1));
1504 Opc = DestType == MVT::f64 ? PPC::FMSUB : PPC::FMSUBS;
1505 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1506 return Result;
1507 }
Nate Begemane88aa5b2005-04-09 03:05:51 +00001508 if (!NoExcessFPPrecision && N.getOperand(1).getOpcode() == ISD::MUL &&
1509 N.getOperand(1).Val->hasOneUse()) {
1510 ++FusedFP; // Statistic
1511 Tmp1 = SelectExpr(N.getOperand(1).getOperand(0));
1512 Tmp2 = SelectExpr(N.getOperand(1).getOperand(1));
1513 Tmp3 = SelectExpr(N.getOperand(0));
1514 Opc = DestType == MVT::f64 ? PPC::FNMSUB : PPC::FNMSUBS;
1515 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1516 return Result;
1517 }
Nate Begeman93075ec2005-04-04 23:40:36 +00001518 Opc = DestType == MVT::f64 ? PPC::FSUB : PPC::FSUBS;
1519 Tmp1 = SelectExpr(N.getOperand(0));
1520 Tmp2 = SelectExpr(N.getOperand(1));
1521 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1522 return Result;
1523
1524 case ISD::MUL:
Nate Begemana9795f82005-03-24 04:41:43 +00001525 case ISD::SDIV:
1526 switch( opcode ) {
1527 case ISD::MUL: Opc = DestType == MVT::f64 ? PPC::FMUL : PPC::FMULS; break;
Nate Begemana9795f82005-03-24 04:41:43 +00001528 case ISD::SDIV: Opc = DestType == MVT::f64 ? PPC::FDIV : PPC::FDIVS; break;
1529 };
Nate Begemana9795f82005-03-24 04:41:43 +00001530 Tmp1 = SelectExpr(N.getOperand(0));
1531 Tmp2 = SelectExpr(N.getOperand(1));
1532 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1533 return Result;
1534
Nate Begemana9795f82005-03-24 04:41:43 +00001535 case ISD::UINT_TO_FP:
Nate Begemanfdcf3412005-03-30 19:38:35 +00001536 case ISD::SINT_TO_FP: {
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001537 assert (N.getOperand(0).getValueType() == MVT::i32
Nate Begemanfdcf3412005-03-30 19:38:35 +00001538 && "int to float must operate on i32");
1539 bool IsUnsigned = (ISD::UINT_TO_FP == opcode);
1540 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
1541 Tmp2 = MakeReg(MVT::f64); // temp reg to load the integer value into
1542 Tmp3 = MakeReg(MVT::i32); // temp reg to hold the conversion constant
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001543
Nate Begemanfdcf3412005-03-30 19:38:35 +00001544 int FrameIdx = BB->getParent()->getFrameInfo()->CreateStackObject(8, 8);
1545 MachineConstantPool *CP = BB->getParent()->getConstantPool();
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001546
Nate Begemanfdcf3412005-03-30 19:38:35 +00001547 if (IsUnsigned) {
Nate Begeman709c8062005-04-10 06:06:10 +00001548 unsigned ConstF = getConstDouble(0x1.000000p52);
Nate Begemanfdcf3412005-03-30 19:38:35 +00001549 // Store the hi & low halves of the fp value, currently in int regs
1550 BuildMI(BB, PPC::LIS, 1, Tmp3).addSImm(0x4330);
1551 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(Tmp3), FrameIdx);
1552 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(Tmp1), FrameIdx, 4);
1553 addFrameReference(BuildMI(BB, PPC::LFD, 2, Tmp2), FrameIdx);
1554 // Generate the return value with a subtract
1555 BuildMI(BB, PPC::FSUB, 2, Result).addReg(Tmp2).addReg(ConstF);
1556 } else {
Nate Begeman709c8062005-04-10 06:06:10 +00001557 unsigned ConstF = getConstDouble(0x1.000008p52);
Nate Begemanfdcf3412005-03-30 19:38:35 +00001558 unsigned TmpL = MakeReg(MVT::i32);
Nate Begemanfdcf3412005-03-30 19:38:35 +00001559 // Store the hi & low halves of the fp value, currently in int regs
1560 BuildMI(BB, PPC::LIS, 1, Tmp3).addSImm(0x4330);
1561 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(Tmp3), FrameIdx);
1562 BuildMI(BB, PPC::XORIS, 2, TmpL).addReg(Tmp1).addImm(0x8000);
1563 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(TmpL), FrameIdx, 4);
1564 addFrameReference(BuildMI(BB, PPC::LFD, 2, Tmp2), FrameIdx);
1565 // Generate the return value with a subtract
1566 BuildMI(BB, PPC::FSUB, 2, Result).addReg(Tmp2).addReg(ConstF);
1567 }
1568 return Result;
1569 }
Nate Begemana9795f82005-03-24 04:41:43 +00001570 }
Nate Begeman6b559972005-04-01 02:59:27 +00001571 assert(0 && "Should never get here");
Nate Begemana9795f82005-03-24 04:41:43 +00001572 return 0;
1573}
1574
Nate Begemanc7bd4822005-04-11 06:34:10 +00001575unsigned ISel::SelectExpr(SDOperand N, bool Recording) {
Nate Begemana9795f82005-03-24 04:41:43 +00001576 unsigned Result;
1577 unsigned Tmp1, Tmp2, Tmp3;
1578 unsigned Opc = 0;
1579 unsigned opcode = N.getOpcode();
1580
1581 SDNode *Node = N.Val;
1582 MVT::ValueType DestType = N.getValueType();
1583
Nate Begemana43b1762005-06-14 03:55:23 +00001584 if (Node->getOpcode() == ISD::CopyFromReg &&
1585 MRegisterInfo::isVirtualRegister(cast<RegSDNode>(Node)->getReg()))
1586 // Just use the specified register as our input.
1587 return cast<RegSDNode>(Node)->getReg();
1588
Nate Begemana9795f82005-03-24 04:41:43 +00001589 unsigned &Reg = ExprMap[N];
1590 if (Reg) return Reg;
1591
Nate Begeman27eeb002005-04-02 05:59:34 +00001592 switch (N.getOpcode()) {
1593 default:
Nate Begemana9795f82005-03-24 04:41:43 +00001594 Reg = Result = (N.getValueType() != MVT::Other) ?
Nate Begeman27eeb002005-04-02 05:59:34 +00001595 MakeReg(N.getValueType()) : 1;
1596 break;
Chris Lattnerb5d8e6e2005-05-13 20:29:26 +00001597 case ISD::TAILCALL:
Nate Begeman27eeb002005-04-02 05:59:34 +00001598 case ISD::CALL:
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001599 // If this is a call instruction, make sure to prepare ALL of the result
1600 // values as well as the chain.
Nate Begeman27eeb002005-04-02 05:59:34 +00001601 if (Node->getNumValues() == 1)
1602 Reg = Result = 1; // Void call, just a chain.
1603 else {
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001604 Result = MakeReg(Node->getValueType(0));
1605 ExprMap[N.getValue(0)] = Result;
Nate Begeman27eeb002005-04-02 05:59:34 +00001606 for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001607 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
Nate Begeman27eeb002005-04-02 05:59:34 +00001608 ExprMap[SDOperand(Node, Node->getNumValues()-1)] = 1;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001609 }
Nate Begeman27eeb002005-04-02 05:59:34 +00001610 break;
1611 case ISD::ADD_PARTS:
1612 case ISD::SUB_PARTS:
1613 case ISD::SHL_PARTS:
1614 case ISD::SRL_PARTS:
1615 case ISD::SRA_PARTS:
1616 Result = MakeReg(Node->getValueType(0));
1617 ExprMap[N.getValue(0)] = Result;
1618 for (unsigned i = 1, e = N.Val->getNumValues(); i != e; ++i)
1619 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
1620 break;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001621 }
1622
Nate Begemane5846682005-04-04 06:52:38 +00001623 if (ISD::CopyFromReg == opcode)
1624 DestType = N.getValue(0).getValueType();
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001625
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001626 if (DestType == MVT::f64 || DestType == MVT::f32)
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001627 if (ISD::LOAD != opcode && ISD::EXTLOAD != opcode &&
Chris Lattnerb5d8e6e2005-05-13 20:29:26 +00001628 ISD::UNDEF != opcode && ISD::CALL != opcode && ISD::TAILCALL != opcode)
Nate Begeman74d73452005-03-31 00:15:26 +00001629 return SelectExprFP(N, Result);
Nate Begemana9795f82005-03-24 04:41:43 +00001630
1631 switch (opcode) {
1632 default:
1633 Node->dump();
1634 assert(0 && "Node not handled!\n");
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001635 case ISD::UNDEF:
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001636 BuildMI(BB, PPC::IMPLICIT_DEF, 0, Result);
1637 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001638 case ISD::DYNAMIC_STACKALLOC:
Nate Begeman5e966612005-03-24 06:28:42 +00001639 // Generate both result values. FIXME: Need a better commment here?
1640 if (Result != 1)
1641 ExprMap[N.getValue(1)] = 1;
1642 else
1643 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1644
1645 // FIXME: We are currently ignoring the requested alignment for handling
1646 // greater than the stack alignment. This will need to be revisited at some
1647 // point. Align = N.getOperand(2);
1648 if (!isa<ConstantSDNode>(N.getOperand(2)) ||
1649 cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) {
1650 std::cerr << "Cannot allocate stack object with greater alignment than"
1651 << " the stack alignment yet!";
1652 abort();
1653 }
1654 Select(N.getOperand(0));
1655 Tmp1 = SelectExpr(N.getOperand(1));
1656 // Subtract size from stack pointer, thereby allocating some space.
1657 BuildMI(BB, PPC::SUBF, 2, PPC::R1).addReg(Tmp1).addReg(PPC::R1);
1658 // Put a pointer to the space into the result register by copying the SP
1659 BuildMI(BB, PPC::OR, 2, Result).addReg(PPC::R1).addReg(PPC::R1);
1660 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001661
1662 case ISD::ConstantPool:
Nate Begemanca12a2b2005-03-28 22:28:37 +00001663 Tmp1 = cast<ConstantPoolSDNode>(N)->getIndex();
1664 Tmp2 = MakeReg(MVT::i32);
1665 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp2).addReg(getGlobalBaseReg())
1666 .addConstantPoolIndex(Tmp1);
1667 BuildMI(BB, PPC::LA, 2, Result).addReg(Tmp2).addConstantPoolIndex(Tmp1);
1668 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001669
1670 case ISD::FrameIndex:
Nate Begemanf3d08f32005-03-29 00:03:27 +00001671 Tmp1 = cast<FrameIndexSDNode>(N)->getIndex();
Nate Begeman58f718c2005-03-30 02:23:08 +00001672 addFrameReference(BuildMI(BB, PPC::ADDI, 2, Result), (int)Tmp1, 0, false);
Nate Begemanf3d08f32005-03-29 00:03:27 +00001673 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001674
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001675 case ISD::GlobalAddress: {
1676 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
Nate Begemanca12a2b2005-03-28 22:28:37 +00001677 Tmp1 = MakeReg(MVT::i32);
Nate Begemanc7b09f12005-03-25 08:34:25 +00001678 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp1).addReg(getGlobalBaseReg())
1679 .addGlobalAddress(GV);
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001680 if (GV->hasWeakLinkage() || GV->isExternal()) {
1681 BuildMI(BB, PPC::LWZ, 2, Result).addGlobalAddress(GV).addReg(Tmp1);
1682 } else {
1683 BuildMI(BB, PPC::LA, 2, Result).addReg(Tmp1).addGlobalAddress(GV);
1684 }
1685 return Result;
1686 }
1687
Nate Begeman5e966612005-03-24 06:28:42 +00001688 case ISD::LOAD:
Nate Begemana9795f82005-03-24 04:41:43 +00001689 case ISD::EXTLOAD:
1690 case ISD::ZEXTLOAD:
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001691 case ISD::SEXTLOAD: {
Nate Begeman9db505c2005-03-28 19:36:43 +00001692 MVT::ValueType TypeBeingLoaded = (ISD::LOAD == opcode) ?
Chris Lattnerbce81ae2005-07-10 01:56:13 +00001693 Node->getValueType(0) : cast<VTSDNode>(Node->getOperand(3))->getVT();
Nate Begeman74d73452005-03-31 00:15:26 +00001694 bool sext = (ISD::SEXTLOAD == opcode);
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001695
Nate Begeman5e966612005-03-24 06:28:42 +00001696 // Make sure we generate both values.
1697 if (Result != 1)
1698 ExprMap[N.getValue(1)] = 1; // Generate the token
1699 else
1700 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1701
1702 SDOperand Chain = N.getOperand(0);
1703 SDOperand Address = N.getOperand(1);
1704 Select(Chain);
1705
Nate Begeman9db505c2005-03-28 19:36:43 +00001706 switch (TypeBeingLoaded) {
Nate Begeman74d73452005-03-31 00:15:26 +00001707 default: Node->dump(); assert(0 && "Cannot load this type!");
Nate Begeman9db505c2005-03-28 19:36:43 +00001708 case MVT::i1: Opc = PPC::LBZ; break;
1709 case MVT::i8: Opc = PPC::LBZ; break;
1710 case MVT::i16: Opc = sext ? PPC::LHA : PPC::LHZ; break;
1711 case MVT::i32: Opc = PPC::LWZ; break;
Nate Begeman74d73452005-03-31 00:15:26 +00001712 case MVT::f32: Opc = PPC::LFS; break;
1713 case MVT::f64: Opc = PPC::LFD; break;
Nate Begeman5e966612005-03-24 06:28:42 +00001714 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001715
Nate Begeman74d73452005-03-31 00:15:26 +00001716 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Address)) {
1717 Tmp1 = MakeReg(MVT::i32);
1718 int CPI = CP->getIndex();
1719 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp1).addReg(getGlobalBaseReg())
1720 .addConstantPoolIndex(CPI);
1721 BuildMI(BB, Opc, 2, Result).addConstantPoolIndex(CPI).addReg(Tmp1);
Nate Begeman9db505c2005-03-28 19:36:43 +00001722 }
Nate Begeman74d73452005-03-31 00:15:26 +00001723 else if(Address.getOpcode() == ISD::FrameIndex) {
Nate Begeman58f718c2005-03-30 02:23:08 +00001724 Tmp1 = cast<FrameIndexSDNode>(Address)->getIndex();
1725 addFrameReference(BuildMI(BB, Opc, 2, Result), (int)Tmp1);
Nate Begeman5e966612005-03-24 06:28:42 +00001726 } else {
1727 int offset;
Nate Begeman04730362005-04-01 04:45:11 +00001728 bool idx = SelectAddr(Address, Tmp1, offset);
1729 if (idx) {
1730 Opc = IndexedOpForOp(Opc);
1731 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(offset);
1732 } else {
1733 BuildMI(BB, Opc, 2, Result).addSImm(offset).addReg(Tmp1);
1734 }
Nate Begeman5e966612005-03-24 06:28:42 +00001735 }
1736 return Result;
1737 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001738
Chris Lattnerb5d8e6e2005-05-13 20:29:26 +00001739 case ISD::TAILCALL:
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001740 case ISD::CALL: {
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001741 unsigned GPR_idx = 0, FPR_idx = 0;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001742 static const unsigned GPR[] = {
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001743 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
1744 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
1745 };
1746 static const unsigned FPR[] = {
1747 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
1748 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
1749 };
1750
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001751 // Lower the chain for this call.
1752 Select(N.getOperand(0));
1753 ExprMap[N.getValue(Node->getNumValues()-1)] = 1;
Nate Begeman74d73452005-03-31 00:15:26 +00001754
Nate Begemand860aa62005-04-04 22:17:48 +00001755 MachineInstr *CallMI;
1756 // Emit the correct call instruction based on the type of symbol called.
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001757 if (GlobalAddressSDNode *GASD =
Nate Begemand860aa62005-04-04 22:17:48 +00001758 dyn_cast<GlobalAddressSDNode>(N.getOperand(1))) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001759 CallMI = BuildMI(PPC::CALLpcrel, 1).addGlobalAddress(GASD->getGlobal(),
Nate Begemand860aa62005-04-04 22:17:48 +00001760 true);
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001761 } else if (ExternalSymbolSDNode *ESSDN =
Nate Begemand860aa62005-04-04 22:17:48 +00001762 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1))) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001763 CallMI = BuildMI(PPC::CALLpcrel, 1).addExternalSymbol(ESSDN->getSymbol(),
Nate Begemand860aa62005-04-04 22:17:48 +00001764 true);
1765 } else {
1766 Tmp1 = SelectExpr(N.getOperand(1));
1767 BuildMI(BB, PPC::OR, 2, PPC::R12).addReg(Tmp1).addReg(Tmp1);
1768 BuildMI(BB, PPC::MTCTR, 1).addReg(PPC::R12);
1769 CallMI = BuildMI(PPC::CALLindirect, 3).addImm(20).addImm(0)
1770 .addReg(PPC::R12);
1771 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001772
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001773 // Load the register args to virtual regs
1774 std::vector<unsigned> ArgVR;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001775 for(int i = 2, e = Node->getNumOperands(); i < e; ++i)
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001776 ArgVR.push_back(SelectExpr(N.getOperand(i)));
1777
1778 // Copy the virtual registers into the appropriate argument register
1779 for(int i = 0, e = ArgVR.size(); i < e; ++i) {
1780 switch(N.getOperand(i+2).getValueType()) {
1781 default: Node->dump(); assert(0 && "Unknown value type for call");
1782 case MVT::i1:
1783 case MVT::i8:
1784 case MVT::i16:
1785 case MVT::i32:
1786 assert(GPR_idx < 8 && "Too many int args");
Nate Begemand860aa62005-04-04 22:17:48 +00001787 if (N.getOperand(i+2).getOpcode() != ISD::UNDEF) {
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001788 BuildMI(BB, PPC::OR,2,GPR[GPR_idx]).addReg(ArgVR[i]).addReg(ArgVR[i]);
Nate Begemand860aa62005-04-04 22:17:48 +00001789 CallMI->addRegOperand(GPR[GPR_idx], MachineOperand::Use);
1790 }
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001791 ++GPR_idx;
1792 break;
1793 case MVT::f64:
1794 case MVT::f32:
1795 assert(FPR_idx < 13 && "Too many fp args");
1796 BuildMI(BB, PPC::FMR, 1, FPR[FPR_idx]).addReg(ArgVR[i]);
Nate Begemand860aa62005-04-04 22:17:48 +00001797 CallMI->addRegOperand(FPR[FPR_idx], MachineOperand::Use);
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001798 ++FPR_idx;
1799 break;
1800 }
1801 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001802
Nate Begemand860aa62005-04-04 22:17:48 +00001803 // Put the call instruction in the correct place in the MachineBasicBlock
1804 BB->push_back(CallMI);
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001805
1806 switch (Node->getValueType(0)) {
1807 default: assert(0 && "Unknown value type for call result!");
1808 case MVT::Other: return 1;
1809 case MVT::i1:
1810 case MVT::i8:
1811 case MVT::i16:
1812 case MVT::i32:
Nate Begemane5846682005-04-04 06:52:38 +00001813 if (Node->getValueType(1) == MVT::i32) {
1814 BuildMI(BB, PPC::OR, 2, Result+1).addReg(PPC::R3).addReg(PPC::R3);
1815 BuildMI(BB, PPC::OR, 2, Result).addReg(PPC::R4).addReg(PPC::R4);
1816 } else {
1817 BuildMI(BB, PPC::OR, 2, Result).addReg(PPC::R3).addReg(PPC::R3);
1818 }
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001819 break;
1820 case MVT::f32:
1821 case MVT::f64:
1822 BuildMI(BB, PPC::FMR, 1, Result).addReg(PPC::F1);
1823 break;
1824 }
1825 return Result+N.ResNo;
1826 }
Nate Begemana9795f82005-03-24 04:41:43 +00001827
1828 case ISD::SIGN_EXTEND:
1829 case ISD::SIGN_EXTEND_INREG:
1830 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattnerbce81ae2005-07-10 01:56:13 +00001831 switch(cast<VTSDNode>(Node->getOperand(1))->getVT()) {
Nate Begeman9db505c2005-03-28 19:36:43 +00001832 default: Node->dump(); assert(0 && "Unhandled SIGN_EXTEND type"); break;
Nate Begemanc7bd4822005-04-11 06:34:10 +00001833 case MVT::i16:
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001834 BuildMI(BB, PPC::EXTSH, 1, Result).addReg(Tmp1);
Nate Begeman9db505c2005-03-28 19:36:43 +00001835 break;
Nate Begemanc7bd4822005-04-11 06:34:10 +00001836 case MVT::i8:
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001837 BuildMI(BB, PPC::EXTSB, 1, Result).addReg(Tmp1);
Nate Begeman9db505c2005-03-28 19:36:43 +00001838 break;
Nate Begeman74747862005-03-29 22:24:51 +00001839 case MVT::i1:
1840 BuildMI(BB, PPC::SUBFIC, 2, Result).addReg(Tmp1).addSImm(0);
1841 break;
Nate Begeman9db505c2005-03-28 19:36:43 +00001842 }
Nate Begemana9795f82005-03-24 04:41:43 +00001843 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001844
Nate Begemana9795f82005-03-24 04:41:43 +00001845 case ISD::CopyFromReg:
1846 if (Result == 1)
1847 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1848 Tmp1 = dyn_cast<RegSDNode>(Node)->getReg();
1849 BuildMI(BB, PPC::OR, 2, Result).addReg(Tmp1).addReg(Tmp1);
1850 return Result;
1851
1852 case ISD::SHL:
Nate Begeman5e966612005-03-24 06:28:42 +00001853 Tmp1 = SelectExpr(N.getOperand(0));
1854 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1855 Tmp2 = CN->getValue() & 0x1F;
Nate Begeman33162522005-03-29 21:54:38 +00001856 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(Tmp2).addImm(0)
Nate Begeman5e966612005-03-24 06:28:42 +00001857 .addImm(31-Tmp2);
1858 } else {
Nate Begeman3664cef2005-04-13 22:14:14 +00001859 Tmp2 = FoldIfWideZeroExtend(N.getOperand(1));
Nate Begeman5e966612005-03-24 06:28:42 +00001860 BuildMI(BB, PPC::SLW, 2, Result).addReg(Tmp1).addReg(Tmp2);
1861 }
1862 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001863
Nate Begeman5e966612005-03-24 06:28:42 +00001864 case ISD::SRL:
1865 Tmp1 = SelectExpr(N.getOperand(0));
1866 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1867 Tmp2 = CN->getValue() & 0x1F;
Nate Begeman33162522005-03-29 21:54:38 +00001868 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(32-Tmp2)
Nate Begeman5e966612005-03-24 06:28:42 +00001869 .addImm(Tmp2).addImm(31);
1870 } else {
Nate Begeman3664cef2005-04-13 22:14:14 +00001871 Tmp2 = FoldIfWideZeroExtend(N.getOperand(1));
Nate Begeman5e966612005-03-24 06:28:42 +00001872 BuildMI(BB, PPC::SRW, 2, Result).addReg(Tmp1).addReg(Tmp2);
1873 }
1874 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001875
Nate Begeman5e966612005-03-24 06:28:42 +00001876 case ISD::SRA:
1877 Tmp1 = SelectExpr(N.getOperand(0));
1878 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1879 Tmp2 = CN->getValue() & 0x1F;
1880 BuildMI(BB, PPC::SRAWI, 2, Result).addReg(Tmp1).addImm(Tmp2);
1881 } else {
Nate Begeman3664cef2005-04-13 22:14:14 +00001882 Tmp2 = FoldIfWideZeroExtend(N.getOperand(1));
Nate Begeman5e966612005-03-24 06:28:42 +00001883 BuildMI(BB, PPC::SRAW, 2, Result).addReg(Tmp1).addReg(Tmp2);
1884 }
1885 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001886
Nate Begemand7c4a4a2005-05-11 23:43:56 +00001887 case ISD::CTLZ:
1888 Tmp1 = SelectExpr(N.getOperand(0));
1889 BuildMI(BB, PPC::CNTLZW, 1, Result).addReg(Tmp1);
1890 return Result;
1891
Nate Begemana9795f82005-03-24 04:41:43 +00001892 case ISD::ADD:
1893 assert (DestType == MVT::i32 && "Only do arithmetic on i32s!");
1894 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman439b4442005-04-05 04:22:58 +00001895 switch(getImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
Nate Begemana9795f82005-03-24 04:41:43 +00001896 default: assert(0 && "unhandled result code");
1897 case 0: // No immediate
1898 Tmp2 = SelectExpr(N.getOperand(1));
1899 BuildMI(BB, PPC::ADD, 2, Result).addReg(Tmp1).addReg(Tmp2);
1900 break;
1901 case 1: // Low immediate
1902 BuildMI(BB, PPC::ADDI, 2, Result).addReg(Tmp1).addSImm(Tmp2);
1903 break;
1904 case 2: // Shifted immediate
1905 BuildMI(BB, PPC::ADDIS, 2, Result).addReg(Tmp1).addSImm(Tmp2);
1906 break;
1907 }
1908 return Result;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001909
Nate Begemana9795f82005-03-24 04:41:43 +00001910 case ISD::AND:
Nate Begeman1cbf3ab2005-04-18 07:48:09 +00001911 if (PPCCRopts) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001912 if (N.getOperand(0).getOpcode() == ISD::SETCC ||
Nate Begeman1cbf3ab2005-04-18 07:48:09 +00001913 N.getOperand(1).getOpcode() == ISD::SETCC) {
1914 bool Inv;
1915 Tmp1 = SelectCCExpr(N, Opc, Inv, Tmp2);
1916 MoveCRtoGPR(Tmp1, Inv, Tmp2, Result);
1917 return Result;
1918 }
1919 }
Nate Begeman7ddecb42005-04-06 23:51:40 +00001920 // FIXME: should add check in getImmediateForOpcode to return a value
1921 // indicating the immediate is a run of set bits so we can emit a bitfield
1922 // clear with RLWINM instead.
1923 switch(getImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
1924 default: assert(0 && "unhandled result code");
1925 case 0: // No immediate
Nate Begemand7c4a4a2005-05-11 23:43:56 +00001926 // Check for andc: and, (xor a, -1), b
1927 if (N.getOperand(0).getOpcode() == ISD::XOR &&
1928 N.getOperand(0).getOperand(1).getOpcode() == ISD::Constant &&
1929 cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->isAllOnesValue()) {
1930 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1931 Tmp2 = SelectExpr(N.getOperand(1));
1932 BuildMI(BB, PPC::ANDC, 2, Result).addReg(Tmp2).addReg(Tmp1);
1933 return Result;
1934 }
1935 // It wasn't and-with-complement, emit a regular and
Chris Lattnercafb67b2005-05-09 17:39:48 +00001936 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman7ddecb42005-04-06 23:51:40 +00001937 Tmp2 = SelectExpr(N.getOperand(1));
Nate Begemanc7bd4822005-04-11 06:34:10 +00001938 Opc = Recording ? PPC::ANDo : PPC::AND;
1939 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
Nate Begeman7ddecb42005-04-06 23:51:40 +00001940 break;
1941 case 1: // Low immediate
Chris Lattnercafb67b2005-05-09 17:39:48 +00001942 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman7ddecb42005-04-06 23:51:40 +00001943 BuildMI(BB, PPC::ANDIo, 2, Result).addReg(Tmp1).addImm(Tmp2);
1944 break;
1945 case 2: // Shifted immediate
Chris Lattnercafb67b2005-05-09 17:39:48 +00001946 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman7ddecb42005-04-06 23:51:40 +00001947 BuildMI(BB, PPC::ANDISo, 2, Result).addReg(Tmp1).addImm(Tmp2);
1948 break;
Nate Begeman9f833d32005-04-12 00:10:02 +00001949 case 5: // Bitfield mask
1950 Opc = Recording ? PPC::RLWINMo : PPC::RLWINM;
1951 Tmp3 = Tmp2 >> 16; // MB
1952 Tmp2 &= 0xFFFF; // ME
Chris Lattnercafb67b2005-05-09 17:39:48 +00001953
1954 if (N.getOperand(0).getOpcode() == ISD::SRL)
1955 if (ConstantSDNode *SA =
1956 dyn_cast<ConstantSDNode>(N.getOperand(0).getOperand(1))) {
1957
1958 // We can fold the RLWINM and the SRL together if the mask is
1959 // clearing the top bits which are rotated around.
1960 unsigned RotAmt = 32-(SA->getValue() & 31);
1961 if (Tmp2 <= RotAmt) {
1962 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1963 BuildMI(BB, Opc, 4, Result).addReg(Tmp1).addImm(RotAmt)
1964 .addImm(Tmp3).addImm(Tmp2);
1965 break;
1966 }
1967 }
1968
1969 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman9f833d32005-04-12 00:10:02 +00001970 BuildMI(BB, Opc, 4, Result).addReg(Tmp1).addImm(0)
1971 .addImm(Tmp3).addImm(Tmp2);
1972 break;
Nate Begeman7ddecb42005-04-06 23:51:40 +00001973 }
Nate Begemanc7bd4822005-04-11 06:34:10 +00001974 RecordSuccess = true;
Nate Begeman7ddecb42005-04-06 23:51:40 +00001975 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001976
Nate Begemana9795f82005-03-24 04:41:43 +00001977 case ISD::OR:
Nate Begeman7ddecb42005-04-06 23:51:40 +00001978 if (SelectBitfieldInsert(N, Result))
1979 return Result;
Nate Begeman1cbf3ab2005-04-18 07:48:09 +00001980 if (PPCCRopts) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001981 if (N.getOperand(0).getOpcode() == ISD::SETCC ||
Nate Begeman1cbf3ab2005-04-18 07:48:09 +00001982 N.getOperand(1).getOpcode() == ISD::SETCC) {
1983 bool Inv;
1984 Tmp1 = SelectCCExpr(N, Opc, Inv, Tmp2);
1985 MoveCRtoGPR(Tmp1, Inv, Tmp2, Result);
1986 return Result;
1987 }
1988 }
Nate Begemana9795f82005-03-24 04:41:43 +00001989 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman439b4442005-04-05 04:22:58 +00001990 switch(getImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
Nate Begemana9795f82005-03-24 04:41:43 +00001991 default: assert(0 && "unhandled result code");
1992 case 0: // No immediate
1993 Tmp2 = SelectExpr(N.getOperand(1));
Nate Begemanc7bd4822005-04-11 06:34:10 +00001994 Opc = Recording ? PPC::ORo : PPC::OR;
1995 RecordSuccess = true;
1996 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +00001997 break;
1998 case 1: // Low immediate
Nate Begeman7ddecb42005-04-06 23:51:40 +00001999 BuildMI(BB, PPC::ORI, 2, Result).addReg(Tmp1).addImm(Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +00002000 break;
2001 case 2: // Shifted immediate
Nate Begeman7ddecb42005-04-06 23:51:40 +00002002 BuildMI(BB, PPC::ORIS, 2, Result).addReg(Tmp1).addImm(Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +00002003 break;
2004 }
2005 return Result;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00002006
Nate Begemanaa73a9f2005-04-03 11:20:20 +00002007 case ISD::XOR: {
2008 // Check for EQV: xor, (xor a, -1), b
2009 if (N.getOperand(0).getOpcode() == ISD::XOR &&
2010 N.getOperand(0).getOperand(1).getOpcode() == ISD::Constant &&
2011 cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->isAllOnesValue()) {
Nate Begemanaa73a9f2005-04-03 11:20:20 +00002012 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
2013 Tmp2 = SelectExpr(N.getOperand(1));
2014 BuildMI(BB, PPC::EQV, 2, Result).addReg(Tmp1).addReg(Tmp2);
2015 return Result;
2016 }
Chris Lattner837a5212005-04-21 21:09:11 +00002017 // Check for NOT, NOR, EQV, and NAND: xor (copy, or, xor, and), -1
Nate Begemanaa73a9f2005-04-03 11:20:20 +00002018 if (N.getOperand(1).getOpcode() == ISD::Constant &&
2019 cast<ConstantSDNode>(N.getOperand(1))->isAllOnesValue()) {
Nate Begemanaa73a9f2005-04-03 11:20:20 +00002020 switch(N.getOperand(0).getOpcode()) {
2021 case ISD::OR:
2022 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
2023 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
2024 BuildMI(BB, PPC::NOR, 2, Result).addReg(Tmp1).addReg(Tmp2);
2025 break;
2026 case ISD::AND:
2027 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
2028 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
2029 BuildMI(BB, PPC::NAND, 2, Result).addReg(Tmp1).addReg(Tmp2);
2030 break;
Chris Lattner837a5212005-04-21 21:09:11 +00002031 case ISD::XOR:
2032 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
2033 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
2034 BuildMI(BB, PPC::EQV, 2, Result).addReg(Tmp1).addReg(Tmp2);
2035 break;
Nate Begemanaa73a9f2005-04-03 11:20:20 +00002036 default:
2037 Tmp1 = SelectExpr(N.getOperand(0));
2038 BuildMI(BB, PPC::NOR, 2, Result).addReg(Tmp1).addReg(Tmp1);
2039 break;
2040 }
2041 return Result;
2042 }
2043 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman439b4442005-04-05 04:22:58 +00002044 switch(getImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
Nate Begemanaa73a9f2005-04-03 11:20:20 +00002045 default: assert(0 && "unhandled result code");
2046 case 0: // No immediate
2047 Tmp2 = SelectExpr(N.getOperand(1));
2048 BuildMI(BB, PPC::XOR, 2, Result).addReg(Tmp1).addReg(Tmp2);
2049 break;
2050 case 1: // Low immediate
2051 BuildMI(BB, PPC::XORI, 2, Result).addReg(Tmp1).addImm(Tmp2);
2052 break;
2053 case 2: // Shifted immediate
2054 BuildMI(BB, PPC::XORIS, 2, Result).addReg(Tmp1).addImm(Tmp2);
2055 break;
2056 }
2057 return Result;
2058 }
2059
Nate Begeman9e3e1b52005-03-24 23:35:30 +00002060 case ISD::SUB:
Nate Begemand7c4a4a2005-05-11 23:43:56 +00002061 if (1 == getImmediateForOpcode(N.getOperand(0), opcode, Tmp1, true)) {
2062 Tmp2 = SelectExpr(N.getOperand(1));
Nate Begeman27523a12005-04-02 00:42:16 +00002063 BuildMI(BB, PPC::SUBFIC, 2, Result).addReg(Tmp2).addSImm(Tmp1);
Nate Begemand7c4a4a2005-05-11 23:43:56 +00002064 } else if (1 == getImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
Nate Begeman27523a12005-04-02 00:42:16 +00002065 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begemand7c4a4a2005-05-11 23:43:56 +00002066 BuildMI(BB, PPC::ADDI, 2, Result).addReg(Tmp1).addSImm(Tmp2);
2067 } else {
2068 Tmp1 = SelectExpr(N.getOperand(0));
2069 Tmp2 = SelectExpr(N.getOperand(1));
Nate Begeman27523a12005-04-02 00:42:16 +00002070 BuildMI(BB, PPC::SUBF, 2, Result).addReg(Tmp2).addReg(Tmp1);
2071 }
Nate Begeman9e3e1b52005-03-24 23:35:30 +00002072 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002073
Nate Begeman5e966612005-03-24 06:28:42 +00002074 case ISD::MUL:
Nate Begeman9e3e1b52005-03-24 23:35:30 +00002075 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman439b4442005-04-05 04:22:58 +00002076 if (1 == getImmediateForOpcode(N.getOperand(1), opcode, Tmp2))
Nate Begeman307e7442005-03-26 01:28:53 +00002077 BuildMI(BB, PPC::MULLI, 2, Result).addReg(Tmp1).addSImm(Tmp2);
2078 else {
2079 Tmp2 = SelectExpr(N.getOperand(1));
2080 BuildMI(BB, PPC::MULLW, 2, Result).addReg(Tmp1).addReg(Tmp2);
2081 }
Nate Begeman9e3e1b52005-03-24 23:35:30 +00002082 return Result;
2083
Nate Begeman815d6da2005-04-06 00:25:27 +00002084 case ISD::MULHS:
2085 case ISD::MULHU:
2086 Tmp1 = SelectExpr(N.getOperand(0));
2087 Tmp2 = SelectExpr(N.getOperand(1));
2088 Opc = (ISD::MULHU == opcode) ? PPC::MULHWU : PPC::MULHW;
2089 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
2090 return Result;
2091
Nate Begemanf3d08f32005-03-29 00:03:27 +00002092 case ISD::SDIV:
2093 case ISD::UDIV:
Nate Begeman815d6da2005-04-06 00:25:27 +00002094 switch (getImmediateForOpcode(N.getOperand(1), opcode, Tmp3)) {
2095 default: break;
2096 // If this is an sdiv by a power of two, we can use an srawi/addze pair.
2097 case 3:
Nate Begeman80196b12005-04-05 00:15:08 +00002098 Tmp1 = MakeReg(MVT::i32);
2099 Tmp2 = SelectExpr(N.getOperand(0));
Nate Begeman9f833d32005-04-12 00:10:02 +00002100 if ((int)Tmp3 < 0) {
2101 unsigned Tmp4 = MakeReg(MVT::i32);
2102 BuildMI(BB, PPC::SRAWI, 2, Tmp1).addReg(Tmp2).addImm(-Tmp3);
2103 BuildMI(BB, PPC::ADDZE, 1, Tmp4).addReg(Tmp1);
2104 BuildMI(BB, PPC::NEG, 1, Result).addReg(Tmp4);
2105 } else {
2106 BuildMI(BB, PPC::SRAWI, 2, Tmp1).addReg(Tmp2).addImm(Tmp3);
2107 BuildMI(BB, PPC::ADDZE, 1, Result).addReg(Tmp1);
2108 }
Nate Begeman80196b12005-04-05 00:15:08 +00002109 return Result;
Nate Begeman815d6da2005-04-06 00:25:27 +00002110 // If this is a divide by constant, we can emit code using some magic
2111 // constants to implement it as a multiply instead.
Nate Begeman27b4c232005-04-06 06:44:57 +00002112 case 4:
2113 ExprMap.erase(N);
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002114 if (opcode == ISD::SDIV)
Nate Begeman27b4c232005-04-06 06:44:57 +00002115 return SelectExpr(BuildSDIVSequence(N));
2116 else
2117 return SelectExpr(BuildUDIVSequence(N));
Nate Begeman80196b12005-04-05 00:15:08 +00002118 }
Nate Begemanf3d08f32005-03-29 00:03:27 +00002119 Tmp1 = SelectExpr(N.getOperand(0));
2120 Tmp2 = SelectExpr(N.getOperand(1));
2121 Opc = (ISD::UDIV == opcode) ? PPC::DIVWU : PPC::DIVW;
2122 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
2123 return Result;
2124
Nate Begeman9e3e1b52005-03-24 23:35:30 +00002125 case ISD::ADD_PARTS:
Nate Begemanca12a2b2005-03-28 22:28:37 +00002126 case ISD::SUB_PARTS: {
2127 assert(N.getNumOperands() == 4 && N.getValueType() == MVT::i32 &&
2128 "Not an i64 add/sub!");
2129 // Emit all of the operands.
2130 std::vector<unsigned> InVals;
2131 for (unsigned i = 0, e = N.getNumOperands(); i != e; ++i)
2132 InVals.push_back(SelectExpr(N.getOperand(i)));
2133 if (N.getOpcode() == ISD::ADD_PARTS) {
Nate Begeman27eeb002005-04-02 05:59:34 +00002134 BuildMI(BB, PPC::ADDC, 2, Result).addReg(InVals[0]).addReg(InVals[2]);
2135 BuildMI(BB, PPC::ADDE, 2, Result+1).addReg(InVals[1]).addReg(InVals[3]);
Nate Begemanca12a2b2005-03-28 22:28:37 +00002136 } else {
Nate Begeman27eeb002005-04-02 05:59:34 +00002137 BuildMI(BB, PPC::SUBFC, 2, Result).addReg(InVals[2]).addReg(InVals[0]);
2138 BuildMI(BB, PPC::SUBFE, 2, Result+1).addReg(InVals[3]).addReg(InVals[1]);
2139 }
2140 return Result+N.ResNo;
2141 }
2142
2143 case ISD::SHL_PARTS:
2144 case ISD::SRA_PARTS:
2145 case ISD::SRL_PARTS: {
2146 assert(N.getNumOperands() == 3 && N.getValueType() == MVT::i32 &&
2147 "Not an i64 shift!");
2148 unsigned ShiftOpLo = SelectExpr(N.getOperand(0));
2149 unsigned ShiftOpHi = SelectExpr(N.getOperand(1));
Nate Begeman3664cef2005-04-13 22:14:14 +00002150 unsigned SHReg = FoldIfWideZeroExtend(N.getOperand(2));
2151 Tmp1 = MakeReg(MVT::i32);
2152 Tmp2 = MakeReg(MVT::i32);
Nate Begeman27eeb002005-04-02 05:59:34 +00002153 Tmp3 = MakeReg(MVT::i32);
2154 unsigned Tmp4 = MakeReg(MVT::i32);
2155 unsigned Tmp5 = MakeReg(MVT::i32);
2156 unsigned Tmp6 = MakeReg(MVT::i32);
2157 BuildMI(BB, PPC::SUBFIC, 2, Tmp1).addReg(SHReg).addSImm(32);
2158 if (ISD::SHL_PARTS == opcode) {
2159 BuildMI(BB, PPC::SLW, 2, Tmp2).addReg(ShiftOpHi).addReg(SHReg);
2160 BuildMI(BB, PPC::SRW, 2, Tmp3).addReg(ShiftOpLo).addReg(Tmp1);
2161 BuildMI(BB, PPC::OR, 2, Tmp4).addReg(Tmp2).addReg(Tmp3);
2162 BuildMI(BB, PPC::ADDI, 2, Tmp5).addReg(SHReg).addSImm(-32);
Nate Begemanfa554702005-04-03 22:13:27 +00002163 BuildMI(BB, PPC::SLW, 2, Tmp6).addReg(ShiftOpLo).addReg(Tmp5);
Nate Begeman27eeb002005-04-02 05:59:34 +00002164 BuildMI(BB, PPC::OR, 2, Result+1).addReg(Tmp4).addReg(Tmp6);
2165 BuildMI(BB, PPC::SLW, 2, Result).addReg(ShiftOpLo).addReg(SHReg);
2166 } else if (ISD::SRL_PARTS == opcode) {
2167 BuildMI(BB, PPC::SRW, 2, Tmp2).addReg(ShiftOpLo).addReg(SHReg);
2168 BuildMI(BB, PPC::SLW, 2, Tmp3).addReg(ShiftOpHi).addReg(Tmp1);
2169 BuildMI(BB, PPC::OR, 2, Tmp4).addReg(Tmp2).addReg(Tmp3);
2170 BuildMI(BB, PPC::ADDI, 2, Tmp5).addReg(SHReg).addSImm(-32);
2171 BuildMI(BB, PPC::SRW, 2, Tmp6).addReg(ShiftOpHi).addReg(Tmp5);
2172 BuildMI(BB, PPC::OR, 2, Result).addReg(Tmp4).addReg(Tmp6);
2173 BuildMI(BB, PPC::SRW, 2, Result+1).addReg(ShiftOpHi).addReg(SHReg);
2174 } else {
2175 MachineBasicBlock *TmpMBB = new MachineBasicBlock(BB->getBasicBlock());
2176 MachineBasicBlock *PhiMBB = new MachineBasicBlock(BB->getBasicBlock());
2177 MachineBasicBlock *OldMBB = BB;
2178 MachineFunction *F = BB->getParent();
2179 ilist<MachineBasicBlock>::iterator It = BB; ++It;
2180 F->getBasicBlockList().insert(It, TmpMBB);
2181 F->getBasicBlockList().insert(It, PhiMBB);
2182 BB->addSuccessor(TmpMBB);
2183 BB->addSuccessor(PhiMBB);
2184 BuildMI(BB, PPC::SRW, 2, Tmp2).addReg(ShiftOpLo).addReg(SHReg);
2185 BuildMI(BB, PPC::SLW, 2, Tmp3).addReg(ShiftOpHi).addReg(Tmp1);
2186 BuildMI(BB, PPC::OR, 2, Tmp4).addReg(Tmp2).addReg(Tmp3);
2187 BuildMI(BB, PPC::ADDICo, 2, Tmp5).addReg(SHReg).addSImm(-32);
2188 BuildMI(BB, PPC::SRAW, 2, Tmp6).addReg(ShiftOpHi).addReg(Tmp5);
2189 BuildMI(BB, PPC::SRAW, 2, Result+1).addReg(ShiftOpHi).addReg(SHReg);
2190 BuildMI(BB, PPC::BLE, 2).addReg(PPC::CR0).addMBB(PhiMBB);
2191 // Select correct least significant half if the shift amount > 32
2192 BB = TmpMBB;
2193 unsigned Tmp7 = MakeReg(MVT::i32);
2194 BuildMI(BB, PPC::OR, 2, Tmp7).addReg(Tmp6).addReg(Tmp6);
2195 TmpMBB->addSuccessor(PhiMBB);
2196 BB = PhiMBB;
2197 BuildMI(BB, PPC::PHI, 4, Result).addReg(Tmp4).addMBB(OldMBB)
2198 .addReg(Tmp7).addMBB(TmpMBB);
Nate Begemanca12a2b2005-03-28 22:28:37 +00002199 }
2200 return Result+N.ResNo;
2201 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002202
Nate Begemana9795f82005-03-24 04:41:43 +00002203 case ISD::FP_TO_UINT:
Nate Begeman6b559972005-04-01 02:59:27 +00002204 case ISD::FP_TO_SINT: {
2205 bool U = (ISD::FP_TO_UINT == opcode);
2206 Tmp1 = SelectExpr(N.getOperand(0));
2207 if (!U) {
2208 Tmp2 = MakeReg(MVT::f64);
2209 BuildMI(BB, PPC::FCTIWZ, 1, Tmp2).addReg(Tmp1);
2210 int FrameIdx = BB->getParent()->getFrameInfo()->CreateStackObject(8, 8);
2211 addFrameReference(BuildMI(BB, PPC::STFD, 3).addReg(Tmp2), FrameIdx);
2212 addFrameReference(BuildMI(BB, PPC::LWZ, 2, Result), FrameIdx, 4);
2213 return Result;
2214 } else {
2215 unsigned Zero = getConstDouble(0.0);
2216 unsigned MaxInt = getConstDouble((1LL << 32) - 1);
2217 unsigned Border = getConstDouble(1LL << 31);
2218 unsigned UseZero = MakeReg(MVT::f64);
2219 unsigned UseMaxInt = MakeReg(MVT::f64);
2220 unsigned UseChoice = MakeReg(MVT::f64);
2221 unsigned TmpReg = MakeReg(MVT::f64);
2222 unsigned TmpReg2 = MakeReg(MVT::f64);
2223 unsigned ConvReg = MakeReg(MVT::f64);
2224 unsigned IntTmp = MakeReg(MVT::i32);
2225 unsigned XorReg = MakeReg(MVT::i32);
2226 MachineFunction *F = BB->getParent();
2227 int FrameIdx = F->getFrameInfo()->CreateStackObject(8, 8);
2228 // Update machine-CFG edges
2229 MachineBasicBlock *XorMBB = new MachineBasicBlock(BB->getBasicBlock());
2230 MachineBasicBlock *PhiMBB = new MachineBasicBlock(BB->getBasicBlock());
2231 MachineBasicBlock *OldMBB = BB;
2232 ilist<MachineBasicBlock>::iterator It = BB; ++It;
2233 F->getBasicBlockList().insert(It, XorMBB);
2234 F->getBasicBlockList().insert(It, PhiMBB);
2235 BB->addSuccessor(XorMBB);
2236 BB->addSuccessor(PhiMBB);
2237 // Convert from floating point to unsigned 32-bit value
2238 // Use 0 if incoming value is < 0.0
2239 BuildMI(BB, PPC::FSEL, 3, UseZero).addReg(Tmp1).addReg(Tmp1).addReg(Zero);
2240 // Use 2**32 - 1 if incoming value is >= 2**32
2241 BuildMI(BB, PPC::FSUB, 2, UseMaxInt).addReg(MaxInt).addReg(Tmp1);
2242 BuildMI(BB, PPC::FSEL, 3, UseChoice).addReg(UseMaxInt).addReg(UseZero)
2243 .addReg(MaxInt);
2244 // Subtract 2**31
2245 BuildMI(BB, PPC::FSUB, 2, TmpReg).addReg(UseChoice).addReg(Border);
2246 // Use difference if >= 2**31
2247 BuildMI(BB, PPC::FCMPU, 2, PPC::CR0).addReg(UseChoice).addReg(Border);
2248 BuildMI(BB, PPC::FSEL, 3, TmpReg2).addReg(TmpReg).addReg(TmpReg)
2249 .addReg(UseChoice);
2250 // Convert to integer
2251 BuildMI(BB, PPC::FCTIWZ, 1, ConvReg).addReg(TmpReg2);
2252 addFrameReference(BuildMI(BB, PPC::STFD, 3).addReg(ConvReg), FrameIdx);
2253 addFrameReference(BuildMI(BB, PPC::LWZ, 2, IntTmp), FrameIdx, 4);
2254 BuildMI(BB, PPC::BLT, 2).addReg(PPC::CR0).addMBB(PhiMBB);
2255 BuildMI(BB, PPC::B, 1).addMBB(XorMBB);
2256
2257 // XorMBB:
2258 // add 2**31 if input was >= 2**31
2259 BB = XorMBB;
2260 BuildMI(BB, PPC::XORIS, 2, XorReg).addReg(IntTmp).addImm(0x8000);
2261 XorMBB->addSuccessor(PhiMBB);
2262
2263 // PhiMBB:
2264 // DestReg = phi [ IntTmp, OldMBB ], [ XorReg, XorMBB ]
2265 BB = PhiMBB;
2266 BuildMI(BB, PPC::PHI, 4, Result).addReg(IntTmp).addMBB(OldMBB)
2267 .addReg(XorReg).addMBB(XorMBB);
2268 return Result;
2269 }
2270 assert(0 && "Should never get here");
2271 return 0;
2272 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002273
Nate Begeman9e3e1b52005-03-24 23:35:30 +00002274 case ISD::SETCC:
Nate Begeman33162522005-03-29 21:54:38 +00002275 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Node)) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002276 if (ConstantSDNode *CN =
Nate Begeman7e7fadd2005-04-07 20:30:01 +00002277 dyn_cast<ConstantSDNode>(SetCC->getOperand(1).Val)) {
Nate Begeman9765c252005-04-12 21:22:28 +00002278 // We can codegen setcc op, imm very efficiently compared to a brcond.
2279 // Check for those cases here.
2280 // setcc op, 0
Nate Begeman7e7fadd2005-04-07 20:30:01 +00002281 if (CN->getValue() == 0) {
2282 Tmp1 = SelectExpr(SetCC->getOperand(0));
2283 switch (SetCC->getCondition()) {
Nate Begeman7bfba7d2005-04-14 09:45:08 +00002284 default: SetCC->dump(); assert(0 && "Unhandled SetCC condition"); abort();
Nate Begeman7e7fadd2005-04-07 20:30:01 +00002285 case ISD::SETEQ:
Nate Begeman7e7fadd2005-04-07 20:30:01 +00002286 Tmp2 = MakeReg(MVT::i32);
2287 BuildMI(BB, PPC::CNTLZW, 1, Tmp2).addReg(Tmp1);
2288 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp2).addImm(27)
2289 .addImm(5).addImm(31);
2290 break;
2291 case ISD::SETNE:
Nate Begeman7e7fadd2005-04-07 20:30:01 +00002292 Tmp2 = MakeReg(MVT::i32);
2293 BuildMI(BB, PPC::ADDIC, 2, Tmp2).addReg(Tmp1).addSImm(-1);
2294 BuildMI(BB, PPC::SUBFE, 2, Result).addReg(Tmp2).addReg(Tmp1);
2295 break;
Nate Begeman7e7fadd2005-04-07 20:30:01 +00002296 case ISD::SETLT:
2297 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(1)
2298 .addImm(31).addImm(31);
2299 break;
Nate Begeman7e7fadd2005-04-07 20:30:01 +00002300 case ISD::SETGT:
2301 Tmp2 = MakeReg(MVT::i32);
2302 Tmp3 = MakeReg(MVT::i32);
2303 BuildMI(BB, PPC::NEG, 2, Tmp2).addReg(Tmp1);
2304 BuildMI(BB, PPC::ANDC, 2, Tmp3).addReg(Tmp2).addReg(Tmp1);
2305 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp3).addImm(1)
2306 .addImm(31).addImm(31);
2307 break;
Nate Begeman9765c252005-04-12 21:22:28 +00002308 }
2309 return Result;
2310 }
2311 // setcc op, -1
2312 if (CN->isAllOnesValue()) {
2313 Tmp1 = SelectExpr(SetCC->getOperand(0));
2314 switch (SetCC->getCondition()) {
2315 default: assert(0 && "Unhandled SetCC condition"); abort();
2316 case ISD::SETEQ:
2317 Tmp2 = MakeReg(MVT::i32);
2318 Tmp3 = MakeReg(MVT::i32);
2319 BuildMI(BB, PPC::ADDIC, 2, Tmp2).addReg(Tmp1).addSImm(1);
2320 BuildMI(BB, PPC::LI, 1, Tmp3).addSImm(0);
2321 BuildMI(BB, PPC::ADDZE, 1, Result).addReg(Tmp3);
Nate Begeman7e7fadd2005-04-07 20:30:01 +00002322 break;
Nate Begeman9765c252005-04-12 21:22:28 +00002323 case ISD::SETNE:
2324 Tmp2 = MakeReg(MVT::i32);
2325 Tmp3 = MakeReg(MVT::i32);
2326 BuildMI(BB, PPC::NOR, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
2327 BuildMI(BB, PPC::ADDIC, 2, Tmp3).addReg(Tmp2).addSImm(-1);
2328 BuildMI(BB, PPC::SUBFE, 2, Result).addReg(Tmp3).addReg(Tmp2);
2329 break;
2330 case ISD::SETLT:
2331 Tmp2 = MakeReg(MVT::i32);
2332 Tmp3 = MakeReg(MVT::i32);
2333 BuildMI(BB, PPC::ADDI, 2, Tmp2).addReg(Tmp1).addSImm(1);
2334 BuildMI(BB, PPC::AND, 2, Tmp3).addReg(Tmp2).addReg(Tmp1);
2335 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp3).addImm(1)
2336 .addImm(31).addImm(31);
2337 break;
2338 case ISD::SETGT:
2339 Tmp2 = MakeReg(MVT::i32);
Nate Begeman7e7fadd2005-04-07 20:30:01 +00002340 BuildMI(BB, PPC::RLWINM, 4, Tmp2).addReg(Tmp1).addImm(1)
2341 .addImm(31).addImm(31);
2342 BuildMI(BB, PPC::XORI, 2, Result).addReg(Tmp2).addImm(1);
2343 break;
2344 }
2345 return Result;
2346 }
2347 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002348
Nate Begeman1cbf3ab2005-04-18 07:48:09 +00002349 bool Inv;
2350 unsigned CCReg = SelectCC(N, Opc, Inv, Tmp2);
2351 MoveCRtoGPR(CCReg, Inv, Tmp2, Result);
Nate Begeman33162522005-03-29 21:54:38 +00002352 return Result;
2353 }
2354 assert(0 && "Is this legal?");
2355 return 0;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002356
Nate Begeman74747862005-03-29 22:24:51 +00002357 case ISD::SELECT: {
Nate Begeman1cbf3ab2005-04-18 07:48:09 +00002358 bool Inv;
Chris Lattner30710192005-04-01 07:10:02 +00002359 unsigned TrueValue = SelectExpr(N.getOperand(1)); //Use if TRUE
2360 unsigned FalseValue = SelectExpr(N.getOperand(2)); //Use if FALSE
Nate Begeman1cbf3ab2005-04-18 07:48:09 +00002361 unsigned CCReg = SelectCC(N.getOperand(0), Opc, Inv, Tmp3);
Chris Lattner30710192005-04-01 07:10:02 +00002362
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002363 // Create an iterator with which to insert the MBB for copying the false
Nate Begeman74747862005-03-29 22:24:51 +00002364 // value and the MBB to hold the PHI instruction for this SetCC.
2365 MachineBasicBlock *thisMBB = BB;
2366 const BasicBlock *LLVM_BB = BB->getBasicBlock();
2367 ilist<MachineBasicBlock>::iterator It = BB;
2368 ++It;
2369
2370 // thisMBB:
2371 // ...
2372 // TrueVal = ...
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00002373 // cmpTY ccX, r1, r2
Nate Begeman74747862005-03-29 22:24:51 +00002374 // bCC copy1MBB
2375 // fallthrough --> copy0MBB
Nate Begeman74747862005-03-29 22:24:51 +00002376 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
2377 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00002378 BuildMI(BB, Opc, 2).addReg(CCReg).addMBB(sinkMBB);
Nate Begeman74747862005-03-29 22:24:51 +00002379 MachineFunction *F = BB->getParent();
2380 F->getBasicBlockList().insert(It, copy0MBB);
2381 F->getBasicBlockList().insert(It, sinkMBB);
2382 // Update machine-CFG edges
2383 BB->addSuccessor(copy0MBB);
2384 BB->addSuccessor(sinkMBB);
2385
2386 // copy0MBB:
2387 // %FalseValue = ...
2388 // # fallthrough to sinkMBB
2389 BB = copy0MBB;
Nate Begeman74747862005-03-29 22:24:51 +00002390 // Update machine-CFG edges
2391 BB->addSuccessor(sinkMBB);
2392
2393 // sinkMBB:
2394 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
2395 // ...
2396 BB = sinkMBB;
2397 BuildMI(BB, PPC::PHI, 4, Result).addReg(FalseValue)
2398 .addMBB(copy0MBB).addReg(TrueValue).addMBB(thisMBB);
Nate Begeman74747862005-03-29 22:24:51 +00002399 return Result;
2400 }
Nate Begemana9795f82005-03-24 04:41:43 +00002401
2402 case ISD::Constant:
2403 switch (N.getValueType()) {
2404 default: assert(0 && "Cannot use constants of this type!");
2405 case MVT::i1:
2406 BuildMI(BB, PPC::LI, 1, Result)
2407 .addSImm(!cast<ConstantSDNode>(N)->isNullValue());
2408 break;
2409 case MVT::i32:
2410 {
2411 int v = (int)cast<ConstantSDNode>(N)->getSignExtended();
2412 if (v < 32768 && v >= -32768) {
2413 BuildMI(BB, PPC::LI, 1, Result).addSImm(v);
2414 } else {
Nate Begeman5e966612005-03-24 06:28:42 +00002415 Tmp1 = MakeReg(MVT::i32);
2416 BuildMI(BB, PPC::LIS, 1, Tmp1).addSImm(v >> 16);
2417 BuildMI(BB, PPC::ORI, 2, Result).addReg(Tmp1).addImm(v & 0xFFFF);
Nate Begemana9795f82005-03-24 04:41:43 +00002418 }
2419 }
2420 }
2421 return Result;
2422 }
2423
2424 return 0;
2425}
2426
2427void ISel::Select(SDOperand N) {
2428 unsigned Tmp1, Tmp2, Opc;
2429 unsigned opcode = N.getOpcode();
2430
2431 if (!ExprMap.insert(std::make_pair(N, 1)).second)
2432 return; // Already selected.
2433
2434 SDNode *Node = N.Val;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002435
Nate Begemana9795f82005-03-24 04:41:43 +00002436 switch (Node->getOpcode()) {
2437 default:
2438 Node->dump(); std::cerr << "\n";
2439 assert(0 && "Node not handled yet!");
2440 case ISD::EntryToken: return; // Noop
2441 case ISD::TokenFactor:
2442 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
2443 Select(Node->getOperand(i));
2444 return;
Chris Lattner16cd04d2005-05-12 23:24:06 +00002445 case ISD::CALLSEQ_START:
2446 case ISD::CALLSEQ_END:
Nate Begemana9795f82005-03-24 04:41:43 +00002447 Select(N.getOperand(0));
2448 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
Chris Lattner16cd04d2005-05-12 23:24:06 +00002449 Opc = N.getOpcode() == ISD::CALLSEQ_START ? PPC::ADJCALLSTACKDOWN :
Nate Begemana9795f82005-03-24 04:41:43 +00002450 PPC::ADJCALLSTACKUP;
2451 BuildMI(BB, Opc, 1).addImm(Tmp1);
2452 return;
2453 case ISD::BR: {
2454 MachineBasicBlock *Dest =
2455 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
Nate Begemana9795f82005-03-24 04:41:43 +00002456 Select(N.getOperand(0));
2457 BuildMI(BB, PPC::B, 1).addMBB(Dest);
2458 return;
2459 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002460 case ISD::BRCOND:
Nate Begemancd08e4c2005-04-09 20:09:12 +00002461 case ISD::BRCONDTWOWAY:
Nate Begemana9795f82005-03-24 04:41:43 +00002462 SelectBranchCC(N);
2463 return;
2464 case ISD::CopyToReg:
2465 Select(N.getOperand(0));
2466 Tmp1 = SelectExpr(N.getOperand(1));
2467 Tmp2 = cast<RegSDNode>(N)->getReg();
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002468
Nate Begemana9795f82005-03-24 04:41:43 +00002469 if (Tmp1 != Tmp2) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002470 if (N.getOperand(1).getValueType() == MVT::f64 ||
Nate Begemana9795f82005-03-24 04:41:43 +00002471 N.getOperand(1).getValueType() == MVT::f32)
2472 BuildMI(BB, PPC::FMR, 1, Tmp2).addReg(Tmp1);
2473 else
2474 BuildMI(BB, PPC::OR, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
2475 }
2476 return;
2477 case ISD::ImplicitDef:
2478 Select(N.getOperand(0));
2479 BuildMI(BB, PPC::IMPLICIT_DEF, 0, cast<RegSDNode>(N)->getReg());
2480 return;
2481 case ISD::RET:
2482 switch (N.getNumOperands()) {
2483 default:
2484 assert(0 && "Unknown return instruction!");
2485 case 3:
2486 assert(N.getOperand(1).getValueType() == MVT::i32 &&
2487 N.getOperand(2).getValueType() == MVT::i32 &&
Misha Brukman7847fca2005-04-22 17:54:37 +00002488 "Unknown two-register value!");
Nate Begemana9795f82005-03-24 04:41:43 +00002489 Select(N.getOperand(0));
2490 Tmp1 = SelectExpr(N.getOperand(1));
2491 Tmp2 = SelectExpr(N.getOperand(2));
Nate Begeman27523a12005-04-02 00:42:16 +00002492 BuildMI(BB, PPC::OR, 2, PPC::R3).addReg(Tmp2).addReg(Tmp2);
2493 BuildMI(BB, PPC::OR, 2, PPC::R4).addReg(Tmp1).addReg(Tmp1);
Nate Begemana9795f82005-03-24 04:41:43 +00002494 break;
2495 case 2:
2496 Select(N.getOperand(0));
2497 Tmp1 = SelectExpr(N.getOperand(1));
2498 switch (N.getOperand(1).getValueType()) {
2499 default:
2500 assert(0 && "Unknown return type!");
2501 case MVT::f64:
2502 case MVT::f32:
2503 BuildMI(BB, PPC::FMR, 1, PPC::F1).addReg(Tmp1);
2504 break;
2505 case MVT::i32:
2506 BuildMI(BB, PPC::OR, 2, PPC::R3).addReg(Tmp1).addReg(Tmp1);
2507 break;
2508 }
Nate Begeman9e3e1b52005-03-24 23:35:30 +00002509 case 1:
2510 Select(N.getOperand(0));
2511 break;
Nate Begemana9795f82005-03-24 04:41:43 +00002512 }
2513 BuildMI(BB, PPC::BLR, 0); // Just emit a 'ret' instruction
2514 return;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002515 case ISD::TRUNCSTORE:
2516 case ISD::STORE:
Nate Begemana9795f82005-03-24 04:41:43 +00002517 {
2518 SDOperand Chain = N.getOperand(0);
2519 SDOperand Value = N.getOperand(1);
2520 SDOperand Address = N.getOperand(2);
2521 Select(Chain);
2522
2523 Tmp1 = SelectExpr(Value); //value
2524
2525 if (opcode == ISD::STORE) {
2526 switch(Value.getValueType()) {
2527 default: assert(0 && "unknown Type in store");
2528 case MVT::i32: Opc = PPC::STW; break;
2529 case MVT::f64: Opc = PPC::STFD; break;
2530 case MVT::f32: Opc = PPC::STFS; break;
2531 }
2532 } else { //ISD::TRUNCSTORE
Chris Lattner9fadb4c2005-07-10 00:29:18 +00002533 switch(cast<VTSDNode>(Node->getOperand(4))->getVT()) {
Nate Begemana9795f82005-03-24 04:41:43 +00002534 default: assert(0 && "unknown Type in store");
Nate Begeman7e7fadd2005-04-07 20:30:01 +00002535 case MVT::i1:
Nate Begemana9795f82005-03-24 04:41:43 +00002536 case MVT::i8: Opc = PPC::STB; break;
2537 case MVT::i16: Opc = PPC::STH; break;
2538 }
2539 }
2540
Nate Begemana7e11a42005-04-01 05:57:17 +00002541 if(Address.getOpcode() == ISD::FrameIndex)
Nate Begemana9795f82005-03-24 04:41:43 +00002542 {
Nate Begeman58f718c2005-03-30 02:23:08 +00002543 Tmp2 = cast<FrameIndexSDNode>(Address)->getIndex();
2544 addFrameReference(BuildMI(BB, Opc, 3).addReg(Tmp1), (int)Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +00002545 }
2546 else
2547 {
2548 int offset;
Nate Begeman04730362005-04-01 04:45:11 +00002549 bool idx = SelectAddr(Address, Tmp2, offset);
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002550 if (idx) {
Nate Begeman04730362005-04-01 04:45:11 +00002551 Opc = IndexedOpForOp(Opc);
2552 BuildMI(BB, Opc, 3).addReg(Tmp1).addReg(Tmp2).addReg(offset);
2553 } else {
2554 BuildMI(BB, Opc, 3).addReg(Tmp1).addImm(offset).addReg(Tmp2);
2555 }
Nate Begemana9795f82005-03-24 04:41:43 +00002556 }
2557 return;
2558 }
2559 case ISD::EXTLOAD:
2560 case ISD::SEXTLOAD:
2561 case ISD::ZEXTLOAD:
2562 case ISD::LOAD:
2563 case ISD::CopyFromReg:
Chris Lattnerb5d8e6e2005-05-13 20:29:26 +00002564 case ISD::TAILCALL:
Nate Begemana9795f82005-03-24 04:41:43 +00002565 case ISD::CALL:
2566 case ISD::DYNAMIC_STACKALLOC:
2567 ExprMap.erase(N);
2568 SelectExpr(N);
2569 return;
2570 }
2571 assert(0 && "Should not be reached!");
2572}
2573
2574
2575/// createPPC32PatternInstructionSelector - This pass converts an LLVM function
2576/// into a machine code representation using pattern matching and a machine
2577/// description file.
2578///
2579FunctionPass *llvm::createPPC32ISelPattern(TargetMachine &TM) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002580 return new ISel(TM);
Chris Lattner246fa632005-03-24 06:16:18 +00002581}
2582