blob: 2cde093bb250a2d91be2c1fa5adb3111755dad58 [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. Adve94c40812002-09-27 14:33:08 +0000271ChooseConvertFPToIntInstr(Type::PrimitiveID tid, const Type* opType)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000272{
273 MachineOpCode opCode = INVALID_OPCODE;;
Vikram S. Adve94c40812002-09-27 14:33:08 +0000274
275 assert((opType == Type::FloatTy || opType == Type::DoubleTy)
276 && "This function should only be called for FLOAT or DOUBLE");
277
278 if (tid==Type::UIntTyID)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000279 {
Vikram S. Adve94c40812002-09-27 14:33:08 +0000280 assert(tid != Type::UIntTyID && "FP-to-uint conversions must be expanded"
281 " into FP->long->uint for SPARC v9: SO RUN PRESELECTION PASS!");
282 }
283 else if (tid==Type::SByteTyID || tid==Type::ShortTyID || tid==Type::IntTyID ||
284 tid==Type::UByteTyID || tid==Type::UShortTyID)
285 {
286 opCode = (opType == Type::FloatTy)? FSTOI : FDTOI;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000287 }
Vikram S. Adve1e606692002-07-31 21:01:34 +0000288 else if (tid==Type::LongTyID || tid==Type::ULongTyID)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000289 {
Vikram S. Adve94c40812002-09-27 14:33:08 +0000290 opCode = (opType == Type::FloatTy)? FSTOX : FDTOX;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000291 }
292 else
293 assert(0 && "Should not get here, Mo!");
Vikram S. Adve94c40812002-09-27 14:33:08 +0000294
Chris Lattner20b1ea02001-09-14 03:47:57 +0000295 return opCode;
296}
297
Vikram S. Advedbc4fad2002-04-25 04:37:51 +0000298MachineInstr*
Vikram S. Adve94c40812002-09-27 14:33:08 +0000299CreateConvertFPToIntInstr(Type::PrimitiveID destTID,
300 Value* srcVal, Value* destVal)
Vikram S. Advedbc4fad2002-04-25 04:37:51 +0000301{
Vikram S. Adve94c40812002-09-27 14:33:08 +0000302 MachineOpCode opCode = ChooseConvertFPToIntInstr(destTID, srcVal->getType());
Vikram S. Advedbc4fad2002-04-25 04:37:51 +0000303 assert(opCode != INVALID_OPCODE && "Expected to need conversion!");
304
305 MachineInstr* M = new MachineInstr(opCode);
306 M->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister, srcVal);
307 M->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister, destVal);
308 return M;
309}
Chris Lattner20b1ea02001-09-14 03:47:57 +0000310
Vikram S. Adve8cfffd32002-08-24 20:56:53 +0000311// CreateCodeToConvertFloatToInt: Convert FP value to signed or unsigned integer
Vikram S. Adve1e606692002-07-31 21:01:34 +0000312// The FP value must be converted to the dest type in an FP register,
313// and the result is then copied from FP to int register via memory.
Vikram S. Advebabc0fa2002-09-05 18:32:13 +0000314//
315// Since fdtoi converts to signed integers, any FP value V between MAXINT+1
316// and MAXUNSIGNED (i.e., 2^31 <= V <= 2^32-1) would be converted incorrectly
317// *only* when converting to an unsigned int. (Unsigned byte, short or long
318// don't have this problem.)
319// For unsigned int, we therefore have to generate the code sequence:
320//
321// if (V > (float) MAXINT) {
322// unsigned result = (unsigned) (V - (float) MAXINT);
323// result = result + (unsigned) MAXINT;
324// }
325// else
326// result = (unsigned int) V;
327//
Vikram S. Adve1e606692002-07-31 21:01:34 +0000328static void
Vikram S. Adve8cfffd32002-08-24 20:56:53 +0000329CreateCodeToConvertFloatToInt(const TargetMachine& target,
330 Value* opVal,
331 Instruction* destI,
332 std::vector<MachineInstr*>& mvec,
333 MachineCodeForInstruction& mcfi)
Vikram S. Adve1e606692002-07-31 21:01:34 +0000334{
335 // Create a temporary to represent the FP register into which the
336 // int value will placed after conversion. The type of this temporary
337 // depends on the type of FP register to use: single-prec for a 32-bit
338 // int or smaller; double-prec for a 64-bit int.
339 //
Vikram S. Advebabc0fa2002-09-05 18:32:13 +0000340 size_t destSize = target.DataLayout.getTypeSize(destI->getType());
341 const Type* destTypeToUse = (destSize > 4)? Type::DoubleTy : Type::FloatTy;
342 TmpInstruction* destForCast = new TmpInstruction(destTypeToUse, opVal);
Vikram S. Adve1e606692002-07-31 21:01:34 +0000343 mcfi.addTemp(destForCast);
344
345 // Create the fp-to-int conversion code
Vikram S. Adve94c40812002-09-27 14:33:08 +0000346 MachineInstr* M =CreateConvertFPToIntInstr(destI->getType()->getPrimitiveID(),
347 opVal, destForCast);
Vikram S. Adve1e606692002-07-31 21:01:34 +0000348 mvec.push_back(M);
349
350 // Create the fpreg-to-intreg copy code
351 target.getInstrInfo().
352 CreateCodeToCopyFloatToInt(target, destI->getParent()->getParent(),
Vikram S. Advebabc0fa2002-09-05 18:32:13 +0000353 destForCast, destI, mvec, mcfi);
Vikram S. Adve1e606692002-07-31 21:01:34 +0000354}
355
356
Chris Lattner20b1ea02001-09-14 03:47:57 +0000357static inline MachineOpCode
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000358ChooseAddInstruction(const InstructionNode* instrNode)
359{
360 return ChooseAddInstructionByType(instrNode->getInstruction()->getType());
361}
362
363
Chris Lattner20b1ea02001-09-14 03:47:57 +0000364static inline MachineInstr*
365CreateMovFloatInstruction(const InstructionNode* instrNode,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000366 const Type* resultType)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000367{
368 MachineInstr* minstr = new MachineInstr((resultType == Type::FloatTy)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000369 ? FMOVS : FMOVD);
Vikram S. Adve74825322002-03-18 03:15:35 +0000370 minstr->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister,
371 instrNode->leftChild()->getValue());
372 minstr->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister,
373 instrNode->getValue());
Chris Lattner20b1ea02001-09-14 03:47:57 +0000374 return minstr;
375}
376
377static inline MachineInstr*
378CreateAddConstInstruction(const InstructionNode* instrNode)
379{
380 MachineInstr* minstr = NULL;
381
382 Value* constOp = ((InstrTreeNode*) instrNode->rightChild())->getValue();
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000383 assert(isa<Constant>(constOp));
Chris Lattner20b1ea02001-09-14 03:47:57 +0000384
385 // Cases worth optimizing are:
386 // (1) Add with 0 for float or double: use an FMOV of appropriate type,
387 // instead of an FADD (1 vs 3 cycles). There is no integer MOV.
388 //
Chris Lattner9b625032002-05-06 16:15:30 +0000389 if (ConstantFP *FPC = dyn_cast<ConstantFP>(constOp)) {
390 double dval = FPC->getValue();
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000391 if (dval == 0.0)
Vikram S. Adve242a8082002-05-19 15:25:51 +0000392 minstr = CreateMovFloatInstruction(instrNode,
393 instrNode->getInstruction()->getType());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000394 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000395
396 return minstr;
397}
398
399
400static inline MachineOpCode
Vikram S. Adve510eec72001-11-04 21:59:14 +0000401ChooseSubInstructionByType(const Type* resultType)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000402{
403 MachineOpCode opCode = INVALID_OPCODE;
404
Chris Lattner0c4e8862002-09-03 01:08:28 +0000405 if (resultType->isInteger() || isa<PointerType>(resultType))
Chris Lattner20b1ea02001-09-14 03:47:57 +0000406 {
407 opCode = SUB;
408 }
409 else
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000410 switch(resultType->getPrimitiveID())
411 {
412 case Type::FloatTyID: opCode = FSUBS; break;
413 case Type::DoubleTyID: opCode = FSUBD; break;
414 default: assert(0 && "Invalid type for SUB instruction"); break;
415 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000416
417 return opCode;
418}
419
420
421static inline MachineInstr*
422CreateSubConstInstruction(const InstructionNode* instrNode)
423{
424 MachineInstr* minstr = NULL;
425
426 Value* constOp = ((InstrTreeNode*) instrNode->rightChild())->getValue();
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000427 assert(isa<Constant>(constOp));
Chris Lattner20b1ea02001-09-14 03:47:57 +0000428
429 // Cases worth optimizing are:
430 // (1) Sub with 0 for float or double: use an FMOV of appropriate type,
431 // instead of an FSUB (1 vs 3 cycles). There is no integer MOV.
432 //
Chris Lattner9b625032002-05-06 16:15:30 +0000433 if (ConstantFP *FPC = dyn_cast<ConstantFP>(constOp)) {
434 double dval = FPC->getValue();
435 if (dval == 0.0)
Vikram S. Adve242a8082002-05-19 15:25:51 +0000436 minstr = CreateMovFloatInstruction(instrNode,
437 instrNode->getInstruction()->getType());
Chris Lattner9b625032002-05-06 16:15:30 +0000438 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000439
440 return minstr;
441}
442
443
444static inline MachineOpCode
445ChooseFcmpInstruction(const InstructionNode* instrNode)
446{
447 MachineOpCode opCode = INVALID_OPCODE;
448
449 Value* operand = ((InstrTreeNode*) instrNode->leftChild())->getValue();
450 switch(operand->getType()->getPrimitiveID()) {
451 case Type::FloatTyID: opCode = FCMPS; break;
452 case Type::DoubleTyID: opCode = FCMPD; break;
453 default: assert(0 && "Invalid type for FCMP instruction"); break;
454 }
455
456 return opCode;
457}
458
459
460// Assumes that leftArg and rightArg are both cast instructions.
461//
462static inline bool
463BothFloatToDouble(const InstructionNode* instrNode)
464{
465 InstrTreeNode* leftArg = instrNode->leftChild();
466 InstrTreeNode* rightArg = instrNode->rightChild();
467 InstrTreeNode* leftArgArg = leftArg->leftChild();
468 InstrTreeNode* rightArgArg = rightArg->leftChild();
469 assert(leftArg->getValue()->getType() == rightArg->getValue()->getType());
470
471 // Check if both arguments are floats cast to double
472 return (leftArg->getValue()->getType() == Type::DoubleTy &&
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000473 leftArgArg->getValue()->getType() == Type::FloatTy &&
474 rightArgArg->getValue()->getType() == Type::FloatTy);
Chris Lattner20b1ea02001-09-14 03:47:57 +0000475}
476
477
478static inline MachineOpCode
Vikram S. Adve510eec72001-11-04 21:59:14 +0000479ChooseMulInstructionByType(const Type* resultType)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000480{
481 MachineOpCode opCode = INVALID_OPCODE;
482
Chris Lattner0c4e8862002-09-03 01:08:28 +0000483 if (resultType->isInteger())
Vikram S. Adve510eec72001-11-04 21:59:14 +0000484 opCode = MULX;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000485 else
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000486 switch(resultType->getPrimitiveID())
487 {
488 case Type::FloatTyID: opCode = FMULS; break;
489 case Type::DoubleTyID: opCode = FMULD; break;
490 default: assert(0 && "Invalid type for MUL instruction"); break;
491 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000492
493 return opCode;
494}
495
496
Vikram S. Adve510eec72001-11-04 21:59:14 +0000497
Chris Lattner20b1ea02001-09-14 03:47:57 +0000498static inline MachineInstr*
Vikram S. Adve74825322002-03-18 03:15:35 +0000499CreateIntNegInstruction(const TargetMachine& target,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000500 Value* vreg)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000501{
502 MachineInstr* minstr = new MachineInstr(SUB);
Vikram S. Adve74825322002-03-18 03:15:35 +0000503 minstr->SetMachineOperandReg(0, target.getRegInfo().getZeroRegNum());
504 minstr->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister, vreg);
505 minstr->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister, vreg);
Chris Lattner20b1ea02001-09-14 03:47:57 +0000506 return minstr;
507}
508
509
Vikram S. Adve242a8082002-05-19 15:25:51 +0000510// Create instruction sequence for any shift operation.
511// SLL or SLLX on an operand smaller than the integer reg. size (64bits)
512// requires a second instruction for explicit sign-extension.
513// Note that we only have to worry about a sign-bit appearing in the
514// most significant bit of the operand after shifting (e.g., bit 32 of
515// Int or bit 16 of Short), so we do not have to worry about results
516// that are as large as a normal integer register.
517//
518static inline void
519CreateShiftInstructions(const TargetMachine& target,
520 Function* F,
521 MachineOpCode shiftOpCode,
522 Value* argVal1,
523 Value* optArgVal2, /* Use optArgVal2 if not NULL */
524 unsigned int optShiftNum, /* else use optShiftNum */
525 Instruction* destVal,
526 vector<MachineInstr*>& mvec,
527 MachineCodeForInstruction& mcfi)
528{
529 assert((optArgVal2 != NULL || optShiftNum <= 64) &&
530 "Large shift sizes unexpected, but can be handled below: "
531 "You need to check whether or not it fits in immed field below");
532
533 // If this is a logical left shift of a type smaller than the standard
534 // integer reg. size, we have to extend the sign-bit into upper bits
535 // of dest, so we need to put the result of the SLL into a temporary.
536 //
537 Value* shiftDest = destVal;
Vikram S. Adve242a8082002-05-19 15:25:51 +0000538 unsigned opSize = target.DataLayout.getTypeSize(argVal1->getType());
539 if ((shiftOpCode == SLL || shiftOpCode == SLLX)
540 && opSize < target.DataLayout.getIntegerRegize())
541 { // put SLL result into a temporary
542 shiftDest = new TmpInstruction(argVal1, optArgVal2, "sllTmp");
543 mcfi.addTemp(shiftDest);
544 }
545
546 MachineInstr* M = (optArgVal2 != NULL)
547 ? Create3OperandInstr(shiftOpCode, argVal1, optArgVal2, shiftDest)
548 : Create3OperandInstr_UImmed(shiftOpCode, argVal1, optShiftNum, shiftDest);
549 mvec.push_back(M);
550
551 if (shiftDest != destVal)
552 { // extend the sign-bit of the result into all upper bits of dest
553 assert(8*opSize <= 32 && "Unexpected type size > 4 and < IntRegSize?");
554 target.getInstrInfo().
Vikram S. Adve94c40812002-09-27 14:33:08 +0000555 CreateSignExtensionInstructions(target, F, shiftDest, destVal,
556 8*opSize, mvec, mcfi);
Vikram S. Adve242a8082002-05-19 15:25:51 +0000557 }
558}
559
560
Vikram S. Adve74825322002-03-18 03:15:35 +0000561// Does not create any instructions if we cannot exploit constant to
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000562// create a cheaper instruction.
563// This returns the approximate cost of the instructions generated,
564// which is used to pick the cheapest when both operands are constant.
565static inline unsigned int
Vikram S. Adve242a8082002-05-19 15:25:51 +0000566CreateMulConstInstruction(const TargetMachine &target, Function* F,
567 Value* lval, Value* rval, Instruction* destVal,
568 vector<MachineInstr*>& mvec,
569 MachineCodeForInstruction& mcfi)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000570{
Vikram S. Adve242a8082002-05-19 15:25:51 +0000571 /* Use max. multiply cost, viz., cost of MULX */
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000572 unsigned int cost = target.getInstrInfo().minLatency(MULX);
Vikram S. Adve242a8082002-05-19 15:25:51 +0000573 unsigned int firstNewInstr = mvec.size();
Vikram S. Adve74825322002-03-18 03:15:35 +0000574
575 Value* constOp = rval;
576 if (! isa<Constant>(constOp))
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000577 return cost;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000578
579 // Cases worth optimizing are:
580 // (1) Multiply by 0 or 1 for any type: replace with copy (ADD or FMOV)
581 // (2) Multiply by 2^x for integer types: replace with Shift
582 //
Vikram S. Adve74825322002-03-18 03:15:35 +0000583 const Type* resultType = destVal->getType();
Chris Lattner20b1ea02001-09-14 03:47:57 +0000584
Chris Lattner0c4e8862002-09-03 01:08:28 +0000585 if (resultType->isInteger() || isa<PointerType>(resultType))
Chris Lattner20b1ea02001-09-14 03:47:57 +0000586 {
Chris Lattner20b1ea02001-09-14 03:47:57 +0000587 bool isValidConst;
588 int64_t C = GetConstantValueAsSignedInt(constOp, isValidConst);
589 if (isValidConst)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000590 {
Vikram S. Adve242a8082002-05-19 15:25:51 +0000591 unsigned pow;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000592 bool needNeg = false;
593 if (C < 0)
594 {
595 needNeg = true;
596 C = -C;
597 }
598
599 if (C == 0 || C == 1)
600 {
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000601 cost = target.getInstrInfo().minLatency(ADD);
Vikram S. Adve242a8082002-05-19 15:25:51 +0000602 MachineInstr* M = (C == 0)
603 ? Create3OperandInstr_Reg(ADD,
604 target.getRegInfo().getZeroRegNum(),
605 target.getRegInfo().getZeroRegNum(),
606 destVal)
607 : Create3OperandInstr_Reg(ADD, lval,
608 target.getRegInfo().getZeroRegNum(),
609 destVal);
610 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000611 }
Chris Lattner36346c72002-05-19 21:20:19 +0000612 else if (isPowerOf2(C, pow))
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000613 {
Vikram S. Adve242a8082002-05-19 15:25:51 +0000614 unsigned int opSize = target.DataLayout.getTypeSize(resultType);
615 MachineOpCode opCode = (opSize <= 32)? SLL : SLLX;
616 CreateShiftInstructions(target, F, opCode, lval, NULL, pow,
617 destVal, mvec, mcfi);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000618 }
619
Vikram S. Adve242a8082002-05-19 15:25:51 +0000620 if (mvec.size() > 0 && needNeg)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000621 { // insert <reg = SUB 0, reg> after the instr to flip the sign
Vikram S. Adve242a8082002-05-19 15:25:51 +0000622 MachineInstr* M = CreateIntNegInstruction(target, destVal);
623 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000624 }
625 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000626 }
627 else
628 {
Chris Lattner9b625032002-05-06 16:15:30 +0000629 if (ConstantFP *FPC = dyn_cast<ConstantFP>(constOp))
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000630 {
Chris Lattner9b625032002-05-06 16:15:30 +0000631 double dval = FPC->getValue();
Vikram S. Adve6ad7c552001-11-09 02:18:16 +0000632 if (fabs(dval) == 1)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000633 {
Vikram S. Adve242a8082002-05-19 15:25:51 +0000634 MachineOpCode opCode = (dval < 0)
Vikram S. Adve6ad7c552001-11-09 02:18:16 +0000635 ? (resultType == Type::FloatTy? FNEGS : FNEGD)
636 : (resultType == Type::FloatTy? FMOVS : FMOVD);
Vikram S. Adve242a8082002-05-19 15:25:51 +0000637 MachineInstr* M = Create2OperandInstr(opCode, lval, destVal);
638 mvec.push_back(M);
Vikram S. Adve6ad7c552001-11-09 02:18:16 +0000639 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000640 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000641 }
642
Vikram S. Adve242a8082002-05-19 15:25:51 +0000643 if (firstNewInstr < mvec.size())
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000644 {
Vikram S. Adve242a8082002-05-19 15:25:51 +0000645 cost = 0;
646 for (unsigned int i=firstNewInstr; i < mvec.size(); ++i)
647 cost += target.getInstrInfo().minLatency(mvec[i]->getOpCode());
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000648 }
649
650 return cost;
Vikram S. Adve74825322002-03-18 03:15:35 +0000651}
652
653
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000654// Does not create any instructions if we cannot exploit constant to
655// create a cheaper instruction.
656//
657static inline void
658CreateCheapestMulConstInstruction(const TargetMachine &target,
Vikram S. Adve242a8082002-05-19 15:25:51 +0000659 Function* F,
660 Value* lval, Value* rval,
661 Instruction* destVal,
662 vector<MachineInstr*>& mvec,
663 MachineCodeForInstruction& mcfi)
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000664{
665 Value* constOp;
666 if (isa<Constant>(lval) && isa<Constant>(rval))
667 { // both operands are constant: try both orders!
668 vector<MachineInstr*> mvec1, mvec2;
Vikram S. Adve242a8082002-05-19 15:25:51 +0000669 unsigned int lcost = CreateMulConstInstruction(target, F, lval, rval,
670 destVal, mvec1, mcfi);
671 unsigned int rcost = CreateMulConstInstruction(target, F, rval, lval,
672 destVal, mvec2, mcfi);
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000673 vector<MachineInstr*>& mincostMvec = (lcost <= rcost)? mvec1 : mvec2;
674 vector<MachineInstr*>& maxcostMvec = (lcost <= rcost)? mvec2 : mvec1;
675 mvec.insert(mvec.end(), mincostMvec.begin(), mincostMvec.end());
676
677 for (unsigned int i=0; i < maxcostMvec.size(); ++i)
678 delete maxcostMvec[i];
679 }
680 else if (isa<Constant>(rval)) // rval is constant, but not lval
Vikram S. Adve242a8082002-05-19 15:25:51 +0000681 CreateMulConstInstruction(target, F, lval, rval, destVal, mvec, mcfi);
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000682 else if (isa<Constant>(lval)) // lval is constant, but not rval
Vikram S. Adve242a8082002-05-19 15:25:51 +0000683 CreateMulConstInstruction(target, F, lval, rval, destVal, mvec, mcfi);
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000684
685 // else neither is constant
686 return;
687}
688
Vikram S. Adve74825322002-03-18 03:15:35 +0000689// Return NULL if we cannot exploit constant to create a cheaper instruction
690static inline void
Vikram S. Adve242a8082002-05-19 15:25:51 +0000691CreateMulInstruction(const TargetMachine &target, Function* F,
692 Value* lval, Value* rval, Instruction* destVal,
Vikram S. Adve74825322002-03-18 03:15:35 +0000693 vector<MachineInstr*>& mvec,
Vikram S. Adve242a8082002-05-19 15:25:51 +0000694 MachineCodeForInstruction& mcfi,
Vikram S. Adve74825322002-03-18 03:15:35 +0000695 MachineOpCode forceMulOp = INVALID_MACHINE_OPCODE)
696{
697 unsigned int L = mvec.size();
Vikram S. Adve242a8082002-05-19 15:25:51 +0000698 CreateCheapestMulConstInstruction(target,F, lval, rval, destVal, mvec, mcfi);
Vikram S. Adve74825322002-03-18 03:15:35 +0000699 if (mvec.size() == L)
700 { // no instructions were added so create MUL reg, reg, reg.
701 // Use FSMULD if both operands are actually floats cast to doubles.
702 // Otherwise, use the default opcode for the appropriate type.
703 MachineOpCode mulOp = ((forceMulOp != INVALID_MACHINE_OPCODE)
704 ? forceMulOp
705 : ChooseMulInstructionByType(destVal->getType()));
706 MachineInstr* M = new MachineInstr(mulOp);
707 M->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister, lval);
708 M->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister, rval);
709 M->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister, destVal);
710 mvec.push_back(M);
711 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000712}
713
714
Vikram S. Adve510eec72001-11-04 21:59:14 +0000715// Generate a divide instruction for Div or Rem.
716// For Rem, this assumes that the operand type will be signed if the result
717// type is signed. This is correct because they must have the same sign.
718//
Chris Lattner20b1ea02001-09-14 03:47:57 +0000719static inline MachineOpCode
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000720ChooseDivInstruction(TargetMachine &target,
721 const InstructionNode* instrNode)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000722{
723 MachineOpCode opCode = INVALID_OPCODE;
724
725 const Type* resultType = instrNode->getInstruction()->getType();
726
Chris Lattner0c4e8862002-09-03 01:08:28 +0000727 if (resultType->isInteger())
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000728 opCode = resultType->isSigned()? SDIVX : UDIVX;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000729 else
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000730 switch(resultType->getPrimitiveID())
731 {
732 case Type::FloatTyID: opCode = FDIVS; break;
733 case Type::DoubleTyID: opCode = FDIVD; break;
734 default: assert(0 && "Invalid type for DIV instruction"); break;
735 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000736
737 return opCode;
738}
739
740
Vikram S. Adve74825322002-03-18 03:15:35 +0000741// Return NULL if we cannot exploit constant to create a cheaper instruction
742static inline void
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000743CreateDivConstInstruction(TargetMachine &target,
744 const InstructionNode* instrNode,
Vikram S. Adve74825322002-03-18 03:15:35 +0000745 vector<MachineInstr*>& mvec)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000746{
Vikram S. Adve74825322002-03-18 03:15:35 +0000747 MachineInstr* minstr1 = NULL;
748 MachineInstr* minstr2 = NULL;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000749
750 Value* constOp = ((InstrTreeNode*) instrNode->rightChild())->getValue();
Vikram S. Adve74825322002-03-18 03:15:35 +0000751 if (! isa<Constant>(constOp))
752 return;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000753
754 // Cases worth optimizing are:
755 // (1) Divide by 1 for any type: replace with copy (ADD or FMOV)
756 // (2) Divide by 2^x for integer types: replace with SR[L or A]{X}
757 //
758 const Type* resultType = instrNode->getInstruction()->getType();
759
Chris Lattner0c4e8862002-09-03 01:08:28 +0000760 if (resultType->isInteger())
Chris Lattner20b1ea02001-09-14 03:47:57 +0000761 {
762 unsigned pow;
763 bool isValidConst;
764 int64_t C = GetConstantValueAsSignedInt(constOp, isValidConst);
765 if (isValidConst)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000766 {
767 bool needNeg = false;
768 if (C < 0)
769 {
770 needNeg = true;
771 C = -C;
772 }
773
774 if (C == 1)
775 {
Vikram S. Adve74825322002-03-18 03:15:35 +0000776 minstr1 = new MachineInstr(ADD);
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000777 minstr1->SetMachineOperandVal(0,
778 MachineOperand::MO_VirtualRegister,
779 instrNode->leftChild()->getValue());
780 minstr1->SetMachineOperandReg(1,
781 target.getRegInfo().getZeroRegNum());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000782 }
Chris Lattner36346c72002-05-19 21:20:19 +0000783 else if (isPowerOf2(C, pow))
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000784 {
785 MachineOpCode opCode= ((resultType->isSigned())
786 ? (resultType==Type::LongTy)? SRAX : SRA
787 : (resultType==Type::LongTy)? SRLX : SRL);
Vikram S. Adve74825322002-03-18 03:15:35 +0000788 minstr1 = new MachineInstr(opCode);
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000789 minstr1->SetMachineOperandVal(0,
790 MachineOperand::MO_VirtualRegister,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000791 instrNode->leftChild()->getValue());
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000792 minstr1->SetMachineOperandConst(1,
793 MachineOperand::MO_UnextendedImmed,
794 pow);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000795 }
796
Vikram S. Adve74825322002-03-18 03:15:35 +0000797 if (minstr1 && needNeg)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000798 { // insert <reg = SUB 0, reg> after the instr to flip the sign
Vikram S. Adve74825322002-03-18 03:15:35 +0000799 minstr2 = CreateIntNegInstruction(target,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000800 instrNode->getValue());
801 }
802 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000803 }
804 else
805 {
Chris Lattner9b625032002-05-06 16:15:30 +0000806 if (ConstantFP *FPC = dyn_cast<ConstantFP>(constOp))
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000807 {
Chris Lattner9b625032002-05-06 16:15:30 +0000808 double dval = FPC->getValue();
Vikram S. Adve6ad7c552001-11-09 02:18:16 +0000809 if (fabs(dval) == 1)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000810 {
811 bool needNeg = (dval < 0);
812
813 MachineOpCode opCode = needNeg
814 ? (resultType == Type::FloatTy? FNEGS : FNEGD)
815 : (resultType == Type::FloatTy? FMOVS : FMOVD);
816
Vikram S. Adve74825322002-03-18 03:15:35 +0000817 minstr1 = new MachineInstr(opCode);
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000818 minstr1->SetMachineOperandVal(0,
819 MachineOperand::MO_VirtualRegister,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000820 instrNode->leftChild()->getValue());
821 }
822 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000823 }
824
Vikram S. Adve74825322002-03-18 03:15:35 +0000825 if (minstr1 != NULL)
826 minstr1->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister,
827 instrNode->getValue());
Chris Lattner20b1ea02001-09-14 03:47:57 +0000828
Vikram S. Adve74825322002-03-18 03:15:35 +0000829 if (minstr1)
830 mvec.push_back(minstr1);
831 if (minstr2)
832 mvec.push_back(minstr2);
Chris Lattner20b1ea02001-09-14 03:47:57 +0000833}
834
835
Vikram S. Adve74825322002-03-18 03:15:35 +0000836static void
837CreateCodeForVariableSizeAlloca(const TargetMachine& target,
838 Instruction* result,
839 unsigned int tsize,
840 Value* numElementsVal,
841 vector<MachineInstr*>& getMvec)
842{
843 MachineInstr* M;
844
845 // Create a Value to hold the (constant) element size
846 Value* tsizeVal = ConstantSInt::get(Type::IntTy, tsize);
847
848 // Get the constant offset from SP for dynamically allocated storage
849 // and create a temporary Value to hold it.
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000850 assert(result && result->getParent() && "Result value is not part of a fn?");
851 Function *F = result->getParent()->getParent();
852 MachineCodeForMethod& mcInfo = MachineCodeForMethod::get(F);
Vikram S. Adve74825322002-03-18 03:15:35 +0000853 bool growUp;
854 ConstantSInt* dynamicAreaOffset =
855 ConstantSInt::get(Type::IntTy,
856 target.getFrameInfo().getDynamicAreaOffset(mcInfo,growUp));
857 assert(! growUp && "Has SPARC v9 stack frame convention changed?");
858
859 // Create a temporary value to hold the result of MUL
860 TmpInstruction* tmpProd = new TmpInstruction(numElementsVal, tsizeVal);
861 MachineCodeForInstruction::get(result).addTemp(tmpProd);
862
863 // Instruction 1: mul numElements, typeSize -> tmpProd
864 M = new MachineInstr(MULX);
865 M->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister, numElementsVal);
866 M->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister, tsizeVal);
867 M->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister, tmpProd);
868 getMvec.push_back(M);
869
870 // Instruction 2: sub %sp, tmpProd -> %sp
871 M = new MachineInstr(SUB);
872 M->SetMachineOperandReg(0, target.getRegInfo().getStackPointer());
873 M->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister, tmpProd);
874 M->SetMachineOperandReg(2, target.getRegInfo().getStackPointer());
875 getMvec.push_back(M);
876
877 // Instruction 3: add %sp, frameSizeBelowDynamicArea -> result
878 M = new MachineInstr(ADD);
879 M->SetMachineOperandReg(0, target.getRegInfo().getStackPointer());
880 M->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister, dynamicAreaOffset);
881 M->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister, result);
882 getMvec.push_back(M);
883}
884
885
886static void
887CreateCodeForFixedSizeAlloca(const TargetMachine& target,
888 Instruction* result,
889 unsigned int tsize,
890 unsigned int numElements,
891 vector<MachineInstr*>& getMvec)
892{
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000893 assert(result && result->getParent() &&
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000894 "Result value is not part of a function?");
895 Function *F = result->getParent()->getParent();
896 MachineCodeForMethod &mcInfo = MachineCodeForMethod::get(F);
Vikram S. Adve74825322002-03-18 03:15:35 +0000897
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000898 // Check if the offset would small enough to use as an immediate in
899 // load/stores (check LDX because all load/stores have the same-size immediate
900 // field). If not, put the variable in the dynamically sized area of the
901 // frame.
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000902 unsigned int paddedSizeIgnored;
Vikram S. Adve74825322002-03-18 03:15:35 +0000903 int offsetFromFP = mcInfo.computeOffsetforLocalVar(target, result,
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000904 paddedSizeIgnored,
Vikram S. Adve74825322002-03-18 03:15:35 +0000905 tsize * numElements);
906 if (! target.getInstrInfo().constantFitsInImmedField(LDX, offsetFromFP))
907 {
908 CreateCodeForVariableSizeAlloca(target, result, tsize,
909 ConstantSInt::get(Type::IntTy,numElements),
910 getMvec);
911 return;
912 }
913
914 // else offset fits in immediate field so go ahead and allocate it.
915 offsetFromFP = mcInfo.allocateLocalVar(target, result, tsize * numElements);
916
917 // Create a temporary Value to hold the constant offset.
918 // This is needed because it may not fit in the immediate field.
919 ConstantSInt* offsetVal = ConstantSInt::get(Type::IntTy, offsetFromFP);
920
921 // Instruction 1: add %fp, offsetFromFP -> result
922 MachineInstr* M = new MachineInstr(ADD);
923 M->SetMachineOperandReg(0, target.getRegInfo().getFramePointer());
924 M->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister, offsetVal);
925 M->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister, result);
926
927 getMvec.push_back(M);
928}
929
930
Chris Lattner20b1ea02001-09-14 03:47:57 +0000931//------------------------------------------------------------------------
932// Function SetOperandsForMemInstr
933//
934// Choose addressing mode for the given load or store instruction.
935// Use [reg+reg] if it is an indexed reference, and the index offset is
936// not a constant or if it cannot fit in the offset field.
937// Use [reg+offset] in all other cases.
938//
939// This assumes that all array refs are "lowered" to one of these forms:
940// %x = load (subarray*) ptr, constant ; single constant offset
941// %x = load (subarray*) ptr, offsetVal ; single non-constant offset
942// Generally, this should happen via strength reduction + LICM.
943// Also, strength reduction should take care of using the same register for
944// the loop index variable and an array index, when that is profitable.
945//------------------------------------------------------------------------
946
947static void
Vikram S. Adve74825322002-03-18 03:15:35 +0000948SetOperandsForMemInstr(vector<MachineInstr*>& mvec,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000949 const InstructionNode* vmInstrNode,
950 const TargetMachine& target)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000951{
Vikram S. Adve8cfffd32002-08-24 20:56:53 +0000952 Instruction* memInst = vmInstrNode->getInstruction();
953 vector<MachineInstr*>::iterator mvecI = mvec.end() - 1;
954
955 // Index vector, ptr value, and flag if all indices are const.
Vikram S. Advea10d1a72002-03-31 19:07:35 +0000956 vector<Value*> idxVec;
Vikram S. Adve8cfffd32002-08-24 20:56:53 +0000957 bool allConstantIndices;
958 Value* ptrVal = GetMemInstArgs(vmInstrNode, idxVec, allConstantIndices);
Vikram S. Adveed3fefb2002-08-03 13:48:21 +0000959
Vikram S. Adve8cfffd32002-08-24 20:56:53 +0000960 // Now create the appropriate operands for the machine instruction.
961 // First, initialize so we default to storing the offset in a register.
Chris Lattner8e5c0b42001-11-07 14:01:59 +0000962 int64_t smallConstOffset = 0;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000963 Value* valueForRegOffset = NULL;
Vikram S. Adveed3fefb2002-08-03 13:48:21 +0000964 MachineOperand::MachineOperandType offsetOpType =
965 MachineOperand::MO_VirtualRegister;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000966
Vikram S. Adve74825322002-03-18 03:15:35 +0000967 // Check if there is an index vector and if so, compute the
968 // right offset for structures and for arrays
Chris Lattner20b1ea02001-09-14 03:47:57 +0000969 //
Chris Lattner3bb8ad22002-08-22 23:37:24 +0000970 if (!idxVec.empty())
Chris Lattner20b1ea02001-09-14 03:47:57 +0000971 {
Vikram S. Adve74825322002-03-18 03:15:35 +0000972 const PointerType* ptrType = cast<PointerType>(ptrVal->getType());
Chris Lattner20b1ea02001-09-14 03:47:57 +0000973
Vikram S. Adve242a8082002-05-19 15:25:51 +0000974 // If all indices are constant, compute the combined offset directly.
975 if (allConstantIndices)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000976 {
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000977 // Compute the offset value using the index vector. Create a
978 // virtual reg. for it since it may not fit in the immed field.
Vikram S. Adve242a8082002-05-19 15:25:51 +0000979 uint64_t offset = target.DataLayout.getIndexedOffset(ptrType,idxVec);
980 valueForRegOffset = ConstantSInt::get(Type::LongTy, offset);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000981 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000982 else
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000983 {
Vikram S. Adve242a8082002-05-19 15:25:51 +0000984 // There is at least one non-constant offset. Therefore, this must
Vikram S. Adveed3fefb2002-08-03 13:48:21 +0000985 // be an array ref, and must have been lowered to a single non-zero
986 // offset. (An extra leading zero offset, if any, can be ignored.)
987 // Generate code sequence to compute address from index.
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000988 //
Chris Lattner0374b8d2002-09-11 01:21:35 +0000989 bool firstIdxIsZero =
990 (idxVec[0] == Constant::getNullValue(idxVec[0]->getType()));
Vikram S. Advebabc0fa2002-09-05 18:32:13 +0000991 assert(idxVec.size() == 1U + firstIdxIsZero
Vikram S. Adveed3fefb2002-08-03 13:48:21 +0000992 && "Array refs must be lowered before Instruction Selection");
993
Vikram S. Advebabc0fa2002-09-05 18:32:13 +0000994 Value* idxVal = idxVec[firstIdxIsZero];
Vikram S. Adveed3fefb2002-08-03 13:48:21 +0000995
996 vector<MachineInstr*> mulVec;
Vikram S. Adve94c40812002-09-27 14:33:08 +0000997 Instruction* addr = new TmpInstruction(Type::ULongTy, memInst);
Vikram S. Adveed3fefb2002-08-03 13:48:21 +0000998 MachineCodeForInstruction::get(memInst).addTemp(addr);
999
Vikram S. Advebabc0fa2002-09-05 18:32:13 +00001000 // Get the array type indexed by idxVal, and compute its element size.
Vikram S. Adveed3fefb2002-08-03 13:48:21 +00001001 // The call to getTypeSize() will fail if size is not constant.
Vikram S. Advebabc0fa2002-09-05 18:32:13 +00001002 const Type* vecType = (firstIdxIsZero
1003 ? GetElementPtrInst::getIndexedType(ptrType,
1004 std::vector<Value*>(1U, idxVec[0]),
1005 /*AllowCompositeLeaf*/ true)
1006 : ptrType);
1007 const Type* eltType = cast<SequentialType>(vecType)->getElementType();
Vikram S. Advee102a642002-09-16 15:56:45 +00001008 ConstantUInt* eltSizeVal = ConstantUInt::get(Type::ULongTy,
Vikram S. Advebabc0fa2002-09-05 18:32:13 +00001009 target.DataLayout.getTypeSize(eltType));
Vikram S. Adveed3fefb2002-08-03 13:48:21 +00001010
1011 // CreateMulInstruction() folds constants intelligently enough.
1012 CreateMulInstruction(target,
1013 memInst->getParent()->getParent(),
Vikram S. Advebabc0fa2002-09-05 18:32:13 +00001014 idxVal, /* lval, not likely to be const*/
1015 eltSizeVal, /* rval, likely to be constant */
1016 addr, /* result */
Vikram S. Adveed3fefb2002-08-03 13:48:21 +00001017 mulVec,
1018 MachineCodeForInstruction::get(memInst),
1019 INVALID_MACHINE_OPCODE);
1020
1021 // Insert mulVec[] before *mvecI in mvec[] and update mvecI
1022 // to point to the same instruction it pointed to before.
1023 assert(mulVec.size() > 0 && "No multiply code created?");
1024 vector<MachineInstr*>::iterator oldMvecI = mvecI;
1025 for (unsigned i=0, N=mulVec.size(); i < N; ++i)
1026 mvecI = mvec.insert(mvecI, mulVec[i]) + 1; // pts to mem instr
1027
1028 valueForRegOffset = addr;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001029 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00001030 }
1031 else
1032 {
1033 offsetOpType = MachineOperand::MO_SignExtendedImmed;
1034 smallConstOffset = 0;
1035 }
Vikram S. Adveed3fefb2002-08-03 13:48:21 +00001036
Vikram S. Advea10d1a72002-03-31 19:07:35 +00001037 // For STORE:
1038 // Operand 0 is value, operand 1 is ptr, operand 2 is offset
1039 // For LOAD or GET_ELEMENT_PTR,
1040 // Operand 0 is ptr, operand 1 is offset, operand 2 is result.
1041 //
1042 unsigned offsetOpNum, ptrOpNum;
1043 if (memInst->getOpcode() == Instruction::Store)
1044 {
1045 (*mvecI)->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister,
1046 vmInstrNode->leftChild()->getValue());
1047 ptrOpNum = 1;
1048 offsetOpNum = 2;
1049 }
1050 else
1051 {
1052 ptrOpNum = 0;
1053 offsetOpNum = 1;
1054 (*mvecI)->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister,
1055 memInst);
1056 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00001057
Vikram S. Advea10d1a72002-03-31 19:07:35 +00001058 (*mvecI)->SetMachineOperandVal(ptrOpNum, MachineOperand::MO_VirtualRegister,
1059 ptrVal);
1060
Chris Lattner20b1ea02001-09-14 03:47:57 +00001061 if (offsetOpType == MachineOperand::MO_VirtualRegister)
1062 {
1063 assert(valueForRegOffset != NULL);
Vikram S. Adve74825322002-03-18 03:15:35 +00001064 (*mvecI)->SetMachineOperandVal(offsetOpNum, offsetOpType,
1065 valueForRegOffset);
Chris Lattner20b1ea02001-09-14 03:47:57 +00001066 }
1067 else
Vikram S. Adve74825322002-03-18 03:15:35 +00001068 (*mvecI)->SetMachineOperandConst(offsetOpNum, offsetOpType,
1069 smallConstOffset);
Chris Lattner20b1ea02001-09-14 03:47:57 +00001070}
1071
1072
Chris Lattner20b1ea02001-09-14 03:47:57 +00001073//
1074// Substitute operand `operandNum' of the instruction in node `treeNode'
Vikram S. Advec025fc12001-10-14 23:28:43 +00001075// in place of the use(s) of that instruction in node `parent'.
1076// Check both explicit and implicit operands!
Vikram S. Adve74825322002-03-18 03:15:35 +00001077// Also make sure to skip over a parent who:
1078// (1) is a list node in the Burg tree, or
1079// (2) itself had its results forwarded to its parent
Chris Lattner20b1ea02001-09-14 03:47:57 +00001080//
1081static void
1082ForwardOperand(InstructionNode* treeNode,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001083 InstrTreeNode* parent,
1084 int operandNum)
Chris Lattner20b1ea02001-09-14 03:47:57 +00001085{
Vikram S. Adve243dd452001-09-18 13:03:13 +00001086 assert(treeNode && parent && "Invalid invocation of ForwardOperand");
1087
Chris Lattner20b1ea02001-09-14 03:47:57 +00001088 Instruction* unusedOp = treeNode->getInstruction();
1089 Value* fwdOp = unusedOp->getOperand(operandNum);
Vikram S. Adve243dd452001-09-18 13:03:13 +00001090
1091 // The parent itself may be a list node, so find the real parent instruction
1092 while (parent->getNodeType() != InstrTreeNode::NTInstructionNode)
1093 {
1094 parent = parent->parent();
1095 assert(parent && "ERROR: Non-instruction node has no parent in tree.");
1096 }
1097 InstructionNode* parentInstrNode = (InstructionNode*) parent;
1098
1099 Instruction* userInstr = parentInstrNode->getInstruction();
Chris Lattner9c461082002-02-03 07:50:56 +00001100 MachineCodeForInstruction &mvec = MachineCodeForInstruction::get(userInstr);
Vikram S. Adve74825322002-03-18 03:15:35 +00001101
1102 // The parent's mvec would be empty if it was itself forwarded.
1103 // Recursively call ForwardOperand in that case...
1104 //
1105 if (mvec.size() == 0)
Chris Lattner20b1ea02001-09-14 03:47:57 +00001106 {
Vikram S. Adve74825322002-03-18 03:15:35 +00001107 assert(parent->parent() != NULL &&
1108 "Parent could not have been forwarded, yet has no instructions?");
1109 ForwardOperand(treeNode, parent->parent(), operandNum);
1110 }
1111 else
1112 {
Vikram S. Adve74825322002-03-18 03:15:35 +00001113 for (unsigned i=0, N=mvec.size(); i < N; i++)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001114 {
Vikram S. Adve74825322002-03-18 03:15:35 +00001115 MachineInstr* minstr = mvec[i];
1116 for (unsigned i=0, numOps=minstr->getNumOperands(); i < numOps; ++i)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001117 {
Vikram S. Adve74825322002-03-18 03:15:35 +00001118 const MachineOperand& mop = minstr->getOperand(i);
1119 if (mop.getOperandType() == MachineOperand::MO_VirtualRegister &&
1120 mop.getVRegValue() == unusedOp)
Vikram S. Adve242a8082002-05-19 15:25:51 +00001121 minstr->SetMachineOperandVal(i,
Vikram S. Adve74825322002-03-18 03:15:35 +00001122 MachineOperand::MO_VirtualRegister, fwdOp);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001123 }
Vikram S. Adve74825322002-03-18 03:15:35 +00001124
1125 for (unsigned i=0,numOps=minstr->getNumImplicitRefs(); i<numOps; ++i)
1126 if (minstr->getImplicitRef(i) == unusedOp)
Vikram S. Adve242a8082002-05-19 15:25:51 +00001127 minstr->setImplicitRef(i, fwdOp,
Vikram S. Adve6418eac2002-07-08 23:30:14 +00001128 minstr->implicitRefIsDefined(i),
1129 minstr->implicitRefIsDefinedAndUsed(i));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001130 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00001131 }
1132}
1133
1134
Vikram S. Adve242a8082002-05-19 15:25:51 +00001135inline bool
1136AllUsesAreBranches(const Instruction* setccI)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001137{
Vikram S. Adve242a8082002-05-19 15:25:51 +00001138 for (Value::use_const_iterator UI=setccI->use_begin(), UE=setccI->use_end();
1139 UI != UE; ++UI)
1140 if (! isa<TmpInstruction>(*UI) // ignore tmp instructions here
1141 && cast<Instruction>(*UI)->getOpcode() != Instruction::Br)
1142 return false;
1143 return true;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001144}
1145
Vikram S. Advefb361122001-10-22 13:36:31 +00001146//******************* Externally Visible Functions *************************/
1147
Vikram S. Advefb361122001-10-22 13:36:31 +00001148//------------------------------------------------------------------------
1149// External Function: ThisIsAChainRule
1150//
1151// Purpose:
1152// Check if a given BURG rule is a chain rule.
1153//------------------------------------------------------------------------
1154
1155extern bool
1156ThisIsAChainRule(int eruleno)
1157{
1158 switch(eruleno)
1159 {
1160 case 111: // stmt: reg
Vikram S. Advefb361122001-10-22 13:36:31 +00001161 case 123:
1162 case 124:
1163 case 125:
1164 case 126:
1165 case 127:
1166 case 128:
1167 case 129:
1168 case 130:
1169 case 131:
1170 case 132:
1171 case 133:
1172 case 155:
1173 case 221:
1174 case 222:
1175 case 241:
1176 case 242:
1177 case 243:
1178 case 244:
Vikram S. Adve30a6f492002-08-22 02:56:10 +00001179 case 245:
Vikram S. Adve85e1e9c2002-04-01 20:28:48 +00001180 case 321:
Vikram S. Advefb361122001-10-22 13:36:31 +00001181 return true; break;
Vikram S. Adve30a6f492002-08-22 02:56:10 +00001182
Vikram S. Advefb361122001-10-22 13:36:31 +00001183 default:
1184 return false; break;
1185 }
1186}
Chris Lattner20b1ea02001-09-14 03:47:57 +00001187
1188
1189//------------------------------------------------------------------------
1190// External Function: GetInstructionsByRule
1191//
1192// Purpose:
1193// Choose machine instructions for the SPARC according to the
1194// patterns chosen by the BURG-generated parser.
1195//------------------------------------------------------------------------
1196
Vikram S. Adve74825322002-03-18 03:15:35 +00001197void
Chris Lattner20b1ea02001-09-14 03:47:57 +00001198GetInstructionsByRule(InstructionNode* subtreeRoot,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001199 int ruleForNode,
1200 short* nts,
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001201 TargetMachine &target,
Vikram S. Adve74825322002-03-18 03:15:35 +00001202 vector<MachineInstr*>& mvec)
Chris Lattner20b1ea02001-09-14 03:47:57 +00001203{
Chris Lattner20b1ea02001-09-14 03:47:57 +00001204 bool checkCast = false; // initialize here to use fall-through
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001205 bool maskUnsignedResult = false;
Chris Lattner20b1ea02001-09-14 03:47:57 +00001206 int nextRule;
1207 int forwardOperandNum = -1;
Vikram S. Adve74825322002-03-18 03:15:35 +00001208 unsigned int allocaSize = 0;
1209 MachineInstr* M, *M2;
1210 unsigned int L;
1211
1212 mvec.clear();
Chris Lattner20b1ea02001-09-14 03:47:57 +00001213
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001214 // If the code for this instruction was folded into the parent (user),
1215 // then do nothing!
1216 if (subtreeRoot->isFoldedIntoParent())
1217 return;
1218
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001219 //
1220 // Let's check for chain rules outside the switch so that we don't have
1221 // to duplicate the list of chain rule production numbers here again
1222 //
1223 if (ThisIsAChainRule(ruleForNode))
Chris Lattner20b1ea02001-09-14 03:47:57 +00001224 {
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001225 // Chain rules have a single nonterminal on the RHS.
1226 // Get the rule that matches the RHS non-terminal and use that instead.
1227 //
1228 assert(nts[0] && ! nts[1]
1229 && "A chain rule should have only one RHS non-terminal!");
1230 nextRule = burm_rule(subtreeRoot->state, nts[0]);
1231 nts = burm_nts[nextRule];
Vikram S. Adve74825322002-03-18 03:15:35 +00001232 GetInstructionsByRule(subtreeRoot, nextRule, nts, target, mvec);
Chris Lattner20b1ea02001-09-14 03:47:57 +00001233 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001234 else
Chris Lattner20b1ea02001-09-14 03:47:57 +00001235 {
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001236 switch(ruleForNode) {
1237 case 1: // stmt: Ret
1238 case 2: // stmt: RetValue(reg)
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001239 { // NOTE: Prepass of register allocation is responsible
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001240 // for moving return value to appropriate register.
1241 // Mark the return-address register as a hidden virtual reg.
Vikram S. Advea995e602001-10-11 04:23:19 +00001242 // Mark the return value register as an implicit ref of
1243 // the machine instruction.
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001244 // Finally put a NOP in the delay slot.
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001245 ReturnInst *returnInstr =
1246 cast<ReturnInst>(subtreeRoot->getInstruction());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001247 assert(returnInstr->getOpcode() == Instruction::Ret);
1248
Chris Lattner9c461082002-02-03 07:50:56 +00001249 Instruction* returnReg = new TmpInstruction(returnInstr);
1250 MachineCodeForInstruction::get(returnInstr).addTemp(returnReg);
Vikram S. Advefb361122001-10-22 13:36:31 +00001251
Vikram S. Adve74825322002-03-18 03:15:35 +00001252 M = new MachineInstr(JMPLRET);
1253 M->SetMachineOperandReg(0, MachineOperand::MO_VirtualRegister,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001254 returnReg);
Vikram S. Adve74825322002-03-18 03:15:35 +00001255 M->SetMachineOperandConst(1,MachineOperand::MO_SignExtendedImmed,
Chris Lattner697954c2002-01-20 22:54:45 +00001256 (int64_t)8);
Vikram S. Adve74825322002-03-18 03:15:35 +00001257 M->SetMachineOperandReg(2, target.getRegInfo().getZeroRegNum());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001258
Vikram S. Advea995e602001-10-11 04:23:19 +00001259 if (returnInstr->getReturnValue() != NULL)
Vikram S. Adve74825322002-03-18 03:15:35 +00001260 M->addImplicitRef(returnInstr->getReturnValue());
Vikram S. Advea995e602001-10-11 04:23:19 +00001261
Vikram S. Adve74825322002-03-18 03:15:35 +00001262 mvec.push_back(M);
1263 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001264
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001265 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001266 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001267
1268 case 3: // stmt: Store(reg,reg)
1269 case 4: // stmt: Store(reg,ptrreg)
Vikram S. Adve74825322002-03-18 03:15:35 +00001270 mvec.push_back(new MachineInstr(
1271 ChooseStoreInstruction(
1272 subtreeRoot->leftChild()->getValue()->getType())));
Vikram S. Adve8cfffd32002-08-24 20:56:53 +00001273 SetOperandsForMemInstr(mvec, subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001274 break;
Chris Lattner20b1ea02001-09-14 03:47:57 +00001275
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001276 case 5: // stmt: BrUncond
Vikram S. Adve74825322002-03-18 03:15:35 +00001277 M = new MachineInstr(BA);
Vikram S. Adve6418eac2002-07-08 23:30:14 +00001278 M->SetMachineOperandVal(0, MachineOperand::MO_PCRelativeDisp,
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001279 cast<BranchInst>(subtreeRoot->getInstruction())->getSuccessor(0));
Vikram S. Adve74825322002-03-18 03:15:35 +00001280 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001281
1282 // delay slot
Vikram S. Adve74825322002-03-18 03:15:35 +00001283 mvec.push_back(new MachineInstr(NOP));
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 206: // stmt: BrCond(setCCconst)
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001287 { // setCCconst => boolean was computed with `%b = setCC type reg1 const'
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001288 // If the constant is ZERO, we can use the branch-on-integer-register
1289 // instructions and avoid the SUBcc instruction entirely.
1290 // Otherwise this is just the same as case 5, so just fall through.
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001291 //
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001292 InstrTreeNode* constNode = subtreeRoot->leftChild()->rightChild();
1293 assert(constNode &&
1294 constNode->getNodeType() ==InstrTreeNode::NTConstNode);
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001295 Constant *constVal = cast<Constant>(constNode->getValue());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001296 bool isValidConst;
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001297
Chris Lattner0c4e8862002-09-03 01:08:28 +00001298 if ((constVal->getType()->isInteger()
Chris Lattner9b625032002-05-06 16:15:30 +00001299 || isa<PointerType>(constVal->getType()))
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001300 && GetConstantValueAsSignedInt(constVal, isValidConst) == 0
1301 && isValidConst)
1302 {
1303 // That constant is a zero after all...
1304 // Use the left child of setCC as the first argument!
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001305 // Mark the setCC node so that no code is generated for it.
1306 InstructionNode* setCCNode = (InstructionNode*)
1307 subtreeRoot->leftChild();
1308 assert(setCCNode->getOpLabel() == SetCCOp);
1309 setCCNode->markFoldedIntoParent();
1310
1311 BranchInst* brInst=cast<BranchInst>(subtreeRoot->getInstruction());
1312
Vikram S. Adve74825322002-03-18 03:15:35 +00001313 M = new MachineInstr(ChooseBprInstruction(subtreeRoot));
1314 M->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister,
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001315 setCCNode->leftChild()->getValue());
Vikram S. Adve74825322002-03-18 03:15:35 +00001316 M->SetMachineOperandVal(1, MachineOperand::MO_PCRelativeDisp,
1317 brInst->getSuccessor(0));
1318 mvec.push_back(M);
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001319
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001320 // delay slot
Vikram S. Adve74825322002-03-18 03:15:35 +00001321 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001322
1323 // false branch
Vikram S. Adve74825322002-03-18 03:15:35 +00001324 M = new MachineInstr(BA);
Vikram S. Adve6418eac2002-07-08 23:30:14 +00001325 M->SetMachineOperandVal(0, MachineOperand::MO_PCRelativeDisp,
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001326 brInst->getSuccessor(1));
Vikram S. Adve74825322002-03-18 03:15:35 +00001327 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001328
1329 // delay slot
Vikram S. Adve74825322002-03-18 03:15:35 +00001330 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001331
1332 break;
1333 }
1334 // ELSE FALL THROUGH
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001335 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001336
Vikram S. Adve30a6f492002-08-22 02:56:10 +00001337 case 6: // stmt: BrCond(setCC)
1338 { // bool => boolean was computed with SetCC.
1339 // The branch to use depends on whether it is FP, signed, or unsigned.
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001340 // If it is an integer CC, we also need to find the unique
1341 // TmpInstruction representing that CC.
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001342 //
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001343 BranchInst* brInst = cast<BranchInst>(subtreeRoot->getInstruction());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001344 bool isFPBranch;
Vikram S. Adve74825322002-03-18 03:15:35 +00001345 M = new MachineInstr(ChooseBccInstruction(subtreeRoot, isFPBranch));
Vikram S. Adve30a6f492002-08-22 02:56:10 +00001346
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001347 Value* ccValue = GetTmpForCC(subtreeRoot->leftChild()->getValue(),
1348 brInst->getParent()->getParent(),
1349 isFPBranch? Type::FloatTy : Type::IntTy);
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001350
Vikram S. Adve74825322002-03-18 03:15:35 +00001351 M->SetMachineOperandVal(0, MachineOperand::MO_CCRegister, ccValue);
1352 M->SetMachineOperandVal(1, MachineOperand::MO_PCRelativeDisp,
1353 brInst->getSuccessor(0));
1354 mvec.push_back(M);
Vikram S. Adve30a6f492002-08-22 02:56:10 +00001355
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001356 // delay slot
Vikram S. Adve74825322002-03-18 03:15:35 +00001357 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adve30a6f492002-08-22 02:56:10 +00001358
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001359 // false branch
Vikram S. Adve74825322002-03-18 03:15:35 +00001360 M = new MachineInstr(BA);
Vikram S. Adve6418eac2002-07-08 23:30:14 +00001361 M->SetMachineOperandVal(0, MachineOperand::MO_PCRelativeDisp,
Vikram S. Adve74825322002-03-18 03:15:35 +00001362 brInst->getSuccessor(1));
1363 mvec.push_back(M);
Vikram S. Adve30a6f492002-08-22 02:56:10 +00001364
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001365 // delay slot
Vikram S. Adve74825322002-03-18 03:15:35 +00001366 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001367 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001368 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001369
1370 case 208: // stmt: BrCond(boolconst)
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001371 {
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001372 // boolconst => boolean is a constant; use BA to first or second label
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001373 Constant* constVal =
1374 cast<Constant>(subtreeRoot->leftChild()->getValue());
1375 unsigned dest = cast<ConstantBool>(constVal)->getValue()? 0 : 1;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001376
Vikram S. Adve74825322002-03-18 03:15:35 +00001377 M = new MachineInstr(BA);
Vikram S. Adve6418eac2002-07-08 23:30:14 +00001378 M->SetMachineOperandVal(0, MachineOperand::MO_PCRelativeDisp,
Chris Lattner35504202002-04-27 03:14:39 +00001379 cast<BranchInst>(subtreeRoot->getInstruction())->getSuccessor(dest));
Vikram S. Adve74825322002-03-18 03:15:35 +00001380 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001381
1382 // delay slot
Vikram S. Adve74825322002-03-18 03:15:35 +00001383 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001384 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001385 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001386
1387 case 8: // stmt: BrCond(boolreg)
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001388 { // boolreg => boolean is stored in an existing register.
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001389 // Just use the branch-on-integer-register instruction!
1390 //
Vikram S. Adve74825322002-03-18 03:15:35 +00001391 M = new MachineInstr(BRNZ);
1392 M->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001393 subtreeRoot->leftChild()->getValue());
Vikram S. Adve74825322002-03-18 03:15:35 +00001394 M->SetMachineOperandVal(1, MachineOperand::MO_PCRelativeDisp,
Chris Lattner35504202002-04-27 03:14:39 +00001395 cast<BranchInst>(subtreeRoot->getInstruction())->getSuccessor(0));
Vikram S. Adve74825322002-03-18 03:15:35 +00001396 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001397
1398 // delay slot
Vikram S. Adve74825322002-03-18 03:15:35 +00001399 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001400
1401 // false branch
Vikram S. Adve74825322002-03-18 03:15:35 +00001402 M = new MachineInstr(BA);
Vikram S. Adve6418eac2002-07-08 23:30:14 +00001403 M->SetMachineOperandVal(0, MachineOperand::MO_PCRelativeDisp,
Chris Lattner35504202002-04-27 03:14:39 +00001404 cast<BranchInst>(subtreeRoot->getInstruction())->getSuccessor(1));
Vikram S. Adve74825322002-03-18 03:15:35 +00001405 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001406
1407 // delay slot
Vikram S. Adve74825322002-03-18 03:15:35 +00001408 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001409 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001410 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00001411
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001412 case 9: // stmt: Switch(reg)
1413 assert(0 && "*** SWITCH instruction is not implemented yet.");
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001414 break;
Chris Lattner20b1ea02001-09-14 03:47:57 +00001415
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001416 case 10: // reg: VRegList(reg, reg)
1417 assert(0 && "VRegList should never be the topmost non-chain rule");
1418 break;
1419
Vikram S. Advece08e1d2002-08-15 14:17:37 +00001420 case 21: // bool: Not(bool,reg): Both these are implemented as:
1421 case 421: // reg: BNot(reg,reg): reg = reg XOR-NOT 0
1422 { // First find the unary operand. It may be left or right, usually right.
1423 Value* notArg = BinaryOperator::getNotArgument(
1424 cast<BinaryOperator>(subtreeRoot->getInstruction()));
1425 mvec.push_back(Create3OperandInstr_Reg(XNOR, notArg,
1426 target.getRegInfo().getZeroRegNum(),
1427 subtreeRoot->getValue()));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001428 break;
Vikram S. Advece08e1d2002-08-15 14:17:37 +00001429 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001430
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001431 case 22: // reg: ToBoolTy(reg):
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001432 {
1433 const Type* opType = subtreeRoot->leftChild()->getValue()->getType();
Chris Lattner0c4e8862002-09-03 01:08:28 +00001434 assert(opType->isIntegral() || isa<PointerType>(opType));
Vikram S. Adve74825322002-03-18 03:15:35 +00001435 forwardOperandNum = 0; // forward first operand to user
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001436 break;
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001437 }
1438
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001439 case 23: // reg: ToUByteTy(reg)
Vikram S. Adve94c40812002-09-27 14:33:08 +00001440 case 24: // reg: ToSByteTy(reg)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001441 case 25: // reg: ToUShortTy(reg)
Vikram S. Adve94c40812002-09-27 14:33:08 +00001442 case 26: // reg: ToShortTy(reg)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001443 case 27: // reg: ToUIntTy(reg)
Vikram S. Adve94c40812002-09-27 14:33:08 +00001444 case 28: // reg: ToIntTy(reg)
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001445 {
Vikram S. Adve94c40812002-09-27 14:33:08 +00001446 //======================================================================
1447 // Rules for integer conversions:
1448 //
1449 //--------
1450 // From ISO 1998 C++ Standard, Sec. 4.7:
1451 //
1452 // 2. If the destination type is unsigned, the resulting value is
1453 // the least unsigned integer congruent to the source integer
1454 // (modulo 2n where n is the number of bits used to represent the
1455 // unsigned type). [Note: In a two s complement representation,
1456 // this conversion is conceptual and there is no change in the
1457 // bit pattern (if there is no truncation). ]
1458 //
1459 // 3. If the destination type is signed, the value is unchanged if
1460 // it can be represented in the destination type (and bitfield width);
1461 // otherwise, the value is implementation-defined.
1462 //--------
1463 //
1464 // Since we assume 2s complement representations, this implies:
1465 //
1466 // -- if operand is smaller than destination, zero-extend or sign-extend
1467 // according to the signedness of the *operand*: source decides.
1468 // ==> we have to do nothing here!
1469 //
1470 // -- if operand is same size as or larger than destination, and the
1471 // destination is *unsigned*, zero-extend the operand: dest. decides
1472 //
1473 // -- if operand is same size as or larger than destination, and the
1474 // destination is *signed*, the choice is implementation defined:
1475 // we sign-extend the operand: i.e., again dest. decides.
1476 // Note: this matches both Sun's cc and gcc3.2.
1477 //======================================================================
1478
Vikram S. Adve242a8082002-05-19 15:25:51 +00001479 Instruction* destI = subtreeRoot->getInstruction();
1480 Value* opVal = subtreeRoot->leftChild()->getValue();
Vikram S. Adve94c40812002-09-27 14:33:08 +00001481 const Type* opType = opVal->getType();
Chris Lattner0c4e8862002-09-03 01:08:28 +00001482 if (opType->isIntegral() || isa<PointerType>(opType))
Vikram S. Adve1e606692002-07-31 21:01:34 +00001483 {
1484 unsigned opSize = target.DataLayout.getTypeSize(opType);
1485 unsigned destSize = target.DataLayout.getTypeSize(destI->getType());
Vikram S. Adve94c40812002-09-27 14:33:08 +00001486 if (opSize >= destSize)
1487 { // Operand is same size as or larger than dest:
1488 // zero- or sign-extend, according to the signeddness of
1489 // the destination (see above).
1490 if (destI->getType()->isSigned())
1491 target.getInstrInfo().CreateSignExtensionInstructions(target,
1492 destI->getParent()->getParent(), opVal, destI, 8*destSize,
1493 mvec, MachineCodeForInstruction::get(destI));
1494 else
1495 target.getInstrInfo().CreateZeroExtensionInstructions(target,
1496 destI->getParent()->getParent(), opVal, destI, 8*destSize,
1497 mvec, MachineCodeForInstruction::get(destI));
Vikram S. Adve1e606692002-07-31 21:01:34 +00001498 }
1499 else
1500 forwardOperandNum = 0; // forward first operand to user
Vikram S. Adve242a8082002-05-19 15:25:51 +00001501 }
Vikram S. Adve1e606692002-07-31 21:01:34 +00001502 else if (opType->isFloatingPoint())
Vikram S. Adve8cfffd32002-08-24 20:56:53 +00001503 {
1504 CreateCodeToConvertFloatToInt(target, opVal, destI, mvec,
1505 MachineCodeForInstruction::get(destI));
Vikram S. Adve94c40812002-09-27 14:33:08 +00001506 if (destI->getType()->isUnsigned())
1507 maskUnsignedResult = true; // not handled by fp->int code
Vikram S. Adve8cfffd32002-08-24 20:56:53 +00001508 }
Vikram S. Adve242a8082002-05-19 15:25:51 +00001509 else
Vikram S. Adve1e606692002-07-31 21:01:34 +00001510 assert(0 && "Unrecognized operand type for convert-to-unsigned");
1511
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001512 break;
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001513 }
Vikram S. Adve94c40812002-09-27 14:33:08 +00001514
1515 case 29: // reg: ToULongTy(reg)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001516 case 30: // reg: ToLongTy(reg)
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001517 {
Vikram S. Adve242a8082002-05-19 15:25:51 +00001518 Value* opVal = subtreeRoot->leftChild()->getValue();
Vikram S. Adve242a8082002-05-19 15:25:51 +00001519 const Type* opType = opVal->getType();
Chris Lattner0c4e8862002-09-03 01:08:28 +00001520 if (opType->isIntegral() || isa<PointerType>(opType))
Vikram S. Adve94c40812002-09-27 14:33:08 +00001521 forwardOperandNum = 0; // forward first operand to user
Vikram S. Adve1e606692002-07-31 21:01:34 +00001522 else if (opType->isFloatingPoint())
Vikram S. Adve94c40812002-09-27 14:33:08 +00001523 {
1524 Instruction* destI = subtreeRoot->getInstruction();
1525 CreateCodeToConvertFloatToInt(target, opVal, destI, mvec,
1526 MachineCodeForInstruction::get(destI));
1527 }
Vikram S. Adve1e606692002-07-31 21:01:34 +00001528 else
1529 assert(0 && "Unrecognized operand type for convert-to-signed");
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001530 break;
Vikram S. Adve94c40812002-09-27 14:33:08 +00001531 }
1532
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001533 case 31: // reg: ToFloatTy(reg):
1534 case 32: // reg: ToDoubleTy(reg):
1535 case 232: // reg: ToDoubleTy(Constant):
Vikram S. Adveec7f4822002-09-09 14:54:21 +00001536
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001537 // If this instruction has a parent (a user) in the tree
1538 // and the user is translated as an FsMULd instruction,
1539 // then the cast is unnecessary. So check that first.
1540 // In the future, we'll want to do the same for the FdMULq instruction,
1541 // so do the check here instead of only for ToFloatTy(reg).
1542 //
Vikram S. Adveec7f4822002-09-09 14:54:21 +00001543 if (subtreeRoot->parent() != NULL)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001544 {
Vikram S. Adveec7f4822002-09-09 14:54:21 +00001545 const MachineCodeForInstruction& mcfi =
1546 MachineCodeForInstruction::get(
1547 cast<InstructionNode>(subtreeRoot->parent())->getInstruction());
1548 if (mcfi.size() == 0 || mcfi.front()->getOpCode() == FSMULD)
1549 forwardOperandNum = 0; // forward first operand to user
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001550 }
Vikram S. Adveec7f4822002-09-09 14:54:21 +00001551
1552 if (forwardOperandNum != 0) // we do need the cast
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001553 {
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001554 Value* leftVal = subtreeRoot->leftChild()->getValue();
1555 const Type* opType = leftVal->getType();
Vikram S. Advedbc4fad2002-04-25 04:37:51 +00001556 MachineOpCode opCode=ChooseConvertToFloatInstr(
1557 subtreeRoot->getOpLabel(), opType);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001558 if (opCode == INVALID_OPCODE) // no conversion needed
1559 {
Vikram S. Adve74825322002-03-18 03:15:35 +00001560 forwardOperandNum = 0; // forward first operand to user
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001561 }
1562 else
1563 {
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001564 // If the source operand is a non-FP type it must be
1565 // first copied from int to float register via memory!
1566 Instruction *dest = subtreeRoot->getInstruction();
1567 Value* srcForCast;
1568 int n = 0;
Vikram S. Adve242a8082002-05-19 15:25:51 +00001569 if (! opType->isFloatingPoint())
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001570 {
1571 // Create a temporary to represent the FP register
1572 // into which the integer will be copied via memory.
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001573 // The type of this temporary will determine the FP
1574 // register used: single-prec for a 32-bit int or smaller,
1575 // double-prec for a 64-bit int.
1576 //
Vikram S. Advebabc0fa2002-09-05 18:32:13 +00001577 uint64_t srcSize =
1578 target.DataLayout.getTypeSize(leftVal->getType());
1579 Type* tmpTypeToUse =
1580 (srcSize <= 4)? Type::FloatTy : Type::DoubleTy;
1581 srcForCast = new TmpInstruction(tmpTypeToUse, dest);
Vikram S. Advedbc4fad2002-04-25 04:37:51 +00001582 MachineCodeForInstruction &destMCFI =
Chris Lattner9c461082002-02-03 07:50:56 +00001583 MachineCodeForInstruction::get(dest);
Vikram S. Advedbc4fad2002-04-25 04:37:51 +00001584 destMCFI.addTemp(srcForCast);
Vikram S. Advebabc0fa2002-09-05 18:32:13 +00001585
Vikram S. Adve242a8082002-05-19 15:25:51 +00001586 target.getInstrInfo().CreateCodeToCopyIntToFloat(target,
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001587 dest->getParent()->getParent(),
Vikram S. Advebabc0fa2002-09-05 18:32:13 +00001588 leftVal, cast<Instruction>(srcForCast),
Vikram S. Adve242a8082002-05-19 15:25:51 +00001589 mvec, destMCFI);
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001590 }
1591 else
1592 srcForCast = leftVal;
Vikram S. Adve94c40812002-09-27 14:33:08 +00001593
Vikram S. Adve74825322002-03-18 03:15:35 +00001594 M = new MachineInstr(opCode);
1595 M->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister,
1596 srcForCast);
1597 M->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister,
1598 dest);
1599 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001600 }
1601 }
1602 break;
1603
1604 case 19: // reg: ToArrayTy(reg):
1605 case 20: // reg: ToPointerTy(reg):
Vikram S. Adve74825322002-03-18 03:15:35 +00001606 forwardOperandNum = 0; // forward first operand to user
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001607 break;
1608
1609 case 233: // reg: Add(reg, Constant)
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001610 maskUnsignedResult = true;
Vikram S. Adve74825322002-03-18 03:15:35 +00001611 M = CreateAddConstInstruction(subtreeRoot);
1612 if (M != NULL)
1613 {
1614 mvec.push_back(M);
1615 break;
1616 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001617 // ELSE FALL THROUGH
Vikram S. Adve74825322002-03-18 03:15:35 +00001618
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001619 case 33: // reg: Add(reg, reg)
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001620 maskUnsignedResult = true;
Vikram S. Adve74825322002-03-18 03:15:35 +00001621 mvec.push_back(new MachineInstr(ChooseAddInstruction(subtreeRoot)));
1622 Set3OperandsFromInstr(mvec.back(), subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001623 break;
1624
1625 case 234: // reg: Sub(reg, Constant)
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001626 maskUnsignedResult = true;
Vikram S. Adve74825322002-03-18 03:15:35 +00001627 M = CreateSubConstInstruction(subtreeRoot);
1628 if (M != NULL)
1629 {
1630 mvec.push_back(M);
1631 break;
1632 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001633 // ELSE FALL THROUGH
Vikram S. Adve74825322002-03-18 03:15:35 +00001634
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001635 case 34: // reg: Sub(reg, reg)
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001636 maskUnsignedResult = true;
Vikram S. Adve74825322002-03-18 03:15:35 +00001637 mvec.push_back(new MachineInstr(ChooseSubInstructionByType(
1638 subtreeRoot->getInstruction()->getType())));
1639 Set3OperandsFromInstr(mvec.back(), subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001640 break;
1641
1642 case 135: // reg: Mul(todouble, todouble)
1643 checkCast = true;
1644 // FALL THROUGH
1645
1646 case 35: // reg: Mul(reg, reg)
Vikram S. Adve74825322002-03-18 03:15:35 +00001647 {
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001648 maskUnsignedResult = true;
Vikram S. Adve74825322002-03-18 03:15:35 +00001649 MachineOpCode forceOp = ((checkCast && BothFloatToDouble(subtreeRoot))
1650 ? FSMULD
1651 : INVALID_MACHINE_OPCODE);
Vikram S. Adve242a8082002-05-19 15:25:51 +00001652 Instruction* mulInstr = subtreeRoot->getInstruction();
1653 CreateMulInstruction(target, mulInstr->getParent()->getParent(),
Vikram S. Adve74825322002-03-18 03:15:35 +00001654 subtreeRoot->leftChild()->getValue(),
1655 subtreeRoot->rightChild()->getValue(),
Vikram S. Adve242a8082002-05-19 15:25:51 +00001656 mulInstr, mvec,
1657 MachineCodeForInstruction::get(mulInstr),forceOp);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001658 break;
Vikram S. Adve74825322002-03-18 03:15:35 +00001659 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001660 case 335: // reg: Mul(todouble, todoubleConst)
1661 checkCast = true;
1662 // FALL THROUGH
1663
1664 case 235: // reg: Mul(reg, Constant)
Vikram S. Adve74825322002-03-18 03:15:35 +00001665 {
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001666 maskUnsignedResult = true;
Vikram S. Adve74825322002-03-18 03:15:35 +00001667 MachineOpCode forceOp = ((checkCast && BothFloatToDouble(subtreeRoot))
1668 ? FSMULD
1669 : INVALID_MACHINE_OPCODE);
Vikram S. Adve242a8082002-05-19 15:25:51 +00001670 Instruction* mulInstr = subtreeRoot->getInstruction();
1671 CreateMulInstruction(target, mulInstr->getParent()->getParent(),
Vikram S. Adve74825322002-03-18 03:15:35 +00001672 subtreeRoot->leftChild()->getValue(),
1673 subtreeRoot->rightChild()->getValue(),
Vikram S. Adve242a8082002-05-19 15:25:51 +00001674 mulInstr, mvec,
1675 MachineCodeForInstruction::get(mulInstr),
1676 forceOp);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001677 break;
Vikram S. Adve74825322002-03-18 03:15:35 +00001678 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001679 case 236: // reg: Div(reg, Constant)
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001680 maskUnsignedResult = true;
Vikram S. Adve74825322002-03-18 03:15:35 +00001681 L = mvec.size();
1682 CreateDivConstInstruction(target, subtreeRoot, mvec);
1683 if (mvec.size() > L)
1684 break;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001685 // ELSE FALL THROUGH
Vikram S. Adve74825322002-03-18 03:15:35 +00001686
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001687 case 36: // reg: Div(reg, reg)
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001688 maskUnsignedResult = true;
Vikram S. Adve74825322002-03-18 03:15:35 +00001689 mvec.push_back(new MachineInstr(ChooseDivInstruction(target, subtreeRoot)));
1690 Set3OperandsFromInstr(mvec.back(), subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001691 break;
1692
1693 case 37: // reg: Rem(reg, reg)
1694 case 237: // reg: Rem(reg, Constant)
Vikram S. Adve510eec72001-11-04 21:59:14 +00001695 {
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001696 maskUnsignedResult = true;
Vikram S. Adve510eec72001-11-04 21:59:14 +00001697 Instruction* remInstr = subtreeRoot->getInstruction();
1698
Chris Lattner9c461082002-02-03 07:50:56 +00001699 TmpInstruction* quot = new TmpInstruction(
Vikram S. Adve510eec72001-11-04 21:59:14 +00001700 subtreeRoot->leftChild()->getValue(),
1701 subtreeRoot->rightChild()->getValue());
Chris Lattner9c461082002-02-03 07:50:56 +00001702 TmpInstruction* prod = new TmpInstruction(
Vikram S. Adve510eec72001-11-04 21:59:14 +00001703 quot,
1704 subtreeRoot->rightChild()->getValue());
Chris Lattner9c461082002-02-03 07:50:56 +00001705 MachineCodeForInstruction::get(remInstr).addTemp(quot).addTemp(prod);
Vikram S. Adve510eec72001-11-04 21:59:14 +00001706
Vikram S. Adve74825322002-03-18 03:15:35 +00001707 M = new MachineInstr(ChooseDivInstruction(target, subtreeRoot));
1708 Set3OperandsFromInstr(M, subtreeRoot, target);
1709 M->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister,quot);
1710 mvec.push_back(M);
Vikram S. Adve510eec72001-11-04 21:59:14 +00001711
Vikram S. Advece08e1d2002-08-15 14:17:37 +00001712 M = Create3OperandInstr(ChooseMulInstructionByType(
1713 subtreeRoot->getInstruction()->getType()),
1714 quot, subtreeRoot->rightChild()->getValue(),
1715 prod);
Vikram S. Adve74825322002-03-18 03:15:35 +00001716 mvec.push_back(M);
Vikram S. Adve510eec72001-11-04 21:59:14 +00001717
Vikram S. Adve74825322002-03-18 03:15:35 +00001718 M = new MachineInstr(ChooseSubInstructionByType(
Vikram S. Adve510eec72001-11-04 21:59:14 +00001719 subtreeRoot->getInstruction()->getType()));
Vikram S. Adve74825322002-03-18 03:15:35 +00001720 Set3OperandsFromInstr(M, subtreeRoot, target);
1721 M->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister,prod);
1722 mvec.push_back(M);
Vikram S. Adve510eec72001-11-04 21:59:14 +00001723
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001724 break;
Vikram S. Adve510eec72001-11-04 21:59:14 +00001725 }
1726
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001727 case 38: // bool: And(bool, bool)
1728 case 238: // bool: And(bool, boolconst)
1729 case 338: // reg : BAnd(reg, reg)
1730 case 538: // reg : BAnd(reg, Constant)
Vikram S. Adve74825322002-03-18 03:15:35 +00001731 mvec.push_back(new MachineInstr(AND));
1732 Set3OperandsFromInstr(mvec.back(), subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001733 break;
1734
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001735 case 138: // bool: And(bool, not)
Vikram S. Advece08e1d2002-08-15 14:17:37 +00001736 case 438: // bool: BAnd(bool, bnot)
1737 { // Use the argument of NOT as the second argument!
1738 // Mark the NOT node so that no code is generated for it.
1739 InstructionNode* notNode = (InstructionNode*) subtreeRoot->rightChild();
1740 Value* notArg = BinaryOperator::getNotArgument(
1741 cast<BinaryOperator>(notNode->getInstruction()));
1742 notNode->markFoldedIntoParent();
1743 mvec.push_back(Create3OperandInstr(ANDN,
1744 subtreeRoot->leftChild()->getValue(),
1745 notArg, subtreeRoot->getValue()));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001746 break;
Vikram S. Advece08e1d2002-08-15 14:17:37 +00001747 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001748
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001749 case 39: // bool: Or(bool, bool)
1750 case 239: // bool: Or(bool, boolconst)
1751 case 339: // reg : BOr(reg, reg)
1752 case 539: // reg : BOr(reg, Constant)
Vikram S. Adve242a8082002-05-19 15:25:51 +00001753 mvec.push_back(new MachineInstr(OR));
Vikram S. Adve74825322002-03-18 03:15:35 +00001754 Set3OperandsFromInstr(mvec.back(), subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001755 break;
1756
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001757 case 139: // bool: Or(bool, not)
Vikram S. Advece08e1d2002-08-15 14:17:37 +00001758 case 439: // bool: BOr(bool, bnot)
1759 { // Use the argument of NOT as the second argument!
1760 // Mark the NOT node so that no code is generated for it.
1761 InstructionNode* notNode = (InstructionNode*) subtreeRoot->rightChild();
1762 Value* notArg = BinaryOperator::getNotArgument(
1763 cast<BinaryOperator>(notNode->getInstruction()));
1764 notNode->markFoldedIntoParent();
1765 mvec.push_back(Create3OperandInstr(ORN,
1766 subtreeRoot->leftChild()->getValue(),
1767 notArg, subtreeRoot->getValue()));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001768 break;
Vikram S. Advece08e1d2002-08-15 14:17:37 +00001769 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001770
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001771 case 40: // bool: Xor(bool, bool)
1772 case 240: // bool: Xor(bool, boolconst)
1773 case 340: // reg : BXor(reg, reg)
1774 case 540: // reg : BXor(reg, Constant)
Vikram S. Adve74825322002-03-18 03:15:35 +00001775 mvec.push_back(new MachineInstr(XOR));
1776 Set3OperandsFromInstr(mvec.back(), subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001777 break;
1778
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001779 case 140: // bool: Xor(bool, not)
Vikram S. Advece08e1d2002-08-15 14:17:37 +00001780 case 440: // bool: BXor(bool, bnot)
1781 { // Use the argument of NOT as the second argument!
1782 // Mark the NOT node so that no code is generated for it.
1783 InstructionNode* notNode = (InstructionNode*) subtreeRoot->rightChild();
1784 Value* notArg = BinaryOperator::getNotArgument(
1785 cast<BinaryOperator>(notNode->getInstruction()));
1786 notNode->markFoldedIntoParent();
1787 mvec.push_back(Create3OperandInstr(XNOR,
1788 subtreeRoot->leftChild()->getValue(),
1789 notArg, subtreeRoot->getValue()));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001790 break;
Vikram S. Advece08e1d2002-08-15 14:17:37 +00001791 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001792
1793 case 41: // boolconst: SetCC(reg, Constant)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001794 //
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001795 // If the SetCC was folded into the user (parent), it will be
1796 // caught above. All other cases are the same as case 42,
1797 // so just fall through.
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001798 //
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001799 case 42: // bool: SetCC(reg, reg):
1800 {
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001801 // This generates a SUBCC instruction, putting the difference in
1802 // a result register, and setting a condition code.
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001803 //
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001804 // If the boolean result of the SetCC is used by anything other
Vikram S. Adve6418eac2002-07-08 23:30:14 +00001805 // than a branch instruction, or if it is used outside the current
1806 // basic block, the boolean must be
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001807 // computed and stored in the result register. Otherwise, discard
1808 // the difference (by using %g0) and keep only the condition code.
1809 //
1810 // To compute the boolean result in a register we use a conditional
1811 // move, unless the result of the SUBCC instruction can be used as
1812 // the bool! This assumes that zero is FALSE and any non-zero
1813 // integer is TRUE.
1814 //
1815 InstructionNode* parentNode = (InstructionNode*) subtreeRoot->parent();
1816 Instruction* setCCInstr = subtreeRoot->getInstruction();
Vikram S. Adve242a8082002-05-19 15:25:51 +00001817
Vikram S. Adve6418eac2002-07-08 23:30:14 +00001818 bool keepBoolVal = parentNode == NULL ||
1819 ! AllUsesAreBranches(setCCInstr);
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001820 bool subValIsBoolVal = setCCInstr->getOpcode() == Instruction::SetNE;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001821 bool keepSubVal = keepBoolVal && subValIsBoolVal;
1822 bool computeBoolVal = keepBoolVal && ! subValIsBoolVal;
1823
1824 bool mustClearReg;
1825 int valueToMove;
Chris Lattner8e5c0b42001-11-07 14:01:59 +00001826 MachineOpCode movOpCode = 0;
Vikram S. Adve6418eac2002-07-08 23:30:14 +00001827
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001828 // Mark the 4th operand as being a CC register, and as a def
1829 // A TmpInstruction is created to represent the CC "result".
1830 // Unlike other instances of TmpInstruction, this one is used
1831 // by machine code of multiple LLVM instructions, viz.,
1832 // the SetCC and the branch. Make sure to get the same one!
1833 // Note that we do this even for FP CC registers even though they
1834 // are explicit operands, because the type of the operand
1835 // needs to be a floating point condition code, not an integer
1836 // condition code. Think of this as casting the bool result to
1837 // a FP condition code register.
1838 //
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001839 Value* leftVal = subtreeRoot->leftChild()->getValue();
Chris Lattner9b625032002-05-06 16:15:30 +00001840 bool isFPCompare = leftVal->getType()->isFloatingPoint();
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001841
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001842 TmpInstruction* tmpForCC = GetTmpForCC(setCCInstr,
1843 setCCInstr->getParent()->getParent(),
Chris Lattner9b625032002-05-06 16:15:30 +00001844 isFPCompare ? Type::FloatTy : Type::IntTy);
Chris Lattner9c461082002-02-03 07:50:56 +00001845 MachineCodeForInstruction::get(setCCInstr).addTemp(tmpForCC);
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001846
1847 if (! isFPCompare)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001848 {
1849 // Integer condition: dest. should be %g0 or an integer register.
1850 // If result must be saved but condition is not SetEQ then we need
1851 // a separate instruction to compute the bool result, so discard
1852 // result of SUBcc instruction anyway.
1853 //
Vikram S. Adve74825322002-03-18 03:15:35 +00001854 M = new MachineInstr(SUBcc);
1855 Set3OperandsFromInstr(M, subtreeRoot, target, ! keepSubVal);
1856 M->SetMachineOperandVal(3, MachineOperand::MO_CCRegister,
1857 tmpForCC, /*def*/true);
1858 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001859
1860 if (computeBoolVal)
1861 { // recompute bool using the integer condition codes
1862 movOpCode =
1863 ChooseMovpccAfterSub(subtreeRoot,mustClearReg,valueToMove);
1864 }
1865 }
1866 else
1867 {
1868 // FP condition: dest of FCMP should be some FCCn register
Vikram S. Adve74825322002-03-18 03:15:35 +00001869 M = new MachineInstr(ChooseFcmpInstruction(subtreeRoot));
1870 M->SetMachineOperandVal(0, MachineOperand::MO_CCRegister,
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001871 tmpForCC);
Vikram S. Adve74825322002-03-18 03:15:35 +00001872 M->SetMachineOperandVal(1,MachineOperand::MO_VirtualRegister,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001873 subtreeRoot->leftChild()->getValue());
Vikram S. Adve74825322002-03-18 03:15:35 +00001874 M->SetMachineOperandVal(2,MachineOperand::MO_VirtualRegister,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001875 subtreeRoot->rightChild()->getValue());
Vikram S. Adve74825322002-03-18 03:15:35 +00001876 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001877
1878 if (computeBoolVal)
1879 {// recompute bool using the FP condition codes
1880 mustClearReg = true;
1881 valueToMove = 1;
1882 movOpCode = ChooseMovFpccInstruction(subtreeRoot);
1883 }
1884 }
1885
1886 if (computeBoolVal)
1887 {
1888 if (mustClearReg)
1889 {// Unconditionally set register to 0
Vikram S. Adve74825322002-03-18 03:15:35 +00001890 M = new MachineInstr(SETHI);
1891 M->SetMachineOperandConst(0,MachineOperand::MO_UnextendedImmed,
1892 (int64_t)0);
1893 M->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister,
1894 setCCInstr);
1895 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001896 }
1897
1898 // Now conditionally move `valueToMove' (0 or 1) into the register
Vikram S. Adve6418eac2002-07-08 23:30:14 +00001899 // Mark the register as a use (as well as a def) because the old
1900 // value should be retained if the condition is false.
Vikram S. Adve74825322002-03-18 03:15:35 +00001901 M = new MachineInstr(movOpCode);
1902 M->SetMachineOperandVal(0, MachineOperand::MO_CCRegister,
1903 tmpForCC);
1904 M->SetMachineOperandConst(1, MachineOperand::MO_UnextendedImmed,
1905 valueToMove);
1906 M->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister,
Vikram S. Adve6418eac2002-07-08 23:30:14 +00001907 setCCInstr, /*isDef*/ true,
1908 /*isDefAndUse*/ true);
Vikram S. Adve74825322002-03-18 03:15:35 +00001909 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001910 }
1911 break;
1912 }
1913
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001914 case 51: // reg: Load(reg)
1915 case 52: // reg: Load(ptrreg)
Vikram S. Adve74825322002-03-18 03:15:35 +00001916 mvec.push_back(new MachineInstr(ChooseLoadInstruction(
1917 subtreeRoot->getValue()->getType())));
Vikram S. Adve8cfffd32002-08-24 20:56:53 +00001918 SetOperandsForMemInstr(mvec, subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001919 break;
1920
1921 case 55: // reg: GetElemPtr(reg)
1922 case 56: // reg: GetElemPtrIdx(reg,reg)
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001923 // If the GetElemPtr was folded into the user (parent), it will be
1924 // caught above. For other cases, we have to compute the address.
Vikram S. Adve74825322002-03-18 03:15:35 +00001925 mvec.push_back(new MachineInstr(ADD));
Vikram S. Adve8cfffd32002-08-24 20:56:53 +00001926 SetOperandsForMemInstr(mvec, subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001927 break;
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001928
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001929 case 57: // reg: Alloca: Implement as 1 instruction:
1930 { // add %fp, offsetFromFP -> result
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001931 AllocationInst* instr =
1932 cast<AllocationInst>(subtreeRoot->getInstruction());
1933 unsigned int tsize =
1934 target.findOptimalStorageSize(instr->getAllocatedType());
Vikram S. Adve74825322002-03-18 03:15:35 +00001935 assert(tsize != 0);
1936 CreateCodeForFixedSizeAlloca(target, instr, tsize, 1, mvec);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001937 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001938 }
Vikram S. Adve74825322002-03-18 03:15:35 +00001939
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001940 case 58: // reg: Alloca(reg): Implement as 3 instructions:
1941 // mul num, typeSz -> tmp
1942 // sub %sp, tmp -> %sp
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001943 { // add %sp, frameSizeBelowDynamicArea -> result
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001944 AllocationInst* instr =
1945 cast<AllocationInst>(subtreeRoot->getInstruction());
Vikram S. Adve74825322002-03-18 03:15:35 +00001946 const Type* eltType = instr->getAllocatedType();
1947
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001948 // If #elements is constant, use simpler code for fixed-size allocas
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001949 int tsize = (int) target.findOptimalStorageSize(eltType);
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001950 Value* numElementsVal = NULL;
1951 bool isArray = instr->isArrayAllocation();
1952
1953 if (!isArray ||
1954 isa<Constant>(numElementsVal = instr->getArraySize()))
1955 { // total size is constant: generate code for fixed-size alloca
1956 unsigned int numElements = isArray?
1957 cast<ConstantUInt>(numElementsVal)->getValue() : 1;
1958 CreateCodeForFixedSizeAlloca(target, instr, tsize,
1959 numElements, mvec);
1960 }
Vikram S. Adve74825322002-03-18 03:15:35 +00001961 else // total size is not constant.
1962 CreateCodeForVariableSizeAlloca(target, instr, tsize,
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001963 numElementsVal, mvec);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001964 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001965 }
Vikram S. Adve74825322002-03-18 03:15:35 +00001966
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001967 case 61: // reg: Call
Vikram S. Adve4a8bb2b2002-09-28 16:55:41 +00001968 { // Generate a direct (CALL) or indirect (JMPL) call.
1969 // Mark the return-address register, the indirection
1970 // register (for indirect calls), the operands of the Call,
1971 // and the return value (if any) as implicit operands
1972 // of the machine instruction.
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001973 //
Vikram S. Advedbc4fad2002-04-25 04:37:51 +00001974 // If this is a varargs function, floating point arguments
1975 // have to passed in integer registers so insert
1976 // copy-float-to-int instructions for each float operand.
1977 //
Chris Lattnerb00c5822001-10-02 03:41:24 +00001978 CallInst *callInstr = cast<CallInst>(subtreeRoot->getInstruction());
Chris Lattner749655f2001-10-13 06:54:30 +00001979 Value *callee = callInstr->getCalledValue();
Vikram S. Adve4a8bb2b2002-09-28 16:55:41 +00001980
1981 // Create hidden virtual register for return address with type void*
Vikram S. Adve242a8082002-05-19 15:25:51 +00001982 TmpInstruction* retAddrReg =
Vikram S. Advea10d1a72002-03-31 19:07:35 +00001983 new TmpInstruction(PointerType::get(Type::VoidTy), callInstr);
Chris Lattner9c461082002-02-03 07:50:56 +00001984 MachineCodeForInstruction::get(callInstr).addTemp(retAddrReg);
Vikram S. Adve4a8bb2b2002-09-28 16:55:41 +00001985
Vikram S. Adveea21a6c2001-10-20 20:57:06 +00001986 // Generate the machine instruction and its operands.
1987 // Use CALL for direct function calls; this optimistically assumes
1988 // the PC-relative address fits in the CALL address field (22 bits).
1989 // Use JMPL for indirect calls.
1990 //
Vikram S. Adve4a8bb2b2002-09-28 16:55:41 +00001991 if (isa<Function>(callee)) // direct function call
1992 M = Create1OperandInstr_Addr(CALL, callee);
1993 else // indirect function call
1994 M = Create3OperandInstr_SImmed(JMPLCALL, callee,
1995 (int64_t) 0, retAddrReg);
Vikram S. Adve74825322002-03-18 03:15:35 +00001996 mvec.push_back(M);
Vikram S. Advea10d1a72002-03-31 19:07:35 +00001997
Vikram S. Adve242a8082002-05-19 15:25:51 +00001998 const FunctionType* funcType =
1999 cast<FunctionType>(cast<PointerType>(callee->getType())
2000 ->getElementType());
2001 bool isVarArgs = funcType->isVarArg();
2002 bool noPrototype = isVarArgs && funcType->getNumParams() == 0;
Vikram S. Advedbc4fad2002-04-25 04:37:51 +00002003
Vikram S. Adve242a8082002-05-19 15:25:51 +00002004 // Use an annotation to pass information about call arguments
2005 // to the register allocator.
2006 CallArgsDescriptor* argDesc = new CallArgsDescriptor(callInstr,
2007 retAddrReg, isVarArgs, noPrototype);
2008 M->addAnnotation(argDesc);
Vikram S. Advea995e602001-10-11 04:23:19 +00002009
Vikram S. Adve242a8082002-05-19 15:25:51 +00002010 assert(callInstr->getOperand(0) == callee
2011 && "This is assumed in the loop below!");
2012
2013 for (unsigned i=1, N=callInstr->getNumOperands(); i < N; ++i)
2014 {
2015 Value* argVal = callInstr->getOperand(i);
2016 Instruction* intArgReg = NULL;
2017
2018 // Check for FP arguments to varargs functions.
2019 // Any such argument in the first $K$ args must be passed in an
2020 // integer register, where K = #integer argument registers.
2021 if (isVarArgs && argVal->getType()->isFloatingPoint())
2022 {
2023 // If it is a function with no prototype, pass value
2024 // as an FP value as well as a varargs value
2025 if (noPrototype)
2026 argDesc->getArgInfo(i-1).setUseFPArgReg();
2027
2028 // If this arg. is in the first $K$ regs, add a copy
2029 // float-to-int instruction to pass the value as an integer.
2030 if (i < target.getRegInfo().GetNumOfIntArgRegs())
2031 {
2032 MachineCodeForInstruction &destMCFI =
2033 MachineCodeForInstruction::get(callInstr);
2034 intArgReg = new TmpInstruction(Type::IntTy, argVal);
2035 destMCFI.addTemp(intArgReg);
2036
2037 vector<MachineInstr*> copyMvec;
2038 target.getInstrInfo().CreateCodeToCopyFloatToInt(target,
2039 callInstr->getParent()->getParent(),
2040 argVal, (TmpInstruction*) intArgReg,
2041 copyMvec, destMCFI);
2042 mvec.insert(mvec.begin(),copyMvec.begin(),copyMvec.end());
2043
2044 argDesc->getArgInfo(i-1).setUseIntArgReg();
2045 argDesc->getArgInfo(i-1).setArgCopy(intArgReg);
2046 }
2047 else
2048 // Cannot fit in first $K$ regs so pass the arg on the stack
2049 argDesc->getArgInfo(i-1).setUseStackSlot();
2050 }
2051
2052 if (intArgReg)
2053 mvec.back()->addImplicitRef(intArgReg);
2054
2055 mvec.back()->addImplicitRef(argVal);
2056 }
2057
2058 // Add the return value as an implicit ref. The call operands
2059 // were added above.
Vikram S. Adveea21a6c2001-10-20 20:57:06 +00002060 if (callInstr->getType() != Type::VoidTy)
Vikram S. Adve74825322002-03-18 03:15:35 +00002061 mvec.back()->addImplicitRef(callInstr, /*isDef*/ true);
Vikram S. Advea995e602001-10-11 04:23:19 +00002062
Vikram S. Adveea21a6c2001-10-20 20:57:06 +00002063 // For the CALL instruction, the ret. addr. reg. is also implicit
Chris Lattnerb0d04722002-03-26 17:58:12 +00002064 if (isa<Function>(callee))
Vikram S. Adve74825322002-03-18 03:15:35 +00002065 mvec.back()->addImplicitRef(retAddrReg, /*isDef*/ true);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002066
Vikram S. Adve74825322002-03-18 03:15:35 +00002067 // delay slot
2068 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002069 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00002070 }
Vikram S. Adve242a8082002-05-19 15:25:51 +00002071
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002072 case 62: // reg: Shl(reg, reg)
Vikram S. Adve242a8082002-05-19 15:25:51 +00002073 {
2074 Value* argVal1 = subtreeRoot->leftChild()->getValue();
2075 Value* argVal2 = subtreeRoot->rightChild()->getValue();
2076 Instruction* shlInstr = subtreeRoot->getInstruction();
2077
2078 const Type* opType = argVal1->getType();
Chris Lattner0c4e8862002-09-03 01:08:28 +00002079 assert((opType->isInteger() || isa<PointerType>(opType)) &&
2080 "Shl unsupported for other types");
Vikram S. Adve242a8082002-05-19 15:25:51 +00002081
2082 CreateShiftInstructions(target, shlInstr->getParent()->getParent(),
2083 (opType == Type::LongTy)? SLLX : SLL,
2084 argVal1, argVal2, 0, shlInstr, mvec,
2085 MachineCodeForInstruction::get(shlInstr));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002086 break;
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00002087 }
2088
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002089 case 63: // reg: Shr(reg, reg)
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00002090 { const Type* opType = subtreeRoot->leftChild()->getValue()->getType();
Chris Lattner0c4e8862002-09-03 01:08:28 +00002091 assert((opType->isInteger() || isa<PointerType>(opType)) &&
2092 "Shr unsupported for other types");
Vikram S. Adve74825322002-03-18 03:15:35 +00002093 mvec.push_back(new MachineInstr((opType->isSigned()
2094 ? ((opType == Type::LongTy)? SRAX : SRA)
2095 : ((opType == Type::LongTy)? SRLX : SRL))));
2096 Set3OperandsFromInstr(mvec.back(), subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002097 break;
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00002098 }
2099
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002100 case 64: // reg: Phi(reg,reg)
Vikram S. Adve74825322002-03-18 03:15:35 +00002101 break; // don't forward the value
2102
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002103 case 71: // reg: VReg
2104 case 72: // reg: Constant
Vikram S. Adve74825322002-03-18 03:15:35 +00002105 break; // don't forward the value
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002106
2107 default:
2108 assert(0 && "Unrecognized BURG rule");
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002109 break;
2110 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00002111 }
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00002112
Chris Lattner20b1ea02001-09-14 03:47:57 +00002113 if (forwardOperandNum >= 0)
2114 { // We did not generate a machine instruction but need to use operand.
2115 // If user is in the same tree, replace Value in its machine operand.
2116 // If not, insert a copy instruction which should get coalesced away
2117 // by register allocation.
2118 if (subtreeRoot->parent() != NULL)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002119 ForwardOperand(subtreeRoot, subtreeRoot->parent(), forwardOperandNum);
Chris Lattner20b1ea02001-09-14 03:47:57 +00002120 else
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002121 {
Vikram S. Adve7fe27872001-10-18 00:26:20 +00002122 vector<MachineInstr*> minstrVec;
Vikram S. Adve242a8082002-05-19 15:25:51 +00002123 Instruction* instr = subtreeRoot->getInstruction();
2124 target.getInstrInfo().
2125 CreateCopyInstructionsByType(target,
2126 instr->getParent()->getParent(),
2127 instr->getOperand(forwardOperandNum),
2128 instr, minstrVec,
2129 MachineCodeForInstruction::get(instr));
Vikram S. Adve7fe27872001-10-18 00:26:20 +00002130 assert(minstrVec.size() > 0);
Vikram S. Adve74825322002-03-18 03:15:35 +00002131 mvec.insert(mvec.end(), minstrVec.begin(), minstrVec.end());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002132 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00002133 }
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00002134
2135 if (maskUnsignedResult)
2136 { // If result is unsigned and smaller than int reg size,
2137 // we need to clear high bits of result value.
2138 assert(forwardOperandNum < 0 && "Need mask but no instruction generated");
2139 Instruction* dest = subtreeRoot->getInstruction();
Vikram S. Adve8cfffd32002-08-24 20:56:53 +00002140 if (dest->getType()->isUnsigned())
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00002141 {
2142 unsigned destSize = target.DataLayout.getTypeSize(dest->getType());
Vikram S. Advebabc0fa2002-09-05 18:32:13 +00002143 if (destSize <= 4)
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00002144 { // Mask high bits. Use a TmpInstruction to represent the
2145 // intermediate result before masking. Since those instructions
2146 // have already been generated, go back and substitute tmpI
2147 // for dest in the result position of each one of them.
2148 TmpInstruction *tmpI = new TmpInstruction(dest->getType(), dest,
2149 NULL, "maskHi");
2150 MachineCodeForInstruction::get(dest).addTemp(tmpI);
2151
2152 for (unsigned i=0, N=mvec.size(); i < N; ++i)
2153 mvec[i]->substituteValue(dest, tmpI);
2154
Vikram S. Adve94c40812002-09-27 14:33:08 +00002155 M = Create3OperandInstr_UImmed(SRL, tmpI, 8*(4-destSize), dest);
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00002156 mvec.push_back(M);
2157 }
Vikram S. Advebabc0fa2002-09-05 18:32:13 +00002158 else if (destSize < target.DataLayout.getIntegerRegize())
2159 assert(0 && "Unsupported type size: 32 < size < 64 bits");
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00002160 }
2161 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00002162}