blob: fa737e3416890f114a95e8a69e0a5236f601e617 [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);
Chris Lattner3f270132005-08-02 19:07:49 +0000574
575 virtual const char *getPassName() const {
576 return "PowerPC Pattern Instruction Selection";
577 }
Nate Begemana9795f82005-03-24 04:41:43 +0000578};
579
Nate Begeman80196b12005-04-05 00:15:08 +0000580/// ExactLog2 - This function solves for (Val == 1 << (N-1)) and returns N. It
581/// returns zero when the input is not exactly a power of two.
582static unsigned ExactLog2(unsigned Val) {
583 if (Val == 0 || (Val & (Val-1))) return 0;
584 unsigned Count = 0;
585 while (Val != 1) {
586 Val >>= 1;
587 ++Count;
588 }
589 return Count;
590}
591
Nate Begeman7ddecb42005-04-06 23:51:40 +0000592// IsRunOfOnes - returns true if Val consists of one contiguous run of 1's with
593// any number of 0's on either side. the 1's are allowed to wrap from LSB to
594// MSB. so 0x000FFF0, 0x0000FFFF, and 0xFF0000FF are all runs. 0x0F0F0000 is
595// not, since all 1's are not contiguous.
596static bool IsRunOfOnes(unsigned Val, unsigned &MB, unsigned &ME) {
597 bool isRun = true;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000598 MB = 0;
Nate Begeman7ddecb42005-04-06 23:51:40 +0000599 ME = 0;
600
601 // look for first set bit
602 int i = 0;
603 for (; i < 32; i++) {
604 if ((Val & (1 << (31 - i))) != 0) {
605 MB = i;
606 ME = i;
607 break;
608 }
609 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000610
Nate Begeman7ddecb42005-04-06 23:51:40 +0000611 // look for last set bit
612 for (; i < 32; i++) {
613 if ((Val & (1 << (31 - i))) == 0)
614 break;
615 ME = i;
616 }
617
618 // look for next set bit
619 for (; i < 32; i++) {
620 if ((Val & (1 << (31 - i))) != 0)
621 break;
622 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000623
Nate Begeman7ddecb42005-04-06 23:51:40 +0000624 // if we exhausted all the bits, we found a match at this point for 0*1*0*
625 if (i == 32)
626 return true;
627
628 // since we just encountered more 1's, if it doesn't wrap around to the
629 // most significant bit of the word, then we did not find a match to 1*0*1* so
630 // exit.
631 if (MB != 0)
632 return false;
633
634 // look for last set bit
635 for (MB = i; i < 32; i++) {
636 if ((Val & (1 << (31 - i))) == 0)
637 break;
638 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000639
Nate Begeman7ddecb42005-04-06 23:51:40 +0000640 // if we exhausted all the bits, then we found a match for 1*0*1*, otherwise,
641 // the value is not a run of ones.
642 if (i == 32)
643 return true;
644 return false;
645}
646
Nate Begeman439b4442005-04-05 04:22:58 +0000647/// getImmediateForOpcode - This method returns a value indicating whether
Nate Begemana9795f82005-03-24 04:41:43 +0000648/// the ConstantSDNode N can be used as an immediate to Opcode. The return
649/// values are either 0, 1 or 2. 0 indicates that either N is not a
Nate Begeman9f833d32005-04-12 00:10:02 +0000650/// ConstantSDNode, or is not suitable for use by that opcode.
651/// Return value codes for turning into an enum someday:
652/// 1: constant may be used in normal immediate form.
653/// 2: constant may be used in shifted immediate form.
654/// 3: log base 2 of the constant may be used.
655/// 4: constant is suitable for integer division conversion
656/// 5: constant is a bitfield mask
Nate Begemana9795f82005-03-24 04:41:43 +0000657///
Nate Begeman439b4442005-04-05 04:22:58 +0000658static unsigned getImmediateForOpcode(SDOperand N, unsigned Opcode,
659 unsigned& Imm, bool U = false) {
Nate Begemana9795f82005-03-24 04:41:43 +0000660 if (N.getOpcode() != ISD::Constant) return 0;
661
662 int v = (int)cast<ConstantSDNode>(N)->getSignExtended();
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000663
Nate Begemana9795f82005-03-24 04:41:43 +0000664 switch(Opcode) {
665 default: return 0;
666 case ISD::ADD:
667 if (v <= 32767 && v >= -32768) { Imm = v & 0xFFFF; return 1; }
668 if ((v & 0x0000FFFF) == 0) { Imm = v >> 16; return 2; }
669 break;
Nate Begeman9f833d32005-04-12 00:10:02 +0000670 case ISD::AND: {
671 unsigned MB, ME;
672 if (IsRunOfOnes(v, MB, ME)) { Imm = MB << 16 | ME & 0xFFFF; return 5; }
673 if (v >= 0 && v <= 65535) { Imm = v & 0xFFFF; return 1; }
674 if ((v & 0x0000FFFF) == 0) { Imm = v >> 16; return 2; }
675 break;
676 }
Nate Begemana9795f82005-03-24 04:41:43 +0000677 case ISD::XOR:
678 case ISD::OR:
679 if (v >= 0 && v <= 65535) { Imm = v & 0xFFFF; return 1; }
680 if ((v & 0x0000FFFF) == 0) { Imm = v >> 16; return 2; }
681 break;
Nate Begeman307e7442005-03-26 01:28:53 +0000682 case ISD::MUL:
683 if (v <= 32767 && v >= -32768) { Imm = v & 0xFFFF; return 1; }
684 break;
Nate Begemand7c4a4a2005-05-11 23:43:56 +0000685 case ISD::SUB:
686 // handle subtract-from separately from subtract, since subi is really addi
687 if (U && v <= 32767 && v >= -32768) { Imm = v & 0xFFFF; return 1; }
688 if (!U && v <= 32768 && v >= -32767) { Imm = (-v) & 0xFFFF; return 1; }
689 break;
Nate Begeman3e897162005-03-31 23:55:40 +0000690 case ISD::SETCC:
691 if (U && (v >= 0 && v <= 65535)) { Imm = v & 0xFFFF; return 1; }
692 if (!U && (v <= 32767 && v >= -32768)) { Imm = v & 0xFFFF; return 1; }
693 break;
Nate Begeman80196b12005-04-05 00:15:08 +0000694 case ISD::SDIV:
Nate Begeman439b4442005-04-05 04:22:58 +0000695 if ((Imm = ExactLog2(v))) { return 3; }
Nate Begeman9f833d32005-04-12 00:10:02 +0000696 if ((Imm = ExactLog2(-v))) { Imm = -Imm; return 3; }
Nate Begeman815d6da2005-04-06 00:25:27 +0000697 if (v <= -2 || v >= 2) { return 4; }
698 break;
699 case ISD::UDIV:
Nate Begeman27b4c232005-04-06 06:44:57 +0000700 if (v > 1) { return 4; }
Nate Begeman80196b12005-04-05 00:15:08 +0000701 break;
Nate Begemana9795f82005-03-24 04:41:43 +0000702 }
703 return 0;
704}
Nate Begeman3e897162005-03-31 23:55:40 +0000705
Nate Begemanc7bd4822005-04-11 06:34:10 +0000706/// NodeHasRecordingVariant - If SelectExpr can always produce code for
707/// NodeOpcode that also sets CR0 as a side effect, return true. Otherwise,
708/// return false.
709static bool NodeHasRecordingVariant(unsigned NodeOpcode) {
710 switch(NodeOpcode) {
711 default: return false;
712 case ISD::AND:
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000713 case ISD::OR:
Chris Lattner519f40b2005-04-13 02:46:17 +0000714 return true;
Nate Begemanc7bd4822005-04-11 06:34:10 +0000715 }
716}
717
Nate Begeman3e897162005-03-31 23:55:40 +0000718/// getBCCForSetCC - Returns the PowerPC condition branch mnemonic corresponding
719/// to Condition. If the Condition is unordered or unsigned, the bool argument
720/// U is set to true, otherwise it is set to false.
721static unsigned getBCCForSetCC(unsigned Condition, bool& U) {
722 U = false;
723 switch (Condition) {
724 default: assert(0 && "Unknown condition!"); abort();
725 case ISD::SETEQ: return PPC::BEQ;
726 case ISD::SETNE: return PPC::BNE;
727 case ISD::SETULT: U = true;
728 case ISD::SETLT: return PPC::BLT;
729 case ISD::SETULE: U = true;
730 case ISD::SETLE: return PPC::BLE;
731 case ISD::SETUGT: U = true;
732 case ISD::SETGT: return PPC::BGT;
733 case ISD::SETUGE: U = true;
734 case ISD::SETGE: return PPC::BGE;
735 }
Nate Begeman04730362005-04-01 04:45:11 +0000736 return 0;
737}
738
Nate Begeman7bfba7d2005-04-14 09:45:08 +0000739/// getCROpForOp - Return the condition register opcode (or inverted opcode)
740/// associated with the SelectionDAG opcode.
741static unsigned getCROpForSetCC(unsigned Opcode, bool Inv1, bool Inv2) {
742 switch (Opcode) {
743 default: assert(0 && "Unknown opcode!"); abort();
744 case ISD::AND:
745 if (Inv1 && Inv2) return PPC::CRNOR; // De Morgan's Law
746 if (!Inv1 && !Inv2) return PPC::CRAND;
747 if (Inv1 ^ Inv2) return PPC::CRANDC;
748 case ISD::OR:
749 if (Inv1 && Inv2) return PPC::CRNAND; // De Morgan's Law
750 if (!Inv1 && !Inv2) return PPC::CROR;
751 if (Inv1 ^ Inv2) return PPC::CRORC;
752 }
753 return 0;
754}
755
756/// getCRIdxForSetCC - Return the index of the condition register field
757/// associated with the SetCC condition, and whether or not the field is
758/// treated as inverted. That is, lt = 0; ge = 0 inverted.
759static unsigned getCRIdxForSetCC(unsigned Condition, bool& Inv) {
760 switch (Condition) {
761 default: assert(0 && "Unknown condition!"); abort();
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000762 case ISD::SETULT:
Nate Begeman7bfba7d2005-04-14 09:45:08 +0000763 case ISD::SETLT: Inv = false; return 0;
764 case ISD::SETUGE:
765 case ISD::SETGE: Inv = true; return 0;
766 case ISD::SETUGT:
767 case ISD::SETGT: Inv = false; return 1;
768 case ISD::SETULE:
769 case ISD::SETLE: Inv = true; return 1;
770 case ISD::SETEQ: Inv = false; return 2;
771 case ISD::SETNE: Inv = true; return 2;
772 }
773 return 0;
774}
775
Nate Begeman04730362005-04-01 04:45:11 +0000776/// IndexedOpForOp - Return the indexed variant for each of the PowerPC load
777/// and store immediate instructions.
778static unsigned IndexedOpForOp(unsigned Opcode) {
779 switch(Opcode) {
780 default: assert(0 && "Unknown opcode!"); abort();
781 case PPC::LBZ: return PPC::LBZX; case PPC::STB: return PPC::STBX;
782 case PPC::LHZ: return PPC::LHZX; case PPC::STH: return PPC::STHX;
783 case PPC::LHA: return PPC::LHAX; case PPC::STW: return PPC::STWX;
784 case PPC::LWZ: return PPC::LWZX; case PPC::STFS: return PPC::STFSX;
785 case PPC::LFS: return PPC::LFSX; case PPC::STFD: return PPC::STFDX;
786 case PPC::LFD: return PPC::LFDX;
787 }
788 return 0;
Nate Begeman3e897162005-03-31 23:55:40 +0000789}
Nate Begeman815d6da2005-04-06 00:25:27 +0000790
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000791// Structure used to return the necessary information to codegen an SDIV as
Nate Begeman815d6da2005-04-06 00:25:27 +0000792// a multiply.
793struct ms {
794 int m; // magic number
795 int s; // shift amount
796};
797
798struct mu {
799 unsigned int m; // magic number
800 int a; // add indicator
801 int s; // shift amount
802};
803
804/// magic - calculate the magic numbers required to codegen an integer sdiv as
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000805/// a sequence of multiply and shifts. Requires that the divisor not be 0, 1,
Nate Begeman815d6da2005-04-06 00:25:27 +0000806/// or -1.
807static struct ms magic(int d) {
808 int p;
809 unsigned int ad, anc, delta, q1, r1, q2, r2, t;
810 const unsigned int two31 = 2147483648U; // 2^31
811 struct ms mag;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000812
Nate Begeman815d6da2005-04-06 00:25:27 +0000813 ad = abs(d);
814 t = two31 + ((unsigned int)d >> 31);
815 anc = t - 1 - t%ad; // absolute value of nc
816 p = 31; // initialize p
817 q1 = two31/anc; // initialize q1 = 2p/abs(nc)
818 r1 = two31 - q1*anc; // initialize r1 = rem(2p,abs(nc))
819 q2 = two31/ad; // initialize q2 = 2p/abs(d)
820 r2 = two31 - q2*ad; // initialize r2 = rem(2p,abs(d))
821 do {
822 p = p + 1;
823 q1 = 2*q1; // update q1 = 2p/abs(nc)
824 r1 = 2*r1; // update r1 = rem(2p/abs(nc))
825 if (r1 >= anc) { // must be unsigned comparison
826 q1 = q1 + 1;
827 r1 = r1 - anc;
828 }
829 q2 = 2*q2; // update q2 = 2p/abs(d)
830 r2 = 2*r2; // update r2 = rem(2p/abs(d))
831 if (r2 >= ad) { // must be unsigned comparison
832 q2 = q2 + 1;
833 r2 = r2 - ad;
834 }
835 delta = ad - r2;
836 } while (q1 < delta || (q1 == delta && r1 == 0));
837
838 mag.m = q2 + 1;
839 if (d < 0) mag.m = -mag.m; // resulting magic number
840 mag.s = p - 32; // resulting shift
841 return mag;
842}
843
844/// magicu - calculate the magic numbers required to codegen an integer udiv as
845/// a sequence of multiply, add and shifts. Requires that the divisor not be 0.
846static struct mu magicu(unsigned d)
847{
848 int p;
849 unsigned int nc, delta, q1, r1, q2, r2;
850 struct mu magu;
851 magu.a = 0; // initialize "add" indicator
852 nc = - 1 - (-d)%d;
853 p = 31; // initialize p
854 q1 = 0x80000000/nc; // initialize q1 = 2p/nc
855 r1 = 0x80000000 - q1*nc; // initialize r1 = rem(2p,nc)
856 q2 = 0x7FFFFFFF/d; // initialize q2 = (2p-1)/d
857 r2 = 0x7FFFFFFF - q2*d; // initialize r2 = rem((2p-1),d)
858 do {
859 p = p + 1;
860 if (r1 >= nc - r1 ) {
861 q1 = 2*q1 + 1; // update q1
862 r1 = 2*r1 - nc; // update r1
863 }
864 else {
865 q1 = 2*q1; // update q1
866 r1 = 2*r1; // update r1
867 }
868 if (r2 + 1 >= d - r2) {
869 if (q2 >= 0x7FFFFFFF) magu.a = 1;
870 q2 = 2*q2 + 1; // update q2
871 r2 = 2*r2 + 1 - d; // update r2
872 }
873 else {
874 if (q2 >= 0x80000000) magu.a = 1;
875 q2 = 2*q2; // update q2
876 r2 = 2*r2 + 1; // update r2
877 }
878 delta = d - 1 - r2;
879 } while (p < 64 && (q1 < delta || (q1 == delta && r1 == 0)));
880 magu.m = q2 + 1; // resulting magic number
881 magu.s = p - 32; // resulting shift
882 return magu;
883}
884}
885
886/// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
887/// return a DAG expression to select that will generate the same value by
888/// multiplying by a magic number. See:
889/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
890SDOperand ISel::BuildSDIVSequence(SDOperand N) {
891 int d = (int)cast<ConstantSDNode>(N.getOperand(1))->getSignExtended();
892 ms magics = magic(d);
893 // Multiply the numerator (operand 0) by the magic value
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000894 SDOperand Q = ISelDAG->getNode(ISD::MULHS, MVT::i32, N.getOperand(0),
Nate Begeman815d6da2005-04-06 00:25:27 +0000895 ISelDAG->getConstant(magics.m, MVT::i32));
896 // If d > 0 and m < 0, add the numerator
897 if (d > 0 && magics.m < 0)
898 Q = ISelDAG->getNode(ISD::ADD, MVT::i32, Q, N.getOperand(0));
899 // If d < 0 and m > 0, subtract the numerator.
900 if (d < 0 && magics.m > 0)
901 Q = ISelDAG->getNode(ISD::SUB, MVT::i32, Q, N.getOperand(0));
902 // Shift right algebraic if shift value is nonzero
903 if (magics.s > 0)
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000904 Q = ISelDAG->getNode(ISD::SRA, MVT::i32, Q,
Nate Begeman815d6da2005-04-06 00:25:27 +0000905 ISelDAG->getConstant(magics.s, MVT::i32));
906 // Extract the sign bit and add it to the quotient
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000907 SDOperand T =
Nate Begeman815d6da2005-04-06 00:25:27 +0000908 ISelDAG->getNode(ISD::SRL, MVT::i32, Q, ISelDAG->getConstant(31, MVT::i32));
Nate Begeman27b4c232005-04-06 06:44:57 +0000909 return ISelDAG->getNode(ISD::ADD, MVT::i32, Q, T);
Nate Begeman815d6da2005-04-06 00:25:27 +0000910}
911
912/// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
913/// return a DAG expression to select that will generate the same value by
914/// multiplying by a magic number. See:
915/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
916SDOperand ISel::BuildUDIVSequence(SDOperand N) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000917 unsigned d =
Nate Begeman815d6da2005-04-06 00:25:27 +0000918 (unsigned)cast<ConstantSDNode>(N.getOperand(1))->getSignExtended();
919 mu magics = magicu(d);
920 // Multiply the numerator (operand 0) by the magic value
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000921 SDOperand Q = ISelDAG->getNode(ISD::MULHU, MVT::i32, N.getOperand(0),
Nate Begeman815d6da2005-04-06 00:25:27 +0000922 ISelDAG->getConstant(magics.m, MVT::i32));
923 if (magics.a == 0) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000924 Q = ISelDAG->getNode(ISD::SRL, MVT::i32, Q,
Nate Begeman815d6da2005-04-06 00:25:27 +0000925 ISelDAG->getConstant(magics.s, MVT::i32));
926 } else {
927 SDOperand NPQ = ISelDAG->getNode(ISD::SUB, MVT::i32, N.getOperand(0), Q);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000928 NPQ = ISelDAG->getNode(ISD::SRL, MVT::i32, NPQ,
Nate Begeman815d6da2005-04-06 00:25:27 +0000929 ISelDAG->getConstant(1, MVT::i32));
930 NPQ = ISelDAG->getNode(ISD::ADD, MVT::i32, NPQ, Q);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000931 Q = ISelDAG->getNode(ISD::SRL, MVT::i32, NPQ,
Nate Begeman815d6da2005-04-06 00:25:27 +0000932 ISelDAG->getConstant(magics.s-1, MVT::i32));
933 }
Nate Begeman27b4c232005-04-06 06:44:57 +0000934 return Q;
Nate Begemana9795f82005-03-24 04:41:43 +0000935}
936
Nate Begemanc7b09f12005-03-25 08:34:25 +0000937/// getGlobalBaseReg - Output the instructions required to put the
938/// base address to use for accessing globals into a register.
939///
940unsigned ISel::getGlobalBaseReg() {
941 if (!GlobalBaseInitialized) {
942 // Insert the set of GlobalBaseReg into the first MBB of the function
943 MachineBasicBlock &FirstMBB = BB->getParent()->front();
944 MachineBasicBlock::iterator MBBI = FirstMBB.begin();
945 GlobalBaseReg = MakeReg(MVT::i32);
946 BuildMI(FirstMBB, MBBI, PPC::MovePCtoLR, 0, PPC::LR);
947 BuildMI(FirstMBB, MBBI, PPC::MFLR, 1, GlobalBaseReg).addReg(PPC::LR);
948 GlobalBaseInitialized = true;
949 }
950 return GlobalBaseReg;
951}
952
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000953/// getConstDouble - Loads a floating point value into a register, via the
Nate Begeman6b559972005-04-01 02:59:27 +0000954/// Constant Pool. Optionally takes a register in which to load the value.
955unsigned ISel::getConstDouble(double doubleVal, unsigned Result=0) {
956 unsigned Tmp1 = MakeReg(MVT::i32);
957 if (0 == Result) Result = MakeReg(MVT::f64);
958 MachineConstantPool *CP = BB->getParent()->getConstantPool();
959 ConstantFP *CFP = ConstantFP::get(Type::DoubleTy, doubleVal);
960 unsigned CPI = CP->getConstantPoolIndex(CFP);
Nate Begeman2497e632005-07-21 20:44:43 +0000961 if (PICEnabled)
962 BuildMI(BB, PPC::ADDIS, 2, Tmp1).addReg(getGlobalBaseReg())
963 .addConstantPoolIndex(CPI);
964 else
965 BuildMI(BB, PPC::LIS, 1, Tmp1).addConstantPoolIndex(CPI);
Nate Begeman6b559972005-04-01 02:59:27 +0000966 BuildMI(BB, PPC::LFD, 2, Result).addConstantPoolIndex(CPI).addReg(Tmp1);
967 return Result;
968}
969
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000970/// MoveCRtoGPR - Move CCReg[Idx] to the least significant bit of Result. If
Nate Begeman1cbf3ab2005-04-18 07:48:09 +0000971/// Inv is true, then invert the result.
972void ISel::MoveCRtoGPR(unsigned CCReg, bool Inv, unsigned Idx, unsigned Result){
973 unsigned IntCR = MakeReg(MVT::i32);
974 BuildMI(BB, PPC::MCRF, 1, PPC::CR7).addReg(CCReg);
Nate Begeman2497e632005-07-21 20:44:43 +0000975 BuildMI(BB, GPOPT ? PPC::MFOCRF : PPC::MFCR, 1, IntCR).addReg(PPC::CR7);
Nate Begeman1cbf3ab2005-04-18 07:48:09 +0000976 if (Inv) {
977 unsigned Tmp1 = MakeReg(MVT::i32);
978 BuildMI(BB, PPC::RLWINM, 4, Tmp1).addReg(IntCR).addImm(32-(3-Idx))
979 .addImm(31).addImm(31);
980 BuildMI(BB, PPC::XORI, 2, Result).addReg(Tmp1).addImm(1);
981 } else {
982 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(IntCR).addImm(32-(3-Idx))
983 .addImm(31).addImm(31);
984 }
985}
986
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000987/// SelectBitfieldInsert - turn an or of two masked values into
Nate Begeman7ddecb42005-04-06 23:51:40 +0000988/// the rotate left word immediate then mask insert (rlwimi) instruction.
989/// Returns true on success, false if the caller still needs to select OR.
990///
991/// Patterns matched:
992/// 1. or shl, and 5. or and, and
993/// 2. or and, shl 6. or shl, shr
994/// 3. or shr, and 7. or shr, shl
995/// 4. or and, shr
996bool ISel::SelectBitfieldInsert(SDOperand OR, unsigned Result) {
Nate Begemancd08e4c2005-04-09 20:09:12 +0000997 bool IsRotate = false;
Nate Begeman7ddecb42005-04-06 23:51:40 +0000998 unsigned TgtMask = 0xFFFFFFFF, InsMask = 0xFFFFFFFF, Amount = 0;
Jeff Cohen00b168892005-07-27 06:12:32 +0000999
Nate Begemanb2c4bf32005-06-08 04:14:27 +00001000 SDOperand Op0 = OR.getOperand(0);
1001 SDOperand Op1 = OR.getOperand(1);
1002
1003 unsigned Op0Opc = Op0.getOpcode();
1004 unsigned Op1Opc = Op1.getOpcode();
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001005
Nate Begeman7ddecb42005-04-06 23:51:40 +00001006 // Verify that we have the correct opcodes
1007 if (ISD::SHL != Op0Opc && ISD::SRL != Op0Opc && ISD::AND != Op0Opc)
1008 return false;
1009 if (ISD::SHL != Op1Opc && ISD::SRL != Op1Opc && ISD::AND != Op1Opc)
1010 return false;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001011
Nate Begeman7ddecb42005-04-06 23:51:40 +00001012 // Generate Mask value for Target
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001013 if (ConstantSDNode *CN =
Nate Begemanb2c4bf32005-06-08 04:14:27 +00001014 dyn_cast<ConstantSDNode>(Op0.getOperand(1).Val)) {
Nate Begeman7ddecb42005-04-06 23:51:40 +00001015 switch(Op0Opc) {
1016 case ISD::SHL: TgtMask <<= (unsigned)CN->getValue(); break;
1017 case ISD::SRL: TgtMask >>= (unsigned)CN->getValue(); break;
1018 case ISD::AND: TgtMask &= (unsigned)CN->getValue(); break;
1019 }
1020 } else {
1021 return false;
1022 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001023
Nate Begeman7ddecb42005-04-06 23:51:40 +00001024 // Generate Mask value for Insert
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001025 if (ConstantSDNode *CN =
Nate Begemanb2c4bf32005-06-08 04:14:27 +00001026 dyn_cast<ConstantSDNode>(Op1.getOperand(1).Val)) {
Nate Begeman7ddecb42005-04-06 23:51:40 +00001027 switch(Op1Opc) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001028 case ISD::SHL:
1029 Amount = CN->getValue();
Nate Begemancd08e4c2005-04-09 20:09:12 +00001030 InsMask <<= Amount;
1031 if (Op0Opc == ISD::SRL) IsRotate = true;
Nate Begeman7ddecb42005-04-06 23:51:40 +00001032 break;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001033 case ISD::SRL:
1034 Amount = CN->getValue();
1035 InsMask >>= Amount;
Nate Begeman7ddecb42005-04-06 23:51:40 +00001036 Amount = 32-Amount;
Nate Begemancd08e4c2005-04-09 20:09:12 +00001037 if (Op0Opc == ISD::SHL) IsRotate = true;
Nate Begeman7ddecb42005-04-06 23:51:40 +00001038 break;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001039 case ISD::AND:
Nate Begeman7ddecb42005-04-06 23:51:40 +00001040 InsMask &= (unsigned)CN->getValue();
1041 break;
1042 }
1043 } else {
1044 return false;
1045 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001046
Nate Begemanb2c4bf32005-06-08 04:14:27 +00001047 unsigned Tmp3 = 0;
1048
1049 // If both of the inputs are ANDs and one of them has a logical shift by
1050 // constant as its input, make that the inserted value so that we can combine
1051 // the shift into the rotate part of the rlwimi instruction
1052 if (Op0Opc == ISD::AND && Op1Opc == ISD::AND) {
Jeff Cohen00b168892005-07-27 06:12:32 +00001053 if (Op1.getOperand(0).getOpcode() == ISD::SHL ||
Nate Begemanb2c4bf32005-06-08 04:14:27 +00001054 Op1.getOperand(0).getOpcode() == ISD::SRL) {
Jeff Cohen00b168892005-07-27 06:12:32 +00001055 if (ConstantSDNode *CN =
Nate Begemanb2c4bf32005-06-08 04:14:27 +00001056 dyn_cast<ConstantSDNode>(Op1.getOperand(0).getOperand(1).Val)) {
Jeff Cohen00b168892005-07-27 06:12:32 +00001057 Amount = Op1.getOperand(0).getOpcode() == ISD::SHL ?
Nate Begemanb2c4bf32005-06-08 04:14:27 +00001058 CN->getValue() : 32 - CN->getValue();
1059 Tmp3 = SelectExpr(Op1.getOperand(0).getOperand(0));
1060 }
1061 } else if (Op0.getOperand(0).getOpcode() == ISD::SHL ||
1062 Op0.getOperand(0).getOpcode() == ISD::SRL) {
Jeff Cohen00b168892005-07-27 06:12:32 +00001063 if (ConstantSDNode *CN =
Nate Begemanb2c4bf32005-06-08 04:14:27 +00001064 dyn_cast<ConstantSDNode>(Op0.getOperand(0).getOperand(1).Val)) {
1065 std::swap(Op0, Op1);
1066 std::swap(TgtMask, InsMask);
Jeff Cohen00b168892005-07-27 06:12:32 +00001067 Amount = Op1.getOperand(0).getOpcode() == ISD::SHL ?
Nate Begemanb2c4bf32005-06-08 04:14:27 +00001068 CN->getValue() : 32 - CN->getValue();
1069 Tmp3 = SelectExpr(Op1.getOperand(0).getOperand(0));
1070 }
1071 }
1072 }
1073
Nate Begeman7ddecb42005-04-06 23:51:40 +00001074 // Verify that the Target mask and Insert mask together form a full word mask
1075 // and that the Insert mask is a run of set bits (which implies both are runs
1076 // of set bits). Given that, Select the arguments and generate the rlwimi
1077 // instruction.
1078 unsigned MB, ME;
Nate Begemanb2c4bf32005-06-08 04:14:27 +00001079 if (((TgtMask & InsMask) == 0) && IsRunOfOnes(InsMask, MB, ME)) {
Nate Begeman7ddecb42005-04-06 23:51:40 +00001080 unsigned Tmp1, Tmp2;
Nate Begemanb2c4bf32005-06-08 04:14:27 +00001081 bool fullMask = (TgtMask ^ InsMask) == 0xFFFFFFFF;
Nate Begemancd08e4c2005-04-09 20:09:12 +00001082 // Check for rotlwi / rotrwi here, a special case of bitfield insert
1083 // where both bitfield halves are sourced from the same value.
Nate Begemanb2c4bf32005-06-08 04:14:27 +00001084 if (IsRotate && fullMask &&
Nate Begemancd08e4c2005-04-09 20:09:12 +00001085 OR.getOperand(0).getOperand(0) == OR.getOperand(1).getOperand(0)) {
Nate Begemancd08e4c2005-04-09 20:09:12 +00001086 Tmp1 = SelectExpr(OR.getOperand(0).getOperand(0));
1087 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(Amount)
1088 .addImm(0).addImm(31);
1089 return true;
1090 }
Nate Begemanb2c4bf32005-06-08 04:14:27 +00001091 if (Op0Opc == ISD::AND && fullMask)
1092 Tmp1 = SelectExpr(Op0.getOperand(0));
Nate Begeman7ddecb42005-04-06 23:51:40 +00001093 else
Nate Begemanb2c4bf32005-06-08 04:14:27 +00001094 Tmp1 = SelectExpr(Op0);
1095 Tmp2 = Tmp3 ? Tmp3 : SelectExpr(Op1.getOperand(0));
Nate Begeman7ddecb42005-04-06 23:51:40 +00001096 BuildMI(BB, PPC::RLWIMI, 5, Result).addReg(Tmp1).addReg(Tmp2)
1097 .addImm(Amount).addImm(MB).addImm(ME);
1098 return true;
1099 }
1100 return false;
1101}
1102
Nate Begeman3664cef2005-04-13 22:14:14 +00001103/// FoldIfWideZeroExtend - 32 bit PowerPC implicit masks shift amounts to the
1104/// low six bits. If the shift amount is an ISD::AND node with a mask that is
1105/// wider than the implicit mask, then we can get rid of the AND and let the
1106/// shift do the mask.
1107unsigned ISel::FoldIfWideZeroExtend(SDOperand N) {
1108 unsigned C;
1109 if (N.getOpcode() == ISD::AND &&
1110 5 == getImmediateForOpcode(N.getOperand(1), ISD::AND, C) && // isMask
1111 31 == (C & 0xFFFF) && // ME
1112 26 >= (C >> 16)) // MB
1113 return SelectExpr(N.getOperand(0));
1114 else
1115 return SelectExpr(N);
1116}
1117
Nate Begeman1cbf3ab2005-04-18 07:48:09 +00001118unsigned ISel::SelectCC(SDOperand CC, unsigned& Opc, bool &Inv, unsigned& Idx) {
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001119 unsigned Result, Tmp1, Tmp2;
Nate Begeman9765c252005-04-12 21:22:28 +00001120 bool AlreadySelected = false;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001121 static const unsigned CompareOpcodes[] =
Nate Begemandffcfcc2005-04-01 00:32:34 +00001122 { PPC::FCMPU, PPC::FCMPU, PPC::CMPW, PPC::CMPLW };
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001123
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001124 // Allocate a condition register for this expression
1125 Result = RegMap->createVirtualRegister(PPC32::CRRCRegisterClass);
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001126
Nate Begemandffcfcc2005-04-01 00:32:34 +00001127 // If the first operand to the select is a SETCC node, then we can fold it
1128 // into the branch that selects which value to return.
Nate Begeman16ac7092005-04-18 02:43:24 +00001129 if (SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(CC.Val)) {
Nate Begemandffcfcc2005-04-01 00:32:34 +00001130 bool U;
1131 Opc = getBCCForSetCC(SetCC->getCondition(), U);
Nate Begeman1cbf3ab2005-04-18 07:48:09 +00001132 Idx = getCRIdxForSetCC(SetCC->getCondition(), Inv);
Nate Begemandffcfcc2005-04-01 00:32:34 +00001133
Nate Begeman439b4442005-04-05 04:22:58 +00001134 // Pass the optional argument U to getImmediateForOpcode for SETCC,
Nate Begemandffcfcc2005-04-01 00:32:34 +00001135 // so that it knows whether the SETCC immediate range is signed or not.
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001136 if (1 == getImmediateForOpcode(SetCC->getOperand(1), ISD::SETCC,
Nate Begeman439b4442005-04-05 04:22:58 +00001137 Tmp2, U)) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001138 // For comparisons against zero, we can implicity set CR0 if a recording
Nate Begemanc7bd4822005-04-11 06:34:10 +00001139 // variant (e.g. 'or.' instead of 'or') of the instruction that defines
1140 // operand zero of the SetCC node is available.
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001141 if (0 == Tmp2 &&
Nate Begeman9765c252005-04-12 21:22:28 +00001142 NodeHasRecordingVariant(SetCC->getOperand(0).getOpcode()) &&
1143 SetCC->getOperand(0).Val->hasOneUse()) {
Nate Begemanc7bd4822005-04-11 06:34:10 +00001144 RecordSuccess = false;
1145 Tmp1 = SelectExpr(SetCC->getOperand(0), true);
1146 if (RecordSuccess) {
1147 ++Recorded;
Nate Begeman7bfba7d2005-04-14 09:45:08 +00001148 BuildMI(BB, PPC::MCRF, 1, Result).addReg(PPC::CR0);
1149 return Result;
Nate Begemanc7bd4822005-04-11 06:34:10 +00001150 }
1151 AlreadySelected = true;
1152 }
1153 // If we could not implicitly set CR0, then emit a compare immediate
1154 // instead.
1155 if (!AlreadySelected) Tmp1 = SelectExpr(SetCC->getOperand(0));
Nate Begemandffcfcc2005-04-01 00:32:34 +00001156 if (U)
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001157 BuildMI(BB, PPC::CMPLWI, 2, Result).addReg(Tmp1).addImm(Tmp2);
Nate Begemandffcfcc2005-04-01 00:32:34 +00001158 else
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001159 BuildMI(BB, PPC::CMPWI, 2, Result).addReg(Tmp1).addSImm(Tmp2);
Nate Begemandffcfcc2005-04-01 00:32:34 +00001160 } else {
1161 bool IsInteger = MVT::isInteger(SetCC->getOperand(0).getValueType());
1162 unsigned CompareOpc = CompareOpcodes[2 * IsInteger + U];
Nate Begemanc7bd4822005-04-11 06:34:10 +00001163 Tmp1 = SelectExpr(SetCC->getOperand(0));
Nate Begemandffcfcc2005-04-01 00:32:34 +00001164 Tmp2 = SelectExpr(SetCC->getOperand(1));
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001165 BuildMI(BB, CompareOpc, 2, Result).addReg(Tmp1).addReg(Tmp2);
Nate Begemandffcfcc2005-04-01 00:32:34 +00001166 }
1167 } else {
Nate Begeman1cbf3ab2005-04-18 07:48:09 +00001168 // If this isn't a SetCC, then select the value and compare it against zero,
1169 // treating it as if it were a boolean.
Nate Begeman9765c252005-04-12 21:22:28 +00001170 Opc = PPC::BNE;
Nate Begeman1cbf3ab2005-04-18 07:48:09 +00001171 Idx = getCRIdxForSetCC(ISD::SETNE, Inv);
Nate Begemandffcfcc2005-04-01 00:32:34 +00001172 Tmp1 = SelectExpr(CC);
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001173 BuildMI(BB, PPC::CMPLWI, 2, Result).addReg(Tmp1).addImm(0);
Nate Begemandffcfcc2005-04-01 00:32:34 +00001174 }
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001175 return Result;
Nate Begemandffcfcc2005-04-01 00:32:34 +00001176}
1177
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001178unsigned ISel::SelectCCExpr(SDOperand N, unsigned& Opc, bool &Inv,
Nate Begeman1cbf3ab2005-04-18 07:48:09 +00001179 unsigned &Idx) {
1180 bool Inv0, Inv1;
1181 unsigned Idx0, Idx1, CROpc, Opc1, Tmp1, Tmp2;
1182
1183 // Allocate a condition register for this expression
1184 unsigned Result = RegMap->createVirtualRegister(PPC32::CRRCRegisterClass);
1185
1186 // Check for the operations we support:
1187 switch(N.getOpcode()) {
1188 default:
1189 Opc = PPC::BNE;
1190 Idx = getCRIdxForSetCC(ISD::SETNE, Inv);
1191 Tmp1 = SelectExpr(N);
1192 BuildMI(BB, PPC::CMPLWI, 2, Result).addReg(Tmp1).addImm(0);
1193 break;
1194 case ISD::OR:
1195 case ISD::AND:
Nate Begeman1cbf3ab2005-04-18 07:48:09 +00001196 Tmp1 = SelectCCExpr(N.getOperand(0), Opc, Inv0, Idx0);
1197 Tmp2 = SelectCCExpr(N.getOperand(1), Opc1, Inv1, Idx1);
1198 CROpc = getCROpForSetCC(N.getOpcode(), Inv0, Inv1);
1199 if (Inv0 && !Inv1) {
1200 std::swap(Tmp1, Tmp2);
1201 std::swap(Idx0, Idx1);
1202 Opc = Opc1;
1203 }
1204 if (Inv0 && Inv1) Opc = PPC32InstrInfo::invertPPCBranchOpcode(Opc);
1205 BuildMI(BB, CROpc, 5, Result).addImm(Idx0).addReg(Tmp1).addImm(Idx0)
1206 .addReg(Tmp2).addImm(Idx1);
1207 Inv = false;
1208 Idx = Idx0;
1209 break;
1210 case ISD::SETCC:
1211 Tmp1 = SelectCC(N, Opc, Inv, Idx);
1212 Result = Tmp1;
1213 break;
1214 }
1215 return Result;
1216}
1217
Nate Begemandffcfcc2005-04-01 00:32:34 +00001218/// Check to see if the load is a constant offset from a base register
Nate Begeman2a05c8e2005-07-28 03:02:05 +00001219unsigned ISel::SelectAddr(SDOperand N, unsigned& Reg, int& offset)
Nate Begemana9795f82005-03-24 04:41:43 +00001220{
Nate Begeman96fc6812005-03-31 02:05:53 +00001221 unsigned imm = 0, opcode = N.getOpcode();
Nate Begeman04730362005-04-01 04:45:11 +00001222 if (N.getOpcode() == ISD::ADD) {
Nate Begeman2a05c8e2005-07-28 03:02:05 +00001223 bool isFrame = N.getOperand(0).getOpcode() == ISD::FrameIndex;
Nate Begeman439b4442005-04-05 04:22:58 +00001224 if (1 == getImmediateForOpcode(N.getOperand(1), opcode, imm)) {
Nate Begeman96fc6812005-03-31 02:05:53 +00001225 offset = imm;
Nate Begeman2a05c8e2005-07-28 03:02:05 +00001226 if (isFrame) {
1227 ++FrameOff;
1228 Reg = cast<FrameIndexSDNode>(N.getOperand(0))->getIndex();
1229 return 1;
1230 } else {
1231 Reg = SelectExpr(N.getOperand(0));
1232 return 0;
1233 }
1234 } else {
1235 Reg = SelectExpr(N.getOperand(0));
1236 offset = SelectExpr(N.getOperand(1));
1237 return 2;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001238 }
Nate Begeman04730362005-04-01 04:45:11 +00001239 }
Nate Begemana9795f82005-03-24 04:41:43 +00001240 Reg = SelectExpr(N);
1241 offset = 0;
Nate Begeman2a05c8e2005-07-28 03:02:05 +00001242 return 0;
Nate Begemana9795f82005-03-24 04:41:43 +00001243}
1244
1245void ISel::SelectBranchCC(SDOperand N)
1246{
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001247 MachineBasicBlock *Dest =
Nate Begemana9795f82005-03-24 04:41:43 +00001248 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
Nate Begeman3e897162005-03-31 23:55:40 +00001249
Nate Begeman1cbf3ab2005-04-18 07:48:09 +00001250 bool Inv;
1251 unsigned Opc, CCReg, Idx;
Nate Begemana9795f82005-03-24 04:41:43 +00001252 Select(N.getOperand(0)); //chain
Nate Begeman1cbf3ab2005-04-18 07:48:09 +00001253 CCReg = SelectCC(N.getOperand(1), Opc, Inv, Idx);
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001254
Nate Begeman439009c2005-06-15 18:22:43 +00001255 // Iterate to the next basic block
1256 ilist<MachineBasicBlock>::iterator It = BB;
1257 ++It;
Nate Begemancd08e4c2005-04-09 20:09:12 +00001258
1259 // If this is a two way branch, then grab the fallthrough basic block argument
1260 // and build a PowerPC branch pseudo-op, suitable for long branch conversion
1261 // if necessary by the branch selection pass. Otherwise, emit a standard
1262 // conditional branch.
1263 if (N.getOpcode() == ISD::BRCONDTWOWAY) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001264 MachineBasicBlock *Fallthrough =
Nate Begemancd08e4c2005-04-09 20:09:12 +00001265 cast<BasicBlockSDNode>(N.getOperand(3))->getBasicBlock();
1266 if (Dest != It) {
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001267 BuildMI(BB, PPC::COND_BRANCH, 4).addReg(CCReg).addImm(Opc)
Nate Begemancd08e4c2005-04-09 20:09:12 +00001268 .addMBB(Dest).addMBB(Fallthrough);
1269 if (Fallthrough != It)
1270 BuildMI(BB, PPC::B, 1).addMBB(Fallthrough);
1271 } else {
1272 if (Fallthrough != It) {
1273 Opc = PPC32InstrInfo::invertPPCBranchOpcode(Opc);
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001274 BuildMI(BB, PPC::COND_BRANCH, 4).addReg(CCReg).addImm(Opc)
Nate Begemancd08e4c2005-04-09 20:09:12 +00001275 .addMBB(Fallthrough).addMBB(Dest);
1276 }
1277 }
1278 } else {
Nate Begeman439009c2005-06-15 18:22:43 +00001279 // If the fallthrough path is off the end of the function, which would be
1280 // undefined behavior, set it to be the same as the current block because
1281 // we have nothing better to set it to, and leaving it alone will cause the
1282 // PowerPC Branch Selection pass to crash.
1283 if (It == BB->getParent()->end()) It = Dest;
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001284 BuildMI(BB, PPC::COND_BRANCH, 4).addReg(CCReg).addImm(Opc)
Nate Begeman27499e32005-04-10 01:48:29 +00001285 .addMBB(Dest).addMBB(It);
Nate Begemancd08e4c2005-04-09 20:09:12 +00001286 }
Nate Begemana9795f82005-03-24 04:41:43 +00001287 return;
1288}
1289
Nate Begemanc7bd4822005-04-11 06:34:10 +00001290unsigned ISel::SelectExpr(SDOperand N, bool Recording) {
Nate Begemana9795f82005-03-24 04:41:43 +00001291 unsigned Result;
1292 unsigned Tmp1, Tmp2, Tmp3;
1293 unsigned Opc = 0;
1294 unsigned opcode = N.getOpcode();
1295
1296 SDNode *Node = N.Val;
1297 MVT::ValueType DestType = N.getValueType();
1298
Nate Begemana43b1762005-06-14 03:55:23 +00001299 if (Node->getOpcode() == ISD::CopyFromReg &&
Chris Lattner988b1dd2005-07-28 05:23:43 +00001300 (MRegisterInfo::isVirtualRegister(cast<RegSDNode>(Node)->getReg()) ||
1301 cast<RegSDNode>(Node)->getReg() == PPC::R1))
Nate Begemana43b1762005-06-14 03:55:23 +00001302 // Just use the specified register as our input.
1303 return cast<RegSDNode>(Node)->getReg();
1304
Nate Begemana9795f82005-03-24 04:41:43 +00001305 unsigned &Reg = ExprMap[N];
1306 if (Reg) return Reg;
1307
Nate Begeman27eeb002005-04-02 05:59:34 +00001308 switch (N.getOpcode()) {
1309 default:
Nate Begemana9795f82005-03-24 04:41:43 +00001310 Reg = Result = (N.getValueType() != MVT::Other) ?
Nate Begeman27eeb002005-04-02 05:59:34 +00001311 MakeReg(N.getValueType()) : 1;
1312 break;
Chris Lattnerb5d8e6e2005-05-13 20:29:26 +00001313 case ISD::TAILCALL:
Nate Begeman27eeb002005-04-02 05:59:34 +00001314 case ISD::CALL:
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001315 // If this is a call instruction, make sure to prepare ALL of the result
1316 // values as well as the chain.
Nate Begeman27eeb002005-04-02 05:59:34 +00001317 if (Node->getNumValues() == 1)
1318 Reg = Result = 1; // Void call, just a chain.
1319 else {
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001320 Result = MakeReg(Node->getValueType(0));
1321 ExprMap[N.getValue(0)] = Result;
Nate Begeman27eeb002005-04-02 05:59:34 +00001322 for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001323 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
Nate Begeman27eeb002005-04-02 05:59:34 +00001324 ExprMap[SDOperand(Node, Node->getNumValues()-1)] = 1;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001325 }
Nate Begeman27eeb002005-04-02 05:59:34 +00001326 break;
1327 case ISD::ADD_PARTS:
1328 case ISD::SUB_PARTS:
1329 case ISD::SHL_PARTS:
1330 case ISD::SRL_PARTS:
1331 case ISD::SRA_PARTS:
1332 Result = MakeReg(Node->getValueType(0));
1333 ExprMap[N.getValue(0)] = Result;
1334 for (unsigned i = 1, e = N.Val->getNumValues(); i != e; ++i)
1335 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
1336 break;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001337 }
1338
Nate Begemana9795f82005-03-24 04:41:43 +00001339 switch (opcode) {
1340 default:
1341 Node->dump();
1342 assert(0 && "Node not handled!\n");
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001343 case ISD::UNDEF:
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001344 BuildMI(BB, PPC::IMPLICIT_DEF, 0, Result);
1345 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001346 case ISD::DYNAMIC_STACKALLOC:
Nate Begeman5e966612005-03-24 06:28:42 +00001347 // Generate both result values. FIXME: Need a better commment here?
1348 if (Result != 1)
1349 ExprMap[N.getValue(1)] = 1;
1350 else
1351 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1352
1353 // FIXME: We are currently ignoring the requested alignment for handling
1354 // greater than the stack alignment. This will need to be revisited at some
1355 // point. Align = N.getOperand(2);
1356 if (!isa<ConstantSDNode>(N.getOperand(2)) ||
1357 cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) {
1358 std::cerr << "Cannot allocate stack object with greater alignment than"
1359 << " the stack alignment yet!";
1360 abort();
1361 }
1362 Select(N.getOperand(0));
1363 Tmp1 = SelectExpr(N.getOperand(1));
1364 // Subtract size from stack pointer, thereby allocating some space.
1365 BuildMI(BB, PPC::SUBF, 2, PPC::R1).addReg(Tmp1).addReg(PPC::R1);
1366 // Put a pointer to the space into the result register by copying the SP
1367 BuildMI(BB, PPC::OR, 2, Result).addReg(PPC::R1).addReg(PPC::R1);
1368 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001369
1370 case ISD::ConstantPool:
Nate Begemanca12a2b2005-03-28 22:28:37 +00001371 Tmp1 = cast<ConstantPoolSDNode>(N)->getIndex();
1372 Tmp2 = MakeReg(MVT::i32);
Nate Begeman2497e632005-07-21 20:44:43 +00001373 if (PICEnabled)
1374 BuildMI(BB, PPC::ADDIS, 2, Tmp2).addReg(getGlobalBaseReg())
1375 .addConstantPoolIndex(Tmp1);
1376 else
1377 BuildMI(BB, PPC::LIS, 1, Tmp2).addConstantPoolIndex(Tmp1);
Nate Begemanca12a2b2005-03-28 22:28:37 +00001378 BuildMI(BB, PPC::LA, 2, Result).addReg(Tmp2).addConstantPoolIndex(Tmp1);
1379 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001380
1381 case ISD::FrameIndex:
Nate Begemanf3d08f32005-03-29 00:03:27 +00001382 Tmp1 = cast<FrameIndexSDNode>(N)->getIndex();
Nate Begeman58f718c2005-03-30 02:23:08 +00001383 addFrameReference(BuildMI(BB, PPC::ADDI, 2, Result), (int)Tmp1, 0, false);
Nate Begemanf3d08f32005-03-29 00:03:27 +00001384 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001385
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001386 case ISD::GlobalAddress: {
1387 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
Nate Begemanca12a2b2005-03-28 22:28:37 +00001388 Tmp1 = MakeReg(MVT::i32);
Nate Begeman2497e632005-07-21 20:44:43 +00001389 if (PICEnabled)
1390 BuildMI(BB, PPC::ADDIS, 2, Tmp1).addReg(getGlobalBaseReg())
1391 .addGlobalAddress(GV);
1392 else
Chris Lattner4015ea82005-07-28 04:42:11 +00001393 BuildMI(BB, PPC::LIS, 1, Tmp1).addGlobalAddress(GV);
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001394 if (GV->hasWeakLinkage() || GV->isExternal()) {
1395 BuildMI(BB, PPC::LWZ, 2, Result).addGlobalAddress(GV).addReg(Tmp1);
1396 } else {
1397 BuildMI(BB, PPC::LA, 2, Result).addReg(Tmp1).addGlobalAddress(GV);
1398 }
1399 return Result;
1400 }
1401
Nate Begeman5e966612005-03-24 06:28:42 +00001402 case ISD::LOAD:
Nate Begemana9795f82005-03-24 04:41:43 +00001403 case ISD::EXTLOAD:
1404 case ISD::ZEXTLOAD:
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001405 case ISD::SEXTLOAD: {
Nate Begeman9db505c2005-03-28 19:36:43 +00001406 MVT::ValueType TypeBeingLoaded = (ISD::LOAD == opcode) ?
Chris Lattnerbce81ae2005-07-10 01:56:13 +00001407 Node->getValueType(0) : cast<VTSDNode>(Node->getOperand(3))->getVT();
Nate Begeman74d73452005-03-31 00:15:26 +00001408 bool sext = (ISD::SEXTLOAD == opcode);
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001409
Nate Begeman5e966612005-03-24 06:28:42 +00001410 // Make sure we generate both values.
1411 if (Result != 1)
1412 ExprMap[N.getValue(1)] = 1; // Generate the token
1413 else
1414 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1415
1416 SDOperand Chain = N.getOperand(0);
1417 SDOperand Address = N.getOperand(1);
1418 Select(Chain);
1419
Nate Begeman9db505c2005-03-28 19:36:43 +00001420 switch (TypeBeingLoaded) {
Nate Begeman74d73452005-03-31 00:15:26 +00001421 default: Node->dump(); assert(0 && "Cannot load this type!");
Nate Begeman9db505c2005-03-28 19:36:43 +00001422 case MVT::i1: Opc = PPC::LBZ; break;
1423 case MVT::i8: Opc = PPC::LBZ; break;
1424 case MVT::i16: Opc = sext ? PPC::LHA : PPC::LHZ; break;
1425 case MVT::i32: Opc = PPC::LWZ; break;
Nate Begeman74d73452005-03-31 00:15:26 +00001426 case MVT::f32: Opc = PPC::LFS; break;
1427 case MVT::f64: Opc = PPC::LFD; break;
Nate Begeman5e966612005-03-24 06:28:42 +00001428 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001429
Nate Begeman74d73452005-03-31 00:15:26 +00001430 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Address)) {
1431 Tmp1 = MakeReg(MVT::i32);
1432 int CPI = CP->getIndex();
Nate Begeman2497e632005-07-21 20:44:43 +00001433 if (PICEnabled)
1434 BuildMI(BB, PPC::ADDIS, 2, Tmp1).addReg(getGlobalBaseReg())
1435 .addConstantPoolIndex(CPI);
1436 else
1437 BuildMI(BB, PPC::LIS, 1, Tmp1).addConstantPoolIndex(CPI);
Nate Begeman74d73452005-03-31 00:15:26 +00001438 BuildMI(BB, Opc, 2, Result).addConstantPoolIndex(CPI).addReg(Tmp1);
Nate Begeman2497e632005-07-21 20:44:43 +00001439 } else if (Address.getOpcode() == ISD::FrameIndex) {
Nate Begeman58f718c2005-03-30 02:23:08 +00001440 Tmp1 = cast<FrameIndexSDNode>(Address)->getIndex();
1441 addFrameReference(BuildMI(BB, Opc, 2, Result), (int)Tmp1);
Nate Begeman2497e632005-07-21 20:44:43 +00001442 } else if(GlobalAddressSDNode *GN = dyn_cast<GlobalAddressSDNode>(Address)){
1443 GlobalValue *GV = GN->getGlobal();
1444 Tmp1 = MakeReg(MVT::i32);
1445 if (PICEnabled)
1446 BuildMI(BB, PPC::ADDIS, 2, Tmp1).addReg(getGlobalBaseReg())
1447 .addGlobalAddress(GV);
1448 else
Chris Lattner4015ea82005-07-28 04:42:11 +00001449 BuildMI(BB, PPC::LIS, 1, Tmp1).addGlobalAddress(GV);
Nate Begeman2497e632005-07-21 20:44:43 +00001450 if (GV->hasWeakLinkage() || GV->isExternal()) {
1451 Tmp2 = MakeReg(MVT::i32);
1452 BuildMI(BB, PPC::LWZ, 2, Tmp2).addGlobalAddress(GV).addReg(Tmp1);
Nate Begeman7b4f0a82005-07-25 21:15:28 +00001453 BuildMI(BB, Opc, 2, Result).addSImm(0).addReg(Tmp2);
1454 } else {
1455 BuildMI(BB, Opc, 2, Result).addGlobalAddress(GV).addReg(Tmp1);
Nate Begeman2497e632005-07-21 20:44:43 +00001456 }
Nate Begeman5e966612005-03-24 06:28:42 +00001457 } else {
1458 int offset;
Nate Begeman2a05c8e2005-07-28 03:02:05 +00001459 switch(SelectAddr(Address, Tmp1, offset)) {
1460 default: assert(0 && "Unhandled return value from SelectAddr");
1461 case 0: // imm offset, no frame, no index
1462 BuildMI(BB, Opc, 2, Result).addSImm(offset).addReg(Tmp1);
1463 break;
1464 case 1: // imm offset + frame index
1465 addFrameReference(BuildMI(BB, Opc, 2, Result), (int)Tmp1, offset);
1466 break;
1467 case 2: // base+index addressing
Nate Begeman04730362005-04-01 04:45:11 +00001468 Opc = IndexedOpForOp(Opc);
1469 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(offset);
Nate Begeman2a05c8e2005-07-28 03:02:05 +00001470 break;
Nate Begeman04730362005-04-01 04:45:11 +00001471 }
Nate Begeman5e966612005-03-24 06:28:42 +00001472 }
1473 return Result;
1474 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001475
Chris Lattnerb5d8e6e2005-05-13 20:29:26 +00001476 case ISD::TAILCALL:
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001477 case ISD::CALL: {
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001478 unsigned GPR_idx = 0, FPR_idx = 0;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001479 static const unsigned GPR[] = {
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001480 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
1481 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
1482 };
1483 static const unsigned FPR[] = {
1484 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
1485 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
1486 };
1487
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001488 // Lower the chain for this call.
1489 Select(N.getOperand(0));
1490 ExprMap[N.getValue(Node->getNumValues()-1)] = 1;
Nate Begeman74d73452005-03-31 00:15:26 +00001491
Nate Begemand860aa62005-04-04 22:17:48 +00001492 MachineInstr *CallMI;
1493 // Emit the correct call instruction based on the type of symbol called.
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001494 if (GlobalAddressSDNode *GASD =
Nate Begemand860aa62005-04-04 22:17:48 +00001495 dyn_cast<GlobalAddressSDNode>(N.getOperand(1))) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001496 CallMI = BuildMI(PPC::CALLpcrel, 1).addGlobalAddress(GASD->getGlobal(),
Nate Begemand860aa62005-04-04 22:17:48 +00001497 true);
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001498 } else if (ExternalSymbolSDNode *ESSDN =
Nate Begemand860aa62005-04-04 22:17:48 +00001499 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1))) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001500 CallMI = BuildMI(PPC::CALLpcrel, 1).addExternalSymbol(ESSDN->getSymbol(),
Nate Begemand860aa62005-04-04 22:17:48 +00001501 true);
1502 } else {
1503 Tmp1 = SelectExpr(N.getOperand(1));
1504 BuildMI(BB, PPC::OR, 2, PPC::R12).addReg(Tmp1).addReg(Tmp1);
1505 BuildMI(BB, PPC::MTCTR, 1).addReg(PPC::R12);
1506 CallMI = BuildMI(PPC::CALLindirect, 3).addImm(20).addImm(0)
1507 .addReg(PPC::R12);
1508 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001509
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001510 // Load the register args to virtual regs
1511 std::vector<unsigned> ArgVR;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001512 for(int i = 2, e = Node->getNumOperands(); i < e; ++i)
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001513 ArgVR.push_back(SelectExpr(N.getOperand(i)));
1514
1515 // Copy the virtual registers into the appropriate argument register
1516 for(int i = 0, e = ArgVR.size(); i < e; ++i) {
1517 switch(N.getOperand(i+2).getValueType()) {
1518 default: Node->dump(); assert(0 && "Unknown value type for call");
1519 case MVT::i1:
1520 case MVT::i8:
1521 case MVT::i16:
1522 case MVT::i32:
1523 assert(GPR_idx < 8 && "Too many int args");
Nate Begemand860aa62005-04-04 22:17:48 +00001524 if (N.getOperand(i+2).getOpcode() != ISD::UNDEF) {
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001525 BuildMI(BB, PPC::OR,2,GPR[GPR_idx]).addReg(ArgVR[i]).addReg(ArgVR[i]);
Nate Begemand860aa62005-04-04 22:17:48 +00001526 CallMI->addRegOperand(GPR[GPR_idx], MachineOperand::Use);
1527 }
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001528 ++GPR_idx;
1529 break;
1530 case MVT::f64:
1531 case MVT::f32:
1532 assert(FPR_idx < 13 && "Too many fp args");
1533 BuildMI(BB, PPC::FMR, 1, FPR[FPR_idx]).addReg(ArgVR[i]);
Nate Begemand860aa62005-04-04 22:17:48 +00001534 CallMI->addRegOperand(FPR[FPR_idx], MachineOperand::Use);
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001535 ++FPR_idx;
1536 break;
1537 }
1538 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001539
Nate Begemand860aa62005-04-04 22:17:48 +00001540 // Put the call instruction in the correct place in the MachineBasicBlock
1541 BB->push_back(CallMI);
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001542
1543 switch (Node->getValueType(0)) {
1544 default: assert(0 && "Unknown value type for call result!");
1545 case MVT::Other: return 1;
1546 case MVT::i1:
1547 case MVT::i8:
1548 case MVT::i16:
1549 case MVT::i32:
Nate Begemane5846682005-04-04 06:52:38 +00001550 if (Node->getValueType(1) == MVT::i32) {
1551 BuildMI(BB, PPC::OR, 2, Result+1).addReg(PPC::R3).addReg(PPC::R3);
1552 BuildMI(BB, PPC::OR, 2, Result).addReg(PPC::R4).addReg(PPC::R4);
1553 } else {
1554 BuildMI(BB, PPC::OR, 2, Result).addReg(PPC::R3).addReg(PPC::R3);
1555 }
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001556 break;
1557 case MVT::f32:
1558 case MVT::f64:
1559 BuildMI(BB, PPC::FMR, 1, Result).addReg(PPC::F1);
1560 break;
1561 }
1562 return Result+N.ResNo;
1563 }
Nate Begemana9795f82005-03-24 04:41:43 +00001564
1565 case ISD::SIGN_EXTEND:
1566 case ISD::SIGN_EXTEND_INREG:
1567 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattnerbce81ae2005-07-10 01:56:13 +00001568 switch(cast<VTSDNode>(Node->getOperand(1))->getVT()) {
Nate Begeman9db505c2005-03-28 19:36:43 +00001569 default: Node->dump(); assert(0 && "Unhandled SIGN_EXTEND type"); break;
Nate Begemanc7bd4822005-04-11 06:34:10 +00001570 case MVT::i16:
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001571 BuildMI(BB, PPC::EXTSH, 1, Result).addReg(Tmp1);
Nate Begeman9db505c2005-03-28 19:36:43 +00001572 break;
Nate Begemanc7bd4822005-04-11 06:34:10 +00001573 case MVT::i8:
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001574 BuildMI(BB, PPC::EXTSB, 1, Result).addReg(Tmp1);
Nate Begeman9db505c2005-03-28 19:36:43 +00001575 break;
Nate Begeman74747862005-03-29 22:24:51 +00001576 case MVT::i1:
1577 BuildMI(BB, PPC::SUBFIC, 2, Result).addReg(Tmp1).addSImm(0);
1578 break;
Nate Begeman9db505c2005-03-28 19:36:43 +00001579 }
Nate Begemana9795f82005-03-24 04:41:43 +00001580 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001581
Nate Begemana9795f82005-03-24 04:41:43 +00001582 case ISD::CopyFromReg:
Nate Begemana3fd4002005-07-19 16:51:05 +00001583 DestType = N.getValue(0).getValueType();
Nate Begemana9795f82005-03-24 04:41:43 +00001584 if (Result == 1)
Nate Begemana3fd4002005-07-19 16:51:05 +00001585 Result = ExprMap[N.getValue(0)] = MakeReg(DestType);
Nate Begemana9795f82005-03-24 04:41:43 +00001586 Tmp1 = dyn_cast<RegSDNode>(Node)->getReg();
Nate Begemana3fd4002005-07-19 16:51:05 +00001587 if (MVT::isInteger(DestType))
1588 BuildMI(BB, PPC::OR, 2, Result).addReg(Tmp1).addReg(Tmp1);
1589 else
1590 BuildMI(BB, PPC::FMR, 1, Result).addReg(Tmp1);
Nate Begemana9795f82005-03-24 04:41:43 +00001591 return Result;
1592
1593 case ISD::SHL:
Nate Begeman5e966612005-03-24 06:28:42 +00001594 Tmp1 = SelectExpr(N.getOperand(0));
1595 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1596 Tmp2 = CN->getValue() & 0x1F;
Nate Begeman33162522005-03-29 21:54:38 +00001597 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(Tmp2).addImm(0)
Nate Begeman5e966612005-03-24 06:28:42 +00001598 .addImm(31-Tmp2);
1599 } else {
Nate Begeman3664cef2005-04-13 22:14:14 +00001600 Tmp2 = FoldIfWideZeroExtend(N.getOperand(1));
Nate Begeman5e966612005-03-24 06:28:42 +00001601 BuildMI(BB, PPC::SLW, 2, Result).addReg(Tmp1).addReg(Tmp2);
1602 }
1603 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001604
Nate Begeman5e966612005-03-24 06:28:42 +00001605 case ISD::SRL:
1606 Tmp1 = SelectExpr(N.getOperand(0));
1607 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1608 Tmp2 = CN->getValue() & 0x1F;
Nate Begeman33162522005-03-29 21:54:38 +00001609 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(32-Tmp2)
Nate Begeman5e966612005-03-24 06:28:42 +00001610 .addImm(Tmp2).addImm(31);
1611 } else {
Nate Begeman3664cef2005-04-13 22:14:14 +00001612 Tmp2 = FoldIfWideZeroExtend(N.getOperand(1));
Nate Begeman5e966612005-03-24 06:28:42 +00001613 BuildMI(BB, PPC::SRW, 2, Result).addReg(Tmp1).addReg(Tmp2);
1614 }
1615 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001616
Nate Begeman5e966612005-03-24 06:28:42 +00001617 case ISD::SRA:
1618 Tmp1 = SelectExpr(N.getOperand(0));
1619 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1620 Tmp2 = CN->getValue() & 0x1F;
1621 BuildMI(BB, PPC::SRAWI, 2, Result).addReg(Tmp1).addImm(Tmp2);
1622 } else {
Nate Begeman3664cef2005-04-13 22:14:14 +00001623 Tmp2 = FoldIfWideZeroExtend(N.getOperand(1));
Nate Begeman5e966612005-03-24 06:28:42 +00001624 BuildMI(BB, PPC::SRAW, 2, Result).addReg(Tmp1).addReg(Tmp2);
1625 }
1626 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001627
Nate Begemand7c4a4a2005-05-11 23:43:56 +00001628 case ISD::CTLZ:
1629 Tmp1 = SelectExpr(N.getOperand(0));
1630 BuildMI(BB, PPC::CNTLZW, 1, Result).addReg(Tmp1);
1631 return Result;
1632
Nate Begemana9795f82005-03-24 04:41:43 +00001633 case ISD::ADD:
Nate Begemana3fd4002005-07-19 16:51:05 +00001634 if (!MVT::isInteger(DestType)) {
1635 if (!NoExcessFPPrecision && N.getOperand(0).getOpcode() == ISD::MUL &&
1636 N.getOperand(0).Val->hasOneUse()) {
1637 ++FusedFP; // Statistic
1638 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1639 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1640 Tmp3 = SelectExpr(N.getOperand(1));
1641 Opc = DestType == MVT::f64 ? PPC::FMADD : PPC::FMADDS;
1642 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1643 return Result;
1644 }
1645 if (!NoExcessFPPrecision && N.getOperand(1).getOpcode() == ISD::MUL &&
1646 N.getOperand(1).Val->hasOneUse()) {
1647 ++FusedFP; // Statistic
1648 Tmp1 = SelectExpr(N.getOperand(1).getOperand(0));
1649 Tmp2 = SelectExpr(N.getOperand(1).getOperand(1));
1650 Tmp3 = SelectExpr(N.getOperand(0));
1651 Opc = DestType == MVT::f64 ? PPC::FMADD : PPC::FMADDS;
1652 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1653 return Result;
1654 }
1655 Opc = DestType == MVT::f64 ? PPC::FADD : PPC::FADDS;
1656 Tmp1 = SelectExpr(N.getOperand(0));
1657 Tmp2 = SelectExpr(N.getOperand(1));
1658 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1659 return Result;
1660 }
Nate Begemana9795f82005-03-24 04:41:43 +00001661 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman439b4442005-04-05 04:22:58 +00001662 switch(getImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
Nate Begemana9795f82005-03-24 04:41:43 +00001663 default: assert(0 && "unhandled result code");
1664 case 0: // No immediate
1665 Tmp2 = SelectExpr(N.getOperand(1));
1666 BuildMI(BB, PPC::ADD, 2, Result).addReg(Tmp1).addReg(Tmp2);
1667 break;
1668 case 1: // Low immediate
1669 BuildMI(BB, PPC::ADDI, 2, Result).addReg(Tmp1).addSImm(Tmp2);
1670 break;
1671 case 2: // Shifted immediate
1672 BuildMI(BB, PPC::ADDIS, 2, Result).addReg(Tmp1).addSImm(Tmp2);
1673 break;
1674 }
1675 return Result;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001676
Nate Begemana9795f82005-03-24 04:41:43 +00001677 case ISD::AND:
Nate Begeman7ddecb42005-04-06 23:51:40 +00001678 switch(getImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
1679 default: assert(0 && "unhandled result code");
1680 case 0: // No immediate
Nate Begemand7c4a4a2005-05-11 23:43:56 +00001681 // Check for andc: and, (xor a, -1), b
1682 if (N.getOperand(0).getOpcode() == ISD::XOR &&
1683 N.getOperand(0).getOperand(1).getOpcode() == ISD::Constant &&
1684 cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->isAllOnesValue()) {
1685 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1686 Tmp2 = SelectExpr(N.getOperand(1));
1687 BuildMI(BB, PPC::ANDC, 2, Result).addReg(Tmp2).addReg(Tmp1);
1688 return Result;
1689 }
1690 // It wasn't and-with-complement, emit a regular and
Chris Lattnercafb67b2005-05-09 17:39:48 +00001691 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman7ddecb42005-04-06 23:51:40 +00001692 Tmp2 = SelectExpr(N.getOperand(1));
Nate Begemanc7bd4822005-04-11 06:34:10 +00001693 Opc = Recording ? PPC::ANDo : PPC::AND;
1694 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
Nate Begeman7ddecb42005-04-06 23:51:40 +00001695 break;
1696 case 1: // Low immediate
Chris Lattnercafb67b2005-05-09 17:39:48 +00001697 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman7ddecb42005-04-06 23:51:40 +00001698 BuildMI(BB, PPC::ANDIo, 2, Result).addReg(Tmp1).addImm(Tmp2);
1699 break;
1700 case 2: // Shifted immediate
Chris Lattnercafb67b2005-05-09 17:39:48 +00001701 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman7ddecb42005-04-06 23:51:40 +00001702 BuildMI(BB, PPC::ANDISo, 2, Result).addReg(Tmp1).addImm(Tmp2);
1703 break;
Nate Begeman9f833d32005-04-12 00:10:02 +00001704 case 5: // Bitfield mask
1705 Opc = Recording ? PPC::RLWINMo : PPC::RLWINM;
1706 Tmp3 = Tmp2 >> 16; // MB
1707 Tmp2 &= 0xFFFF; // ME
Chris Lattnercafb67b2005-05-09 17:39:48 +00001708
Nate Begeman3dee1752005-07-27 23:11:27 +00001709 // FIXME: Catch SHL-AND in addition to SRL-AND in this block.
Chris Lattnercafb67b2005-05-09 17:39:48 +00001710 if (N.getOperand(0).getOpcode() == ISD::SRL)
1711 if (ConstantSDNode *SA =
1712 dyn_cast<ConstantSDNode>(N.getOperand(0).getOperand(1))) {
1713
1714 // We can fold the RLWINM and the SRL together if the mask is
1715 // clearing the top bits which are rotated around.
1716 unsigned RotAmt = 32-(SA->getValue() & 31);
1717 if (Tmp2 <= RotAmt) {
1718 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1719 BuildMI(BB, Opc, 4, Result).addReg(Tmp1).addImm(RotAmt)
1720 .addImm(Tmp3).addImm(Tmp2);
1721 break;
1722 }
1723 }
1724
1725 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman9f833d32005-04-12 00:10:02 +00001726 BuildMI(BB, Opc, 4, Result).addReg(Tmp1).addImm(0)
1727 .addImm(Tmp3).addImm(Tmp2);
1728 break;
Nate Begeman7ddecb42005-04-06 23:51:40 +00001729 }
Nate Begemanc7bd4822005-04-11 06:34:10 +00001730 RecordSuccess = true;
Nate Begeman7ddecb42005-04-06 23:51:40 +00001731 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001732
Nate Begemana9795f82005-03-24 04:41:43 +00001733 case ISD::OR:
Nate Begeman7ddecb42005-04-06 23:51:40 +00001734 if (SelectBitfieldInsert(N, Result))
1735 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001736 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman439b4442005-04-05 04:22:58 +00001737 switch(getImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
Nate Begemana9795f82005-03-24 04:41:43 +00001738 default: assert(0 && "unhandled result code");
1739 case 0: // No immediate
1740 Tmp2 = SelectExpr(N.getOperand(1));
Nate Begemanc7bd4822005-04-11 06:34:10 +00001741 Opc = Recording ? PPC::ORo : PPC::OR;
1742 RecordSuccess = true;
1743 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +00001744 break;
1745 case 1: // Low immediate
Nate Begeman7ddecb42005-04-06 23:51:40 +00001746 BuildMI(BB, PPC::ORI, 2, Result).addReg(Tmp1).addImm(Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +00001747 break;
1748 case 2: // Shifted immediate
Nate Begeman7ddecb42005-04-06 23:51:40 +00001749 BuildMI(BB, PPC::ORIS, 2, Result).addReg(Tmp1).addImm(Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +00001750 break;
1751 }
1752 return Result;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001753
Nate Begemanaa73a9f2005-04-03 11:20:20 +00001754 case ISD::XOR: {
1755 // Check for EQV: xor, (xor a, -1), b
1756 if (N.getOperand(0).getOpcode() == ISD::XOR &&
1757 N.getOperand(0).getOperand(1).getOpcode() == ISD::Constant &&
1758 cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->isAllOnesValue()) {
Nate Begemanaa73a9f2005-04-03 11:20:20 +00001759 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1760 Tmp2 = SelectExpr(N.getOperand(1));
1761 BuildMI(BB, PPC::EQV, 2, Result).addReg(Tmp1).addReg(Tmp2);
1762 return Result;
1763 }
Chris Lattner837a5212005-04-21 21:09:11 +00001764 // Check for NOT, NOR, EQV, and NAND: xor (copy, or, xor, and), -1
Nate Begemanaa73a9f2005-04-03 11:20:20 +00001765 if (N.getOperand(1).getOpcode() == ISD::Constant &&
1766 cast<ConstantSDNode>(N.getOperand(1))->isAllOnesValue()) {
Nate Begemanaa73a9f2005-04-03 11:20:20 +00001767 switch(N.getOperand(0).getOpcode()) {
1768 case ISD::OR:
1769 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1770 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1771 BuildMI(BB, PPC::NOR, 2, Result).addReg(Tmp1).addReg(Tmp2);
1772 break;
1773 case ISD::AND:
1774 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1775 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1776 BuildMI(BB, PPC::NAND, 2, Result).addReg(Tmp1).addReg(Tmp2);
1777 break;
Chris Lattner837a5212005-04-21 21:09:11 +00001778 case ISD::XOR:
1779 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1780 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1781 BuildMI(BB, PPC::EQV, 2, Result).addReg(Tmp1).addReg(Tmp2);
1782 break;
Nate Begemanaa73a9f2005-04-03 11:20:20 +00001783 default:
1784 Tmp1 = SelectExpr(N.getOperand(0));
1785 BuildMI(BB, PPC::NOR, 2, Result).addReg(Tmp1).addReg(Tmp1);
1786 break;
1787 }
1788 return Result;
1789 }
1790 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman439b4442005-04-05 04:22:58 +00001791 switch(getImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
Nate Begemanaa73a9f2005-04-03 11:20:20 +00001792 default: assert(0 && "unhandled result code");
1793 case 0: // No immediate
1794 Tmp2 = SelectExpr(N.getOperand(1));
1795 BuildMI(BB, PPC::XOR, 2, Result).addReg(Tmp1).addReg(Tmp2);
1796 break;
1797 case 1: // Low immediate
1798 BuildMI(BB, PPC::XORI, 2, Result).addReg(Tmp1).addImm(Tmp2);
1799 break;
1800 case 2: // Shifted immediate
1801 BuildMI(BB, PPC::XORIS, 2, Result).addReg(Tmp1).addImm(Tmp2);
1802 break;
1803 }
1804 return Result;
1805 }
1806
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001807 case ISD::SUB:
Nate Begemana3fd4002005-07-19 16:51:05 +00001808 if (!MVT::isInteger(DestType)) {
1809 if (!NoExcessFPPrecision && N.getOperand(0).getOpcode() == ISD::MUL &&
1810 N.getOperand(0).Val->hasOneUse()) {
1811 ++FusedFP; // Statistic
1812 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1813 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1814 Tmp3 = SelectExpr(N.getOperand(1));
1815 Opc = DestType == MVT::f64 ? PPC::FMSUB : PPC::FMSUBS;
1816 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1817 return Result;
1818 }
1819 if (!NoExcessFPPrecision && N.getOperand(1).getOpcode() == ISD::MUL &&
1820 N.getOperand(1).Val->hasOneUse()) {
1821 ++FusedFP; // Statistic
1822 Tmp1 = SelectExpr(N.getOperand(1).getOperand(0));
1823 Tmp2 = SelectExpr(N.getOperand(1).getOperand(1));
1824 Tmp3 = SelectExpr(N.getOperand(0));
1825 Opc = DestType == MVT::f64 ? PPC::FNMSUB : PPC::FNMSUBS;
1826 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1827 return Result;
1828 }
1829 Opc = DestType == MVT::f64 ? PPC::FSUB : PPC::FSUBS;
1830 Tmp1 = SelectExpr(N.getOperand(0));
1831 Tmp2 = SelectExpr(N.getOperand(1));
1832 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1833 return Result;
1834 }
Nate Begemand7c4a4a2005-05-11 23:43:56 +00001835 if (1 == getImmediateForOpcode(N.getOperand(0), opcode, Tmp1, true)) {
1836 Tmp2 = SelectExpr(N.getOperand(1));
Nate Begeman27523a12005-04-02 00:42:16 +00001837 BuildMI(BB, PPC::SUBFIC, 2, Result).addReg(Tmp2).addSImm(Tmp1);
Nate Begemand7c4a4a2005-05-11 23:43:56 +00001838 } else if (1 == getImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) {
Nate Begeman27523a12005-04-02 00:42:16 +00001839 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begemand7c4a4a2005-05-11 23:43:56 +00001840 BuildMI(BB, PPC::ADDI, 2, Result).addReg(Tmp1).addSImm(Tmp2);
1841 } else {
1842 Tmp1 = SelectExpr(N.getOperand(0));
1843 Tmp2 = SelectExpr(N.getOperand(1));
Nate Begeman27523a12005-04-02 00:42:16 +00001844 BuildMI(BB, PPC::SUBF, 2, Result).addReg(Tmp2).addReg(Tmp1);
1845 }
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001846 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001847
Nate Begeman5e966612005-03-24 06:28:42 +00001848 case ISD::MUL:
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001849 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman439b4442005-04-05 04:22:58 +00001850 if (1 == getImmediateForOpcode(N.getOperand(1), opcode, Tmp2))
Nate Begeman307e7442005-03-26 01:28:53 +00001851 BuildMI(BB, PPC::MULLI, 2, Result).addReg(Tmp1).addSImm(Tmp2);
1852 else {
1853 Tmp2 = SelectExpr(N.getOperand(1));
Nate Begemana3fd4002005-07-19 16:51:05 +00001854 switch (DestType) {
1855 default: assert(0 && "Unknown type to ISD::MUL"); break;
1856 case MVT::i32: Opc = PPC::MULLW; break;
1857 case MVT::f32: Opc = PPC::FMULS; break;
1858 case MVT::f64: Opc = PPC::FMUL; break;
1859 }
1860 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
Nate Begeman307e7442005-03-26 01:28:53 +00001861 }
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001862 return Result;
1863
Nate Begeman815d6da2005-04-06 00:25:27 +00001864 case ISD::MULHS:
1865 case ISD::MULHU:
1866 Tmp1 = SelectExpr(N.getOperand(0));
1867 Tmp2 = SelectExpr(N.getOperand(1));
1868 Opc = (ISD::MULHU == opcode) ? PPC::MULHWU : PPC::MULHW;
1869 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1870 return Result;
1871
Nate Begemanf3d08f32005-03-29 00:03:27 +00001872 case ISD::SDIV:
1873 case ISD::UDIV:
Nate Begeman815d6da2005-04-06 00:25:27 +00001874 switch (getImmediateForOpcode(N.getOperand(1), opcode, Tmp3)) {
1875 default: break;
1876 // If this is an sdiv by a power of two, we can use an srawi/addze pair.
1877 case 3:
Nate Begeman80196b12005-04-05 00:15:08 +00001878 Tmp1 = MakeReg(MVT::i32);
1879 Tmp2 = SelectExpr(N.getOperand(0));
Nate Begeman9f833d32005-04-12 00:10:02 +00001880 if ((int)Tmp3 < 0) {
1881 unsigned Tmp4 = MakeReg(MVT::i32);
1882 BuildMI(BB, PPC::SRAWI, 2, Tmp1).addReg(Tmp2).addImm(-Tmp3);
1883 BuildMI(BB, PPC::ADDZE, 1, Tmp4).addReg(Tmp1);
1884 BuildMI(BB, PPC::NEG, 1, Result).addReg(Tmp4);
1885 } else {
1886 BuildMI(BB, PPC::SRAWI, 2, Tmp1).addReg(Tmp2).addImm(Tmp3);
1887 BuildMI(BB, PPC::ADDZE, 1, Result).addReg(Tmp1);
1888 }
Nate Begeman80196b12005-04-05 00:15:08 +00001889 return Result;
Nate Begeman815d6da2005-04-06 00:25:27 +00001890 // If this is a divide by constant, we can emit code using some magic
1891 // constants to implement it as a multiply instead.
Nate Begeman27b4c232005-04-06 06:44:57 +00001892 case 4:
1893 ExprMap.erase(N);
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001894 if (opcode == ISD::SDIV)
Nate Begeman27b4c232005-04-06 06:44:57 +00001895 return SelectExpr(BuildSDIVSequence(N));
1896 else
1897 return SelectExpr(BuildUDIVSequence(N));
Jeff Cohen00b168892005-07-27 06:12:32 +00001898 }
Nate Begemanf3d08f32005-03-29 00:03:27 +00001899 Tmp1 = SelectExpr(N.getOperand(0));
1900 Tmp2 = SelectExpr(N.getOperand(1));
Nate Begemana3fd4002005-07-19 16:51:05 +00001901 switch (DestType) {
1902 default: assert(0 && "Unknown type to ISD::SDIV"); break;
1903 case MVT::i32: Opc = (ISD::UDIV == opcode) ? PPC::DIVWU : PPC::DIVW; break;
1904 case MVT::f32: Opc = PPC::FDIVS; break;
1905 case MVT::f64: Opc = PPC::FDIV; break;
1906 }
Nate Begemanf3d08f32005-03-29 00:03:27 +00001907 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1908 return Result;
1909
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001910 case ISD::ADD_PARTS:
Nate Begemanca12a2b2005-03-28 22:28:37 +00001911 case ISD::SUB_PARTS: {
1912 assert(N.getNumOperands() == 4 && N.getValueType() == MVT::i32 &&
1913 "Not an i64 add/sub!");
1914 // Emit all of the operands.
1915 std::vector<unsigned> InVals;
1916 for (unsigned i = 0, e = N.getNumOperands(); i != e; ++i)
1917 InVals.push_back(SelectExpr(N.getOperand(i)));
1918 if (N.getOpcode() == ISD::ADD_PARTS) {
Nate Begeman27eeb002005-04-02 05:59:34 +00001919 BuildMI(BB, PPC::ADDC, 2, Result).addReg(InVals[0]).addReg(InVals[2]);
1920 BuildMI(BB, PPC::ADDE, 2, Result+1).addReg(InVals[1]).addReg(InVals[3]);
Nate Begemanca12a2b2005-03-28 22:28:37 +00001921 } else {
Nate Begeman27eeb002005-04-02 05:59:34 +00001922 BuildMI(BB, PPC::SUBFC, 2, Result).addReg(InVals[2]).addReg(InVals[0]);
1923 BuildMI(BB, PPC::SUBFE, 2, Result+1).addReg(InVals[3]).addReg(InVals[1]);
1924 }
1925 return Result+N.ResNo;
1926 }
1927
1928 case ISD::SHL_PARTS:
1929 case ISD::SRA_PARTS:
1930 case ISD::SRL_PARTS: {
1931 assert(N.getNumOperands() == 3 && N.getValueType() == MVT::i32 &&
1932 "Not an i64 shift!");
1933 unsigned ShiftOpLo = SelectExpr(N.getOperand(0));
1934 unsigned ShiftOpHi = SelectExpr(N.getOperand(1));
Nate Begeman3664cef2005-04-13 22:14:14 +00001935 unsigned SHReg = FoldIfWideZeroExtend(N.getOperand(2));
1936 Tmp1 = MakeReg(MVT::i32);
1937 Tmp2 = MakeReg(MVT::i32);
Nate Begeman27eeb002005-04-02 05:59:34 +00001938 Tmp3 = MakeReg(MVT::i32);
1939 unsigned Tmp4 = MakeReg(MVT::i32);
1940 unsigned Tmp5 = MakeReg(MVT::i32);
1941 unsigned Tmp6 = MakeReg(MVT::i32);
1942 BuildMI(BB, PPC::SUBFIC, 2, Tmp1).addReg(SHReg).addSImm(32);
1943 if (ISD::SHL_PARTS == opcode) {
1944 BuildMI(BB, PPC::SLW, 2, Tmp2).addReg(ShiftOpHi).addReg(SHReg);
1945 BuildMI(BB, PPC::SRW, 2, Tmp3).addReg(ShiftOpLo).addReg(Tmp1);
1946 BuildMI(BB, PPC::OR, 2, Tmp4).addReg(Tmp2).addReg(Tmp3);
1947 BuildMI(BB, PPC::ADDI, 2, Tmp5).addReg(SHReg).addSImm(-32);
Nate Begemanfa554702005-04-03 22:13:27 +00001948 BuildMI(BB, PPC::SLW, 2, Tmp6).addReg(ShiftOpLo).addReg(Tmp5);
Nate Begeman27eeb002005-04-02 05:59:34 +00001949 BuildMI(BB, PPC::OR, 2, Result+1).addReg(Tmp4).addReg(Tmp6);
1950 BuildMI(BB, PPC::SLW, 2, Result).addReg(ShiftOpLo).addReg(SHReg);
1951 } else if (ISD::SRL_PARTS == opcode) {
1952 BuildMI(BB, PPC::SRW, 2, Tmp2).addReg(ShiftOpLo).addReg(SHReg);
1953 BuildMI(BB, PPC::SLW, 2, Tmp3).addReg(ShiftOpHi).addReg(Tmp1);
1954 BuildMI(BB, PPC::OR, 2, Tmp4).addReg(Tmp2).addReg(Tmp3);
1955 BuildMI(BB, PPC::ADDI, 2, Tmp5).addReg(SHReg).addSImm(-32);
1956 BuildMI(BB, PPC::SRW, 2, Tmp6).addReg(ShiftOpHi).addReg(Tmp5);
1957 BuildMI(BB, PPC::OR, 2, Result).addReg(Tmp4).addReg(Tmp6);
1958 BuildMI(BB, PPC::SRW, 2, Result+1).addReg(ShiftOpHi).addReg(SHReg);
1959 } else {
1960 MachineBasicBlock *TmpMBB = new MachineBasicBlock(BB->getBasicBlock());
1961 MachineBasicBlock *PhiMBB = new MachineBasicBlock(BB->getBasicBlock());
1962 MachineBasicBlock *OldMBB = BB;
1963 MachineFunction *F = BB->getParent();
1964 ilist<MachineBasicBlock>::iterator It = BB; ++It;
1965 F->getBasicBlockList().insert(It, TmpMBB);
1966 F->getBasicBlockList().insert(It, PhiMBB);
1967 BB->addSuccessor(TmpMBB);
1968 BB->addSuccessor(PhiMBB);
1969 BuildMI(BB, PPC::SRW, 2, Tmp2).addReg(ShiftOpLo).addReg(SHReg);
1970 BuildMI(BB, PPC::SLW, 2, Tmp3).addReg(ShiftOpHi).addReg(Tmp1);
1971 BuildMI(BB, PPC::OR, 2, Tmp4).addReg(Tmp2).addReg(Tmp3);
1972 BuildMI(BB, PPC::ADDICo, 2, Tmp5).addReg(SHReg).addSImm(-32);
1973 BuildMI(BB, PPC::SRAW, 2, Tmp6).addReg(ShiftOpHi).addReg(Tmp5);
1974 BuildMI(BB, PPC::SRAW, 2, Result+1).addReg(ShiftOpHi).addReg(SHReg);
1975 BuildMI(BB, PPC::BLE, 2).addReg(PPC::CR0).addMBB(PhiMBB);
1976 // Select correct least significant half if the shift amount > 32
1977 BB = TmpMBB;
1978 unsigned Tmp7 = MakeReg(MVT::i32);
1979 BuildMI(BB, PPC::OR, 2, Tmp7).addReg(Tmp6).addReg(Tmp6);
1980 TmpMBB->addSuccessor(PhiMBB);
1981 BB = PhiMBB;
1982 BuildMI(BB, PPC::PHI, 4, Result).addReg(Tmp4).addMBB(OldMBB)
1983 .addReg(Tmp7).addMBB(TmpMBB);
Nate Begemanca12a2b2005-03-28 22:28:37 +00001984 }
1985 return Result+N.ResNo;
1986 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001987
Nate Begemana9795f82005-03-24 04:41:43 +00001988 case ISD::FP_TO_UINT:
Nate Begeman6b559972005-04-01 02:59:27 +00001989 case ISD::FP_TO_SINT: {
1990 bool U = (ISD::FP_TO_UINT == opcode);
1991 Tmp1 = SelectExpr(N.getOperand(0));
1992 if (!U) {
1993 Tmp2 = MakeReg(MVT::f64);
1994 BuildMI(BB, PPC::FCTIWZ, 1, Tmp2).addReg(Tmp1);
1995 int FrameIdx = BB->getParent()->getFrameInfo()->CreateStackObject(8, 8);
1996 addFrameReference(BuildMI(BB, PPC::STFD, 3).addReg(Tmp2), FrameIdx);
1997 addFrameReference(BuildMI(BB, PPC::LWZ, 2, Result), FrameIdx, 4);
1998 return Result;
1999 } else {
2000 unsigned Zero = getConstDouble(0.0);
2001 unsigned MaxInt = getConstDouble((1LL << 32) - 1);
2002 unsigned Border = getConstDouble(1LL << 31);
2003 unsigned UseZero = MakeReg(MVT::f64);
2004 unsigned UseMaxInt = MakeReg(MVT::f64);
2005 unsigned UseChoice = MakeReg(MVT::f64);
2006 unsigned TmpReg = MakeReg(MVT::f64);
2007 unsigned TmpReg2 = MakeReg(MVT::f64);
2008 unsigned ConvReg = MakeReg(MVT::f64);
2009 unsigned IntTmp = MakeReg(MVT::i32);
2010 unsigned XorReg = MakeReg(MVT::i32);
2011 MachineFunction *F = BB->getParent();
2012 int FrameIdx = F->getFrameInfo()->CreateStackObject(8, 8);
2013 // Update machine-CFG edges
2014 MachineBasicBlock *XorMBB = new MachineBasicBlock(BB->getBasicBlock());
2015 MachineBasicBlock *PhiMBB = new MachineBasicBlock(BB->getBasicBlock());
2016 MachineBasicBlock *OldMBB = BB;
2017 ilist<MachineBasicBlock>::iterator It = BB; ++It;
2018 F->getBasicBlockList().insert(It, XorMBB);
2019 F->getBasicBlockList().insert(It, PhiMBB);
2020 BB->addSuccessor(XorMBB);
2021 BB->addSuccessor(PhiMBB);
2022 // Convert from floating point to unsigned 32-bit value
2023 // Use 0 if incoming value is < 0.0
2024 BuildMI(BB, PPC::FSEL, 3, UseZero).addReg(Tmp1).addReg(Tmp1).addReg(Zero);
2025 // Use 2**32 - 1 if incoming value is >= 2**32
2026 BuildMI(BB, PPC::FSUB, 2, UseMaxInt).addReg(MaxInt).addReg(Tmp1);
2027 BuildMI(BB, PPC::FSEL, 3, UseChoice).addReg(UseMaxInt).addReg(UseZero)
2028 .addReg(MaxInt);
2029 // Subtract 2**31
2030 BuildMI(BB, PPC::FSUB, 2, TmpReg).addReg(UseChoice).addReg(Border);
2031 // Use difference if >= 2**31
2032 BuildMI(BB, PPC::FCMPU, 2, PPC::CR0).addReg(UseChoice).addReg(Border);
2033 BuildMI(BB, PPC::FSEL, 3, TmpReg2).addReg(TmpReg).addReg(TmpReg)
2034 .addReg(UseChoice);
2035 // Convert to integer
2036 BuildMI(BB, PPC::FCTIWZ, 1, ConvReg).addReg(TmpReg2);
2037 addFrameReference(BuildMI(BB, PPC::STFD, 3).addReg(ConvReg), FrameIdx);
2038 addFrameReference(BuildMI(BB, PPC::LWZ, 2, IntTmp), FrameIdx, 4);
2039 BuildMI(BB, PPC::BLT, 2).addReg(PPC::CR0).addMBB(PhiMBB);
2040 BuildMI(BB, PPC::B, 1).addMBB(XorMBB);
2041
2042 // XorMBB:
2043 // add 2**31 if input was >= 2**31
2044 BB = XorMBB;
2045 BuildMI(BB, PPC::XORIS, 2, XorReg).addReg(IntTmp).addImm(0x8000);
2046 XorMBB->addSuccessor(PhiMBB);
2047
2048 // PhiMBB:
2049 // DestReg = phi [ IntTmp, OldMBB ], [ XorReg, XorMBB ]
2050 BB = PhiMBB;
2051 BuildMI(BB, PPC::PHI, 4, Result).addReg(IntTmp).addMBB(OldMBB)
2052 .addReg(XorReg).addMBB(XorMBB);
2053 return Result;
2054 }
2055 assert(0 && "Should never get here");
2056 return 0;
2057 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002058
Nate Begeman9e3e1b52005-03-24 23:35:30 +00002059 case ISD::SETCC:
Nate Begeman33162522005-03-29 21:54:38 +00002060 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Node)) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002061 if (ConstantSDNode *CN =
Nate Begeman7e7fadd2005-04-07 20:30:01 +00002062 dyn_cast<ConstantSDNode>(SetCC->getOperand(1).Val)) {
Nate Begeman9765c252005-04-12 21:22:28 +00002063 // We can codegen setcc op, imm very efficiently compared to a brcond.
2064 // Check for those cases here.
2065 // setcc op, 0
Nate Begeman7e7fadd2005-04-07 20:30:01 +00002066 if (CN->getValue() == 0) {
2067 Tmp1 = SelectExpr(SetCC->getOperand(0));
2068 switch (SetCC->getCondition()) {
Nate Begeman7bfba7d2005-04-14 09:45:08 +00002069 default: SetCC->dump(); assert(0 && "Unhandled SetCC condition"); abort();
Nate Begeman7e7fadd2005-04-07 20:30:01 +00002070 case ISD::SETEQ:
Nate Begeman7e7fadd2005-04-07 20:30:01 +00002071 Tmp2 = MakeReg(MVT::i32);
2072 BuildMI(BB, PPC::CNTLZW, 1, Tmp2).addReg(Tmp1);
2073 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp2).addImm(27)
2074 .addImm(5).addImm(31);
2075 break;
2076 case ISD::SETNE:
Nate Begeman7e7fadd2005-04-07 20:30:01 +00002077 Tmp2 = MakeReg(MVT::i32);
2078 BuildMI(BB, PPC::ADDIC, 2, Tmp2).addReg(Tmp1).addSImm(-1);
2079 BuildMI(BB, PPC::SUBFE, 2, Result).addReg(Tmp2).addReg(Tmp1);
2080 break;
Nate Begeman7e7fadd2005-04-07 20:30:01 +00002081 case ISD::SETLT:
2082 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(1)
2083 .addImm(31).addImm(31);
2084 break;
Nate Begeman7e7fadd2005-04-07 20:30:01 +00002085 case ISD::SETGT:
2086 Tmp2 = MakeReg(MVT::i32);
2087 Tmp3 = MakeReg(MVT::i32);
2088 BuildMI(BB, PPC::NEG, 2, Tmp2).addReg(Tmp1);
2089 BuildMI(BB, PPC::ANDC, 2, Tmp3).addReg(Tmp2).addReg(Tmp1);
2090 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp3).addImm(1)
2091 .addImm(31).addImm(31);
2092 break;
Nate Begeman9765c252005-04-12 21:22:28 +00002093 }
2094 return Result;
2095 }
2096 // setcc op, -1
2097 if (CN->isAllOnesValue()) {
2098 Tmp1 = SelectExpr(SetCC->getOperand(0));
2099 switch (SetCC->getCondition()) {
2100 default: assert(0 && "Unhandled SetCC condition"); abort();
2101 case ISD::SETEQ:
2102 Tmp2 = MakeReg(MVT::i32);
2103 Tmp3 = MakeReg(MVT::i32);
2104 BuildMI(BB, PPC::ADDIC, 2, Tmp2).addReg(Tmp1).addSImm(1);
2105 BuildMI(BB, PPC::LI, 1, Tmp3).addSImm(0);
2106 BuildMI(BB, PPC::ADDZE, 1, Result).addReg(Tmp3);
Nate Begeman7e7fadd2005-04-07 20:30:01 +00002107 break;
Nate Begeman9765c252005-04-12 21:22:28 +00002108 case ISD::SETNE:
2109 Tmp2 = MakeReg(MVT::i32);
2110 Tmp3 = MakeReg(MVT::i32);
2111 BuildMI(BB, PPC::NOR, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
2112 BuildMI(BB, PPC::ADDIC, 2, Tmp3).addReg(Tmp2).addSImm(-1);
2113 BuildMI(BB, PPC::SUBFE, 2, Result).addReg(Tmp3).addReg(Tmp2);
2114 break;
2115 case ISD::SETLT:
2116 Tmp2 = MakeReg(MVT::i32);
2117 Tmp3 = MakeReg(MVT::i32);
2118 BuildMI(BB, PPC::ADDI, 2, Tmp2).addReg(Tmp1).addSImm(1);
2119 BuildMI(BB, PPC::AND, 2, Tmp3).addReg(Tmp2).addReg(Tmp1);
2120 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp3).addImm(1)
2121 .addImm(31).addImm(31);
2122 break;
2123 case ISD::SETGT:
2124 Tmp2 = MakeReg(MVT::i32);
Nate Begeman7e7fadd2005-04-07 20:30:01 +00002125 BuildMI(BB, PPC::RLWINM, 4, Tmp2).addReg(Tmp1).addImm(1)
2126 .addImm(31).addImm(31);
2127 BuildMI(BB, PPC::XORI, 2, Result).addReg(Tmp2).addImm(1);
2128 break;
2129 }
2130 return Result;
2131 }
2132 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002133
Nate Begeman1cbf3ab2005-04-18 07:48:09 +00002134 bool Inv;
2135 unsigned CCReg = SelectCC(N, Opc, Inv, Tmp2);
2136 MoveCRtoGPR(CCReg, Inv, Tmp2, Result);
Nate Begeman33162522005-03-29 21:54:38 +00002137 return Result;
2138 }
2139 assert(0 && "Is this legal?");
2140 return 0;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002141
Nate Begeman74747862005-03-29 22:24:51 +00002142 case ISD::SELECT: {
Nate Begemana3fd4002005-07-19 16:51:05 +00002143 SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(N.getOperand(0).Val);
2144 if (SetCC && N.getOperand(0).getOpcode() == ISD::SETCC &&
2145 !MVT::isInteger(SetCC->getOperand(0).getValueType()) &&
2146 !MVT::isInteger(N.getOperand(1).getValueType()) &&
2147 !MVT::isInteger(N.getOperand(2).getValueType()) &&
2148 SetCC->getCondition() != ISD::SETEQ &&
2149 SetCC->getCondition() != ISD::SETNE) {
2150 MVT::ValueType VT = SetCC->getOperand(0).getValueType();
2151 unsigned TV = SelectExpr(N.getOperand(1)); // Use if TRUE
2152 unsigned FV = SelectExpr(N.getOperand(2)); // Use if FALSE
2153
2154 ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(1));
2155 if (CN && (CN->isExactlyValue(-0.0) || CN->isExactlyValue(0.0))) {
2156 switch(SetCC->getCondition()) {
2157 default: assert(0 && "Invalid FSEL condition"); abort();
2158 case ISD::SETULT:
2159 case ISD::SETLT:
2160 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
2161 case ISD::SETUGE:
2162 case ISD::SETGE:
2163 Tmp1 = SelectExpr(SetCC->getOperand(0)); // Val to compare against
2164 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp1).addReg(TV).addReg(FV);
2165 return Result;
2166 case ISD::SETUGT:
2167 case ISD::SETGT:
2168 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
2169 case ISD::SETULE:
2170 case ISD::SETLE: {
2171 if (SetCC->getOperand(0).getOpcode() == ISD::FNEG) {
2172 Tmp2 = SelectExpr(SetCC->getOperand(0).getOperand(0));
2173 } else {
2174 Tmp2 = MakeReg(VT);
2175 Tmp1 = SelectExpr(SetCC->getOperand(0)); // Val to compare against
2176 BuildMI(BB, PPC::FNEG, 1, Tmp2).addReg(Tmp1);
2177 }
2178 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp2).addReg(TV).addReg(FV);
2179 return Result;
2180 }
2181 }
2182 } else {
2183 Opc = (MVT::f64 == VT) ? PPC::FSUB : PPC::FSUBS;
2184 Tmp1 = SelectExpr(SetCC->getOperand(0)); // Val to compare against
2185 Tmp2 = SelectExpr(SetCC->getOperand(1));
2186 Tmp3 = MakeReg(VT);
2187 switch(SetCC->getCondition()) {
2188 default: assert(0 && "Invalid FSEL condition"); abort();
2189 case ISD::SETULT:
2190 case ISD::SETLT:
2191 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
2192 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(FV).addReg(TV);
2193 return Result;
2194 case ISD::SETUGE:
2195 case ISD::SETGE:
2196 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
2197 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(TV).addReg(FV);
2198 return Result;
2199 case ISD::SETUGT:
2200 case ISD::SETGT:
2201 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp2).addReg(Tmp1);
2202 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(FV).addReg(TV);
2203 return Result;
2204 case ISD::SETULE:
2205 case ISD::SETLE:
2206 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp2).addReg(Tmp1);
2207 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(TV).addReg(FV);
2208 return Result;
2209 }
2210 }
2211 assert(0 && "Should never get here");
2212 return 0;
2213 }
2214
Nate Begeman1cbf3ab2005-04-18 07:48:09 +00002215 bool Inv;
Chris Lattner30710192005-04-01 07:10:02 +00002216 unsigned TrueValue = SelectExpr(N.getOperand(1)); //Use if TRUE
2217 unsigned FalseValue = SelectExpr(N.getOperand(2)); //Use if FALSE
Nate Begeman1cbf3ab2005-04-18 07:48:09 +00002218 unsigned CCReg = SelectCC(N.getOperand(0), Opc, Inv, Tmp3);
Chris Lattner30710192005-04-01 07:10:02 +00002219
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002220 // Create an iterator with which to insert the MBB for copying the false
Nate Begeman74747862005-03-29 22:24:51 +00002221 // value and the MBB to hold the PHI instruction for this SetCC.
2222 MachineBasicBlock *thisMBB = BB;
2223 const BasicBlock *LLVM_BB = BB->getBasicBlock();
2224 ilist<MachineBasicBlock>::iterator It = BB;
2225 ++It;
2226
2227 // thisMBB:
2228 // ...
2229 // TrueVal = ...
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00002230 // cmpTY ccX, r1, r2
Nate Begeman74747862005-03-29 22:24:51 +00002231 // bCC copy1MBB
2232 // fallthrough --> copy0MBB
Nate Begeman74747862005-03-29 22:24:51 +00002233 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
2234 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00002235 BuildMI(BB, Opc, 2).addReg(CCReg).addMBB(sinkMBB);
Nate Begeman74747862005-03-29 22:24:51 +00002236 MachineFunction *F = BB->getParent();
2237 F->getBasicBlockList().insert(It, copy0MBB);
2238 F->getBasicBlockList().insert(It, sinkMBB);
2239 // Update machine-CFG edges
2240 BB->addSuccessor(copy0MBB);
2241 BB->addSuccessor(sinkMBB);
2242
2243 // copy0MBB:
2244 // %FalseValue = ...
2245 // # fallthrough to sinkMBB
2246 BB = copy0MBB;
Nate Begeman74747862005-03-29 22:24:51 +00002247 // Update machine-CFG edges
2248 BB->addSuccessor(sinkMBB);
2249
2250 // sinkMBB:
2251 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
2252 // ...
2253 BB = sinkMBB;
2254 BuildMI(BB, PPC::PHI, 4, Result).addReg(FalseValue)
2255 .addMBB(copy0MBB).addReg(TrueValue).addMBB(thisMBB);
Nate Begeman74747862005-03-29 22:24:51 +00002256 return Result;
2257 }
Nate Begemana9795f82005-03-24 04:41:43 +00002258
2259 case ISD::Constant:
2260 switch (N.getValueType()) {
2261 default: assert(0 && "Cannot use constants of this type!");
2262 case MVT::i1:
2263 BuildMI(BB, PPC::LI, 1, Result)
2264 .addSImm(!cast<ConstantSDNode>(N)->isNullValue());
2265 break;
2266 case MVT::i32:
2267 {
2268 int v = (int)cast<ConstantSDNode>(N)->getSignExtended();
2269 if (v < 32768 && v >= -32768) {
2270 BuildMI(BB, PPC::LI, 1, Result).addSImm(v);
2271 } else {
Nate Begeman5e966612005-03-24 06:28:42 +00002272 Tmp1 = MakeReg(MVT::i32);
2273 BuildMI(BB, PPC::LIS, 1, Tmp1).addSImm(v >> 16);
2274 BuildMI(BB, PPC::ORI, 2, Result).addReg(Tmp1).addImm(v & 0xFFFF);
Nate Begemana9795f82005-03-24 04:41:43 +00002275 }
2276 }
2277 }
2278 return Result;
Nate Begemana3fd4002005-07-19 16:51:05 +00002279
2280 case ISD::ConstantFP: {
2281 ConstantFPSDNode *CN = cast<ConstantFPSDNode>(N);
2282 Result = getConstDouble(CN->getValue(), Result);
2283 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00002284 }
2285
Nate Begemana3fd4002005-07-19 16:51:05 +00002286 case ISD::FNEG:
2287 if (!NoExcessFPPrecision &&
2288 ISD::ADD == N.getOperand(0).getOpcode() &&
2289 N.getOperand(0).Val->hasOneUse() &&
2290 ISD::MUL == N.getOperand(0).getOperand(0).getOpcode() &&
2291 N.getOperand(0).getOperand(0).Val->hasOneUse()) {
2292 ++FusedFP; // Statistic
2293 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(0));
2294 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(1));
2295 Tmp3 = SelectExpr(N.getOperand(0).getOperand(1));
2296 Opc = DestType == MVT::f64 ? PPC::FNMADD : PPC::FNMADDS;
2297 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
2298 } else if (!NoExcessFPPrecision &&
2299 ISD::ADD == N.getOperand(0).getOpcode() &&
2300 N.getOperand(0).Val->hasOneUse() &&
2301 ISD::MUL == N.getOperand(0).getOperand(1).getOpcode() &&
2302 N.getOperand(0).getOperand(1).Val->hasOneUse()) {
2303 ++FusedFP; // Statistic
2304 Tmp1 = SelectExpr(N.getOperand(0).getOperand(1).getOperand(0));
2305 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1).getOperand(1));
2306 Tmp3 = SelectExpr(N.getOperand(0).getOperand(0));
2307 Opc = DestType == MVT::f64 ? PPC::FNMADD : PPC::FNMADDS;
2308 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
2309 } else if (ISD::FABS == N.getOperand(0).getOpcode()) {
2310 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
2311 BuildMI(BB, PPC::FNABS, 1, Result).addReg(Tmp1);
2312 } else {
2313 Tmp1 = SelectExpr(N.getOperand(0));
2314 BuildMI(BB, PPC::FNEG, 1, Result).addReg(Tmp1);
2315 }
2316 return Result;
2317
2318 case ISD::FABS:
2319 Tmp1 = SelectExpr(N.getOperand(0));
2320 BuildMI(BB, PPC::FABS, 1, Result).addReg(Tmp1);
2321 return Result;
2322
Nate Begemanadeb43d2005-07-20 22:42:00 +00002323 case ISD::FSQRT:
2324 Tmp1 = SelectExpr(N.getOperand(0));
2325 Opc = DestType == MVT::f64 ? PPC::FSQRT : PPC::FSQRTS;
2326 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
2327 return Result;
2328
Nate Begemana3fd4002005-07-19 16:51:05 +00002329 case ISD::FP_ROUND:
2330 assert (DestType == MVT::f32 &&
2331 N.getOperand(0).getValueType() == MVT::f64 &&
2332 "only f64 to f32 conversion supported here");
2333 Tmp1 = SelectExpr(N.getOperand(0));
2334 BuildMI(BB, PPC::FRSP, 1, Result).addReg(Tmp1);
2335 return Result;
2336
2337 case ISD::FP_EXTEND:
2338 assert (DestType == MVT::f64 &&
2339 N.getOperand(0).getValueType() == MVT::f32 &&
2340 "only f32 to f64 conversion supported here");
2341 Tmp1 = SelectExpr(N.getOperand(0));
2342 BuildMI(BB, PPC::FMR, 1, Result).addReg(Tmp1);
2343 return Result;
2344
2345 case ISD::UINT_TO_FP:
2346 case ISD::SINT_TO_FP: {
2347 assert (N.getOperand(0).getValueType() == MVT::i32
2348 && "int to float must operate on i32");
2349 bool IsUnsigned = (ISD::UINT_TO_FP == opcode);
2350 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
2351 Tmp2 = MakeReg(MVT::f64); // temp reg to load the integer value into
2352 Tmp3 = MakeReg(MVT::i32); // temp reg to hold the conversion constant
2353
2354 int FrameIdx = BB->getParent()->getFrameInfo()->CreateStackObject(8, 8);
2355 MachineConstantPool *CP = BB->getParent()->getConstantPool();
2356
2357 if (IsUnsigned) {
2358 unsigned ConstF = getConstDouble(0x1.000000p52);
2359 // Store the hi & low halves of the fp value, currently in int regs
2360 BuildMI(BB, PPC::LIS, 1, Tmp3).addSImm(0x4330);
2361 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(Tmp3), FrameIdx);
2362 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(Tmp1), FrameIdx, 4);
2363 addFrameReference(BuildMI(BB, PPC::LFD, 2, Tmp2), FrameIdx);
2364 // Generate the return value with a subtract
2365 BuildMI(BB, PPC::FSUB, 2, Result).addReg(Tmp2).addReg(ConstF);
2366 } else {
2367 unsigned ConstF = getConstDouble(0x1.000008p52);
2368 unsigned TmpL = MakeReg(MVT::i32);
2369 // Store the hi & low halves of the fp value, currently in int regs
2370 BuildMI(BB, PPC::LIS, 1, Tmp3).addSImm(0x4330);
2371 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(Tmp3), FrameIdx);
2372 BuildMI(BB, PPC::XORIS, 2, TmpL).addReg(Tmp1).addImm(0x8000);
2373 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(TmpL), FrameIdx, 4);
2374 addFrameReference(BuildMI(BB, PPC::LFD, 2, Tmp2), FrameIdx);
2375 // Generate the return value with a subtract
2376 BuildMI(BB, PPC::FSUB, 2, Result).addReg(Tmp2).addReg(ConstF);
2377 }
2378 return Result;
2379 }
2380 }
Nate Begemana9795f82005-03-24 04:41:43 +00002381 return 0;
2382}
2383
2384void ISel::Select(SDOperand N) {
Nate Begeman2497e632005-07-21 20:44:43 +00002385 unsigned Tmp1, Tmp2, Tmp3, Opc;
Nate Begemana9795f82005-03-24 04:41:43 +00002386 unsigned opcode = N.getOpcode();
2387
2388 if (!ExprMap.insert(std::make_pair(N, 1)).second)
2389 return; // Already selected.
2390
2391 SDNode *Node = N.Val;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002392
Nate Begemana9795f82005-03-24 04:41:43 +00002393 switch (Node->getOpcode()) {
2394 default:
2395 Node->dump(); std::cerr << "\n";
2396 assert(0 && "Node not handled yet!");
2397 case ISD::EntryToken: return; // Noop
2398 case ISD::TokenFactor:
2399 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
2400 Select(Node->getOperand(i));
2401 return;
Chris Lattner16cd04d2005-05-12 23:24:06 +00002402 case ISD::CALLSEQ_START:
2403 case ISD::CALLSEQ_END:
Nate Begemana9795f82005-03-24 04:41:43 +00002404 Select(N.getOperand(0));
2405 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
Chris Lattner16cd04d2005-05-12 23:24:06 +00002406 Opc = N.getOpcode() == ISD::CALLSEQ_START ? PPC::ADJCALLSTACKDOWN :
Nate Begemana9795f82005-03-24 04:41:43 +00002407 PPC::ADJCALLSTACKUP;
2408 BuildMI(BB, Opc, 1).addImm(Tmp1);
2409 return;
2410 case ISD::BR: {
2411 MachineBasicBlock *Dest =
2412 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
Nate Begemana9795f82005-03-24 04:41:43 +00002413 Select(N.getOperand(0));
2414 BuildMI(BB, PPC::B, 1).addMBB(Dest);
2415 return;
2416 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002417 case ISD::BRCOND:
Nate Begemancd08e4c2005-04-09 20:09:12 +00002418 case ISD::BRCONDTWOWAY:
Nate Begemana9795f82005-03-24 04:41:43 +00002419 SelectBranchCC(N);
2420 return;
2421 case ISD::CopyToReg:
2422 Select(N.getOperand(0));
2423 Tmp1 = SelectExpr(N.getOperand(1));
2424 Tmp2 = cast<RegSDNode>(N)->getReg();
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002425
Nate Begemana9795f82005-03-24 04:41:43 +00002426 if (Tmp1 != Tmp2) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002427 if (N.getOperand(1).getValueType() == MVT::f64 ||
Nate Begemana9795f82005-03-24 04:41:43 +00002428 N.getOperand(1).getValueType() == MVT::f32)
2429 BuildMI(BB, PPC::FMR, 1, Tmp2).addReg(Tmp1);
2430 else
2431 BuildMI(BB, PPC::OR, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
2432 }
2433 return;
2434 case ISD::ImplicitDef:
2435 Select(N.getOperand(0));
2436 BuildMI(BB, PPC::IMPLICIT_DEF, 0, cast<RegSDNode>(N)->getReg());
2437 return;
2438 case ISD::RET:
2439 switch (N.getNumOperands()) {
2440 default:
2441 assert(0 && "Unknown return instruction!");
2442 case 3:
2443 assert(N.getOperand(1).getValueType() == MVT::i32 &&
2444 N.getOperand(2).getValueType() == MVT::i32 &&
Misha Brukman7847fca2005-04-22 17:54:37 +00002445 "Unknown two-register value!");
Nate Begemana9795f82005-03-24 04:41:43 +00002446 Select(N.getOperand(0));
2447 Tmp1 = SelectExpr(N.getOperand(1));
2448 Tmp2 = SelectExpr(N.getOperand(2));
Nate Begeman27523a12005-04-02 00:42:16 +00002449 BuildMI(BB, PPC::OR, 2, PPC::R3).addReg(Tmp2).addReg(Tmp2);
2450 BuildMI(BB, PPC::OR, 2, PPC::R4).addReg(Tmp1).addReg(Tmp1);
Nate Begemana9795f82005-03-24 04:41:43 +00002451 break;
2452 case 2:
2453 Select(N.getOperand(0));
2454 Tmp1 = SelectExpr(N.getOperand(1));
2455 switch (N.getOperand(1).getValueType()) {
2456 default:
2457 assert(0 && "Unknown return type!");
2458 case MVT::f64:
2459 case MVT::f32:
2460 BuildMI(BB, PPC::FMR, 1, PPC::F1).addReg(Tmp1);
2461 break;
2462 case MVT::i32:
2463 BuildMI(BB, PPC::OR, 2, PPC::R3).addReg(Tmp1).addReg(Tmp1);
2464 break;
2465 }
Nate Begeman9e3e1b52005-03-24 23:35:30 +00002466 case 1:
2467 Select(N.getOperand(0));
2468 break;
Nate Begemana9795f82005-03-24 04:41:43 +00002469 }
2470 BuildMI(BB, PPC::BLR, 0); // Just emit a 'ret' instruction
2471 return;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002472 case ISD::TRUNCSTORE:
Nate Begeman2497e632005-07-21 20:44:43 +00002473 case ISD::STORE: {
2474 SDOperand Chain = N.getOperand(0);
2475 SDOperand Value = N.getOperand(1);
2476 SDOperand Address = N.getOperand(2);
2477 Select(Chain);
Nate Begemana9795f82005-03-24 04:41:43 +00002478
Nate Begeman2497e632005-07-21 20:44:43 +00002479 Tmp1 = SelectExpr(Value); //value
Nate Begemana9795f82005-03-24 04:41:43 +00002480
Nate Begeman2497e632005-07-21 20:44:43 +00002481 if (opcode == ISD::STORE) {
2482 switch(Value.getValueType()) {
2483 default: assert(0 && "unknown Type in store");
2484 case MVT::i32: Opc = PPC::STW; break;
2485 case MVT::f64: Opc = PPC::STFD; break;
2486 case MVT::f32: Opc = PPC::STFS; break;
Nate Begemana9795f82005-03-24 04:41:43 +00002487 }
Nate Begeman2497e632005-07-21 20:44:43 +00002488 } else { //ISD::TRUNCSTORE
2489 switch(cast<VTSDNode>(Node->getOperand(4))->getVT()) {
2490 default: assert(0 && "unknown Type in store");
2491 case MVT::i1:
2492 case MVT::i8: Opc = PPC::STB; break;
2493 case MVT::i16: Opc = PPC::STH; break;
Nate Begemana9795f82005-03-24 04:41:43 +00002494 }
Nate Begemana9795f82005-03-24 04:41:43 +00002495 }
Nate Begeman2497e632005-07-21 20:44:43 +00002496
2497 if(Address.getOpcode() == ISD::FrameIndex) {
2498 Tmp2 = cast<FrameIndexSDNode>(Address)->getIndex();
2499 addFrameReference(BuildMI(BB, Opc, 3).addReg(Tmp1), (int)Tmp2);
2500 } else if(GlobalAddressSDNode *GN = dyn_cast<GlobalAddressSDNode>(Address)){
2501 GlobalValue *GV = GN->getGlobal();
2502 Tmp2 = MakeReg(MVT::i32);
2503 if (PICEnabled)
2504 BuildMI(BB, PPC::ADDIS, 2, Tmp2).addReg(getGlobalBaseReg())
2505 .addGlobalAddress(GV);
2506 else
Chris Lattner4015ea82005-07-28 04:42:11 +00002507 BuildMI(BB, PPC::LIS, 1, Tmp2).addGlobalAddress(GV);
Nate Begeman2497e632005-07-21 20:44:43 +00002508 if (GV->hasWeakLinkage() || GV->isExternal()) {
2509 Tmp3 = MakeReg(MVT::i32);
2510 BuildMI(BB, PPC::LWZ, 2, Tmp3).addGlobalAddress(GV).addReg(Tmp2);
Nate Begeman7b4f0a82005-07-25 21:15:28 +00002511 BuildMI(BB, Opc, 3).addReg(Tmp1).addSImm(0).addReg(Tmp3);
2512 } else {
2513 BuildMI(BB, Opc, 3).addReg(Tmp1).addGlobalAddress(GV).addReg(Tmp2);
Nate Begeman2497e632005-07-21 20:44:43 +00002514 }
Nate Begeman2497e632005-07-21 20:44:43 +00002515 } else {
2516 int offset;
Nate Begeman2a05c8e2005-07-28 03:02:05 +00002517 switch(SelectAddr(Address, Tmp2, offset)) {
2518 default: assert(0 && "Unhandled return value from SelectAddr");
2519 case 0: // imm offset, no frame, no index
2520 BuildMI(BB, Opc, 3).addReg(Tmp1).addSImm(offset).addReg(Tmp2);
2521 break;
2522 case 1: // imm offset + frame index
2523 addFrameReference(BuildMI(BB, Opc, 3).addReg(Tmp1), (int)Tmp2, offset);
2524 break;
2525 case 2: // base+index addressing
Nate Begeman2497e632005-07-21 20:44:43 +00002526 Opc = IndexedOpForOp(Opc);
2527 BuildMI(BB, Opc, 3).addReg(Tmp1).addReg(Tmp2).addReg(offset);
Nate Begeman2a05c8e2005-07-28 03:02:05 +00002528 break;
Nate Begeman2497e632005-07-21 20:44:43 +00002529 }
2530 }
2531 return;
2532 }
Nate Begemana9795f82005-03-24 04:41:43 +00002533 case ISD::EXTLOAD:
2534 case ISD::SEXTLOAD:
2535 case ISD::ZEXTLOAD:
2536 case ISD::LOAD:
2537 case ISD::CopyFromReg:
Chris Lattnerb5d8e6e2005-05-13 20:29:26 +00002538 case ISD::TAILCALL:
Nate Begemana9795f82005-03-24 04:41:43 +00002539 case ISD::CALL:
2540 case ISD::DYNAMIC_STACKALLOC:
2541 ExprMap.erase(N);
2542 SelectExpr(N);
2543 return;
2544 }
2545 assert(0 && "Should not be reached!");
2546}
2547
2548
2549/// createPPC32PatternInstructionSelector - This pass converts an LLVM function
2550/// into a machine code representation using pattern matching and a machine
2551/// description file.
2552///
2553FunctionPass *llvm::createPPC32ISelPattern(TargetMachine &TM) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002554 return new ISel(TM);
Chris Lattner246fa632005-03-24 06:16:18 +00002555}
2556