blob: ce7e4dc0d925fb10deccdeb16b0fa05f288ddad4 [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. Adve8cfffd32002-08-24 20:56:53 +00001001 assert(! isa<Constant>(idxVal) && "Need to sign-extend uint to 64b!");
Vikram S. Adveed3fefb2002-08-03 13:48:21 +00001002
1003 vector<MachineInstr*> mulVec;
1004 Instruction* addr = new TmpInstruction(Type::UIntTy, memInst);
1005 MachineCodeForInstruction::get(memInst).addTemp(addr);
1006
Vikram S. Advebabc0fa2002-09-05 18:32:13 +00001007 // Get the array type indexed by idxVal, and compute its element size.
Vikram S. Adveed3fefb2002-08-03 13:48:21 +00001008 // The call to getTypeSize() will fail if size is not constant.
Vikram S. Advebabc0fa2002-09-05 18:32:13 +00001009 const Type* vecType = (firstIdxIsZero
1010 ? GetElementPtrInst::getIndexedType(ptrType,
1011 std::vector<Value*>(1U, idxVec[0]),
1012 /*AllowCompositeLeaf*/ true)
1013 : ptrType);
1014 const Type* eltType = cast<SequentialType>(vecType)->getElementType();
1015 ConstantUInt* eltSizeVal = ConstantUInt::get(Type::UIntTy,
1016 target.DataLayout.getTypeSize(eltType));
Vikram S. Adveed3fefb2002-08-03 13:48:21 +00001017
1018 // CreateMulInstruction() folds constants intelligently enough.
1019 CreateMulInstruction(target,
1020 memInst->getParent()->getParent(),
Vikram S. Advebabc0fa2002-09-05 18:32:13 +00001021 idxVal, /* lval, not likely to be const*/
1022 eltSizeVal, /* rval, likely to be constant */
1023 addr, /* result */
Vikram S. Adveed3fefb2002-08-03 13:48:21 +00001024 mulVec,
1025 MachineCodeForInstruction::get(memInst),
1026 INVALID_MACHINE_OPCODE);
1027
Vikram S. Adve8cfffd32002-08-24 20:56:53 +00001028 // Sign-extend the result of MUL from 32 to 64 bits.
1029 target.getInstrInfo().CreateSignExtensionInstructions(target, memInst->getParent()->getParent(), addr, /*srcSizeInBits*/32, addr, mulVec, MachineCodeForInstruction::get(memInst));
1030
Vikram S. Adveed3fefb2002-08-03 13:48:21 +00001031 // Insert mulVec[] before *mvecI in mvec[] and update mvecI
1032 // to point to the same instruction it pointed to before.
1033 assert(mulVec.size() > 0 && "No multiply code created?");
1034 vector<MachineInstr*>::iterator oldMvecI = mvecI;
1035 for (unsigned i=0, N=mulVec.size(); i < N; ++i)
1036 mvecI = mvec.insert(mvecI, mulVec[i]) + 1; // pts to mem instr
1037
1038 valueForRegOffset = addr;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001039 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00001040 }
1041 else
1042 {
1043 offsetOpType = MachineOperand::MO_SignExtendedImmed;
1044 smallConstOffset = 0;
1045 }
Vikram S. Adveed3fefb2002-08-03 13:48:21 +00001046
Vikram S. Advea10d1a72002-03-31 19:07:35 +00001047 // For STORE:
1048 // Operand 0 is value, operand 1 is ptr, operand 2 is offset
1049 // For LOAD or GET_ELEMENT_PTR,
1050 // Operand 0 is ptr, operand 1 is offset, operand 2 is result.
1051 //
1052 unsigned offsetOpNum, ptrOpNum;
1053 if (memInst->getOpcode() == Instruction::Store)
1054 {
1055 (*mvecI)->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister,
1056 vmInstrNode->leftChild()->getValue());
1057 ptrOpNum = 1;
1058 offsetOpNum = 2;
1059 }
1060 else
1061 {
1062 ptrOpNum = 0;
1063 offsetOpNum = 1;
1064 (*mvecI)->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister,
1065 memInst);
1066 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00001067
Vikram S. Advea10d1a72002-03-31 19:07:35 +00001068 (*mvecI)->SetMachineOperandVal(ptrOpNum, MachineOperand::MO_VirtualRegister,
1069 ptrVal);
1070
Chris Lattner20b1ea02001-09-14 03:47:57 +00001071 if (offsetOpType == MachineOperand::MO_VirtualRegister)
1072 {
1073 assert(valueForRegOffset != NULL);
Vikram S. Adve74825322002-03-18 03:15:35 +00001074 (*mvecI)->SetMachineOperandVal(offsetOpNum, offsetOpType,
1075 valueForRegOffset);
Chris Lattner20b1ea02001-09-14 03:47:57 +00001076 }
1077 else
Vikram S. Adve74825322002-03-18 03:15:35 +00001078 (*mvecI)->SetMachineOperandConst(offsetOpNum, offsetOpType,
1079 smallConstOffset);
Chris Lattner20b1ea02001-09-14 03:47:57 +00001080}
1081
1082
Chris Lattner20b1ea02001-09-14 03:47:57 +00001083//
1084// Substitute operand `operandNum' of the instruction in node `treeNode'
Vikram S. Advec025fc12001-10-14 23:28:43 +00001085// in place of the use(s) of that instruction in node `parent'.
1086// Check both explicit and implicit operands!
Vikram S. Adve74825322002-03-18 03:15:35 +00001087// Also make sure to skip over a parent who:
1088// (1) is a list node in the Burg tree, or
1089// (2) itself had its results forwarded to its parent
Chris Lattner20b1ea02001-09-14 03:47:57 +00001090//
1091static void
1092ForwardOperand(InstructionNode* treeNode,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001093 InstrTreeNode* parent,
1094 int operandNum)
Chris Lattner20b1ea02001-09-14 03:47:57 +00001095{
Vikram S. Adve243dd452001-09-18 13:03:13 +00001096 assert(treeNode && parent && "Invalid invocation of ForwardOperand");
1097
Chris Lattner20b1ea02001-09-14 03:47:57 +00001098 Instruction* unusedOp = treeNode->getInstruction();
1099 Value* fwdOp = unusedOp->getOperand(operandNum);
Vikram S. Adve243dd452001-09-18 13:03:13 +00001100
1101 // The parent itself may be a list node, so find the real parent instruction
1102 while (parent->getNodeType() != InstrTreeNode::NTInstructionNode)
1103 {
1104 parent = parent->parent();
1105 assert(parent && "ERROR: Non-instruction node has no parent in tree.");
1106 }
1107 InstructionNode* parentInstrNode = (InstructionNode*) parent;
1108
1109 Instruction* userInstr = parentInstrNode->getInstruction();
Chris Lattner9c461082002-02-03 07:50:56 +00001110 MachineCodeForInstruction &mvec = MachineCodeForInstruction::get(userInstr);
Vikram S. Adve74825322002-03-18 03:15:35 +00001111
1112 // The parent's mvec would be empty if it was itself forwarded.
1113 // Recursively call ForwardOperand in that case...
1114 //
1115 if (mvec.size() == 0)
Chris Lattner20b1ea02001-09-14 03:47:57 +00001116 {
Vikram S. Adve74825322002-03-18 03:15:35 +00001117 assert(parent->parent() != NULL &&
1118 "Parent could not have been forwarded, yet has no instructions?");
1119 ForwardOperand(treeNode, parent->parent(), operandNum);
1120 }
1121 else
1122 {
Vikram S. Adve74825322002-03-18 03:15:35 +00001123 for (unsigned i=0, N=mvec.size(); i < N; i++)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001124 {
Vikram S. Adve74825322002-03-18 03:15:35 +00001125 MachineInstr* minstr = mvec[i];
1126 for (unsigned i=0, numOps=minstr->getNumOperands(); i < numOps; ++i)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001127 {
Vikram S. Adve74825322002-03-18 03:15:35 +00001128 const MachineOperand& mop = minstr->getOperand(i);
1129 if (mop.getOperandType() == MachineOperand::MO_VirtualRegister &&
1130 mop.getVRegValue() == unusedOp)
Vikram S. Adve242a8082002-05-19 15:25:51 +00001131 minstr->SetMachineOperandVal(i,
Vikram S. Adve74825322002-03-18 03:15:35 +00001132 MachineOperand::MO_VirtualRegister, fwdOp);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001133 }
Vikram S. Adve74825322002-03-18 03:15:35 +00001134
1135 for (unsigned i=0,numOps=minstr->getNumImplicitRefs(); i<numOps; ++i)
1136 if (minstr->getImplicitRef(i) == unusedOp)
Vikram S. Adve242a8082002-05-19 15:25:51 +00001137 minstr->setImplicitRef(i, fwdOp,
Vikram S. Adve6418eac2002-07-08 23:30:14 +00001138 minstr->implicitRefIsDefined(i),
1139 minstr->implicitRefIsDefinedAndUsed(i));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001140 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00001141 }
1142}
1143
1144
Vikram S. Adve242a8082002-05-19 15:25:51 +00001145inline bool
1146AllUsesAreBranches(const Instruction* setccI)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001147{
Vikram S. Adve242a8082002-05-19 15:25:51 +00001148 for (Value::use_const_iterator UI=setccI->use_begin(), UE=setccI->use_end();
1149 UI != UE; ++UI)
1150 if (! isa<TmpInstruction>(*UI) // ignore tmp instructions here
1151 && cast<Instruction>(*UI)->getOpcode() != Instruction::Br)
1152 return false;
1153 return true;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001154}
1155
Vikram S. Advefb361122001-10-22 13:36:31 +00001156//******************* Externally Visible Functions *************************/
1157
Vikram S. Advefb361122001-10-22 13:36:31 +00001158//------------------------------------------------------------------------
1159// External Function: ThisIsAChainRule
1160//
1161// Purpose:
1162// Check if a given BURG rule is a chain rule.
1163//------------------------------------------------------------------------
1164
1165extern bool
1166ThisIsAChainRule(int eruleno)
1167{
1168 switch(eruleno)
1169 {
1170 case 111: // stmt: reg
Vikram S. Advefb361122001-10-22 13:36:31 +00001171 case 123:
1172 case 124:
1173 case 125:
1174 case 126:
1175 case 127:
1176 case 128:
1177 case 129:
1178 case 130:
1179 case 131:
1180 case 132:
1181 case 133:
1182 case 155:
1183 case 221:
1184 case 222:
1185 case 241:
1186 case 242:
1187 case 243:
1188 case 244:
Vikram S. Adve30a6f492002-08-22 02:56:10 +00001189 case 245:
Vikram S. Adve85e1e9c2002-04-01 20:28:48 +00001190 case 321:
Vikram S. Advefb361122001-10-22 13:36:31 +00001191 return true; break;
Vikram S. Adve30a6f492002-08-22 02:56:10 +00001192
Vikram S. Advefb361122001-10-22 13:36:31 +00001193 default:
1194 return false; break;
1195 }
1196}
Chris Lattner20b1ea02001-09-14 03:47:57 +00001197
1198
1199//------------------------------------------------------------------------
1200// External Function: GetInstructionsByRule
1201//
1202// Purpose:
1203// Choose machine instructions for the SPARC according to the
1204// patterns chosen by the BURG-generated parser.
1205//------------------------------------------------------------------------
1206
Vikram S. Adve74825322002-03-18 03:15:35 +00001207void
Chris Lattner20b1ea02001-09-14 03:47:57 +00001208GetInstructionsByRule(InstructionNode* subtreeRoot,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001209 int ruleForNode,
1210 short* nts,
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001211 TargetMachine &target,
Vikram S. Adve74825322002-03-18 03:15:35 +00001212 vector<MachineInstr*>& mvec)
Chris Lattner20b1ea02001-09-14 03:47:57 +00001213{
Chris Lattner20b1ea02001-09-14 03:47:57 +00001214 bool checkCast = false; // initialize here to use fall-through
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001215 bool maskUnsignedResult = false;
Chris Lattner20b1ea02001-09-14 03:47:57 +00001216 int nextRule;
1217 int forwardOperandNum = -1;
Vikram S. Adve74825322002-03-18 03:15:35 +00001218 unsigned int allocaSize = 0;
1219 MachineInstr* M, *M2;
1220 unsigned int L;
1221
1222 mvec.clear();
Chris Lattner20b1ea02001-09-14 03:47:57 +00001223
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001224 // If the code for this instruction was folded into the parent (user),
1225 // then do nothing!
1226 if (subtreeRoot->isFoldedIntoParent())
1227 return;
1228
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001229 //
1230 // Let's check for chain rules outside the switch so that we don't have
1231 // to duplicate the list of chain rule production numbers here again
1232 //
1233 if (ThisIsAChainRule(ruleForNode))
Chris Lattner20b1ea02001-09-14 03:47:57 +00001234 {
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001235 // Chain rules have a single nonterminal on the RHS.
1236 // Get the rule that matches the RHS non-terminal and use that instead.
1237 //
1238 assert(nts[0] && ! nts[1]
1239 && "A chain rule should have only one RHS non-terminal!");
1240 nextRule = burm_rule(subtreeRoot->state, nts[0]);
1241 nts = burm_nts[nextRule];
Vikram S. Adve74825322002-03-18 03:15:35 +00001242 GetInstructionsByRule(subtreeRoot, nextRule, nts, target, mvec);
Chris Lattner20b1ea02001-09-14 03:47:57 +00001243 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001244 else
Chris Lattner20b1ea02001-09-14 03:47:57 +00001245 {
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001246 switch(ruleForNode) {
1247 case 1: // stmt: Ret
1248 case 2: // stmt: RetValue(reg)
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001249 { // NOTE: Prepass of register allocation is responsible
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001250 // for moving return value to appropriate register.
1251 // Mark the return-address register as a hidden virtual reg.
Vikram S. Advea995e602001-10-11 04:23:19 +00001252 // Mark the return value register as an implicit ref of
1253 // the machine instruction.
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001254 // Finally put a NOP in the delay slot.
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001255 ReturnInst *returnInstr =
1256 cast<ReturnInst>(subtreeRoot->getInstruction());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001257 assert(returnInstr->getOpcode() == Instruction::Ret);
1258
Chris Lattner9c461082002-02-03 07:50:56 +00001259 Instruction* returnReg = new TmpInstruction(returnInstr);
1260 MachineCodeForInstruction::get(returnInstr).addTemp(returnReg);
Vikram S. Advefb361122001-10-22 13:36:31 +00001261
Vikram S. Adve74825322002-03-18 03:15:35 +00001262 M = new MachineInstr(JMPLRET);
1263 M->SetMachineOperandReg(0, MachineOperand::MO_VirtualRegister,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001264 returnReg);
Vikram S. Adve74825322002-03-18 03:15:35 +00001265 M->SetMachineOperandConst(1,MachineOperand::MO_SignExtendedImmed,
Chris Lattner697954c2002-01-20 22:54:45 +00001266 (int64_t)8);
Vikram S. Adve74825322002-03-18 03:15:35 +00001267 M->SetMachineOperandReg(2, target.getRegInfo().getZeroRegNum());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001268
Vikram S. Advea995e602001-10-11 04:23:19 +00001269 if (returnInstr->getReturnValue() != NULL)
Vikram S. Adve74825322002-03-18 03:15:35 +00001270 M->addImplicitRef(returnInstr->getReturnValue());
Vikram S. Advea995e602001-10-11 04:23:19 +00001271
Vikram S. Adve74825322002-03-18 03:15:35 +00001272 mvec.push_back(M);
1273 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001274
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001275 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001276 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001277
1278 case 3: // stmt: Store(reg,reg)
1279 case 4: // stmt: Store(reg,ptrreg)
Vikram S. Adve74825322002-03-18 03:15:35 +00001280 mvec.push_back(new MachineInstr(
1281 ChooseStoreInstruction(
1282 subtreeRoot->leftChild()->getValue()->getType())));
Vikram S. Adve8cfffd32002-08-24 20:56:53 +00001283 SetOperandsForMemInstr(mvec, subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001284 break;
Chris Lattner20b1ea02001-09-14 03:47:57 +00001285
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001286 case 5: // stmt: BrUncond
Vikram S. Adve74825322002-03-18 03:15:35 +00001287 M = new MachineInstr(BA);
Vikram S. Adve6418eac2002-07-08 23:30:14 +00001288 M->SetMachineOperandVal(0, MachineOperand::MO_PCRelativeDisp,
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001289 cast<BranchInst>(subtreeRoot->getInstruction())->getSuccessor(0));
Vikram S. Adve74825322002-03-18 03:15:35 +00001290 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001291
1292 // delay slot
Vikram S. Adve74825322002-03-18 03:15:35 +00001293 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001294 break;
Chris Lattner20b1ea02001-09-14 03:47:57 +00001295
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001296 case 206: // stmt: BrCond(setCCconst)
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001297 { // setCCconst => boolean was computed with `%b = setCC type reg1 const'
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001298 // If the constant is ZERO, we can use the branch-on-integer-register
1299 // instructions and avoid the SUBcc instruction entirely.
1300 // Otherwise this is just the same as case 5, so just fall through.
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001301 //
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001302 InstrTreeNode* constNode = subtreeRoot->leftChild()->rightChild();
1303 assert(constNode &&
1304 constNode->getNodeType() ==InstrTreeNode::NTConstNode);
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001305 Constant *constVal = cast<Constant>(constNode->getValue());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001306 bool isValidConst;
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001307
Chris Lattner0c4e8862002-09-03 01:08:28 +00001308 if ((constVal->getType()->isInteger()
Chris Lattner9b625032002-05-06 16:15:30 +00001309 || isa<PointerType>(constVal->getType()))
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001310 && GetConstantValueAsSignedInt(constVal, isValidConst) == 0
1311 && isValidConst)
1312 {
1313 // That constant is a zero after all...
1314 // Use the left child of setCC as the first argument!
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001315 // Mark the setCC node so that no code is generated for it.
1316 InstructionNode* setCCNode = (InstructionNode*)
1317 subtreeRoot->leftChild();
1318 assert(setCCNode->getOpLabel() == SetCCOp);
1319 setCCNode->markFoldedIntoParent();
1320
1321 BranchInst* brInst=cast<BranchInst>(subtreeRoot->getInstruction());
1322
Vikram S. Adve74825322002-03-18 03:15:35 +00001323 M = new MachineInstr(ChooseBprInstruction(subtreeRoot));
1324 M->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister,
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001325 setCCNode->leftChild()->getValue());
Vikram S. Adve74825322002-03-18 03:15:35 +00001326 M->SetMachineOperandVal(1, MachineOperand::MO_PCRelativeDisp,
1327 brInst->getSuccessor(0));
1328 mvec.push_back(M);
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001329
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001330 // delay slot
Vikram S. Adve74825322002-03-18 03:15:35 +00001331 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001332
1333 // false branch
Vikram S. Adve74825322002-03-18 03:15:35 +00001334 M = new MachineInstr(BA);
Vikram S. Adve6418eac2002-07-08 23:30:14 +00001335 M->SetMachineOperandVal(0, MachineOperand::MO_PCRelativeDisp,
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001336 brInst->getSuccessor(1));
Vikram S. Adve74825322002-03-18 03:15:35 +00001337 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001338
1339 // delay slot
Vikram S. Adve74825322002-03-18 03:15:35 +00001340 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001341
1342 break;
1343 }
1344 // ELSE FALL THROUGH
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001345 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001346
Vikram S. Adve30a6f492002-08-22 02:56:10 +00001347 case 6: // stmt: BrCond(setCC)
1348 { // bool => boolean was computed with SetCC.
1349 // The branch to use depends on whether it is FP, signed, or unsigned.
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001350 // If it is an integer CC, we also need to find the unique
1351 // TmpInstruction representing that CC.
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001352 //
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001353 BranchInst* brInst = cast<BranchInst>(subtreeRoot->getInstruction());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001354 bool isFPBranch;
Vikram S. Adve74825322002-03-18 03:15:35 +00001355 M = new MachineInstr(ChooseBccInstruction(subtreeRoot, isFPBranch));
Vikram S. Adve30a6f492002-08-22 02:56:10 +00001356
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001357 Value* ccValue = GetTmpForCC(subtreeRoot->leftChild()->getValue(),
1358 brInst->getParent()->getParent(),
1359 isFPBranch? Type::FloatTy : Type::IntTy);
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001360
Vikram S. Adve74825322002-03-18 03:15:35 +00001361 M->SetMachineOperandVal(0, MachineOperand::MO_CCRegister, ccValue);
1362 M->SetMachineOperandVal(1, MachineOperand::MO_PCRelativeDisp,
1363 brInst->getSuccessor(0));
1364 mvec.push_back(M);
Vikram S. Adve30a6f492002-08-22 02:56:10 +00001365
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001366 // delay slot
Vikram S. Adve74825322002-03-18 03:15:35 +00001367 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adve30a6f492002-08-22 02:56:10 +00001368
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001369 // false branch
Vikram S. Adve74825322002-03-18 03:15:35 +00001370 M = new MachineInstr(BA);
Vikram S. Adve6418eac2002-07-08 23:30:14 +00001371 M->SetMachineOperandVal(0, MachineOperand::MO_PCRelativeDisp,
Vikram S. Adve74825322002-03-18 03:15:35 +00001372 brInst->getSuccessor(1));
1373 mvec.push_back(M);
Vikram S. Adve30a6f492002-08-22 02:56:10 +00001374
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001375 // delay slot
Vikram S. Adve74825322002-03-18 03:15:35 +00001376 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001377 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001378 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001379
1380 case 208: // stmt: BrCond(boolconst)
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001381 {
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001382 // boolconst => boolean is a constant; use BA to first or second label
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001383 Constant* constVal =
1384 cast<Constant>(subtreeRoot->leftChild()->getValue());
1385 unsigned dest = cast<ConstantBool>(constVal)->getValue()? 0 : 1;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001386
Vikram S. Adve74825322002-03-18 03:15:35 +00001387 M = new MachineInstr(BA);
Vikram S. Adve6418eac2002-07-08 23:30:14 +00001388 M->SetMachineOperandVal(0, MachineOperand::MO_PCRelativeDisp,
Chris Lattner35504202002-04-27 03:14:39 +00001389 cast<BranchInst>(subtreeRoot->getInstruction())->getSuccessor(dest));
Vikram S. Adve74825322002-03-18 03:15:35 +00001390 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001391
1392 // delay slot
Vikram S. Adve74825322002-03-18 03:15:35 +00001393 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001394 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001395 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001396
1397 case 8: // stmt: BrCond(boolreg)
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001398 { // boolreg => boolean is stored in an existing register.
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001399 // Just use the branch-on-integer-register instruction!
1400 //
Vikram S. Adve74825322002-03-18 03:15:35 +00001401 M = new MachineInstr(BRNZ);
1402 M->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001403 subtreeRoot->leftChild()->getValue());
Vikram S. Adve74825322002-03-18 03:15:35 +00001404 M->SetMachineOperandVal(1, MachineOperand::MO_PCRelativeDisp,
Chris Lattner35504202002-04-27 03:14:39 +00001405 cast<BranchInst>(subtreeRoot->getInstruction())->getSuccessor(0));
Vikram S. Adve74825322002-03-18 03:15:35 +00001406 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001407
1408 // delay slot
Vikram S. Adve74825322002-03-18 03:15:35 +00001409 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001410
1411 // false branch
Vikram S. Adve74825322002-03-18 03:15:35 +00001412 M = new MachineInstr(BA);
Vikram S. Adve6418eac2002-07-08 23:30:14 +00001413 M->SetMachineOperandVal(0, MachineOperand::MO_PCRelativeDisp,
Chris Lattner35504202002-04-27 03:14:39 +00001414 cast<BranchInst>(subtreeRoot->getInstruction())->getSuccessor(1));
Vikram S. Adve74825322002-03-18 03:15:35 +00001415 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001416
1417 // delay slot
Vikram S. Adve74825322002-03-18 03:15:35 +00001418 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001419 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001420 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00001421
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001422 case 9: // stmt: Switch(reg)
1423 assert(0 && "*** SWITCH instruction is not implemented yet.");
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001424 break;
Chris Lattner20b1ea02001-09-14 03:47:57 +00001425
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001426 case 10: // reg: VRegList(reg, reg)
1427 assert(0 && "VRegList should never be the topmost non-chain rule");
1428 break;
1429
Vikram S. Advece08e1d2002-08-15 14:17:37 +00001430 case 21: // bool: Not(bool,reg): Both these are implemented as:
1431 case 421: // reg: BNot(reg,reg): reg = reg XOR-NOT 0
1432 { // First find the unary operand. It may be left or right, usually right.
1433 Value* notArg = BinaryOperator::getNotArgument(
1434 cast<BinaryOperator>(subtreeRoot->getInstruction()));
1435 mvec.push_back(Create3OperandInstr_Reg(XNOR, notArg,
1436 target.getRegInfo().getZeroRegNum(),
1437 subtreeRoot->getValue()));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001438 break;
Vikram S. Advece08e1d2002-08-15 14:17:37 +00001439 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001440
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001441 case 22: // reg: ToBoolTy(reg):
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001442 {
1443 const Type* opType = subtreeRoot->leftChild()->getValue()->getType();
Chris Lattner0c4e8862002-09-03 01:08:28 +00001444 assert(opType->isIntegral() || isa<PointerType>(opType));
Vikram S. Adve74825322002-03-18 03:15:35 +00001445 forwardOperandNum = 0; // forward first operand to user
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001446 break;
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001447 }
1448
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001449 case 23: // reg: ToUByteTy(reg)
1450 case 25: // reg: ToUShortTy(reg)
1451 case 27: // reg: ToUIntTy(reg)
1452 case 29: // reg: ToULongTy(reg)
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001453 {
Vikram S. Adve242a8082002-05-19 15:25:51 +00001454 Instruction* destI = subtreeRoot->getInstruction();
1455 Value* opVal = subtreeRoot->leftChild()->getValue();
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001456 const Type* opType = subtreeRoot->leftChild()->getValue()->getType();
Chris Lattner0c4e8862002-09-03 01:08:28 +00001457 if (opType->isIntegral() || isa<PointerType>(opType))
Vikram S. Adve1e606692002-07-31 21:01:34 +00001458 {
1459 unsigned opSize = target.DataLayout.getTypeSize(opType);
1460 unsigned destSize = target.DataLayout.getTypeSize(destI->getType());
1461 if (opSize > destSize ||
1462 (opType->isSigned()
1463 && destSize < target.DataLayout.getIntegerRegize()))
1464 { // operand is larger than dest,
1465 // OR both are equal but smaller than the full register size
1466 // AND operand is signed, so it may have extra sign bits:
1467 // mask high bits using AND
1468 M = Create3OperandInstr(AND, opVal,
1469 ConstantUInt::get(Type::ULongTy,
1470 ((uint64_t) 1 << 8*destSize) - 1),
1471 destI);
1472 mvec.push_back(M);
1473 }
1474 else
1475 forwardOperandNum = 0; // forward first operand to user
Vikram S. Adve242a8082002-05-19 15:25:51 +00001476 }
Vikram S. Adve1e606692002-07-31 21:01:34 +00001477 else if (opType->isFloatingPoint())
Vikram S. Adve8cfffd32002-08-24 20:56:53 +00001478 {
1479 CreateCodeToConvertFloatToInt(target, opVal, destI, mvec,
1480 MachineCodeForInstruction::get(destI));
1481 maskUnsignedResult = true; // not handled by convert code
1482 }
Vikram S. Adve242a8082002-05-19 15:25:51 +00001483 else
Vikram S. Adve1e606692002-07-31 21:01:34 +00001484 assert(0 && "Unrecognized operand type for convert-to-unsigned");
1485
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001486 break;
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001487 }
1488
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001489 case 24: // reg: ToSByteTy(reg)
1490 case 26: // reg: ToShortTy(reg)
1491 case 28: // reg: ToIntTy(reg)
1492 case 30: // reg: ToLongTy(reg)
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001493 {
Vikram S. Adve242a8082002-05-19 15:25:51 +00001494 Instruction* destI = subtreeRoot->getInstruction();
1495 Value* opVal = subtreeRoot->leftChild()->getValue();
1496 MachineCodeForInstruction& mcfi =MachineCodeForInstruction::get(destI);
Vikram S. Adve1e606692002-07-31 21:01:34 +00001497
Vikram S. Adve242a8082002-05-19 15:25:51 +00001498 const Type* opType = opVal->getType();
Chris Lattner0c4e8862002-09-03 01:08:28 +00001499 if (opType->isIntegral() || isa<PointerType>(opType))
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001500 {
Vikram S. Adve242a8082002-05-19 15:25:51 +00001501 // These operand types have the same format as the destination,
1502 // but may have different size: add sign bits or mask as needed.
1503 //
1504 const Type* destType = destI->getType();
1505 unsigned opSize = target.DataLayout.getTypeSize(opType);
1506 unsigned destSize = target.DataLayout.getTypeSize(destType);
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001507
1508 if (opSize < destSize ||
1509 (opSize == destSize &&
1510 opSize == target.DataLayout.getIntegerRegize()))
1511 { // operand is smaller or both operand and result fill register
1512 forwardOperandNum = 0; // forward first operand to user
Vikram S. Adve242a8082002-05-19 15:25:51 +00001513 }
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001514 else
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001515 { // need to mask (possibly) and then sign-extend (definitely)
1516 Value* srcForSignExt = opVal;
1517 unsigned srcSizeForSignExt = 8 * opSize;
1518 if (opSize > destSize)
1519 { // operand is larger than dest: mask high bits
1520 TmpInstruction *tmpI = new TmpInstruction(destType, opVal,
1521 destI, "maskHi");
1522 mcfi.addTemp(tmpI);
1523 M = Create3OperandInstr(AND, opVal,
1524 ConstantUInt::get(Type::ULongTy,
1525 ((uint64_t) 1 << 8*destSize)-1),
1526 tmpI);
1527 mvec.push_back(M);
1528 srcForSignExt = tmpI;
1529 srcSizeForSignExt = 8 * destSize;
1530 }
1531
1532 // sign-extend
1533 target.getInstrInfo().CreateSignExtensionInstructions(target, destI->getParent()->getParent(), srcForSignExt, srcSizeForSignExt, destI, mvec, mcfi);
1534 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001535 }
Vikram S. Adve1e606692002-07-31 21:01:34 +00001536 else if (opType->isFloatingPoint())
Vikram S. Adve8cfffd32002-08-24 20:56:53 +00001537 CreateCodeToConvertFloatToInt(target, opVal, destI, mvec, mcfi);
Vikram S. Adve1e606692002-07-31 21:01:34 +00001538 else
1539 assert(0 && "Unrecognized operand type for convert-to-signed");
1540
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001541 break;
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001542 }
Vikram S. Advebabc0fa2002-09-05 18:32:13 +00001543
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001544 case 31: // reg: ToFloatTy(reg):
1545 case 32: // reg: ToDoubleTy(reg):
1546 case 232: // reg: ToDoubleTy(Constant):
Vikram S. Adveec7f4822002-09-09 14:54:21 +00001547
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001548 // If this instruction has a parent (a user) in the tree
1549 // and the user is translated as an FsMULd instruction,
1550 // then the cast is unnecessary. So check that first.
1551 // In the future, we'll want to do the same for the FdMULq instruction,
1552 // so do the check here instead of only for ToFloatTy(reg).
1553 //
Vikram S. Adveec7f4822002-09-09 14:54:21 +00001554 if (subtreeRoot->parent() != NULL)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001555 {
Vikram S. Adveec7f4822002-09-09 14:54:21 +00001556 const MachineCodeForInstruction& mcfi =
1557 MachineCodeForInstruction::get(
1558 cast<InstructionNode>(subtreeRoot->parent())->getInstruction());
1559 if (mcfi.size() == 0 || mcfi.front()->getOpCode() == FSMULD)
1560 forwardOperandNum = 0; // forward first operand to user
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001561 }
Vikram S. Adveec7f4822002-09-09 14:54:21 +00001562
1563 if (forwardOperandNum != 0) // we do need the cast
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001564 {
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001565 Value* leftVal = subtreeRoot->leftChild()->getValue();
1566 const Type* opType = leftVal->getType();
Vikram S. Advedbc4fad2002-04-25 04:37:51 +00001567 MachineOpCode opCode=ChooseConvertToFloatInstr(
1568 subtreeRoot->getOpLabel(), opType);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001569 if (opCode == INVALID_OPCODE) // no conversion needed
1570 {
Vikram S. Adve74825322002-03-18 03:15:35 +00001571 forwardOperandNum = 0; // forward first operand to user
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001572 }
1573 else
1574 {
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001575 // If the source operand is a non-FP type it must be
1576 // first copied from int to float register via memory!
1577 Instruction *dest = subtreeRoot->getInstruction();
1578 Value* srcForCast;
1579 int n = 0;
Vikram S. Adve242a8082002-05-19 15:25:51 +00001580 if (! opType->isFloatingPoint())
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001581 {
1582 // Create a temporary to represent the FP register
1583 // into which the integer will be copied via memory.
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001584 // The type of this temporary will determine the FP
1585 // register used: single-prec for a 32-bit int or smaller,
1586 // double-prec for a 64-bit int.
1587 //
Vikram S. Advebabc0fa2002-09-05 18:32:13 +00001588 uint64_t srcSize =
1589 target.DataLayout.getTypeSize(leftVal->getType());
1590 Type* tmpTypeToUse =
1591 (srcSize <= 4)? Type::FloatTy : Type::DoubleTy;
1592 srcForCast = new TmpInstruction(tmpTypeToUse, dest);
Vikram S. Advedbc4fad2002-04-25 04:37:51 +00001593 MachineCodeForInstruction &destMCFI =
Chris Lattner9c461082002-02-03 07:50:56 +00001594 MachineCodeForInstruction::get(dest);
Vikram S. Advedbc4fad2002-04-25 04:37:51 +00001595 destMCFI.addTemp(srcForCast);
Vikram S. Advebabc0fa2002-09-05 18:32:13 +00001596
Vikram S. Adve242a8082002-05-19 15:25:51 +00001597 target.getInstrInfo().CreateCodeToCopyIntToFloat(target,
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001598 dest->getParent()->getParent(),
Vikram S. Advebabc0fa2002-09-05 18:32:13 +00001599 leftVal, cast<Instruction>(srcForCast),
Vikram S. Adve242a8082002-05-19 15:25:51 +00001600 mvec, destMCFI);
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001601 }
1602 else
1603 srcForCast = leftVal;
1604
Vikram S. Adve74825322002-03-18 03:15:35 +00001605 M = new MachineInstr(opCode);
1606 M->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister,
1607 srcForCast);
1608 M->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister,
1609 dest);
1610 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001611 }
1612 }
1613 break;
1614
1615 case 19: // reg: ToArrayTy(reg):
1616 case 20: // reg: ToPointerTy(reg):
Vikram S. Adve74825322002-03-18 03:15:35 +00001617 forwardOperandNum = 0; // forward first operand to user
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001618 break;
1619
1620 case 233: // reg: Add(reg, Constant)
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001621 maskUnsignedResult = true;
Vikram S. Adve74825322002-03-18 03:15:35 +00001622 M = CreateAddConstInstruction(subtreeRoot);
1623 if (M != NULL)
1624 {
1625 mvec.push_back(M);
1626 break;
1627 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001628 // ELSE FALL THROUGH
Vikram S. Adve74825322002-03-18 03:15:35 +00001629
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001630 case 33: // reg: Add(reg, reg)
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001631 maskUnsignedResult = true;
Vikram S. Adve74825322002-03-18 03:15:35 +00001632 mvec.push_back(new MachineInstr(ChooseAddInstruction(subtreeRoot)));
1633 Set3OperandsFromInstr(mvec.back(), subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001634 break;
1635
1636 case 234: // reg: Sub(reg, Constant)
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001637 maskUnsignedResult = true;
Vikram S. Adve74825322002-03-18 03:15:35 +00001638 M = CreateSubConstInstruction(subtreeRoot);
1639 if (M != NULL)
1640 {
1641 mvec.push_back(M);
1642 break;
1643 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001644 // ELSE FALL THROUGH
Vikram S. Adve74825322002-03-18 03:15:35 +00001645
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001646 case 34: // reg: Sub(reg, reg)
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001647 maskUnsignedResult = true;
Vikram S. Adve74825322002-03-18 03:15:35 +00001648 mvec.push_back(new MachineInstr(ChooseSubInstructionByType(
1649 subtreeRoot->getInstruction()->getType())));
1650 Set3OperandsFromInstr(mvec.back(), subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001651 break;
1652
1653 case 135: // reg: Mul(todouble, todouble)
1654 checkCast = true;
1655 // FALL THROUGH
1656
1657 case 35: // reg: Mul(reg, reg)
Vikram S. Adve74825322002-03-18 03:15:35 +00001658 {
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001659 maskUnsignedResult = true;
Vikram S. Adve74825322002-03-18 03:15:35 +00001660 MachineOpCode forceOp = ((checkCast && BothFloatToDouble(subtreeRoot))
1661 ? FSMULD
1662 : INVALID_MACHINE_OPCODE);
Vikram S. Adve242a8082002-05-19 15:25:51 +00001663 Instruction* mulInstr = subtreeRoot->getInstruction();
1664 CreateMulInstruction(target, mulInstr->getParent()->getParent(),
Vikram S. Adve74825322002-03-18 03:15:35 +00001665 subtreeRoot->leftChild()->getValue(),
1666 subtreeRoot->rightChild()->getValue(),
Vikram S. Adve242a8082002-05-19 15:25:51 +00001667 mulInstr, mvec,
1668 MachineCodeForInstruction::get(mulInstr),forceOp);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001669 break;
Vikram S. Adve74825322002-03-18 03:15:35 +00001670 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001671 case 335: // reg: Mul(todouble, todoubleConst)
1672 checkCast = true;
1673 // FALL THROUGH
1674
1675 case 235: // reg: Mul(reg, Constant)
Vikram S. Adve74825322002-03-18 03:15:35 +00001676 {
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001677 maskUnsignedResult = true;
Vikram S. Adve74825322002-03-18 03:15:35 +00001678 MachineOpCode forceOp = ((checkCast && BothFloatToDouble(subtreeRoot))
1679 ? FSMULD
1680 : INVALID_MACHINE_OPCODE);
Vikram S. Adve242a8082002-05-19 15:25:51 +00001681 Instruction* mulInstr = subtreeRoot->getInstruction();
1682 CreateMulInstruction(target, mulInstr->getParent()->getParent(),
Vikram S. Adve74825322002-03-18 03:15:35 +00001683 subtreeRoot->leftChild()->getValue(),
1684 subtreeRoot->rightChild()->getValue(),
Vikram S. Adve242a8082002-05-19 15:25:51 +00001685 mulInstr, mvec,
1686 MachineCodeForInstruction::get(mulInstr),
1687 forceOp);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001688 break;
Vikram S. Adve74825322002-03-18 03:15:35 +00001689 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001690 case 236: // reg: Div(reg, Constant)
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001691 maskUnsignedResult = true;
Vikram S. Adve74825322002-03-18 03:15:35 +00001692 L = mvec.size();
1693 CreateDivConstInstruction(target, subtreeRoot, mvec);
1694 if (mvec.size() > L)
1695 break;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001696 // ELSE FALL THROUGH
Vikram S. Adve74825322002-03-18 03:15:35 +00001697
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001698 case 36: // reg: Div(reg, reg)
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001699 maskUnsignedResult = true;
Vikram S. Adve74825322002-03-18 03:15:35 +00001700 mvec.push_back(new MachineInstr(ChooseDivInstruction(target, subtreeRoot)));
1701 Set3OperandsFromInstr(mvec.back(), subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001702 break;
1703
1704 case 37: // reg: Rem(reg, reg)
1705 case 237: // reg: Rem(reg, Constant)
Vikram S. Adve510eec72001-11-04 21:59:14 +00001706 {
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001707 maskUnsignedResult = true;
Vikram S. Adve510eec72001-11-04 21:59:14 +00001708 Instruction* remInstr = subtreeRoot->getInstruction();
1709
Chris Lattner9c461082002-02-03 07:50:56 +00001710 TmpInstruction* quot = new TmpInstruction(
Vikram S. Adve510eec72001-11-04 21:59:14 +00001711 subtreeRoot->leftChild()->getValue(),
1712 subtreeRoot->rightChild()->getValue());
Chris Lattner9c461082002-02-03 07:50:56 +00001713 TmpInstruction* prod = new TmpInstruction(
Vikram S. Adve510eec72001-11-04 21:59:14 +00001714 quot,
1715 subtreeRoot->rightChild()->getValue());
Chris Lattner9c461082002-02-03 07:50:56 +00001716 MachineCodeForInstruction::get(remInstr).addTemp(quot).addTemp(prod);
Vikram S. Adve510eec72001-11-04 21:59:14 +00001717
Vikram S. Adve74825322002-03-18 03:15:35 +00001718 M = new MachineInstr(ChooseDivInstruction(target, subtreeRoot));
1719 Set3OperandsFromInstr(M, subtreeRoot, target);
1720 M->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister,quot);
1721 mvec.push_back(M);
Vikram S. Adve510eec72001-11-04 21:59:14 +00001722
Vikram S. Advece08e1d2002-08-15 14:17:37 +00001723 M = Create3OperandInstr(ChooseMulInstructionByType(
1724 subtreeRoot->getInstruction()->getType()),
1725 quot, subtreeRoot->rightChild()->getValue(),
1726 prod);
Vikram S. Adve74825322002-03-18 03:15:35 +00001727 mvec.push_back(M);
Vikram S. Adve510eec72001-11-04 21:59:14 +00001728
Vikram S. Adve74825322002-03-18 03:15:35 +00001729 M = new MachineInstr(ChooseSubInstructionByType(
Vikram S. Adve510eec72001-11-04 21:59:14 +00001730 subtreeRoot->getInstruction()->getType()));
Vikram S. Adve74825322002-03-18 03:15:35 +00001731 Set3OperandsFromInstr(M, subtreeRoot, target);
1732 M->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister,prod);
1733 mvec.push_back(M);
Vikram S. Adve510eec72001-11-04 21:59:14 +00001734
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001735 break;
Vikram S. Adve510eec72001-11-04 21:59:14 +00001736 }
1737
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001738 case 38: // bool: And(bool, bool)
1739 case 238: // bool: And(bool, boolconst)
1740 case 338: // reg : BAnd(reg, reg)
1741 case 538: // reg : BAnd(reg, Constant)
Vikram S. Adve74825322002-03-18 03:15:35 +00001742 mvec.push_back(new MachineInstr(AND));
1743 Set3OperandsFromInstr(mvec.back(), subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001744 break;
1745
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001746 case 138: // bool: And(bool, not)
Vikram S. Advece08e1d2002-08-15 14:17:37 +00001747 case 438: // bool: BAnd(bool, bnot)
1748 { // Use the argument of NOT as the second argument!
1749 // Mark the NOT node so that no code is generated for it.
1750 InstructionNode* notNode = (InstructionNode*) subtreeRoot->rightChild();
1751 Value* notArg = BinaryOperator::getNotArgument(
1752 cast<BinaryOperator>(notNode->getInstruction()));
1753 notNode->markFoldedIntoParent();
1754 mvec.push_back(Create3OperandInstr(ANDN,
1755 subtreeRoot->leftChild()->getValue(),
1756 notArg, subtreeRoot->getValue()));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001757 break;
Vikram S. Advece08e1d2002-08-15 14:17:37 +00001758 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001759
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001760 case 39: // bool: Or(bool, bool)
1761 case 239: // bool: Or(bool, boolconst)
1762 case 339: // reg : BOr(reg, reg)
1763 case 539: // reg : BOr(reg, Constant)
Vikram S. Adve242a8082002-05-19 15:25:51 +00001764 mvec.push_back(new MachineInstr(OR));
Vikram S. Adve74825322002-03-18 03:15:35 +00001765 Set3OperandsFromInstr(mvec.back(), subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001766 break;
1767
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001768 case 139: // bool: Or(bool, not)
Vikram S. Advece08e1d2002-08-15 14:17:37 +00001769 case 439: // bool: BOr(bool, bnot)
1770 { // Use the argument of NOT as the second argument!
1771 // Mark the NOT node so that no code is generated for it.
1772 InstructionNode* notNode = (InstructionNode*) subtreeRoot->rightChild();
1773 Value* notArg = BinaryOperator::getNotArgument(
1774 cast<BinaryOperator>(notNode->getInstruction()));
1775 notNode->markFoldedIntoParent();
1776 mvec.push_back(Create3OperandInstr(ORN,
1777 subtreeRoot->leftChild()->getValue(),
1778 notArg, subtreeRoot->getValue()));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001779 break;
Vikram S. Advece08e1d2002-08-15 14:17:37 +00001780 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001781
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001782 case 40: // bool: Xor(bool, bool)
1783 case 240: // bool: Xor(bool, boolconst)
1784 case 340: // reg : BXor(reg, reg)
1785 case 540: // reg : BXor(reg, Constant)
Vikram S. Adve74825322002-03-18 03:15:35 +00001786 mvec.push_back(new MachineInstr(XOR));
1787 Set3OperandsFromInstr(mvec.back(), subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001788 break;
1789
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001790 case 140: // bool: Xor(bool, not)
Vikram S. Advece08e1d2002-08-15 14:17:37 +00001791 case 440: // bool: BXor(bool, bnot)
1792 { // Use the argument of NOT as the second argument!
1793 // Mark the NOT node so that no code is generated for it.
1794 InstructionNode* notNode = (InstructionNode*) subtreeRoot->rightChild();
1795 Value* notArg = BinaryOperator::getNotArgument(
1796 cast<BinaryOperator>(notNode->getInstruction()));
1797 notNode->markFoldedIntoParent();
1798 mvec.push_back(Create3OperandInstr(XNOR,
1799 subtreeRoot->leftChild()->getValue(),
1800 notArg, subtreeRoot->getValue()));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001801 break;
Vikram S. Advece08e1d2002-08-15 14:17:37 +00001802 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001803
1804 case 41: // boolconst: SetCC(reg, Constant)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001805 //
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001806 // If the SetCC was folded into the user (parent), it will be
1807 // caught above. All other cases are the same as case 42,
1808 // so just fall through.
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001809 //
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001810 case 42: // bool: SetCC(reg, reg):
1811 {
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001812 // This generates a SUBCC instruction, putting the difference in
1813 // a result register, and setting a condition code.
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001814 //
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001815 // If the boolean result of the SetCC is used by anything other
Vikram S. Adve6418eac2002-07-08 23:30:14 +00001816 // than a branch instruction, or if it is used outside the current
1817 // basic block, the boolean must be
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001818 // computed and stored in the result register. Otherwise, discard
1819 // the difference (by using %g0) and keep only the condition code.
1820 //
1821 // To compute the boolean result in a register we use a conditional
1822 // move, unless the result of the SUBCC instruction can be used as
1823 // the bool! This assumes that zero is FALSE and any non-zero
1824 // integer is TRUE.
1825 //
1826 InstructionNode* parentNode = (InstructionNode*) subtreeRoot->parent();
1827 Instruction* setCCInstr = subtreeRoot->getInstruction();
Vikram S. Adve242a8082002-05-19 15:25:51 +00001828
Vikram S. Adve6418eac2002-07-08 23:30:14 +00001829 bool keepBoolVal = parentNode == NULL ||
1830 ! AllUsesAreBranches(setCCInstr);
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001831 bool subValIsBoolVal = setCCInstr->getOpcode() == Instruction::SetNE;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001832 bool keepSubVal = keepBoolVal && subValIsBoolVal;
1833 bool computeBoolVal = keepBoolVal && ! subValIsBoolVal;
1834
1835 bool mustClearReg;
1836 int valueToMove;
Chris Lattner8e5c0b42001-11-07 14:01:59 +00001837 MachineOpCode movOpCode = 0;
Vikram S. Adve6418eac2002-07-08 23:30:14 +00001838
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001839 // Mark the 4th operand as being a CC register, and as a def
1840 // A TmpInstruction is created to represent the CC "result".
1841 // Unlike other instances of TmpInstruction, this one is used
1842 // by machine code of multiple LLVM instructions, viz.,
1843 // the SetCC and the branch. Make sure to get the same one!
1844 // Note that we do this even for FP CC registers even though they
1845 // are explicit operands, because the type of the operand
1846 // needs to be a floating point condition code, not an integer
1847 // condition code. Think of this as casting the bool result to
1848 // a FP condition code register.
1849 //
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001850 Value* leftVal = subtreeRoot->leftChild()->getValue();
Chris Lattner9b625032002-05-06 16:15:30 +00001851 bool isFPCompare = leftVal->getType()->isFloatingPoint();
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001852
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001853 TmpInstruction* tmpForCC = GetTmpForCC(setCCInstr,
1854 setCCInstr->getParent()->getParent(),
Chris Lattner9b625032002-05-06 16:15:30 +00001855 isFPCompare ? Type::FloatTy : Type::IntTy);
Chris Lattner9c461082002-02-03 07:50:56 +00001856 MachineCodeForInstruction::get(setCCInstr).addTemp(tmpForCC);
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001857
1858 if (! isFPCompare)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001859 {
1860 // Integer condition: dest. should be %g0 or an integer register.
1861 // If result must be saved but condition is not SetEQ then we need
1862 // a separate instruction to compute the bool result, so discard
1863 // result of SUBcc instruction anyway.
1864 //
Vikram S. Adve74825322002-03-18 03:15:35 +00001865 M = new MachineInstr(SUBcc);
1866 Set3OperandsFromInstr(M, subtreeRoot, target, ! keepSubVal);
1867 M->SetMachineOperandVal(3, MachineOperand::MO_CCRegister,
1868 tmpForCC, /*def*/true);
1869 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001870
1871 if (computeBoolVal)
1872 { // recompute bool using the integer condition codes
1873 movOpCode =
1874 ChooseMovpccAfterSub(subtreeRoot,mustClearReg,valueToMove);
1875 }
1876 }
1877 else
1878 {
1879 // FP condition: dest of FCMP should be some FCCn register
Vikram S. Adve74825322002-03-18 03:15:35 +00001880 M = new MachineInstr(ChooseFcmpInstruction(subtreeRoot));
1881 M->SetMachineOperandVal(0, MachineOperand::MO_CCRegister,
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001882 tmpForCC);
Vikram S. Adve74825322002-03-18 03:15:35 +00001883 M->SetMachineOperandVal(1,MachineOperand::MO_VirtualRegister,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001884 subtreeRoot->leftChild()->getValue());
Vikram S. Adve74825322002-03-18 03:15:35 +00001885 M->SetMachineOperandVal(2,MachineOperand::MO_VirtualRegister,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001886 subtreeRoot->rightChild()->getValue());
Vikram S. Adve74825322002-03-18 03:15:35 +00001887 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001888
1889 if (computeBoolVal)
1890 {// recompute bool using the FP condition codes
1891 mustClearReg = true;
1892 valueToMove = 1;
1893 movOpCode = ChooseMovFpccInstruction(subtreeRoot);
1894 }
1895 }
1896
1897 if (computeBoolVal)
1898 {
1899 if (mustClearReg)
1900 {// Unconditionally set register to 0
Vikram S. Adve74825322002-03-18 03:15:35 +00001901 M = new MachineInstr(SETHI);
1902 M->SetMachineOperandConst(0,MachineOperand::MO_UnextendedImmed,
1903 (int64_t)0);
1904 M->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister,
1905 setCCInstr);
1906 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001907 }
1908
1909 // Now conditionally move `valueToMove' (0 or 1) into the register
Vikram S. Adve6418eac2002-07-08 23:30:14 +00001910 // Mark the register as a use (as well as a def) because the old
1911 // value should be retained if the condition is false.
Vikram S. Adve74825322002-03-18 03:15:35 +00001912 M = new MachineInstr(movOpCode);
1913 M->SetMachineOperandVal(0, MachineOperand::MO_CCRegister,
1914 tmpForCC);
1915 M->SetMachineOperandConst(1, MachineOperand::MO_UnextendedImmed,
1916 valueToMove);
1917 M->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister,
Vikram S. Adve6418eac2002-07-08 23:30:14 +00001918 setCCInstr, /*isDef*/ true,
1919 /*isDefAndUse*/ true);
Vikram S. Adve74825322002-03-18 03:15:35 +00001920 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001921 }
1922 break;
1923 }
1924
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001925 case 51: // reg: Load(reg)
1926 case 52: // reg: Load(ptrreg)
Vikram S. Adve74825322002-03-18 03:15:35 +00001927 mvec.push_back(new MachineInstr(ChooseLoadInstruction(
1928 subtreeRoot->getValue()->getType())));
Vikram S. Adve8cfffd32002-08-24 20:56:53 +00001929 SetOperandsForMemInstr(mvec, subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001930 break;
1931
1932 case 55: // reg: GetElemPtr(reg)
1933 case 56: // reg: GetElemPtrIdx(reg,reg)
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001934 // If the GetElemPtr was folded into the user (parent), it will be
1935 // caught above. For other cases, we have to compute the address.
Vikram S. Adve74825322002-03-18 03:15:35 +00001936 mvec.push_back(new MachineInstr(ADD));
Vikram S. Adve8cfffd32002-08-24 20:56:53 +00001937 SetOperandsForMemInstr(mvec, subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001938 break;
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001939
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001940 case 57: // reg: Alloca: Implement as 1 instruction:
1941 { // add %fp, offsetFromFP -> result
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001942 AllocationInst* instr =
1943 cast<AllocationInst>(subtreeRoot->getInstruction());
1944 unsigned int tsize =
1945 target.findOptimalStorageSize(instr->getAllocatedType());
Vikram S. Adve74825322002-03-18 03:15:35 +00001946 assert(tsize != 0);
1947 CreateCodeForFixedSizeAlloca(target, instr, tsize, 1, mvec);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001948 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001949 }
Vikram S. Adve74825322002-03-18 03:15:35 +00001950
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001951 case 58: // reg: Alloca(reg): Implement as 3 instructions:
1952 // mul num, typeSz -> tmp
1953 // sub %sp, tmp -> %sp
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001954 { // add %sp, frameSizeBelowDynamicArea -> result
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001955 AllocationInst* instr =
1956 cast<AllocationInst>(subtreeRoot->getInstruction());
Vikram S. Adve74825322002-03-18 03:15:35 +00001957 const Type* eltType = instr->getAllocatedType();
1958
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001959 // If #elements is constant, use simpler code for fixed-size allocas
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001960 int tsize = (int) target.findOptimalStorageSize(eltType);
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001961 Value* numElementsVal = NULL;
1962 bool isArray = instr->isArrayAllocation();
1963
1964 if (!isArray ||
1965 isa<Constant>(numElementsVal = instr->getArraySize()))
1966 { // total size is constant: generate code for fixed-size alloca
1967 unsigned int numElements = isArray?
1968 cast<ConstantUInt>(numElementsVal)->getValue() : 1;
1969 CreateCodeForFixedSizeAlloca(target, instr, tsize,
1970 numElements, mvec);
1971 }
Vikram S. Adve74825322002-03-18 03:15:35 +00001972 else // total size is not constant.
1973 CreateCodeForVariableSizeAlloca(target, instr, tsize,
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001974 numElementsVal, mvec);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001975 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001976 }
Vikram S. Adve74825322002-03-18 03:15:35 +00001977
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001978 case 61: // reg: Call
Vikram S. Advedbc4fad2002-04-25 04:37:51 +00001979 { // Generate a direct (CALL) or indirect (JMPL). depending
1980 // Mark the return-address register and the indirection
1981 // register (if any) as hidden virtual registers.
Vikram S. Advea995e602001-10-11 04:23:19 +00001982 // Also, mark the operands of the Call and return value (if
1983 // any) as implicit operands of the CALL machine instruction.
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001984 //
Vikram S. Advedbc4fad2002-04-25 04:37:51 +00001985 // If this is a varargs function, floating point arguments
1986 // have to passed in integer registers so insert
1987 // copy-float-to-int instructions for each float operand.
1988 //
Chris Lattnerb00c5822001-10-02 03:41:24 +00001989 CallInst *callInstr = cast<CallInst>(subtreeRoot->getInstruction());
Chris Lattner749655f2001-10-13 06:54:30 +00001990 Value *callee = callInstr->getCalledValue();
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001991
Vikram S. Advea10d1a72002-03-31 19:07:35 +00001992 // Create hidden virtual register for return address, with type void*.
Vikram S. Adve242a8082002-05-19 15:25:51 +00001993 TmpInstruction* retAddrReg =
Vikram S. Advea10d1a72002-03-31 19:07:35 +00001994 new TmpInstruction(PointerType::get(Type::VoidTy), callInstr);
Chris Lattner9c461082002-02-03 07:50:56 +00001995 MachineCodeForInstruction::get(callInstr).addTemp(retAddrReg);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001996
Vikram S. Adveea21a6c2001-10-20 20:57:06 +00001997 // Generate the machine instruction and its operands.
1998 // Use CALL for direct function calls; this optimistically assumes
1999 // the PC-relative address fits in the CALL address field (22 bits).
2000 // Use JMPL for indirect calls.
2001 //
Chris Lattnerb0d04722002-03-26 17:58:12 +00002002 if (isa<Function>(callee))
Vikram S. Adveea21a6c2001-10-20 20:57:06 +00002003 { // direct function call
Vikram S. Adve74825322002-03-18 03:15:35 +00002004 M = new MachineInstr(CALL);
2005 M->SetMachineOperandVal(0, MachineOperand::MO_PCRelativeDisp,
2006 callee);
Vikram S. Adveea21a6c2001-10-20 20:57:06 +00002007 }
2008 else
2009 { // indirect function call
Vikram S. Adve74825322002-03-18 03:15:35 +00002010 M = new MachineInstr(JMPLCALL);
2011 M->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister,
2012 callee);
2013 M->SetMachineOperandConst(1, MachineOperand::MO_SignExtendedImmed,
2014 (int64_t) 0);
2015 M->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister,
2016 retAddrReg);
Vikram S. Adveea21a6c2001-10-20 20:57:06 +00002017 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002018
Vikram S. Adve74825322002-03-18 03:15:35 +00002019 mvec.push_back(M);
Vikram S. Advea10d1a72002-03-31 19:07:35 +00002020
Vikram S. Adve242a8082002-05-19 15:25:51 +00002021 const FunctionType* funcType =
2022 cast<FunctionType>(cast<PointerType>(callee->getType())
2023 ->getElementType());
2024 bool isVarArgs = funcType->isVarArg();
2025 bool noPrototype = isVarArgs && funcType->getNumParams() == 0;
Vikram S. Advedbc4fad2002-04-25 04:37:51 +00002026
Vikram S. Adve242a8082002-05-19 15:25:51 +00002027 // Use an annotation to pass information about call arguments
2028 // to the register allocator.
2029 CallArgsDescriptor* argDesc = new CallArgsDescriptor(callInstr,
2030 retAddrReg, isVarArgs, noPrototype);
2031 M->addAnnotation(argDesc);
Vikram S. Advea995e602001-10-11 04:23:19 +00002032
Vikram S. Adve242a8082002-05-19 15:25:51 +00002033 assert(callInstr->getOperand(0) == callee
2034 && "This is assumed in the loop below!");
2035
2036 for (unsigned i=1, N=callInstr->getNumOperands(); i < N; ++i)
2037 {
2038 Value* argVal = callInstr->getOperand(i);
2039 Instruction* intArgReg = NULL;
2040
2041 // Check for FP arguments to varargs functions.
2042 // Any such argument in the first $K$ args must be passed in an
2043 // integer register, where K = #integer argument registers.
2044 if (isVarArgs && argVal->getType()->isFloatingPoint())
2045 {
2046 // If it is a function with no prototype, pass value
2047 // as an FP value as well as a varargs value
2048 if (noPrototype)
2049 argDesc->getArgInfo(i-1).setUseFPArgReg();
2050
2051 // If this arg. is in the first $K$ regs, add a copy
2052 // float-to-int instruction to pass the value as an integer.
2053 if (i < target.getRegInfo().GetNumOfIntArgRegs())
2054 {
2055 MachineCodeForInstruction &destMCFI =
2056 MachineCodeForInstruction::get(callInstr);
2057 intArgReg = new TmpInstruction(Type::IntTy, argVal);
2058 destMCFI.addTemp(intArgReg);
2059
2060 vector<MachineInstr*> copyMvec;
2061 target.getInstrInfo().CreateCodeToCopyFloatToInt(target,
2062 callInstr->getParent()->getParent(),
2063 argVal, (TmpInstruction*) intArgReg,
2064 copyMvec, destMCFI);
2065 mvec.insert(mvec.begin(),copyMvec.begin(),copyMvec.end());
2066
2067 argDesc->getArgInfo(i-1).setUseIntArgReg();
2068 argDesc->getArgInfo(i-1).setArgCopy(intArgReg);
2069 }
2070 else
2071 // Cannot fit in first $K$ regs so pass the arg on the stack
2072 argDesc->getArgInfo(i-1).setUseStackSlot();
2073 }
2074
2075 if (intArgReg)
2076 mvec.back()->addImplicitRef(intArgReg);
2077
2078 mvec.back()->addImplicitRef(argVal);
2079 }
2080
2081 // Add the return value as an implicit ref. The call operands
2082 // were added above.
Vikram S. Adveea21a6c2001-10-20 20:57:06 +00002083 if (callInstr->getType() != Type::VoidTy)
Vikram S. Adve74825322002-03-18 03:15:35 +00002084 mvec.back()->addImplicitRef(callInstr, /*isDef*/ true);
Vikram S. Advea995e602001-10-11 04:23:19 +00002085
Vikram S. Adveea21a6c2001-10-20 20:57:06 +00002086 // For the CALL instruction, the ret. addr. reg. is also implicit
Chris Lattnerb0d04722002-03-26 17:58:12 +00002087 if (isa<Function>(callee))
Vikram S. Adve74825322002-03-18 03:15:35 +00002088 mvec.back()->addImplicitRef(retAddrReg, /*isDef*/ true);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002089
Vikram S. Adve74825322002-03-18 03:15:35 +00002090 // delay slot
2091 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002092 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00002093 }
Vikram S. Adve242a8082002-05-19 15:25:51 +00002094
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002095 case 62: // reg: Shl(reg, reg)
Vikram S. Adve242a8082002-05-19 15:25:51 +00002096 {
2097 Value* argVal1 = subtreeRoot->leftChild()->getValue();
2098 Value* argVal2 = subtreeRoot->rightChild()->getValue();
2099 Instruction* shlInstr = subtreeRoot->getInstruction();
2100
2101 const Type* opType = argVal1->getType();
Chris Lattner0c4e8862002-09-03 01:08:28 +00002102 assert((opType->isInteger() || isa<PointerType>(opType)) &&
2103 "Shl unsupported for other types");
Vikram S. Adve242a8082002-05-19 15:25:51 +00002104
2105 CreateShiftInstructions(target, shlInstr->getParent()->getParent(),
2106 (opType == Type::LongTy)? SLLX : SLL,
2107 argVal1, argVal2, 0, shlInstr, mvec,
2108 MachineCodeForInstruction::get(shlInstr));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002109 break;
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00002110 }
2111
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002112 case 63: // reg: Shr(reg, reg)
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00002113 { const Type* opType = subtreeRoot->leftChild()->getValue()->getType();
Chris Lattner0c4e8862002-09-03 01:08:28 +00002114 assert((opType->isInteger() || isa<PointerType>(opType)) &&
2115 "Shr unsupported for other types");
Vikram S. Adve74825322002-03-18 03:15:35 +00002116 mvec.push_back(new MachineInstr((opType->isSigned()
2117 ? ((opType == Type::LongTy)? SRAX : SRA)
2118 : ((opType == Type::LongTy)? SRLX : SRL))));
2119 Set3OperandsFromInstr(mvec.back(), subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002120 break;
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00002121 }
2122
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002123 case 64: // reg: Phi(reg,reg)
Vikram S. Adve74825322002-03-18 03:15:35 +00002124 break; // don't forward the value
2125
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002126 case 71: // reg: VReg
2127 case 72: // reg: Constant
Vikram S. Adve74825322002-03-18 03:15:35 +00002128 break; // don't forward the value
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002129
2130 default:
2131 assert(0 && "Unrecognized BURG rule");
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002132 break;
2133 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00002134 }
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00002135
Chris Lattner20b1ea02001-09-14 03:47:57 +00002136 if (forwardOperandNum >= 0)
2137 { // We did not generate a machine instruction but need to use operand.
2138 // If user is in the same tree, replace Value in its machine operand.
2139 // If not, insert a copy instruction which should get coalesced away
2140 // by register allocation.
2141 if (subtreeRoot->parent() != NULL)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002142 ForwardOperand(subtreeRoot, subtreeRoot->parent(), forwardOperandNum);
Chris Lattner20b1ea02001-09-14 03:47:57 +00002143 else
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002144 {
Vikram S. Adve7fe27872001-10-18 00:26:20 +00002145 vector<MachineInstr*> minstrVec;
Vikram S. Adve242a8082002-05-19 15:25:51 +00002146 Instruction* instr = subtreeRoot->getInstruction();
2147 target.getInstrInfo().
2148 CreateCopyInstructionsByType(target,
2149 instr->getParent()->getParent(),
2150 instr->getOperand(forwardOperandNum),
2151 instr, minstrVec,
2152 MachineCodeForInstruction::get(instr));
Vikram S. Adve7fe27872001-10-18 00:26:20 +00002153 assert(minstrVec.size() > 0);
Vikram S. Adve74825322002-03-18 03:15:35 +00002154 mvec.insert(mvec.end(), minstrVec.begin(), minstrVec.end());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002155 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00002156 }
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00002157
2158 if (maskUnsignedResult)
2159 { // If result is unsigned and smaller than int reg size,
2160 // we need to clear high bits of result value.
2161 assert(forwardOperandNum < 0 && "Need mask but no instruction generated");
2162 Instruction* dest = subtreeRoot->getInstruction();
Vikram S. Adve8cfffd32002-08-24 20:56:53 +00002163 if (dest->getType()->isUnsigned())
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00002164 {
2165 unsigned destSize = target.DataLayout.getTypeSize(dest->getType());
Vikram S. Advebabc0fa2002-09-05 18:32:13 +00002166 if (destSize <= 4)
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00002167 { // Mask high bits. Use a TmpInstruction to represent the
2168 // intermediate result before masking. Since those instructions
2169 // have already been generated, go back and substitute tmpI
2170 // for dest in the result position of each one of them.
2171 TmpInstruction *tmpI = new TmpInstruction(dest->getType(), dest,
2172 NULL, "maskHi");
2173 MachineCodeForInstruction::get(dest).addTemp(tmpI);
2174
2175 for (unsigned i=0, N=mvec.size(); i < N; ++i)
2176 mvec[i]->substituteValue(dest, tmpI);
2177
Vikram S. Advebabc0fa2002-09-05 18:32:13 +00002178 M = Create3OperandInstr_UImmed(SRL, tmpI, 4-destSize, dest);
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00002179 mvec.push_back(M);
2180 }
Vikram S. Advebabc0fa2002-09-05 18:32:13 +00002181 else if (destSize < target.DataLayout.getIntegerRegize())
2182 assert(0 && "Unsupported type size: 32 < size < 64 bits");
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00002183 }
2184 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00002185}