blob: 2c07df5eabeed51e0dd65df3091e1c04398d9aeb [file] [log] [blame]
Chris Lattner035dfbe2002-08-09 20:08:06 +00001//===-- SparcInstrSelection.cpp -------------------------------------------===//
2//
3// BURS instruction selection for SPARC V9 architecture.
4//
5//===----------------------------------------------------------------------===//
Chris Lattner20b1ea02001-09-14 03:47:57 +00006
7#include "SparcInternals.h"
Vikram S. Adve7fe27872001-10-18 00:26:20 +00008#include "SparcInstrSelectionSupport.h"
Vikram S. Adve74825322002-03-18 03:15:35 +00009#include "SparcRegClassInfo.h"
Vikram S. Adve8557b222001-10-10 20:56:33 +000010#include "llvm/CodeGen/InstrSelectionSupport.h"
Chris Lattner20b1ea02001-09-14 03:47:57 +000011#include "llvm/CodeGen/MachineInstr.h"
Vikram S. Adve242a8082002-05-19 15:25:51 +000012#include "llvm/CodeGen/MachineInstrAnnot.h"
Chris Lattner20b1ea02001-09-14 03:47:57 +000013#include "llvm/CodeGen/InstrForest.h"
14#include "llvm/CodeGen/InstrSelection.h"
Chris Lattner9c461082002-02-03 07:50:56 +000015#include "llvm/CodeGen/MachineCodeForMethod.h"
16#include "llvm/CodeGen/MachineCodeForInstruction.h"
Chris Lattner20b1ea02001-09-14 03:47:57 +000017#include "llvm/DerivedTypes.h"
18#include "llvm/iTerminators.h"
19#include "llvm/iMemory.h"
20#include "llvm/iOther.h"
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000021#include "llvm/Function.h"
Chris Lattner31bcdb82002-04-28 19:55:58 +000022#include "llvm/Constants.h"
Chris Lattnercee8f9a2001-11-27 00:03:19 +000023#include "Support/MathExtras.h"
Chris Lattner749655f2001-10-13 06:54:30 +000024#include <math.h>
Chris Lattner697954c2002-01-20 22:54:45 +000025using std::vector;
Chris Lattner20b1ea02001-09-14 03:47:57 +000026
27//************************* Forward Declarations ***************************/
28
29
Vikram S. Adve74825322002-03-18 03:15:35 +000030static void SetMemOperands_Internal (vector<MachineInstr*>& mvec,
31 vector<MachineInstr*>::iterator mvecI,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +000032 const InstructionNode* vmInstrNode,
33 Value* ptrVal,
Vikram S. Advefd3900a2002-03-24 03:33:02 +000034 std::vector<Value*>& idxVec,
Vikram S. Adve242a8082002-05-19 15:25:51 +000035 bool allConstantIndices,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +000036 const TargetMachine& target);
Chris Lattner20b1ea02001-09-14 03:47:57 +000037
38
39//************************ Internal Functions ******************************/
40
Chris Lattner20b1ea02001-09-14 03:47:57 +000041
Chris Lattner20b1ea02001-09-14 03:47:57 +000042static inline MachineOpCode
43ChooseBprInstruction(const InstructionNode* instrNode)
44{
45 MachineOpCode opCode;
46
47 Instruction* setCCInstr =
48 ((InstructionNode*) instrNode->leftChild())->getInstruction();
49
50 switch(setCCInstr->getOpcode())
51 {
52 case Instruction::SetEQ: opCode = BRZ; break;
53 case Instruction::SetNE: opCode = BRNZ; break;
54 case Instruction::SetLE: opCode = BRLEZ; break;
55 case Instruction::SetGE: opCode = BRGEZ; break;
56 case Instruction::SetLT: opCode = BRLZ; break;
57 case Instruction::SetGT: opCode = BRGZ; break;
58 default:
59 assert(0 && "Unrecognized VM instruction!");
60 opCode = INVALID_OPCODE;
61 break;
62 }
63
64 return opCode;
65}
66
67
68static inline MachineOpCode
Chris Lattner20b1ea02001-09-14 03:47:57 +000069ChooseBpccInstruction(const InstructionNode* instrNode,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +000070 const BinaryOperator* setCCInstr)
Chris Lattner20b1ea02001-09-14 03:47:57 +000071{
72 MachineOpCode opCode = INVALID_OPCODE;
73
74 bool isSigned = setCCInstr->getOperand(0)->getType()->isSigned();
75
76 if (isSigned)
77 {
78 switch(setCCInstr->getOpcode())
Vikram S. Adve4cecdd22001-10-01 00:12:53 +000079 {
80 case Instruction::SetEQ: opCode = BE; break;
81 case Instruction::SetNE: opCode = BNE; break;
82 case Instruction::SetLE: opCode = BLE; break;
83 case Instruction::SetGE: opCode = BGE; break;
84 case Instruction::SetLT: opCode = BL; break;
85 case Instruction::SetGT: opCode = BG; break;
86 default:
87 assert(0 && "Unrecognized VM instruction!");
88 break;
89 }
Chris Lattner20b1ea02001-09-14 03:47:57 +000090 }
91 else
92 {
93 switch(setCCInstr->getOpcode())
Vikram S. Adve4cecdd22001-10-01 00:12:53 +000094 {
95 case Instruction::SetEQ: opCode = BE; break;
96 case Instruction::SetNE: opCode = BNE; break;
97 case Instruction::SetLE: opCode = BLEU; break;
98 case Instruction::SetGE: opCode = BCC; break;
99 case Instruction::SetLT: opCode = BCS; break;
100 case Instruction::SetGT: opCode = BGU; break;
101 default:
102 assert(0 && "Unrecognized VM instruction!");
103 break;
104 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000105 }
106
107 return opCode;
108}
109
110static inline MachineOpCode
111ChooseBFpccInstruction(const InstructionNode* instrNode,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000112 const BinaryOperator* setCCInstr)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000113{
114 MachineOpCode opCode = INVALID_OPCODE;
115
116 switch(setCCInstr->getOpcode())
117 {
118 case Instruction::SetEQ: opCode = FBE; break;
119 case Instruction::SetNE: opCode = FBNE; break;
120 case Instruction::SetLE: opCode = FBLE; break;
121 case Instruction::SetGE: opCode = FBGE; break;
122 case Instruction::SetLT: opCode = FBL; break;
123 case Instruction::SetGT: opCode = FBG; break;
124 default:
125 assert(0 && "Unrecognized VM instruction!");
126 break;
127 }
128
129 return opCode;
130}
131
132
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000133// Create a unique TmpInstruction for a boolean value,
134// representing the CC register used by a branch on that value.
135// For now, hack this using a little static cache of TmpInstructions.
136// Eventually the entire BURG instruction selection should be put
137// into a separate class that can hold such information.
Vikram S. Adveff5a09e2001-11-08 05:04:09 +0000138// The static cache is not too bad because the memory for these
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000139// TmpInstructions will be freed along with the rest of the Function anyway.
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000140//
141static TmpInstruction*
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000142GetTmpForCC(Value* boolVal, const Function *F, const Type* ccType)
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000143{
Chris Lattner09ff1122002-07-24 21:21:32 +0000144 typedef hash_map<const Value*, TmpInstruction*> BoolTmpCache;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000145 static BoolTmpCache boolToTmpCache; // Map boolVal -> TmpInstruction*
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000146 static const Function *lastFunction = 0;// Use to flush cache between funcs
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000147
148 assert(boolVal->getType() == Type::BoolTy && "Weird but ok! Delete assert");
149
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000150 if (lastFunction != F)
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000151 {
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000152 lastFunction = F;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000153 boolToTmpCache.clear();
154 }
155
Vikram S. Adveff5a09e2001-11-08 05:04:09 +0000156 // Look for tmpI and create a new one otherwise. The new value is
157 // directly written to map using the ref returned by operator[].
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000158 TmpInstruction*& tmpI = boolToTmpCache[boolVal];
159 if (tmpI == NULL)
Chris Lattner9c461082002-02-03 07:50:56 +0000160 tmpI = new TmpInstruction(ccType, boolVal);
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000161
162 return tmpI;
163}
164
165
Chris Lattner20b1ea02001-09-14 03:47:57 +0000166static inline MachineOpCode
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000167ChooseBccInstruction(const InstructionNode* instrNode,
168 bool& isFPBranch)
169{
170 InstructionNode* setCCNode = (InstructionNode*) instrNode->leftChild();
Vikram S. Adve30a6f492002-08-22 02:56:10 +0000171 assert(setCCNode->getOpLabel() == SetCCOp);
172 BinaryOperator* setCCInstr =cast<BinaryOperator>(setCCNode->getInstruction());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000173 const Type* setCCType = setCCInstr->getOperand(0)->getType();
174
Vikram S. Adve242a8082002-05-19 15:25:51 +0000175 isFPBranch = setCCType->isFloatingPoint(); // Return value: don't delete!
176
177 if (isFPBranch)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000178 return ChooseBFpccInstruction(instrNode, setCCInstr);
179 else
180 return ChooseBpccInstruction(instrNode, setCCInstr);
181}
182
183
184static inline MachineOpCode
Chris Lattner20b1ea02001-09-14 03:47:57 +0000185ChooseMovFpccInstruction(const InstructionNode* instrNode)
186{
187 MachineOpCode opCode = INVALID_OPCODE;
188
189 switch(instrNode->getInstruction()->getOpcode())
190 {
191 case Instruction::SetEQ: opCode = MOVFE; break;
192 case Instruction::SetNE: opCode = MOVFNE; break;
193 case Instruction::SetLE: opCode = MOVFLE; break;
194 case Instruction::SetGE: opCode = MOVFGE; break;
195 case Instruction::SetLT: opCode = MOVFL; break;
196 case Instruction::SetGT: opCode = MOVFG; break;
197 default:
198 assert(0 && "Unrecognized VM instruction!");
199 break;
200 }
201
202 return opCode;
203}
204
205
206// Assumes that SUBcc v1, v2 -> v3 has been executed.
207// In most cases, we want to clear v3 and then follow it by instruction
208// MOVcc 1 -> v3.
209// Set mustClearReg=false if v3 need not be cleared before conditional move.
210// Set valueToMove=0 if we want to conditionally move 0 instead of 1
211// (i.e., we want to test inverse of a condition)
Vikram S. Adve243dd452001-09-18 13:03:13 +0000212// (The latter two cases do not seem to arise because SetNE needs nothing.)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000213//
214static MachineOpCode
215ChooseMovpccAfterSub(const InstructionNode* instrNode,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000216 bool& mustClearReg,
217 int& valueToMove)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000218{
219 MachineOpCode opCode = INVALID_OPCODE;
220 mustClearReg = true;
221 valueToMove = 1;
222
223 switch(instrNode->getInstruction()->getOpcode())
224 {
Vikram S. Adve243dd452001-09-18 13:03:13 +0000225 case Instruction::SetEQ: opCode = MOVE; break;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000226 case Instruction::SetLE: opCode = MOVLE; break;
227 case Instruction::SetGE: opCode = MOVGE; break;
228 case Instruction::SetLT: opCode = MOVL; break;
229 case Instruction::SetGT: opCode = MOVG; break;
Vikram S. Adve243dd452001-09-18 13:03:13 +0000230 case Instruction::SetNE: assert(0 && "No move required!"); break;
231 default: assert(0 && "Unrecognized VM instr!"); break;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000232 }
233
234 return opCode;
235}
236
Chris Lattner20b1ea02001-09-14 03:47:57 +0000237static inline MachineOpCode
Vikram S. Advedbc4fad2002-04-25 04:37:51 +0000238ChooseConvertToFloatInstr(OpLabel vopCode, const Type* opType)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000239{
240 MachineOpCode opCode = INVALID_OPCODE;
241
Vikram S. Advedbc4fad2002-04-25 04:37:51 +0000242 switch(vopCode)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000243 {
244 case ToFloatTy:
245 if (opType == Type::SByteTy || opType == Type::ShortTy || opType == Type::IntTy)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000246 opCode = FITOS;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000247 else if (opType == Type::LongTy)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000248 opCode = FXTOS;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000249 else if (opType == Type::DoubleTy)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000250 opCode = FDTOS;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000251 else if (opType == Type::FloatTy)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000252 ;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000253 else
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000254 assert(0 && "Cannot convert this type to FLOAT on SPARC");
Chris Lattner20b1ea02001-09-14 03:47:57 +0000255 break;
256
257 case ToDoubleTy:
Vikram S. Adve74825322002-03-18 03:15:35 +0000258 // This is usually used in conjunction with CreateCodeToCopyIntToFloat().
259 // Both functions should treat the integer as a 32-bit value for types
260 // of 4 bytes or less, and as a 64-bit value otherwise.
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000261 if (opType == Type::SByteTy || opType == Type::UByteTy ||
262 opType == Type::ShortTy || opType == Type::UShortTy ||
263 opType == Type::IntTy || opType == Type::UIntTy)
Vikram S. Adve74825322002-03-18 03:15:35 +0000264 opCode = FITOD;
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000265 else if (opType == Type::LongTy || opType == Type::ULongTy)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000266 opCode = FXTOD;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000267 else if (opType == Type::FloatTy)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000268 opCode = FSTOD;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000269 else if (opType == Type::DoubleTy)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000270 ;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000271 else
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000272 assert(0 && "Cannot convert this type to DOUBLE on SPARC");
Chris Lattner20b1ea02001-09-14 03:47:57 +0000273 break;
274
275 default:
276 break;
277 }
278
279 return opCode;
280}
281
282static inline MachineOpCode
Vikram S. Adve1e606692002-07-31 21:01:34 +0000283ChooseConvertToIntInstr(Type::PrimitiveID tid, const Type* opType)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000284{
285 MachineOpCode opCode = INVALID_OPCODE;;
286
Vikram S. Adve1e606692002-07-31 21:01:34 +0000287 if (tid==Type::SByteTyID || tid==Type::ShortTyID || tid==Type::IntTyID ||
288 tid==Type::UByteTyID || tid==Type::UShortTyID || tid==Type::UIntTyID)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000289 {
290 switch (opType->getPrimitiveID())
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000291 {
Chris Lattner20b1ea02001-09-14 03:47:57 +0000292 case Type::FloatTyID: opCode = FSTOI; break;
293 case Type::DoubleTyID: opCode = FDTOI; break;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000294 default:
295 assert(0 && "Non-numeric non-bool type cannot be converted to Int");
296 break;
297 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000298 }
Vikram S. Adve1e606692002-07-31 21:01:34 +0000299 else if (tid==Type::LongTyID || tid==Type::ULongTyID)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000300 {
301 switch (opType->getPrimitiveID())
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000302 {
Chris Lattner20b1ea02001-09-14 03:47:57 +0000303 case Type::FloatTyID: opCode = FSTOX; break;
304 case Type::DoubleTyID: opCode = FDTOX; break;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000305 default:
306 assert(0 && "Non-numeric non-bool type cannot be converted to Long");
307 break;
308 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000309 }
310 else
311 assert(0 && "Should not get here, Mo!");
312
313 return opCode;
314}
315
Vikram S. Advedbc4fad2002-04-25 04:37:51 +0000316MachineInstr*
Vikram S. Adve1e606692002-07-31 21:01:34 +0000317CreateConvertToIntInstr(Type::PrimitiveID destTID, Value* srcVal,Value* destVal)
Vikram S. Advedbc4fad2002-04-25 04:37:51 +0000318{
Vikram S. Adve1e606692002-07-31 21:01:34 +0000319 MachineOpCode opCode = ChooseConvertToIntInstr(destTID, srcVal->getType());
Vikram S. Advedbc4fad2002-04-25 04:37:51 +0000320 assert(opCode != INVALID_OPCODE && "Expected to need conversion!");
321
322 MachineInstr* M = new MachineInstr(opCode);
323 M->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister, srcVal);
324 M->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister, destVal);
325 return M;
326}
Chris Lattner20b1ea02001-09-14 03:47:57 +0000327
Vikram S. Adve1e606692002-07-31 21:01:34 +0000328// CreateCodeToConvertIntToFloat: Convert FP value to signed or unsigned integer
329// The FP value must be converted to the dest type in an FP register,
330// and the result is then copied from FP to int register via memory.
331static void
332CreateCodeToConvertIntToFloat (const TargetMachine& target,
333 Value* opVal,
334 Instruction* destI,
335 std::vector<MachineInstr*>& mvec,
336 MachineCodeForInstruction& mcfi)
337{
338 // Create a temporary to represent the FP register into which the
339 // int value will placed after conversion. The type of this temporary
340 // depends on the type of FP register to use: single-prec for a 32-bit
341 // int or smaller; double-prec for a 64-bit int.
342 //
343 const Type* destTypeToUse = (destI->getType() == Type::LongTy)? Type::DoubleTy
344 : Type::FloatTy;
345 Value* destForCast = new TmpInstruction(destTypeToUse, opVal);
346 mcfi.addTemp(destForCast);
347
348 // Create the fp-to-int conversion code
349 MachineInstr* M = CreateConvertToIntInstr(destI->getType()->getPrimitiveID(),
350 opVal, destForCast);
351 mvec.push_back(M);
352
353 // Create the fpreg-to-intreg copy code
354 target.getInstrInfo().
355 CreateCodeToCopyFloatToInt(target, destI->getParent()->getParent(),
356 (TmpInstruction*)destForCast, destI, mvec, mcfi);
357}
358
359
Chris Lattner20b1ea02001-09-14 03:47:57 +0000360static inline MachineOpCode
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000361ChooseAddInstruction(const InstructionNode* instrNode)
362{
363 return ChooseAddInstructionByType(instrNode->getInstruction()->getType());
364}
365
366
Chris Lattner20b1ea02001-09-14 03:47:57 +0000367static inline MachineInstr*
368CreateMovFloatInstruction(const InstructionNode* instrNode,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000369 const Type* resultType)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000370{
371 MachineInstr* minstr = new MachineInstr((resultType == Type::FloatTy)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000372 ? FMOVS : FMOVD);
Vikram S. Adve74825322002-03-18 03:15:35 +0000373 minstr->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister,
374 instrNode->leftChild()->getValue());
375 minstr->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister,
376 instrNode->getValue());
Chris Lattner20b1ea02001-09-14 03:47:57 +0000377 return minstr;
378}
379
380static inline MachineInstr*
381CreateAddConstInstruction(const InstructionNode* instrNode)
382{
383 MachineInstr* minstr = NULL;
384
385 Value* constOp = ((InstrTreeNode*) instrNode->rightChild())->getValue();
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000386 assert(isa<Constant>(constOp));
Chris Lattner20b1ea02001-09-14 03:47:57 +0000387
388 // Cases worth optimizing are:
389 // (1) Add with 0 for float or double: use an FMOV of appropriate type,
390 // instead of an FADD (1 vs 3 cycles). There is no integer MOV.
391 //
Chris Lattner9b625032002-05-06 16:15:30 +0000392 if (ConstantFP *FPC = dyn_cast<ConstantFP>(constOp)) {
393 double dval = FPC->getValue();
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000394 if (dval == 0.0)
Vikram S. Adve242a8082002-05-19 15:25:51 +0000395 minstr = CreateMovFloatInstruction(instrNode,
396 instrNode->getInstruction()->getType());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000397 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000398
399 return minstr;
400}
401
402
403static inline MachineOpCode
Vikram S. Adve510eec72001-11-04 21:59:14 +0000404ChooseSubInstructionByType(const Type* resultType)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000405{
406 MachineOpCode opCode = INVALID_OPCODE;
407
Chris Lattner9b625032002-05-06 16:15:30 +0000408 if (resultType->isIntegral() || isa<PointerType>(resultType))
Chris Lattner20b1ea02001-09-14 03:47:57 +0000409 {
410 opCode = SUB;
411 }
412 else
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000413 switch(resultType->getPrimitiveID())
414 {
415 case Type::FloatTyID: opCode = FSUBS; break;
416 case Type::DoubleTyID: opCode = FSUBD; break;
417 default: assert(0 && "Invalid type for SUB instruction"); break;
418 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000419
420 return opCode;
421}
422
423
424static inline MachineInstr*
425CreateSubConstInstruction(const InstructionNode* instrNode)
426{
427 MachineInstr* minstr = NULL;
428
429 Value* constOp = ((InstrTreeNode*) instrNode->rightChild())->getValue();
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000430 assert(isa<Constant>(constOp));
Chris Lattner20b1ea02001-09-14 03:47:57 +0000431
432 // Cases worth optimizing are:
433 // (1) Sub with 0 for float or double: use an FMOV of appropriate type,
434 // instead of an FSUB (1 vs 3 cycles). There is no integer MOV.
435 //
Chris Lattner9b625032002-05-06 16:15:30 +0000436 if (ConstantFP *FPC = dyn_cast<ConstantFP>(constOp)) {
437 double dval = FPC->getValue();
438 if (dval == 0.0)
Vikram S. Adve242a8082002-05-19 15:25:51 +0000439 minstr = CreateMovFloatInstruction(instrNode,
440 instrNode->getInstruction()->getType());
Chris Lattner9b625032002-05-06 16:15:30 +0000441 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000442
443 return minstr;
444}
445
446
447static inline MachineOpCode
448ChooseFcmpInstruction(const InstructionNode* instrNode)
449{
450 MachineOpCode opCode = INVALID_OPCODE;
451
452 Value* operand = ((InstrTreeNode*) instrNode->leftChild())->getValue();
453 switch(operand->getType()->getPrimitiveID()) {
454 case Type::FloatTyID: opCode = FCMPS; break;
455 case Type::DoubleTyID: opCode = FCMPD; break;
456 default: assert(0 && "Invalid type for FCMP instruction"); break;
457 }
458
459 return opCode;
460}
461
462
463// Assumes that leftArg and rightArg are both cast instructions.
464//
465static inline bool
466BothFloatToDouble(const InstructionNode* instrNode)
467{
468 InstrTreeNode* leftArg = instrNode->leftChild();
469 InstrTreeNode* rightArg = instrNode->rightChild();
470 InstrTreeNode* leftArgArg = leftArg->leftChild();
471 InstrTreeNode* rightArgArg = rightArg->leftChild();
472 assert(leftArg->getValue()->getType() == rightArg->getValue()->getType());
473
474 // Check if both arguments are floats cast to double
475 return (leftArg->getValue()->getType() == Type::DoubleTy &&
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000476 leftArgArg->getValue()->getType() == Type::FloatTy &&
477 rightArgArg->getValue()->getType() == Type::FloatTy);
Chris Lattner20b1ea02001-09-14 03:47:57 +0000478}
479
480
481static inline MachineOpCode
Vikram S. Adve510eec72001-11-04 21:59:14 +0000482ChooseMulInstructionByType(const Type* resultType)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000483{
484 MachineOpCode opCode = INVALID_OPCODE;
485
Chris Lattner20b1ea02001-09-14 03:47:57 +0000486 if (resultType->isIntegral())
Vikram S. Adve510eec72001-11-04 21:59:14 +0000487 opCode = MULX;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000488 else
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000489 switch(resultType->getPrimitiveID())
490 {
491 case Type::FloatTyID: opCode = FMULS; break;
492 case Type::DoubleTyID: opCode = FMULD; break;
493 default: assert(0 && "Invalid type for MUL instruction"); break;
494 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000495
496 return opCode;
497}
498
499
Vikram S. Adve510eec72001-11-04 21:59:14 +0000500
Chris Lattner20b1ea02001-09-14 03:47:57 +0000501static inline MachineInstr*
Vikram S. Adve74825322002-03-18 03:15:35 +0000502CreateIntNegInstruction(const TargetMachine& target,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000503 Value* vreg)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000504{
505 MachineInstr* minstr = new MachineInstr(SUB);
Vikram S. Adve74825322002-03-18 03:15:35 +0000506 minstr->SetMachineOperandReg(0, target.getRegInfo().getZeroRegNum());
507 minstr->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister, vreg);
508 minstr->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister, vreg);
Chris Lattner20b1ea02001-09-14 03:47:57 +0000509 return minstr;
510}
511
512
Vikram S. Adve242a8082002-05-19 15:25:51 +0000513// Create instruction sequence for any shift operation.
514// SLL or SLLX on an operand smaller than the integer reg. size (64bits)
515// requires a second instruction for explicit sign-extension.
516// Note that we only have to worry about a sign-bit appearing in the
517// most significant bit of the operand after shifting (e.g., bit 32 of
518// Int or bit 16 of Short), so we do not have to worry about results
519// that are as large as a normal integer register.
520//
521static inline void
522CreateShiftInstructions(const TargetMachine& target,
523 Function* F,
524 MachineOpCode shiftOpCode,
525 Value* argVal1,
526 Value* optArgVal2, /* Use optArgVal2 if not NULL */
527 unsigned int optShiftNum, /* else use optShiftNum */
528 Instruction* destVal,
529 vector<MachineInstr*>& mvec,
530 MachineCodeForInstruction& mcfi)
531{
532 assert((optArgVal2 != NULL || optShiftNum <= 64) &&
533 "Large shift sizes unexpected, but can be handled below: "
534 "You need to check whether or not it fits in immed field below");
535
536 // If this is a logical left shift of a type smaller than the standard
537 // integer reg. size, we have to extend the sign-bit into upper bits
538 // of dest, so we need to put the result of the SLL into a temporary.
539 //
540 Value* shiftDest = destVal;
541 const Type* opType = argVal1->getType();
542 unsigned opSize = target.DataLayout.getTypeSize(argVal1->getType());
543 if ((shiftOpCode == SLL || shiftOpCode == SLLX)
544 && opSize < target.DataLayout.getIntegerRegize())
545 { // put SLL result into a temporary
546 shiftDest = new TmpInstruction(argVal1, optArgVal2, "sllTmp");
547 mcfi.addTemp(shiftDest);
548 }
549
550 MachineInstr* M = (optArgVal2 != NULL)
551 ? Create3OperandInstr(shiftOpCode, argVal1, optArgVal2, shiftDest)
552 : Create3OperandInstr_UImmed(shiftOpCode, argVal1, optShiftNum, shiftDest);
553 mvec.push_back(M);
554
555 if (shiftDest != destVal)
556 { // extend the sign-bit of the result into all upper bits of dest
557 assert(8*opSize <= 32 && "Unexpected type size > 4 and < IntRegSize?");
558 target.getInstrInfo().
559 CreateSignExtensionInstructions(target, F, shiftDest, 8*opSize,
560 destVal, mvec, mcfi);
561 }
562}
563
564
Vikram S. Adve74825322002-03-18 03:15:35 +0000565// Does not create any instructions if we cannot exploit constant to
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000566// create a cheaper instruction.
567// This returns the approximate cost of the instructions generated,
568// which is used to pick the cheapest when both operands are constant.
569static inline unsigned int
Vikram S. Adve242a8082002-05-19 15:25:51 +0000570CreateMulConstInstruction(const TargetMachine &target, Function* F,
571 Value* lval, Value* rval, Instruction* destVal,
572 vector<MachineInstr*>& mvec,
573 MachineCodeForInstruction& mcfi)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000574{
Vikram S. Adve242a8082002-05-19 15:25:51 +0000575 /* Use max. multiply cost, viz., cost of MULX */
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000576 unsigned int cost = target.getInstrInfo().minLatency(MULX);
Vikram S. Adve242a8082002-05-19 15:25:51 +0000577 unsigned int firstNewInstr = mvec.size();
Vikram S. Adve74825322002-03-18 03:15:35 +0000578
579 Value* constOp = rval;
580 if (! isa<Constant>(constOp))
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000581 return cost;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000582
583 // Cases worth optimizing are:
584 // (1) Multiply by 0 or 1 for any type: replace with copy (ADD or FMOV)
585 // (2) Multiply by 2^x for integer types: replace with Shift
586 //
Vikram S. Adve74825322002-03-18 03:15:35 +0000587 const Type* resultType = destVal->getType();
Chris Lattner20b1ea02001-09-14 03:47:57 +0000588
Chris Lattner9b625032002-05-06 16:15:30 +0000589 if (resultType->isIntegral() || isa<PointerType>(resultType))
Chris Lattner20b1ea02001-09-14 03:47:57 +0000590 {
Chris Lattner20b1ea02001-09-14 03:47:57 +0000591 bool isValidConst;
592 int64_t C = GetConstantValueAsSignedInt(constOp, isValidConst);
593 if (isValidConst)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000594 {
Vikram S. Adve242a8082002-05-19 15:25:51 +0000595 unsigned pow;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000596 bool needNeg = false;
597 if (C < 0)
598 {
599 needNeg = true;
600 C = -C;
601 }
602
603 if (C == 0 || C == 1)
604 {
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000605 cost = target.getInstrInfo().minLatency(ADD);
Vikram S. Adve242a8082002-05-19 15:25:51 +0000606 MachineInstr* M = (C == 0)
607 ? Create3OperandInstr_Reg(ADD,
608 target.getRegInfo().getZeroRegNum(),
609 target.getRegInfo().getZeroRegNum(),
610 destVal)
611 : Create3OperandInstr_Reg(ADD, lval,
612 target.getRegInfo().getZeroRegNum(),
613 destVal);
614 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000615 }
Chris Lattner36346c72002-05-19 21:20:19 +0000616 else if (isPowerOf2(C, pow))
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000617 {
Vikram S. Adve242a8082002-05-19 15:25:51 +0000618 unsigned int opSize = target.DataLayout.getTypeSize(resultType);
619 MachineOpCode opCode = (opSize <= 32)? SLL : SLLX;
620 CreateShiftInstructions(target, F, opCode, lval, NULL, pow,
621 destVal, mvec, mcfi);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000622 }
623
Vikram S. Adve242a8082002-05-19 15:25:51 +0000624 if (mvec.size() > 0 && needNeg)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000625 { // insert <reg = SUB 0, reg> after the instr to flip the sign
Vikram S. Adve242a8082002-05-19 15:25:51 +0000626 MachineInstr* M = CreateIntNegInstruction(target, destVal);
627 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000628 }
629 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000630 }
631 else
632 {
Chris Lattner9b625032002-05-06 16:15:30 +0000633 if (ConstantFP *FPC = dyn_cast<ConstantFP>(constOp))
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000634 {
Chris Lattner9b625032002-05-06 16:15:30 +0000635 double dval = FPC->getValue();
Vikram S. Adve6ad7c552001-11-09 02:18:16 +0000636 if (fabs(dval) == 1)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000637 {
Vikram S. Adve242a8082002-05-19 15:25:51 +0000638 MachineOpCode opCode = (dval < 0)
Vikram S. Adve6ad7c552001-11-09 02:18:16 +0000639 ? (resultType == Type::FloatTy? FNEGS : FNEGD)
640 : (resultType == Type::FloatTy? FMOVS : FMOVD);
Vikram S. Adve242a8082002-05-19 15:25:51 +0000641 MachineInstr* M = Create2OperandInstr(opCode, lval, destVal);
642 mvec.push_back(M);
Vikram S. Adve6ad7c552001-11-09 02:18:16 +0000643 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000644 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000645 }
646
Vikram S. Adve242a8082002-05-19 15:25:51 +0000647 if (firstNewInstr < mvec.size())
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000648 {
Vikram S. Adve242a8082002-05-19 15:25:51 +0000649 cost = 0;
650 for (unsigned int i=firstNewInstr; i < mvec.size(); ++i)
651 cost += target.getInstrInfo().minLatency(mvec[i]->getOpCode());
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000652 }
653
654 return cost;
Vikram S. Adve74825322002-03-18 03:15:35 +0000655}
656
657
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000658// Does not create any instructions if we cannot exploit constant to
659// create a cheaper instruction.
660//
661static inline void
662CreateCheapestMulConstInstruction(const TargetMachine &target,
Vikram S. Adve242a8082002-05-19 15:25:51 +0000663 Function* F,
664 Value* lval, Value* rval,
665 Instruction* destVal,
666 vector<MachineInstr*>& mvec,
667 MachineCodeForInstruction& mcfi)
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000668{
669 Value* constOp;
670 if (isa<Constant>(lval) && isa<Constant>(rval))
671 { // both operands are constant: try both orders!
672 vector<MachineInstr*> mvec1, mvec2;
Vikram S. Adve242a8082002-05-19 15:25:51 +0000673 unsigned int lcost = CreateMulConstInstruction(target, F, lval, rval,
674 destVal, mvec1, mcfi);
675 unsigned int rcost = CreateMulConstInstruction(target, F, rval, lval,
676 destVal, mvec2, mcfi);
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000677 vector<MachineInstr*>& mincostMvec = (lcost <= rcost)? mvec1 : mvec2;
678 vector<MachineInstr*>& maxcostMvec = (lcost <= rcost)? mvec2 : mvec1;
679 mvec.insert(mvec.end(), mincostMvec.begin(), mincostMvec.end());
680
681 for (unsigned int i=0; i < maxcostMvec.size(); ++i)
682 delete maxcostMvec[i];
683 }
684 else if (isa<Constant>(rval)) // rval is constant, but not lval
Vikram S. Adve242a8082002-05-19 15:25:51 +0000685 CreateMulConstInstruction(target, F, lval, rval, destVal, mvec, mcfi);
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000686 else if (isa<Constant>(lval)) // lval is constant, but not rval
Vikram S. Adve242a8082002-05-19 15:25:51 +0000687 CreateMulConstInstruction(target, F, lval, rval, destVal, mvec, mcfi);
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000688
689 // else neither is constant
690 return;
691}
692
Vikram S. Adve74825322002-03-18 03:15:35 +0000693// Return NULL if we cannot exploit constant to create a cheaper instruction
694static inline void
Vikram S. Adve242a8082002-05-19 15:25:51 +0000695CreateMulInstruction(const TargetMachine &target, Function* F,
696 Value* lval, Value* rval, Instruction* destVal,
Vikram S. Adve74825322002-03-18 03:15:35 +0000697 vector<MachineInstr*>& mvec,
Vikram S. Adve242a8082002-05-19 15:25:51 +0000698 MachineCodeForInstruction& mcfi,
Vikram S. Adve74825322002-03-18 03:15:35 +0000699 MachineOpCode forceMulOp = INVALID_MACHINE_OPCODE)
700{
701 unsigned int L = mvec.size();
Vikram S. Adve242a8082002-05-19 15:25:51 +0000702 CreateCheapestMulConstInstruction(target,F, lval, rval, destVal, mvec, mcfi);
Vikram S. Adve74825322002-03-18 03:15:35 +0000703 if (mvec.size() == L)
704 { // no instructions were added so create MUL reg, reg, reg.
705 // Use FSMULD if both operands are actually floats cast to doubles.
706 // Otherwise, use the default opcode for the appropriate type.
707 MachineOpCode mulOp = ((forceMulOp != INVALID_MACHINE_OPCODE)
708 ? forceMulOp
709 : ChooseMulInstructionByType(destVal->getType()));
710 MachineInstr* M = new MachineInstr(mulOp);
711 M->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister, lval);
712 M->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister, rval);
713 M->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister, destVal);
714 mvec.push_back(M);
715 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000716}
717
718
Vikram S. Adve510eec72001-11-04 21:59:14 +0000719// Generate a divide instruction for Div or Rem.
720// For Rem, this assumes that the operand type will be signed if the result
721// type is signed. This is correct because they must have the same sign.
722//
Chris Lattner20b1ea02001-09-14 03:47:57 +0000723static inline MachineOpCode
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000724ChooseDivInstruction(TargetMachine &target,
725 const InstructionNode* instrNode)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000726{
727 MachineOpCode opCode = INVALID_OPCODE;
728
729 const Type* resultType = instrNode->getInstruction()->getType();
730
731 if (resultType->isIntegral())
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000732 opCode = resultType->isSigned()? SDIVX : UDIVX;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000733 else
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000734 switch(resultType->getPrimitiveID())
735 {
736 case Type::FloatTyID: opCode = FDIVS; break;
737 case Type::DoubleTyID: opCode = FDIVD; break;
738 default: assert(0 && "Invalid type for DIV instruction"); break;
739 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000740
741 return opCode;
742}
743
744
Vikram S. Adve74825322002-03-18 03:15:35 +0000745// Return NULL if we cannot exploit constant to create a cheaper instruction
746static inline void
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000747CreateDivConstInstruction(TargetMachine &target,
748 const InstructionNode* instrNode,
Vikram S. Adve74825322002-03-18 03:15:35 +0000749 vector<MachineInstr*>& mvec)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000750{
Vikram S. Adve74825322002-03-18 03:15:35 +0000751 MachineInstr* minstr1 = NULL;
752 MachineInstr* minstr2 = NULL;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000753
754 Value* constOp = ((InstrTreeNode*) instrNode->rightChild())->getValue();
Vikram S. Adve74825322002-03-18 03:15:35 +0000755 if (! isa<Constant>(constOp))
756 return;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000757
758 // Cases worth optimizing are:
759 // (1) Divide by 1 for any type: replace with copy (ADD or FMOV)
760 // (2) Divide by 2^x for integer types: replace with SR[L or A]{X}
761 //
762 const Type* resultType = instrNode->getInstruction()->getType();
763
764 if (resultType->isIntegral())
765 {
766 unsigned pow;
767 bool isValidConst;
768 int64_t C = GetConstantValueAsSignedInt(constOp, isValidConst);
769 if (isValidConst)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000770 {
771 bool needNeg = false;
772 if (C < 0)
773 {
774 needNeg = true;
775 C = -C;
776 }
777
778 if (C == 1)
779 {
Vikram S. Adve74825322002-03-18 03:15:35 +0000780 minstr1 = new MachineInstr(ADD);
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000781 minstr1->SetMachineOperandVal(0,
782 MachineOperand::MO_VirtualRegister,
783 instrNode->leftChild()->getValue());
784 minstr1->SetMachineOperandReg(1,
785 target.getRegInfo().getZeroRegNum());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000786 }
Chris Lattner36346c72002-05-19 21:20:19 +0000787 else if (isPowerOf2(C, pow))
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000788 {
789 MachineOpCode opCode= ((resultType->isSigned())
790 ? (resultType==Type::LongTy)? SRAX : SRA
791 : (resultType==Type::LongTy)? SRLX : SRL);
Vikram S. Adve74825322002-03-18 03:15:35 +0000792 minstr1 = new MachineInstr(opCode);
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000793 minstr1->SetMachineOperandVal(0,
794 MachineOperand::MO_VirtualRegister,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000795 instrNode->leftChild()->getValue());
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000796 minstr1->SetMachineOperandConst(1,
797 MachineOperand::MO_UnextendedImmed,
798 pow);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000799 }
800
Vikram S. Adve74825322002-03-18 03:15:35 +0000801 if (minstr1 && needNeg)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000802 { // insert <reg = SUB 0, reg> after the instr to flip the sign
Vikram S. Adve74825322002-03-18 03:15:35 +0000803 minstr2 = CreateIntNegInstruction(target,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000804 instrNode->getValue());
805 }
806 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000807 }
808 else
809 {
Chris Lattner9b625032002-05-06 16:15:30 +0000810 if (ConstantFP *FPC = dyn_cast<ConstantFP>(constOp))
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000811 {
Chris Lattner9b625032002-05-06 16:15:30 +0000812 double dval = FPC->getValue();
Vikram S. Adve6ad7c552001-11-09 02:18:16 +0000813 if (fabs(dval) == 1)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000814 {
815 bool needNeg = (dval < 0);
816
817 MachineOpCode opCode = needNeg
818 ? (resultType == Type::FloatTy? FNEGS : FNEGD)
819 : (resultType == Type::FloatTy? FMOVS : FMOVD);
820
Vikram S. Adve74825322002-03-18 03:15:35 +0000821 minstr1 = new MachineInstr(opCode);
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000822 minstr1->SetMachineOperandVal(0,
823 MachineOperand::MO_VirtualRegister,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000824 instrNode->leftChild()->getValue());
825 }
826 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000827 }
828
Vikram S. Adve74825322002-03-18 03:15:35 +0000829 if (minstr1 != NULL)
830 minstr1->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister,
831 instrNode->getValue());
Chris Lattner20b1ea02001-09-14 03:47:57 +0000832
Vikram S. Adve74825322002-03-18 03:15:35 +0000833 if (minstr1)
834 mvec.push_back(minstr1);
835 if (minstr2)
836 mvec.push_back(minstr2);
Chris Lattner20b1ea02001-09-14 03:47:57 +0000837}
838
839
Vikram S. Adve74825322002-03-18 03:15:35 +0000840static void
841CreateCodeForVariableSizeAlloca(const TargetMachine& target,
842 Instruction* result,
843 unsigned int tsize,
844 Value* numElementsVal,
845 vector<MachineInstr*>& getMvec)
846{
847 MachineInstr* M;
848
849 // Create a Value to hold the (constant) element size
850 Value* tsizeVal = ConstantSInt::get(Type::IntTy, tsize);
851
852 // Get the constant offset from SP for dynamically allocated storage
853 // and create a temporary Value to hold it.
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000854 assert(result && result->getParent() && "Result value is not part of a fn?");
855 Function *F = result->getParent()->getParent();
856 MachineCodeForMethod& mcInfo = MachineCodeForMethod::get(F);
Vikram S. Adve74825322002-03-18 03:15:35 +0000857 bool growUp;
858 ConstantSInt* dynamicAreaOffset =
859 ConstantSInt::get(Type::IntTy,
860 target.getFrameInfo().getDynamicAreaOffset(mcInfo,growUp));
861 assert(! growUp && "Has SPARC v9 stack frame convention changed?");
862
863 // Create a temporary value to hold the result of MUL
864 TmpInstruction* tmpProd = new TmpInstruction(numElementsVal, tsizeVal);
865 MachineCodeForInstruction::get(result).addTemp(tmpProd);
866
867 // Instruction 1: mul numElements, typeSize -> tmpProd
868 M = new MachineInstr(MULX);
869 M->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister, numElementsVal);
870 M->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister, tsizeVal);
871 M->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister, tmpProd);
872 getMvec.push_back(M);
873
874 // Instruction 2: sub %sp, tmpProd -> %sp
875 M = new MachineInstr(SUB);
876 M->SetMachineOperandReg(0, target.getRegInfo().getStackPointer());
877 M->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister, tmpProd);
878 M->SetMachineOperandReg(2, target.getRegInfo().getStackPointer());
879 getMvec.push_back(M);
880
881 // Instruction 3: add %sp, frameSizeBelowDynamicArea -> result
882 M = new MachineInstr(ADD);
883 M->SetMachineOperandReg(0, target.getRegInfo().getStackPointer());
884 M->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister, dynamicAreaOffset);
885 M->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister, result);
886 getMvec.push_back(M);
887}
888
889
890static void
891CreateCodeForFixedSizeAlloca(const TargetMachine& target,
892 Instruction* result,
893 unsigned int tsize,
894 unsigned int numElements,
895 vector<MachineInstr*>& getMvec)
896{
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000897 assert(result && result->getParent() &&
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000898 "Result value is not part of a function?");
899 Function *F = result->getParent()->getParent();
900 MachineCodeForMethod &mcInfo = MachineCodeForMethod::get(F);
Vikram S. Adve74825322002-03-18 03:15:35 +0000901
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000902 // Check if the offset would small enough to use as an immediate in
903 // load/stores (check LDX because all load/stores have the same-size immediate
904 // field). If not, put the variable in the dynamically sized area of the
905 // frame.
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000906 unsigned int paddedSizeIgnored;
Vikram S. Adve74825322002-03-18 03:15:35 +0000907 int offsetFromFP = mcInfo.computeOffsetforLocalVar(target, result,
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000908 paddedSizeIgnored,
Vikram S. Adve74825322002-03-18 03:15:35 +0000909 tsize * numElements);
910 if (! target.getInstrInfo().constantFitsInImmedField(LDX, offsetFromFP))
911 {
912 CreateCodeForVariableSizeAlloca(target, result, tsize,
913 ConstantSInt::get(Type::IntTy,numElements),
914 getMvec);
915 return;
916 }
917
918 // else offset fits in immediate field so go ahead and allocate it.
919 offsetFromFP = mcInfo.allocateLocalVar(target, result, tsize * numElements);
920
921 // Create a temporary Value to hold the constant offset.
922 // This is needed because it may not fit in the immediate field.
923 ConstantSInt* offsetVal = ConstantSInt::get(Type::IntTy, offsetFromFP);
924
925 // Instruction 1: add %fp, offsetFromFP -> result
926 MachineInstr* M = new MachineInstr(ADD);
927 M->SetMachineOperandReg(0, target.getRegInfo().getFramePointer());
928 M->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister, offsetVal);
929 M->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister, result);
930
931 getMvec.push_back(M);
932}
933
934
935
Vikram S. Adveed3fefb2002-08-03 13:48:21 +0000936// Check for a constant (uint) 0.
937inline bool
938IsZero(Value* idx)
939{
940 return (isa<ConstantInt>(idx) && cast<ConstantInt>(idx)->isNullValue());
941}
Vikram S. Adve242a8082002-05-19 15:25:51 +0000942
943
Chris Lattner20b1ea02001-09-14 03:47:57 +0000944//------------------------------------------------------------------------
945// Function SetOperandsForMemInstr
946//
947// Choose addressing mode for the given load or store instruction.
948// Use [reg+reg] if it is an indexed reference, and the index offset is
949// not a constant or if it cannot fit in the offset field.
950// Use [reg+offset] in all other cases.
951//
952// This assumes that all array refs are "lowered" to one of these forms:
953// %x = load (subarray*) ptr, constant ; single constant offset
954// %x = load (subarray*) ptr, offsetVal ; single non-constant offset
955// Generally, this should happen via strength reduction + LICM.
956// Also, strength reduction should take care of using the same register for
957// the loop index variable and an array index, when that is profitable.
958//------------------------------------------------------------------------
959
960static void
Vikram S. Adve74825322002-03-18 03:15:35 +0000961SetOperandsForMemInstr(vector<MachineInstr*>& mvec,
962 vector<MachineInstr*>::iterator mvecI,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000963 const InstructionNode* vmInstrNode,
964 const TargetMachine& target)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000965{
966 MemAccessInst* memInst = (MemAccessInst*) vmInstrNode->getInstruction();
967
Vikram S. Adve242a8082002-05-19 15:25:51 +0000968 // Variables to hold the index vector and ptr value.
Chris Lattner20b1ea02001-09-14 03:47:57 +0000969 // The major work here is to extract these for all 3 instruction types
Vikram S. Adve242a8082002-05-19 15:25:51 +0000970 // and to try to fold chains of constant indices into a single offset.
971 // After that, we call SetMemOperands_Internal(), which creates the
972 // appropriate operands for the machine instruction.
Vikram S. Advea10d1a72002-03-31 19:07:35 +0000973 vector<Value*> idxVec;
Vikram S. Adve242a8082002-05-19 15:25:51 +0000974 bool allConstantIndices = true;
975 Value* ptrVal = memInst->getPointerOperand();
Vikram S. Adveed3fefb2002-08-03 13:48:21 +0000976
Vikram S. Advea10d1a72002-03-31 19:07:35 +0000977 // If there is a GetElemPtr instruction to fold in to this instr,
978 // it must be in the left child for Load and GetElemPtr, and in the
979 // right child for Store instructions.
Chris Lattner20b1ea02001-09-14 03:47:57 +0000980 InstrTreeNode* ptrChild = (vmInstrNode->getOpLabel() == Instruction::Store
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000981 ? vmInstrNode->rightChild()
982 : vmInstrNode->leftChild());
Vikram S. Adveed3fefb2002-08-03 13:48:21 +0000983
Vikram S. Adve242a8082002-05-19 15:25:51 +0000984 // Check if all indices are constant for this instruction
Vikram S. Adveed3fefb2002-08-03 13:48:21 +0000985 for (MemAccessInst::op_iterator OI=memInst->idx_begin(),OE=memInst->idx_end();
986 allConstantIndices && OI != OE; ++OI)
987 if (! isa<Constant>(*OI))
988 allConstantIndices = false;
989
Vikram S. Adve242a8082002-05-19 15:25:51 +0000990 // If we have only constant indices, fold chains of constant indices
991 // in this and any preceding GetElemPtr instructions.
Vikram S. Adve99d4a382002-08-04 20:51:05 +0000992 bool foldedGEPs = false;
Vikram S. Adve242a8082002-05-19 15:25:51 +0000993 if (allConstantIndices &&
Vikram S. Adveed3fefb2002-08-03 13:48:21 +0000994 (ptrChild->getOpLabel() == Instruction::GetElementPtr ||
995 ptrChild->getOpLabel() == GetElemPtrIdx))
Vikram S. Adve99d4a382002-08-04 20:51:05 +0000996 if (Value* newPtr = FoldGetElemChain((InstructionNode*) ptrChild, idxVec)) {
Vikram S. Adveed3fefb2002-08-03 13:48:21 +0000997 ptrVal = newPtr;
Vikram S. Adve99d4a382002-08-04 20:51:05 +0000998 foldedGEPs = true;
999 }
Vikram S. Adveed3fefb2002-08-03 13:48:21 +00001000
Vikram S. Adve242a8082002-05-19 15:25:51 +00001001 // Append the index vector of the current instruction, if any.
Vikram S. Adve99d4a382002-08-04 20:51:05 +00001002 // Skip the leading [0] index if preceding GEPs were folded into this.
1003 if (memInst->getNumIndices() > 0) {
1004 assert((!foldedGEPs || IsZero(*memInst->idx_begin())) && "1st index not 0");
Chris Lattner75ac4e52002-08-03 20:57:38 +00001005 idxVec.insert(idxVec.end(),
Vikram S. Adve99d4a382002-08-04 20:51:05 +00001006 memInst->idx_begin() + foldedGEPs, memInst->idx_end());
1007 }
Vikram S. Adveed3fefb2002-08-03 13:48:21 +00001008
Vikram S. Adve242a8082002-05-19 15:25:51 +00001009 // Now create the appropriate operands for the machine instruction
1010 SetMemOperands_Internal(mvec, mvecI, vmInstrNode,
1011 ptrVal, idxVec, allConstantIndices, target);
Chris Lattner20b1ea02001-09-14 03:47:57 +00001012}
1013
1014
Vikram S. Adve74825322002-03-18 03:15:35 +00001015// Generate the correct operands (and additional instructions if needed)
1016// for the given pointer and given index vector.
1017//
Chris Lattner20b1ea02001-09-14 03:47:57 +00001018static void
Vikram S. Adve74825322002-03-18 03:15:35 +00001019SetMemOperands_Internal(vector<MachineInstr*>& mvec,
1020 vector<MachineInstr*>::iterator mvecI,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001021 const InstructionNode* vmInstrNode,
1022 Value* ptrVal,
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001023 vector<Value*>& idxVec,
Vikram S. Adve242a8082002-05-19 15:25:51 +00001024 bool allConstantIndices,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001025 const TargetMachine& target)
Chris Lattner20b1ea02001-09-14 03:47:57 +00001026{
1027 MemAccessInst* memInst = (MemAccessInst*) vmInstrNode->getInstruction();
1028
1029 // Initialize so we default to storing the offset in a register.
Chris Lattner8e5c0b42001-11-07 14:01:59 +00001030 int64_t smallConstOffset = 0;
Chris Lattner20b1ea02001-09-14 03:47:57 +00001031 Value* valueForRegOffset = NULL;
Vikram S. Adveed3fefb2002-08-03 13:48:21 +00001032 MachineOperand::MachineOperandType offsetOpType =
1033 MachineOperand::MO_VirtualRegister;
Chris Lattner20b1ea02001-09-14 03:47:57 +00001034
Vikram S. Adve74825322002-03-18 03:15:35 +00001035 // Check if there is an index vector and if so, compute the
1036 // right offset for structures and for arrays
Chris Lattner20b1ea02001-09-14 03:47:57 +00001037 //
1038 if (idxVec.size() > 0)
1039 {
Vikram S. Adve74825322002-03-18 03:15:35 +00001040 const PointerType* ptrType = cast<PointerType>(ptrVal->getType());
Chris Lattner20b1ea02001-09-14 03:47:57 +00001041
Vikram S. Adve242a8082002-05-19 15:25:51 +00001042 // If all indices are constant, compute the combined offset directly.
1043 if (allConstantIndices)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001044 {
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001045 // Compute the offset value using the index vector. Create a
1046 // virtual reg. for it since it may not fit in the immed field.
Vikram S. Adve242a8082002-05-19 15:25:51 +00001047 uint64_t offset = target.DataLayout.getIndexedOffset(ptrType,idxVec);
1048 valueForRegOffset = ConstantSInt::get(Type::LongTy, offset);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001049 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00001050 else
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001051 {
Vikram S. Adve242a8082002-05-19 15:25:51 +00001052 // There is at least one non-constant offset. Therefore, this must
Vikram S. Adveed3fefb2002-08-03 13:48:21 +00001053 // be an array ref, and must have been lowered to a single non-zero
1054 // offset. (An extra leading zero offset, if any, can be ignored.)
1055 // Generate code sequence to compute address from index.
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001056 //
Chris Lattner75ac4e52002-08-03 20:57:38 +00001057 assert(idxVec.size() == 1U + IsZero(idxVec[0])
Vikram S. Adveed3fefb2002-08-03 13:48:21 +00001058 && "Array refs must be lowered before Instruction Selection");
1059
Chris Lattner75ac4e52002-08-03 20:57:38 +00001060 Value* idxVal = idxVec[IsZero(idxVec[0])];
Vikram S. Adveed3fefb2002-08-03 13:48:21 +00001061
1062 vector<MachineInstr*> mulVec;
1063 Instruction* addr = new TmpInstruction(Type::UIntTy, memInst);
1064 MachineCodeForInstruction::get(memInst).addTemp(addr);
1065
1066 // The call to getTypeSize() will fail if size is not constant.
1067 unsigned int eltSize =
1068 target.DataLayout.getTypeSize(ptrType->getElementType());
1069 assert(eltSize > 0 && "Invalid or non-const array element size");
1070 ConstantUInt* eltVal = ConstantUInt::get(Type::UIntTy, eltSize);
1071
1072 // CreateMulInstruction() folds constants intelligently enough.
1073 CreateMulInstruction(target,
1074 memInst->getParent()->getParent(),
1075 idxVal, /* lval, not likely const */
1076 eltVal, /* rval, likely constant */
1077 addr, /* result*/
1078 mulVec,
1079 MachineCodeForInstruction::get(memInst),
1080 INVALID_MACHINE_OPCODE);
1081
1082 // Insert mulVec[] before *mvecI in mvec[] and update mvecI
1083 // to point to the same instruction it pointed to before.
1084 assert(mulVec.size() > 0 && "No multiply code created?");
1085 vector<MachineInstr*>::iterator oldMvecI = mvecI;
1086 for (unsigned i=0, N=mulVec.size(); i < N; ++i)
1087 mvecI = mvec.insert(mvecI, mulVec[i]) + 1; // pts to mem instr
1088
1089 valueForRegOffset = addr;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001090 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00001091 }
1092 else
1093 {
1094 offsetOpType = MachineOperand::MO_SignExtendedImmed;
1095 smallConstOffset = 0;
1096 }
Vikram S. Adveed3fefb2002-08-03 13:48:21 +00001097
Vikram S. Advea10d1a72002-03-31 19:07:35 +00001098 // For STORE:
1099 // Operand 0 is value, operand 1 is ptr, operand 2 is offset
1100 // For LOAD or GET_ELEMENT_PTR,
1101 // Operand 0 is ptr, operand 1 is offset, operand 2 is result.
1102 //
1103 unsigned offsetOpNum, ptrOpNum;
1104 if (memInst->getOpcode() == Instruction::Store)
1105 {
1106 (*mvecI)->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister,
1107 vmInstrNode->leftChild()->getValue());
1108 ptrOpNum = 1;
1109 offsetOpNum = 2;
1110 }
1111 else
1112 {
1113 ptrOpNum = 0;
1114 offsetOpNum = 1;
1115 (*mvecI)->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister,
1116 memInst);
1117 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00001118
Vikram S. Advea10d1a72002-03-31 19:07:35 +00001119 (*mvecI)->SetMachineOperandVal(ptrOpNum, MachineOperand::MO_VirtualRegister,
1120 ptrVal);
1121
Chris Lattner20b1ea02001-09-14 03:47:57 +00001122 if (offsetOpType == MachineOperand::MO_VirtualRegister)
1123 {
1124 assert(valueForRegOffset != NULL);
Vikram S. Adve74825322002-03-18 03:15:35 +00001125 (*mvecI)->SetMachineOperandVal(offsetOpNum, offsetOpType,
1126 valueForRegOffset);
Chris Lattner20b1ea02001-09-14 03:47:57 +00001127 }
1128 else
Vikram S. Adve74825322002-03-18 03:15:35 +00001129 (*mvecI)->SetMachineOperandConst(offsetOpNum, offsetOpType,
1130 smallConstOffset);
Chris Lattner20b1ea02001-09-14 03:47:57 +00001131}
1132
1133
Chris Lattner20b1ea02001-09-14 03:47:57 +00001134//
1135// Substitute operand `operandNum' of the instruction in node `treeNode'
Vikram S. Advec025fc12001-10-14 23:28:43 +00001136// in place of the use(s) of that instruction in node `parent'.
1137// Check both explicit and implicit operands!
Vikram S. Adve74825322002-03-18 03:15:35 +00001138// Also make sure to skip over a parent who:
1139// (1) is a list node in the Burg tree, or
1140// (2) itself had its results forwarded to its parent
Chris Lattner20b1ea02001-09-14 03:47:57 +00001141//
1142static void
1143ForwardOperand(InstructionNode* treeNode,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001144 InstrTreeNode* parent,
1145 int operandNum)
Chris Lattner20b1ea02001-09-14 03:47:57 +00001146{
Vikram S. Adve243dd452001-09-18 13:03:13 +00001147 assert(treeNode && parent && "Invalid invocation of ForwardOperand");
1148
Chris Lattner20b1ea02001-09-14 03:47:57 +00001149 Instruction* unusedOp = treeNode->getInstruction();
1150 Value* fwdOp = unusedOp->getOperand(operandNum);
Vikram S. Adve243dd452001-09-18 13:03:13 +00001151
1152 // The parent itself may be a list node, so find the real parent instruction
1153 while (parent->getNodeType() != InstrTreeNode::NTInstructionNode)
1154 {
1155 parent = parent->parent();
1156 assert(parent && "ERROR: Non-instruction node has no parent in tree.");
1157 }
1158 InstructionNode* parentInstrNode = (InstructionNode*) parent;
1159
1160 Instruction* userInstr = parentInstrNode->getInstruction();
Chris Lattner9c461082002-02-03 07:50:56 +00001161 MachineCodeForInstruction &mvec = MachineCodeForInstruction::get(userInstr);
Vikram S. Adve74825322002-03-18 03:15:35 +00001162
1163 // The parent's mvec would be empty if it was itself forwarded.
1164 // Recursively call ForwardOperand in that case...
1165 //
1166 if (mvec.size() == 0)
Chris Lattner20b1ea02001-09-14 03:47:57 +00001167 {
Vikram S. Adve74825322002-03-18 03:15:35 +00001168 assert(parent->parent() != NULL &&
1169 "Parent could not have been forwarded, yet has no instructions?");
1170 ForwardOperand(treeNode, parent->parent(), operandNum);
1171 }
1172 else
1173 {
Vikram S. Adve74825322002-03-18 03:15:35 +00001174 for (unsigned i=0, N=mvec.size(); i < N; i++)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001175 {
Vikram S. Adve74825322002-03-18 03:15:35 +00001176 MachineInstr* minstr = mvec[i];
1177 for (unsigned i=0, numOps=minstr->getNumOperands(); i < numOps; ++i)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001178 {
Vikram S. Adve74825322002-03-18 03:15:35 +00001179 const MachineOperand& mop = minstr->getOperand(i);
1180 if (mop.getOperandType() == MachineOperand::MO_VirtualRegister &&
1181 mop.getVRegValue() == unusedOp)
Vikram S. Adve242a8082002-05-19 15:25:51 +00001182 minstr->SetMachineOperandVal(i,
Vikram S. Adve74825322002-03-18 03:15:35 +00001183 MachineOperand::MO_VirtualRegister, fwdOp);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001184 }
Vikram S. Adve74825322002-03-18 03:15:35 +00001185
1186 for (unsigned i=0,numOps=minstr->getNumImplicitRefs(); i<numOps; ++i)
1187 if (minstr->getImplicitRef(i) == unusedOp)
Vikram S. Adve242a8082002-05-19 15:25:51 +00001188 minstr->setImplicitRef(i, fwdOp,
Vikram S. Adve6418eac2002-07-08 23:30:14 +00001189 minstr->implicitRefIsDefined(i),
1190 minstr->implicitRefIsDefinedAndUsed(i));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001191 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00001192 }
1193}
1194
1195
Vikram S. Adve242a8082002-05-19 15:25:51 +00001196inline bool
1197AllUsesAreBranches(const Instruction* setccI)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001198{
Vikram S. Adve242a8082002-05-19 15:25:51 +00001199 for (Value::use_const_iterator UI=setccI->use_begin(), UE=setccI->use_end();
1200 UI != UE; ++UI)
1201 if (! isa<TmpInstruction>(*UI) // ignore tmp instructions here
1202 && cast<Instruction>(*UI)->getOpcode() != Instruction::Br)
1203 return false;
1204 return true;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001205}
1206
Vikram S. Advefb361122001-10-22 13:36:31 +00001207//******************* Externally Visible Functions *************************/
1208
Vikram S. Advefb361122001-10-22 13:36:31 +00001209//------------------------------------------------------------------------
1210// External Function: ThisIsAChainRule
1211//
1212// Purpose:
1213// Check if a given BURG rule is a chain rule.
1214//------------------------------------------------------------------------
1215
1216extern bool
1217ThisIsAChainRule(int eruleno)
1218{
1219 switch(eruleno)
1220 {
1221 case 111: // stmt: reg
Vikram S. Advefb361122001-10-22 13:36:31 +00001222 case 123:
1223 case 124:
1224 case 125:
1225 case 126:
1226 case 127:
1227 case 128:
1228 case 129:
1229 case 130:
1230 case 131:
1231 case 132:
1232 case 133:
1233 case 155:
1234 case 221:
1235 case 222:
1236 case 241:
1237 case 242:
1238 case 243:
1239 case 244:
Vikram S. Adve30a6f492002-08-22 02:56:10 +00001240 case 245:
Vikram S. Adve85e1e9c2002-04-01 20:28:48 +00001241 case 321:
Vikram S. Advefb361122001-10-22 13:36:31 +00001242 return true; break;
Vikram S. Adve30a6f492002-08-22 02:56:10 +00001243
Vikram S. Advefb361122001-10-22 13:36:31 +00001244 default:
1245 return false; break;
1246 }
1247}
Chris Lattner20b1ea02001-09-14 03:47:57 +00001248
1249
1250//------------------------------------------------------------------------
1251// External Function: GetInstructionsByRule
1252//
1253// Purpose:
1254// Choose machine instructions for the SPARC according to the
1255// patterns chosen by the BURG-generated parser.
1256//------------------------------------------------------------------------
1257
Vikram S. Adve74825322002-03-18 03:15:35 +00001258void
Chris Lattner20b1ea02001-09-14 03:47:57 +00001259GetInstructionsByRule(InstructionNode* subtreeRoot,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001260 int ruleForNode,
1261 short* nts,
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001262 TargetMachine &target,
Vikram S. Adve74825322002-03-18 03:15:35 +00001263 vector<MachineInstr*>& mvec)
Chris Lattner20b1ea02001-09-14 03:47:57 +00001264{
Chris Lattner20b1ea02001-09-14 03:47:57 +00001265 bool checkCast = false; // initialize here to use fall-through
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001266 bool maskUnsignedResult = false;
Chris Lattner20b1ea02001-09-14 03:47:57 +00001267 int nextRule;
1268 int forwardOperandNum = -1;
Vikram S. Adve74825322002-03-18 03:15:35 +00001269 unsigned int allocaSize = 0;
1270 MachineInstr* M, *M2;
1271 unsigned int L;
1272
1273 mvec.clear();
Chris Lattner20b1ea02001-09-14 03:47:57 +00001274
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001275 // If the code for this instruction was folded into the parent (user),
1276 // then do nothing!
1277 if (subtreeRoot->isFoldedIntoParent())
1278 return;
1279
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001280 //
1281 // Let's check for chain rules outside the switch so that we don't have
1282 // to duplicate the list of chain rule production numbers here again
1283 //
1284 if (ThisIsAChainRule(ruleForNode))
Chris Lattner20b1ea02001-09-14 03:47:57 +00001285 {
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001286 // Chain rules have a single nonterminal on the RHS.
1287 // Get the rule that matches the RHS non-terminal and use that instead.
1288 //
1289 assert(nts[0] && ! nts[1]
1290 && "A chain rule should have only one RHS non-terminal!");
1291 nextRule = burm_rule(subtreeRoot->state, nts[0]);
1292 nts = burm_nts[nextRule];
Vikram S. Adve74825322002-03-18 03:15:35 +00001293 GetInstructionsByRule(subtreeRoot, nextRule, nts, target, mvec);
Chris Lattner20b1ea02001-09-14 03:47:57 +00001294 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001295 else
Chris Lattner20b1ea02001-09-14 03:47:57 +00001296 {
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001297 switch(ruleForNode) {
1298 case 1: // stmt: Ret
1299 case 2: // stmt: RetValue(reg)
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001300 { // NOTE: Prepass of register allocation is responsible
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001301 // for moving return value to appropriate register.
1302 // Mark the return-address register as a hidden virtual reg.
Vikram S. Advea995e602001-10-11 04:23:19 +00001303 // Mark the return value register as an implicit ref of
1304 // the machine instruction.
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001305 // Finally put a NOP in the delay slot.
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001306 ReturnInst *returnInstr =
1307 cast<ReturnInst>(subtreeRoot->getInstruction());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001308 assert(returnInstr->getOpcode() == Instruction::Ret);
1309
Chris Lattner9c461082002-02-03 07:50:56 +00001310 Instruction* returnReg = new TmpInstruction(returnInstr);
1311 MachineCodeForInstruction::get(returnInstr).addTemp(returnReg);
Vikram S. Advefb361122001-10-22 13:36:31 +00001312
Vikram S. Adve74825322002-03-18 03:15:35 +00001313 M = new MachineInstr(JMPLRET);
1314 M->SetMachineOperandReg(0, MachineOperand::MO_VirtualRegister,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001315 returnReg);
Vikram S. Adve74825322002-03-18 03:15:35 +00001316 M->SetMachineOperandConst(1,MachineOperand::MO_SignExtendedImmed,
Chris Lattner697954c2002-01-20 22:54:45 +00001317 (int64_t)8);
Vikram S. Adve74825322002-03-18 03:15:35 +00001318 M->SetMachineOperandReg(2, target.getRegInfo().getZeroRegNum());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001319
Vikram S. Advea995e602001-10-11 04:23:19 +00001320 if (returnInstr->getReturnValue() != NULL)
Vikram S. Adve74825322002-03-18 03:15:35 +00001321 M->addImplicitRef(returnInstr->getReturnValue());
Vikram S. Advea995e602001-10-11 04:23:19 +00001322
Vikram S. Adve74825322002-03-18 03:15:35 +00001323 mvec.push_back(M);
1324 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001325
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001326 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001327 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001328
1329 case 3: // stmt: Store(reg,reg)
1330 case 4: // stmt: Store(reg,ptrreg)
Vikram S. Adve74825322002-03-18 03:15:35 +00001331 mvec.push_back(new MachineInstr(
1332 ChooseStoreInstruction(
1333 subtreeRoot->leftChild()->getValue()->getType())));
1334 SetOperandsForMemInstr(mvec, mvec.end()-1, subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001335 break;
Chris Lattner20b1ea02001-09-14 03:47:57 +00001336
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001337 case 5: // stmt: BrUncond
Vikram S. Adve74825322002-03-18 03:15:35 +00001338 M = new MachineInstr(BA);
Vikram S. Adve6418eac2002-07-08 23:30:14 +00001339 M->SetMachineOperandVal(0, MachineOperand::MO_PCRelativeDisp,
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001340 cast<BranchInst>(subtreeRoot->getInstruction())->getSuccessor(0));
Vikram S. Adve74825322002-03-18 03:15:35 +00001341 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001342
1343 // delay slot
Vikram S. Adve74825322002-03-18 03:15:35 +00001344 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001345 break;
Chris Lattner20b1ea02001-09-14 03:47:57 +00001346
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001347 case 206: // stmt: BrCond(setCCconst)
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001348 { // setCCconst => boolean was computed with `%b = setCC type reg1 const'
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001349 // If the constant is ZERO, we can use the branch-on-integer-register
1350 // instructions and avoid the SUBcc instruction entirely.
1351 // Otherwise this is just the same as case 5, so just fall through.
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001352 //
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001353 InstrTreeNode* constNode = subtreeRoot->leftChild()->rightChild();
1354 assert(constNode &&
1355 constNode->getNodeType() ==InstrTreeNode::NTConstNode);
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001356 Constant *constVal = cast<Constant>(constNode->getValue());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001357 bool isValidConst;
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001358
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001359 if ((constVal->getType()->isIntegral()
Chris Lattner9b625032002-05-06 16:15:30 +00001360 || isa<PointerType>(constVal->getType()))
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001361 && GetConstantValueAsSignedInt(constVal, isValidConst) == 0
1362 && isValidConst)
1363 {
1364 // That constant is a zero after all...
1365 // Use the left child of setCC as the first argument!
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001366 // Mark the setCC node so that no code is generated for it.
1367 InstructionNode* setCCNode = (InstructionNode*)
1368 subtreeRoot->leftChild();
1369 assert(setCCNode->getOpLabel() == SetCCOp);
1370 setCCNode->markFoldedIntoParent();
1371
1372 BranchInst* brInst=cast<BranchInst>(subtreeRoot->getInstruction());
1373
Vikram S. Adve74825322002-03-18 03:15:35 +00001374 M = new MachineInstr(ChooseBprInstruction(subtreeRoot));
1375 M->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister,
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001376 setCCNode->leftChild()->getValue());
Vikram S. Adve74825322002-03-18 03:15:35 +00001377 M->SetMachineOperandVal(1, MachineOperand::MO_PCRelativeDisp,
1378 brInst->getSuccessor(0));
1379 mvec.push_back(M);
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001380
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001381 // delay slot
Vikram S. Adve74825322002-03-18 03:15:35 +00001382 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001383
1384 // false branch
Vikram S. Adve74825322002-03-18 03:15:35 +00001385 M = new MachineInstr(BA);
Vikram S. Adve6418eac2002-07-08 23:30:14 +00001386 M->SetMachineOperandVal(0, MachineOperand::MO_PCRelativeDisp,
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001387 brInst->getSuccessor(1));
Vikram S. Adve74825322002-03-18 03:15:35 +00001388 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001389
1390 // delay slot
Vikram S. Adve74825322002-03-18 03:15:35 +00001391 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001392
1393 break;
1394 }
1395 // ELSE FALL THROUGH
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001396 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001397
Vikram S. Adve30a6f492002-08-22 02:56:10 +00001398 case 6: // stmt: BrCond(setCC)
1399 { // bool => boolean was computed with SetCC.
1400 // The branch to use depends on whether it is FP, signed, or unsigned.
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001401 // If it is an integer CC, we also need to find the unique
1402 // TmpInstruction representing that CC.
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001403 //
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001404 BranchInst* brInst = cast<BranchInst>(subtreeRoot->getInstruction());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001405 bool isFPBranch;
Vikram S. Adve74825322002-03-18 03:15:35 +00001406 M = new MachineInstr(ChooseBccInstruction(subtreeRoot, isFPBranch));
Vikram S. Adve30a6f492002-08-22 02:56:10 +00001407
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001408 Value* ccValue = GetTmpForCC(subtreeRoot->leftChild()->getValue(),
1409 brInst->getParent()->getParent(),
1410 isFPBranch? Type::FloatTy : Type::IntTy);
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001411
Vikram S. Adve74825322002-03-18 03:15:35 +00001412 M->SetMachineOperandVal(0, MachineOperand::MO_CCRegister, ccValue);
1413 M->SetMachineOperandVal(1, MachineOperand::MO_PCRelativeDisp,
1414 brInst->getSuccessor(0));
1415 mvec.push_back(M);
Vikram S. Adve30a6f492002-08-22 02:56:10 +00001416
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001417 // delay slot
Vikram S. Adve74825322002-03-18 03:15:35 +00001418 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adve30a6f492002-08-22 02:56:10 +00001419
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001420 // false branch
Vikram S. Adve74825322002-03-18 03:15:35 +00001421 M = new MachineInstr(BA);
Vikram S. Adve6418eac2002-07-08 23:30:14 +00001422 M->SetMachineOperandVal(0, MachineOperand::MO_PCRelativeDisp,
Vikram S. Adve74825322002-03-18 03:15:35 +00001423 brInst->getSuccessor(1));
1424 mvec.push_back(M);
Vikram S. Adve30a6f492002-08-22 02:56:10 +00001425
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001426 // delay slot
Vikram S. Adve74825322002-03-18 03:15:35 +00001427 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001428 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001429 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001430
1431 case 208: // stmt: BrCond(boolconst)
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001432 {
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001433 // boolconst => boolean is a constant; use BA to first or second label
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001434 Constant* constVal =
1435 cast<Constant>(subtreeRoot->leftChild()->getValue());
1436 unsigned dest = cast<ConstantBool>(constVal)->getValue()? 0 : 1;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001437
Vikram S. Adve74825322002-03-18 03:15:35 +00001438 M = new MachineInstr(BA);
Vikram S. Adve6418eac2002-07-08 23:30:14 +00001439 M->SetMachineOperandVal(0, MachineOperand::MO_PCRelativeDisp,
Chris Lattner35504202002-04-27 03:14:39 +00001440 cast<BranchInst>(subtreeRoot->getInstruction())->getSuccessor(dest));
Vikram S. Adve74825322002-03-18 03:15:35 +00001441 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001442
1443 // delay slot
Vikram S. Adve74825322002-03-18 03:15:35 +00001444 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001445 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001446 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001447
1448 case 8: // stmt: BrCond(boolreg)
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001449 { // boolreg => boolean is stored in an existing register.
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001450 // Just use the branch-on-integer-register instruction!
1451 //
Vikram S. Adve74825322002-03-18 03:15:35 +00001452 M = new MachineInstr(BRNZ);
1453 M->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001454 subtreeRoot->leftChild()->getValue());
Vikram S. Adve74825322002-03-18 03:15:35 +00001455 M->SetMachineOperandVal(1, MachineOperand::MO_PCRelativeDisp,
Chris Lattner35504202002-04-27 03:14:39 +00001456 cast<BranchInst>(subtreeRoot->getInstruction())->getSuccessor(0));
Vikram S. Adve74825322002-03-18 03:15:35 +00001457 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001458
1459 // delay slot
Vikram S. Adve74825322002-03-18 03:15:35 +00001460 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001461
1462 // false branch
Vikram S. Adve74825322002-03-18 03:15:35 +00001463 M = new MachineInstr(BA);
Vikram S. Adve6418eac2002-07-08 23:30:14 +00001464 M->SetMachineOperandVal(0, MachineOperand::MO_PCRelativeDisp,
Chris Lattner35504202002-04-27 03:14:39 +00001465 cast<BranchInst>(subtreeRoot->getInstruction())->getSuccessor(1));
Vikram S. Adve74825322002-03-18 03:15:35 +00001466 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001467
1468 // delay slot
Vikram S. Adve74825322002-03-18 03:15:35 +00001469 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001470 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001471 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00001472
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001473 case 9: // stmt: Switch(reg)
1474 assert(0 && "*** SWITCH instruction is not implemented yet.");
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001475 break;
Chris Lattner20b1ea02001-09-14 03:47:57 +00001476
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001477 case 10: // reg: VRegList(reg, reg)
1478 assert(0 && "VRegList should never be the topmost non-chain rule");
1479 break;
1480
Vikram S. Advece08e1d2002-08-15 14:17:37 +00001481 case 21: // bool: Not(bool,reg): Both these are implemented as:
1482 case 421: // reg: BNot(reg,reg): reg = reg XOR-NOT 0
1483 { // First find the unary operand. It may be left or right, usually right.
1484 Value* notArg = BinaryOperator::getNotArgument(
1485 cast<BinaryOperator>(subtreeRoot->getInstruction()));
1486 mvec.push_back(Create3OperandInstr_Reg(XNOR, notArg,
1487 target.getRegInfo().getZeroRegNum(),
1488 subtreeRoot->getValue()));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001489 break;
Vikram S. Advece08e1d2002-08-15 14:17:37 +00001490 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001491
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001492 case 22: // reg: ToBoolTy(reg):
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001493 {
1494 const Type* opType = subtreeRoot->leftChild()->getValue()->getType();
Chris Lattner9b625032002-05-06 16:15:30 +00001495 assert(opType->isIntegral() || isa<PointerType>(opType)
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001496 || opType == Type::BoolTy);
Vikram S. Adve74825322002-03-18 03:15:35 +00001497 forwardOperandNum = 0; // forward first operand to user
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001498 break;
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001499 }
1500
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001501 case 23: // reg: ToUByteTy(reg)
1502 case 25: // reg: ToUShortTy(reg)
1503 case 27: // reg: ToUIntTy(reg)
1504 case 29: // reg: ToULongTy(reg)
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001505 {
Vikram S. Adve242a8082002-05-19 15:25:51 +00001506 Instruction* destI = subtreeRoot->getInstruction();
1507 Value* opVal = subtreeRoot->leftChild()->getValue();
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001508 const Type* opType = subtreeRoot->leftChild()->getValue()->getType();
Vikram S. Adve1e606692002-07-31 21:01:34 +00001509 if (opType->isIntegral()
1510 || isa<PointerType>(opType)
1511 || opType == Type::BoolTy)
1512 {
1513 unsigned opSize = target.DataLayout.getTypeSize(opType);
1514 unsigned destSize = target.DataLayout.getTypeSize(destI->getType());
1515 if (opSize > destSize ||
1516 (opType->isSigned()
1517 && destSize < target.DataLayout.getIntegerRegize()))
1518 { // operand is larger than dest,
1519 // OR both are equal but smaller than the full register size
1520 // AND operand is signed, so it may have extra sign bits:
1521 // mask high bits using AND
1522 M = Create3OperandInstr(AND, opVal,
1523 ConstantUInt::get(Type::ULongTy,
1524 ((uint64_t) 1 << 8*destSize) - 1),
1525 destI);
1526 mvec.push_back(M);
1527 }
1528 else
1529 forwardOperandNum = 0; // forward first operand to user
Vikram S. Adve242a8082002-05-19 15:25:51 +00001530 }
Vikram S. Adve1e606692002-07-31 21:01:34 +00001531 else if (opType->isFloatingPoint())
1532 CreateCodeToConvertIntToFloat(target, opVal, destI, mvec,
1533 MachineCodeForInstruction::get(destI));
Vikram S. Adve242a8082002-05-19 15:25:51 +00001534 else
Vikram S. Adve1e606692002-07-31 21:01:34 +00001535 assert(0 && "Unrecognized operand type for convert-to-unsigned");
1536
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001537 break;
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001538 }
1539
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001540 case 24: // reg: ToSByteTy(reg)
1541 case 26: // reg: ToShortTy(reg)
1542 case 28: // reg: ToIntTy(reg)
1543 case 30: // reg: ToLongTy(reg)
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001544 {
Vikram S. Adve242a8082002-05-19 15:25:51 +00001545 Instruction* destI = subtreeRoot->getInstruction();
1546 Value* opVal = subtreeRoot->leftChild()->getValue();
1547 MachineCodeForInstruction& mcfi =MachineCodeForInstruction::get(destI);
Vikram S. Adve1e606692002-07-31 21:01:34 +00001548
Vikram S. Adve242a8082002-05-19 15:25:51 +00001549 const Type* opType = opVal->getType();
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001550 if (opType->isIntegral()
Chris Lattner9b625032002-05-06 16:15:30 +00001551 || isa<PointerType>(opType)
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001552 || opType == Type::BoolTy)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001553 {
Vikram S. Adve242a8082002-05-19 15:25:51 +00001554 // These operand types have the same format as the destination,
1555 // but may have different size: add sign bits or mask as needed.
1556 //
1557 const Type* destType = destI->getType();
1558 unsigned opSize = target.DataLayout.getTypeSize(opType);
1559 unsigned destSize = target.DataLayout.getTypeSize(destType);
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001560
1561 if (opSize < destSize ||
1562 (opSize == destSize &&
1563 opSize == target.DataLayout.getIntegerRegize()))
1564 { // operand is smaller or both operand and result fill register
1565 forwardOperandNum = 0; // forward first operand to user
Vikram S. Adve242a8082002-05-19 15:25:51 +00001566 }
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001567 else
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001568 { // need to mask (possibly) and then sign-extend (definitely)
1569 Value* srcForSignExt = opVal;
1570 unsigned srcSizeForSignExt = 8 * opSize;
1571 if (opSize > destSize)
1572 { // operand is larger than dest: mask high bits
1573 TmpInstruction *tmpI = new TmpInstruction(destType, opVal,
1574 destI, "maskHi");
1575 mcfi.addTemp(tmpI);
1576 M = Create3OperandInstr(AND, opVal,
1577 ConstantUInt::get(Type::ULongTy,
1578 ((uint64_t) 1 << 8*destSize)-1),
1579 tmpI);
1580 mvec.push_back(M);
1581 srcForSignExt = tmpI;
1582 srcSizeForSignExt = 8 * destSize;
1583 }
1584
1585 // sign-extend
1586 target.getInstrInfo().CreateSignExtensionInstructions(target, destI->getParent()->getParent(), srcForSignExt, srcSizeForSignExt, destI, mvec, mcfi);
1587 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001588 }
Vikram S. Adve1e606692002-07-31 21:01:34 +00001589 else if (opType->isFloatingPoint())
1590 CreateCodeToConvertIntToFloat(target, opVal, destI, mvec, mcfi);
1591 else
1592 assert(0 && "Unrecognized operand type for convert-to-signed");
1593
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001594 break;
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001595 }
1596
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001597 case 31: // reg: ToFloatTy(reg):
1598 case 32: // reg: ToDoubleTy(reg):
1599 case 232: // reg: ToDoubleTy(Constant):
1600
1601 // If this instruction has a parent (a user) in the tree
1602 // and the user is translated as an FsMULd instruction,
1603 // then the cast is unnecessary. So check that first.
1604 // In the future, we'll want to do the same for the FdMULq instruction,
1605 // so do the check here instead of only for ToFloatTy(reg).
1606 //
1607 if (subtreeRoot->parent() != NULL &&
Chris Lattner9c461082002-02-03 07:50:56 +00001608 MachineCodeForInstruction::get(((InstructionNode*)subtreeRoot->parent())->getInstruction())[0]->getOpCode() == FSMULD)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001609 {
Vikram S. Adve74825322002-03-18 03:15:35 +00001610 forwardOperandNum = 0; // forward first operand to user
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001611 }
1612 else
1613 {
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001614 Value* leftVal = subtreeRoot->leftChild()->getValue();
1615 const Type* opType = leftVal->getType();
Vikram S. Advedbc4fad2002-04-25 04:37:51 +00001616 MachineOpCode opCode=ChooseConvertToFloatInstr(
1617 subtreeRoot->getOpLabel(), opType);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001618 if (opCode == INVALID_OPCODE) // no conversion needed
1619 {
Vikram S. Adve74825322002-03-18 03:15:35 +00001620 forwardOperandNum = 0; // forward first operand to user
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001621 }
1622 else
1623 {
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001624 // If the source operand is a non-FP type it must be
1625 // first copied from int to float register via memory!
1626 Instruction *dest = subtreeRoot->getInstruction();
1627 Value* srcForCast;
1628 int n = 0;
Vikram S. Adve242a8082002-05-19 15:25:51 +00001629 if (! opType->isFloatingPoint())
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001630 {
1631 // Create a temporary to represent the FP register
1632 // into which the integer will be copied via memory.
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001633 // The type of this temporary will determine the FP
1634 // register used: single-prec for a 32-bit int or smaller,
1635 // double-prec for a 64-bit int.
1636 //
1637 const Type* srcTypeToUse =
1638 (leftVal->getType() == Type::LongTy)? Type::DoubleTy
1639 : Type::FloatTy;
1640
Chris Lattner9c461082002-02-03 07:50:56 +00001641 srcForCast = new TmpInstruction(srcTypeToUse, dest);
Vikram S. Advedbc4fad2002-04-25 04:37:51 +00001642 MachineCodeForInstruction &destMCFI =
Chris Lattner9c461082002-02-03 07:50:56 +00001643 MachineCodeForInstruction::get(dest);
Vikram S. Advedbc4fad2002-04-25 04:37:51 +00001644 destMCFI.addTemp(srcForCast);
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001645
Vikram S. Adve242a8082002-05-19 15:25:51 +00001646 target.getInstrInfo().CreateCodeToCopyIntToFloat(target,
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001647 dest->getParent()->getParent(),
1648 leftVal, (TmpInstruction*) srcForCast,
Vikram S. Adve242a8082002-05-19 15:25:51 +00001649 mvec, destMCFI);
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001650 }
1651 else
1652 srcForCast = leftVal;
1653
Vikram S. Adve74825322002-03-18 03:15:35 +00001654 M = new MachineInstr(opCode);
1655 M->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister,
1656 srcForCast);
1657 M->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister,
1658 dest);
1659 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001660 }
1661 }
1662 break;
1663
1664 case 19: // reg: ToArrayTy(reg):
1665 case 20: // reg: ToPointerTy(reg):
Vikram S. Adve74825322002-03-18 03:15:35 +00001666 forwardOperandNum = 0; // forward first operand to user
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001667 break;
1668
1669 case 233: // reg: Add(reg, Constant)
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001670 maskUnsignedResult = true;
Vikram S. Adve74825322002-03-18 03:15:35 +00001671 M = CreateAddConstInstruction(subtreeRoot);
1672 if (M != NULL)
1673 {
1674 mvec.push_back(M);
1675 break;
1676 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001677 // ELSE FALL THROUGH
Vikram S. Adve74825322002-03-18 03:15:35 +00001678
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001679 case 33: // reg: Add(reg, reg)
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001680 maskUnsignedResult = true;
Vikram S. Adve74825322002-03-18 03:15:35 +00001681 mvec.push_back(new MachineInstr(ChooseAddInstruction(subtreeRoot)));
1682 Set3OperandsFromInstr(mvec.back(), subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001683 break;
1684
1685 case 234: // reg: Sub(reg, Constant)
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001686 maskUnsignedResult = true;
Vikram S. Adve74825322002-03-18 03:15:35 +00001687 M = CreateSubConstInstruction(subtreeRoot);
1688 if (M != NULL)
1689 {
1690 mvec.push_back(M);
1691 break;
1692 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001693 // ELSE FALL THROUGH
Vikram S. Adve74825322002-03-18 03:15:35 +00001694
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001695 case 34: // reg: Sub(reg, reg)
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001696 maskUnsignedResult = true;
Vikram S. Adve74825322002-03-18 03:15:35 +00001697 mvec.push_back(new MachineInstr(ChooseSubInstructionByType(
1698 subtreeRoot->getInstruction()->getType())));
1699 Set3OperandsFromInstr(mvec.back(), subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001700 break;
1701
1702 case 135: // reg: Mul(todouble, todouble)
1703 checkCast = true;
1704 // FALL THROUGH
1705
1706 case 35: // reg: Mul(reg, reg)
Vikram S. Adve74825322002-03-18 03:15:35 +00001707 {
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001708 maskUnsignedResult = true;
Vikram S. Adve74825322002-03-18 03:15:35 +00001709 MachineOpCode forceOp = ((checkCast && BothFloatToDouble(subtreeRoot))
1710 ? FSMULD
1711 : INVALID_MACHINE_OPCODE);
Vikram S. Adve242a8082002-05-19 15:25:51 +00001712 Instruction* mulInstr = subtreeRoot->getInstruction();
1713 CreateMulInstruction(target, mulInstr->getParent()->getParent(),
Vikram S. Adve74825322002-03-18 03:15:35 +00001714 subtreeRoot->leftChild()->getValue(),
1715 subtreeRoot->rightChild()->getValue(),
Vikram S. Adve242a8082002-05-19 15:25:51 +00001716 mulInstr, mvec,
1717 MachineCodeForInstruction::get(mulInstr),forceOp);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001718 break;
Vikram S. Adve74825322002-03-18 03:15:35 +00001719 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001720 case 335: // reg: Mul(todouble, todoubleConst)
1721 checkCast = true;
1722 // FALL THROUGH
1723
1724 case 235: // reg: Mul(reg, Constant)
Vikram S. Adve74825322002-03-18 03:15:35 +00001725 {
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001726 maskUnsignedResult = true;
Vikram S. Adve74825322002-03-18 03:15:35 +00001727 MachineOpCode forceOp = ((checkCast && BothFloatToDouble(subtreeRoot))
1728 ? FSMULD
1729 : INVALID_MACHINE_OPCODE);
Vikram S. Adve242a8082002-05-19 15:25:51 +00001730 Instruction* mulInstr = subtreeRoot->getInstruction();
1731 CreateMulInstruction(target, mulInstr->getParent()->getParent(),
Vikram S. Adve74825322002-03-18 03:15:35 +00001732 subtreeRoot->leftChild()->getValue(),
1733 subtreeRoot->rightChild()->getValue(),
Vikram S. Adve242a8082002-05-19 15:25:51 +00001734 mulInstr, mvec,
1735 MachineCodeForInstruction::get(mulInstr),
1736 forceOp);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001737 break;
Vikram S. Adve74825322002-03-18 03:15:35 +00001738 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001739 case 236: // reg: Div(reg, Constant)
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001740 maskUnsignedResult = true;
Vikram S. Adve74825322002-03-18 03:15:35 +00001741 L = mvec.size();
1742 CreateDivConstInstruction(target, subtreeRoot, mvec);
1743 if (mvec.size() > L)
1744 break;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001745 // ELSE FALL THROUGH
Vikram S. Adve74825322002-03-18 03:15:35 +00001746
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001747 case 36: // reg: Div(reg, reg)
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001748 maskUnsignedResult = true;
Vikram S. Adve74825322002-03-18 03:15:35 +00001749 mvec.push_back(new MachineInstr(ChooseDivInstruction(target, subtreeRoot)));
1750 Set3OperandsFromInstr(mvec.back(), subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001751 break;
1752
1753 case 37: // reg: Rem(reg, reg)
1754 case 237: // reg: Rem(reg, Constant)
Vikram S. Adve510eec72001-11-04 21:59:14 +00001755 {
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001756 maskUnsignedResult = true;
Vikram S. Adve510eec72001-11-04 21:59:14 +00001757 Instruction* remInstr = subtreeRoot->getInstruction();
1758
Chris Lattner9c461082002-02-03 07:50:56 +00001759 TmpInstruction* quot = new TmpInstruction(
Vikram S. Adve510eec72001-11-04 21:59:14 +00001760 subtreeRoot->leftChild()->getValue(),
1761 subtreeRoot->rightChild()->getValue());
Chris Lattner9c461082002-02-03 07:50:56 +00001762 TmpInstruction* prod = new TmpInstruction(
Vikram S. Adve510eec72001-11-04 21:59:14 +00001763 quot,
1764 subtreeRoot->rightChild()->getValue());
Chris Lattner9c461082002-02-03 07:50:56 +00001765 MachineCodeForInstruction::get(remInstr).addTemp(quot).addTemp(prod);
Vikram S. Adve510eec72001-11-04 21:59:14 +00001766
Vikram S. Adve74825322002-03-18 03:15:35 +00001767 M = new MachineInstr(ChooseDivInstruction(target, subtreeRoot));
1768 Set3OperandsFromInstr(M, subtreeRoot, target);
1769 M->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister,quot);
1770 mvec.push_back(M);
Vikram S. Adve510eec72001-11-04 21:59:14 +00001771
Vikram S. Advece08e1d2002-08-15 14:17:37 +00001772 M = Create3OperandInstr(ChooseMulInstructionByType(
1773 subtreeRoot->getInstruction()->getType()),
1774 quot, subtreeRoot->rightChild()->getValue(),
1775 prod);
Vikram S. Adve74825322002-03-18 03:15:35 +00001776 mvec.push_back(M);
Vikram S. Adve510eec72001-11-04 21:59:14 +00001777
Vikram S. Adve74825322002-03-18 03:15:35 +00001778 M = new MachineInstr(ChooseSubInstructionByType(
Vikram S. Adve510eec72001-11-04 21:59:14 +00001779 subtreeRoot->getInstruction()->getType()));
Vikram S. Adve74825322002-03-18 03:15:35 +00001780 Set3OperandsFromInstr(M, subtreeRoot, target);
1781 M->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister,prod);
1782 mvec.push_back(M);
Vikram S. Adve510eec72001-11-04 21:59:14 +00001783
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001784 break;
Vikram S. Adve510eec72001-11-04 21:59:14 +00001785 }
1786
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001787 case 38: // bool: And(bool, bool)
1788 case 238: // bool: And(bool, boolconst)
1789 case 338: // reg : BAnd(reg, reg)
1790 case 538: // reg : BAnd(reg, Constant)
Vikram S. Adve74825322002-03-18 03:15:35 +00001791 mvec.push_back(new MachineInstr(AND));
1792 Set3OperandsFromInstr(mvec.back(), subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001793 break;
1794
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001795 case 138: // bool: And(bool, not)
Vikram S. Advece08e1d2002-08-15 14:17:37 +00001796 case 438: // bool: BAnd(bool, bnot)
1797 { // Use the argument of NOT as the second argument!
1798 // Mark the NOT node so that no code is generated for it.
1799 InstructionNode* notNode = (InstructionNode*) subtreeRoot->rightChild();
1800 Value* notArg = BinaryOperator::getNotArgument(
1801 cast<BinaryOperator>(notNode->getInstruction()));
1802 notNode->markFoldedIntoParent();
1803 mvec.push_back(Create3OperandInstr(ANDN,
1804 subtreeRoot->leftChild()->getValue(),
1805 notArg, subtreeRoot->getValue()));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001806 break;
Vikram S. Advece08e1d2002-08-15 14:17:37 +00001807 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001808
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001809 case 39: // bool: Or(bool, bool)
1810 case 239: // bool: Or(bool, boolconst)
1811 case 339: // reg : BOr(reg, reg)
1812 case 539: // reg : BOr(reg, Constant)
Vikram S. Adve242a8082002-05-19 15:25:51 +00001813 mvec.push_back(new MachineInstr(OR));
Vikram S. Adve74825322002-03-18 03:15:35 +00001814 Set3OperandsFromInstr(mvec.back(), subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001815 break;
1816
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001817 case 139: // bool: Or(bool, not)
Vikram S. Advece08e1d2002-08-15 14:17:37 +00001818 case 439: // bool: BOr(bool, bnot)
1819 { // Use the argument of NOT as the second argument!
1820 // Mark the NOT node so that no code is generated for it.
1821 InstructionNode* notNode = (InstructionNode*) subtreeRoot->rightChild();
1822 Value* notArg = BinaryOperator::getNotArgument(
1823 cast<BinaryOperator>(notNode->getInstruction()));
1824 notNode->markFoldedIntoParent();
1825 mvec.push_back(Create3OperandInstr(ORN,
1826 subtreeRoot->leftChild()->getValue(),
1827 notArg, subtreeRoot->getValue()));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001828 break;
Vikram S. Advece08e1d2002-08-15 14:17:37 +00001829 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001830
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001831 case 40: // bool: Xor(bool, bool)
1832 case 240: // bool: Xor(bool, boolconst)
1833 case 340: // reg : BXor(reg, reg)
1834 case 540: // reg : BXor(reg, Constant)
Vikram S. Adve74825322002-03-18 03:15:35 +00001835 mvec.push_back(new MachineInstr(XOR));
1836 Set3OperandsFromInstr(mvec.back(), subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001837 break;
1838
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001839 case 140: // bool: Xor(bool, not)
Vikram S. Advece08e1d2002-08-15 14:17:37 +00001840 case 440: // bool: BXor(bool, bnot)
1841 { // Use the argument of NOT as the second argument!
1842 // Mark the NOT node so that no code is generated for it.
1843 InstructionNode* notNode = (InstructionNode*) subtreeRoot->rightChild();
1844 Value* notArg = BinaryOperator::getNotArgument(
1845 cast<BinaryOperator>(notNode->getInstruction()));
1846 notNode->markFoldedIntoParent();
1847 mvec.push_back(Create3OperandInstr(XNOR,
1848 subtreeRoot->leftChild()->getValue(),
1849 notArg, subtreeRoot->getValue()));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001850 break;
Vikram S. Advece08e1d2002-08-15 14:17:37 +00001851 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001852
1853 case 41: // boolconst: SetCC(reg, Constant)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001854 //
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001855 // If the SetCC was folded into the user (parent), it will be
1856 // caught above. All other cases are the same as case 42,
1857 // so just fall through.
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001858 //
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001859 case 42: // bool: SetCC(reg, reg):
1860 {
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001861 // This generates a SUBCC instruction, putting the difference in
1862 // a result register, and setting a condition code.
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001863 //
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001864 // If the boolean result of the SetCC is used by anything other
Vikram S. Adve6418eac2002-07-08 23:30:14 +00001865 // than a branch instruction, or if it is used outside the current
1866 // basic block, the boolean must be
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001867 // computed and stored in the result register. Otherwise, discard
1868 // the difference (by using %g0) and keep only the condition code.
1869 //
1870 // To compute the boolean result in a register we use a conditional
1871 // move, unless the result of the SUBCC instruction can be used as
1872 // the bool! This assumes that zero is FALSE and any non-zero
1873 // integer is TRUE.
1874 //
1875 InstructionNode* parentNode = (InstructionNode*) subtreeRoot->parent();
1876 Instruction* setCCInstr = subtreeRoot->getInstruction();
Vikram S. Adve242a8082002-05-19 15:25:51 +00001877
Vikram S. Adve6418eac2002-07-08 23:30:14 +00001878 bool keepBoolVal = parentNode == NULL ||
1879 ! AllUsesAreBranches(setCCInstr);
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001880 bool subValIsBoolVal = setCCInstr->getOpcode() == Instruction::SetNE;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001881 bool keepSubVal = keepBoolVal && subValIsBoolVal;
1882 bool computeBoolVal = keepBoolVal && ! subValIsBoolVal;
1883
1884 bool mustClearReg;
1885 int valueToMove;
Chris Lattner8e5c0b42001-11-07 14:01:59 +00001886 MachineOpCode movOpCode = 0;
Vikram S. Adve6418eac2002-07-08 23:30:14 +00001887
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001888 // Mark the 4th operand as being a CC register, and as a def
1889 // A TmpInstruction is created to represent the CC "result".
1890 // Unlike other instances of TmpInstruction, this one is used
1891 // by machine code of multiple LLVM instructions, viz.,
1892 // the SetCC and the branch. Make sure to get the same one!
1893 // Note that we do this even for FP CC registers even though they
1894 // are explicit operands, because the type of the operand
1895 // needs to be a floating point condition code, not an integer
1896 // condition code. Think of this as casting the bool result to
1897 // a FP condition code register.
1898 //
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001899 Value* leftVal = subtreeRoot->leftChild()->getValue();
Chris Lattner9b625032002-05-06 16:15:30 +00001900 bool isFPCompare = leftVal->getType()->isFloatingPoint();
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001901
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001902 TmpInstruction* tmpForCC = GetTmpForCC(setCCInstr,
1903 setCCInstr->getParent()->getParent(),
Chris Lattner9b625032002-05-06 16:15:30 +00001904 isFPCompare ? Type::FloatTy : Type::IntTy);
Chris Lattner9c461082002-02-03 07:50:56 +00001905 MachineCodeForInstruction::get(setCCInstr).addTemp(tmpForCC);
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001906
1907 if (! isFPCompare)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001908 {
1909 // Integer condition: dest. should be %g0 or an integer register.
1910 // If result must be saved but condition is not SetEQ then we need
1911 // a separate instruction to compute the bool result, so discard
1912 // result of SUBcc instruction anyway.
1913 //
Vikram S. Adve74825322002-03-18 03:15:35 +00001914 M = new MachineInstr(SUBcc);
1915 Set3OperandsFromInstr(M, subtreeRoot, target, ! keepSubVal);
1916 M->SetMachineOperandVal(3, MachineOperand::MO_CCRegister,
1917 tmpForCC, /*def*/true);
1918 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001919
1920 if (computeBoolVal)
1921 { // recompute bool using the integer condition codes
1922 movOpCode =
1923 ChooseMovpccAfterSub(subtreeRoot,mustClearReg,valueToMove);
1924 }
1925 }
1926 else
1927 {
1928 // FP condition: dest of FCMP should be some FCCn register
Vikram S. Adve74825322002-03-18 03:15:35 +00001929 M = new MachineInstr(ChooseFcmpInstruction(subtreeRoot));
1930 M->SetMachineOperandVal(0, MachineOperand::MO_CCRegister,
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001931 tmpForCC);
Vikram S. Adve74825322002-03-18 03:15:35 +00001932 M->SetMachineOperandVal(1,MachineOperand::MO_VirtualRegister,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001933 subtreeRoot->leftChild()->getValue());
Vikram S. Adve74825322002-03-18 03:15:35 +00001934 M->SetMachineOperandVal(2,MachineOperand::MO_VirtualRegister,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001935 subtreeRoot->rightChild()->getValue());
Vikram S. Adve74825322002-03-18 03:15:35 +00001936 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001937
1938 if (computeBoolVal)
1939 {// recompute bool using the FP condition codes
1940 mustClearReg = true;
1941 valueToMove = 1;
1942 movOpCode = ChooseMovFpccInstruction(subtreeRoot);
1943 }
1944 }
1945
1946 if (computeBoolVal)
1947 {
1948 if (mustClearReg)
1949 {// Unconditionally set register to 0
Vikram S. Adve74825322002-03-18 03:15:35 +00001950 M = new MachineInstr(SETHI);
1951 M->SetMachineOperandConst(0,MachineOperand::MO_UnextendedImmed,
1952 (int64_t)0);
1953 M->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister,
1954 setCCInstr);
1955 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001956 }
1957
1958 // Now conditionally move `valueToMove' (0 or 1) into the register
Vikram S. Adve6418eac2002-07-08 23:30:14 +00001959 // Mark the register as a use (as well as a def) because the old
1960 // value should be retained if the condition is false.
Vikram S. Adve74825322002-03-18 03:15:35 +00001961 M = new MachineInstr(movOpCode);
1962 M->SetMachineOperandVal(0, MachineOperand::MO_CCRegister,
1963 tmpForCC);
1964 M->SetMachineOperandConst(1, MachineOperand::MO_UnextendedImmed,
1965 valueToMove);
1966 M->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister,
Vikram S. Adve6418eac2002-07-08 23:30:14 +00001967 setCCInstr, /*isDef*/ true,
1968 /*isDefAndUse*/ true);
Vikram S. Adve74825322002-03-18 03:15:35 +00001969 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001970 }
1971 break;
1972 }
1973
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001974 case 51: // reg: Load(reg)
1975 case 52: // reg: Load(ptrreg)
1976 case 53: // reg: LoadIdx(reg,reg)
1977 case 54: // reg: LoadIdx(ptrreg,reg)
Vikram S. Adve74825322002-03-18 03:15:35 +00001978 mvec.push_back(new MachineInstr(ChooseLoadInstruction(
1979 subtreeRoot->getValue()->getType())));
1980 SetOperandsForMemInstr(mvec, mvec.end()-1, subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001981 break;
1982
1983 case 55: // reg: GetElemPtr(reg)
1984 case 56: // reg: GetElemPtrIdx(reg,reg)
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001985 // If the GetElemPtr was folded into the user (parent), it will be
1986 // caught above. For other cases, we have to compute the address.
Vikram S. Adve74825322002-03-18 03:15:35 +00001987 mvec.push_back(new MachineInstr(ADD));
1988 SetOperandsForMemInstr(mvec, mvec.end()-1, subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001989 break;
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001990
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001991 case 57: // reg: Alloca: Implement as 1 instruction:
1992 { // add %fp, offsetFromFP -> result
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001993 AllocationInst* instr =
1994 cast<AllocationInst>(subtreeRoot->getInstruction());
1995 unsigned int tsize =
1996 target.findOptimalStorageSize(instr->getAllocatedType());
Vikram S. Adve74825322002-03-18 03:15:35 +00001997 assert(tsize != 0);
1998 CreateCodeForFixedSizeAlloca(target, instr, tsize, 1, mvec);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001999 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00002000 }
Vikram S. Adve74825322002-03-18 03:15:35 +00002001
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002002 case 58: // reg: Alloca(reg): Implement as 3 instructions:
2003 // mul num, typeSz -> tmp
2004 // sub %sp, tmp -> %sp
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00002005 { // add %sp, frameSizeBelowDynamicArea -> result
Vikram S. Advefd3900a2002-03-24 03:33:02 +00002006 AllocationInst* instr =
2007 cast<AllocationInst>(subtreeRoot->getInstruction());
Vikram S. Adve74825322002-03-18 03:15:35 +00002008 const Type* eltType = instr->getAllocatedType();
2009
Vikram S. Advefd3900a2002-03-24 03:33:02 +00002010 // If #elements is constant, use simpler code for fixed-size allocas
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002011 int tsize = (int) target.findOptimalStorageSize(eltType);
Vikram S. Advefd3900a2002-03-24 03:33:02 +00002012 Value* numElementsVal = NULL;
2013 bool isArray = instr->isArrayAllocation();
2014
2015 if (!isArray ||
2016 isa<Constant>(numElementsVal = instr->getArraySize()))
2017 { // total size is constant: generate code for fixed-size alloca
2018 unsigned int numElements = isArray?
2019 cast<ConstantUInt>(numElementsVal)->getValue() : 1;
2020 CreateCodeForFixedSizeAlloca(target, instr, tsize,
2021 numElements, mvec);
2022 }
Vikram S. Adve74825322002-03-18 03:15:35 +00002023 else // total size is not constant.
2024 CreateCodeForVariableSizeAlloca(target, instr, tsize,
Vikram S. Advefd3900a2002-03-24 03:33:02 +00002025 numElementsVal, mvec);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002026 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00002027 }
Vikram S. Adve74825322002-03-18 03:15:35 +00002028
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002029 case 61: // reg: Call
Vikram S. Advedbc4fad2002-04-25 04:37:51 +00002030 { // Generate a direct (CALL) or indirect (JMPL). depending
2031 // Mark the return-address register and the indirection
2032 // register (if any) as hidden virtual registers.
Vikram S. Advea995e602001-10-11 04:23:19 +00002033 // Also, mark the operands of the Call and return value (if
2034 // any) as implicit operands of the CALL machine instruction.
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00002035 //
Vikram S. Advedbc4fad2002-04-25 04:37:51 +00002036 // If this is a varargs function, floating point arguments
2037 // have to passed in integer registers so insert
2038 // copy-float-to-int instructions for each float operand.
2039 //
Chris Lattnerb00c5822001-10-02 03:41:24 +00002040 CallInst *callInstr = cast<CallInst>(subtreeRoot->getInstruction());
Chris Lattner749655f2001-10-13 06:54:30 +00002041 Value *callee = callInstr->getCalledValue();
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002042
Vikram S. Advea10d1a72002-03-31 19:07:35 +00002043 // Create hidden virtual register for return address, with type void*.
Vikram S. Adve242a8082002-05-19 15:25:51 +00002044 TmpInstruction* retAddrReg =
Vikram S. Advea10d1a72002-03-31 19:07:35 +00002045 new TmpInstruction(PointerType::get(Type::VoidTy), callInstr);
Chris Lattner9c461082002-02-03 07:50:56 +00002046 MachineCodeForInstruction::get(callInstr).addTemp(retAddrReg);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002047
Vikram S. Adveea21a6c2001-10-20 20:57:06 +00002048 // Generate the machine instruction and its operands.
2049 // Use CALL for direct function calls; this optimistically assumes
2050 // the PC-relative address fits in the CALL address field (22 bits).
2051 // Use JMPL for indirect calls.
2052 //
Chris Lattnerb0d04722002-03-26 17:58:12 +00002053 if (isa<Function>(callee))
Vikram S. Adveea21a6c2001-10-20 20:57:06 +00002054 { // direct function call
Vikram S. Adve74825322002-03-18 03:15:35 +00002055 M = new MachineInstr(CALL);
2056 M->SetMachineOperandVal(0, MachineOperand::MO_PCRelativeDisp,
2057 callee);
Vikram S. Adveea21a6c2001-10-20 20:57:06 +00002058 }
2059 else
2060 { // indirect function call
Vikram S. Adve74825322002-03-18 03:15:35 +00002061 M = new MachineInstr(JMPLCALL);
2062 M->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister,
2063 callee);
2064 M->SetMachineOperandConst(1, MachineOperand::MO_SignExtendedImmed,
2065 (int64_t) 0);
2066 M->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister,
2067 retAddrReg);
Vikram S. Adveea21a6c2001-10-20 20:57:06 +00002068 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002069
Vikram S. Adve74825322002-03-18 03:15:35 +00002070 mvec.push_back(M);
Vikram S. Advea10d1a72002-03-31 19:07:35 +00002071
Vikram S. Adve242a8082002-05-19 15:25:51 +00002072 const FunctionType* funcType =
2073 cast<FunctionType>(cast<PointerType>(callee->getType())
2074 ->getElementType());
2075 bool isVarArgs = funcType->isVarArg();
2076 bool noPrototype = isVarArgs && funcType->getNumParams() == 0;
Vikram S. Advedbc4fad2002-04-25 04:37:51 +00002077
Vikram S. Adve242a8082002-05-19 15:25:51 +00002078 // Use an annotation to pass information about call arguments
2079 // to the register allocator.
2080 CallArgsDescriptor* argDesc = new CallArgsDescriptor(callInstr,
2081 retAddrReg, isVarArgs, noPrototype);
2082 M->addAnnotation(argDesc);
Vikram S. Advea995e602001-10-11 04:23:19 +00002083
Vikram S. Adve242a8082002-05-19 15:25:51 +00002084 assert(callInstr->getOperand(0) == callee
2085 && "This is assumed in the loop below!");
2086
2087 for (unsigned i=1, N=callInstr->getNumOperands(); i < N; ++i)
2088 {
2089 Value* argVal = callInstr->getOperand(i);
2090 Instruction* intArgReg = NULL;
2091
2092 // Check for FP arguments to varargs functions.
2093 // Any such argument in the first $K$ args must be passed in an
2094 // integer register, where K = #integer argument registers.
2095 if (isVarArgs && argVal->getType()->isFloatingPoint())
2096 {
2097 // If it is a function with no prototype, pass value
2098 // as an FP value as well as a varargs value
2099 if (noPrototype)
2100 argDesc->getArgInfo(i-1).setUseFPArgReg();
2101
2102 // If this arg. is in the first $K$ regs, add a copy
2103 // float-to-int instruction to pass the value as an integer.
2104 if (i < target.getRegInfo().GetNumOfIntArgRegs())
2105 {
2106 MachineCodeForInstruction &destMCFI =
2107 MachineCodeForInstruction::get(callInstr);
2108 intArgReg = new TmpInstruction(Type::IntTy, argVal);
2109 destMCFI.addTemp(intArgReg);
2110
2111 vector<MachineInstr*> copyMvec;
2112 target.getInstrInfo().CreateCodeToCopyFloatToInt(target,
2113 callInstr->getParent()->getParent(),
2114 argVal, (TmpInstruction*) intArgReg,
2115 copyMvec, destMCFI);
2116 mvec.insert(mvec.begin(),copyMvec.begin(),copyMvec.end());
2117
2118 argDesc->getArgInfo(i-1).setUseIntArgReg();
2119 argDesc->getArgInfo(i-1).setArgCopy(intArgReg);
2120 }
2121 else
2122 // Cannot fit in first $K$ regs so pass the arg on the stack
2123 argDesc->getArgInfo(i-1).setUseStackSlot();
2124 }
2125
2126 if (intArgReg)
2127 mvec.back()->addImplicitRef(intArgReg);
2128
2129 mvec.back()->addImplicitRef(argVal);
2130 }
2131
2132 // Add the return value as an implicit ref. The call operands
2133 // were added above.
Vikram S. Adveea21a6c2001-10-20 20:57:06 +00002134 if (callInstr->getType() != Type::VoidTy)
Vikram S. Adve74825322002-03-18 03:15:35 +00002135 mvec.back()->addImplicitRef(callInstr, /*isDef*/ true);
Vikram S. Advea995e602001-10-11 04:23:19 +00002136
Vikram S. Adveea21a6c2001-10-20 20:57:06 +00002137 // For the CALL instruction, the ret. addr. reg. is also implicit
Chris Lattnerb0d04722002-03-26 17:58:12 +00002138 if (isa<Function>(callee))
Vikram S. Adve74825322002-03-18 03:15:35 +00002139 mvec.back()->addImplicitRef(retAddrReg, /*isDef*/ true);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002140
Vikram S. Adve74825322002-03-18 03:15:35 +00002141 // delay slot
2142 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002143 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00002144 }
Vikram S. Adve242a8082002-05-19 15:25:51 +00002145
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002146 case 62: // reg: Shl(reg, reg)
Vikram S. Adve242a8082002-05-19 15:25:51 +00002147 {
2148 Value* argVal1 = subtreeRoot->leftChild()->getValue();
2149 Value* argVal2 = subtreeRoot->rightChild()->getValue();
2150 Instruction* shlInstr = subtreeRoot->getInstruction();
2151
2152 const Type* opType = argVal1->getType();
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002153 assert(opType->isIntegral()
Vikram S. Adve242a8082002-05-19 15:25:51 +00002154 || opType == Type::BoolTy
2155 || isa<PointerType>(opType)&&"Shl unsupported for other types");
2156
2157 CreateShiftInstructions(target, shlInstr->getParent()->getParent(),
2158 (opType == Type::LongTy)? SLLX : SLL,
2159 argVal1, argVal2, 0, shlInstr, mvec,
2160 MachineCodeForInstruction::get(shlInstr));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002161 break;
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00002162 }
2163
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002164 case 63: // reg: Shr(reg, reg)
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00002165 { const Type* opType = subtreeRoot->leftChild()->getValue()->getType();
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002166 assert(opType->isIntegral()
Vikram S. Adve242a8082002-05-19 15:25:51 +00002167 || isa<PointerType>(opType)&&"Shr unsupported for other types");
Vikram S. Adve74825322002-03-18 03:15:35 +00002168 mvec.push_back(new MachineInstr((opType->isSigned()
2169 ? ((opType == Type::LongTy)? SRAX : SRA)
2170 : ((opType == Type::LongTy)? SRLX : SRL))));
2171 Set3OperandsFromInstr(mvec.back(), subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002172 break;
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00002173 }
2174
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002175 case 64: // reg: Phi(reg,reg)
Vikram S. Adve74825322002-03-18 03:15:35 +00002176 break; // don't forward the value
2177
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002178 case 71: // reg: VReg
2179 case 72: // reg: Constant
Vikram S. Adve74825322002-03-18 03:15:35 +00002180 break; // don't forward the value
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002181
2182 default:
2183 assert(0 && "Unrecognized BURG rule");
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002184 break;
2185 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00002186 }
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00002187
Chris Lattner20b1ea02001-09-14 03:47:57 +00002188 if (forwardOperandNum >= 0)
2189 { // We did not generate a machine instruction but need to use operand.
2190 // If user is in the same tree, replace Value in its machine operand.
2191 // If not, insert a copy instruction which should get coalesced away
2192 // by register allocation.
2193 if (subtreeRoot->parent() != NULL)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002194 ForwardOperand(subtreeRoot, subtreeRoot->parent(), forwardOperandNum);
Chris Lattner20b1ea02001-09-14 03:47:57 +00002195 else
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002196 {
Vikram S. Adve7fe27872001-10-18 00:26:20 +00002197 vector<MachineInstr*> minstrVec;
Vikram S. Adve242a8082002-05-19 15:25:51 +00002198 Instruction* instr = subtreeRoot->getInstruction();
2199 target.getInstrInfo().
2200 CreateCopyInstructionsByType(target,
2201 instr->getParent()->getParent(),
2202 instr->getOperand(forwardOperandNum),
2203 instr, minstrVec,
2204 MachineCodeForInstruction::get(instr));
Vikram S. Adve7fe27872001-10-18 00:26:20 +00002205 assert(minstrVec.size() > 0);
Vikram S. Adve74825322002-03-18 03:15:35 +00002206 mvec.insert(mvec.end(), minstrVec.begin(), minstrVec.end());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002207 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00002208 }
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00002209
2210 if (maskUnsignedResult)
2211 { // If result is unsigned and smaller than int reg size,
2212 // we need to clear high bits of result value.
2213 assert(forwardOperandNum < 0 && "Need mask but no instruction generated");
2214 Instruction* dest = subtreeRoot->getInstruction();
2215 if (! dest->getType()->isSigned())
2216 {
2217 unsigned destSize = target.DataLayout.getTypeSize(dest->getType());
2218 if (destSize < target.DataLayout.getIntegerRegize())
2219 { // Mask high bits. Use a TmpInstruction to represent the
2220 // intermediate result before masking. Since those instructions
2221 // have already been generated, go back and substitute tmpI
2222 // for dest in the result position of each one of them.
2223 TmpInstruction *tmpI = new TmpInstruction(dest->getType(), dest,
2224 NULL, "maskHi");
2225 MachineCodeForInstruction::get(dest).addTemp(tmpI);
2226
2227 for (unsigned i=0, N=mvec.size(); i < N; ++i)
2228 mvec[i]->substituteValue(dest, tmpI);
2229
2230 M = Create3OperandInstr(AND, tmpI,
2231 ConstantUInt::get(Type::ULongTy,
2232 ((uint64_t) 1 << 8*destSize) - 1),
2233 dest);
2234 mvec.push_back(M);
2235 }
2236 }
2237 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00002238}