blob: 63188420493e74aee02a084bf961f18022de02e2 [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 Begemana3fd4002005-07-19 16:51:05 +000020#include "llvm/Constants.h"
Nate Begemana9795f82005-03-24 04:41:43 +000021#include "llvm/Function.h"
Nate Begemana3fd4002005-07-19 16:51:05 +000022#include "llvm/CodeGen/MachineConstantPool.h"
Nate Begemana9795f82005-03-24 04:41:43 +000023#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);
Chris Lattner32f3cf62005-05-13 16:20:22 +000070 setOperationAction(ISD::SREM , MVT::f64, Expand);
Chris Lattner17234b72005-04-30 04:26:06 +000071 setOperationAction(ISD::FSIN , MVT::f32, Expand);
72 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Chris Lattner32f3cf62005-05-13 16:20:22 +000073 setOperationAction(ISD::SREM , MVT::f32, Expand);
Chris Lattner17234b72005-04-30 04:26:06 +000074
Nate Begemanadeb43d2005-07-20 22:42:00 +000075 // If we're enabling GP optimizations, use hardware square root
Nate Begeman2497e632005-07-21 20:44:43 +000076 if (!GPOPT) {
Nate Begemanadeb43d2005-07-20 22:42:00 +000077 setOperationAction(ISD::FSQRT, MVT::f64, Expand);
78 setOperationAction(ISD::FSQRT, MVT::f32, Expand);
79 }
Jeff Cohen00b168892005-07-27 06:12:32 +000080
Nate Begemand7c4a4a2005-05-11 23:43:56 +000081 //PowerPC does not have CTPOP or CTTZ
Andrew Lenharth691ef2b2005-05-03 17:19:30 +000082 setOperationAction(ISD::CTPOP, MVT::i32 , Expand);
83 setOperationAction(ISD::CTTZ , MVT::i32 , Expand);
Andrew Lenharth691ef2b2005-05-03 17:19:30 +000084
Chris Lattnercbd06fc2005-04-07 19:41:49 +000085 setSetCCResultContents(ZeroOrOneSetCCResult);
Nate Begeman3e897162005-03-31 23:55:40 +000086 addLegalFPImmediate(+0.0); // Necessary for FSEL
Misha Brukmanb5f662f2005-04-21 23:30:14 +000087 addLegalFPImmediate(-0.0); //
Nate Begeman3e897162005-03-31 23:55:40 +000088
Nate Begemana9795f82005-03-24 04:41:43 +000089 computeRegisterProperties();
90 }
91
92 /// LowerArguments - This hook must be implemented to indicate how we should
93 /// lower the arguments for the specified function, into the specified DAG.
94 virtual std::vector<SDOperand>
95 LowerArguments(Function &F, SelectionDAG &DAG);
Misha Brukmanb5f662f2005-04-21 23:30:14 +000096
Nate Begemana9795f82005-03-24 04:41:43 +000097 /// LowerCallTo - This hook lowers an abstract call to a function into an
98 /// actual call.
99 virtual std::pair<SDOperand, SDOperand>
Chris Lattnerc57f6822005-05-12 19:56:45 +0000100 LowerCallTo(SDOperand Chain, const Type *RetTy, bool isVarArg, unsigned CC,
Chris Lattneradf6a962005-05-13 18:50:42 +0000101 bool isTailCall, SDOperand Callee, ArgListTy &Args,
102 SelectionDAG &DAG);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000103
Chris Lattnere0fe2252005-07-05 19:58:54 +0000104 virtual SDOperand LowerVAStart(SDOperand Chain, SDOperand VAListP,
105 Value *VAListV, SelectionDAG &DAG);
Jeff Cohen00b168892005-07-27 06:12:32 +0000106
Nate Begemana9795f82005-03-24 04:41:43 +0000107 virtual std::pair<SDOperand,SDOperand>
Chris Lattnere0fe2252005-07-05 19:58:54 +0000108 LowerVAArg(SDOperand Chain, SDOperand VAListP, Value *VAListV,
109 const Type *ArgTy, SelectionDAG &DAG);
Jeff Cohen00b168892005-07-27 06:12:32 +0000110
Nate Begemana9795f82005-03-24 04:41:43 +0000111 virtual std::pair<SDOperand, SDOperand>
112 LowerFrameReturnAddress(bool isFrameAddr, SDOperand Chain, unsigned Depth,
113 SelectionDAG &DAG);
114 };
115}
116
117
118std::vector<SDOperand>
119PPC32TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
120 //
121 // add beautiful description of PPC stack frame format, or at least some docs
122 //
123 MachineFunction &MF = DAG.getMachineFunction();
124 MachineFrameInfo *MFI = MF.getFrameInfo();
125 MachineBasicBlock& BB = MF.front();
126 std::vector<SDOperand> ArgValues;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000127
128 // Due to the rather complicated nature of the PowerPC ABI, rather than a
Nate Begemana9795f82005-03-24 04:41:43 +0000129 // fixed size array of physical args, for the sake of simplicity let the STL
130 // handle tracking them for us.
131 std::vector<unsigned> argVR, argPR, argOp;
132 unsigned ArgOffset = 24;
133 unsigned GPR_remaining = 8;
134 unsigned FPR_remaining = 13;
135 unsigned GPR_idx = 0, FPR_idx = 0;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000136 static const unsigned GPR[] = {
Nate Begemana9795f82005-03-24 04:41:43 +0000137 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
138 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
139 };
140 static const unsigned FPR[] = {
141 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
142 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
143 };
144
145 // Add DAG nodes to load the arguments... On entry to a function on PPC,
146 // the arguments start at offset 24, although they are likely to be passed
147 // in registers.
148 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
149 SDOperand newroot, argt;
150 unsigned ObjSize;
151 bool needsLoad = false;
Nate Begemancd08e4c2005-04-09 20:09:12 +0000152 bool ArgLive = !I->use_empty();
Nate Begemana9795f82005-03-24 04:41:43 +0000153 MVT::ValueType ObjectVT = getValueType(I->getType());
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000154
Nate Begemana9795f82005-03-24 04:41:43 +0000155 switch (ObjectVT) {
156 default: assert(0 && "Unhandled argument type!");
157 case MVT::i1:
158 case MVT::i8:
159 case MVT::i16:
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000160 case MVT::i32:
Nate Begemana9795f82005-03-24 04:41:43 +0000161 ObjSize = 4;
Nate Begemancd08e4c2005-04-09 20:09:12 +0000162 if (!ArgLive) break;
Nate Begemana9795f82005-03-24 04:41:43 +0000163 if (GPR_remaining > 0) {
Nate Begemancd08e4c2005-04-09 20:09:12 +0000164 MF.addLiveIn(GPR[GPR_idx]);
Nate Begemanf70b5762005-03-28 23:08:54 +0000165 argt = newroot = DAG.getCopyFromReg(GPR[GPR_idx], MVT::i32,
166 DAG.getRoot());
Nate Begemana9795f82005-03-24 04:41:43 +0000167 if (ObjectVT != MVT::i32)
168 argt = DAG.getNode(ISD::TRUNCATE, ObjectVT, newroot);
Nate Begemana9795f82005-03-24 04:41:43 +0000169 } else {
170 needsLoad = true;
171 }
172 break;
Nate Begemanf7e43382005-03-26 07:46:36 +0000173 case MVT::i64: ObjSize = 8;
Nate Begemancd08e4c2005-04-09 20:09:12 +0000174 if (!ArgLive) break;
Nate Begemanc5b1cd22005-04-10 05:53:14 +0000175 if (GPR_remaining > 0) {
176 SDOperand argHi, argLo;
Nate Begemancd08e4c2005-04-09 20:09:12 +0000177 MF.addLiveIn(GPR[GPR_idx]);
Nate Begemanc5b1cd22005-04-10 05:53:14 +0000178 argHi = DAG.getCopyFromReg(GPR[GPR_idx], MVT::i32, DAG.getRoot());
179 // If we have two or more remaining argument registers, then both halves
180 // of the i64 can be sourced from there. Otherwise, the lower half will
181 // have to come off the stack. This can happen when an i64 is preceded
182 // by 28 bytes of arguments.
183 if (GPR_remaining > 1) {
184 MF.addLiveIn(GPR[GPR_idx+1]);
185 argLo = DAG.getCopyFromReg(GPR[GPR_idx+1], MVT::i32, argHi);
186 } else {
187 int FI = MFI->CreateFixedObject(4, ArgOffset+4);
188 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
Chris Lattner022ed322005-05-15 19:54:37 +0000189 argLo = DAG.getLoad(MVT::i32, DAG.getEntryNode(), FIN,
190 DAG.getSrcValue(NULL));
Nate Begemanc5b1cd22005-04-10 05:53:14 +0000191 }
Nate Begemanca12a2b2005-03-28 22:28:37 +0000192 // Build the outgoing arg thingy
Nate Begemanf70b5762005-03-28 23:08:54 +0000193 argt = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, argLo, argHi);
194 newroot = argLo;
Nate Begemana9795f82005-03-24 04:41:43 +0000195 } else {
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000196 needsLoad = true;
Nate Begemana9795f82005-03-24 04:41:43 +0000197 }
198 break;
Nate Begemancd08e4c2005-04-09 20:09:12 +0000199 case MVT::f32:
200 case MVT::f64:
201 ObjSize = (ObjectVT == MVT::f64) ? 8 : 4;
202 if (!ArgLive) break;
Nate Begemana9795f82005-03-24 04:41:43 +0000203 if (FPR_remaining > 0) {
Nate Begemancd08e4c2005-04-09 20:09:12 +0000204 MF.addLiveIn(FPR[FPR_idx]);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000205 argt = newroot = DAG.getCopyFromReg(FPR[FPR_idx], ObjectVT,
Nate Begemanf70b5762005-03-28 23:08:54 +0000206 DAG.getRoot());
Nate Begemana9795f82005-03-24 04:41:43 +0000207 --FPR_remaining;
208 ++FPR_idx;
209 } else {
210 needsLoad = true;
211 }
212 break;
213 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000214
Nate Begemana9795f82005-03-24 04:41:43 +0000215 // We need to load the argument to a virtual register if we determined above
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000216 // that we ran out of physical registers of the appropriate type
Nate Begemana9795f82005-03-24 04:41:43 +0000217 if (needsLoad) {
Nate Begemane5846682005-04-04 06:52:38 +0000218 unsigned SubregOffset = 0;
Nate Begemanc3e2db42005-04-04 09:09:00 +0000219 if (ObjectVT == MVT::i8 || ObjectVT == MVT::i1) SubregOffset = 3;
Nate Begemane5846682005-04-04 06:52:38 +0000220 if (ObjectVT == MVT::i16) SubregOffset = 2;
Nate Begemana9795f82005-03-24 04:41:43 +0000221 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
222 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000223 FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN,
Nate Begemane5846682005-04-04 06:52:38 +0000224 DAG.getConstant(SubregOffset, MVT::i32));
Chris Lattner022ed322005-05-15 19:54:37 +0000225 argt = newroot = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
226 DAG.getSrcValue(NULL));
Nate Begemana9795f82005-03-24 04:41:43 +0000227 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000228
Nate Begemana9795f82005-03-24 04:41:43 +0000229 // Every 4 bytes of argument space consumes one of the GPRs available for
230 // argument passing.
231 if (GPR_remaining > 0) {
232 unsigned delta = (GPR_remaining > 1 && ObjSize == 8) ? 2 : 1;
233 GPR_remaining -= delta;
234 GPR_idx += delta;
235 }
236 ArgOffset += ObjSize;
Chris Lattner91277ea2005-04-09 21:23:24 +0000237 if (newroot.Val)
238 DAG.setRoot(newroot.getValue(1));
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000239
Nate Begemana9795f82005-03-24 04:41:43 +0000240 ArgValues.push_back(argt);
241 }
242
Nate Begemana9795f82005-03-24 04:41:43 +0000243 // If the function takes variable number of arguments, make a frame index for
244 // the start of the first vararg value... for expansion of llvm.va_start.
Nate Begemanfa554702005-04-03 22:13:27 +0000245 if (F.isVarArg()) {
Nate Begemana9795f82005-03-24 04:41:43 +0000246 VarArgsFrameIndex = MFI->CreateFixedObject(4, ArgOffset);
Nate Begemanfa554702005-04-03 22:13:27 +0000247 SDOperand FIN = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
Nate Begeman6644d4c2005-04-03 23:11:17 +0000248 // If this function is vararg, store any remaining integer argument regs
249 // to their spots on the stack so that they may be loaded by deferencing the
250 // result of va_next.
251 std::vector<SDOperand> MemOps;
252 for (; GPR_remaining > 0; --GPR_remaining, ++GPR_idx) {
Nate Begemancd08e4c2005-04-09 20:09:12 +0000253 MF.addLiveIn(GPR[GPR_idx]);
Nate Begeman6644d4c2005-04-03 23:11:17 +0000254 SDOperand Val = DAG.getCopyFromReg(GPR[GPR_idx], MVT::i32, DAG.getRoot());
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000255 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Val.getValue(1),
Andrew Lenharth2d86ea22005-04-27 20:10:01 +0000256 Val, FIN, DAG.getSrcValue(NULL));
Nate Begeman6644d4c2005-04-03 23:11:17 +0000257 MemOps.push_back(Store);
258 // Increment the address by four for the next argument to store
259 SDOperand PtrOff = DAG.getConstant(4, getPointerTy());
260 FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN, PtrOff);
261 }
262 DAG.setRoot(DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps));
Nate Begemanfa554702005-04-03 22:13:27 +0000263 }
Nate Begemana9795f82005-03-24 04:41:43 +0000264
Nate Begemancd08e4c2005-04-09 20:09:12 +0000265 // Finally, inform the code generator which regs we return values in.
266 switch (getValueType(F.getReturnType())) {
267 default: assert(0 && "Unknown type!");
268 case MVT::isVoid: break;
269 case MVT::i1:
270 case MVT::i8:
271 case MVT::i16:
272 case MVT::i32:
273 MF.addLiveOut(PPC::R3);
274 break;
275 case MVT::i64:
276 MF.addLiveOut(PPC::R3);
277 MF.addLiveOut(PPC::R4);
278 break;
279 case MVT::f32:
280 case MVT::f64:
281 MF.addLiveOut(PPC::F1);
282 break;
283 }
284
Nate Begemana9795f82005-03-24 04:41:43 +0000285 return ArgValues;
286}
287
288std::pair<SDOperand, SDOperand>
289PPC32TargetLowering::LowerCallTo(SDOperand Chain,
Misha Brukman7847fca2005-04-22 17:54:37 +0000290 const Type *RetTy, bool isVarArg,
Jeff Cohen00b168892005-07-27 06:12:32 +0000291 unsigned CallingConv, bool isTailCall,
Misha Brukman7847fca2005-04-22 17:54:37 +0000292 SDOperand Callee, ArgListTy &Args,
293 SelectionDAG &DAG) {
Nate Begeman307e7442005-03-26 01:28:53 +0000294 // args_to_use will accumulate outgoing args for the ISD::CALL case in
295 // SelectExpr to use to put the arguments in the appropriate registers.
Nate Begemana9795f82005-03-24 04:41:43 +0000296 std::vector<SDOperand> args_to_use;
Nate Begeman307e7442005-03-26 01:28:53 +0000297
298 // Count how many bytes are to be pushed on the stack, including the linkage
299 // area, and parameter passing area.
300 unsigned NumBytes = 24;
301
302 if (Args.empty()) {
Chris Lattner16cd04d2005-05-12 23:24:06 +0000303 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
Nate Begemana7e11a42005-04-01 05:57:17 +0000304 DAG.getConstant(NumBytes, getPointerTy()));
Nate Begeman307e7442005-03-26 01:28:53 +0000305 } else {
306 for (unsigned i = 0, e = Args.size(); i != e; ++i)
307 switch (getValueType(Args[i].second)) {
308 default: assert(0 && "Unknown value type!");
309 case MVT::i1:
310 case MVT::i8:
311 case MVT::i16:
312 case MVT::i32:
313 case MVT::f32:
314 NumBytes += 4;
315 break;
316 case MVT::i64:
317 case MVT::f64:
318 NumBytes += 8;
319 break;
320 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000321
322 // Just to be safe, we'll always reserve the full 24 bytes of linkage area
Nate Begeman307e7442005-03-26 01:28:53 +0000323 // plus 32 bytes of argument space in case any called code gets funky on us.
324 if (NumBytes < 56) NumBytes = 56;
325
326 // Adjust the stack pointer for the new arguments...
327 // These operations are automatically eliminated by the prolog/epilog pass
Chris Lattner16cd04d2005-05-12 23:24:06 +0000328 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
Nate Begeman307e7442005-03-26 01:28:53 +0000329 DAG.getConstant(NumBytes, getPointerTy()));
330
331 // Set up a copy of the stack pointer for use loading and storing any
332 // arguments that may not fit in the registers available for argument
333 // passing.
334 SDOperand StackPtr = DAG.getCopyFromReg(PPC::R1, MVT::i32,
335 DAG.getEntryNode());
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000336
Nate Begeman307e7442005-03-26 01:28:53 +0000337 // Figure out which arguments are going to go in registers, and which in
338 // memory. Also, if this is a vararg function, floating point operations
339 // must be stored to our stack, and loaded into integer regs as well, if
340 // any integer regs are available for argument passing.
341 unsigned ArgOffset = 24;
342 unsigned GPR_remaining = 8;
343 unsigned FPR_remaining = 13;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000344
Nate Begeman74d73452005-03-31 00:15:26 +0000345 std::vector<SDOperand> MemOps;
Nate Begeman307e7442005-03-26 01:28:53 +0000346 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
347 // PtrOff will be used to store the current argument to the stack if a
348 // register cannot be found for it.
349 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
350 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
Nate Begemanf7e43382005-03-26 07:46:36 +0000351 MVT::ValueType ArgVT = getValueType(Args[i].second);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000352
Nate Begemanf7e43382005-03-26 07:46:36 +0000353 switch (ArgVT) {
Nate Begeman307e7442005-03-26 01:28:53 +0000354 default: assert(0 && "Unexpected ValueType for argument!");
355 case MVT::i1:
356 case MVT::i8:
357 case MVT::i16:
358 // Promote the integer to 32 bits. If the input type is signed use a
359 // sign extend, otherwise use a zero extend.
360 if (Args[i].second->isSigned())
361 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
362 else
363 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
364 // FALL THROUGH
365 case MVT::i32:
366 if (GPR_remaining > 0) {
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000367 args_to_use.push_back(Args[i].first);
Nate Begeman307e7442005-03-26 01:28:53 +0000368 --GPR_remaining;
369 } else {
Nate Begeman74d73452005-03-31 00:15:26 +0000370 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
Chris Lattner022ed322005-05-15 19:54:37 +0000371 Args[i].first, PtrOff,
372 DAG.getSrcValue(NULL)));
Nate Begeman307e7442005-03-26 01:28:53 +0000373 }
374 ArgOffset += 4;
375 break;
376 case MVT::i64:
Nate Begemanf7e43382005-03-26 07:46:36 +0000377 // If we have one free GPR left, we can place the upper half of the i64
378 // in it, and store the other half to the stack. If we have two or more
379 // free GPRs, then we can pass both halves of the i64 in registers.
380 if (GPR_remaining > 0) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000381 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
Nate Begemanf2622612005-03-26 02:17:46 +0000382 Args[i].first, DAG.getConstant(1, MVT::i32));
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000383 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
Nate Begemanf2622612005-03-26 02:17:46 +0000384 Args[i].first, DAG.getConstant(0, MVT::i32));
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000385 args_to_use.push_back(Hi);
Nate Begeman74d73452005-03-31 00:15:26 +0000386 --GPR_remaining;
Nate Begeman74d73452005-03-31 00:15:26 +0000387 if (GPR_remaining > 0) {
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000388 args_to_use.push_back(Lo);
Nate Begeman74d73452005-03-31 00:15:26 +0000389 --GPR_remaining;
Nate Begemanf7e43382005-03-26 07:46:36 +0000390 } else {
391 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
392 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
Nate Begeman74d73452005-03-31 00:15:26 +0000393 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +0000394 Lo, PtrOff, DAG.getSrcValue(NULL)));
Nate Begemanf7e43382005-03-26 07:46:36 +0000395 }
Nate Begeman307e7442005-03-26 01:28:53 +0000396 } else {
Nate Begeman74d73452005-03-31 00:15:26 +0000397 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
Chris Lattner022ed322005-05-15 19:54:37 +0000398 Args[i].first, PtrOff,
399 DAG.getSrcValue(NULL)));
Nate Begeman307e7442005-03-26 01:28:53 +0000400 }
401 ArgOffset += 8;
402 break;
403 case MVT::f32:
Nate Begeman307e7442005-03-26 01:28:53 +0000404 case MVT::f64:
Nate Begemanf7e43382005-03-26 07:46:36 +0000405 if (FPR_remaining > 0) {
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000406 args_to_use.push_back(Args[i].first);
407 --FPR_remaining;
Nate Begemanf7e43382005-03-26 07:46:36 +0000408 if (isVarArg) {
Nate Begeman96fc6812005-03-31 02:05:53 +0000409 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Chain,
Chris Lattner022ed322005-05-15 19:54:37 +0000410 Args[i].first, PtrOff,
411 DAG.getSrcValue(NULL));
Nate Begeman96fc6812005-03-31 02:05:53 +0000412 MemOps.push_back(Store);
Nate Begeman74d73452005-03-31 00:15:26 +0000413 // Float varargs are always shadowed in available integer registers
414 if (GPR_remaining > 0) {
Chris Lattner022ed322005-05-15 19:54:37 +0000415 SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff,
416 DAG.getSrcValue(NULL));
Nate Begeman74d73452005-03-31 00:15:26 +0000417 MemOps.push_back(Load);
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000418 args_to_use.push_back(Load);
419 --GPR_remaining;
Nate Begeman74d73452005-03-31 00:15:26 +0000420 }
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000421 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
Nate Begeman74d73452005-03-31 00:15:26 +0000422 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
423 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
Chris Lattner022ed322005-05-15 19:54:37 +0000424 SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff,
425 DAG.getSrcValue(NULL));
Nate Begeman74d73452005-03-31 00:15:26 +0000426 MemOps.push_back(Load);
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000427 args_to_use.push_back(Load);
428 --GPR_remaining;
Nate Begeman74d73452005-03-31 00:15:26 +0000429 }
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000430 } else {
431 // If we have any FPRs remaining, we may also have GPRs remaining.
432 // Args passed in FPRs consume either 1 (f32) or 2 (f64) available
433 // GPRs.
434 if (GPR_remaining > 0) {
435 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
436 --GPR_remaining;
437 }
438 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
439 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
440 --GPR_remaining;
441 }
Nate Begeman74d73452005-03-31 00:15:26 +0000442 }
Nate Begeman307e7442005-03-26 01:28:53 +0000443 } else {
Nate Begeman74d73452005-03-31 00:15:26 +0000444 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
Chris Lattner022ed322005-05-15 19:54:37 +0000445 Args[i].first, PtrOff,
446 DAG.getSrcValue(NULL)));
Nate Begeman307e7442005-03-26 01:28:53 +0000447 }
Nate Begemanf7e43382005-03-26 07:46:36 +0000448 ArgOffset += (ArgVT == MVT::f32) ? 4 : 8;
Nate Begeman307e7442005-03-26 01:28:53 +0000449 break;
450 }
Nate Begemana9795f82005-03-24 04:41:43 +0000451 }
Nate Begeman74d73452005-03-31 00:15:26 +0000452 if (!MemOps.empty())
453 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps);
Nate Begemana9795f82005-03-24 04:41:43 +0000454 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000455
Nate Begemana9795f82005-03-24 04:41:43 +0000456 std::vector<MVT::ValueType> RetVals;
457 MVT::ValueType RetTyVT = getValueType(RetTy);
458 if (RetTyVT != MVT::isVoid)
459 RetVals.push_back(RetTyVT);
460 RetVals.push_back(MVT::Other);
461
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000462 SDOperand TheCall = SDOperand(DAG.getCall(RetVals,
Nate Begemana9795f82005-03-24 04:41:43 +0000463 Chain, Callee, args_to_use), 0);
464 Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
Chris Lattner16cd04d2005-05-12 23:24:06 +0000465 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain,
Nate Begemana9795f82005-03-24 04:41:43 +0000466 DAG.getConstant(NumBytes, getPointerTy()));
467 return std::make_pair(TheCall, Chain);
468}
469
Chris Lattnere0fe2252005-07-05 19:58:54 +0000470SDOperand PPC32TargetLowering::LowerVAStart(SDOperand Chain, SDOperand VAListP,
471 Value *VAListV, SelectionDAG &DAG) {
Chris Lattnerf84a2ac2005-07-05 17:48:31 +0000472 // vastart just stores the address of the VarArgsFrameIndex slot into the
473 // memory location argument.
474 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
Chris Lattnere0fe2252005-07-05 19:58:54 +0000475 return DAG.getNode(ISD::STORE, MVT::Other, Chain, FR, VAListP,
476 DAG.getSrcValue(VAListV));
Nate Begemana9795f82005-03-24 04:41:43 +0000477}
478
Chris Lattnere0fe2252005-07-05 19:58:54 +0000479std::pair<SDOperand,SDOperand>
480PPC32TargetLowering::LowerVAArg(SDOperand Chain,
481 SDOperand VAListP, Value *VAListV,
482 const Type *ArgTy, SelectionDAG &DAG) {
Nate Begemanc7b09f12005-03-25 08:34:25 +0000483 MVT::ValueType ArgVT = getValueType(ArgTy);
Chris Lattnerf84a2ac2005-07-05 17:48:31 +0000484
485 SDOperand VAList =
Chris Lattnere0fe2252005-07-05 19:58:54 +0000486 DAG.getLoad(MVT::i32, Chain, VAListP, DAG.getSrcValue(VAListV));
487 SDOperand Result = DAG.getLoad(ArgVT, Chain, VAList, DAG.getSrcValue(NULL));
Chris Lattnerf84a2ac2005-07-05 17:48:31 +0000488 unsigned Amt;
489 if (ArgVT == MVT::i32 || ArgVT == MVT::f32)
490 Amt = 4;
491 else {
492 assert((ArgVT == MVT::i64 || ArgVT == MVT::f64) &&
493 "Other types should have been promoted for varargs!");
494 Amt = 8;
Nate Begemanc7b09f12005-03-25 08:34:25 +0000495 }
Chris Lattnerf84a2ac2005-07-05 17:48:31 +0000496 VAList = DAG.getNode(ISD::ADD, VAList.getValueType(), VAList,
497 DAG.getConstant(Amt, VAList.getValueType()));
498 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain,
Chris Lattnere0fe2252005-07-05 19:58:54 +0000499 VAList, VAListP, DAG.getSrcValue(VAListV));
Nate Begemanc7b09f12005-03-25 08:34:25 +0000500 return std::make_pair(Result, Chain);
Nate Begemana9795f82005-03-24 04:41:43 +0000501}
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000502
Nate Begemana9795f82005-03-24 04:41:43 +0000503
504std::pair<SDOperand, SDOperand> PPC32TargetLowering::
505LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
506 SelectionDAG &DAG) {
Nate Begeman01d05262005-03-30 01:45:43 +0000507 assert(0 && "LowerFrameReturnAddress unimplemented");
Nate Begemana9795f82005-03-24 04:41:43 +0000508 abort();
509}
510
511namespace {
Nate Begemanc7bd4822005-04-11 06:34:10 +0000512Statistic<>Recorded("ppc-codegen", "Number of recording ops emitted");
Nate Begeman93075ec2005-04-04 23:40:36 +0000513Statistic<>FusedFP("ppc-codegen", "Number of fused fp operations");
Nate Begeman2a05c8e2005-07-28 03:02:05 +0000514Statistic<>FrameOff("ppc-codegen", "Number of frame idx offsets collapsed");
Nate Begemana9795f82005-03-24 04:41:43 +0000515//===--------------------------------------------------------------------===//
516/// ISel - PPC32 specific code to select PPC32 machine instructions for
517/// SelectionDAG operations.
518//===--------------------------------------------------------------------===//
519class ISel : public SelectionDAGISel {
Nate Begemana9795f82005-03-24 04:41:43 +0000520 PPC32TargetLowering PPC32Lowering;
Nate Begeman815d6da2005-04-06 00:25:27 +0000521 SelectionDAG *ISelDAG; // Hack to support us having a dag->dag transform
522 // for sdiv and udiv until it is put into the future
523 // dag combiner.
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000524
Nate Begemana9795f82005-03-24 04:41:43 +0000525 /// ExprMap - As shared expressions are codegen'd, we keep track of which
526 /// vreg the value is produced in, so we only emit one copy of each compiled
527 /// tree.
528 std::map<SDOperand, unsigned> ExprMap;
Nate Begemanc7b09f12005-03-25 08:34:25 +0000529
530 unsigned GlobalBaseReg;
531 bool GlobalBaseInitialized;
Nate Begemanc7bd4822005-04-11 06:34:10 +0000532 bool RecordSuccess;
Nate Begemana9795f82005-03-24 04:41:43 +0000533public:
Nate Begeman815d6da2005-04-06 00:25:27 +0000534 ISel(TargetMachine &TM) : SelectionDAGISel(PPC32Lowering), PPC32Lowering(TM),
535 ISelDAG(0) {}
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000536
Nate Begemanc7b09f12005-03-25 08:34:25 +0000537 /// runOnFunction - Override this function in order to reset our per-function
538 /// variables.
539 virtual bool runOnFunction(Function &Fn) {
540 // Make sure we re-emit a set of the global base reg if necessary
541 GlobalBaseInitialized = false;
542 return SelectionDAGISel::runOnFunction(Fn);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000543 }
544
Nate Begemana9795f82005-03-24 04:41:43 +0000545 /// InstructionSelectBasicBlock - This callback is invoked by
546 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
547 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG) {
548 DEBUG(BB->dump());
549 // Codegen the basic block.
Nate Begeman815d6da2005-04-06 00:25:27 +0000550 ISelDAG = &DAG;
Nate Begemana9795f82005-03-24 04:41:43 +0000551 Select(DAG.getRoot());
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000552
Nate Begemana9795f82005-03-24 04:41:43 +0000553 // Clear state used for selection.
554 ExprMap.clear();
Nate Begeman815d6da2005-04-06 00:25:27 +0000555 ISelDAG = 0;
Nate Begemana9795f82005-03-24 04:41:43 +0000556 }
Nate Begeman815d6da2005-04-06 00:25:27 +0000557
558 // dag -> dag expanders for integer divide by constant
559 SDOperand BuildSDIVSequence(SDOperand N);
560 SDOperand BuildUDIVSequence(SDOperand N);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000561
Nate Begemandffcfcc2005-04-01 00:32:34 +0000562 unsigned getGlobalBaseReg();
Nate Begeman6b559972005-04-01 02:59:27 +0000563 unsigned getConstDouble(double floatVal, unsigned Result);
Nate Begeman1cbf3ab2005-04-18 07:48:09 +0000564 void MoveCRtoGPR(unsigned CCReg, bool Inv, unsigned Idx, unsigned Result);
Nate Begeman7ddecb42005-04-06 23:51:40 +0000565 bool SelectBitfieldInsert(SDOperand OR, unsigned Result);
Nate Begeman3664cef2005-04-13 22:14:14 +0000566 unsigned FoldIfWideZeroExtend(SDOperand N);
Nate Begeman1cbf3ab2005-04-18 07:48:09 +0000567 unsigned SelectCC(SDOperand CC, unsigned &Opc, bool &Inv, unsigned &Idx);
568 unsigned SelectCCExpr(SDOperand N, unsigned& Opc, bool &Inv, unsigned &Idx);
Nate Begemanc7bd4822005-04-11 06:34:10 +0000569 unsigned SelectExpr(SDOperand N, bool Recording=false);
Nate Begemana9795f82005-03-24 04:41:43 +0000570 void Select(SDOperand N);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000571
Nate Begeman2a05c8e2005-07-28 03:02:05 +0000572 unsigned SelectAddr(SDOperand N, unsigned& Reg, int& offset);
Nate Begemana9795f82005-03-24 04:41:43 +0000573 void SelectBranchCC(SDOperand N);
574};
575
Nate Begeman80196b12005-04-05 00:15:08 +0000576/// ExactLog2 - This function solves for (Val == 1 << (N-1)) and returns N. It
577/// returns zero when the input is not exactly a power of two.
578static unsigned ExactLog2(unsigned Val) {
579 if (Val == 0 || (Val & (Val-1))) return 0;
580 unsigned Count = 0;
581 while (Val != 1) {
582 Val >>= 1;
583 ++Count;
584 }
585 return Count;
586}
587
Nate Begeman7ddecb42005-04-06 23:51:40 +0000588// IsRunOfOnes - returns true if Val consists of one contiguous run of 1's with
589// any number of 0's on either side. the 1's are allowed to wrap from LSB to
590// MSB. so 0x000FFF0, 0x0000FFFF, and 0xFF0000FF are all runs. 0x0F0F0000 is
591// not, since all 1's are not contiguous.
592static bool IsRunOfOnes(unsigned Val, unsigned &MB, unsigned &ME) {
593 bool isRun = true;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000594 MB = 0;
Nate Begeman7ddecb42005-04-06 23:51:40 +0000595 ME = 0;
596
597 // look for first set bit
598 int i = 0;
599 for (; i < 32; i++) {
600 if ((Val & (1 << (31 - i))) != 0) {
601 MB = i;
602 ME = i;
603 break;
604 }
605 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000606
Nate Begeman7ddecb42005-04-06 23:51:40 +0000607 // look for last set bit
608 for (; i < 32; i++) {
609 if ((Val & (1 << (31 - i))) == 0)
610 break;
611 ME = i;
612 }
613
614 // look for next set bit
615 for (; i < 32; i++) {
616 if ((Val & (1 << (31 - i))) != 0)
617 break;
618 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000619
Nate Begeman7ddecb42005-04-06 23:51:40 +0000620 // if we exhausted all the bits, we found a match at this point for 0*1*0*
621 if (i == 32)
622 return true;
623
624 // since we just encountered more 1's, if it doesn't wrap around to the
625 // most significant bit of the word, then we did not find a match to 1*0*1* so
626 // exit.
627 if (MB != 0)
628 return false;
629
630 // look for last set bit
631 for (MB = i; i < 32; i++) {
632 if ((Val & (1 << (31 - i))) == 0)
633 break;
634 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000635
Nate Begeman7ddecb42005-04-06 23:51:40 +0000636 // if we exhausted all the bits, then we found a match for 1*0*1*, otherwise,
637 // the value is not a run of ones.
638 if (i == 32)
639 return true;
640 return false;
641}
642
Nate Begeman439b4442005-04-05 04:22:58 +0000643/// getImmediateForOpcode - This method returns a value indicating whether
Nate Begemana9795f82005-03-24 04:41:43 +0000644/// the ConstantSDNode N can be used as an immediate to Opcode. The return
645/// values are either 0, 1 or 2. 0 indicates that either N is not a
Nate Begeman9f833d32005-04-12 00:10:02 +0000646/// ConstantSDNode, or is not suitable for use by that opcode.
647/// Return value codes for turning into an enum someday:
648/// 1: constant may be used in normal immediate form.
649/// 2: constant may be used in shifted immediate form.
650/// 3: log base 2 of the constant may be used.
651/// 4: constant is suitable for integer division conversion
652/// 5: constant is a bitfield mask
Nate Begemana9795f82005-03-24 04:41:43 +0000653///
Nate Begeman439b4442005-04-05 04:22:58 +0000654static unsigned getImmediateForOpcode(SDOperand N, unsigned Opcode,
655 unsigned& Imm, bool U = false) {
Nate Begemana9795f82005-03-24 04:41:43 +0000656 if (N.getOpcode() != ISD::Constant) return 0;
657
658 int v = (int)cast<ConstantSDNode>(N)->getSignExtended();
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000659
Nate Begemana9795f82005-03-24 04:41:43 +0000660 switch(Opcode) {
661 default: return 0;
662 case ISD::ADD:
663 if (v <= 32767 && v >= -32768) { Imm = v & 0xFFFF; return 1; }
664 if ((v & 0x0000FFFF) == 0) { Imm = v >> 16; return 2; }
665 break;
Nate Begeman9f833d32005-04-12 00:10:02 +0000666 case ISD::AND: {
667 unsigned MB, ME;
668 if (IsRunOfOnes(v, MB, ME)) { Imm = MB << 16 | ME & 0xFFFF; return 5; }
669 if (v >= 0 && v <= 65535) { Imm = v & 0xFFFF; return 1; }
670 if ((v & 0x0000FFFF) == 0) { Imm = v >> 16; return 2; }
671 break;
672 }
Nate Begemana9795f82005-03-24 04:41:43 +0000673 case ISD::XOR:
674 case ISD::OR:
675 if (v >= 0 && v <= 65535) { Imm = v & 0xFFFF; return 1; }
676 if ((v & 0x0000FFFF) == 0) { Imm = v >> 16; return 2; }
677 break;
Nate Begeman307e7442005-03-26 01:28:53 +0000678 case ISD::MUL:
679 if (v <= 32767 && v >= -32768) { Imm = v & 0xFFFF; return 1; }
680 break;
Nate Begemand7c4a4a2005-05-11 23:43:56 +0000681 case ISD::SUB:
682 // handle subtract-from separately from subtract, since subi is really addi
683 if (U && v <= 32767 && v >= -32768) { Imm = v & 0xFFFF; return 1; }
684 if (!U && v <= 32768 && v >= -32767) { Imm = (-v) & 0xFFFF; return 1; }
685 break;
Nate Begeman3e897162005-03-31 23:55:40 +0000686 case ISD::SETCC:
687 if (U && (v >= 0 && v <= 65535)) { Imm = v & 0xFFFF; return 1; }
688 if (!U && (v <= 32767 && v >= -32768)) { Imm = v & 0xFFFF; return 1; }
689 break;
Nate Begeman80196b12005-04-05 00:15:08 +0000690 case ISD::SDIV:
Nate Begeman439b4442005-04-05 04:22:58 +0000691 if ((Imm = ExactLog2(v))) { return 3; }
Nate Begeman9f833d32005-04-12 00:10:02 +0000692 if ((Imm = ExactLog2(-v))) { Imm = -Imm; return 3; }
Nate Begeman815d6da2005-04-06 00:25:27 +0000693 if (v <= -2 || v >= 2) { return 4; }
694 break;
695 case ISD::UDIV:
Nate Begeman27b4c232005-04-06 06:44:57 +0000696 if (v > 1) { return 4; }
Nate Begeman80196b12005-04-05 00:15:08 +0000697 break;
Nate Begemana9795f82005-03-24 04:41:43 +0000698 }
699 return 0;
700}
Nate Begeman3e897162005-03-31 23:55:40 +0000701
Nate Begemanc7bd4822005-04-11 06:34:10 +0000702/// NodeHasRecordingVariant - If SelectExpr can always produce code for
703/// NodeOpcode that also sets CR0 as a side effect, return true. Otherwise,
704/// return false.
705static bool NodeHasRecordingVariant(unsigned NodeOpcode) {
706 switch(NodeOpcode) {
707 default: return false;
708 case ISD::AND:
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000709 case ISD::OR:
Chris Lattner519f40b2005-04-13 02:46:17 +0000710 return true;
Nate Begemanc7bd4822005-04-11 06:34:10 +0000711 }
712}
713
Nate Begeman3e897162005-03-31 23:55:40 +0000714/// getBCCForSetCC - Returns the PowerPC condition branch mnemonic corresponding
715/// to Condition. If the Condition is unordered or unsigned, the bool argument
716/// U is set to true, otherwise it is set to false.
717static unsigned getBCCForSetCC(unsigned Condition, bool& U) {
718 U = false;
719 switch (Condition) {
720 default: assert(0 && "Unknown condition!"); abort();
721 case ISD::SETEQ: return PPC::BEQ;
722 case ISD::SETNE: return PPC::BNE;
723 case ISD::SETULT: U = true;
724 case ISD::SETLT: return PPC::BLT;
725 case ISD::SETULE: U = true;
726 case ISD::SETLE: return PPC::BLE;
727 case ISD::SETUGT: U = true;
728 case ISD::SETGT: return PPC::BGT;
729 case ISD::SETUGE: U = true;
730 case ISD::SETGE: return PPC::BGE;
731 }
Nate Begeman04730362005-04-01 04:45:11 +0000732 return 0;
733}
734
Nate Begeman7bfba7d2005-04-14 09:45:08 +0000735/// getCROpForOp - Return the condition register opcode (or inverted opcode)
736/// associated with the SelectionDAG opcode.
737static unsigned getCROpForSetCC(unsigned Opcode, bool Inv1, bool Inv2) {
738 switch (Opcode) {
739 default: assert(0 && "Unknown opcode!"); abort();
740 case ISD::AND:
741 if (Inv1 && Inv2) return PPC::CRNOR; // De Morgan's Law
742 if (!Inv1 && !Inv2) return PPC::CRAND;
743 if (Inv1 ^ Inv2) return PPC::CRANDC;
744 case ISD::OR:
745 if (Inv1 && Inv2) return PPC::CRNAND; // De Morgan's Law
746 if (!Inv1 && !Inv2) return PPC::CROR;
747 if (Inv1 ^ Inv2) return PPC::CRORC;
748 }
749 return 0;
750}
751
752/// getCRIdxForSetCC - Return the index of the condition register field
753/// associated with the SetCC condition, and whether or not the field is
754/// treated as inverted. That is, lt = 0; ge = 0 inverted.
755static unsigned getCRIdxForSetCC(unsigned Condition, bool& Inv) {
756 switch (Condition) {
757 default: assert(0 && "Unknown condition!"); abort();
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000758 case ISD::SETULT:
Nate Begeman7bfba7d2005-04-14 09:45:08 +0000759 case ISD::SETLT: Inv = false; return 0;
760 case ISD::SETUGE:
761 case ISD::SETGE: Inv = true; return 0;
762 case ISD::SETUGT:
763 case ISD::SETGT: Inv = false; return 1;
764 case ISD::SETULE:
765 case ISD::SETLE: Inv = true; return 1;
766 case ISD::SETEQ: Inv = false; return 2;
767 case ISD::SETNE: Inv = true; return 2;
768 }
769 return 0;
770}
771
Nate Begeman04730362005-04-01 04:45:11 +0000772/// IndexedOpForOp - Return the indexed variant for each of the PowerPC load
773/// and store immediate instructions.
774static unsigned IndexedOpForOp(unsigned Opcode) {
775 switch(Opcode) {
776 default: assert(0 && "Unknown opcode!"); abort();
777 case PPC::LBZ: return PPC::LBZX; case PPC::STB: return PPC::STBX;
778 case PPC::LHZ: return PPC::LHZX; case PPC::STH: return PPC::STHX;
779 case PPC::LHA: return PPC::LHAX; case PPC::STW: return PPC::STWX;
780 case PPC::LWZ: return PPC::LWZX; case PPC::STFS: return PPC::STFSX;
781 case PPC::LFS: return PPC::LFSX; case PPC::STFD: return PPC::STFDX;
782 case PPC::LFD: return PPC::LFDX;
783 }
784 return 0;
Nate Begeman3e897162005-03-31 23:55:40 +0000785}
Nate Begeman815d6da2005-04-06 00:25:27 +0000786
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000787// Structure used to return the necessary information to codegen an SDIV as
Nate Begeman815d6da2005-04-06 00:25:27 +0000788// a multiply.
789struct ms {
790 int m; // magic number
791 int s; // shift amount
792};
793
794struct mu {
795 unsigned int m; // magic number
796 int a; // add indicator
797 int s; // shift amount
798};
799
800/// magic - calculate the magic numbers required to codegen an integer sdiv as
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000801/// a sequence of multiply and shifts. Requires that the divisor not be 0, 1,
Nate Begeman815d6da2005-04-06 00:25:27 +0000802/// or -1.
803static struct ms magic(int d) {
804 int p;
805 unsigned int ad, anc, delta, q1, r1, q2, r2, t;
806 const unsigned int two31 = 2147483648U; // 2^31
807 struct ms mag;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000808
Nate Begeman815d6da2005-04-06 00:25:27 +0000809 ad = abs(d);
810 t = two31 + ((unsigned int)d >> 31);
811 anc = t - 1 - t%ad; // absolute value of nc
812 p = 31; // initialize p
813 q1 = two31/anc; // initialize q1 = 2p/abs(nc)
814 r1 = two31 - q1*anc; // initialize r1 = rem(2p,abs(nc))
815 q2 = two31/ad; // initialize q2 = 2p/abs(d)
816 r2 = two31 - q2*ad; // initialize r2 = rem(2p,abs(d))
817 do {
818 p = p + 1;
819 q1 = 2*q1; // update q1 = 2p/abs(nc)
820 r1 = 2*r1; // update r1 = rem(2p/abs(nc))
821 if (r1 >= anc) { // must be unsigned comparison
822 q1 = q1 + 1;
823 r1 = r1 - anc;
824 }
825 q2 = 2*q2; // update q2 = 2p/abs(d)
826 r2 = 2*r2; // update r2 = rem(2p/abs(d))
827 if (r2 >= ad) { // must be unsigned comparison
828 q2 = q2 + 1;
829 r2 = r2 - ad;
830 }
831 delta = ad - r2;
832 } while (q1 < delta || (q1 == delta && r1 == 0));
833
834 mag.m = q2 + 1;
835 if (d < 0) mag.m = -mag.m; // resulting magic number
836 mag.s = p - 32; // resulting shift
837 return mag;
838}
839
840/// magicu - calculate the magic numbers required to codegen an integer udiv as
841/// a sequence of multiply, add and shifts. Requires that the divisor not be 0.
842static struct mu magicu(unsigned d)
843{
844 int p;
845 unsigned int nc, delta, q1, r1, q2, r2;
846 struct mu magu;
847 magu.a = 0; // initialize "add" indicator
848 nc = - 1 - (-d)%d;
849 p = 31; // initialize p
850 q1 = 0x80000000/nc; // initialize q1 = 2p/nc
851 r1 = 0x80000000 - q1*nc; // initialize r1 = rem(2p,nc)
852 q2 = 0x7FFFFFFF/d; // initialize q2 = (2p-1)/d
853 r2 = 0x7FFFFFFF - q2*d; // initialize r2 = rem((2p-1),d)
854 do {
855 p = p + 1;
856 if (r1 >= nc - r1 ) {
857 q1 = 2*q1 + 1; // update q1
858 r1 = 2*r1 - nc; // update r1
859 }
860 else {
861 q1 = 2*q1; // update q1
862 r1 = 2*r1; // update r1
863 }
864 if (r2 + 1 >= d - r2) {
865 if (q2 >= 0x7FFFFFFF) magu.a = 1;
866 q2 = 2*q2 + 1; // update q2
867 r2 = 2*r2 + 1 - d; // update r2
868 }
869 else {
870 if (q2 >= 0x80000000) magu.a = 1;
871 q2 = 2*q2; // update q2
872 r2 = 2*r2 + 1; // update r2
873 }
874 delta = d - 1 - r2;
875 } while (p < 64 && (q1 < delta || (q1 == delta && r1 == 0)));
876 magu.m = q2 + 1; // resulting magic number
877 magu.s = p - 32; // resulting shift
878 return magu;
879}
880}
881
882/// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
883/// return a DAG expression to select that will generate the same value by
884/// multiplying by a magic number. See:
885/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
886SDOperand ISel::BuildSDIVSequence(SDOperand N) {
887 int d = (int)cast<ConstantSDNode>(N.getOperand(1))->getSignExtended();
888 ms magics = magic(d);
889 // Multiply the numerator (operand 0) by the magic value
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000890 SDOperand Q = ISelDAG->getNode(ISD::MULHS, MVT::i32, N.getOperand(0),
Nate Begeman815d6da2005-04-06 00:25:27 +0000891 ISelDAG->getConstant(magics.m, MVT::i32));
892 // If d > 0 and m < 0, add the numerator
893 if (d > 0 && magics.m < 0)
894 Q = ISelDAG->getNode(ISD::ADD, MVT::i32, Q, N.getOperand(0));
895 // If d < 0 and m > 0, subtract the numerator.
896 if (d < 0 && magics.m > 0)
897 Q = ISelDAG->getNode(ISD::SUB, MVT::i32, Q, N.getOperand(0));
898 // Shift right algebraic if shift value is nonzero
899 if (magics.s > 0)
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000900 Q = ISelDAG->getNode(ISD::SRA, MVT::i32, Q,
Nate Begeman815d6da2005-04-06 00:25:27 +0000901 ISelDAG->getConstant(magics.s, MVT::i32));
902 // Extract the sign bit and add it to the quotient
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000903 SDOperand T =
Nate Begeman815d6da2005-04-06 00:25:27 +0000904 ISelDAG->getNode(ISD::SRL, MVT::i32, Q, ISelDAG->getConstant(31, MVT::i32));
Nate Begeman27b4c232005-04-06 06:44:57 +0000905 return ISelDAG->getNode(ISD::ADD, MVT::i32, Q, T);
Nate Begeman815d6da2005-04-06 00:25:27 +0000906}
907
908/// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
909/// return a DAG expression to select that will generate the same value by
910/// multiplying by a magic number. See:
911/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
912SDOperand ISel::BuildUDIVSequence(SDOperand N) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000913 unsigned d =
Nate Begeman815d6da2005-04-06 00:25:27 +0000914 (unsigned)cast<ConstantSDNode>(N.getOperand(1))->getSignExtended();
915 mu magics = magicu(d);
916 // Multiply the numerator (operand 0) by the magic value
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000917 SDOperand Q = ISelDAG->getNode(ISD::MULHU, MVT::i32, N.getOperand(0),
Nate Begeman815d6da2005-04-06 00:25:27 +0000918 ISelDAG->getConstant(magics.m, MVT::i32));
919 if (magics.a == 0) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000920 Q = ISelDAG->getNode(ISD::SRL, MVT::i32, Q,
Nate Begeman815d6da2005-04-06 00:25:27 +0000921 ISelDAG->getConstant(magics.s, MVT::i32));
922 } else {
923 SDOperand NPQ = ISelDAG->getNode(ISD::SUB, MVT::i32, N.getOperand(0), Q);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000924 NPQ = ISelDAG->getNode(ISD::SRL, MVT::i32, NPQ,
Nate Begeman815d6da2005-04-06 00:25:27 +0000925 ISelDAG->getConstant(1, MVT::i32));
926 NPQ = ISelDAG->getNode(ISD::ADD, MVT::i32, NPQ, Q);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000927 Q = ISelDAG->getNode(ISD::SRL, MVT::i32, NPQ,
Nate Begeman815d6da2005-04-06 00:25:27 +0000928 ISelDAG->getConstant(magics.s-1, MVT::i32));
929 }
Nate Begeman27b4c232005-04-06 06:44:57 +0000930 return Q;
Nate Begemana9795f82005-03-24 04:41:43 +0000931}
932
Nate Begemanc7b09f12005-03-25 08:34:25 +0000933/// getGlobalBaseReg - Output the instructions required to put the
934/// base address to use for accessing globals into a register.
935///
936unsigned ISel::getGlobalBaseReg() {
937 if (!GlobalBaseInitialized) {
938 // Insert the set of GlobalBaseReg into the first MBB of the function
939 MachineBasicBlock &FirstMBB = BB->getParent()->front();
940 MachineBasicBlock::iterator MBBI = FirstMBB.begin();
941 GlobalBaseReg = MakeReg(MVT::i32);
942 BuildMI(FirstMBB, MBBI, PPC::MovePCtoLR, 0, PPC::LR);
943 BuildMI(FirstMBB, MBBI, PPC::MFLR, 1, GlobalBaseReg).addReg(PPC::LR);
944 GlobalBaseInitialized = true;
945 }
946 return GlobalBaseReg;
947}
948
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000949/// getConstDouble - Loads a floating point value into a register, via the
Nate Begeman6b559972005-04-01 02:59:27 +0000950/// Constant Pool. Optionally takes a register in which to load the value.
951unsigned ISel::getConstDouble(double doubleVal, unsigned Result=0) {
952 unsigned Tmp1 = MakeReg(MVT::i32);
953 if (0 == Result) Result = MakeReg(MVT::f64);
954 MachineConstantPool *CP = BB->getParent()->getConstantPool();
955 ConstantFP *CFP = ConstantFP::get(Type::DoubleTy, doubleVal);
956 unsigned CPI = CP->getConstantPoolIndex(CFP);
Nate Begeman2497e632005-07-21 20:44:43 +0000957 if (PICEnabled)
958 BuildMI(BB, PPC::ADDIS, 2, Tmp1).addReg(getGlobalBaseReg())
959 .addConstantPoolIndex(CPI);
960 else
961 BuildMI(BB, PPC::LIS, 1, Tmp1).addConstantPoolIndex(CPI);
Nate Begeman6b559972005-04-01 02:59:27 +0000962 BuildMI(BB, PPC::LFD, 2, Result).addConstantPoolIndex(CPI).addReg(Tmp1);
963 return Result;
964}
965
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000966/// MoveCRtoGPR - Move CCReg[Idx] to the least significant bit of Result. If
Nate Begeman1cbf3ab2005-04-18 07:48:09 +0000967/// Inv is true, then invert the result.
968void ISel::MoveCRtoGPR(unsigned CCReg, bool Inv, unsigned Idx, unsigned Result){
969 unsigned IntCR = MakeReg(MVT::i32);
970 BuildMI(BB, PPC::MCRF, 1, PPC::CR7).addReg(CCReg);
Nate Begeman2497e632005-07-21 20:44:43 +0000971 BuildMI(BB, GPOPT ? PPC::MFOCRF : PPC::MFCR, 1, IntCR).addReg(PPC::CR7);
Nate Begeman1cbf3ab2005-04-18 07:48:09 +0000972 if (Inv) {
973 unsigned Tmp1 = MakeReg(MVT::i32);
974 BuildMI(BB, PPC::RLWINM, 4, Tmp1).addReg(IntCR).addImm(32-(3-Idx))
975 .addImm(31).addImm(31);
976 BuildMI(BB, PPC::XORI, 2, Result).addReg(Tmp1).addImm(1);
977 } else {
978 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(IntCR).addImm(32-(3-Idx))
979 .addImm(31).addImm(31);
980 }
981}
982
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000983/// SelectBitfieldInsert - turn an or of two masked values into
Nate Begeman7ddecb42005-04-06 23:51:40 +0000984/// the rotate left word immediate then mask insert (rlwimi) instruction.
985/// Returns true on success, false if the caller still needs to select OR.
986///
987/// Patterns matched:
988/// 1. or shl, and 5. or and, and
989/// 2. or and, shl 6. or shl, shr
990/// 3. or shr, and 7. or shr, shl
991/// 4. or and, shr
992bool ISel::SelectBitfieldInsert(SDOperand OR, unsigned Result) {
Nate Begemancd08e4c2005-04-09 20:09:12 +0000993 bool IsRotate = false;
Nate Begeman7ddecb42005-04-06 23:51:40 +0000994 unsigned TgtMask = 0xFFFFFFFF, InsMask = 0xFFFFFFFF, Amount = 0;
Jeff Cohen00b168892005-07-27 06:12:32 +0000995
Nate Begemanb2c4bf32005-06-08 04:14:27 +0000996 SDOperand Op0 = OR.getOperand(0);
997 SDOperand Op1 = OR.getOperand(1);
998
999 unsigned Op0Opc = Op0.getOpcode();
1000 unsigned Op1Opc = Op1.getOpcode();
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001001
Nate Begeman7ddecb42005-04-06 23:51:40 +00001002 // Verify that we have the correct opcodes
1003 if (ISD::SHL != Op0Opc && ISD::SRL != Op0Opc && ISD::AND != Op0Opc)
1004 return false;
1005 if (ISD::SHL != Op1Opc && ISD::SRL != Op1Opc && ISD::AND != Op1Opc)
1006 return false;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001007
Nate Begeman7ddecb42005-04-06 23:51:40 +00001008 // Generate Mask value for Target
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001009 if (ConstantSDNode *CN =
Nate Begemanb2c4bf32005-06-08 04:14:27 +00001010 dyn_cast<ConstantSDNode>(Op0.getOperand(1).Val)) {
Nate Begeman7ddecb42005-04-06 23:51:40 +00001011 switch(Op0Opc) {
1012 case ISD::SHL: TgtMask <<= (unsigned)CN->getValue(); break;
1013 case ISD::SRL: TgtMask >>= (unsigned)CN->getValue(); break;
1014 case ISD::AND: TgtMask &= (unsigned)CN->getValue(); break;
1015 }
1016 } else {
1017 return false;
1018 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001019
Nate Begeman7ddecb42005-04-06 23:51:40 +00001020 // Generate Mask value for Insert
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001021 if (ConstantSDNode *CN =
Nate Begemanb2c4bf32005-06-08 04:14:27 +00001022 dyn_cast<ConstantSDNode>(Op1.getOperand(1).Val)) {
Nate Begeman7ddecb42005-04-06 23:51:40 +00001023 switch(Op1Opc) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001024 case ISD::SHL:
1025 Amount = CN->getValue();
Nate Begemancd08e4c2005-04-09 20:09:12 +00001026 InsMask <<= Amount;
1027 if (Op0Opc == ISD::SRL) IsRotate = true;
Nate Begeman7ddecb42005-04-06 23:51:40 +00001028 break;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001029 case ISD::SRL:
1030 Amount = CN->getValue();
1031 InsMask >>= Amount;
Nate Begeman7ddecb42005-04-06 23:51:40 +00001032 Amount = 32-Amount;
Nate Begemancd08e4c2005-04-09 20:09:12 +00001033 if (Op0Opc == ISD::SHL) IsRotate = true;
Nate Begeman7ddecb42005-04-06 23:51:40 +00001034 break;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001035 case ISD::AND:
Nate Begeman7ddecb42005-04-06 23:51:40 +00001036 InsMask &= (unsigned)CN->getValue();
1037 break;
1038 }
1039 } else {
1040 return false;
1041 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001042
Nate Begemanb2c4bf32005-06-08 04:14:27 +00001043 unsigned Tmp3 = 0;
1044
1045 // If both of the inputs are ANDs and one of them has a logical shift by
1046 // constant as its input, make that the inserted value so that we can combine
1047 // the shift into the rotate part of the rlwimi instruction
1048 if (Op0Opc == ISD::AND && Op1Opc == ISD::AND) {
Jeff Cohen00b168892005-07-27 06:12:32 +00001049 if (Op1.getOperand(0).getOpcode() == ISD::SHL ||
Nate Begemanb2c4bf32005-06-08 04:14:27 +00001050 Op1.getOperand(0).getOpcode() == ISD::SRL) {
Jeff Cohen00b168892005-07-27 06:12:32 +00001051 if (ConstantSDNode *CN =
Nate Begemanb2c4bf32005-06-08 04:14:27 +00001052 dyn_cast<ConstantSDNode>(Op1.getOperand(0).getOperand(1).Val)) {
Jeff Cohen00b168892005-07-27 06:12:32 +00001053 Amount = Op1.getOperand(0).getOpcode() == ISD::SHL ?
Nate Begemanb2c4bf32005-06-08 04:14:27 +00001054 CN->getValue() : 32 - CN->getValue();
1055 Tmp3 = SelectExpr(Op1.getOperand(0).getOperand(0));
1056 }
1057 } else if (Op0.getOperand(0).getOpcode() == ISD::SHL ||
1058 Op0.getOperand(0).getOpcode() == ISD::SRL) {
Jeff Cohen00b168892005-07-27 06:12:32 +00001059 if (ConstantSDNode *CN =
Nate Begemanb2c4bf32005-06-08 04:14:27 +00001060 dyn_cast<ConstantSDNode>(Op0.getOperand(0).getOperand(1).Val)) {
1061 std::swap(Op0, Op1);
1062 std::swap(TgtMask, InsMask);
Jeff Cohen00b168892005-07-27 06:12:32 +00001063 Amount = Op1.getOperand(0).getOpcode() == ISD::SHL ?
Nate Begemanb2c4bf32005-06-08 04:14:27 +00001064 CN->getValue() : 32 - CN->getValue();
1065 Tmp3 = SelectExpr(Op1.getOperand(0).getOperand(0));
1066 }
1067 }
1068 }
1069
Nate Begeman7ddecb42005-04-06 23:51:40 +00001070 // Verify that the Target mask and Insert mask together form a full word mask
1071 // and that the Insert mask is a run of set bits (which implies both are runs
1072 // of set bits). Given that, Select the arguments and generate the rlwimi
1073 // instruction.
1074 unsigned MB, ME;
Nate Begemanb2c4bf32005-06-08 04:14:27 +00001075 if (((TgtMask & InsMask) == 0) && IsRunOfOnes(InsMask, MB, ME)) {
Nate Begeman7ddecb42005-04-06 23:51:40 +00001076 unsigned Tmp1, Tmp2;
Nate Begemanb2c4bf32005-06-08 04:14:27 +00001077 bool fullMask = (TgtMask ^ InsMask) == 0xFFFFFFFF;
Nate Begemancd08e4c2005-04-09 20:09:12 +00001078 // Check for rotlwi / rotrwi here, a special case of bitfield insert
1079 // where both bitfield halves are sourced from the same value.
Nate Begemanb2c4bf32005-06-08 04:14:27 +00001080 if (IsRotate && fullMask &&
Nate Begemancd08e4c2005-04-09 20:09:12 +00001081 OR.getOperand(0).getOperand(0) == OR.getOperand(1).getOperand(0)) {
Nate Begemancd08e4c2005-04-09 20:09:12 +00001082 Tmp1 = SelectExpr(OR.getOperand(0).getOperand(0));
1083 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(Amount)
1084 .addImm(0).addImm(31);
1085 return true;
1086 }
Nate Begemanb2c4bf32005-06-08 04:14:27 +00001087 if (Op0Opc == ISD::AND && fullMask)
1088 Tmp1 = SelectExpr(Op0.getOperand(0));
Nate Begeman7ddecb42005-04-06 23:51:40 +00001089 else
Nate Begemanb2c4bf32005-06-08 04:14:27 +00001090 Tmp1 = SelectExpr(Op0);
1091 Tmp2 = Tmp3 ? Tmp3 : SelectExpr(Op1.getOperand(0));
Nate Begeman7ddecb42005-04-06 23:51:40 +00001092 BuildMI(BB, PPC::RLWIMI, 5, Result).addReg(Tmp1).addReg(Tmp2)
1093 .addImm(Amount).addImm(MB).addImm(ME);
1094 return true;
1095 }
1096 return false;
1097}
1098
Nate Begeman3664cef2005-04-13 22:14:14 +00001099/// FoldIfWideZeroExtend - 32 bit PowerPC implicit masks shift amounts to the
1100/// low six bits. If the shift amount is an ISD::AND node with a mask that is
1101/// wider than the implicit mask, then we can get rid of the AND and let the
1102/// shift do the mask.
1103unsigned ISel::FoldIfWideZeroExtend(SDOperand N) {
1104 unsigned C;
1105 if (N.getOpcode() == ISD::AND &&
1106 5 == getImmediateForOpcode(N.getOperand(1), ISD::AND, C) && // isMask
1107 31 == (C & 0xFFFF) && // ME
1108 26 >= (C >> 16)) // MB
1109 return SelectExpr(N.getOperand(0));
1110 else
1111 return SelectExpr(N);
1112}
1113
Nate Begeman1cbf3ab2005-04-18 07:48:09 +00001114unsigned ISel::SelectCC(SDOperand CC, unsigned& Opc, bool &Inv, unsigned& Idx) {
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001115 unsigned Result, Tmp1, Tmp2;
Nate Begeman9765c252005-04-12 21:22:28 +00001116 bool AlreadySelected = false;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001117 static const unsigned CompareOpcodes[] =
Nate Begemandffcfcc2005-04-01 00:32:34 +00001118 { PPC::FCMPU, PPC::FCMPU, PPC::CMPW, PPC::CMPLW };
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001119
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001120 // Allocate a condition register for this expression
1121 Result = RegMap->createVirtualRegister(PPC32::CRRCRegisterClass);
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001122
Nate Begemandffcfcc2005-04-01 00:32:34 +00001123 // If the first operand to the select is a SETCC node, then we can fold it
1124 // into the branch that selects which value to return.
Nate Begeman16ac7092005-04-18 02:43:24 +00001125 if (SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(CC.Val)) {
Nate Begemandffcfcc2005-04-01 00:32:34 +00001126 bool U;
1127 Opc = getBCCForSetCC(SetCC->getCondition(), U);
Nate Begeman1cbf3ab2005-04-18 07:48:09 +00001128 Idx = getCRIdxForSetCC(SetCC->getCondition(), Inv);
Nate Begemandffcfcc2005-04-01 00:32:34 +00001129
Nate Begeman439b4442005-04-05 04:22:58 +00001130 // Pass the optional argument U to getImmediateForOpcode for SETCC,
Nate Begemandffcfcc2005-04-01 00:32:34 +00001131 // so that it knows whether the SETCC immediate range is signed or not.
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001132 if (1 == getImmediateForOpcode(SetCC->getOperand(1), ISD::SETCC,
Nate Begeman439b4442005-04-05 04:22:58 +00001133 Tmp2, U)) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001134 // For comparisons against zero, we can implicity set CR0 if a recording
Nate Begemanc7bd4822005-04-11 06:34:10 +00001135 // variant (e.g. 'or.' instead of 'or') of the instruction that defines
1136 // operand zero of the SetCC node is available.
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001137 if (0 == Tmp2 &&
Nate Begeman9765c252005-04-12 21:22:28 +00001138 NodeHasRecordingVariant(SetCC->getOperand(0).getOpcode()) &&
1139 SetCC->getOperand(0).Val->hasOneUse()) {
Nate Begemanc7bd4822005-04-11 06:34:10 +00001140 RecordSuccess = false;
1141 Tmp1 = SelectExpr(SetCC->getOperand(0), true);
1142 if (RecordSuccess) {
1143 ++Recorded;
Nate Begeman7bfba7d2005-04-14 09:45:08 +00001144 BuildMI(BB, PPC::MCRF, 1, Result).addReg(PPC::CR0);
1145 return Result;
Nate Begemanc7bd4822005-04-11 06:34:10 +00001146 }
1147 AlreadySelected = true;
1148 }
1149 // If we could not implicitly set CR0, then emit a compare immediate
1150 // instead.
1151 if (!AlreadySelected) Tmp1 = SelectExpr(SetCC->getOperand(0));
Nate Begemandffcfcc2005-04-01 00:32:34 +00001152 if (U)
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001153 BuildMI(BB, PPC::CMPLWI, 2, Result).addReg(Tmp1).addImm(Tmp2);
Nate Begemandffcfcc2005-04-01 00:32:34 +00001154 else
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001155 BuildMI(BB, PPC::CMPWI, 2, Result).addReg(Tmp1).addSImm(Tmp2);
Nate Begemandffcfcc2005-04-01 00:32:34 +00001156 } else {
1157 bool IsInteger = MVT::isInteger(SetCC->getOperand(0).getValueType());
1158 unsigned CompareOpc = CompareOpcodes[2 * IsInteger + U];
Nate Begemanc7bd4822005-04-11 06:34:10 +00001159 Tmp1 = SelectExpr(SetCC->getOperand(0));
Nate Begemandffcfcc2005-04-01 00:32:34 +00001160 Tmp2 = SelectExpr(SetCC->getOperand(1));
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001161 BuildMI(BB, CompareOpc, 2, Result).addReg(Tmp1).addReg(Tmp2);
Nate Begemandffcfcc2005-04-01 00:32:34 +00001162 }
1163 } else {
Nate Begeman1cbf3ab2005-04-18 07:48:09 +00001164 // If this isn't a SetCC, then select the value and compare it against zero,
1165 // treating it as if it were a boolean.
Nate Begeman9765c252005-04-12 21:22:28 +00001166 Opc = PPC::BNE;
Nate Begeman1cbf3ab2005-04-18 07:48:09 +00001167 Idx = getCRIdxForSetCC(ISD::SETNE, Inv);
Nate Begemandffcfcc2005-04-01 00:32:34 +00001168 Tmp1 = SelectExpr(CC);
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001169 BuildMI(BB, PPC::CMPLWI, 2, Result).addReg(Tmp1).addImm(0);
Nate Begemandffcfcc2005-04-01 00:32:34 +00001170 }
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001171 return Result;
Nate Begemandffcfcc2005-04-01 00:32:34 +00001172}
1173
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001174unsigned ISel::SelectCCExpr(SDOperand N, unsigned& Opc, bool &Inv,
Nate Begeman1cbf3ab2005-04-18 07:48:09 +00001175 unsigned &Idx) {
1176 bool Inv0, Inv1;
1177 unsigned Idx0, Idx1, CROpc, Opc1, Tmp1, Tmp2;
1178
1179 // Allocate a condition register for this expression
1180 unsigned Result = RegMap->createVirtualRegister(PPC32::CRRCRegisterClass);
1181
1182 // Check for the operations we support:
1183 switch(N.getOpcode()) {
1184 default:
1185 Opc = PPC::BNE;
1186 Idx = getCRIdxForSetCC(ISD::SETNE, Inv);
1187 Tmp1 = SelectExpr(N);
1188 BuildMI(BB, PPC::CMPLWI, 2, Result).addReg(Tmp1).addImm(0);
1189 break;
1190 case ISD::OR:
1191 case ISD::AND:
Nate Begeman1cbf3ab2005-04-18 07:48:09 +00001192 Tmp1 = SelectCCExpr(N.getOperand(0), Opc, Inv0, Idx0);
1193 Tmp2 = SelectCCExpr(N.getOperand(1), Opc1, Inv1, Idx1);
1194 CROpc = getCROpForSetCC(N.getOpcode(), Inv0, Inv1);
1195 if (Inv0 && !Inv1) {
1196 std::swap(Tmp1, Tmp2);
1197 std::swap(Idx0, Idx1);
1198 Opc = Opc1;
1199 }
1200 if (Inv0 && Inv1) Opc = PPC32InstrInfo::invertPPCBranchOpcode(Opc);
1201 BuildMI(BB, CROpc, 5, Result).addImm(Idx0).addReg(Tmp1).addImm(Idx0)
1202 .addReg(Tmp2).addImm(Idx1);
1203 Inv = false;
1204 Idx = Idx0;
1205 break;
1206 case ISD::SETCC:
1207 Tmp1 = SelectCC(N, Opc, Inv, Idx);
1208 Result = Tmp1;
1209 break;
1210 }
1211 return Result;
1212}
1213
Nate Begemandffcfcc2005-04-01 00:32:34 +00001214/// Check to see if the load is a constant offset from a base register
Nate Begeman2a05c8e2005-07-28 03:02:05 +00001215unsigned ISel::SelectAddr(SDOperand N, unsigned& Reg, int& offset)
Nate Begemana9795f82005-03-24 04:41:43 +00001216{
Nate Begeman96fc6812005-03-31 02:05:53 +00001217 unsigned imm = 0, opcode = N.getOpcode();
Nate Begeman04730362005-04-01 04:45:11 +00001218 if (N.getOpcode() == ISD::ADD) {
Nate Begeman2a05c8e2005-07-28 03:02:05 +00001219 bool isFrame = N.getOperand(0).getOpcode() == ISD::FrameIndex;
Nate Begeman439b4442005-04-05 04:22:58 +00001220 if (1 == getImmediateForOpcode(N.getOperand(1), opcode, imm)) {
Nate Begeman96fc6812005-03-31 02:05:53 +00001221 offset = imm;
Nate Begeman2a05c8e2005-07-28 03:02:05 +00001222 if (isFrame) {
1223 ++FrameOff;
1224 Reg = cast<FrameIndexSDNode>(N.getOperand(0))->getIndex();
1225 return 1;
1226 } else {
1227 Reg = SelectExpr(N.getOperand(0));
1228 return 0;
1229 }
1230 } else {
1231 Reg = SelectExpr(N.getOperand(0));
1232 offset = SelectExpr(N.getOperand(1));
1233 return 2;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001234 }
Nate Begeman04730362005-04-01 04:45:11 +00001235 }
Nate Begemana9795f82005-03-24 04:41:43 +00001236 Reg = SelectExpr(N);
1237 offset = 0;
Nate Begeman2a05c8e2005-07-28 03:02:05 +00001238 return 0;
Nate Begemana9795f82005-03-24 04:41:43 +00001239}
1240
1241void ISel::SelectBranchCC(SDOperand N)
1242{
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001243 MachineBasicBlock *Dest =
Nate Begemana9795f82005-03-24 04:41:43 +00001244 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
Nate Begeman3e897162005-03-31 23:55:40 +00001245
Nate Begeman1cbf3ab2005-04-18 07:48:09 +00001246 bool Inv;
1247 unsigned Opc, CCReg, Idx;
Nate Begemana9795f82005-03-24 04:41:43 +00001248 Select(N.getOperand(0)); //chain
Nate Begeman1cbf3ab2005-04-18 07:48:09 +00001249 CCReg = SelectCC(N.getOperand(1), Opc, Inv, Idx);
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001250
Nate Begeman439009c2005-06-15 18:22:43 +00001251 // Iterate to the next basic block
1252 ilist<MachineBasicBlock>::iterator It = BB;
1253 ++It;
Nate Begemancd08e4c2005-04-09 20:09:12 +00001254
1255 // If this is a two way branch, then grab the fallthrough basic block argument
1256 // and build a PowerPC branch pseudo-op, suitable for long branch conversion
1257 // if necessary by the branch selection pass. Otherwise, emit a standard
1258 // conditional branch.
1259 if (N.getOpcode() == ISD::BRCONDTWOWAY) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001260 MachineBasicBlock *Fallthrough =
Nate Begemancd08e4c2005-04-09 20:09:12 +00001261 cast<BasicBlockSDNode>(N.getOperand(3))->getBasicBlock();
1262 if (Dest != It) {
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001263 BuildMI(BB, PPC::COND_BRANCH, 4).addReg(CCReg).addImm(Opc)
Nate Begemancd08e4c2005-04-09 20:09:12 +00001264 .addMBB(Dest).addMBB(Fallthrough);
1265 if (Fallthrough != It)
1266 BuildMI(BB, PPC::B, 1).addMBB(Fallthrough);
1267 } else {
1268 if (Fallthrough != It) {
1269 Opc = PPC32InstrInfo::invertPPCBranchOpcode(Opc);
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001270 BuildMI(BB, PPC::COND_BRANCH, 4).addReg(CCReg).addImm(Opc)
Nate Begemancd08e4c2005-04-09 20:09:12 +00001271 .addMBB(Fallthrough).addMBB(Dest);
1272 }
1273 }
1274 } else {
Nate Begeman439009c2005-06-15 18:22:43 +00001275 // If the fallthrough path is off the end of the function, which would be
1276 // undefined behavior, set it to be the same as the current block because
1277 // we have nothing better to set it to, and leaving it alone will cause the
1278 // PowerPC Branch Selection pass to crash.
1279 if (It == BB->getParent()->end()) It = Dest;
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001280 BuildMI(BB, PPC::COND_BRANCH, 4).addReg(CCReg).addImm(Opc)
Nate Begeman27499e32005-04-10 01:48:29 +00001281 .addMBB(Dest).addMBB(It);
Nate Begemancd08e4c2005-04-09 20:09:12 +00001282 }
Nate Begemana9795f82005-03-24 04:41:43 +00001283 return;
1284}
1285
Nate Begemanc7bd4822005-04-11 06:34:10 +00001286unsigned ISel::SelectExpr(SDOperand N, bool Recording) {
Nate Begemana9795f82005-03-24 04:41:43 +00001287 unsigned Result;
1288 unsigned Tmp1, Tmp2, Tmp3;
1289 unsigned Opc = 0;
1290 unsigned opcode = N.getOpcode();
1291
1292 SDNode *Node = N.Val;
1293 MVT::ValueType DestType = N.getValueType();
1294
Nate Begemana43b1762005-06-14 03:55:23 +00001295 if (Node->getOpcode() == ISD::CopyFromReg &&
1296 MRegisterInfo::isVirtualRegister(cast<RegSDNode>(Node)->getReg()))
1297 // Just use the specified register as our input.
1298 return cast<RegSDNode>(Node)->getReg();
1299
Nate Begemana9795f82005-03-24 04:41:43 +00001300 unsigned &Reg = ExprMap[N];
1301 if (Reg) return Reg;
1302
Nate Begeman27eeb002005-04-02 05:59:34 +00001303 switch (N.getOpcode()) {
1304 default:
Nate Begemana9795f82005-03-24 04:41:43 +00001305 Reg = Result = (N.getValueType() != MVT::Other) ?
Nate Begeman27eeb002005-04-02 05:59:34 +00001306 MakeReg(N.getValueType()) : 1;
1307 break;
Chris Lattnerb5d8e6e2005-05-13 20:29:26 +00001308 case ISD::TAILCALL:
Nate Begeman27eeb002005-04-02 05:59:34 +00001309 case ISD::CALL:
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001310 // If this is a call instruction, make sure to prepare ALL of the result
1311 // values as well as the chain.
Nate Begeman27eeb002005-04-02 05:59:34 +00001312 if (Node->getNumValues() == 1)
1313 Reg = Result = 1; // Void call, just a chain.
1314 else {
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001315 Result = MakeReg(Node->getValueType(0));
1316 ExprMap[N.getValue(0)] = Result;
Nate Begeman27eeb002005-04-02 05:59:34 +00001317 for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001318 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
Nate Begeman27eeb002005-04-02 05:59:34 +00001319 ExprMap[SDOperand(Node, Node->getNumValues()-1)] = 1;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001320 }
Nate Begeman27eeb002005-04-02 05:59:34 +00001321 break;
1322 case ISD::ADD_PARTS:
1323 case ISD::SUB_PARTS:
1324 case ISD::SHL_PARTS:
1325 case ISD::SRL_PARTS:
1326 case ISD::SRA_PARTS:
1327 Result = MakeReg(Node->getValueType(0));
1328 ExprMap[N.getValue(0)] = Result;
1329 for (unsigned i = 1, e = N.Val->getNumValues(); i != e; ++i)
1330 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
1331 break;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001332 }
1333
Nate Begemana9795f82005-03-24 04:41:43 +00001334 switch (opcode) {
1335 default:
1336 Node->dump();
1337 assert(0 && "Node not handled!\n");
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001338 case ISD::UNDEF:
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001339 BuildMI(BB, PPC::IMPLICIT_DEF, 0, Result);
1340 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001341 case ISD::DYNAMIC_STACKALLOC:
Nate Begeman5e966612005-03-24 06:28:42 +00001342 // Generate both result values. FIXME: Need a better commment here?
1343 if (Result != 1)
1344 ExprMap[N.getValue(1)] = 1;
1345 else
1346 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1347
1348 // FIXME: We are currently ignoring the requested alignment for handling
1349 // greater than the stack alignment. This will need to be revisited at some
1350 // point. Align = N.getOperand(2);
1351 if (!isa<ConstantSDNode>(N.getOperand(2)) ||
1352 cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) {
1353 std::cerr << "Cannot allocate stack object with greater alignment than"
1354 << " the stack alignment yet!";
1355 abort();
1356 }
1357 Select(N.getOperand(0));
1358 Tmp1 = SelectExpr(N.getOperand(1));
1359 // Subtract size from stack pointer, thereby allocating some space.
1360 BuildMI(BB, PPC::SUBF, 2, PPC::R1).addReg(Tmp1).addReg(PPC::R1);
1361 // Put a pointer to the space into the result register by copying the SP
1362 BuildMI(BB, PPC::OR, 2, Result).addReg(PPC::R1).addReg(PPC::R1);
1363 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001364
1365 case ISD::ConstantPool:
Nate Begemanca12a2b2005-03-28 22:28:37 +00001366 Tmp1 = cast<ConstantPoolSDNode>(N)->getIndex();
1367 Tmp2 = MakeReg(MVT::i32);
Nate Begeman2497e632005-07-21 20:44:43 +00001368 if (PICEnabled)
1369 BuildMI(BB, PPC::ADDIS, 2, Tmp2).addReg(getGlobalBaseReg())
1370 .addConstantPoolIndex(Tmp1);
1371 else
1372 BuildMI(BB, PPC::LIS, 1, Tmp2).addConstantPoolIndex(Tmp1);
Nate Begemanca12a2b2005-03-28 22:28:37 +00001373 BuildMI(BB, PPC::LA, 2, Result).addReg(Tmp2).addConstantPoolIndex(Tmp1);
1374 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001375
1376 case ISD::FrameIndex:
Nate Begemanf3d08f32005-03-29 00:03:27 +00001377 Tmp1 = cast<FrameIndexSDNode>(N)->getIndex();
Nate Begeman58f718c2005-03-30 02:23:08 +00001378 addFrameReference(BuildMI(BB, PPC::ADDI, 2, Result), (int)Tmp1, 0, false);
Nate Begemanf3d08f32005-03-29 00:03:27 +00001379 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001380
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001381 case ISD::GlobalAddress: {
1382 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
Nate Begemanca12a2b2005-03-28 22:28:37 +00001383 Tmp1 = MakeReg(MVT::i32);
Nate Begeman2497e632005-07-21 20:44:43 +00001384 if (PICEnabled)
1385 BuildMI(BB, PPC::ADDIS, 2, Tmp1).addReg(getGlobalBaseReg())
1386 .addGlobalAddress(GV);
1387 else
1388 BuildMI(BB, PPC::LIS, 2, Tmp1).addGlobalAddress(GV);
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001389 if (GV->hasWeakLinkage() || GV->isExternal()) {
1390 BuildMI(BB, PPC::LWZ, 2, Result).addGlobalAddress(GV).addReg(Tmp1);
1391 } else {
1392 BuildMI(BB, PPC::LA, 2, Result).addReg(Tmp1).addGlobalAddress(GV);
1393 }
1394 return Result;
1395 }
1396
Nate Begeman5e966612005-03-24 06:28:42 +00001397 case ISD::LOAD:
Nate Begemana9795f82005-03-24 04:41:43 +00001398 case ISD::EXTLOAD:
1399 case ISD::ZEXTLOAD:
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001400 case ISD::SEXTLOAD: {
Nate Begeman9db505c2005-03-28 19:36:43 +00001401 MVT::ValueType TypeBeingLoaded = (ISD::LOAD == opcode) ?
Chris Lattnerbce81ae2005-07-10 01:56:13 +00001402 Node->getValueType(0) : cast<VTSDNode>(Node->getOperand(3))->getVT();
Nate Begeman74d73452005-03-31 00:15:26 +00001403 bool sext = (ISD::SEXTLOAD == opcode);
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001404
Nate Begeman5e966612005-03-24 06:28:42 +00001405 // Make sure we generate both values.
1406 if (Result != 1)
1407 ExprMap[N.getValue(1)] = 1; // Generate the token
1408 else
1409 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1410
1411 SDOperand Chain = N.getOperand(0);
1412 SDOperand Address = N.getOperand(1);
1413 Select(Chain);
1414
Nate Begeman9db505c2005-03-28 19:36:43 +00001415 switch (TypeBeingLoaded) {
Nate Begeman74d73452005-03-31 00:15:26 +00001416 default: Node->dump(); assert(0 && "Cannot load this type!");
Nate Begeman9db505c2005-03-28 19:36:43 +00001417 case MVT::i1: Opc = PPC::LBZ; break;
1418 case MVT::i8: Opc = PPC::LBZ; break;
1419 case MVT::i16: Opc = sext ? PPC::LHA : PPC::LHZ; break;
1420 case MVT::i32: Opc = PPC::LWZ; break;
Nate Begeman74d73452005-03-31 00:15:26 +00001421 case MVT::f32: Opc = PPC::LFS; break;
1422 case MVT::f64: Opc = PPC::LFD; break;
Nate Begeman5e966612005-03-24 06:28:42 +00001423 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001424
Nate Begeman74d73452005-03-31 00:15:26 +00001425 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Address)) {
1426 Tmp1 = MakeReg(MVT::i32);
1427 int CPI = CP->getIndex();
Nate Begeman2497e632005-07-21 20:44:43 +00001428 if (PICEnabled)
1429 BuildMI(BB, PPC::ADDIS, 2, Tmp1).addReg(getGlobalBaseReg())
1430 .addConstantPoolIndex(CPI);
1431 else
1432 BuildMI(BB, PPC::LIS, 1, Tmp1).addConstantPoolIndex(CPI);
Nate Begeman74d73452005-03-31 00:15:26 +00001433 BuildMI(BB, Opc, 2, Result).addConstantPoolIndex(CPI).addReg(Tmp1);
Nate Begeman2497e632005-07-21 20:44:43 +00001434 } else if (Address.getOpcode() == ISD::FrameIndex) {
Nate Begeman58f718c2005-03-30 02:23:08 +00001435 Tmp1 = cast<FrameIndexSDNode>(Address)->getIndex();
1436 addFrameReference(BuildMI(BB, Opc, 2, Result), (int)Tmp1);
Nate Begeman2497e632005-07-21 20:44:43 +00001437 } else if(GlobalAddressSDNode *GN = dyn_cast<GlobalAddressSDNode>(Address)){
1438 GlobalValue *GV = GN->getGlobal();
1439 Tmp1 = MakeReg(MVT::i32);
1440 if (PICEnabled)
1441 BuildMI(BB, PPC::ADDIS, 2, Tmp1).addReg(getGlobalBaseReg())
1442 .addGlobalAddress(GV);
1443 else
1444 BuildMI(BB, PPC::LIS, 2, Tmp1).addGlobalAddress(GV);
1445 if (GV->hasWeakLinkage() || GV->isExternal()) {
1446 Tmp2 = MakeReg(MVT::i32);
1447 BuildMI(BB, PPC::LWZ, 2, Tmp2).addGlobalAddress(GV).addReg(Tmp1);
Nate Begeman7b4f0a82005-07-25 21:15:28 +00001448 BuildMI(BB, Opc, 2, Result).addSImm(0).addReg(Tmp2);
1449 } else {
1450 BuildMI(BB, Opc, 2, Result).addGlobalAddress(GV).addReg(Tmp1);
Nate Begeman2497e632005-07-21 20:44:43 +00001451 }
Nate Begeman5e966612005-03-24 06:28:42 +00001452 } else {
1453 int offset;
Nate Begeman2a05c8e2005-07-28 03:02:05 +00001454 switch(SelectAddr(Address, Tmp1, offset)) {
1455 default: assert(0 && "Unhandled return value from SelectAddr");
1456 case 0: // imm offset, no frame, no index
1457 BuildMI(BB, Opc, 2, Result).addSImm(offset).addReg(Tmp1);
1458 break;
1459 case 1: // imm offset + frame index
1460 addFrameReference(BuildMI(BB, Opc, 2, Result), (int)Tmp1, offset);
1461 break;
1462 case 2: // base+index addressing
Nate Begeman04730362005-04-01 04:45:11 +00001463 Opc = IndexedOpForOp(Opc);
1464 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(offset);
Nate Begeman2a05c8e2005-07-28 03:02:05 +00001465 break;
Nate Begeman04730362005-04-01 04:45:11 +00001466 }
Nate Begeman5e966612005-03-24 06:28:42 +00001467 }
1468 return Result;
1469 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001470
Chris Lattnerb5d8e6e2005-05-13 20:29:26 +00001471 case ISD::TAILCALL:
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001472 case ISD::CALL: {
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001473 unsigned GPR_idx = 0, FPR_idx = 0;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001474 static const unsigned GPR[] = {
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001475 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
1476 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
1477 };
1478 static const unsigned FPR[] = {
1479 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
1480 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
1481 };
1482
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001483 // Lower the chain for this call.
1484 Select(N.getOperand(0));
1485 ExprMap[N.getValue(Node->getNumValues()-1)] = 1;
Nate Begeman74d73452005-03-31 00:15:26 +00001486
Nate Begemand860aa62005-04-04 22:17:48 +00001487 MachineInstr *CallMI;
1488 // Emit the correct call instruction based on the type of symbol called.
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001489 if (GlobalAddressSDNode *GASD =
Nate Begemand860aa62005-04-04 22:17:48 +00001490 dyn_cast<GlobalAddressSDNode>(N.getOperand(1))) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001491 CallMI = BuildMI(PPC::CALLpcrel, 1).addGlobalAddress(GASD->getGlobal(),
Nate Begemand860aa62005-04-04 22:17:48 +00001492 true);
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001493 } else if (ExternalSymbolSDNode *ESSDN =
Nate Begemand860aa62005-04-04 22:17:48 +00001494 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1))) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001495 CallMI = BuildMI(PPC::CALLpcrel, 1).addExternalSymbol(ESSDN->getSymbol(),
Nate Begemand860aa62005-04-04 22:17:48 +00001496 true);
1497 } else {
1498 Tmp1 = SelectExpr(N.getOperand(1));
1499 BuildMI(BB, PPC::OR, 2, PPC::R12).addReg(Tmp1).addReg(Tmp1);
1500 BuildMI(BB, PPC::MTCTR, 1).addReg(PPC::R12);
1501 CallMI = BuildMI(PPC::CALLindirect, 3).addImm(20).addImm(0)
1502 .addReg(PPC::R12);
1503 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001504
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001505 // Load the register args to virtual regs
1506 std::vector<unsigned> ArgVR;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001507 for(int i = 2, e = Node->getNumOperands(); i < e; ++i)
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001508 ArgVR.push_back(SelectExpr(N.getOperand(i)));
1509
1510 // Copy the virtual registers into the appropriate argument register
1511 for(int i = 0, e = ArgVR.size(); i < e; ++i) {
1512 switch(N.getOperand(i+2).getValueType()) {
1513 default: Node->dump(); assert(0 && "Unknown value type for call");
1514 case MVT::i1:
1515 case MVT::i8:
1516 case MVT::i16:
1517 case MVT::i32:
1518 assert(GPR_idx < 8 && "Too many int args");
Nate Begemand860aa62005-04-04 22:17:48 +00001519 if (N.getOperand(i+2).getOpcode() != ISD::UNDEF) {
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001520 BuildMI(BB, PPC::OR,2,GPR[GPR_idx]).addReg(ArgVR[i]).addReg(ArgVR[i]);
Nate Begemand860aa62005-04-04 22:17:48 +00001521 CallMI->addRegOperand(GPR[GPR_idx], MachineOperand::Use);
1522 }
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001523 ++GPR_idx;
1524 break;
1525 case MVT::f64:
1526 case MVT::f32:
1527 assert(FPR_idx < 13 && "Too many fp args");
1528 BuildMI(BB, PPC::FMR, 1, FPR[FPR_idx]).addReg(ArgVR[i]);
Nate Begemand860aa62005-04-04 22:17:48 +00001529 CallMI->addRegOperand(FPR[FPR_idx], MachineOperand::Use);
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001530 ++FPR_idx;
1531 break;
1532 }
1533 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001534
Nate Begemand860aa62005-04-04 22:17:48 +00001535 // Put the call instruction in the correct place in the MachineBasicBlock
1536 BB->push_back(CallMI);
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001537
1538 switch (Node->getValueType(0)) {
1539 default: assert(0 && "Unknown value type for call result!");
1540 case MVT::Other: return 1;
1541 case MVT::i1:
1542 case MVT::i8:
1543 case MVT::i16:
1544 case MVT::i32:
Nate Begemane5846682005-04-04 06:52:38 +00001545 if (Node->getValueType(1) == MVT::i32) {
1546 BuildMI(BB, PPC::OR, 2, Result+1).addReg(PPC::R3).addReg(PPC::R3);
1547 BuildMI(BB, PPC::OR, 2, Result).addReg(PPC::R4).addReg(PPC::R4);
1548 } else {
1549 BuildMI(BB, PPC::OR, 2, Result).addReg(PPC::R3).addReg(PPC::R3);
1550 }
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001551 break;
1552 case MVT::f32:
1553 case MVT::f64:
1554 BuildMI(BB, PPC::FMR, 1, Result).addReg(PPC::F1);
1555 break;
1556 }
1557 return Result+N.ResNo;
1558 }
Nate Begemana9795f82005-03-24 04:41:43 +00001559
1560 case ISD::SIGN_EXTEND:
1561 case ISD::SIGN_EXTEND_INREG:
1562 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattnerbce81ae2005-07-10 01:56:13 +00001563 switch(cast<VTSDNode>(Node->getOperand(1))->getVT()) {
Nate Begeman9db505c2005-03-28 19:36:43 +00001564 default: Node->dump(); assert(0 && "Unhandled SIGN_EXTEND type"); break;
Nate Begemanc7bd4822005-04-11 06:34:10 +00001565 case MVT::i16:
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001566 BuildMI(BB, PPC::EXTSH, 1, Result).addReg(Tmp1);
Nate Begeman9db505c2005-03-28 19:36:43 +00001567 break;
Nate Begemanc7bd4822005-04-11 06:34:10 +00001568 case MVT::i8:
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001569 BuildMI(BB, PPC::EXTSB, 1, Result).addReg(Tmp1);
Nate Begeman9db505c2005-03-28 19:36:43 +00001570 break;
Nate Begeman74747862005-03-29 22:24:51 +00001571 case MVT::i1:
1572 BuildMI(BB, PPC::SUBFIC, 2, Result).addReg(Tmp1).addSImm(0);
1573 break;
Nate Begeman9db505c2005-03-28 19:36:43 +00001574 }
Nate Begemana9795f82005-03-24 04:41:43 +00001575 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001576
Nate Begemana9795f82005-03-24 04:41:43 +00001577 case ISD::CopyFromReg:
Nate Begemana3fd4002005-07-19 16:51:05 +00001578 DestType = N.getValue(0).getValueType();
Nate Begemana9795f82005-03-24 04:41:43 +00001579 if (Result == 1)
Nate Begemana3fd4002005-07-19 16:51:05 +00001580 Result = ExprMap[N.getValue(0)] = MakeReg(DestType);
Nate Begemana9795f82005-03-24 04:41:43 +00001581 Tmp1 = dyn_cast<RegSDNode>(Node)->getReg();
Nate Begemana3fd4002005-07-19 16:51:05 +00001582 if (MVT::isInteger(DestType))
1583 BuildMI(BB, PPC::OR, 2, Result).addReg(Tmp1).addReg(Tmp1);
1584 else
1585 BuildMI(BB, PPC::FMR, 1, Result).addReg(Tmp1);
Nate Begemana9795f82005-03-24 04:41:43 +00001586 return Result;
1587
1588 case ISD::SHL:
Nate Begeman5e966612005-03-24 06:28:42 +00001589 Tmp1 = SelectExpr(N.getOperand(0));
1590 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1591 Tmp2 = CN->getValue() & 0x1F;
Nate Begeman33162522005-03-29 21:54:38 +00001592 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(Tmp2).addImm(0)
Nate Begeman5e966612005-03-24 06:28:42 +00001593 .addImm(31-Tmp2);
1594 } else {
Nate Begeman3664cef2005-04-13 22:14:14 +00001595 Tmp2 = FoldIfWideZeroExtend(N.getOperand(1));
Nate Begeman5e966612005-03-24 06:28:42 +00001596 BuildMI(BB, PPC::SLW, 2, Result).addReg(Tmp1).addReg(Tmp2);
1597 }
1598 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001599
Nate Begeman5e966612005-03-24 06:28:42 +00001600 case ISD::SRL:
1601 Tmp1 = SelectExpr(N.getOperand(0));
1602 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1603 Tmp2 = CN->getValue() & 0x1F;
Nate Begeman33162522005-03-29 21:54:38 +00001604 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(32-Tmp2)
Nate Begeman5e966612005-03-24 06:28:42 +00001605 .addImm(Tmp2).addImm(31);
1606 } else {
Nate Begeman3664cef2005-04-13 22:14:14 +00001607 Tmp2 = FoldIfWideZeroExtend(N.getOperand(1));
Nate Begeman5e966612005-03-24 06:28:42 +00001608 BuildMI(BB, PPC::SRW, 2, Result).addReg(Tmp1).addReg(Tmp2);
1609 }
1610 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001611
Nate Begeman5e966612005-03-24 06:28:42 +00001612 case ISD::SRA:
1613 Tmp1 = SelectExpr(N.getOperand(0));
1614 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1615 Tmp2 = CN->getValue() & 0x1F;
1616 BuildMI(BB, PPC::SRAWI, 2, Result).addReg(Tmp1).addImm(Tmp2);
1617 } else {
Nate Begeman3664cef2005-04-13 22:14:14 +00001618 Tmp2 = FoldIfWideZeroExtend(N.getOperand(1));
Nate Begeman5e966612005-03-24 06:28:42 +00001619 BuildMI(BB, PPC::SRAW, 2, Result).addReg(Tmp1).addReg(Tmp2);
1620 }
1621 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001622
Nate Begemand7c4a4a2005-05-11 23:43:56 +00001623 case ISD::CTLZ:
1624 Tmp1 = SelectExpr(N.getOperand(0));
1625 BuildMI(BB, PPC::CNTLZW, 1, Result).addReg(Tmp1);
1626 return Result;
1627
Nate Begemana9795f82005-03-24 04:41:43 +00001628 case ISD::ADD:
Nate Begemana3fd4002005-07-19 16:51:05 +00001629 if (!MVT::isInteger(DestType)) {
1630 if (!NoExcessFPPrecision && N.getOperand(0).getOpcode() == ISD::MUL &&
1631 N.getOperand(0).Val->hasOneUse()) {
1632 ++FusedFP; // Statistic
1633 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1634 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1635 Tmp3 = SelectExpr(N.getOperand(1));
1636 Opc = DestType == MVT::f64 ? PPC::FMADD : PPC::FMADDS;
1637 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1638 return Result;
1639 }
1640 if (!NoExcessFPPrecision && N.getOperand(1).getOpcode() == ISD::MUL &&
1641 N.getOperand(1).Val->hasOneUse()) {
1642 ++FusedFP; // Statistic
1643 Tmp1 = SelectExpr(N.getOperand(1).getOperand(0));
1644 Tmp2 = SelectExpr(N.getOperand(1).getOperand(1));
1645 Tmp3 = SelectExpr(N.getOperand(0));
1646 Opc = DestType == MVT::f64 ? PPC::FMADD : PPC::FMADDS;
1647 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1648 return Result;
1649 }
1650 Opc = DestType == MVT::f64 ? PPC::FADD : PPC::FADDS;
1651 Tmp1 = SelectExpr(N.getOperand(0));
1652 Tmp2 = SelectExpr(N.getOperand(1));
1653 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1654 return Result;
1655 }
Nate Begemana9795f82005-03-24 04:41:43 +00001656 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman439b4442005-04-05 04:22:58 +00001657 switch(getImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
Nate Begemana9795f82005-03-24 04:41:43 +00001658 default: assert(0 && "unhandled result code");
1659 case 0: // No immediate
1660 Tmp2 = SelectExpr(N.getOperand(1));
1661 BuildMI(BB, PPC::ADD, 2, Result).addReg(Tmp1).addReg(Tmp2);
1662 break;
1663 case 1: // Low immediate
1664 BuildMI(BB, PPC::ADDI, 2, Result).addReg(Tmp1).addSImm(Tmp2);
1665 break;
1666 case 2: // Shifted immediate
1667 BuildMI(BB, PPC::ADDIS, 2, Result).addReg(Tmp1).addSImm(Tmp2);
1668 break;
1669 }
1670 return Result;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001671
Nate Begemana9795f82005-03-24 04:41:43 +00001672 case ISD::AND:
Nate Begeman7ddecb42005-04-06 23:51:40 +00001673 switch(getImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
1674 default: assert(0 && "unhandled result code");
1675 case 0: // No immediate
Nate Begemand7c4a4a2005-05-11 23:43:56 +00001676 // Check for andc: and, (xor a, -1), b
1677 if (N.getOperand(0).getOpcode() == ISD::XOR &&
1678 N.getOperand(0).getOperand(1).getOpcode() == ISD::Constant &&
1679 cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->isAllOnesValue()) {
1680 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1681 Tmp2 = SelectExpr(N.getOperand(1));
1682 BuildMI(BB, PPC::ANDC, 2, Result).addReg(Tmp2).addReg(Tmp1);
1683 return Result;
1684 }
1685 // It wasn't and-with-complement, emit a regular and
Chris Lattnercafb67b2005-05-09 17:39:48 +00001686 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman7ddecb42005-04-06 23:51:40 +00001687 Tmp2 = SelectExpr(N.getOperand(1));
Nate Begemanc7bd4822005-04-11 06:34:10 +00001688 Opc = Recording ? PPC::ANDo : PPC::AND;
1689 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
Nate Begeman7ddecb42005-04-06 23:51:40 +00001690 break;
1691 case 1: // Low immediate
Chris Lattnercafb67b2005-05-09 17:39:48 +00001692 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman7ddecb42005-04-06 23:51:40 +00001693 BuildMI(BB, PPC::ANDIo, 2, Result).addReg(Tmp1).addImm(Tmp2);
1694 break;
1695 case 2: // Shifted immediate
Chris Lattnercafb67b2005-05-09 17:39:48 +00001696 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman7ddecb42005-04-06 23:51:40 +00001697 BuildMI(BB, PPC::ANDISo, 2, Result).addReg(Tmp1).addImm(Tmp2);
1698 break;
Nate Begeman9f833d32005-04-12 00:10:02 +00001699 case 5: // Bitfield mask
1700 Opc = Recording ? PPC::RLWINMo : PPC::RLWINM;
1701 Tmp3 = Tmp2 >> 16; // MB
1702 Tmp2 &= 0xFFFF; // ME
Chris Lattnercafb67b2005-05-09 17:39:48 +00001703
Nate Begeman3dee1752005-07-27 23:11:27 +00001704 // FIXME: Catch SHL-AND in addition to SRL-AND in this block.
Chris Lattnercafb67b2005-05-09 17:39:48 +00001705 if (N.getOperand(0).getOpcode() == ISD::SRL)
1706 if (ConstantSDNode *SA =
1707 dyn_cast<ConstantSDNode>(N.getOperand(0).getOperand(1))) {
1708
1709 // We can fold the RLWINM and the SRL together if the mask is
1710 // clearing the top bits which are rotated around.
1711 unsigned RotAmt = 32-(SA->getValue() & 31);
1712 if (Tmp2 <= RotAmt) {
1713 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1714 BuildMI(BB, Opc, 4, Result).addReg(Tmp1).addImm(RotAmt)
1715 .addImm(Tmp3).addImm(Tmp2);
1716 break;
1717 }
1718 }
1719
1720 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman9f833d32005-04-12 00:10:02 +00001721 BuildMI(BB, Opc, 4, Result).addReg(Tmp1).addImm(0)
1722 .addImm(Tmp3).addImm(Tmp2);
1723 break;
Nate Begeman7ddecb42005-04-06 23:51:40 +00001724 }
Nate Begemanc7bd4822005-04-11 06:34:10 +00001725 RecordSuccess = true;
Nate Begeman7ddecb42005-04-06 23:51:40 +00001726 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001727
Nate Begemana9795f82005-03-24 04:41:43 +00001728 case ISD::OR:
Nate Begeman7ddecb42005-04-06 23:51:40 +00001729 if (SelectBitfieldInsert(N, Result))
1730 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001731 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman439b4442005-04-05 04:22:58 +00001732 switch(getImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
Nate Begemana9795f82005-03-24 04:41:43 +00001733 default: assert(0 && "unhandled result code");
1734 case 0: // No immediate
1735 Tmp2 = SelectExpr(N.getOperand(1));
Nate Begemanc7bd4822005-04-11 06:34:10 +00001736 Opc = Recording ? PPC::ORo : PPC::OR;
1737 RecordSuccess = true;
1738 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +00001739 break;
1740 case 1: // Low immediate
Nate Begeman7ddecb42005-04-06 23:51:40 +00001741 BuildMI(BB, PPC::ORI, 2, Result).addReg(Tmp1).addImm(Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +00001742 break;
1743 case 2: // Shifted immediate
Nate Begeman7ddecb42005-04-06 23:51:40 +00001744 BuildMI(BB, PPC::ORIS, 2, Result).addReg(Tmp1).addImm(Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +00001745 break;
1746 }
1747 return Result;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001748
Nate Begemanaa73a9f2005-04-03 11:20:20 +00001749 case ISD::XOR: {
1750 // Check for EQV: xor, (xor a, -1), b
1751 if (N.getOperand(0).getOpcode() == ISD::XOR &&
1752 N.getOperand(0).getOperand(1).getOpcode() == ISD::Constant &&
1753 cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->isAllOnesValue()) {
Nate Begemanaa73a9f2005-04-03 11:20:20 +00001754 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1755 Tmp2 = SelectExpr(N.getOperand(1));
1756 BuildMI(BB, PPC::EQV, 2, Result).addReg(Tmp1).addReg(Tmp2);
1757 return Result;
1758 }
Chris Lattner837a5212005-04-21 21:09:11 +00001759 // Check for NOT, NOR, EQV, and NAND: xor (copy, or, xor, and), -1
Nate Begemanaa73a9f2005-04-03 11:20:20 +00001760 if (N.getOperand(1).getOpcode() == ISD::Constant &&
1761 cast<ConstantSDNode>(N.getOperand(1))->isAllOnesValue()) {
Nate Begemanaa73a9f2005-04-03 11:20:20 +00001762 switch(N.getOperand(0).getOpcode()) {
1763 case ISD::OR:
1764 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1765 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1766 BuildMI(BB, PPC::NOR, 2, Result).addReg(Tmp1).addReg(Tmp2);
1767 break;
1768 case ISD::AND:
1769 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1770 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1771 BuildMI(BB, PPC::NAND, 2, Result).addReg(Tmp1).addReg(Tmp2);
1772 break;
Chris Lattner837a5212005-04-21 21:09:11 +00001773 case ISD::XOR:
1774 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1775 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1776 BuildMI(BB, PPC::EQV, 2, Result).addReg(Tmp1).addReg(Tmp2);
1777 break;
Nate Begemanaa73a9f2005-04-03 11:20:20 +00001778 default:
1779 Tmp1 = SelectExpr(N.getOperand(0));
1780 BuildMI(BB, PPC::NOR, 2, Result).addReg(Tmp1).addReg(Tmp1);
1781 break;
1782 }
1783 return Result;
1784 }
1785 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman439b4442005-04-05 04:22:58 +00001786 switch(getImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
Nate Begemanaa73a9f2005-04-03 11:20:20 +00001787 default: assert(0 && "unhandled result code");
1788 case 0: // No immediate
1789 Tmp2 = SelectExpr(N.getOperand(1));
1790 BuildMI(BB, PPC::XOR, 2, Result).addReg(Tmp1).addReg(Tmp2);
1791 break;
1792 case 1: // Low immediate
1793 BuildMI(BB, PPC::XORI, 2, Result).addReg(Tmp1).addImm(Tmp2);
1794 break;
1795 case 2: // Shifted immediate
1796 BuildMI(BB, PPC::XORIS, 2, Result).addReg(Tmp1).addImm(Tmp2);
1797 break;
1798 }
1799 return Result;
1800 }
1801
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001802 case ISD::SUB:
Nate Begemana3fd4002005-07-19 16:51:05 +00001803 if (!MVT::isInteger(DestType)) {
1804 if (!NoExcessFPPrecision && N.getOperand(0).getOpcode() == ISD::MUL &&
1805 N.getOperand(0).Val->hasOneUse()) {
1806 ++FusedFP; // Statistic
1807 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1808 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1809 Tmp3 = SelectExpr(N.getOperand(1));
1810 Opc = DestType == MVT::f64 ? PPC::FMSUB : PPC::FMSUBS;
1811 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1812 return Result;
1813 }
1814 if (!NoExcessFPPrecision && N.getOperand(1).getOpcode() == ISD::MUL &&
1815 N.getOperand(1).Val->hasOneUse()) {
1816 ++FusedFP; // Statistic
1817 Tmp1 = SelectExpr(N.getOperand(1).getOperand(0));
1818 Tmp2 = SelectExpr(N.getOperand(1).getOperand(1));
1819 Tmp3 = SelectExpr(N.getOperand(0));
1820 Opc = DestType == MVT::f64 ? PPC::FNMSUB : PPC::FNMSUBS;
1821 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1822 return Result;
1823 }
1824 Opc = DestType == MVT::f64 ? PPC::FSUB : PPC::FSUBS;
1825 Tmp1 = SelectExpr(N.getOperand(0));
1826 Tmp2 = SelectExpr(N.getOperand(1));
1827 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1828 return Result;
1829 }
Nate Begemand7c4a4a2005-05-11 23:43:56 +00001830 if (1 == getImmediateForOpcode(N.getOperand(0), opcode, Tmp1, true)) {
1831 Tmp2 = SelectExpr(N.getOperand(1));
Nate Begeman27523a12005-04-02 00:42:16 +00001832 BuildMI(BB, PPC::SUBFIC, 2, Result).addReg(Tmp2).addSImm(Tmp1);
Nate Begemand7c4a4a2005-05-11 23:43:56 +00001833 } else if (1 == getImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
Nate Begeman27523a12005-04-02 00:42:16 +00001834 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begemand7c4a4a2005-05-11 23:43:56 +00001835 BuildMI(BB, PPC::ADDI, 2, Result).addReg(Tmp1).addSImm(Tmp2);
1836 } else {
1837 Tmp1 = SelectExpr(N.getOperand(0));
1838 Tmp2 = SelectExpr(N.getOperand(1));
Nate Begeman27523a12005-04-02 00:42:16 +00001839 BuildMI(BB, PPC::SUBF, 2, Result).addReg(Tmp2).addReg(Tmp1);
1840 }
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001841 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001842
Nate Begeman5e966612005-03-24 06:28:42 +00001843 case ISD::MUL:
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001844 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman439b4442005-04-05 04:22:58 +00001845 if (1 == getImmediateForOpcode(N.getOperand(1), opcode, Tmp2))
Nate Begeman307e7442005-03-26 01:28:53 +00001846 BuildMI(BB, PPC::MULLI, 2, Result).addReg(Tmp1).addSImm(Tmp2);
1847 else {
1848 Tmp2 = SelectExpr(N.getOperand(1));
Nate Begemana3fd4002005-07-19 16:51:05 +00001849 switch (DestType) {
1850 default: assert(0 && "Unknown type to ISD::MUL"); break;
1851 case MVT::i32: Opc = PPC::MULLW; break;
1852 case MVT::f32: Opc = PPC::FMULS; break;
1853 case MVT::f64: Opc = PPC::FMUL; break;
1854 }
1855 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
Nate Begeman307e7442005-03-26 01:28:53 +00001856 }
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001857 return Result;
1858
Nate Begeman815d6da2005-04-06 00:25:27 +00001859 case ISD::MULHS:
1860 case ISD::MULHU:
1861 Tmp1 = SelectExpr(N.getOperand(0));
1862 Tmp2 = SelectExpr(N.getOperand(1));
1863 Opc = (ISD::MULHU == opcode) ? PPC::MULHWU : PPC::MULHW;
1864 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1865 return Result;
1866
Nate Begemanf3d08f32005-03-29 00:03:27 +00001867 case ISD::SDIV:
1868 case ISD::UDIV:
Nate Begeman815d6da2005-04-06 00:25:27 +00001869 switch (getImmediateForOpcode(N.getOperand(1), opcode, Tmp3)) {
1870 default: break;
1871 // If this is an sdiv by a power of two, we can use an srawi/addze pair.
1872 case 3:
Nate Begeman80196b12005-04-05 00:15:08 +00001873 Tmp1 = MakeReg(MVT::i32);
1874 Tmp2 = SelectExpr(N.getOperand(0));
Nate Begeman9f833d32005-04-12 00:10:02 +00001875 if ((int)Tmp3 < 0) {
1876 unsigned Tmp4 = MakeReg(MVT::i32);
1877 BuildMI(BB, PPC::SRAWI, 2, Tmp1).addReg(Tmp2).addImm(-Tmp3);
1878 BuildMI(BB, PPC::ADDZE, 1, Tmp4).addReg(Tmp1);
1879 BuildMI(BB, PPC::NEG, 1, Result).addReg(Tmp4);
1880 } else {
1881 BuildMI(BB, PPC::SRAWI, 2, Tmp1).addReg(Tmp2).addImm(Tmp3);
1882 BuildMI(BB, PPC::ADDZE, 1, Result).addReg(Tmp1);
1883 }
Nate Begeman80196b12005-04-05 00:15:08 +00001884 return Result;
Nate Begeman815d6da2005-04-06 00:25:27 +00001885 // If this is a divide by constant, we can emit code using some magic
1886 // constants to implement it as a multiply instead.
Nate Begeman27b4c232005-04-06 06:44:57 +00001887 case 4:
1888 ExprMap.erase(N);
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001889 if (opcode == ISD::SDIV)
Nate Begeman27b4c232005-04-06 06:44:57 +00001890 return SelectExpr(BuildSDIVSequence(N));
1891 else
1892 return SelectExpr(BuildUDIVSequence(N));
Jeff Cohen00b168892005-07-27 06:12:32 +00001893 }
Nate Begemanf3d08f32005-03-29 00:03:27 +00001894 Tmp1 = SelectExpr(N.getOperand(0));
1895 Tmp2 = SelectExpr(N.getOperand(1));
Nate Begemana3fd4002005-07-19 16:51:05 +00001896 switch (DestType) {
1897 default: assert(0 && "Unknown type to ISD::SDIV"); break;
1898 case MVT::i32: Opc = (ISD::UDIV == opcode) ? PPC::DIVWU : PPC::DIVW; break;
1899 case MVT::f32: Opc = PPC::FDIVS; break;
1900 case MVT::f64: Opc = PPC::FDIV; break;
1901 }
Nate Begemanf3d08f32005-03-29 00:03:27 +00001902 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1903 return Result;
1904
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001905 case ISD::ADD_PARTS:
Nate Begemanca12a2b2005-03-28 22:28:37 +00001906 case ISD::SUB_PARTS: {
1907 assert(N.getNumOperands() == 4 && N.getValueType() == MVT::i32 &&
1908 "Not an i64 add/sub!");
1909 // Emit all of the operands.
1910 std::vector<unsigned> InVals;
1911 for (unsigned i = 0, e = N.getNumOperands(); i != e; ++i)
1912 InVals.push_back(SelectExpr(N.getOperand(i)));
1913 if (N.getOpcode() == ISD::ADD_PARTS) {
Nate Begeman27eeb002005-04-02 05:59:34 +00001914 BuildMI(BB, PPC::ADDC, 2, Result).addReg(InVals[0]).addReg(InVals[2]);
1915 BuildMI(BB, PPC::ADDE, 2, Result+1).addReg(InVals[1]).addReg(InVals[3]);
Nate Begemanca12a2b2005-03-28 22:28:37 +00001916 } else {
Nate Begeman27eeb002005-04-02 05:59:34 +00001917 BuildMI(BB, PPC::SUBFC, 2, Result).addReg(InVals[2]).addReg(InVals[0]);
1918 BuildMI(BB, PPC::SUBFE, 2, Result+1).addReg(InVals[3]).addReg(InVals[1]);
1919 }
1920 return Result+N.ResNo;
1921 }
1922
1923 case ISD::SHL_PARTS:
1924 case ISD::SRA_PARTS:
1925 case ISD::SRL_PARTS: {
1926 assert(N.getNumOperands() == 3 && N.getValueType() == MVT::i32 &&
1927 "Not an i64 shift!");
1928 unsigned ShiftOpLo = SelectExpr(N.getOperand(0));
1929 unsigned ShiftOpHi = SelectExpr(N.getOperand(1));
Nate Begeman3664cef2005-04-13 22:14:14 +00001930 unsigned SHReg = FoldIfWideZeroExtend(N.getOperand(2));
1931 Tmp1 = MakeReg(MVT::i32);
1932 Tmp2 = MakeReg(MVT::i32);
Nate Begeman27eeb002005-04-02 05:59:34 +00001933 Tmp3 = MakeReg(MVT::i32);
1934 unsigned Tmp4 = MakeReg(MVT::i32);
1935 unsigned Tmp5 = MakeReg(MVT::i32);
1936 unsigned Tmp6 = MakeReg(MVT::i32);
1937 BuildMI(BB, PPC::SUBFIC, 2, Tmp1).addReg(SHReg).addSImm(32);
1938 if (ISD::SHL_PARTS == opcode) {
1939 BuildMI(BB, PPC::SLW, 2, Tmp2).addReg(ShiftOpHi).addReg(SHReg);
1940 BuildMI(BB, PPC::SRW, 2, Tmp3).addReg(ShiftOpLo).addReg(Tmp1);
1941 BuildMI(BB, PPC::OR, 2, Tmp4).addReg(Tmp2).addReg(Tmp3);
1942 BuildMI(BB, PPC::ADDI, 2, Tmp5).addReg(SHReg).addSImm(-32);
Nate Begemanfa554702005-04-03 22:13:27 +00001943 BuildMI(BB, PPC::SLW, 2, Tmp6).addReg(ShiftOpLo).addReg(Tmp5);
Nate Begeman27eeb002005-04-02 05:59:34 +00001944 BuildMI(BB, PPC::OR, 2, Result+1).addReg(Tmp4).addReg(Tmp6);
1945 BuildMI(BB, PPC::SLW, 2, Result).addReg(ShiftOpLo).addReg(SHReg);
1946 } else if (ISD::SRL_PARTS == opcode) {
1947 BuildMI(BB, PPC::SRW, 2, Tmp2).addReg(ShiftOpLo).addReg(SHReg);
1948 BuildMI(BB, PPC::SLW, 2, Tmp3).addReg(ShiftOpHi).addReg(Tmp1);
1949 BuildMI(BB, PPC::OR, 2, Tmp4).addReg(Tmp2).addReg(Tmp3);
1950 BuildMI(BB, PPC::ADDI, 2, Tmp5).addReg(SHReg).addSImm(-32);
1951 BuildMI(BB, PPC::SRW, 2, Tmp6).addReg(ShiftOpHi).addReg(Tmp5);
1952 BuildMI(BB, PPC::OR, 2, Result).addReg(Tmp4).addReg(Tmp6);
1953 BuildMI(BB, PPC::SRW, 2, Result+1).addReg(ShiftOpHi).addReg(SHReg);
1954 } else {
1955 MachineBasicBlock *TmpMBB = new MachineBasicBlock(BB->getBasicBlock());
1956 MachineBasicBlock *PhiMBB = new MachineBasicBlock(BB->getBasicBlock());
1957 MachineBasicBlock *OldMBB = BB;
1958 MachineFunction *F = BB->getParent();
1959 ilist<MachineBasicBlock>::iterator It = BB; ++It;
1960 F->getBasicBlockList().insert(It, TmpMBB);
1961 F->getBasicBlockList().insert(It, PhiMBB);
1962 BB->addSuccessor(TmpMBB);
1963 BB->addSuccessor(PhiMBB);
1964 BuildMI(BB, PPC::SRW, 2, Tmp2).addReg(ShiftOpLo).addReg(SHReg);
1965 BuildMI(BB, PPC::SLW, 2, Tmp3).addReg(ShiftOpHi).addReg(Tmp1);
1966 BuildMI(BB, PPC::OR, 2, Tmp4).addReg(Tmp2).addReg(Tmp3);
1967 BuildMI(BB, PPC::ADDICo, 2, Tmp5).addReg(SHReg).addSImm(-32);
1968 BuildMI(BB, PPC::SRAW, 2, Tmp6).addReg(ShiftOpHi).addReg(Tmp5);
1969 BuildMI(BB, PPC::SRAW, 2, Result+1).addReg(ShiftOpHi).addReg(SHReg);
1970 BuildMI(BB, PPC::BLE, 2).addReg(PPC::CR0).addMBB(PhiMBB);
1971 // Select correct least significant half if the shift amount > 32
1972 BB = TmpMBB;
1973 unsigned Tmp7 = MakeReg(MVT::i32);
1974 BuildMI(BB, PPC::OR, 2, Tmp7).addReg(Tmp6).addReg(Tmp6);
1975 TmpMBB->addSuccessor(PhiMBB);
1976 BB = PhiMBB;
1977 BuildMI(BB, PPC::PHI, 4, Result).addReg(Tmp4).addMBB(OldMBB)
1978 .addReg(Tmp7).addMBB(TmpMBB);
Nate Begemanca12a2b2005-03-28 22:28:37 +00001979 }
1980 return Result+N.ResNo;
1981 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001982
Nate Begemana9795f82005-03-24 04:41:43 +00001983 case ISD::FP_TO_UINT:
Nate Begeman6b559972005-04-01 02:59:27 +00001984 case ISD::FP_TO_SINT: {
1985 bool U = (ISD::FP_TO_UINT == opcode);
1986 Tmp1 = SelectExpr(N.getOperand(0));
1987 if (!U) {
1988 Tmp2 = MakeReg(MVT::f64);
1989 BuildMI(BB, PPC::FCTIWZ, 1, Tmp2).addReg(Tmp1);
1990 int FrameIdx = BB->getParent()->getFrameInfo()->CreateStackObject(8, 8);
1991 addFrameReference(BuildMI(BB, PPC::STFD, 3).addReg(Tmp2), FrameIdx);
1992 addFrameReference(BuildMI(BB, PPC::LWZ, 2, Result), FrameIdx, 4);
1993 return Result;
1994 } else {
1995 unsigned Zero = getConstDouble(0.0);
1996 unsigned MaxInt = getConstDouble((1LL << 32) - 1);
1997 unsigned Border = getConstDouble(1LL << 31);
1998 unsigned UseZero = MakeReg(MVT::f64);
1999 unsigned UseMaxInt = MakeReg(MVT::f64);
2000 unsigned UseChoice = MakeReg(MVT::f64);
2001 unsigned TmpReg = MakeReg(MVT::f64);
2002 unsigned TmpReg2 = MakeReg(MVT::f64);
2003 unsigned ConvReg = MakeReg(MVT::f64);
2004 unsigned IntTmp = MakeReg(MVT::i32);
2005 unsigned XorReg = MakeReg(MVT::i32);
2006 MachineFunction *F = BB->getParent();
2007 int FrameIdx = F->getFrameInfo()->CreateStackObject(8, 8);
2008 // Update machine-CFG edges
2009 MachineBasicBlock *XorMBB = new MachineBasicBlock(BB->getBasicBlock());
2010 MachineBasicBlock *PhiMBB = new MachineBasicBlock(BB->getBasicBlock());
2011 MachineBasicBlock *OldMBB = BB;
2012 ilist<MachineBasicBlock>::iterator It = BB; ++It;
2013 F->getBasicBlockList().insert(It, XorMBB);
2014 F->getBasicBlockList().insert(It, PhiMBB);
2015 BB->addSuccessor(XorMBB);
2016 BB->addSuccessor(PhiMBB);
2017 // Convert from floating point to unsigned 32-bit value
2018 // Use 0 if incoming value is < 0.0
2019 BuildMI(BB, PPC::FSEL, 3, UseZero).addReg(Tmp1).addReg(Tmp1).addReg(Zero);
2020 // Use 2**32 - 1 if incoming value is >= 2**32
2021 BuildMI(BB, PPC::FSUB, 2, UseMaxInt).addReg(MaxInt).addReg(Tmp1);
2022 BuildMI(BB, PPC::FSEL, 3, UseChoice).addReg(UseMaxInt).addReg(UseZero)
2023 .addReg(MaxInt);
2024 // Subtract 2**31
2025 BuildMI(BB, PPC::FSUB, 2, TmpReg).addReg(UseChoice).addReg(Border);
2026 // Use difference if >= 2**31
2027 BuildMI(BB, PPC::FCMPU, 2, PPC::CR0).addReg(UseChoice).addReg(Border);
2028 BuildMI(BB, PPC::FSEL, 3, TmpReg2).addReg(TmpReg).addReg(TmpReg)
2029 .addReg(UseChoice);
2030 // Convert to integer
2031 BuildMI(BB, PPC::FCTIWZ, 1, ConvReg).addReg(TmpReg2);
2032 addFrameReference(BuildMI(BB, PPC::STFD, 3).addReg(ConvReg), FrameIdx);
2033 addFrameReference(BuildMI(BB, PPC::LWZ, 2, IntTmp), FrameIdx, 4);
2034 BuildMI(BB, PPC::BLT, 2).addReg(PPC::CR0).addMBB(PhiMBB);
2035 BuildMI(BB, PPC::B, 1).addMBB(XorMBB);
2036
2037 // XorMBB:
2038 // add 2**31 if input was >= 2**31
2039 BB = XorMBB;
2040 BuildMI(BB, PPC::XORIS, 2, XorReg).addReg(IntTmp).addImm(0x8000);
2041 XorMBB->addSuccessor(PhiMBB);
2042
2043 // PhiMBB:
2044 // DestReg = phi [ IntTmp, OldMBB ], [ XorReg, XorMBB ]
2045 BB = PhiMBB;
2046 BuildMI(BB, PPC::PHI, 4, Result).addReg(IntTmp).addMBB(OldMBB)
2047 .addReg(XorReg).addMBB(XorMBB);
2048 return Result;
2049 }
2050 assert(0 && "Should never get here");
2051 return 0;
2052 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002053
Nate Begeman9e3e1b52005-03-24 23:35:30 +00002054 case ISD::SETCC:
Nate Begeman33162522005-03-29 21:54:38 +00002055 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Node)) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002056 if (ConstantSDNode *CN =
Nate Begeman7e7fadd2005-04-07 20:30:01 +00002057 dyn_cast<ConstantSDNode>(SetCC->getOperand(1).Val)) {
Nate Begeman9765c252005-04-12 21:22:28 +00002058 // We can codegen setcc op, imm very efficiently compared to a brcond.
2059 // Check for those cases here.
2060 // setcc op, 0
Nate Begeman7e7fadd2005-04-07 20:30:01 +00002061 if (CN->getValue() == 0) {
2062 Tmp1 = SelectExpr(SetCC->getOperand(0));
2063 switch (SetCC->getCondition()) {
Nate Begeman7bfba7d2005-04-14 09:45:08 +00002064 default: SetCC->dump(); assert(0 && "Unhandled SetCC condition"); abort();
Nate Begeman7e7fadd2005-04-07 20:30:01 +00002065 case ISD::SETEQ:
Nate Begeman7e7fadd2005-04-07 20:30:01 +00002066 Tmp2 = MakeReg(MVT::i32);
2067 BuildMI(BB, PPC::CNTLZW, 1, Tmp2).addReg(Tmp1);
2068 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp2).addImm(27)
2069 .addImm(5).addImm(31);
2070 break;
2071 case ISD::SETNE:
Nate Begeman7e7fadd2005-04-07 20:30:01 +00002072 Tmp2 = MakeReg(MVT::i32);
2073 BuildMI(BB, PPC::ADDIC, 2, Tmp2).addReg(Tmp1).addSImm(-1);
2074 BuildMI(BB, PPC::SUBFE, 2, Result).addReg(Tmp2).addReg(Tmp1);
2075 break;
Nate Begeman7e7fadd2005-04-07 20:30:01 +00002076 case ISD::SETLT:
2077 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(1)
2078 .addImm(31).addImm(31);
2079 break;
Nate Begeman7e7fadd2005-04-07 20:30:01 +00002080 case ISD::SETGT:
2081 Tmp2 = MakeReg(MVT::i32);
2082 Tmp3 = MakeReg(MVT::i32);
2083 BuildMI(BB, PPC::NEG, 2, Tmp2).addReg(Tmp1);
2084 BuildMI(BB, PPC::ANDC, 2, Tmp3).addReg(Tmp2).addReg(Tmp1);
2085 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp3).addImm(1)
2086 .addImm(31).addImm(31);
2087 break;
Nate Begeman9765c252005-04-12 21:22:28 +00002088 }
2089 return Result;
2090 }
2091 // setcc op, -1
2092 if (CN->isAllOnesValue()) {
2093 Tmp1 = SelectExpr(SetCC->getOperand(0));
2094 switch (SetCC->getCondition()) {
2095 default: assert(0 && "Unhandled SetCC condition"); abort();
2096 case ISD::SETEQ:
2097 Tmp2 = MakeReg(MVT::i32);
2098 Tmp3 = MakeReg(MVT::i32);
2099 BuildMI(BB, PPC::ADDIC, 2, Tmp2).addReg(Tmp1).addSImm(1);
2100 BuildMI(BB, PPC::LI, 1, Tmp3).addSImm(0);
2101 BuildMI(BB, PPC::ADDZE, 1, Result).addReg(Tmp3);
Nate Begeman7e7fadd2005-04-07 20:30:01 +00002102 break;
Nate Begeman9765c252005-04-12 21:22:28 +00002103 case ISD::SETNE:
2104 Tmp2 = MakeReg(MVT::i32);
2105 Tmp3 = MakeReg(MVT::i32);
2106 BuildMI(BB, PPC::NOR, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
2107 BuildMI(BB, PPC::ADDIC, 2, Tmp3).addReg(Tmp2).addSImm(-1);
2108 BuildMI(BB, PPC::SUBFE, 2, Result).addReg(Tmp3).addReg(Tmp2);
2109 break;
2110 case ISD::SETLT:
2111 Tmp2 = MakeReg(MVT::i32);
2112 Tmp3 = MakeReg(MVT::i32);
2113 BuildMI(BB, PPC::ADDI, 2, Tmp2).addReg(Tmp1).addSImm(1);
2114 BuildMI(BB, PPC::AND, 2, Tmp3).addReg(Tmp2).addReg(Tmp1);
2115 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp3).addImm(1)
2116 .addImm(31).addImm(31);
2117 break;
2118 case ISD::SETGT:
2119 Tmp2 = MakeReg(MVT::i32);
Nate Begeman7e7fadd2005-04-07 20:30:01 +00002120 BuildMI(BB, PPC::RLWINM, 4, Tmp2).addReg(Tmp1).addImm(1)
2121 .addImm(31).addImm(31);
2122 BuildMI(BB, PPC::XORI, 2, Result).addReg(Tmp2).addImm(1);
2123 break;
2124 }
2125 return Result;
2126 }
2127 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002128
Nate Begeman1cbf3ab2005-04-18 07:48:09 +00002129 bool Inv;
2130 unsigned CCReg = SelectCC(N, Opc, Inv, Tmp2);
2131 MoveCRtoGPR(CCReg, Inv, Tmp2, Result);
Nate Begeman33162522005-03-29 21:54:38 +00002132 return Result;
2133 }
2134 assert(0 && "Is this legal?");
2135 return 0;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002136
Nate Begeman74747862005-03-29 22:24:51 +00002137 case ISD::SELECT: {
Nate Begemana3fd4002005-07-19 16:51:05 +00002138 SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(N.getOperand(0).Val);
2139 if (SetCC && N.getOperand(0).getOpcode() == ISD::SETCC &&
2140 !MVT::isInteger(SetCC->getOperand(0).getValueType()) &&
2141 !MVT::isInteger(N.getOperand(1).getValueType()) &&
2142 !MVT::isInteger(N.getOperand(2).getValueType()) &&
2143 SetCC->getCondition() != ISD::SETEQ &&
2144 SetCC->getCondition() != ISD::SETNE) {
2145 MVT::ValueType VT = SetCC->getOperand(0).getValueType();
2146 unsigned TV = SelectExpr(N.getOperand(1)); // Use if TRUE
2147 unsigned FV = SelectExpr(N.getOperand(2)); // Use if FALSE
2148
2149 ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(1));
2150 if (CN && (CN->isExactlyValue(-0.0) || CN->isExactlyValue(0.0))) {
2151 switch(SetCC->getCondition()) {
2152 default: assert(0 && "Invalid FSEL condition"); abort();
2153 case ISD::SETULT:
2154 case ISD::SETLT:
2155 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
2156 case ISD::SETUGE:
2157 case ISD::SETGE:
2158 Tmp1 = SelectExpr(SetCC->getOperand(0)); // Val to compare against
2159 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp1).addReg(TV).addReg(FV);
2160 return Result;
2161 case ISD::SETUGT:
2162 case ISD::SETGT:
2163 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
2164 case ISD::SETULE:
2165 case ISD::SETLE: {
2166 if (SetCC->getOperand(0).getOpcode() == ISD::FNEG) {
2167 Tmp2 = SelectExpr(SetCC->getOperand(0).getOperand(0));
2168 } else {
2169 Tmp2 = MakeReg(VT);
2170 Tmp1 = SelectExpr(SetCC->getOperand(0)); // Val to compare against
2171 BuildMI(BB, PPC::FNEG, 1, Tmp2).addReg(Tmp1);
2172 }
2173 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp2).addReg(TV).addReg(FV);
2174 return Result;
2175 }
2176 }
2177 } else {
2178 Opc = (MVT::f64 == VT) ? PPC::FSUB : PPC::FSUBS;
2179 Tmp1 = SelectExpr(SetCC->getOperand(0)); // Val to compare against
2180 Tmp2 = SelectExpr(SetCC->getOperand(1));
2181 Tmp3 = MakeReg(VT);
2182 switch(SetCC->getCondition()) {
2183 default: assert(0 && "Invalid FSEL condition"); abort();
2184 case ISD::SETULT:
2185 case ISD::SETLT:
2186 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
2187 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(FV).addReg(TV);
2188 return Result;
2189 case ISD::SETUGE:
2190 case ISD::SETGE:
2191 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
2192 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(TV).addReg(FV);
2193 return Result;
2194 case ISD::SETUGT:
2195 case ISD::SETGT:
2196 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp2).addReg(Tmp1);
2197 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(FV).addReg(TV);
2198 return Result;
2199 case ISD::SETULE:
2200 case ISD::SETLE:
2201 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp2).addReg(Tmp1);
2202 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(TV).addReg(FV);
2203 return Result;
2204 }
2205 }
2206 assert(0 && "Should never get here");
2207 return 0;
2208 }
2209
Nate Begeman1cbf3ab2005-04-18 07:48:09 +00002210 bool Inv;
Chris Lattner30710192005-04-01 07:10:02 +00002211 unsigned TrueValue = SelectExpr(N.getOperand(1)); //Use if TRUE
2212 unsigned FalseValue = SelectExpr(N.getOperand(2)); //Use if FALSE
Nate Begeman1cbf3ab2005-04-18 07:48:09 +00002213 unsigned CCReg = SelectCC(N.getOperand(0), Opc, Inv, Tmp3);
Chris Lattner30710192005-04-01 07:10:02 +00002214
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002215 // Create an iterator with which to insert the MBB for copying the false
Nate Begeman74747862005-03-29 22:24:51 +00002216 // value and the MBB to hold the PHI instruction for this SetCC.
2217 MachineBasicBlock *thisMBB = BB;
2218 const BasicBlock *LLVM_BB = BB->getBasicBlock();
2219 ilist<MachineBasicBlock>::iterator It = BB;
2220 ++It;
2221
2222 // thisMBB:
2223 // ...
2224 // TrueVal = ...
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00002225 // cmpTY ccX, r1, r2
Nate Begeman74747862005-03-29 22:24:51 +00002226 // bCC copy1MBB
2227 // fallthrough --> copy0MBB
Nate Begeman74747862005-03-29 22:24:51 +00002228 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
2229 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00002230 BuildMI(BB, Opc, 2).addReg(CCReg).addMBB(sinkMBB);
Nate Begeman74747862005-03-29 22:24:51 +00002231 MachineFunction *F = BB->getParent();
2232 F->getBasicBlockList().insert(It, copy0MBB);
2233 F->getBasicBlockList().insert(It, sinkMBB);
2234 // Update machine-CFG edges
2235 BB->addSuccessor(copy0MBB);
2236 BB->addSuccessor(sinkMBB);
2237
2238 // copy0MBB:
2239 // %FalseValue = ...
2240 // # fallthrough to sinkMBB
2241 BB = copy0MBB;
Nate Begeman74747862005-03-29 22:24:51 +00002242 // Update machine-CFG edges
2243 BB->addSuccessor(sinkMBB);
2244
2245 // sinkMBB:
2246 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
2247 // ...
2248 BB = sinkMBB;
2249 BuildMI(BB, PPC::PHI, 4, Result).addReg(FalseValue)
2250 .addMBB(copy0MBB).addReg(TrueValue).addMBB(thisMBB);
Nate Begeman74747862005-03-29 22:24:51 +00002251 return Result;
2252 }
Nate Begemana9795f82005-03-24 04:41:43 +00002253
2254 case ISD::Constant:
2255 switch (N.getValueType()) {
2256 default: assert(0 && "Cannot use constants of this type!");
2257 case MVT::i1:
2258 BuildMI(BB, PPC::LI, 1, Result)
2259 .addSImm(!cast<ConstantSDNode>(N)->isNullValue());
2260 break;
2261 case MVT::i32:
2262 {
2263 int v = (int)cast<ConstantSDNode>(N)->getSignExtended();
2264 if (v < 32768 && v >= -32768) {
2265 BuildMI(BB, PPC::LI, 1, Result).addSImm(v);
2266 } else {
Nate Begeman5e966612005-03-24 06:28:42 +00002267 Tmp1 = MakeReg(MVT::i32);
2268 BuildMI(BB, PPC::LIS, 1, Tmp1).addSImm(v >> 16);
2269 BuildMI(BB, PPC::ORI, 2, Result).addReg(Tmp1).addImm(v & 0xFFFF);
Nate Begemana9795f82005-03-24 04:41:43 +00002270 }
2271 }
2272 }
2273 return Result;
Nate Begemana3fd4002005-07-19 16:51:05 +00002274
2275 case ISD::ConstantFP: {
2276 ConstantFPSDNode *CN = cast<ConstantFPSDNode>(N);
2277 Result = getConstDouble(CN->getValue(), Result);
2278 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00002279 }
2280
Nate Begemana3fd4002005-07-19 16:51:05 +00002281 case ISD::FNEG:
2282 if (!NoExcessFPPrecision &&
2283 ISD::ADD == N.getOperand(0).getOpcode() &&
2284 N.getOperand(0).Val->hasOneUse() &&
2285 ISD::MUL == N.getOperand(0).getOperand(0).getOpcode() &&
2286 N.getOperand(0).getOperand(0).Val->hasOneUse()) {
2287 ++FusedFP; // Statistic
2288 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(0));
2289 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(1));
2290 Tmp3 = SelectExpr(N.getOperand(0).getOperand(1));
2291 Opc = DestType == MVT::f64 ? PPC::FNMADD : PPC::FNMADDS;
2292 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
2293 } else if (!NoExcessFPPrecision &&
2294 ISD::ADD == N.getOperand(0).getOpcode() &&
2295 N.getOperand(0).Val->hasOneUse() &&
2296 ISD::MUL == N.getOperand(0).getOperand(1).getOpcode() &&
2297 N.getOperand(0).getOperand(1).Val->hasOneUse()) {
2298 ++FusedFP; // Statistic
2299 Tmp1 = SelectExpr(N.getOperand(0).getOperand(1).getOperand(0));
2300 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1).getOperand(1));
2301 Tmp3 = SelectExpr(N.getOperand(0).getOperand(0));
2302 Opc = DestType == MVT::f64 ? PPC::FNMADD : PPC::FNMADDS;
2303 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
2304 } else if (ISD::FABS == N.getOperand(0).getOpcode()) {
2305 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
2306 BuildMI(BB, PPC::FNABS, 1, Result).addReg(Tmp1);
2307 } else {
2308 Tmp1 = SelectExpr(N.getOperand(0));
2309 BuildMI(BB, PPC::FNEG, 1, Result).addReg(Tmp1);
2310 }
2311 return Result;
2312
2313 case ISD::FABS:
2314 Tmp1 = SelectExpr(N.getOperand(0));
2315 BuildMI(BB, PPC::FABS, 1, Result).addReg(Tmp1);
2316 return Result;
2317
Nate Begemanadeb43d2005-07-20 22:42:00 +00002318 case ISD::FSQRT:
2319 Tmp1 = SelectExpr(N.getOperand(0));
2320 Opc = DestType == MVT::f64 ? PPC::FSQRT : PPC::FSQRTS;
2321 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
2322 return Result;
2323
Nate Begemana3fd4002005-07-19 16:51:05 +00002324 case ISD::FP_ROUND:
2325 assert (DestType == MVT::f32 &&
2326 N.getOperand(0).getValueType() == MVT::f64 &&
2327 "only f64 to f32 conversion supported here");
2328 Tmp1 = SelectExpr(N.getOperand(0));
2329 BuildMI(BB, PPC::FRSP, 1, Result).addReg(Tmp1);
2330 return Result;
2331
2332 case ISD::FP_EXTEND:
2333 assert (DestType == MVT::f64 &&
2334 N.getOperand(0).getValueType() == MVT::f32 &&
2335 "only f32 to f64 conversion supported here");
2336 Tmp1 = SelectExpr(N.getOperand(0));
2337 BuildMI(BB, PPC::FMR, 1, Result).addReg(Tmp1);
2338 return Result;
2339
2340 case ISD::UINT_TO_FP:
2341 case ISD::SINT_TO_FP: {
2342 assert (N.getOperand(0).getValueType() == MVT::i32
2343 && "int to float must operate on i32");
2344 bool IsUnsigned = (ISD::UINT_TO_FP == opcode);
2345 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
2346 Tmp2 = MakeReg(MVT::f64); // temp reg to load the integer value into
2347 Tmp3 = MakeReg(MVT::i32); // temp reg to hold the conversion constant
2348
2349 int FrameIdx = BB->getParent()->getFrameInfo()->CreateStackObject(8, 8);
2350 MachineConstantPool *CP = BB->getParent()->getConstantPool();
2351
2352 if (IsUnsigned) {
2353 unsigned ConstF = getConstDouble(0x1.000000p52);
2354 // Store the hi & low halves of the fp value, currently in int regs
2355 BuildMI(BB, PPC::LIS, 1, Tmp3).addSImm(0x4330);
2356 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(Tmp3), FrameIdx);
2357 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(Tmp1), FrameIdx, 4);
2358 addFrameReference(BuildMI(BB, PPC::LFD, 2, Tmp2), FrameIdx);
2359 // Generate the return value with a subtract
2360 BuildMI(BB, PPC::FSUB, 2, Result).addReg(Tmp2).addReg(ConstF);
2361 } else {
2362 unsigned ConstF = getConstDouble(0x1.000008p52);
2363 unsigned TmpL = MakeReg(MVT::i32);
2364 // Store the hi & low halves of the fp value, currently in int regs
2365 BuildMI(BB, PPC::LIS, 1, Tmp3).addSImm(0x4330);
2366 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(Tmp3), FrameIdx);
2367 BuildMI(BB, PPC::XORIS, 2, TmpL).addReg(Tmp1).addImm(0x8000);
2368 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(TmpL), FrameIdx, 4);
2369 addFrameReference(BuildMI(BB, PPC::LFD, 2, Tmp2), FrameIdx);
2370 // Generate the return value with a subtract
2371 BuildMI(BB, PPC::FSUB, 2, Result).addReg(Tmp2).addReg(ConstF);
2372 }
2373 return Result;
2374 }
2375 }
Nate Begemana9795f82005-03-24 04:41:43 +00002376 return 0;
2377}
2378
2379void ISel::Select(SDOperand N) {
Nate Begeman2497e632005-07-21 20:44:43 +00002380 unsigned Tmp1, Tmp2, Tmp3, Opc;
Nate Begemana9795f82005-03-24 04:41:43 +00002381 unsigned opcode = N.getOpcode();
2382
2383 if (!ExprMap.insert(std::make_pair(N, 1)).second)
2384 return; // Already selected.
2385
2386 SDNode *Node = N.Val;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002387
Nate Begemana9795f82005-03-24 04:41:43 +00002388 switch (Node->getOpcode()) {
2389 default:
2390 Node->dump(); std::cerr << "\n";
2391 assert(0 && "Node not handled yet!");
2392 case ISD::EntryToken: return; // Noop
2393 case ISD::TokenFactor:
2394 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
2395 Select(Node->getOperand(i));
2396 return;
Chris Lattner16cd04d2005-05-12 23:24:06 +00002397 case ISD::CALLSEQ_START:
2398 case ISD::CALLSEQ_END:
Nate Begemana9795f82005-03-24 04:41:43 +00002399 Select(N.getOperand(0));
2400 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
Chris Lattner16cd04d2005-05-12 23:24:06 +00002401 Opc = N.getOpcode() == ISD::CALLSEQ_START ? PPC::ADJCALLSTACKDOWN :
Nate Begemana9795f82005-03-24 04:41:43 +00002402 PPC::ADJCALLSTACKUP;
2403 BuildMI(BB, Opc, 1).addImm(Tmp1);
2404 return;
2405 case ISD::BR: {
2406 MachineBasicBlock *Dest =
2407 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
Nate Begemana9795f82005-03-24 04:41:43 +00002408 Select(N.getOperand(0));
2409 BuildMI(BB, PPC::B, 1).addMBB(Dest);
2410 return;
2411 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002412 case ISD::BRCOND:
Nate Begemancd08e4c2005-04-09 20:09:12 +00002413 case ISD::BRCONDTWOWAY:
Nate Begemana9795f82005-03-24 04:41:43 +00002414 SelectBranchCC(N);
2415 return;
2416 case ISD::CopyToReg:
2417 Select(N.getOperand(0));
2418 Tmp1 = SelectExpr(N.getOperand(1));
2419 Tmp2 = cast<RegSDNode>(N)->getReg();
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002420
Nate Begemana9795f82005-03-24 04:41:43 +00002421 if (Tmp1 != Tmp2) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002422 if (N.getOperand(1).getValueType() == MVT::f64 ||
Nate Begemana9795f82005-03-24 04:41:43 +00002423 N.getOperand(1).getValueType() == MVT::f32)
2424 BuildMI(BB, PPC::FMR, 1, Tmp2).addReg(Tmp1);
2425 else
2426 BuildMI(BB, PPC::OR, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
2427 }
2428 return;
2429 case ISD::ImplicitDef:
2430 Select(N.getOperand(0));
2431 BuildMI(BB, PPC::IMPLICIT_DEF, 0, cast<RegSDNode>(N)->getReg());
2432 return;
2433 case ISD::RET:
2434 switch (N.getNumOperands()) {
2435 default:
2436 assert(0 && "Unknown return instruction!");
2437 case 3:
2438 assert(N.getOperand(1).getValueType() == MVT::i32 &&
2439 N.getOperand(2).getValueType() == MVT::i32 &&
Misha Brukman7847fca2005-04-22 17:54:37 +00002440 "Unknown two-register value!");
Nate Begemana9795f82005-03-24 04:41:43 +00002441 Select(N.getOperand(0));
2442 Tmp1 = SelectExpr(N.getOperand(1));
2443 Tmp2 = SelectExpr(N.getOperand(2));
Nate Begeman27523a12005-04-02 00:42:16 +00002444 BuildMI(BB, PPC::OR, 2, PPC::R3).addReg(Tmp2).addReg(Tmp2);
2445 BuildMI(BB, PPC::OR, 2, PPC::R4).addReg(Tmp1).addReg(Tmp1);
Nate Begemana9795f82005-03-24 04:41:43 +00002446 break;
2447 case 2:
2448 Select(N.getOperand(0));
2449 Tmp1 = SelectExpr(N.getOperand(1));
2450 switch (N.getOperand(1).getValueType()) {
2451 default:
2452 assert(0 && "Unknown return type!");
2453 case MVT::f64:
2454 case MVT::f32:
2455 BuildMI(BB, PPC::FMR, 1, PPC::F1).addReg(Tmp1);
2456 break;
2457 case MVT::i32:
2458 BuildMI(BB, PPC::OR, 2, PPC::R3).addReg(Tmp1).addReg(Tmp1);
2459 break;
2460 }
Nate Begeman9e3e1b52005-03-24 23:35:30 +00002461 case 1:
2462 Select(N.getOperand(0));
2463 break;
Nate Begemana9795f82005-03-24 04:41:43 +00002464 }
2465 BuildMI(BB, PPC::BLR, 0); // Just emit a 'ret' instruction
2466 return;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002467 case ISD::TRUNCSTORE:
Nate Begeman2497e632005-07-21 20:44:43 +00002468 case ISD::STORE: {
2469 SDOperand Chain = N.getOperand(0);
2470 SDOperand Value = N.getOperand(1);
2471 SDOperand Address = N.getOperand(2);
2472 Select(Chain);
Nate Begemana9795f82005-03-24 04:41:43 +00002473
Nate Begeman2497e632005-07-21 20:44:43 +00002474 Tmp1 = SelectExpr(Value); //value
Nate Begemana9795f82005-03-24 04:41:43 +00002475
Nate Begeman2497e632005-07-21 20:44:43 +00002476 if (opcode == ISD::STORE) {
2477 switch(Value.getValueType()) {
2478 default: assert(0 && "unknown Type in store");
2479 case MVT::i32: Opc = PPC::STW; break;
2480 case MVT::f64: Opc = PPC::STFD; break;
2481 case MVT::f32: Opc = PPC::STFS; break;
Nate Begemana9795f82005-03-24 04:41:43 +00002482 }
Nate Begeman2497e632005-07-21 20:44:43 +00002483 } else { //ISD::TRUNCSTORE
2484 switch(cast<VTSDNode>(Node->getOperand(4))->getVT()) {
2485 default: assert(0 && "unknown Type in store");
2486 case MVT::i1:
2487 case MVT::i8: Opc = PPC::STB; break;
2488 case MVT::i16: Opc = PPC::STH; break;
Nate Begemana9795f82005-03-24 04:41:43 +00002489 }
Nate Begemana9795f82005-03-24 04:41:43 +00002490 }
Nate Begeman2497e632005-07-21 20:44:43 +00002491
2492 if(Address.getOpcode() == ISD::FrameIndex) {
2493 Tmp2 = cast<FrameIndexSDNode>(Address)->getIndex();
2494 addFrameReference(BuildMI(BB, Opc, 3).addReg(Tmp1), (int)Tmp2);
2495 } else if(GlobalAddressSDNode *GN = dyn_cast<GlobalAddressSDNode>(Address)){
2496 GlobalValue *GV = GN->getGlobal();
2497 Tmp2 = MakeReg(MVT::i32);
2498 if (PICEnabled)
2499 BuildMI(BB, PPC::ADDIS, 2, Tmp2).addReg(getGlobalBaseReg())
2500 .addGlobalAddress(GV);
2501 else
2502 BuildMI(BB, PPC::LIS, 2, Tmp2).addGlobalAddress(GV);
2503 if (GV->hasWeakLinkage() || GV->isExternal()) {
2504 Tmp3 = MakeReg(MVT::i32);
2505 BuildMI(BB, PPC::LWZ, 2, Tmp3).addGlobalAddress(GV).addReg(Tmp2);
Nate Begeman7b4f0a82005-07-25 21:15:28 +00002506 BuildMI(BB, Opc, 3).addReg(Tmp1).addSImm(0).addReg(Tmp3);
2507 } else {
2508 BuildMI(BB, Opc, 3).addReg(Tmp1).addGlobalAddress(GV).addReg(Tmp2);
Nate Begeman2497e632005-07-21 20:44:43 +00002509 }
Nate Begeman2497e632005-07-21 20:44:43 +00002510 } else {
2511 int offset;
Nate Begeman2a05c8e2005-07-28 03:02:05 +00002512 switch(SelectAddr(Address, Tmp2, offset)) {
2513 default: assert(0 && "Unhandled return value from SelectAddr");
2514 case 0: // imm offset, no frame, no index
2515 BuildMI(BB, Opc, 3).addReg(Tmp1).addSImm(offset).addReg(Tmp2);
2516 break;
2517 case 1: // imm offset + frame index
2518 addFrameReference(BuildMI(BB, Opc, 3).addReg(Tmp1), (int)Tmp2, offset);
2519 break;
2520 case 2: // base+index addressing
Nate Begeman2497e632005-07-21 20:44:43 +00002521 Opc = IndexedOpForOp(Opc);
2522 BuildMI(BB, Opc, 3).addReg(Tmp1).addReg(Tmp2).addReg(offset);
Nate Begeman2a05c8e2005-07-28 03:02:05 +00002523 break;
Nate Begeman2497e632005-07-21 20:44:43 +00002524 }
2525 }
2526 return;
2527 }
Nate Begemana9795f82005-03-24 04:41:43 +00002528 case ISD::EXTLOAD:
2529 case ISD::SEXTLOAD:
2530 case ISD::ZEXTLOAD:
2531 case ISD::LOAD:
2532 case ISD::CopyFromReg:
Chris Lattnerb5d8e6e2005-05-13 20:29:26 +00002533 case ISD::TAILCALL:
Nate Begemana9795f82005-03-24 04:41:43 +00002534 case ISD::CALL:
2535 case ISD::DYNAMIC_STACKALLOC:
2536 ExprMap.erase(N);
2537 SelectExpr(N);
2538 return;
2539 }
2540 assert(0 && "Should not be reached!");
2541}
2542
2543
2544/// createPPC32PatternInstructionSelector - This pass converts an LLVM function
2545/// into a machine code representation using pattern matching and a machine
2546/// description file.
2547///
2548FunctionPass *llvm::createPPC32ISelPattern(TargetMachine &TM) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002549 return new ISel(TM);
Chris Lattner246fa632005-03-24 06:16:18 +00002550}
2551