blob: ff1a9fefe605373dd7b93ebf2f322aade1e81044 [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
Vikram S. Adve74825322002-03-18 03:15:35 +000030static void SetMemOperands_Internal (vector<MachineInstr*>& mvec,
31 vector<MachineInstr*>::iterator mvecI,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +000032 const InstructionNode* vmInstrNode,
33 Value* ptrVal,
Vikram S. Advefd3900a2002-03-24 03:33:02 +000034 std::vector<Value*>& idxVec,
Vikram S. Adve242a8082002-05-19 15:25:51 +000035 bool allConstantIndices,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +000036 const TargetMachine& target);
Chris Lattner20b1ea02001-09-14 03:47:57 +000037
38
39//************************ Internal Functions ******************************/
40
Chris Lattner20b1ea02001-09-14 03:47:57 +000041
Chris Lattner20b1ea02001-09-14 03:47:57 +000042static inline MachineOpCode
43ChooseBprInstruction(const InstructionNode* instrNode)
44{
45 MachineOpCode opCode;
46
47 Instruction* setCCInstr =
48 ((InstructionNode*) instrNode->leftChild())->getInstruction();
49
50 switch(setCCInstr->getOpcode())
51 {
52 case Instruction::SetEQ: opCode = BRZ; break;
53 case Instruction::SetNE: opCode = BRNZ; break;
54 case Instruction::SetLE: opCode = BRLEZ; break;
55 case Instruction::SetGE: opCode = BRGEZ; break;
56 case Instruction::SetLT: opCode = BRLZ; break;
57 case Instruction::SetGT: opCode = BRGZ; break;
58 default:
59 assert(0 && "Unrecognized VM instruction!");
60 opCode = INVALID_OPCODE;
61 break;
62 }
63
64 return opCode;
65}
66
67
68static inline MachineOpCode
Chris Lattner20b1ea02001-09-14 03:47:57 +000069ChooseBpccInstruction(const InstructionNode* instrNode,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +000070 const BinaryOperator* setCCInstr)
Chris Lattner20b1ea02001-09-14 03:47:57 +000071{
72 MachineOpCode opCode = INVALID_OPCODE;
73
74 bool isSigned = setCCInstr->getOperand(0)->getType()->isSigned();
75
76 if (isSigned)
77 {
78 switch(setCCInstr->getOpcode())
Vikram S. Adve4cecdd22001-10-01 00:12:53 +000079 {
80 case Instruction::SetEQ: opCode = BE; break;
81 case Instruction::SetNE: opCode = BNE; break;
82 case Instruction::SetLE: opCode = BLE; break;
83 case Instruction::SetGE: opCode = BGE; break;
84 case Instruction::SetLT: opCode = BL; break;
85 case Instruction::SetGT: opCode = BG; break;
86 default:
87 assert(0 && "Unrecognized VM instruction!");
88 break;
89 }
Chris Lattner20b1ea02001-09-14 03:47:57 +000090 }
91 else
92 {
93 switch(setCCInstr->getOpcode())
Vikram S. Adve4cecdd22001-10-01 00:12:53 +000094 {
95 case Instruction::SetEQ: opCode = BE; break;
96 case Instruction::SetNE: opCode = BNE; break;
97 case Instruction::SetLE: opCode = BLEU; break;
98 case Instruction::SetGE: opCode = BCC; break;
99 case Instruction::SetLT: opCode = BCS; break;
100 case Instruction::SetGT: opCode = BGU; break;
101 default:
102 assert(0 && "Unrecognized VM instruction!");
103 break;
104 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000105 }
106
107 return opCode;
108}
109
110static inline MachineOpCode
111ChooseBFpccInstruction(const InstructionNode* instrNode,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000112 const BinaryOperator* setCCInstr)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000113{
114 MachineOpCode opCode = INVALID_OPCODE;
115
116 switch(setCCInstr->getOpcode())
117 {
118 case Instruction::SetEQ: opCode = FBE; break;
119 case Instruction::SetNE: opCode = FBNE; break;
120 case Instruction::SetLE: opCode = FBLE; break;
121 case Instruction::SetGE: opCode = FBGE; break;
122 case Instruction::SetLT: opCode = FBL; break;
123 case Instruction::SetGT: opCode = FBG; break;
124 default:
125 assert(0 && "Unrecognized VM instruction!");
126 break;
127 }
128
129 return opCode;
130}
131
132
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000133// Create a unique TmpInstruction for a boolean value,
134// representing the CC register used by a branch on that value.
135// For now, hack this using a little static cache of TmpInstructions.
136// Eventually the entire BURG instruction selection should be put
137// into a separate class that can hold such information.
Vikram S. Adveff5a09e2001-11-08 05:04:09 +0000138// The static cache is not too bad because the memory for these
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000139// TmpInstructions will be freed along with the rest of the Function anyway.
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000140//
141static TmpInstruction*
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000142GetTmpForCC(Value* boolVal, const Function *F, const Type* ccType)
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000143{
Chris Lattner09ff1122002-07-24 21:21:32 +0000144 typedef hash_map<const Value*, TmpInstruction*> BoolTmpCache;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000145 static BoolTmpCache boolToTmpCache; // Map boolVal -> TmpInstruction*
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000146 static const Function *lastFunction = 0;// Use to flush cache between funcs
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000147
148 assert(boolVal->getType() == Type::BoolTy && "Weird but ok! Delete assert");
149
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000150 if (lastFunction != F)
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000151 {
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000152 lastFunction = F;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000153 boolToTmpCache.clear();
154 }
155
Vikram S. Adveff5a09e2001-11-08 05:04:09 +0000156 // Look for tmpI and create a new one otherwise. The new value is
157 // directly written to map using the ref returned by operator[].
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000158 TmpInstruction*& tmpI = boolToTmpCache[boolVal];
159 if (tmpI == NULL)
Chris Lattner9c461082002-02-03 07:50:56 +0000160 tmpI = new TmpInstruction(ccType, boolVal);
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000161
162 return tmpI;
163}
164
165
Chris Lattner20b1ea02001-09-14 03:47:57 +0000166static inline MachineOpCode
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000167ChooseBccInstruction(const InstructionNode* instrNode,
168 bool& isFPBranch)
169{
170 InstructionNode* setCCNode = (InstructionNode*) instrNode->leftChild();
171 BinaryOperator* setCCInstr = (BinaryOperator*) setCCNode->getInstruction();
172 const Type* setCCType = setCCInstr->getOperand(0)->getType();
173
Vikram S. Adve242a8082002-05-19 15:25:51 +0000174 isFPBranch = setCCType->isFloatingPoint(); // Return value: don't delete!
175
176 if (isFPBranch)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000177 return ChooseBFpccInstruction(instrNode, setCCInstr);
178 else
179 return ChooseBpccInstruction(instrNode, setCCInstr);
180}
181
182
183static inline MachineOpCode
Chris Lattner20b1ea02001-09-14 03:47:57 +0000184ChooseMovFpccInstruction(const InstructionNode* instrNode)
185{
186 MachineOpCode opCode = INVALID_OPCODE;
187
188 switch(instrNode->getInstruction()->getOpcode())
189 {
190 case Instruction::SetEQ: opCode = MOVFE; break;
191 case Instruction::SetNE: opCode = MOVFNE; break;
192 case Instruction::SetLE: opCode = MOVFLE; break;
193 case Instruction::SetGE: opCode = MOVFGE; break;
194 case Instruction::SetLT: opCode = MOVFL; break;
195 case Instruction::SetGT: opCode = MOVFG; break;
196 default:
197 assert(0 && "Unrecognized VM instruction!");
198 break;
199 }
200
201 return opCode;
202}
203
204
205// Assumes that SUBcc v1, v2 -> v3 has been executed.
206// In most cases, we want to clear v3 and then follow it by instruction
207// MOVcc 1 -> v3.
208// Set mustClearReg=false if v3 need not be cleared before conditional move.
209// Set valueToMove=0 if we want to conditionally move 0 instead of 1
210// (i.e., we want to test inverse of a condition)
Vikram S. Adve243dd452001-09-18 13:03:13 +0000211// (The latter two cases do not seem to arise because SetNE needs nothing.)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000212//
213static MachineOpCode
214ChooseMovpccAfterSub(const InstructionNode* instrNode,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000215 bool& mustClearReg,
216 int& valueToMove)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000217{
218 MachineOpCode opCode = INVALID_OPCODE;
219 mustClearReg = true;
220 valueToMove = 1;
221
222 switch(instrNode->getInstruction()->getOpcode())
223 {
Vikram S. Adve243dd452001-09-18 13:03:13 +0000224 case Instruction::SetEQ: opCode = MOVE; break;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000225 case Instruction::SetLE: opCode = MOVLE; break;
226 case Instruction::SetGE: opCode = MOVGE; break;
227 case Instruction::SetLT: opCode = MOVL; break;
228 case Instruction::SetGT: opCode = MOVG; break;
Vikram S. Adve243dd452001-09-18 13:03:13 +0000229 case Instruction::SetNE: assert(0 && "No move required!"); break;
230 default: assert(0 && "Unrecognized VM instr!"); break;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000231 }
232
233 return opCode;
234}
235
Chris Lattner20b1ea02001-09-14 03:47:57 +0000236static inline MachineOpCode
Vikram S. Advedbc4fad2002-04-25 04:37:51 +0000237ChooseConvertToFloatInstr(OpLabel vopCode, const Type* opType)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000238{
239 MachineOpCode opCode = INVALID_OPCODE;
240
Vikram S. Advedbc4fad2002-04-25 04:37:51 +0000241 switch(vopCode)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000242 {
243 case ToFloatTy:
244 if (opType == Type::SByteTy || opType == Type::ShortTy || opType == Type::IntTy)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000245 opCode = FITOS;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000246 else if (opType == Type::LongTy)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000247 opCode = FXTOS;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000248 else if (opType == Type::DoubleTy)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000249 opCode = FDTOS;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000250 else if (opType == Type::FloatTy)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000251 ;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000252 else
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000253 assert(0 && "Cannot convert this type to FLOAT on SPARC");
Chris Lattner20b1ea02001-09-14 03:47:57 +0000254 break;
255
256 case ToDoubleTy:
Vikram S. Adve74825322002-03-18 03:15:35 +0000257 // This is usually used in conjunction with CreateCodeToCopyIntToFloat().
258 // Both functions should treat the integer as a 32-bit value for types
259 // of 4 bytes or less, and as a 64-bit value otherwise.
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000260 if (opType == Type::SByteTy || opType == Type::UByteTy ||
261 opType == Type::ShortTy || opType == Type::UShortTy ||
262 opType == Type::IntTy || opType == Type::UIntTy)
Vikram S. Adve74825322002-03-18 03:15:35 +0000263 opCode = FITOD;
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000264 else if (opType == Type::LongTy || opType == Type::ULongTy)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000265 opCode = FXTOD;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000266 else if (opType == Type::FloatTy)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000267 opCode = FSTOD;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000268 else if (opType == Type::DoubleTy)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000269 ;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000270 else
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000271 assert(0 && "Cannot convert this type to DOUBLE on SPARC");
Chris Lattner20b1ea02001-09-14 03:47:57 +0000272 break;
273
274 default:
275 break;
276 }
277
278 return opCode;
279}
280
281static inline MachineOpCode
Vikram S. Adve1e606692002-07-31 21:01:34 +0000282ChooseConvertToIntInstr(Type::PrimitiveID tid, const Type* opType)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000283{
284 MachineOpCode opCode = INVALID_OPCODE;;
285
Vikram S. Adve1e606692002-07-31 21:01:34 +0000286 if (tid==Type::SByteTyID || tid==Type::ShortTyID || tid==Type::IntTyID ||
287 tid==Type::UByteTyID || tid==Type::UShortTyID || tid==Type::UIntTyID)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000288 {
289 switch (opType->getPrimitiveID())
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000290 {
Chris Lattner20b1ea02001-09-14 03:47:57 +0000291 case Type::FloatTyID: opCode = FSTOI; break;
292 case Type::DoubleTyID: opCode = FDTOI; break;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000293 default:
294 assert(0 && "Non-numeric non-bool type cannot be converted to Int");
295 break;
296 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000297 }
Vikram S. Adve1e606692002-07-31 21:01:34 +0000298 else if (tid==Type::LongTyID || tid==Type::ULongTyID)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000299 {
300 switch (opType->getPrimitiveID())
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000301 {
Chris Lattner20b1ea02001-09-14 03:47:57 +0000302 case Type::FloatTyID: opCode = FSTOX; break;
303 case Type::DoubleTyID: opCode = FDTOX; break;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000304 default:
305 assert(0 && "Non-numeric non-bool type cannot be converted to Long");
306 break;
307 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000308 }
309 else
310 assert(0 && "Should not get here, Mo!");
311
312 return opCode;
313}
314
Vikram S. Advedbc4fad2002-04-25 04:37:51 +0000315MachineInstr*
Vikram S. Adve1e606692002-07-31 21:01:34 +0000316CreateConvertToIntInstr(Type::PrimitiveID destTID, Value* srcVal,Value* destVal)
Vikram S. Advedbc4fad2002-04-25 04:37:51 +0000317{
Vikram S. Adve1e606692002-07-31 21:01:34 +0000318 MachineOpCode opCode = ChooseConvertToIntInstr(destTID, srcVal->getType());
Vikram S. Advedbc4fad2002-04-25 04:37:51 +0000319 assert(opCode != INVALID_OPCODE && "Expected to need conversion!");
320
321 MachineInstr* M = new MachineInstr(opCode);
322 M->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister, srcVal);
323 M->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister, destVal);
324 return M;
325}
Chris Lattner20b1ea02001-09-14 03:47:57 +0000326
Vikram S. Adve1e606692002-07-31 21:01:34 +0000327// CreateCodeToConvertIntToFloat: Convert FP value to signed or unsigned integer
328// The FP value must be converted to the dest type in an FP register,
329// and the result is then copied from FP to int register via memory.
330static void
331CreateCodeToConvertIntToFloat (const TargetMachine& target,
332 Value* opVal,
333 Instruction* destI,
334 std::vector<MachineInstr*>& mvec,
335 MachineCodeForInstruction& mcfi)
336{
337 // Create a temporary to represent the FP register into which the
338 // int value will placed after conversion. The type of this temporary
339 // depends on the type of FP register to use: single-prec for a 32-bit
340 // int or smaller; double-prec for a 64-bit int.
341 //
342 const Type* destTypeToUse = (destI->getType() == Type::LongTy)? Type::DoubleTy
343 : Type::FloatTy;
344 Value* destForCast = new TmpInstruction(destTypeToUse, opVal);
345 mcfi.addTemp(destForCast);
346
347 // Create the fp-to-int conversion code
348 MachineInstr* M = CreateConvertToIntInstr(destI->getType()->getPrimitiveID(),
349 opVal, destForCast);
350 mvec.push_back(M);
351
352 // Create the fpreg-to-intreg copy code
353 target.getInstrInfo().
354 CreateCodeToCopyFloatToInt(target, destI->getParent()->getParent(),
355 (TmpInstruction*)destForCast, destI, mvec, mcfi);
356}
357
358
Chris Lattner20b1ea02001-09-14 03:47:57 +0000359static inline MachineOpCode
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000360ChooseAddInstruction(const InstructionNode* instrNode)
361{
362 return ChooseAddInstructionByType(instrNode->getInstruction()->getType());
363}
364
365
Chris Lattner20b1ea02001-09-14 03:47:57 +0000366static inline MachineInstr*
367CreateMovFloatInstruction(const InstructionNode* instrNode,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000368 const Type* resultType)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000369{
370 MachineInstr* minstr = new MachineInstr((resultType == Type::FloatTy)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000371 ? FMOVS : FMOVD);
Vikram S. Adve74825322002-03-18 03:15:35 +0000372 minstr->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister,
373 instrNode->leftChild()->getValue());
374 minstr->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister,
375 instrNode->getValue());
Chris Lattner20b1ea02001-09-14 03:47:57 +0000376 return minstr;
377}
378
379static inline MachineInstr*
380CreateAddConstInstruction(const InstructionNode* instrNode)
381{
382 MachineInstr* minstr = NULL;
383
384 Value* constOp = ((InstrTreeNode*) instrNode->rightChild())->getValue();
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000385 assert(isa<Constant>(constOp));
Chris Lattner20b1ea02001-09-14 03:47:57 +0000386
387 // Cases worth optimizing are:
388 // (1) Add with 0 for float or double: use an FMOV of appropriate type,
389 // instead of an FADD (1 vs 3 cycles). There is no integer MOV.
390 //
Chris Lattner9b625032002-05-06 16:15:30 +0000391 if (ConstantFP *FPC = dyn_cast<ConstantFP>(constOp)) {
392 double dval = FPC->getValue();
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000393 if (dval == 0.0)
Vikram S. Adve242a8082002-05-19 15:25:51 +0000394 minstr = CreateMovFloatInstruction(instrNode,
395 instrNode->getInstruction()->getType());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000396 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000397
398 return minstr;
399}
400
401
402static inline MachineOpCode
Vikram S. Adve510eec72001-11-04 21:59:14 +0000403ChooseSubInstructionByType(const Type* resultType)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000404{
405 MachineOpCode opCode = INVALID_OPCODE;
406
Chris Lattner9b625032002-05-06 16:15:30 +0000407 if (resultType->isIntegral() || isa<PointerType>(resultType))
Chris Lattner20b1ea02001-09-14 03:47:57 +0000408 {
409 opCode = SUB;
410 }
411 else
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000412 switch(resultType->getPrimitiveID())
413 {
414 case Type::FloatTyID: opCode = FSUBS; break;
415 case Type::DoubleTyID: opCode = FSUBD; break;
416 default: assert(0 && "Invalid type for SUB instruction"); break;
417 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000418
419 return opCode;
420}
421
422
423static inline MachineInstr*
424CreateSubConstInstruction(const InstructionNode* instrNode)
425{
426 MachineInstr* minstr = NULL;
427
428 Value* constOp = ((InstrTreeNode*) instrNode->rightChild())->getValue();
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000429 assert(isa<Constant>(constOp));
Chris Lattner20b1ea02001-09-14 03:47:57 +0000430
431 // Cases worth optimizing are:
432 // (1) Sub with 0 for float or double: use an FMOV of appropriate type,
433 // instead of an FSUB (1 vs 3 cycles). There is no integer MOV.
434 //
Chris Lattner9b625032002-05-06 16:15:30 +0000435 if (ConstantFP *FPC = dyn_cast<ConstantFP>(constOp)) {
436 double dval = FPC->getValue();
437 if (dval == 0.0)
Vikram S. Adve242a8082002-05-19 15:25:51 +0000438 minstr = CreateMovFloatInstruction(instrNode,
439 instrNode->getInstruction()->getType());
Chris Lattner9b625032002-05-06 16:15:30 +0000440 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000441
442 return minstr;
443}
444
445
446static inline MachineOpCode
447ChooseFcmpInstruction(const InstructionNode* instrNode)
448{
449 MachineOpCode opCode = INVALID_OPCODE;
450
451 Value* operand = ((InstrTreeNode*) instrNode->leftChild())->getValue();
452 switch(operand->getType()->getPrimitiveID()) {
453 case Type::FloatTyID: opCode = FCMPS; break;
454 case Type::DoubleTyID: opCode = FCMPD; break;
455 default: assert(0 && "Invalid type for FCMP instruction"); break;
456 }
457
458 return opCode;
459}
460
461
462// Assumes that leftArg and rightArg are both cast instructions.
463//
464static inline bool
465BothFloatToDouble(const InstructionNode* instrNode)
466{
467 InstrTreeNode* leftArg = instrNode->leftChild();
468 InstrTreeNode* rightArg = instrNode->rightChild();
469 InstrTreeNode* leftArgArg = leftArg->leftChild();
470 InstrTreeNode* rightArgArg = rightArg->leftChild();
471 assert(leftArg->getValue()->getType() == rightArg->getValue()->getType());
472
473 // Check if both arguments are floats cast to double
474 return (leftArg->getValue()->getType() == Type::DoubleTy &&
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000475 leftArgArg->getValue()->getType() == Type::FloatTy &&
476 rightArgArg->getValue()->getType() == Type::FloatTy);
Chris Lattner20b1ea02001-09-14 03:47:57 +0000477}
478
479
480static inline MachineOpCode
Vikram S. Adve510eec72001-11-04 21:59:14 +0000481ChooseMulInstructionByType(const Type* resultType)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000482{
483 MachineOpCode opCode = INVALID_OPCODE;
484
Chris Lattner20b1ea02001-09-14 03:47:57 +0000485 if (resultType->isIntegral())
Vikram S. Adve510eec72001-11-04 21:59:14 +0000486 opCode = MULX;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000487 else
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000488 switch(resultType->getPrimitiveID())
489 {
490 case Type::FloatTyID: opCode = FMULS; break;
491 case Type::DoubleTyID: opCode = FMULD; break;
492 default: assert(0 && "Invalid type for MUL instruction"); break;
493 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000494
495 return opCode;
496}
497
498
Vikram S. Adve510eec72001-11-04 21:59:14 +0000499
Chris Lattner20b1ea02001-09-14 03:47:57 +0000500static inline MachineInstr*
Vikram S. Adve74825322002-03-18 03:15:35 +0000501CreateIntNegInstruction(const TargetMachine& target,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000502 Value* vreg)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000503{
504 MachineInstr* minstr = new MachineInstr(SUB);
Vikram S. Adve74825322002-03-18 03:15:35 +0000505 minstr->SetMachineOperandReg(0, target.getRegInfo().getZeroRegNum());
506 minstr->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister, vreg);
507 minstr->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister, vreg);
Chris Lattner20b1ea02001-09-14 03:47:57 +0000508 return minstr;
509}
510
511
Vikram S. Adve242a8082002-05-19 15:25:51 +0000512// Create instruction sequence for any shift operation.
513// SLL or SLLX on an operand smaller than the integer reg. size (64bits)
514// requires a second instruction for explicit sign-extension.
515// Note that we only have to worry about a sign-bit appearing in the
516// most significant bit of the operand after shifting (e.g., bit 32 of
517// Int or bit 16 of Short), so we do not have to worry about results
518// that are as large as a normal integer register.
519//
520static inline void
521CreateShiftInstructions(const TargetMachine& target,
522 Function* F,
523 MachineOpCode shiftOpCode,
524 Value* argVal1,
525 Value* optArgVal2, /* Use optArgVal2 if not NULL */
526 unsigned int optShiftNum, /* else use optShiftNum */
527 Instruction* destVal,
528 vector<MachineInstr*>& mvec,
529 MachineCodeForInstruction& mcfi)
530{
531 assert((optArgVal2 != NULL || optShiftNum <= 64) &&
532 "Large shift sizes unexpected, but can be handled below: "
533 "You need to check whether or not it fits in immed field below");
534
535 // If this is a logical left shift of a type smaller than the standard
536 // integer reg. size, we have to extend the sign-bit into upper bits
537 // of dest, so we need to put the result of the SLL into a temporary.
538 //
539 Value* shiftDest = destVal;
540 const Type* opType = argVal1->getType();
541 unsigned opSize = target.DataLayout.getTypeSize(argVal1->getType());
542 if ((shiftOpCode == SLL || shiftOpCode == SLLX)
543 && opSize < target.DataLayout.getIntegerRegize())
544 { // put SLL result into a temporary
545 shiftDest = new TmpInstruction(argVal1, optArgVal2, "sllTmp");
546 mcfi.addTemp(shiftDest);
547 }
548
549 MachineInstr* M = (optArgVal2 != NULL)
550 ? Create3OperandInstr(shiftOpCode, argVal1, optArgVal2, shiftDest)
551 : Create3OperandInstr_UImmed(shiftOpCode, argVal1, optShiftNum, shiftDest);
552 mvec.push_back(M);
553
554 if (shiftDest != destVal)
555 { // extend the sign-bit of the result into all upper bits of dest
556 assert(8*opSize <= 32 && "Unexpected type size > 4 and < IntRegSize?");
557 target.getInstrInfo().
558 CreateSignExtensionInstructions(target, F, shiftDest, 8*opSize,
559 destVal, mvec, mcfi);
560 }
561}
562
563
Vikram S. Adve74825322002-03-18 03:15:35 +0000564// Does not create any instructions if we cannot exploit constant to
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000565// create a cheaper instruction.
566// This returns the approximate cost of the instructions generated,
567// which is used to pick the cheapest when both operands are constant.
568static inline unsigned int
Vikram S. Adve242a8082002-05-19 15:25:51 +0000569CreateMulConstInstruction(const TargetMachine &target, Function* F,
570 Value* lval, Value* rval, Instruction* destVal,
571 vector<MachineInstr*>& mvec,
572 MachineCodeForInstruction& mcfi)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000573{
Vikram S. Adve242a8082002-05-19 15:25:51 +0000574 /* Use max. multiply cost, viz., cost of MULX */
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000575 unsigned int cost = target.getInstrInfo().minLatency(MULX);
Vikram S. Adve242a8082002-05-19 15:25:51 +0000576 unsigned int firstNewInstr = mvec.size();
Vikram S. Adve74825322002-03-18 03:15:35 +0000577
578 Value* constOp = rval;
579 if (! isa<Constant>(constOp))
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000580 return cost;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000581
582 // Cases worth optimizing are:
583 // (1) Multiply by 0 or 1 for any type: replace with copy (ADD or FMOV)
584 // (2) Multiply by 2^x for integer types: replace with Shift
585 //
Vikram S. Adve74825322002-03-18 03:15:35 +0000586 const Type* resultType = destVal->getType();
Chris Lattner20b1ea02001-09-14 03:47:57 +0000587
Chris Lattner9b625032002-05-06 16:15:30 +0000588 if (resultType->isIntegral() || isa<PointerType>(resultType))
Chris Lattner20b1ea02001-09-14 03:47:57 +0000589 {
Chris Lattner20b1ea02001-09-14 03:47:57 +0000590 bool isValidConst;
591 int64_t C = GetConstantValueAsSignedInt(constOp, isValidConst);
592 if (isValidConst)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000593 {
Vikram S. Adve242a8082002-05-19 15:25:51 +0000594 unsigned pow;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000595 bool needNeg = false;
596 if (C < 0)
597 {
598 needNeg = true;
599 C = -C;
600 }
601
602 if (C == 0 || C == 1)
603 {
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000604 cost = target.getInstrInfo().minLatency(ADD);
Vikram S. Adve242a8082002-05-19 15:25:51 +0000605 MachineInstr* M = (C == 0)
606 ? Create3OperandInstr_Reg(ADD,
607 target.getRegInfo().getZeroRegNum(),
608 target.getRegInfo().getZeroRegNum(),
609 destVal)
610 : Create3OperandInstr_Reg(ADD, lval,
611 target.getRegInfo().getZeroRegNum(),
612 destVal);
613 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000614 }
Chris Lattner36346c72002-05-19 21:20:19 +0000615 else if (isPowerOf2(C, pow))
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000616 {
Vikram S. Adve242a8082002-05-19 15:25:51 +0000617 unsigned int opSize = target.DataLayout.getTypeSize(resultType);
618 MachineOpCode opCode = (opSize <= 32)? SLL : SLLX;
619 CreateShiftInstructions(target, F, opCode, lval, NULL, pow,
620 destVal, mvec, mcfi);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000621 }
622
Vikram S. Adve242a8082002-05-19 15:25:51 +0000623 if (mvec.size() > 0 && needNeg)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000624 { // insert <reg = SUB 0, reg> after the instr to flip the sign
Vikram S. Adve242a8082002-05-19 15:25:51 +0000625 MachineInstr* M = CreateIntNegInstruction(target, destVal);
626 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000627 }
628 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000629 }
630 else
631 {
Chris Lattner9b625032002-05-06 16:15:30 +0000632 if (ConstantFP *FPC = dyn_cast<ConstantFP>(constOp))
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000633 {
Chris Lattner9b625032002-05-06 16:15:30 +0000634 double dval = FPC->getValue();
Vikram S. Adve6ad7c552001-11-09 02:18:16 +0000635 if (fabs(dval) == 1)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000636 {
Vikram S. Adve242a8082002-05-19 15:25:51 +0000637 MachineOpCode opCode = (dval < 0)
Vikram S. Adve6ad7c552001-11-09 02:18:16 +0000638 ? (resultType == Type::FloatTy? FNEGS : FNEGD)
639 : (resultType == Type::FloatTy? FMOVS : FMOVD);
Vikram S. Adve242a8082002-05-19 15:25:51 +0000640 MachineInstr* M = Create2OperandInstr(opCode, lval, destVal);
641 mvec.push_back(M);
Vikram S. Adve6ad7c552001-11-09 02:18:16 +0000642 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000643 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000644 }
645
Vikram S. Adve242a8082002-05-19 15:25:51 +0000646 if (firstNewInstr < mvec.size())
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000647 {
Vikram S. Adve242a8082002-05-19 15:25:51 +0000648 cost = 0;
649 for (unsigned int i=firstNewInstr; i < mvec.size(); ++i)
650 cost += target.getInstrInfo().minLatency(mvec[i]->getOpCode());
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000651 }
652
653 return cost;
Vikram S. Adve74825322002-03-18 03:15:35 +0000654}
655
656
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000657// Does not create any instructions if we cannot exploit constant to
658// create a cheaper instruction.
659//
660static inline void
661CreateCheapestMulConstInstruction(const TargetMachine &target,
Vikram S. Adve242a8082002-05-19 15:25:51 +0000662 Function* F,
663 Value* lval, Value* rval,
664 Instruction* destVal,
665 vector<MachineInstr*>& mvec,
666 MachineCodeForInstruction& mcfi)
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000667{
668 Value* constOp;
669 if (isa<Constant>(lval) && isa<Constant>(rval))
670 { // both operands are constant: try both orders!
671 vector<MachineInstr*> mvec1, mvec2;
Vikram S. Adve242a8082002-05-19 15:25:51 +0000672 unsigned int lcost = CreateMulConstInstruction(target, F, lval, rval,
673 destVal, mvec1, mcfi);
674 unsigned int rcost = CreateMulConstInstruction(target, F, rval, lval,
675 destVal, mvec2, mcfi);
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000676 vector<MachineInstr*>& mincostMvec = (lcost <= rcost)? mvec1 : mvec2;
677 vector<MachineInstr*>& maxcostMvec = (lcost <= rcost)? mvec2 : mvec1;
678 mvec.insert(mvec.end(), mincostMvec.begin(), mincostMvec.end());
679
680 for (unsigned int i=0; i < maxcostMvec.size(); ++i)
681 delete maxcostMvec[i];
682 }
683 else if (isa<Constant>(rval)) // rval is constant, but not lval
Vikram S. Adve242a8082002-05-19 15:25:51 +0000684 CreateMulConstInstruction(target, F, lval, rval, destVal, mvec, mcfi);
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000685 else if (isa<Constant>(lval)) // lval is constant, but not rval
Vikram S. Adve242a8082002-05-19 15:25:51 +0000686 CreateMulConstInstruction(target, F, lval, rval, destVal, mvec, mcfi);
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000687
688 // else neither is constant
689 return;
690}
691
Vikram S. Adve74825322002-03-18 03:15:35 +0000692// Return NULL if we cannot exploit constant to create a cheaper instruction
693static inline void
Vikram S. Adve242a8082002-05-19 15:25:51 +0000694CreateMulInstruction(const TargetMachine &target, Function* F,
695 Value* lval, Value* rval, Instruction* destVal,
Vikram S. Adve74825322002-03-18 03:15:35 +0000696 vector<MachineInstr*>& mvec,
Vikram S. Adve242a8082002-05-19 15:25:51 +0000697 MachineCodeForInstruction& mcfi,
Vikram S. Adve74825322002-03-18 03:15:35 +0000698 MachineOpCode forceMulOp = INVALID_MACHINE_OPCODE)
699{
700 unsigned int L = mvec.size();
Vikram S. Adve242a8082002-05-19 15:25:51 +0000701 CreateCheapestMulConstInstruction(target,F, lval, rval, destVal, mvec, mcfi);
Vikram S. Adve74825322002-03-18 03:15:35 +0000702 if (mvec.size() == L)
703 { // no instructions were added so create MUL reg, reg, reg.
704 // Use FSMULD if both operands are actually floats cast to doubles.
705 // Otherwise, use the default opcode for the appropriate type.
706 MachineOpCode mulOp = ((forceMulOp != INVALID_MACHINE_OPCODE)
707 ? forceMulOp
708 : ChooseMulInstructionByType(destVal->getType()));
709 MachineInstr* M = new MachineInstr(mulOp);
710 M->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister, lval);
711 M->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister, rval);
712 M->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister, destVal);
713 mvec.push_back(M);
714 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000715}
716
717
Vikram S. Adve510eec72001-11-04 21:59:14 +0000718// Generate a divide instruction for Div or Rem.
719// For Rem, this assumes that the operand type will be signed if the result
720// type is signed. This is correct because they must have the same sign.
721//
Chris Lattner20b1ea02001-09-14 03:47:57 +0000722static inline MachineOpCode
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000723ChooseDivInstruction(TargetMachine &target,
724 const InstructionNode* instrNode)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000725{
726 MachineOpCode opCode = INVALID_OPCODE;
727
728 const Type* resultType = instrNode->getInstruction()->getType();
729
730 if (resultType->isIntegral())
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000731 opCode = resultType->isSigned()? SDIVX : UDIVX;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000732 else
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000733 switch(resultType->getPrimitiveID())
734 {
735 case Type::FloatTyID: opCode = FDIVS; break;
736 case Type::DoubleTyID: opCode = FDIVD; break;
737 default: assert(0 && "Invalid type for DIV instruction"); break;
738 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000739
740 return opCode;
741}
742
743
Vikram S. Adve74825322002-03-18 03:15:35 +0000744// Return NULL if we cannot exploit constant to create a cheaper instruction
745static inline void
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000746CreateDivConstInstruction(TargetMachine &target,
747 const InstructionNode* instrNode,
Vikram S. Adve74825322002-03-18 03:15:35 +0000748 vector<MachineInstr*>& mvec)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000749{
Vikram S. Adve74825322002-03-18 03:15:35 +0000750 MachineInstr* minstr1 = NULL;
751 MachineInstr* minstr2 = NULL;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000752
753 Value* constOp = ((InstrTreeNode*) instrNode->rightChild())->getValue();
Vikram S. Adve74825322002-03-18 03:15:35 +0000754 if (! isa<Constant>(constOp))
755 return;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000756
757 // Cases worth optimizing are:
758 // (1) Divide by 1 for any type: replace with copy (ADD or FMOV)
759 // (2) Divide by 2^x for integer types: replace with SR[L or A]{X}
760 //
761 const Type* resultType = instrNode->getInstruction()->getType();
762
763 if (resultType->isIntegral())
764 {
765 unsigned pow;
766 bool isValidConst;
767 int64_t C = GetConstantValueAsSignedInt(constOp, isValidConst);
768 if (isValidConst)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000769 {
770 bool needNeg = false;
771 if (C < 0)
772 {
773 needNeg = true;
774 C = -C;
775 }
776
777 if (C == 1)
778 {
Vikram S. Adve74825322002-03-18 03:15:35 +0000779 minstr1 = new MachineInstr(ADD);
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000780 minstr1->SetMachineOperandVal(0,
781 MachineOperand::MO_VirtualRegister,
782 instrNode->leftChild()->getValue());
783 minstr1->SetMachineOperandReg(1,
784 target.getRegInfo().getZeroRegNum());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000785 }
Chris Lattner36346c72002-05-19 21:20:19 +0000786 else if (isPowerOf2(C, pow))
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000787 {
788 MachineOpCode opCode= ((resultType->isSigned())
789 ? (resultType==Type::LongTy)? SRAX : SRA
790 : (resultType==Type::LongTy)? SRLX : SRL);
Vikram S. Adve74825322002-03-18 03:15:35 +0000791 minstr1 = new MachineInstr(opCode);
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000792 minstr1->SetMachineOperandVal(0,
793 MachineOperand::MO_VirtualRegister,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000794 instrNode->leftChild()->getValue());
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000795 minstr1->SetMachineOperandConst(1,
796 MachineOperand::MO_UnextendedImmed,
797 pow);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000798 }
799
Vikram S. Adve74825322002-03-18 03:15:35 +0000800 if (minstr1 && needNeg)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000801 { // insert <reg = SUB 0, reg> after the instr to flip the sign
Vikram S. Adve74825322002-03-18 03:15:35 +0000802 minstr2 = CreateIntNegInstruction(target,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000803 instrNode->getValue());
804 }
805 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000806 }
807 else
808 {
Chris Lattner9b625032002-05-06 16:15:30 +0000809 if (ConstantFP *FPC = dyn_cast<ConstantFP>(constOp))
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000810 {
Chris Lattner9b625032002-05-06 16:15:30 +0000811 double dval = FPC->getValue();
Vikram S. Adve6ad7c552001-11-09 02:18:16 +0000812 if (fabs(dval) == 1)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000813 {
814 bool needNeg = (dval < 0);
815
816 MachineOpCode opCode = needNeg
817 ? (resultType == Type::FloatTy? FNEGS : FNEGD)
818 : (resultType == Type::FloatTy? FMOVS : FMOVD);
819
Vikram S. Adve74825322002-03-18 03:15:35 +0000820 minstr1 = new MachineInstr(opCode);
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000821 minstr1->SetMachineOperandVal(0,
822 MachineOperand::MO_VirtualRegister,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000823 instrNode->leftChild()->getValue());
824 }
825 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000826 }
827
Vikram S. Adve74825322002-03-18 03:15:35 +0000828 if (minstr1 != NULL)
829 minstr1->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister,
830 instrNode->getValue());
Chris Lattner20b1ea02001-09-14 03:47:57 +0000831
Vikram S. Adve74825322002-03-18 03:15:35 +0000832 if (minstr1)
833 mvec.push_back(minstr1);
834 if (minstr2)
835 mvec.push_back(minstr2);
Chris Lattner20b1ea02001-09-14 03:47:57 +0000836}
837
838
Vikram S. Adve74825322002-03-18 03:15:35 +0000839static void
840CreateCodeForVariableSizeAlloca(const TargetMachine& target,
841 Instruction* result,
842 unsigned int tsize,
843 Value* numElementsVal,
844 vector<MachineInstr*>& getMvec)
845{
846 MachineInstr* M;
847
848 // Create a Value to hold the (constant) element size
849 Value* tsizeVal = ConstantSInt::get(Type::IntTy, tsize);
850
851 // Get the constant offset from SP for dynamically allocated storage
852 // and create a temporary Value to hold it.
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000853 assert(result && result->getParent() && "Result value is not part of a fn?");
854 Function *F = result->getParent()->getParent();
855 MachineCodeForMethod& mcInfo = MachineCodeForMethod::get(F);
Vikram S. Adve74825322002-03-18 03:15:35 +0000856 bool growUp;
857 ConstantSInt* dynamicAreaOffset =
858 ConstantSInt::get(Type::IntTy,
859 target.getFrameInfo().getDynamicAreaOffset(mcInfo,growUp));
860 assert(! growUp && "Has SPARC v9 stack frame convention changed?");
861
862 // Create a temporary value to hold the result of MUL
863 TmpInstruction* tmpProd = new TmpInstruction(numElementsVal, tsizeVal);
864 MachineCodeForInstruction::get(result).addTemp(tmpProd);
865
866 // Instruction 1: mul numElements, typeSize -> tmpProd
867 M = new MachineInstr(MULX);
868 M->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister, numElementsVal);
869 M->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister, tsizeVal);
870 M->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister, tmpProd);
871 getMvec.push_back(M);
872
873 // Instruction 2: sub %sp, tmpProd -> %sp
874 M = new MachineInstr(SUB);
875 M->SetMachineOperandReg(0, target.getRegInfo().getStackPointer());
876 M->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister, tmpProd);
877 M->SetMachineOperandReg(2, target.getRegInfo().getStackPointer());
878 getMvec.push_back(M);
879
880 // Instruction 3: add %sp, frameSizeBelowDynamicArea -> result
881 M = new MachineInstr(ADD);
882 M->SetMachineOperandReg(0, target.getRegInfo().getStackPointer());
883 M->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister, dynamicAreaOffset);
884 M->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister, result);
885 getMvec.push_back(M);
886}
887
888
889static void
890CreateCodeForFixedSizeAlloca(const TargetMachine& target,
891 Instruction* result,
892 unsigned int tsize,
893 unsigned int numElements,
894 vector<MachineInstr*>& getMvec)
895{
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000896 assert(result && result->getParent() &&
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000897 "Result value is not part of a function?");
898 Function *F = result->getParent()->getParent();
899 MachineCodeForMethod &mcInfo = MachineCodeForMethod::get(F);
Vikram S. Adve74825322002-03-18 03:15:35 +0000900
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000901 // Check if the offset would small enough to use as an immediate in
902 // load/stores (check LDX because all load/stores have the same-size immediate
903 // field). If not, put the variable in the dynamically sized area of the
904 // frame.
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000905 unsigned int paddedSizeIgnored;
Vikram S. Adve74825322002-03-18 03:15:35 +0000906 int offsetFromFP = mcInfo.computeOffsetforLocalVar(target, result,
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000907 paddedSizeIgnored,
Vikram S. Adve74825322002-03-18 03:15:35 +0000908 tsize * numElements);
909 if (! target.getInstrInfo().constantFitsInImmedField(LDX, offsetFromFP))
910 {
911 CreateCodeForVariableSizeAlloca(target, result, tsize,
912 ConstantSInt::get(Type::IntTy,numElements),
913 getMvec);
914 return;
915 }
916
917 // else offset fits in immediate field so go ahead and allocate it.
918 offsetFromFP = mcInfo.allocateLocalVar(target, result, tsize * numElements);
919
920 // Create a temporary Value to hold the constant offset.
921 // This is needed because it may not fit in the immediate field.
922 ConstantSInt* offsetVal = ConstantSInt::get(Type::IntTy, offsetFromFP);
923
924 // Instruction 1: add %fp, offsetFromFP -> result
925 MachineInstr* M = new MachineInstr(ADD);
926 M->SetMachineOperandReg(0, target.getRegInfo().getFramePointer());
927 M->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister, offsetVal);
928 M->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister, result);
929
930 getMvec.push_back(M);
931}
932
933
934
Vikram S. Adveed3fefb2002-08-03 13:48:21 +0000935// Check for a constant (uint) 0.
936inline bool
937IsZero(Value* idx)
938{
939 return (isa<ConstantInt>(idx) && cast<ConstantInt>(idx)->isNullValue());
940}
Vikram S. Adve242a8082002-05-19 15:25:51 +0000941
942
Chris Lattner20b1ea02001-09-14 03:47:57 +0000943//------------------------------------------------------------------------
944// Function SetOperandsForMemInstr
945//
946// Choose addressing mode for the given load or store instruction.
947// Use [reg+reg] if it is an indexed reference, and the index offset is
948// not a constant or if it cannot fit in the offset field.
949// Use [reg+offset] in all other cases.
950//
951// This assumes that all array refs are "lowered" to one of these forms:
952// %x = load (subarray*) ptr, constant ; single constant offset
953// %x = load (subarray*) ptr, offsetVal ; single non-constant offset
954// Generally, this should happen via strength reduction + LICM.
955// Also, strength reduction should take care of using the same register for
956// the loop index variable and an array index, when that is profitable.
957//------------------------------------------------------------------------
958
959static void
Vikram S. Adve74825322002-03-18 03:15:35 +0000960SetOperandsForMemInstr(vector<MachineInstr*>& mvec,
961 vector<MachineInstr*>::iterator mvecI,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000962 const InstructionNode* vmInstrNode,
963 const TargetMachine& target)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000964{
965 MemAccessInst* memInst = (MemAccessInst*) vmInstrNode->getInstruction();
966
Vikram S. Adve242a8082002-05-19 15:25:51 +0000967 // Variables to hold the index vector and ptr value.
Chris Lattner20b1ea02001-09-14 03:47:57 +0000968 // The major work here is to extract these for all 3 instruction types
Vikram S. Adve242a8082002-05-19 15:25:51 +0000969 // and to try to fold chains of constant indices into a single offset.
970 // After that, we call SetMemOperands_Internal(), which creates the
971 // appropriate operands for the machine instruction.
Vikram S. Advea10d1a72002-03-31 19:07:35 +0000972 vector<Value*> idxVec;
Vikram S. Adve242a8082002-05-19 15:25:51 +0000973 bool allConstantIndices = true;
974 Value* ptrVal = memInst->getPointerOperand();
Vikram S. Adveed3fefb2002-08-03 13:48:21 +0000975
Vikram S. Advea10d1a72002-03-31 19:07:35 +0000976 // If there is a GetElemPtr instruction to fold in to this instr,
977 // it must be in the left child for Load and GetElemPtr, and in the
978 // right child for Store instructions.
Chris Lattner20b1ea02001-09-14 03:47:57 +0000979 InstrTreeNode* ptrChild = (vmInstrNode->getOpLabel() == Instruction::Store
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000980 ? vmInstrNode->rightChild()
981 : vmInstrNode->leftChild());
Vikram S. Adveed3fefb2002-08-03 13:48:21 +0000982
Vikram S. Adve242a8082002-05-19 15:25:51 +0000983 // Check if all indices are constant for this instruction
Vikram S. Adveed3fefb2002-08-03 13:48:21 +0000984 for (MemAccessInst::op_iterator OI=memInst->idx_begin(),OE=memInst->idx_end();
985 allConstantIndices && OI != OE; ++OI)
986 if (! isa<Constant>(*OI))
987 allConstantIndices = false;
988
Vikram S. Adve242a8082002-05-19 15:25:51 +0000989 // If we have only constant indices, fold chains of constant indices
990 // in this and any preceding GetElemPtr instructions.
Vikram S. Adve99d4a382002-08-04 20:51:05 +0000991 bool foldedGEPs = false;
Vikram S. Adve242a8082002-05-19 15:25:51 +0000992 if (allConstantIndices &&
Vikram S. Adveed3fefb2002-08-03 13:48:21 +0000993 (ptrChild->getOpLabel() == Instruction::GetElementPtr ||
994 ptrChild->getOpLabel() == GetElemPtrIdx))
Vikram S. Adve99d4a382002-08-04 20:51:05 +0000995 if (Value* newPtr = FoldGetElemChain((InstructionNode*) ptrChild, idxVec)) {
Vikram S. Adveed3fefb2002-08-03 13:48:21 +0000996 ptrVal = newPtr;
Vikram S. Adve99d4a382002-08-04 20:51:05 +0000997 foldedGEPs = true;
998 }
Vikram S. Adveed3fefb2002-08-03 13:48:21 +0000999
Vikram S. Adve242a8082002-05-19 15:25:51 +00001000 // Append the index vector of the current instruction, if any.
Vikram S. Adve99d4a382002-08-04 20:51:05 +00001001 // Skip the leading [0] index if preceding GEPs were folded into this.
1002 if (memInst->getNumIndices() > 0) {
1003 assert((!foldedGEPs || IsZero(*memInst->idx_begin())) && "1st index not 0");
Chris Lattner75ac4e52002-08-03 20:57:38 +00001004 idxVec.insert(idxVec.end(),
Vikram S. Adve99d4a382002-08-04 20:51:05 +00001005 memInst->idx_begin() + foldedGEPs, memInst->idx_end());
1006 }
Vikram S. Adveed3fefb2002-08-03 13:48:21 +00001007
Vikram S. Adve242a8082002-05-19 15:25:51 +00001008 // Now create the appropriate operands for the machine instruction
1009 SetMemOperands_Internal(mvec, mvecI, vmInstrNode,
1010 ptrVal, idxVec, allConstantIndices, target);
Chris Lattner20b1ea02001-09-14 03:47:57 +00001011}
1012
1013
Vikram S. Adve74825322002-03-18 03:15:35 +00001014// Generate the correct operands (and additional instructions if needed)
1015// for the given pointer and given index vector.
1016//
Chris Lattner20b1ea02001-09-14 03:47:57 +00001017static void
Vikram S. Adve74825322002-03-18 03:15:35 +00001018SetMemOperands_Internal(vector<MachineInstr*>& mvec,
1019 vector<MachineInstr*>::iterator mvecI,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001020 const InstructionNode* vmInstrNode,
1021 Value* ptrVal,
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001022 vector<Value*>& idxVec,
Vikram S. Adve242a8082002-05-19 15:25:51 +00001023 bool allConstantIndices,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001024 const TargetMachine& target)
Chris Lattner20b1ea02001-09-14 03:47:57 +00001025{
1026 MemAccessInst* memInst = (MemAccessInst*) vmInstrNode->getInstruction();
1027
1028 // Initialize so we default to storing the offset in a register.
Chris Lattner8e5c0b42001-11-07 14:01:59 +00001029 int64_t smallConstOffset = 0;
Chris Lattner20b1ea02001-09-14 03:47:57 +00001030 Value* valueForRegOffset = NULL;
Vikram S. Adveed3fefb2002-08-03 13:48:21 +00001031 MachineOperand::MachineOperandType offsetOpType =
1032 MachineOperand::MO_VirtualRegister;
Chris Lattner20b1ea02001-09-14 03:47:57 +00001033
Vikram S. Adve74825322002-03-18 03:15:35 +00001034 // Check if there is an index vector and if so, compute the
1035 // right offset for structures and for arrays
Chris Lattner20b1ea02001-09-14 03:47:57 +00001036 //
1037 if (idxVec.size() > 0)
1038 {
Vikram S. Adve74825322002-03-18 03:15:35 +00001039 const PointerType* ptrType = cast<PointerType>(ptrVal->getType());
Chris Lattner20b1ea02001-09-14 03:47:57 +00001040
Vikram S. Adve242a8082002-05-19 15:25:51 +00001041 // If all indices are constant, compute the combined offset directly.
1042 if (allConstantIndices)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001043 {
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001044 // Compute the offset value using the index vector. Create a
1045 // virtual reg. for it since it may not fit in the immed field.
Vikram S. Adve242a8082002-05-19 15:25:51 +00001046 uint64_t offset = target.DataLayout.getIndexedOffset(ptrType,idxVec);
1047 valueForRegOffset = ConstantSInt::get(Type::LongTy, offset);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001048 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00001049 else
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001050 {
Vikram S. Adve242a8082002-05-19 15:25:51 +00001051 // There is at least one non-constant offset. Therefore, this must
Vikram S. Adveed3fefb2002-08-03 13:48:21 +00001052 // be an array ref, and must have been lowered to a single non-zero
1053 // offset. (An extra leading zero offset, if any, can be ignored.)
1054 // Generate code sequence to compute address from index.
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001055 //
Chris Lattner75ac4e52002-08-03 20:57:38 +00001056 assert(idxVec.size() == 1U + IsZero(idxVec[0])
Vikram S. Adveed3fefb2002-08-03 13:48:21 +00001057 && "Array refs must be lowered before Instruction Selection");
1058
Chris Lattner75ac4e52002-08-03 20:57:38 +00001059 Value* idxVal = idxVec[IsZero(idxVec[0])];
Vikram S. Adveed3fefb2002-08-03 13:48:21 +00001060
1061 vector<MachineInstr*> mulVec;
1062 Instruction* addr = new TmpInstruction(Type::UIntTy, memInst);
1063 MachineCodeForInstruction::get(memInst).addTemp(addr);
1064
1065 // The call to getTypeSize() will fail if size is not constant.
1066 unsigned int eltSize =
1067 target.DataLayout.getTypeSize(ptrType->getElementType());
1068 assert(eltSize > 0 && "Invalid or non-const array element size");
1069 ConstantUInt* eltVal = ConstantUInt::get(Type::UIntTy, eltSize);
1070
1071 // CreateMulInstruction() folds constants intelligently enough.
1072 CreateMulInstruction(target,
1073 memInst->getParent()->getParent(),
1074 idxVal, /* lval, not likely const */
1075 eltVal, /* rval, likely constant */
1076 addr, /* result*/
1077 mulVec,
1078 MachineCodeForInstruction::get(memInst),
1079 INVALID_MACHINE_OPCODE);
1080
1081 // Insert mulVec[] before *mvecI in mvec[] and update mvecI
1082 // to point to the same instruction it pointed to before.
1083 assert(mulVec.size() > 0 && "No multiply code created?");
1084 vector<MachineInstr*>::iterator oldMvecI = mvecI;
1085 for (unsigned i=0, N=mulVec.size(); i < N; ++i)
1086 mvecI = mvec.insert(mvecI, mulVec[i]) + 1; // pts to mem instr
1087
1088 valueForRegOffset = addr;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001089 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00001090 }
1091 else
1092 {
1093 offsetOpType = MachineOperand::MO_SignExtendedImmed;
1094 smallConstOffset = 0;
1095 }
Vikram S. Adveed3fefb2002-08-03 13:48:21 +00001096
Vikram S. Advea10d1a72002-03-31 19:07:35 +00001097 // For STORE:
1098 // Operand 0 is value, operand 1 is ptr, operand 2 is offset
1099 // For LOAD or GET_ELEMENT_PTR,
1100 // Operand 0 is ptr, operand 1 is offset, operand 2 is result.
1101 //
1102 unsigned offsetOpNum, ptrOpNum;
1103 if (memInst->getOpcode() == Instruction::Store)
1104 {
1105 (*mvecI)->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister,
1106 vmInstrNode->leftChild()->getValue());
1107 ptrOpNum = 1;
1108 offsetOpNum = 2;
1109 }
1110 else
1111 {
1112 ptrOpNum = 0;
1113 offsetOpNum = 1;
1114 (*mvecI)->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister,
1115 memInst);
1116 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00001117
Vikram S. Advea10d1a72002-03-31 19:07:35 +00001118 (*mvecI)->SetMachineOperandVal(ptrOpNum, MachineOperand::MO_VirtualRegister,
1119 ptrVal);
1120
Chris Lattner20b1ea02001-09-14 03:47:57 +00001121 if (offsetOpType == MachineOperand::MO_VirtualRegister)
1122 {
1123 assert(valueForRegOffset != NULL);
Vikram S. Adve74825322002-03-18 03:15:35 +00001124 (*mvecI)->SetMachineOperandVal(offsetOpNum, offsetOpType,
1125 valueForRegOffset);
Chris Lattner20b1ea02001-09-14 03:47:57 +00001126 }
1127 else
Vikram S. Adve74825322002-03-18 03:15:35 +00001128 (*mvecI)->SetMachineOperandConst(offsetOpNum, offsetOpType,
1129 smallConstOffset);
Chris Lattner20b1ea02001-09-14 03:47:57 +00001130}
1131
1132
Chris Lattner20b1ea02001-09-14 03:47:57 +00001133//
1134// Substitute operand `operandNum' of the instruction in node `treeNode'
Vikram S. Advec025fc12001-10-14 23:28:43 +00001135// in place of the use(s) of that instruction in node `parent'.
1136// Check both explicit and implicit operands!
Vikram S. Adve74825322002-03-18 03:15:35 +00001137// Also make sure to skip over a parent who:
1138// (1) is a list node in the Burg tree, or
1139// (2) itself had its results forwarded to its parent
Chris Lattner20b1ea02001-09-14 03:47:57 +00001140//
1141static void
1142ForwardOperand(InstructionNode* treeNode,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001143 InstrTreeNode* parent,
1144 int operandNum)
Chris Lattner20b1ea02001-09-14 03:47:57 +00001145{
Vikram S. Adve243dd452001-09-18 13:03:13 +00001146 assert(treeNode && parent && "Invalid invocation of ForwardOperand");
1147
Chris Lattner20b1ea02001-09-14 03:47:57 +00001148 Instruction* unusedOp = treeNode->getInstruction();
1149 Value* fwdOp = unusedOp->getOperand(operandNum);
Vikram S. Adve243dd452001-09-18 13:03:13 +00001150
1151 // The parent itself may be a list node, so find the real parent instruction
1152 while (parent->getNodeType() != InstrTreeNode::NTInstructionNode)
1153 {
1154 parent = parent->parent();
1155 assert(parent && "ERROR: Non-instruction node has no parent in tree.");
1156 }
1157 InstructionNode* parentInstrNode = (InstructionNode*) parent;
1158
1159 Instruction* userInstr = parentInstrNode->getInstruction();
Chris Lattner9c461082002-02-03 07:50:56 +00001160 MachineCodeForInstruction &mvec = MachineCodeForInstruction::get(userInstr);
Vikram S. Adve74825322002-03-18 03:15:35 +00001161
1162 // The parent's mvec would be empty if it was itself forwarded.
1163 // Recursively call ForwardOperand in that case...
1164 //
1165 if (mvec.size() == 0)
Chris Lattner20b1ea02001-09-14 03:47:57 +00001166 {
Vikram S. Adve74825322002-03-18 03:15:35 +00001167 assert(parent->parent() != NULL &&
1168 "Parent could not have been forwarded, yet has no instructions?");
1169 ForwardOperand(treeNode, parent->parent(), operandNum);
1170 }
1171 else
1172 {
Vikram S. Adve74825322002-03-18 03:15:35 +00001173 for (unsigned i=0, N=mvec.size(); i < N; i++)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001174 {
Vikram S. Adve74825322002-03-18 03:15:35 +00001175 MachineInstr* minstr = mvec[i];
1176 for (unsigned i=0, numOps=minstr->getNumOperands(); i < numOps; ++i)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001177 {
Vikram S. Adve74825322002-03-18 03:15:35 +00001178 const MachineOperand& mop = minstr->getOperand(i);
1179 if (mop.getOperandType() == MachineOperand::MO_VirtualRegister &&
1180 mop.getVRegValue() == unusedOp)
Vikram S. Adve242a8082002-05-19 15:25:51 +00001181 minstr->SetMachineOperandVal(i,
Vikram S. Adve74825322002-03-18 03:15:35 +00001182 MachineOperand::MO_VirtualRegister, fwdOp);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001183 }
Vikram S. Adve74825322002-03-18 03:15:35 +00001184
1185 for (unsigned i=0,numOps=minstr->getNumImplicitRefs(); i<numOps; ++i)
1186 if (minstr->getImplicitRef(i) == unusedOp)
Vikram S. Adve242a8082002-05-19 15:25:51 +00001187 minstr->setImplicitRef(i, fwdOp,
Vikram S. Adve6418eac2002-07-08 23:30:14 +00001188 minstr->implicitRefIsDefined(i),
1189 minstr->implicitRefIsDefinedAndUsed(i));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001190 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00001191 }
1192}
1193
1194
Vikram S. Adve242a8082002-05-19 15:25:51 +00001195inline bool
1196AllUsesAreBranches(const Instruction* setccI)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001197{
Vikram S. Adve242a8082002-05-19 15:25:51 +00001198 for (Value::use_const_iterator UI=setccI->use_begin(), UE=setccI->use_end();
1199 UI != UE; ++UI)
1200 if (! isa<TmpInstruction>(*UI) // ignore tmp instructions here
1201 && cast<Instruction>(*UI)->getOpcode() != Instruction::Br)
1202 return false;
1203 return true;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001204}
1205
Vikram S. Advefb361122001-10-22 13:36:31 +00001206//******************* Externally Visible Functions *************************/
1207
Vikram S. Advefb361122001-10-22 13:36:31 +00001208//------------------------------------------------------------------------
1209// External Function: ThisIsAChainRule
1210//
1211// Purpose:
1212// Check if a given BURG rule is a chain rule.
1213//------------------------------------------------------------------------
1214
1215extern bool
1216ThisIsAChainRule(int eruleno)
1217{
1218 switch(eruleno)
1219 {
1220 case 111: // stmt: reg
1221 case 113: // stmt: bool
1222 case 123:
1223 case 124:
1224 case 125:
1225 case 126:
1226 case 127:
1227 case 128:
1228 case 129:
1229 case 130:
1230 case 131:
1231 case 132:
1232 case 133:
1233 case 155:
1234 case 221:
1235 case 222:
1236 case 241:
1237 case 242:
1238 case 243:
1239 case 244:
Vikram S. Adve85e1e9c2002-04-01 20:28:48 +00001240 case 321:
Vikram S. Advefb361122001-10-22 13:36:31 +00001241 return true; break;
1242
1243 default:
1244 return false; break;
1245 }
1246}
Chris Lattner20b1ea02001-09-14 03:47:57 +00001247
1248
1249//------------------------------------------------------------------------
1250// External Function: GetInstructionsByRule
1251//
1252// Purpose:
1253// Choose machine instructions for the SPARC according to the
1254// patterns chosen by the BURG-generated parser.
1255//------------------------------------------------------------------------
1256
Vikram S. Adve74825322002-03-18 03:15:35 +00001257void
Chris Lattner20b1ea02001-09-14 03:47:57 +00001258GetInstructionsByRule(InstructionNode* subtreeRoot,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001259 int ruleForNode,
1260 short* nts,
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001261 TargetMachine &target,
Vikram S. Adve74825322002-03-18 03:15:35 +00001262 vector<MachineInstr*>& mvec)
Chris Lattner20b1ea02001-09-14 03:47:57 +00001263{
Chris Lattner20b1ea02001-09-14 03:47:57 +00001264 bool checkCast = false; // initialize here to use fall-through
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001265 bool maskUnsignedResult = false;
Chris Lattner20b1ea02001-09-14 03:47:57 +00001266 int nextRule;
1267 int forwardOperandNum = -1;
Vikram S. Adve74825322002-03-18 03:15:35 +00001268 unsigned int allocaSize = 0;
1269 MachineInstr* M, *M2;
1270 unsigned int L;
1271
1272 mvec.clear();
Chris Lattner20b1ea02001-09-14 03:47:57 +00001273
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001274 // If the code for this instruction was folded into the parent (user),
1275 // then do nothing!
1276 if (subtreeRoot->isFoldedIntoParent())
1277 return;
1278
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001279 //
1280 // Let's check for chain rules outside the switch so that we don't have
1281 // to duplicate the list of chain rule production numbers here again
1282 //
1283 if (ThisIsAChainRule(ruleForNode))
Chris Lattner20b1ea02001-09-14 03:47:57 +00001284 {
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001285 // Chain rules have a single nonterminal on the RHS.
1286 // Get the rule that matches the RHS non-terminal and use that instead.
1287 //
1288 assert(nts[0] && ! nts[1]
1289 && "A chain rule should have only one RHS non-terminal!");
1290 nextRule = burm_rule(subtreeRoot->state, nts[0]);
1291 nts = burm_nts[nextRule];
Vikram S. Adve74825322002-03-18 03:15:35 +00001292 GetInstructionsByRule(subtreeRoot, nextRule, nts, target, mvec);
Chris Lattner20b1ea02001-09-14 03:47:57 +00001293 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001294 else
Chris Lattner20b1ea02001-09-14 03:47:57 +00001295 {
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001296 switch(ruleForNode) {
1297 case 1: // stmt: Ret
1298 case 2: // stmt: RetValue(reg)
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001299 { // NOTE: Prepass of register allocation is responsible
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001300 // for moving return value to appropriate register.
1301 // Mark the return-address register as a hidden virtual reg.
Vikram S. Advea995e602001-10-11 04:23:19 +00001302 // Mark the return value register as an implicit ref of
1303 // the machine instruction.
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001304 // Finally put a NOP in the delay slot.
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001305 ReturnInst *returnInstr =
1306 cast<ReturnInst>(subtreeRoot->getInstruction());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001307 assert(returnInstr->getOpcode() == Instruction::Ret);
1308
Chris Lattner9c461082002-02-03 07:50:56 +00001309 Instruction* returnReg = new TmpInstruction(returnInstr);
1310 MachineCodeForInstruction::get(returnInstr).addTemp(returnReg);
Vikram S. Advefb361122001-10-22 13:36:31 +00001311
Vikram S. Adve74825322002-03-18 03:15:35 +00001312 M = new MachineInstr(JMPLRET);
1313 M->SetMachineOperandReg(0, MachineOperand::MO_VirtualRegister,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001314 returnReg);
Vikram S. Adve74825322002-03-18 03:15:35 +00001315 M->SetMachineOperandConst(1,MachineOperand::MO_SignExtendedImmed,
Chris Lattner697954c2002-01-20 22:54:45 +00001316 (int64_t)8);
Vikram S. Adve74825322002-03-18 03:15:35 +00001317 M->SetMachineOperandReg(2, target.getRegInfo().getZeroRegNum());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001318
Vikram S. Advea995e602001-10-11 04:23:19 +00001319 if (returnInstr->getReturnValue() != NULL)
Vikram S. Adve74825322002-03-18 03:15:35 +00001320 M->addImplicitRef(returnInstr->getReturnValue());
Vikram S. Advea995e602001-10-11 04:23:19 +00001321
Vikram S. Adve74825322002-03-18 03:15:35 +00001322 mvec.push_back(M);
1323 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001324
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001325 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001326 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001327
1328 case 3: // stmt: Store(reg,reg)
1329 case 4: // stmt: Store(reg,ptrreg)
Vikram S. Adve74825322002-03-18 03:15:35 +00001330 mvec.push_back(new MachineInstr(
1331 ChooseStoreInstruction(
1332 subtreeRoot->leftChild()->getValue()->getType())));
1333 SetOperandsForMemInstr(mvec, mvec.end()-1, subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001334 break;
Chris Lattner20b1ea02001-09-14 03:47:57 +00001335
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001336 case 5: // stmt: BrUncond
Vikram S. Adve74825322002-03-18 03:15:35 +00001337 M = new MachineInstr(BA);
Vikram S. Adve6418eac2002-07-08 23:30:14 +00001338 M->SetMachineOperandVal(0, MachineOperand::MO_PCRelativeDisp,
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001339 cast<BranchInst>(subtreeRoot->getInstruction())->getSuccessor(0));
Vikram S. Adve74825322002-03-18 03:15:35 +00001340 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001341
1342 // delay slot
Vikram S. Adve74825322002-03-18 03:15:35 +00001343 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001344 break;
Chris Lattner20b1ea02001-09-14 03:47:57 +00001345
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001346 case 206: // stmt: BrCond(setCCconst)
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001347 { // setCCconst => boolean was computed with `%b = setCC type reg1 const'
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001348 // If the constant is ZERO, we can use the branch-on-integer-register
1349 // instructions and avoid the SUBcc instruction entirely.
1350 // Otherwise this is just the same as case 5, so just fall through.
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001351 //
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001352 InstrTreeNode* constNode = subtreeRoot->leftChild()->rightChild();
1353 assert(constNode &&
1354 constNode->getNodeType() ==InstrTreeNode::NTConstNode);
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001355 Constant *constVal = cast<Constant>(constNode->getValue());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001356 bool isValidConst;
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001357
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001358 if ((constVal->getType()->isIntegral()
Chris Lattner9b625032002-05-06 16:15:30 +00001359 || isa<PointerType>(constVal->getType()))
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001360 && GetConstantValueAsSignedInt(constVal, isValidConst) == 0
1361 && isValidConst)
1362 {
1363 // That constant is a zero after all...
1364 // Use the left child of setCC as the first argument!
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001365 // Mark the setCC node so that no code is generated for it.
1366 InstructionNode* setCCNode = (InstructionNode*)
1367 subtreeRoot->leftChild();
1368 assert(setCCNode->getOpLabel() == SetCCOp);
1369 setCCNode->markFoldedIntoParent();
1370
1371 BranchInst* brInst=cast<BranchInst>(subtreeRoot->getInstruction());
1372
Vikram S. Adve74825322002-03-18 03:15:35 +00001373 M = new MachineInstr(ChooseBprInstruction(subtreeRoot));
1374 M->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister,
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001375 setCCNode->leftChild()->getValue());
Vikram S. Adve74825322002-03-18 03:15:35 +00001376 M->SetMachineOperandVal(1, MachineOperand::MO_PCRelativeDisp,
1377 brInst->getSuccessor(0));
1378 mvec.push_back(M);
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001379
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001380 // delay slot
Vikram S. Adve74825322002-03-18 03:15:35 +00001381 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001382
1383 // false branch
Vikram S. Adve74825322002-03-18 03:15:35 +00001384 M = new MachineInstr(BA);
Vikram S. Adve6418eac2002-07-08 23:30:14 +00001385 M->SetMachineOperandVal(0, MachineOperand::MO_PCRelativeDisp,
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001386 brInst->getSuccessor(1));
Vikram S. Adve74825322002-03-18 03:15:35 +00001387 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001388
1389 // delay slot
Vikram S. Adve74825322002-03-18 03:15:35 +00001390 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001391
1392 break;
1393 }
1394 // ELSE FALL THROUGH
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001395 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001396
1397 case 6: // stmt: BrCond(bool)
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001398 { // bool => boolean was computed with some boolean operator
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001399 // (SetCC, Not, ...). We need to check whether the type was a FP,
1400 // signed int or unsigned int, and check the branching condition in
1401 // order to choose the branch to use.
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001402 // If it is an integer CC, we also need to find the unique
1403 // TmpInstruction representing that CC.
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001404 //
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001405 BranchInst* brInst = cast<BranchInst>(subtreeRoot->getInstruction());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001406 bool isFPBranch;
Vikram S. Adve74825322002-03-18 03:15:35 +00001407 M = new MachineInstr(ChooseBccInstruction(subtreeRoot, isFPBranch));
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001408
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001409 Value* ccValue = GetTmpForCC(subtreeRoot->leftChild()->getValue(),
1410 brInst->getParent()->getParent(),
1411 isFPBranch? Type::FloatTy : Type::IntTy);
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001412
Vikram S. Adve74825322002-03-18 03:15:35 +00001413 M->SetMachineOperandVal(0, MachineOperand::MO_CCRegister, ccValue);
1414 M->SetMachineOperandVal(1, MachineOperand::MO_PCRelativeDisp,
1415 brInst->getSuccessor(0));
1416 mvec.push_back(M);
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001417
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001418 // delay slot
Vikram S. Adve74825322002-03-18 03:15:35 +00001419 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001420
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001421 // false branch
Vikram S. Adve74825322002-03-18 03:15:35 +00001422 M = new MachineInstr(BA);
Vikram S. Adve6418eac2002-07-08 23:30:14 +00001423 M->SetMachineOperandVal(0, MachineOperand::MO_PCRelativeDisp,
Vikram S. Adve74825322002-03-18 03:15:35 +00001424 brInst->getSuccessor(1));
1425 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001426
1427 // delay slot
Vikram S. Adve74825322002-03-18 03:15:35 +00001428 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001429 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001430 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001431
1432 case 208: // stmt: BrCond(boolconst)
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001433 {
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001434 // boolconst => boolean is a constant; use BA to first or second label
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001435 Constant* constVal =
1436 cast<Constant>(subtreeRoot->leftChild()->getValue());
1437 unsigned dest = cast<ConstantBool>(constVal)->getValue()? 0 : 1;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001438
Vikram S. Adve74825322002-03-18 03:15:35 +00001439 M = new MachineInstr(BA);
Vikram S. Adve6418eac2002-07-08 23:30:14 +00001440 M->SetMachineOperandVal(0, MachineOperand::MO_PCRelativeDisp,
Chris Lattner35504202002-04-27 03:14:39 +00001441 cast<BranchInst>(subtreeRoot->getInstruction())->getSuccessor(dest));
Vikram S. Adve74825322002-03-18 03:15:35 +00001442 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001443
1444 // delay slot
Vikram S. Adve74825322002-03-18 03:15:35 +00001445 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001446 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001447 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001448
1449 case 8: // stmt: BrCond(boolreg)
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001450 { // boolreg => boolean is stored in an existing register.
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001451 // Just use the branch-on-integer-register instruction!
1452 //
Vikram S. Adve74825322002-03-18 03:15:35 +00001453 M = new MachineInstr(BRNZ);
1454 M->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001455 subtreeRoot->leftChild()->getValue());
Vikram S. Adve74825322002-03-18 03:15:35 +00001456 M->SetMachineOperandVal(1, MachineOperand::MO_PCRelativeDisp,
Chris Lattner35504202002-04-27 03:14:39 +00001457 cast<BranchInst>(subtreeRoot->getInstruction())->getSuccessor(0));
Vikram S. Adve74825322002-03-18 03:15:35 +00001458 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001459
1460 // delay slot
Vikram S. Adve74825322002-03-18 03:15:35 +00001461 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001462
1463 // false branch
Vikram S. Adve74825322002-03-18 03:15:35 +00001464 M = new MachineInstr(BA);
Vikram S. Adve6418eac2002-07-08 23:30:14 +00001465 M->SetMachineOperandVal(0, MachineOperand::MO_PCRelativeDisp,
Chris Lattner35504202002-04-27 03:14:39 +00001466 cast<BranchInst>(subtreeRoot->getInstruction())->getSuccessor(1));
Vikram S. Adve74825322002-03-18 03:15:35 +00001467 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001468
1469 // delay slot
Vikram S. Adve74825322002-03-18 03:15:35 +00001470 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001471 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001472 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00001473
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001474 case 9: // stmt: Switch(reg)
1475 assert(0 && "*** SWITCH instruction is not implemented yet.");
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001476 break;
Chris Lattner20b1ea02001-09-14 03:47:57 +00001477
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001478 case 10: // reg: VRegList(reg, reg)
1479 assert(0 && "VRegList should never be the topmost non-chain rule");
1480 break;
1481
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001482 case 21: // bool: Not(bool): Both these are implemented as:
Vikram S. Adve85e1e9c2002-04-01 20:28:48 +00001483 case 421: // reg: BNot(reg) : reg = reg XOR-NOT 0
Vikram S. Adve74825322002-03-18 03:15:35 +00001484 M = new MachineInstr(XNOR);
1485 M->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister,
1486 subtreeRoot->leftChild()->getValue());
1487 M->SetMachineOperandReg(1, target.getRegInfo().getZeroRegNum());
1488 M->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister,
1489 subtreeRoot->getValue());
1490 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001491 break;
1492
1493 case 322: // reg: ToBoolTy(bool):
1494 case 22: // reg: ToBoolTy(reg):
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001495 {
1496 const Type* opType = subtreeRoot->leftChild()->getValue()->getType();
Chris Lattner9b625032002-05-06 16:15:30 +00001497 assert(opType->isIntegral() || isa<PointerType>(opType)
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001498 || opType == Type::BoolTy);
Vikram S. Adve74825322002-03-18 03:15:35 +00001499 forwardOperandNum = 0; // forward first operand to user
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001500 break;
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001501 }
1502
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001503 case 23: // reg: ToUByteTy(reg)
1504 case 25: // reg: ToUShortTy(reg)
1505 case 27: // reg: ToUIntTy(reg)
1506 case 29: // reg: ToULongTy(reg)
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001507 {
Vikram S. Adve242a8082002-05-19 15:25:51 +00001508 Instruction* destI = subtreeRoot->getInstruction();
1509 Value* opVal = subtreeRoot->leftChild()->getValue();
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001510 const Type* opType = subtreeRoot->leftChild()->getValue()->getType();
Vikram S. Adve1e606692002-07-31 21:01:34 +00001511 if (opType->isIntegral()
1512 || isa<PointerType>(opType)
1513 || opType == Type::BoolTy)
1514 {
1515 unsigned opSize = target.DataLayout.getTypeSize(opType);
1516 unsigned destSize = target.DataLayout.getTypeSize(destI->getType());
1517 if (opSize > destSize ||
1518 (opType->isSigned()
1519 && destSize < target.DataLayout.getIntegerRegize()))
1520 { // operand is larger than dest,
1521 // OR both are equal but smaller than the full register size
1522 // AND operand is signed, so it may have extra sign bits:
1523 // mask high bits using AND
1524 M = Create3OperandInstr(AND, opVal,
1525 ConstantUInt::get(Type::ULongTy,
1526 ((uint64_t) 1 << 8*destSize) - 1),
1527 destI);
1528 mvec.push_back(M);
1529 }
1530 else
1531 forwardOperandNum = 0; // forward first operand to user
Vikram S. Adve242a8082002-05-19 15:25:51 +00001532 }
Vikram S. Adve1e606692002-07-31 21:01:34 +00001533 else if (opType->isFloatingPoint())
1534 CreateCodeToConvertIntToFloat(target, opVal, destI, mvec,
1535 MachineCodeForInstruction::get(destI));
Vikram S. Adve242a8082002-05-19 15:25:51 +00001536 else
Vikram S. Adve1e606692002-07-31 21:01:34 +00001537 assert(0 && "Unrecognized operand type for convert-to-unsigned");
1538
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001539 break;
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001540 }
1541
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001542 case 24: // reg: ToSByteTy(reg)
1543 case 26: // reg: ToShortTy(reg)
1544 case 28: // reg: ToIntTy(reg)
1545 case 30: // reg: ToLongTy(reg)
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001546 {
Vikram S. Adve242a8082002-05-19 15:25:51 +00001547 Instruction* destI = subtreeRoot->getInstruction();
1548 Value* opVal = subtreeRoot->leftChild()->getValue();
1549 MachineCodeForInstruction& mcfi =MachineCodeForInstruction::get(destI);
Vikram S. Adve1e606692002-07-31 21:01:34 +00001550
Vikram S. Adve242a8082002-05-19 15:25:51 +00001551 const Type* opType = opVal->getType();
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001552 if (opType->isIntegral()
Chris Lattner9b625032002-05-06 16:15:30 +00001553 || isa<PointerType>(opType)
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001554 || opType == Type::BoolTy)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001555 {
Vikram S. Adve242a8082002-05-19 15:25:51 +00001556 // These operand types have the same format as the destination,
1557 // but may have different size: add sign bits or mask as needed.
1558 //
1559 const Type* destType = destI->getType();
1560 unsigned opSize = target.DataLayout.getTypeSize(opType);
1561 unsigned destSize = target.DataLayout.getTypeSize(destType);
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001562
1563 if (opSize < destSize ||
1564 (opSize == destSize &&
1565 opSize == target.DataLayout.getIntegerRegize()))
1566 { // operand is smaller or both operand and result fill register
1567 forwardOperandNum = 0; // forward first operand to user
Vikram S. Adve242a8082002-05-19 15:25:51 +00001568 }
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001569 else
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001570 { // need to mask (possibly) and then sign-extend (definitely)
1571 Value* srcForSignExt = opVal;
1572 unsigned srcSizeForSignExt = 8 * opSize;
1573 if (opSize > destSize)
1574 { // operand is larger than dest: mask high bits
1575 TmpInstruction *tmpI = new TmpInstruction(destType, opVal,
1576 destI, "maskHi");
1577 mcfi.addTemp(tmpI);
1578 M = Create3OperandInstr(AND, opVal,
1579 ConstantUInt::get(Type::ULongTy,
1580 ((uint64_t) 1 << 8*destSize)-1),
1581 tmpI);
1582 mvec.push_back(M);
1583 srcForSignExt = tmpI;
1584 srcSizeForSignExt = 8 * destSize;
1585 }
1586
1587 // sign-extend
1588 target.getInstrInfo().CreateSignExtensionInstructions(target, destI->getParent()->getParent(), srcForSignExt, srcSizeForSignExt, destI, mvec, mcfi);
1589 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001590 }
Vikram S. Adve1e606692002-07-31 21:01:34 +00001591 else if (opType->isFloatingPoint())
1592 CreateCodeToConvertIntToFloat(target, opVal, destI, mvec, mcfi);
1593 else
1594 assert(0 && "Unrecognized operand type for convert-to-signed");
1595
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001596 break;
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001597 }
1598
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001599 case 31: // reg: ToFloatTy(reg):
1600 case 32: // reg: ToDoubleTy(reg):
1601 case 232: // reg: ToDoubleTy(Constant):
1602
1603 // If this instruction has a parent (a user) in the tree
1604 // and the user is translated as an FsMULd instruction,
1605 // then the cast is unnecessary. So check that first.
1606 // In the future, we'll want to do the same for the FdMULq instruction,
1607 // so do the check here instead of only for ToFloatTy(reg).
1608 //
1609 if (subtreeRoot->parent() != NULL &&
Chris Lattner9c461082002-02-03 07:50:56 +00001610 MachineCodeForInstruction::get(((InstructionNode*)subtreeRoot->parent())->getInstruction())[0]->getOpCode() == FSMULD)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001611 {
Vikram S. Adve74825322002-03-18 03:15:35 +00001612 forwardOperandNum = 0; // forward first operand to user
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001613 }
1614 else
1615 {
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001616 Value* leftVal = subtreeRoot->leftChild()->getValue();
1617 const Type* opType = leftVal->getType();
Vikram S. Advedbc4fad2002-04-25 04:37:51 +00001618 MachineOpCode opCode=ChooseConvertToFloatInstr(
1619 subtreeRoot->getOpLabel(), opType);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001620 if (opCode == INVALID_OPCODE) // no conversion needed
1621 {
Vikram S. Adve74825322002-03-18 03:15:35 +00001622 forwardOperandNum = 0; // forward first operand to user
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001623 }
1624 else
1625 {
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001626 // If the source operand is a non-FP type it must be
1627 // first copied from int to float register via memory!
1628 Instruction *dest = subtreeRoot->getInstruction();
1629 Value* srcForCast;
1630 int n = 0;
Vikram S. Adve242a8082002-05-19 15:25:51 +00001631 if (! opType->isFloatingPoint())
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001632 {
1633 // Create a temporary to represent the FP register
1634 // into which the integer will be copied via memory.
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001635 // The type of this temporary will determine the FP
1636 // register used: single-prec for a 32-bit int or smaller,
1637 // double-prec for a 64-bit int.
1638 //
1639 const Type* srcTypeToUse =
1640 (leftVal->getType() == Type::LongTy)? Type::DoubleTy
1641 : Type::FloatTy;
1642
Chris Lattner9c461082002-02-03 07:50:56 +00001643 srcForCast = new TmpInstruction(srcTypeToUse, dest);
Vikram S. Advedbc4fad2002-04-25 04:37:51 +00001644 MachineCodeForInstruction &destMCFI =
Chris Lattner9c461082002-02-03 07:50:56 +00001645 MachineCodeForInstruction::get(dest);
Vikram S. Advedbc4fad2002-04-25 04:37:51 +00001646 destMCFI.addTemp(srcForCast);
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001647
Vikram S. Adve242a8082002-05-19 15:25:51 +00001648 target.getInstrInfo().CreateCodeToCopyIntToFloat(target,
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001649 dest->getParent()->getParent(),
1650 leftVal, (TmpInstruction*) srcForCast,
Vikram S. Adve242a8082002-05-19 15:25:51 +00001651 mvec, destMCFI);
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001652 }
1653 else
1654 srcForCast = leftVal;
1655
Vikram S. Adve74825322002-03-18 03:15:35 +00001656 M = new MachineInstr(opCode);
1657 M->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister,
1658 srcForCast);
1659 M->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister,
1660 dest);
1661 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001662 }
1663 }
1664 break;
1665
1666 case 19: // reg: ToArrayTy(reg):
1667 case 20: // reg: ToPointerTy(reg):
Vikram S. Adve74825322002-03-18 03:15:35 +00001668 forwardOperandNum = 0; // forward first operand to user
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001669 break;
1670
1671 case 233: // reg: Add(reg, Constant)
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001672 maskUnsignedResult = true;
Vikram S. Adve74825322002-03-18 03:15:35 +00001673 M = CreateAddConstInstruction(subtreeRoot);
1674 if (M != NULL)
1675 {
1676 mvec.push_back(M);
1677 break;
1678 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001679 // ELSE FALL THROUGH
Vikram S. Adve74825322002-03-18 03:15:35 +00001680
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001681 case 33: // reg: Add(reg, reg)
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001682 maskUnsignedResult = true;
Vikram S. Adve74825322002-03-18 03:15:35 +00001683 mvec.push_back(new MachineInstr(ChooseAddInstruction(subtreeRoot)));
1684 Set3OperandsFromInstr(mvec.back(), subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001685 break;
1686
1687 case 234: // reg: Sub(reg, Constant)
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001688 maskUnsignedResult = true;
Vikram S. Adve74825322002-03-18 03:15:35 +00001689 M = CreateSubConstInstruction(subtreeRoot);
1690 if (M != NULL)
1691 {
1692 mvec.push_back(M);
1693 break;
1694 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001695 // ELSE FALL THROUGH
Vikram S. Adve74825322002-03-18 03:15:35 +00001696
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001697 case 34: // reg: Sub(reg, reg)
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001698 maskUnsignedResult = true;
Vikram S. Adve74825322002-03-18 03:15:35 +00001699 mvec.push_back(new MachineInstr(ChooseSubInstructionByType(
1700 subtreeRoot->getInstruction()->getType())));
1701 Set3OperandsFromInstr(mvec.back(), subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001702 break;
1703
1704 case 135: // reg: Mul(todouble, todouble)
1705 checkCast = true;
1706 // FALL THROUGH
1707
1708 case 35: // reg: Mul(reg, reg)
Vikram S. Adve74825322002-03-18 03:15:35 +00001709 {
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001710 maskUnsignedResult = true;
Vikram S. Adve74825322002-03-18 03:15:35 +00001711 MachineOpCode forceOp = ((checkCast && BothFloatToDouble(subtreeRoot))
1712 ? FSMULD
1713 : INVALID_MACHINE_OPCODE);
Vikram S. Adve242a8082002-05-19 15:25:51 +00001714 Instruction* mulInstr = subtreeRoot->getInstruction();
1715 CreateMulInstruction(target, mulInstr->getParent()->getParent(),
Vikram S. Adve74825322002-03-18 03:15:35 +00001716 subtreeRoot->leftChild()->getValue(),
1717 subtreeRoot->rightChild()->getValue(),
Vikram S. Adve242a8082002-05-19 15:25:51 +00001718 mulInstr, mvec,
1719 MachineCodeForInstruction::get(mulInstr),forceOp);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001720 break;
Vikram S. Adve74825322002-03-18 03:15:35 +00001721 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001722 case 335: // reg: Mul(todouble, todoubleConst)
1723 checkCast = true;
1724 // FALL THROUGH
1725
1726 case 235: // reg: Mul(reg, Constant)
Vikram S. Adve74825322002-03-18 03:15:35 +00001727 {
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001728 maskUnsignedResult = true;
Vikram S. Adve74825322002-03-18 03:15:35 +00001729 MachineOpCode forceOp = ((checkCast && BothFloatToDouble(subtreeRoot))
1730 ? FSMULD
1731 : INVALID_MACHINE_OPCODE);
Vikram S. Adve242a8082002-05-19 15:25:51 +00001732 Instruction* mulInstr = subtreeRoot->getInstruction();
1733 CreateMulInstruction(target, mulInstr->getParent()->getParent(),
Vikram S. Adve74825322002-03-18 03:15:35 +00001734 subtreeRoot->leftChild()->getValue(),
1735 subtreeRoot->rightChild()->getValue(),
Vikram S. Adve242a8082002-05-19 15:25:51 +00001736 mulInstr, mvec,
1737 MachineCodeForInstruction::get(mulInstr),
1738 forceOp);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001739 break;
Vikram S. Adve74825322002-03-18 03:15:35 +00001740 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001741 case 236: // reg: Div(reg, Constant)
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001742 maskUnsignedResult = true;
Vikram S. Adve74825322002-03-18 03:15:35 +00001743 L = mvec.size();
1744 CreateDivConstInstruction(target, subtreeRoot, mvec);
1745 if (mvec.size() > L)
1746 break;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001747 // ELSE FALL THROUGH
Vikram S. Adve74825322002-03-18 03:15:35 +00001748
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001749 case 36: // reg: Div(reg, reg)
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001750 maskUnsignedResult = true;
Vikram S. Adve74825322002-03-18 03:15:35 +00001751 mvec.push_back(new MachineInstr(ChooseDivInstruction(target, subtreeRoot)));
1752 Set3OperandsFromInstr(mvec.back(), subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001753 break;
1754
1755 case 37: // reg: Rem(reg, reg)
1756 case 237: // reg: Rem(reg, Constant)
Vikram S. Adve510eec72001-11-04 21:59:14 +00001757 {
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001758 maskUnsignedResult = true;
Vikram S. Adve510eec72001-11-04 21:59:14 +00001759 Instruction* remInstr = subtreeRoot->getInstruction();
1760
Chris Lattner9c461082002-02-03 07:50:56 +00001761 TmpInstruction* quot = new TmpInstruction(
Vikram S. Adve510eec72001-11-04 21:59:14 +00001762 subtreeRoot->leftChild()->getValue(),
1763 subtreeRoot->rightChild()->getValue());
Chris Lattner9c461082002-02-03 07:50:56 +00001764 TmpInstruction* prod = new TmpInstruction(
Vikram S. Adve510eec72001-11-04 21:59:14 +00001765 quot,
1766 subtreeRoot->rightChild()->getValue());
Chris Lattner9c461082002-02-03 07:50:56 +00001767 MachineCodeForInstruction::get(remInstr).addTemp(quot).addTemp(prod);
Vikram S. Adve510eec72001-11-04 21:59:14 +00001768
Vikram S. Adve74825322002-03-18 03:15:35 +00001769 M = new MachineInstr(ChooseDivInstruction(target, subtreeRoot));
1770 Set3OperandsFromInstr(M, subtreeRoot, target);
1771 M->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister,quot);
1772 mvec.push_back(M);
Vikram S. Adve510eec72001-11-04 21:59:14 +00001773
Vikram S. Adve74825322002-03-18 03:15:35 +00001774 M = new MachineInstr(ChooseMulInstructionByType(
Vikram S. Adve510eec72001-11-04 21:59:14 +00001775 subtreeRoot->getInstruction()->getType()));
Vikram S. Adve74825322002-03-18 03:15:35 +00001776 M->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister,quot);
1777 M->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister,
Vikram S. Adve510eec72001-11-04 21:59:14 +00001778 subtreeRoot->rightChild()->getValue());
Vikram S. Adve74825322002-03-18 03:15:35 +00001779 M->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister,prod);
1780 mvec.push_back(M);
Vikram S. Adve510eec72001-11-04 21:59:14 +00001781
Vikram S. Adve74825322002-03-18 03:15:35 +00001782 M = new MachineInstr(ChooseSubInstructionByType(
Vikram S. Adve510eec72001-11-04 21:59:14 +00001783 subtreeRoot->getInstruction()->getType()));
Vikram S. Adve74825322002-03-18 03:15:35 +00001784 Set3OperandsFromInstr(M, subtreeRoot, target);
1785 M->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister,prod);
1786 mvec.push_back(M);
Vikram S. Adve510eec72001-11-04 21:59:14 +00001787
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001788 break;
Vikram S. Adve510eec72001-11-04 21:59:14 +00001789 }
1790
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001791 case 38: // bool: And(bool, bool)
1792 case 238: // bool: And(bool, boolconst)
1793 case 338: // reg : BAnd(reg, reg)
1794 case 538: // reg : BAnd(reg, Constant)
Vikram S. Adve74825322002-03-18 03:15:35 +00001795 mvec.push_back(new MachineInstr(AND));
1796 Set3OperandsFromInstr(mvec.back(), subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001797 break;
1798
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001799 case 138: // bool: And(bool, not)
1800 case 438: // bool: BAnd(bool, not)
Vikram S. Adve74825322002-03-18 03:15:35 +00001801 mvec.push_back(new MachineInstr(ANDN));
1802 Set3OperandsFromInstr(mvec.back(), subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001803 break;
1804
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001805 case 39: // bool: Or(bool, bool)
1806 case 239: // bool: Or(bool, boolconst)
1807 case 339: // reg : BOr(reg, reg)
1808 case 539: // reg : BOr(reg, Constant)
Vikram S. Adve242a8082002-05-19 15:25:51 +00001809 mvec.push_back(new MachineInstr(OR));
Vikram S. Adve74825322002-03-18 03:15:35 +00001810 Set3OperandsFromInstr(mvec.back(), subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001811 break;
1812
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001813 case 139: // bool: Or(bool, not)
1814 case 439: // bool: BOr(bool, not)
Vikram S. Adve74825322002-03-18 03:15:35 +00001815 mvec.push_back(new MachineInstr(ORN));
1816 Set3OperandsFromInstr(mvec.back(), subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001817 break;
1818
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001819 case 40: // bool: Xor(bool, bool)
1820 case 240: // bool: Xor(bool, boolconst)
1821 case 340: // reg : BXor(reg, reg)
1822 case 540: // reg : BXor(reg, Constant)
Vikram S. Adve74825322002-03-18 03:15:35 +00001823 mvec.push_back(new MachineInstr(XOR));
1824 Set3OperandsFromInstr(mvec.back(), subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001825 break;
1826
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001827 case 140: // bool: Xor(bool, not)
1828 case 440: // bool: BXor(bool, not)
Vikram S. Adve74825322002-03-18 03:15:35 +00001829 mvec.push_back(new MachineInstr(XNOR));
1830 Set3OperandsFromInstr(mvec.back(), subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001831 break;
1832
1833 case 41: // boolconst: SetCC(reg, Constant)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001834 //
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001835 // If the SetCC was folded into the user (parent), it will be
1836 // caught above. All other cases are the same as case 42,
1837 // so just fall through.
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001838 //
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001839 case 42: // bool: SetCC(reg, reg):
1840 {
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001841 // This generates a SUBCC instruction, putting the difference in
1842 // a result register, and setting a condition code.
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001843 //
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001844 // If the boolean result of the SetCC is used by anything other
Vikram S. Adve6418eac2002-07-08 23:30:14 +00001845 // than a branch instruction, or if it is used outside the current
1846 // basic block, the boolean must be
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001847 // computed and stored in the result register. Otherwise, discard
1848 // the difference (by using %g0) and keep only the condition code.
1849 //
1850 // To compute the boolean result in a register we use a conditional
1851 // move, unless the result of the SUBCC instruction can be used as
1852 // the bool! This assumes that zero is FALSE and any non-zero
1853 // integer is TRUE.
1854 //
1855 InstructionNode* parentNode = (InstructionNode*) subtreeRoot->parent();
1856 Instruction* setCCInstr = subtreeRoot->getInstruction();
Vikram S. Adve242a8082002-05-19 15:25:51 +00001857
Vikram S. Adve6418eac2002-07-08 23:30:14 +00001858 bool keepBoolVal = parentNode == NULL ||
1859 ! AllUsesAreBranches(setCCInstr);
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001860 bool subValIsBoolVal = setCCInstr->getOpcode() == Instruction::SetNE;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001861 bool keepSubVal = keepBoolVal && subValIsBoolVal;
1862 bool computeBoolVal = keepBoolVal && ! subValIsBoolVal;
1863
1864 bool mustClearReg;
1865 int valueToMove;
Chris Lattner8e5c0b42001-11-07 14:01:59 +00001866 MachineOpCode movOpCode = 0;
Vikram S. Adve6418eac2002-07-08 23:30:14 +00001867
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001868 // Mark the 4th operand as being a CC register, and as a def
1869 // A TmpInstruction is created to represent the CC "result".
1870 // Unlike other instances of TmpInstruction, this one is used
1871 // by machine code of multiple LLVM instructions, viz.,
1872 // the SetCC and the branch. Make sure to get the same one!
1873 // Note that we do this even for FP CC registers even though they
1874 // are explicit operands, because the type of the operand
1875 // needs to be a floating point condition code, not an integer
1876 // condition code. Think of this as casting the bool result to
1877 // a FP condition code register.
1878 //
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001879 Value* leftVal = subtreeRoot->leftChild()->getValue();
Chris Lattner9b625032002-05-06 16:15:30 +00001880 bool isFPCompare = leftVal->getType()->isFloatingPoint();
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001881
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001882 TmpInstruction* tmpForCC = GetTmpForCC(setCCInstr,
1883 setCCInstr->getParent()->getParent(),
Chris Lattner9b625032002-05-06 16:15:30 +00001884 isFPCompare ? Type::FloatTy : Type::IntTy);
Chris Lattner9c461082002-02-03 07:50:56 +00001885 MachineCodeForInstruction::get(setCCInstr).addTemp(tmpForCC);
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001886
1887 if (! isFPCompare)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001888 {
1889 // Integer condition: dest. should be %g0 or an integer register.
1890 // If result must be saved but condition is not SetEQ then we need
1891 // a separate instruction to compute the bool result, so discard
1892 // result of SUBcc instruction anyway.
1893 //
Vikram S. Adve74825322002-03-18 03:15:35 +00001894 M = new MachineInstr(SUBcc);
1895 Set3OperandsFromInstr(M, subtreeRoot, target, ! keepSubVal);
1896 M->SetMachineOperandVal(3, MachineOperand::MO_CCRegister,
1897 tmpForCC, /*def*/true);
1898 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001899
1900 if (computeBoolVal)
1901 { // recompute bool using the integer condition codes
1902 movOpCode =
1903 ChooseMovpccAfterSub(subtreeRoot,mustClearReg,valueToMove);
1904 }
1905 }
1906 else
1907 {
1908 // FP condition: dest of FCMP should be some FCCn register
Vikram S. Adve74825322002-03-18 03:15:35 +00001909 M = new MachineInstr(ChooseFcmpInstruction(subtreeRoot));
1910 M->SetMachineOperandVal(0, MachineOperand::MO_CCRegister,
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001911 tmpForCC);
Vikram S. Adve74825322002-03-18 03:15:35 +00001912 M->SetMachineOperandVal(1,MachineOperand::MO_VirtualRegister,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001913 subtreeRoot->leftChild()->getValue());
Vikram S. Adve74825322002-03-18 03:15:35 +00001914 M->SetMachineOperandVal(2,MachineOperand::MO_VirtualRegister,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001915 subtreeRoot->rightChild()->getValue());
Vikram S. Adve74825322002-03-18 03:15:35 +00001916 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001917
1918 if (computeBoolVal)
1919 {// recompute bool using the FP condition codes
1920 mustClearReg = true;
1921 valueToMove = 1;
1922 movOpCode = ChooseMovFpccInstruction(subtreeRoot);
1923 }
1924 }
1925
1926 if (computeBoolVal)
1927 {
1928 if (mustClearReg)
1929 {// Unconditionally set register to 0
Vikram S. Adve74825322002-03-18 03:15:35 +00001930 M = new MachineInstr(SETHI);
1931 M->SetMachineOperandConst(0,MachineOperand::MO_UnextendedImmed,
1932 (int64_t)0);
1933 M->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister,
1934 setCCInstr);
1935 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001936 }
1937
1938 // Now conditionally move `valueToMove' (0 or 1) into the register
Vikram S. Adve6418eac2002-07-08 23:30:14 +00001939 // Mark the register as a use (as well as a def) because the old
1940 // value should be retained if the condition is false.
Vikram S. Adve74825322002-03-18 03:15:35 +00001941 M = new MachineInstr(movOpCode);
1942 M->SetMachineOperandVal(0, MachineOperand::MO_CCRegister,
1943 tmpForCC);
1944 M->SetMachineOperandConst(1, MachineOperand::MO_UnextendedImmed,
1945 valueToMove);
1946 M->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister,
Vikram S. Adve6418eac2002-07-08 23:30:14 +00001947 setCCInstr, /*isDef*/ true,
1948 /*isDefAndUse*/ true);
Vikram S. Adve74825322002-03-18 03:15:35 +00001949 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001950 }
1951 break;
1952 }
1953
1954 case 43: // boolreg: VReg
1955 case 44: // boolreg: Constant
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001956 break;
1957
1958 case 51: // reg: Load(reg)
1959 case 52: // reg: Load(ptrreg)
1960 case 53: // reg: LoadIdx(reg,reg)
1961 case 54: // reg: LoadIdx(ptrreg,reg)
Vikram S. Adve74825322002-03-18 03:15:35 +00001962 mvec.push_back(new MachineInstr(ChooseLoadInstruction(
1963 subtreeRoot->getValue()->getType())));
1964 SetOperandsForMemInstr(mvec, mvec.end()-1, subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001965 break;
1966
1967 case 55: // reg: GetElemPtr(reg)
1968 case 56: // reg: GetElemPtrIdx(reg,reg)
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001969 // If the GetElemPtr was folded into the user (parent), it will be
1970 // caught above. For other cases, we have to compute the address.
Vikram S. Adve74825322002-03-18 03:15:35 +00001971 mvec.push_back(new MachineInstr(ADD));
1972 SetOperandsForMemInstr(mvec, mvec.end()-1, subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001973 break;
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001974
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001975 case 57: // reg: Alloca: Implement as 1 instruction:
1976 { // add %fp, offsetFromFP -> result
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001977 AllocationInst* instr =
1978 cast<AllocationInst>(subtreeRoot->getInstruction());
1979 unsigned int tsize =
1980 target.findOptimalStorageSize(instr->getAllocatedType());
Vikram S. Adve74825322002-03-18 03:15:35 +00001981 assert(tsize != 0);
1982 CreateCodeForFixedSizeAlloca(target, instr, tsize, 1, mvec);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001983 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001984 }
Vikram S. Adve74825322002-03-18 03:15:35 +00001985
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001986 case 58: // reg: Alloca(reg): Implement as 3 instructions:
1987 // mul num, typeSz -> tmp
1988 // sub %sp, tmp -> %sp
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001989 { // add %sp, frameSizeBelowDynamicArea -> result
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001990 AllocationInst* instr =
1991 cast<AllocationInst>(subtreeRoot->getInstruction());
Vikram S. Adve74825322002-03-18 03:15:35 +00001992 const Type* eltType = instr->getAllocatedType();
1993
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001994 // If #elements is constant, use simpler code for fixed-size allocas
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001995 int tsize = (int) target.findOptimalStorageSize(eltType);
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001996 Value* numElementsVal = NULL;
1997 bool isArray = instr->isArrayAllocation();
1998
1999 if (!isArray ||
2000 isa<Constant>(numElementsVal = instr->getArraySize()))
2001 { // total size is constant: generate code for fixed-size alloca
2002 unsigned int numElements = isArray?
2003 cast<ConstantUInt>(numElementsVal)->getValue() : 1;
2004 CreateCodeForFixedSizeAlloca(target, instr, tsize,
2005 numElements, mvec);
2006 }
Vikram S. Adve74825322002-03-18 03:15:35 +00002007 else // total size is not constant.
2008 CreateCodeForVariableSizeAlloca(target, instr, tsize,
Vikram S. Advefd3900a2002-03-24 03:33:02 +00002009 numElementsVal, mvec);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002010 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00002011 }
Vikram S. Adve74825322002-03-18 03:15:35 +00002012
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002013 case 61: // reg: Call
Vikram S. Advedbc4fad2002-04-25 04:37:51 +00002014 { // Generate a direct (CALL) or indirect (JMPL). depending
2015 // Mark the return-address register and the indirection
2016 // register (if any) as hidden virtual registers.
Vikram S. Advea995e602001-10-11 04:23:19 +00002017 // Also, mark the operands of the Call and return value (if
2018 // any) as implicit operands of the CALL machine instruction.
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00002019 //
Vikram S. Advedbc4fad2002-04-25 04:37:51 +00002020 // If this is a varargs function, floating point arguments
2021 // have to passed in integer registers so insert
2022 // copy-float-to-int instructions for each float operand.
2023 //
Chris Lattnerb00c5822001-10-02 03:41:24 +00002024 CallInst *callInstr = cast<CallInst>(subtreeRoot->getInstruction());
Chris Lattner749655f2001-10-13 06:54:30 +00002025 Value *callee = callInstr->getCalledValue();
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002026
Vikram S. Advea10d1a72002-03-31 19:07:35 +00002027 // Create hidden virtual register for return address, with type void*.
Vikram S. Adve242a8082002-05-19 15:25:51 +00002028 TmpInstruction* retAddrReg =
Vikram S. Advea10d1a72002-03-31 19:07:35 +00002029 new TmpInstruction(PointerType::get(Type::VoidTy), callInstr);
Chris Lattner9c461082002-02-03 07:50:56 +00002030 MachineCodeForInstruction::get(callInstr).addTemp(retAddrReg);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002031
Vikram S. Adveea21a6c2001-10-20 20:57:06 +00002032 // Generate the machine instruction and its operands.
2033 // Use CALL for direct function calls; this optimistically assumes
2034 // the PC-relative address fits in the CALL address field (22 bits).
2035 // Use JMPL for indirect calls.
2036 //
Chris Lattnerb0d04722002-03-26 17:58:12 +00002037 if (isa<Function>(callee))
Vikram S. Adveea21a6c2001-10-20 20:57:06 +00002038 { // direct function call
Vikram S. Adve74825322002-03-18 03:15:35 +00002039 M = new MachineInstr(CALL);
2040 M->SetMachineOperandVal(0, MachineOperand::MO_PCRelativeDisp,
2041 callee);
Vikram S. Adveea21a6c2001-10-20 20:57:06 +00002042 }
2043 else
2044 { // indirect function call
Vikram S. Adve74825322002-03-18 03:15:35 +00002045 M = new MachineInstr(JMPLCALL);
2046 M->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister,
2047 callee);
2048 M->SetMachineOperandConst(1, MachineOperand::MO_SignExtendedImmed,
2049 (int64_t) 0);
2050 M->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister,
2051 retAddrReg);
Vikram S. Adveea21a6c2001-10-20 20:57:06 +00002052 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002053
Vikram S. Adve74825322002-03-18 03:15:35 +00002054 mvec.push_back(M);
Vikram S. Advea10d1a72002-03-31 19:07:35 +00002055
Vikram S. Adve242a8082002-05-19 15:25:51 +00002056 const FunctionType* funcType =
2057 cast<FunctionType>(cast<PointerType>(callee->getType())
2058 ->getElementType());
2059 bool isVarArgs = funcType->isVarArg();
2060 bool noPrototype = isVarArgs && funcType->getNumParams() == 0;
Vikram S. Advedbc4fad2002-04-25 04:37:51 +00002061
Vikram S. Adve242a8082002-05-19 15:25:51 +00002062 // Use an annotation to pass information about call arguments
2063 // to the register allocator.
2064 CallArgsDescriptor* argDesc = new CallArgsDescriptor(callInstr,
2065 retAddrReg, isVarArgs, noPrototype);
2066 M->addAnnotation(argDesc);
Vikram S. Advea995e602001-10-11 04:23:19 +00002067
Vikram S. Adve242a8082002-05-19 15:25:51 +00002068 assert(callInstr->getOperand(0) == callee
2069 && "This is assumed in the loop below!");
2070
2071 for (unsigned i=1, N=callInstr->getNumOperands(); i < N; ++i)
2072 {
2073 Value* argVal = callInstr->getOperand(i);
2074 Instruction* intArgReg = NULL;
2075
2076 // Check for FP arguments to varargs functions.
2077 // Any such argument in the first $K$ args must be passed in an
2078 // integer register, where K = #integer argument registers.
2079 if (isVarArgs && argVal->getType()->isFloatingPoint())
2080 {
2081 // If it is a function with no prototype, pass value
2082 // as an FP value as well as a varargs value
2083 if (noPrototype)
2084 argDesc->getArgInfo(i-1).setUseFPArgReg();
2085
2086 // If this arg. is in the first $K$ regs, add a copy
2087 // float-to-int instruction to pass the value as an integer.
2088 if (i < target.getRegInfo().GetNumOfIntArgRegs())
2089 {
2090 MachineCodeForInstruction &destMCFI =
2091 MachineCodeForInstruction::get(callInstr);
2092 intArgReg = new TmpInstruction(Type::IntTy, argVal);
2093 destMCFI.addTemp(intArgReg);
2094
2095 vector<MachineInstr*> copyMvec;
2096 target.getInstrInfo().CreateCodeToCopyFloatToInt(target,
2097 callInstr->getParent()->getParent(),
2098 argVal, (TmpInstruction*) intArgReg,
2099 copyMvec, destMCFI);
2100 mvec.insert(mvec.begin(),copyMvec.begin(),copyMvec.end());
2101
2102 argDesc->getArgInfo(i-1).setUseIntArgReg();
2103 argDesc->getArgInfo(i-1).setArgCopy(intArgReg);
2104 }
2105 else
2106 // Cannot fit in first $K$ regs so pass the arg on the stack
2107 argDesc->getArgInfo(i-1).setUseStackSlot();
2108 }
2109
2110 if (intArgReg)
2111 mvec.back()->addImplicitRef(intArgReg);
2112
2113 mvec.back()->addImplicitRef(argVal);
2114 }
2115
2116 // Add the return value as an implicit ref. The call operands
2117 // were added above.
Vikram S. Adveea21a6c2001-10-20 20:57:06 +00002118 if (callInstr->getType() != Type::VoidTy)
Vikram S. Adve74825322002-03-18 03:15:35 +00002119 mvec.back()->addImplicitRef(callInstr, /*isDef*/ true);
Vikram S. Advea995e602001-10-11 04:23:19 +00002120
Vikram S. Adveea21a6c2001-10-20 20:57:06 +00002121 // For the CALL instruction, the ret. addr. reg. is also implicit
Chris Lattnerb0d04722002-03-26 17:58:12 +00002122 if (isa<Function>(callee))
Vikram S. Adve74825322002-03-18 03:15:35 +00002123 mvec.back()->addImplicitRef(retAddrReg, /*isDef*/ true);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002124
Vikram S. Adve74825322002-03-18 03:15:35 +00002125 // delay slot
2126 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002127 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00002128 }
Vikram S. Adve242a8082002-05-19 15:25:51 +00002129
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002130 case 62: // reg: Shl(reg, reg)
Vikram S. Adve242a8082002-05-19 15:25:51 +00002131 {
2132 Value* argVal1 = subtreeRoot->leftChild()->getValue();
2133 Value* argVal2 = subtreeRoot->rightChild()->getValue();
2134 Instruction* shlInstr = subtreeRoot->getInstruction();
2135
2136 const Type* opType = argVal1->getType();
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002137 assert(opType->isIntegral()
Vikram S. Adve242a8082002-05-19 15:25:51 +00002138 || opType == Type::BoolTy
2139 || isa<PointerType>(opType)&&"Shl unsupported for other types");
2140
2141 CreateShiftInstructions(target, shlInstr->getParent()->getParent(),
2142 (opType == Type::LongTy)? SLLX : SLL,
2143 argVal1, argVal2, 0, shlInstr, mvec,
2144 MachineCodeForInstruction::get(shlInstr));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002145 break;
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00002146 }
2147
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002148 case 63: // reg: Shr(reg, reg)
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00002149 { const Type* opType = subtreeRoot->leftChild()->getValue()->getType();
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002150 assert(opType->isIntegral()
Vikram S. Adve242a8082002-05-19 15:25:51 +00002151 || isa<PointerType>(opType)&&"Shr unsupported for other types");
Vikram S. Adve74825322002-03-18 03:15:35 +00002152 mvec.push_back(new MachineInstr((opType->isSigned()
2153 ? ((opType == Type::LongTy)? SRAX : SRA)
2154 : ((opType == Type::LongTy)? SRLX : SRL))));
2155 Set3OperandsFromInstr(mvec.back(), subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002156 break;
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00002157 }
2158
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002159 case 64: // reg: Phi(reg,reg)
Vikram S. Adve74825322002-03-18 03:15:35 +00002160 break; // don't forward the value
2161
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002162 case 71: // reg: VReg
2163 case 72: // reg: Constant
Vikram S. Adve74825322002-03-18 03:15:35 +00002164 break; // don't forward the value
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002165
2166 default:
2167 assert(0 && "Unrecognized BURG rule");
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002168 break;
2169 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00002170 }
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00002171
Chris Lattner20b1ea02001-09-14 03:47:57 +00002172 if (forwardOperandNum >= 0)
2173 { // We did not generate a machine instruction but need to use operand.
2174 // If user is in the same tree, replace Value in its machine operand.
2175 // If not, insert a copy instruction which should get coalesced away
2176 // by register allocation.
2177 if (subtreeRoot->parent() != NULL)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002178 ForwardOperand(subtreeRoot, subtreeRoot->parent(), forwardOperandNum);
Chris Lattner20b1ea02001-09-14 03:47:57 +00002179 else
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002180 {
Vikram S. Adve7fe27872001-10-18 00:26:20 +00002181 vector<MachineInstr*> minstrVec;
Vikram S. Adve242a8082002-05-19 15:25:51 +00002182 Instruction* instr = subtreeRoot->getInstruction();
2183 target.getInstrInfo().
2184 CreateCopyInstructionsByType(target,
2185 instr->getParent()->getParent(),
2186 instr->getOperand(forwardOperandNum),
2187 instr, minstrVec,
2188 MachineCodeForInstruction::get(instr));
Vikram S. Adve7fe27872001-10-18 00:26:20 +00002189 assert(minstrVec.size() > 0);
Vikram S. Adve74825322002-03-18 03:15:35 +00002190 mvec.insert(mvec.end(), minstrVec.begin(), minstrVec.end());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002191 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00002192 }
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00002193
2194 if (maskUnsignedResult)
2195 { // If result is unsigned and smaller than int reg size,
2196 // we need to clear high bits of result value.
2197 assert(forwardOperandNum < 0 && "Need mask but no instruction generated");
2198 Instruction* dest = subtreeRoot->getInstruction();
2199 if (! dest->getType()->isSigned())
2200 {
2201 unsigned destSize = target.DataLayout.getTypeSize(dest->getType());
2202 if (destSize < target.DataLayout.getIntegerRegize())
2203 { // Mask high bits. Use a TmpInstruction to represent the
2204 // intermediate result before masking. Since those instructions
2205 // have already been generated, go back and substitute tmpI
2206 // for dest in the result position of each one of them.
2207 TmpInstruction *tmpI = new TmpInstruction(dest->getType(), dest,
2208 NULL, "maskHi");
2209 MachineCodeForInstruction::get(dest).addTemp(tmpI);
2210
2211 for (unsigned i=0, N=mvec.size(); i < N; ++i)
2212 mvec[i]->substituteValue(dest, tmpI);
2213
2214 M = Create3OperandInstr(AND, tmpI,
2215 ConstantUInt::get(Type::ULongTy,
2216 ((uint64_t) 1 << 8*destSize) - 1),
2217 dest);
2218 mvec.push_back(M);
2219 }
2220 }
2221 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00002222}