blob: a68331b53ed3ca15dfcf838218857c4111d6ded3 [file] [log] [blame]
Chris Lattner035dfbe2002-08-09 20:08:06 +00001//===-- SparcInstrSelection.cpp -------------------------------------------===//
2//
3// BURS instruction selection for SPARC V9 architecture.
4//
5//===----------------------------------------------------------------------===//
Chris Lattner20b1ea02001-09-14 03:47:57 +00006
7#include "SparcInternals.h"
Vikram S. Adve7fe27872001-10-18 00:26:20 +00008#include "SparcInstrSelectionSupport.h"
Vikram S. Adve74825322002-03-18 03:15:35 +00009#include "SparcRegClassInfo.h"
Vikram S. Adve8557b222001-10-10 20:56:33 +000010#include "llvm/CodeGen/InstrSelectionSupport.h"
Chris Lattner20b1ea02001-09-14 03:47:57 +000011#include "llvm/CodeGen/MachineInstr.h"
Vikram S. Adve242a8082002-05-19 15:25:51 +000012#include "llvm/CodeGen/MachineInstrAnnot.h"
Chris Lattner20b1ea02001-09-14 03:47:57 +000013#include "llvm/CodeGen/InstrForest.h"
14#include "llvm/CodeGen/InstrSelection.h"
Chris Lattner9c461082002-02-03 07:50:56 +000015#include "llvm/CodeGen/MachineCodeForMethod.h"
16#include "llvm/CodeGen/MachineCodeForInstruction.h"
Chris Lattner20b1ea02001-09-14 03:47:57 +000017#include "llvm/DerivedTypes.h"
18#include "llvm/iTerminators.h"
19#include "llvm/iMemory.h"
20#include "llvm/iOther.h"
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000021#include "llvm/Function.h"
Chris Lattner31bcdb82002-04-28 19:55:58 +000022#include "llvm/Constants.h"
Chris Lattnercee8f9a2001-11-27 00:03:19 +000023#include "Support/MathExtras.h"
Chris Lattner749655f2001-10-13 06:54:30 +000024#include <math.h>
Chris Lattner697954c2002-01-20 22:54:45 +000025using std::vector;
Chris Lattner20b1ea02001-09-14 03:47:57 +000026
27//************************* Forward Declarations ***************************/
28
29
Chris Lattner20b1ea02001-09-14 03:47:57 +000030//************************ Internal Functions ******************************/
31
Chris Lattner20b1ea02001-09-14 03:47:57 +000032
Chris Lattner20b1ea02001-09-14 03:47:57 +000033static inline MachineOpCode
34ChooseBprInstruction(const InstructionNode* instrNode)
35{
36 MachineOpCode opCode;
37
38 Instruction* setCCInstr =
39 ((InstructionNode*) instrNode->leftChild())->getInstruction();
40
41 switch(setCCInstr->getOpcode())
42 {
43 case Instruction::SetEQ: opCode = BRZ; break;
44 case Instruction::SetNE: opCode = BRNZ; break;
45 case Instruction::SetLE: opCode = BRLEZ; break;
46 case Instruction::SetGE: opCode = BRGEZ; break;
47 case Instruction::SetLT: opCode = BRLZ; break;
48 case Instruction::SetGT: opCode = BRGZ; break;
49 default:
50 assert(0 && "Unrecognized VM instruction!");
51 opCode = INVALID_OPCODE;
52 break;
53 }
54
55 return opCode;
56}
57
58
59static inline MachineOpCode
Chris Lattner20b1ea02001-09-14 03:47:57 +000060ChooseBpccInstruction(const InstructionNode* instrNode,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +000061 const BinaryOperator* setCCInstr)
Chris Lattner20b1ea02001-09-14 03:47:57 +000062{
63 MachineOpCode opCode = INVALID_OPCODE;
64
65 bool isSigned = setCCInstr->getOperand(0)->getType()->isSigned();
66
67 if (isSigned)
68 {
69 switch(setCCInstr->getOpcode())
Vikram S. Adve4cecdd22001-10-01 00:12:53 +000070 {
71 case Instruction::SetEQ: opCode = BE; break;
72 case Instruction::SetNE: opCode = BNE; break;
73 case Instruction::SetLE: opCode = BLE; break;
74 case Instruction::SetGE: opCode = BGE; break;
75 case Instruction::SetLT: opCode = BL; break;
76 case Instruction::SetGT: opCode = BG; break;
77 default:
78 assert(0 && "Unrecognized VM instruction!");
79 break;
80 }
Chris Lattner20b1ea02001-09-14 03:47:57 +000081 }
82 else
83 {
84 switch(setCCInstr->getOpcode())
Vikram S. Adve4cecdd22001-10-01 00:12:53 +000085 {
86 case Instruction::SetEQ: opCode = BE; break;
87 case Instruction::SetNE: opCode = BNE; break;
88 case Instruction::SetLE: opCode = BLEU; break;
89 case Instruction::SetGE: opCode = BCC; break;
90 case Instruction::SetLT: opCode = BCS; break;
91 case Instruction::SetGT: opCode = BGU; break;
92 default:
93 assert(0 && "Unrecognized VM instruction!");
94 break;
95 }
Chris Lattner20b1ea02001-09-14 03:47:57 +000096 }
97
98 return opCode;
99}
100
101static inline MachineOpCode
102ChooseBFpccInstruction(const InstructionNode* instrNode,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000103 const BinaryOperator* setCCInstr)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000104{
105 MachineOpCode opCode = INVALID_OPCODE;
106
107 switch(setCCInstr->getOpcode())
108 {
109 case Instruction::SetEQ: opCode = FBE; break;
110 case Instruction::SetNE: opCode = FBNE; break;
111 case Instruction::SetLE: opCode = FBLE; break;
112 case Instruction::SetGE: opCode = FBGE; break;
113 case Instruction::SetLT: opCode = FBL; break;
114 case Instruction::SetGT: opCode = FBG; break;
115 default:
116 assert(0 && "Unrecognized VM instruction!");
117 break;
118 }
119
120 return opCode;
121}
122
123
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000124// Create a unique TmpInstruction for a boolean value,
125// representing the CC register used by a branch on that value.
126// For now, hack this using a little static cache of TmpInstructions.
127// Eventually the entire BURG instruction selection should be put
128// into a separate class that can hold such information.
Vikram S. Adveff5a09e2001-11-08 05:04:09 +0000129// The static cache is not too bad because the memory for these
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000130// TmpInstructions will be freed along with the rest of the Function anyway.
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000131//
132static TmpInstruction*
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000133GetTmpForCC(Value* boolVal, const Function *F, const Type* ccType)
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000134{
Chris Lattner09ff1122002-07-24 21:21:32 +0000135 typedef hash_map<const Value*, TmpInstruction*> BoolTmpCache;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000136 static BoolTmpCache boolToTmpCache; // Map boolVal -> TmpInstruction*
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000137 static const Function *lastFunction = 0;// Use to flush cache between funcs
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000138
139 assert(boolVal->getType() == Type::BoolTy && "Weird but ok! Delete assert");
140
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000141 if (lastFunction != F)
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000142 {
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000143 lastFunction = F;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000144 boolToTmpCache.clear();
145 }
146
Vikram S. Adveff5a09e2001-11-08 05:04:09 +0000147 // Look for tmpI and create a new one otherwise. The new value is
148 // directly written to map using the ref returned by operator[].
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000149 TmpInstruction*& tmpI = boolToTmpCache[boolVal];
150 if (tmpI == NULL)
Chris Lattner9c461082002-02-03 07:50:56 +0000151 tmpI = new TmpInstruction(ccType, boolVal);
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000152
153 return tmpI;
154}
155
156
Chris Lattner20b1ea02001-09-14 03:47:57 +0000157static inline MachineOpCode
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000158ChooseBccInstruction(const InstructionNode* instrNode,
159 bool& isFPBranch)
160{
161 InstructionNode* setCCNode = (InstructionNode*) instrNode->leftChild();
Vikram S. Adve30a6f492002-08-22 02:56:10 +0000162 assert(setCCNode->getOpLabel() == SetCCOp);
163 BinaryOperator* setCCInstr =cast<BinaryOperator>(setCCNode->getInstruction());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000164 const Type* setCCType = setCCInstr->getOperand(0)->getType();
165
Vikram S. Adve242a8082002-05-19 15:25:51 +0000166 isFPBranch = setCCType->isFloatingPoint(); // Return value: don't delete!
167
168 if (isFPBranch)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000169 return ChooseBFpccInstruction(instrNode, setCCInstr);
170 else
171 return ChooseBpccInstruction(instrNode, setCCInstr);
172}
173
174
175static inline MachineOpCode
Chris Lattner20b1ea02001-09-14 03:47:57 +0000176ChooseMovFpccInstruction(const InstructionNode* instrNode)
177{
178 MachineOpCode opCode = INVALID_OPCODE;
179
180 switch(instrNode->getInstruction()->getOpcode())
181 {
182 case Instruction::SetEQ: opCode = MOVFE; break;
183 case Instruction::SetNE: opCode = MOVFNE; break;
184 case Instruction::SetLE: opCode = MOVFLE; break;
185 case Instruction::SetGE: opCode = MOVFGE; break;
186 case Instruction::SetLT: opCode = MOVFL; break;
187 case Instruction::SetGT: opCode = MOVFG; break;
188 default:
189 assert(0 && "Unrecognized VM instruction!");
190 break;
191 }
192
193 return opCode;
194}
195
196
197// Assumes that SUBcc v1, v2 -> v3 has been executed.
198// In most cases, we want to clear v3 and then follow it by instruction
199// MOVcc 1 -> v3.
200// Set mustClearReg=false if v3 need not be cleared before conditional move.
201// Set valueToMove=0 if we want to conditionally move 0 instead of 1
202// (i.e., we want to test inverse of a condition)
Vikram S. Adve243dd452001-09-18 13:03:13 +0000203// (The latter two cases do not seem to arise because SetNE needs nothing.)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000204//
205static MachineOpCode
206ChooseMovpccAfterSub(const InstructionNode* instrNode,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000207 bool& mustClearReg,
208 int& valueToMove)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000209{
210 MachineOpCode opCode = INVALID_OPCODE;
211 mustClearReg = true;
212 valueToMove = 1;
213
214 switch(instrNode->getInstruction()->getOpcode())
215 {
Vikram S. Adve243dd452001-09-18 13:03:13 +0000216 case Instruction::SetEQ: opCode = MOVE; break;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000217 case Instruction::SetLE: opCode = MOVLE; break;
218 case Instruction::SetGE: opCode = MOVGE; break;
219 case Instruction::SetLT: opCode = MOVL; break;
220 case Instruction::SetGT: opCode = MOVG; break;
Vikram S. Adve243dd452001-09-18 13:03:13 +0000221 case Instruction::SetNE: assert(0 && "No move required!"); break;
222 default: assert(0 && "Unrecognized VM instr!"); break;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000223 }
224
225 return opCode;
226}
227
Chris Lattner20b1ea02001-09-14 03:47:57 +0000228static inline MachineOpCode
Vikram S. Advedbc4fad2002-04-25 04:37:51 +0000229ChooseConvertToFloatInstr(OpLabel vopCode, const Type* opType)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000230{
231 MachineOpCode opCode = INVALID_OPCODE;
232
Vikram S. Advedbc4fad2002-04-25 04:37:51 +0000233 switch(vopCode)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000234 {
235 case ToFloatTy:
236 if (opType == Type::SByteTy || opType == Type::ShortTy || opType == Type::IntTy)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000237 opCode = FITOS;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000238 else if (opType == Type::LongTy)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000239 opCode = FXTOS;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000240 else if (opType == Type::DoubleTy)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000241 opCode = FDTOS;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000242 else if (opType == Type::FloatTy)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000243 ;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000244 else
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000245 assert(0 && "Cannot convert this type to FLOAT on SPARC");
Chris Lattner20b1ea02001-09-14 03:47:57 +0000246 break;
247
248 case ToDoubleTy:
Vikram S. Adve74825322002-03-18 03:15:35 +0000249 // This is usually used in conjunction with CreateCodeToCopyIntToFloat().
250 // Both functions should treat the integer as a 32-bit value for types
251 // of 4 bytes or less, and as a 64-bit value otherwise.
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000252 if (opType == Type::SByteTy || opType == Type::UByteTy ||
253 opType == Type::ShortTy || opType == Type::UShortTy ||
254 opType == Type::IntTy || opType == Type::UIntTy)
Vikram S. Adve74825322002-03-18 03:15:35 +0000255 opCode = FITOD;
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000256 else if (opType == Type::LongTy || opType == Type::ULongTy)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000257 opCode = FXTOD;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000258 else if (opType == Type::FloatTy)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000259 opCode = FSTOD;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000260 else if (opType == Type::DoubleTy)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000261 ;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000262 else
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000263 assert(0 && "Cannot convert this type to DOUBLE on SPARC");
Chris Lattner20b1ea02001-09-14 03:47:57 +0000264 break;
265
266 default:
267 break;
268 }
269
270 return opCode;
271}
272
273static inline MachineOpCode
Vikram S. Adve1e606692002-07-31 21:01:34 +0000274ChooseConvertToIntInstr(Type::PrimitiveID tid, const Type* opType)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000275{
276 MachineOpCode opCode = INVALID_OPCODE;;
277
Vikram S. Adve1e606692002-07-31 21:01:34 +0000278 if (tid==Type::SByteTyID || tid==Type::ShortTyID || tid==Type::IntTyID ||
279 tid==Type::UByteTyID || tid==Type::UShortTyID || tid==Type::UIntTyID)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000280 {
281 switch (opType->getPrimitiveID())
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000282 {
Chris Lattner20b1ea02001-09-14 03:47:57 +0000283 case Type::FloatTyID: opCode = FSTOI; break;
284 case Type::DoubleTyID: opCode = FDTOI; break;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000285 default:
286 assert(0 && "Non-numeric non-bool type cannot be converted to Int");
287 break;
288 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000289 }
Vikram S. Adve1e606692002-07-31 21:01:34 +0000290 else if (tid==Type::LongTyID || tid==Type::ULongTyID)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000291 {
292 switch (opType->getPrimitiveID())
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000293 {
Chris Lattner20b1ea02001-09-14 03:47:57 +0000294 case Type::FloatTyID: opCode = FSTOX; break;
295 case Type::DoubleTyID: opCode = FDTOX; break;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000296 default:
297 assert(0 && "Non-numeric non-bool type cannot be converted to Long");
298 break;
299 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000300 }
301 else
302 assert(0 && "Should not get here, Mo!");
303
304 return opCode;
305}
306
Vikram S. Advedbc4fad2002-04-25 04:37:51 +0000307MachineInstr*
Vikram S. Adve1e606692002-07-31 21:01:34 +0000308CreateConvertToIntInstr(Type::PrimitiveID destTID, Value* srcVal,Value* destVal)
Vikram S. Advedbc4fad2002-04-25 04:37:51 +0000309{
Vikram S. Adve1e606692002-07-31 21:01:34 +0000310 MachineOpCode opCode = ChooseConvertToIntInstr(destTID, srcVal->getType());
Vikram S. Advedbc4fad2002-04-25 04:37:51 +0000311 assert(opCode != INVALID_OPCODE && "Expected to need conversion!");
312
313 MachineInstr* M = new MachineInstr(opCode);
314 M->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister, srcVal);
315 M->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister, destVal);
316 return M;
317}
Chris Lattner20b1ea02001-09-14 03:47:57 +0000318
Vikram S. Adve8cfffd32002-08-24 20:56:53 +0000319// CreateCodeToConvertFloatToInt: Convert FP value to signed or unsigned integer
Vikram S. Adve1e606692002-07-31 21:01:34 +0000320// The FP value must be converted to the dest type in an FP register,
321// and the result is then copied from FP to int register via memory.
322static void
Vikram S. Adve8cfffd32002-08-24 20:56:53 +0000323CreateCodeToConvertFloatToInt(const TargetMachine& target,
324 Value* opVal,
325 Instruction* destI,
326 std::vector<MachineInstr*>& mvec,
327 MachineCodeForInstruction& mcfi)
Vikram S. Adve1e606692002-07-31 21:01:34 +0000328{
329 // Create a temporary to represent the FP register into which the
330 // int value will placed after conversion. The type of this temporary
331 // depends on the type of FP register to use: single-prec for a 32-bit
332 // int or smaller; double-prec for a 64-bit int.
333 //
334 const Type* destTypeToUse = (destI->getType() == Type::LongTy)? Type::DoubleTy
335 : Type::FloatTy;
336 Value* destForCast = new TmpInstruction(destTypeToUse, opVal);
337 mcfi.addTemp(destForCast);
338
339 // Create the fp-to-int conversion code
340 MachineInstr* M = CreateConvertToIntInstr(destI->getType()->getPrimitiveID(),
341 opVal, destForCast);
342 mvec.push_back(M);
343
344 // Create the fpreg-to-intreg copy code
345 target.getInstrInfo().
346 CreateCodeToCopyFloatToInt(target, destI->getParent()->getParent(),
347 (TmpInstruction*)destForCast, destI, mvec, mcfi);
348}
349
350
Chris Lattner20b1ea02001-09-14 03:47:57 +0000351static inline MachineOpCode
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000352ChooseAddInstruction(const InstructionNode* instrNode)
353{
354 return ChooseAddInstructionByType(instrNode->getInstruction()->getType());
355}
356
357
Chris Lattner20b1ea02001-09-14 03:47:57 +0000358static inline MachineInstr*
359CreateMovFloatInstruction(const InstructionNode* instrNode,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000360 const Type* resultType)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000361{
362 MachineInstr* minstr = new MachineInstr((resultType == Type::FloatTy)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000363 ? FMOVS : FMOVD);
Vikram S. Adve74825322002-03-18 03:15:35 +0000364 minstr->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister,
365 instrNode->leftChild()->getValue());
366 minstr->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister,
367 instrNode->getValue());
Chris Lattner20b1ea02001-09-14 03:47:57 +0000368 return minstr;
369}
370
371static inline MachineInstr*
372CreateAddConstInstruction(const InstructionNode* instrNode)
373{
374 MachineInstr* minstr = NULL;
375
376 Value* constOp = ((InstrTreeNode*) instrNode->rightChild())->getValue();
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000377 assert(isa<Constant>(constOp));
Chris Lattner20b1ea02001-09-14 03:47:57 +0000378
379 // Cases worth optimizing are:
380 // (1) Add with 0 for float or double: use an FMOV of appropriate type,
381 // instead of an FADD (1 vs 3 cycles). There is no integer MOV.
382 //
Chris Lattner9b625032002-05-06 16:15:30 +0000383 if (ConstantFP *FPC = dyn_cast<ConstantFP>(constOp)) {
384 double dval = FPC->getValue();
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000385 if (dval == 0.0)
Vikram S. Adve242a8082002-05-19 15:25:51 +0000386 minstr = CreateMovFloatInstruction(instrNode,
387 instrNode->getInstruction()->getType());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000388 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000389
390 return minstr;
391}
392
393
394static inline MachineOpCode
Vikram S. Adve510eec72001-11-04 21:59:14 +0000395ChooseSubInstructionByType(const Type* resultType)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000396{
397 MachineOpCode opCode = INVALID_OPCODE;
398
Chris Lattner0c4e8862002-09-03 01:08:28 +0000399 if (resultType->isInteger() || isa<PointerType>(resultType))
Chris Lattner20b1ea02001-09-14 03:47:57 +0000400 {
401 opCode = SUB;
402 }
403 else
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000404 switch(resultType->getPrimitiveID())
405 {
406 case Type::FloatTyID: opCode = FSUBS; break;
407 case Type::DoubleTyID: opCode = FSUBD; break;
408 default: assert(0 && "Invalid type for SUB instruction"); break;
409 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000410
411 return opCode;
412}
413
414
415static inline MachineInstr*
416CreateSubConstInstruction(const InstructionNode* instrNode)
417{
418 MachineInstr* minstr = NULL;
419
420 Value* constOp = ((InstrTreeNode*) instrNode->rightChild())->getValue();
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000421 assert(isa<Constant>(constOp));
Chris Lattner20b1ea02001-09-14 03:47:57 +0000422
423 // Cases worth optimizing are:
424 // (1) Sub with 0 for float or double: use an FMOV of appropriate type,
425 // instead of an FSUB (1 vs 3 cycles). There is no integer MOV.
426 //
Chris Lattner9b625032002-05-06 16:15:30 +0000427 if (ConstantFP *FPC = dyn_cast<ConstantFP>(constOp)) {
428 double dval = FPC->getValue();
429 if (dval == 0.0)
Vikram S. Adve242a8082002-05-19 15:25:51 +0000430 minstr = CreateMovFloatInstruction(instrNode,
431 instrNode->getInstruction()->getType());
Chris Lattner9b625032002-05-06 16:15:30 +0000432 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000433
434 return minstr;
435}
436
437
438static inline MachineOpCode
439ChooseFcmpInstruction(const InstructionNode* instrNode)
440{
441 MachineOpCode opCode = INVALID_OPCODE;
442
443 Value* operand = ((InstrTreeNode*) instrNode->leftChild())->getValue();
444 switch(operand->getType()->getPrimitiveID()) {
445 case Type::FloatTyID: opCode = FCMPS; break;
446 case Type::DoubleTyID: opCode = FCMPD; break;
447 default: assert(0 && "Invalid type for FCMP instruction"); break;
448 }
449
450 return opCode;
451}
452
453
454// Assumes that leftArg and rightArg are both cast instructions.
455//
456static inline bool
457BothFloatToDouble(const InstructionNode* instrNode)
458{
459 InstrTreeNode* leftArg = instrNode->leftChild();
460 InstrTreeNode* rightArg = instrNode->rightChild();
461 InstrTreeNode* leftArgArg = leftArg->leftChild();
462 InstrTreeNode* rightArgArg = rightArg->leftChild();
463 assert(leftArg->getValue()->getType() == rightArg->getValue()->getType());
464
465 // Check if both arguments are floats cast to double
466 return (leftArg->getValue()->getType() == Type::DoubleTy &&
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000467 leftArgArg->getValue()->getType() == Type::FloatTy &&
468 rightArgArg->getValue()->getType() == Type::FloatTy);
Chris Lattner20b1ea02001-09-14 03:47:57 +0000469}
470
471
472static inline MachineOpCode
Vikram S. Adve510eec72001-11-04 21:59:14 +0000473ChooseMulInstructionByType(const Type* resultType)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000474{
475 MachineOpCode opCode = INVALID_OPCODE;
476
Chris Lattner0c4e8862002-09-03 01:08:28 +0000477 if (resultType->isInteger())
Vikram S. Adve510eec72001-11-04 21:59:14 +0000478 opCode = MULX;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000479 else
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000480 switch(resultType->getPrimitiveID())
481 {
482 case Type::FloatTyID: opCode = FMULS; break;
483 case Type::DoubleTyID: opCode = FMULD; break;
484 default: assert(0 && "Invalid type for MUL instruction"); break;
485 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000486
487 return opCode;
488}
489
490
Vikram S. Adve510eec72001-11-04 21:59:14 +0000491
Chris Lattner20b1ea02001-09-14 03:47:57 +0000492static inline MachineInstr*
Vikram S. Adve74825322002-03-18 03:15:35 +0000493CreateIntNegInstruction(const TargetMachine& target,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000494 Value* vreg)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000495{
496 MachineInstr* minstr = new MachineInstr(SUB);
Vikram S. Adve74825322002-03-18 03:15:35 +0000497 minstr->SetMachineOperandReg(0, target.getRegInfo().getZeroRegNum());
498 minstr->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister, vreg);
499 minstr->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister, vreg);
Chris Lattner20b1ea02001-09-14 03:47:57 +0000500 return minstr;
501}
502
503
Vikram S. Adve242a8082002-05-19 15:25:51 +0000504// Create instruction sequence for any shift operation.
505// SLL or SLLX on an operand smaller than the integer reg. size (64bits)
506// requires a second instruction for explicit sign-extension.
507// Note that we only have to worry about a sign-bit appearing in the
508// most significant bit of the operand after shifting (e.g., bit 32 of
509// Int or bit 16 of Short), so we do not have to worry about results
510// that are as large as a normal integer register.
511//
512static inline void
513CreateShiftInstructions(const TargetMachine& target,
514 Function* F,
515 MachineOpCode shiftOpCode,
516 Value* argVal1,
517 Value* optArgVal2, /* Use optArgVal2 if not NULL */
518 unsigned int optShiftNum, /* else use optShiftNum */
519 Instruction* destVal,
520 vector<MachineInstr*>& mvec,
521 MachineCodeForInstruction& mcfi)
522{
523 assert((optArgVal2 != NULL || optShiftNum <= 64) &&
524 "Large shift sizes unexpected, but can be handled below: "
525 "You need to check whether or not it fits in immed field below");
526
527 // If this is a logical left shift of a type smaller than the standard
528 // integer reg. size, we have to extend the sign-bit into upper bits
529 // of dest, so we need to put the result of the SLL into a temporary.
530 //
531 Value* shiftDest = destVal;
532 const Type* opType = argVal1->getType();
533 unsigned opSize = target.DataLayout.getTypeSize(argVal1->getType());
534 if ((shiftOpCode == SLL || shiftOpCode == SLLX)
535 && opSize < target.DataLayout.getIntegerRegize())
536 { // put SLL result into a temporary
537 shiftDest = new TmpInstruction(argVal1, optArgVal2, "sllTmp");
538 mcfi.addTemp(shiftDest);
539 }
540
541 MachineInstr* M = (optArgVal2 != NULL)
542 ? Create3OperandInstr(shiftOpCode, argVal1, optArgVal2, shiftDest)
543 : Create3OperandInstr_UImmed(shiftOpCode, argVal1, optShiftNum, shiftDest);
544 mvec.push_back(M);
545
546 if (shiftDest != destVal)
547 { // extend the sign-bit of the result into all upper bits of dest
548 assert(8*opSize <= 32 && "Unexpected type size > 4 and < IntRegSize?");
549 target.getInstrInfo().
550 CreateSignExtensionInstructions(target, F, shiftDest, 8*opSize,
551 destVal, mvec, mcfi);
552 }
553}
554
555
Vikram S. Adve74825322002-03-18 03:15:35 +0000556// Does not create any instructions if we cannot exploit constant to
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000557// create a cheaper instruction.
558// This returns the approximate cost of the instructions generated,
559// which is used to pick the cheapest when both operands are constant.
560static inline unsigned int
Vikram S. Adve242a8082002-05-19 15:25:51 +0000561CreateMulConstInstruction(const TargetMachine &target, Function* F,
562 Value* lval, Value* rval, Instruction* destVal,
563 vector<MachineInstr*>& mvec,
564 MachineCodeForInstruction& mcfi)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000565{
Vikram S. Adve242a8082002-05-19 15:25:51 +0000566 /* Use max. multiply cost, viz., cost of MULX */
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000567 unsigned int cost = target.getInstrInfo().minLatency(MULX);
Vikram S. Adve242a8082002-05-19 15:25:51 +0000568 unsigned int firstNewInstr = mvec.size();
Vikram S. Adve74825322002-03-18 03:15:35 +0000569
570 Value* constOp = rval;
571 if (! isa<Constant>(constOp))
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000572 return cost;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000573
574 // Cases worth optimizing are:
575 // (1) Multiply by 0 or 1 for any type: replace with copy (ADD or FMOV)
576 // (2) Multiply by 2^x for integer types: replace with Shift
577 //
Vikram S. Adve74825322002-03-18 03:15:35 +0000578 const Type* resultType = destVal->getType();
Chris Lattner20b1ea02001-09-14 03:47:57 +0000579
Chris Lattner0c4e8862002-09-03 01:08:28 +0000580 if (resultType->isInteger() || isa<PointerType>(resultType))
Chris Lattner20b1ea02001-09-14 03:47:57 +0000581 {
Chris Lattner20b1ea02001-09-14 03:47:57 +0000582 bool isValidConst;
583 int64_t C = GetConstantValueAsSignedInt(constOp, isValidConst);
584 if (isValidConst)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000585 {
Vikram S. Adve242a8082002-05-19 15:25:51 +0000586 unsigned pow;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000587 bool needNeg = false;
588 if (C < 0)
589 {
590 needNeg = true;
591 C = -C;
592 }
593
594 if (C == 0 || C == 1)
595 {
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000596 cost = target.getInstrInfo().minLatency(ADD);
Vikram S. Adve242a8082002-05-19 15:25:51 +0000597 MachineInstr* M = (C == 0)
598 ? Create3OperandInstr_Reg(ADD,
599 target.getRegInfo().getZeroRegNum(),
600 target.getRegInfo().getZeroRegNum(),
601 destVal)
602 : Create3OperandInstr_Reg(ADD, lval,
603 target.getRegInfo().getZeroRegNum(),
604 destVal);
605 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000606 }
Chris Lattner36346c72002-05-19 21:20:19 +0000607 else if (isPowerOf2(C, pow))
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000608 {
Vikram S. Adve242a8082002-05-19 15:25:51 +0000609 unsigned int opSize = target.DataLayout.getTypeSize(resultType);
610 MachineOpCode opCode = (opSize <= 32)? SLL : SLLX;
611 CreateShiftInstructions(target, F, opCode, lval, NULL, pow,
612 destVal, mvec, mcfi);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000613 }
614
Vikram S. Adve242a8082002-05-19 15:25:51 +0000615 if (mvec.size() > 0 && needNeg)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000616 { // insert <reg = SUB 0, reg> after the instr to flip the sign
Vikram S. Adve242a8082002-05-19 15:25:51 +0000617 MachineInstr* M = CreateIntNegInstruction(target, destVal);
618 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000619 }
620 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000621 }
622 else
623 {
Chris Lattner9b625032002-05-06 16:15:30 +0000624 if (ConstantFP *FPC = dyn_cast<ConstantFP>(constOp))
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000625 {
Chris Lattner9b625032002-05-06 16:15:30 +0000626 double dval = FPC->getValue();
Vikram S. Adve6ad7c552001-11-09 02:18:16 +0000627 if (fabs(dval) == 1)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000628 {
Vikram S. Adve242a8082002-05-19 15:25:51 +0000629 MachineOpCode opCode = (dval < 0)
Vikram S. Adve6ad7c552001-11-09 02:18:16 +0000630 ? (resultType == Type::FloatTy? FNEGS : FNEGD)
631 : (resultType == Type::FloatTy? FMOVS : FMOVD);
Vikram S. Adve242a8082002-05-19 15:25:51 +0000632 MachineInstr* M = Create2OperandInstr(opCode, lval, destVal);
633 mvec.push_back(M);
Vikram S. Adve6ad7c552001-11-09 02:18:16 +0000634 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000635 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000636 }
637
Vikram S. Adve242a8082002-05-19 15:25:51 +0000638 if (firstNewInstr < mvec.size())
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000639 {
Vikram S. Adve242a8082002-05-19 15:25:51 +0000640 cost = 0;
641 for (unsigned int i=firstNewInstr; i < mvec.size(); ++i)
642 cost += target.getInstrInfo().minLatency(mvec[i]->getOpCode());
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000643 }
644
645 return cost;
Vikram S. Adve74825322002-03-18 03:15:35 +0000646}
647
648
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000649// Does not create any instructions if we cannot exploit constant to
650// create a cheaper instruction.
651//
652static inline void
653CreateCheapestMulConstInstruction(const TargetMachine &target,
Vikram S. Adve242a8082002-05-19 15:25:51 +0000654 Function* F,
655 Value* lval, Value* rval,
656 Instruction* destVal,
657 vector<MachineInstr*>& mvec,
658 MachineCodeForInstruction& mcfi)
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000659{
660 Value* constOp;
661 if (isa<Constant>(lval) && isa<Constant>(rval))
662 { // both operands are constant: try both orders!
663 vector<MachineInstr*> mvec1, mvec2;
Vikram S. Adve242a8082002-05-19 15:25:51 +0000664 unsigned int lcost = CreateMulConstInstruction(target, F, lval, rval,
665 destVal, mvec1, mcfi);
666 unsigned int rcost = CreateMulConstInstruction(target, F, rval, lval,
667 destVal, mvec2, mcfi);
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000668 vector<MachineInstr*>& mincostMvec = (lcost <= rcost)? mvec1 : mvec2;
669 vector<MachineInstr*>& maxcostMvec = (lcost <= rcost)? mvec2 : mvec1;
670 mvec.insert(mvec.end(), mincostMvec.begin(), mincostMvec.end());
671
672 for (unsigned int i=0; i < maxcostMvec.size(); ++i)
673 delete maxcostMvec[i];
674 }
675 else if (isa<Constant>(rval)) // rval is constant, but not lval
Vikram S. Adve242a8082002-05-19 15:25:51 +0000676 CreateMulConstInstruction(target, F, lval, rval, destVal, mvec, mcfi);
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000677 else if (isa<Constant>(lval)) // lval is constant, but not rval
Vikram S. Adve242a8082002-05-19 15:25:51 +0000678 CreateMulConstInstruction(target, F, lval, rval, destVal, mvec, mcfi);
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000679
680 // else neither is constant
681 return;
682}
683
Vikram S. Adve74825322002-03-18 03:15:35 +0000684// Return NULL if we cannot exploit constant to create a cheaper instruction
685static inline void
Vikram S. Adve242a8082002-05-19 15:25:51 +0000686CreateMulInstruction(const TargetMachine &target, Function* F,
687 Value* lval, Value* rval, Instruction* destVal,
Vikram S. Adve74825322002-03-18 03:15:35 +0000688 vector<MachineInstr*>& mvec,
Vikram S. Adve242a8082002-05-19 15:25:51 +0000689 MachineCodeForInstruction& mcfi,
Vikram S. Adve74825322002-03-18 03:15:35 +0000690 MachineOpCode forceMulOp = INVALID_MACHINE_OPCODE)
691{
692 unsigned int L = mvec.size();
Vikram S. Adve242a8082002-05-19 15:25:51 +0000693 CreateCheapestMulConstInstruction(target,F, lval, rval, destVal, mvec, mcfi);
Vikram S. Adve74825322002-03-18 03:15:35 +0000694 if (mvec.size() == L)
695 { // no instructions were added so create MUL reg, reg, reg.
696 // Use FSMULD if both operands are actually floats cast to doubles.
697 // Otherwise, use the default opcode for the appropriate type.
698 MachineOpCode mulOp = ((forceMulOp != INVALID_MACHINE_OPCODE)
699 ? forceMulOp
700 : ChooseMulInstructionByType(destVal->getType()));
701 MachineInstr* M = new MachineInstr(mulOp);
702 M->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister, lval);
703 M->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister, rval);
704 M->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister, destVal);
705 mvec.push_back(M);
706 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000707}
708
709
Vikram S. Adve510eec72001-11-04 21:59:14 +0000710// Generate a divide instruction for Div or Rem.
711// For Rem, this assumes that the operand type will be signed if the result
712// type is signed. This is correct because they must have the same sign.
713//
Chris Lattner20b1ea02001-09-14 03:47:57 +0000714static inline MachineOpCode
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000715ChooseDivInstruction(TargetMachine &target,
716 const InstructionNode* instrNode)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000717{
718 MachineOpCode opCode = INVALID_OPCODE;
719
720 const Type* resultType = instrNode->getInstruction()->getType();
721
Chris Lattner0c4e8862002-09-03 01:08:28 +0000722 if (resultType->isInteger())
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000723 opCode = resultType->isSigned()? SDIVX : UDIVX;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000724 else
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000725 switch(resultType->getPrimitiveID())
726 {
727 case Type::FloatTyID: opCode = FDIVS; break;
728 case Type::DoubleTyID: opCode = FDIVD; break;
729 default: assert(0 && "Invalid type for DIV instruction"); break;
730 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000731
732 return opCode;
733}
734
735
Vikram S. Adve74825322002-03-18 03:15:35 +0000736// Return NULL if we cannot exploit constant to create a cheaper instruction
737static inline void
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000738CreateDivConstInstruction(TargetMachine &target,
739 const InstructionNode* instrNode,
Vikram S. Adve74825322002-03-18 03:15:35 +0000740 vector<MachineInstr*>& mvec)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000741{
Vikram S. Adve74825322002-03-18 03:15:35 +0000742 MachineInstr* minstr1 = NULL;
743 MachineInstr* minstr2 = NULL;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000744
745 Value* constOp = ((InstrTreeNode*) instrNode->rightChild())->getValue();
Vikram S. Adve74825322002-03-18 03:15:35 +0000746 if (! isa<Constant>(constOp))
747 return;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000748
749 // Cases worth optimizing are:
750 // (1) Divide by 1 for any type: replace with copy (ADD or FMOV)
751 // (2) Divide by 2^x for integer types: replace with SR[L or A]{X}
752 //
753 const Type* resultType = instrNode->getInstruction()->getType();
754
Chris Lattner0c4e8862002-09-03 01:08:28 +0000755 if (resultType->isInteger())
Chris Lattner20b1ea02001-09-14 03:47:57 +0000756 {
757 unsigned pow;
758 bool isValidConst;
759 int64_t C = GetConstantValueAsSignedInt(constOp, isValidConst);
760 if (isValidConst)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000761 {
762 bool needNeg = false;
763 if (C < 0)
764 {
765 needNeg = true;
766 C = -C;
767 }
768
769 if (C == 1)
770 {
Vikram S. Adve74825322002-03-18 03:15:35 +0000771 minstr1 = new MachineInstr(ADD);
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000772 minstr1->SetMachineOperandVal(0,
773 MachineOperand::MO_VirtualRegister,
774 instrNode->leftChild()->getValue());
775 minstr1->SetMachineOperandReg(1,
776 target.getRegInfo().getZeroRegNum());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000777 }
Chris Lattner36346c72002-05-19 21:20:19 +0000778 else if (isPowerOf2(C, pow))
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000779 {
780 MachineOpCode opCode= ((resultType->isSigned())
781 ? (resultType==Type::LongTy)? SRAX : SRA
782 : (resultType==Type::LongTy)? SRLX : SRL);
Vikram S. Adve74825322002-03-18 03:15:35 +0000783 minstr1 = new MachineInstr(opCode);
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000784 minstr1->SetMachineOperandVal(0,
785 MachineOperand::MO_VirtualRegister,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000786 instrNode->leftChild()->getValue());
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000787 minstr1->SetMachineOperandConst(1,
788 MachineOperand::MO_UnextendedImmed,
789 pow);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000790 }
791
Vikram S. Adve74825322002-03-18 03:15:35 +0000792 if (minstr1 && needNeg)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000793 { // insert <reg = SUB 0, reg> after the instr to flip the sign
Vikram S. Adve74825322002-03-18 03:15:35 +0000794 minstr2 = CreateIntNegInstruction(target,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000795 instrNode->getValue());
796 }
797 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000798 }
799 else
800 {
Chris Lattner9b625032002-05-06 16:15:30 +0000801 if (ConstantFP *FPC = dyn_cast<ConstantFP>(constOp))
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000802 {
Chris Lattner9b625032002-05-06 16:15:30 +0000803 double dval = FPC->getValue();
Vikram S. Adve6ad7c552001-11-09 02:18:16 +0000804 if (fabs(dval) == 1)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000805 {
806 bool needNeg = (dval < 0);
807
808 MachineOpCode opCode = needNeg
809 ? (resultType == Type::FloatTy? FNEGS : FNEGD)
810 : (resultType == Type::FloatTy? FMOVS : FMOVD);
811
Vikram S. Adve74825322002-03-18 03:15:35 +0000812 minstr1 = new MachineInstr(opCode);
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000813 minstr1->SetMachineOperandVal(0,
814 MachineOperand::MO_VirtualRegister,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000815 instrNode->leftChild()->getValue());
816 }
817 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000818 }
819
Vikram S. Adve74825322002-03-18 03:15:35 +0000820 if (minstr1 != NULL)
821 minstr1->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister,
822 instrNode->getValue());
Chris Lattner20b1ea02001-09-14 03:47:57 +0000823
Vikram S. Adve74825322002-03-18 03:15:35 +0000824 if (minstr1)
825 mvec.push_back(minstr1);
826 if (minstr2)
827 mvec.push_back(minstr2);
Chris Lattner20b1ea02001-09-14 03:47:57 +0000828}
829
830
Vikram S. Adve74825322002-03-18 03:15:35 +0000831static void
832CreateCodeForVariableSizeAlloca(const TargetMachine& target,
833 Instruction* result,
834 unsigned int tsize,
835 Value* numElementsVal,
836 vector<MachineInstr*>& getMvec)
837{
838 MachineInstr* M;
839
840 // Create a Value to hold the (constant) element size
841 Value* tsizeVal = ConstantSInt::get(Type::IntTy, tsize);
842
843 // Get the constant offset from SP for dynamically allocated storage
844 // and create a temporary Value to hold it.
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000845 assert(result && result->getParent() && "Result value is not part of a fn?");
846 Function *F = result->getParent()->getParent();
847 MachineCodeForMethod& mcInfo = MachineCodeForMethod::get(F);
Vikram S. Adve74825322002-03-18 03:15:35 +0000848 bool growUp;
849 ConstantSInt* dynamicAreaOffset =
850 ConstantSInt::get(Type::IntTy,
851 target.getFrameInfo().getDynamicAreaOffset(mcInfo,growUp));
852 assert(! growUp && "Has SPARC v9 stack frame convention changed?");
853
854 // Create a temporary value to hold the result of MUL
855 TmpInstruction* tmpProd = new TmpInstruction(numElementsVal, tsizeVal);
856 MachineCodeForInstruction::get(result).addTemp(tmpProd);
857
858 // Instruction 1: mul numElements, typeSize -> tmpProd
859 M = new MachineInstr(MULX);
860 M->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister, numElementsVal);
861 M->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister, tsizeVal);
862 M->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister, tmpProd);
863 getMvec.push_back(M);
864
865 // Instruction 2: sub %sp, tmpProd -> %sp
866 M = new MachineInstr(SUB);
867 M->SetMachineOperandReg(0, target.getRegInfo().getStackPointer());
868 M->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister, tmpProd);
869 M->SetMachineOperandReg(2, target.getRegInfo().getStackPointer());
870 getMvec.push_back(M);
871
872 // Instruction 3: add %sp, frameSizeBelowDynamicArea -> result
873 M = new MachineInstr(ADD);
874 M->SetMachineOperandReg(0, target.getRegInfo().getStackPointer());
875 M->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister, dynamicAreaOffset);
876 M->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister, result);
877 getMvec.push_back(M);
878}
879
880
881static void
882CreateCodeForFixedSizeAlloca(const TargetMachine& target,
883 Instruction* result,
884 unsigned int tsize,
885 unsigned int numElements,
886 vector<MachineInstr*>& getMvec)
887{
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000888 assert(result && result->getParent() &&
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000889 "Result value is not part of a function?");
890 Function *F = result->getParent()->getParent();
891 MachineCodeForMethod &mcInfo = MachineCodeForMethod::get(F);
Vikram S. Adve74825322002-03-18 03:15:35 +0000892
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000893 // Check if the offset would small enough to use as an immediate in
894 // load/stores (check LDX because all load/stores have the same-size immediate
895 // field). If not, put the variable in the dynamically sized area of the
896 // frame.
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000897 unsigned int paddedSizeIgnored;
Vikram S. Adve74825322002-03-18 03:15:35 +0000898 int offsetFromFP = mcInfo.computeOffsetforLocalVar(target, result,
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000899 paddedSizeIgnored,
Vikram S. Adve74825322002-03-18 03:15:35 +0000900 tsize * numElements);
901 if (! target.getInstrInfo().constantFitsInImmedField(LDX, offsetFromFP))
902 {
903 CreateCodeForVariableSizeAlloca(target, result, tsize,
904 ConstantSInt::get(Type::IntTy,numElements),
905 getMvec);
906 return;
907 }
908
909 // else offset fits in immediate field so go ahead and allocate it.
910 offsetFromFP = mcInfo.allocateLocalVar(target, result, tsize * numElements);
911
912 // Create a temporary Value to hold the constant offset.
913 // This is needed because it may not fit in the immediate field.
914 ConstantSInt* offsetVal = ConstantSInt::get(Type::IntTy, offsetFromFP);
915
916 // Instruction 1: add %fp, offsetFromFP -> result
917 MachineInstr* M = new MachineInstr(ADD);
918 M->SetMachineOperandReg(0, target.getRegInfo().getFramePointer());
919 M->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister, offsetVal);
920 M->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister, result);
921
922 getMvec.push_back(M);
923}
924
925
926
Vikram S. Adveed3fefb2002-08-03 13:48:21 +0000927// Check for a constant (uint) 0.
928inline bool
929IsZero(Value* idx)
930{
931 return (isa<ConstantInt>(idx) && cast<ConstantInt>(idx)->isNullValue());
932}
Vikram S. Adve242a8082002-05-19 15:25:51 +0000933
934
Chris Lattner20b1ea02001-09-14 03:47:57 +0000935//------------------------------------------------------------------------
936// Function SetOperandsForMemInstr
937//
938// Choose addressing mode for the given load or store instruction.
939// Use [reg+reg] if it is an indexed reference, and the index offset is
940// not a constant or if it cannot fit in the offset field.
941// Use [reg+offset] in all other cases.
942//
943// This assumes that all array refs are "lowered" to one of these forms:
944// %x = load (subarray*) ptr, constant ; single constant offset
945// %x = load (subarray*) ptr, offsetVal ; single non-constant offset
946// Generally, this should happen via strength reduction + LICM.
947// Also, strength reduction should take care of using the same register for
948// the loop index variable and an array index, when that is profitable.
949//------------------------------------------------------------------------
950
951static void
Vikram S. Adve74825322002-03-18 03:15:35 +0000952SetOperandsForMemInstr(vector<MachineInstr*>& mvec,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000953 const InstructionNode* vmInstrNode,
954 const TargetMachine& target)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000955{
Vikram S. Adve8cfffd32002-08-24 20:56:53 +0000956 Instruction* memInst = vmInstrNode->getInstruction();
957 vector<MachineInstr*>::iterator mvecI = mvec.end() - 1;
958
959 // Index vector, ptr value, and flag if all indices are const.
Vikram S. Advea10d1a72002-03-31 19:07:35 +0000960 vector<Value*> idxVec;
Vikram S. Adve8cfffd32002-08-24 20:56:53 +0000961 bool allConstantIndices;
962 Value* ptrVal = GetMemInstArgs(vmInstrNode, idxVec, allConstantIndices);
Vikram S. Adveed3fefb2002-08-03 13:48:21 +0000963
Vikram S. Adve8cfffd32002-08-24 20:56:53 +0000964 // Now create the appropriate operands for the machine instruction.
965 // First, initialize so we default to storing the offset in a register.
Chris Lattner8e5c0b42001-11-07 14:01:59 +0000966 int64_t smallConstOffset = 0;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000967 Value* valueForRegOffset = NULL;
Vikram S. Adveed3fefb2002-08-03 13:48:21 +0000968 MachineOperand::MachineOperandType offsetOpType =
969 MachineOperand::MO_VirtualRegister;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000970
Vikram S. Adve74825322002-03-18 03:15:35 +0000971 // Check if there is an index vector and if so, compute the
972 // right offset for structures and for arrays
Chris Lattner20b1ea02001-09-14 03:47:57 +0000973 //
Chris Lattner3bb8ad22002-08-22 23:37:24 +0000974 if (!idxVec.empty())
Chris Lattner20b1ea02001-09-14 03:47:57 +0000975 {
Vikram S. Adve74825322002-03-18 03:15:35 +0000976 const PointerType* ptrType = cast<PointerType>(ptrVal->getType());
Chris Lattner20b1ea02001-09-14 03:47:57 +0000977
Vikram S. Adve242a8082002-05-19 15:25:51 +0000978 // If all indices are constant, compute the combined offset directly.
979 if (allConstantIndices)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000980 {
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000981 // Compute the offset value using the index vector. Create a
982 // virtual reg. for it since it may not fit in the immed field.
Vikram S. Adve242a8082002-05-19 15:25:51 +0000983 uint64_t offset = target.DataLayout.getIndexedOffset(ptrType,idxVec);
984 valueForRegOffset = ConstantSInt::get(Type::LongTy, offset);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000985 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000986 else
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000987 {
Vikram S. Adve242a8082002-05-19 15:25:51 +0000988 // There is at least one non-constant offset. Therefore, this must
Vikram S. Adveed3fefb2002-08-03 13:48:21 +0000989 // be an array ref, and must have been lowered to a single non-zero
990 // offset. (An extra leading zero offset, if any, can be ignored.)
991 // Generate code sequence to compute address from index.
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000992 //
Chris Lattner75ac4e52002-08-03 20:57:38 +0000993 assert(idxVec.size() == 1U + IsZero(idxVec[0])
Vikram S. Adveed3fefb2002-08-03 13:48:21 +0000994 && "Array refs must be lowered before Instruction Selection");
995
Chris Lattner75ac4e52002-08-03 20:57:38 +0000996 Value* idxVal = idxVec[IsZero(idxVec[0])];
Vikram S. Adve8cfffd32002-08-24 20:56:53 +0000997 assert(! isa<Constant>(idxVal) && "Need to sign-extend uint to 64b!");
Vikram S. Adveed3fefb2002-08-03 13:48:21 +0000998
999 vector<MachineInstr*> mulVec;
1000 Instruction* addr = new TmpInstruction(Type::UIntTy, memInst);
1001 MachineCodeForInstruction::get(memInst).addTemp(addr);
1002
1003 // The call to getTypeSize() will fail if size is not constant.
1004 unsigned int eltSize =
1005 target.DataLayout.getTypeSize(ptrType->getElementType());
1006 assert(eltSize > 0 && "Invalid or non-const array element size");
1007 ConstantUInt* eltVal = ConstantUInt::get(Type::UIntTy, eltSize);
1008
1009 // CreateMulInstruction() folds constants intelligently enough.
1010 CreateMulInstruction(target,
1011 memInst->getParent()->getParent(),
1012 idxVal, /* lval, not likely const */
1013 eltVal, /* rval, likely constant */
1014 addr, /* result*/
1015 mulVec,
1016 MachineCodeForInstruction::get(memInst),
1017 INVALID_MACHINE_OPCODE);
1018
Vikram S. Adve8cfffd32002-08-24 20:56:53 +00001019 // Sign-extend the result of MUL from 32 to 64 bits.
1020 target.getInstrInfo().CreateSignExtensionInstructions(target, memInst->getParent()->getParent(), addr, /*srcSizeInBits*/32, addr, mulVec, MachineCodeForInstruction::get(memInst));
1021
Vikram S. Adveed3fefb2002-08-03 13:48:21 +00001022 // Insert mulVec[] before *mvecI in mvec[] and update mvecI
1023 // to point to the same instruction it pointed to before.
1024 assert(mulVec.size() > 0 && "No multiply code created?");
1025 vector<MachineInstr*>::iterator oldMvecI = mvecI;
1026 for (unsigned i=0, N=mulVec.size(); i < N; ++i)
1027 mvecI = mvec.insert(mvecI, mulVec[i]) + 1; // pts to mem instr
1028
1029 valueForRegOffset = addr;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001030 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00001031 }
1032 else
1033 {
1034 offsetOpType = MachineOperand::MO_SignExtendedImmed;
1035 smallConstOffset = 0;
1036 }
Vikram S. Adveed3fefb2002-08-03 13:48:21 +00001037
Vikram S. Advea10d1a72002-03-31 19:07:35 +00001038 // For STORE:
1039 // Operand 0 is value, operand 1 is ptr, operand 2 is offset
1040 // For LOAD or GET_ELEMENT_PTR,
1041 // Operand 0 is ptr, operand 1 is offset, operand 2 is result.
1042 //
1043 unsigned offsetOpNum, ptrOpNum;
1044 if (memInst->getOpcode() == Instruction::Store)
1045 {
1046 (*mvecI)->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister,
1047 vmInstrNode->leftChild()->getValue());
1048 ptrOpNum = 1;
1049 offsetOpNum = 2;
1050 }
1051 else
1052 {
1053 ptrOpNum = 0;
1054 offsetOpNum = 1;
1055 (*mvecI)->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister,
1056 memInst);
1057 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00001058
Vikram S. Advea10d1a72002-03-31 19:07:35 +00001059 (*mvecI)->SetMachineOperandVal(ptrOpNum, MachineOperand::MO_VirtualRegister,
1060 ptrVal);
1061
Chris Lattner20b1ea02001-09-14 03:47:57 +00001062 if (offsetOpType == MachineOperand::MO_VirtualRegister)
1063 {
1064 assert(valueForRegOffset != NULL);
Vikram S. Adve74825322002-03-18 03:15:35 +00001065 (*mvecI)->SetMachineOperandVal(offsetOpNum, offsetOpType,
1066 valueForRegOffset);
Chris Lattner20b1ea02001-09-14 03:47:57 +00001067 }
1068 else
Vikram S. Adve74825322002-03-18 03:15:35 +00001069 (*mvecI)->SetMachineOperandConst(offsetOpNum, offsetOpType,
1070 smallConstOffset);
Chris Lattner20b1ea02001-09-14 03:47:57 +00001071}
1072
1073
Chris Lattner20b1ea02001-09-14 03:47:57 +00001074//
1075// Substitute operand `operandNum' of the instruction in node `treeNode'
Vikram S. Advec025fc12001-10-14 23:28:43 +00001076// in place of the use(s) of that instruction in node `parent'.
1077// Check both explicit and implicit operands!
Vikram S. Adve74825322002-03-18 03:15:35 +00001078// Also make sure to skip over a parent who:
1079// (1) is a list node in the Burg tree, or
1080// (2) itself had its results forwarded to its parent
Chris Lattner20b1ea02001-09-14 03:47:57 +00001081//
1082static void
1083ForwardOperand(InstructionNode* treeNode,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001084 InstrTreeNode* parent,
1085 int operandNum)
Chris Lattner20b1ea02001-09-14 03:47:57 +00001086{
Vikram S. Adve243dd452001-09-18 13:03:13 +00001087 assert(treeNode && parent && "Invalid invocation of ForwardOperand");
1088
Chris Lattner20b1ea02001-09-14 03:47:57 +00001089 Instruction* unusedOp = treeNode->getInstruction();
1090 Value* fwdOp = unusedOp->getOperand(operandNum);
Vikram S. Adve243dd452001-09-18 13:03:13 +00001091
1092 // The parent itself may be a list node, so find the real parent instruction
1093 while (parent->getNodeType() != InstrTreeNode::NTInstructionNode)
1094 {
1095 parent = parent->parent();
1096 assert(parent && "ERROR: Non-instruction node has no parent in tree.");
1097 }
1098 InstructionNode* parentInstrNode = (InstructionNode*) parent;
1099
1100 Instruction* userInstr = parentInstrNode->getInstruction();
Chris Lattner9c461082002-02-03 07:50:56 +00001101 MachineCodeForInstruction &mvec = MachineCodeForInstruction::get(userInstr);
Vikram S. Adve74825322002-03-18 03:15:35 +00001102
1103 // The parent's mvec would be empty if it was itself forwarded.
1104 // Recursively call ForwardOperand in that case...
1105 //
1106 if (mvec.size() == 0)
Chris Lattner20b1ea02001-09-14 03:47:57 +00001107 {
Vikram S. Adve74825322002-03-18 03:15:35 +00001108 assert(parent->parent() != NULL &&
1109 "Parent could not have been forwarded, yet has no instructions?");
1110 ForwardOperand(treeNode, parent->parent(), operandNum);
1111 }
1112 else
1113 {
Vikram S. Adve74825322002-03-18 03:15:35 +00001114 for (unsigned i=0, N=mvec.size(); i < N; i++)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001115 {
Vikram S. Adve74825322002-03-18 03:15:35 +00001116 MachineInstr* minstr = mvec[i];
1117 for (unsigned i=0, numOps=minstr->getNumOperands(); i < numOps; ++i)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001118 {
Vikram S. Adve74825322002-03-18 03:15:35 +00001119 const MachineOperand& mop = minstr->getOperand(i);
1120 if (mop.getOperandType() == MachineOperand::MO_VirtualRegister &&
1121 mop.getVRegValue() == unusedOp)
Vikram S. Adve242a8082002-05-19 15:25:51 +00001122 minstr->SetMachineOperandVal(i,
Vikram S. Adve74825322002-03-18 03:15:35 +00001123 MachineOperand::MO_VirtualRegister, fwdOp);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001124 }
Vikram S. Adve74825322002-03-18 03:15:35 +00001125
1126 for (unsigned i=0,numOps=minstr->getNumImplicitRefs(); i<numOps; ++i)
1127 if (minstr->getImplicitRef(i) == unusedOp)
Vikram S. Adve242a8082002-05-19 15:25:51 +00001128 minstr->setImplicitRef(i, fwdOp,
Vikram S. Adve6418eac2002-07-08 23:30:14 +00001129 minstr->implicitRefIsDefined(i),
1130 minstr->implicitRefIsDefinedAndUsed(i));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001131 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00001132 }
1133}
1134
1135
Vikram S. Adve242a8082002-05-19 15:25:51 +00001136inline bool
1137AllUsesAreBranches(const Instruction* setccI)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001138{
Vikram S. Adve242a8082002-05-19 15:25:51 +00001139 for (Value::use_const_iterator UI=setccI->use_begin(), UE=setccI->use_end();
1140 UI != UE; ++UI)
1141 if (! isa<TmpInstruction>(*UI) // ignore tmp instructions here
1142 && cast<Instruction>(*UI)->getOpcode() != Instruction::Br)
1143 return false;
1144 return true;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001145}
1146
Vikram S. Advefb361122001-10-22 13:36:31 +00001147//******************* Externally Visible Functions *************************/
1148
Vikram S. Advefb361122001-10-22 13:36:31 +00001149//------------------------------------------------------------------------
1150// External Function: ThisIsAChainRule
1151//
1152// Purpose:
1153// Check if a given BURG rule is a chain rule.
1154//------------------------------------------------------------------------
1155
1156extern bool
1157ThisIsAChainRule(int eruleno)
1158{
1159 switch(eruleno)
1160 {
1161 case 111: // stmt: reg
Vikram S. Advefb361122001-10-22 13:36:31 +00001162 case 123:
1163 case 124:
1164 case 125:
1165 case 126:
1166 case 127:
1167 case 128:
1168 case 129:
1169 case 130:
1170 case 131:
1171 case 132:
1172 case 133:
1173 case 155:
1174 case 221:
1175 case 222:
1176 case 241:
1177 case 242:
1178 case 243:
1179 case 244:
Vikram S. Adve30a6f492002-08-22 02:56:10 +00001180 case 245:
Vikram S. Adve85e1e9c2002-04-01 20:28:48 +00001181 case 321:
Vikram S. Advefb361122001-10-22 13:36:31 +00001182 return true; break;
Vikram S. Adve30a6f492002-08-22 02:56:10 +00001183
Vikram S. Advefb361122001-10-22 13:36:31 +00001184 default:
1185 return false; break;
1186 }
1187}
Chris Lattner20b1ea02001-09-14 03:47:57 +00001188
1189
1190//------------------------------------------------------------------------
1191// External Function: GetInstructionsByRule
1192//
1193// Purpose:
1194// Choose machine instructions for the SPARC according to the
1195// patterns chosen by the BURG-generated parser.
1196//------------------------------------------------------------------------
1197
Vikram S. Adve74825322002-03-18 03:15:35 +00001198void
Chris Lattner20b1ea02001-09-14 03:47:57 +00001199GetInstructionsByRule(InstructionNode* subtreeRoot,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001200 int ruleForNode,
1201 short* nts,
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001202 TargetMachine &target,
Vikram S. Adve74825322002-03-18 03:15:35 +00001203 vector<MachineInstr*>& mvec)
Chris Lattner20b1ea02001-09-14 03:47:57 +00001204{
Chris Lattner20b1ea02001-09-14 03:47:57 +00001205 bool checkCast = false; // initialize here to use fall-through
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001206 bool maskUnsignedResult = false;
Chris Lattner20b1ea02001-09-14 03:47:57 +00001207 int nextRule;
1208 int forwardOperandNum = -1;
Vikram S. Adve74825322002-03-18 03:15:35 +00001209 unsigned int allocaSize = 0;
1210 MachineInstr* M, *M2;
1211 unsigned int L;
1212
1213 mvec.clear();
Chris Lattner20b1ea02001-09-14 03:47:57 +00001214
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001215 // If the code for this instruction was folded into the parent (user),
1216 // then do nothing!
1217 if (subtreeRoot->isFoldedIntoParent())
1218 return;
1219
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001220 //
1221 // Let's check for chain rules outside the switch so that we don't have
1222 // to duplicate the list of chain rule production numbers here again
1223 //
1224 if (ThisIsAChainRule(ruleForNode))
Chris Lattner20b1ea02001-09-14 03:47:57 +00001225 {
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001226 // Chain rules have a single nonterminal on the RHS.
1227 // Get the rule that matches the RHS non-terminal and use that instead.
1228 //
1229 assert(nts[0] && ! nts[1]
1230 && "A chain rule should have only one RHS non-terminal!");
1231 nextRule = burm_rule(subtreeRoot->state, nts[0]);
1232 nts = burm_nts[nextRule];
Vikram S. Adve74825322002-03-18 03:15:35 +00001233 GetInstructionsByRule(subtreeRoot, nextRule, nts, target, mvec);
Chris Lattner20b1ea02001-09-14 03:47:57 +00001234 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001235 else
Chris Lattner20b1ea02001-09-14 03:47:57 +00001236 {
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001237 switch(ruleForNode) {
1238 case 1: // stmt: Ret
1239 case 2: // stmt: RetValue(reg)
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001240 { // NOTE: Prepass of register allocation is responsible
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001241 // for moving return value to appropriate register.
1242 // Mark the return-address register as a hidden virtual reg.
Vikram S. Advea995e602001-10-11 04:23:19 +00001243 // Mark the return value register as an implicit ref of
1244 // the machine instruction.
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001245 // Finally put a NOP in the delay slot.
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001246 ReturnInst *returnInstr =
1247 cast<ReturnInst>(subtreeRoot->getInstruction());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001248 assert(returnInstr->getOpcode() == Instruction::Ret);
1249
Chris Lattner9c461082002-02-03 07:50:56 +00001250 Instruction* returnReg = new TmpInstruction(returnInstr);
1251 MachineCodeForInstruction::get(returnInstr).addTemp(returnReg);
Vikram S. Advefb361122001-10-22 13:36:31 +00001252
Vikram S. Adve74825322002-03-18 03:15:35 +00001253 M = new MachineInstr(JMPLRET);
1254 M->SetMachineOperandReg(0, MachineOperand::MO_VirtualRegister,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001255 returnReg);
Vikram S. Adve74825322002-03-18 03:15:35 +00001256 M->SetMachineOperandConst(1,MachineOperand::MO_SignExtendedImmed,
Chris Lattner697954c2002-01-20 22:54:45 +00001257 (int64_t)8);
Vikram S. Adve74825322002-03-18 03:15:35 +00001258 M->SetMachineOperandReg(2, target.getRegInfo().getZeroRegNum());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001259
Vikram S. Advea995e602001-10-11 04:23:19 +00001260 if (returnInstr->getReturnValue() != NULL)
Vikram S. Adve74825322002-03-18 03:15:35 +00001261 M->addImplicitRef(returnInstr->getReturnValue());
Vikram S. Advea995e602001-10-11 04:23:19 +00001262
Vikram S. Adve74825322002-03-18 03:15:35 +00001263 mvec.push_back(M);
1264 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001265
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001266 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001267 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001268
1269 case 3: // stmt: Store(reg,reg)
1270 case 4: // stmt: Store(reg,ptrreg)
Vikram S. Adve74825322002-03-18 03:15:35 +00001271 mvec.push_back(new MachineInstr(
1272 ChooseStoreInstruction(
1273 subtreeRoot->leftChild()->getValue()->getType())));
Vikram S. Adve8cfffd32002-08-24 20:56:53 +00001274 SetOperandsForMemInstr(mvec, subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001275 break;
Chris Lattner20b1ea02001-09-14 03:47:57 +00001276
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001277 case 5: // stmt: BrUncond
Vikram S. Adve74825322002-03-18 03:15:35 +00001278 M = new MachineInstr(BA);
Vikram S. Adve6418eac2002-07-08 23:30:14 +00001279 M->SetMachineOperandVal(0, MachineOperand::MO_PCRelativeDisp,
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001280 cast<BranchInst>(subtreeRoot->getInstruction())->getSuccessor(0));
Vikram S. Adve74825322002-03-18 03:15:35 +00001281 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001282
1283 // delay slot
Vikram S. Adve74825322002-03-18 03:15:35 +00001284 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001285 break;
Chris Lattner20b1ea02001-09-14 03:47:57 +00001286
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001287 case 206: // stmt: BrCond(setCCconst)
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001288 { // setCCconst => boolean was computed with `%b = setCC type reg1 const'
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001289 // If the constant is ZERO, we can use the branch-on-integer-register
1290 // instructions and avoid the SUBcc instruction entirely.
1291 // Otherwise this is just the same as case 5, so just fall through.
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001292 //
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001293 InstrTreeNode* constNode = subtreeRoot->leftChild()->rightChild();
1294 assert(constNode &&
1295 constNode->getNodeType() ==InstrTreeNode::NTConstNode);
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001296 Constant *constVal = cast<Constant>(constNode->getValue());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001297 bool isValidConst;
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001298
Chris Lattner0c4e8862002-09-03 01:08:28 +00001299 if ((constVal->getType()->isInteger()
Chris Lattner9b625032002-05-06 16:15:30 +00001300 || isa<PointerType>(constVal->getType()))
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001301 && GetConstantValueAsSignedInt(constVal, isValidConst) == 0
1302 && isValidConst)
1303 {
1304 // That constant is a zero after all...
1305 // Use the left child of setCC as the first argument!
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001306 // Mark the setCC node so that no code is generated for it.
1307 InstructionNode* setCCNode = (InstructionNode*)
1308 subtreeRoot->leftChild();
1309 assert(setCCNode->getOpLabel() == SetCCOp);
1310 setCCNode->markFoldedIntoParent();
1311
1312 BranchInst* brInst=cast<BranchInst>(subtreeRoot->getInstruction());
1313
Vikram S. Adve74825322002-03-18 03:15:35 +00001314 M = new MachineInstr(ChooseBprInstruction(subtreeRoot));
1315 M->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister,
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001316 setCCNode->leftChild()->getValue());
Vikram S. Adve74825322002-03-18 03:15:35 +00001317 M->SetMachineOperandVal(1, MachineOperand::MO_PCRelativeDisp,
1318 brInst->getSuccessor(0));
1319 mvec.push_back(M);
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001320
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001321 // delay slot
Vikram S. Adve74825322002-03-18 03:15:35 +00001322 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001323
1324 // false branch
Vikram S. Adve74825322002-03-18 03:15:35 +00001325 M = new MachineInstr(BA);
Vikram S. Adve6418eac2002-07-08 23:30:14 +00001326 M->SetMachineOperandVal(0, MachineOperand::MO_PCRelativeDisp,
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001327 brInst->getSuccessor(1));
Vikram S. Adve74825322002-03-18 03:15:35 +00001328 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001329
1330 // delay slot
Vikram S. Adve74825322002-03-18 03:15:35 +00001331 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001332
1333 break;
1334 }
1335 // ELSE FALL THROUGH
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001336 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001337
Vikram S. Adve30a6f492002-08-22 02:56:10 +00001338 case 6: // stmt: BrCond(setCC)
1339 { // bool => boolean was computed with SetCC.
1340 // The branch to use depends on whether it is FP, signed, or unsigned.
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001341 // If it is an integer CC, we also need to find the unique
1342 // TmpInstruction representing that CC.
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001343 //
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001344 BranchInst* brInst = cast<BranchInst>(subtreeRoot->getInstruction());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001345 bool isFPBranch;
Vikram S. Adve74825322002-03-18 03:15:35 +00001346 M = new MachineInstr(ChooseBccInstruction(subtreeRoot, isFPBranch));
Vikram S. Adve30a6f492002-08-22 02:56:10 +00001347
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001348 Value* ccValue = GetTmpForCC(subtreeRoot->leftChild()->getValue(),
1349 brInst->getParent()->getParent(),
1350 isFPBranch? Type::FloatTy : Type::IntTy);
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001351
Vikram S. Adve74825322002-03-18 03:15:35 +00001352 M->SetMachineOperandVal(0, MachineOperand::MO_CCRegister, ccValue);
1353 M->SetMachineOperandVal(1, MachineOperand::MO_PCRelativeDisp,
1354 brInst->getSuccessor(0));
1355 mvec.push_back(M);
Vikram S. Adve30a6f492002-08-22 02:56:10 +00001356
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001357 // delay slot
Vikram S. Adve74825322002-03-18 03:15:35 +00001358 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adve30a6f492002-08-22 02:56:10 +00001359
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001360 // false branch
Vikram S. Adve74825322002-03-18 03:15:35 +00001361 M = new MachineInstr(BA);
Vikram S. Adve6418eac2002-07-08 23:30:14 +00001362 M->SetMachineOperandVal(0, MachineOperand::MO_PCRelativeDisp,
Vikram S. Adve74825322002-03-18 03:15:35 +00001363 brInst->getSuccessor(1));
1364 mvec.push_back(M);
Vikram S. Adve30a6f492002-08-22 02:56:10 +00001365
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001366 // delay slot
Vikram S. Adve74825322002-03-18 03:15:35 +00001367 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001368 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001369 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001370
1371 case 208: // stmt: BrCond(boolconst)
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001372 {
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001373 // boolconst => boolean is a constant; use BA to first or second label
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001374 Constant* constVal =
1375 cast<Constant>(subtreeRoot->leftChild()->getValue());
1376 unsigned dest = cast<ConstantBool>(constVal)->getValue()? 0 : 1;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001377
Vikram S. Adve74825322002-03-18 03:15:35 +00001378 M = new MachineInstr(BA);
Vikram S. Adve6418eac2002-07-08 23:30:14 +00001379 M->SetMachineOperandVal(0, MachineOperand::MO_PCRelativeDisp,
Chris Lattner35504202002-04-27 03:14:39 +00001380 cast<BranchInst>(subtreeRoot->getInstruction())->getSuccessor(dest));
Vikram S. Adve74825322002-03-18 03:15:35 +00001381 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001382
1383 // delay slot
Vikram S. Adve74825322002-03-18 03:15:35 +00001384 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001385 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001386 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001387
1388 case 8: // stmt: BrCond(boolreg)
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001389 { // boolreg => boolean is stored in an existing register.
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001390 // Just use the branch-on-integer-register instruction!
1391 //
Vikram S. Adve74825322002-03-18 03:15:35 +00001392 M = new MachineInstr(BRNZ);
1393 M->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001394 subtreeRoot->leftChild()->getValue());
Vikram S. Adve74825322002-03-18 03:15:35 +00001395 M->SetMachineOperandVal(1, MachineOperand::MO_PCRelativeDisp,
Chris Lattner35504202002-04-27 03:14:39 +00001396 cast<BranchInst>(subtreeRoot->getInstruction())->getSuccessor(0));
Vikram S. Adve74825322002-03-18 03:15:35 +00001397 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001398
1399 // delay slot
Vikram S. Adve74825322002-03-18 03:15:35 +00001400 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001401
1402 // false branch
Vikram S. Adve74825322002-03-18 03:15:35 +00001403 M = new MachineInstr(BA);
Vikram S. Adve6418eac2002-07-08 23:30:14 +00001404 M->SetMachineOperandVal(0, MachineOperand::MO_PCRelativeDisp,
Chris Lattner35504202002-04-27 03:14:39 +00001405 cast<BranchInst>(subtreeRoot->getInstruction())->getSuccessor(1));
Vikram S. Adve74825322002-03-18 03:15:35 +00001406 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001407
1408 // delay slot
Vikram S. Adve74825322002-03-18 03:15:35 +00001409 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001410 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001411 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00001412
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001413 case 9: // stmt: Switch(reg)
1414 assert(0 && "*** SWITCH instruction is not implemented yet.");
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001415 break;
Chris Lattner20b1ea02001-09-14 03:47:57 +00001416
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001417 case 10: // reg: VRegList(reg, reg)
1418 assert(0 && "VRegList should never be the topmost non-chain rule");
1419 break;
1420
Vikram S. Advece08e1d2002-08-15 14:17:37 +00001421 case 21: // bool: Not(bool,reg): Both these are implemented as:
1422 case 421: // reg: BNot(reg,reg): reg = reg XOR-NOT 0
1423 { // First find the unary operand. It may be left or right, usually right.
1424 Value* notArg = BinaryOperator::getNotArgument(
1425 cast<BinaryOperator>(subtreeRoot->getInstruction()));
1426 mvec.push_back(Create3OperandInstr_Reg(XNOR, notArg,
1427 target.getRegInfo().getZeroRegNum(),
1428 subtreeRoot->getValue()));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001429 break;
Vikram S. Advece08e1d2002-08-15 14:17:37 +00001430 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001431
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001432 case 22: // reg: ToBoolTy(reg):
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001433 {
1434 const Type* opType = subtreeRoot->leftChild()->getValue()->getType();
Chris Lattner0c4e8862002-09-03 01:08:28 +00001435 assert(opType->isIntegral() || isa<PointerType>(opType));
Vikram S. Adve74825322002-03-18 03:15:35 +00001436 forwardOperandNum = 0; // forward first operand to user
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001437 break;
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001438 }
1439
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001440 case 23: // reg: ToUByteTy(reg)
1441 case 25: // reg: ToUShortTy(reg)
1442 case 27: // reg: ToUIntTy(reg)
1443 case 29: // reg: ToULongTy(reg)
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001444 {
Vikram S. Adve242a8082002-05-19 15:25:51 +00001445 Instruction* destI = subtreeRoot->getInstruction();
1446 Value* opVal = subtreeRoot->leftChild()->getValue();
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001447 const Type* opType = subtreeRoot->leftChild()->getValue()->getType();
Chris Lattner0c4e8862002-09-03 01:08:28 +00001448 if (opType->isIntegral() || isa<PointerType>(opType))
Vikram S. Adve1e606692002-07-31 21:01:34 +00001449 {
1450 unsigned opSize = target.DataLayout.getTypeSize(opType);
1451 unsigned destSize = target.DataLayout.getTypeSize(destI->getType());
1452 if (opSize > destSize ||
1453 (opType->isSigned()
1454 && destSize < target.DataLayout.getIntegerRegize()))
1455 { // operand is larger than dest,
1456 // OR both are equal but smaller than the full register size
1457 // AND operand is signed, so it may have extra sign bits:
1458 // mask high bits using AND
1459 M = Create3OperandInstr(AND, opVal,
1460 ConstantUInt::get(Type::ULongTy,
1461 ((uint64_t) 1 << 8*destSize) - 1),
1462 destI);
1463 mvec.push_back(M);
1464 }
1465 else
1466 forwardOperandNum = 0; // forward first operand to user
Vikram S. Adve242a8082002-05-19 15:25:51 +00001467 }
Vikram S. Adve1e606692002-07-31 21:01:34 +00001468 else if (opType->isFloatingPoint())
Vikram S. Adve8cfffd32002-08-24 20:56:53 +00001469 {
1470 CreateCodeToConvertFloatToInt(target, opVal, destI, mvec,
1471 MachineCodeForInstruction::get(destI));
1472 maskUnsignedResult = true; // not handled by convert code
1473 }
Vikram S. Adve242a8082002-05-19 15:25:51 +00001474 else
Vikram S. Adve1e606692002-07-31 21:01:34 +00001475 assert(0 && "Unrecognized operand type for convert-to-unsigned");
1476
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001477 break;
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001478 }
1479
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001480 case 24: // reg: ToSByteTy(reg)
1481 case 26: // reg: ToShortTy(reg)
1482 case 28: // reg: ToIntTy(reg)
1483 case 30: // reg: ToLongTy(reg)
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001484 {
Vikram S. Adve242a8082002-05-19 15:25:51 +00001485 Instruction* destI = subtreeRoot->getInstruction();
1486 Value* opVal = subtreeRoot->leftChild()->getValue();
1487 MachineCodeForInstruction& mcfi =MachineCodeForInstruction::get(destI);
Vikram S. Adve1e606692002-07-31 21:01:34 +00001488
Vikram S. Adve242a8082002-05-19 15:25:51 +00001489 const Type* opType = opVal->getType();
Chris Lattner0c4e8862002-09-03 01:08:28 +00001490 if (opType->isIntegral() || isa<PointerType>(opType))
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001491 {
Vikram S. Adve242a8082002-05-19 15:25:51 +00001492 // These operand types have the same format as the destination,
1493 // but may have different size: add sign bits or mask as needed.
1494 //
1495 const Type* destType = destI->getType();
1496 unsigned opSize = target.DataLayout.getTypeSize(opType);
1497 unsigned destSize = target.DataLayout.getTypeSize(destType);
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001498
1499 if (opSize < destSize ||
1500 (opSize == destSize &&
1501 opSize == target.DataLayout.getIntegerRegize()))
1502 { // operand is smaller or both operand and result fill register
1503 forwardOperandNum = 0; // forward first operand to user
Vikram S. Adve242a8082002-05-19 15:25:51 +00001504 }
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001505 else
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001506 { // need to mask (possibly) and then sign-extend (definitely)
1507 Value* srcForSignExt = opVal;
1508 unsigned srcSizeForSignExt = 8 * opSize;
1509 if (opSize > destSize)
1510 { // operand is larger than dest: mask high bits
1511 TmpInstruction *tmpI = new TmpInstruction(destType, opVal,
1512 destI, "maskHi");
1513 mcfi.addTemp(tmpI);
1514 M = Create3OperandInstr(AND, opVal,
1515 ConstantUInt::get(Type::ULongTy,
1516 ((uint64_t) 1 << 8*destSize)-1),
1517 tmpI);
1518 mvec.push_back(M);
1519 srcForSignExt = tmpI;
1520 srcSizeForSignExt = 8 * destSize;
1521 }
1522
1523 // sign-extend
1524 target.getInstrInfo().CreateSignExtensionInstructions(target, destI->getParent()->getParent(), srcForSignExt, srcSizeForSignExt, destI, mvec, mcfi);
1525 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001526 }
Vikram S. Adve1e606692002-07-31 21:01:34 +00001527 else if (opType->isFloatingPoint())
Vikram S. Adve8cfffd32002-08-24 20:56:53 +00001528 CreateCodeToConvertFloatToInt(target, opVal, destI, mvec, mcfi);
Vikram S. Adve1e606692002-07-31 21:01:34 +00001529 else
1530 assert(0 && "Unrecognized operand type for convert-to-signed");
1531
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001532 break;
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001533 }
1534
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001535 case 31: // reg: ToFloatTy(reg):
1536 case 32: // reg: ToDoubleTy(reg):
1537 case 232: // reg: ToDoubleTy(Constant):
1538
1539 // If this instruction has a parent (a user) in the tree
1540 // and the user is translated as an FsMULd instruction,
1541 // then the cast is unnecessary. So check that first.
1542 // In the future, we'll want to do the same for the FdMULq instruction,
1543 // so do the check here instead of only for ToFloatTy(reg).
1544 //
1545 if (subtreeRoot->parent() != NULL &&
Chris Lattner9c461082002-02-03 07:50:56 +00001546 MachineCodeForInstruction::get(((InstructionNode*)subtreeRoot->parent())->getInstruction())[0]->getOpCode() == FSMULD)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001547 {
Vikram S. Adve74825322002-03-18 03:15:35 +00001548 forwardOperandNum = 0; // forward first operand to user
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001549 }
1550 else
1551 {
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001552 Value* leftVal = subtreeRoot->leftChild()->getValue();
1553 const Type* opType = leftVal->getType();
Vikram S. Advedbc4fad2002-04-25 04:37:51 +00001554 MachineOpCode opCode=ChooseConvertToFloatInstr(
1555 subtreeRoot->getOpLabel(), opType);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001556 if (opCode == INVALID_OPCODE) // no conversion needed
1557 {
Vikram S. Adve74825322002-03-18 03:15:35 +00001558 forwardOperandNum = 0; // forward first operand to user
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001559 }
1560 else
1561 {
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001562 // If the source operand is a non-FP type it must be
1563 // first copied from int to float register via memory!
1564 Instruction *dest = subtreeRoot->getInstruction();
1565 Value* srcForCast;
1566 int n = 0;
Vikram S. Adve242a8082002-05-19 15:25:51 +00001567 if (! opType->isFloatingPoint())
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001568 {
1569 // Create a temporary to represent the FP register
1570 // into which the integer will be copied via memory.
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001571 // The type of this temporary will determine the FP
1572 // register used: single-prec for a 32-bit int or smaller,
1573 // double-prec for a 64-bit int.
1574 //
1575 const Type* srcTypeToUse =
1576 (leftVal->getType() == Type::LongTy)? Type::DoubleTy
1577 : Type::FloatTy;
1578
Chris Lattner9c461082002-02-03 07:50:56 +00001579 srcForCast = new TmpInstruction(srcTypeToUse, dest);
Vikram S. Advedbc4fad2002-04-25 04:37:51 +00001580 MachineCodeForInstruction &destMCFI =
Chris Lattner9c461082002-02-03 07:50:56 +00001581 MachineCodeForInstruction::get(dest);
Vikram S. Advedbc4fad2002-04-25 04:37:51 +00001582 destMCFI.addTemp(srcForCast);
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001583
Vikram S. Adve242a8082002-05-19 15:25:51 +00001584 target.getInstrInfo().CreateCodeToCopyIntToFloat(target,
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001585 dest->getParent()->getParent(),
1586 leftVal, (TmpInstruction*) srcForCast,
Vikram S. Adve242a8082002-05-19 15:25:51 +00001587 mvec, destMCFI);
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001588 }
1589 else
1590 srcForCast = leftVal;
1591
Vikram S. Adve74825322002-03-18 03:15:35 +00001592 M = new MachineInstr(opCode);
1593 M->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister,
1594 srcForCast);
1595 M->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister,
1596 dest);
1597 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001598 }
1599 }
1600 break;
1601
1602 case 19: // reg: ToArrayTy(reg):
1603 case 20: // reg: ToPointerTy(reg):
Vikram S. Adve74825322002-03-18 03:15:35 +00001604 forwardOperandNum = 0; // forward first operand to user
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001605 break;
1606
1607 case 233: // reg: Add(reg, Constant)
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001608 maskUnsignedResult = true;
Vikram S. Adve74825322002-03-18 03:15:35 +00001609 M = CreateAddConstInstruction(subtreeRoot);
1610 if (M != NULL)
1611 {
1612 mvec.push_back(M);
1613 break;
1614 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001615 // ELSE FALL THROUGH
Vikram S. Adve74825322002-03-18 03:15:35 +00001616
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001617 case 33: // reg: Add(reg, reg)
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001618 maskUnsignedResult = true;
Vikram S. Adve74825322002-03-18 03:15:35 +00001619 mvec.push_back(new MachineInstr(ChooseAddInstruction(subtreeRoot)));
1620 Set3OperandsFromInstr(mvec.back(), subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001621 break;
1622
1623 case 234: // reg: Sub(reg, Constant)
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001624 maskUnsignedResult = true;
Vikram S. Adve74825322002-03-18 03:15:35 +00001625 M = CreateSubConstInstruction(subtreeRoot);
1626 if (M != NULL)
1627 {
1628 mvec.push_back(M);
1629 break;
1630 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001631 // ELSE FALL THROUGH
Vikram S. Adve74825322002-03-18 03:15:35 +00001632
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001633 case 34: // reg: Sub(reg, reg)
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001634 maskUnsignedResult = true;
Vikram S. Adve74825322002-03-18 03:15:35 +00001635 mvec.push_back(new MachineInstr(ChooseSubInstructionByType(
1636 subtreeRoot->getInstruction()->getType())));
1637 Set3OperandsFromInstr(mvec.back(), subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001638 break;
1639
1640 case 135: // reg: Mul(todouble, todouble)
1641 checkCast = true;
1642 // FALL THROUGH
1643
1644 case 35: // reg: Mul(reg, reg)
Vikram S. Adve74825322002-03-18 03:15:35 +00001645 {
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001646 maskUnsignedResult = true;
Vikram S. Adve74825322002-03-18 03:15:35 +00001647 MachineOpCode forceOp = ((checkCast && BothFloatToDouble(subtreeRoot))
1648 ? FSMULD
1649 : INVALID_MACHINE_OPCODE);
Vikram S. Adve242a8082002-05-19 15:25:51 +00001650 Instruction* mulInstr = subtreeRoot->getInstruction();
1651 CreateMulInstruction(target, mulInstr->getParent()->getParent(),
Vikram S. Adve74825322002-03-18 03:15:35 +00001652 subtreeRoot->leftChild()->getValue(),
1653 subtreeRoot->rightChild()->getValue(),
Vikram S. Adve242a8082002-05-19 15:25:51 +00001654 mulInstr, mvec,
1655 MachineCodeForInstruction::get(mulInstr),forceOp);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001656 break;
Vikram S. Adve74825322002-03-18 03:15:35 +00001657 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001658 case 335: // reg: Mul(todouble, todoubleConst)
1659 checkCast = true;
1660 // FALL THROUGH
1661
1662 case 235: // reg: Mul(reg, Constant)
Vikram S. Adve74825322002-03-18 03:15:35 +00001663 {
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001664 maskUnsignedResult = true;
Vikram S. Adve74825322002-03-18 03:15:35 +00001665 MachineOpCode forceOp = ((checkCast && BothFloatToDouble(subtreeRoot))
1666 ? FSMULD
1667 : INVALID_MACHINE_OPCODE);
Vikram S. Adve242a8082002-05-19 15:25:51 +00001668 Instruction* mulInstr = subtreeRoot->getInstruction();
1669 CreateMulInstruction(target, mulInstr->getParent()->getParent(),
Vikram S. Adve74825322002-03-18 03:15:35 +00001670 subtreeRoot->leftChild()->getValue(),
1671 subtreeRoot->rightChild()->getValue(),
Vikram S. Adve242a8082002-05-19 15:25:51 +00001672 mulInstr, mvec,
1673 MachineCodeForInstruction::get(mulInstr),
1674 forceOp);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001675 break;
Vikram S. Adve74825322002-03-18 03:15:35 +00001676 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001677 case 236: // reg: Div(reg, Constant)
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001678 maskUnsignedResult = true;
Vikram S. Adve74825322002-03-18 03:15:35 +00001679 L = mvec.size();
1680 CreateDivConstInstruction(target, subtreeRoot, mvec);
1681 if (mvec.size() > L)
1682 break;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001683 // ELSE FALL THROUGH
Vikram S. Adve74825322002-03-18 03:15:35 +00001684
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001685 case 36: // reg: Div(reg, reg)
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001686 maskUnsignedResult = true;
Vikram S. Adve74825322002-03-18 03:15:35 +00001687 mvec.push_back(new MachineInstr(ChooseDivInstruction(target, subtreeRoot)));
1688 Set3OperandsFromInstr(mvec.back(), subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001689 break;
1690
1691 case 37: // reg: Rem(reg, reg)
1692 case 237: // reg: Rem(reg, Constant)
Vikram S. Adve510eec72001-11-04 21:59:14 +00001693 {
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001694 maskUnsignedResult = true;
Vikram S. Adve510eec72001-11-04 21:59:14 +00001695 Instruction* remInstr = subtreeRoot->getInstruction();
1696
Chris Lattner9c461082002-02-03 07:50:56 +00001697 TmpInstruction* quot = new TmpInstruction(
Vikram S. Adve510eec72001-11-04 21:59:14 +00001698 subtreeRoot->leftChild()->getValue(),
1699 subtreeRoot->rightChild()->getValue());
Chris Lattner9c461082002-02-03 07:50:56 +00001700 TmpInstruction* prod = new TmpInstruction(
Vikram S. Adve510eec72001-11-04 21:59:14 +00001701 quot,
1702 subtreeRoot->rightChild()->getValue());
Chris Lattner9c461082002-02-03 07:50:56 +00001703 MachineCodeForInstruction::get(remInstr).addTemp(quot).addTemp(prod);
Vikram S. Adve510eec72001-11-04 21:59:14 +00001704
Vikram S. Adve74825322002-03-18 03:15:35 +00001705 M = new MachineInstr(ChooseDivInstruction(target, subtreeRoot));
1706 Set3OperandsFromInstr(M, subtreeRoot, target);
1707 M->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister,quot);
1708 mvec.push_back(M);
Vikram S. Adve510eec72001-11-04 21:59:14 +00001709
Vikram S. Advece08e1d2002-08-15 14:17:37 +00001710 M = Create3OperandInstr(ChooseMulInstructionByType(
1711 subtreeRoot->getInstruction()->getType()),
1712 quot, subtreeRoot->rightChild()->getValue(),
1713 prod);
Vikram S. Adve74825322002-03-18 03:15:35 +00001714 mvec.push_back(M);
Vikram S. Adve510eec72001-11-04 21:59:14 +00001715
Vikram S. Adve74825322002-03-18 03:15:35 +00001716 M = new MachineInstr(ChooseSubInstructionByType(
Vikram S. Adve510eec72001-11-04 21:59:14 +00001717 subtreeRoot->getInstruction()->getType()));
Vikram S. Adve74825322002-03-18 03:15:35 +00001718 Set3OperandsFromInstr(M, subtreeRoot, target);
1719 M->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister,prod);
1720 mvec.push_back(M);
Vikram S. Adve510eec72001-11-04 21:59:14 +00001721
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001722 break;
Vikram S. Adve510eec72001-11-04 21:59:14 +00001723 }
1724
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001725 case 38: // bool: And(bool, bool)
1726 case 238: // bool: And(bool, boolconst)
1727 case 338: // reg : BAnd(reg, reg)
1728 case 538: // reg : BAnd(reg, Constant)
Vikram S. Adve74825322002-03-18 03:15:35 +00001729 mvec.push_back(new MachineInstr(AND));
1730 Set3OperandsFromInstr(mvec.back(), subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001731 break;
1732
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001733 case 138: // bool: And(bool, not)
Vikram S. Advece08e1d2002-08-15 14:17:37 +00001734 case 438: // bool: BAnd(bool, bnot)
1735 { // Use the argument of NOT as the second argument!
1736 // Mark the NOT node so that no code is generated for it.
1737 InstructionNode* notNode = (InstructionNode*) subtreeRoot->rightChild();
1738 Value* notArg = BinaryOperator::getNotArgument(
1739 cast<BinaryOperator>(notNode->getInstruction()));
1740 notNode->markFoldedIntoParent();
1741 mvec.push_back(Create3OperandInstr(ANDN,
1742 subtreeRoot->leftChild()->getValue(),
1743 notArg, subtreeRoot->getValue()));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001744 break;
Vikram S. Advece08e1d2002-08-15 14:17:37 +00001745 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001746
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001747 case 39: // bool: Or(bool, bool)
1748 case 239: // bool: Or(bool, boolconst)
1749 case 339: // reg : BOr(reg, reg)
1750 case 539: // reg : BOr(reg, Constant)
Vikram S. Adve242a8082002-05-19 15:25:51 +00001751 mvec.push_back(new MachineInstr(OR));
Vikram S. Adve74825322002-03-18 03:15:35 +00001752 Set3OperandsFromInstr(mvec.back(), subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001753 break;
1754
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001755 case 139: // bool: Or(bool, not)
Vikram S. Advece08e1d2002-08-15 14:17:37 +00001756 case 439: // bool: BOr(bool, bnot)
1757 { // Use the argument of NOT as the second argument!
1758 // Mark the NOT node so that no code is generated for it.
1759 InstructionNode* notNode = (InstructionNode*) subtreeRoot->rightChild();
1760 Value* notArg = BinaryOperator::getNotArgument(
1761 cast<BinaryOperator>(notNode->getInstruction()));
1762 notNode->markFoldedIntoParent();
1763 mvec.push_back(Create3OperandInstr(ORN,
1764 subtreeRoot->leftChild()->getValue(),
1765 notArg, subtreeRoot->getValue()));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001766 break;
Vikram S. Advece08e1d2002-08-15 14:17:37 +00001767 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001768
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001769 case 40: // bool: Xor(bool, bool)
1770 case 240: // bool: Xor(bool, boolconst)
1771 case 340: // reg : BXor(reg, reg)
1772 case 540: // reg : BXor(reg, Constant)
Vikram S. Adve74825322002-03-18 03:15:35 +00001773 mvec.push_back(new MachineInstr(XOR));
1774 Set3OperandsFromInstr(mvec.back(), subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001775 break;
1776
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001777 case 140: // bool: Xor(bool, not)
Vikram S. Advece08e1d2002-08-15 14:17:37 +00001778 case 440: // bool: BXor(bool, bnot)
1779 { // Use the argument of NOT as the second argument!
1780 // Mark the NOT node so that no code is generated for it.
1781 InstructionNode* notNode = (InstructionNode*) subtreeRoot->rightChild();
1782 Value* notArg = BinaryOperator::getNotArgument(
1783 cast<BinaryOperator>(notNode->getInstruction()));
1784 notNode->markFoldedIntoParent();
1785 mvec.push_back(Create3OperandInstr(XNOR,
1786 subtreeRoot->leftChild()->getValue(),
1787 notArg, subtreeRoot->getValue()));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001788 break;
Vikram S. Advece08e1d2002-08-15 14:17:37 +00001789 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001790
1791 case 41: // boolconst: SetCC(reg, Constant)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001792 //
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001793 // If the SetCC was folded into the user (parent), it will be
1794 // caught above. All other cases are the same as case 42,
1795 // so just fall through.
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001796 //
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001797 case 42: // bool: SetCC(reg, reg):
1798 {
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001799 // This generates a SUBCC instruction, putting the difference in
1800 // a result register, and setting a condition code.
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001801 //
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001802 // If the boolean result of the SetCC is used by anything other
Vikram S. Adve6418eac2002-07-08 23:30:14 +00001803 // than a branch instruction, or if it is used outside the current
1804 // basic block, the boolean must be
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001805 // computed and stored in the result register. Otherwise, discard
1806 // the difference (by using %g0) and keep only the condition code.
1807 //
1808 // To compute the boolean result in a register we use a conditional
1809 // move, unless the result of the SUBCC instruction can be used as
1810 // the bool! This assumes that zero is FALSE and any non-zero
1811 // integer is TRUE.
1812 //
1813 InstructionNode* parentNode = (InstructionNode*) subtreeRoot->parent();
1814 Instruction* setCCInstr = subtreeRoot->getInstruction();
Vikram S. Adve242a8082002-05-19 15:25:51 +00001815
Vikram S. Adve6418eac2002-07-08 23:30:14 +00001816 bool keepBoolVal = parentNode == NULL ||
1817 ! AllUsesAreBranches(setCCInstr);
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001818 bool subValIsBoolVal = setCCInstr->getOpcode() == Instruction::SetNE;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001819 bool keepSubVal = keepBoolVal && subValIsBoolVal;
1820 bool computeBoolVal = keepBoolVal && ! subValIsBoolVal;
1821
1822 bool mustClearReg;
1823 int valueToMove;
Chris Lattner8e5c0b42001-11-07 14:01:59 +00001824 MachineOpCode movOpCode = 0;
Vikram S. Adve6418eac2002-07-08 23:30:14 +00001825
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001826 // Mark the 4th operand as being a CC register, and as a def
1827 // A TmpInstruction is created to represent the CC "result".
1828 // Unlike other instances of TmpInstruction, this one is used
1829 // by machine code of multiple LLVM instructions, viz.,
1830 // the SetCC and the branch. Make sure to get the same one!
1831 // Note that we do this even for FP CC registers even though they
1832 // are explicit operands, because the type of the operand
1833 // needs to be a floating point condition code, not an integer
1834 // condition code. Think of this as casting the bool result to
1835 // a FP condition code register.
1836 //
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001837 Value* leftVal = subtreeRoot->leftChild()->getValue();
Chris Lattner9b625032002-05-06 16:15:30 +00001838 bool isFPCompare = leftVal->getType()->isFloatingPoint();
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001839
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001840 TmpInstruction* tmpForCC = GetTmpForCC(setCCInstr,
1841 setCCInstr->getParent()->getParent(),
Chris Lattner9b625032002-05-06 16:15:30 +00001842 isFPCompare ? Type::FloatTy : Type::IntTy);
Chris Lattner9c461082002-02-03 07:50:56 +00001843 MachineCodeForInstruction::get(setCCInstr).addTemp(tmpForCC);
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001844
1845 if (! isFPCompare)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001846 {
1847 // Integer condition: dest. should be %g0 or an integer register.
1848 // If result must be saved but condition is not SetEQ then we need
1849 // a separate instruction to compute the bool result, so discard
1850 // result of SUBcc instruction anyway.
1851 //
Vikram S. Adve74825322002-03-18 03:15:35 +00001852 M = new MachineInstr(SUBcc);
1853 Set3OperandsFromInstr(M, subtreeRoot, target, ! keepSubVal);
1854 M->SetMachineOperandVal(3, MachineOperand::MO_CCRegister,
1855 tmpForCC, /*def*/true);
1856 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001857
1858 if (computeBoolVal)
1859 { // recompute bool using the integer condition codes
1860 movOpCode =
1861 ChooseMovpccAfterSub(subtreeRoot,mustClearReg,valueToMove);
1862 }
1863 }
1864 else
1865 {
1866 // FP condition: dest of FCMP should be some FCCn register
Vikram S. Adve74825322002-03-18 03:15:35 +00001867 M = new MachineInstr(ChooseFcmpInstruction(subtreeRoot));
1868 M->SetMachineOperandVal(0, MachineOperand::MO_CCRegister,
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001869 tmpForCC);
Vikram S. Adve74825322002-03-18 03:15:35 +00001870 M->SetMachineOperandVal(1,MachineOperand::MO_VirtualRegister,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001871 subtreeRoot->leftChild()->getValue());
Vikram S. Adve74825322002-03-18 03:15:35 +00001872 M->SetMachineOperandVal(2,MachineOperand::MO_VirtualRegister,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001873 subtreeRoot->rightChild()->getValue());
Vikram S. Adve74825322002-03-18 03:15:35 +00001874 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001875
1876 if (computeBoolVal)
1877 {// recompute bool using the FP condition codes
1878 mustClearReg = true;
1879 valueToMove = 1;
1880 movOpCode = ChooseMovFpccInstruction(subtreeRoot);
1881 }
1882 }
1883
1884 if (computeBoolVal)
1885 {
1886 if (mustClearReg)
1887 {// Unconditionally set register to 0
Vikram S. Adve74825322002-03-18 03:15:35 +00001888 M = new MachineInstr(SETHI);
1889 M->SetMachineOperandConst(0,MachineOperand::MO_UnextendedImmed,
1890 (int64_t)0);
1891 M->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister,
1892 setCCInstr);
1893 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001894 }
1895
1896 // Now conditionally move `valueToMove' (0 or 1) into the register
Vikram S. Adve6418eac2002-07-08 23:30:14 +00001897 // Mark the register as a use (as well as a def) because the old
1898 // value should be retained if the condition is false.
Vikram S. Adve74825322002-03-18 03:15:35 +00001899 M = new MachineInstr(movOpCode);
1900 M->SetMachineOperandVal(0, MachineOperand::MO_CCRegister,
1901 tmpForCC);
1902 M->SetMachineOperandConst(1, MachineOperand::MO_UnextendedImmed,
1903 valueToMove);
1904 M->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister,
Vikram S. Adve6418eac2002-07-08 23:30:14 +00001905 setCCInstr, /*isDef*/ true,
1906 /*isDefAndUse*/ true);
Vikram S. Adve74825322002-03-18 03:15:35 +00001907 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001908 }
1909 break;
1910 }
1911
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001912 case 51: // reg: Load(reg)
1913 case 52: // reg: Load(ptrreg)
Vikram S. Adve74825322002-03-18 03:15:35 +00001914 mvec.push_back(new MachineInstr(ChooseLoadInstruction(
1915 subtreeRoot->getValue()->getType())));
Vikram S. Adve8cfffd32002-08-24 20:56:53 +00001916 SetOperandsForMemInstr(mvec, subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001917 break;
1918
1919 case 55: // reg: GetElemPtr(reg)
1920 case 56: // reg: GetElemPtrIdx(reg,reg)
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001921 // If the GetElemPtr was folded into the user (parent), it will be
1922 // caught above. For other cases, we have to compute the address.
Vikram S. Adve74825322002-03-18 03:15:35 +00001923 mvec.push_back(new MachineInstr(ADD));
Vikram S. Adve8cfffd32002-08-24 20:56:53 +00001924 SetOperandsForMemInstr(mvec, subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001925 break;
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001926
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001927 case 57: // reg: Alloca: Implement as 1 instruction:
1928 { // add %fp, offsetFromFP -> result
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001929 AllocationInst* instr =
1930 cast<AllocationInst>(subtreeRoot->getInstruction());
1931 unsigned int tsize =
1932 target.findOptimalStorageSize(instr->getAllocatedType());
Vikram S. Adve74825322002-03-18 03:15:35 +00001933 assert(tsize != 0);
1934 CreateCodeForFixedSizeAlloca(target, instr, tsize, 1, mvec);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001935 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001936 }
Vikram S. Adve74825322002-03-18 03:15:35 +00001937
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001938 case 58: // reg: Alloca(reg): Implement as 3 instructions:
1939 // mul num, typeSz -> tmp
1940 // sub %sp, tmp -> %sp
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001941 { // add %sp, frameSizeBelowDynamicArea -> result
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001942 AllocationInst* instr =
1943 cast<AllocationInst>(subtreeRoot->getInstruction());
Vikram S. Adve74825322002-03-18 03:15:35 +00001944 const Type* eltType = instr->getAllocatedType();
1945
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001946 // If #elements is constant, use simpler code for fixed-size allocas
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001947 int tsize = (int) target.findOptimalStorageSize(eltType);
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001948 Value* numElementsVal = NULL;
1949 bool isArray = instr->isArrayAllocation();
1950
1951 if (!isArray ||
1952 isa<Constant>(numElementsVal = instr->getArraySize()))
1953 { // total size is constant: generate code for fixed-size alloca
1954 unsigned int numElements = isArray?
1955 cast<ConstantUInt>(numElementsVal)->getValue() : 1;
1956 CreateCodeForFixedSizeAlloca(target, instr, tsize,
1957 numElements, mvec);
1958 }
Vikram S. Adve74825322002-03-18 03:15:35 +00001959 else // total size is not constant.
1960 CreateCodeForVariableSizeAlloca(target, instr, tsize,
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001961 numElementsVal, mvec);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001962 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001963 }
Vikram S. Adve74825322002-03-18 03:15:35 +00001964
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001965 case 61: // reg: Call
Vikram S. Advedbc4fad2002-04-25 04:37:51 +00001966 { // Generate a direct (CALL) or indirect (JMPL). depending
1967 // Mark the return-address register and the indirection
1968 // register (if any) as hidden virtual registers.
Vikram S. Advea995e602001-10-11 04:23:19 +00001969 // Also, mark the operands of the Call and return value (if
1970 // any) as implicit operands of the CALL machine instruction.
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001971 //
Vikram S. Advedbc4fad2002-04-25 04:37:51 +00001972 // If this is a varargs function, floating point arguments
1973 // have to passed in integer registers so insert
1974 // copy-float-to-int instructions for each float operand.
1975 //
Chris Lattnerb00c5822001-10-02 03:41:24 +00001976 CallInst *callInstr = cast<CallInst>(subtreeRoot->getInstruction());
Chris Lattner749655f2001-10-13 06:54:30 +00001977 Value *callee = callInstr->getCalledValue();
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001978
Vikram S. Advea10d1a72002-03-31 19:07:35 +00001979 // Create hidden virtual register for return address, with type void*.
Vikram S. Adve242a8082002-05-19 15:25:51 +00001980 TmpInstruction* retAddrReg =
Vikram S. Advea10d1a72002-03-31 19:07:35 +00001981 new TmpInstruction(PointerType::get(Type::VoidTy), callInstr);
Chris Lattner9c461082002-02-03 07:50:56 +00001982 MachineCodeForInstruction::get(callInstr).addTemp(retAddrReg);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001983
Vikram S. Adveea21a6c2001-10-20 20:57:06 +00001984 // Generate the machine instruction and its operands.
1985 // Use CALL for direct function calls; this optimistically assumes
1986 // the PC-relative address fits in the CALL address field (22 bits).
1987 // Use JMPL for indirect calls.
1988 //
Chris Lattnerb0d04722002-03-26 17:58:12 +00001989 if (isa<Function>(callee))
Vikram S. Adveea21a6c2001-10-20 20:57:06 +00001990 { // direct function call
Vikram S. Adve74825322002-03-18 03:15:35 +00001991 M = new MachineInstr(CALL);
1992 M->SetMachineOperandVal(0, MachineOperand::MO_PCRelativeDisp,
1993 callee);
Vikram S. Adveea21a6c2001-10-20 20:57:06 +00001994 }
1995 else
1996 { // indirect function call
Vikram S. Adve74825322002-03-18 03:15:35 +00001997 M = new MachineInstr(JMPLCALL);
1998 M->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister,
1999 callee);
2000 M->SetMachineOperandConst(1, MachineOperand::MO_SignExtendedImmed,
2001 (int64_t) 0);
2002 M->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister,
2003 retAddrReg);
Vikram S. Adveea21a6c2001-10-20 20:57:06 +00002004 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002005
Vikram S. Adve74825322002-03-18 03:15:35 +00002006 mvec.push_back(M);
Vikram S. Advea10d1a72002-03-31 19:07:35 +00002007
Vikram S. Adve242a8082002-05-19 15:25:51 +00002008 const FunctionType* funcType =
2009 cast<FunctionType>(cast<PointerType>(callee->getType())
2010 ->getElementType());
2011 bool isVarArgs = funcType->isVarArg();
2012 bool noPrototype = isVarArgs && funcType->getNumParams() == 0;
Vikram S. Advedbc4fad2002-04-25 04:37:51 +00002013
Vikram S. Adve242a8082002-05-19 15:25:51 +00002014 // Use an annotation to pass information about call arguments
2015 // to the register allocator.
2016 CallArgsDescriptor* argDesc = new CallArgsDescriptor(callInstr,
2017 retAddrReg, isVarArgs, noPrototype);
2018 M->addAnnotation(argDesc);
Vikram S. Advea995e602001-10-11 04:23:19 +00002019
Vikram S. Adve242a8082002-05-19 15:25:51 +00002020 assert(callInstr->getOperand(0) == callee
2021 && "This is assumed in the loop below!");
2022
2023 for (unsigned i=1, N=callInstr->getNumOperands(); i < N; ++i)
2024 {
2025 Value* argVal = callInstr->getOperand(i);
2026 Instruction* intArgReg = NULL;
2027
2028 // Check for FP arguments to varargs functions.
2029 // Any such argument in the first $K$ args must be passed in an
2030 // integer register, where K = #integer argument registers.
2031 if (isVarArgs && argVal->getType()->isFloatingPoint())
2032 {
2033 // If it is a function with no prototype, pass value
2034 // as an FP value as well as a varargs value
2035 if (noPrototype)
2036 argDesc->getArgInfo(i-1).setUseFPArgReg();
2037
2038 // If this arg. is in the first $K$ regs, add a copy
2039 // float-to-int instruction to pass the value as an integer.
2040 if (i < target.getRegInfo().GetNumOfIntArgRegs())
2041 {
2042 MachineCodeForInstruction &destMCFI =
2043 MachineCodeForInstruction::get(callInstr);
2044 intArgReg = new TmpInstruction(Type::IntTy, argVal);
2045 destMCFI.addTemp(intArgReg);
2046
2047 vector<MachineInstr*> copyMvec;
2048 target.getInstrInfo().CreateCodeToCopyFloatToInt(target,
2049 callInstr->getParent()->getParent(),
2050 argVal, (TmpInstruction*) intArgReg,
2051 copyMvec, destMCFI);
2052 mvec.insert(mvec.begin(),copyMvec.begin(),copyMvec.end());
2053
2054 argDesc->getArgInfo(i-1).setUseIntArgReg();
2055 argDesc->getArgInfo(i-1).setArgCopy(intArgReg);
2056 }
2057 else
2058 // Cannot fit in first $K$ regs so pass the arg on the stack
2059 argDesc->getArgInfo(i-1).setUseStackSlot();
2060 }
2061
2062 if (intArgReg)
2063 mvec.back()->addImplicitRef(intArgReg);
2064
2065 mvec.back()->addImplicitRef(argVal);
2066 }
2067
2068 // Add the return value as an implicit ref. The call operands
2069 // were added above.
Vikram S. Adveea21a6c2001-10-20 20:57:06 +00002070 if (callInstr->getType() != Type::VoidTy)
Vikram S. Adve74825322002-03-18 03:15:35 +00002071 mvec.back()->addImplicitRef(callInstr, /*isDef*/ true);
Vikram S. Advea995e602001-10-11 04:23:19 +00002072
Vikram S. Adveea21a6c2001-10-20 20:57:06 +00002073 // For the CALL instruction, the ret. addr. reg. is also implicit
Chris Lattnerb0d04722002-03-26 17:58:12 +00002074 if (isa<Function>(callee))
Vikram S. Adve74825322002-03-18 03:15:35 +00002075 mvec.back()->addImplicitRef(retAddrReg, /*isDef*/ true);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002076
Vikram S. Adve74825322002-03-18 03:15:35 +00002077 // delay slot
2078 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002079 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00002080 }
Vikram S. Adve242a8082002-05-19 15:25:51 +00002081
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002082 case 62: // reg: Shl(reg, reg)
Vikram S. Adve242a8082002-05-19 15:25:51 +00002083 {
2084 Value* argVal1 = subtreeRoot->leftChild()->getValue();
2085 Value* argVal2 = subtreeRoot->rightChild()->getValue();
2086 Instruction* shlInstr = subtreeRoot->getInstruction();
2087
2088 const Type* opType = argVal1->getType();
Chris Lattner0c4e8862002-09-03 01:08:28 +00002089 assert((opType->isInteger() || isa<PointerType>(opType)) &&
2090 "Shl unsupported for other types");
Vikram S. Adve242a8082002-05-19 15:25:51 +00002091
2092 CreateShiftInstructions(target, shlInstr->getParent()->getParent(),
2093 (opType == Type::LongTy)? SLLX : SLL,
2094 argVal1, argVal2, 0, shlInstr, mvec,
2095 MachineCodeForInstruction::get(shlInstr));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002096 break;
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00002097 }
2098
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002099 case 63: // reg: Shr(reg, reg)
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00002100 { const Type* opType = subtreeRoot->leftChild()->getValue()->getType();
Chris Lattner0c4e8862002-09-03 01:08:28 +00002101 assert((opType->isInteger() || isa<PointerType>(opType)) &&
2102 "Shr unsupported for other types");
Vikram S. Adve74825322002-03-18 03:15:35 +00002103 mvec.push_back(new MachineInstr((opType->isSigned()
2104 ? ((opType == Type::LongTy)? SRAX : SRA)
2105 : ((opType == Type::LongTy)? SRLX : SRL))));
2106 Set3OperandsFromInstr(mvec.back(), subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002107 break;
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00002108 }
2109
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002110 case 64: // reg: Phi(reg,reg)
Vikram S. Adve74825322002-03-18 03:15:35 +00002111 break; // don't forward the value
2112
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002113 case 71: // reg: VReg
2114 case 72: // reg: Constant
Vikram S. Adve74825322002-03-18 03:15:35 +00002115 break; // don't forward the value
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002116
2117 default:
2118 assert(0 && "Unrecognized BURG rule");
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002119 break;
2120 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00002121 }
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00002122
Chris Lattner20b1ea02001-09-14 03:47:57 +00002123 if (forwardOperandNum >= 0)
2124 { // We did not generate a machine instruction but need to use operand.
2125 // If user is in the same tree, replace Value in its machine operand.
2126 // If not, insert a copy instruction which should get coalesced away
2127 // by register allocation.
2128 if (subtreeRoot->parent() != NULL)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002129 ForwardOperand(subtreeRoot, subtreeRoot->parent(), forwardOperandNum);
Chris Lattner20b1ea02001-09-14 03:47:57 +00002130 else
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002131 {
Vikram S. Adve7fe27872001-10-18 00:26:20 +00002132 vector<MachineInstr*> minstrVec;
Vikram S. Adve242a8082002-05-19 15:25:51 +00002133 Instruction* instr = subtreeRoot->getInstruction();
2134 target.getInstrInfo().
2135 CreateCopyInstructionsByType(target,
2136 instr->getParent()->getParent(),
2137 instr->getOperand(forwardOperandNum),
2138 instr, minstrVec,
2139 MachineCodeForInstruction::get(instr));
Vikram S. Adve7fe27872001-10-18 00:26:20 +00002140 assert(minstrVec.size() > 0);
Vikram S. Adve74825322002-03-18 03:15:35 +00002141 mvec.insert(mvec.end(), minstrVec.begin(), minstrVec.end());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002142 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00002143 }
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00002144
2145 if (maskUnsignedResult)
2146 { // If result is unsigned and smaller than int reg size,
2147 // we need to clear high bits of result value.
2148 assert(forwardOperandNum < 0 && "Need mask but no instruction generated");
2149 Instruction* dest = subtreeRoot->getInstruction();
Vikram S. Adve8cfffd32002-08-24 20:56:53 +00002150 if (dest->getType()->isUnsigned())
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00002151 {
2152 unsigned destSize = target.DataLayout.getTypeSize(dest->getType());
2153 if (destSize < target.DataLayout.getIntegerRegize())
2154 { // Mask high bits. Use a TmpInstruction to represent the
2155 // intermediate result before masking. Since those instructions
2156 // have already been generated, go back and substitute tmpI
2157 // for dest in the result position of each one of them.
2158 TmpInstruction *tmpI = new TmpInstruction(dest->getType(), dest,
2159 NULL, "maskHi");
2160 MachineCodeForInstruction::get(dest).addTemp(tmpI);
2161
2162 for (unsigned i=0, N=mvec.size(); i < N; ++i)
2163 mvec[i]->substituteValue(dest, tmpI);
2164
2165 M = Create3OperandInstr(AND, tmpI,
2166 ConstantUInt::get(Type::ULongTy,
2167 ((uint64_t) 1 << 8*destSize) - 1),
2168 dest);
2169 mvec.push_back(M);
2170 }
2171 }
2172 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00002173}