blob: 49bbeb0a59bf9da62e7dc2ff556f3063622aa3c1 [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
Chris Lattner0561b3f2005-08-02 19:26:06 +000038
Nate Begemana9795f82005-03-24 04:41:43 +000039//===----------------------------------------------------------------------===//
40// PPC32TargetLowering - PPC32 Implementation of the TargetLowering interface
41namespace {
42 class PPC32TargetLowering : public TargetLowering {
43 int VarArgsFrameIndex; // FrameIndex for start of varargs area.
44 int ReturnAddrIndex; // FrameIndex for return slot.
45 public:
46 PPC32TargetLowering(TargetMachine &TM) : TargetLowering(TM) {
Chris Lattner9bce0f92005-05-12 02:06:00 +000047 // Fold away setcc operations if possible.
48 setSetCCIsExpensive();
49
Nate Begemana9795f82005-03-24 04:41:43 +000050 // Set up the register classes.
51 addRegisterClass(MVT::i32, PPC32::GPRCRegisterClass);
Nate Begeman7532e2f2005-03-26 08:25:22 +000052 addRegisterClass(MVT::f32, PPC32::FPRCRegisterClass);
Nate Begemana9795f82005-03-24 04:41:43 +000053 addRegisterClass(MVT::f64, PPC32::FPRCRegisterClass);
Misha Brukmanb5f662f2005-04-21 23:30:14 +000054
Nate Begeman74d73452005-03-31 00:15:26 +000055 // PowerPC has no intrinsics for these particular operations
Nate Begeman01d05262005-03-30 01:45:43 +000056 setOperationAction(ISD::MEMMOVE, MVT::Other, Expand);
57 setOperationAction(ISD::MEMSET, MVT::Other, Expand);
58 setOperationAction(ISD::MEMCPY, MVT::Other, Expand);
59
Nate Begeman74d73452005-03-31 00:15:26 +000060 // PowerPC has an i16 but no i8 (or i1) SEXTLOAD
61 setOperationAction(ISD::SEXTLOAD, MVT::i1, Expand);
62 setOperationAction(ISD::SEXTLOAD, MVT::i8, Expand);
Misha Brukmanb5f662f2005-04-21 23:30:14 +000063
Nate Begeman815d6da2005-04-06 00:25:27 +000064 // PowerPC has no SREM/UREM instructions
65 setOperationAction(ISD::SREM, MVT::i32, Expand);
66 setOperationAction(ISD::UREM, MVT::i32, Expand);
Chris Lattner43fdea02005-04-02 05:03:24 +000067
Chris Lattner32f3cf62005-05-13 16:20:22 +000068 // We don't support sin/cos/sqrt/fmod
Chris Lattner17234b72005-04-30 04:26:06 +000069 setOperationAction(ISD::FSIN , MVT::f64, Expand);
70 setOperationAction(ISD::FCOS , MVT::f64, Expand);
Chris Lattner32f3cf62005-05-13 16:20:22 +000071 setOperationAction(ISD::SREM , MVT::f64, Expand);
Chris Lattner17234b72005-04-30 04:26:06 +000072 setOperationAction(ISD::FSIN , MVT::f32, Expand);
73 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Chris Lattner32f3cf62005-05-13 16:20:22 +000074 setOperationAction(ISD::SREM , MVT::f32, Expand);
Chris Lattner17234b72005-04-30 04:26:06 +000075
Nate Begemanadeb43d2005-07-20 22:42:00 +000076 // If we're enabling GP optimizations, use hardware square root
Chris Lattner3c304a32005-08-05 22:05:03 +000077 if (!TM.getSubtarget<PPCSubtarget>().isGigaProcessor()) {
Nate Begemanadeb43d2005-07-20 22:42:00 +000078 setOperationAction(ISD::FSQRT, MVT::f64, Expand);
79 setOperationAction(ISD::FSQRT, MVT::f32, Expand);
80 }
Jeff Cohen00b168892005-07-27 06:12:32 +000081
Nate Begemand7c4a4a2005-05-11 23:43:56 +000082 //PowerPC does not have CTPOP or CTTZ
Andrew Lenharth691ef2b2005-05-03 17:19:30 +000083 setOperationAction(ISD::CTPOP, MVT::i32 , Expand);
84 setOperationAction(ISD::CTTZ , MVT::i32 , Expand);
Andrew Lenharth691ef2b2005-05-03 17:19:30 +000085
Chris Lattnercbd06fc2005-04-07 19:41:49 +000086 setSetCCResultContents(ZeroOrOneSetCCResult);
Nate Begeman3e897162005-03-31 23:55:40 +000087 addLegalFPImmediate(+0.0); // Necessary for FSEL
Misha Brukmanb5f662f2005-04-21 23:30:14 +000088 addLegalFPImmediate(-0.0); //
Nate Begeman3e897162005-03-31 23:55:40 +000089
Nate Begemana9795f82005-03-24 04:41:43 +000090 computeRegisterProperties();
91 }
92
93 /// LowerArguments - This hook must be implemented to indicate how we should
94 /// lower the arguments for the specified function, into the specified DAG.
95 virtual std::vector<SDOperand>
96 LowerArguments(Function &F, SelectionDAG &DAG);
Misha Brukmanb5f662f2005-04-21 23:30:14 +000097
Nate Begemana9795f82005-03-24 04:41:43 +000098 /// LowerCallTo - This hook lowers an abstract call to a function into an
99 /// actual call.
100 virtual std::pair<SDOperand, SDOperand>
Chris Lattnerc57f6822005-05-12 19:56:45 +0000101 LowerCallTo(SDOperand Chain, const Type *RetTy, bool isVarArg, unsigned CC,
Chris Lattneradf6a962005-05-13 18:50:42 +0000102 bool isTailCall, SDOperand Callee, ArgListTy &Args,
103 SelectionDAG &DAG);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000104
Chris Lattnere0fe2252005-07-05 19:58:54 +0000105 virtual SDOperand LowerVAStart(SDOperand Chain, SDOperand VAListP,
106 Value *VAListV, SelectionDAG &DAG);
Jeff Cohen00b168892005-07-27 06:12:32 +0000107
Nate Begemana9795f82005-03-24 04:41:43 +0000108 virtual std::pair<SDOperand,SDOperand>
Chris Lattnere0fe2252005-07-05 19:58:54 +0000109 LowerVAArg(SDOperand Chain, SDOperand VAListP, Value *VAListV,
110 const Type *ArgTy, SelectionDAG &DAG);
Jeff Cohen00b168892005-07-27 06:12:32 +0000111
Nate Begemana9795f82005-03-24 04:41:43 +0000112 virtual std::pair<SDOperand, SDOperand>
113 LowerFrameReturnAddress(bool isFrameAddr, SDOperand Chain, unsigned Depth,
114 SelectionDAG &DAG);
115 };
116}
117
118
119std::vector<SDOperand>
120PPC32TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
121 //
122 // add beautiful description of PPC stack frame format, or at least some docs
123 //
124 MachineFunction &MF = DAG.getMachineFunction();
125 MachineFrameInfo *MFI = MF.getFrameInfo();
126 MachineBasicBlock& BB = MF.front();
127 std::vector<SDOperand> ArgValues;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000128
129 // Due to the rather complicated nature of the PowerPC ABI, rather than a
Nate Begemana9795f82005-03-24 04:41:43 +0000130 // fixed size array of physical args, for the sake of simplicity let the STL
131 // handle tracking them for us.
132 std::vector<unsigned> argVR, argPR, argOp;
133 unsigned ArgOffset = 24;
134 unsigned GPR_remaining = 8;
135 unsigned FPR_remaining = 13;
136 unsigned GPR_idx = 0, FPR_idx = 0;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000137 static const unsigned GPR[] = {
Nate Begemana9795f82005-03-24 04:41:43 +0000138 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
139 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
140 };
141 static const unsigned FPR[] = {
142 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
143 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
144 };
145
146 // Add DAG nodes to load the arguments... On entry to a function on PPC,
147 // the arguments start at offset 24, although they are likely to be passed
148 // in registers.
149 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
150 SDOperand newroot, argt;
151 unsigned ObjSize;
152 bool needsLoad = false;
Nate Begemancd08e4c2005-04-09 20:09:12 +0000153 bool ArgLive = !I->use_empty();
Nate Begemana9795f82005-03-24 04:41:43 +0000154 MVT::ValueType ObjectVT = getValueType(I->getType());
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000155
Nate Begemana9795f82005-03-24 04:41:43 +0000156 switch (ObjectVT) {
157 default: assert(0 && "Unhandled argument type!");
158 case MVT::i1:
159 case MVT::i8:
160 case MVT::i16:
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000161 case MVT::i32:
Nate Begemana9795f82005-03-24 04:41:43 +0000162 ObjSize = 4;
Nate Begemancd08e4c2005-04-09 20:09:12 +0000163 if (!ArgLive) break;
Nate Begemana9795f82005-03-24 04:41:43 +0000164 if (GPR_remaining > 0) {
Nate Begemancd08e4c2005-04-09 20:09:12 +0000165 MF.addLiveIn(GPR[GPR_idx]);
Nate Begemanf70b5762005-03-28 23:08:54 +0000166 argt = newroot = DAG.getCopyFromReg(GPR[GPR_idx], MVT::i32,
167 DAG.getRoot());
Nate Begemana9795f82005-03-24 04:41:43 +0000168 if (ObjectVT != MVT::i32)
169 argt = DAG.getNode(ISD::TRUNCATE, ObjectVT, newroot);
Nate Begemana9795f82005-03-24 04:41:43 +0000170 } else {
171 needsLoad = true;
172 }
173 break;
Nate Begemanf7e43382005-03-26 07:46:36 +0000174 case MVT::i64: ObjSize = 8;
Nate Begemancd08e4c2005-04-09 20:09:12 +0000175 if (!ArgLive) break;
Nate Begemanc5b1cd22005-04-10 05:53:14 +0000176 if (GPR_remaining > 0) {
177 SDOperand argHi, argLo;
Nate Begemancd08e4c2005-04-09 20:09:12 +0000178 MF.addLiveIn(GPR[GPR_idx]);
Nate Begemanc5b1cd22005-04-10 05:53:14 +0000179 argHi = DAG.getCopyFromReg(GPR[GPR_idx], MVT::i32, DAG.getRoot());
180 // If we have two or more remaining argument registers, then both halves
181 // of the i64 can be sourced from there. Otherwise, the lower half will
182 // have to come off the stack. This can happen when an i64 is preceded
183 // by 28 bytes of arguments.
184 if (GPR_remaining > 1) {
185 MF.addLiveIn(GPR[GPR_idx+1]);
186 argLo = DAG.getCopyFromReg(GPR[GPR_idx+1], MVT::i32, argHi);
187 } else {
188 int FI = MFI->CreateFixedObject(4, ArgOffset+4);
189 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
Chris Lattner022ed322005-05-15 19:54:37 +0000190 argLo = DAG.getLoad(MVT::i32, DAG.getEntryNode(), FIN,
191 DAG.getSrcValue(NULL));
Nate Begemanc5b1cd22005-04-10 05:53:14 +0000192 }
Nate Begemanca12a2b2005-03-28 22:28:37 +0000193 // Build the outgoing arg thingy
Nate Begemanf70b5762005-03-28 23:08:54 +0000194 argt = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, argLo, argHi);
195 newroot = argLo;
Nate Begemana9795f82005-03-24 04:41:43 +0000196 } else {
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000197 needsLoad = true;
Nate Begemana9795f82005-03-24 04:41:43 +0000198 }
199 break;
Nate Begemancd08e4c2005-04-09 20:09:12 +0000200 case MVT::f32:
201 case MVT::f64:
202 ObjSize = (ObjectVT == MVT::f64) ? 8 : 4;
203 if (!ArgLive) break;
Nate Begemana9795f82005-03-24 04:41:43 +0000204 if (FPR_remaining > 0) {
Nate Begemancd08e4c2005-04-09 20:09:12 +0000205 MF.addLiveIn(FPR[FPR_idx]);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000206 argt = newroot = DAG.getCopyFromReg(FPR[FPR_idx], ObjectVT,
Nate Begemanf70b5762005-03-28 23:08:54 +0000207 DAG.getRoot());
Nate Begemana9795f82005-03-24 04:41:43 +0000208 --FPR_remaining;
209 ++FPR_idx;
210 } else {
211 needsLoad = true;
212 }
213 break;
214 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000215
Nate Begemana9795f82005-03-24 04:41:43 +0000216 // We need to load the argument to a virtual register if we determined above
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000217 // that we ran out of physical registers of the appropriate type
Nate Begemana9795f82005-03-24 04:41:43 +0000218 if (needsLoad) {
Nate Begemane5846682005-04-04 06:52:38 +0000219 unsigned SubregOffset = 0;
Nate Begemanc3e2db42005-04-04 09:09:00 +0000220 if (ObjectVT == MVT::i8 || ObjectVT == MVT::i1) SubregOffset = 3;
Nate Begemane5846682005-04-04 06:52:38 +0000221 if (ObjectVT == MVT::i16) SubregOffset = 2;
Nate Begemana9795f82005-03-24 04:41:43 +0000222 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
223 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000224 FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN,
Nate Begemane5846682005-04-04 06:52:38 +0000225 DAG.getConstant(SubregOffset, MVT::i32));
Chris Lattner022ed322005-05-15 19:54:37 +0000226 argt = newroot = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
227 DAG.getSrcValue(NULL));
Nate Begemana9795f82005-03-24 04:41:43 +0000228 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000229
Nate Begemana9795f82005-03-24 04:41:43 +0000230 // Every 4 bytes of argument space consumes one of the GPRs available for
231 // argument passing.
232 if (GPR_remaining > 0) {
233 unsigned delta = (GPR_remaining > 1 && ObjSize == 8) ? 2 : 1;
234 GPR_remaining -= delta;
235 GPR_idx += delta;
236 }
237 ArgOffset += ObjSize;
Chris Lattner91277ea2005-04-09 21:23:24 +0000238 if (newroot.Val)
239 DAG.setRoot(newroot.getValue(1));
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000240
Nate Begemana9795f82005-03-24 04:41:43 +0000241 ArgValues.push_back(argt);
242 }
243
Nate Begemana9795f82005-03-24 04:41:43 +0000244 // If the function takes variable number of arguments, make a frame index for
245 // the start of the first vararg value... for expansion of llvm.va_start.
Nate Begemanfa554702005-04-03 22:13:27 +0000246 if (F.isVarArg()) {
Nate Begemana9795f82005-03-24 04:41:43 +0000247 VarArgsFrameIndex = MFI->CreateFixedObject(4, ArgOffset);
Nate Begemanfa554702005-04-03 22:13:27 +0000248 SDOperand FIN = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
Nate Begeman6644d4c2005-04-03 23:11:17 +0000249 // If this function is vararg, store any remaining integer argument regs
250 // to their spots on the stack so that they may be loaded by deferencing the
251 // result of va_next.
252 std::vector<SDOperand> MemOps;
253 for (; GPR_remaining > 0; --GPR_remaining, ++GPR_idx) {
Nate Begemancd08e4c2005-04-09 20:09:12 +0000254 MF.addLiveIn(GPR[GPR_idx]);
Nate Begeman6644d4c2005-04-03 23:11:17 +0000255 SDOperand Val = DAG.getCopyFromReg(GPR[GPR_idx], MVT::i32, DAG.getRoot());
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000256 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Val.getValue(1),
Andrew Lenharth2d86ea22005-04-27 20:10:01 +0000257 Val, FIN, DAG.getSrcValue(NULL));
Nate Begeman6644d4c2005-04-03 23:11:17 +0000258 MemOps.push_back(Store);
259 // Increment the address by four for the next argument to store
260 SDOperand PtrOff = DAG.getConstant(4, getPointerTy());
261 FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN, PtrOff);
262 }
263 DAG.setRoot(DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps));
Nate Begemanfa554702005-04-03 22:13:27 +0000264 }
Nate Begemana9795f82005-03-24 04:41:43 +0000265
Nate Begemancd08e4c2005-04-09 20:09:12 +0000266 // Finally, inform the code generator which regs we return values in.
267 switch (getValueType(F.getReturnType())) {
268 default: assert(0 && "Unknown type!");
269 case MVT::isVoid: break;
270 case MVT::i1:
271 case MVT::i8:
272 case MVT::i16:
273 case MVT::i32:
274 MF.addLiveOut(PPC::R3);
275 break;
276 case MVT::i64:
277 MF.addLiveOut(PPC::R3);
278 MF.addLiveOut(PPC::R4);
279 break;
280 case MVT::f32:
281 case MVT::f64:
282 MF.addLiveOut(PPC::F1);
283 break;
284 }
285
Nate Begemana9795f82005-03-24 04:41:43 +0000286 return ArgValues;
287}
288
289std::pair<SDOperand, SDOperand>
290PPC32TargetLowering::LowerCallTo(SDOperand Chain,
Misha Brukman7847fca2005-04-22 17:54:37 +0000291 const Type *RetTy, bool isVarArg,
Jeff Cohen00b168892005-07-27 06:12:32 +0000292 unsigned CallingConv, bool isTailCall,
Misha Brukman7847fca2005-04-22 17:54:37 +0000293 SDOperand Callee, ArgListTy &Args,
294 SelectionDAG &DAG) {
Nate Begeman307e7442005-03-26 01:28:53 +0000295 // args_to_use will accumulate outgoing args for the ISD::CALL case in
296 // SelectExpr to use to put the arguments in the appropriate registers.
Nate Begemana9795f82005-03-24 04:41:43 +0000297 std::vector<SDOperand> args_to_use;
Nate Begeman307e7442005-03-26 01:28:53 +0000298
299 // Count how many bytes are to be pushed on the stack, including the linkage
300 // area, and parameter passing area.
301 unsigned NumBytes = 24;
302
303 if (Args.empty()) {
Chris Lattner16cd04d2005-05-12 23:24:06 +0000304 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
Nate Begemana7e11a42005-04-01 05:57:17 +0000305 DAG.getConstant(NumBytes, getPointerTy()));
Nate Begeman307e7442005-03-26 01:28:53 +0000306 } else {
307 for (unsigned i = 0, e = Args.size(); i != e; ++i)
308 switch (getValueType(Args[i].second)) {
309 default: assert(0 && "Unknown value type!");
310 case MVT::i1:
311 case MVT::i8:
312 case MVT::i16:
313 case MVT::i32:
314 case MVT::f32:
315 NumBytes += 4;
316 break;
317 case MVT::i64:
318 case MVT::f64:
319 NumBytes += 8;
320 break;
321 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000322
323 // Just to be safe, we'll always reserve the full 24 bytes of linkage area
Nate Begeman307e7442005-03-26 01:28:53 +0000324 // plus 32 bytes of argument space in case any called code gets funky on us.
Chris Lattner0561b3f2005-08-02 19:26:06 +0000325 // (Required by ABI to support var arg)
Nate Begeman307e7442005-03-26 01:28:53 +0000326 if (NumBytes < 56) NumBytes = 56;
327
328 // Adjust the stack pointer for the new arguments...
329 // These operations are automatically eliminated by the prolog/epilog pass
Chris Lattner16cd04d2005-05-12 23:24:06 +0000330 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
Nate Begeman307e7442005-03-26 01:28:53 +0000331 DAG.getConstant(NumBytes, getPointerTy()));
332
333 // Set up a copy of the stack pointer for use loading and storing any
334 // arguments that may not fit in the registers available for argument
335 // passing.
336 SDOperand StackPtr = DAG.getCopyFromReg(PPC::R1, MVT::i32,
337 DAG.getEntryNode());
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000338
Nate Begeman307e7442005-03-26 01:28:53 +0000339 // Figure out which arguments are going to go in registers, and which in
340 // memory. Also, if this is a vararg function, floating point operations
341 // must be stored to our stack, and loaded into integer regs as well, if
342 // any integer regs are available for argument passing.
343 unsigned ArgOffset = 24;
344 unsigned GPR_remaining = 8;
345 unsigned FPR_remaining = 13;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000346
Nate Begeman74d73452005-03-31 00:15:26 +0000347 std::vector<SDOperand> MemOps;
Nate Begeman307e7442005-03-26 01:28:53 +0000348 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
349 // PtrOff will be used to store the current argument to the stack if a
350 // register cannot be found for it.
351 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
352 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
Nate Begemanf7e43382005-03-26 07:46:36 +0000353 MVT::ValueType ArgVT = getValueType(Args[i].second);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000354
Nate Begemanf7e43382005-03-26 07:46:36 +0000355 switch (ArgVT) {
Nate Begeman307e7442005-03-26 01:28:53 +0000356 default: assert(0 && "Unexpected ValueType for argument!");
357 case MVT::i1:
358 case MVT::i8:
359 case MVT::i16:
360 // Promote the integer to 32 bits. If the input type is signed use a
361 // sign extend, otherwise use a zero extend.
362 if (Args[i].second->isSigned())
363 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
364 else
365 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
366 // FALL THROUGH
367 case MVT::i32:
368 if (GPR_remaining > 0) {
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000369 args_to_use.push_back(Args[i].first);
Nate Begeman307e7442005-03-26 01:28:53 +0000370 --GPR_remaining;
371 } else {
Nate Begeman74d73452005-03-31 00:15:26 +0000372 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
Chris Lattner022ed322005-05-15 19:54:37 +0000373 Args[i].first, PtrOff,
374 DAG.getSrcValue(NULL)));
Nate Begeman307e7442005-03-26 01:28:53 +0000375 }
376 ArgOffset += 4;
377 break;
378 case MVT::i64:
Nate Begemanf7e43382005-03-26 07:46:36 +0000379 // If we have one free GPR left, we can place the upper half of the i64
380 // in it, and store the other half to the stack. If we have two or more
381 // free GPRs, then we can pass both halves of the i64 in registers.
382 if (GPR_remaining > 0) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000383 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
Nate Begemanf2622612005-03-26 02:17:46 +0000384 Args[i].first, DAG.getConstant(1, MVT::i32));
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000385 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
Nate Begemanf2622612005-03-26 02:17:46 +0000386 Args[i].first, DAG.getConstant(0, MVT::i32));
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000387 args_to_use.push_back(Hi);
Nate Begeman74d73452005-03-31 00:15:26 +0000388 --GPR_remaining;
Nate Begeman74d73452005-03-31 00:15:26 +0000389 if (GPR_remaining > 0) {
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000390 args_to_use.push_back(Lo);
Nate Begeman74d73452005-03-31 00:15:26 +0000391 --GPR_remaining;
Nate Begemanf7e43382005-03-26 07:46:36 +0000392 } else {
393 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
394 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
Nate Begeman74d73452005-03-31 00:15:26 +0000395 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +0000396 Lo, PtrOff, DAG.getSrcValue(NULL)));
Nate Begemanf7e43382005-03-26 07:46:36 +0000397 }
Nate Begeman307e7442005-03-26 01:28:53 +0000398 } else {
Nate Begeman74d73452005-03-31 00:15:26 +0000399 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
Chris Lattner022ed322005-05-15 19:54:37 +0000400 Args[i].first, PtrOff,
401 DAG.getSrcValue(NULL)));
Nate Begeman307e7442005-03-26 01:28:53 +0000402 }
403 ArgOffset += 8;
404 break;
405 case MVT::f32:
Nate Begeman307e7442005-03-26 01:28:53 +0000406 case MVT::f64:
Nate Begemanf7e43382005-03-26 07:46:36 +0000407 if (FPR_remaining > 0) {
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000408 args_to_use.push_back(Args[i].first);
409 --FPR_remaining;
Nate Begemanf7e43382005-03-26 07:46:36 +0000410 if (isVarArg) {
Nate Begeman96fc6812005-03-31 02:05:53 +0000411 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Chain,
Chris Lattner022ed322005-05-15 19:54:37 +0000412 Args[i].first, PtrOff,
413 DAG.getSrcValue(NULL));
Nate Begeman96fc6812005-03-31 02:05:53 +0000414 MemOps.push_back(Store);
Nate Begeman74d73452005-03-31 00:15:26 +0000415 // Float varargs are always shadowed in available integer registers
416 if (GPR_remaining > 0) {
Chris Lattner022ed322005-05-15 19:54:37 +0000417 SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff,
418 DAG.getSrcValue(NULL));
Nate Begeman74d73452005-03-31 00:15:26 +0000419 MemOps.push_back(Load);
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000420 args_to_use.push_back(Load);
421 --GPR_remaining;
Nate Begeman74d73452005-03-31 00:15:26 +0000422 }
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000423 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
Nate Begeman74d73452005-03-31 00:15:26 +0000424 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
425 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
Chris Lattner022ed322005-05-15 19:54:37 +0000426 SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff,
427 DAG.getSrcValue(NULL));
Nate Begeman74d73452005-03-31 00:15:26 +0000428 MemOps.push_back(Load);
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000429 args_to_use.push_back(Load);
430 --GPR_remaining;
Nate Begeman74d73452005-03-31 00:15:26 +0000431 }
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000432 } else {
433 // If we have any FPRs remaining, we may also have GPRs remaining.
434 // Args passed in FPRs consume either 1 (f32) or 2 (f64) available
435 // GPRs.
436 if (GPR_remaining > 0) {
437 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
438 --GPR_remaining;
439 }
440 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
441 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
442 --GPR_remaining;
443 }
Nate Begeman74d73452005-03-31 00:15:26 +0000444 }
Nate Begeman307e7442005-03-26 01:28:53 +0000445 } else {
Nate Begeman74d73452005-03-31 00:15:26 +0000446 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
Chris Lattner022ed322005-05-15 19:54:37 +0000447 Args[i].first, PtrOff,
448 DAG.getSrcValue(NULL)));
Nate Begeman307e7442005-03-26 01:28:53 +0000449 }
Nate Begemanf7e43382005-03-26 07:46:36 +0000450 ArgOffset += (ArgVT == MVT::f32) ? 4 : 8;
Nate Begeman307e7442005-03-26 01:28:53 +0000451 break;
452 }
Nate Begemana9795f82005-03-24 04:41:43 +0000453 }
Nate Begeman74d73452005-03-31 00:15:26 +0000454 if (!MemOps.empty())
455 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps);
Nate Begemana9795f82005-03-24 04:41:43 +0000456 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000457
Nate Begemana9795f82005-03-24 04:41:43 +0000458 std::vector<MVT::ValueType> RetVals;
459 MVT::ValueType RetTyVT = getValueType(RetTy);
460 if (RetTyVT != MVT::isVoid)
461 RetVals.push_back(RetTyVT);
462 RetVals.push_back(MVT::Other);
463
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000464 SDOperand TheCall = SDOperand(DAG.getCall(RetVals,
Nate Begemana9795f82005-03-24 04:41:43 +0000465 Chain, Callee, args_to_use), 0);
466 Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
Chris Lattner16cd04d2005-05-12 23:24:06 +0000467 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain,
Nate Begemana9795f82005-03-24 04:41:43 +0000468 DAG.getConstant(NumBytes, getPointerTy()));
469 return std::make_pair(TheCall, Chain);
470}
471
Chris Lattnere0fe2252005-07-05 19:58:54 +0000472SDOperand PPC32TargetLowering::LowerVAStart(SDOperand Chain, SDOperand VAListP,
473 Value *VAListV, SelectionDAG &DAG) {
Chris Lattnerf84a2ac2005-07-05 17:48:31 +0000474 // vastart just stores the address of the VarArgsFrameIndex slot into the
475 // memory location argument.
476 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
Chris Lattnere0fe2252005-07-05 19:58:54 +0000477 return DAG.getNode(ISD::STORE, MVT::Other, Chain, FR, VAListP,
478 DAG.getSrcValue(VAListV));
Nate Begemana9795f82005-03-24 04:41:43 +0000479}
480
Chris Lattnere0fe2252005-07-05 19:58:54 +0000481std::pair<SDOperand,SDOperand>
482PPC32TargetLowering::LowerVAArg(SDOperand Chain,
483 SDOperand VAListP, Value *VAListV,
484 const Type *ArgTy, SelectionDAG &DAG) {
Nate Begemanc7b09f12005-03-25 08:34:25 +0000485 MVT::ValueType ArgVT = getValueType(ArgTy);
Chris Lattnerf84a2ac2005-07-05 17:48:31 +0000486
487 SDOperand VAList =
Chris Lattnere0fe2252005-07-05 19:58:54 +0000488 DAG.getLoad(MVT::i32, Chain, VAListP, DAG.getSrcValue(VAListV));
489 SDOperand Result = DAG.getLoad(ArgVT, Chain, VAList, DAG.getSrcValue(NULL));
Chris Lattnerf84a2ac2005-07-05 17:48:31 +0000490 unsigned Amt;
491 if (ArgVT == MVT::i32 || ArgVT == MVT::f32)
492 Amt = 4;
493 else {
494 assert((ArgVT == MVT::i64 || ArgVT == MVT::f64) &&
495 "Other types should have been promoted for varargs!");
496 Amt = 8;
Nate Begemanc7b09f12005-03-25 08:34:25 +0000497 }
Chris Lattnerf84a2ac2005-07-05 17:48:31 +0000498 VAList = DAG.getNode(ISD::ADD, VAList.getValueType(), VAList,
499 DAG.getConstant(Amt, VAList.getValueType()));
500 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain,
Chris Lattnere0fe2252005-07-05 19:58:54 +0000501 VAList, VAListP, DAG.getSrcValue(VAListV));
Nate Begemanc7b09f12005-03-25 08:34:25 +0000502 return std::make_pair(Result, Chain);
Nate Begemana9795f82005-03-24 04:41:43 +0000503}
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000504
Nate Begemana9795f82005-03-24 04:41:43 +0000505
506std::pair<SDOperand, SDOperand> PPC32TargetLowering::
507LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
508 SelectionDAG &DAG) {
Nate Begeman01d05262005-03-30 01:45:43 +0000509 assert(0 && "LowerFrameReturnAddress unimplemented");
Nate Begemana9795f82005-03-24 04:41:43 +0000510 abort();
511}
512
513namespace {
Nate Begemanc7bd4822005-04-11 06:34:10 +0000514Statistic<>Recorded("ppc-codegen", "Number of recording ops emitted");
Nate Begeman93075ec2005-04-04 23:40:36 +0000515Statistic<>FusedFP("ppc-codegen", "Number of fused fp operations");
Nate Begeman2a05c8e2005-07-28 03:02:05 +0000516Statistic<>FrameOff("ppc-codegen", "Number of frame idx offsets collapsed");
Chris Lattner3c304a32005-08-05 22:05:03 +0000517
Nate Begemana9795f82005-03-24 04:41:43 +0000518//===--------------------------------------------------------------------===//
519/// ISel - PPC32 specific code to select PPC32 machine instructions for
520/// SelectionDAG operations.
521//===--------------------------------------------------------------------===//
522class ISel : public SelectionDAGISel {
Nate Begemana9795f82005-03-24 04:41:43 +0000523 PPC32TargetLowering PPC32Lowering;
Nate Begeman815d6da2005-04-06 00:25:27 +0000524 SelectionDAG *ISelDAG; // Hack to support us having a dag->dag transform
525 // for sdiv and udiv until it is put into the future
526 // dag combiner.
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000527
Nate Begemana9795f82005-03-24 04:41:43 +0000528 /// ExprMap - As shared expressions are codegen'd, we keep track of which
529 /// vreg the value is produced in, so we only emit one copy of each compiled
530 /// tree.
531 std::map<SDOperand, unsigned> ExprMap;
Nate Begemanc7b09f12005-03-25 08:34:25 +0000532
533 unsigned GlobalBaseReg;
534 bool GlobalBaseInitialized;
Nate Begemanc7bd4822005-04-11 06:34:10 +0000535 bool RecordSuccess;
Nate Begemana9795f82005-03-24 04:41:43 +0000536public:
Nate Begeman815d6da2005-04-06 00:25:27 +0000537 ISel(TargetMachine &TM) : SelectionDAGISel(PPC32Lowering), PPC32Lowering(TM),
538 ISelDAG(0) {}
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000539
Nate Begemanc7b09f12005-03-25 08:34:25 +0000540 /// runOnFunction - Override this function in order to reset our per-function
541 /// variables.
542 virtual bool runOnFunction(Function &Fn) {
543 // Make sure we re-emit a set of the global base reg if necessary
544 GlobalBaseInitialized = false;
545 return SelectionDAGISel::runOnFunction(Fn);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000546 }
547
Nate Begemana9795f82005-03-24 04:41:43 +0000548 /// InstructionSelectBasicBlock - This callback is invoked by
549 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
550 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG) {
551 DEBUG(BB->dump());
552 // Codegen the basic block.
Nate Begeman815d6da2005-04-06 00:25:27 +0000553 ISelDAG = &DAG;
Nate Begemana9795f82005-03-24 04:41:43 +0000554 Select(DAG.getRoot());
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000555
Nate Begemana9795f82005-03-24 04:41:43 +0000556 // Clear state used for selection.
557 ExprMap.clear();
Nate Begeman815d6da2005-04-06 00:25:27 +0000558 ISelDAG = 0;
Nate Begemana9795f82005-03-24 04:41:43 +0000559 }
Nate Begeman815d6da2005-04-06 00:25:27 +0000560
561 // dag -> dag expanders for integer divide by constant
562 SDOperand BuildSDIVSequence(SDOperand N);
563 SDOperand BuildUDIVSequence(SDOperand N);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000564
Nate Begemandffcfcc2005-04-01 00:32:34 +0000565 unsigned getGlobalBaseReg();
Nate Begeman6b559972005-04-01 02:59:27 +0000566 unsigned getConstDouble(double floatVal, unsigned Result);
Nate Begeman1cbf3ab2005-04-18 07:48:09 +0000567 void MoveCRtoGPR(unsigned CCReg, bool Inv, unsigned Idx, unsigned Result);
Nate Begeman7ddecb42005-04-06 23:51:40 +0000568 bool SelectBitfieldInsert(SDOperand OR, unsigned Result);
Nate Begeman3664cef2005-04-13 22:14:14 +0000569 unsigned FoldIfWideZeroExtend(SDOperand N);
Nate Begeman1cbf3ab2005-04-18 07:48:09 +0000570 unsigned SelectCC(SDOperand CC, unsigned &Opc, bool &Inv, unsigned &Idx);
571 unsigned SelectCCExpr(SDOperand N, unsigned& Opc, bool &Inv, unsigned &Idx);
Chris Lattner0d7d99f2005-08-10 16:34:52 +0000572 bool SelectIntImmediateExpr(SDOperand N, unsigned Result, unsigned C,
573 unsigned OCHi, unsigned OCLo,
574 bool IsArithmetic);
Nate Begemanc7bd4822005-04-11 06:34:10 +0000575 unsigned SelectExpr(SDOperand N, bool Recording=false);
Nate Begemana9795f82005-03-24 04:41:43 +0000576 void Select(SDOperand N);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000577
Nate Begeman2a05c8e2005-07-28 03:02:05 +0000578 unsigned SelectAddr(SDOperand N, unsigned& Reg, int& offset);
Nate Begemana9795f82005-03-24 04:41:43 +0000579 void SelectBranchCC(SDOperand N);
Chris Lattner3f270132005-08-02 19:07:49 +0000580
581 virtual const char *getPassName() const {
582 return "PowerPC Pattern Instruction Selection";
583 }
Nate Begemana9795f82005-03-24 04:41:43 +0000584};
585
Chris Lattner02efa6c2005-08-08 21:08:09 +0000586// isRunOfOnes - Returns true iff Val consists of one contiguous run of 1s with
587// any number of 0s on either side. The 1s are allowed to wrap from LSB to
588// MSB, so 0x000FFF0, 0x0000FFFF, and 0xFF0000FF are all runs. 0x0F0F0000 is
589// not, since all 1s are not contiguous.
590static bool isRunOfOnes(unsigned Val, unsigned &MB, unsigned &ME) {
591 if (isShiftedMask_32(Val)) {
592 // look for the first non-zero bit
593 MB = CountLeadingZeros_32(Val);
594 // look for the first zero bit after the run of ones
595 ME = CountLeadingZeros_32((Val - 1) ^ Val);
596 return true;
597 } else if (isShiftedMask_32(Val = ~Val)) { // invert mask
598 // effectively look for the first zero bit
599 ME = CountLeadingZeros_32(Val) - 1;
600 // effectively look for the first one bit after the run of zeros
601 MB = CountLeadingZeros_32((Val - 1) ^ Val) + 1;
602 return true;
603 }
604 // no run present
605 return false;
606}
607
Chris Lattnercf1cf182005-08-08 21:10:27 +0000608// isRotateAndMask - Returns true if Mask and Shift can be folded in to a rotate
609// and mask opcode and mask operation.
610static bool isRotateAndMask(unsigned Opcode, unsigned Shift, unsigned Mask,
611 bool IsShiftMask,
612 unsigned &SH, unsigned &MB, unsigned &ME) {
613 if (Shift > 31) return false;
614 unsigned Indeterminant = ~0; // bit mask marking indeterminant results
615
616 if (Opcode == ISD::SHL) { // shift left
617 // apply shift to mask if it comes first
618 if (IsShiftMask) Mask = Mask << Shift;
619 // determine which bits are made indeterminant by shift
620 Indeterminant = ~(0xFFFFFFFFu << Shift);
621 } else if (Opcode == ISD::SRA || Opcode == ISD::SRL) { // shift rights
622 // apply shift to mask if it comes first
623 if (IsShiftMask) Mask = Mask >> Shift;
624 // determine which bits are made indeterminant by shift
625 Indeterminant = ~(0xFFFFFFFFu >> Shift);
626 // adjust for the left rotate
627 Shift = 32 - Shift;
628 }
629
630 // if the mask doesn't intersect any Indeterminant bits
631 if (!(Mask & Indeterminant)) {
632 SH = Shift;
633 // make sure the mask is still a mask (wrap arounds may not be)
634 return isRunOfOnes(Mask, MB, ME);
635 }
636
637 // can't do it
638 return false;
639}
640
Chris Lattner59b21c22005-08-09 18:29:55 +0000641// isIntImmediate - This method tests to see if a constant operand.
Chris Lattnercf1cf182005-08-08 21:10:27 +0000642// If so Imm will receive the 32 bit value.
Chris Lattner59b21c22005-08-09 18:29:55 +0000643static bool isIntImmediate(SDOperand N, unsigned& Imm) {
Chris Lattnercf1cf182005-08-08 21:10:27 +0000644 // test for constant
Chris Lattner59b21c22005-08-09 18:29:55 +0000645 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N)) {
Chris Lattnercf1cf182005-08-08 21:10:27 +0000646 // retrieve value
Chris Lattner59b21c22005-08-09 18:29:55 +0000647 Imm = (unsigned)CN->getSignExtended();
Chris Lattnercf1cf182005-08-08 21:10:27 +0000648 // passes muster
649 return true;
650 }
651 // not a constant
652 return false;
653}
654
655// isOprShiftImm - Returns true if the specified operand is a shift opcode with
656// a immediate shift count less than 32.
657static bool isOprShiftImm(SDOperand N, unsigned& Opc, unsigned& SH) {
658 Opc = N.getOpcode();
659 return (Opc == ISD::SHL || Opc == ISD::SRL || Opc == ISD::SRA) &&
Chris Lattner59b21c22005-08-09 18:29:55 +0000660 isIntImmediate(N.getOperand(1), SH) && SH < 32;
Chris Lattnercf1cf182005-08-08 21:10:27 +0000661}
662
663// isOprNot - Returns true if the specified operand is an xor with immediate -1.
664static bool isOprNot(SDOperand N) {
665 unsigned Imm;
666 return N.getOpcode() == ISD::XOR &&
Chris Lattner59b21c22005-08-09 18:29:55 +0000667 isIntImmediate(N.getOperand(1), Imm) && (signed)Imm == -1;
Chris Lattnercf1cf182005-08-08 21:10:27 +0000668}
669
670// Immediate constant composers.
671// Lo16 - grabs the lo 16 bits from a 32 bit constant.
672// Hi16 - grabs the hi 16 bits from a 32 bit constant.
673// HA16 - computes the hi bits required if the lo bits are add/subtracted in
674// arithmethically.
675static unsigned Lo16(unsigned x) { return x & 0x0000FFFF; }
676static unsigned Hi16(unsigned x) { return Lo16(x >> 16); }
677static unsigned HA16(unsigned x) { return Hi16((signed)x - (signed short)x); }
678
Nate Begemanc7bd4822005-04-11 06:34:10 +0000679/// NodeHasRecordingVariant - If SelectExpr can always produce code for
680/// NodeOpcode that also sets CR0 as a side effect, return true. Otherwise,
681/// return false.
682static bool NodeHasRecordingVariant(unsigned NodeOpcode) {
683 switch(NodeOpcode) {
684 default: return false;
685 case ISD::AND:
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000686 case ISD::OR:
Chris Lattner519f40b2005-04-13 02:46:17 +0000687 return true;
Nate Begemanc7bd4822005-04-11 06:34:10 +0000688 }
689}
690
Nate Begeman3e897162005-03-31 23:55:40 +0000691/// getBCCForSetCC - Returns the PowerPC condition branch mnemonic corresponding
692/// to Condition. If the Condition is unordered or unsigned, the bool argument
693/// U is set to true, otherwise it is set to false.
694static unsigned getBCCForSetCC(unsigned Condition, bool& U) {
695 U = false;
696 switch (Condition) {
697 default: assert(0 && "Unknown condition!"); abort();
698 case ISD::SETEQ: return PPC::BEQ;
699 case ISD::SETNE: return PPC::BNE;
700 case ISD::SETULT: U = true;
701 case ISD::SETLT: return PPC::BLT;
702 case ISD::SETULE: U = true;
703 case ISD::SETLE: return PPC::BLE;
704 case ISD::SETUGT: U = true;
705 case ISD::SETGT: return PPC::BGT;
706 case ISD::SETUGE: U = true;
707 case ISD::SETGE: return PPC::BGE;
708 }
Nate Begeman04730362005-04-01 04:45:11 +0000709 return 0;
710}
711
Nate Begeman7bfba7d2005-04-14 09:45:08 +0000712/// getCROpForOp - Return the condition register opcode (or inverted opcode)
713/// associated with the SelectionDAG opcode.
714static unsigned getCROpForSetCC(unsigned Opcode, bool Inv1, bool Inv2) {
715 switch (Opcode) {
716 default: assert(0 && "Unknown opcode!"); abort();
717 case ISD::AND:
718 if (Inv1 && Inv2) return PPC::CRNOR; // De Morgan's Law
719 if (!Inv1 && !Inv2) return PPC::CRAND;
720 if (Inv1 ^ Inv2) return PPC::CRANDC;
721 case ISD::OR:
722 if (Inv1 && Inv2) return PPC::CRNAND; // De Morgan's Law
723 if (!Inv1 && !Inv2) return PPC::CROR;
724 if (Inv1 ^ Inv2) return PPC::CRORC;
725 }
726 return 0;
727}
728
729/// getCRIdxForSetCC - Return the index of the condition register field
730/// associated with the SetCC condition, and whether or not the field is
731/// treated as inverted. That is, lt = 0; ge = 0 inverted.
732static unsigned getCRIdxForSetCC(unsigned Condition, bool& Inv) {
733 switch (Condition) {
734 default: assert(0 && "Unknown condition!"); abort();
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000735 case ISD::SETULT:
Nate Begeman7bfba7d2005-04-14 09:45:08 +0000736 case ISD::SETLT: Inv = false; return 0;
737 case ISD::SETUGE:
738 case ISD::SETGE: Inv = true; return 0;
739 case ISD::SETUGT:
740 case ISD::SETGT: Inv = false; return 1;
741 case ISD::SETULE:
742 case ISD::SETLE: Inv = true; return 1;
743 case ISD::SETEQ: Inv = false; return 2;
744 case ISD::SETNE: Inv = true; return 2;
745 }
746 return 0;
747}
748
Nate Begeman04730362005-04-01 04:45:11 +0000749/// IndexedOpForOp - Return the indexed variant for each of the PowerPC load
750/// and store immediate instructions.
751static unsigned IndexedOpForOp(unsigned Opcode) {
752 switch(Opcode) {
753 default: assert(0 && "Unknown opcode!"); abort();
754 case PPC::LBZ: return PPC::LBZX; case PPC::STB: return PPC::STBX;
755 case PPC::LHZ: return PPC::LHZX; case PPC::STH: return PPC::STHX;
756 case PPC::LHA: return PPC::LHAX; case PPC::STW: return PPC::STWX;
757 case PPC::LWZ: return PPC::LWZX; case PPC::STFS: return PPC::STFSX;
758 case PPC::LFS: return PPC::LFSX; case PPC::STFD: return PPC::STFDX;
759 case PPC::LFD: return PPC::LFDX;
760 }
761 return 0;
Nate Begeman3e897162005-03-31 23:55:40 +0000762}
Nate Begeman815d6da2005-04-06 00:25:27 +0000763
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000764// Structure used to return the necessary information to codegen an SDIV as
Nate Begeman815d6da2005-04-06 00:25:27 +0000765// a multiply.
766struct ms {
767 int m; // magic number
768 int s; // shift amount
769};
770
771struct mu {
772 unsigned int m; // magic number
773 int a; // add indicator
774 int s; // shift amount
775};
776
777/// magic - calculate the magic numbers required to codegen an integer sdiv as
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000778/// a sequence of multiply and shifts. Requires that the divisor not be 0, 1,
Nate Begeman815d6da2005-04-06 00:25:27 +0000779/// or -1.
780static struct ms magic(int d) {
781 int p;
782 unsigned int ad, anc, delta, q1, r1, q2, r2, t;
Chris Lattner0561b3f2005-08-02 19:26:06 +0000783 const unsigned int two31 = 0x80000000U;
Nate Begeman815d6da2005-04-06 00:25:27 +0000784 struct ms mag;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000785
Nate Begeman815d6da2005-04-06 00:25:27 +0000786 ad = abs(d);
787 t = two31 + ((unsigned int)d >> 31);
788 anc = t - 1 - t%ad; // absolute value of nc
789 p = 31; // initialize p
790 q1 = two31/anc; // initialize q1 = 2p/abs(nc)
791 r1 = two31 - q1*anc; // initialize r1 = rem(2p,abs(nc))
792 q2 = two31/ad; // initialize q2 = 2p/abs(d)
793 r2 = two31 - q2*ad; // initialize r2 = rem(2p,abs(d))
794 do {
795 p = p + 1;
796 q1 = 2*q1; // update q1 = 2p/abs(nc)
797 r1 = 2*r1; // update r1 = rem(2p/abs(nc))
798 if (r1 >= anc) { // must be unsigned comparison
799 q1 = q1 + 1;
800 r1 = r1 - anc;
801 }
802 q2 = 2*q2; // update q2 = 2p/abs(d)
803 r2 = 2*r2; // update r2 = rem(2p/abs(d))
804 if (r2 >= ad) { // must be unsigned comparison
805 q2 = q2 + 1;
806 r2 = r2 - ad;
807 }
808 delta = ad - r2;
809 } while (q1 < delta || (q1 == delta && r1 == 0));
810
811 mag.m = q2 + 1;
812 if (d < 0) mag.m = -mag.m; // resulting magic number
813 mag.s = p - 32; // resulting shift
814 return mag;
815}
816
817/// magicu - calculate the magic numbers required to codegen an integer udiv as
818/// a sequence of multiply, add and shifts. Requires that the divisor not be 0.
819static struct mu magicu(unsigned d)
820{
821 int p;
822 unsigned int nc, delta, q1, r1, q2, r2;
823 struct mu magu;
824 magu.a = 0; // initialize "add" indicator
825 nc = - 1 - (-d)%d;
826 p = 31; // initialize p
827 q1 = 0x80000000/nc; // initialize q1 = 2p/nc
828 r1 = 0x80000000 - q1*nc; // initialize r1 = rem(2p,nc)
829 q2 = 0x7FFFFFFF/d; // initialize q2 = (2p-1)/d
830 r2 = 0x7FFFFFFF - q2*d; // initialize r2 = rem((2p-1),d)
831 do {
832 p = p + 1;
833 if (r1 >= nc - r1 ) {
834 q1 = 2*q1 + 1; // update q1
835 r1 = 2*r1 - nc; // update r1
836 }
837 else {
838 q1 = 2*q1; // update q1
839 r1 = 2*r1; // update r1
840 }
841 if (r2 + 1 >= d - r2) {
842 if (q2 >= 0x7FFFFFFF) magu.a = 1;
843 q2 = 2*q2 + 1; // update q2
844 r2 = 2*r2 + 1 - d; // update r2
845 }
846 else {
847 if (q2 >= 0x80000000) magu.a = 1;
848 q2 = 2*q2; // update q2
849 r2 = 2*r2 + 1; // update r2
850 }
851 delta = d - 1 - r2;
852 } while (p < 64 && (q1 < delta || (q1 == delta && r1 == 0)));
853 magu.m = q2 + 1; // resulting magic number
854 magu.s = p - 32; // resulting shift
855 return magu;
856}
857}
858
859/// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
860/// return a DAG expression to select that will generate the same value by
861/// multiplying by a magic number. See:
862/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
863SDOperand ISel::BuildSDIVSequence(SDOperand N) {
864 int d = (int)cast<ConstantSDNode>(N.getOperand(1))->getSignExtended();
865 ms magics = magic(d);
866 // Multiply the numerator (operand 0) by the magic value
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000867 SDOperand Q = ISelDAG->getNode(ISD::MULHS, MVT::i32, N.getOperand(0),
Nate Begeman815d6da2005-04-06 00:25:27 +0000868 ISelDAG->getConstant(magics.m, MVT::i32));
869 // If d > 0 and m < 0, add the numerator
870 if (d > 0 && magics.m < 0)
871 Q = ISelDAG->getNode(ISD::ADD, MVT::i32, Q, N.getOperand(0));
872 // If d < 0 and m > 0, subtract the numerator.
873 if (d < 0 && magics.m > 0)
874 Q = ISelDAG->getNode(ISD::SUB, MVT::i32, Q, N.getOperand(0));
875 // Shift right algebraic if shift value is nonzero
876 if (magics.s > 0)
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000877 Q = ISelDAG->getNode(ISD::SRA, MVT::i32, Q,
Nate Begeman815d6da2005-04-06 00:25:27 +0000878 ISelDAG->getConstant(magics.s, MVT::i32));
879 // Extract the sign bit and add it to the quotient
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000880 SDOperand T =
Nate Begeman815d6da2005-04-06 00:25:27 +0000881 ISelDAG->getNode(ISD::SRL, MVT::i32, Q, ISelDAG->getConstant(31, MVT::i32));
Nate Begeman27b4c232005-04-06 06:44:57 +0000882 return ISelDAG->getNode(ISD::ADD, MVT::i32, Q, T);
Nate Begeman815d6da2005-04-06 00:25:27 +0000883}
884
885/// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
886/// return a DAG expression to select that will generate the same value by
887/// multiplying by a magic number. See:
888/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
889SDOperand ISel::BuildUDIVSequence(SDOperand N) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000890 unsigned d =
Nate Begeman815d6da2005-04-06 00:25:27 +0000891 (unsigned)cast<ConstantSDNode>(N.getOperand(1))->getSignExtended();
892 mu magics = magicu(d);
893 // Multiply the numerator (operand 0) by the magic value
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000894 SDOperand Q = ISelDAG->getNode(ISD::MULHU, MVT::i32, N.getOperand(0),
Nate Begeman815d6da2005-04-06 00:25:27 +0000895 ISelDAG->getConstant(magics.m, MVT::i32));
896 if (magics.a == 0) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000897 Q = ISelDAG->getNode(ISD::SRL, MVT::i32, Q,
Nate Begeman815d6da2005-04-06 00:25:27 +0000898 ISelDAG->getConstant(magics.s, MVT::i32));
899 } else {
900 SDOperand NPQ = ISelDAG->getNode(ISD::SUB, MVT::i32, N.getOperand(0), Q);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000901 NPQ = ISelDAG->getNode(ISD::SRL, MVT::i32, NPQ,
Nate Begeman815d6da2005-04-06 00:25:27 +0000902 ISelDAG->getConstant(1, MVT::i32));
903 NPQ = ISelDAG->getNode(ISD::ADD, MVT::i32, NPQ, Q);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000904 Q = ISelDAG->getNode(ISD::SRL, MVT::i32, NPQ,
Nate Begeman815d6da2005-04-06 00:25:27 +0000905 ISelDAG->getConstant(magics.s-1, MVT::i32));
906 }
Nate Begeman27b4c232005-04-06 06:44:57 +0000907 return Q;
Nate Begemana9795f82005-03-24 04:41:43 +0000908}
909
Nate Begemanc7b09f12005-03-25 08:34:25 +0000910/// getGlobalBaseReg - Output the instructions required to put the
911/// base address to use for accessing globals into a register.
912///
913unsigned ISel::getGlobalBaseReg() {
914 if (!GlobalBaseInitialized) {
915 // Insert the set of GlobalBaseReg into the first MBB of the function
916 MachineBasicBlock &FirstMBB = BB->getParent()->front();
917 MachineBasicBlock::iterator MBBI = FirstMBB.begin();
918 GlobalBaseReg = MakeReg(MVT::i32);
919 BuildMI(FirstMBB, MBBI, PPC::MovePCtoLR, 0, PPC::LR);
920 BuildMI(FirstMBB, MBBI, PPC::MFLR, 1, GlobalBaseReg).addReg(PPC::LR);
921 GlobalBaseInitialized = true;
922 }
923 return GlobalBaseReg;
924}
925
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000926/// getConstDouble - Loads a floating point value into a register, via the
Nate Begeman6b559972005-04-01 02:59:27 +0000927/// Constant Pool. Optionally takes a register in which to load the value.
928unsigned ISel::getConstDouble(double doubleVal, unsigned Result=0) {
929 unsigned Tmp1 = MakeReg(MVT::i32);
930 if (0 == Result) Result = MakeReg(MVT::f64);
931 MachineConstantPool *CP = BB->getParent()->getConstantPool();
932 ConstantFP *CFP = ConstantFP::get(Type::DoubleTy, doubleVal);
933 unsigned CPI = CP->getConstantPoolIndex(CFP);
Nate Begeman2497e632005-07-21 20:44:43 +0000934 if (PICEnabled)
935 BuildMI(BB, PPC::ADDIS, 2, Tmp1).addReg(getGlobalBaseReg())
936 .addConstantPoolIndex(CPI);
937 else
938 BuildMI(BB, PPC::LIS, 1, Tmp1).addConstantPoolIndex(CPI);
Nate Begeman6b559972005-04-01 02:59:27 +0000939 BuildMI(BB, PPC::LFD, 2, Result).addConstantPoolIndex(CPI).addReg(Tmp1);
940 return Result;
941}
942
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000943/// MoveCRtoGPR - Move CCReg[Idx] to the least significant bit of Result. If
Nate Begeman1cbf3ab2005-04-18 07:48:09 +0000944/// Inv is true, then invert the result.
945void ISel::MoveCRtoGPR(unsigned CCReg, bool Inv, unsigned Idx, unsigned Result){
946 unsigned IntCR = MakeReg(MVT::i32);
947 BuildMI(BB, PPC::MCRF, 1, PPC::CR7).addReg(CCReg);
Chris Lattner3c304a32005-08-05 22:05:03 +0000948 bool GPOpt =
949 TLI.getTargetMachine().getSubtarget<PPCSubtarget>().isGigaProcessor();
950 BuildMI(BB, GPOpt ? PPC::MFOCRF : PPC::MFCR, 1, IntCR).addReg(PPC::CR7);
Nate Begeman1cbf3ab2005-04-18 07:48:09 +0000951 if (Inv) {
952 unsigned Tmp1 = MakeReg(MVT::i32);
953 BuildMI(BB, PPC::RLWINM, 4, Tmp1).addReg(IntCR).addImm(32-(3-Idx))
954 .addImm(31).addImm(31);
955 BuildMI(BB, PPC::XORI, 2, Result).addReg(Tmp1).addImm(1);
956 } else {
957 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(IntCR).addImm(32-(3-Idx))
958 .addImm(31).addImm(31);
959 }
960}
961
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000962/// SelectBitfieldInsert - turn an or of two masked values into
Nate Begeman7ddecb42005-04-06 23:51:40 +0000963/// the rotate left word immediate then mask insert (rlwimi) instruction.
964/// Returns true on success, false if the caller still needs to select OR.
965///
966/// Patterns matched:
967/// 1. or shl, and 5. or and, and
968/// 2. or and, shl 6. or shl, shr
969/// 3. or shr, and 7. or shr, shl
970/// 4. or and, shr
971bool ISel::SelectBitfieldInsert(SDOperand OR, unsigned Result) {
Nate Begemancd08e4c2005-04-09 20:09:12 +0000972 bool IsRotate = false;
Nate Begeman7ddecb42005-04-06 23:51:40 +0000973 unsigned TgtMask = 0xFFFFFFFF, InsMask = 0xFFFFFFFF, Amount = 0;
Jeff Cohen00b168892005-07-27 06:12:32 +0000974
Nate Begemanb2c4bf32005-06-08 04:14:27 +0000975 SDOperand Op0 = OR.getOperand(0);
976 SDOperand Op1 = OR.getOperand(1);
977
978 unsigned Op0Opc = Op0.getOpcode();
979 unsigned Op1Opc = Op1.getOpcode();
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000980
Nate Begeman7ddecb42005-04-06 23:51:40 +0000981 // Verify that we have the correct opcodes
982 if (ISD::SHL != Op0Opc && ISD::SRL != Op0Opc && ISD::AND != Op0Opc)
983 return false;
984 if (ISD::SHL != Op1Opc && ISD::SRL != Op1Opc && ISD::AND != Op1Opc)
985 return false;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000986
Nate Begeman7ddecb42005-04-06 23:51:40 +0000987 // Generate Mask value for Target
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000988 if (ConstantSDNode *CN =
Nate Begemanb2c4bf32005-06-08 04:14:27 +0000989 dyn_cast<ConstantSDNode>(Op0.getOperand(1).Val)) {
Nate Begeman7ddecb42005-04-06 23:51:40 +0000990 switch(Op0Opc) {
991 case ISD::SHL: TgtMask <<= (unsigned)CN->getValue(); break;
992 case ISD::SRL: TgtMask >>= (unsigned)CN->getValue(); break;
993 case ISD::AND: TgtMask &= (unsigned)CN->getValue(); break;
994 }
995 } else {
996 return false;
997 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000998
Nate Begeman7ddecb42005-04-06 23:51:40 +0000999 // Generate Mask value for Insert
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001000 if (ConstantSDNode *CN =
Nate Begemanb2c4bf32005-06-08 04:14:27 +00001001 dyn_cast<ConstantSDNode>(Op1.getOperand(1).Val)) {
Nate Begeman7ddecb42005-04-06 23:51:40 +00001002 switch(Op1Opc) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001003 case ISD::SHL:
1004 Amount = CN->getValue();
Nate Begemancd08e4c2005-04-09 20:09:12 +00001005 InsMask <<= Amount;
1006 if (Op0Opc == ISD::SRL) IsRotate = true;
Nate Begeman7ddecb42005-04-06 23:51:40 +00001007 break;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001008 case ISD::SRL:
1009 Amount = CN->getValue();
1010 InsMask >>= Amount;
Nate Begeman7ddecb42005-04-06 23:51:40 +00001011 Amount = 32-Amount;
Nate Begemancd08e4c2005-04-09 20:09:12 +00001012 if (Op0Opc == ISD::SHL) IsRotate = true;
Nate Begeman7ddecb42005-04-06 23:51:40 +00001013 break;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001014 case ISD::AND:
Nate Begeman7ddecb42005-04-06 23:51:40 +00001015 InsMask &= (unsigned)CN->getValue();
1016 break;
1017 }
1018 } else {
1019 return false;
1020 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001021
Nate Begemanb2c4bf32005-06-08 04:14:27 +00001022 unsigned Tmp3 = 0;
1023
1024 // If both of the inputs are ANDs and one of them has a logical shift by
1025 // constant as its input, make that the inserted value so that we can combine
1026 // the shift into the rotate part of the rlwimi instruction
1027 if (Op0Opc == ISD::AND && Op1Opc == ISD::AND) {
Jeff Cohen00b168892005-07-27 06:12:32 +00001028 if (Op1.getOperand(0).getOpcode() == ISD::SHL ||
Nate Begemanb2c4bf32005-06-08 04:14:27 +00001029 Op1.getOperand(0).getOpcode() == ISD::SRL) {
Jeff Cohen00b168892005-07-27 06:12:32 +00001030 if (ConstantSDNode *CN =
Nate Begemanb2c4bf32005-06-08 04:14:27 +00001031 dyn_cast<ConstantSDNode>(Op1.getOperand(0).getOperand(1).Val)) {
Jeff Cohen00b168892005-07-27 06:12:32 +00001032 Amount = Op1.getOperand(0).getOpcode() == ISD::SHL ?
Nate Begemanb2c4bf32005-06-08 04:14:27 +00001033 CN->getValue() : 32 - CN->getValue();
1034 Tmp3 = SelectExpr(Op1.getOperand(0).getOperand(0));
1035 }
1036 } else if (Op0.getOperand(0).getOpcode() == ISD::SHL ||
1037 Op0.getOperand(0).getOpcode() == ISD::SRL) {
Jeff Cohen00b168892005-07-27 06:12:32 +00001038 if (ConstantSDNode *CN =
Nate Begemanb2c4bf32005-06-08 04:14:27 +00001039 dyn_cast<ConstantSDNode>(Op0.getOperand(0).getOperand(1).Val)) {
1040 std::swap(Op0, Op1);
1041 std::swap(TgtMask, InsMask);
Jeff Cohen00b168892005-07-27 06:12:32 +00001042 Amount = Op1.getOperand(0).getOpcode() == ISD::SHL ?
Nate Begemanb2c4bf32005-06-08 04:14:27 +00001043 CN->getValue() : 32 - CN->getValue();
1044 Tmp3 = SelectExpr(Op1.getOperand(0).getOperand(0));
1045 }
1046 }
1047 }
1048
Nate Begeman7ddecb42005-04-06 23:51:40 +00001049 // Verify that the Target mask and Insert mask together form a full word mask
1050 // and that the Insert mask is a run of set bits (which implies both are runs
1051 // of set bits). Given that, Select the arguments and generate the rlwimi
1052 // instruction.
1053 unsigned MB, ME;
Chris Lattner02efa6c2005-08-08 21:08:09 +00001054 if (((TgtMask & InsMask) == 0) && isRunOfOnes(InsMask, MB, ME)) {
Nate Begeman7ddecb42005-04-06 23:51:40 +00001055 unsigned Tmp1, Tmp2;
Nate Begemanb2c4bf32005-06-08 04:14:27 +00001056 bool fullMask = (TgtMask ^ InsMask) == 0xFFFFFFFF;
Nate Begemancd08e4c2005-04-09 20:09:12 +00001057 // Check for rotlwi / rotrwi here, a special case of bitfield insert
1058 // where both bitfield halves are sourced from the same value.
Nate Begemanb2c4bf32005-06-08 04:14:27 +00001059 if (IsRotate && fullMask &&
Nate Begemancd08e4c2005-04-09 20:09:12 +00001060 OR.getOperand(0).getOperand(0) == OR.getOperand(1).getOperand(0)) {
Nate Begemancd08e4c2005-04-09 20:09:12 +00001061 Tmp1 = SelectExpr(OR.getOperand(0).getOperand(0));
1062 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(Amount)
1063 .addImm(0).addImm(31);
1064 return true;
1065 }
Nate Begemanb2c4bf32005-06-08 04:14:27 +00001066 if (Op0Opc == ISD::AND && fullMask)
1067 Tmp1 = SelectExpr(Op0.getOperand(0));
Nate Begeman7ddecb42005-04-06 23:51:40 +00001068 else
Nate Begemanb2c4bf32005-06-08 04:14:27 +00001069 Tmp1 = SelectExpr(Op0);
1070 Tmp2 = Tmp3 ? Tmp3 : SelectExpr(Op1.getOperand(0));
Nate Begeman7ddecb42005-04-06 23:51:40 +00001071 BuildMI(BB, PPC::RLWIMI, 5, Result).addReg(Tmp1).addReg(Tmp2)
1072 .addImm(Amount).addImm(MB).addImm(ME);
1073 return true;
1074 }
1075 return false;
1076}
1077
Nate Begeman3664cef2005-04-13 22:14:14 +00001078/// FoldIfWideZeroExtend - 32 bit PowerPC implicit masks shift amounts to the
1079/// low six bits. If the shift amount is an ISD::AND node with a mask that is
1080/// wider than the implicit mask, then we can get rid of the AND and let the
1081/// shift do the mask.
1082unsigned ISel::FoldIfWideZeroExtend(SDOperand N) {
Chris Lattner8fd19802005-08-08 21:12:35 +00001083 unsigned C, MB, ME;
Nate Begeman3664cef2005-04-13 22:14:14 +00001084 if (N.getOpcode() == ISD::AND &&
Chris Lattner59b21c22005-08-09 18:29:55 +00001085 isIntImmediate(N.getOperand(1), C) && isRunOfOnes(C, MB, ME) &&
Chris Lattner8fd19802005-08-08 21:12:35 +00001086 MB <= 26 && ME == 31)
Nate Begeman3664cef2005-04-13 22:14:14 +00001087 return SelectExpr(N.getOperand(0));
1088 else
1089 return SelectExpr(N);
1090}
1091
Chris Lattner88ac32c2005-08-09 20:21:10 +00001092unsigned ISel::SelectCC(SDOperand Cond, unsigned& Opc, bool &Inv, unsigned& Idx) {
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001093 unsigned Result, Tmp1, Tmp2;
Nate Begeman9765c252005-04-12 21:22:28 +00001094 bool AlreadySelected = false;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001095 static const unsigned CompareOpcodes[] =
Nate Begemandffcfcc2005-04-01 00:32:34 +00001096 { PPC::FCMPU, PPC::FCMPU, PPC::CMPW, PPC::CMPLW };
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001097
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001098 // Allocate a condition register for this expression
1099 Result = RegMap->createVirtualRegister(PPC32::CRRCRegisterClass);
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001100
Nate Begemandffcfcc2005-04-01 00:32:34 +00001101 // If the first operand to the select is a SETCC node, then we can fold it
1102 // into the branch that selects which value to return.
Chris Lattner88ac32c2005-08-09 20:21:10 +00001103 if (Cond.getOpcode() == ISD::SETCC) {
1104 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
Nate Begemandffcfcc2005-04-01 00:32:34 +00001105 bool U;
Chris Lattner88ac32c2005-08-09 20:21:10 +00001106 Opc = getBCCForSetCC(CC, U);
1107 Idx = getCRIdxForSetCC(CC, Inv);
Nate Begemandffcfcc2005-04-01 00:32:34 +00001108
Chris Lattner8fd19802005-08-08 21:12:35 +00001109 // Use U to determine whether the SETCC immediate range is signed or not.
Chris Lattner88ac32c2005-08-09 20:21:10 +00001110 if (isIntImmediate(Cond.getOperand(1), Tmp2) &&
Chris Lattner8fd19802005-08-08 21:12:35 +00001111 ((U && isUInt16(Tmp2)) || (!U && isInt16(Tmp2)))) {
1112 Tmp2 = Lo16(Tmp2);
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001113 // For comparisons against zero, we can implicity set CR0 if a recording
Nate Begemanc7bd4822005-04-11 06:34:10 +00001114 // variant (e.g. 'or.' instead of 'or') of the instruction that defines
1115 // operand zero of the SetCC node is available.
Chris Lattner88ac32c2005-08-09 20:21:10 +00001116 if (Tmp2 == 0 &&
1117 NodeHasRecordingVariant(Cond.getOperand(0).getOpcode()) &&
1118 Cond.getOperand(0).Val->hasOneUse()) {
Nate Begemanc7bd4822005-04-11 06:34:10 +00001119 RecordSuccess = false;
Chris Lattner88ac32c2005-08-09 20:21:10 +00001120 Tmp1 = SelectExpr(Cond.getOperand(0), true);
Nate Begemanc7bd4822005-04-11 06:34:10 +00001121 if (RecordSuccess) {
1122 ++Recorded;
Nate Begeman7bfba7d2005-04-14 09:45:08 +00001123 BuildMI(BB, PPC::MCRF, 1, Result).addReg(PPC::CR0);
1124 return Result;
Nate Begemanc7bd4822005-04-11 06:34:10 +00001125 }
1126 AlreadySelected = true;
1127 }
1128 // If we could not implicitly set CR0, then emit a compare immediate
1129 // instead.
Chris Lattner88ac32c2005-08-09 20:21:10 +00001130 if (!AlreadySelected) Tmp1 = SelectExpr(Cond.getOperand(0));
Nate Begemandffcfcc2005-04-01 00:32:34 +00001131 if (U)
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001132 BuildMI(BB, PPC::CMPLWI, 2, Result).addReg(Tmp1).addImm(Tmp2);
Nate Begemandffcfcc2005-04-01 00:32:34 +00001133 else
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001134 BuildMI(BB, PPC::CMPWI, 2, Result).addReg(Tmp1).addSImm(Tmp2);
Nate Begemandffcfcc2005-04-01 00:32:34 +00001135 } else {
Chris Lattner88ac32c2005-08-09 20:21:10 +00001136 bool IsInteger = MVT::isInteger(Cond.getOperand(0).getValueType());
Nate Begemandffcfcc2005-04-01 00:32:34 +00001137 unsigned CompareOpc = CompareOpcodes[2 * IsInteger + U];
Chris Lattner88ac32c2005-08-09 20:21:10 +00001138 Tmp1 = SelectExpr(Cond.getOperand(0));
1139 Tmp2 = SelectExpr(Cond.getOperand(1));
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001140 BuildMI(BB, CompareOpc, 2, Result).addReg(Tmp1).addReg(Tmp2);
Nate Begemandffcfcc2005-04-01 00:32:34 +00001141 }
1142 } else {
Nate Begeman1cbf3ab2005-04-18 07:48:09 +00001143 // If this isn't a SetCC, then select the value and compare it against zero,
1144 // treating it as if it were a boolean.
Nate Begeman9765c252005-04-12 21:22:28 +00001145 Opc = PPC::BNE;
Nate Begeman1cbf3ab2005-04-18 07:48:09 +00001146 Idx = getCRIdxForSetCC(ISD::SETNE, Inv);
Chris Lattner88ac32c2005-08-09 20:21:10 +00001147 Tmp1 = SelectExpr(Cond);
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001148 BuildMI(BB, PPC::CMPLWI, 2, Result).addReg(Tmp1).addImm(0);
Nate Begemandffcfcc2005-04-01 00:32:34 +00001149 }
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001150 return Result;
Nate Begemandffcfcc2005-04-01 00:32:34 +00001151}
1152
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001153unsigned ISel::SelectCCExpr(SDOperand N, unsigned& Opc, bool &Inv,
Nate Begeman1cbf3ab2005-04-18 07:48:09 +00001154 unsigned &Idx) {
1155 bool Inv0, Inv1;
1156 unsigned Idx0, Idx1, CROpc, Opc1, Tmp1, Tmp2;
1157
1158 // Allocate a condition register for this expression
1159 unsigned Result = RegMap->createVirtualRegister(PPC32::CRRCRegisterClass);
1160
1161 // Check for the operations we support:
1162 switch(N.getOpcode()) {
1163 default:
1164 Opc = PPC::BNE;
1165 Idx = getCRIdxForSetCC(ISD::SETNE, Inv);
1166 Tmp1 = SelectExpr(N);
1167 BuildMI(BB, PPC::CMPLWI, 2, Result).addReg(Tmp1).addImm(0);
1168 break;
1169 case ISD::OR:
1170 case ISD::AND:
Nate Begeman1cbf3ab2005-04-18 07:48:09 +00001171 Tmp1 = SelectCCExpr(N.getOperand(0), Opc, Inv0, Idx0);
1172 Tmp2 = SelectCCExpr(N.getOperand(1), Opc1, Inv1, Idx1);
1173 CROpc = getCROpForSetCC(N.getOpcode(), Inv0, Inv1);
1174 if (Inv0 && !Inv1) {
1175 std::swap(Tmp1, Tmp2);
1176 std::swap(Idx0, Idx1);
1177 Opc = Opc1;
1178 }
1179 if (Inv0 && Inv1) Opc = PPC32InstrInfo::invertPPCBranchOpcode(Opc);
1180 BuildMI(BB, CROpc, 5, Result).addImm(Idx0).addReg(Tmp1).addImm(Idx0)
1181 .addReg(Tmp2).addImm(Idx1);
1182 Inv = false;
1183 Idx = Idx0;
1184 break;
1185 case ISD::SETCC:
1186 Tmp1 = SelectCC(N, Opc, Inv, Idx);
1187 Result = Tmp1;
1188 break;
1189 }
1190 return Result;
1191}
1192
Nate Begemand3ded2d2005-08-08 22:22:56 +00001193/// Check to see if the load is a constant offset from a base register.
Nate Begeman2a05c8e2005-07-28 03:02:05 +00001194unsigned ISel::SelectAddr(SDOperand N, unsigned& Reg, int& offset)
Nate Begemana9795f82005-03-24 04:41:43 +00001195{
Nate Begeman96fc6812005-03-31 02:05:53 +00001196 unsigned imm = 0, opcode = N.getOpcode();
Nate Begeman04730362005-04-01 04:45:11 +00001197 if (N.getOpcode() == ISD::ADD) {
Nate Begeman2a05c8e2005-07-28 03:02:05 +00001198 bool isFrame = N.getOperand(0).getOpcode() == ISD::FrameIndex;
Chris Lattner59b21c22005-08-09 18:29:55 +00001199 if (isIntImmediate(N.getOperand(1), imm) && isInt16(imm)) {
Chris Lattner8fd19802005-08-08 21:12:35 +00001200 offset = Lo16(imm);
Nate Begeman2a05c8e2005-07-28 03:02:05 +00001201 if (isFrame) {
1202 ++FrameOff;
1203 Reg = cast<FrameIndexSDNode>(N.getOperand(0))->getIndex();
1204 return 1;
1205 } else {
1206 Reg = SelectExpr(N.getOperand(0));
1207 return 0;
1208 }
1209 } else {
1210 Reg = SelectExpr(N.getOperand(0));
1211 offset = SelectExpr(N.getOperand(1));
1212 return 2;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001213 }
Nate Begeman04730362005-04-01 04:45:11 +00001214 }
Nate Begemand3ded2d2005-08-08 22:22:56 +00001215 // Now check if we're dealing with a global, and whether or not we should emit
1216 // an optimized load or store for statics.
1217 if(GlobalAddressSDNode *GN = dyn_cast<GlobalAddressSDNode>(N)) {
1218 GlobalValue *GV = GN->getGlobal();
1219 if (!GV->hasWeakLinkage() && !GV->isExternal()) {
1220 unsigned GlobalHi = MakeReg(MVT::i32);
1221 if (PICEnabled)
1222 BuildMI(BB, PPC::ADDIS, 2, GlobalHi).addReg(getGlobalBaseReg())
1223 .addGlobalAddress(GV);
1224 else
1225 BuildMI(BB, PPC::LIS, 1, GlobalHi).addGlobalAddress(GV);
1226 Reg = GlobalHi;
1227 offset = 0;
1228 return 3;
1229 }
1230 }
Nate Begemana9795f82005-03-24 04:41:43 +00001231 Reg = SelectExpr(N);
1232 offset = 0;
Nate Begeman2a05c8e2005-07-28 03:02:05 +00001233 return 0;
Nate Begemana9795f82005-03-24 04:41:43 +00001234}
1235
1236void ISel::SelectBranchCC(SDOperand N)
1237{
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001238 MachineBasicBlock *Dest =
Nate Begemana9795f82005-03-24 04:41:43 +00001239 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
Nate Begeman3e897162005-03-31 23:55:40 +00001240
Nate Begeman1cbf3ab2005-04-18 07:48:09 +00001241 bool Inv;
1242 unsigned Opc, CCReg, Idx;
Nate Begemana9795f82005-03-24 04:41:43 +00001243 Select(N.getOperand(0)); //chain
Nate Begeman1cbf3ab2005-04-18 07:48:09 +00001244 CCReg = SelectCC(N.getOperand(1), Opc, Inv, Idx);
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001245
Nate Begeman439009c2005-06-15 18:22:43 +00001246 // Iterate to the next basic block
1247 ilist<MachineBasicBlock>::iterator It = BB;
1248 ++It;
Nate Begemancd08e4c2005-04-09 20:09:12 +00001249
1250 // If this is a two way branch, then grab the fallthrough basic block argument
1251 // and build a PowerPC branch pseudo-op, suitable for long branch conversion
1252 // if necessary by the branch selection pass. Otherwise, emit a standard
1253 // conditional branch.
1254 if (N.getOpcode() == ISD::BRCONDTWOWAY) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001255 MachineBasicBlock *Fallthrough =
Nate Begemancd08e4c2005-04-09 20:09:12 +00001256 cast<BasicBlockSDNode>(N.getOperand(3))->getBasicBlock();
1257 if (Dest != It) {
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001258 BuildMI(BB, PPC::COND_BRANCH, 4).addReg(CCReg).addImm(Opc)
Nate Begemancd08e4c2005-04-09 20:09:12 +00001259 .addMBB(Dest).addMBB(Fallthrough);
1260 if (Fallthrough != It)
1261 BuildMI(BB, PPC::B, 1).addMBB(Fallthrough);
1262 } else {
1263 if (Fallthrough != It) {
1264 Opc = PPC32InstrInfo::invertPPCBranchOpcode(Opc);
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001265 BuildMI(BB, PPC::COND_BRANCH, 4).addReg(CCReg).addImm(Opc)
Nate Begemancd08e4c2005-04-09 20:09:12 +00001266 .addMBB(Fallthrough).addMBB(Dest);
1267 }
1268 }
1269 } else {
Nate Begeman439009c2005-06-15 18:22:43 +00001270 // If the fallthrough path is off the end of the function, which would be
1271 // undefined behavior, set it to be the same as the current block because
1272 // we have nothing better to set it to, and leaving it alone will cause the
1273 // PowerPC Branch Selection pass to crash.
1274 if (It == BB->getParent()->end()) It = Dest;
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00001275 BuildMI(BB, PPC::COND_BRANCH, 4).addReg(CCReg).addImm(Opc)
Nate Begeman27499e32005-04-10 01:48:29 +00001276 .addMBB(Dest).addMBB(It);
Nate Begemancd08e4c2005-04-09 20:09:12 +00001277 }
Nate Begemana9795f82005-03-24 04:41:43 +00001278 return;
1279}
1280
Chris Lattner0d7d99f2005-08-10 16:34:52 +00001281// SelectIntImmediateExpr - Choose code for opcodes with immediate value.
1282// Note: immediate constant must be second operand so that the use count can be
1283// determined.
1284bool ISel::SelectIntImmediateExpr(SDOperand N, unsigned Result, unsigned C,
1285 unsigned OCHi, unsigned OCLo,
1286 bool IsArithmetic) {
1287 // get the hi and lo portions of constant
1288 unsigned Hi = IsArithmetic ? HA16(C) : Hi16(C);
1289 unsigned Lo = Lo16(C);
1290 // assume no intermediate result from lo instruction (same as final result)
1291 unsigned Tmp = Result;
1292 // check if two instructions are needed
1293 if (Hi && Lo) {
1294 // exit if usage indicates it would be better to load immediate into a
1295 // register
1296 if (dyn_cast<ConstantSDNode>(N.getOperand(1))->use_size() > 2)
1297 return false;
1298 // need intermediate result for two instructions
1299 Tmp = MakeReg(MVT::i32);
1300 }
1301 // get first operand
1302 unsigned Opr0 = SelectExpr(N.getOperand(0));
1303 // is a lo instruction needed
1304 if (Lo) {
1305 // generate instruction for hi portion
1306 const MachineInstrBuilder &MIBLo = BuildMI(BB, OCLo, 2, Tmp).addReg(Opr0);
1307 if (IsArithmetic) MIBLo.addSImm(Lo); else MIBLo.addImm(Lo);
1308 // need to switch out first operand for hi instruction
1309 Opr0 = Tmp;
1310 }
1311 // is a ho instruction needed
1312 if (Hi) {
1313 // generate instruction for hi portion
1314 const MachineInstrBuilder &MIBHi = BuildMI(BB, OCHi, 2, Result).addReg(Opr0);
1315 if (IsArithmetic) MIBHi.addSImm(Hi); else MIBHi.addImm(Hi);
1316 }
1317 return true;
1318}
1319
Nate Begemanc7bd4822005-04-11 06:34:10 +00001320unsigned ISel::SelectExpr(SDOperand N, bool Recording) {
Nate Begemana9795f82005-03-24 04:41:43 +00001321 unsigned Result;
1322 unsigned Tmp1, Tmp2, Tmp3;
1323 unsigned Opc = 0;
1324 unsigned opcode = N.getOpcode();
1325
1326 SDNode *Node = N.Val;
1327 MVT::ValueType DestType = N.getValueType();
1328
Nate Begemana43b1762005-06-14 03:55:23 +00001329 if (Node->getOpcode() == ISD::CopyFromReg &&
Chris Lattner988b1dd2005-07-28 05:23:43 +00001330 (MRegisterInfo::isVirtualRegister(cast<RegSDNode>(Node)->getReg()) ||
1331 cast<RegSDNode>(Node)->getReg() == PPC::R1))
Nate Begemana43b1762005-06-14 03:55:23 +00001332 // Just use the specified register as our input.
1333 return cast<RegSDNode>(Node)->getReg();
1334
Nate Begemana9795f82005-03-24 04:41:43 +00001335 unsigned &Reg = ExprMap[N];
1336 if (Reg) return Reg;
1337
Nate Begeman27eeb002005-04-02 05:59:34 +00001338 switch (N.getOpcode()) {
1339 default:
Nate Begemana9795f82005-03-24 04:41:43 +00001340 Reg = Result = (N.getValueType() != MVT::Other) ?
Nate Begeman27eeb002005-04-02 05:59:34 +00001341 MakeReg(N.getValueType()) : 1;
1342 break;
Chris Lattnerb5d8e6e2005-05-13 20:29:26 +00001343 case ISD::TAILCALL:
Nate Begeman27eeb002005-04-02 05:59:34 +00001344 case ISD::CALL:
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001345 // If this is a call instruction, make sure to prepare ALL of the result
1346 // values as well as the chain.
Nate Begeman27eeb002005-04-02 05:59:34 +00001347 if (Node->getNumValues() == 1)
1348 Reg = Result = 1; // Void call, just a chain.
1349 else {
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001350 Result = MakeReg(Node->getValueType(0));
1351 ExprMap[N.getValue(0)] = Result;
Nate Begeman27eeb002005-04-02 05:59:34 +00001352 for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001353 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
Nate Begeman27eeb002005-04-02 05:59:34 +00001354 ExprMap[SDOperand(Node, Node->getNumValues()-1)] = 1;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001355 }
Nate Begeman27eeb002005-04-02 05:59:34 +00001356 break;
1357 case ISD::ADD_PARTS:
1358 case ISD::SUB_PARTS:
1359 case ISD::SHL_PARTS:
1360 case ISD::SRL_PARTS:
1361 case ISD::SRA_PARTS:
1362 Result = MakeReg(Node->getValueType(0));
1363 ExprMap[N.getValue(0)] = Result;
1364 for (unsigned i = 1, e = N.Val->getNumValues(); i != e; ++i)
1365 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
1366 break;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001367 }
1368
Nate Begemana9795f82005-03-24 04:41:43 +00001369 switch (opcode) {
1370 default:
1371 Node->dump();
1372 assert(0 && "Node not handled!\n");
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001373 case ISD::UNDEF:
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001374 BuildMI(BB, PPC::IMPLICIT_DEF, 0, Result);
1375 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001376 case ISD::DYNAMIC_STACKALLOC:
Nate Begeman5e966612005-03-24 06:28:42 +00001377 // Generate both result values. FIXME: Need a better commment here?
1378 if (Result != 1)
1379 ExprMap[N.getValue(1)] = 1;
1380 else
1381 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1382
1383 // FIXME: We are currently ignoring the requested alignment for handling
1384 // greater than the stack alignment. This will need to be revisited at some
1385 // point. Align = N.getOperand(2);
1386 if (!isa<ConstantSDNode>(N.getOperand(2)) ||
1387 cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) {
1388 std::cerr << "Cannot allocate stack object with greater alignment than"
1389 << " the stack alignment yet!";
1390 abort();
1391 }
1392 Select(N.getOperand(0));
1393 Tmp1 = SelectExpr(N.getOperand(1));
1394 // Subtract size from stack pointer, thereby allocating some space.
1395 BuildMI(BB, PPC::SUBF, 2, PPC::R1).addReg(Tmp1).addReg(PPC::R1);
1396 // Put a pointer to the space into the result register by copying the SP
1397 BuildMI(BB, PPC::OR, 2, Result).addReg(PPC::R1).addReg(PPC::R1);
1398 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001399
1400 case ISD::ConstantPool:
Nate Begemanca12a2b2005-03-28 22:28:37 +00001401 Tmp1 = cast<ConstantPoolSDNode>(N)->getIndex();
1402 Tmp2 = MakeReg(MVT::i32);
Nate Begeman2497e632005-07-21 20:44:43 +00001403 if (PICEnabled)
1404 BuildMI(BB, PPC::ADDIS, 2, Tmp2).addReg(getGlobalBaseReg())
1405 .addConstantPoolIndex(Tmp1);
1406 else
1407 BuildMI(BB, PPC::LIS, 1, Tmp2).addConstantPoolIndex(Tmp1);
Nate Begemanca12a2b2005-03-28 22:28:37 +00001408 BuildMI(BB, PPC::LA, 2, Result).addReg(Tmp2).addConstantPoolIndex(Tmp1);
1409 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001410
1411 case ISD::FrameIndex:
Nate Begemanf3d08f32005-03-29 00:03:27 +00001412 Tmp1 = cast<FrameIndexSDNode>(N)->getIndex();
Nate Begeman58f718c2005-03-30 02:23:08 +00001413 addFrameReference(BuildMI(BB, PPC::ADDI, 2, Result), (int)Tmp1, 0, false);
Nate Begemanf3d08f32005-03-29 00:03:27 +00001414 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001415
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001416 case ISD::GlobalAddress: {
1417 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
Nate Begemanca12a2b2005-03-28 22:28:37 +00001418 Tmp1 = MakeReg(MVT::i32);
Nate Begeman2497e632005-07-21 20:44:43 +00001419 if (PICEnabled)
1420 BuildMI(BB, PPC::ADDIS, 2, Tmp1).addReg(getGlobalBaseReg())
1421 .addGlobalAddress(GV);
1422 else
Chris Lattner4015ea82005-07-28 04:42:11 +00001423 BuildMI(BB, PPC::LIS, 1, Tmp1).addGlobalAddress(GV);
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001424 if (GV->hasWeakLinkage() || GV->isExternal()) {
1425 BuildMI(BB, PPC::LWZ, 2, Result).addGlobalAddress(GV).addReg(Tmp1);
1426 } else {
1427 BuildMI(BB, PPC::LA, 2, Result).addReg(Tmp1).addGlobalAddress(GV);
1428 }
1429 return Result;
1430 }
1431
Nate Begeman5e966612005-03-24 06:28:42 +00001432 case ISD::LOAD:
Nate Begemana9795f82005-03-24 04:41:43 +00001433 case ISD::EXTLOAD:
1434 case ISD::ZEXTLOAD:
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001435 case ISD::SEXTLOAD: {
Nate Begeman9db505c2005-03-28 19:36:43 +00001436 MVT::ValueType TypeBeingLoaded = (ISD::LOAD == opcode) ?
Chris Lattnerbce81ae2005-07-10 01:56:13 +00001437 Node->getValueType(0) : cast<VTSDNode>(Node->getOperand(3))->getVT();
Nate Begeman74d73452005-03-31 00:15:26 +00001438 bool sext = (ISD::SEXTLOAD == opcode);
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001439
Nate Begeman5e966612005-03-24 06:28:42 +00001440 // Make sure we generate both values.
1441 if (Result != 1)
1442 ExprMap[N.getValue(1)] = 1; // Generate the token
1443 else
1444 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1445
1446 SDOperand Chain = N.getOperand(0);
1447 SDOperand Address = N.getOperand(1);
1448 Select(Chain);
1449
Nate Begeman9db505c2005-03-28 19:36:43 +00001450 switch (TypeBeingLoaded) {
Nate Begeman74d73452005-03-31 00:15:26 +00001451 default: Node->dump(); assert(0 && "Cannot load this type!");
Nate Begeman9db505c2005-03-28 19:36:43 +00001452 case MVT::i1: Opc = PPC::LBZ; break;
1453 case MVT::i8: Opc = PPC::LBZ; break;
1454 case MVT::i16: Opc = sext ? PPC::LHA : PPC::LHZ; break;
1455 case MVT::i32: Opc = PPC::LWZ; break;
Nate Begeman74d73452005-03-31 00:15:26 +00001456 case MVT::f32: Opc = PPC::LFS; break;
1457 case MVT::f64: Opc = PPC::LFD; break;
Nate Begeman5e966612005-03-24 06:28:42 +00001458 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001459
Nate Begeman74d73452005-03-31 00:15:26 +00001460 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Address)) {
1461 Tmp1 = MakeReg(MVT::i32);
1462 int CPI = CP->getIndex();
Nate Begeman2497e632005-07-21 20:44:43 +00001463 if (PICEnabled)
1464 BuildMI(BB, PPC::ADDIS, 2, Tmp1).addReg(getGlobalBaseReg())
1465 .addConstantPoolIndex(CPI);
1466 else
1467 BuildMI(BB, PPC::LIS, 1, Tmp1).addConstantPoolIndex(CPI);
Nate Begeman74d73452005-03-31 00:15:26 +00001468 BuildMI(BB, Opc, 2, Result).addConstantPoolIndex(CPI).addReg(Tmp1);
Nate Begeman2497e632005-07-21 20:44:43 +00001469 } else if (Address.getOpcode() == ISD::FrameIndex) {
Nate Begeman58f718c2005-03-30 02:23:08 +00001470 Tmp1 = cast<FrameIndexSDNode>(Address)->getIndex();
1471 addFrameReference(BuildMI(BB, Opc, 2, Result), (int)Tmp1);
Nate Begeman5e966612005-03-24 06:28:42 +00001472 } else {
1473 int offset;
Nate Begeman2a05c8e2005-07-28 03:02:05 +00001474 switch(SelectAddr(Address, Tmp1, offset)) {
1475 default: assert(0 && "Unhandled return value from SelectAddr");
1476 case 0: // imm offset, no frame, no index
1477 BuildMI(BB, Opc, 2, Result).addSImm(offset).addReg(Tmp1);
1478 break;
1479 case 1: // imm offset + frame index
1480 addFrameReference(BuildMI(BB, Opc, 2, Result), (int)Tmp1, offset);
1481 break;
1482 case 2: // base+index addressing
Nate Begeman04730362005-04-01 04:45:11 +00001483 Opc = IndexedOpForOp(Opc);
1484 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(offset);
Nate Begeman2a05c8e2005-07-28 03:02:05 +00001485 break;
Nate Begemand3ded2d2005-08-08 22:22:56 +00001486 case 3: {
1487 GlobalAddressSDNode *GN = cast<GlobalAddressSDNode>(Address);
1488 GlobalValue *GV = GN->getGlobal();
1489 BuildMI(BB, Opc, 2, Result).addGlobalAddress(GV).addReg(Tmp1);
1490 }
Nate Begeman04730362005-04-01 04:45:11 +00001491 }
Nate Begeman5e966612005-03-24 06:28:42 +00001492 }
1493 return Result;
1494 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001495
Chris Lattnerb5d8e6e2005-05-13 20:29:26 +00001496 case ISD::TAILCALL:
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001497 case ISD::CALL: {
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001498 unsigned GPR_idx = 0, FPR_idx = 0;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001499 static const unsigned GPR[] = {
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001500 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
1501 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
1502 };
1503 static const unsigned FPR[] = {
1504 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
1505 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
1506 };
1507
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001508 // Lower the chain for this call.
1509 Select(N.getOperand(0));
1510 ExprMap[N.getValue(Node->getNumValues()-1)] = 1;
Nate Begeman74d73452005-03-31 00:15:26 +00001511
Nate Begemand860aa62005-04-04 22:17:48 +00001512 MachineInstr *CallMI;
1513 // Emit the correct call instruction based on the type of symbol called.
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001514 if (GlobalAddressSDNode *GASD =
Nate Begemand860aa62005-04-04 22:17:48 +00001515 dyn_cast<GlobalAddressSDNode>(N.getOperand(1))) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001516 CallMI = BuildMI(PPC::CALLpcrel, 1).addGlobalAddress(GASD->getGlobal(),
Nate Begemand860aa62005-04-04 22:17:48 +00001517 true);
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001518 } else if (ExternalSymbolSDNode *ESSDN =
Nate Begemand860aa62005-04-04 22:17:48 +00001519 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1))) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001520 CallMI = BuildMI(PPC::CALLpcrel, 1).addExternalSymbol(ESSDN->getSymbol(),
Nate Begemand860aa62005-04-04 22:17:48 +00001521 true);
1522 } else {
1523 Tmp1 = SelectExpr(N.getOperand(1));
1524 BuildMI(BB, PPC::OR, 2, PPC::R12).addReg(Tmp1).addReg(Tmp1);
1525 BuildMI(BB, PPC::MTCTR, 1).addReg(PPC::R12);
1526 CallMI = BuildMI(PPC::CALLindirect, 3).addImm(20).addImm(0)
1527 .addReg(PPC::R12);
1528 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001529
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001530 // Load the register args to virtual regs
1531 std::vector<unsigned> ArgVR;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001532 for(int i = 2, e = Node->getNumOperands(); i < e; ++i)
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001533 ArgVR.push_back(SelectExpr(N.getOperand(i)));
1534
1535 // Copy the virtual registers into the appropriate argument register
1536 for(int i = 0, e = ArgVR.size(); i < e; ++i) {
1537 switch(N.getOperand(i+2).getValueType()) {
1538 default: Node->dump(); assert(0 && "Unknown value type for call");
1539 case MVT::i1:
1540 case MVT::i8:
1541 case MVT::i16:
1542 case MVT::i32:
1543 assert(GPR_idx < 8 && "Too many int args");
Nate Begemand860aa62005-04-04 22:17:48 +00001544 if (N.getOperand(i+2).getOpcode() != ISD::UNDEF) {
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001545 BuildMI(BB, PPC::OR,2,GPR[GPR_idx]).addReg(ArgVR[i]).addReg(ArgVR[i]);
Nate Begemand860aa62005-04-04 22:17:48 +00001546 CallMI->addRegOperand(GPR[GPR_idx], MachineOperand::Use);
1547 }
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001548 ++GPR_idx;
1549 break;
1550 case MVT::f64:
1551 case MVT::f32:
1552 assert(FPR_idx < 13 && "Too many fp args");
1553 BuildMI(BB, PPC::FMR, 1, FPR[FPR_idx]).addReg(ArgVR[i]);
Nate Begemand860aa62005-04-04 22:17:48 +00001554 CallMI->addRegOperand(FPR[FPR_idx], MachineOperand::Use);
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001555 ++FPR_idx;
1556 break;
1557 }
1558 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001559
Nate Begemand860aa62005-04-04 22:17:48 +00001560 // Put the call instruction in the correct place in the MachineBasicBlock
1561 BB->push_back(CallMI);
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001562
1563 switch (Node->getValueType(0)) {
1564 default: assert(0 && "Unknown value type for call result!");
1565 case MVT::Other: return 1;
1566 case MVT::i1:
1567 case MVT::i8:
1568 case MVT::i16:
1569 case MVT::i32:
Nate Begemane5846682005-04-04 06:52:38 +00001570 if (Node->getValueType(1) == MVT::i32) {
1571 BuildMI(BB, PPC::OR, 2, Result+1).addReg(PPC::R3).addReg(PPC::R3);
1572 BuildMI(BB, PPC::OR, 2, Result).addReg(PPC::R4).addReg(PPC::R4);
1573 } else {
1574 BuildMI(BB, PPC::OR, 2, Result).addReg(PPC::R3).addReg(PPC::R3);
1575 }
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001576 break;
1577 case MVT::f32:
1578 case MVT::f64:
1579 BuildMI(BB, PPC::FMR, 1, Result).addReg(PPC::F1);
1580 break;
1581 }
1582 return Result+N.ResNo;
1583 }
Nate Begemana9795f82005-03-24 04:41:43 +00001584
1585 case ISD::SIGN_EXTEND:
1586 case ISD::SIGN_EXTEND_INREG:
1587 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattnerbce81ae2005-07-10 01:56:13 +00001588 switch(cast<VTSDNode>(Node->getOperand(1))->getVT()) {
Nate Begeman9db505c2005-03-28 19:36:43 +00001589 default: Node->dump(); assert(0 && "Unhandled SIGN_EXTEND type"); break;
Nate Begemanc7bd4822005-04-11 06:34:10 +00001590 case MVT::i16:
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001591 BuildMI(BB, PPC::EXTSH, 1, Result).addReg(Tmp1);
Nate Begeman9db505c2005-03-28 19:36:43 +00001592 break;
Nate Begemanc7bd4822005-04-11 06:34:10 +00001593 case MVT::i8:
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001594 BuildMI(BB, PPC::EXTSB, 1, Result).addReg(Tmp1);
Nate Begeman9db505c2005-03-28 19:36:43 +00001595 break;
Nate Begeman74747862005-03-29 22:24:51 +00001596 case MVT::i1:
1597 BuildMI(BB, PPC::SUBFIC, 2, Result).addReg(Tmp1).addSImm(0);
1598 break;
Nate Begeman9db505c2005-03-28 19:36:43 +00001599 }
Nate Begemana9795f82005-03-24 04:41:43 +00001600 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001601
Nate Begemana9795f82005-03-24 04:41:43 +00001602 case ISD::CopyFromReg:
Nate Begemana3fd4002005-07-19 16:51:05 +00001603 DestType = N.getValue(0).getValueType();
Nate Begemana9795f82005-03-24 04:41:43 +00001604 if (Result == 1)
Nate Begemana3fd4002005-07-19 16:51:05 +00001605 Result = ExprMap[N.getValue(0)] = MakeReg(DestType);
Nate Begemana9795f82005-03-24 04:41:43 +00001606 Tmp1 = dyn_cast<RegSDNode>(Node)->getReg();
Nate Begemana3fd4002005-07-19 16:51:05 +00001607 if (MVT::isInteger(DestType))
1608 BuildMI(BB, PPC::OR, 2, Result).addReg(Tmp1).addReg(Tmp1);
1609 else
1610 BuildMI(BB, PPC::FMR, 1, Result).addReg(Tmp1);
Nate Begemana9795f82005-03-24 04:41:43 +00001611 return Result;
1612
1613 case ISD::SHL:
Nate Begeman5e966612005-03-24 06:28:42 +00001614 Tmp1 = SelectExpr(N.getOperand(0));
1615 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1616 Tmp2 = CN->getValue() & 0x1F;
Nate Begeman33162522005-03-29 21:54:38 +00001617 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(Tmp2).addImm(0)
Nate Begeman5e966612005-03-24 06:28:42 +00001618 .addImm(31-Tmp2);
1619 } else {
Nate Begeman3664cef2005-04-13 22:14:14 +00001620 Tmp2 = FoldIfWideZeroExtend(N.getOperand(1));
Nate Begeman5e966612005-03-24 06:28:42 +00001621 BuildMI(BB, PPC::SLW, 2, Result).addReg(Tmp1).addReg(Tmp2);
1622 }
1623 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001624
Nate Begeman5e966612005-03-24 06:28:42 +00001625 case ISD::SRL:
1626 Tmp1 = SelectExpr(N.getOperand(0));
1627 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1628 Tmp2 = CN->getValue() & 0x1F;
Nate Begeman33162522005-03-29 21:54:38 +00001629 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(32-Tmp2)
Nate Begeman5e966612005-03-24 06:28:42 +00001630 .addImm(Tmp2).addImm(31);
1631 } else {
Nate Begeman3664cef2005-04-13 22:14:14 +00001632 Tmp2 = FoldIfWideZeroExtend(N.getOperand(1));
Nate Begeman5e966612005-03-24 06:28:42 +00001633 BuildMI(BB, PPC::SRW, 2, Result).addReg(Tmp1).addReg(Tmp2);
1634 }
1635 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001636
Nate Begeman5e966612005-03-24 06:28:42 +00001637 case ISD::SRA:
1638 Tmp1 = SelectExpr(N.getOperand(0));
1639 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1640 Tmp2 = CN->getValue() & 0x1F;
1641 BuildMI(BB, PPC::SRAWI, 2, Result).addReg(Tmp1).addImm(Tmp2);
1642 } else {
Nate Begeman3664cef2005-04-13 22:14:14 +00001643 Tmp2 = FoldIfWideZeroExtend(N.getOperand(1));
Nate Begeman5e966612005-03-24 06:28:42 +00001644 BuildMI(BB, PPC::SRAW, 2, Result).addReg(Tmp1).addReg(Tmp2);
1645 }
1646 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001647
Nate Begemand7c4a4a2005-05-11 23:43:56 +00001648 case ISD::CTLZ:
1649 Tmp1 = SelectExpr(N.getOperand(0));
1650 BuildMI(BB, PPC::CNTLZW, 1, Result).addReg(Tmp1);
1651 return Result;
1652
Nate Begemana9795f82005-03-24 04:41:43 +00001653 case ISD::ADD:
Nate Begemana3fd4002005-07-19 16:51:05 +00001654 if (!MVT::isInteger(DestType)) {
1655 if (!NoExcessFPPrecision && N.getOperand(0).getOpcode() == ISD::MUL &&
1656 N.getOperand(0).Val->hasOneUse()) {
1657 ++FusedFP; // Statistic
1658 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1659 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1660 Tmp3 = SelectExpr(N.getOperand(1));
1661 Opc = DestType == MVT::f64 ? PPC::FMADD : PPC::FMADDS;
1662 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1663 return Result;
1664 }
1665 if (!NoExcessFPPrecision && N.getOperand(1).getOpcode() == ISD::MUL &&
1666 N.getOperand(1).Val->hasOneUse()) {
1667 ++FusedFP; // Statistic
1668 Tmp1 = SelectExpr(N.getOperand(1).getOperand(0));
1669 Tmp2 = SelectExpr(N.getOperand(1).getOperand(1));
1670 Tmp3 = SelectExpr(N.getOperand(0));
1671 Opc = DestType == MVT::f64 ? PPC::FMADD : PPC::FMADDS;
1672 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1673 return Result;
1674 }
1675 Opc = DestType == MVT::f64 ? PPC::FADD : PPC::FADDS;
1676 Tmp1 = SelectExpr(N.getOperand(0));
1677 Tmp2 = SelectExpr(N.getOperand(1));
1678 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1679 return Result;
1680 }
Chris Lattner59b21c22005-08-09 18:29:55 +00001681 if (isIntImmediate(N.getOperand(1), Tmp2)) {
Chris Lattner0d7d99f2005-08-10 16:34:52 +00001682 if (SelectIntImmediateExpr(N, Result, Tmp2, PPC::ADDIS, PPC::ADDI, true))
1683 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001684 }
Chris Lattner39c68962005-08-08 21:21:03 +00001685
Chris Lattner0d7d99f2005-08-10 16:34:52 +00001686 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner39c68962005-08-08 21:21:03 +00001687 Tmp2 = SelectExpr(N.getOperand(1));
1688 BuildMI(BB, PPC::ADD, 2, Result).addReg(Tmp1).addReg(Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +00001689 return Result;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001690
Nate Begemana9795f82005-03-24 04:41:43 +00001691 case ISD::AND:
Chris Lattner59b21c22005-08-09 18:29:55 +00001692 if (isIntImmediate(N.getOperand(1), Tmp2)) {
Chris Lattner2f57c4d2005-08-08 21:24:57 +00001693 if (isShiftedMask_32(Tmp2) || isShiftedMask_32(~Tmp2)) {
1694 unsigned SH, MB, ME;
1695 Opc = Recording ? PPC::RLWINMo : PPC::RLWINM;
1696 unsigned OprOpc;
1697 if (isOprShiftImm(N.getOperand(0), OprOpc, Tmp3) &&
1698 isRotateAndMask(OprOpc, Tmp3, Tmp2, false, SH, MB, ME)) {
Nate Begemand7c4a4a2005-05-11 23:43:56 +00001699 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
Chris Lattner2f57c4d2005-08-08 21:24:57 +00001700 } else {
1701 Tmp1 = SelectExpr(N.getOperand(0));
1702 isRunOfOnes(Tmp2, MB, ME);
1703 SH = 0;
Nate Begemand7c4a4a2005-05-11 23:43:56 +00001704 }
Chris Lattner2f57c4d2005-08-08 21:24:57 +00001705 BuildMI(BB, Opc, 4, Result).addReg(Tmp1).addImm(SH)
1706 .addImm(MB).addImm(ME);
1707 RecordSuccess = true;
1708 return Result;
1709 } else if (isUInt16(Tmp2)) {
1710 Tmp2 = Lo16(Tmp2);
Chris Lattnercafb67b2005-05-09 17:39:48 +00001711 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman7ddecb42005-04-06 23:51:40 +00001712 BuildMI(BB, PPC::ANDIo, 2, Result).addReg(Tmp1).addImm(Tmp2);
Chris Lattner2f57c4d2005-08-08 21:24:57 +00001713 RecordSuccess = true;
1714 return Result;
1715 } else if (isUInt16(Tmp2)) {
1716 Tmp2 = Hi16(Tmp2);
Chris Lattnercafb67b2005-05-09 17:39:48 +00001717 Tmp1 = SelectExpr(N.getOperand(0));
Nate Begeman7ddecb42005-04-06 23:51:40 +00001718 BuildMI(BB, PPC::ANDISo, 2, Result).addReg(Tmp1).addImm(Tmp2);
Chris Lattner2f57c4d2005-08-08 21:24:57 +00001719 RecordSuccess = true;
1720 return Result;
1721 }
Nate Begeman7ddecb42005-04-06 23:51:40 +00001722 }
Chris Lattner2f57c4d2005-08-08 21:24:57 +00001723 if (isOprNot(N.getOperand(0))) {
1724 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1725 Tmp2 = SelectExpr(N.getOperand(1));
1726 BuildMI(BB, PPC::ANDC, 2, Result).addReg(Tmp2).addReg(Tmp1);
1727 RecordSuccess = false;
1728 return Result;
1729 }
1730 // emit a regular and
1731 Tmp1 = SelectExpr(N.getOperand(0));
1732 Tmp2 = SelectExpr(N.getOperand(1));
1733 Opc = Recording ? PPC::ANDo : PPC::AND;
1734 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
Nate Begemanc7bd4822005-04-11 06:34:10 +00001735 RecordSuccess = true;
Nate Begeman7ddecb42005-04-06 23:51:40 +00001736 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001737
Nate Begemana9795f82005-03-24 04:41:43 +00001738 case ISD::OR:
Nate Begeman7ddecb42005-04-06 23:51:40 +00001739 if (SelectBitfieldInsert(N, Result))
1740 return Result;
Chris Lattner59b21c22005-08-09 18:29:55 +00001741 if (isIntImmediate(N.getOperand(1), Tmp2)) {
Chris Lattner0d7d99f2005-08-10 16:34:52 +00001742 if (SelectIntImmediateExpr(N, Result, Tmp2, PPC::ORIS, PPC::ORI, false))
1743 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00001744 }
Chris Lattner0d7d99f2005-08-10 16:34:52 +00001745 // emit regular or
1746 Tmp1 = SelectExpr(N.getOperand(0));
1747 Tmp2 = SelectExpr(N.getOperand(1));
1748 Opc = Recording ? PPC::ORo : PPC::OR;
1749 RecordSuccess = true;
1750 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
Nate Begemana9795f82005-03-24 04:41:43 +00001751 return Result;
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001752
Nate Begemanaa73a9f2005-04-03 11:20:20 +00001753 case ISD::XOR: {
1754 // Check for EQV: xor, (xor a, -1), b
1755 if (N.getOperand(0).getOpcode() == ISD::XOR &&
Chris Lattner59b21c22005-08-09 18:29:55 +00001756 isIntImmediate(N.getOperand(0).getOperand(1), Tmp2) &&
Chris Lattner5b909172005-08-08 21:30:29 +00001757 (signed)Tmp2 == -1) {
Nate Begemanaa73a9f2005-04-03 11:20:20 +00001758 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1759 Tmp2 = SelectExpr(N.getOperand(1));
1760 BuildMI(BB, PPC::EQV, 2, Result).addReg(Tmp1).addReg(Tmp2);
1761 return Result;
1762 }
Chris Lattner837a5212005-04-21 21:09:11 +00001763 // Check for NOT, NOR, EQV, and NAND: xor (copy, or, xor, and), -1
Chris Lattner5b909172005-08-08 21:30:29 +00001764 if (isOprNot(N)) {
Nate Begemanaa73a9f2005-04-03 11:20:20 +00001765 switch(N.getOperand(0).getOpcode()) {
1766 case ISD::OR:
1767 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1768 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1769 BuildMI(BB, PPC::NOR, 2, Result).addReg(Tmp1).addReg(Tmp2);
1770 break;
1771 case ISD::AND:
1772 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1773 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1774 BuildMI(BB, PPC::NAND, 2, Result).addReg(Tmp1).addReg(Tmp2);
1775 break;
Chris Lattner837a5212005-04-21 21:09:11 +00001776 case ISD::XOR:
1777 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1778 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1779 BuildMI(BB, PPC::EQV, 2, Result).addReg(Tmp1).addReg(Tmp2);
1780 break;
Nate Begemanaa73a9f2005-04-03 11:20:20 +00001781 default:
1782 Tmp1 = SelectExpr(N.getOperand(0));
1783 BuildMI(BB, PPC::NOR, 2, Result).addReg(Tmp1).addReg(Tmp1);
1784 break;
1785 }
1786 return Result;
1787 }
Chris Lattner59b21c22005-08-09 18:29:55 +00001788 if (isIntImmediate(N.getOperand(1), Tmp2)) {
Chris Lattner0d7d99f2005-08-10 16:34:52 +00001789 if (SelectIntImmediateExpr(N, Result, Tmp2, PPC::XORIS, PPC::XORI, false))
1790 return Result;
Nate Begemanaa73a9f2005-04-03 11:20:20 +00001791 }
Chris Lattner0d7d99f2005-08-10 16:34:52 +00001792 // emit regular xor
1793 Tmp1 = SelectExpr(N.getOperand(0));
1794 Tmp2 = SelectExpr(N.getOperand(1));
1795 BuildMI(BB, PPC::XOR, 2, Result).addReg(Tmp1).addReg(Tmp2);
Nate Begemanaa73a9f2005-04-03 11:20:20 +00001796 return Result;
1797 }
1798
Chris Lattner5b909172005-08-08 21:30:29 +00001799 case ISD::SUB:
Nate Begemana3fd4002005-07-19 16:51:05 +00001800 if (!MVT::isInteger(DestType)) {
1801 if (!NoExcessFPPrecision && N.getOperand(0).getOpcode() == ISD::MUL &&
1802 N.getOperand(0).Val->hasOneUse()) {
1803 ++FusedFP; // Statistic
1804 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1805 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1806 Tmp3 = SelectExpr(N.getOperand(1));
1807 Opc = DestType == MVT::f64 ? PPC::FMSUB : PPC::FMSUBS;
1808 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1809 return Result;
1810 }
1811 if (!NoExcessFPPrecision && N.getOperand(1).getOpcode() == ISD::MUL &&
1812 N.getOperand(1).Val->hasOneUse()) {
1813 ++FusedFP; // Statistic
1814 Tmp1 = SelectExpr(N.getOperand(1).getOperand(0));
1815 Tmp2 = SelectExpr(N.getOperand(1).getOperand(1));
1816 Tmp3 = SelectExpr(N.getOperand(0));
1817 Opc = DestType == MVT::f64 ? PPC::FNMSUB : PPC::FNMSUBS;
1818 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
1819 return Result;
1820 }
1821 Opc = DestType == MVT::f64 ? PPC::FSUB : PPC::FSUBS;
1822 Tmp1 = SelectExpr(N.getOperand(0));
1823 Tmp2 = SelectExpr(N.getOperand(1));
1824 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1825 return Result;
1826 }
Chris Lattner59b21c22005-08-09 18:29:55 +00001827 if (isIntImmediate(N.getOperand(0), Tmp1) && isInt16(Tmp1)) {
Nate Begemand7c4a4a2005-05-11 23:43:56 +00001828 Tmp2 = SelectExpr(N.getOperand(1));
Nate Begeman27523a12005-04-02 00:42:16 +00001829 BuildMI(BB, PPC::SUBFIC, 2, Result).addReg(Tmp2).addSImm(Tmp1);
Chris Lattner5b909172005-08-08 21:30:29 +00001830 return Result;
Chris Lattner59b21c22005-08-09 18:29:55 +00001831 } else if (isIntImmediate(N.getOperand(1), Tmp2)) {
Chris Lattner0d7d99f2005-08-10 16:34:52 +00001832 if (SelectIntImmediateExpr(N, Result, -Tmp2, PPC::ADDIS, PPC::ADDI, true))
1833 return Result;
Chris Lattner5b909172005-08-08 21:30:29 +00001834 }
1835 Tmp1 = SelectExpr(N.getOperand(0));
1836 Tmp2 = SelectExpr(N.getOperand(1));
1837 BuildMI(BB, PPC::SUBF, 2, Result).addReg(Tmp2).addReg(Tmp1);
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001838 return Result;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001839
Nate Begeman5e966612005-03-24 06:28:42 +00001840 case ISD::MUL:
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001841 Tmp1 = SelectExpr(N.getOperand(0));
Chris Lattner59b21c22005-08-09 18:29:55 +00001842 if (isIntImmediate(N.getOperand(1), Tmp2) && isInt16(Tmp2)) {
Chris Lattnerfd784542005-08-08 21:33:23 +00001843 Tmp2 = Lo16(Tmp2);
Nate Begeman307e7442005-03-26 01:28:53 +00001844 BuildMI(BB, PPC::MULLI, 2, Result).addReg(Tmp1).addSImm(Tmp2);
Chris Lattnerfd784542005-08-08 21:33:23 +00001845 } else {
Nate Begeman307e7442005-03-26 01:28:53 +00001846 Tmp2 = SelectExpr(N.getOperand(1));
Nate Begemana3fd4002005-07-19 16:51:05 +00001847 switch (DestType) {
1848 default: assert(0 && "Unknown type to ISD::MUL"); break;
1849 case MVT::i32: Opc = PPC::MULLW; break;
1850 case MVT::f32: Opc = PPC::FMULS; break;
1851 case MVT::f64: Opc = PPC::FMUL; break;
1852 }
1853 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
Nate Begeman307e7442005-03-26 01:28:53 +00001854 }
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001855 return Result;
1856
Nate Begeman815d6da2005-04-06 00:25:27 +00001857 case ISD::MULHS:
1858 case ISD::MULHU:
1859 Tmp1 = SelectExpr(N.getOperand(0));
1860 Tmp2 = SelectExpr(N.getOperand(1));
1861 Opc = (ISD::MULHU == opcode) ? PPC::MULHWU : PPC::MULHW;
1862 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1863 return Result;
1864
Nate Begemanf3d08f32005-03-29 00:03:27 +00001865 case ISD::SDIV:
Chris Lattner59b21c22005-08-09 18:29:55 +00001866 if (isIntImmediate(N.getOperand(1), Tmp3)) {
Chris Lattnerfd784542005-08-08 21:33:23 +00001867 if ((signed)Tmp3 > 0 && isPowerOf2_32(Tmp3)) {
1868 Tmp3 = Log2_32(Tmp3);
1869 Tmp1 = MakeReg(MVT::i32);
1870 Tmp2 = SelectExpr(N.getOperand(0));
Nate Begeman9f833d32005-04-12 00:10:02 +00001871 BuildMI(BB, PPC::SRAWI, 2, Tmp1).addReg(Tmp2).addImm(Tmp3);
1872 BuildMI(BB, PPC::ADDZE, 1, Result).addReg(Tmp1);
Chris Lattnerfd784542005-08-08 21:33:23 +00001873 return Result;
1874 } else if ((signed)Tmp3 < 0 && isPowerOf2_32(-Tmp3)) {
1875 Tmp3 = Log2_32(-Tmp3);
Chris Lattner2f460552005-08-09 18:08:41 +00001876 Tmp2 = SelectExpr(N.getOperand(0));
1877 Tmp1 = MakeReg(MVT::i32);
Chris Lattnerfd784542005-08-08 21:33:23 +00001878 unsigned Tmp4 = MakeReg(MVT::i32);
1879 BuildMI(BB, PPC::SRAWI, 2, Tmp1).addReg(Tmp2).addImm(Tmp3);
1880 BuildMI(BB, PPC::ADDZE, 1, Tmp4).addReg(Tmp1);
1881 BuildMI(BB, PPC::NEG, 1, Result).addReg(Tmp4);
1882 return Result;
Nate Begeman9f833d32005-04-12 00:10:02 +00001883 }
Chris Lattnerfd784542005-08-08 21:33:23 +00001884 }
1885 // fall thru
1886 case ISD::UDIV:
Nate Begeman815d6da2005-04-06 00:25:27 +00001887 // If this is a divide by constant, we can emit code using some magic
1888 // constants to implement it as a multiply instead.
Chris Lattner59b21c22005-08-09 18:29:55 +00001889 if (isIntImmediate(N.getOperand(1), Tmp3)) {
Chris Lattnerfd784542005-08-08 21:33:23 +00001890 if (opcode == ISD::SDIV) {
1891 if ((signed)Tmp3 < -1 || (signed)Tmp3 > 1) {
1892 ExprMap.erase(N);
1893 return SelectExpr(BuildSDIVSequence(N));
1894 }
1895 } else {
1896 if ((signed)Tmp3 > 1) {
1897 ExprMap.erase(N);
1898 return SelectExpr(BuildUDIVSequence(N));
1899 }
1900 }
Jeff Cohen00b168892005-07-27 06:12:32 +00001901 }
Nate Begemanf3d08f32005-03-29 00:03:27 +00001902 Tmp1 = SelectExpr(N.getOperand(0));
1903 Tmp2 = SelectExpr(N.getOperand(1));
Nate Begemana3fd4002005-07-19 16:51:05 +00001904 switch (DestType) {
1905 default: assert(0 && "Unknown type to ISD::SDIV"); break;
1906 case MVT::i32: Opc = (ISD::UDIV == opcode) ? PPC::DIVWU : PPC::DIVW; break;
1907 case MVT::f32: Opc = PPC::FDIVS; break;
1908 case MVT::f64: Opc = PPC::FDIV; break;
1909 }
Nate Begemanf3d08f32005-03-29 00:03:27 +00001910 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1911 return Result;
1912
Nate Begeman9e3e1b52005-03-24 23:35:30 +00001913 case ISD::ADD_PARTS:
Nate Begemanca12a2b2005-03-28 22:28:37 +00001914 case ISD::SUB_PARTS: {
1915 assert(N.getNumOperands() == 4 && N.getValueType() == MVT::i32 &&
1916 "Not an i64 add/sub!");
1917 // Emit all of the operands.
1918 std::vector<unsigned> InVals;
1919 for (unsigned i = 0, e = N.getNumOperands(); i != e; ++i)
1920 InVals.push_back(SelectExpr(N.getOperand(i)));
1921 if (N.getOpcode() == ISD::ADD_PARTS) {
Nate Begeman27eeb002005-04-02 05:59:34 +00001922 BuildMI(BB, PPC::ADDC, 2, Result).addReg(InVals[0]).addReg(InVals[2]);
1923 BuildMI(BB, PPC::ADDE, 2, Result+1).addReg(InVals[1]).addReg(InVals[3]);
Nate Begemanca12a2b2005-03-28 22:28:37 +00001924 } else {
Nate Begeman27eeb002005-04-02 05:59:34 +00001925 BuildMI(BB, PPC::SUBFC, 2, Result).addReg(InVals[2]).addReg(InVals[0]);
1926 BuildMI(BB, PPC::SUBFE, 2, Result+1).addReg(InVals[3]).addReg(InVals[1]);
1927 }
1928 return Result+N.ResNo;
1929 }
1930
1931 case ISD::SHL_PARTS:
1932 case ISD::SRA_PARTS:
1933 case ISD::SRL_PARTS: {
1934 assert(N.getNumOperands() == 3 && N.getValueType() == MVT::i32 &&
1935 "Not an i64 shift!");
1936 unsigned ShiftOpLo = SelectExpr(N.getOperand(0));
1937 unsigned ShiftOpHi = SelectExpr(N.getOperand(1));
Nate Begeman3664cef2005-04-13 22:14:14 +00001938 unsigned SHReg = FoldIfWideZeroExtend(N.getOperand(2));
1939 Tmp1 = MakeReg(MVT::i32);
1940 Tmp2 = MakeReg(MVT::i32);
Nate Begeman27eeb002005-04-02 05:59:34 +00001941 Tmp3 = MakeReg(MVT::i32);
1942 unsigned Tmp4 = MakeReg(MVT::i32);
1943 unsigned Tmp5 = MakeReg(MVT::i32);
1944 unsigned Tmp6 = MakeReg(MVT::i32);
1945 BuildMI(BB, PPC::SUBFIC, 2, Tmp1).addReg(SHReg).addSImm(32);
1946 if (ISD::SHL_PARTS == opcode) {
1947 BuildMI(BB, PPC::SLW, 2, Tmp2).addReg(ShiftOpHi).addReg(SHReg);
1948 BuildMI(BB, PPC::SRW, 2, Tmp3).addReg(ShiftOpLo).addReg(Tmp1);
1949 BuildMI(BB, PPC::OR, 2, Tmp4).addReg(Tmp2).addReg(Tmp3);
1950 BuildMI(BB, PPC::ADDI, 2, Tmp5).addReg(SHReg).addSImm(-32);
Nate Begemanfa554702005-04-03 22:13:27 +00001951 BuildMI(BB, PPC::SLW, 2, Tmp6).addReg(ShiftOpLo).addReg(Tmp5);
Nate Begeman27eeb002005-04-02 05:59:34 +00001952 BuildMI(BB, PPC::OR, 2, Result+1).addReg(Tmp4).addReg(Tmp6);
1953 BuildMI(BB, PPC::SLW, 2, Result).addReg(ShiftOpLo).addReg(SHReg);
1954 } else if (ISD::SRL_PARTS == opcode) {
1955 BuildMI(BB, PPC::SRW, 2, Tmp2).addReg(ShiftOpLo).addReg(SHReg);
1956 BuildMI(BB, PPC::SLW, 2, Tmp3).addReg(ShiftOpHi).addReg(Tmp1);
1957 BuildMI(BB, PPC::OR, 2, Tmp4).addReg(Tmp2).addReg(Tmp3);
1958 BuildMI(BB, PPC::ADDI, 2, Tmp5).addReg(SHReg).addSImm(-32);
1959 BuildMI(BB, PPC::SRW, 2, Tmp6).addReg(ShiftOpHi).addReg(Tmp5);
1960 BuildMI(BB, PPC::OR, 2, Result).addReg(Tmp4).addReg(Tmp6);
1961 BuildMI(BB, PPC::SRW, 2, Result+1).addReg(ShiftOpHi).addReg(SHReg);
1962 } else {
1963 MachineBasicBlock *TmpMBB = new MachineBasicBlock(BB->getBasicBlock());
1964 MachineBasicBlock *PhiMBB = new MachineBasicBlock(BB->getBasicBlock());
1965 MachineBasicBlock *OldMBB = BB;
1966 MachineFunction *F = BB->getParent();
1967 ilist<MachineBasicBlock>::iterator It = BB; ++It;
1968 F->getBasicBlockList().insert(It, TmpMBB);
1969 F->getBasicBlockList().insert(It, PhiMBB);
1970 BB->addSuccessor(TmpMBB);
1971 BB->addSuccessor(PhiMBB);
1972 BuildMI(BB, PPC::SRW, 2, Tmp2).addReg(ShiftOpLo).addReg(SHReg);
1973 BuildMI(BB, PPC::SLW, 2, Tmp3).addReg(ShiftOpHi).addReg(Tmp1);
1974 BuildMI(BB, PPC::OR, 2, Tmp4).addReg(Tmp2).addReg(Tmp3);
1975 BuildMI(BB, PPC::ADDICo, 2, Tmp5).addReg(SHReg).addSImm(-32);
1976 BuildMI(BB, PPC::SRAW, 2, Tmp6).addReg(ShiftOpHi).addReg(Tmp5);
1977 BuildMI(BB, PPC::SRAW, 2, Result+1).addReg(ShiftOpHi).addReg(SHReg);
1978 BuildMI(BB, PPC::BLE, 2).addReg(PPC::CR0).addMBB(PhiMBB);
1979 // Select correct least significant half if the shift amount > 32
1980 BB = TmpMBB;
1981 unsigned Tmp7 = MakeReg(MVT::i32);
1982 BuildMI(BB, PPC::OR, 2, Tmp7).addReg(Tmp6).addReg(Tmp6);
1983 TmpMBB->addSuccessor(PhiMBB);
1984 BB = PhiMBB;
1985 BuildMI(BB, PPC::PHI, 4, Result).addReg(Tmp4).addMBB(OldMBB)
1986 .addReg(Tmp7).addMBB(TmpMBB);
Nate Begemanca12a2b2005-03-28 22:28:37 +00001987 }
1988 return Result+N.ResNo;
1989 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001990
Nate Begemana9795f82005-03-24 04:41:43 +00001991 case ISD::FP_TO_UINT:
Nate Begeman6b559972005-04-01 02:59:27 +00001992 case ISD::FP_TO_SINT: {
1993 bool U = (ISD::FP_TO_UINT == opcode);
1994 Tmp1 = SelectExpr(N.getOperand(0));
1995 if (!U) {
1996 Tmp2 = MakeReg(MVT::f64);
1997 BuildMI(BB, PPC::FCTIWZ, 1, Tmp2).addReg(Tmp1);
1998 int FrameIdx = BB->getParent()->getFrameInfo()->CreateStackObject(8, 8);
1999 addFrameReference(BuildMI(BB, PPC::STFD, 3).addReg(Tmp2), FrameIdx);
2000 addFrameReference(BuildMI(BB, PPC::LWZ, 2, Result), FrameIdx, 4);
2001 return Result;
2002 } else {
2003 unsigned Zero = getConstDouble(0.0);
2004 unsigned MaxInt = getConstDouble((1LL << 32) - 1);
2005 unsigned Border = getConstDouble(1LL << 31);
2006 unsigned UseZero = MakeReg(MVT::f64);
2007 unsigned UseMaxInt = MakeReg(MVT::f64);
2008 unsigned UseChoice = MakeReg(MVT::f64);
2009 unsigned TmpReg = MakeReg(MVT::f64);
2010 unsigned TmpReg2 = MakeReg(MVT::f64);
2011 unsigned ConvReg = MakeReg(MVT::f64);
2012 unsigned IntTmp = MakeReg(MVT::i32);
2013 unsigned XorReg = MakeReg(MVT::i32);
2014 MachineFunction *F = BB->getParent();
2015 int FrameIdx = F->getFrameInfo()->CreateStackObject(8, 8);
2016 // Update machine-CFG edges
2017 MachineBasicBlock *XorMBB = new MachineBasicBlock(BB->getBasicBlock());
2018 MachineBasicBlock *PhiMBB = new MachineBasicBlock(BB->getBasicBlock());
2019 MachineBasicBlock *OldMBB = BB;
2020 ilist<MachineBasicBlock>::iterator It = BB; ++It;
2021 F->getBasicBlockList().insert(It, XorMBB);
2022 F->getBasicBlockList().insert(It, PhiMBB);
2023 BB->addSuccessor(XorMBB);
2024 BB->addSuccessor(PhiMBB);
2025 // Convert from floating point to unsigned 32-bit value
2026 // Use 0 if incoming value is < 0.0
2027 BuildMI(BB, PPC::FSEL, 3, UseZero).addReg(Tmp1).addReg(Tmp1).addReg(Zero);
2028 // Use 2**32 - 1 if incoming value is >= 2**32
2029 BuildMI(BB, PPC::FSUB, 2, UseMaxInt).addReg(MaxInt).addReg(Tmp1);
2030 BuildMI(BB, PPC::FSEL, 3, UseChoice).addReg(UseMaxInt).addReg(UseZero)
2031 .addReg(MaxInt);
2032 // Subtract 2**31
2033 BuildMI(BB, PPC::FSUB, 2, TmpReg).addReg(UseChoice).addReg(Border);
2034 // Use difference if >= 2**31
2035 BuildMI(BB, PPC::FCMPU, 2, PPC::CR0).addReg(UseChoice).addReg(Border);
2036 BuildMI(BB, PPC::FSEL, 3, TmpReg2).addReg(TmpReg).addReg(TmpReg)
2037 .addReg(UseChoice);
2038 // Convert to integer
2039 BuildMI(BB, PPC::FCTIWZ, 1, ConvReg).addReg(TmpReg2);
2040 addFrameReference(BuildMI(BB, PPC::STFD, 3).addReg(ConvReg), FrameIdx);
2041 addFrameReference(BuildMI(BB, PPC::LWZ, 2, IntTmp), FrameIdx, 4);
2042 BuildMI(BB, PPC::BLT, 2).addReg(PPC::CR0).addMBB(PhiMBB);
2043 BuildMI(BB, PPC::B, 1).addMBB(XorMBB);
2044
2045 // XorMBB:
2046 // add 2**31 if input was >= 2**31
2047 BB = XorMBB;
2048 BuildMI(BB, PPC::XORIS, 2, XorReg).addReg(IntTmp).addImm(0x8000);
2049 XorMBB->addSuccessor(PhiMBB);
2050
2051 // PhiMBB:
2052 // DestReg = phi [ IntTmp, OldMBB ], [ XorReg, XorMBB ]
2053 BB = PhiMBB;
2054 BuildMI(BB, PPC::PHI, 4, Result).addReg(IntTmp).addMBB(OldMBB)
2055 .addReg(XorReg).addMBB(XorMBB);
2056 return Result;
2057 }
2058 assert(0 && "Should never get here");
2059 return 0;
2060 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002061
Chris Lattner88ac32c2005-08-09 20:21:10 +00002062 case ISD::SETCC: {
2063 ISD::CondCode CC = cast<CondCodeSDNode>(Node->getOperand(2))->get();
2064 if (isIntImmediate(Node->getOperand(1), Tmp3)) {
2065 // We can codegen setcc op, imm very efficiently compared to a brcond.
2066 // Check for those cases here.
2067 // setcc op, 0
2068 if (Tmp3 == 0) {
2069 Tmp1 = SelectExpr(Node->getOperand(0));
2070 switch (CC) {
2071 default: Node->dump(); assert(0 && "Unhandled SetCC condition"); abort();
2072 case ISD::SETEQ:
2073 Tmp2 = MakeReg(MVT::i32);
2074 BuildMI(BB, PPC::CNTLZW, 1, Tmp2).addReg(Tmp1);
2075 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp2).addImm(27)
2076 .addImm(5).addImm(31);
2077 break;
2078 case ISD::SETNE:
2079 Tmp2 = MakeReg(MVT::i32);
2080 BuildMI(BB, PPC::ADDIC, 2, Tmp2).addReg(Tmp1).addSImm(-1);
2081 BuildMI(BB, PPC::SUBFE, 2, Result).addReg(Tmp2).addReg(Tmp1);
2082 break;
2083 case ISD::SETLT:
2084 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(1)
2085 .addImm(31).addImm(31);
2086 break;
2087 case ISD::SETGT:
2088 Tmp2 = MakeReg(MVT::i32);
2089 Tmp3 = MakeReg(MVT::i32);
2090 BuildMI(BB, PPC::NEG, 2, Tmp2).addReg(Tmp1);
2091 BuildMI(BB, PPC::ANDC, 2, Tmp3).addReg(Tmp2).addReg(Tmp1);
2092 BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp3).addImm(1)
2093 .addImm(31).addImm(31);
2094 break;
Nate Begeman9765c252005-04-12 21:22:28 +00002095 }
Chris Lattner88ac32c2005-08-09 20:21:10 +00002096 return Result;
2097 } else if (Tmp3 == ~0U) { // setcc op, -1
2098 Tmp1 = SelectExpr(Node->getOperand(0));
2099 switch (CC) {
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);
2107 break;
2108 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);
2125 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;
Nate Begeman7e7fadd2005-04-07 20:30:01 +00002129 }
Chris Lattner88ac32c2005-08-09 20:21:10 +00002130 return Result;
Nate Begeman7e7fadd2005-04-07 20:30:01 +00002131 }
Nate Begeman33162522005-03-29 21:54:38 +00002132 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002133
Chris Lattner88ac32c2005-08-09 20:21:10 +00002134 bool Inv;
2135 unsigned CCReg = SelectCC(N, Opc, Inv, Tmp2);
2136 MoveCRtoGPR(CCReg, Inv, Tmp2, Result);
2137 return Result;
2138 }
Nate Begeman74747862005-03-29 22:24:51 +00002139 case ISD::SELECT: {
Chris Lattner88ac32c2005-08-09 20:21:10 +00002140 SDNode *Cond = N.getOperand(0).Val;
2141 ISD::CondCode CC;
2142 if (Cond->getOpcode() == ISD::SETCC &&
Nate Begemana3fd4002005-07-19 16:51:05 +00002143 !MVT::isInteger(N.getOperand(1).getValueType()) &&
Chris Lattner979a21e2005-08-10 03:40:09 +00002144 !MVT::isInteger(Cond->getOperand(1).getValueType()) &&
Chris Lattner88ac32c2005-08-09 20:21:10 +00002145 cast<CondCodeSDNode>(Cond->getOperand(2))->get() != ISD::SETEQ &&
2146 cast<CondCodeSDNode>(Cond->getOperand(2))->get() != ISD::SETNE) {
2147 MVT::ValueType VT = Cond->getOperand(0).getValueType();
2148 ISD::CondCode CC = cast<CondCodeSDNode>(Cond->getOperand(2))->get();
Nate Begemana3fd4002005-07-19 16:51:05 +00002149 unsigned TV = SelectExpr(N.getOperand(1)); // Use if TRUE
2150 unsigned FV = SelectExpr(N.getOperand(2)); // Use if FALSE
2151
Chris Lattner88ac32c2005-08-09 20:21:10 +00002152 ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(Cond->getOperand(1));
Nate Begemana3fd4002005-07-19 16:51:05 +00002153 if (CN && (CN->isExactlyValue(-0.0) || CN->isExactlyValue(0.0))) {
Chris Lattner88ac32c2005-08-09 20:21:10 +00002154 switch(CC) {
Nate Begemana3fd4002005-07-19 16:51:05 +00002155 default: assert(0 && "Invalid FSEL condition"); abort();
2156 case ISD::SETULT:
2157 case ISD::SETLT:
2158 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
2159 case ISD::SETUGE:
2160 case ISD::SETGE:
Chris Lattner88ac32c2005-08-09 20:21:10 +00002161 Tmp1 = SelectExpr(Cond->getOperand(0)); // Val to compare against
Nate Begemana3fd4002005-07-19 16:51:05 +00002162 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp1).addReg(TV).addReg(FV);
2163 return Result;
2164 case ISD::SETUGT:
2165 case ISD::SETGT:
2166 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
2167 case ISD::SETULE:
2168 case ISD::SETLE: {
Chris Lattner88ac32c2005-08-09 20:21:10 +00002169 if (Cond->getOperand(0).getOpcode() == ISD::FNEG) {
2170 Tmp2 = SelectExpr(Cond->getOperand(0).getOperand(0));
Nate Begemana3fd4002005-07-19 16:51:05 +00002171 } else {
2172 Tmp2 = MakeReg(VT);
Chris Lattner88ac32c2005-08-09 20:21:10 +00002173 Tmp1 = SelectExpr(Cond->getOperand(0)); // Val to compare against
Nate Begemana3fd4002005-07-19 16:51:05 +00002174 BuildMI(BB, PPC::FNEG, 1, Tmp2).addReg(Tmp1);
2175 }
2176 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp2).addReg(TV).addReg(FV);
2177 return Result;
2178 }
2179 }
2180 } else {
2181 Opc = (MVT::f64 == VT) ? PPC::FSUB : PPC::FSUBS;
Chris Lattner88ac32c2005-08-09 20:21:10 +00002182 Tmp1 = SelectExpr(Cond->getOperand(0)); // Val to compare against
2183 Tmp2 = SelectExpr(Cond->getOperand(1));
Nate Begemana3fd4002005-07-19 16:51:05 +00002184 Tmp3 = MakeReg(VT);
Chris Lattner88ac32c2005-08-09 20:21:10 +00002185 switch(CC) {
Nate Begemana3fd4002005-07-19 16:51:05 +00002186 default: assert(0 && "Invalid FSEL condition"); abort();
2187 case ISD::SETULT:
2188 case ISD::SETLT:
2189 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
2190 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(FV).addReg(TV);
2191 return Result;
2192 case ISD::SETUGE:
2193 case ISD::SETGE:
2194 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
2195 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(TV).addReg(FV);
2196 return Result;
2197 case ISD::SETUGT:
2198 case ISD::SETGT:
2199 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp2).addReg(Tmp1);
2200 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(FV).addReg(TV);
2201 return Result;
2202 case ISD::SETULE:
2203 case ISD::SETLE:
2204 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp2).addReg(Tmp1);
2205 BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(TV).addReg(FV);
2206 return Result;
2207 }
2208 }
2209 assert(0 && "Should never get here");
Nate Begemana3fd4002005-07-19 16:51:05 +00002210 }
2211
Nate Begeman1cbf3ab2005-04-18 07:48:09 +00002212 bool Inv;
Chris Lattner30710192005-04-01 07:10:02 +00002213 unsigned TrueValue = SelectExpr(N.getOperand(1)); //Use if TRUE
2214 unsigned FalseValue = SelectExpr(N.getOperand(2)); //Use if FALSE
Nate Begeman1cbf3ab2005-04-18 07:48:09 +00002215 unsigned CCReg = SelectCC(N.getOperand(0), Opc, Inv, Tmp3);
Chris Lattner30710192005-04-01 07:10:02 +00002216
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002217 // Create an iterator with which to insert the MBB for copying the false
Nate Begeman74747862005-03-29 22:24:51 +00002218 // value and the MBB to hold the PHI instruction for this SetCC.
2219 MachineBasicBlock *thisMBB = BB;
2220 const BasicBlock *LLVM_BB = BB->getBasicBlock();
2221 ilist<MachineBasicBlock>::iterator It = BB;
2222 ++It;
2223
2224 // thisMBB:
2225 // ...
2226 // TrueVal = ...
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00002227 // cmpTY ccX, r1, r2
Nate Begeman74747862005-03-29 22:24:51 +00002228 // bCC copy1MBB
2229 // fallthrough --> copy0MBB
Nate Begeman74747862005-03-29 22:24:51 +00002230 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
2231 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
Nate Begeman1b7f7fb2005-04-13 23:15:44 +00002232 BuildMI(BB, Opc, 2).addReg(CCReg).addMBB(sinkMBB);
Nate Begeman74747862005-03-29 22:24:51 +00002233 MachineFunction *F = BB->getParent();
2234 F->getBasicBlockList().insert(It, copy0MBB);
2235 F->getBasicBlockList().insert(It, sinkMBB);
2236 // Update machine-CFG edges
2237 BB->addSuccessor(copy0MBB);
2238 BB->addSuccessor(sinkMBB);
2239
2240 // copy0MBB:
2241 // %FalseValue = ...
2242 // # fallthrough to sinkMBB
2243 BB = copy0MBB;
Nate Begeman74747862005-03-29 22:24:51 +00002244 // Update machine-CFG edges
2245 BB->addSuccessor(sinkMBB);
2246
2247 // sinkMBB:
2248 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
2249 // ...
2250 BB = sinkMBB;
2251 BuildMI(BB, PPC::PHI, 4, Result).addReg(FalseValue)
2252 .addMBB(copy0MBB).addReg(TrueValue).addMBB(thisMBB);
Nate Begeman74747862005-03-29 22:24:51 +00002253 return Result;
2254 }
Nate Begemana9795f82005-03-24 04:41:43 +00002255
2256 case ISD::Constant:
2257 switch (N.getValueType()) {
2258 default: assert(0 && "Cannot use constants of this type!");
2259 case MVT::i1:
2260 BuildMI(BB, PPC::LI, 1, Result)
2261 .addSImm(!cast<ConstantSDNode>(N)->isNullValue());
2262 break;
2263 case MVT::i32:
2264 {
2265 int v = (int)cast<ConstantSDNode>(N)->getSignExtended();
2266 if (v < 32768 && v >= -32768) {
2267 BuildMI(BB, PPC::LI, 1, Result).addSImm(v);
2268 } else {
Nate Begeman5e966612005-03-24 06:28:42 +00002269 Tmp1 = MakeReg(MVT::i32);
2270 BuildMI(BB, PPC::LIS, 1, Tmp1).addSImm(v >> 16);
2271 BuildMI(BB, PPC::ORI, 2, Result).addReg(Tmp1).addImm(v & 0xFFFF);
Nate Begemana9795f82005-03-24 04:41:43 +00002272 }
2273 }
2274 }
2275 return Result;
Nate Begemana3fd4002005-07-19 16:51:05 +00002276
2277 case ISD::ConstantFP: {
2278 ConstantFPSDNode *CN = cast<ConstantFPSDNode>(N);
2279 Result = getConstDouble(CN->getValue(), Result);
2280 return Result;
Nate Begemana9795f82005-03-24 04:41:43 +00002281 }
2282
Nate Begemana3fd4002005-07-19 16:51:05 +00002283 case ISD::FNEG:
2284 if (!NoExcessFPPrecision &&
2285 ISD::ADD == N.getOperand(0).getOpcode() &&
2286 N.getOperand(0).Val->hasOneUse() &&
2287 ISD::MUL == N.getOperand(0).getOperand(0).getOpcode() &&
2288 N.getOperand(0).getOperand(0).Val->hasOneUse()) {
2289 ++FusedFP; // Statistic
2290 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(0));
2291 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(1));
2292 Tmp3 = SelectExpr(N.getOperand(0).getOperand(1));
2293 Opc = DestType == MVT::f64 ? PPC::FNMADD : PPC::FNMADDS;
2294 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
2295 } else if (!NoExcessFPPrecision &&
2296 ISD::ADD == N.getOperand(0).getOpcode() &&
2297 N.getOperand(0).Val->hasOneUse() &&
2298 ISD::MUL == N.getOperand(0).getOperand(1).getOpcode() &&
2299 N.getOperand(0).getOperand(1).Val->hasOneUse()) {
2300 ++FusedFP; // Statistic
2301 Tmp1 = SelectExpr(N.getOperand(0).getOperand(1).getOperand(0));
2302 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1).getOperand(1));
2303 Tmp3 = SelectExpr(N.getOperand(0).getOperand(0));
2304 Opc = DestType == MVT::f64 ? PPC::FNMADD : PPC::FNMADDS;
2305 BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3);
2306 } else if (ISD::FABS == N.getOperand(0).getOpcode()) {
2307 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
2308 BuildMI(BB, PPC::FNABS, 1, Result).addReg(Tmp1);
2309 } else {
2310 Tmp1 = SelectExpr(N.getOperand(0));
2311 BuildMI(BB, PPC::FNEG, 1, Result).addReg(Tmp1);
2312 }
2313 return Result;
2314
2315 case ISD::FABS:
2316 Tmp1 = SelectExpr(N.getOperand(0));
2317 BuildMI(BB, PPC::FABS, 1, Result).addReg(Tmp1);
2318 return Result;
2319
Nate Begemanadeb43d2005-07-20 22:42:00 +00002320 case ISD::FSQRT:
2321 Tmp1 = SelectExpr(N.getOperand(0));
2322 Opc = DestType == MVT::f64 ? PPC::FSQRT : PPC::FSQRTS;
2323 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
2324 return Result;
2325
Nate Begemana3fd4002005-07-19 16:51:05 +00002326 case ISD::FP_ROUND:
2327 assert (DestType == MVT::f32 &&
2328 N.getOperand(0).getValueType() == MVT::f64 &&
2329 "only f64 to f32 conversion supported here");
2330 Tmp1 = SelectExpr(N.getOperand(0));
2331 BuildMI(BB, PPC::FRSP, 1, Result).addReg(Tmp1);
2332 return Result;
2333
2334 case ISD::FP_EXTEND:
2335 assert (DestType == MVT::f64 &&
2336 N.getOperand(0).getValueType() == MVT::f32 &&
2337 "only f32 to f64 conversion supported here");
2338 Tmp1 = SelectExpr(N.getOperand(0));
2339 BuildMI(BB, PPC::FMR, 1, Result).addReg(Tmp1);
2340 return Result;
2341
2342 case ISD::UINT_TO_FP:
2343 case ISD::SINT_TO_FP: {
2344 assert (N.getOperand(0).getValueType() == MVT::i32
2345 && "int to float must operate on i32");
2346 bool IsUnsigned = (ISD::UINT_TO_FP == opcode);
2347 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
2348 Tmp2 = MakeReg(MVT::f64); // temp reg to load the integer value into
2349 Tmp3 = MakeReg(MVT::i32); // temp reg to hold the conversion constant
2350
2351 int FrameIdx = BB->getParent()->getFrameInfo()->CreateStackObject(8, 8);
2352 MachineConstantPool *CP = BB->getParent()->getConstantPool();
2353
2354 if (IsUnsigned) {
2355 unsigned ConstF = getConstDouble(0x1.000000p52);
2356 // Store the hi & low halves of the fp value, currently in int regs
2357 BuildMI(BB, PPC::LIS, 1, Tmp3).addSImm(0x4330);
2358 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(Tmp3), FrameIdx);
2359 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(Tmp1), FrameIdx, 4);
2360 addFrameReference(BuildMI(BB, PPC::LFD, 2, Tmp2), FrameIdx);
2361 // Generate the return value with a subtract
2362 BuildMI(BB, PPC::FSUB, 2, Result).addReg(Tmp2).addReg(ConstF);
2363 } else {
2364 unsigned ConstF = getConstDouble(0x1.000008p52);
2365 unsigned TmpL = MakeReg(MVT::i32);
2366 // Store the hi & low halves of the fp value, currently in int regs
2367 BuildMI(BB, PPC::LIS, 1, Tmp3).addSImm(0x4330);
2368 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(Tmp3), FrameIdx);
2369 BuildMI(BB, PPC::XORIS, 2, TmpL).addReg(Tmp1).addImm(0x8000);
2370 addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(TmpL), FrameIdx, 4);
2371 addFrameReference(BuildMI(BB, PPC::LFD, 2, Tmp2), FrameIdx);
2372 // Generate the return value with a subtract
2373 BuildMI(BB, PPC::FSUB, 2, Result).addReg(Tmp2).addReg(ConstF);
2374 }
2375 return Result;
2376 }
2377 }
Nate Begemana9795f82005-03-24 04:41:43 +00002378 return 0;
2379}
2380
2381void ISel::Select(SDOperand N) {
Nate Begeman2497e632005-07-21 20:44:43 +00002382 unsigned Tmp1, Tmp2, Tmp3, Opc;
Nate Begemana9795f82005-03-24 04:41:43 +00002383 unsigned opcode = N.getOpcode();
2384
2385 if (!ExprMap.insert(std::make_pair(N, 1)).second)
2386 return; // Already selected.
2387
2388 SDNode *Node = N.Val;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002389
Nate Begemana9795f82005-03-24 04:41:43 +00002390 switch (Node->getOpcode()) {
2391 default:
2392 Node->dump(); std::cerr << "\n";
2393 assert(0 && "Node not handled yet!");
2394 case ISD::EntryToken: return; // Noop
2395 case ISD::TokenFactor:
2396 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
2397 Select(Node->getOperand(i));
2398 return;
Chris Lattner16cd04d2005-05-12 23:24:06 +00002399 case ISD::CALLSEQ_START:
2400 case ISD::CALLSEQ_END:
Nate Begemana9795f82005-03-24 04:41:43 +00002401 Select(N.getOperand(0));
2402 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
Chris Lattner16cd04d2005-05-12 23:24:06 +00002403 Opc = N.getOpcode() == ISD::CALLSEQ_START ? PPC::ADJCALLSTACKDOWN :
Nate Begemana9795f82005-03-24 04:41:43 +00002404 PPC::ADJCALLSTACKUP;
2405 BuildMI(BB, Opc, 1).addImm(Tmp1);
2406 return;
2407 case ISD::BR: {
2408 MachineBasicBlock *Dest =
2409 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
Nate Begemana9795f82005-03-24 04:41:43 +00002410 Select(N.getOperand(0));
2411 BuildMI(BB, PPC::B, 1).addMBB(Dest);
2412 return;
2413 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002414 case ISD::BRCOND:
Nate Begemancd08e4c2005-04-09 20:09:12 +00002415 case ISD::BRCONDTWOWAY:
Nate Begemana9795f82005-03-24 04:41:43 +00002416 SelectBranchCC(N);
2417 return;
2418 case ISD::CopyToReg:
2419 Select(N.getOperand(0));
2420 Tmp1 = SelectExpr(N.getOperand(1));
2421 Tmp2 = cast<RegSDNode>(N)->getReg();
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002422
Nate Begemana9795f82005-03-24 04:41:43 +00002423 if (Tmp1 != Tmp2) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002424 if (N.getOperand(1).getValueType() == MVT::f64 ||
Nate Begemana9795f82005-03-24 04:41:43 +00002425 N.getOperand(1).getValueType() == MVT::f32)
2426 BuildMI(BB, PPC::FMR, 1, Tmp2).addReg(Tmp1);
2427 else
2428 BuildMI(BB, PPC::OR, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
2429 }
2430 return;
2431 case ISD::ImplicitDef:
2432 Select(N.getOperand(0));
2433 BuildMI(BB, PPC::IMPLICIT_DEF, 0, cast<RegSDNode>(N)->getReg());
2434 return;
2435 case ISD::RET:
2436 switch (N.getNumOperands()) {
2437 default:
2438 assert(0 && "Unknown return instruction!");
2439 case 3:
2440 assert(N.getOperand(1).getValueType() == MVT::i32 &&
2441 N.getOperand(2).getValueType() == MVT::i32 &&
Misha Brukman7847fca2005-04-22 17:54:37 +00002442 "Unknown two-register value!");
Nate Begemana9795f82005-03-24 04:41:43 +00002443 Select(N.getOperand(0));
2444 Tmp1 = SelectExpr(N.getOperand(1));
2445 Tmp2 = SelectExpr(N.getOperand(2));
Nate Begeman27523a12005-04-02 00:42:16 +00002446 BuildMI(BB, PPC::OR, 2, PPC::R3).addReg(Tmp2).addReg(Tmp2);
2447 BuildMI(BB, PPC::OR, 2, PPC::R4).addReg(Tmp1).addReg(Tmp1);
Nate Begemana9795f82005-03-24 04:41:43 +00002448 break;
2449 case 2:
2450 Select(N.getOperand(0));
2451 Tmp1 = SelectExpr(N.getOperand(1));
2452 switch (N.getOperand(1).getValueType()) {
2453 default:
2454 assert(0 && "Unknown return type!");
2455 case MVT::f64:
2456 case MVT::f32:
2457 BuildMI(BB, PPC::FMR, 1, PPC::F1).addReg(Tmp1);
2458 break;
2459 case MVT::i32:
2460 BuildMI(BB, PPC::OR, 2, PPC::R3).addReg(Tmp1).addReg(Tmp1);
2461 break;
2462 }
Nate Begeman9e3e1b52005-03-24 23:35:30 +00002463 case 1:
2464 Select(N.getOperand(0));
2465 break;
Nate Begemana9795f82005-03-24 04:41:43 +00002466 }
2467 BuildMI(BB, PPC::BLR, 0); // Just emit a 'ret' instruction
2468 return;
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002469 case ISD::TRUNCSTORE:
Nate Begeman2497e632005-07-21 20:44:43 +00002470 case ISD::STORE: {
2471 SDOperand Chain = N.getOperand(0);
2472 SDOperand Value = N.getOperand(1);
2473 SDOperand Address = N.getOperand(2);
2474 Select(Chain);
Nate Begemana9795f82005-03-24 04:41:43 +00002475
Nate Begeman2497e632005-07-21 20:44:43 +00002476 Tmp1 = SelectExpr(Value); //value
Nate Begemana9795f82005-03-24 04:41:43 +00002477
Nate Begeman2497e632005-07-21 20:44:43 +00002478 if (opcode == ISD::STORE) {
2479 switch(Value.getValueType()) {
2480 default: assert(0 && "unknown Type in store");
2481 case MVT::i32: Opc = PPC::STW; break;
2482 case MVT::f64: Opc = PPC::STFD; break;
2483 case MVT::f32: Opc = PPC::STFS; break;
Nate Begemana9795f82005-03-24 04:41:43 +00002484 }
Nate Begeman2497e632005-07-21 20:44:43 +00002485 } else { //ISD::TRUNCSTORE
2486 switch(cast<VTSDNode>(Node->getOperand(4))->getVT()) {
2487 default: assert(0 && "unknown Type in store");
2488 case MVT::i1:
2489 case MVT::i8: Opc = PPC::STB; break;
2490 case MVT::i16: Opc = PPC::STH; break;
Nate Begemana9795f82005-03-24 04:41:43 +00002491 }
Nate Begemana9795f82005-03-24 04:41:43 +00002492 }
Nate Begeman2497e632005-07-21 20:44:43 +00002493
2494 if(Address.getOpcode() == ISD::FrameIndex) {
2495 Tmp2 = cast<FrameIndexSDNode>(Address)->getIndex();
2496 addFrameReference(BuildMI(BB, Opc, 3).addReg(Tmp1), (int)Tmp2);
Nate Begeman2497e632005-07-21 20:44:43 +00002497 } else {
2498 int offset;
Nate Begeman2a05c8e2005-07-28 03:02:05 +00002499 switch(SelectAddr(Address, Tmp2, offset)) {
2500 default: assert(0 && "Unhandled return value from SelectAddr");
2501 case 0: // imm offset, no frame, no index
2502 BuildMI(BB, Opc, 3).addReg(Tmp1).addSImm(offset).addReg(Tmp2);
2503 break;
2504 case 1: // imm offset + frame index
2505 addFrameReference(BuildMI(BB, Opc, 3).addReg(Tmp1), (int)Tmp2, offset);
2506 break;
2507 case 2: // base+index addressing
Nate Begeman2497e632005-07-21 20:44:43 +00002508 Opc = IndexedOpForOp(Opc);
2509 BuildMI(BB, Opc, 3).addReg(Tmp1).addReg(Tmp2).addReg(offset);
Nate Begeman2a05c8e2005-07-28 03:02:05 +00002510 break;
Nate Begemand3ded2d2005-08-08 22:22:56 +00002511 case 3: {
2512 GlobalAddressSDNode *GN = cast<GlobalAddressSDNode>(Address);
2513 GlobalValue *GV = GN->getGlobal();
2514 BuildMI(BB, Opc, 3).addReg(Tmp1).addGlobalAddress(GV).addReg(Tmp2);
2515 }
Nate Begeman2497e632005-07-21 20:44:43 +00002516 }
2517 }
2518 return;
2519 }
Nate Begemana9795f82005-03-24 04:41:43 +00002520 case ISD::EXTLOAD:
2521 case ISD::SEXTLOAD:
2522 case ISD::ZEXTLOAD:
2523 case ISD::LOAD:
2524 case ISD::CopyFromReg:
Chris Lattnerb5d8e6e2005-05-13 20:29:26 +00002525 case ISD::TAILCALL:
Nate Begemana9795f82005-03-24 04:41:43 +00002526 case ISD::CALL:
2527 case ISD::DYNAMIC_STACKALLOC:
2528 ExprMap.erase(N);
2529 SelectExpr(N);
2530 return;
2531 }
2532 assert(0 && "Should not be reached!");
2533}
2534
2535
2536/// createPPC32PatternInstructionSelector - This pass converts an LLVM function
2537/// into a machine code representation using pattern matching and a machine
2538/// description file.
2539///
2540FunctionPass *llvm::createPPC32ISelPattern(TargetMachine &TM) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002541 return new ISel(TM);
Chris Lattner246fa632005-03-24 06:16:18 +00002542}
2543