blob: 2094f6ec41c8ecbf804ac0eb03e9af54775855bf [file] [log] [blame]
Vikram S. Adve78d50d02001-09-18 13:03:13 +00001// $Id$
Chris Lattnerb0ddffa2001-09-14 03:47:57 +00002//***************************************************************************
3// File:
4// SparcInstrSelection.cpp
5//
6// Purpose:
Vikram S. Adve3652ef72001-10-01 00:12:53 +00007// BURS instruction selection for SPARC V9 architecture.
Chris Lattnerb0ddffa2001-09-14 03:47:57 +00008//
9// History:
10// 7/02/01 - Vikram Adve - Created
11//**************************************************************************/
12
13#include "SparcInternals.h"
Vikram S. Adve6c697522001-10-18 00:26:20 +000014#include "SparcInstrSelectionSupport.h"
Vikram S. Adve493b8242002-03-18 03:15:35 +000015#include "SparcRegClassInfo.h"
Vikram S. Adve021d5232001-10-10 20:56:33 +000016#include "llvm/CodeGen/InstrSelectionSupport.h"
Chris Lattnerb0ddffa2001-09-14 03:47:57 +000017#include "llvm/CodeGen/MachineInstr.h"
18#include "llvm/CodeGen/InstrForest.h"
19#include "llvm/CodeGen/InstrSelection.h"
Chris Lattner477923d2002-02-03 07:50:56 +000020#include "llvm/CodeGen/MachineCodeForMethod.h"
21#include "llvm/CodeGen/MachineCodeForInstruction.h"
Chris Lattnerb0ddffa2001-09-14 03:47:57 +000022#include "llvm/DerivedTypes.h"
23#include "llvm/iTerminators.h"
24#include "llvm/iMemory.h"
25#include "llvm/iOther.h"
26#include "llvm/BasicBlock.h"
Chris Lattner62b7fd12002-04-07 20:49:59 +000027#include "llvm/Function.h"
Chris Lattner3462ae32001-12-03 22:26:30 +000028#include "llvm/ConstantVals.h"
Chris Lattner5de22042001-11-27 00:03:19 +000029#include "Support/MathExtras.h"
Chris Lattnera742b6a2001-10-13 06:54:30 +000030#include <math.h>
Chris Lattner7f74a562002-01-20 22:54:45 +000031using std::vector;
Chris Lattnerb0ddffa2001-09-14 03:47:57 +000032
33//************************* Forward Declarations ***************************/
34
35
Vikram S. Adve493b8242002-03-18 03:15:35 +000036static void SetMemOperands_Internal (vector<MachineInstr*>& mvec,
37 vector<MachineInstr*>::iterator mvecI,
Vikram S. Adve3652ef72001-10-01 00:12:53 +000038 const InstructionNode* vmInstrNode,
39 Value* ptrVal,
Vikram S. Adve72213c92002-03-24 03:33:02 +000040 std::vector<Value*>& idxVec,
Vikram S. Adve3652ef72001-10-01 00:12:53 +000041 const TargetMachine& target);
Chris Lattnerb0ddffa2001-09-14 03:47:57 +000042
43
44//************************ Internal Functions ******************************/
45
Chris Lattnerb0ddffa2001-09-14 03:47:57 +000046
Chris Lattnerb0ddffa2001-09-14 03:47:57 +000047static inline MachineOpCode
48ChooseBprInstruction(const InstructionNode* instrNode)
49{
50 MachineOpCode opCode;
51
52 Instruction* setCCInstr =
53 ((InstructionNode*) instrNode->leftChild())->getInstruction();
54
55 switch(setCCInstr->getOpcode())
56 {
57 case Instruction::SetEQ: opCode = BRZ; break;
58 case Instruction::SetNE: opCode = BRNZ; break;
59 case Instruction::SetLE: opCode = BRLEZ; break;
60 case Instruction::SetGE: opCode = BRGEZ; break;
61 case Instruction::SetLT: opCode = BRLZ; break;
62 case Instruction::SetGT: opCode = BRGZ; break;
63 default:
64 assert(0 && "Unrecognized VM instruction!");
65 opCode = INVALID_OPCODE;
66 break;
67 }
68
69 return opCode;
70}
71
72
73static inline MachineOpCode
Chris Lattnerb0ddffa2001-09-14 03:47:57 +000074ChooseBpccInstruction(const InstructionNode* instrNode,
Vikram S. Adve3652ef72001-10-01 00:12:53 +000075 const BinaryOperator* setCCInstr)
Chris Lattnerb0ddffa2001-09-14 03:47:57 +000076{
77 MachineOpCode opCode = INVALID_OPCODE;
78
79 bool isSigned = setCCInstr->getOperand(0)->getType()->isSigned();
80
81 if (isSigned)
82 {
83 switch(setCCInstr->getOpcode())
Vikram S. Adve3652ef72001-10-01 00:12:53 +000084 {
85 case Instruction::SetEQ: opCode = BE; break;
86 case Instruction::SetNE: opCode = BNE; break;
87 case Instruction::SetLE: opCode = BLE; break;
88 case Instruction::SetGE: opCode = BGE; break;
89 case Instruction::SetLT: opCode = BL; break;
90 case Instruction::SetGT: opCode = BG; break;
91 default:
92 assert(0 && "Unrecognized VM instruction!");
93 break;
94 }
Chris Lattnerb0ddffa2001-09-14 03:47:57 +000095 }
96 else
97 {
98 switch(setCCInstr->getOpcode())
Vikram S. Adve3652ef72001-10-01 00:12:53 +000099 {
100 case Instruction::SetEQ: opCode = BE; break;
101 case Instruction::SetNE: opCode = BNE; break;
102 case Instruction::SetLE: opCode = BLEU; break;
103 case Instruction::SetGE: opCode = BCC; break;
104 case Instruction::SetLT: opCode = BCS; break;
105 case Instruction::SetGT: opCode = BGU; break;
106 default:
107 assert(0 && "Unrecognized VM instruction!");
108 break;
109 }
Chris Lattnerb0ddffa2001-09-14 03:47:57 +0000110 }
111
112 return opCode;
113}
114
115static inline MachineOpCode
116ChooseBFpccInstruction(const InstructionNode* instrNode,
Vikram S. Adve3652ef72001-10-01 00:12:53 +0000117 const BinaryOperator* setCCInstr)
Chris Lattnerb0ddffa2001-09-14 03:47:57 +0000118{
119 MachineOpCode opCode = INVALID_OPCODE;
120
121 switch(setCCInstr->getOpcode())
122 {
123 case Instruction::SetEQ: opCode = FBE; break;
124 case Instruction::SetNE: opCode = FBNE; break;
125 case Instruction::SetLE: opCode = FBLE; break;
126 case Instruction::SetGE: opCode = FBGE; break;
127 case Instruction::SetLT: opCode = FBL; break;
128 case Instruction::SetGT: opCode = FBG; break;
129 default:
130 assert(0 && "Unrecognized VM instruction!");
131 break;
132 }
133
134 return opCode;
135}
136
137
Vikram S. Adveea5d1f52001-11-04 19:34:49 +0000138// Create a unique TmpInstruction for a boolean value,
139// representing the CC register used by a branch on that value.
140// For now, hack this using a little static cache of TmpInstructions.
141// Eventually the entire BURG instruction selection should be put
142// into a separate class that can hold such information.
Vikram S. Advef889a112001-11-08 05:04:09 +0000143// The static cache is not too bad because the memory for these
Chris Lattner62b7fd12002-04-07 20:49:59 +0000144// TmpInstructions will be freed along with the rest of the Function anyway.
Vikram S. Adveea5d1f52001-11-04 19:34:49 +0000145//
146static TmpInstruction*
Chris Lattner62b7fd12002-04-07 20:49:59 +0000147GetTmpForCC(Value* boolVal, const Function *F, const Type* ccType)
Vikram S. Adveea5d1f52001-11-04 19:34:49 +0000148{
Chris Lattner7f74a562002-01-20 22:54:45 +0000149 typedef std::hash_map<const Value*, TmpInstruction*> BoolTmpCache;
Vikram S. Adveea5d1f52001-11-04 19:34:49 +0000150 static BoolTmpCache boolToTmpCache; // Map boolVal -> TmpInstruction*
Chris Lattner62b7fd12002-04-07 20:49:59 +0000151 static const Function *lastFunction = 0;// Use to flush cache between funcs
Vikram S. Adveea5d1f52001-11-04 19:34:49 +0000152
153 assert(boolVal->getType() == Type::BoolTy && "Weird but ok! Delete assert");
154
Chris Lattner62b7fd12002-04-07 20:49:59 +0000155 if (lastFunction != F)
Vikram S. Adveea5d1f52001-11-04 19:34:49 +0000156 {
Chris Lattner62b7fd12002-04-07 20:49:59 +0000157 lastFunction = F;
Vikram S. Adveea5d1f52001-11-04 19:34:49 +0000158 boolToTmpCache.clear();
159 }
160
Vikram S. Advef889a112001-11-08 05:04:09 +0000161 // Look for tmpI and create a new one otherwise. The new value is
162 // directly written to map using the ref returned by operator[].
Vikram S. Adveea5d1f52001-11-04 19:34:49 +0000163 TmpInstruction*& tmpI = boolToTmpCache[boolVal];
164 if (tmpI == NULL)
Chris Lattner477923d2002-02-03 07:50:56 +0000165 tmpI = new TmpInstruction(ccType, boolVal);
Vikram S. Adveea5d1f52001-11-04 19:34:49 +0000166
167 return tmpI;
168}
169
170
Chris Lattnerb0ddffa2001-09-14 03:47:57 +0000171static inline MachineOpCode
Vikram S. Adve3652ef72001-10-01 00:12:53 +0000172ChooseBccInstruction(const InstructionNode* instrNode,
173 bool& isFPBranch)
174{
175 InstructionNode* setCCNode = (InstructionNode*) instrNode->leftChild();
176 BinaryOperator* setCCInstr = (BinaryOperator*) setCCNode->getInstruction();
177 const Type* setCCType = setCCInstr->getOperand(0)->getType();
178
179 isFPBranch = (setCCType == Type::FloatTy || setCCType == Type::DoubleTy);
180
181 if (isFPBranch)
182 return ChooseBFpccInstruction(instrNode, setCCInstr);
183 else
184 return ChooseBpccInstruction(instrNode, setCCInstr);
185}
186
187
188static inline MachineOpCode
Chris Lattnerb0ddffa2001-09-14 03:47:57 +0000189ChooseMovFpccInstruction(const InstructionNode* instrNode)
190{
191 MachineOpCode opCode = INVALID_OPCODE;
192
193 switch(instrNode->getInstruction()->getOpcode())
194 {
195 case Instruction::SetEQ: opCode = MOVFE; break;
196 case Instruction::SetNE: opCode = MOVFNE; break;
197 case Instruction::SetLE: opCode = MOVFLE; break;
198 case Instruction::SetGE: opCode = MOVFGE; break;
199 case Instruction::SetLT: opCode = MOVFL; break;
200 case Instruction::SetGT: opCode = MOVFG; break;
201 default:
202 assert(0 && "Unrecognized VM instruction!");
203 break;
204 }
205
206 return opCode;
207}
208
209
210// Assumes that SUBcc v1, v2 -> v3 has been executed.
211// In most cases, we want to clear v3 and then follow it by instruction
212// MOVcc 1 -> v3.
213// Set mustClearReg=false if v3 need not be cleared before conditional move.
214// Set valueToMove=0 if we want to conditionally move 0 instead of 1
215// (i.e., we want to test inverse of a condition)
Vikram S. Adve78d50d02001-09-18 13:03:13 +0000216// (The latter two cases do not seem to arise because SetNE needs nothing.)
Chris Lattnerb0ddffa2001-09-14 03:47:57 +0000217//
218static MachineOpCode
219ChooseMovpccAfterSub(const InstructionNode* instrNode,
Vikram S. Adve3652ef72001-10-01 00:12:53 +0000220 bool& mustClearReg,
221 int& valueToMove)
Chris Lattnerb0ddffa2001-09-14 03:47:57 +0000222{
223 MachineOpCode opCode = INVALID_OPCODE;
224 mustClearReg = true;
225 valueToMove = 1;
226
227 switch(instrNode->getInstruction()->getOpcode())
228 {
Vikram S. Adve78d50d02001-09-18 13:03:13 +0000229 case Instruction::SetEQ: opCode = MOVE; break;
Chris Lattnerb0ddffa2001-09-14 03:47:57 +0000230 case Instruction::SetLE: opCode = MOVLE; break;
231 case Instruction::SetGE: opCode = MOVGE; break;
232 case Instruction::SetLT: opCode = MOVL; break;
233 case Instruction::SetGT: opCode = MOVG; break;
Vikram S. Adve78d50d02001-09-18 13:03:13 +0000234 case Instruction::SetNE: assert(0 && "No move required!"); break;
235 default: assert(0 && "Unrecognized VM instr!"); break;
Chris Lattnerb0ddffa2001-09-14 03:47:57 +0000236 }
237
238 return opCode;
239}
240
Chris Lattnerb0ddffa2001-09-14 03:47:57 +0000241static inline MachineOpCode
242ChooseConvertToFloatInstr(const InstructionNode* instrNode,
Vikram S. Adve3652ef72001-10-01 00:12:53 +0000243 const Type* opType)
Chris Lattnerb0ddffa2001-09-14 03:47:57 +0000244{
245 MachineOpCode opCode = INVALID_OPCODE;
246
247 switch(instrNode->getOpLabel())
248 {
249 case ToFloatTy:
250 if (opType == Type::SByteTy || opType == Type::ShortTy || opType == Type::IntTy)
Vikram S. Adve3652ef72001-10-01 00:12:53 +0000251 opCode = FITOS;
Chris Lattnerb0ddffa2001-09-14 03:47:57 +0000252 else if (opType == Type::LongTy)
Vikram S. Adve3652ef72001-10-01 00:12:53 +0000253 opCode = FXTOS;
Chris Lattnerb0ddffa2001-09-14 03:47:57 +0000254 else if (opType == Type::DoubleTy)
Vikram S. Adve3652ef72001-10-01 00:12:53 +0000255 opCode = FDTOS;
Chris Lattnerb0ddffa2001-09-14 03:47:57 +0000256 else if (opType == Type::FloatTy)
Vikram S. Adve3652ef72001-10-01 00:12:53 +0000257 ;
Chris Lattnerb0ddffa2001-09-14 03:47:57 +0000258 else
Vikram S. Adve3652ef72001-10-01 00:12:53 +0000259 assert(0 && "Cannot convert this type to FLOAT on SPARC");
Chris Lattnerb0ddffa2001-09-14 03:47:57 +0000260 break;
261
262 case ToDoubleTy:
Vikram S. Adve493b8242002-03-18 03:15:35 +0000263 // This is usually used in conjunction with CreateCodeToCopyIntToFloat().
264 // Both functions should treat the integer as a 32-bit value for types
265 // of 4 bytes or less, and as a 64-bit value otherwise.
Vikram S. Adve72213c92002-03-24 03:33:02 +0000266 if (opType == Type::SByteTy || opType == Type::UByteTy ||
267 opType == Type::ShortTy || opType == Type::UShortTy ||
268 opType == Type::IntTy || opType == Type::UIntTy)
Vikram S. Adve493b8242002-03-18 03:15:35 +0000269 opCode = FITOD;
Vikram S. Adve72213c92002-03-24 03:33:02 +0000270 else if (opType == Type::LongTy || opType == Type::ULongTy)
Vikram S. Adve3652ef72001-10-01 00:12:53 +0000271 opCode = FXTOD;
Chris Lattnerb0ddffa2001-09-14 03:47:57 +0000272 else if (opType == Type::FloatTy)
Vikram S. Adve3652ef72001-10-01 00:12:53 +0000273 opCode = FSTOD;
Chris Lattnerb0ddffa2001-09-14 03:47:57 +0000274 else if (opType == Type::DoubleTy)
Vikram S. Adve3652ef72001-10-01 00:12:53 +0000275 ;
Chris Lattnerb0ddffa2001-09-14 03:47:57 +0000276 else
Vikram S. Adve3652ef72001-10-01 00:12:53 +0000277 assert(0 && "Cannot convert this type to DOUBLE on SPARC");
Chris Lattnerb0ddffa2001-09-14 03:47:57 +0000278 break;
279
280 default:
281 break;
282 }
283
284 return opCode;
285}
286
287static inline MachineOpCode
288ChooseConvertToIntInstr(const InstructionNode* instrNode,
Vikram S. Adve3652ef72001-10-01 00:12:53 +0000289 const Type* opType)
Chris Lattnerb0ddffa2001-09-14 03:47:57 +0000290{
291 MachineOpCode opCode = INVALID_OPCODE;;
292
293 int instrType = (int) instrNode->getOpLabel();
294
295 if (instrType == ToSByteTy || instrType == ToShortTy || instrType == ToIntTy)
296 {
297 switch (opType->getPrimitiveID())
Vikram S. Adve3652ef72001-10-01 00:12:53 +0000298 {
Chris Lattnerb0ddffa2001-09-14 03:47:57 +0000299 case Type::FloatTyID: opCode = FSTOI; break;
300 case Type::DoubleTyID: opCode = FDTOI; break;
Vikram S. Adve3652ef72001-10-01 00:12:53 +0000301 default:
302 assert(0 && "Non-numeric non-bool type cannot be converted to Int");
303 break;
304 }
Chris Lattnerb0ddffa2001-09-14 03:47:57 +0000305 }
306 else if (instrType == ToLongTy)
307 {
308 switch (opType->getPrimitiveID())
Vikram S. Adve3652ef72001-10-01 00:12:53 +0000309 {
Chris Lattnerb0ddffa2001-09-14 03:47:57 +0000310 case Type::FloatTyID: opCode = FSTOX; break;
311 case Type::DoubleTyID: opCode = FDTOX; break;
Vikram S. Adve3652ef72001-10-01 00:12:53 +0000312 default:
313 assert(0 && "Non-numeric non-bool type cannot be converted to Long");
314 break;
315 }
Chris Lattnerb0ddffa2001-09-14 03:47:57 +0000316 }
317 else
318 assert(0 && "Should not get here, Mo!");
319
320 return opCode;
321}
322
323
324static inline MachineOpCode
Vikram S. Adve3652ef72001-10-01 00:12:53 +0000325ChooseAddInstructionByType(const Type* resultType)
Chris Lattnerb0ddffa2001-09-14 03:47:57 +0000326{
327 MachineOpCode opCode = INVALID_OPCODE;
328
Chris Lattnerb0ddffa2001-09-14 03:47:57 +0000329 if (resultType->isIntegral() ||
Chris Lattnere2f2f542002-04-04 22:19:18 +0000330 isa<PointerType>(resultType) ||
331 isa<FunctionType>(resultType) ||
332 resultType == Type::LabelTy ||
Vikram S. Adve3652ef72001-10-01 00:12:53 +0000333 resultType == Type::BoolTy)
Chris Lattnerb0ddffa2001-09-14 03:47:57 +0000334 {
335 opCode = ADD;
336 }
337 else
Vikram S. Adve3652ef72001-10-01 00:12:53 +0000338 switch(resultType->getPrimitiveID())
339 {
340 case Type::FloatTyID: opCode = FADDS; break;
341 case Type::DoubleTyID: opCode = FADDD; break;
342 default: assert(0 && "Invalid type for ADD instruction"); break;
343 }
Chris Lattnerb0ddffa2001-09-14 03:47:57 +0000344
345 return opCode;
346}
347
348
Vikram S. Adve3652ef72001-10-01 00:12:53 +0000349static inline MachineOpCode
350ChooseAddInstruction(const InstructionNode* instrNode)
351{
352 return ChooseAddInstructionByType(instrNode->getInstruction()->getType());
353}
354
355
Chris Lattnerb0ddffa2001-09-14 03:47:57 +0000356static inline MachineInstr*
357CreateMovFloatInstruction(const InstructionNode* instrNode,
Vikram S. Adve3652ef72001-10-01 00:12:53 +0000358 const Type* resultType)
Chris Lattnerb0ddffa2001-09-14 03:47:57 +0000359{
360 MachineInstr* minstr = new MachineInstr((resultType == Type::FloatTy)
Vikram S. Adve3652ef72001-10-01 00:12:53 +0000361 ? FMOVS : FMOVD);
Vikram S. Adve493b8242002-03-18 03:15:35 +0000362 minstr->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister,
363 instrNode->leftChild()->getValue());
364 minstr->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister,
365 instrNode->getValue());
Chris Lattnerb0ddffa2001-09-14 03:47:57 +0000366 return minstr;
367}
368
369static inline MachineInstr*
370CreateAddConstInstruction(const InstructionNode* instrNode)
371{
372 MachineInstr* minstr = NULL;
373
374 Value* constOp = ((InstrTreeNode*) instrNode->rightChild())->getValue();
Chris Lattner3462ae32001-12-03 22:26:30 +0000375 assert(isa<Constant>(constOp));
Chris Lattnerb0ddffa2001-09-14 03:47:57 +0000376
377 // Cases worth optimizing are:
378 // (1) Add with 0 for float or double: use an FMOV of appropriate type,
379 // instead of an FADD (1 vs 3 cycles). There is no integer MOV.
380 //
381 const Type* resultType = instrNode->getInstruction()->getType();
382
Vikram S. Adve3652ef72001-10-01 00:12:53 +0000383 if (resultType == Type::FloatTy ||
384 resultType == Type::DoubleTy)
385 {
Chris Lattner3462ae32001-12-03 22:26:30 +0000386 double dval = cast<ConstantFP>(constOp)->getValue();
Vikram S. Adve3652ef72001-10-01 00:12:53 +0000387 if (dval == 0.0)
388 minstr = CreateMovFloatInstruction(instrNode, resultType);
389 }
Chris Lattnerb0ddffa2001-09-14 03:47:57 +0000390
391 return minstr;
392}
393
394
395static inline MachineOpCode
Vikram S. Adve80cf6062001-11-04 21:59:14 +0000396ChooseSubInstructionByType(const Type* resultType)
Chris Lattnerb0ddffa2001-09-14 03:47:57 +0000397{
398 MachineOpCode opCode = INVALID_OPCODE;
399
Chris Lattnerb0ddffa2001-09-14 03:47:57 +0000400 if (resultType->isIntegral() ||
401 resultType->isPointerType())
402 {
403 opCode = SUB;
404 }
405 else
Vikram S. Adve3652ef72001-10-01 00:12:53 +0000406 switch(resultType->getPrimitiveID())
407 {
408 case Type::FloatTyID: opCode = FSUBS; break;
409 case Type::DoubleTyID: opCode = FSUBD; break;
410 default: assert(0 && "Invalid type for SUB instruction"); break;
411 }
Chris Lattnerb0ddffa2001-09-14 03:47:57 +0000412
413 return opCode;
414}
415
416
417static inline MachineInstr*
418CreateSubConstInstruction(const InstructionNode* instrNode)
419{
420 MachineInstr* minstr = NULL;
421
422 Value* constOp = ((InstrTreeNode*) instrNode->rightChild())->getValue();
Chris Lattner3462ae32001-12-03 22:26:30 +0000423 assert(isa<Constant>(constOp));
Chris Lattnerb0ddffa2001-09-14 03:47:57 +0000424
425 // Cases worth optimizing are:
426 // (1) Sub with 0 for float or double: use an FMOV of appropriate type,
427 // instead of an FSUB (1 vs 3 cycles). There is no integer MOV.
428 //
429 const Type* resultType = instrNode->getInstruction()->getType();
430
431 if (resultType == Type::FloatTy ||
432 resultType == Type::DoubleTy)
433 {
Chris Lattner3462ae32001-12-03 22:26:30 +0000434 double dval = cast<ConstantFP>(constOp)->getValue();
Chris Lattnerb0ddffa2001-09-14 03:47:57 +0000435 if (dval == 0.0)
Vikram S. Adve3652ef72001-10-01 00:12:53 +0000436 minstr = CreateMovFloatInstruction(instrNode, resultType);
Chris Lattnerb0ddffa2001-09-14 03:47:57 +0000437 }
438
439 return minstr;
440}
441
442
443static inline MachineOpCode
444ChooseFcmpInstruction(const InstructionNode* instrNode)
445{
446 MachineOpCode opCode = INVALID_OPCODE;
447
448 Value* operand = ((InstrTreeNode*) instrNode->leftChild())->getValue();
449 switch(operand->getType()->getPrimitiveID()) {
450 case Type::FloatTyID: opCode = FCMPS; break;
451 case Type::DoubleTyID: opCode = FCMPD; break;
452 default: assert(0 && "Invalid type for FCMP instruction"); break;
453 }
454
455 return opCode;
456}
457
458
459// Assumes that leftArg and rightArg are both cast instructions.
460//
461static inline bool
462BothFloatToDouble(const InstructionNode* instrNode)
463{
464 InstrTreeNode* leftArg = instrNode->leftChild();
465 InstrTreeNode* rightArg = instrNode->rightChild();
466 InstrTreeNode* leftArgArg = leftArg->leftChild();
467 InstrTreeNode* rightArgArg = rightArg->leftChild();
468 assert(leftArg->getValue()->getType() == rightArg->getValue()->getType());
469
470 // Check if both arguments are floats cast to double
471 return (leftArg->getValue()->getType() == Type::DoubleTy &&
Vikram S. Adve3652ef72001-10-01 00:12:53 +0000472 leftArgArg->getValue()->getType() == Type::FloatTy &&
473 rightArgArg->getValue()->getType() == Type::FloatTy);
Chris Lattnerb0ddffa2001-09-14 03:47:57 +0000474}
475
476
477static inline MachineOpCode
Vikram S. Adve80cf6062001-11-04 21:59:14 +0000478ChooseMulInstructionByType(const Type* resultType)
Chris Lattnerb0ddffa2001-09-14 03:47:57 +0000479{
480 MachineOpCode opCode = INVALID_OPCODE;
481
Chris Lattnerb0ddffa2001-09-14 03:47:57 +0000482 if (resultType->isIntegral())
Vikram S. Adve80cf6062001-11-04 21:59:14 +0000483 opCode = MULX;
Chris Lattnerb0ddffa2001-09-14 03:47:57 +0000484 else
Vikram S. Adve3652ef72001-10-01 00:12:53 +0000485 switch(resultType->getPrimitiveID())
486 {
487 case Type::FloatTyID: opCode = FMULS; break;
488 case Type::DoubleTyID: opCode = FMULD; break;
489 default: assert(0 && "Invalid type for MUL instruction"); break;
490 }
Chris Lattnerb0ddffa2001-09-14 03:47:57 +0000491
492 return opCode;
493}
494
495
Vikram S. Adve80cf6062001-11-04 21:59:14 +0000496
Chris Lattnerb0ddffa2001-09-14 03:47:57 +0000497static inline MachineInstr*
Vikram S. Adve493b8242002-03-18 03:15:35 +0000498CreateIntNegInstruction(const TargetMachine& target,
Vikram S. Adve3652ef72001-10-01 00:12:53 +0000499 Value* vreg)
Chris Lattnerb0ddffa2001-09-14 03:47:57 +0000500{
501 MachineInstr* minstr = new MachineInstr(SUB);
Vikram S. Adve493b8242002-03-18 03:15:35 +0000502 minstr->SetMachineOperandReg(0, target.getRegInfo().getZeroRegNum());
503 minstr->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister, vreg);
504 minstr->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister, vreg);
Chris Lattnerb0ddffa2001-09-14 03:47:57 +0000505 return minstr;
506}
507
508
Vikram S. Adve493b8242002-03-18 03:15:35 +0000509// Does not create any instructions if we cannot exploit constant to
Vikram S. Adve72213c92002-03-24 03:33:02 +0000510// create a cheaper instruction.
511// This returns the approximate cost of the instructions generated,
512// which is used to pick the cheapest when both operands are constant.
513static inline unsigned int
Vikram S. Adve493b8242002-03-18 03:15:35 +0000514CreateMulConstInstruction(const TargetMachine &target,
515 Value* lval, Value* rval, Value* destVal,
516 vector<MachineInstr*>& mvec)
Chris Lattnerb0ddffa2001-09-14 03:47:57 +0000517{
Vikram S. Adve72213c92002-03-24 03:33:02 +0000518 /* An integer multiply is generally more costly than FP multiply */
519 unsigned int cost = target.getInstrInfo().minLatency(MULX);
Vikram S. Adve493b8242002-03-18 03:15:35 +0000520 MachineInstr* minstr1 = NULL;
521 MachineInstr* minstr2 = NULL;
522
523 Value* constOp = rval;
524 if (! isa<Constant>(constOp))
Vikram S. Adve72213c92002-03-24 03:33:02 +0000525 return cost;
Chris Lattnerb0ddffa2001-09-14 03:47:57 +0000526
527 // Cases worth optimizing are:
528 // (1) Multiply by 0 or 1 for any type: replace with copy (ADD or FMOV)
529 // (2) Multiply by 2^x for integer types: replace with Shift
530 //
Vikram S. Adve493b8242002-03-18 03:15:35 +0000531 const Type* resultType = destVal->getType();
Chris Lattnerb0ddffa2001-09-14 03:47:57 +0000532
Vikram S. Adve78d50d02001-09-18 13:03:13 +0000533 if (resultType->isIntegral() || resultType->isPointerType())
Chris Lattnerb0ddffa2001-09-14 03:47:57 +0000534 {
535 unsigned pow;
536 bool isValidConst;
537 int64_t C = GetConstantValueAsSignedInt(constOp, isValidConst);
538 if (isValidConst)
Vikram S. Adve3652ef72001-10-01 00:12:53 +0000539 {
540 bool needNeg = false;
541 if (C < 0)
542 {
543 needNeg = true;
544 C = -C;
545 }
546
547 if (C == 0 || C == 1)
548 {
Vikram S. Adve72213c92002-03-24 03:33:02 +0000549 cost = target.getInstrInfo().minLatency(ADD);
Vikram S. Adve493b8242002-03-18 03:15:35 +0000550 minstr1 = new MachineInstr(ADD);
Vikram S. Adve3652ef72001-10-01 00:12:53 +0000551 if (C == 0)
Vikram S. Adve493b8242002-03-18 03:15:35 +0000552 minstr1->SetMachineOperandReg(0,
Vikram S. Adve72213c92002-03-24 03:33:02 +0000553 target.getRegInfo().getZeroRegNum());
Vikram S. Adve3652ef72001-10-01 00:12:53 +0000554 else
Vikram S. Adve72213c92002-03-24 03:33:02 +0000555 minstr1->SetMachineOperandVal(0,
556 MachineOperand::MO_VirtualRegister, lval);
557 minstr1->SetMachineOperandReg(1,
558 target.getRegInfo().getZeroRegNum());
Vikram S. Adve3652ef72001-10-01 00:12:53 +0000559 }
560 else if (IsPowerOf2(C, pow))
561 {
Vikram S. Adve493b8242002-03-18 03:15:35 +0000562 minstr1 = new MachineInstr((resultType == Type::LongTy)
Vikram S. Adve72213c92002-03-24 03:33:02 +0000563 ? SLLX : SLL);
564 minstr1->SetMachineOperandVal(0,
565 MachineOperand::MO_VirtualRegister, lval);
566 minstr1->SetMachineOperandConst(1,
567 MachineOperand::MO_UnextendedImmed, pow);
Vikram S. Adve3652ef72001-10-01 00:12:53 +0000568 }
569
Vikram S. Adve493b8242002-03-18 03:15:35 +0000570 if (minstr1 && needNeg)
Vikram S. Adve3652ef72001-10-01 00:12:53 +0000571 { // insert <reg = SUB 0, reg> after the instr to flip the sign
Vikram S. Adve493b8242002-03-18 03:15:35 +0000572 minstr2 = CreateIntNegInstruction(target, destVal);
Vikram S. Adve72213c92002-03-24 03:33:02 +0000573 cost += target.getInstrInfo().minLatency(minstr2->getOpCode());
Vikram S. Adve3652ef72001-10-01 00:12:53 +0000574 }
575 }
Chris Lattnerb0ddffa2001-09-14 03:47:57 +0000576 }
577 else
578 {
579 if (resultType == Type::FloatTy ||
Vikram S. Adve3652ef72001-10-01 00:12:53 +0000580 resultType == Type::DoubleTy)
581 {
Chris Lattner3462ae32001-12-03 22:26:30 +0000582 double dval = cast<ConstantFP>(constOp)->getValue();
Vikram S. Advebc23cf52001-11-09 02:18:16 +0000583 if (fabs(dval) == 1)
Vikram S. Adve3652ef72001-10-01 00:12:53 +0000584 {
Vikram S. Advebc23cf52001-11-09 02:18:16 +0000585 bool needNeg = (dval < 0);
586
587 MachineOpCode opCode = needNeg
588 ? (resultType == Type::FloatTy? FNEGS : FNEGD)
589 : (resultType == Type::FloatTy? FMOVS : FMOVD);
590
Vikram S. Adve493b8242002-03-18 03:15:35 +0000591 minstr1 = new MachineInstr(opCode);
592 minstr1->SetMachineOperandVal(0,
593 MachineOperand::MO_VirtualRegister,
594 lval);
Vikram S. Advebc23cf52001-11-09 02:18:16 +0000595 }
Vikram S. Adve3652ef72001-10-01 00:12:53 +0000596 }
Chris Lattnerb0ddffa2001-09-14 03:47:57 +0000597 }
598
Vikram S. Adve493b8242002-03-18 03:15:35 +0000599 if (minstr1 != NULL)
600 minstr1->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister,
601 destVal);
Chris Lattnerb0ddffa2001-09-14 03:47:57 +0000602
Vikram S. Adve493b8242002-03-18 03:15:35 +0000603 if (minstr1)
Vikram S. Adve72213c92002-03-24 03:33:02 +0000604 {
605 mvec.push_back(minstr1);
606 cost = target.getInstrInfo().minLatency(minstr1->getOpCode());
607 }
Vikram S. Adve493b8242002-03-18 03:15:35 +0000608 if (minstr2)
Vikram S. Adve72213c92002-03-24 03:33:02 +0000609 {
610 assert(minstr1 && "Otherwise cost needs to be initialized to 0");
611 cost += target.getInstrInfo().minLatency(minstr2->getOpCode());
612 mvec.push_back(minstr2);
613 }
614
615 return cost;
Vikram S. Adve493b8242002-03-18 03:15:35 +0000616}
617
618
Vikram S. Adve72213c92002-03-24 03:33:02 +0000619// Does not create any instructions if we cannot exploit constant to
620// create a cheaper instruction.
621//
622static inline void
623CreateCheapestMulConstInstruction(const TargetMachine &target,
624 Value* lval, Value* rval, Value* destVal,
625 vector<MachineInstr*>& mvec)
626{
627 Value* constOp;
628 if (isa<Constant>(lval) && isa<Constant>(rval))
629 { // both operands are constant: try both orders!
630 vector<MachineInstr*> mvec1, mvec2;
631 unsigned int lcost = CreateMulConstInstruction(target, lval, rval,
632 destVal, mvec1);
633 unsigned int rcost = CreateMulConstInstruction(target, rval, lval,
634 destVal, mvec2);
635 vector<MachineInstr*>& mincostMvec = (lcost <= rcost)? mvec1 : mvec2;
636 vector<MachineInstr*>& maxcostMvec = (lcost <= rcost)? mvec2 : mvec1;
637 mvec.insert(mvec.end(), mincostMvec.begin(), mincostMvec.end());
638
639 for (unsigned int i=0; i < maxcostMvec.size(); ++i)
640 delete maxcostMvec[i];
641 }
642 else if (isa<Constant>(rval)) // rval is constant, but not lval
643 CreateMulConstInstruction(target, lval, rval, destVal, mvec);
644 else if (isa<Constant>(lval)) // lval is constant, but not rval
645 CreateMulConstInstruction(target, lval, rval, destVal, mvec);
646
647 // else neither is constant
648 return;
649}
650
Vikram S. Adve493b8242002-03-18 03:15:35 +0000651// Return NULL if we cannot exploit constant to create a cheaper instruction
652static inline void
653CreateMulInstruction(const TargetMachine &target,
654 Value* lval, Value* rval, Value* destVal,
655 vector<MachineInstr*>& mvec,
656 MachineOpCode forceMulOp = INVALID_MACHINE_OPCODE)
657{
658 unsigned int L = mvec.size();
Vikram S. Adve72213c92002-03-24 03:33:02 +0000659 CreateCheapestMulConstInstruction(target, lval, rval, destVal, mvec);
Vikram S. Adve493b8242002-03-18 03:15:35 +0000660 if (mvec.size() == L)
661 { // no instructions were added so create MUL reg, reg, reg.
662 // Use FSMULD if both operands are actually floats cast to doubles.
663 // Otherwise, use the default opcode for the appropriate type.
664 MachineOpCode mulOp = ((forceMulOp != INVALID_MACHINE_OPCODE)
665 ? forceMulOp
666 : ChooseMulInstructionByType(destVal->getType()));
667 MachineInstr* M = new MachineInstr(mulOp);
668 M->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister, lval);
669 M->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister, rval);
670 M->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister, destVal);
671 mvec.push_back(M);
672 }
Chris Lattnerb0ddffa2001-09-14 03:47:57 +0000673}
674
675
Vikram S. Adve80cf6062001-11-04 21:59:14 +0000676// Generate a divide instruction for Div or Rem.
677// For Rem, this assumes that the operand type will be signed if the result
678// type is signed. This is correct because they must have the same sign.
679//
Chris Lattnerb0ddffa2001-09-14 03:47:57 +0000680static inline MachineOpCode
Vikram S. Adve3652ef72001-10-01 00:12:53 +0000681ChooseDivInstruction(TargetMachine &target,
682 const InstructionNode* instrNode)
Chris Lattnerb0ddffa2001-09-14 03:47:57 +0000683{
684 MachineOpCode opCode = INVALID_OPCODE;
685
686 const Type* resultType = instrNode->getInstruction()->getType();
687
688 if (resultType->isIntegral())
Vikram S. Adve3652ef72001-10-01 00:12:53 +0000689 opCode = resultType->isSigned()? SDIVX : UDIVX;
Chris Lattnerb0ddffa2001-09-14 03:47:57 +0000690 else
Vikram S. Adve3652ef72001-10-01 00:12:53 +0000691 switch(resultType->getPrimitiveID())
692 {
693 case Type::FloatTyID: opCode = FDIVS; break;
694 case Type::DoubleTyID: opCode = FDIVD; break;
695 default: assert(0 && "Invalid type for DIV instruction"); break;
696 }
Chris Lattnerb0ddffa2001-09-14 03:47:57 +0000697
698 return opCode;
699}
700
701
Vikram S. Adve493b8242002-03-18 03:15:35 +0000702// Return NULL if we cannot exploit constant to create a cheaper instruction
703static inline void
Vikram S. Adve3652ef72001-10-01 00:12:53 +0000704CreateDivConstInstruction(TargetMachine &target,
705 const InstructionNode* instrNode,
Vikram S. Adve493b8242002-03-18 03:15:35 +0000706 vector<MachineInstr*>& mvec)
Chris Lattnerb0ddffa2001-09-14 03:47:57 +0000707{
Vikram S. Adve493b8242002-03-18 03:15:35 +0000708 MachineInstr* minstr1 = NULL;
709 MachineInstr* minstr2 = NULL;
Chris Lattnerb0ddffa2001-09-14 03:47:57 +0000710
711 Value* constOp = ((InstrTreeNode*) instrNode->rightChild())->getValue();
Vikram S. Adve493b8242002-03-18 03:15:35 +0000712 if (! isa<Constant>(constOp))
713 return;
Chris Lattnerb0ddffa2001-09-14 03:47:57 +0000714
715 // Cases worth optimizing are:
716 // (1) Divide by 1 for any type: replace with copy (ADD or FMOV)
717 // (2) Divide by 2^x for integer types: replace with SR[L or A]{X}
718 //
719 const Type* resultType = instrNode->getInstruction()->getType();
720
721 if (resultType->isIntegral())
722 {
723 unsigned pow;
724 bool isValidConst;
725 int64_t C = GetConstantValueAsSignedInt(constOp, isValidConst);
726 if (isValidConst)
Vikram S. Adve3652ef72001-10-01 00:12:53 +0000727 {
728 bool needNeg = false;
729 if (C < 0)
730 {
731 needNeg = true;
732 C = -C;
733 }
734
735 if (C == 1)
736 {
Vikram S. Adve493b8242002-03-18 03:15:35 +0000737 minstr1 = new MachineInstr(ADD);
Vikram S. Adve72213c92002-03-24 03:33:02 +0000738 minstr1->SetMachineOperandVal(0,
739 MachineOperand::MO_VirtualRegister,
740 instrNode->leftChild()->getValue());
741 minstr1->SetMachineOperandReg(1,
742 target.getRegInfo().getZeroRegNum());
Vikram S. Adve3652ef72001-10-01 00:12:53 +0000743 }
744 else if (IsPowerOf2(C, pow))
745 {
746 MachineOpCode opCode= ((resultType->isSigned())
747 ? (resultType==Type::LongTy)? SRAX : SRA
748 : (resultType==Type::LongTy)? SRLX : SRL);
Vikram S. Adve493b8242002-03-18 03:15:35 +0000749 minstr1 = new MachineInstr(opCode);
Vikram S. Adve72213c92002-03-24 03:33:02 +0000750 minstr1->SetMachineOperandVal(0,
751 MachineOperand::MO_VirtualRegister,
Vikram S. Adve3652ef72001-10-01 00:12:53 +0000752 instrNode->leftChild()->getValue());
Vikram S. Adve72213c92002-03-24 03:33:02 +0000753 minstr1->SetMachineOperandConst(1,
754 MachineOperand::MO_UnextendedImmed,
755 pow);
Vikram S. Adve3652ef72001-10-01 00:12:53 +0000756 }
757
Vikram S. Adve493b8242002-03-18 03:15:35 +0000758 if (minstr1 && needNeg)
Vikram S. Adve3652ef72001-10-01 00:12:53 +0000759 { // insert <reg = SUB 0, reg> after the instr to flip the sign
Vikram S. Adve493b8242002-03-18 03:15:35 +0000760 minstr2 = CreateIntNegInstruction(target,
Vikram S. Adve3652ef72001-10-01 00:12:53 +0000761 instrNode->getValue());
762 }
763 }
Chris Lattnerb0ddffa2001-09-14 03:47:57 +0000764 }
765 else
766 {
767 if (resultType == Type::FloatTy ||
Vikram S. Adve3652ef72001-10-01 00:12:53 +0000768 resultType == Type::DoubleTy)
769 {
Chris Lattner3462ae32001-12-03 22:26:30 +0000770 double dval = cast<ConstantFP>(constOp)->getValue();
Vikram S. Advebc23cf52001-11-09 02:18:16 +0000771 if (fabs(dval) == 1)
Vikram S. Adve3652ef72001-10-01 00:12:53 +0000772 {
773 bool needNeg = (dval < 0);
774
775 MachineOpCode opCode = needNeg
776 ? (resultType == Type::FloatTy? FNEGS : FNEGD)
777 : (resultType == Type::FloatTy? FMOVS : FMOVD);
778
Vikram S. Adve493b8242002-03-18 03:15:35 +0000779 minstr1 = new MachineInstr(opCode);
Vikram S. Adve72213c92002-03-24 03:33:02 +0000780 minstr1->SetMachineOperandVal(0,
781 MachineOperand::MO_VirtualRegister,
Vikram S. Adve3652ef72001-10-01 00:12:53 +0000782 instrNode->leftChild()->getValue());
783 }
784 }
Chris Lattnerb0ddffa2001-09-14 03:47:57 +0000785 }
786
Vikram S. Adve493b8242002-03-18 03:15:35 +0000787 if (minstr1 != NULL)
788 minstr1->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister,
789 instrNode->getValue());
Chris Lattnerb0ddffa2001-09-14 03:47:57 +0000790
Vikram S. Adve493b8242002-03-18 03:15:35 +0000791 if (minstr1)
792 mvec.push_back(minstr1);
793 if (minstr2)
794 mvec.push_back(minstr2);
Chris Lattnerb0ddffa2001-09-14 03:47:57 +0000795}
796
797
Vikram S. Adve493b8242002-03-18 03:15:35 +0000798static void
799CreateCodeForVariableSizeAlloca(const TargetMachine& target,
800 Instruction* result,
801 unsigned int tsize,
802 Value* numElementsVal,
803 vector<MachineInstr*>& getMvec)
804{
805 MachineInstr* M;
806
807 // Create a Value to hold the (constant) element size
808 Value* tsizeVal = ConstantSInt::get(Type::IntTy, tsize);
809
810 // Get the constant offset from SP for dynamically allocated storage
811 // and create a temporary Value to hold it.
Chris Lattner62b7fd12002-04-07 20:49:59 +0000812 assert(result && result->getParent() && "Result value is not part of a fn?");
813 Function *F = result->getParent()->getParent();
814 MachineCodeForMethod& mcInfo = MachineCodeForMethod::get(F);
Vikram S. Adve493b8242002-03-18 03:15:35 +0000815 bool growUp;
816 ConstantSInt* dynamicAreaOffset =
817 ConstantSInt::get(Type::IntTy,
818 target.getFrameInfo().getDynamicAreaOffset(mcInfo,growUp));
819 assert(! growUp && "Has SPARC v9 stack frame convention changed?");
820
821 // Create a temporary value to hold the result of MUL
822 TmpInstruction* tmpProd = new TmpInstruction(numElementsVal, tsizeVal);
823 MachineCodeForInstruction::get(result).addTemp(tmpProd);
824
825 // Instruction 1: mul numElements, typeSize -> tmpProd
826 M = new MachineInstr(MULX);
827 M->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister, numElementsVal);
828 M->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister, tsizeVal);
829 M->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister, tmpProd);
830 getMvec.push_back(M);
831
832 // Instruction 2: sub %sp, tmpProd -> %sp
833 M = new MachineInstr(SUB);
834 M->SetMachineOperandReg(0, target.getRegInfo().getStackPointer());
835 M->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister, tmpProd);
836 M->SetMachineOperandReg(2, target.getRegInfo().getStackPointer());
837 getMvec.push_back(M);
838
839 // Instruction 3: add %sp, frameSizeBelowDynamicArea -> result
840 M = new MachineInstr(ADD);
841 M->SetMachineOperandReg(0, target.getRegInfo().getStackPointer());
842 M->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister, dynamicAreaOffset);
843 M->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister, result);
844 getMvec.push_back(M);
845}
846
847
848static void
849CreateCodeForFixedSizeAlloca(const TargetMachine& target,
850 Instruction* result,
851 unsigned int tsize,
852 unsigned int numElements,
853 vector<MachineInstr*>& getMvec)
854{
Vikram S. Adve72213c92002-03-24 03:33:02 +0000855 assert(result && result->getParent() &&
Chris Lattner62b7fd12002-04-07 20:49:59 +0000856 "Result value is not part of a function?");
857 Function *F = result->getParent()->getParent();
858 MachineCodeForMethod &mcInfo = MachineCodeForMethod::get(F);
Vikram S. Adve493b8242002-03-18 03:15:35 +0000859
Chris Lattner62b7fd12002-04-07 20:49:59 +0000860 // Check if the offset would small enough to use as an immediate in
861 // load/stores (check LDX because all load/stores have the same-size immediate
862 // field). If not, put the variable in the dynamically sized area of the
863 // frame.
Vikram S. Adve72213c92002-03-24 03:33:02 +0000864 unsigned int paddedSizeIgnored;
Vikram S. Adve493b8242002-03-18 03:15:35 +0000865 int offsetFromFP = mcInfo.computeOffsetforLocalVar(target, result,
Vikram S. Adve72213c92002-03-24 03:33:02 +0000866 paddedSizeIgnored,
Vikram S. Adve493b8242002-03-18 03:15:35 +0000867 tsize * numElements);
868 if (! target.getInstrInfo().constantFitsInImmedField(LDX, offsetFromFP))
869 {
870 CreateCodeForVariableSizeAlloca(target, result, tsize,
871 ConstantSInt::get(Type::IntTy,numElements),
872 getMvec);
873 return;
874 }
875
876 // else offset fits in immediate field so go ahead and allocate it.
877 offsetFromFP = mcInfo.allocateLocalVar(target, result, tsize * numElements);
878
879 // Create a temporary Value to hold the constant offset.
880 // This is needed because it may not fit in the immediate field.
881 ConstantSInt* offsetVal = ConstantSInt::get(Type::IntTy, offsetFromFP);
882
883 // Instruction 1: add %fp, offsetFromFP -> result
884 MachineInstr* M = new MachineInstr(ADD);
885 M->SetMachineOperandReg(0, target.getRegInfo().getFramePointer());
886 M->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister, offsetVal);
887 M->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister, result);
888
889 getMvec.push_back(M);
890}
891
892
893
Chris Lattnerb0ddffa2001-09-14 03:47:57 +0000894//------------------------------------------------------------------------
895// Function SetOperandsForMemInstr
896//
897// Choose addressing mode for the given load or store instruction.
898// Use [reg+reg] if it is an indexed reference, and the index offset is
899// not a constant or if it cannot fit in the offset field.
900// Use [reg+offset] in all other cases.
901//
902// This assumes that all array refs are "lowered" to one of these forms:
903// %x = load (subarray*) ptr, constant ; single constant offset
904// %x = load (subarray*) ptr, offsetVal ; single non-constant offset
905// Generally, this should happen via strength reduction + LICM.
906// Also, strength reduction should take care of using the same register for
907// the loop index variable and an array index, when that is profitable.
908//------------------------------------------------------------------------
909
910static void
Vikram S. Adve493b8242002-03-18 03:15:35 +0000911SetOperandsForMemInstr(vector<MachineInstr*>& mvec,
912 vector<MachineInstr*>::iterator mvecI,
Vikram S. Adve3652ef72001-10-01 00:12:53 +0000913 const InstructionNode* vmInstrNode,
914 const TargetMachine& target)
Chris Lattnerb0ddffa2001-09-14 03:47:57 +0000915{
916 MemAccessInst* memInst = (MemAccessInst*) vmInstrNode->getInstruction();
917
918 // Variables to hold the index vector, ptr value, and offset value.
919 // The major work here is to extract these for all 3 instruction types
920 // and then call the common function SetMemOperands_Internal().
921 //
Vikram S. Adve493b8242002-03-18 03:15:35 +0000922 Value* ptrVal = memInst->getPointerOperand();
Chris Lattnerb0ddffa2001-09-14 03:47:57 +0000923
Vikram S. Adve4973f732002-03-31 19:07:35 +0000924 // Start with the index vector of this instruction, if any.
925 vector<Value*> idxVec;
926 idxVec.insert(idxVec.end(), memInst->idx_begin(), memInst->idx_end());
927
928 // If there is a GetElemPtr instruction to fold in to this instr,
929 // it must be in the left child for Load and GetElemPtr, and in the
930 // right child for Store instructions.
Chris Lattnerb0ddffa2001-09-14 03:47:57 +0000931 InstrTreeNode* ptrChild = (vmInstrNode->getOpLabel() == Instruction::Store
Vikram S. Adve3652ef72001-10-01 00:12:53 +0000932 ? vmInstrNode->rightChild()
933 : vmInstrNode->leftChild());
Chris Lattnerb0ddffa2001-09-14 03:47:57 +0000934
Vikram S. Adve72213c92002-03-24 03:33:02 +0000935 // Fold chains of GetElemPtr instructions for structure references.
Vikram S. Adve493b8242002-03-18 03:15:35 +0000936 if (isa<StructType>(cast<PointerType>(ptrVal->getType())->getElementType())
937 && (ptrChild->getOpLabel() == Instruction::GetElementPtr ||
938 ptrChild->getOpLabel() == GetElemPtrIdx))
Chris Lattnerb0ddffa2001-09-14 03:47:57 +0000939 {
Vikram S. Adve72213c92002-03-24 03:33:02 +0000940 Value* newPtr = FoldGetElemChain((InstructionNode*) ptrChild, idxVec);
941 if (newPtr)
942 ptrVal = newPtr;
Chris Lattnerb0ddffa2001-09-14 03:47:57 +0000943 }
944
Vikram S. Adve493b8242002-03-18 03:15:35 +0000945 SetMemOperands_Internal(mvec, mvecI, vmInstrNode, ptrVal, idxVec, target);
Chris Lattnerb0ddffa2001-09-14 03:47:57 +0000946}
947
948
Vikram S. Adve493b8242002-03-18 03:15:35 +0000949// Generate the correct operands (and additional instructions if needed)
950// for the given pointer and given index vector.
951//
Chris Lattnerb0ddffa2001-09-14 03:47:57 +0000952static void
Vikram S. Adve493b8242002-03-18 03:15:35 +0000953SetMemOperands_Internal(vector<MachineInstr*>& mvec,
954 vector<MachineInstr*>::iterator mvecI,
Vikram S. Adve3652ef72001-10-01 00:12:53 +0000955 const InstructionNode* vmInstrNode,
956 Value* ptrVal,
Vikram S. Adve72213c92002-03-24 03:33:02 +0000957 vector<Value*>& idxVec,
Vikram S. Adve3652ef72001-10-01 00:12:53 +0000958 const TargetMachine& target)
Chris Lattnerb0ddffa2001-09-14 03:47:57 +0000959{
960 MemAccessInst* memInst = (MemAccessInst*) vmInstrNode->getInstruction();
961
962 // Initialize so we default to storing the offset in a register.
Chris Lattnere147d062001-11-07 14:01:59 +0000963 int64_t smallConstOffset = 0;
Chris Lattnerb0ddffa2001-09-14 03:47:57 +0000964 Value* valueForRegOffset = NULL;
965 MachineOperand::MachineOperandType offsetOpType =MachineOperand::MO_VirtualRegister;
966
Vikram S. Adve493b8242002-03-18 03:15:35 +0000967 // Check if there is an index vector and if so, compute the
968 // right offset for structures and for arrays
Chris Lattnerb0ddffa2001-09-14 03:47:57 +0000969 //
970 if (idxVec.size() > 0)
971 {
Chris Lattnere147d062001-11-07 14:01:59 +0000972 unsigned offset = 0;
Chris Lattnerb0ddffa2001-09-14 03:47:57 +0000973
Vikram S. Adve493b8242002-03-18 03:15:35 +0000974 const PointerType* ptrType = cast<PointerType>(ptrVal->getType());
Chris Lattnerb0ddffa2001-09-14 03:47:57 +0000975
Vikram S. Adve72213c92002-03-24 03:33:02 +0000976 // Handle special common case of leading [0] index.
977 bool firstIndexIsZero =
978 bool(isa<ConstantUInt>(idxVec.front()) &&
979 cast<ConstantUInt>(idxVec.front())->getValue() == 0);
980
981 // This is a real structure reference if the ptr target is a
982 // structure type, and the first offset is [0] (eliminate that offset).
983 if (firstIndexIsZero && ptrType->getElementType()->isStructType())
Vikram S. Adve3652ef72001-10-01 00:12:53 +0000984 {
Vikram S. Adve72213c92002-03-24 03:33:02 +0000985 // Compute the offset value using the index vector. Create a
986 // virtual reg. for it since it may not fit in the immed field.
987 assert(idxVec.size() >= 2);
988 idxVec.erase(idxVec.begin());
Vikram S. Adve493b8242002-03-18 03:15:35 +0000989 unsigned offset = target.DataLayout.getIndexedOffset(ptrType,idxVec);
990 valueForRegOffset = ConstantSInt::get(Type::IntTy, offset);
Vikram S. Adve3652ef72001-10-01 00:12:53 +0000991 }
Chris Lattnerb0ddffa2001-09-14 03:47:57 +0000992 else
Vikram S. Adve3652ef72001-10-01 00:12:53 +0000993 {
Vikram S. Adve72213c92002-03-24 03:33:02 +0000994 // It is an array ref, and must have been lowered to a single offset.
Vikram S. Adve493b8242002-03-18 03:15:35 +0000995 assert((memInst->getNumOperands()
996 == (unsigned) 1 + memInst->getFirstIndexOperandNumber())
997 && "Array refs must be lowered before Instruction Selection");
Vikram S. Adve3652ef72001-10-01 00:12:53 +0000998
Vikram S. Adve493b8242002-03-18 03:15:35 +0000999 Value* arrayOffsetVal = * memInst->idx_begin();
1000
Vikram S. Adve72213c92002-03-24 03:33:02 +00001001 // If index is 0, the offset value is just 0. Otherwise,
1002 // generate a MUL instruction to compute address from index.
1003 // The call to getTypeSize() will fail if size is not constant.
1004 // CreateMulInstruction() folds constants intelligently enough.
1005 //
1006 if (firstIndexIsZero)
Vikram S. Adve3652ef72001-10-01 00:12:53 +00001007 {
Vikram S. Adve72213c92002-03-24 03:33:02 +00001008 offsetOpType = MachineOperand::MO_SignExtendedImmed;
1009 smallConstOffset = 0;
Vikram S. Adve3652ef72001-10-01 00:12:53 +00001010 }
Vikram S. Adve72213c92002-03-24 03:33:02 +00001011 else
1012 {
1013 vector<MachineInstr*> mulVec;
1014 Instruction* addr = new TmpInstruction(Type::UIntTy, memInst);
1015 MachineCodeForInstruction::get(memInst).addTemp(addr);
1016
1017 unsigned int eltSize =
1018 target.DataLayout.getTypeSize(ptrType->getElementType());
1019 assert(eltSize > 0 && "Invalid or non-const array element size");
1020 ConstantUInt* eltVal = ConstantUInt::get(Type::UIntTy, eltSize);
1021
1022 CreateMulInstruction(target,
1023 arrayOffsetVal, /* lval, not likely const */
1024 eltVal, /* rval, likely constant */
1025 addr, /* result*/
1026 mulVec, INVALID_MACHINE_OPCODE);
1027 assert(mulVec.size() > 0 && "No multiply instruction created?");
1028 for (vector<MachineInstr*>::const_iterator I = mulVec.begin();
1029 I != mulVec.end(); ++I)
1030 {
1031 mvecI = mvec.insert(mvecI, *I); // ptr to inserted value
1032 ++mvecI; // ptr to mem. instr.
1033 }
1034
1035 valueForRegOffset = addr;
1036 }
Vikram S. Adve3652ef72001-10-01 00:12:53 +00001037 }
Chris Lattnerb0ddffa2001-09-14 03:47:57 +00001038 }
1039 else
1040 {
1041 offsetOpType = MachineOperand::MO_SignExtendedImmed;
1042 smallConstOffset = 0;
1043 }
1044
Vikram S. Adve4973f732002-03-31 19:07:35 +00001045 // For STORE:
1046 // Operand 0 is value, operand 1 is ptr, operand 2 is offset
1047 // For LOAD or GET_ELEMENT_PTR,
1048 // Operand 0 is ptr, operand 1 is offset, operand 2 is result.
1049 //
1050 unsigned offsetOpNum, ptrOpNum;
1051 if (memInst->getOpcode() == Instruction::Store)
1052 {
1053 (*mvecI)->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister,
1054 vmInstrNode->leftChild()->getValue());
1055 ptrOpNum = 1;
1056 offsetOpNum = 2;
1057 }
1058 else
1059 {
1060 ptrOpNum = 0;
1061 offsetOpNum = 1;
1062 (*mvecI)->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister,
1063 memInst);
1064 }
Chris Lattnerb0ddffa2001-09-14 03:47:57 +00001065
Vikram S. Adve4973f732002-03-31 19:07:35 +00001066 (*mvecI)->SetMachineOperandVal(ptrOpNum, MachineOperand::MO_VirtualRegister,
1067 ptrVal);
1068
Chris Lattnerb0ddffa2001-09-14 03:47:57 +00001069 if (offsetOpType == MachineOperand::MO_VirtualRegister)
1070 {
1071 assert(valueForRegOffset != NULL);
Vikram S. Adve493b8242002-03-18 03:15:35 +00001072 (*mvecI)->SetMachineOperandVal(offsetOpNum, offsetOpType,
1073 valueForRegOffset);
Chris Lattnerb0ddffa2001-09-14 03:47:57 +00001074 }
1075 else
Vikram S. Adve493b8242002-03-18 03:15:35 +00001076 (*mvecI)->SetMachineOperandConst(offsetOpNum, offsetOpType,
1077 smallConstOffset);
Chris Lattnerb0ddffa2001-09-14 03:47:57 +00001078}
1079
1080
Chris Lattnerb0ddffa2001-09-14 03:47:57 +00001081//
1082// Substitute operand `operandNum' of the instruction in node `treeNode'
Vikram S. Advee6c611b2001-10-14 23:28:43 +00001083// in place of the use(s) of that instruction in node `parent'.
1084// Check both explicit and implicit operands!
Vikram S. Adve493b8242002-03-18 03:15:35 +00001085// Also make sure to skip over a parent who:
1086// (1) is a list node in the Burg tree, or
1087// (2) itself had its results forwarded to its parent
Chris Lattnerb0ddffa2001-09-14 03:47:57 +00001088//
1089static void
1090ForwardOperand(InstructionNode* treeNode,
Vikram S. Adve3652ef72001-10-01 00:12:53 +00001091 InstrTreeNode* parent,
1092 int operandNum)
Chris Lattnerb0ddffa2001-09-14 03:47:57 +00001093{
Vikram S. Adve78d50d02001-09-18 13:03:13 +00001094 assert(treeNode && parent && "Invalid invocation of ForwardOperand");
1095
Chris Lattnerb0ddffa2001-09-14 03:47:57 +00001096 Instruction* unusedOp = treeNode->getInstruction();
1097 Value* fwdOp = unusedOp->getOperand(operandNum);
Vikram S. Adve78d50d02001-09-18 13:03:13 +00001098
1099 // The parent itself may be a list node, so find the real parent instruction
1100 while (parent->getNodeType() != InstrTreeNode::NTInstructionNode)
1101 {
1102 parent = parent->parent();
1103 assert(parent && "ERROR: Non-instruction node has no parent in tree.");
1104 }
1105 InstructionNode* parentInstrNode = (InstructionNode*) parent;
1106
1107 Instruction* userInstr = parentInstrNode->getInstruction();
Chris Lattner477923d2002-02-03 07:50:56 +00001108 MachineCodeForInstruction &mvec = MachineCodeForInstruction::get(userInstr);
Vikram S. Adve493b8242002-03-18 03:15:35 +00001109
1110 // The parent's mvec would be empty if it was itself forwarded.
1111 // Recursively call ForwardOperand in that case...
1112 //
1113 if (mvec.size() == 0)
Chris Lattnerb0ddffa2001-09-14 03:47:57 +00001114 {
Vikram S. Adve493b8242002-03-18 03:15:35 +00001115 assert(parent->parent() != NULL &&
1116 "Parent could not have been forwarded, yet has no instructions?");
1117 ForwardOperand(treeNode, parent->parent(), operandNum);
1118 }
1119 else
1120 {
1121 bool fwdSuccessful = false;
1122 for (unsigned i=0, N=mvec.size(); i < N; i++)
Vikram S. Adve3652ef72001-10-01 00:12:53 +00001123 {
Vikram S. Adve493b8242002-03-18 03:15:35 +00001124 MachineInstr* minstr = mvec[i];
1125 for (unsigned i=0, numOps=minstr->getNumOperands(); i < numOps; ++i)
Vikram S. Adve3652ef72001-10-01 00:12:53 +00001126 {
Vikram S. Adve493b8242002-03-18 03:15:35 +00001127 const MachineOperand& mop = minstr->getOperand(i);
1128 if (mop.getOperandType() == MachineOperand::MO_VirtualRegister &&
1129 mop.getVRegValue() == unusedOp)
1130 {
1131 minstr->SetMachineOperandVal(i,
1132 MachineOperand::MO_VirtualRegister, fwdOp);
1133 fwdSuccessful = true;
1134 }
Vikram S. Adve3652ef72001-10-01 00:12:53 +00001135 }
Vikram S. Adve493b8242002-03-18 03:15:35 +00001136
1137 for (unsigned i=0,numOps=minstr->getNumImplicitRefs(); i<numOps; ++i)
1138 if (minstr->getImplicitRef(i) == unusedOp)
1139 {
1140 minstr->setImplicitRef(i, fwdOp,
1141 minstr->implicitRefIsDefined(i));
1142 fwdSuccessful = true;
1143 }
Vikram S. Adve3652ef72001-10-01 00:12:53 +00001144 }
Vikram S. Adve493b8242002-03-18 03:15:35 +00001145 assert(fwdSuccessful && "Value to be forwarded is never used!");
Chris Lattnerb0ddffa2001-09-14 03:47:57 +00001146 }
1147}
1148
1149
Ruchira Sasankafcdc2ff2001-11-12 14:45:33 +00001150void UltraSparcInstrInfo::
Vikram S. Adve3652ef72001-10-01 00:12:53 +00001151CreateCopyInstructionsByType(const TargetMachine& target,
Chris Lattner62b7fd12002-04-07 20:49:59 +00001152 Function *F,
Vikram S. Adve3652ef72001-10-01 00:12:53 +00001153 Value* src,
1154 Instruction* dest,
Ruchira Sasankafcdc2ff2001-11-12 14:45:33 +00001155 vector<MachineInstr*>& minstrVec) const
Vikram S. Adve3652ef72001-10-01 00:12:53 +00001156{
Vikram S. Adve6c697522001-10-18 00:26:20 +00001157 bool loadConstantToReg = false;
Vikram S. Adve3652ef72001-10-01 00:12:53 +00001158
1159 const Type* resultType = dest->getType();
1160
1161 MachineOpCode opCode = ChooseAddInstructionByType(resultType);
1162 if (opCode == INVALID_OPCODE)
1163 {
1164 assert(0 && "Unsupported result type in CreateCopyInstructionsByType()");
Vikram S. Adve6c697522001-10-18 00:26:20 +00001165 return;
Vikram S. Adve3652ef72001-10-01 00:12:53 +00001166 }
1167
Vikram S. Adve6c697522001-10-18 00:26:20 +00001168 // if `src' is a constant that doesn't fit in the immed field or if it is
1169 // a global variable (i.e., a constant address), generate a load
1170 // instruction instead of an add
1171 //
Chris Lattner3462ae32001-12-03 22:26:30 +00001172 if (isa<Constant>(src))
Vikram S. Adve3652ef72001-10-01 00:12:53 +00001173 {
1174 unsigned int machineRegNum;
1175 int64_t immedValue;
1176 MachineOperand::MachineOperandType opType =
1177 ChooseRegOrImmed(src, opCode, target, /*canUseImmed*/ true,
1178 machineRegNum, immedValue);
1179
1180 if (opType == MachineOperand::MO_VirtualRegister)
Vikram S. Adve6c697522001-10-18 00:26:20 +00001181 loadConstantToReg = true;
Vikram S. Adve3652ef72001-10-01 00:12:53 +00001182 }
Vikram S. Adve6c697522001-10-18 00:26:20 +00001183 else if (isa<GlobalValue>(src))
1184 loadConstantToReg = true;
Vikram S. Adve3652ef72001-10-01 00:12:53 +00001185
Vikram S. Adve6c697522001-10-18 00:26:20 +00001186 if (loadConstantToReg)
1187 { // `src' is constant and cannot fit in immed field for the ADD
1188 // Insert instructions to "load" the constant into a register
1189 vector<TmpInstruction*> tempVec;
Chris Lattner62b7fd12002-04-07 20:49:59 +00001190 target.getInstrInfo().CreateCodeToLoadConst(F, src, dest,
1191 minstrVec, tempVec);
Vikram S. Adve6c697522001-10-18 00:26:20 +00001192 for (unsigned i=0; i < tempVec.size(); i++)
Chris Lattner477923d2002-02-03 07:50:56 +00001193 MachineCodeForInstruction::get(dest).addTemp(tempVec[i]);
Vikram S. Adve6c697522001-10-18 00:26:20 +00001194 }
1195 else
Vikram S. Adve493b8242002-03-18 03:15:35 +00001196 { // Create an add-with-0 instruction of the appropriate type.
Vikram S. Adve3652ef72001-10-01 00:12:53 +00001197 // Make `src' the second operand, in case it is a constant
1198 // Use (unsigned long) 0 for a NULL pointer value.
1199 //
Vikram S. Adve493b8242002-03-18 03:15:35 +00001200 const Type* zeroValueType =
Vikram S. Adve3652ef72001-10-01 00:12:53 +00001201 (resultType->getPrimitiveID() == Type::PointerTyID)? Type::ULongTy
1202 : resultType;
Vikram S. Adve6c697522001-10-18 00:26:20 +00001203 MachineInstr* minstr = new MachineInstr(opCode);
Vikram S. Adve493b8242002-03-18 03:15:35 +00001204 minstr->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister,
1205 Constant::getNullConstant(zeroValueType));
1206 minstr->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister, src);
1207 minstr->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister,dest);
Vikram S. Adve6c697522001-10-18 00:26:20 +00001208 minstrVec.push_back(minstr);
Vikram S. Adve3652ef72001-10-01 00:12:53 +00001209 }
Vikram S. Adve3652ef72001-10-01 00:12:53 +00001210}
1211
1212
Ruchira Sasankafcdc2ff2001-11-12 14:45:33 +00001213
Vikram S. Adve52e41002001-10-22 13:36:31 +00001214//******************* Externally Visible Functions *************************/
1215
1216
1217//------------------------------------------------------------------------
1218// External Function: GetInstructionsForProlog
1219// External Function: GetInstructionsForEpilog
1220//
1221// Purpose:
1222// Create prolog and epilog code for procedure entry and exit
1223//------------------------------------------------------------------------
1224
1225extern unsigned
1226GetInstructionsForProlog(BasicBlock* entryBB,
1227 TargetMachine &target,
1228 MachineInstr** mvec)
Chris Lattnerb0ddffa2001-09-14 03:47:57 +00001229{
Vikram S. Adve493b8242002-03-18 03:15:35 +00001230 MachineInstr* M;
Vikram S. Advef889a112001-11-08 05:04:09 +00001231 const MachineFrameInfo& frameInfo = target.getFrameInfo();
Vikram S. Adve493b8242002-03-18 03:15:35 +00001232 unsigned int N = 0;
Vikram S. Advef889a112001-11-08 05:04:09 +00001233
Vikram S. Adve52e41002001-10-22 13:36:31 +00001234 // The second operand is the stack size. If it does not fit in the
Vikram S. Adve493b8242002-03-18 03:15:35 +00001235 // immediate field, we have to use a free register to hold the size.
1236 // We will assume that local register `l0' is unused since the SAVE
1237 // instruction must be the first instruction in each procedure.
1238 //
Chris Lattner62b7fd12002-04-07 20:49:59 +00001239 Function *F = entryBB->getParent();
1240 MachineCodeForMethod& mcInfo = MachineCodeForMethod::get(F);
Vikram S. Advef889a112001-11-08 05:04:09 +00001241 unsigned int staticStackSize = mcInfo.getStaticStackSize();
1242
1243 if (staticStackSize < (unsigned) frameInfo.getMinStackFrameSize())
1244 staticStackSize = (unsigned) frameInfo.getMinStackFrameSize();
1245
1246 if (unsigned padsz = (staticStackSize %
1247 (unsigned) frameInfo.getStackFrameSizeAlignment()))
Vikram S. Advee7dc82e2001-11-12 05:16:39 +00001248 staticStackSize += frameInfo.getStackFrameSizeAlignment() - padsz;
Chris Lattnerb0ddffa2001-09-14 03:47:57 +00001249
Vikram S. Adve493b8242002-03-18 03:15:35 +00001250 if (target.getInstrInfo().constantFitsInImmedField(SAVE, staticStackSize))
1251 {
1252 M = new MachineInstr(SAVE);
1253 M->SetMachineOperandReg(0, target.getRegInfo().getStackPointer());
1254 M->SetMachineOperandConst(1, MachineOperand::MO_SignExtendedImmed,
1255 - (int) staticStackSize);
1256 M->SetMachineOperandReg(2, target.getRegInfo().getStackPointer());
1257 mvec[N++] = M;
1258 }
1259 else
1260 {
1261 M = new MachineInstr(SETSW);
Vikram S. Adve72213c92002-03-24 03:33:02 +00001262 M->SetMachineOperandConst(0, MachineOperand::MO_SignExtendedImmed,
1263 - (int) staticStackSize);
Vikram S. Adve493b8242002-03-18 03:15:35 +00001264 M->SetMachineOperandReg(1, MachineOperand::MO_MachineRegister,
1265 target.getRegInfo().getUnifiedRegNum(
1266 target.getRegInfo().getRegClassIDOfType(Type::IntTy),
1267 SparcIntRegOrder::l0));
1268 mvec[N++] = M;
1269
1270 M = new MachineInstr(SAVE);
1271 M->SetMachineOperandReg(0, target.getRegInfo().getStackPointer());
1272 M->SetMachineOperandReg(1, MachineOperand::MO_MachineRegister,
1273 target.getRegInfo().getUnifiedRegNum(
1274 target.getRegInfo().getRegClassIDOfType(Type::IntTy),
1275 SparcIntRegOrder::l0));
1276 M->SetMachineOperandReg(2, target.getRegInfo().getStackPointer());
1277 mvec[N++] = M;
1278 }
Chris Lattnerb0ddffa2001-09-14 03:47:57 +00001279
Vikram S. Adve493b8242002-03-18 03:15:35 +00001280 return N;
Chris Lattnerb0ddffa2001-09-14 03:47:57 +00001281}
1282
1283
Vikram S. Adve52e41002001-10-22 13:36:31 +00001284extern unsigned
1285GetInstructionsForEpilog(BasicBlock* anExitBB,
1286 TargetMachine &target,
1287 MachineInstr** mvec)
1288{
Vikram S. Adve52e41002001-10-22 13:36:31 +00001289 mvec[0] = new MachineInstr(RESTORE);
Vikram S. Adve493b8242002-03-18 03:15:35 +00001290 mvec[0]->SetMachineOperandReg(0, target.getRegInfo().getZeroRegNum());
1291 mvec[0]->SetMachineOperandConst(1, MachineOperand::MO_SignExtendedImmed,
Chris Lattner7f74a562002-01-20 22:54:45 +00001292 (int64_t)0);
Vikram S. Adve493b8242002-03-18 03:15:35 +00001293 mvec[0]->SetMachineOperandReg(2, target.getRegInfo().getZeroRegNum());
Vikram S. Adve52e41002001-10-22 13:36:31 +00001294
1295 return 1;
1296}
1297
1298
1299//------------------------------------------------------------------------
1300// External Function: ThisIsAChainRule
1301//
1302// Purpose:
1303// Check if a given BURG rule is a chain rule.
1304//------------------------------------------------------------------------
1305
1306extern bool
1307ThisIsAChainRule(int eruleno)
1308{
1309 switch(eruleno)
1310 {
1311 case 111: // stmt: reg
1312 case 113: // stmt: bool
1313 case 123:
1314 case 124:
1315 case 125:
1316 case 126:
1317 case 127:
1318 case 128:
1319 case 129:
1320 case 130:
1321 case 131:
1322 case 132:
1323 case 133:
1324 case 155:
1325 case 221:
1326 case 222:
1327 case 241:
1328 case 242:
1329 case 243:
1330 case 244:
Vikram S. Adve313ca142002-04-01 20:28:48 +00001331 case 321:
Vikram S. Adve52e41002001-10-22 13:36:31 +00001332 return true; break;
1333
1334 default:
1335 return false; break;
1336 }
1337}
Chris Lattnerb0ddffa2001-09-14 03:47:57 +00001338
1339
1340//------------------------------------------------------------------------
1341// External Function: GetInstructionsByRule
1342//
1343// Purpose:
1344// Choose machine instructions for the SPARC according to the
1345// patterns chosen by the BURG-generated parser.
1346//------------------------------------------------------------------------
1347
Vikram S. Adve493b8242002-03-18 03:15:35 +00001348void
Chris Lattnerb0ddffa2001-09-14 03:47:57 +00001349GetInstructionsByRule(InstructionNode* subtreeRoot,
Vikram S. Adve3652ef72001-10-01 00:12:53 +00001350 int ruleForNode,
1351 short* nts,
Vikram S. Advef889a112001-11-08 05:04:09 +00001352 TargetMachine &target,
Vikram S. Adve493b8242002-03-18 03:15:35 +00001353 vector<MachineInstr*>& mvec)
Chris Lattnerb0ddffa2001-09-14 03:47:57 +00001354{
Chris Lattnerb0ddffa2001-09-14 03:47:57 +00001355 bool checkCast = false; // initialize here to use fall-through
Chris Lattnerb0ddffa2001-09-14 03:47:57 +00001356 int nextRule;
1357 int forwardOperandNum = -1;
Vikram S. Adve493b8242002-03-18 03:15:35 +00001358 unsigned int allocaSize = 0;
1359 MachineInstr* M, *M2;
1360 unsigned int L;
1361
1362 mvec.clear();
Chris Lattnerb0ddffa2001-09-14 03:47:57 +00001363
Vikram S. Adve72213c92002-03-24 03:33:02 +00001364 // If the code for this instruction was folded into the parent (user),
1365 // then do nothing!
1366 if (subtreeRoot->isFoldedIntoParent())
1367 return;
1368
Vikram S. Adve3652ef72001-10-01 00:12:53 +00001369 //
1370 // Let's check for chain rules outside the switch so that we don't have
1371 // to duplicate the list of chain rule production numbers here again
1372 //
1373 if (ThisIsAChainRule(ruleForNode))
Chris Lattnerb0ddffa2001-09-14 03:47:57 +00001374 {
Vikram S. Adve3652ef72001-10-01 00:12:53 +00001375 // Chain rules have a single nonterminal on the RHS.
1376 // Get the rule that matches the RHS non-terminal and use that instead.
1377 //
1378 assert(nts[0] && ! nts[1]
1379 && "A chain rule should have only one RHS non-terminal!");
1380 nextRule = burm_rule(subtreeRoot->state, nts[0]);
1381 nts = burm_nts[nextRule];
Vikram S. Adve493b8242002-03-18 03:15:35 +00001382 GetInstructionsByRule(subtreeRoot, nextRule, nts, target, mvec);
Chris Lattnerb0ddffa2001-09-14 03:47:57 +00001383 }
Vikram S. Adve3652ef72001-10-01 00:12:53 +00001384 else
Chris Lattnerb0ddffa2001-09-14 03:47:57 +00001385 {
Vikram S. Adve3652ef72001-10-01 00:12:53 +00001386 switch(ruleForNode) {
1387 case 1: // stmt: Ret
1388 case 2: // stmt: RetValue(reg)
Vikram S. Adveea5d1f52001-11-04 19:34:49 +00001389 { // NOTE: Prepass of register allocation is responsible
Vikram S. Adve3652ef72001-10-01 00:12:53 +00001390 // for moving return value to appropriate register.
1391 // Mark the return-address register as a hidden virtual reg.
Vikram S. Adve8aeb09f2001-10-11 04:23:19 +00001392 // Mark the return value register as an implicit ref of
1393 // the machine instruction.
Vikram S. Adveea5d1f52001-11-04 19:34:49 +00001394 // Finally put a NOP in the delay slot.
Chris Lattner3462ae32001-12-03 22:26:30 +00001395 ReturnInst *returnInstr =
1396 cast<ReturnInst>(subtreeRoot->getInstruction());
Vikram S. Adve3652ef72001-10-01 00:12:53 +00001397 assert(returnInstr->getOpcode() == Instruction::Ret);
1398
Chris Lattner477923d2002-02-03 07:50:56 +00001399 Instruction* returnReg = new TmpInstruction(returnInstr);
1400 MachineCodeForInstruction::get(returnInstr).addTemp(returnReg);
Vikram S. Adve52e41002001-10-22 13:36:31 +00001401
Vikram S. Adve493b8242002-03-18 03:15:35 +00001402 M = new MachineInstr(JMPLRET);
1403 M->SetMachineOperandReg(0, MachineOperand::MO_VirtualRegister,
Vikram S. Adve3652ef72001-10-01 00:12:53 +00001404 returnReg);
Vikram S. Adve493b8242002-03-18 03:15:35 +00001405 M->SetMachineOperandConst(1,MachineOperand::MO_SignExtendedImmed,
Chris Lattner7f74a562002-01-20 22:54:45 +00001406 (int64_t)8);
Vikram S. Adve493b8242002-03-18 03:15:35 +00001407 M->SetMachineOperandReg(2, target.getRegInfo().getZeroRegNum());
Vikram S. Adve3652ef72001-10-01 00:12:53 +00001408
Vikram S. Adve8aeb09f2001-10-11 04:23:19 +00001409 if (returnInstr->getReturnValue() != NULL)
Vikram S. Adve493b8242002-03-18 03:15:35 +00001410 M->addImplicitRef(returnInstr->getReturnValue());
Vikram S. Adve8aeb09f2001-10-11 04:23:19 +00001411
Vikram S. Adve493b8242002-03-18 03:15:35 +00001412 mvec.push_back(M);
1413 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adve3652ef72001-10-01 00:12:53 +00001414
Vikram S. Adve3652ef72001-10-01 00:12:53 +00001415 break;
Vikram S. Adveea5d1f52001-11-04 19:34:49 +00001416 }
Vikram S. Adve3652ef72001-10-01 00:12:53 +00001417
1418 case 3: // stmt: Store(reg,reg)
1419 case 4: // stmt: Store(reg,ptrreg)
Vikram S. Adve493b8242002-03-18 03:15:35 +00001420 mvec.push_back(new MachineInstr(
1421 ChooseStoreInstruction(
1422 subtreeRoot->leftChild()->getValue()->getType())));
1423 SetOperandsForMemInstr(mvec, mvec.end()-1, subtreeRoot, target);
Vikram S. Adve3652ef72001-10-01 00:12:53 +00001424 break;
Chris Lattnerb0ddffa2001-09-14 03:47:57 +00001425
Vikram S. Adve3652ef72001-10-01 00:12:53 +00001426 case 5: // stmt: BrUncond
Vikram S. Adve493b8242002-03-18 03:15:35 +00001427 M = new MachineInstr(BA);
1428 M->SetMachineOperandVal(0, MachineOperand::MO_CCRegister,
Vikram S. Adve3652ef72001-10-01 00:12:53 +00001429 (Value*)NULL);
Vikram S. Adve493b8242002-03-18 03:15:35 +00001430 M->SetMachineOperandVal(1, MachineOperand::MO_PCRelativeDisp,
Chris Lattner3462ae32001-12-03 22:26:30 +00001431 cast<BranchInst>(subtreeRoot->getInstruction())->getSuccessor(0));
Vikram S. Adve493b8242002-03-18 03:15:35 +00001432 mvec.push_back(M);
Vikram S. Adve3652ef72001-10-01 00:12:53 +00001433
1434 // delay slot
Vikram S. Adve493b8242002-03-18 03:15:35 +00001435 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adve3652ef72001-10-01 00:12:53 +00001436 break;
Chris Lattnerb0ddffa2001-09-14 03:47:57 +00001437
Vikram S. Adve3652ef72001-10-01 00:12:53 +00001438 case 206: // stmt: BrCond(setCCconst)
Vikram S. Adveea5d1f52001-11-04 19:34:49 +00001439 { // setCCconst => boolean was computed with `%b = setCC type reg1 const'
Vikram S. Adve3652ef72001-10-01 00:12:53 +00001440 // If the constant is ZERO, we can use the branch-on-integer-register
1441 // instructions and avoid the SUBcc instruction entirely.
1442 // Otherwise this is just the same as case 5, so just fall through.
Vikram S. Adveea5d1f52001-11-04 19:34:49 +00001443 //
Vikram S. Adve3652ef72001-10-01 00:12:53 +00001444 InstrTreeNode* constNode = subtreeRoot->leftChild()->rightChild();
1445 assert(constNode &&
1446 constNode->getNodeType() ==InstrTreeNode::NTConstNode);
Chris Lattner3462ae32001-12-03 22:26:30 +00001447 Constant *constVal = cast<Constant>(constNode->getValue());
Vikram S. Adve3652ef72001-10-01 00:12:53 +00001448 bool isValidConst;
Vikram S. Adve72213c92002-03-24 03:33:02 +00001449
Vikram S. Adve3652ef72001-10-01 00:12:53 +00001450 if ((constVal->getType()->isIntegral()
1451 || constVal->getType()->isPointerType())
1452 && GetConstantValueAsSignedInt(constVal, isValidConst) == 0
1453 && isValidConst)
1454 {
1455 // That constant is a zero after all...
1456 // Use the left child of setCC as the first argument!
Vikram S. Adve72213c92002-03-24 03:33:02 +00001457 // Mark the setCC node so that no code is generated for it.
1458 InstructionNode* setCCNode = (InstructionNode*)
1459 subtreeRoot->leftChild();
1460 assert(setCCNode->getOpLabel() == SetCCOp);
1461 setCCNode->markFoldedIntoParent();
1462
1463 BranchInst* brInst=cast<BranchInst>(subtreeRoot->getInstruction());
1464
Vikram S. Adve493b8242002-03-18 03:15:35 +00001465 M = new MachineInstr(ChooseBprInstruction(subtreeRoot));
1466 M->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister,
Vikram S. Adve72213c92002-03-24 03:33:02 +00001467 setCCNode->leftChild()->getValue());
Vikram S. Adve493b8242002-03-18 03:15:35 +00001468 M->SetMachineOperandVal(1, MachineOperand::MO_PCRelativeDisp,
1469 brInst->getSuccessor(0));
1470 mvec.push_back(M);
Vikram S. Adve72213c92002-03-24 03:33:02 +00001471
Vikram S. Adve3652ef72001-10-01 00:12:53 +00001472 // delay slot
Vikram S. Adve493b8242002-03-18 03:15:35 +00001473 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adve3652ef72001-10-01 00:12:53 +00001474
1475 // false branch
Vikram S. Adve493b8242002-03-18 03:15:35 +00001476 M = new MachineInstr(BA);
1477 M->SetMachineOperandVal(0, MachineOperand::MO_CCRegister,
1478 (Value*) NULL);
1479 M->SetMachineOperandVal(1, MachineOperand::MO_PCRelativeDisp,
Vikram S. Adve72213c92002-03-24 03:33:02 +00001480 brInst->getSuccessor(1));
Vikram S. Adve493b8242002-03-18 03:15:35 +00001481 mvec.push_back(M);
Vikram S. Adve3652ef72001-10-01 00:12:53 +00001482
1483 // delay slot
Vikram S. Adve493b8242002-03-18 03:15:35 +00001484 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adve3652ef72001-10-01 00:12:53 +00001485
1486 break;
1487 }
1488 // ELSE FALL THROUGH
Vikram S. Adveea5d1f52001-11-04 19:34:49 +00001489 }
Vikram S. Adve3652ef72001-10-01 00:12:53 +00001490
1491 case 6: // stmt: BrCond(bool)
Vikram S. Adveea5d1f52001-11-04 19:34:49 +00001492 { // bool => boolean was computed with some boolean operator
Vikram S. Adve3652ef72001-10-01 00:12:53 +00001493 // (SetCC, Not, ...). We need to check whether the type was a FP,
1494 // signed int or unsigned int, and check the branching condition in
1495 // order to choose the branch to use.
Vikram S. Adveea5d1f52001-11-04 19:34:49 +00001496 // If it is an integer CC, we also need to find the unique
1497 // TmpInstruction representing that CC.
Vikram S. Adve3652ef72001-10-01 00:12:53 +00001498 //
Vikram S. Adveea5d1f52001-11-04 19:34:49 +00001499 BranchInst* brInst = cast<BranchInst>(subtreeRoot->getInstruction());
Vikram S. Adve3652ef72001-10-01 00:12:53 +00001500 bool isFPBranch;
Vikram S. Adve493b8242002-03-18 03:15:35 +00001501 M = new MachineInstr(ChooseBccInstruction(subtreeRoot, isFPBranch));
Vikram S. Adveea5d1f52001-11-04 19:34:49 +00001502
Vikram S. Advef889a112001-11-08 05:04:09 +00001503 Value* ccValue = GetTmpForCC(subtreeRoot->leftChild()->getValue(),
1504 brInst->getParent()->getParent(),
1505 isFPBranch? Type::FloatTy : Type::IntTy);
Vikram S. Adveea5d1f52001-11-04 19:34:49 +00001506
Vikram S. Adve493b8242002-03-18 03:15:35 +00001507 M->SetMachineOperandVal(0, MachineOperand::MO_CCRegister, ccValue);
1508 M->SetMachineOperandVal(1, MachineOperand::MO_PCRelativeDisp,
1509 brInst->getSuccessor(0));
1510 mvec.push_back(M);
Vikram S. Adveea5d1f52001-11-04 19:34:49 +00001511
Vikram S. Adve3652ef72001-10-01 00:12:53 +00001512 // delay slot
Vikram S. Adve493b8242002-03-18 03:15:35 +00001513 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adveea5d1f52001-11-04 19:34:49 +00001514
Vikram S. Adve3652ef72001-10-01 00:12:53 +00001515 // false branch
Vikram S. Adve493b8242002-03-18 03:15:35 +00001516 M = new MachineInstr(BA);
1517 M->SetMachineOperandVal(0, MachineOperand::MO_CCRegister,
1518 (Value*) NULL);
1519 M->SetMachineOperandVal(1, MachineOperand::MO_PCRelativeDisp,
1520 brInst->getSuccessor(1));
1521 mvec.push_back(M);
Vikram S. Adve3652ef72001-10-01 00:12:53 +00001522
1523 // delay slot
Vikram S. Adve493b8242002-03-18 03:15:35 +00001524 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adve3652ef72001-10-01 00:12:53 +00001525 break;
Vikram S. Adveea5d1f52001-11-04 19:34:49 +00001526 }
Vikram S. Adve3652ef72001-10-01 00:12:53 +00001527
1528 case 208: // stmt: BrCond(boolconst)
Vikram S. Adveea5d1f52001-11-04 19:34:49 +00001529 {
Vikram S. Adve3652ef72001-10-01 00:12:53 +00001530 // boolconst => boolean is a constant; use BA to first or second label
Chris Lattner3462ae32001-12-03 22:26:30 +00001531 Constant* constVal =
1532 cast<Constant>(subtreeRoot->leftChild()->getValue());
1533 unsigned dest = cast<ConstantBool>(constVal)->getValue()? 0 : 1;
Vikram S. Adve3652ef72001-10-01 00:12:53 +00001534
Vikram S. Adve493b8242002-03-18 03:15:35 +00001535 M = new MachineInstr(BA);
1536 M->SetMachineOperandVal(0, MachineOperand::MO_CCRegister,
1537 (Value*) NULL);
1538 M->SetMachineOperandVal(1, MachineOperand::MO_PCRelativeDisp,
Vikram S. Adve3652ef72001-10-01 00:12:53 +00001539 ((BranchInst*) subtreeRoot->getInstruction())->getSuccessor(dest));
Vikram S. Adve493b8242002-03-18 03:15:35 +00001540 mvec.push_back(M);
Vikram S. Adve3652ef72001-10-01 00:12:53 +00001541
1542 // delay slot
Vikram S. Adve493b8242002-03-18 03:15:35 +00001543 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adve3652ef72001-10-01 00:12:53 +00001544 break;
Vikram S. Adveea5d1f52001-11-04 19:34:49 +00001545 }
Vikram S. Adve3652ef72001-10-01 00:12:53 +00001546
1547 case 8: // stmt: BrCond(boolreg)
Vikram S. Adveea5d1f52001-11-04 19:34:49 +00001548 { // boolreg => boolean is stored in an existing register.
Vikram S. Adve3652ef72001-10-01 00:12:53 +00001549 // Just use the branch-on-integer-register instruction!
1550 //
Vikram S. Adve493b8242002-03-18 03:15:35 +00001551 M = new MachineInstr(BRNZ);
1552 M->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister,
Vikram S. Adve3652ef72001-10-01 00:12:53 +00001553 subtreeRoot->leftChild()->getValue());
Vikram S. Adve493b8242002-03-18 03:15:35 +00001554 M->SetMachineOperandVal(1, MachineOperand::MO_PCRelativeDisp,
Vikram S. Adve3652ef72001-10-01 00:12:53 +00001555 ((BranchInst*) subtreeRoot->getInstruction())->getSuccessor(0));
Vikram S. Adve493b8242002-03-18 03:15:35 +00001556 mvec.push_back(M);
Vikram S. Adve3652ef72001-10-01 00:12:53 +00001557
1558 // delay slot
Vikram S. Adve493b8242002-03-18 03:15:35 +00001559 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adve3652ef72001-10-01 00:12:53 +00001560
1561 // false branch
Vikram S. Adve493b8242002-03-18 03:15:35 +00001562 M = new MachineInstr(BA);
1563 M->SetMachineOperandVal(0, MachineOperand::MO_CCRegister,
1564 (Value*) NULL);
1565 M->SetMachineOperandVal(1, MachineOperand::MO_PCRelativeDisp,
Vikram S. Adve3652ef72001-10-01 00:12:53 +00001566 ((BranchInst*) subtreeRoot->getInstruction())->getSuccessor(1));
Vikram S. Adve493b8242002-03-18 03:15:35 +00001567 mvec.push_back(M);
Vikram S. Adve3652ef72001-10-01 00:12:53 +00001568
1569 // delay slot
Vikram S. Adve493b8242002-03-18 03:15:35 +00001570 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adve3652ef72001-10-01 00:12:53 +00001571 break;
Vikram S. Adveea5d1f52001-11-04 19:34:49 +00001572 }
Chris Lattnerb0ddffa2001-09-14 03:47:57 +00001573
Vikram S. Adve3652ef72001-10-01 00:12:53 +00001574 case 9: // stmt: Switch(reg)
1575 assert(0 && "*** SWITCH instruction is not implemented yet.");
Vikram S. Adve3652ef72001-10-01 00:12:53 +00001576 break;
Chris Lattnerb0ddffa2001-09-14 03:47:57 +00001577
Vikram S. Adve3652ef72001-10-01 00:12:53 +00001578 case 10: // reg: VRegList(reg, reg)
1579 assert(0 && "VRegList should never be the topmost non-chain rule");
1580 break;
1581
Vikram S. Advef889a112001-11-08 05:04:09 +00001582 case 21: // bool: Not(bool): Both these are implemented as:
Vikram S. Adve313ca142002-04-01 20:28:48 +00001583 case 421: // reg: BNot(reg) : reg = reg XOR-NOT 0
Vikram S. Adve493b8242002-03-18 03:15:35 +00001584 M = new MachineInstr(XNOR);
1585 M->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister,
1586 subtreeRoot->leftChild()->getValue());
1587 M->SetMachineOperandReg(1, target.getRegInfo().getZeroRegNum());
1588 M->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister,
1589 subtreeRoot->getValue());
1590 mvec.push_back(M);
Vikram S. Adve3652ef72001-10-01 00:12:53 +00001591 break;
1592
1593 case 322: // reg: ToBoolTy(bool):
1594 case 22: // reg: ToBoolTy(reg):
Vikram S. Advebc23cf52001-11-09 02:18:16 +00001595 {
1596 const Type* opType = subtreeRoot->leftChild()->getValue()->getType();
1597 assert(opType->isIntegral() || opType->isPointerType()
1598 || opType == Type::BoolTy);
Vikram S. Adve493b8242002-03-18 03:15:35 +00001599 forwardOperandNum = 0; // forward first operand to user
Vikram S. Adve3652ef72001-10-01 00:12:53 +00001600 break;
Vikram S. Advebc23cf52001-11-09 02:18:16 +00001601 }
1602
Vikram S. Adve3652ef72001-10-01 00:12:53 +00001603 case 23: // reg: ToUByteTy(reg)
1604 case 25: // reg: ToUShortTy(reg)
1605 case 27: // reg: ToUIntTy(reg)
1606 case 29: // reg: ToULongTy(reg)
Vikram S. Advebc23cf52001-11-09 02:18:16 +00001607 {
1608 const Type* opType = subtreeRoot->leftChild()->getValue()->getType();
Vikram S. Adve3652ef72001-10-01 00:12:53 +00001609 assert(opType->isIntegral() ||
1610 opType->isPointerType() ||
1611 opType == Type::BoolTy && "Cast is illegal for other types");
Vikram S. Adve493b8242002-03-18 03:15:35 +00001612 forwardOperandNum = 0; // forward first operand to user
Vikram S. Adve3652ef72001-10-01 00:12:53 +00001613 break;
Vikram S. Advebc23cf52001-11-09 02:18:16 +00001614 }
1615
Vikram S. Adve3652ef72001-10-01 00:12:53 +00001616 case 24: // reg: ToSByteTy(reg)
1617 case 26: // reg: ToShortTy(reg)
1618 case 28: // reg: ToIntTy(reg)
1619 case 30: // reg: ToLongTy(reg)
Vikram S. Advebc23cf52001-11-09 02:18:16 +00001620 {
1621 const Type* opType = subtreeRoot->leftChild()->getValue()->getType();
1622 if (opType->isIntegral()
1623 || opType->isPointerType()
1624 || opType == Type::BoolTy)
Vikram S. Adve3652ef72001-10-01 00:12:53 +00001625 {
Vikram S. Adve493b8242002-03-18 03:15:35 +00001626 forwardOperandNum = 0; // forward first operand to user
Vikram S. Adve3652ef72001-10-01 00:12:53 +00001627 }
1628 else
1629 {
Vikram S. Advebc23cf52001-11-09 02:18:16 +00001630 // If the source operand is an FP type, the int result must be
1631 // copied from float to int register via memory!
1632 Instruction *dest = subtreeRoot->getInstruction();
1633 Value* leftVal = subtreeRoot->leftChild()->getValue();
1634 Value* destForCast;
1635 vector<MachineInstr*> minstrVec;
1636
1637 if (opType == Type::FloatTy || opType == Type::DoubleTy)
1638 {
1639 // Create a temporary to represent the INT register
1640 // into which the FP value will be copied via memory.
1641 // The type of this temporary will determine the FP
1642 // register used: single-prec for a 32-bit int or smaller,
1643 // double-prec for a 64-bit int.
1644 //
1645 const Type* destTypeToUse =
1646 (dest->getType() == Type::LongTy)? Type::DoubleTy
1647 : Type::FloatTy;
Chris Lattner477923d2002-02-03 07:50:56 +00001648 destForCast = new TmpInstruction(destTypeToUse, leftVal);
1649 MachineCodeForInstruction &MCFI =
1650 MachineCodeForInstruction::get(dest);
1651 MCFI.addTemp(destForCast);
Vikram S. Advebc23cf52001-11-09 02:18:16 +00001652
1653 vector<TmpInstruction*> tempVec;
1654 target.getInstrInfo().CreateCodeToCopyFloatToInt(
1655 dest->getParent()->getParent(),
1656 (TmpInstruction*) destForCast, dest,
1657 minstrVec, tempVec, target);
1658
1659 for (unsigned i=0; i < tempVec.size(); ++i)
Chris Lattner477923d2002-02-03 07:50:56 +00001660 MCFI.addTemp(tempVec[i]);
Vikram S. Advebc23cf52001-11-09 02:18:16 +00001661 }
1662 else
1663 destForCast = leftVal;
1664
1665 MachineOpCode opCode=ChooseConvertToIntInstr(subtreeRoot, opType);
1666 assert(opCode != INVALID_OPCODE && "Expected to need conversion!");
1667
Vikram S. Adve493b8242002-03-18 03:15:35 +00001668 M = new MachineInstr(opCode);
1669 M->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister,
1670 leftVal);
1671 M->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister,
1672 destForCast);
1673 mvec.push_back(M);
Vikram S. Advebc23cf52001-11-09 02:18:16 +00001674
Vikram S. Adve493b8242002-03-18 03:15:35 +00001675 // Append the copy code, if any, after the conversion instr.
1676 mvec.insert(mvec.end(), minstrVec.begin(), minstrVec.end());
Vikram S. Adve3652ef72001-10-01 00:12:53 +00001677 }
1678 break;
Vikram S. Advebc23cf52001-11-09 02:18:16 +00001679 }
1680
Vikram S. Adve3652ef72001-10-01 00:12:53 +00001681 case 31: // reg: ToFloatTy(reg):
1682 case 32: // reg: ToDoubleTy(reg):
1683 case 232: // reg: ToDoubleTy(Constant):
1684
1685 // If this instruction has a parent (a user) in the tree
1686 // and the user is translated as an FsMULd instruction,
1687 // then the cast is unnecessary. So check that first.
1688 // In the future, we'll want to do the same for the FdMULq instruction,
1689 // so do the check here instead of only for ToFloatTy(reg).
1690 //
1691 if (subtreeRoot->parent() != NULL &&
Chris Lattner477923d2002-02-03 07:50:56 +00001692 MachineCodeForInstruction::get(((InstructionNode*)subtreeRoot->parent())->getInstruction())[0]->getOpCode() == FSMULD)
Vikram S. Adve3652ef72001-10-01 00:12:53 +00001693 {
Vikram S. Adve493b8242002-03-18 03:15:35 +00001694 forwardOperandNum = 0; // forward first operand to user
Vikram S. Adve3652ef72001-10-01 00:12:53 +00001695 }
1696 else
1697 {
Vikram S. Advebc23cf52001-11-09 02:18:16 +00001698 Value* leftVal = subtreeRoot->leftChild()->getValue();
1699 const Type* opType = leftVal->getType();
Vikram S. Adve3652ef72001-10-01 00:12:53 +00001700 MachineOpCode opCode=ChooseConvertToFloatInstr(subtreeRoot,opType);
1701 if (opCode == INVALID_OPCODE) // no conversion needed
1702 {
Vikram S. Adve493b8242002-03-18 03:15:35 +00001703 forwardOperandNum = 0; // forward first operand to user
Vikram S. Adve3652ef72001-10-01 00:12:53 +00001704 }
1705 else
1706 {
Vikram S. Advef889a112001-11-08 05:04:09 +00001707 // If the source operand is a non-FP type it must be
1708 // first copied from int to float register via memory!
1709 Instruction *dest = subtreeRoot->getInstruction();
1710 Value* srcForCast;
1711 int n = 0;
1712 if (opType != Type::FloatTy && opType != Type::DoubleTy)
1713 {
1714 // Create a temporary to represent the FP register
1715 // into which the integer will be copied via memory.
Vikram S. Advebc23cf52001-11-09 02:18:16 +00001716 // The type of this temporary will determine the FP
1717 // register used: single-prec for a 32-bit int or smaller,
1718 // double-prec for a 64-bit int.
1719 //
1720 const Type* srcTypeToUse =
1721 (leftVal->getType() == Type::LongTy)? Type::DoubleTy
1722 : Type::FloatTy;
1723
Chris Lattner477923d2002-02-03 07:50:56 +00001724 srcForCast = new TmpInstruction(srcTypeToUse, dest);
1725 MachineCodeForInstruction &DestMCFI =
1726 MachineCodeForInstruction::get(dest);
1727 DestMCFI.addTemp(srcForCast);
Vikram S. Advef889a112001-11-08 05:04:09 +00001728
1729 vector<MachineInstr*> minstrVec;
1730 vector<TmpInstruction*> tempVec;
1731 target.getInstrInfo().CreateCodeToCopyIntToFloat(
1732 dest->getParent()->getParent(),
1733 leftVal, (TmpInstruction*) srcForCast,
1734 minstrVec, tempVec, target);
1735
Vikram S. Adve493b8242002-03-18 03:15:35 +00001736 mvec.insert(mvec.end(), minstrVec.begin(),minstrVec.end());
1737
Vikram S. Advef889a112001-11-08 05:04:09 +00001738 for (unsigned i=0; i < tempVec.size(); ++i)
Chris Lattner477923d2002-02-03 07:50:56 +00001739 DestMCFI.addTemp(tempVec[i]);
Vikram S. Advef889a112001-11-08 05:04:09 +00001740 }
1741 else
1742 srcForCast = leftVal;
1743
Vikram S. Adve493b8242002-03-18 03:15:35 +00001744 M = new MachineInstr(opCode);
1745 M->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister,
1746 srcForCast);
1747 M->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister,
1748 dest);
1749 mvec.push_back(M);
Vikram S. Adve3652ef72001-10-01 00:12:53 +00001750 }
1751 }
1752 break;
1753
1754 case 19: // reg: ToArrayTy(reg):
1755 case 20: // reg: ToPointerTy(reg):
Vikram S. Adve493b8242002-03-18 03:15:35 +00001756 forwardOperandNum = 0; // forward first operand to user
Vikram S. Adve3652ef72001-10-01 00:12:53 +00001757 break;
1758
1759 case 233: // reg: Add(reg, Constant)
Vikram S. Adve493b8242002-03-18 03:15:35 +00001760 M = CreateAddConstInstruction(subtreeRoot);
1761 if (M != NULL)
1762 {
1763 mvec.push_back(M);
1764 break;
1765 }
Vikram S. Adve3652ef72001-10-01 00:12:53 +00001766 // ELSE FALL THROUGH
Vikram S. Adve493b8242002-03-18 03:15:35 +00001767
Vikram S. Adve3652ef72001-10-01 00:12:53 +00001768 case 33: // reg: Add(reg, reg)
Vikram S. Adve493b8242002-03-18 03:15:35 +00001769 mvec.push_back(new MachineInstr(ChooseAddInstruction(subtreeRoot)));
1770 Set3OperandsFromInstr(mvec.back(), subtreeRoot, target);
Vikram S. Adve3652ef72001-10-01 00:12:53 +00001771 break;
1772
1773 case 234: // reg: Sub(reg, Constant)
Vikram S. Adve493b8242002-03-18 03:15:35 +00001774 M = CreateSubConstInstruction(subtreeRoot);
1775 if (M != NULL)
1776 {
1777 mvec.push_back(M);
1778 break;
1779 }
Vikram S. Adve3652ef72001-10-01 00:12:53 +00001780 // ELSE FALL THROUGH
Vikram S. Adve493b8242002-03-18 03:15:35 +00001781
Vikram S. Adve3652ef72001-10-01 00:12:53 +00001782 case 34: // reg: Sub(reg, reg)
Vikram S. Adve493b8242002-03-18 03:15:35 +00001783 mvec.push_back(new MachineInstr(ChooseSubInstructionByType(
1784 subtreeRoot->getInstruction()->getType())));
1785 Set3OperandsFromInstr(mvec.back(), subtreeRoot, target);
Vikram S. Adve3652ef72001-10-01 00:12:53 +00001786 break;
1787
1788 case 135: // reg: Mul(todouble, todouble)
1789 checkCast = true;
1790 // FALL THROUGH
1791
1792 case 35: // reg: Mul(reg, reg)
Vikram S. Adve493b8242002-03-18 03:15:35 +00001793 {
1794 MachineOpCode forceOp = ((checkCast && BothFloatToDouble(subtreeRoot))
1795 ? FSMULD
1796 : INVALID_MACHINE_OPCODE);
1797 CreateMulInstruction(target,
1798 subtreeRoot->leftChild()->getValue(),
1799 subtreeRoot->rightChild()->getValue(),
1800 subtreeRoot->getInstruction(),
1801 mvec, forceOp);
Vikram S. Adve3652ef72001-10-01 00:12:53 +00001802 break;
Vikram S. Adve493b8242002-03-18 03:15:35 +00001803 }
Vikram S. Adve3652ef72001-10-01 00:12:53 +00001804 case 335: // reg: Mul(todouble, todoubleConst)
1805 checkCast = true;
1806 // FALL THROUGH
1807
1808 case 235: // reg: Mul(reg, Constant)
Vikram S. Adve493b8242002-03-18 03:15:35 +00001809 {
1810 MachineOpCode forceOp = ((checkCast && BothFloatToDouble(subtreeRoot))
1811 ? FSMULD
1812 : INVALID_MACHINE_OPCODE);
1813 CreateMulInstruction(target,
1814 subtreeRoot->leftChild()->getValue(),
1815 subtreeRoot->rightChild()->getValue(),
1816 subtreeRoot->getInstruction(),
1817 mvec, forceOp);
Vikram S. Adve3652ef72001-10-01 00:12:53 +00001818 break;
Vikram S. Adve493b8242002-03-18 03:15:35 +00001819 }
Vikram S. Adve3652ef72001-10-01 00:12:53 +00001820 case 236: // reg: Div(reg, Constant)
Vikram S. Adve493b8242002-03-18 03:15:35 +00001821 L = mvec.size();
1822 CreateDivConstInstruction(target, subtreeRoot, mvec);
1823 if (mvec.size() > L)
1824 break;
Vikram S. Adve3652ef72001-10-01 00:12:53 +00001825 // ELSE FALL THROUGH
Vikram S. Adve493b8242002-03-18 03:15:35 +00001826
Vikram S. Adve3652ef72001-10-01 00:12:53 +00001827 case 36: // reg: Div(reg, reg)
Vikram S. Adve493b8242002-03-18 03:15:35 +00001828 mvec.push_back(new MachineInstr(ChooseDivInstruction(target, subtreeRoot)));
1829 Set3OperandsFromInstr(mvec.back(), subtreeRoot, target);
Vikram S. Adve3652ef72001-10-01 00:12:53 +00001830 break;
1831
1832 case 37: // reg: Rem(reg, reg)
1833 case 237: // reg: Rem(reg, Constant)
Vikram S. Adve80cf6062001-11-04 21:59:14 +00001834 {
1835 Instruction* remInstr = subtreeRoot->getInstruction();
1836
Chris Lattner477923d2002-02-03 07:50:56 +00001837 TmpInstruction* quot = new TmpInstruction(
Vikram S. Adve80cf6062001-11-04 21:59:14 +00001838 subtreeRoot->leftChild()->getValue(),
1839 subtreeRoot->rightChild()->getValue());
Chris Lattner477923d2002-02-03 07:50:56 +00001840 TmpInstruction* prod = new TmpInstruction(
Vikram S. Adve80cf6062001-11-04 21:59:14 +00001841 quot,
1842 subtreeRoot->rightChild()->getValue());
Chris Lattner477923d2002-02-03 07:50:56 +00001843 MachineCodeForInstruction::get(remInstr).addTemp(quot).addTemp(prod);
Vikram S. Adve80cf6062001-11-04 21:59:14 +00001844
Vikram S. Adve493b8242002-03-18 03:15:35 +00001845 M = new MachineInstr(ChooseDivInstruction(target, subtreeRoot));
1846 Set3OperandsFromInstr(M, subtreeRoot, target);
1847 M->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister,quot);
1848 mvec.push_back(M);
Vikram S. Adve80cf6062001-11-04 21:59:14 +00001849
Vikram S. Adve493b8242002-03-18 03:15:35 +00001850 M = new MachineInstr(ChooseMulInstructionByType(
Vikram S. Adve80cf6062001-11-04 21:59:14 +00001851 subtreeRoot->getInstruction()->getType()));
Vikram S. Adve493b8242002-03-18 03:15:35 +00001852 M->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister,quot);
1853 M->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister,
Vikram S. Adve80cf6062001-11-04 21:59:14 +00001854 subtreeRoot->rightChild()->getValue());
Vikram S. Adve493b8242002-03-18 03:15:35 +00001855 M->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister,prod);
1856 mvec.push_back(M);
Vikram S. Adve80cf6062001-11-04 21:59:14 +00001857
Vikram S. Adve493b8242002-03-18 03:15:35 +00001858 M = new MachineInstr(ChooseSubInstructionByType(
Vikram S. Adve80cf6062001-11-04 21:59:14 +00001859 subtreeRoot->getInstruction()->getType()));
Vikram S. Adve493b8242002-03-18 03:15:35 +00001860 Set3OperandsFromInstr(M, subtreeRoot, target);
1861 M->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister,prod);
1862 mvec.push_back(M);
Vikram S. Adve80cf6062001-11-04 21:59:14 +00001863
Vikram S. Adve3652ef72001-10-01 00:12:53 +00001864 break;
Vikram S. Adve80cf6062001-11-04 21:59:14 +00001865 }
1866
Vikram S. Advef889a112001-11-08 05:04:09 +00001867 case 38: // bool: And(bool, bool)
1868 case 238: // bool: And(bool, boolconst)
1869 case 338: // reg : BAnd(reg, reg)
1870 case 538: // reg : BAnd(reg, Constant)
Vikram S. Adve493b8242002-03-18 03:15:35 +00001871 mvec.push_back(new MachineInstr(AND));
1872 Set3OperandsFromInstr(mvec.back(), subtreeRoot, target);
Vikram S. Adve3652ef72001-10-01 00:12:53 +00001873 break;
1874
Vikram S. Advef889a112001-11-08 05:04:09 +00001875 case 138: // bool: And(bool, not)
1876 case 438: // bool: BAnd(bool, not)
Vikram S. Adve493b8242002-03-18 03:15:35 +00001877 mvec.push_back(new MachineInstr(ANDN));
1878 Set3OperandsFromInstr(mvec.back(), subtreeRoot, target);
Vikram S. Adve3652ef72001-10-01 00:12:53 +00001879 break;
1880
Vikram S. Advef889a112001-11-08 05:04:09 +00001881 case 39: // bool: Or(bool, bool)
1882 case 239: // bool: Or(bool, boolconst)
1883 case 339: // reg : BOr(reg, reg)
1884 case 539: // reg : BOr(reg, Constant)
Vikram S. Adve493b8242002-03-18 03:15:35 +00001885 mvec.push_back(new MachineInstr(ORN));
1886 Set3OperandsFromInstr(mvec.back(), subtreeRoot, target);
Vikram S. Adve3652ef72001-10-01 00:12:53 +00001887 break;
1888
Vikram S. Advef889a112001-11-08 05:04:09 +00001889 case 139: // bool: Or(bool, not)
1890 case 439: // bool: BOr(bool, not)
Vikram S. Adve493b8242002-03-18 03:15:35 +00001891 mvec.push_back(new MachineInstr(ORN));
1892 Set3OperandsFromInstr(mvec.back(), subtreeRoot, target);
Vikram S. Adve3652ef72001-10-01 00:12:53 +00001893 break;
1894
Vikram S. Advef889a112001-11-08 05:04:09 +00001895 case 40: // bool: Xor(bool, bool)
1896 case 240: // bool: Xor(bool, boolconst)
1897 case 340: // reg : BXor(reg, reg)
1898 case 540: // reg : BXor(reg, Constant)
Vikram S. Adve493b8242002-03-18 03:15:35 +00001899 mvec.push_back(new MachineInstr(XOR));
1900 Set3OperandsFromInstr(mvec.back(), subtreeRoot, target);
Vikram S. Adve3652ef72001-10-01 00:12:53 +00001901 break;
1902
Vikram S. Advef889a112001-11-08 05:04:09 +00001903 case 140: // bool: Xor(bool, not)
1904 case 440: // bool: BXor(bool, not)
Vikram S. Adve493b8242002-03-18 03:15:35 +00001905 mvec.push_back(new MachineInstr(XNOR));
1906 Set3OperandsFromInstr(mvec.back(), subtreeRoot, target);
Vikram S. Adve3652ef72001-10-01 00:12:53 +00001907 break;
1908
1909 case 41: // boolconst: SetCC(reg, Constant)
Vikram S. Adve3652ef72001-10-01 00:12:53 +00001910 //
Vikram S. Adve72213c92002-03-24 03:33:02 +00001911 // If the SetCC was folded into the user (parent), it will be
1912 // caught above. All other cases are the same as case 42,
1913 // so just fall through.
Vikram S. Adve3652ef72001-10-01 00:12:53 +00001914 //
Vikram S. Adve3652ef72001-10-01 00:12:53 +00001915 case 42: // bool: SetCC(reg, reg):
1916 {
Vikram S. Adveea5d1f52001-11-04 19:34:49 +00001917 // This generates a SUBCC instruction, putting the difference in
1918 // a result register, and setting a condition code.
Vikram S. Adve3652ef72001-10-01 00:12:53 +00001919 //
Vikram S. Adveea5d1f52001-11-04 19:34:49 +00001920 // If the boolean result of the SetCC is used by anything other
1921 // than a single branch instruction, the boolean must be
1922 // computed and stored in the result register. Otherwise, discard
1923 // the difference (by using %g0) and keep only the condition code.
1924 //
1925 // To compute the boolean result in a register we use a conditional
1926 // move, unless the result of the SUBCC instruction can be used as
1927 // the bool! This assumes that zero is FALSE and any non-zero
1928 // integer is TRUE.
1929 //
1930 InstructionNode* parentNode = (InstructionNode*) subtreeRoot->parent();
1931 Instruction* setCCInstr = subtreeRoot->getInstruction();
1932 bool keepBoolVal = (parentNode == NULL ||
1933 parentNode->getInstruction()->getOpcode()
1934 != Instruction::Br);
1935 bool subValIsBoolVal = setCCInstr->getOpcode() == Instruction::SetNE;
Vikram S. Adve3652ef72001-10-01 00:12:53 +00001936 bool keepSubVal = keepBoolVal && subValIsBoolVal;
1937 bool computeBoolVal = keepBoolVal && ! subValIsBoolVal;
1938
1939 bool mustClearReg;
1940 int valueToMove;
Chris Lattnere147d062001-11-07 14:01:59 +00001941 MachineOpCode movOpCode = 0;
Vikram S. Advef889a112001-11-08 05:04:09 +00001942
1943 // Mark the 4th operand as being a CC register, and as a def
1944 // A TmpInstruction is created to represent the CC "result".
1945 // Unlike other instances of TmpInstruction, this one is used
1946 // by machine code of multiple LLVM instructions, viz.,
1947 // the SetCC and the branch. Make sure to get the same one!
1948 // Note that we do this even for FP CC registers even though they
1949 // are explicit operands, because the type of the operand
1950 // needs to be a floating point condition code, not an integer
1951 // condition code. Think of this as casting the bool result to
1952 // a FP condition code register.
1953 //
Vikram S. Advebc23cf52001-11-09 02:18:16 +00001954 Value* leftVal = subtreeRoot->leftChild()->getValue();
Vikram S. Advef889a112001-11-08 05:04:09 +00001955 bool isFPCompare = (leftVal->getType() == Type::FloatTy ||
1956 leftVal->getType() == Type::DoubleTy);
Vikram S. Adve3652ef72001-10-01 00:12:53 +00001957
Vikram S. Advef889a112001-11-08 05:04:09 +00001958 TmpInstruction* tmpForCC = GetTmpForCC(setCCInstr,
1959 setCCInstr->getParent()->getParent(),
1960 isFPCompare? Type::FloatTy : Type::IntTy);
Chris Lattner477923d2002-02-03 07:50:56 +00001961 MachineCodeForInstruction::get(setCCInstr).addTemp(tmpForCC);
Vikram S. Advef889a112001-11-08 05:04:09 +00001962
1963 if (! isFPCompare)
Vikram S. Adve3652ef72001-10-01 00:12:53 +00001964 {
1965 // Integer condition: dest. should be %g0 or an integer register.
1966 // If result must be saved but condition is not SetEQ then we need
1967 // a separate instruction to compute the bool result, so discard
1968 // result of SUBcc instruction anyway.
1969 //
Vikram S. Adve493b8242002-03-18 03:15:35 +00001970 M = new MachineInstr(SUBcc);
1971 Set3OperandsFromInstr(M, subtreeRoot, target, ! keepSubVal);
1972 M->SetMachineOperandVal(3, MachineOperand::MO_CCRegister,
1973 tmpForCC, /*def*/true);
1974 mvec.push_back(M);
Vikram S. Adve3652ef72001-10-01 00:12:53 +00001975
1976 if (computeBoolVal)
1977 { // recompute bool using the integer condition codes
1978 movOpCode =
1979 ChooseMovpccAfterSub(subtreeRoot,mustClearReg,valueToMove);
1980 }
1981 }
1982 else
1983 {
1984 // FP condition: dest of FCMP should be some FCCn register
Vikram S. Adve493b8242002-03-18 03:15:35 +00001985 M = new MachineInstr(ChooseFcmpInstruction(subtreeRoot));
1986 M->SetMachineOperandVal(0, MachineOperand::MO_CCRegister,
Vikram S. Advef889a112001-11-08 05:04:09 +00001987 tmpForCC);
Vikram S. Adve493b8242002-03-18 03:15:35 +00001988 M->SetMachineOperandVal(1,MachineOperand::MO_VirtualRegister,
Vikram S. Adve3652ef72001-10-01 00:12:53 +00001989 subtreeRoot->leftChild()->getValue());
Vikram S. Adve493b8242002-03-18 03:15:35 +00001990 M->SetMachineOperandVal(2,MachineOperand::MO_VirtualRegister,
Vikram S. Adve3652ef72001-10-01 00:12:53 +00001991 subtreeRoot->rightChild()->getValue());
Vikram S. Adve493b8242002-03-18 03:15:35 +00001992 mvec.push_back(M);
Vikram S. Adve3652ef72001-10-01 00:12:53 +00001993
1994 if (computeBoolVal)
1995 {// recompute bool using the FP condition codes
1996 mustClearReg = true;
1997 valueToMove = 1;
1998 movOpCode = ChooseMovFpccInstruction(subtreeRoot);
1999 }
2000 }
2001
2002 if (computeBoolVal)
2003 {
2004 if (mustClearReg)
2005 {// Unconditionally set register to 0
Vikram S. Adve493b8242002-03-18 03:15:35 +00002006 M = new MachineInstr(SETHI);
2007 M->SetMachineOperandConst(0,MachineOperand::MO_UnextendedImmed,
2008 (int64_t)0);
2009 M->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister,
2010 setCCInstr);
2011 mvec.push_back(M);
Vikram S. Adve3652ef72001-10-01 00:12:53 +00002012 }
2013
2014 // Now conditionally move `valueToMove' (0 or 1) into the register
Vikram S. Adve493b8242002-03-18 03:15:35 +00002015 M = new MachineInstr(movOpCode);
2016 M->SetMachineOperandVal(0, MachineOperand::MO_CCRegister,
2017 tmpForCC);
2018 M->SetMachineOperandConst(1, MachineOperand::MO_UnextendedImmed,
2019 valueToMove);
2020 M->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister,
2021 setCCInstr);
2022 mvec.push_back(M);
Vikram S. Adve3652ef72001-10-01 00:12:53 +00002023 }
2024 break;
2025 }
2026
2027 case 43: // boolreg: VReg
2028 case 44: // boolreg: Constant
Vikram S. Adve3652ef72001-10-01 00:12:53 +00002029 break;
2030
2031 case 51: // reg: Load(reg)
2032 case 52: // reg: Load(ptrreg)
2033 case 53: // reg: LoadIdx(reg,reg)
2034 case 54: // reg: LoadIdx(ptrreg,reg)
Vikram S. Adve493b8242002-03-18 03:15:35 +00002035 mvec.push_back(new MachineInstr(ChooseLoadInstruction(
2036 subtreeRoot->getValue()->getType())));
2037 SetOperandsForMemInstr(mvec, mvec.end()-1, subtreeRoot, target);
Vikram S. Adve3652ef72001-10-01 00:12:53 +00002038 break;
2039
2040 case 55: // reg: GetElemPtr(reg)
2041 case 56: // reg: GetElemPtrIdx(reg,reg)
Vikram S. Adve72213c92002-03-24 03:33:02 +00002042 // If the GetElemPtr was folded into the user (parent), it will be
2043 // caught above. For other cases, we have to compute the address.
Vikram S. Adve493b8242002-03-18 03:15:35 +00002044 mvec.push_back(new MachineInstr(ADD));
2045 SetOperandsForMemInstr(mvec, mvec.end()-1, subtreeRoot, target);
Vikram S. Adve3652ef72001-10-01 00:12:53 +00002046 break;
Vikram S. Adve72213c92002-03-24 03:33:02 +00002047
Vikram S. Adveea5d1f52001-11-04 19:34:49 +00002048 case 57: // reg: Alloca: Implement as 1 instruction:
2049 { // add %fp, offsetFromFP -> result
Vikram S. Adve72213c92002-03-24 03:33:02 +00002050 AllocationInst* instr =
2051 cast<AllocationInst>(subtreeRoot->getInstruction());
2052 unsigned int tsize =
2053 target.findOptimalStorageSize(instr->getAllocatedType());
Vikram S. Adve493b8242002-03-18 03:15:35 +00002054 assert(tsize != 0);
2055 CreateCodeForFixedSizeAlloca(target, instr, tsize, 1, mvec);
Vikram S. Adve3652ef72001-10-01 00:12:53 +00002056 break;
Vikram S. Adveea5d1f52001-11-04 19:34:49 +00002057 }
Vikram S. Adve493b8242002-03-18 03:15:35 +00002058
Vikram S. Adve3652ef72001-10-01 00:12:53 +00002059 case 58: // reg: Alloca(reg): Implement as 3 instructions:
2060 // mul num, typeSz -> tmp
2061 // sub %sp, tmp -> %sp
Vikram S. Adveea5d1f52001-11-04 19:34:49 +00002062 { // add %sp, frameSizeBelowDynamicArea -> result
Vikram S. Adve72213c92002-03-24 03:33:02 +00002063 AllocationInst* instr =
2064 cast<AllocationInst>(subtreeRoot->getInstruction());
Vikram S. Adve493b8242002-03-18 03:15:35 +00002065 const Type* eltType = instr->getAllocatedType();
2066
Vikram S. Adve72213c92002-03-24 03:33:02 +00002067 // If #elements is constant, use simpler code for fixed-size allocas
Vikram S. Adve3652ef72001-10-01 00:12:53 +00002068 int tsize = (int) target.findOptimalStorageSize(eltType);
Vikram S. Adve72213c92002-03-24 03:33:02 +00002069 Value* numElementsVal = NULL;
2070 bool isArray = instr->isArrayAllocation();
2071
2072 if (!isArray ||
2073 isa<Constant>(numElementsVal = instr->getArraySize()))
2074 { // total size is constant: generate code for fixed-size alloca
2075 unsigned int numElements = isArray?
2076 cast<ConstantUInt>(numElementsVal)->getValue() : 1;
2077 CreateCodeForFixedSizeAlloca(target, instr, tsize,
2078 numElements, mvec);
2079 }
Vikram S. Adve493b8242002-03-18 03:15:35 +00002080 else // total size is not constant.
2081 CreateCodeForVariableSizeAlloca(target, instr, tsize,
Vikram S. Adve72213c92002-03-24 03:33:02 +00002082 numElementsVal, mvec);
Vikram S. Adve3652ef72001-10-01 00:12:53 +00002083 break;
Vikram S. Adveea5d1f52001-11-04 19:34:49 +00002084 }
Vikram S. Adve493b8242002-03-18 03:15:35 +00002085
Vikram S. Adve3652ef72001-10-01 00:12:53 +00002086 case 61: // reg: Call
Vikram S. Adveea5d1f52001-11-04 19:34:49 +00002087 { // Generate a call-indirect (i.e., jmpl) for now to expose
Vikram S. Adve3652ef72001-10-01 00:12:53 +00002088 // the potential need for registers. If an absolute address
2089 // is available, replace this with a CALL instruction.
2090 // Mark both the indirection register and the return-address
2091 // register as hidden virtual registers.
Vikram S. Adve8aeb09f2001-10-11 04:23:19 +00002092 // Also, mark the operands of the Call and return value (if
2093 // any) as implicit operands of the CALL machine instruction.
Vikram S. Adveea5d1f52001-11-04 19:34:49 +00002094 //
Chris Lattnerda558102001-10-02 03:41:24 +00002095 CallInst *callInstr = cast<CallInst>(subtreeRoot->getInstruction());
Chris Lattnera742b6a2001-10-13 06:54:30 +00002096 Value *callee = callInstr->getCalledValue();
Vikram S. Adve3652ef72001-10-01 00:12:53 +00002097
Vikram S. Adve4973f732002-03-31 19:07:35 +00002098 // Create hidden virtual register for return address, with type void*.
2099 Instruction* retAddrReg =
2100 new TmpInstruction(PointerType::get(Type::VoidTy), callInstr);
Chris Lattner477923d2002-02-03 07:50:56 +00002101 MachineCodeForInstruction::get(callInstr).addTemp(retAddrReg);
Vikram S. Adve3652ef72001-10-01 00:12:53 +00002102
Vikram S. Adve57312492001-10-20 20:57:06 +00002103 // Generate the machine instruction and its operands.
2104 // Use CALL for direct function calls; this optimistically assumes
2105 // the PC-relative address fits in the CALL address field (22 bits).
2106 // Use JMPL for indirect calls.
2107 //
Chris Lattner5d088b02002-03-26 17:58:12 +00002108 if (isa<Function>(callee))
Vikram S. Adve57312492001-10-20 20:57:06 +00002109 { // direct function call
Vikram S. Adve493b8242002-03-18 03:15:35 +00002110 M = new MachineInstr(CALL);
2111 M->SetMachineOperandVal(0, MachineOperand::MO_PCRelativeDisp,
2112 callee);
Vikram S. Adve57312492001-10-20 20:57:06 +00002113 }
2114 else
2115 { // indirect function call
Vikram S. Adve493b8242002-03-18 03:15:35 +00002116 M = new MachineInstr(JMPLCALL);
2117 M->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister,
2118 callee);
2119 M->SetMachineOperandConst(1, MachineOperand::MO_SignExtendedImmed,
2120 (int64_t) 0);
2121 M->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister,
2122 retAddrReg);
Vikram S. Adve57312492001-10-20 20:57:06 +00002123 }
Vikram S. Adve3652ef72001-10-01 00:12:53 +00002124
Vikram S. Adve493b8242002-03-18 03:15:35 +00002125 mvec.push_back(M);
Vikram S. Adve4973f732002-03-31 19:07:35 +00002126
2127 // WARNING: Operands 0..N-1 must go in slots 0..N-1 of implicitUses.
2128 // The result value must go in slot N. This is assumed
2129 // in register allocation.
2130 //
Vikram S. Adve8aeb09f2001-10-11 04:23:19 +00002131 // Add the call operands and return value as implicit refs
2132 for (unsigned i=0, N=callInstr->getNumOperands(); i < N; ++i)
2133 if (callInstr->getOperand(i) != callee)
Vikram S. Adve493b8242002-03-18 03:15:35 +00002134 mvec.back()->addImplicitRef(callInstr->getOperand(i));
Vikram S. Adve8aeb09f2001-10-11 04:23:19 +00002135
Vikram S. Adve57312492001-10-20 20:57:06 +00002136 if (callInstr->getType() != Type::VoidTy)
Vikram S. Adve493b8242002-03-18 03:15:35 +00002137 mvec.back()->addImplicitRef(callInstr, /*isDef*/ true);
Vikram S. Adve8aeb09f2001-10-11 04:23:19 +00002138
Vikram S. Adve57312492001-10-20 20:57:06 +00002139 // For the CALL instruction, the ret. addr. reg. is also implicit
Chris Lattner5d088b02002-03-26 17:58:12 +00002140 if (isa<Function>(callee))
Vikram S. Adve493b8242002-03-18 03:15:35 +00002141 mvec.back()->addImplicitRef(retAddrReg, /*isDef*/ true);
Vikram S. Adve3652ef72001-10-01 00:12:53 +00002142
Vikram S. Adve493b8242002-03-18 03:15:35 +00002143 // delay slot
2144 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adve3652ef72001-10-01 00:12:53 +00002145 break;
Vikram S. Adveea5d1f52001-11-04 19:34:49 +00002146 }
Vikram S. Adve3652ef72001-10-01 00:12:53 +00002147
2148 case 62: // reg: Shl(reg, reg)
Vikram S. Advebc23cf52001-11-09 02:18:16 +00002149 { const Type* opType = subtreeRoot->leftChild()->getValue()->getType();
Vikram S. Adve3652ef72001-10-01 00:12:53 +00002150 assert(opType->isIntegral()
2151 || opType == Type::BoolTy
2152 || opType->isPointerType()&& "Shl unsupported for other types");
Vikram S. Adve493b8242002-03-18 03:15:35 +00002153 mvec.push_back(new MachineInstr((opType == Type::LongTy)? SLLX : SLL));
2154 Set3OperandsFromInstr(mvec.back(), subtreeRoot, target);
Vikram S. Adve3652ef72001-10-01 00:12:53 +00002155 break;
Vikram S. Advebc23cf52001-11-09 02:18:16 +00002156 }
2157
Vikram S. Adve3652ef72001-10-01 00:12:53 +00002158 case 63: // reg: Shr(reg, reg)
Vikram S. Advebc23cf52001-11-09 02:18:16 +00002159 { const Type* opType = subtreeRoot->leftChild()->getValue()->getType();
Vikram S. Adve3652ef72001-10-01 00:12:53 +00002160 assert(opType->isIntegral()
2161 || opType == Type::BoolTy
2162 || opType->isPointerType() &&"Shr unsupported for other types");
Vikram S. Adve493b8242002-03-18 03:15:35 +00002163 mvec.push_back(new MachineInstr((opType->isSigned()
2164 ? ((opType == Type::LongTy)? SRAX : SRA)
2165 : ((opType == Type::LongTy)? SRLX : SRL))));
2166 Set3OperandsFromInstr(mvec.back(), subtreeRoot, target);
Vikram S. Adve3652ef72001-10-01 00:12:53 +00002167 break;
Vikram S. Advebc23cf52001-11-09 02:18:16 +00002168 }
2169
Vikram S. Adve3652ef72001-10-01 00:12:53 +00002170 case 64: // reg: Phi(reg,reg)
Vikram S. Adve493b8242002-03-18 03:15:35 +00002171 break; // don't forward the value
2172
Vikram S. Adve01daa322001-11-12 18:54:11 +00002173#undef NEED_PHI_MACHINE_INSTRS
2174#ifdef NEED_PHI_MACHINE_INSTRS
Vikram S. Adve3652ef72001-10-01 00:12:53 +00002175 { // This instruction has variable #operands, so resultPos is 0.
2176 Instruction* phi = subtreeRoot->getInstruction();
Vikram S. Adve493b8242002-03-18 03:15:35 +00002177 M = new MachineInstr(PHI, 1 + phi->getNumOperands());
2178 M->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister,
Vikram S. Adve3652ef72001-10-01 00:12:53 +00002179 subtreeRoot->getValue());
2180 for (unsigned i=0, N=phi->getNumOperands(); i < N; i++)
Vikram S. Adve493b8242002-03-18 03:15:35 +00002181 M->SetMachineOperandVal(i+1, MachineOperand::MO_VirtualRegister,
2182 phi->getOperand(i));
2183 mvec.push_back(M);
Vikram S. Adve3652ef72001-10-01 00:12:53 +00002184 break;
2185 }
Chris Lattner7f74a562002-01-20 22:54:45 +00002186#endif // NEED_PHI_MACHINE_INSTRS
Vikram S. Advebc23cf52001-11-09 02:18:16 +00002187
Vikram S. Adve493b8242002-03-18 03:15:35 +00002188
Vikram S. Adve3652ef72001-10-01 00:12:53 +00002189 case 71: // reg: VReg
2190 case 72: // reg: Constant
Vikram S. Adve493b8242002-03-18 03:15:35 +00002191 break; // don't forward the value
Vikram S. Adve3652ef72001-10-01 00:12:53 +00002192
2193 default:
2194 assert(0 && "Unrecognized BURG rule");
Vikram S. Adve3652ef72001-10-01 00:12:53 +00002195 break;
2196 }
Chris Lattnerb0ddffa2001-09-14 03:47:57 +00002197 }
Chris Lattnerb0ddffa2001-09-14 03:47:57 +00002198
2199 if (forwardOperandNum >= 0)
2200 { // We did not generate a machine instruction but need to use operand.
2201 // If user is in the same tree, replace Value in its machine operand.
2202 // If not, insert a copy instruction which should get coalesced away
2203 // by register allocation.
2204 if (subtreeRoot->parent() != NULL)
Vikram S. Adve3652ef72001-10-01 00:12:53 +00002205 ForwardOperand(subtreeRoot, subtreeRoot->parent(), forwardOperandNum);
Chris Lattnerb0ddffa2001-09-14 03:47:57 +00002206 else
Vikram S. Adve3652ef72001-10-01 00:12:53 +00002207 {
Vikram S. Adve6c697522001-10-18 00:26:20 +00002208 vector<MachineInstr*> minstrVec;
Vikram S. Adve493b8242002-03-18 03:15:35 +00002209 target.getInstrInfo().CreateCopyInstructionsByType(target,
2210 subtreeRoot->getInstruction()->getParent()->getParent(),
Vikram S. Adve3652ef72001-10-01 00:12:53 +00002211 subtreeRoot->getInstruction()->getOperand(forwardOperandNum),
Vikram S. Adve6c697522001-10-18 00:26:20 +00002212 subtreeRoot->getInstruction(), minstrVec);
2213 assert(minstrVec.size() > 0);
Vikram S. Adve493b8242002-03-18 03:15:35 +00002214 mvec.insert(mvec.end(), minstrVec.begin(), minstrVec.end());
Vikram S. Adve3652ef72001-10-01 00:12:53 +00002215 }
Chris Lattnerb0ddffa2001-09-14 03:47:57 +00002216 }
Chris Lattnerb0ddffa2001-09-14 03:47:57 +00002217}
2218
2219