blob: 00990023c15cb93b200664931b1288a82d0347ab [file] [log] [blame]
Nate Begemana9795f82005-03-24 04:41:43 +00001//===-- PPC32ISelPattern.cpp - A pattern matching inst selector for PPC32 -===//
2//
3// The LLVM Compiler Infrastructure
4//
Nate Begeman5e966612005-03-24 06:28:42 +00005// This file was developed by Nate Begeman and is distributed under
Nate Begemana9795f82005-03-24 04:41:43 +00006// the University of Illinois Open Source License. See LICENSE.TXT for details.
Misha Brukmanb5f662f2005-04-21 23:30:14 +00007//
Nate Begemana9795f82005-03-24 04:41:43 +00008//===----------------------------------------------------------------------===//
9//
10// This file defines a pattern matching instruction selector for 32 bit PowerPC.
Nate Begeman815d6da2005-04-06 00:25:27 +000011// Magic number generation for integer divide from the PowerPC Compiler Writer's
12// Guide, section 3.2.3.5
Nate Begemana9795f82005-03-24 04:41:43 +000013//
14//===----------------------------------------------------------------------===//
15
16#include "PowerPC.h"
17#include "PowerPCInstrBuilder.h"
18#include "PowerPCInstrInfo.h"
Nate Begemancd08e4c2005-04-09 20:09:12 +000019#include "PPC32TargetMachine.h"
Nate Begemana9795f82005-03-24 04:41:43 +000020#include "llvm/Constants.h" // FIXME: REMOVE
21#include "llvm/Function.h"
22#include "llvm/CodeGen/MachineConstantPool.h" // FIXME: REMOVE
23#include "llvm/CodeGen/MachineFunction.h"
24#include "llvm/CodeGen/MachineFrameInfo.h"
25#include "llvm/CodeGen/SelectionDAG.h"
26#include "llvm/CodeGen/SelectionDAGISel.h"
27#include "llvm/CodeGen/SSARegMap.h"
28#include "llvm/Target/TargetData.h"
29#include "llvm/Target/TargetLowering.h"
Nate Begeman93075ec2005-04-04 23:40:36 +000030#include "llvm/Target/TargetOptions.h"
Nate Begemana9795f82005-03-24 04:41:43 +000031#include "llvm/Support/Debug.h"
32#include "llvm/Support/MathExtras.h"
33#include "llvm/ADT/Statistic.h"
34#include <set>
35#include <algorithm>
36using namespace llvm;
37
38//===----------------------------------------------------------------------===//
39// PPC32TargetLowering - PPC32 Implementation of the TargetLowering interface
40namespace {
41 class PPC32TargetLowering : public TargetLowering {
42 int VarArgsFrameIndex; // FrameIndex for start of varargs area.
43 int ReturnAddrIndex; // FrameIndex for return slot.
44 public:
45 PPC32TargetLowering(TargetMachine &TM) : TargetLowering(TM) {
Chris Lattner9bce0f92005-05-12 02:06:00 +000046 // Fold away setcc operations if possible.
47 setSetCCIsExpensive();
48
Nate Begemana9795f82005-03-24 04:41:43 +000049 // Set up the register classes.
50 addRegisterClass(MVT::i32, PPC32::GPRCRegisterClass);
Nate Begeman7532e2f2005-03-26 08:25:22 +000051 addRegisterClass(MVT::f32, PPC32::FPRCRegisterClass);
Nate Begemana9795f82005-03-24 04:41:43 +000052 addRegisterClass(MVT::f64, PPC32::FPRCRegisterClass);
Misha Brukmanb5f662f2005-04-21 23:30:14 +000053
Nate Begeman74d73452005-03-31 00:15:26 +000054 // PowerPC has no intrinsics for these particular operations
Nate Begeman01d05262005-03-30 01:45:43 +000055 setOperationAction(ISD::MEMMOVE, MVT::Other, Expand);
56 setOperationAction(ISD::MEMSET, MVT::Other, Expand);
57 setOperationAction(ISD::MEMCPY, MVT::Other, Expand);
58
Nate Begeman74d73452005-03-31 00:15:26 +000059 // PowerPC has an i16 but no i8 (or i1) SEXTLOAD
60 setOperationAction(ISD::SEXTLOAD, MVT::i1, Expand);
61 setOperationAction(ISD::SEXTLOAD, MVT::i8, Expand);
Misha Brukmanb5f662f2005-04-21 23:30:14 +000062
Nate Begeman815d6da2005-04-06 00:25:27 +000063 // PowerPC has no SREM/UREM instructions
64 setOperationAction(ISD::SREM, MVT::i32, Expand);
65 setOperationAction(ISD::UREM, MVT::i32, Expand);
Chris Lattner43fdea02005-04-02 05:03:24 +000066
Chris Lattner32f3cf62005-05-13 16:20:22 +000067 // We don't support sin/cos/sqrt/fmod
Chris Lattner17234b72005-04-30 04:26:06 +000068 setOperationAction(ISD::FSIN , MVT::f64, Expand);
69 setOperationAction(ISD::FCOS , MVT::f64, Expand);
70 setOperationAction(ISD::FSQRT, MVT::f64, Expand);
Chris Lattner32f3cf62005-05-13 16:20:22 +000071 setOperationAction(ISD::SREM , MVT::f64, Expand);
Chris Lattner17234b72005-04-30 04:26:06 +000072 setOperationAction(ISD::FSIN , MVT::f32, Expand);
73 setOperationAction(ISD::FCOS , MVT::f32, Expand);
74 setOperationAction(ISD::FSQRT, MVT::f32, Expand);
Chris Lattner32f3cf62005-05-13 16:20:22 +000075 setOperationAction(ISD::SREM , MVT::f32, Expand);
Chris Lattner17234b72005-04-30 04:26:06 +000076
Nate Begemand7c4a4a2005-05-11 23:43:56 +000077 //PowerPC does not have CTPOP or CTTZ
Andrew Lenharth691ef2b2005-05-03 17:19:30 +000078 setOperationAction(ISD::CTPOP, MVT::i32 , Expand);
79 setOperationAction(ISD::CTTZ , MVT::i32 , Expand);
Andrew Lenharth691ef2b2005-05-03 17:19:30 +000080
Chris Lattnercbd06fc2005-04-07 19:41:49 +000081 setSetCCResultContents(ZeroOrOneSetCCResult);
Nate Begeman3e897162005-03-31 23:55:40 +000082 addLegalFPImmediate(+0.0); // Necessary for FSEL
Misha Brukmanb5f662f2005-04-21 23:30:14 +000083 addLegalFPImmediate(-0.0); //
Nate Begeman3e897162005-03-31 23:55:40 +000084
Nate Begemana9795f82005-03-24 04:41:43 +000085 computeRegisterProperties();
86 }
87
88 /// LowerArguments - This hook must be implemented to indicate how we should
89 /// lower the arguments for the specified function, into the specified DAG.
90 virtual std::vector<SDOperand>
91 LowerArguments(Function &F, SelectionDAG &DAG);
Misha Brukmanb5f662f2005-04-21 23:30:14 +000092
Nate Begemana9795f82005-03-24 04:41:43 +000093 /// LowerCallTo - This hook lowers an abstract call to a function into an
94 /// actual call.
95 virtual std::pair<SDOperand, SDOperand>
Chris Lattnerc57f6822005-05-12 19:56:45 +000096 LowerCallTo(SDOperand Chain, const Type *RetTy, bool isVarArg, unsigned CC,
Chris Lattneradf6a962005-05-13 18:50:42 +000097 bool isTailCall, SDOperand Callee, ArgListTy &Args,
98 SelectionDAG &DAG);
Misha Brukmanb5f662f2005-04-21 23:30:14 +000099
Nate Begemana9795f82005-03-24 04:41:43 +0000100 virtual std::pair<SDOperand, SDOperand>
Chris Lattnerc1671e22005-07-01 23:11:56 +0000101 LowerVAStart(SDOperand Chain, SelectionDAG &DAG, SDOperand Dest);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000102
Nate Begemana9795f82005-03-24 04:41:43 +0000103 virtual std::pair<SDOperand,SDOperand>
Chris Lattnerc1671e22005-07-01 23:11:56 +0000104 LowerVAArgNext(SDOperand Chain, SDOperand VAList,
Nate Begemana9795f82005-03-24 04:41:43 +0000105 const Type *ArgTy, SelectionDAG &DAG);
106
107 virtual std::pair<SDOperand, SDOperand>
108 LowerFrameReturnAddress(bool isFrameAddr, SDOperand Chain, unsigned Depth,
109 SelectionDAG &DAG);
110 };
111}
112
113
114std::vector<SDOperand>
115PPC32TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
116 //
117 // add beautiful description of PPC stack frame format, or at least some docs
118 //
119 MachineFunction &MF = DAG.getMachineFunction();
120 MachineFrameInfo *MFI = MF.getFrameInfo();
121 MachineBasicBlock& BB = MF.front();
122 std::vector<SDOperand> ArgValues;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000123
124 // Due to the rather complicated nature of the PowerPC ABI, rather than a
Nate Begemana9795f82005-03-24 04:41:43 +0000125 // fixed size array of physical args, for the sake of simplicity let the STL
126 // handle tracking them for us.
127 std::vector<unsigned> argVR, argPR, argOp;
128 unsigned ArgOffset = 24;
129 unsigned GPR_remaining = 8;
130 unsigned FPR_remaining = 13;
131 unsigned GPR_idx = 0, FPR_idx = 0;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000132 static const unsigned GPR[] = {
Nate Begemana9795f82005-03-24 04:41:43 +0000133 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
134 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
135 };
136 static const unsigned FPR[] = {
137 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
138 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
139 };
140
141 // Add DAG nodes to load the arguments... On entry to a function on PPC,
142 // the arguments start at offset 24, although they are likely to be passed
143 // in registers.
144 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
145 SDOperand newroot, argt;
146 unsigned ObjSize;
147 bool needsLoad = false;
Nate Begemancd08e4c2005-04-09 20:09:12 +0000148 bool ArgLive = !I->use_empty();
Nate Begemana9795f82005-03-24 04:41:43 +0000149 MVT::ValueType ObjectVT = getValueType(I->getType());
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000150
Nate Begemana9795f82005-03-24 04:41:43 +0000151 switch (ObjectVT) {
152 default: assert(0 && "Unhandled argument type!");
153 case MVT::i1:
154 case MVT::i8:
155 case MVT::i16:
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000156 case MVT::i32:
Nate Begemana9795f82005-03-24 04:41:43 +0000157 ObjSize = 4;
Nate Begemancd08e4c2005-04-09 20:09:12 +0000158 if (!ArgLive) break;
Nate Begemana9795f82005-03-24 04:41:43 +0000159 if (GPR_remaining > 0) {
Nate Begemancd08e4c2005-04-09 20:09:12 +0000160 MF.addLiveIn(GPR[GPR_idx]);
Nate Begemanf70b5762005-03-28 23:08:54 +0000161 argt = newroot = DAG.getCopyFromReg(GPR[GPR_idx], MVT::i32,
162 DAG.getRoot());
Nate Begemana9795f82005-03-24 04:41:43 +0000163 if (ObjectVT != MVT::i32)
164 argt = DAG.getNode(ISD::TRUNCATE, ObjectVT, newroot);
Nate Begemana9795f82005-03-24 04:41:43 +0000165 } else {
166 needsLoad = true;
167 }
168 break;
Nate Begemanf7e43382005-03-26 07:46:36 +0000169 case MVT::i64: ObjSize = 8;
Nate Begemancd08e4c2005-04-09 20:09:12 +0000170 if (!ArgLive) break;
Nate Begemanc5b1cd22005-04-10 05:53:14 +0000171 if (GPR_remaining > 0) {
172 SDOperand argHi, argLo;
Nate Begemancd08e4c2005-04-09 20:09:12 +0000173 MF.addLiveIn(GPR[GPR_idx]);
Nate Begemanc5b1cd22005-04-10 05:53:14 +0000174 argHi = DAG.getCopyFromReg(GPR[GPR_idx], MVT::i32, DAG.getRoot());
175 // If we have two or more remaining argument registers, then both halves
176 // of the i64 can be sourced from there. Otherwise, the lower half will
177 // have to come off the stack. This can happen when an i64 is preceded
178 // by 28 bytes of arguments.
179 if (GPR_remaining > 1) {
180 MF.addLiveIn(GPR[GPR_idx+1]);
181 argLo = DAG.getCopyFromReg(GPR[GPR_idx+1], MVT::i32, argHi);
182 } else {
183 int FI = MFI->CreateFixedObject(4, ArgOffset+4);
184 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
Chris Lattner022ed322005-05-15 19:54:37 +0000185 argLo = DAG.getLoad(MVT::i32, DAG.getEntryNode(), FIN,
186 DAG.getSrcValue(NULL));
Nate Begemanc5b1cd22005-04-10 05:53:14 +0000187 }
Nate Begemanca12a2b2005-03-28 22:28:37 +0000188 // Build the outgoing arg thingy
Nate Begemanf70b5762005-03-28 23:08:54 +0000189 argt = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, argLo, argHi);
190 newroot = argLo;
Nate Begemana9795f82005-03-24 04:41:43 +0000191 } else {
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000192 needsLoad = true;
Nate Begemana9795f82005-03-24 04:41:43 +0000193 }
194 break;
Nate Begemancd08e4c2005-04-09 20:09:12 +0000195 case MVT::f32:
196 case MVT::f64:
197 ObjSize = (ObjectVT == MVT::f64) ? 8 : 4;
198 if (!ArgLive) break;
Nate Begemana9795f82005-03-24 04:41:43 +0000199 if (FPR_remaining > 0) {
Nate Begemancd08e4c2005-04-09 20:09:12 +0000200 MF.addLiveIn(FPR[FPR_idx]);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000201 argt = newroot = DAG.getCopyFromReg(FPR[FPR_idx], ObjectVT,
Nate Begemanf70b5762005-03-28 23:08:54 +0000202 DAG.getRoot());
Nate Begemana9795f82005-03-24 04:41:43 +0000203 --FPR_remaining;
204 ++FPR_idx;
205 } else {
206 needsLoad = true;
207 }
208 break;
209 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000210
Nate Begemana9795f82005-03-24 04:41:43 +0000211 // We need to load the argument to a virtual register if we determined above
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000212 // that we ran out of physical registers of the appropriate type
Nate Begemana9795f82005-03-24 04:41:43 +0000213 if (needsLoad) {
Nate Begemane5846682005-04-04 06:52:38 +0000214 unsigned SubregOffset = 0;
Nate Begemanc3e2db42005-04-04 09:09:00 +0000215 if (ObjectVT == MVT::i8 || ObjectVT == MVT::i1) SubregOffset = 3;
Nate Begemane5846682005-04-04 06:52:38 +0000216 if (ObjectVT == MVT::i16) SubregOffset = 2;
Nate Begemana9795f82005-03-24 04:41:43 +0000217 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
218 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000219 FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN,
Nate Begemane5846682005-04-04 06:52:38 +0000220 DAG.getConstant(SubregOffset, MVT::i32));
Chris Lattner022ed322005-05-15 19:54:37 +0000221 argt = newroot = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
222 DAG.getSrcValue(NULL));
Nate Begemana9795f82005-03-24 04:41:43 +0000223 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000224
Nate Begemana9795f82005-03-24 04:41:43 +0000225 // Every 4 bytes of argument space consumes one of the GPRs available for
226 // argument passing.
227 if (GPR_remaining > 0) {
228 unsigned delta = (GPR_remaining > 1 && ObjSize == 8) ? 2 : 1;
229 GPR_remaining -= delta;
230 GPR_idx += delta;
231 }
232 ArgOffset += ObjSize;
Chris Lattner91277ea2005-04-09 21:23:24 +0000233 if (newroot.Val)
234 DAG.setRoot(newroot.getValue(1));
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000235
Nate Begemana9795f82005-03-24 04:41:43 +0000236 ArgValues.push_back(argt);
237 }
238
Nate Begemana9795f82005-03-24 04:41:43 +0000239 // If the function takes variable number of arguments, make a frame index for
240 // the start of the first vararg value... for expansion of llvm.va_start.
Nate Begemanfa554702005-04-03 22:13:27 +0000241 if (F.isVarArg()) {
Nate Begemana9795f82005-03-24 04:41:43 +0000242 VarArgsFrameIndex = MFI->CreateFixedObject(4, ArgOffset);
Nate Begemanfa554702005-04-03 22:13:27 +0000243 SDOperand FIN = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
Nate Begeman6644d4c2005-04-03 23:11:17 +0000244 // If this function is vararg, store any remaining integer argument regs
245 // to their spots on the stack so that they may be loaded by deferencing the
246 // result of va_next.
247 std::vector<SDOperand> MemOps;
248 for (; GPR_remaining > 0; --GPR_remaining, ++GPR_idx) {
Nate Begemancd08e4c2005-04-09 20:09:12 +0000249 MF.addLiveIn(GPR[GPR_idx]);
Nate Begeman6644d4c2005-04-03 23:11:17 +0000250 SDOperand Val = DAG.getCopyFromReg(GPR[GPR_idx], MVT::i32, DAG.getRoot());
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000251 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Val.getValue(1),
Andrew Lenharth2d86ea22005-04-27 20:10:01 +0000252 Val, FIN, DAG.getSrcValue(NULL));
Nate Begeman6644d4c2005-04-03 23:11:17 +0000253 MemOps.push_back(Store);
254 // Increment the address by four for the next argument to store
255 SDOperand PtrOff = DAG.getConstant(4, getPointerTy());
256 FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN, PtrOff);
257 }
258 DAG.setRoot(DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps));
Nate Begemanfa554702005-04-03 22:13:27 +0000259 }
Nate Begemana9795f82005-03-24 04:41:43 +0000260
Nate Begemancd08e4c2005-04-09 20:09:12 +0000261 // Finally, inform the code generator which regs we return values in.
262 switch (getValueType(F.getReturnType())) {
263 default: assert(0 && "Unknown type!");
264 case MVT::isVoid: break;
265 case MVT::i1:
266 case MVT::i8:
267 case MVT::i16:
268 case MVT::i32:
269 MF.addLiveOut(PPC::R3);
270 break;
271 case MVT::i64:
272 MF.addLiveOut(PPC::R3);
273 MF.addLiveOut(PPC::R4);
274 break;
275 case MVT::f32:
276 case MVT::f64:
277 MF.addLiveOut(PPC::F1);
278 break;
279 }
280
Nate Begemana9795f82005-03-24 04:41:43 +0000281 return ArgValues;
282}
283
284std::pair<SDOperand, SDOperand>
285PPC32TargetLowering::LowerCallTo(SDOperand Chain,
Misha Brukman7847fca2005-04-22 17:54:37 +0000286 const Type *RetTy, bool isVarArg,
Chris Lattneradf6a962005-05-13 18:50:42 +0000287 unsigned CallingConv, bool isTailCall,
Misha Brukman7847fca2005-04-22 17:54:37 +0000288 SDOperand Callee, ArgListTy &Args,
289 SelectionDAG &DAG) {
Nate Begeman307e7442005-03-26 01:28:53 +0000290 // args_to_use will accumulate outgoing args for the ISD::CALL case in
291 // SelectExpr to use to put the arguments in the appropriate registers.
Nate Begemana9795f82005-03-24 04:41:43 +0000292 std::vector<SDOperand> args_to_use;
Nate Begeman307e7442005-03-26 01:28:53 +0000293
294 // Count how many bytes are to be pushed on the stack, including the linkage
295 // area, and parameter passing area.
296 unsigned NumBytes = 24;
297
298 if (Args.empty()) {
Chris Lattner16cd04d2005-05-12 23:24:06 +0000299 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
Nate Begemana7e11a42005-04-01 05:57:17 +0000300 DAG.getConstant(NumBytes, getPointerTy()));
Nate Begeman307e7442005-03-26 01:28:53 +0000301 } else {
302 for (unsigned i = 0, e = Args.size(); i != e; ++i)
303 switch (getValueType(Args[i].second)) {
304 default: assert(0 && "Unknown value type!");
305 case MVT::i1:
306 case MVT::i8:
307 case MVT::i16:
308 case MVT::i32:
309 case MVT::f32:
310 NumBytes += 4;
311 break;
312 case MVT::i64:
313 case MVT::f64:
314 NumBytes += 8;
315 break;
316 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000317
318 // Just to be safe, we'll always reserve the full 24 bytes of linkage area
Nate Begeman307e7442005-03-26 01:28:53 +0000319 // plus 32 bytes of argument space in case any called code gets funky on us.
320 if (NumBytes < 56) NumBytes = 56;
321
322 // Adjust the stack pointer for the new arguments...
323 // These operations are automatically eliminated by the prolog/epilog pass
Chris Lattner16cd04d2005-05-12 23:24:06 +0000324 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
Nate Begeman307e7442005-03-26 01:28:53 +0000325 DAG.getConstant(NumBytes, getPointerTy()));
326
327 // Set up a copy of the stack pointer for use loading and storing any
328 // arguments that may not fit in the registers available for argument
329 // passing.
330 SDOperand StackPtr = DAG.getCopyFromReg(PPC::R1, MVT::i32,
331 DAG.getEntryNode());
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000332
Nate Begeman307e7442005-03-26 01:28:53 +0000333 // Figure out which arguments are going to go in registers, and which in
334 // memory. Also, if this is a vararg function, floating point operations
335 // must be stored to our stack, and loaded into integer regs as well, if
336 // any integer regs are available for argument passing.
337 unsigned ArgOffset = 24;
338 unsigned GPR_remaining = 8;
339 unsigned FPR_remaining = 13;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000340
Nate Begeman74d73452005-03-31 00:15:26 +0000341 std::vector<SDOperand> MemOps;
Nate Begeman307e7442005-03-26 01:28:53 +0000342 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
343 // PtrOff will be used to store the current argument to the stack if a
344 // register cannot be found for it.
345 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
346 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
Nate Begemanf7e43382005-03-26 07:46:36 +0000347 MVT::ValueType ArgVT = getValueType(Args[i].second);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000348
Nate Begemanf7e43382005-03-26 07:46:36 +0000349 switch (ArgVT) {
Nate Begeman307e7442005-03-26 01:28:53 +0000350 default: assert(0 && "Unexpected ValueType for argument!");
351 case MVT::i1:
352 case MVT::i8:
353 case MVT::i16:
354 // Promote the integer to 32 bits. If the input type is signed use a
355 // sign extend, otherwise use a zero extend.
356 if (Args[i].second->isSigned())
357 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
358 else
359 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
360 // FALL THROUGH
361 case MVT::i32:
362 if (GPR_remaining > 0) {
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000363 args_to_use.push_back(Args[i].first);
Nate Begeman307e7442005-03-26 01:28:53 +0000364 --GPR_remaining;
365 } else {
Nate Begeman74d73452005-03-31 00:15:26 +0000366 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
Chris Lattner022ed322005-05-15 19:54:37 +0000367 Args[i].first, PtrOff,
368 DAG.getSrcValue(NULL)));
Nate Begeman307e7442005-03-26 01:28:53 +0000369 }
370 ArgOffset += 4;
371 break;
372 case MVT::i64:
Nate Begemanf7e43382005-03-26 07:46:36 +0000373 // If we have one free GPR left, we can place the upper half of the i64
374 // in it, and store the other half to the stack. If we have two or more
375 // free GPRs, then we can pass both halves of the i64 in registers.
376 if (GPR_remaining > 0) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000377 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
Nate Begemanf2622612005-03-26 02:17:46 +0000378 Args[i].first, DAG.getConstant(1, MVT::i32));
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000379 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
Nate Begemanf2622612005-03-26 02:17:46 +0000380 Args[i].first, DAG.getConstant(0, MVT::i32));
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000381 args_to_use.push_back(Hi);
Nate Begeman74d73452005-03-31 00:15:26 +0000382 --GPR_remaining;
Nate Begeman74d73452005-03-31 00:15:26 +0000383 if (GPR_remaining > 0) {
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000384 args_to_use.push_back(Lo);
Nate Begeman74d73452005-03-31 00:15:26 +0000385 --GPR_remaining;
Nate Begemanf7e43382005-03-26 07:46:36 +0000386 } else {
387 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
388 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
Nate Begeman74d73452005-03-31 00:15:26 +0000389 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +0000390 Lo, PtrOff, DAG.getSrcValue(NULL)));
Nate Begemanf7e43382005-03-26 07:46:36 +0000391 }
Nate Begeman307e7442005-03-26 01:28:53 +0000392 } else {
Nate Begeman74d73452005-03-31 00:15:26 +0000393 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
Chris Lattner022ed322005-05-15 19:54:37 +0000394 Args[i].first, PtrOff,
395 DAG.getSrcValue(NULL)));
Nate Begeman307e7442005-03-26 01:28:53 +0000396 }
397 ArgOffset += 8;
398 break;
399 case MVT::f32:
Nate Begeman307e7442005-03-26 01:28:53 +0000400 case MVT::f64:
Nate Begemanf7e43382005-03-26 07:46:36 +0000401 if (FPR_remaining > 0) {
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000402 args_to_use.push_back(Args[i].first);
403 --FPR_remaining;
Nate Begemanf7e43382005-03-26 07:46:36 +0000404 if (isVarArg) {
Nate Begeman96fc6812005-03-31 02:05:53 +0000405 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Chain,
Chris Lattner022ed322005-05-15 19:54:37 +0000406 Args[i].first, PtrOff,
407 DAG.getSrcValue(NULL));
Nate Begeman96fc6812005-03-31 02:05:53 +0000408 MemOps.push_back(Store);
Nate Begeman74d73452005-03-31 00:15:26 +0000409 // Float varargs are always shadowed in available integer registers
410 if (GPR_remaining > 0) {
Chris Lattner022ed322005-05-15 19:54:37 +0000411 SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff,
412 DAG.getSrcValue(NULL));
Nate Begeman74d73452005-03-31 00:15:26 +0000413 MemOps.push_back(Load);
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000414 args_to_use.push_back(Load);
415 --GPR_remaining;
Nate Begeman74d73452005-03-31 00:15:26 +0000416 }
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000417 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
Nate Begeman74d73452005-03-31 00:15:26 +0000418 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
419 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
Chris Lattner022ed322005-05-15 19:54:37 +0000420 SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff,
421 DAG.getSrcValue(NULL));
Nate Begeman74d73452005-03-31 00:15:26 +0000422 MemOps.push_back(Load);
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000423 args_to_use.push_back(Load);
424 --GPR_remaining;
Nate Begeman74d73452005-03-31 00:15:26 +0000425 }
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000426 } else {
427 // If we have any FPRs remaining, we may also have GPRs remaining.
428 // Args passed in FPRs consume either 1 (f32) or 2 (f64) available
429 // GPRs.
430 if (GPR_remaining > 0) {
431 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
432 --GPR_remaining;
433 }
434 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
435 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
436 --GPR_remaining;
437 }
Nate Begeman74d73452005-03-31 00:15:26 +0000438 }
Nate Begeman307e7442005-03-26 01:28:53 +0000439 } else {
Nate Begeman74d73452005-03-31 00:15:26 +0000440 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
Chris Lattner022ed322005-05-15 19:54:37 +0000441 Args[i].first, PtrOff,
442 DAG.getSrcValue(NULL)));
Nate Begeman307e7442005-03-26 01:28:53 +0000443 }
Nate Begemanf7e43382005-03-26 07:46:36 +0000444 ArgOffset += (ArgVT == MVT::f32) ? 4 : 8;
Nate Begeman307e7442005-03-26 01:28:53 +0000445 break;
446 }
Nate Begemana9795f82005-03-24 04:41:43 +0000447 }
Nate Begeman74d73452005-03-31 00:15:26 +0000448 if (!MemOps.empty())
449 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps);
Nate Begemana9795f82005-03-24 04:41:43 +0000450 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000451
Nate Begemana9795f82005-03-24 04:41:43 +0000452 std::vector<MVT::ValueType> RetVals;
453 MVT::ValueType RetTyVT = getValueType(RetTy);
454 if (RetTyVT != MVT::isVoid)
455 RetVals.push_back(RetTyVT);
456 RetVals.push_back(MVT::Other);
457
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000458 SDOperand TheCall = SDOperand(DAG.getCall(RetVals,
Nate Begemana9795f82005-03-24 04:41:43 +0000459 Chain, Callee, args_to_use), 0);
460 Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
Chris Lattner16cd04d2005-05-12 23:24:06 +0000461 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain,
Nate Begemana9795f82005-03-24 04:41:43 +0000462 DAG.getConstant(NumBytes, getPointerTy()));
463 return std::make_pair(TheCall, Chain);
464}
465
466std::pair<SDOperand, SDOperand>
Chris Lattnerf84a2ac2005-07-05 17:48:31 +0000467PPC32TargetLowering::LowerVAStart(SDOperand Chain, SelectionDAG &DAG,
468 SDOperand Dest) {
469 // vastart just stores the address of the VarArgsFrameIndex slot into the
470 // memory location argument.
471 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
472 SDOperand Result = DAG.getNode(ISD::STORE, MVT::Other, Chain, FR, Dest,
473 DAG.getSrcValue(NULL));
474 return std::make_pair(Result, Result);
Nate Begemana9795f82005-03-24 04:41:43 +0000475}
476
477std::pair<SDOperand,SDOperand> PPC32TargetLowering::
Chris Lattnerf84a2ac2005-07-05 17:48:31 +0000478LowerVAArgNext(SDOperand Chain, SDOperand VAArgOp,
Nate Begemana9795f82005-03-24 04:41:43 +0000479 const Type *ArgTy, SelectionDAG &DAG) {
Nate Begemanc7b09f12005-03-25 08:34:25 +0000480 MVT::ValueType ArgVT = getValueType(ArgTy);
Chris Lattnerf84a2ac2005-07-05 17:48:31 +0000481
482 SDOperand VAList =
483 DAG.getLoad(MVT::i32, Chain, VAArgOp, DAG.getSrcValue(NULL));
484 SDOperand Result = DAG.getLoad(ArgVT, VAList.getValue(1), VAList,
485 DAG.getSrcValue(NULL));
486 unsigned Amt;
487 if (ArgVT == MVT::i32 || ArgVT == MVT::f32)
488 Amt = 4;
489 else {
490 assert((ArgVT == MVT::i64 || ArgVT == MVT::f64) &&
491 "Other types should have been promoted for varargs!");
492 Amt = 8;
Nate Begemanc7b09f12005-03-25 08:34:25 +0000493 }
Chris Lattnerf84a2ac2005-07-05 17:48:31 +0000494 VAList = DAG.getNode(ISD::ADD, VAList.getValueType(), VAList,
495 DAG.getConstant(Amt, VAList.getValueType()));
496 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain,
497 VAList, VAArgOp, DAG.getSrcValue(NULL));
Nate Begemanc7b09f12005-03-25 08:34:25 +0000498 return std::make_pair(Result, Chain);
Nate Begemana9795f82005-03-24 04:41:43 +0000499}
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000500
Nate Begemana9795f82005-03-24 04:41:43 +0000501
502std::pair<SDOperand, SDOperand> PPC32TargetLowering::
503LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
504 SelectionDAG &DAG) {
Nate Begeman01d05262005-03-30 01:45:43 +0000505 assert(0 && "LowerFrameReturnAddress unimplemented");
Nate Begemana9795f82005-03-24 04:41:43 +0000506 abort();
507}
508
509namespace {
Nate Begemanc7bd4822005-04-11 06:34:10 +0000510Statistic<>Recorded("ppc-codegen", "Number of recording ops emitted");
Nate Begeman93075ec2005-04-04 23:40:36 +0000511Statistic<>FusedFP("ppc-codegen", "Number of fused fp operations");
Nate Begeman7bfba7d2005-04-14 09:45:08 +0000512Statistic<>MultiBranch("ppc-codegen", "Number of setcc logical ops collapsed");
Nate Begemana9795f82005-03-24 04:41:43 +0000513//===--------------------------------------------------------------------===//
514/// ISel - PPC32 specific code to select PPC32 machine instructions for
515/// SelectionDAG operations.
516//===--------------------------------------------------------------------===//
517class ISel : public SelectionDAGISel {
Nate Begemana9795f82005-03-24 04:41:43 +0000518 PPC32TargetLowering PPC32Lowering;
Nate Begeman815d6da2005-04-06 00:25:27 +0000519 SelectionDAG *ISelDAG; // Hack to support us having a dag->dag transform
520 // for sdiv and udiv until it is put into the future
521 // dag combiner.
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000522
Nate Begemana9795f82005-03-24 04:41:43 +0000523 /// ExprMap - As shared expressions are codegen'd, we keep track of which
524 /// vreg the value is produced in, so we only emit one copy of each compiled
525 /// tree.
526 std::map<SDOperand, unsigned> ExprMap;
Nate Begemanc7b09f12005-03-25 08:34:25 +0000527
528 unsigned GlobalBaseReg;
529 bool GlobalBaseInitialized;
Nate Begemanc7bd4822005-04-11 06:34:10 +0000530 bool RecordSuccess;
Nate Begemana9795f82005-03-24 04:41:43 +0000531public:
Nate Begeman815d6da2005-04-06 00:25:27 +0000532 ISel(TargetMachine &TM) : SelectionDAGISel(PPC32Lowering), PPC32Lowering(TM),
533 ISelDAG(0) {}
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000534
Nate Begemanc7b09f12005-03-25 08:34:25 +0000535 /// runOnFunction - Override this function in order to reset our per-function
536 /// variables.
537 virtual bool runOnFunction(Function &Fn) {
538 // Make sure we re-emit a set of the global base reg if necessary
539 GlobalBaseInitialized = false;
540 return SelectionDAGISel::runOnFunction(Fn);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000541 }
542
Nate Begemana9795f82005-03-24 04:41:43 +0000543 /// InstructionSelectBasicBlock - This callback is invoked by
544 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
545 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG) {
546 DEBUG(BB->dump());
547 // Codegen the basic block.
Nate Begeman815d6da2005-04-06 00:25:27 +0000548 ISelDAG = &DAG;
Nate Begemana9795f82005-03-24 04:41:43 +0000549 Select(DAG.getRoot());
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000550
Nate Begemana9795f82005-03-24 04:41:43 +0000551 // Clear state used for selection.
552 ExprMap.clear();
Nate Begeman815d6da2005-04-06 00:25:27 +0000553 ISelDAG = 0;
Nate Begemana9795f82005-03-24 04:41:43 +0000554 }
Nate Begeman815d6da2005-04-06 00:25:27 +0000555
556 // dag -> dag expanders for integer divide by constant
557 SDOperand BuildSDIVSequence(SDOperand N);
558 SDOperand BuildUDIVSequence(SDOperand N);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000559
Nate Begemandffcfcc2005-04-01 00:32:34 +0000560 unsigned getGlobalBaseReg();
Nate Begeman6b559972005-04-01 02:59:27 +0000561 unsigned getConstDouble(double floatVal, unsigned Result);
Nate Begeman1cbf3ab2005-04-18 07:48:09 +0000562 void MoveCRtoGPR(unsigned CCReg, bool Inv, unsigned Idx, unsigned Result);
Nate Begeman7ddecb42005-04-06 23:51:40 +0000563 bool SelectBitfieldInsert(SDOperand OR, unsigned Result);
Nate Begeman3664cef2005-04-13 22:14:14 +0000564 unsigned FoldIfWideZeroExtend(SDOperand N);
Nate Begeman1cbf3ab2005-04-18 07:48:09 +0000565 unsigned SelectCC(SDOperand CC, unsigned &Opc, bool &Inv, unsigned &Idx);
566 unsigned SelectCCExpr(SDOperand N, unsigned& Opc, bool &Inv, unsigned &Idx);
Nate Begemanc7bd4822005-04-11 06:34:10 +0000567 unsigned SelectExpr(SDOperand N, bool Recording=false);
Nate Begemana9795f82005-03-24 04:41:43 +0000568 unsigned SelectExprFP(SDOperand N, unsigned Result);
569 void Select(SDOperand N);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000570
Nate Begeman04730362005-04-01 04:45:11 +0000571 bool SelectAddr(SDOperand N, unsigned& Reg, int& offset);
Nate Begemana9795f82005-03-24 04:41:43 +0000572 void SelectBranchCC(SDOperand N);
573};
574
Nate Begeman80196b12005-04-05 00:15:08 +0000575/// ExactLog2 - This function solves for (Val == 1 << (N-1)) and returns N. It
576/// returns zero when the input is not exactly a power of two.
577static unsigned ExactLog2(unsigned Val) {
578 if (Val == 0 || (Val & (Val-1))) return 0;
579 unsigned Count = 0;
580 while (Val != 1) {
581 Val >>= 1;
582 ++Count;
583 }
584 return Count;
585}
586
Nate Begeman7ddecb42005-04-06 23:51:40 +0000587// IsRunOfOnes - returns true if Val consists of one contiguous run of 1's with
588// any number of 0's on either side. the 1's are allowed to wrap from LSB to
589// MSB. so 0x000FFF0, 0x0000FFFF, and 0xFF0000FF are all runs. 0x0F0F0000 is
590// not, since all 1's are not contiguous.
591static bool IsRunOfOnes(unsigned Val, unsigned &MB, unsigned &ME) {
592 bool isRun = true;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000593 MB = 0;
Nate Begeman7ddecb42005-04-06 23:51:40 +0000594 ME = 0;
595
596 // look for first set bit
597 int i = 0;
598 for (; i < 32; i++) {
599 if ((Val & (1 << (31 - i))) != 0) {
600 MB = i;
601 ME = i;
602 break;
603 }
604 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000605
Nate Begeman7ddecb42005-04-06 23:51:40 +0000606 // look for last set bit
607 for (; i < 32; i++) {
608 if ((Val & (1 << (31 - i))) == 0)
609 break;
610 ME = i;
611 }
612
613 // look for next set bit
614 for (; i < 32; i++) {
615 if ((Val & (1 << (31 - i))) != 0)
616 break;
617 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000618
Nate Begeman7ddecb42005-04-06 23:51:40 +0000619 // if we exhausted all the bits, we found a match at this point for 0*1*0*
620 if (i == 32)
621 return true;
622
623 // since we just encountered more 1's, if it doesn't wrap around to the
624 // most significant bit of the word, then we did not find a match to 1*0*1* so
625 // exit.
626 if (MB != 0)
627 return false;
628
629 // look for last set bit
630 for (MB = i; i < 32; i++) {
631 if ((Val & (1 << (31 - i))) == 0)
632 break;
633 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000634
Nate Begeman7ddecb42005-04-06 23:51:40 +0000635 // if we exhausted all the bits, then we found a match for 1*0*1*, otherwise,
636 // the value is not a run of ones.
637 if (i == 32)
638 return true;
639 return false;
640}
641
Nate Begeman439b4442005-04-05 04:22:58 +0000642/// getImmediateForOpcode - This method returns a value indicating whether
Nate Begemana9795f82005-03-24 04:41:43 +0000643/// the ConstantSDNode N can be used as an immediate to Opcode. The return
644/// values are either 0, 1 or 2. 0 indicates that either N is not a
Nate Begeman9f833d32005-04-12 00:10:02 +0000645/// ConstantSDNode, or is not suitable for use by that opcode.
646/// Return value codes for turning into an enum someday:
647/// 1: constant may be used in normal immediate form.
648/// 2: constant may be used in shifted immediate form.
649/// 3: log base 2 of the constant may be used.
650/// 4: constant is suitable for integer division conversion
651/// 5: constant is a bitfield mask
Nate Begemana9795f82005-03-24 04:41:43 +0000652///
Nate Begeman439b4442005-04-05 04:22:58 +0000653static unsigned getImmediateForOpcode(SDOperand N, unsigned Opcode,
654 unsigned& Imm, bool U = false) {
Nate Begemana9795f82005-03-24 04:41:43 +0000655 if (N.getOpcode() != ISD::Constant) return 0;
656
657 int v = (int)cast<ConstantSDNode>(N)->getSignExtended();
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000658
Nate Begemana9795f82005-03-24 04:41:43 +0000659 switch(Opcode) {
660 default: return 0;
661 case ISD::ADD:
662 if (v <= 32767 && v >= -32768) { Imm = v & 0xFFFF; return 1; }
663 if ((v & 0x0000FFFF) == 0) { Imm = v >> 16; return 2; }
664 break;
Nate Begeman9f833d32005-04-12 00:10:02 +0000665 case ISD::AND: {
666 unsigned MB, ME;
667 if (IsRunOfOnes(v, MB, ME)) { Imm = MB << 16 | ME & 0xFFFF; return 5; }
668 if (v >= 0 && v <= 65535) { Imm = v & 0xFFFF; return 1; }
669 if ((v & 0x0000FFFF) == 0) { Imm = v >> 16; return 2; }
670 break;
671 }
Nate Begemana9795f82005-03-24 04:41:43 +0000672 case ISD::XOR:
673 case ISD::OR:
674 if (v >= 0 && v <= 65535) { Imm = v & 0xFFFF; return 1; }
675 if ((v & 0x0000FFFF) == 0) { Imm = v >> 16; return 2; }
676 break;
Nate Begeman307e7442005-03-26 01:28:53 +0000677 case ISD::MUL:
678 if (v <= 32767 && v >= -32768) { Imm = v & 0xFFFF; return 1; }
679 break;
Nate Begemand7c4a4a2005-05-11 23:43:56 +0000680 case ISD::SUB:
681 // handle subtract-from separately from subtract, since subi is really addi
682 if (U && v <= 32767 && v >= -32768) { Imm = v & 0xFFFF; return 1; }
683 if (!U && v <= 32768 && v >= -32767) { Imm = (-v) & 0xFFFF; return 1; }
684 break;
Nate Begeman3e897162005-03-31 23:55:40 +0000685 case ISD::SETCC:
686 if (U && (v >= 0 && v <= 65535)) { Imm = v & 0xFFFF; return 1; }
687 if (!U && (v <= 32767 && v >= -32768)) { Imm = v & 0xFFFF; return 1; }
688 break;
Nate Begeman80196b12005-04-05 00:15:08 +0000689 case ISD::SDIV:
Nate Begeman439b4442005-04-05 04:22:58 +0000690 if ((Imm = ExactLog2(v))) { return 3; }
Nate Begeman9f833d32005-04-12 00:10:02 +0000691 if ((Imm = ExactLog2(-v))) { Imm = -Imm; return 3; }
Nate Begeman815d6da2005-04-06 00:25:27 +0000692 if (v <= -2 || v >= 2) { return 4; }
693 break;
694 case ISD::UDIV:
Nate Begeman27b4c232005-04-06 06:44:57 +0000695 if (v > 1) { return 4; }
Nate Begeman80196b12005-04-05 00:15:08 +0000696 break;
Nate Begemana9795f82005-03-24 04:41:43 +0000697 }
698 return 0;
699}
Nate Begeman3e897162005-03-31 23:55:40 +0000700
Nate Begemanc7bd4822005-04-11 06:34:10 +0000701/// NodeHasRecordingVariant - If SelectExpr can always produce code for
702/// NodeOpcode that also sets CR0 as a side effect, return true. Otherwise,
703/// return false.
704static bool NodeHasRecordingVariant(unsigned NodeOpcode) {
705 switch(NodeOpcode) {
706 default: return false;
707 case ISD::AND:
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000708 case ISD::OR:
Chris Lattner519f40b2005-04-13 02:46:17 +0000709 return true;
Nate Begemanc7bd4822005-04-11 06:34:10 +0000710 }
711}
712
Nate Begeman3e897162005-03-31 23:55:40 +0000713/// getBCCForSetCC - Returns the PowerPC condition branch mnemonic corresponding
714/// to Condition. If the Condition is unordered or unsigned, the bool argument
715/// U is set to true, otherwise it is set to false.
716static unsigned getBCCForSetCC(unsigned Condition, bool& U) {
717 U = false;
718 switch (Condition) {
719 default: assert(0 && "Unknown condition!"); abort();
720 case ISD::SETEQ: return PPC::BEQ;
721 case ISD::SETNE: return PPC::BNE;
722 case ISD::SETULT: U = true;
723 case ISD::SETLT: return PPC::BLT;
724 case ISD::SETULE: U = true;
725 case ISD::SETLE: return PPC::BLE;
726 case ISD::SETUGT: U = true;
727 case ISD::SETGT: return PPC::BGT;
728 case ISD::SETUGE: U = true;
729 case ISD::SETGE: return PPC::BGE;
730 }
Nate Begeman04730362005-04-01 04:45:11 +0000731 return 0;
732}
733
Nate Begeman7bfba7d2005-04-14 09:45:08 +0000734/// getCROpForOp - Return the condition register opcode (or inverted opcode)
735/// associated with the SelectionDAG opcode.
736static unsigned getCROpForSetCC(unsigned Opcode, bool Inv1, bool Inv2) {
737 switch (Opcode) {
738 default: assert(0 && "Unknown opcode!"); abort();
739 case ISD::AND:
740 if (Inv1 && Inv2) return PPC::CRNOR; // De Morgan's Law
741 if (!Inv1 && !Inv2) return PPC::CRAND;
742 if (Inv1 ^ Inv2) return PPC::CRANDC;
743 case ISD::OR:
744 if (Inv1 && Inv2) return PPC::CRNAND; // De Morgan's Law
745 if (!Inv1 && !Inv2) return PPC::CROR;
746 if (Inv1 ^ Inv2) return PPC::CRORC;
747 }
748 return 0;
749}
750
751/// getCRIdxForSetCC - Return the index of the condition register field
752/// associated with the SetCC condition, and whether or not the field is
753/// treated as inverted. That is, lt = 0; ge = 0 inverted.
754static unsigned getCRIdxForSetCC(unsigned Condition, bool& Inv) {
755 switch (Condition) {
756 default: assert(0 && "Unknown condition!"); abort();
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000757 case ISD::SETULT:
Nate Begeman7bfba7d2005-04-14 09:45:08 +0000758 case ISD::SETLT: Inv = false; return 0;
759 case ISD::SETUGE:
760 case ISD::SETGE: Inv = true; return 0;
761 case ISD::SETUGT:
762 case ISD::SETGT: Inv = false; return 1;
763 case ISD::SETULE:
764 case ISD::SETLE: Inv = true; return 1;
765 case ISD::SETEQ: Inv = false; return 2;
766 case ISD::SETNE: Inv = true; return 2;
767 }
768 return 0;
769}
770
Nate Begeman04730362005-04-01 04:45:11 +0000771/// IndexedOpForOp - Return the indexed variant for each of the PowerPC load
772/// and store immediate instructions.
773static unsigned IndexedOpForOp(unsigned Opcode) {
774 switch(Opcode) {
775 default: assert(0 && "Unknown opcode!"); abort();
776 case PPC::LBZ: return PPC::LBZX; case PPC::STB: return PPC::STBX;
777 case PPC::LHZ: return PPC::LHZX; case PPC::STH: return PPC::STHX;
778 case PPC::LHA: return PPC::LHAX; case PPC::STW: return PPC::STWX;
779 case PPC::LWZ: return PPC::LWZX; case PPC::STFS: return PPC::STFSX;
780 case PPC::LFS: return PPC::LFSX; case PPC::STFD: return PPC::STFDX;
781 case PPC::LFD: return PPC::LFDX;
782 }
783 return 0;
Nate Begeman3e897162005-03-31 23:55:40 +0000784}
Nate Begeman815d6da2005-04-06 00:25:27 +0000785
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000786// Structure used to return the necessary information to codegen an SDIV as
Nate Begeman815d6da2005-04-06 00:25:27 +0000787// a multiply.
788struct ms {
789 int m; // magic number
790 int s; // shift amount
791};
792
793struct mu {
794 unsigned int m; // magic number
795 int a; // add indicator
796 int s; // shift amount
797};
798
799/// magic - calculate the magic numbers required to codegen an integer sdiv as
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000800/// a sequence of multiply and shifts. Requires that the divisor not be 0, 1,
Nate Begeman815d6da2005-04-06 00:25:27 +0000801/// or -1.
802static struct ms magic(int d) {
803 int p;
804 unsigned int ad, anc, delta, q1, r1, q2, r2, t;
805 const unsigned int two31 = 2147483648U; // 2^31
806 struct ms mag;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000807
Nate Begeman815d6da2005-04-06 00:25:27 +0000808 ad = abs(d);
809 t = two31 + ((unsigned int)d >> 31);
810 anc = t - 1 - t%ad; // absolute value of nc
811 p = 31; // initialize p
812 q1 = two31/anc; // initialize q1 = 2p/abs(nc)
813 r1 = two31 - q1*anc; // initialize r1 = rem(2p,abs(nc))
814 q2 = two31/ad; // initialize q2 = 2p/abs(d)
815 r2 = two31 - q2*ad; // initialize r2 = rem(2p,abs(d))
816 do {
817 p = p + 1;
818 q1 = 2*q1; // update q1 = 2p/abs(nc)
819 r1 = 2*r1; // update r1 = rem(2p/abs(nc))
820 if (r1 >= anc) { // must be unsigned comparison
821 q1 = q1 + 1;
822 r1 = r1 - anc;
823 }
824 q2 = 2*q2; // update q2 = 2p/abs(d)
825 r2 = 2*r2; // update r2 = rem(2p/abs(d))
826 if (r2 >= ad) { // must be unsigned comparison
827 q2 = q2 + 1;
828 r2 = r2 - ad;
829 }
830 delta = ad - r2;
831 } while (q1 < delta || (q1 == delta && r1 == 0));
832
833 mag.m = q2 + 1;
834 if (d < 0) mag.m = -mag.m; // resulting magic number
835 mag.s = p - 32; // resulting shift
836 return mag;
837}
838
839/// magicu - calculate the magic numbers required to codegen an integer udiv as
840/// a sequence of multiply, add and shifts. Requires that the divisor not be 0.
841static struct mu magicu(unsigned d)
842{
843 int p;
844 unsigned int nc, delta, q1, r1, q2, r2;
845 struct mu magu;
846 magu.a = 0; // initialize "add" indicator
847 nc = - 1 - (-d)%d;
848 p = 31; // initialize p
849 q1 = 0x80000000/nc; // initialize q1 = 2p/nc
850 r1 = 0x80000000 - q1*nc; // initialize r1 = rem(2p,nc)
851 q2 = 0x7FFFFFFF/d; // initialize q2 = (2p-1)/d
852 r2 = 0x7FFFFFFF - q2*d; // initialize r2 = rem((2p-1),d)
853 do {
854 p = p + 1;
855 if (r1 >= nc - r1 ) {
856 q1 = 2*q1 + 1; // update q1
857 r1 = 2*r1 - nc; // update r1
858 }
859 else {
860 q1 = 2*q1; // update q1
861 r1 = 2*r1; // update r1
862 }
863 if (r2 + 1 >= d - r2) {
864 if (q2 >= 0x7FFFFFFF) magu.a = 1;
865 q2 = 2*q2 + 1; // update q2
866 r2 = 2*r2 + 1 - d; // update r2
867 }
868 else {
869 if (q2 >= 0x80000000) magu.a = 1;
870 q2 = 2*q2; // update q2
871 r2 = 2*r2 + 1; // update r2
872 }
873 delta = d - 1 - r2;
874 } while (p < 64 && (q1 < delta || (q1 == delta && r1 == 0)));
875 magu.m = q2 + 1; // resulting magic number
876 magu.s = p - 32; // resulting shift
877 return magu;
878}
879}
880
881/// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
882/// return a DAG expression to select that will generate the same value by
883/// multiplying by a magic number. See:
884/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
885SDOperand ISel::BuildSDIVSequence(SDOperand N) {
886 int d = (int)cast<ConstantSDNode>(N.getOperand(1))->getSignExtended();
887 ms magics = magic(d);
888 // Multiply the numerator (operand 0) by the magic value
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000889 SDOperand Q = ISelDAG->getNode(ISD::MULHS, MVT::i32, N.getOperand(0),
Nate Begeman815d6da2005-04-06 00:25:27 +0000890 ISelDAG->getConstant(magics.m, MVT::i32));
891 // If d > 0 and m < 0, add the numerator
892 if (d > 0 && magics.m < 0)
893 Q = ISelDAG->getNode(ISD::ADD, MVT::i32, Q, N.getOperand(0));
894 // If d < 0 and m > 0, subtract the numerator.
895 if (d < 0 && magics.m > 0)
896 Q = ISelDAG->getNode(ISD::SUB, MVT::i32, Q, N.getOperand(0));
897 // Shift right algebraic if shift value is nonzero
898 if (magics.s > 0)
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000899 Q = ISelDAG->getNode(ISD::SRA, MVT::i32, Q,
Nate Begeman815d6da2005-04-06 00:25:27 +0000900 ISelDAG->getConstant(magics.s, MVT::i32));
901 // Extract the sign bit and add it to the quotient
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000902 SDOperand T =
Nate Begeman815d6da2005-04-06 00:25:27 +0000903 ISelDAG->getNode(ISD::SRL, MVT::i32, Q, ISelDAG->getConstant(31, MVT::i32));
Nate Begeman27b4c232005-04-06 06:44:57 +0000904 return ISelDAG->getNode(ISD::ADD, MVT::i32, Q, T);
Nate Begeman815d6da2005-04-06 00:25:27 +0000905}
906
907/// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
908/// return a DAG expression to select that will generate the same value by
909/// multiplying by a magic number. See:
910/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
911SDOperand ISel::BuildUDIVSequence(SDOperand N) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000912 unsigned d =
Nate Begeman815d6da2005-04-06 00:25:27 +0000913 (unsigned)cast<ConstantSDNode>(N.getOperand(1))->getSignExtended();
914 mu magics = magicu(d);
915 // Multiply the numerator (operand 0) by the magic value
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000916 SDOperand Q = ISelDAG->getNode(ISD::MULHU, MVT::i32, N.getOperand(0),
Nate Begeman815d6da2005-04-06 00:25:27 +0000917 ISelDAG->getConstant(magics.m, MVT::i32));
918 if (magics.a == 0) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000919 Q = ISelDAG->getNode(ISD::SRL, MVT::i32, Q,
Nate Begeman815d6da2005-04-06 00:25:27 +0000920 ISelDAG->getConstant(magics.s, MVT::i32));
921 } else {
922 SDOperand NPQ = ISelDAG->getNode(ISD::SUB, MVT::i32, N.getOperand(0), Q);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000923 NPQ = ISelDAG->getNode(ISD::SRL, MVT::i32, NPQ,
Nate Begeman815d6da2005-04-06 00:25:27 +0000924 ISelDAG->getConstant(1, MVT::i32));
925 NPQ = ISelDAG->getNode(ISD::ADD, MVT::i32, NPQ, Q);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000926 Q = ISelDAG->getNode(ISD::SRL, MVT::i32, NPQ,
Nate Begeman815d6da2005-04-06 00:25:27 +0000927 ISelDAG->getConstant(magics.s-1, MVT::i32));
928 }
Nate Begeman27b4c232005-04-06 06:44:57 +0000929 return Q;
Nate Begemana9795f82005-03-24 04:41:43 +0000930}
931
Nate Begemanc7b09f12005-03-25 08:34:25 +0000932/// getGlobalBaseReg - Output the instructions required to put the
933/// base address to use for accessing globals into a register.
934///
935unsigned ISel::getGlobalBaseReg() {
936 if (!GlobalBaseInitialized) {
937 // Insert the set of GlobalBaseReg into the first MBB of the function
938 MachineBasicBlock &FirstMBB = BB->getParent()->front();
939 MachineBasicBlock::iterator MBBI = FirstMBB.begin();
940 GlobalBaseReg = MakeReg(MVT::i32);
941 BuildMI(FirstMBB, MBBI, PPC::MovePCtoLR, 0, PPC::LR);
942 BuildMI(FirstMBB, MBBI, PPC::MFLR, 1, GlobalBaseReg).addReg(PPC::LR);
943 GlobalBaseInitialized = true;
944 }
945 return GlobalBaseReg;
946}
947
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000948/// getConstDouble - Loads a floating point value into a register, via the
Nate Begeman6b559972005-04-01 02:59:27 +0000949/// Constant Pool. Optionally takes a register in which to load the value.
950unsigned ISel::getConstDouble(double doubleVal, unsigned Result=0) {
951 unsigned Tmp1 = MakeReg(MVT::i32);
952 if (0 == Result) Result = MakeReg(MVT::f64);
953 MachineConstantPool *CP = BB->getParent()->getConstantPool();
954 ConstantFP *CFP = ConstantFP::get(Type::DoubleTy, doubleVal);
955 unsigned CPI = CP->getConstantPoolIndex(CFP);
956 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp1).addReg(getGlobalBaseReg())
957 .addConstantPoolIndex(CPI);
958 BuildMI(BB, PPC::LFD, 2, Result).addConstantPoolIndex(CPI).addReg(Tmp1);
959 return Result;
960}
961
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000962/// MoveCRtoGPR - Move CCReg[Idx] to the least significant bit of Result. If
Nate Begeman1cbf3ab2005-04-18 07:48:09 +0000963/// Inv is true, then invert the result.
964void ISel::MoveCRtoGPR(unsigned CCReg, bool Inv, unsigned Idx, unsigned Result){
965 unsigned IntCR = MakeReg(MVT::i32);
966 BuildMI(BB, PPC::MCRF, 1, PPC::CR7).addReg(CCReg);
967 BuildMI(BB, PPC::MFCR, 1, IntCR).addReg(PPC::CR7);
968 if (Inv) {
969 unsigned Tmp1 = MakeReg(MVT::i32);
970 BuildMI(BB, PPC::RLWINM, 4, Tmp1).addReg(IntCR).addImm(32-(3-Idx))
971 .addImm(31).addImm(31);
972 BuildMI(BB, PPC::XORI, 2, Result).addReg(Tmp1).addImm(1);
973 } else {
974 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(IntCR).addImm(32-(3-Idx))
975 .addImm(31).addImm(31);
976 }
977}
978
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000979/// SelectBitfieldInsert - turn an or of two masked values into
Nate Begeman7ddecb42005-04-06 23:51:40 +0000980/// the rotate left word immediate then mask insert (rlwimi) instruction.
981/// Returns true on success, false if the caller still needs to select OR.
982///
983/// Patterns matched:
984/// 1. or shl, and 5. or and, and
985/// 2. or and, shl 6. or shl, shr
986/// 3. or shr, and 7. or shr, shl
987/// 4. or and, shr
988bool ISel::SelectBitfieldInsert(SDOperand OR, unsigned Result) {
Nate Begemancd08e4c2005-04-09 20:09:12 +0000989 bool IsRotate = false;
Nate Begeman7ddecb42005-04-06 23:51:40 +0000990 unsigned TgtMask = 0xFFFFFFFF, InsMask = 0xFFFFFFFF, Amount = 0;
Nate Begemanb2c4bf32005-06-08 04:14:27 +0000991
992 SDOperand Op0 = OR.getOperand(0);
993 SDOperand Op1 = OR.getOperand(1);
994
995 unsigned Op0Opc = Op0.getOpcode();
996 unsigned Op1Opc = Op1.getOpcode();
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000997
Nate Begeman7ddecb42005-04-06 23:51:40 +0000998 // Verify that we have the correct opcodes
999 if (ISD::SHL != Op0Opc && ISD::SRL != Op0Opc && ISD::AND != Op0Opc)
1000 return false;
1001 if (ISD::SHL != Op1Opc && ISD::SRL != Op1Opc && ISD::AND != Op1Opc)
1002 return false;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001003
Nate Begeman7ddecb42005-04-06 23:51:40 +00001004 // Generate Mask value for Target
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001005 if (ConstantSDNode *CN =
Nate Begemanb2c4bf32005-06-08 04:14:27 +00001006 dyn_cast<ConstantSDNode>(Op0.getOperand(1).Val)) {
Nate Begeman7ddecb42005-04-06 23:51:40 +00001007 switch(Op0Opc) {
1008 case ISD::SHL: TgtMask <<= (unsigned)CN->getValue(); break;
1009 case ISD::SRL: TgtMask >>= (unsigned)CN->getValue(); break;
1010 case ISD::AND: TgtMask &= (unsigned)CN->getValue(); break;
1011 }
1012 } else {
1013 return false;
1014 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001015
Nate Begeman7ddecb42005-04-06 23:51:40 +00001016 // Generate Mask value for Insert
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001017 if (ConstantSDNode *CN =
Nate Begemanb2c4bf32005-06-08 04:14:27 +00001018 dyn_cast<ConstantSDNode>(Op1.getOperand(1).Val)) {
Nate Begeman7ddecb42005-04-06 23:51:40 +00001019 switch(Op1Opc) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001020 case ISD::SHL:
1021 Amount = CN->getValue();
Nate Begemancd08e4c2005-04-09 20:09:12 +00001022 InsMask <<= Amount;
1023 if (Op0Opc == ISD::SRL) IsRotate = true;
Nate Begeman7ddecb42005-04-06 23:51:40 +00001024 break;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001025 case ISD::SRL:
1026 Amount = CN->getValue();
1027 InsMask >>= Amount;
Nate Begeman7ddecb42005-04-06 23:51:40 +00001028 Amount = 32-Amount;
Nate Begemancd08e4c2005-04-09 20:09:12 +00001029 if (Op0Opc == ISD::SHL) IsRotate = true;
Nate Begeman7ddecb42005-04-06 23:51:40 +00001030 break;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001031 case ISD::AND:
Nate Begeman7ddecb42005-04-06 23:51:40 +00001032 InsMask &= (unsigned)CN->getValue();
1033 break;
1034 }
1035 } else {
1036 return false;
1037 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001038
Nate Begemanb2c4bf32005-06-08 04:14:27 +00001039 unsigned Tmp3 = 0;
1040
1041 // If both of the inputs are ANDs and one of them has a logical shift by
1042 // constant as its input, make that the inserted value so that we can combine
1043 // the shift into the rotate part of the rlwimi instruction
1044 if (Op0Opc == ISD::AND && Op1Opc == ISD::AND) {
1045 if (Op1.getOperand(0).getOpcode() == ISD::SHL ||
1046 Op1.getOperand(0).getOpcode() == ISD::SRL) {
1047 if (ConstantSDNode *CN =
1048 dyn_cast<ConstantSDNode>(Op1.getOperand(0).getOperand(1).Val)) {
1049 Amount = Op1.getOperand(0).getOpcode() == ISD::SHL ?
1050 CN->getValue() : 32 - CN->getValue();
1051 Tmp3 = SelectExpr(Op1.getOperand(0).getOperand(0));
1052 }
1053 } else if (Op0.getOperand(0).getOpcode() == ISD::SHL ||
1054 Op0.getOperand(0).getOpcode() == ISD::SRL) {
1055 if (ConstantSDNode *CN =
1056 dyn_cast<ConstantSDNode>(Op0.getOperand(0).getOperand(1).Val)) {
1057 std::swap(Op0, Op1);
1058 std::swap(TgtMask, InsMask);
1059 Amount = Op1.getOperand(0).getOpcode() == ISD::SHL ?
1060 CN->getValue() : 32 - CN->getValue();
1061 Tmp3 = SelectExpr(Op1.getOperand(0).getOperand(0));
1062 }
1063 }
1064 }
1065
Nate Begeman7ddecb42005-04-06 23:51:40 +00001066 // Verify that the Target mask and Insert mask together form a full word mask
1067 // and that the Insert mask is a run of set bits (which implies both are runs
1068 // of set bits). Given that, Select the arguments and generate the rlwimi
1069 // instruction.
1070 unsigned MB, ME;
Nate Begemanb2c4bf32005-06-08 04:14:27 +00001071 if (((TgtMask & InsMask) == 0) && IsRunOfOnes(InsMask, MB, ME)) {
Nate Begeman7ddecb42005-04-06 23:51:40 +00001072 unsigned Tmp1, Tmp2;
Nate Begemanb2c4bf32005-06-08 04:14:27 +00001073 bool fullMask = (TgtMask ^ InsMask) == 0xFFFFFFFF;
Nate Begemancd08e4c2005-04-09 20:09:12 +00001074 // Check for rotlwi / rotrwi here, a special case of bitfield insert
1075 // where both bitfield halves are sourced from the same value.
Nate Begemanb2c4bf32005-06-08 04:14:27 +00001076 if (IsRotate && fullMask &&
Nate Begemancd08e4c2005-04-09 20:09:12 +00001077 OR.getOperand(0).getOperand(0) == OR.getOperand(1).getOperand(0)) {
Nate Begemancd08e4c2005-04-09 20:09:12 +00001078 Tmp1 = SelectExpr(OR.getOperand(0).getOperand(0));
1079 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(Amount)
1080 .addImm(0).addImm(31);
1081 return true;
1082 }
Nate Begemanb2c4bf32005-06-08 04:14:27 +00001083 if (Op0Opc == ISD::AND && fullMask)
1084 Tmp1 = SelectExpr(Op0.getOperand(0));
Nate Begeman7ddecb42005-04-06 23:51:40 +00001085 else
Nate Begemanb2c4bf32005-06-08 04:14:27 +00001086 Tmp1 = SelectExpr(Op0);
1087 Tmp2 = Tmp3 ? Tmp3 : SelectExpr(Op1.getOperand(0));
Nate Begeman7ddecb42005-04-06 23:51:40 +00001088 BuildMI(BB, PPC::RLWIMI, 5, Result).addReg(Tmp1).addReg(Tmp2)
1089 .addImm(Amount).addImm(MB).addImm(ME);
1090 return true;
1091 }
1092 return false;
1093}
1094
Nate Begeman3664cef2005-04-13 22:14:14 +00001095/// FoldIfWideZeroExtend - 32 bit PowerPC implicit masks shift amounts to the
1096/// low six bits. If the shift amount is an ISD::AND node with a mask that is
1097/// wider than the implicit mask, then we can get rid of the AND and let the
1098/// shift do the mask.
1099unsigned ISel::FoldIfWideZeroExtend(SDOperand N) {
1100 unsigned C;
1101 if (N.getOpcode() == ISD::AND &&
1102 5 == getImmediateForOpcode(N.getOperand(1), ISD::AND, C) && // isMask
1103 31 == (C & 0xFFFF) && // ME
1104 26 >= (C >> 16)) // MB
1105 return SelectExpr(N.getOperand(0));
1106 else
1107 return SelectExpr(N);
1108}
1109
Nate Begeman1cbf3ab2005-04-18 07:48:09 +00001110unsigned ISel::SelectCC(SDOperand CC, unsigned& Opc, bool &Inv, unsigned& Idx) {
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001111 unsigned Result, Tmp1, Tmp2;
Nate Begeman9765c252005-04-12 21:22:28 +00001112 bool AlreadySelected = false;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001113 static const unsigned CompareOpcodes[] =
Nate Begemandffcfcc2005-04-01 00:32:34 +00001114 { PPC::FCMPU, PPC::FCMPU, PPC::CMPW, PPC::CMPLW };
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001115
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001116 // Allocate a condition register for this expression
1117 Result = RegMap->createVirtualRegister(PPC32::CRRCRegisterClass);
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001118
Nate Begemandffcfcc2005-04-01 00:32:34 +00001119 // If the first operand to the select is a SETCC node, then we can fold it
1120 // into the branch that selects which value to return.
Nate Begeman16ac7092005-04-18 02:43:24 +00001121 if (SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(CC.Val)) {
Nate Begemandffcfcc2005-04-01 00:32:34 +00001122 bool U;
1123 Opc = getBCCForSetCC(SetCC->getCondition(), U);
Nate Begeman1cbf3ab2005-04-18 07:48:09 +00001124 Idx = getCRIdxForSetCC(SetCC->getCondition(), Inv);
Nate Begemandffcfcc2005-04-01 00:32:34 +00001125
Nate Begeman439b4442005-04-05 04:22:58 +00001126 // Pass the optional argument U to getImmediateForOpcode for SETCC,
Nate Begemandffcfcc2005-04-01 00:32:34 +00001127 // so that it knows whether the SETCC immediate range is signed or not.
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001128 if (1 == getImmediateForOpcode(SetCC->getOperand(1), ISD::SETCC,
Nate Begeman439b4442005-04-05 04:22:58 +00001129 Tmp2, U)) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001130 // For comparisons against zero, we can implicity set CR0 if a recording
Nate Begemanc7bd4822005-04-11 06:34:10 +00001131 // variant (e.g. 'or.' instead of 'or') of the instruction that defines
1132 // operand zero of the SetCC node is available.
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001133 if (0 == Tmp2 &&
Nate Begeman9765c252005-04-12 21:22:28 +00001134 NodeHasRecordingVariant(SetCC->getOperand(0).getOpcode()) &&
1135 SetCC->getOperand(0).Val->hasOneUse()) {
Nate Begemanc7bd4822005-04-11 06:34:10 +00001136 RecordSuccess = false;
1137 Tmp1 = SelectExpr(SetCC->getOperand(0), true);
1138 if (RecordSuccess) {
1139 ++Recorded;
Nate Begeman7bfba7d2005-04-14 09:45:08 +00001140 BuildMI(BB, PPC::MCRF, 1, Result).addReg(PPC::CR0);
1141 return Result;
Nate Begemanc7bd4822005-04-11 06:34:10 +00001142 }
1143 AlreadySelected = true;
1144 }
1145 // If we could not implicitly set CR0, then emit a compare immediate
1146 // instead.
1147 if (!AlreadySelected) Tmp1 = SelectExpr(SetCC->getOperand(0));
Nate Begemandffcfcc2005-04-01 00:32:34 +00001148 if (U)
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001149 BuildMI(BB, PPC::CMPLWI, 2, Result).addReg(Tmp1).addImm(Tmp2);
Nate Begemandffcfcc2005-04-01 00:32:34 +00001150 else
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001151 BuildMI(BB, PPC::CMPWI, 2, Result).addReg(Tmp1).addSImm(Tmp2);
Nate Begemandffcfcc2005-04-01 00:32:34 +00001152 } else {
1153 bool IsInteger = MVT::isInteger(SetCC->getOperand(0).getValueType());
1154 unsigned CompareOpc = CompareOpcodes[2 * IsInteger + U];
Nate Begemanc7bd4822005-04-11 06:34:10 +00001155 Tmp1 = SelectExpr(SetCC->getOperand(0));
Nate Begemandffcfcc2005-04-01 00:32:34 +00001156 Tmp2 = SelectExpr(SetCC->getOperand(1));
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001157 BuildMI(BB, CompareOpc, 2, Result).addReg(Tmp1).addReg(Tmp2);
Nate Begemandffcfcc2005-04-01 00:32:34 +00001158 }
1159 } else {
Nate Begemanf8b02942005-04-15 22:12:16 +00001160 if (PPCCRopts)
Nate Begeman1cbf3ab2005-04-18 07:48:09 +00001161 return SelectCCExpr(CC, Opc, Inv, Idx);
1162 // If this isn't a SetCC, then select the value and compare it against zero,
1163 // treating it as if it were a boolean.
Nate Begeman9765c252005-04-12 21:22:28 +00001164 Opc = PPC::BNE;
Nate Begeman1cbf3ab2005-04-18 07:48:09 +00001165 Idx = getCRIdxForSetCC(ISD::SETNE, Inv);
Nate Begemandffcfcc2005-04-01 00:32:34 +00001166 Tmp1 = SelectExpr(CC);
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001167 BuildMI(BB, PPC::CMPLWI, 2, Result).addReg(Tmp1).addImm(0);
Nate Begemandffcfcc2005-04-01 00:32:34 +00001168 }
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001169 return Result;
Nate Begemandffcfcc2005-04-01 00:32:34 +00001170}
1171
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001172unsigned ISel::SelectCCExpr(SDOperand N, unsigned& Opc, bool &Inv,
Nate Begeman1cbf3ab2005-04-18 07:48:09 +00001173 unsigned &Idx) {
1174 bool Inv0, Inv1;
1175 unsigned Idx0, Idx1, CROpc, Opc1, Tmp1, Tmp2;
1176
1177 // Allocate a condition register for this expression
1178 unsigned Result = RegMap->createVirtualRegister(PPC32::CRRCRegisterClass);
1179
1180 // Check for the operations we support:
1181 switch(N.getOpcode()) {
1182 default:
1183 Opc = PPC::BNE;
1184 Idx = getCRIdxForSetCC(ISD::SETNE, Inv);
1185 Tmp1 = SelectExpr(N);
1186 BuildMI(BB, PPC::CMPLWI, 2, Result).addReg(Tmp1).addImm(0);
1187 break;
1188 case ISD::OR:
1189 case ISD::AND:
1190 ++MultiBranch;
1191 Tmp1 = SelectCCExpr(N.getOperand(0), Opc, Inv0, Idx0);
1192 Tmp2 = SelectCCExpr(N.getOperand(1), Opc1, Inv1, Idx1);
1193 CROpc = getCROpForSetCC(N.getOpcode(), Inv0, Inv1);
1194 if (Inv0 && !Inv1) {
1195 std::swap(Tmp1, Tmp2);
1196 std::swap(Idx0, Idx1);
1197 Opc = Opc1;
1198 }
1199 if (Inv0 && Inv1) Opc = PPC32InstrInfo::invertPPCBranchOpcode(Opc);
1200 BuildMI(BB, CROpc, 5, Result).addImm(Idx0).addReg(Tmp1).addImm(Idx0)
1201 .addReg(Tmp2).addImm(Idx1);
1202 Inv = false;
1203 Idx = Idx0;
1204 break;
1205 case ISD::SETCC:
1206 Tmp1 = SelectCC(N, Opc, Inv, Idx);
1207 Result = Tmp1;
1208 break;
1209 }
1210 return Result;
1211}
1212
Nate Begemandffcfcc2005-04-01 00:32:34 +00001213/// Check to see if the load is a constant offset from a base register
Nate Begeman04730362005-04-01 04:45:11 +00001214bool ISel::SelectAddr(SDOperand N, unsigned& Reg, int& offset)
Nate Begemana9795f82005-03-24 04:41:43 +00001215{
Nate Begeman96fc6812005-03-31 02:05:53 +00001216 unsigned imm = 0, opcode = N.getOpcode();
Nate Begeman04730362005-04-01 04:45:11 +00001217 if (N.getOpcode() == ISD::ADD) {
1218 Reg = SelectExpr(N.getOperand(0));
Nate Begeman439b4442005-04-05 04:22:58 +00001219 if (1 == getImmediateForOpcode(N.getOperand(1), opcode, imm)) {
Nate Begeman96fc6812005-03-31 02:05:53 +00001220 offset = imm;
Nate Begeman04730362005-04-01 04:45:11 +00001221 return false;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001222 }
Nate Begeman04730362005-04-01 04:45:11 +00001223 offset = SelectExpr(N.getOperand(1));
1224 return true;
1225 }
Nate Begemana9795f82005-03-24 04:41:43 +00001226 Reg = SelectExpr(N);
1227 offset = 0;
Nate Begeman04730362005-04-01 04:45:11 +00001228 return false;
Nate Begemana9795f82005-03-24 04:41:43 +00001229}
1230
1231void ISel::SelectBranchCC(SDOperand N)
1232{
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001233 MachineBasicBlock *Dest =
Nate Begemana9795f82005-03-24 04:41:43 +00001234 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
Nate Begeman3e897162005-03-31 23:55:40 +00001235
Nate Begeman1cbf3ab2005-04-18 07:48:09 +00001236 bool Inv;
1237 unsigned Opc, CCReg, Idx;
Nate Begemana9795f82005-03-24 04:41:43 +00001238 Select(N.getOperand(0)); //chain
Nate Begeman1cbf3ab2005-04-18 07:48:09 +00001239 CCReg = SelectCC(N.getOperand(1), Opc, Inv, Idx);
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001240
Nate Begeman439009c2005-06-15 18:22:43 +00001241 // Iterate to the next basic block
1242 ilist<MachineBasicBlock>::iterator It = BB;
1243 ++It;
Nate Begemancd08e4c2005-04-09 20:09:12 +00001244
1245 // If this is a two way branch, then grab the fallthrough basic block argument
1246 // and build a PowerPC branch pseudo-op, suitable for long branch conversion
1247 // if necessary by the branch selection pass. Otherwise, emit a standard
1248 // conditional branch.
1249 if (N.getOpcode() == ISD::BRCONDTWOWAY) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001250 MachineBasicBlock *Fallthrough =
Nate Begemancd08e4c2005-04-09 20:09:12 +00001251 cast<BasicBlockSDNode>(N.getOperand(3))->getBasicBlock();
1252 if (Dest != It) {
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001253 BuildMI(BB, PPC::COND_BRANCH, 4).addReg(CCReg).addImm(Opc)
Nate Begemancd08e4c2005-04-09 20:09:12 +00001254 .addMBB(Dest).addMBB(Fallthrough);
1255 if (Fallthrough != It)
1256 BuildMI(BB, PPC::B, 1).addMBB(Fallthrough);
1257 } else {
1258 if (Fallthrough != It) {
1259 Opc = PPC32InstrInfo::invertPPCBranchOpcode(Opc);
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001260 BuildMI(BB, PPC::COND_BRANCH, 4).addReg(CCReg).addImm(Opc)
Nate Begemancd08e4c2005-04-09 20:09:12 +00001261 .addMBB(Fallthrough).addMBB(Dest);
1262 }
1263 }
1264 } else {
Nate Begeman439009c2005-06-15 18:22:43 +00001265 // If the fallthrough path is off the end of the function, which would be
1266 // undefined behavior, set it to be the same as the current block because
1267 // we have nothing better to set it to, and leaving it alone will cause the
1268 // PowerPC Branch Selection pass to crash.
1269 if (It == BB->getParent()->end()) It = Dest;
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001270 BuildMI(BB, PPC::COND_BRANCH, 4).addReg(CCReg).addImm(Opc)
Nate Begeman27499e32005-04-10 01:48:29 +00001271 .addMBB(Dest).addMBB(It);
Nate Begemancd08e4c2005-04-09 20:09:12 +00001272 }
Nate Begemana9795f82005-03-24 04:41:43 +00001273 return;
1274}
1275
1276unsigned ISel::SelectExprFP(SDOperand N, unsigned Result)
1277{
1278 unsigned Tmp1, Tmp2, Tmp3;
1279 unsigned Opc = 0;
1280 SDNode *Node = N.Val;
1281 MVT::ValueType DestType = N.getValueType();
1282 unsigned opcode = N.getOpcode();
1283
1284 switch (opcode) {
1285 default:
1286 Node->dump();
1287 assert(0 && "Node not handled!\n");
1288
Nate Begeman23afcfb2005-03-29 22:48:55 +00001289 case ISD::SELECT: {
Nate Begeman3e897162005-03-31 23:55:40 +00001290 // Attempt to generate FSEL. We can do this whenever we have an FP result,
1291 // and an FP comparison in the SetCC node.
1292 SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(N.getOperand(0).Val);
1293 if (SetCC && N.getOperand(0).getOpcode() == ISD::SETCC &&
1294 !MVT::isInteger(SetCC->getOperand(0).getValueType()) &&
1295 SetCC->getCondition() != ISD::SETEQ &&
1296 SetCC->getCondition() != ISD::SETNE) {
1297 MVT::ValueType VT = SetCC->getOperand(0).getValueType();
Nate Begeman3e897162005-03-31 23:55:40 +00001298 unsigned TV = SelectExpr(N.getOperand(1)); // Use if TRUE
1299 unsigned FV = SelectExpr(N.getOperand(2)); // Use if FALSE
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001300
Nate Begeman3e897162005-03-31 23:55:40 +00001301 ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(1));
1302 if (CN && (CN->isExactlyValue(-0.0) || CN->isExactlyValue(0.0))) {
1303 switch(SetCC->getCondition()) {
1304 default: assert(0 && "Invalid FSEL condition"); abort();
1305 case ISD::SETULT:
1306 case ISD::SETLT:
Nate Begemanaf4ab1b2005-04-09 09:33:07 +00001307 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
Nate Begeman3e897162005-03-31 23:55:40 +00001308 case ISD::SETUGE:
1309 case ISD::SETGE:
Nate Begemanaf4ab1b2005-04-09 09:33:07 +00001310 Tmp1 = SelectExpr(SetCC->getOperand(0)); // Val to compare against
Nate Begeman3e897162005-03-31 23:55:40 +00001311 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp1).addReg(TV).addReg(FV);
1312 return Result;
1313 case ISD::SETUGT:
Nate Begemanaf4ab1b2005-04-09 09:33:07 +00001314 case ISD::SETGT:
1315 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
Nate Begeman3e897162005-03-31 23:55:40 +00001316 case ISD::SETULE:
1317 case ISD::SETLE: {
Nate Begemanaf4ab1b2005-04-09 09:33:07 +00001318 if (SetCC->getOperand(0).getOpcode() == ISD::FNEG) {
1319 Tmp2 = SelectExpr(SetCC->getOperand(0).getOperand(0));
1320 } else {
1321 Tmp2 = MakeReg(VT);
1322 Tmp1 = SelectExpr(SetCC->getOperand(0)); // Val to compare against
1323 BuildMI(BB, PPC::FNEG, 1, Tmp2).addReg(Tmp1);
1324 }
Nate Begeman3e897162005-03-31 23:55:40 +00001325 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp2).addReg(TV).addReg(FV);
1326 return Result;
1327 }
1328 }
1329 } else {
1330 Opc = (MVT::f64 == VT) ? PPC::FSUB : PPC::FSUBS;
Nate Begemanaf4ab1b2005-04-09 09:33:07 +00001331 Tmp1 = SelectExpr(SetCC->getOperand(0)); // Val to compare against
Nate Begeman3e897162005-03-31 23:55:40 +00001332 Tmp2 = SelectExpr(SetCC->getOperand(1));
1333 Tmp3 = MakeReg(VT);
1334 switch(SetCC->getCondition()) {
1335 default: assert(0 && "Invalid FSEL condition"); abort();
1336 case ISD::SETULT:
1337 case ISD::SETLT:
1338 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
1339 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(FV).addReg(TV);
1340 return Result;
1341 case ISD::SETUGE:
1342 case ISD::SETGE:
1343 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
1344 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(TV).addReg(FV);
1345 return Result;
1346 case ISD::SETUGT:
1347 case ISD::SETGT:
1348 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp2).addReg(Tmp1);
1349 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(FV).addReg(TV);
1350 return Result;
1351 case ISD::SETULE:
1352 case ISD::SETLE:
1353 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp2).addReg(Tmp1);
1354 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(TV).addReg(FV);
1355 return Result;
1356 }
1357 }
1358 assert(0 && "Should never get here");
1359 return 0;
1360 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001361
Nate Begeman1cbf3ab2005-04-18 07:48:09 +00001362 bool Inv;
Nate Begeman31318e42005-04-01 07:21:30 +00001363 unsigned TrueValue = SelectExpr(N.getOperand(1)); //Use if TRUE
1364 unsigned FalseValue = SelectExpr(N.getOperand(2)); //Use if FALSE
Nate Begeman1cbf3ab2005-04-18 07:48:09 +00001365 unsigned CCReg = SelectCC(N.getOperand(0), Opc, Inv, Tmp3);
Nate Begeman31318e42005-04-01 07:21:30 +00001366
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001367 // Create an iterator with which to insert the MBB for copying the false
Nate Begeman23afcfb2005-03-29 22:48:55 +00001368 // value and the MBB to hold the PHI instruction for this SetCC.
1369 MachineBasicBlock *thisMBB = BB;
1370 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1371 ilist<MachineBasicBlock>::iterator It = BB;
1372 ++It;
1373
1374 // thisMBB:
1375 // ...
1376 // TrueVal = ...
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001377 // cmpTY ccX, r1, r2
Nate Begeman23afcfb2005-03-29 22:48:55 +00001378 // bCC copy1MBB
1379 // fallthrough --> copy0MBB
Nate Begeman23afcfb2005-03-29 22:48:55 +00001380 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
1381 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001382 BuildMI(BB, Opc, 2).addReg(CCReg).addMBB(sinkMBB);
Nate Begeman23afcfb2005-03-29 22:48:55 +00001383 MachineFunction *F = BB->getParent();
1384 F->getBasicBlockList().insert(It, copy0MBB);
1385 F->getBasicBlockList().insert(It, sinkMBB);
1386 // Update machine-CFG edges
1387 BB->addSuccessor(copy0MBB);
1388 BB->addSuccessor(sinkMBB);
1389
1390 // copy0MBB:
1391 // %FalseValue = ...
1392 // # fallthrough to sinkMBB
1393 BB = copy0MBB;
Nate Begeman23afcfb2005-03-29 22:48:55 +00001394 // Update machine-CFG edges
1395 BB->addSuccessor(sinkMBB);
1396
1397 // sinkMBB:
1398 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1399 // ...
1400 BB = sinkMBB;
1401 BuildMI(BB, PPC::PHI, 4, Result).addReg(FalseValue)
1402 .addMBB(copy0MBB).addReg(TrueValue).addMBB(thisMBB);
1403 return Result;
1404 }
Nate Begeman27eeb002005-04-02 05:59:34 +00001405
1406 case ISD::FNEG:
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001407 if (!NoExcessFPPrecision &&
Nate Begeman93075ec2005-04-04 23:40:36 +00001408 ISD::ADD == N.getOperand(0).getOpcode() &&
1409 N.getOperand(0).Val->hasOneUse() &&
1410 ISD::MUL == N.getOperand(0).getOperand(0).getOpcode() &&
1411 N.getOperand(0).getOperand(0).Val->hasOneUse()) {
Nate Begeman80196b12005-04-05 00:15:08 +00001412 ++FusedFP; // Statistic
Nate Begeman93075ec2005-04-04 23:40:36 +00001413 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(0));
1414 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(1));
1415 Tmp3 = SelectExpr(N.getOperand(0).getOperand(1));
1416 Opc = DestType == MVT::f64 ? PPC::FNMADD : PPC::FNMADDS;
1417 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001418 } else if (!NoExcessFPPrecision &&
Nate Begemane88aa5b2005-04-09 03:05:51 +00001419 ISD::ADD == N.getOperand(0).getOpcode() &&
Nate Begeman93075ec2005-04-04 23:40:36 +00001420 N.getOperand(0).Val->hasOneUse() &&
Nate Begemane88aa5b2005-04-09 03:05:51 +00001421 ISD::MUL == N.getOperand(0).getOperand(1).getOpcode() &&
1422 N.getOperand(0).getOperand(1).Val->hasOneUse()) {
Nate Begeman80196b12005-04-05 00:15:08 +00001423 ++FusedFP; // Statistic
Nate Begemane88aa5b2005-04-09 03:05:51 +00001424 Tmp1 = SelectExpr(N.getOperand(0).getOperand(1).getOperand(0));
1425 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1).getOperand(1));
1426 Tmp3 = SelectExpr(N.getOperand(0).getOperand(0));
1427 Opc = DestType == MVT::f64 ? PPC::FNMADD : PPC::FNMADDS;
Nate Begeman93075ec2005-04-04 23:40:36 +00001428 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1429 } else if (ISD::FABS == N.getOperand(0).getOpcode()) {
Nate Begeman27eeb002005-04-02 05:59:34 +00001430 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1431 BuildMI(BB, PPC::FNABS, 1, Result).addReg(Tmp1);
1432 } else {
1433 Tmp1 = SelectExpr(N.getOperand(0));
1434 BuildMI(BB, PPC::FNEG, 1, Result).addReg(Tmp1);
1435 }
1436 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001437
Nate Begeman27eeb002005-04-02 05:59:34 +00001438 case ISD::FABS:
1439 Tmp1 = SelectExpr(N.getOperand(0));
1440 BuildMI(BB, PPC::FABS, 1, Result).addReg(Tmp1);
1441 return Result;
1442
Nate Begemana9795f82005-03-24 04:41:43 +00001443 case ISD::FP_ROUND:
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001444 assert (DestType == MVT::f32 &&
1445 N.getOperand(0).getValueType() == MVT::f64 &&
Nate Begemana9795f82005-03-24 04:41:43 +00001446 "only f64 to f32 conversion supported here");
1447 Tmp1 = SelectExpr(N.getOperand(0));
1448 BuildMI(BB, PPC::FRSP, 1, Result).addReg(Tmp1);
1449 return Result;
1450
1451 case ISD::FP_EXTEND:
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001452 assert (DestType == MVT::f64 &&
1453 N.getOperand(0).getValueType() == MVT::f32 &&
Nate Begemana9795f82005-03-24 04:41:43 +00001454 "only f32 to f64 conversion supported here");
1455 Tmp1 = SelectExpr(N.getOperand(0));
1456 BuildMI(BB, PPC::FMR, 1, Result).addReg(Tmp1);
1457 return Result;
1458
1459 case ISD::CopyFromReg:
Nate Begemanf2622612005-03-26 02:17:46 +00001460 if (Result == 1)
1461 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1462 Tmp1 = dyn_cast<RegSDNode>(Node)->getReg();
1463 BuildMI(BB, PPC::FMR, 1, Result).addReg(Tmp1);
1464 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001465
Nate Begeman6d369cc2005-04-01 01:08:07 +00001466 case ISD::ConstantFP: {
Nate Begeman6d369cc2005-04-01 01:08:07 +00001467 ConstantFPSDNode *CN = cast<ConstantFPSDNode>(N);
Nate Begeman6b559972005-04-01 02:59:27 +00001468 Result = getConstDouble(CN->getValue(), Result);
Nate Begeman6d369cc2005-04-01 01:08:07 +00001469 return Result;
1470 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001471
Nate Begemana9795f82005-03-24 04:41:43 +00001472 case ISD::ADD:
Nate Begeman93075ec2005-04-04 23:40:36 +00001473 if (!NoExcessFPPrecision && N.getOperand(0).getOpcode() == ISD::MUL &&
1474 N.getOperand(0).Val->hasOneUse()) {
1475 ++FusedFP; // Statistic
1476 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1477 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1478 Tmp3 = SelectExpr(N.getOperand(1));
1479 Opc = DestType == MVT::f64 ? PPC::FMADD : PPC::FMADDS;
1480 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1481 return Result;
1482 }
Nate Begemane88aa5b2005-04-09 03:05:51 +00001483 if (!NoExcessFPPrecision && N.getOperand(1).getOpcode() == ISD::MUL &&
1484 N.getOperand(1).Val->hasOneUse()) {
1485 ++FusedFP; // Statistic
1486 Tmp1 = SelectExpr(N.getOperand(1).getOperand(0));
1487 Tmp2 = SelectExpr(N.getOperand(1).getOperand(1));
1488 Tmp3 = SelectExpr(N.getOperand(0));
1489 Opc = DestType == MVT::f64 ? PPC::FMADD : PPC::FMADDS;
1490 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1491 return Result;
1492 }
Nate Begeman93075ec2005-04-04 23:40:36 +00001493 Opc = DestType == MVT::f64 ? PPC::FADD : PPC::FADDS;
1494 Tmp1 = SelectExpr(N.getOperand(0));
1495 Tmp2 = SelectExpr(N.getOperand(1));
1496 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1497 return Result;
1498
Nate Begemana9795f82005-03-24 04:41:43 +00001499 case ISD::SUB:
Nate Begeman93075ec2005-04-04 23:40:36 +00001500 if (!NoExcessFPPrecision && N.getOperand(0).getOpcode() == ISD::MUL &&
1501 N.getOperand(0).Val->hasOneUse()) {
1502 ++FusedFP; // Statistic
1503 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1504 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1505 Tmp3 = SelectExpr(N.getOperand(1));
1506 Opc = DestType == MVT::f64 ? PPC::FMSUB : PPC::FMSUBS;
1507 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1508 return Result;
1509 }
Nate Begemane88aa5b2005-04-09 03:05:51 +00001510 if (!NoExcessFPPrecision && N.getOperand(1).getOpcode() == ISD::MUL &&
1511 N.getOperand(1).Val->hasOneUse()) {
1512 ++FusedFP; // Statistic
1513 Tmp1 = SelectExpr(N.getOperand(1).getOperand(0));
1514 Tmp2 = SelectExpr(N.getOperand(1).getOperand(1));
1515 Tmp3 = SelectExpr(N.getOperand(0));
1516 Opc = DestType == MVT::f64 ? PPC::FNMSUB : PPC::FNMSUBS;
1517 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1518 return Result;
1519 }
Nate Begeman93075ec2005-04-04 23:40:36 +00001520 Opc = DestType == MVT::f64 ? PPC::FSUB : PPC::FSUBS;
1521 Tmp1 = SelectExpr(N.getOperand(0));
1522 Tmp2 = SelectExpr(N.getOperand(1));
1523 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1524 return Result;
1525
1526 case ISD::MUL:
Nate Begemana9795f82005-03-24 04:41:43 +00001527 case ISD::SDIV:
1528 switch( opcode ) {
1529 case ISD::MUL: Opc = DestType == MVT::f64 ? PPC::FMUL : PPC::FMULS; break;
Nate Begemana9795f82005-03-24 04:41:43 +00001530 case ISD::SDIV: Opc = DestType == MVT::f64 ? PPC::FDIV : PPC::FDIVS; break;
1531 };
Nate Begemana9795f82005-03-24 04:41:43 +00001532 Tmp1 = SelectExpr(N.getOperand(0));
1533 Tmp2 = SelectExpr(N.getOperand(1));
1534 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1535 return Result;
1536
Nate Begemana9795f82005-03-24 04:41:43 +00001537 case ISD::UINT_TO_FP:
Nate Begemanfdcf3412005-03-30 19:38:35 +00001538 case ISD::SINT_TO_FP: {
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001539 assert (N.getOperand(0).getValueType() == MVT::i32
Nate Begemanfdcf3412005-03-30 19:38:35 +00001540 && "int to float must operate on i32");
1541 bool IsUnsigned = (ISD::UINT_TO_FP == opcode);
1542 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
1543 Tmp2 = MakeReg(MVT::f64); // temp reg to load the integer value into
1544 Tmp3 = MakeReg(MVT::i32); // temp reg to hold the conversion constant
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001545
Nate Begemanfdcf3412005-03-30 19:38:35 +00001546 int FrameIdx = BB->getParent()->getFrameInfo()->CreateStackObject(8, 8);
1547 MachineConstantPool *CP = BB->getParent()->getConstantPool();
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001548
Nate Begemanfdcf3412005-03-30 19:38:35 +00001549 if (IsUnsigned) {
Nate Begeman709c8062005-04-10 06:06:10 +00001550 unsigned ConstF = getConstDouble(0x1.000000p52);
Nate Begemanfdcf3412005-03-30 19:38:35 +00001551 // Store the hi & low halves of the fp value, currently in int regs
1552 BuildMI(BB, PPC::LIS, 1, Tmp3).addSImm(0x4330);
1553 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(Tmp3), FrameIdx);
1554 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(Tmp1), FrameIdx, 4);
1555 addFrameReference(BuildMI(BB, PPC::LFD, 2, Tmp2), FrameIdx);
1556 // Generate the return value with a subtract
1557 BuildMI(BB, PPC::FSUB, 2, Result).addReg(Tmp2).addReg(ConstF);
1558 } else {
Nate Begeman709c8062005-04-10 06:06:10 +00001559 unsigned ConstF = getConstDouble(0x1.000008p52);
Nate Begemanfdcf3412005-03-30 19:38:35 +00001560 unsigned TmpL = MakeReg(MVT::i32);
Nate Begemanfdcf3412005-03-30 19:38:35 +00001561 // Store the hi & low halves of the fp value, currently in int regs
1562 BuildMI(BB, PPC::LIS, 1, Tmp3).addSImm(0x4330);
1563 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(Tmp3), FrameIdx);
1564 BuildMI(BB, PPC::XORIS, 2, TmpL).addReg(Tmp1).addImm(0x8000);
1565 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(TmpL), FrameIdx, 4);
1566 addFrameReference(BuildMI(BB, PPC::LFD, 2, Tmp2), FrameIdx);
1567 // Generate the return value with a subtract
1568 BuildMI(BB, PPC::FSUB, 2, Result).addReg(Tmp2).addReg(ConstF);
1569 }
1570 return Result;
1571 }
Nate Begemana9795f82005-03-24 04:41:43 +00001572 }
Nate Begeman6b559972005-04-01 02:59:27 +00001573 assert(0 && "Should never get here");
Nate Begemana9795f82005-03-24 04:41:43 +00001574 return 0;
1575}
1576
Nate Begemanc7bd4822005-04-11 06:34:10 +00001577unsigned ISel::SelectExpr(SDOperand N, bool Recording) {
Nate Begemana9795f82005-03-24 04:41:43 +00001578 unsigned Result;
1579 unsigned Tmp1, Tmp2, Tmp3;
1580 unsigned Opc = 0;
1581 unsigned opcode = N.getOpcode();
1582
1583 SDNode *Node = N.Val;
1584 MVT::ValueType DestType = N.getValueType();
1585
Nate Begemana43b1762005-06-14 03:55:23 +00001586 if (Node->getOpcode() == ISD::CopyFromReg &&
1587 MRegisterInfo::isVirtualRegister(cast<RegSDNode>(Node)->getReg()))
1588 // Just use the specified register as our input.
1589 return cast<RegSDNode>(Node)->getReg();
1590
Nate Begemana9795f82005-03-24 04:41:43 +00001591 unsigned &Reg = ExprMap[N];
1592 if (Reg) return Reg;
1593
Nate Begeman27eeb002005-04-02 05:59:34 +00001594 switch (N.getOpcode()) {
1595 default:
Nate Begemana9795f82005-03-24 04:41:43 +00001596 Reg = Result = (N.getValueType() != MVT::Other) ?
Nate Begeman27eeb002005-04-02 05:59:34 +00001597 MakeReg(N.getValueType()) : 1;
1598 break;
Chris Lattnerb5d8e6e2005-05-13 20:29:26 +00001599 case ISD::TAILCALL:
Nate Begeman27eeb002005-04-02 05:59:34 +00001600 case ISD::CALL:
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001601 // If this is a call instruction, make sure to prepare ALL of the result
1602 // values as well as the chain.
Nate Begeman27eeb002005-04-02 05:59:34 +00001603 if (Node->getNumValues() == 1)
1604 Reg = Result = 1; // Void call, just a chain.
1605 else {
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001606 Result = MakeReg(Node->getValueType(0));
1607 ExprMap[N.getValue(0)] = Result;
Nate Begeman27eeb002005-04-02 05:59:34 +00001608 for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001609 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
Nate Begeman27eeb002005-04-02 05:59:34 +00001610 ExprMap[SDOperand(Node, Node->getNumValues()-1)] = 1;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001611 }
Nate Begeman27eeb002005-04-02 05:59:34 +00001612 break;
1613 case ISD::ADD_PARTS:
1614 case ISD::SUB_PARTS:
1615 case ISD::SHL_PARTS:
1616 case ISD::SRL_PARTS:
1617 case ISD::SRA_PARTS:
1618 Result = MakeReg(Node->getValueType(0));
1619 ExprMap[N.getValue(0)] = Result;
1620 for (unsigned i = 1, e = N.Val->getNumValues(); i != e; ++i)
1621 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
1622 break;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001623 }
1624
Nate Begemane5846682005-04-04 06:52:38 +00001625 if (ISD::CopyFromReg == opcode)
1626 DestType = N.getValue(0).getValueType();
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001627
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001628 if (DestType == MVT::f64 || DestType == MVT::f32)
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001629 if (ISD::LOAD != opcode && ISD::EXTLOAD != opcode &&
Chris Lattnerb5d8e6e2005-05-13 20:29:26 +00001630 ISD::UNDEF != opcode && ISD::CALL != opcode && ISD::TAILCALL != opcode)
Nate Begeman74d73452005-03-31 00:15:26 +00001631 return SelectExprFP(N, Result);
Nate Begemana9795f82005-03-24 04:41:43 +00001632
1633 switch (opcode) {
1634 default:
1635 Node->dump();
1636 assert(0 && "Node not handled!\n");
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001637 case ISD::UNDEF:
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001638 BuildMI(BB, PPC::IMPLICIT_DEF, 0, Result);
1639 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001640 case ISD::DYNAMIC_STACKALLOC:
Nate Begeman5e966612005-03-24 06:28:42 +00001641 // Generate both result values. FIXME: Need a better commment here?
1642 if (Result != 1)
1643 ExprMap[N.getValue(1)] = 1;
1644 else
1645 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1646
1647 // FIXME: We are currently ignoring the requested alignment for handling
1648 // greater than the stack alignment. This will need to be revisited at some
1649 // point. Align = N.getOperand(2);
1650 if (!isa<ConstantSDNode>(N.getOperand(2)) ||
1651 cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) {
1652 std::cerr << "Cannot allocate stack object with greater alignment than"
1653 << " the stack alignment yet!";
1654 abort();
1655 }
1656 Select(N.getOperand(0));
1657 Tmp1 = SelectExpr(N.getOperand(1));
1658 // Subtract size from stack pointer, thereby allocating some space.
1659 BuildMI(BB, PPC::SUBF, 2, PPC::R1).addReg(Tmp1).addReg(PPC::R1);
1660 // Put a pointer to the space into the result register by copying the SP
1661 BuildMI(BB, PPC::OR, 2, Result).addReg(PPC::R1).addReg(PPC::R1);
1662 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001663
1664 case ISD::ConstantPool:
Nate Begemanca12a2b2005-03-28 22:28:37 +00001665 Tmp1 = cast<ConstantPoolSDNode>(N)->getIndex();
1666 Tmp2 = MakeReg(MVT::i32);
1667 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp2).addReg(getGlobalBaseReg())
1668 .addConstantPoolIndex(Tmp1);
1669 BuildMI(BB, PPC::LA, 2, Result).addReg(Tmp2).addConstantPoolIndex(Tmp1);
1670 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001671
1672 case ISD::FrameIndex:
Nate Begemanf3d08f32005-03-29 00:03:27 +00001673 Tmp1 = cast<FrameIndexSDNode>(N)->getIndex();
Nate Begeman58f718c2005-03-30 02:23:08 +00001674 addFrameReference(BuildMI(BB, PPC::ADDI, 2, Result), (int)Tmp1, 0, false);
Nate Begemanf3d08f32005-03-29 00:03:27 +00001675 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001676
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001677 case ISD::GlobalAddress: {
1678 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
Nate Begemanca12a2b2005-03-28 22:28:37 +00001679 Tmp1 = MakeReg(MVT::i32);
Nate Begemanc7b09f12005-03-25 08:34:25 +00001680 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp1).addReg(getGlobalBaseReg())
1681 .addGlobalAddress(GV);
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001682 if (GV->hasWeakLinkage() || GV->isExternal()) {
1683 BuildMI(BB, PPC::LWZ, 2, Result).addGlobalAddress(GV).addReg(Tmp1);
1684 } else {
1685 BuildMI(BB, PPC::LA, 2, Result).addReg(Tmp1).addGlobalAddress(GV);
1686 }
1687 return Result;
1688 }
1689
Nate Begeman5e966612005-03-24 06:28:42 +00001690 case ISD::LOAD:
Nate Begemana9795f82005-03-24 04:41:43 +00001691 case ISD::EXTLOAD:
1692 case ISD::ZEXTLOAD:
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001693 case ISD::SEXTLOAD: {
Nate Begeman9db505c2005-03-28 19:36:43 +00001694 MVT::ValueType TypeBeingLoaded = (ISD::LOAD == opcode) ?
1695 Node->getValueType(0) : cast<MVTSDNode>(Node)->getExtraValueType();
Nate Begeman74d73452005-03-31 00:15:26 +00001696 bool sext = (ISD::SEXTLOAD == opcode);
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001697
Nate Begeman5e966612005-03-24 06:28:42 +00001698 // Make sure we generate both values.
1699 if (Result != 1)
1700 ExprMap[N.getValue(1)] = 1; // Generate the token
1701 else
1702 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1703
1704 SDOperand Chain = N.getOperand(0);
1705 SDOperand Address = N.getOperand(1);
1706 Select(Chain);
1707
Nate Begeman9db505c2005-03-28 19:36:43 +00001708 switch (TypeBeingLoaded) {
Nate Begeman74d73452005-03-31 00:15:26 +00001709 default: Node->dump(); assert(0 && "Cannot load this type!");
Nate Begeman9db505c2005-03-28 19:36:43 +00001710 case MVT::i1: Opc = PPC::LBZ; break;
1711 case MVT::i8: Opc = PPC::LBZ; break;
1712 case MVT::i16: Opc = sext ? PPC::LHA : PPC::LHZ; break;
1713 case MVT::i32: Opc = PPC::LWZ; break;
Nate Begeman74d73452005-03-31 00:15:26 +00001714 case MVT::f32: Opc = PPC::LFS; break;
1715 case MVT::f64: Opc = PPC::LFD; break;
Nate Begeman5e966612005-03-24 06:28:42 +00001716 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001717
Nate Begeman74d73452005-03-31 00:15:26 +00001718 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Address)) {
1719 Tmp1 = MakeReg(MVT::i32);
1720 int CPI = CP->getIndex();
1721 BuildMI(BB, PPC::LOADHiAddr, 2, Tmp1).addReg(getGlobalBaseReg())
1722 .addConstantPoolIndex(CPI);
1723 BuildMI(BB, Opc, 2, Result).addConstantPoolIndex(CPI).addReg(Tmp1);
Nate Begeman9db505c2005-03-28 19:36:43 +00001724 }
Nate Begeman74d73452005-03-31 00:15:26 +00001725 else if(Address.getOpcode() == ISD::FrameIndex) {
Nate Begeman58f718c2005-03-30 02:23:08 +00001726 Tmp1 = cast<FrameIndexSDNode>(Address)->getIndex();
1727 addFrameReference(BuildMI(BB, Opc, 2, Result), (int)Tmp1);
Nate Begeman5e966612005-03-24 06:28:42 +00001728 } else {
1729 int offset;
Nate Begeman04730362005-04-01 04:45:11 +00001730 bool idx = SelectAddr(Address, Tmp1, offset);
1731 if (idx) {
1732 Opc = IndexedOpForOp(Opc);
1733 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(offset);
1734 } else {
1735 BuildMI(BB, Opc, 2, Result).addSImm(offset).addReg(Tmp1);
1736 }
Nate Begeman5e966612005-03-24 06:28:42 +00001737 }
1738 return Result;
1739 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001740
Chris Lattnerb5d8e6e2005-05-13 20:29:26 +00001741 case ISD::TAILCALL:
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001742 case ISD::CALL: {
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001743 unsigned GPR_idx = 0, FPR_idx = 0;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001744 static const unsigned GPR[] = {
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001745 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
1746 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
1747 };
1748 static const unsigned FPR[] = {
1749 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
1750 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
1751 };
1752
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001753 // Lower the chain for this call.
1754 Select(N.getOperand(0));
1755 ExprMap[N.getValue(Node->getNumValues()-1)] = 1;
Nate Begeman74d73452005-03-31 00:15:26 +00001756
Nate Begemand860aa62005-04-04 22:17:48 +00001757 MachineInstr *CallMI;
1758 // Emit the correct call instruction based on the type of symbol called.
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001759 if (GlobalAddressSDNode *GASD =
Nate Begemand860aa62005-04-04 22:17:48 +00001760 dyn_cast<GlobalAddressSDNode>(N.getOperand(1))) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001761 CallMI = BuildMI(PPC::CALLpcrel, 1).addGlobalAddress(GASD->getGlobal(),
Nate Begemand860aa62005-04-04 22:17:48 +00001762 true);
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001763 } else if (ExternalSymbolSDNode *ESSDN =
Nate Begemand860aa62005-04-04 22:17:48 +00001764 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1))) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001765 CallMI = BuildMI(PPC::CALLpcrel, 1).addExternalSymbol(ESSDN->getSymbol(),
Nate Begemand860aa62005-04-04 22:17:48 +00001766 true);
1767 } else {
1768 Tmp1 = SelectExpr(N.getOperand(1));
1769 BuildMI(BB, PPC::OR, 2, PPC::R12).addReg(Tmp1).addReg(Tmp1);
1770 BuildMI(BB, PPC::MTCTR, 1).addReg(PPC::R12);
1771 CallMI = BuildMI(PPC::CALLindirect, 3).addImm(20).addImm(0)
1772 .addReg(PPC::R12);
1773 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001774
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001775 // Load the register args to virtual regs
1776 std::vector<unsigned> ArgVR;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001777 for(int i = 2, e = Node->getNumOperands(); i < e; ++i)
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001778 ArgVR.push_back(SelectExpr(N.getOperand(i)));
1779
1780 // Copy the virtual registers into the appropriate argument register
1781 for(int i = 0, e = ArgVR.size(); i < e; ++i) {
1782 switch(N.getOperand(i+2).getValueType()) {
1783 default: Node->dump(); assert(0 && "Unknown value type for call");
1784 case MVT::i1:
1785 case MVT::i8:
1786 case MVT::i16:
1787 case MVT::i32:
1788 assert(GPR_idx < 8 && "Too many int args");
Nate Begemand860aa62005-04-04 22:17:48 +00001789 if (N.getOperand(i+2).getOpcode() != ISD::UNDEF) {
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001790 BuildMI(BB, PPC::OR,2,GPR[GPR_idx]).addReg(ArgVR[i]).addReg(ArgVR[i]);
Nate Begemand860aa62005-04-04 22:17:48 +00001791 CallMI->addRegOperand(GPR[GPR_idx], MachineOperand::Use);
1792 }
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001793 ++GPR_idx;
1794 break;
1795 case MVT::f64:
1796 case MVT::f32:
1797 assert(FPR_idx < 13 && "Too many fp args");
1798 BuildMI(BB, PPC::FMR, 1, FPR[FPR_idx]).addReg(ArgVR[i]);
Nate Begemand860aa62005-04-04 22:17:48 +00001799 CallMI->addRegOperand(FPR[FPR_idx], MachineOperand::Use);
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001800 ++FPR_idx;
1801 break;
1802 }
1803 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001804
Nate Begemand860aa62005-04-04 22:17:48 +00001805 // Put the call instruction in the correct place in the MachineBasicBlock
1806 BB->push_back(CallMI);
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001807
1808 switch (Node->getValueType(0)) {
1809 default: assert(0 && "Unknown value type for call result!");
1810 case MVT::Other: return 1;
1811 case MVT::i1:
1812 case MVT::i8:
1813 case MVT::i16:
1814 case MVT::i32:
Nate Begemane5846682005-04-04 06:52:38 +00001815 if (Node->getValueType(1) == MVT::i32) {
1816 BuildMI(BB, PPC::OR, 2, Result+1).addReg(PPC::R3).addReg(PPC::R3);
1817 BuildMI(BB, PPC::OR, 2, Result).addReg(PPC::R4).addReg(PPC::R4);
1818 } else {
1819 BuildMI(BB, PPC::OR, 2, Result).addReg(PPC::R3).addReg(PPC::R3);
1820 }
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001821 break;
1822 case MVT::f32:
1823 case MVT::f64:
1824 BuildMI(BB, PPC::FMR, 1, Result).addReg(PPC::F1);
1825 break;
1826 }
1827 return Result+N.ResNo;
1828 }
Nate Begemana9795f82005-03-24 04:41:43 +00001829
1830 case ISD::SIGN_EXTEND:
1831 case ISD::SIGN_EXTEND_INREG:
1832 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman9db505c2005-03-28 19:36:43 +00001833 switch(cast<MVTSDNode>(Node)->getExtraValueType()) {
1834 default: Node->dump(); assert(0 && "Unhandled SIGN_EXTEND type"); break;
Nate Begemanc7bd4822005-04-11 06:34:10 +00001835 case MVT::i16:
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001836 BuildMI(BB, PPC::EXTSH, 1, Result).addReg(Tmp1);
Nate Begeman9db505c2005-03-28 19:36:43 +00001837 break;
Nate Begemanc7bd4822005-04-11 06:34:10 +00001838 case MVT::i8:
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001839 BuildMI(BB, PPC::EXTSB, 1, Result).addReg(Tmp1);
Nate Begeman9db505c2005-03-28 19:36:43 +00001840 break;
Nate Begeman74747862005-03-29 22:24:51 +00001841 case MVT::i1:
1842 BuildMI(BB, PPC::SUBFIC, 2, Result).addReg(Tmp1).addSImm(0);
1843 break;
Nate Begeman9db505c2005-03-28 19:36:43 +00001844 }
Nate Begemana9795f82005-03-24 04:41:43 +00001845 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001846
Nate Begemana9795f82005-03-24 04:41:43 +00001847 case ISD::CopyFromReg:
1848 if (Result == 1)
1849 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1850 Tmp1 = dyn_cast<RegSDNode>(Node)->getReg();
1851 BuildMI(BB, PPC::OR, 2, Result).addReg(Tmp1).addReg(Tmp1);
1852 return Result;
1853
1854 case ISD::SHL:
Nate Begeman5e966612005-03-24 06:28:42 +00001855 Tmp1 = SelectExpr(N.getOperand(0));
1856 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1857 Tmp2 = CN->getValue() & 0x1F;
Nate Begeman33162522005-03-29 21:54:38 +00001858 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(Tmp2).addImm(0)
Nate Begeman5e966612005-03-24 06:28:42 +00001859 .addImm(31-Tmp2);
1860 } else {
Nate Begeman3664cef2005-04-13 22:14:14 +00001861 Tmp2 = FoldIfWideZeroExtend(N.getOperand(1));
Nate Begeman5e966612005-03-24 06:28:42 +00001862 BuildMI(BB, PPC::SLW, 2, Result).addReg(Tmp1).addReg(Tmp2);
1863 }
1864 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001865
Nate Begeman5e966612005-03-24 06:28:42 +00001866 case ISD::SRL:
1867 Tmp1 = SelectExpr(N.getOperand(0));
1868 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1869 Tmp2 = CN->getValue() & 0x1F;
Nate Begeman33162522005-03-29 21:54:38 +00001870 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(32-Tmp2)
Nate Begeman5e966612005-03-24 06:28:42 +00001871 .addImm(Tmp2).addImm(31);
1872 } else {
Nate Begeman3664cef2005-04-13 22:14:14 +00001873 Tmp2 = FoldIfWideZeroExtend(N.getOperand(1));
Nate Begeman5e966612005-03-24 06:28:42 +00001874 BuildMI(BB, PPC::SRW, 2, Result).addReg(Tmp1).addReg(Tmp2);
1875 }
1876 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001877
Nate Begeman5e966612005-03-24 06:28:42 +00001878 case ISD::SRA:
1879 Tmp1 = SelectExpr(N.getOperand(0));
1880 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1881 Tmp2 = CN->getValue() & 0x1F;
1882 BuildMI(BB, PPC::SRAWI, 2, Result).addReg(Tmp1).addImm(Tmp2);
1883 } else {
Nate Begeman3664cef2005-04-13 22:14:14 +00001884 Tmp2 = FoldIfWideZeroExtend(N.getOperand(1));
Nate Begeman5e966612005-03-24 06:28:42 +00001885 BuildMI(BB, PPC::SRAW, 2, Result).addReg(Tmp1).addReg(Tmp2);
1886 }
1887 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001888
Nate Begemand7c4a4a2005-05-11 23:43:56 +00001889 case ISD::CTLZ:
1890 Tmp1 = SelectExpr(N.getOperand(0));
1891 BuildMI(BB, PPC::CNTLZW, 1, Result).addReg(Tmp1);
1892 return Result;
1893
Nate Begemana9795f82005-03-24 04:41:43 +00001894 case ISD::ADD:
1895 assert (DestType == MVT::i32 && "Only do arithmetic on i32s!");
1896 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman439b4442005-04-05 04:22:58 +00001897 switch(getImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
Nate Begemana9795f82005-03-24 04:41:43 +00001898 default: assert(0 && "unhandled result code");
1899 case 0: // No immediate
1900 Tmp2 = SelectExpr(N.getOperand(1));
1901 BuildMI(BB, PPC::ADD, 2, Result).addReg(Tmp1).addReg(Tmp2);
1902 break;
1903 case 1: // Low immediate
1904 BuildMI(BB, PPC::ADDI, 2, Result).addReg(Tmp1).addSImm(Tmp2);
1905 break;
1906 case 2: // Shifted immediate
1907 BuildMI(BB, PPC::ADDIS, 2, Result).addReg(Tmp1).addSImm(Tmp2);
1908 break;
1909 }
1910 return Result;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001911
Nate Begemana9795f82005-03-24 04:41:43 +00001912 case ISD::AND:
Nate Begeman1cbf3ab2005-04-18 07:48:09 +00001913 if (PPCCRopts) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001914 if (N.getOperand(0).getOpcode() == ISD::SETCC ||
Nate Begeman1cbf3ab2005-04-18 07:48:09 +00001915 N.getOperand(1).getOpcode() == ISD::SETCC) {
1916 bool Inv;
1917 Tmp1 = SelectCCExpr(N, Opc, Inv, Tmp2);
1918 MoveCRtoGPR(Tmp1, Inv, Tmp2, Result);
1919 return Result;
1920 }
1921 }
Nate Begeman7ddecb42005-04-06 23:51:40 +00001922 // FIXME: should add check in getImmediateForOpcode to return a value
1923 // indicating the immediate is a run of set bits so we can emit a bitfield
1924 // clear with RLWINM instead.
1925 switch(getImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
1926 default: assert(0 && "unhandled result code");
1927 case 0: // No immediate
Nate Begemand7c4a4a2005-05-11 23:43:56 +00001928 // Check for andc: and, (xor a, -1), b
1929 if (N.getOperand(0).getOpcode() == ISD::XOR &&
1930 N.getOperand(0).getOperand(1).getOpcode() == ISD::Constant &&
1931 cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->isAllOnesValue()) {
1932 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1933 Tmp2 = SelectExpr(N.getOperand(1));
1934 BuildMI(BB, PPC::ANDC, 2, Result).addReg(Tmp2).addReg(Tmp1);
1935 return Result;
1936 }
1937 // It wasn't and-with-complement, emit a regular and
Chris Lattnercafb67b2005-05-09 17:39:48 +00001938 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman7ddecb42005-04-06 23:51:40 +00001939 Tmp2 = SelectExpr(N.getOperand(1));
Nate Begemanc7bd4822005-04-11 06:34:10 +00001940 Opc = Recording ? PPC::ANDo : PPC::AND;
1941 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
Nate Begeman7ddecb42005-04-06 23:51:40 +00001942 break;
1943 case 1: // Low immediate
Chris Lattnercafb67b2005-05-09 17:39:48 +00001944 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman7ddecb42005-04-06 23:51:40 +00001945 BuildMI(BB, PPC::ANDIo, 2, Result).addReg(Tmp1).addImm(Tmp2);
1946 break;
1947 case 2: // Shifted immediate
Chris Lattnercafb67b2005-05-09 17:39:48 +00001948 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman7ddecb42005-04-06 23:51:40 +00001949 BuildMI(BB, PPC::ANDISo, 2, Result).addReg(Tmp1).addImm(Tmp2);
1950 break;
Nate Begeman9f833d32005-04-12 00:10:02 +00001951 case 5: // Bitfield mask
1952 Opc = Recording ? PPC::RLWINMo : PPC::RLWINM;
1953 Tmp3 = Tmp2 >> 16; // MB
1954 Tmp2 &= 0xFFFF; // ME
Chris Lattnercafb67b2005-05-09 17:39:48 +00001955
1956 if (N.getOperand(0).getOpcode() == ISD::SRL)
1957 if (ConstantSDNode *SA =
1958 dyn_cast<ConstantSDNode>(N.getOperand(0).getOperand(1))) {
1959
1960 // We can fold the RLWINM and the SRL together if the mask is
1961 // clearing the top bits which are rotated around.
1962 unsigned RotAmt = 32-(SA->getValue() & 31);
1963 if (Tmp2 <= RotAmt) {
1964 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1965 BuildMI(BB, Opc, 4, Result).addReg(Tmp1).addImm(RotAmt)
1966 .addImm(Tmp3).addImm(Tmp2);
1967 break;
1968 }
1969 }
1970
1971 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman9f833d32005-04-12 00:10:02 +00001972 BuildMI(BB, Opc, 4, Result).addReg(Tmp1).addImm(0)
1973 .addImm(Tmp3).addImm(Tmp2);
1974 break;
Nate Begeman7ddecb42005-04-06 23:51:40 +00001975 }
Nate Begemanc7bd4822005-04-11 06:34:10 +00001976 RecordSuccess = true;
Nate Begeman7ddecb42005-04-06 23:51:40 +00001977 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001978
Nate Begemana9795f82005-03-24 04:41:43 +00001979 case ISD::OR:
Nate Begeman7ddecb42005-04-06 23:51:40 +00001980 if (SelectBitfieldInsert(N, Result))
1981 return Result;
Nate Begeman1cbf3ab2005-04-18 07:48:09 +00001982 if (PPCCRopts) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001983 if (N.getOperand(0).getOpcode() == ISD::SETCC ||
Nate Begeman1cbf3ab2005-04-18 07:48:09 +00001984 N.getOperand(1).getOpcode() == ISD::SETCC) {
1985 bool Inv;
1986 Tmp1 = SelectCCExpr(N, Opc, Inv, Tmp2);
1987 MoveCRtoGPR(Tmp1, Inv, Tmp2, Result);
1988 return Result;
1989 }
1990 }
Nate Begemana9795f82005-03-24 04:41:43 +00001991 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman439b4442005-04-05 04:22:58 +00001992 switch(getImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
Nate Begemana9795f82005-03-24 04:41:43 +00001993 default: assert(0 && "unhandled result code");
1994 case 0: // No immediate
1995 Tmp2 = SelectExpr(N.getOperand(1));
Nate Begemanc7bd4822005-04-11 06:34:10 +00001996 Opc = Recording ? PPC::ORo : PPC::OR;
1997 RecordSuccess = true;
1998 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +00001999 break;
2000 case 1: // Low immediate
Nate Begeman7ddecb42005-04-06 23:51:40 +00002001 BuildMI(BB, PPC::ORI, 2, Result).addReg(Tmp1).addImm(Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +00002002 break;
2003 case 2: // Shifted immediate
Nate Begeman7ddecb42005-04-06 23:51:40 +00002004 BuildMI(BB, PPC::ORIS, 2, Result).addReg(Tmp1).addImm(Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +00002005 break;
2006 }
2007 return Result;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00002008
Nate Begemanaa73a9f2005-04-03 11:20:20 +00002009 case ISD::XOR: {
2010 // Check for EQV: xor, (xor a, -1), b
2011 if (N.getOperand(0).getOpcode() == ISD::XOR &&
2012 N.getOperand(0).getOperand(1).getOpcode() == ISD::Constant &&
2013 cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->isAllOnesValue()) {
Nate Begemanaa73a9f2005-04-03 11:20:20 +00002014 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
2015 Tmp2 = SelectExpr(N.getOperand(1));
2016 BuildMI(BB, PPC::EQV, 2, Result).addReg(Tmp1).addReg(Tmp2);
2017 return Result;
2018 }
Chris Lattner837a5212005-04-21 21:09:11 +00002019 // Check for NOT, NOR, EQV, and NAND: xor (copy, or, xor, and), -1
Nate Begemanaa73a9f2005-04-03 11:20:20 +00002020 if (N.getOperand(1).getOpcode() == ISD::Constant &&
2021 cast<ConstantSDNode>(N.getOperand(1))->isAllOnesValue()) {
Nate Begemanaa73a9f2005-04-03 11:20:20 +00002022 switch(N.getOperand(0).getOpcode()) {
2023 case ISD::OR:
2024 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
2025 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
2026 BuildMI(BB, PPC::NOR, 2, Result).addReg(Tmp1).addReg(Tmp2);
2027 break;
2028 case ISD::AND:
2029 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
2030 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
2031 BuildMI(BB, PPC::NAND, 2, Result).addReg(Tmp1).addReg(Tmp2);
2032 break;
Chris Lattner837a5212005-04-21 21:09:11 +00002033 case ISD::XOR:
2034 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
2035 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
2036 BuildMI(BB, PPC::EQV, 2, Result).addReg(Tmp1).addReg(Tmp2);
2037 break;
Nate Begemanaa73a9f2005-04-03 11:20:20 +00002038 default:
2039 Tmp1 = SelectExpr(N.getOperand(0));
2040 BuildMI(BB, PPC::NOR, 2, Result).addReg(Tmp1).addReg(Tmp1);
2041 break;
2042 }
2043 return Result;
2044 }
2045 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman439b4442005-04-05 04:22:58 +00002046 switch(getImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
Nate Begemanaa73a9f2005-04-03 11:20:20 +00002047 default: assert(0 && "unhandled result code");
2048 case 0: // No immediate
2049 Tmp2 = SelectExpr(N.getOperand(1));
2050 BuildMI(BB, PPC::XOR, 2, Result).addReg(Tmp1).addReg(Tmp2);
2051 break;
2052 case 1: // Low immediate
2053 BuildMI(BB, PPC::XORI, 2, Result).addReg(Tmp1).addImm(Tmp2);
2054 break;
2055 case 2: // Shifted immediate
2056 BuildMI(BB, PPC::XORIS, 2, Result).addReg(Tmp1).addImm(Tmp2);
2057 break;
2058 }
2059 return Result;
2060 }
2061
Nate Begeman9e3e1b52005-03-24 23:35:30 +00002062 case ISD::SUB:
Nate Begemand7c4a4a2005-05-11 23:43:56 +00002063 if (1 == getImmediateForOpcode(N.getOperand(0), opcode, Tmp1, true)) {
2064 Tmp2 = SelectExpr(N.getOperand(1));
Nate Begeman27523a12005-04-02 00:42:16 +00002065 BuildMI(BB, PPC::SUBFIC, 2, Result).addReg(Tmp2).addSImm(Tmp1);
Nate Begemand7c4a4a2005-05-11 23:43:56 +00002066 } else if (1 == getImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
Nate Begeman27523a12005-04-02 00:42:16 +00002067 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begemand7c4a4a2005-05-11 23:43:56 +00002068 BuildMI(BB, PPC::ADDI, 2, Result).addReg(Tmp1).addSImm(Tmp2);
2069 } else {
2070 Tmp1 = SelectExpr(N.getOperand(0));
2071 Tmp2 = SelectExpr(N.getOperand(1));
Nate Begeman27523a12005-04-02 00:42:16 +00002072 BuildMI(BB, PPC::SUBF, 2, Result).addReg(Tmp2).addReg(Tmp1);
2073 }
Nate Begeman9e3e1b52005-03-24 23:35:30 +00002074 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002075
Nate Begeman5e966612005-03-24 06:28:42 +00002076 case ISD::MUL:
Nate Begeman9e3e1b52005-03-24 23:35:30 +00002077 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman439b4442005-04-05 04:22:58 +00002078 if (1 == getImmediateForOpcode(N.getOperand(1), opcode, Tmp2))
Nate Begeman307e7442005-03-26 01:28:53 +00002079 BuildMI(BB, PPC::MULLI, 2, Result).addReg(Tmp1).addSImm(Tmp2);
2080 else {
2081 Tmp2 = SelectExpr(N.getOperand(1));
2082 BuildMI(BB, PPC::MULLW, 2, Result).addReg(Tmp1).addReg(Tmp2);
2083 }
Nate Begeman9e3e1b52005-03-24 23:35:30 +00002084 return Result;
2085
Nate Begeman815d6da2005-04-06 00:25:27 +00002086 case ISD::MULHS:
2087 case ISD::MULHU:
2088 Tmp1 = SelectExpr(N.getOperand(0));
2089 Tmp2 = SelectExpr(N.getOperand(1));
2090 Opc = (ISD::MULHU == opcode) ? PPC::MULHWU : PPC::MULHW;
2091 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
2092 return Result;
2093
Nate Begemanf3d08f32005-03-29 00:03:27 +00002094 case ISD::SDIV:
2095 case ISD::UDIV:
Nate Begeman815d6da2005-04-06 00:25:27 +00002096 switch (getImmediateForOpcode(N.getOperand(1), opcode, Tmp3)) {
2097 default: break;
2098 // If this is an sdiv by a power of two, we can use an srawi/addze pair.
2099 case 3:
Nate Begeman80196b12005-04-05 00:15:08 +00002100 Tmp1 = MakeReg(MVT::i32);
2101 Tmp2 = SelectExpr(N.getOperand(0));
Nate Begeman9f833d32005-04-12 00:10:02 +00002102 if ((int)Tmp3 < 0) {
2103 unsigned Tmp4 = MakeReg(MVT::i32);
2104 BuildMI(BB, PPC::SRAWI, 2, Tmp1).addReg(Tmp2).addImm(-Tmp3);
2105 BuildMI(BB, PPC::ADDZE, 1, Tmp4).addReg(Tmp1);
2106 BuildMI(BB, PPC::NEG, 1, Result).addReg(Tmp4);
2107 } else {
2108 BuildMI(BB, PPC::SRAWI, 2, Tmp1).addReg(Tmp2).addImm(Tmp3);
2109 BuildMI(BB, PPC::ADDZE, 1, Result).addReg(Tmp1);
2110 }
Nate Begeman80196b12005-04-05 00:15:08 +00002111 return Result;
Nate Begeman815d6da2005-04-06 00:25:27 +00002112 // If this is a divide by constant, we can emit code using some magic
2113 // constants to implement it as a multiply instead.
Nate Begeman27b4c232005-04-06 06:44:57 +00002114 case 4:
2115 ExprMap.erase(N);
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002116 if (opcode == ISD::SDIV)
Nate Begeman27b4c232005-04-06 06:44:57 +00002117 return SelectExpr(BuildSDIVSequence(N));
2118 else
2119 return SelectExpr(BuildUDIVSequence(N));
Nate Begeman80196b12005-04-05 00:15:08 +00002120 }
Nate Begemanf3d08f32005-03-29 00:03:27 +00002121 Tmp1 = SelectExpr(N.getOperand(0));
2122 Tmp2 = SelectExpr(N.getOperand(1));
2123 Opc = (ISD::UDIV == opcode) ? PPC::DIVWU : PPC::DIVW;
2124 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
2125 return Result;
2126
Nate Begeman9e3e1b52005-03-24 23:35:30 +00002127 case ISD::ADD_PARTS:
Nate Begemanca12a2b2005-03-28 22:28:37 +00002128 case ISD::SUB_PARTS: {
2129 assert(N.getNumOperands() == 4 && N.getValueType() == MVT::i32 &&
2130 "Not an i64 add/sub!");
2131 // Emit all of the operands.
2132 std::vector<unsigned> InVals;
2133 for (unsigned i = 0, e = N.getNumOperands(); i != e; ++i)
2134 InVals.push_back(SelectExpr(N.getOperand(i)));
2135 if (N.getOpcode() == ISD::ADD_PARTS) {
Nate Begeman27eeb002005-04-02 05:59:34 +00002136 BuildMI(BB, PPC::ADDC, 2, Result).addReg(InVals[0]).addReg(InVals[2]);
2137 BuildMI(BB, PPC::ADDE, 2, Result+1).addReg(InVals[1]).addReg(InVals[3]);
Nate Begemanca12a2b2005-03-28 22:28:37 +00002138 } else {
Nate Begeman27eeb002005-04-02 05:59:34 +00002139 BuildMI(BB, PPC::SUBFC, 2, Result).addReg(InVals[2]).addReg(InVals[0]);
2140 BuildMI(BB, PPC::SUBFE, 2, Result+1).addReg(InVals[3]).addReg(InVals[1]);
2141 }
2142 return Result+N.ResNo;
2143 }
2144
2145 case ISD::SHL_PARTS:
2146 case ISD::SRA_PARTS:
2147 case ISD::SRL_PARTS: {
2148 assert(N.getNumOperands() == 3 && N.getValueType() == MVT::i32 &&
2149 "Not an i64 shift!");
2150 unsigned ShiftOpLo = SelectExpr(N.getOperand(0));
2151 unsigned ShiftOpHi = SelectExpr(N.getOperand(1));
Nate Begeman3664cef2005-04-13 22:14:14 +00002152 unsigned SHReg = FoldIfWideZeroExtend(N.getOperand(2));
2153 Tmp1 = MakeReg(MVT::i32);
2154 Tmp2 = MakeReg(MVT::i32);
Nate Begeman27eeb002005-04-02 05:59:34 +00002155 Tmp3 = MakeReg(MVT::i32);
2156 unsigned Tmp4 = MakeReg(MVT::i32);
2157 unsigned Tmp5 = MakeReg(MVT::i32);
2158 unsigned Tmp6 = MakeReg(MVT::i32);
2159 BuildMI(BB, PPC::SUBFIC, 2, Tmp1).addReg(SHReg).addSImm(32);
2160 if (ISD::SHL_PARTS == opcode) {
2161 BuildMI(BB, PPC::SLW, 2, Tmp2).addReg(ShiftOpHi).addReg(SHReg);
2162 BuildMI(BB, PPC::SRW, 2, Tmp3).addReg(ShiftOpLo).addReg(Tmp1);
2163 BuildMI(BB, PPC::OR, 2, Tmp4).addReg(Tmp2).addReg(Tmp3);
2164 BuildMI(BB, PPC::ADDI, 2, Tmp5).addReg(SHReg).addSImm(-32);
Nate Begemanfa554702005-04-03 22:13:27 +00002165 BuildMI(BB, PPC::SLW, 2, Tmp6).addReg(ShiftOpLo).addReg(Tmp5);
Nate Begeman27eeb002005-04-02 05:59:34 +00002166 BuildMI(BB, PPC::OR, 2, Result+1).addReg(Tmp4).addReg(Tmp6);
2167 BuildMI(BB, PPC::SLW, 2, Result).addReg(ShiftOpLo).addReg(SHReg);
2168 } else if (ISD::SRL_PARTS == opcode) {
2169 BuildMI(BB, PPC::SRW, 2, Tmp2).addReg(ShiftOpLo).addReg(SHReg);
2170 BuildMI(BB, PPC::SLW, 2, Tmp3).addReg(ShiftOpHi).addReg(Tmp1);
2171 BuildMI(BB, PPC::OR, 2, Tmp4).addReg(Tmp2).addReg(Tmp3);
2172 BuildMI(BB, PPC::ADDI, 2, Tmp5).addReg(SHReg).addSImm(-32);
2173 BuildMI(BB, PPC::SRW, 2, Tmp6).addReg(ShiftOpHi).addReg(Tmp5);
2174 BuildMI(BB, PPC::OR, 2, Result).addReg(Tmp4).addReg(Tmp6);
2175 BuildMI(BB, PPC::SRW, 2, Result+1).addReg(ShiftOpHi).addReg(SHReg);
2176 } else {
2177 MachineBasicBlock *TmpMBB = new MachineBasicBlock(BB->getBasicBlock());
2178 MachineBasicBlock *PhiMBB = new MachineBasicBlock(BB->getBasicBlock());
2179 MachineBasicBlock *OldMBB = BB;
2180 MachineFunction *F = BB->getParent();
2181 ilist<MachineBasicBlock>::iterator It = BB; ++It;
2182 F->getBasicBlockList().insert(It, TmpMBB);
2183 F->getBasicBlockList().insert(It, PhiMBB);
2184 BB->addSuccessor(TmpMBB);
2185 BB->addSuccessor(PhiMBB);
2186 BuildMI(BB, PPC::SRW, 2, Tmp2).addReg(ShiftOpLo).addReg(SHReg);
2187 BuildMI(BB, PPC::SLW, 2, Tmp3).addReg(ShiftOpHi).addReg(Tmp1);
2188 BuildMI(BB, PPC::OR, 2, Tmp4).addReg(Tmp2).addReg(Tmp3);
2189 BuildMI(BB, PPC::ADDICo, 2, Tmp5).addReg(SHReg).addSImm(-32);
2190 BuildMI(BB, PPC::SRAW, 2, Tmp6).addReg(ShiftOpHi).addReg(Tmp5);
2191 BuildMI(BB, PPC::SRAW, 2, Result+1).addReg(ShiftOpHi).addReg(SHReg);
2192 BuildMI(BB, PPC::BLE, 2).addReg(PPC::CR0).addMBB(PhiMBB);
2193 // Select correct least significant half if the shift amount > 32
2194 BB = TmpMBB;
2195 unsigned Tmp7 = MakeReg(MVT::i32);
2196 BuildMI(BB, PPC::OR, 2, Tmp7).addReg(Tmp6).addReg(Tmp6);
2197 TmpMBB->addSuccessor(PhiMBB);
2198 BB = PhiMBB;
2199 BuildMI(BB, PPC::PHI, 4, Result).addReg(Tmp4).addMBB(OldMBB)
2200 .addReg(Tmp7).addMBB(TmpMBB);
Nate Begemanca12a2b2005-03-28 22:28:37 +00002201 }
2202 return Result+N.ResNo;
2203 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002204
Nate Begemana9795f82005-03-24 04:41:43 +00002205 case ISD::FP_TO_UINT:
Nate Begeman6b559972005-04-01 02:59:27 +00002206 case ISD::FP_TO_SINT: {
2207 bool U = (ISD::FP_TO_UINT == opcode);
2208 Tmp1 = SelectExpr(N.getOperand(0));
2209 if (!U) {
2210 Tmp2 = MakeReg(MVT::f64);
2211 BuildMI(BB, PPC::FCTIWZ, 1, Tmp2).addReg(Tmp1);
2212 int FrameIdx = BB->getParent()->getFrameInfo()->CreateStackObject(8, 8);
2213 addFrameReference(BuildMI(BB, PPC::STFD, 3).addReg(Tmp2), FrameIdx);
2214 addFrameReference(BuildMI(BB, PPC::LWZ, 2, Result), FrameIdx, 4);
2215 return Result;
2216 } else {
2217 unsigned Zero = getConstDouble(0.0);
2218 unsigned MaxInt = getConstDouble((1LL << 32) - 1);
2219 unsigned Border = getConstDouble(1LL << 31);
2220 unsigned UseZero = MakeReg(MVT::f64);
2221 unsigned UseMaxInt = MakeReg(MVT::f64);
2222 unsigned UseChoice = MakeReg(MVT::f64);
2223 unsigned TmpReg = MakeReg(MVT::f64);
2224 unsigned TmpReg2 = MakeReg(MVT::f64);
2225 unsigned ConvReg = MakeReg(MVT::f64);
2226 unsigned IntTmp = MakeReg(MVT::i32);
2227 unsigned XorReg = MakeReg(MVT::i32);
2228 MachineFunction *F = BB->getParent();
2229 int FrameIdx = F->getFrameInfo()->CreateStackObject(8, 8);
2230 // Update machine-CFG edges
2231 MachineBasicBlock *XorMBB = new MachineBasicBlock(BB->getBasicBlock());
2232 MachineBasicBlock *PhiMBB = new MachineBasicBlock(BB->getBasicBlock());
2233 MachineBasicBlock *OldMBB = BB;
2234 ilist<MachineBasicBlock>::iterator It = BB; ++It;
2235 F->getBasicBlockList().insert(It, XorMBB);
2236 F->getBasicBlockList().insert(It, PhiMBB);
2237 BB->addSuccessor(XorMBB);
2238 BB->addSuccessor(PhiMBB);
2239 // Convert from floating point to unsigned 32-bit value
2240 // Use 0 if incoming value is < 0.0
2241 BuildMI(BB, PPC::FSEL, 3, UseZero).addReg(Tmp1).addReg(Tmp1).addReg(Zero);
2242 // Use 2**32 - 1 if incoming value is >= 2**32
2243 BuildMI(BB, PPC::FSUB, 2, UseMaxInt).addReg(MaxInt).addReg(Tmp1);
2244 BuildMI(BB, PPC::FSEL, 3, UseChoice).addReg(UseMaxInt).addReg(UseZero)
2245 .addReg(MaxInt);
2246 // Subtract 2**31
2247 BuildMI(BB, PPC::FSUB, 2, TmpReg).addReg(UseChoice).addReg(Border);
2248 // Use difference if >= 2**31
2249 BuildMI(BB, PPC::FCMPU, 2, PPC::CR0).addReg(UseChoice).addReg(Border);
2250 BuildMI(BB, PPC::FSEL, 3, TmpReg2).addReg(TmpReg).addReg(TmpReg)
2251 .addReg(UseChoice);
2252 // Convert to integer
2253 BuildMI(BB, PPC::FCTIWZ, 1, ConvReg).addReg(TmpReg2);
2254 addFrameReference(BuildMI(BB, PPC::STFD, 3).addReg(ConvReg), FrameIdx);
2255 addFrameReference(BuildMI(BB, PPC::LWZ, 2, IntTmp), FrameIdx, 4);
2256 BuildMI(BB, PPC::BLT, 2).addReg(PPC::CR0).addMBB(PhiMBB);
2257 BuildMI(BB, PPC::B, 1).addMBB(XorMBB);
2258
2259 // XorMBB:
2260 // add 2**31 if input was >= 2**31
2261 BB = XorMBB;
2262 BuildMI(BB, PPC::XORIS, 2, XorReg).addReg(IntTmp).addImm(0x8000);
2263 XorMBB->addSuccessor(PhiMBB);
2264
2265 // PhiMBB:
2266 // DestReg = phi [ IntTmp, OldMBB ], [ XorReg, XorMBB ]
2267 BB = PhiMBB;
2268 BuildMI(BB, PPC::PHI, 4, Result).addReg(IntTmp).addMBB(OldMBB)
2269 .addReg(XorReg).addMBB(XorMBB);
2270 return Result;
2271 }
2272 assert(0 && "Should never get here");
2273 return 0;
2274 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002275
Nate Begeman9e3e1b52005-03-24 23:35:30 +00002276 case ISD::SETCC:
Nate Begeman33162522005-03-29 21:54:38 +00002277 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Node)) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002278 if (ConstantSDNode *CN =
Nate Begeman7e7fadd2005-04-07 20:30:01 +00002279 dyn_cast<ConstantSDNode>(SetCC->getOperand(1).Val)) {
Nate Begeman9765c252005-04-12 21:22:28 +00002280 // We can codegen setcc op, imm very efficiently compared to a brcond.
2281 // Check for those cases here.
2282 // setcc op, 0
Nate Begeman7e7fadd2005-04-07 20:30:01 +00002283 if (CN->getValue() == 0) {
2284 Tmp1 = SelectExpr(SetCC->getOperand(0));
2285 switch (SetCC->getCondition()) {
Nate Begeman7bfba7d2005-04-14 09:45:08 +00002286 default: SetCC->dump(); assert(0 && "Unhandled SetCC condition"); abort();
Nate Begeman7e7fadd2005-04-07 20:30:01 +00002287 case ISD::SETEQ:
Nate Begeman7e7fadd2005-04-07 20:30:01 +00002288 Tmp2 = MakeReg(MVT::i32);
2289 BuildMI(BB, PPC::CNTLZW, 1, Tmp2).addReg(Tmp1);
2290 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp2).addImm(27)
2291 .addImm(5).addImm(31);
2292 break;
2293 case ISD::SETNE:
Nate Begeman7e7fadd2005-04-07 20:30:01 +00002294 Tmp2 = MakeReg(MVT::i32);
2295 BuildMI(BB, PPC::ADDIC, 2, Tmp2).addReg(Tmp1).addSImm(-1);
2296 BuildMI(BB, PPC::SUBFE, 2, Result).addReg(Tmp2).addReg(Tmp1);
2297 break;
Nate Begeman7e7fadd2005-04-07 20:30:01 +00002298 case ISD::SETLT:
2299 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(1)
2300 .addImm(31).addImm(31);
2301 break;
Nate Begeman7e7fadd2005-04-07 20:30:01 +00002302 case ISD::SETGT:
2303 Tmp2 = MakeReg(MVT::i32);
2304 Tmp3 = MakeReg(MVT::i32);
2305 BuildMI(BB, PPC::NEG, 2, Tmp2).addReg(Tmp1);
2306 BuildMI(BB, PPC::ANDC, 2, Tmp3).addReg(Tmp2).addReg(Tmp1);
2307 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp3).addImm(1)
2308 .addImm(31).addImm(31);
2309 break;
Nate Begeman9765c252005-04-12 21:22:28 +00002310 }
2311 return Result;
2312 }
2313 // setcc op, -1
2314 if (CN->isAllOnesValue()) {
2315 Tmp1 = SelectExpr(SetCC->getOperand(0));
2316 switch (SetCC->getCondition()) {
2317 default: assert(0 && "Unhandled SetCC condition"); abort();
2318 case ISD::SETEQ:
2319 Tmp2 = MakeReg(MVT::i32);
2320 Tmp3 = MakeReg(MVT::i32);
2321 BuildMI(BB, PPC::ADDIC, 2, Tmp2).addReg(Tmp1).addSImm(1);
2322 BuildMI(BB, PPC::LI, 1, Tmp3).addSImm(0);
2323 BuildMI(BB, PPC::ADDZE, 1, Result).addReg(Tmp3);
Nate Begeman7e7fadd2005-04-07 20:30:01 +00002324 break;
Nate Begeman9765c252005-04-12 21:22:28 +00002325 case ISD::SETNE:
2326 Tmp2 = MakeReg(MVT::i32);
2327 Tmp3 = MakeReg(MVT::i32);
2328 BuildMI(BB, PPC::NOR, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
2329 BuildMI(BB, PPC::ADDIC, 2, Tmp3).addReg(Tmp2).addSImm(-1);
2330 BuildMI(BB, PPC::SUBFE, 2, Result).addReg(Tmp3).addReg(Tmp2);
2331 break;
2332 case ISD::SETLT:
2333 Tmp2 = MakeReg(MVT::i32);
2334 Tmp3 = MakeReg(MVT::i32);
2335 BuildMI(BB, PPC::ADDI, 2, Tmp2).addReg(Tmp1).addSImm(1);
2336 BuildMI(BB, PPC::AND, 2, Tmp3).addReg(Tmp2).addReg(Tmp1);
2337 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp3).addImm(1)
2338 .addImm(31).addImm(31);
2339 break;
2340 case ISD::SETGT:
2341 Tmp2 = MakeReg(MVT::i32);
Nate Begeman7e7fadd2005-04-07 20:30:01 +00002342 BuildMI(BB, PPC::RLWINM, 4, Tmp2).addReg(Tmp1).addImm(1)
2343 .addImm(31).addImm(31);
2344 BuildMI(BB, PPC::XORI, 2, Result).addReg(Tmp2).addImm(1);
2345 break;
2346 }
2347 return Result;
2348 }
2349 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002350
Nate Begeman1cbf3ab2005-04-18 07:48:09 +00002351 bool Inv;
2352 unsigned CCReg = SelectCC(N, Opc, Inv, Tmp2);
2353 MoveCRtoGPR(CCReg, Inv, Tmp2, Result);
Nate Begeman33162522005-03-29 21:54:38 +00002354 return Result;
2355 }
2356 assert(0 && "Is this legal?");
2357 return 0;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002358
Nate Begeman74747862005-03-29 22:24:51 +00002359 case ISD::SELECT: {
Nate Begeman1cbf3ab2005-04-18 07:48:09 +00002360 bool Inv;
Chris Lattner30710192005-04-01 07:10:02 +00002361 unsigned TrueValue = SelectExpr(N.getOperand(1)); //Use if TRUE
2362 unsigned FalseValue = SelectExpr(N.getOperand(2)); //Use if FALSE
Nate Begeman1cbf3ab2005-04-18 07:48:09 +00002363 unsigned CCReg = SelectCC(N.getOperand(0), Opc, Inv, Tmp3);
Chris Lattner30710192005-04-01 07:10:02 +00002364
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002365 // Create an iterator with which to insert the MBB for copying the false
Nate Begeman74747862005-03-29 22:24:51 +00002366 // value and the MBB to hold the PHI instruction for this SetCC.
2367 MachineBasicBlock *thisMBB = BB;
2368 const BasicBlock *LLVM_BB = BB->getBasicBlock();
2369 ilist<MachineBasicBlock>::iterator It = BB;
2370 ++It;
2371
2372 // thisMBB:
2373 // ...
2374 // TrueVal = ...
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00002375 // cmpTY ccX, r1, r2
Nate Begeman74747862005-03-29 22:24:51 +00002376 // bCC copy1MBB
2377 // fallthrough --> copy0MBB
Nate Begeman74747862005-03-29 22:24:51 +00002378 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
2379 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00002380 BuildMI(BB, Opc, 2).addReg(CCReg).addMBB(sinkMBB);
Nate Begeman74747862005-03-29 22:24:51 +00002381 MachineFunction *F = BB->getParent();
2382 F->getBasicBlockList().insert(It, copy0MBB);
2383 F->getBasicBlockList().insert(It, sinkMBB);
2384 // Update machine-CFG edges
2385 BB->addSuccessor(copy0MBB);
2386 BB->addSuccessor(sinkMBB);
2387
2388 // copy0MBB:
2389 // %FalseValue = ...
2390 // # fallthrough to sinkMBB
2391 BB = copy0MBB;
Nate Begeman74747862005-03-29 22:24:51 +00002392 // Update machine-CFG edges
2393 BB->addSuccessor(sinkMBB);
2394
2395 // sinkMBB:
2396 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
2397 // ...
2398 BB = sinkMBB;
2399 BuildMI(BB, PPC::PHI, 4, Result).addReg(FalseValue)
2400 .addMBB(copy0MBB).addReg(TrueValue).addMBB(thisMBB);
Nate Begeman74747862005-03-29 22:24:51 +00002401 return Result;
2402 }
Nate Begemana9795f82005-03-24 04:41:43 +00002403
2404 case ISD::Constant:
2405 switch (N.getValueType()) {
2406 default: assert(0 && "Cannot use constants of this type!");
2407 case MVT::i1:
2408 BuildMI(BB, PPC::LI, 1, Result)
2409 .addSImm(!cast<ConstantSDNode>(N)->isNullValue());
2410 break;
2411 case MVT::i32:
2412 {
2413 int v = (int)cast<ConstantSDNode>(N)->getSignExtended();
2414 if (v < 32768 && v >= -32768) {
2415 BuildMI(BB, PPC::LI, 1, Result).addSImm(v);
2416 } else {
Nate Begeman5e966612005-03-24 06:28:42 +00002417 Tmp1 = MakeReg(MVT::i32);
2418 BuildMI(BB, PPC::LIS, 1, Tmp1).addSImm(v >> 16);
2419 BuildMI(BB, PPC::ORI, 2, Result).addReg(Tmp1).addImm(v & 0xFFFF);
Nate Begemana9795f82005-03-24 04:41:43 +00002420 }
2421 }
2422 }
2423 return Result;
2424 }
2425
2426 return 0;
2427}
2428
2429void ISel::Select(SDOperand N) {
2430 unsigned Tmp1, Tmp2, Opc;
2431 unsigned opcode = N.getOpcode();
2432
2433 if (!ExprMap.insert(std::make_pair(N, 1)).second)
2434 return; // Already selected.
2435
2436 SDNode *Node = N.Val;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002437
Nate Begemana9795f82005-03-24 04:41:43 +00002438 switch (Node->getOpcode()) {
2439 default:
2440 Node->dump(); std::cerr << "\n";
2441 assert(0 && "Node not handled yet!");
2442 case ISD::EntryToken: return; // Noop
2443 case ISD::TokenFactor:
2444 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
2445 Select(Node->getOperand(i));
2446 return;
Chris Lattner16cd04d2005-05-12 23:24:06 +00002447 case ISD::CALLSEQ_START:
2448 case ISD::CALLSEQ_END:
Nate Begemana9795f82005-03-24 04:41:43 +00002449 Select(N.getOperand(0));
2450 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
Chris Lattner16cd04d2005-05-12 23:24:06 +00002451 Opc = N.getOpcode() == ISD::CALLSEQ_START ? PPC::ADJCALLSTACKDOWN :
Nate Begemana9795f82005-03-24 04:41:43 +00002452 PPC::ADJCALLSTACKUP;
2453 BuildMI(BB, Opc, 1).addImm(Tmp1);
2454 return;
2455 case ISD::BR: {
2456 MachineBasicBlock *Dest =
2457 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
Nate Begemana9795f82005-03-24 04:41:43 +00002458 Select(N.getOperand(0));
2459 BuildMI(BB, PPC::B, 1).addMBB(Dest);
2460 return;
2461 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002462 case ISD::BRCOND:
Nate Begemancd08e4c2005-04-09 20:09:12 +00002463 case ISD::BRCONDTWOWAY:
Nate Begemana9795f82005-03-24 04:41:43 +00002464 SelectBranchCC(N);
2465 return;
2466 case ISD::CopyToReg:
2467 Select(N.getOperand(0));
2468 Tmp1 = SelectExpr(N.getOperand(1));
2469 Tmp2 = cast<RegSDNode>(N)->getReg();
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002470
Nate Begemana9795f82005-03-24 04:41:43 +00002471 if (Tmp1 != Tmp2) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002472 if (N.getOperand(1).getValueType() == MVT::f64 ||
Nate Begemana9795f82005-03-24 04:41:43 +00002473 N.getOperand(1).getValueType() == MVT::f32)
2474 BuildMI(BB, PPC::FMR, 1, Tmp2).addReg(Tmp1);
2475 else
2476 BuildMI(BB, PPC::OR, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
2477 }
2478 return;
2479 case ISD::ImplicitDef:
2480 Select(N.getOperand(0));
2481 BuildMI(BB, PPC::IMPLICIT_DEF, 0, cast<RegSDNode>(N)->getReg());
2482 return;
2483 case ISD::RET:
2484 switch (N.getNumOperands()) {
2485 default:
2486 assert(0 && "Unknown return instruction!");
2487 case 3:
2488 assert(N.getOperand(1).getValueType() == MVT::i32 &&
2489 N.getOperand(2).getValueType() == MVT::i32 &&
Misha Brukman7847fca2005-04-22 17:54:37 +00002490 "Unknown two-register value!");
Nate Begemana9795f82005-03-24 04:41:43 +00002491 Select(N.getOperand(0));
2492 Tmp1 = SelectExpr(N.getOperand(1));
2493 Tmp2 = SelectExpr(N.getOperand(2));
Nate Begeman27523a12005-04-02 00:42:16 +00002494 BuildMI(BB, PPC::OR, 2, PPC::R3).addReg(Tmp2).addReg(Tmp2);
2495 BuildMI(BB, PPC::OR, 2, PPC::R4).addReg(Tmp1).addReg(Tmp1);
Nate Begemana9795f82005-03-24 04:41:43 +00002496 break;
2497 case 2:
2498 Select(N.getOperand(0));
2499 Tmp1 = SelectExpr(N.getOperand(1));
2500 switch (N.getOperand(1).getValueType()) {
2501 default:
2502 assert(0 && "Unknown return type!");
2503 case MVT::f64:
2504 case MVT::f32:
2505 BuildMI(BB, PPC::FMR, 1, PPC::F1).addReg(Tmp1);
2506 break;
2507 case MVT::i32:
2508 BuildMI(BB, PPC::OR, 2, PPC::R3).addReg(Tmp1).addReg(Tmp1);
2509 break;
2510 }
Nate Begeman9e3e1b52005-03-24 23:35:30 +00002511 case 1:
2512 Select(N.getOperand(0));
2513 break;
Nate Begemana9795f82005-03-24 04:41:43 +00002514 }
2515 BuildMI(BB, PPC::BLR, 0); // Just emit a 'ret' instruction
2516 return;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002517 case ISD::TRUNCSTORE:
2518 case ISD::STORE:
Nate Begemana9795f82005-03-24 04:41:43 +00002519 {
2520 SDOperand Chain = N.getOperand(0);
2521 SDOperand Value = N.getOperand(1);
2522 SDOperand Address = N.getOperand(2);
2523 Select(Chain);
2524
2525 Tmp1 = SelectExpr(Value); //value
2526
2527 if (opcode == ISD::STORE) {
2528 switch(Value.getValueType()) {
2529 default: assert(0 && "unknown Type in store");
2530 case MVT::i32: Opc = PPC::STW; break;
2531 case MVT::f64: Opc = PPC::STFD; break;
2532 case MVT::f32: Opc = PPC::STFS; break;
2533 }
2534 } else { //ISD::TRUNCSTORE
2535 switch(cast<MVTSDNode>(Node)->getExtraValueType()) {
2536 default: assert(0 && "unknown Type in store");
Nate Begeman7e7fadd2005-04-07 20:30:01 +00002537 case MVT::i1:
Nate Begemana9795f82005-03-24 04:41:43 +00002538 case MVT::i8: Opc = PPC::STB; break;
2539 case MVT::i16: Opc = PPC::STH; break;
2540 }
2541 }
2542
Nate Begemana7e11a42005-04-01 05:57:17 +00002543 if(Address.getOpcode() == ISD::FrameIndex)
Nate Begemana9795f82005-03-24 04:41:43 +00002544 {
Nate Begeman58f718c2005-03-30 02:23:08 +00002545 Tmp2 = cast<FrameIndexSDNode>(Address)->getIndex();
2546 addFrameReference(BuildMI(BB, Opc, 3).addReg(Tmp1), (int)Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +00002547 }
2548 else
2549 {
2550 int offset;
Nate Begeman04730362005-04-01 04:45:11 +00002551 bool idx = SelectAddr(Address, Tmp2, offset);
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002552 if (idx) {
Nate Begeman04730362005-04-01 04:45:11 +00002553 Opc = IndexedOpForOp(Opc);
2554 BuildMI(BB, Opc, 3).addReg(Tmp1).addReg(Tmp2).addReg(offset);
2555 } else {
2556 BuildMI(BB, Opc, 3).addReg(Tmp1).addImm(offset).addReg(Tmp2);
2557 }
Nate Begemana9795f82005-03-24 04:41:43 +00002558 }
2559 return;
2560 }
2561 case ISD::EXTLOAD:
2562 case ISD::SEXTLOAD:
2563 case ISD::ZEXTLOAD:
2564 case ISD::LOAD:
2565 case ISD::CopyFromReg:
Chris Lattnerb5d8e6e2005-05-13 20:29:26 +00002566 case ISD::TAILCALL:
Nate Begemana9795f82005-03-24 04:41:43 +00002567 case ISD::CALL:
2568 case ISD::DYNAMIC_STACKALLOC:
2569 ExprMap.erase(N);
2570 SelectExpr(N);
2571 return;
2572 }
2573 assert(0 && "Should not be reached!");
2574}
2575
2576
2577/// createPPC32PatternInstructionSelector - This pass converts an LLVM function
2578/// into a machine code representation using pattern matching and a machine
2579/// description file.
2580///
2581FunctionPass *llvm::createPPC32ISelPattern(TargetMachine &TM) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002582 return new ISel(TM);
Chris Lattner246fa632005-03-24 06:16:18 +00002583}
2584