blob: 7bdf716cac031be525b327755d28a92e4f944464 [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
Chris Lattner20b1ea02001-09-14 03:47:57 +000027//************************ Internal Functions ******************************/
28
Chris Lattner20b1ea02001-09-14 03:47:57 +000029
Chris Lattner20b1ea02001-09-14 03:47:57 +000030static inline MachineOpCode
31ChooseBprInstruction(const InstructionNode* instrNode)
32{
33 MachineOpCode opCode;
34
35 Instruction* setCCInstr =
36 ((InstructionNode*) instrNode->leftChild())->getInstruction();
37
38 switch(setCCInstr->getOpcode())
39 {
40 case Instruction::SetEQ: opCode = BRZ; break;
41 case Instruction::SetNE: opCode = BRNZ; break;
42 case Instruction::SetLE: opCode = BRLEZ; break;
43 case Instruction::SetGE: opCode = BRGEZ; break;
44 case Instruction::SetLT: opCode = BRLZ; break;
45 case Instruction::SetGT: opCode = BRGZ; break;
46 default:
47 assert(0 && "Unrecognized VM instruction!");
48 opCode = INVALID_OPCODE;
49 break;
50 }
51
52 return opCode;
53}
54
55
56static inline MachineOpCode
Chris Lattner20b1ea02001-09-14 03:47:57 +000057ChooseBpccInstruction(const InstructionNode* instrNode,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +000058 const BinaryOperator* setCCInstr)
Chris Lattner20b1ea02001-09-14 03:47:57 +000059{
60 MachineOpCode opCode = INVALID_OPCODE;
61
62 bool isSigned = setCCInstr->getOperand(0)->getType()->isSigned();
63
64 if (isSigned)
65 {
66 switch(setCCInstr->getOpcode())
Vikram S. Adve4cecdd22001-10-01 00:12:53 +000067 {
68 case Instruction::SetEQ: opCode = BE; break;
69 case Instruction::SetNE: opCode = BNE; break;
70 case Instruction::SetLE: opCode = BLE; break;
71 case Instruction::SetGE: opCode = BGE; break;
72 case Instruction::SetLT: opCode = BL; break;
73 case Instruction::SetGT: opCode = BG; break;
74 default:
75 assert(0 && "Unrecognized VM instruction!");
76 break;
77 }
Chris Lattner20b1ea02001-09-14 03:47:57 +000078 }
79 else
80 {
81 switch(setCCInstr->getOpcode())
Vikram S. Adve4cecdd22001-10-01 00:12:53 +000082 {
83 case Instruction::SetEQ: opCode = BE; break;
84 case Instruction::SetNE: opCode = BNE; break;
85 case Instruction::SetLE: opCode = BLEU; break;
86 case Instruction::SetGE: opCode = BCC; break;
87 case Instruction::SetLT: opCode = BCS; break;
88 case Instruction::SetGT: opCode = BGU; break;
89 default:
90 assert(0 && "Unrecognized VM instruction!");
91 break;
92 }
Chris Lattner20b1ea02001-09-14 03:47:57 +000093 }
94
95 return opCode;
96}
97
98static inline MachineOpCode
99ChooseBFpccInstruction(const InstructionNode* instrNode,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000100 const BinaryOperator* setCCInstr)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000101{
102 MachineOpCode opCode = INVALID_OPCODE;
103
104 switch(setCCInstr->getOpcode())
105 {
106 case Instruction::SetEQ: opCode = FBE; break;
107 case Instruction::SetNE: opCode = FBNE; break;
108 case Instruction::SetLE: opCode = FBLE; break;
109 case Instruction::SetGE: opCode = FBGE; break;
110 case Instruction::SetLT: opCode = FBL; break;
111 case Instruction::SetGT: opCode = FBG; break;
112 default:
113 assert(0 && "Unrecognized VM instruction!");
114 break;
115 }
116
117 return opCode;
118}
119
120
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000121// Create a unique TmpInstruction for a boolean value,
122// representing the CC register used by a branch on that value.
123// For now, hack this using a little static cache of TmpInstructions.
124// Eventually the entire BURG instruction selection should be put
125// into a separate class that can hold such information.
Vikram S. Adveff5a09e2001-11-08 05:04:09 +0000126// The static cache is not too bad because the memory for these
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000127// TmpInstructions will be freed along with the rest of the Function anyway.
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000128//
129static TmpInstruction*
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000130GetTmpForCC(Value* boolVal, const Function *F, const Type* ccType)
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000131{
Chris Lattner09ff1122002-07-24 21:21:32 +0000132 typedef hash_map<const Value*, TmpInstruction*> BoolTmpCache;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000133 static BoolTmpCache boolToTmpCache; // Map boolVal -> TmpInstruction*
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000134 static const Function *lastFunction = 0;// Use to flush cache between funcs
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000135
136 assert(boolVal->getType() == Type::BoolTy && "Weird but ok! Delete assert");
137
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000138 if (lastFunction != F)
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000139 {
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000140 lastFunction = F;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000141 boolToTmpCache.clear();
142 }
143
Vikram S. Adveff5a09e2001-11-08 05:04:09 +0000144 // Look for tmpI and create a new one otherwise. The new value is
145 // directly written to map using the ref returned by operator[].
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000146 TmpInstruction*& tmpI = boolToTmpCache[boolVal];
147 if (tmpI == NULL)
Chris Lattner9c461082002-02-03 07:50:56 +0000148 tmpI = new TmpInstruction(ccType, boolVal);
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000149
150 return tmpI;
151}
152
153
Chris Lattner20b1ea02001-09-14 03:47:57 +0000154static inline MachineOpCode
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000155ChooseBccInstruction(const InstructionNode* instrNode,
156 bool& isFPBranch)
157{
158 InstructionNode* setCCNode = (InstructionNode*) instrNode->leftChild();
Vikram S. Adve30a6f492002-08-22 02:56:10 +0000159 assert(setCCNode->getOpLabel() == SetCCOp);
160 BinaryOperator* setCCInstr =cast<BinaryOperator>(setCCNode->getInstruction());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000161 const Type* setCCType = setCCInstr->getOperand(0)->getType();
162
Vikram S. Adve242a8082002-05-19 15:25:51 +0000163 isFPBranch = setCCType->isFloatingPoint(); // Return value: don't delete!
164
165 if (isFPBranch)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000166 return ChooseBFpccInstruction(instrNode, setCCInstr);
167 else
168 return ChooseBpccInstruction(instrNode, setCCInstr);
169}
170
171
172static inline MachineOpCode
Chris Lattner20b1ea02001-09-14 03:47:57 +0000173ChooseMovFpccInstruction(const InstructionNode* instrNode)
174{
175 MachineOpCode opCode = INVALID_OPCODE;
176
177 switch(instrNode->getInstruction()->getOpcode())
178 {
179 case Instruction::SetEQ: opCode = MOVFE; break;
180 case Instruction::SetNE: opCode = MOVFNE; break;
181 case Instruction::SetLE: opCode = MOVFLE; break;
182 case Instruction::SetGE: opCode = MOVFGE; break;
183 case Instruction::SetLT: opCode = MOVFL; break;
184 case Instruction::SetGT: opCode = MOVFG; break;
185 default:
186 assert(0 && "Unrecognized VM instruction!");
187 break;
188 }
189
190 return opCode;
191}
192
193
194// Assumes that SUBcc v1, v2 -> v3 has been executed.
195// In most cases, we want to clear v3 and then follow it by instruction
196// MOVcc 1 -> v3.
197// Set mustClearReg=false if v3 need not be cleared before conditional move.
198// Set valueToMove=0 if we want to conditionally move 0 instead of 1
199// (i.e., we want to test inverse of a condition)
Vikram S. Adve243dd452001-09-18 13:03:13 +0000200// (The latter two cases do not seem to arise because SetNE needs nothing.)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000201//
202static MachineOpCode
203ChooseMovpccAfterSub(const InstructionNode* instrNode,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000204 bool& mustClearReg,
205 int& valueToMove)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000206{
207 MachineOpCode opCode = INVALID_OPCODE;
208 mustClearReg = true;
209 valueToMove = 1;
210
211 switch(instrNode->getInstruction()->getOpcode())
212 {
Vikram S. Adve243dd452001-09-18 13:03:13 +0000213 case Instruction::SetEQ: opCode = MOVE; break;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000214 case Instruction::SetLE: opCode = MOVLE; break;
215 case Instruction::SetGE: opCode = MOVGE; break;
216 case Instruction::SetLT: opCode = MOVL; break;
217 case Instruction::SetGT: opCode = MOVG; break;
Vikram S. Adve243dd452001-09-18 13:03:13 +0000218 case Instruction::SetNE: assert(0 && "No move required!"); break;
219 default: assert(0 && "Unrecognized VM instr!"); break;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000220 }
221
222 return opCode;
223}
224
Chris Lattner20b1ea02001-09-14 03:47:57 +0000225static inline MachineOpCode
Vikram S. Advedbc4fad2002-04-25 04:37:51 +0000226ChooseConvertToFloatInstr(OpLabel vopCode, const Type* opType)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000227{
228 MachineOpCode opCode = INVALID_OPCODE;
229
Vikram S. Advedbc4fad2002-04-25 04:37:51 +0000230 switch(vopCode)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000231 {
232 case ToFloatTy:
233 if (opType == Type::SByteTy || opType == Type::ShortTy || opType == Type::IntTy)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000234 opCode = FITOS;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000235 else if (opType == Type::LongTy)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000236 opCode = FXTOS;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000237 else if (opType == Type::DoubleTy)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000238 opCode = FDTOS;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000239 else if (opType == Type::FloatTy)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000240 ;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000241 else
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000242 assert(0 && "Cannot convert this type to FLOAT on SPARC");
Chris Lattner20b1ea02001-09-14 03:47:57 +0000243 break;
244
245 case ToDoubleTy:
Vikram S. Adve74825322002-03-18 03:15:35 +0000246 // This is usually used in conjunction with CreateCodeToCopyIntToFloat().
247 // Both functions should treat the integer as a 32-bit value for types
248 // of 4 bytes or less, and as a 64-bit value otherwise.
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000249 if (opType == Type::SByteTy || opType == Type::UByteTy ||
250 opType == Type::ShortTy || opType == Type::UShortTy ||
251 opType == Type::IntTy || opType == Type::UIntTy)
Vikram S. Adve74825322002-03-18 03:15:35 +0000252 opCode = FITOD;
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000253 else if (opType == Type::LongTy || opType == Type::ULongTy)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000254 opCode = FXTOD;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000255 else if (opType == Type::FloatTy)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000256 opCode = FSTOD;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000257 else if (opType == Type::DoubleTy)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000258 ;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000259 else
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000260 assert(0 && "Cannot convert this type to DOUBLE on SPARC");
Chris Lattner20b1ea02001-09-14 03:47:57 +0000261 break;
262
263 default:
264 break;
265 }
266
267 return opCode;
268}
269
270static inline MachineOpCode
Vikram S. Adve1e606692002-07-31 21:01:34 +0000271ChooseConvertToIntInstr(Type::PrimitiveID tid, const Type* opType)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000272{
273 MachineOpCode opCode = INVALID_OPCODE;;
274
Vikram S. Adve1e606692002-07-31 21:01:34 +0000275 if (tid==Type::SByteTyID || tid==Type::ShortTyID || tid==Type::IntTyID ||
276 tid==Type::UByteTyID || tid==Type::UShortTyID || tid==Type::UIntTyID)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000277 {
278 switch (opType->getPrimitiveID())
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000279 {
Chris Lattner20b1ea02001-09-14 03:47:57 +0000280 case Type::FloatTyID: opCode = FSTOI; break;
281 case Type::DoubleTyID: opCode = FDTOI; break;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000282 default:
283 assert(0 && "Non-numeric non-bool type cannot be converted to Int");
284 break;
285 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000286 }
Vikram S. Adve1e606692002-07-31 21:01:34 +0000287 else if (tid==Type::LongTyID || tid==Type::ULongTyID)
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 = FSTOX; break;
292 case Type::DoubleTyID: opCode = FDTOX; break;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000293 default:
294 assert(0 && "Non-numeric non-bool type cannot be converted to Long");
295 break;
296 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000297 }
298 else
299 assert(0 && "Should not get here, Mo!");
300
301 return opCode;
302}
303
Vikram S. Advedbc4fad2002-04-25 04:37:51 +0000304MachineInstr*
Vikram S. Adve1e606692002-07-31 21:01:34 +0000305CreateConvertToIntInstr(Type::PrimitiveID destTID, Value* srcVal,Value* destVal)
Vikram S. Advedbc4fad2002-04-25 04:37:51 +0000306{
Vikram S. Adve1e606692002-07-31 21:01:34 +0000307 MachineOpCode opCode = ChooseConvertToIntInstr(destTID, srcVal->getType());
Vikram S. Advedbc4fad2002-04-25 04:37:51 +0000308 assert(opCode != INVALID_OPCODE && "Expected to need conversion!");
309
310 MachineInstr* M = new MachineInstr(opCode);
311 M->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister, srcVal);
312 M->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister, destVal);
313 return M;
314}
Chris Lattner20b1ea02001-09-14 03:47:57 +0000315
Vikram S. Adve8cfffd32002-08-24 20:56:53 +0000316// CreateCodeToConvertFloatToInt: Convert FP value to signed or unsigned integer
Vikram S. Adve1e606692002-07-31 21:01:34 +0000317// The FP value must be converted to the dest type in an FP register,
318// and the result is then copied from FP to int register via memory.
Vikram S. Advebabc0fa2002-09-05 18:32:13 +0000319//
320// Since fdtoi converts to signed integers, any FP value V between MAXINT+1
321// and MAXUNSIGNED (i.e., 2^31 <= V <= 2^32-1) would be converted incorrectly
322// *only* when converting to an unsigned int. (Unsigned byte, short or long
323// don't have this problem.)
324// For unsigned int, we therefore have to generate the code sequence:
325//
326// if (V > (float) MAXINT) {
327// unsigned result = (unsigned) (V - (float) MAXINT);
328// result = result + (unsigned) MAXINT;
329// }
330// else
331// result = (unsigned int) V;
332//
Vikram S. Adve1e606692002-07-31 21:01:34 +0000333static void
Vikram S. Adve8cfffd32002-08-24 20:56:53 +0000334CreateCodeToConvertFloatToInt(const TargetMachine& target,
335 Value* opVal,
336 Instruction* destI,
337 std::vector<MachineInstr*>& mvec,
338 MachineCodeForInstruction& mcfi)
Vikram S. Adve1e606692002-07-31 21:01:34 +0000339{
340 // Create a temporary to represent the FP register into which the
341 // int value will placed after conversion. The type of this temporary
342 // depends on the type of FP register to use: single-prec for a 32-bit
343 // int or smaller; double-prec for a 64-bit int.
344 //
Vikram S. Advebabc0fa2002-09-05 18:32:13 +0000345 size_t destSize = target.DataLayout.getTypeSize(destI->getType());
346 const Type* destTypeToUse = (destSize > 4)? Type::DoubleTy : Type::FloatTy;
347 TmpInstruction* destForCast = new TmpInstruction(destTypeToUse, opVal);
Vikram S. Adve1e606692002-07-31 21:01:34 +0000348 mcfi.addTemp(destForCast);
349
350 // Create the fp-to-int conversion code
351 MachineInstr* M = CreateConvertToIntInstr(destI->getType()->getPrimitiveID(),
352 opVal, destForCast);
353 mvec.push_back(M);
354
355 // Create the fpreg-to-intreg copy code
356 target.getInstrInfo().
357 CreateCodeToCopyFloatToInt(target, destI->getParent()->getParent(),
Vikram S. Advebabc0fa2002-09-05 18:32:13 +0000358 destForCast, destI, mvec, mcfi);
Vikram S. Adve1e606692002-07-31 21:01:34 +0000359}
360
361
Chris Lattner20b1ea02001-09-14 03:47:57 +0000362static inline MachineOpCode
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000363ChooseAddInstruction(const InstructionNode* instrNode)
364{
365 return ChooseAddInstructionByType(instrNode->getInstruction()->getType());
366}
367
368
Chris Lattner20b1ea02001-09-14 03:47:57 +0000369static inline MachineInstr*
370CreateMovFloatInstruction(const InstructionNode* instrNode,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000371 const Type* resultType)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000372{
373 MachineInstr* minstr = new MachineInstr((resultType == Type::FloatTy)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000374 ? FMOVS : FMOVD);
Vikram S. Adve74825322002-03-18 03:15:35 +0000375 minstr->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister,
376 instrNode->leftChild()->getValue());
377 minstr->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister,
378 instrNode->getValue());
Chris Lattner20b1ea02001-09-14 03:47:57 +0000379 return minstr;
380}
381
382static inline MachineInstr*
383CreateAddConstInstruction(const InstructionNode* instrNode)
384{
385 MachineInstr* minstr = NULL;
386
387 Value* constOp = ((InstrTreeNode*) instrNode->rightChild())->getValue();
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000388 assert(isa<Constant>(constOp));
Chris Lattner20b1ea02001-09-14 03:47:57 +0000389
390 // Cases worth optimizing are:
391 // (1) Add with 0 for float or double: use an FMOV of appropriate type,
392 // instead of an FADD (1 vs 3 cycles). There is no integer MOV.
393 //
Chris Lattner9b625032002-05-06 16:15:30 +0000394 if (ConstantFP *FPC = dyn_cast<ConstantFP>(constOp)) {
395 double dval = FPC->getValue();
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000396 if (dval == 0.0)
Vikram S. Adve242a8082002-05-19 15:25:51 +0000397 minstr = CreateMovFloatInstruction(instrNode,
398 instrNode->getInstruction()->getType());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000399 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000400
401 return minstr;
402}
403
404
405static inline MachineOpCode
Vikram S. Adve510eec72001-11-04 21:59:14 +0000406ChooseSubInstructionByType(const Type* resultType)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000407{
408 MachineOpCode opCode = INVALID_OPCODE;
409
Chris Lattner0c4e8862002-09-03 01:08:28 +0000410 if (resultType->isInteger() || isa<PointerType>(resultType))
Chris Lattner20b1ea02001-09-14 03:47:57 +0000411 {
412 opCode = SUB;
413 }
414 else
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000415 switch(resultType->getPrimitiveID())
416 {
417 case Type::FloatTyID: opCode = FSUBS; break;
418 case Type::DoubleTyID: opCode = FSUBD; break;
419 default: assert(0 && "Invalid type for SUB instruction"); break;
420 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000421
422 return opCode;
423}
424
425
426static inline MachineInstr*
427CreateSubConstInstruction(const InstructionNode* instrNode)
428{
429 MachineInstr* minstr = NULL;
430
431 Value* constOp = ((InstrTreeNode*) instrNode->rightChild())->getValue();
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000432 assert(isa<Constant>(constOp));
Chris Lattner20b1ea02001-09-14 03:47:57 +0000433
434 // Cases worth optimizing are:
435 // (1) Sub with 0 for float or double: use an FMOV of appropriate type,
436 // instead of an FSUB (1 vs 3 cycles). There is no integer MOV.
437 //
Chris Lattner9b625032002-05-06 16:15:30 +0000438 if (ConstantFP *FPC = dyn_cast<ConstantFP>(constOp)) {
439 double dval = FPC->getValue();
440 if (dval == 0.0)
Vikram S. Adve242a8082002-05-19 15:25:51 +0000441 minstr = CreateMovFloatInstruction(instrNode,
442 instrNode->getInstruction()->getType());
Chris Lattner9b625032002-05-06 16:15:30 +0000443 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000444
445 return minstr;
446}
447
448
449static inline MachineOpCode
450ChooseFcmpInstruction(const InstructionNode* instrNode)
451{
452 MachineOpCode opCode = INVALID_OPCODE;
453
454 Value* operand = ((InstrTreeNode*) instrNode->leftChild())->getValue();
455 switch(operand->getType()->getPrimitiveID()) {
456 case Type::FloatTyID: opCode = FCMPS; break;
457 case Type::DoubleTyID: opCode = FCMPD; break;
458 default: assert(0 && "Invalid type for FCMP instruction"); break;
459 }
460
461 return opCode;
462}
463
464
465// Assumes that leftArg and rightArg are both cast instructions.
466//
467static inline bool
468BothFloatToDouble(const InstructionNode* instrNode)
469{
470 InstrTreeNode* leftArg = instrNode->leftChild();
471 InstrTreeNode* rightArg = instrNode->rightChild();
472 InstrTreeNode* leftArgArg = leftArg->leftChild();
473 InstrTreeNode* rightArgArg = rightArg->leftChild();
474 assert(leftArg->getValue()->getType() == rightArg->getValue()->getType());
475
476 // Check if both arguments are floats cast to double
477 return (leftArg->getValue()->getType() == Type::DoubleTy &&
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000478 leftArgArg->getValue()->getType() == Type::FloatTy &&
479 rightArgArg->getValue()->getType() == Type::FloatTy);
Chris Lattner20b1ea02001-09-14 03:47:57 +0000480}
481
482
483static inline MachineOpCode
Vikram S. Adve510eec72001-11-04 21:59:14 +0000484ChooseMulInstructionByType(const Type* resultType)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000485{
486 MachineOpCode opCode = INVALID_OPCODE;
487
Chris Lattner0c4e8862002-09-03 01:08:28 +0000488 if (resultType->isInteger())
Vikram S. Adve510eec72001-11-04 21:59:14 +0000489 opCode = MULX;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000490 else
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000491 switch(resultType->getPrimitiveID())
492 {
493 case Type::FloatTyID: opCode = FMULS; break;
494 case Type::DoubleTyID: opCode = FMULD; break;
495 default: assert(0 && "Invalid type for MUL instruction"); break;
496 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000497
498 return opCode;
499}
500
501
Vikram S. Adve510eec72001-11-04 21:59:14 +0000502
Chris Lattner20b1ea02001-09-14 03:47:57 +0000503static inline MachineInstr*
Vikram S. Adve74825322002-03-18 03:15:35 +0000504CreateIntNegInstruction(const TargetMachine& target,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000505 Value* vreg)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000506{
507 MachineInstr* minstr = new MachineInstr(SUB);
Vikram S. Adve74825322002-03-18 03:15:35 +0000508 minstr->SetMachineOperandReg(0, target.getRegInfo().getZeroRegNum());
509 minstr->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister, vreg);
510 minstr->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister, vreg);
Chris Lattner20b1ea02001-09-14 03:47:57 +0000511 return minstr;
512}
513
514
Vikram S. Adve242a8082002-05-19 15:25:51 +0000515// Create instruction sequence for any shift operation.
516// SLL or SLLX on an operand smaller than the integer reg. size (64bits)
517// requires a second instruction for explicit sign-extension.
518// Note that we only have to worry about a sign-bit appearing in the
519// most significant bit of the operand after shifting (e.g., bit 32 of
520// Int or bit 16 of Short), so we do not have to worry about results
521// that are as large as a normal integer register.
522//
523static inline void
524CreateShiftInstructions(const TargetMachine& target,
525 Function* F,
526 MachineOpCode shiftOpCode,
527 Value* argVal1,
528 Value* optArgVal2, /* Use optArgVal2 if not NULL */
529 unsigned int optShiftNum, /* else use optShiftNum */
530 Instruction* destVal,
531 vector<MachineInstr*>& mvec,
532 MachineCodeForInstruction& mcfi)
533{
534 assert((optArgVal2 != NULL || optShiftNum <= 64) &&
535 "Large shift sizes unexpected, but can be handled below: "
536 "You need to check whether or not it fits in immed field below");
537
538 // If this is a logical left shift of a type smaller than the standard
539 // integer reg. size, we have to extend the sign-bit into upper bits
540 // of dest, so we need to put the result of the SLL into a temporary.
541 //
542 Value* shiftDest = destVal;
543 const Type* opType = argVal1->getType();
544 unsigned opSize = target.DataLayout.getTypeSize(argVal1->getType());
545 if ((shiftOpCode == SLL || shiftOpCode == SLLX)
546 && opSize < target.DataLayout.getIntegerRegize())
547 { // put SLL result into a temporary
548 shiftDest = new TmpInstruction(argVal1, optArgVal2, "sllTmp");
549 mcfi.addTemp(shiftDest);
550 }
551
552 MachineInstr* M = (optArgVal2 != NULL)
553 ? Create3OperandInstr(shiftOpCode, argVal1, optArgVal2, shiftDest)
554 : Create3OperandInstr_UImmed(shiftOpCode, argVal1, optShiftNum, shiftDest);
555 mvec.push_back(M);
556
557 if (shiftDest != destVal)
558 { // extend the sign-bit of the result into all upper bits of dest
559 assert(8*opSize <= 32 && "Unexpected type size > 4 and < IntRegSize?");
560 target.getInstrInfo().
561 CreateSignExtensionInstructions(target, F, shiftDest, 8*opSize,
562 destVal, mvec, mcfi);
563 }
564}
565
566
Vikram S. Adve74825322002-03-18 03:15:35 +0000567// Does not create any instructions if we cannot exploit constant to
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000568// create a cheaper instruction.
569// This returns the approximate cost of the instructions generated,
570// which is used to pick the cheapest when both operands are constant.
571static inline unsigned int
Vikram S. Adve242a8082002-05-19 15:25:51 +0000572CreateMulConstInstruction(const TargetMachine &target, Function* F,
573 Value* lval, Value* rval, Instruction* destVal,
574 vector<MachineInstr*>& mvec,
575 MachineCodeForInstruction& mcfi)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000576{
Vikram S. Adve242a8082002-05-19 15:25:51 +0000577 /* Use max. multiply cost, viz., cost of MULX */
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000578 unsigned int cost = target.getInstrInfo().minLatency(MULX);
Vikram S. Adve242a8082002-05-19 15:25:51 +0000579 unsigned int firstNewInstr = mvec.size();
Vikram S. Adve74825322002-03-18 03:15:35 +0000580
581 Value* constOp = rval;
582 if (! isa<Constant>(constOp))
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000583 return cost;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000584
585 // Cases worth optimizing are:
586 // (1) Multiply by 0 or 1 for any type: replace with copy (ADD or FMOV)
587 // (2) Multiply by 2^x for integer types: replace with Shift
588 //
Vikram S. Adve74825322002-03-18 03:15:35 +0000589 const Type* resultType = destVal->getType();
Chris Lattner20b1ea02001-09-14 03:47:57 +0000590
Chris Lattner0c4e8862002-09-03 01:08:28 +0000591 if (resultType->isInteger() || isa<PointerType>(resultType))
Chris Lattner20b1ea02001-09-14 03:47:57 +0000592 {
Chris Lattner20b1ea02001-09-14 03:47:57 +0000593 bool isValidConst;
594 int64_t C = GetConstantValueAsSignedInt(constOp, isValidConst);
595 if (isValidConst)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000596 {
Vikram S. Adve242a8082002-05-19 15:25:51 +0000597 unsigned pow;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000598 bool needNeg = false;
599 if (C < 0)
600 {
601 needNeg = true;
602 C = -C;
603 }
604
605 if (C == 0 || C == 1)
606 {
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000607 cost = target.getInstrInfo().minLatency(ADD);
Vikram S. Adve242a8082002-05-19 15:25:51 +0000608 MachineInstr* M = (C == 0)
609 ? Create3OperandInstr_Reg(ADD,
610 target.getRegInfo().getZeroRegNum(),
611 target.getRegInfo().getZeroRegNum(),
612 destVal)
613 : Create3OperandInstr_Reg(ADD, lval,
614 target.getRegInfo().getZeroRegNum(),
615 destVal);
616 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000617 }
Chris Lattner36346c72002-05-19 21:20:19 +0000618 else if (isPowerOf2(C, pow))
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000619 {
Vikram S. Adve242a8082002-05-19 15:25:51 +0000620 unsigned int opSize = target.DataLayout.getTypeSize(resultType);
621 MachineOpCode opCode = (opSize <= 32)? SLL : SLLX;
622 CreateShiftInstructions(target, F, opCode, lval, NULL, pow,
623 destVal, mvec, mcfi);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000624 }
625
Vikram S. Adve242a8082002-05-19 15:25:51 +0000626 if (mvec.size() > 0 && needNeg)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000627 { // insert <reg = SUB 0, reg> after the instr to flip the sign
Vikram S. Adve242a8082002-05-19 15:25:51 +0000628 MachineInstr* M = CreateIntNegInstruction(target, destVal);
629 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000630 }
631 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000632 }
633 else
634 {
Chris Lattner9b625032002-05-06 16:15:30 +0000635 if (ConstantFP *FPC = dyn_cast<ConstantFP>(constOp))
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000636 {
Chris Lattner9b625032002-05-06 16:15:30 +0000637 double dval = FPC->getValue();
Vikram S. Adve6ad7c552001-11-09 02:18:16 +0000638 if (fabs(dval) == 1)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000639 {
Vikram S. Adve242a8082002-05-19 15:25:51 +0000640 MachineOpCode opCode = (dval < 0)
Vikram S. Adve6ad7c552001-11-09 02:18:16 +0000641 ? (resultType == Type::FloatTy? FNEGS : FNEGD)
642 : (resultType == Type::FloatTy? FMOVS : FMOVD);
Vikram S. Adve242a8082002-05-19 15:25:51 +0000643 MachineInstr* M = Create2OperandInstr(opCode, lval, destVal);
644 mvec.push_back(M);
Vikram S. Adve6ad7c552001-11-09 02:18:16 +0000645 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000646 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000647 }
648
Vikram S. Adve242a8082002-05-19 15:25:51 +0000649 if (firstNewInstr < mvec.size())
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000650 {
Vikram S. Adve242a8082002-05-19 15:25:51 +0000651 cost = 0;
652 for (unsigned int i=firstNewInstr; i < mvec.size(); ++i)
653 cost += target.getInstrInfo().minLatency(mvec[i]->getOpCode());
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000654 }
655
656 return cost;
Vikram S. Adve74825322002-03-18 03:15:35 +0000657}
658
659
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000660// Does not create any instructions if we cannot exploit constant to
661// create a cheaper instruction.
662//
663static inline void
664CreateCheapestMulConstInstruction(const TargetMachine &target,
Vikram S. Adve242a8082002-05-19 15:25:51 +0000665 Function* F,
666 Value* lval, Value* rval,
667 Instruction* destVal,
668 vector<MachineInstr*>& mvec,
669 MachineCodeForInstruction& mcfi)
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000670{
671 Value* constOp;
672 if (isa<Constant>(lval) && isa<Constant>(rval))
673 { // both operands are constant: try both orders!
674 vector<MachineInstr*> mvec1, mvec2;
Vikram S. Adve242a8082002-05-19 15:25:51 +0000675 unsigned int lcost = CreateMulConstInstruction(target, F, lval, rval,
676 destVal, mvec1, mcfi);
677 unsigned int rcost = CreateMulConstInstruction(target, F, rval, lval,
678 destVal, mvec2, mcfi);
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000679 vector<MachineInstr*>& mincostMvec = (lcost <= rcost)? mvec1 : mvec2;
680 vector<MachineInstr*>& maxcostMvec = (lcost <= rcost)? mvec2 : mvec1;
681 mvec.insert(mvec.end(), mincostMvec.begin(), mincostMvec.end());
682
683 for (unsigned int i=0; i < maxcostMvec.size(); ++i)
684 delete maxcostMvec[i];
685 }
686 else if (isa<Constant>(rval)) // rval is constant, but not lval
Vikram S. Adve242a8082002-05-19 15:25:51 +0000687 CreateMulConstInstruction(target, F, lval, rval, destVal, mvec, mcfi);
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000688 else if (isa<Constant>(lval)) // lval is constant, but not rval
Vikram S. Adve242a8082002-05-19 15:25:51 +0000689 CreateMulConstInstruction(target, F, lval, rval, destVal, mvec, mcfi);
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000690
691 // else neither is constant
692 return;
693}
694
Vikram S. Adve74825322002-03-18 03:15:35 +0000695// Return NULL if we cannot exploit constant to create a cheaper instruction
696static inline void
Vikram S. Adve242a8082002-05-19 15:25:51 +0000697CreateMulInstruction(const TargetMachine &target, Function* F,
698 Value* lval, Value* rval, Instruction* destVal,
Vikram S. Adve74825322002-03-18 03:15:35 +0000699 vector<MachineInstr*>& mvec,
Vikram S. Adve242a8082002-05-19 15:25:51 +0000700 MachineCodeForInstruction& mcfi,
Vikram S. Adve74825322002-03-18 03:15:35 +0000701 MachineOpCode forceMulOp = INVALID_MACHINE_OPCODE)
702{
703 unsigned int L = mvec.size();
Vikram S. Adve242a8082002-05-19 15:25:51 +0000704 CreateCheapestMulConstInstruction(target,F, lval, rval, destVal, mvec, mcfi);
Vikram S. Adve74825322002-03-18 03:15:35 +0000705 if (mvec.size() == L)
706 { // no instructions were added so create MUL reg, reg, reg.
707 // Use FSMULD if both operands are actually floats cast to doubles.
708 // Otherwise, use the default opcode for the appropriate type.
709 MachineOpCode mulOp = ((forceMulOp != INVALID_MACHINE_OPCODE)
710 ? forceMulOp
711 : ChooseMulInstructionByType(destVal->getType()));
712 MachineInstr* M = new MachineInstr(mulOp);
713 M->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister, lval);
714 M->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister, rval);
715 M->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister, destVal);
716 mvec.push_back(M);
717 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000718}
719
720
Vikram S. Adve510eec72001-11-04 21:59:14 +0000721// Generate a divide instruction for Div or Rem.
722// For Rem, this assumes that the operand type will be signed if the result
723// type is signed. This is correct because they must have the same sign.
724//
Chris Lattner20b1ea02001-09-14 03:47:57 +0000725static inline MachineOpCode
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000726ChooseDivInstruction(TargetMachine &target,
727 const InstructionNode* instrNode)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000728{
729 MachineOpCode opCode = INVALID_OPCODE;
730
731 const Type* resultType = instrNode->getInstruction()->getType();
732
Chris Lattner0c4e8862002-09-03 01:08:28 +0000733 if (resultType->isInteger())
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000734 opCode = resultType->isSigned()? SDIVX : UDIVX;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000735 else
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000736 switch(resultType->getPrimitiveID())
737 {
738 case Type::FloatTyID: opCode = FDIVS; break;
739 case Type::DoubleTyID: opCode = FDIVD; break;
740 default: assert(0 && "Invalid type for DIV instruction"); break;
741 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000742
743 return opCode;
744}
745
746
Vikram S. Adve74825322002-03-18 03:15:35 +0000747// Return NULL if we cannot exploit constant to create a cheaper instruction
748static inline void
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000749CreateDivConstInstruction(TargetMachine &target,
750 const InstructionNode* instrNode,
Vikram S. Adve74825322002-03-18 03:15:35 +0000751 vector<MachineInstr*>& mvec)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000752{
Vikram S. Adve74825322002-03-18 03:15:35 +0000753 MachineInstr* minstr1 = NULL;
754 MachineInstr* minstr2 = NULL;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000755
756 Value* constOp = ((InstrTreeNode*) instrNode->rightChild())->getValue();
Vikram S. Adve74825322002-03-18 03:15:35 +0000757 if (! isa<Constant>(constOp))
758 return;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000759
760 // Cases worth optimizing are:
761 // (1) Divide by 1 for any type: replace with copy (ADD or FMOV)
762 // (2) Divide by 2^x for integer types: replace with SR[L or A]{X}
763 //
764 const Type* resultType = instrNode->getInstruction()->getType();
765
Chris Lattner0c4e8862002-09-03 01:08:28 +0000766 if (resultType->isInteger())
Chris Lattner20b1ea02001-09-14 03:47:57 +0000767 {
768 unsigned pow;
769 bool isValidConst;
770 int64_t C = GetConstantValueAsSignedInt(constOp, isValidConst);
771 if (isValidConst)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000772 {
773 bool needNeg = false;
774 if (C < 0)
775 {
776 needNeg = true;
777 C = -C;
778 }
779
780 if (C == 1)
781 {
Vikram S. Adve74825322002-03-18 03:15:35 +0000782 minstr1 = new MachineInstr(ADD);
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000783 minstr1->SetMachineOperandVal(0,
784 MachineOperand::MO_VirtualRegister,
785 instrNode->leftChild()->getValue());
786 minstr1->SetMachineOperandReg(1,
787 target.getRegInfo().getZeroRegNum());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000788 }
Chris Lattner36346c72002-05-19 21:20:19 +0000789 else if (isPowerOf2(C, pow))
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000790 {
791 MachineOpCode opCode= ((resultType->isSigned())
792 ? (resultType==Type::LongTy)? SRAX : SRA
793 : (resultType==Type::LongTy)? SRLX : SRL);
Vikram S. Adve74825322002-03-18 03:15:35 +0000794 minstr1 = new MachineInstr(opCode);
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000795 minstr1->SetMachineOperandVal(0,
796 MachineOperand::MO_VirtualRegister,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000797 instrNode->leftChild()->getValue());
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000798 minstr1->SetMachineOperandConst(1,
799 MachineOperand::MO_UnextendedImmed,
800 pow);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000801 }
802
Vikram S. Adve74825322002-03-18 03:15:35 +0000803 if (minstr1 && needNeg)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000804 { // insert <reg = SUB 0, reg> after the instr to flip the sign
Vikram S. Adve74825322002-03-18 03:15:35 +0000805 minstr2 = CreateIntNegInstruction(target,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000806 instrNode->getValue());
807 }
808 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000809 }
810 else
811 {
Chris Lattner9b625032002-05-06 16:15:30 +0000812 if (ConstantFP *FPC = dyn_cast<ConstantFP>(constOp))
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000813 {
Chris Lattner9b625032002-05-06 16:15:30 +0000814 double dval = FPC->getValue();
Vikram S. Adve6ad7c552001-11-09 02:18:16 +0000815 if (fabs(dval) == 1)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000816 {
817 bool needNeg = (dval < 0);
818
819 MachineOpCode opCode = needNeg
820 ? (resultType == Type::FloatTy? FNEGS : FNEGD)
821 : (resultType == Type::FloatTy? FMOVS : FMOVD);
822
Vikram S. Adve74825322002-03-18 03:15:35 +0000823 minstr1 = new MachineInstr(opCode);
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000824 minstr1->SetMachineOperandVal(0,
825 MachineOperand::MO_VirtualRegister,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000826 instrNode->leftChild()->getValue());
827 }
828 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000829 }
830
Vikram S. Adve74825322002-03-18 03:15:35 +0000831 if (minstr1 != NULL)
832 minstr1->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister,
833 instrNode->getValue());
Chris Lattner20b1ea02001-09-14 03:47:57 +0000834
Vikram S. Adve74825322002-03-18 03:15:35 +0000835 if (minstr1)
836 mvec.push_back(minstr1);
837 if (minstr2)
838 mvec.push_back(minstr2);
Chris Lattner20b1ea02001-09-14 03:47:57 +0000839}
840
841
Vikram S. Adve74825322002-03-18 03:15:35 +0000842static void
843CreateCodeForVariableSizeAlloca(const TargetMachine& target,
844 Instruction* result,
845 unsigned int tsize,
846 Value* numElementsVal,
847 vector<MachineInstr*>& getMvec)
848{
849 MachineInstr* M;
850
851 // Create a Value to hold the (constant) element size
852 Value* tsizeVal = ConstantSInt::get(Type::IntTy, tsize);
853
854 // Get the constant offset from SP for dynamically allocated storage
855 // and create a temporary Value to hold it.
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000856 assert(result && result->getParent() && "Result value is not part of a fn?");
857 Function *F = result->getParent()->getParent();
858 MachineCodeForMethod& mcInfo = MachineCodeForMethod::get(F);
Vikram S. Adve74825322002-03-18 03:15:35 +0000859 bool growUp;
860 ConstantSInt* dynamicAreaOffset =
861 ConstantSInt::get(Type::IntTy,
862 target.getFrameInfo().getDynamicAreaOffset(mcInfo,growUp));
863 assert(! growUp && "Has SPARC v9 stack frame convention changed?");
864
865 // Create a temporary value to hold the result of MUL
866 TmpInstruction* tmpProd = new TmpInstruction(numElementsVal, tsizeVal);
867 MachineCodeForInstruction::get(result).addTemp(tmpProd);
868
869 // Instruction 1: mul numElements, typeSize -> tmpProd
870 M = new MachineInstr(MULX);
871 M->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister, numElementsVal);
872 M->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister, tsizeVal);
873 M->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister, tmpProd);
874 getMvec.push_back(M);
875
876 // Instruction 2: sub %sp, tmpProd -> %sp
877 M = new MachineInstr(SUB);
878 M->SetMachineOperandReg(0, target.getRegInfo().getStackPointer());
879 M->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister, tmpProd);
880 M->SetMachineOperandReg(2, target.getRegInfo().getStackPointer());
881 getMvec.push_back(M);
882
883 // Instruction 3: add %sp, frameSizeBelowDynamicArea -> result
884 M = new MachineInstr(ADD);
885 M->SetMachineOperandReg(0, target.getRegInfo().getStackPointer());
886 M->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister, dynamicAreaOffset);
887 M->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister, result);
888 getMvec.push_back(M);
889}
890
891
892static void
893CreateCodeForFixedSizeAlloca(const TargetMachine& target,
894 Instruction* result,
895 unsigned int tsize,
896 unsigned int numElements,
897 vector<MachineInstr*>& getMvec)
898{
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000899 assert(result && result->getParent() &&
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000900 "Result value is not part of a function?");
901 Function *F = result->getParent()->getParent();
902 MachineCodeForMethod &mcInfo = MachineCodeForMethod::get(F);
Vikram S. Adve74825322002-03-18 03:15:35 +0000903
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000904 // Check if the offset would small enough to use as an immediate in
905 // load/stores (check LDX because all load/stores have the same-size immediate
906 // field). If not, put the variable in the dynamically sized area of the
907 // frame.
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000908 unsigned int paddedSizeIgnored;
Vikram S. Adve74825322002-03-18 03:15:35 +0000909 int offsetFromFP = mcInfo.computeOffsetforLocalVar(target, result,
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000910 paddedSizeIgnored,
Vikram S. Adve74825322002-03-18 03:15:35 +0000911 tsize * numElements);
912 if (! target.getInstrInfo().constantFitsInImmedField(LDX, offsetFromFP))
913 {
914 CreateCodeForVariableSizeAlloca(target, result, tsize,
915 ConstantSInt::get(Type::IntTy,numElements),
916 getMvec);
917 return;
918 }
919
920 // else offset fits in immediate field so go ahead and allocate it.
921 offsetFromFP = mcInfo.allocateLocalVar(target, result, tsize * numElements);
922
923 // Create a temporary Value to hold the constant offset.
924 // This is needed because it may not fit in the immediate field.
925 ConstantSInt* offsetVal = ConstantSInt::get(Type::IntTy, offsetFromFP);
926
927 // Instruction 1: add %fp, offsetFromFP -> result
928 MachineInstr* M = new MachineInstr(ADD);
929 M->SetMachineOperandReg(0, target.getRegInfo().getFramePointer());
930 M->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister, offsetVal);
931 M->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister, result);
932
933 getMvec.push_back(M);
934}
935
936
Chris Lattner20b1ea02001-09-14 03:47:57 +0000937//------------------------------------------------------------------------
938// Function SetOperandsForMemInstr
939//
940// Choose addressing mode for the given load or store instruction.
941// Use [reg+reg] if it is an indexed reference, and the index offset is
942// not a constant or if it cannot fit in the offset field.
943// Use [reg+offset] in all other cases.
944//
945// This assumes that all array refs are "lowered" to one of these forms:
946// %x = load (subarray*) ptr, constant ; single constant offset
947// %x = load (subarray*) ptr, offsetVal ; single non-constant offset
948// Generally, this should happen via strength reduction + LICM.
949// Also, strength reduction should take care of using the same register for
950// the loop index variable and an array index, when that is profitable.
951//------------------------------------------------------------------------
952
953static void
Vikram S. Adve74825322002-03-18 03:15:35 +0000954SetOperandsForMemInstr(vector<MachineInstr*>& mvec,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000955 const InstructionNode* vmInstrNode,
956 const TargetMachine& target)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000957{
Vikram S. Adve8cfffd32002-08-24 20:56:53 +0000958 Instruction* memInst = vmInstrNode->getInstruction();
959 vector<MachineInstr*>::iterator mvecI = mvec.end() - 1;
960
961 // Index vector, ptr value, and flag if all indices are const.
Vikram S. Advea10d1a72002-03-31 19:07:35 +0000962 vector<Value*> idxVec;
Vikram S. Adve8cfffd32002-08-24 20:56:53 +0000963 bool allConstantIndices;
964 Value* ptrVal = GetMemInstArgs(vmInstrNode, idxVec, allConstantIndices);
Vikram S. Adveed3fefb2002-08-03 13:48:21 +0000965
Vikram S. Adve8cfffd32002-08-24 20:56:53 +0000966 // Now create the appropriate operands for the machine instruction.
967 // First, initialize so we default to storing the offset in a register.
Chris Lattner8e5c0b42001-11-07 14:01:59 +0000968 int64_t smallConstOffset = 0;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000969 Value* valueForRegOffset = NULL;
Vikram S. Adveed3fefb2002-08-03 13:48:21 +0000970 MachineOperand::MachineOperandType offsetOpType =
971 MachineOperand::MO_VirtualRegister;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000972
Vikram S. Adve74825322002-03-18 03:15:35 +0000973 // Check if there is an index vector and if so, compute the
974 // right offset for structures and for arrays
Chris Lattner20b1ea02001-09-14 03:47:57 +0000975 //
Chris Lattner3bb8ad22002-08-22 23:37:24 +0000976 if (!idxVec.empty())
Chris Lattner20b1ea02001-09-14 03:47:57 +0000977 {
Vikram S. Adve74825322002-03-18 03:15:35 +0000978 const PointerType* ptrType = cast<PointerType>(ptrVal->getType());
Chris Lattner20b1ea02001-09-14 03:47:57 +0000979
Vikram S. Adve242a8082002-05-19 15:25:51 +0000980 // If all indices are constant, compute the combined offset directly.
981 if (allConstantIndices)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000982 {
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000983 // Compute the offset value using the index vector. Create a
984 // virtual reg. for it since it may not fit in the immed field.
Vikram S. Adve242a8082002-05-19 15:25:51 +0000985 uint64_t offset = target.DataLayout.getIndexedOffset(ptrType,idxVec);
986 valueForRegOffset = ConstantSInt::get(Type::LongTy, offset);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000987 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000988 else
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000989 {
Vikram S. Adve242a8082002-05-19 15:25:51 +0000990 // There is at least one non-constant offset. Therefore, this must
Vikram S. Adveed3fefb2002-08-03 13:48:21 +0000991 // be an array ref, and must have been lowered to a single non-zero
992 // offset. (An extra leading zero offset, if any, can be ignored.)
993 // Generate code sequence to compute address from index.
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000994 //
Chris Lattner0374b8d2002-09-11 01:21:35 +0000995 bool firstIdxIsZero =
996 (idxVec[0] == Constant::getNullValue(idxVec[0]->getType()));
Vikram S. Advebabc0fa2002-09-05 18:32:13 +0000997 assert(idxVec.size() == 1U + firstIdxIsZero
Vikram S. Adveed3fefb2002-08-03 13:48:21 +0000998 && "Array refs must be lowered before Instruction Selection");
999
Vikram S. Advebabc0fa2002-09-05 18:32:13 +00001000 Value* idxVal = idxVec[firstIdxIsZero];
Vikram S. Adveed3fefb2002-08-03 13:48:21 +00001001
1002 vector<MachineInstr*> mulVec;
1003 Instruction* addr = new TmpInstruction(Type::UIntTy, memInst);
1004 MachineCodeForInstruction::get(memInst).addTemp(addr);
1005
Vikram S. Advebabc0fa2002-09-05 18:32:13 +00001006 // Get the array type indexed by idxVal, and compute its element size.
Vikram S. Adveed3fefb2002-08-03 13:48:21 +00001007 // The call to getTypeSize() will fail if size is not constant.
Vikram S. Advebabc0fa2002-09-05 18:32:13 +00001008 const Type* vecType = (firstIdxIsZero
1009 ? GetElementPtrInst::getIndexedType(ptrType,
1010 std::vector<Value*>(1U, idxVec[0]),
1011 /*AllowCompositeLeaf*/ true)
1012 : ptrType);
1013 const Type* eltType = cast<SequentialType>(vecType)->getElementType();
Vikram S. Advee102a642002-09-16 15:56:45 +00001014 ConstantUInt* eltSizeVal = ConstantUInt::get(Type::ULongTy,
Vikram S. Advebabc0fa2002-09-05 18:32:13 +00001015 target.DataLayout.getTypeSize(eltType));
Vikram S. Adveed3fefb2002-08-03 13:48:21 +00001016
1017 // CreateMulInstruction() folds constants intelligently enough.
1018 CreateMulInstruction(target,
1019 memInst->getParent()->getParent(),
Vikram S. Advebabc0fa2002-09-05 18:32:13 +00001020 idxVal, /* lval, not likely to be const*/
1021 eltSizeVal, /* rval, likely to be constant */
1022 addr, /* result */
Vikram S. Adveed3fefb2002-08-03 13:48:21 +00001023 mulVec,
1024 MachineCodeForInstruction::get(memInst),
1025 INVALID_MACHINE_OPCODE);
1026
Vikram S. Adve8cfffd32002-08-24 20:56:53 +00001027 // Sign-extend the result of MUL from 32 to 64 bits.
1028 target.getInstrInfo().CreateSignExtensionInstructions(target, memInst->getParent()->getParent(), addr, /*srcSizeInBits*/32, addr, mulVec, MachineCodeForInstruction::get(memInst));
1029
Vikram S. Adveed3fefb2002-08-03 13:48:21 +00001030 // Insert mulVec[] before *mvecI in mvec[] and update mvecI
1031 // to point to the same instruction it pointed to before.
1032 assert(mulVec.size() > 0 && "No multiply code created?");
1033 vector<MachineInstr*>::iterator oldMvecI = mvecI;
1034 for (unsigned i=0, N=mulVec.size(); i < N; ++i)
1035 mvecI = mvec.insert(mvecI, mulVec[i]) + 1; // pts to mem instr
1036
1037 valueForRegOffset = addr;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001038 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00001039 }
1040 else
1041 {
1042 offsetOpType = MachineOperand::MO_SignExtendedImmed;
1043 smallConstOffset = 0;
1044 }
Vikram S. Adveed3fefb2002-08-03 13:48:21 +00001045
Vikram S. Advea10d1a72002-03-31 19:07:35 +00001046 // For STORE:
1047 // Operand 0 is value, operand 1 is ptr, operand 2 is offset
1048 // For LOAD or GET_ELEMENT_PTR,
1049 // Operand 0 is ptr, operand 1 is offset, operand 2 is result.
1050 //
1051 unsigned offsetOpNum, ptrOpNum;
1052 if (memInst->getOpcode() == Instruction::Store)
1053 {
1054 (*mvecI)->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister,
1055 vmInstrNode->leftChild()->getValue());
1056 ptrOpNum = 1;
1057 offsetOpNum = 2;
1058 }
1059 else
1060 {
1061 ptrOpNum = 0;
1062 offsetOpNum = 1;
1063 (*mvecI)->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister,
1064 memInst);
1065 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00001066
Vikram S. Advea10d1a72002-03-31 19:07:35 +00001067 (*mvecI)->SetMachineOperandVal(ptrOpNum, MachineOperand::MO_VirtualRegister,
1068 ptrVal);
1069
Chris Lattner20b1ea02001-09-14 03:47:57 +00001070 if (offsetOpType == MachineOperand::MO_VirtualRegister)
1071 {
1072 assert(valueForRegOffset != NULL);
Vikram S. Adve74825322002-03-18 03:15:35 +00001073 (*mvecI)->SetMachineOperandVal(offsetOpNum, offsetOpType,
1074 valueForRegOffset);
Chris Lattner20b1ea02001-09-14 03:47:57 +00001075 }
1076 else
Vikram S. Adve74825322002-03-18 03:15:35 +00001077 (*mvecI)->SetMachineOperandConst(offsetOpNum, offsetOpType,
1078 smallConstOffset);
Chris Lattner20b1ea02001-09-14 03:47:57 +00001079}
1080
1081
Chris Lattner20b1ea02001-09-14 03:47:57 +00001082//
1083// Substitute operand `operandNum' of the instruction in node `treeNode'
Vikram S. Advec025fc12001-10-14 23:28:43 +00001084// in place of the use(s) of that instruction in node `parent'.
1085// Check both explicit and implicit operands!
Vikram S. Adve74825322002-03-18 03:15:35 +00001086// Also make sure to skip over a parent who:
1087// (1) is a list node in the Burg tree, or
1088// (2) itself had its results forwarded to its parent
Chris Lattner20b1ea02001-09-14 03:47:57 +00001089//
1090static void
1091ForwardOperand(InstructionNode* treeNode,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001092 InstrTreeNode* parent,
1093 int operandNum)
Chris Lattner20b1ea02001-09-14 03:47:57 +00001094{
Vikram S. Adve243dd452001-09-18 13:03:13 +00001095 assert(treeNode && parent && "Invalid invocation of ForwardOperand");
1096
Chris Lattner20b1ea02001-09-14 03:47:57 +00001097 Instruction* unusedOp = treeNode->getInstruction();
1098 Value* fwdOp = unusedOp->getOperand(operandNum);
Vikram S. Adve243dd452001-09-18 13:03:13 +00001099
1100 // The parent itself may be a list node, so find the real parent instruction
1101 while (parent->getNodeType() != InstrTreeNode::NTInstructionNode)
1102 {
1103 parent = parent->parent();
1104 assert(parent && "ERROR: Non-instruction node has no parent in tree.");
1105 }
1106 InstructionNode* parentInstrNode = (InstructionNode*) parent;
1107
1108 Instruction* userInstr = parentInstrNode->getInstruction();
Chris Lattner9c461082002-02-03 07:50:56 +00001109 MachineCodeForInstruction &mvec = MachineCodeForInstruction::get(userInstr);
Vikram S. Adve74825322002-03-18 03:15:35 +00001110
1111 // The parent's mvec would be empty if it was itself forwarded.
1112 // Recursively call ForwardOperand in that case...
1113 //
1114 if (mvec.size() == 0)
Chris Lattner20b1ea02001-09-14 03:47:57 +00001115 {
Vikram S. Adve74825322002-03-18 03:15:35 +00001116 assert(parent->parent() != NULL &&
1117 "Parent could not have been forwarded, yet has no instructions?");
1118 ForwardOperand(treeNode, parent->parent(), operandNum);
1119 }
1120 else
1121 {
Vikram S. Adve74825322002-03-18 03:15:35 +00001122 for (unsigned i=0, N=mvec.size(); i < N; i++)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001123 {
Vikram S. Adve74825322002-03-18 03:15:35 +00001124 MachineInstr* minstr = mvec[i];
1125 for (unsigned i=0, numOps=minstr->getNumOperands(); i < numOps; ++i)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001126 {
Vikram S. Adve74825322002-03-18 03:15:35 +00001127 const MachineOperand& mop = minstr->getOperand(i);
1128 if (mop.getOperandType() == MachineOperand::MO_VirtualRegister &&
1129 mop.getVRegValue() == unusedOp)
Vikram S. Adve242a8082002-05-19 15:25:51 +00001130 minstr->SetMachineOperandVal(i,
Vikram S. Adve74825322002-03-18 03:15:35 +00001131 MachineOperand::MO_VirtualRegister, fwdOp);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001132 }
Vikram S. Adve74825322002-03-18 03:15:35 +00001133
1134 for (unsigned i=0,numOps=minstr->getNumImplicitRefs(); i<numOps; ++i)
1135 if (minstr->getImplicitRef(i) == unusedOp)
Vikram S. Adve242a8082002-05-19 15:25:51 +00001136 minstr->setImplicitRef(i, fwdOp,
Vikram S. Adve6418eac2002-07-08 23:30:14 +00001137 minstr->implicitRefIsDefined(i),
1138 minstr->implicitRefIsDefinedAndUsed(i));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001139 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00001140 }
1141}
1142
1143
Vikram S. Adve242a8082002-05-19 15:25:51 +00001144inline bool
1145AllUsesAreBranches(const Instruction* setccI)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001146{
Vikram S. Adve242a8082002-05-19 15:25:51 +00001147 for (Value::use_const_iterator UI=setccI->use_begin(), UE=setccI->use_end();
1148 UI != UE; ++UI)
1149 if (! isa<TmpInstruction>(*UI) // ignore tmp instructions here
1150 && cast<Instruction>(*UI)->getOpcode() != Instruction::Br)
1151 return false;
1152 return true;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001153}
1154
Vikram S. Advefb361122001-10-22 13:36:31 +00001155//******************* Externally Visible Functions *************************/
1156
Vikram S. Advefb361122001-10-22 13:36:31 +00001157//------------------------------------------------------------------------
1158// External Function: ThisIsAChainRule
1159//
1160// Purpose:
1161// Check if a given BURG rule is a chain rule.
1162//------------------------------------------------------------------------
1163
1164extern bool
1165ThisIsAChainRule(int eruleno)
1166{
1167 switch(eruleno)
1168 {
1169 case 111: // stmt: reg
Vikram S. Advefb361122001-10-22 13:36:31 +00001170 case 123:
1171 case 124:
1172 case 125:
1173 case 126:
1174 case 127:
1175 case 128:
1176 case 129:
1177 case 130:
1178 case 131:
1179 case 132:
1180 case 133:
1181 case 155:
1182 case 221:
1183 case 222:
1184 case 241:
1185 case 242:
1186 case 243:
1187 case 244:
Vikram S. Adve30a6f492002-08-22 02:56:10 +00001188 case 245:
Vikram S. Adve85e1e9c2002-04-01 20:28:48 +00001189 case 321:
Vikram S. Advefb361122001-10-22 13:36:31 +00001190 return true; break;
Vikram S. Adve30a6f492002-08-22 02:56:10 +00001191
Vikram S. Advefb361122001-10-22 13:36:31 +00001192 default:
1193 return false; break;
1194 }
1195}
Chris Lattner20b1ea02001-09-14 03:47:57 +00001196
1197
1198//------------------------------------------------------------------------
1199// External Function: GetInstructionsByRule
1200//
1201// Purpose:
1202// Choose machine instructions for the SPARC according to the
1203// patterns chosen by the BURG-generated parser.
1204//------------------------------------------------------------------------
1205
Vikram S. Adve74825322002-03-18 03:15:35 +00001206void
Chris Lattner20b1ea02001-09-14 03:47:57 +00001207GetInstructionsByRule(InstructionNode* subtreeRoot,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001208 int ruleForNode,
1209 short* nts,
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001210 TargetMachine &target,
Vikram S. Adve74825322002-03-18 03:15:35 +00001211 vector<MachineInstr*>& mvec)
Chris Lattner20b1ea02001-09-14 03:47:57 +00001212{
Chris Lattner20b1ea02001-09-14 03:47:57 +00001213 bool checkCast = false; // initialize here to use fall-through
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001214 bool maskUnsignedResult = false;
Chris Lattner20b1ea02001-09-14 03:47:57 +00001215 int nextRule;
1216 int forwardOperandNum = -1;
Vikram S. Adve74825322002-03-18 03:15:35 +00001217 unsigned int allocaSize = 0;
1218 MachineInstr* M, *M2;
1219 unsigned int L;
1220
1221 mvec.clear();
Chris Lattner20b1ea02001-09-14 03:47:57 +00001222
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001223 // If the code for this instruction was folded into the parent (user),
1224 // then do nothing!
1225 if (subtreeRoot->isFoldedIntoParent())
1226 return;
1227
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001228 //
1229 // Let's check for chain rules outside the switch so that we don't have
1230 // to duplicate the list of chain rule production numbers here again
1231 //
1232 if (ThisIsAChainRule(ruleForNode))
Chris Lattner20b1ea02001-09-14 03:47:57 +00001233 {
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001234 // Chain rules have a single nonterminal on the RHS.
1235 // Get the rule that matches the RHS non-terminal and use that instead.
1236 //
1237 assert(nts[0] && ! nts[1]
1238 && "A chain rule should have only one RHS non-terminal!");
1239 nextRule = burm_rule(subtreeRoot->state, nts[0]);
1240 nts = burm_nts[nextRule];
Vikram S. Adve74825322002-03-18 03:15:35 +00001241 GetInstructionsByRule(subtreeRoot, nextRule, nts, target, mvec);
Chris Lattner20b1ea02001-09-14 03:47:57 +00001242 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001243 else
Chris Lattner20b1ea02001-09-14 03:47:57 +00001244 {
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001245 switch(ruleForNode) {
1246 case 1: // stmt: Ret
1247 case 2: // stmt: RetValue(reg)
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001248 { // NOTE: Prepass of register allocation is responsible
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001249 // for moving return value to appropriate register.
1250 // Mark the return-address register as a hidden virtual reg.
Vikram S. Advea995e602001-10-11 04:23:19 +00001251 // Mark the return value register as an implicit ref of
1252 // the machine instruction.
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001253 // Finally put a NOP in the delay slot.
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001254 ReturnInst *returnInstr =
1255 cast<ReturnInst>(subtreeRoot->getInstruction());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001256 assert(returnInstr->getOpcode() == Instruction::Ret);
1257
Chris Lattner9c461082002-02-03 07:50:56 +00001258 Instruction* returnReg = new TmpInstruction(returnInstr);
1259 MachineCodeForInstruction::get(returnInstr).addTemp(returnReg);
Vikram S. Advefb361122001-10-22 13:36:31 +00001260
Vikram S. Adve74825322002-03-18 03:15:35 +00001261 M = new MachineInstr(JMPLRET);
1262 M->SetMachineOperandReg(0, MachineOperand::MO_VirtualRegister,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001263 returnReg);
Vikram S. Adve74825322002-03-18 03:15:35 +00001264 M->SetMachineOperandConst(1,MachineOperand::MO_SignExtendedImmed,
Chris Lattner697954c2002-01-20 22:54:45 +00001265 (int64_t)8);
Vikram S. Adve74825322002-03-18 03:15:35 +00001266 M->SetMachineOperandReg(2, target.getRegInfo().getZeroRegNum());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001267
Vikram S. Advea995e602001-10-11 04:23:19 +00001268 if (returnInstr->getReturnValue() != NULL)
Vikram S. Adve74825322002-03-18 03:15:35 +00001269 M->addImplicitRef(returnInstr->getReturnValue());
Vikram S. Advea995e602001-10-11 04:23:19 +00001270
Vikram S. Adve74825322002-03-18 03:15:35 +00001271 mvec.push_back(M);
1272 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001273
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001274 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001275 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001276
1277 case 3: // stmt: Store(reg,reg)
1278 case 4: // stmt: Store(reg,ptrreg)
Vikram S. Adve74825322002-03-18 03:15:35 +00001279 mvec.push_back(new MachineInstr(
1280 ChooseStoreInstruction(
1281 subtreeRoot->leftChild()->getValue()->getType())));
Vikram S. Adve8cfffd32002-08-24 20:56:53 +00001282 SetOperandsForMemInstr(mvec, subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001283 break;
Chris Lattner20b1ea02001-09-14 03:47:57 +00001284
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001285 case 5: // stmt: BrUncond
Vikram S. Adve74825322002-03-18 03:15:35 +00001286 M = new MachineInstr(BA);
Vikram S. Adve6418eac2002-07-08 23:30:14 +00001287 M->SetMachineOperandVal(0, MachineOperand::MO_PCRelativeDisp,
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001288 cast<BranchInst>(subtreeRoot->getInstruction())->getSuccessor(0));
Vikram S. Adve74825322002-03-18 03:15:35 +00001289 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001290
1291 // delay slot
Vikram S. Adve74825322002-03-18 03:15:35 +00001292 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001293 break;
Chris Lattner20b1ea02001-09-14 03:47:57 +00001294
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001295 case 206: // stmt: BrCond(setCCconst)
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001296 { // setCCconst => boolean was computed with `%b = setCC type reg1 const'
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001297 // If the constant is ZERO, we can use the branch-on-integer-register
1298 // instructions and avoid the SUBcc instruction entirely.
1299 // Otherwise this is just the same as case 5, so just fall through.
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001300 //
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001301 InstrTreeNode* constNode = subtreeRoot->leftChild()->rightChild();
1302 assert(constNode &&
1303 constNode->getNodeType() ==InstrTreeNode::NTConstNode);
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001304 Constant *constVal = cast<Constant>(constNode->getValue());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001305 bool isValidConst;
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001306
Chris Lattner0c4e8862002-09-03 01:08:28 +00001307 if ((constVal->getType()->isInteger()
Chris Lattner9b625032002-05-06 16:15:30 +00001308 || isa<PointerType>(constVal->getType()))
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001309 && GetConstantValueAsSignedInt(constVal, isValidConst) == 0
1310 && isValidConst)
1311 {
1312 // That constant is a zero after all...
1313 // Use the left child of setCC as the first argument!
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001314 // Mark the setCC node so that no code is generated for it.
1315 InstructionNode* setCCNode = (InstructionNode*)
1316 subtreeRoot->leftChild();
1317 assert(setCCNode->getOpLabel() == SetCCOp);
1318 setCCNode->markFoldedIntoParent();
1319
1320 BranchInst* brInst=cast<BranchInst>(subtreeRoot->getInstruction());
1321
Vikram S. Adve74825322002-03-18 03:15:35 +00001322 M = new MachineInstr(ChooseBprInstruction(subtreeRoot));
1323 M->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister,
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001324 setCCNode->leftChild()->getValue());
Vikram S. Adve74825322002-03-18 03:15:35 +00001325 M->SetMachineOperandVal(1, MachineOperand::MO_PCRelativeDisp,
1326 brInst->getSuccessor(0));
1327 mvec.push_back(M);
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001328
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001329 // delay slot
Vikram S. Adve74825322002-03-18 03:15:35 +00001330 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001331
1332 // false branch
Vikram S. Adve74825322002-03-18 03:15:35 +00001333 M = new MachineInstr(BA);
Vikram S. Adve6418eac2002-07-08 23:30:14 +00001334 M->SetMachineOperandVal(0, MachineOperand::MO_PCRelativeDisp,
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001335 brInst->getSuccessor(1));
Vikram S. Adve74825322002-03-18 03:15:35 +00001336 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001337
1338 // delay slot
Vikram S. Adve74825322002-03-18 03:15:35 +00001339 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001340
1341 break;
1342 }
1343 // ELSE FALL THROUGH
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001344 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001345
Vikram S. Adve30a6f492002-08-22 02:56:10 +00001346 case 6: // stmt: BrCond(setCC)
1347 { // bool => boolean was computed with SetCC.
1348 // The branch to use depends on whether it is FP, signed, or unsigned.
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001349 // If it is an integer CC, we also need to find the unique
1350 // TmpInstruction representing that CC.
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001351 //
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001352 BranchInst* brInst = cast<BranchInst>(subtreeRoot->getInstruction());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001353 bool isFPBranch;
Vikram S. Adve74825322002-03-18 03:15:35 +00001354 M = new MachineInstr(ChooseBccInstruction(subtreeRoot, isFPBranch));
Vikram S. Adve30a6f492002-08-22 02:56:10 +00001355
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001356 Value* ccValue = GetTmpForCC(subtreeRoot->leftChild()->getValue(),
1357 brInst->getParent()->getParent(),
1358 isFPBranch? Type::FloatTy : Type::IntTy);
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001359
Vikram S. Adve74825322002-03-18 03:15:35 +00001360 M->SetMachineOperandVal(0, MachineOperand::MO_CCRegister, ccValue);
1361 M->SetMachineOperandVal(1, MachineOperand::MO_PCRelativeDisp,
1362 brInst->getSuccessor(0));
1363 mvec.push_back(M);
Vikram S. Adve30a6f492002-08-22 02:56:10 +00001364
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001365 // delay slot
Vikram S. Adve74825322002-03-18 03:15:35 +00001366 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adve30a6f492002-08-22 02:56:10 +00001367
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001368 // false branch
Vikram S. Adve74825322002-03-18 03:15:35 +00001369 M = new MachineInstr(BA);
Vikram S. Adve6418eac2002-07-08 23:30:14 +00001370 M->SetMachineOperandVal(0, MachineOperand::MO_PCRelativeDisp,
Vikram S. Adve74825322002-03-18 03:15:35 +00001371 brInst->getSuccessor(1));
1372 mvec.push_back(M);
Vikram S. Adve30a6f492002-08-22 02:56:10 +00001373
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001374 // delay slot
Vikram S. Adve74825322002-03-18 03:15:35 +00001375 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001376 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001377 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001378
1379 case 208: // stmt: BrCond(boolconst)
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001380 {
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001381 // boolconst => boolean is a constant; use BA to first or second label
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001382 Constant* constVal =
1383 cast<Constant>(subtreeRoot->leftChild()->getValue());
1384 unsigned dest = cast<ConstantBool>(constVal)->getValue()? 0 : 1;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001385
Vikram S. Adve74825322002-03-18 03:15:35 +00001386 M = new MachineInstr(BA);
Vikram S. Adve6418eac2002-07-08 23:30:14 +00001387 M->SetMachineOperandVal(0, MachineOperand::MO_PCRelativeDisp,
Chris Lattner35504202002-04-27 03:14:39 +00001388 cast<BranchInst>(subtreeRoot->getInstruction())->getSuccessor(dest));
Vikram S. Adve74825322002-03-18 03:15:35 +00001389 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001390
1391 // delay slot
Vikram S. Adve74825322002-03-18 03:15:35 +00001392 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001393 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001394 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001395
1396 case 8: // stmt: BrCond(boolreg)
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001397 { // boolreg => boolean is stored in an existing register.
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001398 // Just use the branch-on-integer-register instruction!
1399 //
Vikram S. Adve74825322002-03-18 03:15:35 +00001400 M = new MachineInstr(BRNZ);
1401 M->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001402 subtreeRoot->leftChild()->getValue());
Vikram S. Adve74825322002-03-18 03:15:35 +00001403 M->SetMachineOperandVal(1, MachineOperand::MO_PCRelativeDisp,
Chris Lattner35504202002-04-27 03:14:39 +00001404 cast<BranchInst>(subtreeRoot->getInstruction())->getSuccessor(0));
Vikram S. Adve74825322002-03-18 03:15:35 +00001405 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001406
1407 // delay slot
Vikram S. Adve74825322002-03-18 03:15:35 +00001408 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001409
1410 // false branch
Vikram S. Adve74825322002-03-18 03:15:35 +00001411 M = new MachineInstr(BA);
Vikram S. Adve6418eac2002-07-08 23:30:14 +00001412 M->SetMachineOperandVal(0, MachineOperand::MO_PCRelativeDisp,
Chris Lattner35504202002-04-27 03:14:39 +00001413 cast<BranchInst>(subtreeRoot->getInstruction())->getSuccessor(1));
Vikram S. Adve74825322002-03-18 03:15:35 +00001414 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001415
1416 // delay slot
Vikram S. Adve74825322002-03-18 03:15:35 +00001417 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001418 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001419 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00001420
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001421 case 9: // stmt: Switch(reg)
1422 assert(0 && "*** SWITCH instruction is not implemented yet.");
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001423 break;
Chris Lattner20b1ea02001-09-14 03:47:57 +00001424
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001425 case 10: // reg: VRegList(reg, reg)
1426 assert(0 && "VRegList should never be the topmost non-chain rule");
1427 break;
1428
Vikram S. Advece08e1d2002-08-15 14:17:37 +00001429 case 21: // bool: Not(bool,reg): Both these are implemented as:
1430 case 421: // reg: BNot(reg,reg): reg = reg XOR-NOT 0
1431 { // First find the unary operand. It may be left or right, usually right.
1432 Value* notArg = BinaryOperator::getNotArgument(
1433 cast<BinaryOperator>(subtreeRoot->getInstruction()));
1434 mvec.push_back(Create3OperandInstr_Reg(XNOR, notArg,
1435 target.getRegInfo().getZeroRegNum(),
1436 subtreeRoot->getValue()));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001437 break;
Vikram S. Advece08e1d2002-08-15 14:17:37 +00001438 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001439
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001440 case 22: // reg: ToBoolTy(reg):
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001441 {
1442 const Type* opType = subtreeRoot->leftChild()->getValue()->getType();
Chris Lattner0c4e8862002-09-03 01:08:28 +00001443 assert(opType->isIntegral() || isa<PointerType>(opType));
Vikram S. Adve74825322002-03-18 03:15:35 +00001444 forwardOperandNum = 0; // forward first operand to user
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001445 break;
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001446 }
1447
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001448 case 23: // reg: ToUByteTy(reg)
1449 case 25: // reg: ToUShortTy(reg)
1450 case 27: // reg: ToUIntTy(reg)
1451 case 29: // reg: ToULongTy(reg)
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001452 {
Vikram S. Adve242a8082002-05-19 15:25:51 +00001453 Instruction* destI = subtreeRoot->getInstruction();
1454 Value* opVal = subtreeRoot->leftChild()->getValue();
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001455 const Type* opType = subtreeRoot->leftChild()->getValue()->getType();
Chris Lattner0c4e8862002-09-03 01:08:28 +00001456 if (opType->isIntegral() || isa<PointerType>(opType))
Vikram S. Adve1e606692002-07-31 21:01:34 +00001457 {
1458 unsigned opSize = target.DataLayout.getTypeSize(opType);
1459 unsigned destSize = target.DataLayout.getTypeSize(destI->getType());
1460 if (opSize > destSize ||
1461 (opType->isSigned()
1462 && destSize < target.DataLayout.getIntegerRegize()))
1463 { // operand is larger than dest,
1464 // OR both are equal but smaller than the full register size
1465 // AND operand is signed, so it may have extra sign bits:
1466 // mask high bits using AND
1467 M = Create3OperandInstr(AND, opVal,
1468 ConstantUInt::get(Type::ULongTy,
1469 ((uint64_t) 1 << 8*destSize) - 1),
1470 destI);
1471 mvec.push_back(M);
1472 }
1473 else
1474 forwardOperandNum = 0; // forward first operand to user
Vikram S. Adve242a8082002-05-19 15:25:51 +00001475 }
Vikram S. Adve1e606692002-07-31 21:01:34 +00001476 else if (opType->isFloatingPoint())
Vikram S. Adve8cfffd32002-08-24 20:56:53 +00001477 {
1478 CreateCodeToConvertFloatToInt(target, opVal, destI, mvec,
1479 MachineCodeForInstruction::get(destI));
1480 maskUnsignedResult = true; // not handled by convert code
1481 }
Vikram S. Adve242a8082002-05-19 15:25:51 +00001482 else
Vikram S. Adve1e606692002-07-31 21:01:34 +00001483 assert(0 && "Unrecognized operand type for convert-to-unsigned");
1484
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001485 break;
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001486 }
1487
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001488 case 24: // reg: ToSByteTy(reg)
1489 case 26: // reg: ToShortTy(reg)
1490 case 28: // reg: ToIntTy(reg)
1491 case 30: // reg: ToLongTy(reg)
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001492 {
Vikram S. Adve242a8082002-05-19 15:25:51 +00001493 Instruction* destI = subtreeRoot->getInstruction();
1494 Value* opVal = subtreeRoot->leftChild()->getValue();
1495 MachineCodeForInstruction& mcfi =MachineCodeForInstruction::get(destI);
Vikram S. Adve1e606692002-07-31 21:01:34 +00001496
Vikram S. Adve242a8082002-05-19 15:25:51 +00001497 const Type* opType = opVal->getType();
Chris Lattner0c4e8862002-09-03 01:08:28 +00001498 if (opType->isIntegral() || isa<PointerType>(opType))
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001499 {
Vikram S. Adve242a8082002-05-19 15:25:51 +00001500 // These operand types have the same format as the destination,
1501 // but may have different size: add sign bits or mask as needed.
1502 //
1503 const Type* destType = destI->getType();
1504 unsigned opSize = target.DataLayout.getTypeSize(opType);
1505 unsigned destSize = target.DataLayout.getTypeSize(destType);
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001506
1507 if (opSize < destSize ||
1508 (opSize == destSize &&
1509 opSize == target.DataLayout.getIntegerRegize()))
1510 { // operand is smaller or both operand and result fill register
1511 forwardOperandNum = 0; // forward first operand to user
Vikram S. Adve242a8082002-05-19 15:25:51 +00001512 }
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001513 else
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001514 { // need to mask (possibly) and then sign-extend (definitely)
1515 Value* srcForSignExt = opVal;
1516 unsigned srcSizeForSignExt = 8 * opSize;
1517 if (opSize > destSize)
1518 { // operand is larger than dest: mask high bits
1519 TmpInstruction *tmpI = new TmpInstruction(destType, opVal,
1520 destI, "maskHi");
1521 mcfi.addTemp(tmpI);
1522 M = Create3OperandInstr(AND, opVal,
1523 ConstantUInt::get(Type::ULongTy,
1524 ((uint64_t) 1 << 8*destSize)-1),
1525 tmpI);
1526 mvec.push_back(M);
1527 srcForSignExt = tmpI;
1528 srcSizeForSignExt = 8 * destSize;
1529 }
1530
1531 // sign-extend
1532 target.getInstrInfo().CreateSignExtensionInstructions(target, destI->getParent()->getParent(), srcForSignExt, srcSizeForSignExt, destI, mvec, mcfi);
1533 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001534 }
Vikram S. Adve1e606692002-07-31 21:01:34 +00001535 else if (opType->isFloatingPoint())
Vikram S. Adve8cfffd32002-08-24 20:56:53 +00001536 CreateCodeToConvertFloatToInt(target, opVal, destI, mvec, mcfi);
Vikram S. Adve1e606692002-07-31 21:01:34 +00001537 else
1538 assert(0 && "Unrecognized operand type for convert-to-signed");
1539
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001540 break;
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001541 }
Vikram S. Advebabc0fa2002-09-05 18:32:13 +00001542
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001543 case 31: // reg: ToFloatTy(reg):
1544 case 32: // reg: ToDoubleTy(reg):
1545 case 232: // reg: ToDoubleTy(Constant):
Vikram S. Adveec7f4822002-09-09 14:54:21 +00001546
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001547 // If this instruction has a parent (a user) in the tree
1548 // and the user is translated as an FsMULd instruction,
1549 // then the cast is unnecessary. So check that first.
1550 // In the future, we'll want to do the same for the FdMULq instruction,
1551 // so do the check here instead of only for ToFloatTy(reg).
1552 //
Vikram S. Adveec7f4822002-09-09 14:54:21 +00001553 if (subtreeRoot->parent() != NULL)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001554 {
Vikram S. Adveec7f4822002-09-09 14:54:21 +00001555 const MachineCodeForInstruction& mcfi =
1556 MachineCodeForInstruction::get(
1557 cast<InstructionNode>(subtreeRoot->parent())->getInstruction());
1558 if (mcfi.size() == 0 || mcfi.front()->getOpCode() == FSMULD)
1559 forwardOperandNum = 0; // forward first operand to user
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001560 }
Vikram S. Adveec7f4822002-09-09 14:54:21 +00001561
1562 if (forwardOperandNum != 0) // we do need the cast
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001563 {
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001564 Value* leftVal = subtreeRoot->leftChild()->getValue();
1565 const Type* opType = leftVal->getType();
Vikram S. Advedbc4fad2002-04-25 04:37:51 +00001566 MachineOpCode opCode=ChooseConvertToFloatInstr(
1567 subtreeRoot->getOpLabel(), opType);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001568 if (opCode == INVALID_OPCODE) // no conversion needed
1569 {
Vikram S. Adve74825322002-03-18 03:15:35 +00001570 forwardOperandNum = 0; // forward first operand to user
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001571 }
1572 else
1573 {
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001574 // If the source operand is a non-FP type it must be
1575 // first copied from int to float register via memory!
1576 Instruction *dest = subtreeRoot->getInstruction();
1577 Value* srcForCast;
1578 int n = 0;
Vikram S. Adve242a8082002-05-19 15:25:51 +00001579 if (! opType->isFloatingPoint())
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001580 {
1581 // Create a temporary to represent the FP register
1582 // into which the integer will be copied via memory.
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001583 // The type of this temporary will determine the FP
1584 // register used: single-prec for a 32-bit int or smaller,
1585 // double-prec for a 64-bit int.
1586 //
Vikram S. Advebabc0fa2002-09-05 18:32:13 +00001587 uint64_t srcSize =
1588 target.DataLayout.getTypeSize(leftVal->getType());
1589 Type* tmpTypeToUse =
1590 (srcSize <= 4)? Type::FloatTy : Type::DoubleTy;
1591 srcForCast = new TmpInstruction(tmpTypeToUse, dest);
Vikram S. Advedbc4fad2002-04-25 04:37:51 +00001592 MachineCodeForInstruction &destMCFI =
Chris Lattner9c461082002-02-03 07:50:56 +00001593 MachineCodeForInstruction::get(dest);
Vikram S. Advedbc4fad2002-04-25 04:37:51 +00001594 destMCFI.addTemp(srcForCast);
Vikram S. Advebabc0fa2002-09-05 18:32:13 +00001595
Vikram S. Adve242a8082002-05-19 15:25:51 +00001596 target.getInstrInfo().CreateCodeToCopyIntToFloat(target,
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001597 dest->getParent()->getParent(),
Vikram S. Advebabc0fa2002-09-05 18:32:13 +00001598 leftVal, cast<Instruction>(srcForCast),
Vikram S. Adve242a8082002-05-19 15:25:51 +00001599 mvec, destMCFI);
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001600 }
1601 else
1602 srcForCast = leftVal;
1603
Vikram S. Adve74825322002-03-18 03:15:35 +00001604 M = new MachineInstr(opCode);
1605 M->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister,
1606 srcForCast);
1607 M->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister,
1608 dest);
1609 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001610 }
1611 }
1612 break;
1613
1614 case 19: // reg: ToArrayTy(reg):
1615 case 20: // reg: ToPointerTy(reg):
Vikram S. Adve74825322002-03-18 03:15:35 +00001616 forwardOperandNum = 0; // forward first operand to user
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001617 break;
1618
1619 case 233: // reg: Add(reg, Constant)
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001620 maskUnsignedResult = true;
Vikram S. Adve74825322002-03-18 03:15:35 +00001621 M = CreateAddConstInstruction(subtreeRoot);
1622 if (M != NULL)
1623 {
1624 mvec.push_back(M);
1625 break;
1626 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001627 // ELSE FALL THROUGH
Vikram S. Adve74825322002-03-18 03:15:35 +00001628
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001629 case 33: // reg: Add(reg, reg)
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001630 maskUnsignedResult = true;
Vikram S. Adve74825322002-03-18 03:15:35 +00001631 mvec.push_back(new MachineInstr(ChooseAddInstruction(subtreeRoot)));
1632 Set3OperandsFromInstr(mvec.back(), subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001633 break;
1634
1635 case 234: // reg: Sub(reg, Constant)
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001636 maskUnsignedResult = true;
Vikram S. Adve74825322002-03-18 03:15:35 +00001637 M = CreateSubConstInstruction(subtreeRoot);
1638 if (M != NULL)
1639 {
1640 mvec.push_back(M);
1641 break;
1642 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001643 // ELSE FALL THROUGH
Vikram S. Adve74825322002-03-18 03:15:35 +00001644
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001645 case 34: // reg: Sub(reg, reg)
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001646 maskUnsignedResult = true;
Vikram S. Adve74825322002-03-18 03:15:35 +00001647 mvec.push_back(new MachineInstr(ChooseSubInstructionByType(
1648 subtreeRoot->getInstruction()->getType())));
1649 Set3OperandsFromInstr(mvec.back(), subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001650 break;
1651
1652 case 135: // reg: Mul(todouble, todouble)
1653 checkCast = true;
1654 // FALL THROUGH
1655
1656 case 35: // reg: Mul(reg, reg)
Vikram S. Adve74825322002-03-18 03:15:35 +00001657 {
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001658 maskUnsignedResult = true;
Vikram S. Adve74825322002-03-18 03:15:35 +00001659 MachineOpCode forceOp = ((checkCast && BothFloatToDouble(subtreeRoot))
1660 ? FSMULD
1661 : INVALID_MACHINE_OPCODE);
Vikram S. Adve242a8082002-05-19 15:25:51 +00001662 Instruction* mulInstr = subtreeRoot->getInstruction();
1663 CreateMulInstruction(target, mulInstr->getParent()->getParent(),
Vikram S. Adve74825322002-03-18 03:15:35 +00001664 subtreeRoot->leftChild()->getValue(),
1665 subtreeRoot->rightChild()->getValue(),
Vikram S. Adve242a8082002-05-19 15:25:51 +00001666 mulInstr, mvec,
1667 MachineCodeForInstruction::get(mulInstr),forceOp);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001668 break;
Vikram S. Adve74825322002-03-18 03:15:35 +00001669 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001670 case 335: // reg: Mul(todouble, todoubleConst)
1671 checkCast = true;
1672 // FALL THROUGH
1673
1674 case 235: // reg: Mul(reg, Constant)
Vikram S. Adve74825322002-03-18 03:15:35 +00001675 {
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001676 maskUnsignedResult = true;
Vikram S. Adve74825322002-03-18 03:15:35 +00001677 MachineOpCode forceOp = ((checkCast && BothFloatToDouble(subtreeRoot))
1678 ? FSMULD
1679 : INVALID_MACHINE_OPCODE);
Vikram S. Adve242a8082002-05-19 15:25:51 +00001680 Instruction* mulInstr = subtreeRoot->getInstruction();
1681 CreateMulInstruction(target, mulInstr->getParent()->getParent(),
Vikram S. Adve74825322002-03-18 03:15:35 +00001682 subtreeRoot->leftChild()->getValue(),
1683 subtreeRoot->rightChild()->getValue(),
Vikram S. Adve242a8082002-05-19 15:25:51 +00001684 mulInstr, mvec,
1685 MachineCodeForInstruction::get(mulInstr),
1686 forceOp);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001687 break;
Vikram S. Adve74825322002-03-18 03:15:35 +00001688 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001689 case 236: // reg: Div(reg, Constant)
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001690 maskUnsignedResult = true;
Vikram S. Adve74825322002-03-18 03:15:35 +00001691 L = mvec.size();
1692 CreateDivConstInstruction(target, subtreeRoot, mvec);
1693 if (mvec.size() > L)
1694 break;
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 36: // reg: Div(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(ChooseDivInstruction(target, subtreeRoot)));
1700 Set3OperandsFromInstr(mvec.back(), subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001701 break;
1702
1703 case 37: // reg: Rem(reg, reg)
1704 case 237: // reg: Rem(reg, Constant)
Vikram S. Adve510eec72001-11-04 21:59:14 +00001705 {
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001706 maskUnsignedResult = true;
Vikram S. Adve510eec72001-11-04 21:59:14 +00001707 Instruction* remInstr = subtreeRoot->getInstruction();
1708
Chris Lattner9c461082002-02-03 07:50:56 +00001709 TmpInstruction* quot = new TmpInstruction(
Vikram S. Adve510eec72001-11-04 21:59:14 +00001710 subtreeRoot->leftChild()->getValue(),
1711 subtreeRoot->rightChild()->getValue());
Chris Lattner9c461082002-02-03 07:50:56 +00001712 TmpInstruction* prod = new TmpInstruction(
Vikram S. Adve510eec72001-11-04 21:59:14 +00001713 quot,
1714 subtreeRoot->rightChild()->getValue());
Chris Lattner9c461082002-02-03 07:50:56 +00001715 MachineCodeForInstruction::get(remInstr).addTemp(quot).addTemp(prod);
Vikram S. Adve510eec72001-11-04 21:59:14 +00001716
Vikram S. Adve74825322002-03-18 03:15:35 +00001717 M = new MachineInstr(ChooseDivInstruction(target, subtreeRoot));
1718 Set3OperandsFromInstr(M, subtreeRoot, target);
1719 M->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister,quot);
1720 mvec.push_back(M);
Vikram S. Adve510eec72001-11-04 21:59:14 +00001721
Vikram S. Advece08e1d2002-08-15 14:17:37 +00001722 M = Create3OperandInstr(ChooseMulInstructionByType(
1723 subtreeRoot->getInstruction()->getType()),
1724 quot, subtreeRoot->rightChild()->getValue(),
1725 prod);
Vikram S. Adve74825322002-03-18 03:15:35 +00001726 mvec.push_back(M);
Vikram S. Adve510eec72001-11-04 21:59:14 +00001727
Vikram S. Adve74825322002-03-18 03:15:35 +00001728 M = new MachineInstr(ChooseSubInstructionByType(
Vikram S. Adve510eec72001-11-04 21:59:14 +00001729 subtreeRoot->getInstruction()->getType()));
Vikram S. Adve74825322002-03-18 03:15:35 +00001730 Set3OperandsFromInstr(M, subtreeRoot, target);
1731 M->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister,prod);
1732 mvec.push_back(M);
Vikram S. Adve510eec72001-11-04 21:59:14 +00001733
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001734 break;
Vikram S. Adve510eec72001-11-04 21:59:14 +00001735 }
1736
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001737 case 38: // bool: And(bool, bool)
1738 case 238: // bool: And(bool, boolconst)
1739 case 338: // reg : BAnd(reg, reg)
1740 case 538: // reg : BAnd(reg, Constant)
Vikram S. Adve74825322002-03-18 03:15:35 +00001741 mvec.push_back(new MachineInstr(AND));
1742 Set3OperandsFromInstr(mvec.back(), subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001743 break;
1744
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001745 case 138: // bool: And(bool, not)
Vikram S. Advece08e1d2002-08-15 14:17:37 +00001746 case 438: // bool: BAnd(bool, bnot)
1747 { // Use the argument of NOT as the second argument!
1748 // Mark the NOT node so that no code is generated for it.
1749 InstructionNode* notNode = (InstructionNode*) subtreeRoot->rightChild();
1750 Value* notArg = BinaryOperator::getNotArgument(
1751 cast<BinaryOperator>(notNode->getInstruction()));
1752 notNode->markFoldedIntoParent();
1753 mvec.push_back(Create3OperandInstr(ANDN,
1754 subtreeRoot->leftChild()->getValue(),
1755 notArg, subtreeRoot->getValue()));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001756 break;
Vikram S. Advece08e1d2002-08-15 14:17:37 +00001757 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001758
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001759 case 39: // bool: Or(bool, bool)
1760 case 239: // bool: Or(bool, boolconst)
1761 case 339: // reg : BOr(reg, reg)
1762 case 539: // reg : BOr(reg, Constant)
Vikram S. Adve242a8082002-05-19 15:25:51 +00001763 mvec.push_back(new MachineInstr(OR));
Vikram S. Adve74825322002-03-18 03:15:35 +00001764 Set3OperandsFromInstr(mvec.back(), subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001765 break;
1766
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001767 case 139: // bool: Or(bool, not)
Vikram S. Advece08e1d2002-08-15 14:17:37 +00001768 case 439: // bool: BOr(bool, bnot)
1769 { // Use the argument of NOT as the second argument!
1770 // Mark the NOT node so that no code is generated for it.
1771 InstructionNode* notNode = (InstructionNode*) subtreeRoot->rightChild();
1772 Value* notArg = BinaryOperator::getNotArgument(
1773 cast<BinaryOperator>(notNode->getInstruction()));
1774 notNode->markFoldedIntoParent();
1775 mvec.push_back(Create3OperandInstr(ORN,
1776 subtreeRoot->leftChild()->getValue(),
1777 notArg, subtreeRoot->getValue()));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001778 break;
Vikram S. Advece08e1d2002-08-15 14:17:37 +00001779 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001780
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001781 case 40: // bool: Xor(bool, bool)
1782 case 240: // bool: Xor(bool, boolconst)
1783 case 340: // reg : BXor(reg, reg)
1784 case 540: // reg : BXor(reg, Constant)
Vikram S. Adve74825322002-03-18 03:15:35 +00001785 mvec.push_back(new MachineInstr(XOR));
1786 Set3OperandsFromInstr(mvec.back(), subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001787 break;
1788
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001789 case 140: // bool: Xor(bool, not)
Vikram S. Advece08e1d2002-08-15 14:17:37 +00001790 case 440: // bool: BXor(bool, bnot)
1791 { // Use the argument of NOT as the second argument!
1792 // Mark the NOT node so that no code is generated for it.
1793 InstructionNode* notNode = (InstructionNode*) subtreeRoot->rightChild();
1794 Value* notArg = BinaryOperator::getNotArgument(
1795 cast<BinaryOperator>(notNode->getInstruction()));
1796 notNode->markFoldedIntoParent();
1797 mvec.push_back(Create3OperandInstr(XNOR,
1798 subtreeRoot->leftChild()->getValue(),
1799 notArg, subtreeRoot->getValue()));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001800 break;
Vikram S. Advece08e1d2002-08-15 14:17:37 +00001801 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001802
1803 case 41: // boolconst: SetCC(reg, Constant)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001804 //
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001805 // If the SetCC was folded into the user (parent), it will be
1806 // caught above. All other cases are the same as case 42,
1807 // so just fall through.
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001808 //
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001809 case 42: // bool: SetCC(reg, reg):
1810 {
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001811 // This generates a SUBCC instruction, putting the difference in
1812 // a result register, and setting a condition code.
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001813 //
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001814 // If the boolean result of the SetCC is used by anything other
Vikram S. Adve6418eac2002-07-08 23:30:14 +00001815 // than a branch instruction, or if it is used outside the current
1816 // basic block, the boolean must be
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001817 // computed and stored in the result register. Otherwise, discard
1818 // the difference (by using %g0) and keep only the condition code.
1819 //
1820 // To compute the boolean result in a register we use a conditional
1821 // move, unless the result of the SUBCC instruction can be used as
1822 // the bool! This assumes that zero is FALSE and any non-zero
1823 // integer is TRUE.
1824 //
1825 InstructionNode* parentNode = (InstructionNode*) subtreeRoot->parent();
1826 Instruction* setCCInstr = subtreeRoot->getInstruction();
Vikram S. Adve242a8082002-05-19 15:25:51 +00001827
Vikram S. Adve6418eac2002-07-08 23:30:14 +00001828 bool keepBoolVal = parentNode == NULL ||
1829 ! AllUsesAreBranches(setCCInstr);
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001830 bool subValIsBoolVal = setCCInstr->getOpcode() == Instruction::SetNE;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001831 bool keepSubVal = keepBoolVal && subValIsBoolVal;
1832 bool computeBoolVal = keepBoolVal && ! subValIsBoolVal;
1833
1834 bool mustClearReg;
1835 int valueToMove;
Chris Lattner8e5c0b42001-11-07 14:01:59 +00001836 MachineOpCode movOpCode = 0;
Vikram S. Adve6418eac2002-07-08 23:30:14 +00001837
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001838 // Mark the 4th operand as being a CC register, and as a def
1839 // A TmpInstruction is created to represent the CC "result".
1840 // Unlike other instances of TmpInstruction, this one is used
1841 // by machine code of multiple LLVM instructions, viz.,
1842 // the SetCC and the branch. Make sure to get the same one!
1843 // Note that we do this even for FP CC registers even though they
1844 // are explicit operands, because the type of the operand
1845 // needs to be a floating point condition code, not an integer
1846 // condition code. Think of this as casting the bool result to
1847 // a FP condition code register.
1848 //
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001849 Value* leftVal = subtreeRoot->leftChild()->getValue();
Chris Lattner9b625032002-05-06 16:15:30 +00001850 bool isFPCompare = leftVal->getType()->isFloatingPoint();
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001851
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001852 TmpInstruction* tmpForCC = GetTmpForCC(setCCInstr,
1853 setCCInstr->getParent()->getParent(),
Chris Lattner9b625032002-05-06 16:15:30 +00001854 isFPCompare ? Type::FloatTy : Type::IntTy);
Chris Lattner9c461082002-02-03 07:50:56 +00001855 MachineCodeForInstruction::get(setCCInstr).addTemp(tmpForCC);
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001856
1857 if (! isFPCompare)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001858 {
1859 // Integer condition: dest. should be %g0 or an integer register.
1860 // If result must be saved but condition is not SetEQ then we need
1861 // a separate instruction to compute the bool result, so discard
1862 // result of SUBcc instruction anyway.
1863 //
Vikram S. Adve74825322002-03-18 03:15:35 +00001864 M = new MachineInstr(SUBcc);
1865 Set3OperandsFromInstr(M, subtreeRoot, target, ! keepSubVal);
1866 M->SetMachineOperandVal(3, MachineOperand::MO_CCRegister,
1867 tmpForCC, /*def*/true);
1868 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001869
1870 if (computeBoolVal)
1871 { // recompute bool using the integer condition codes
1872 movOpCode =
1873 ChooseMovpccAfterSub(subtreeRoot,mustClearReg,valueToMove);
1874 }
1875 }
1876 else
1877 {
1878 // FP condition: dest of FCMP should be some FCCn register
Vikram S. Adve74825322002-03-18 03:15:35 +00001879 M = new MachineInstr(ChooseFcmpInstruction(subtreeRoot));
1880 M->SetMachineOperandVal(0, MachineOperand::MO_CCRegister,
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001881 tmpForCC);
Vikram S. Adve74825322002-03-18 03:15:35 +00001882 M->SetMachineOperandVal(1,MachineOperand::MO_VirtualRegister,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001883 subtreeRoot->leftChild()->getValue());
Vikram S. Adve74825322002-03-18 03:15:35 +00001884 M->SetMachineOperandVal(2,MachineOperand::MO_VirtualRegister,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001885 subtreeRoot->rightChild()->getValue());
Vikram S. Adve74825322002-03-18 03:15:35 +00001886 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001887
1888 if (computeBoolVal)
1889 {// recompute bool using the FP condition codes
1890 mustClearReg = true;
1891 valueToMove = 1;
1892 movOpCode = ChooseMovFpccInstruction(subtreeRoot);
1893 }
1894 }
1895
1896 if (computeBoolVal)
1897 {
1898 if (mustClearReg)
1899 {// Unconditionally set register to 0
Vikram S. Adve74825322002-03-18 03:15:35 +00001900 M = new MachineInstr(SETHI);
1901 M->SetMachineOperandConst(0,MachineOperand::MO_UnextendedImmed,
1902 (int64_t)0);
1903 M->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister,
1904 setCCInstr);
1905 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001906 }
1907
1908 // Now conditionally move `valueToMove' (0 or 1) into the register
Vikram S. Adve6418eac2002-07-08 23:30:14 +00001909 // Mark the register as a use (as well as a def) because the old
1910 // value should be retained if the condition is false.
Vikram S. Adve74825322002-03-18 03:15:35 +00001911 M = new MachineInstr(movOpCode);
1912 M->SetMachineOperandVal(0, MachineOperand::MO_CCRegister,
1913 tmpForCC);
1914 M->SetMachineOperandConst(1, MachineOperand::MO_UnextendedImmed,
1915 valueToMove);
1916 M->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister,
Vikram S. Adve6418eac2002-07-08 23:30:14 +00001917 setCCInstr, /*isDef*/ true,
1918 /*isDefAndUse*/ true);
Vikram S. Adve74825322002-03-18 03:15:35 +00001919 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001920 }
1921 break;
1922 }
1923
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001924 case 51: // reg: Load(reg)
1925 case 52: // reg: Load(ptrreg)
Vikram S. Adve74825322002-03-18 03:15:35 +00001926 mvec.push_back(new MachineInstr(ChooseLoadInstruction(
1927 subtreeRoot->getValue()->getType())));
Vikram S. Adve8cfffd32002-08-24 20:56:53 +00001928 SetOperandsForMemInstr(mvec, subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001929 break;
1930
1931 case 55: // reg: GetElemPtr(reg)
1932 case 56: // reg: GetElemPtrIdx(reg,reg)
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001933 // If the GetElemPtr was folded into the user (parent), it will be
1934 // caught above. For other cases, we have to compute the address.
Vikram S. Adve74825322002-03-18 03:15:35 +00001935 mvec.push_back(new MachineInstr(ADD));
Vikram S. Adve8cfffd32002-08-24 20:56:53 +00001936 SetOperandsForMemInstr(mvec, subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001937 break;
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001938
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001939 case 57: // reg: Alloca: Implement as 1 instruction:
1940 { // add %fp, offsetFromFP -> result
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001941 AllocationInst* instr =
1942 cast<AllocationInst>(subtreeRoot->getInstruction());
1943 unsigned int tsize =
1944 target.findOptimalStorageSize(instr->getAllocatedType());
Vikram S. Adve74825322002-03-18 03:15:35 +00001945 assert(tsize != 0);
1946 CreateCodeForFixedSizeAlloca(target, instr, tsize, 1, mvec);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001947 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001948 }
Vikram S. Adve74825322002-03-18 03:15:35 +00001949
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001950 case 58: // reg: Alloca(reg): Implement as 3 instructions:
1951 // mul num, typeSz -> tmp
1952 // sub %sp, tmp -> %sp
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001953 { // add %sp, frameSizeBelowDynamicArea -> result
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001954 AllocationInst* instr =
1955 cast<AllocationInst>(subtreeRoot->getInstruction());
Vikram S. Adve74825322002-03-18 03:15:35 +00001956 const Type* eltType = instr->getAllocatedType();
1957
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001958 // If #elements is constant, use simpler code for fixed-size allocas
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001959 int tsize = (int) target.findOptimalStorageSize(eltType);
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001960 Value* numElementsVal = NULL;
1961 bool isArray = instr->isArrayAllocation();
1962
1963 if (!isArray ||
1964 isa<Constant>(numElementsVal = instr->getArraySize()))
1965 { // total size is constant: generate code for fixed-size alloca
1966 unsigned int numElements = isArray?
1967 cast<ConstantUInt>(numElementsVal)->getValue() : 1;
1968 CreateCodeForFixedSizeAlloca(target, instr, tsize,
1969 numElements, mvec);
1970 }
Vikram S. Adve74825322002-03-18 03:15:35 +00001971 else // total size is not constant.
1972 CreateCodeForVariableSizeAlloca(target, instr, tsize,
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001973 numElementsVal, mvec);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001974 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001975 }
Vikram S. Adve74825322002-03-18 03:15:35 +00001976
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001977 case 61: // reg: Call
Vikram S. Advedbc4fad2002-04-25 04:37:51 +00001978 { // Generate a direct (CALL) or indirect (JMPL). depending
1979 // Mark the return-address register and the indirection
1980 // register (if any) as hidden virtual registers.
Vikram S. Advea995e602001-10-11 04:23:19 +00001981 // Also, mark the operands of the Call and return value (if
1982 // any) as implicit operands of the CALL machine instruction.
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001983 //
Vikram S. Advedbc4fad2002-04-25 04:37:51 +00001984 // If this is a varargs function, floating point arguments
1985 // have to passed in integer registers so insert
1986 // copy-float-to-int instructions for each float operand.
1987 //
Chris Lattnerb00c5822001-10-02 03:41:24 +00001988 CallInst *callInstr = cast<CallInst>(subtreeRoot->getInstruction());
Chris Lattner749655f2001-10-13 06:54:30 +00001989 Value *callee = callInstr->getCalledValue();
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001990
Vikram S. Advea10d1a72002-03-31 19:07:35 +00001991 // Create hidden virtual register for return address, with type void*.
Vikram S. Adve242a8082002-05-19 15:25:51 +00001992 TmpInstruction* retAddrReg =
Vikram S. Advea10d1a72002-03-31 19:07:35 +00001993 new TmpInstruction(PointerType::get(Type::VoidTy), callInstr);
Chris Lattner9c461082002-02-03 07:50:56 +00001994 MachineCodeForInstruction::get(callInstr).addTemp(retAddrReg);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001995
Vikram S. Adveea21a6c2001-10-20 20:57:06 +00001996 // Generate the machine instruction and its operands.
1997 // Use CALL for direct function calls; this optimistically assumes
1998 // the PC-relative address fits in the CALL address field (22 bits).
1999 // Use JMPL for indirect calls.
2000 //
Chris Lattnerb0d04722002-03-26 17:58:12 +00002001 if (isa<Function>(callee))
Vikram S. Adveea21a6c2001-10-20 20:57:06 +00002002 { // direct function call
Vikram S. Adve74825322002-03-18 03:15:35 +00002003 M = new MachineInstr(CALL);
2004 M->SetMachineOperandVal(0, MachineOperand::MO_PCRelativeDisp,
2005 callee);
Vikram S. Adveea21a6c2001-10-20 20:57:06 +00002006 }
2007 else
2008 { // indirect function call
Vikram S. Adve74825322002-03-18 03:15:35 +00002009 M = new MachineInstr(JMPLCALL);
2010 M->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister,
2011 callee);
2012 M->SetMachineOperandConst(1, MachineOperand::MO_SignExtendedImmed,
2013 (int64_t) 0);
2014 M->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister,
2015 retAddrReg);
Vikram S. Adveea21a6c2001-10-20 20:57:06 +00002016 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002017
Vikram S. Adve74825322002-03-18 03:15:35 +00002018 mvec.push_back(M);
Vikram S. Advea10d1a72002-03-31 19:07:35 +00002019
Vikram S. Adve242a8082002-05-19 15:25:51 +00002020 const FunctionType* funcType =
2021 cast<FunctionType>(cast<PointerType>(callee->getType())
2022 ->getElementType());
2023 bool isVarArgs = funcType->isVarArg();
2024 bool noPrototype = isVarArgs && funcType->getNumParams() == 0;
Vikram S. Advedbc4fad2002-04-25 04:37:51 +00002025
Vikram S. Adve242a8082002-05-19 15:25:51 +00002026 // Use an annotation to pass information about call arguments
2027 // to the register allocator.
2028 CallArgsDescriptor* argDesc = new CallArgsDescriptor(callInstr,
2029 retAddrReg, isVarArgs, noPrototype);
2030 M->addAnnotation(argDesc);
Vikram S. Advea995e602001-10-11 04:23:19 +00002031
Vikram S. Adve242a8082002-05-19 15:25:51 +00002032 assert(callInstr->getOperand(0) == callee
2033 && "This is assumed in the loop below!");
2034
2035 for (unsigned i=1, N=callInstr->getNumOperands(); i < N; ++i)
2036 {
2037 Value* argVal = callInstr->getOperand(i);
2038 Instruction* intArgReg = NULL;
2039
2040 // Check for FP arguments to varargs functions.
2041 // Any such argument in the first $K$ args must be passed in an
2042 // integer register, where K = #integer argument registers.
2043 if (isVarArgs && argVal->getType()->isFloatingPoint())
2044 {
2045 // If it is a function with no prototype, pass value
2046 // as an FP value as well as a varargs value
2047 if (noPrototype)
2048 argDesc->getArgInfo(i-1).setUseFPArgReg();
2049
2050 // If this arg. is in the first $K$ regs, add a copy
2051 // float-to-int instruction to pass the value as an integer.
2052 if (i < target.getRegInfo().GetNumOfIntArgRegs())
2053 {
2054 MachineCodeForInstruction &destMCFI =
2055 MachineCodeForInstruction::get(callInstr);
2056 intArgReg = new TmpInstruction(Type::IntTy, argVal);
2057 destMCFI.addTemp(intArgReg);
2058
2059 vector<MachineInstr*> copyMvec;
2060 target.getInstrInfo().CreateCodeToCopyFloatToInt(target,
2061 callInstr->getParent()->getParent(),
2062 argVal, (TmpInstruction*) intArgReg,
2063 copyMvec, destMCFI);
2064 mvec.insert(mvec.begin(),copyMvec.begin(),copyMvec.end());
2065
2066 argDesc->getArgInfo(i-1).setUseIntArgReg();
2067 argDesc->getArgInfo(i-1).setArgCopy(intArgReg);
2068 }
2069 else
2070 // Cannot fit in first $K$ regs so pass the arg on the stack
2071 argDesc->getArgInfo(i-1).setUseStackSlot();
2072 }
2073
2074 if (intArgReg)
2075 mvec.back()->addImplicitRef(intArgReg);
2076
2077 mvec.back()->addImplicitRef(argVal);
2078 }
2079
2080 // Add the return value as an implicit ref. The call operands
2081 // were added above.
Vikram S. Adveea21a6c2001-10-20 20:57:06 +00002082 if (callInstr->getType() != Type::VoidTy)
Vikram S. Adve74825322002-03-18 03:15:35 +00002083 mvec.back()->addImplicitRef(callInstr, /*isDef*/ true);
Vikram S. Advea995e602001-10-11 04:23:19 +00002084
Vikram S. Adveea21a6c2001-10-20 20:57:06 +00002085 // For the CALL instruction, the ret. addr. reg. is also implicit
Chris Lattnerb0d04722002-03-26 17:58:12 +00002086 if (isa<Function>(callee))
Vikram S. Adve74825322002-03-18 03:15:35 +00002087 mvec.back()->addImplicitRef(retAddrReg, /*isDef*/ true);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002088
Vikram S. Adve74825322002-03-18 03:15:35 +00002089 // delay slot
2090 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002091 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00002092 }
Vikram S. Adve242a8082002-05-19 15:25:51 +00002093
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002094 case 62: // reg: Shl(reg, reg)
Vikram S. Adve242a8082002-05-19 15:25:51 +00002095 {
2096 Value* argVal1 = subtreeRoot->leftChild()->getValue();
2097 Value* argVal2 = subtreeRoot->rightChild()->getValue();
2098 Instruction* shlInstr = subtreeRoot->getInstruction();
2099
2100 const Type* opType = argVal1->getType();
Chris Lattner0c4e8862002-09-03 01:08:28 +00002101 assert((opType->isInteger() || isa<PointerType>(opType)) &&
2102 "Shl unsupported for other types");
Vikram S. Adve242a8082002-05-19 15:25:51 +00002103
2104 CreateShiftInstructions(target, shlInstr->getParent()->getParent(),
2105 (opType == Type::LongTy)? SLLX : SLL,
2106 argVal1, argVal2, 0, shlInstr, mvec,
2107 MachineCodeForInstruction::get(shlInstr));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002108 break;
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00002109 }
2110
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002111 case 63: // reg: Shr(reg, reg)
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00002112 { const Type* opType = subtreeRoot->leftChild()->getValue()->getType();
Chris Lattner0c4e8862002-09-03 01:08:28 +00002113 assert((opType->isInteger() || isa<PointerType>(opType)) &&
2114 "Shr unsupported for other types");
Vikram S. Adve74825322002-03-18 03:15:35 +00002115 mvec.push_back(new MachineInstr((opType->isSigned()
2116 ? ((opType == Type::LongTy)? SRAX : SRA)
2117 : ((opType == Type::LongTy)? SRLX : SRL))));
2118 Set3OperandsFromInstr(mvec.back(), subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002119 break;
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00002120 }
2121
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002122 case 64: // reg: Phi(reg,reg)
Vikram S. Adve74825322002-03-18 03:15:35 +00002123 break; // don't forward the value
2124
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002125 case 71: // reg: VReg
2126 case 72: // reg: Constant
Vikram S. Adve74825322002-03-18 03:15:35 +00002127 break; // don't forward the value
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002128
2129 default:
2130 assert(0 && "Unrecognized BURG rule");
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002131 break;
2132 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00002133 }
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00002134
Chris Lattner20b1ea02001-09-14 03:47:57 +00002135 if (forwardOperandNum >= 0)
2136 { // We did not generate a machine instruction but need to use operand.
2137 // If user is in the same tree, replace Value in its machine operand.
2138 // If not, insert a copy instruction which should get coalesced away
2139 // by register allocation.
2140 if (subtreeRoot->parent() != NULL)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002141 ForwardOperand(subtreeRoot, subtreeRoot->parent(), forwardOperandNum);
Chris Lattner20b1ea02001-09-14 03:47:57 +00002142 else
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002143 {
Vikram S. Adve7fe27872001-10-18 00:26:20 +00002144 vector<MachineInstr*> minstrVec;
Vikram S. Adve242a8082002-05-19 15:25:51 +00002145 Instruction* instr = subtreeRoot->getInstruction();
2146 target.getInstrInfo().
2147 CreateCopyInstructionsByType(target,
2148 instr->getParent()->getParent(),
2149 instr->getOperand(forwardOperandNum),
2150 instr, minstrVec,
2151 MachineCodeForInstruction::get(instr));
Vikram S. Adve7fe27872001-10-18 00:26:20 +00002152 assert(minstrVec.size() > 0);
Vikram S. Adve74825322002-03-18 03:15:35 +00002153 mvec.insert(mvec.end(), minstrVec.begin(), minstrVec.end());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002154 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00002155 }
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00002156
2157 if (maskUnsignedResult)
2158 { // If result is unsigned and smaller than int reg size,
2159 // we need to clear high bits of result value.
2160 assert(forwardOperandNum < 0 && "Need mask but no instruction generated");
2161 Instruction* dest = subtreeRoot->getInstruction();
Vikram S. Adve8cfffd32002-08-24 20:56:53 +00002162 if (dest->getType()->isUnsigned())
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00002163 {
2164 unsigned destSize = target.DataLayout.getTypeSize(dest->getType());
Vikram S. Advebabc0fa2002-09-05 18:32:13 +00002165 if (destSize <= 4)
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00002166 { // Mask high bits. Use a TmpInstruction to represent the
2167 // intermediate result before masking. Since those instructions
2168 // have already been generated, go back and substitute tmpI
2169 // for dest in the result position of each one of them.
2170 TmpInstruction *tmpI = new TmpInstruction(dest->getType(), dest,
2171 NULL, "maskHi");
2172 MachineCodeForInstruction::get(dest).addTemp(tmpI);
2173
2174 for (unsigned i=0, N=mvec.size(); i < N; ++i)
2175 mvec[i]->substituteValue(dest, tmpI);
2176
Vikram S. Advebabc0fa2002-09-05 18:32:13 +00002177 M = Create3OperandInstr_UImmed(SRL, tmpI, 4-destSize, dest);
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00002178 mvec.push_back(M);
2179 }
Vikram S. Advebabc0fa2002-09-05 18:32:13 +00002180 else if (destSize < target.DataLayout.getIntegerRegize())
2181 assert(0 && "Unsupported type size: 32 < size < 64 bits");
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00002182 }
2183 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00002184}