blob: 8d3c443877abfb69388f752163cd7fe6d9177f88 [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{
Chris Lattner3bb8ad22002-08-22 23:37:24 +0000966 GetElementPtrInst* memInst =
967 cast<GetElementPtrInst>(vmInstrNode->getInstruction());
Chris Lattner20b1ea02001-09-14 03:47:57 +0000968
Vikram S. Adve242a8082002-05-19 15:25:51 +0000969 // Variables to hold the index vector and ptr value.
Chris Lattner20b1ea02001-09-14 03:47:57 +0000970 // The major work here is to extract these for all 3 instruction types
Vikram S. Adve242a8082002-05-19 15:25:51 +0000971 // and to try to fold chains of constant indices into a single offset.
972 // After that, we call SetMemOperands_Internal(), which creates the
973 // appropriate operands for the machine instruction.
Vikram S. Advea10d1a72002-03-31 19:07:35 +0000974 vector<Value*> idxVec;
Vikram S. Adve242a8082002-05-19 15:25:51 +0000975 bool allConstantIndices = true;
976 Value* ptrVal = memInst->getPointerOperand();
Vikram S. Adveed3fefb2002-08-03 13:48:21 +0000977
Vikram S. Advea10d1a72002-03-31 19:07:35 +0000978 // If there is a GetElemPtr instruction to fold in to this instr,
979 // it must be in the left child for Load and GetElemPtr, and in the
980 // right child for Store instructions.
Chris Lattner20b1ea02001-09-14 03:47:57 +0000981 InstrTreeNode* ptrChild = (vmInstrNode->getOpLabel() == Instruction::Store
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000982 ? vmInstrNode->rightChild()
983 : vmInstrNode->leftChild());
Vikram S. Adveed3fefb2002-08-03 13:48:21 +0000984
Vikram S. Adve242a8082002-05-19 15:25:51 +0000985 // Check if all indices are constant for this instruction
Chris Lattner3bb8ad22002-08-22 23:37:24 +0000986 for (User::op_iterator OI=memInst->idx_begin(),OE=memInst->idx_end();
Vikram S. Adveed3fefb2002-08-03 13:48:21 +0000987 allConstantIndices && OI != OE; ++OI)
988 if (! isa<Constant>(*OI))
989 allConstantIndices = false;
990
Vikram S. Adve242a8082002-05-19 15:25:51 +0000991 // If we have only constant indices, fold chains of constant indices
992 // in this and any preceding GetElemPtr instructions.
Vikram S. Adve99d4a382002-08-04 20:51:05 +0000993 bool foldedGEPs = false;
Vikram S. Adve242a8082002-05-19 15:25:51 +0000994 if (allConstantIndices &&
Vikram S. Adveed3fefb2002-08-03 13:48:21 +0000995 (ptrChild->getOpLabel() == Instruction::GetElementPtr ||
996 ptrChild->getOpLabel() == GetElemPtrIdx))
Vikram S. Adve99d4a382002-08-04 20:51:05 +0000997 if (Value* newPtr = FoldGetElemChain((InstructionNode*) ptrChild, idxVec)) {
Vikram S. Adveed3fefb2002-08-03 13:48:21 +0000998 ptrVal = newPtr;
Vikram S. Adve99d4a382002-08-04 20:51:05 +0000999 foldedGEPs = true;
1000 }
Vikram S. Adveed3fefb2002-08-03 13:48:21 +00001001
Vikram S. Adve242a8082002-05-19 15:25:51 +00001002 // Append the index vector of the current instruction, if any.
Vikram S. Adve99d4a382002-08-04 20:51:05 +00001003 // Skip the leading [0] index if preceding GEPs were folded into this.
1004 if (memInst->getNumIndices() > 0) {
1005 assert((!foldedGEPs || IsZero(*memInst->idx_begin())) && "1st index not 0");
Chris Lattner75ac4e52002-08-03 20:57:38 +00001006 idxVec.insert(idxVec.end(),
Vikram S. Adve99d4a382002-08-04 20:51:05 +00001007 memInst->idx_begin() + foldedGEPs, memInst->idx_end());
1008 }
Vikram S. Adveed3fefb2002-08-03 13:48:21 +00001009
Vikram S. Adve242a8082002-05-19 15:25:51 +00001010 // Now create the appropriate operands for the machine instruction
1011 SetMemOperands_Internal(mvec, mvecI, vmInstrNode,
1012 ptrVal, idxVec, allConstantIndices, target);
Chris Lattner20b1ea02001-09-14 03:47:57 +00001013}
1014
1015
Vikram S. Adve74825322002-03-18 03:15:35 +00001016// Generate the correct operands (and additional instructions if needed)
1017// for the given pointer and given index vector.
1018//
Chris Lattner20b1ea02001-09-14 03:47:57 +00001019static void
Vikram S. Adve74825322002-03-18 03:15:35 +00001020SetMemOperands_Internal(vector<MachineInstr*>& mvec,
1021 vector<MachineInstr*>::iterator mvecI,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001022 const InstructionNode* vmInstrNode,
1023 Value* ptrVal,
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001024 vector<Value*>& idxVec,
Vikram S. Adve242a8082002-05-19 15:25:51 +00001025 bool allConstantIndices,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001026 const TargetMachine& target)
Chris Lattner20b1ea02001-09-14 03:47:57 +00001027{
Chris Lattner3bb8ad22002-08-22 23:37:24 +00001028 GetElementPtrInst* memInst =
1029 cast<GetElementPtrInst>(vmInstrNode->getInstruction());
Chris Lattner20b1ea02001-09-14 03:47:57 +00001030
1031 // Initialize so we default to storing the offset in a register.
Chris Lattner8e5c0b42001-11-07 14:01:59 +00001032 int64_t smallConstOffset = 0;
Chris Lattner20b1ea02001-09-14 03:47:57 +00001033 Value* valueForRegOffset = NULL;
Vikram S. Adveed3fefb2002-08-03 13:48:21 +00001034 MachineOperand::MachineOperandType offsetOpType =
1035 MachineOperand::MO_VirtualRegister;
Chris Lattner20b1ea02001-09-14 03:47:57 +00001036
Vikram S. Adve74825322002-03-18 03:15:35 +00001037 // Check if there is an index vector and if so, compute the
1038 // right offset for structures and for arrays
Chris Lattner20b1ea02001-09-14 03:47:57 +00001039 //
Chris Lattner3bb8ad22002-08-22 23:37:24 +00001040 if (!idxVec.empty())
Chris Lattner20b1ea02001-09-14 03:47:57 +00001041 {
Vikram S. Adve74825322002-03-18 03:15:35 +00001042 const PointerType* ptrType = cast<PointerType>(ptrVal->getType());
Chris Lattner20b1ea02001-09-14 03:47:57 +00001043
Vikram S. Adve242a8082002-05-19 15:25:51 +00001044 // If all indices are constant, compute the combined offset directly.
1045 if (allConstantIndices)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001046 {
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001047 // Compute the offset value using the index vector. Create a
1048 // virtual reg. for it since it may not fit in the immed field.
Vikram S. Adve242a8082002-05-19 15:25:51 +00001049 uint64_t offset = target.DataLayout.getIndexedOffset(ptrType,idxVec);
1050 valueForRegOffset = ConstantSInt::get(Type::LongTy, offset);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001051 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00001052 else
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001053 {
Vikram S. Adve242a8082002-05-19 15:25:51 +00001054 // There is at least one non-constant offset. Therefore, this must
Vikram S. Adveed3fefb2002-08-03 13:48:21 +00001055 // be an array ref, and must have been lowered to a single non-zero
1056 // offset. (An extra leading zero offset, if any, can be ignored.)
1057 // Generate code sequence to compute address from index.
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001058 //
Chris Lattner75ac4e52002-08-03 20:57:38 +00001059 assert(idxVec.size() == 1U + IsZero(idxVec[0])
Vikram S. Adveed3fefb2002-08-03 13:48:21 +00001060 && "Array refs must be lowered before Instruction Selection");
1061
Chris Lattner75ac4e52002-08-03 20:57:38 +00001062 Value* idxVal = idxVec[IsZero(idxVec[0])];
Vikram S. Adveed3fefb2002-08-03 13:48:21 +00001063
1064 vector<MachineInstr*> mulVec;
1065 Instruction* addr = new TmpInstruction(Type::UIntTy, memInst);
1066 MachineCodeForInstruction::get(memInst).addTemp(addr);
1067
1068 // The call to getTypeSize() will fail if size is not constant.
1069 unsigned int eltSize =
1070 target.DataLayout.getTypeSize(ptrType->getElementType());
1071 assert(eltSize > 0 && "Invalid or non-const array element size");
1072 ConstantUInt* eltVal = ConstantUInt::get(Type::UIntTy, eltSize);
1073
1074 // CreateMulInstruction() folds constants intelligently enough.
1075 CreateMulInstruction(target,
1076 memInst->getParent()->getParent(),
1077 idxVal, /* lval, not likely const */
1078 eltVal, /* rval, likely constant */
1079 addr, /* result*/
1080 mulVec,
1081 MachineCodeForInstruction::get(memInst),
1082 INVALID_MACHINE_OPCODE);
1083
1084 // Insert mulVec[] before *mvecI in mvec[] and update mvecI
1085 // to point to the same instruction it pointed to before.
1086 assert(mulVec.size() > 0 && "No multiply code created?");
1087 vector<MachineInstr*>::iterator oldMvecI = mvecI;
1088 for (unsigned i=0, N=mulVec.size(); i < N; ++i)
1089 mvecI = mvec.insert(mvecI, mulVec[i]) + 1; // pts to mem instr
1090
1091 valueForRegOffset = addr;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001092 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00001093 }
1094 else
1095 {
1096 offsetOpType = MachineOperand::MO_SignExtendedImmed;
1097 smallConstOffset = 0;
1098 }
Vikram S. Adveed3fefb2002-08-03 13:48:21 +00001099
Vikram S. Advea10d1a72002-03-31 19:07:35 +00001100 // For STORE:
1101 // Operand 0 is value, operand 1 is ptr, operand 2 is offset
1102 // For LOAD or GET_ELEMENT_PTR,
1103 // Operand 0 is ptr, operand 1 is offset, operand 2 is result.
1104 //
1105 unsigned offsetOpNum, ptrOpNum;
1106 if (memInst->getOpcode() == Instruction::Store)
1107 {
1108 (*mvecI)->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister,
1109 vmInstrNode->leftChild()->getValue());
1110 ptrOpNum = 1;
1111 offsetOpNum = 2;
1112 }
1113 else
1114 {
1115 ptrOpNum = 0;
1116 offsetOpNum = 1;
1117 (*mvecI)->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister,
1118 memInst);
1119 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00001120
Vikram S. Advea10d1a72002-03-31 19:07:35 +00001121 (*mvecI)->SetMachineOperandVal(ptrOpNum, MachineOperand::MO_VirtualRegister,
1122 ptrVal);
1123
Chris Lattner20b1ea02001-09-14 03:47:57 +00001124 if (offsetOpType == MachineOperand::MO_VirtualRegister)
1125 {
1126 assert(valueForRegOffset != NULL);
Vikram S. Adve74825322002-03-18 03:15:35 +00001127 (*mvecI)->SetMachineOperandVal(offsetOpNum, offsetOpType,
1128 valueForRegOffset);
Chris Lattner20b1ea02001-09-14 03:47:57 +00001129 }
1130 else
Vikram S. Adve74825322002-03-18 03:15:35 +00001131 (*mvecI)->SetMachineOperandConst(offsetOpNum, offsetOpType,
1132 smallConstOffset);
Chris Lattner20b1ea02001-09-14 03:47:57 +00001133}
1134
1135
Chris Lattner20b1ea02001-09-14 03:47:57 +00001136//
1137// Substitute operand `operandNum' of the instruction in node `treeNode'
Vikram S. Advec025fc12001-10-14 23:28:43 +00001138// in place of the use(s) of that instruction in node `parent'.
1139// Check both explicit and implicit operands!
Vikram S. Adve74825322002-03-18 03:15:35 +00001140// Also make sure to skip over a parent who:
1141// (1) is a list node in the Burg tree, or
1142// (2) itself had its results forwarded to its parent
Chris Lattner20b1ea02001-09-14 03:47:57 +00001143//
1144static void
1145ForwardOperand(InstructionNode* treeNode,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001146 InstrTreeNode* parent,
1147 int operandNum)
Chris Lattner20b1ea02001-09-14 03:47:57 +00001148{
Vikram S. Adve243dd452001-09-18 13:03:13 +00001149 assert(treeNode && parent && "Invalid invocation of ForwardOperand");
1150
Chris Lattner20b1ea02001-09-14 03:47:57 +00001151 Instruction* unusedOp = treeNode->getInstruction();
1152 Value* fwdOp = unusedOp->getOperand(operandNum);
Vikram S. Adve243dd452001-09-18 13:03:13 +00001153
1154 // The parent itself may be a list node, so find the real parent instruction
1155 while (parent->getNodeType() != InstrTreeNode::NTInstructionNode)
1156 {
1157 parent = parent->parent();
1158 assert(parent && "ERROR: Non-instruction node has no parent in tree.");
1159 }
1160 InstructionNode* parentInstrNode = (InstructionNode*) parent;
1161
1162 Instruction* userInstr = parentInstrNode->getInstruction();
Chris Lattner9c461082002-02-03 07:50:56 +00001163 MachineCodeForInstruction &mvec = MachineCodeForInstruction::get(userInstr);
Vikram S. Adve74825322002-03-18 03:15:35 +00001164
1165 // The parent's mvec would be empty if it was itself forwarded.
1166 // Recursively call ForwardOperand in that case...
1167 //
1168 if (mvec.size() == 0)
Chris Lattner20b1ea02001-09-14 03:47:57 +00001169 {
Vikram S. Adve74825322002-03-18 03:15:35 +00001170 assert(parent->parent() != NULL &&
1171 "Parent could not have been forwarded, yet has no instructions?");
1172 ForwardOperand(treeNode, parent->parent(), operandNum);
1173 }
1174 else
1175 {
Vikram S. Adve74825322002-03-18 03:15:35 +00001176 for (unsigned i=0, N=mvec.size(); i < N; i++)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001177 {
Vikram S. Adve74825322002-03-18 03:15:35 +00001178 MachineInstr* minstr = mvec[i];
1179 for (unsigned i=0, numOps=minstr->getNumOperands(); i < numOps; ++i)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001180 {
Vikram S. Adve74825322002-03-18 03:15:35 +00001181 const MachineOperand& mop = minstr->getOperand(i);
1182 if (mop.getOperandType() == MachineOperand::MO_VirtualRegister &&
1183 mop.getVRegValue() == unusedOp)
Vikram S. Adve242a8082002-05-19 15:25:51 +00001184 minstr->SetMachineOperandVal(i,
Vikram S. Adve74825322002-03-18 03:15:35 +00001185 MachineOperand::MO_VirtualRegister, fwdOp);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001186 }
Vikram S. Adve74825322002-03-18 03:15:35 +00001187
1188 for (unsigned i=0,numOps=minstr->getNumImplicitRefs(); i<numOps; ++i)
1189 if (minstr->getImplicitRef(i) == unusedOp)
Vikram S. Adve242a8082002-05-19 15:25:51 +00001190 minstr->setImplicitRef(i, fwdOp,
Vikram S. Adve6418eac2002-07-08 23:30:14 +00001191 minstr->implicitRefIsDefined(i),
1192 minstr->implicitRefIsDefinedAndUsed(i));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001193 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00001194 }
1195}
1196
1197
Vikram S. Adve242a8082002-05-19 15:25:51 +00001198inline bool
1199AllUsesAreBranches(const Instruction* setccI)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001200{
Vikram S. Adve242a8082002-05-19 15:25:51 +00001201 for (Value::use_const_iterator UI=setccI->use_begin(), UE=setccI->use_end();
1202 UI != UE; ++UI)
1203 if (! isa<TmpInstruction>(*UI) // ignore tmp instructions here
1204 && cast<Instruction>(*UI)->getOpcode() != Instruction::Br)
1205 return false;
1206 return true;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001207}
1208
Vikram S. Advefb361122001-10-22 13:36:31 +00001209//******************* Externally Visible Functions *************************/
1210
Vikram S. Advefb361122001-10-22 13:36:31 +00001211//------------------------------------------------------------------------
1212// External Function: ThisIsAChainRule
1213//
1214// Purpose:
1215// Check if a given BURG rule is a chain rule.
1216//------------------------------------------------------------------------
1217
1218extern bool
1219ThisIsAChainRule(int eruleno)
1220{
1221 switch(eruleno)
1222 {
1223 case 111: // stmt: reg
Vikram S. Advefb361122001-10-22 13:36:31 +00001224 case 123:
1225 case 124:
1226 case 125:
1227 case 126:
1228 case 127:
1229 case 128:
1230 case 129:
1231 case 130:
1232 case 131:
1233 case 132:
1234 case 133:
1235 case 155:
1236 case 221:
1237 case 222:
1238 case 241:
1239 case 242:
1240 case 243:
1241 case 244:
Vikram S. Adve30a6f492002-08-22 02:56:10 +00001242 case 245:
Vikram S. Adve85e1e9c2002-04-01 20:28:48 +00001243 case 321:
Vikram S. Advefb361122001-10-22 13:36:31 +00001244 return true; break;
Vikram S. Adve30a6f492002-08-22 02:56:10 +00001245
Vikram S. Advefb361122001-10-22 13:36:31 +00001246 default:
1247 return false; break;
1248 }
1249}
Chris Lattner20b1ea02001-09-14 03:47:57 +00001250
1251
1252//------------------------------------------------------------------------
1253// External Function: GetInstructionsByRule
1254//
1255// Purpose:
1256// Choose machine instructions for the SPARC according to the
1257// patterns chosen by the BURG-generated parser.
1258//------------------------------------------------------------------------
1259
Vikram S. Adve74825322002-03-18 03:15:35 +00001260void
Chris Lattner20b1ea02001-09-14 03:47:57 +00001261GetInstructionsByRule(InstructionNode* subtreeRoot,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001262 int ruleForNode,
1263 short* nts,
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001264 TargetMachine &target,
Vikram S. Adve74825322002-03-18 03:15:35 +00001265 vector<MachineInstr*>& mvec)
Chris Lattner20b1ea02001-09-14 03:47:57 +00001266{
Chris Lattner20b1ea02001-09-14 03:47:57 +00001267 bool checkCast = false; // initialize here to use fall-through
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001268 bool maskUnsignedResult = false;
Chris Lattner20b1ea02001-09-14 03:47:57 +00001269 int nextRule;
1270 int forwardOperandNum = -1;
Vikram S. Adve74825322002-03-18 03:15:35 +00001271 unsigned int allocaSize = 0;
1272 MachineInstr* M, *M2;
1273 unsigned int L;
1274
1275 mvec.clear();
Chris Lattner20b1ea02001-09-14 03:47:57 +00001276
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001277 // If the code for this instruction was folded into the parent (user),
1278 // then do nothing!
1279 if (subtreeRoot->isFoldedIntoParent())
1280 return;
1281
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001282 //
1283 // Let's check for chain rules outside the switch so that we don't have
1284 // to duplicate the list of chain rule production numbers here again
1285 //
1286 if (ThisIsAChainRule(ruleForNode))
Chris Lattner20b1ea02001-09-14 03:47:57 +00001287 {
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001288 // Chain rules have a single nonterminal on the RHS.
1289 // Get the rule that matches the RHS non-terminal and use that instead.
1290 //
1291 assert(nts[0] && ! nts[1]
1292 && "A chain rule should have only one RHS non-terminal!");
1293 nextRule = burm_rule(subtreeRoot->state, nts[0]);
1294 nts = burm_nts[nextRule];
Vikram S. Adve74825322002-03-18 03:15:35 +00001295 GetInstructionsByRule(subtreeRoot, nextRule, nts, target, mvec);
Chris Lattner20b1ea02001-09-14 03:47:57 +00001296 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001297 else
Chris Lattner20b1ea02001-09-14 03:47:57 +00001298 {
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001299 switch(ruleForNode) {
1300 case 1: // stmt: Ret
1301 case 2: // stmt: RetValue(reg)
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001302 { // NOTE: Prepass of register allocation is responsible
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001303 // for moving return value to appropriate register.
1304 // Mark the return-address register as a hidden virtual reg.
Vikram S. Advea995e602001-10-11 04:23:19 +00001305 // Mark the return value register as an implicit ref of
1306 // the machine instruction.
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001307 // Finally put a NOP in the delay slot.
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001308 ReturnInst *returnInstr =
1309 cast<ReturnInst>(subtreeRoot->getInstruction());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001310 assert(returnInstr->getOpcode() == Instruction::Ret);
1311
Chris Lattner9c461082002-02-03 07:50:56 +00001312 Instruction* returnReg = new TmpInstruction(returnInstr);
1313 MachineCodeForInstruction::get(returnInstr).addTemp(returnReg);
Vikram S. Advefb361122001-10-22 13:36:31 +00001314
Vikram S. Adve74825322002-03-18 03:15:35 +00001315 M = new MachineInstr(JMPLRET);
1316 M->SetMachineOperandReg(0, MachineOperand::MO_VirtualRegister,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001317 returnReg);
Vikram S. Adve74825322002-03-18 03:15:35 +00001318 M->SetMachineOperandConst(1,MachineOperand::MO_SignExtendedImmed,
Chris Lattner697954c2002-01-20 22:54:45 +00001319 (int64_t)8);
Vikram S. Adve74825322002-03-18 03:15:35 +00001320 M->SetMachineOperandReg(2, target.getRegInfo().getZeroRegNum());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001321
Vikram S. Advea995e602001-10-11 04:23:19 +00001322 if (returnInstr->getReturnValue() != NULL)
Vikram S. Adve74825322002-03-18 03:15:35 +00001323 M->addImplicitRef(returnInstr->getReturnValue());
Vikram S. Advea995e602001-10-11 04:23:19 +00001324
Vikram S. Adve74825322002-03-18 03:15:35 +00001325 mvec.push_back(M);
1326 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001327
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001328 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001329 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001330
1331 case 3: // stmt: Store(reg,reg)
1332 case 4: // stmt: Store(reg,ptrreg)
Vikram S. Adve74825322002-03-18 03:15:35 +00001333 mvec.push_back(new MachineInstr(
1334 ChooseStoreInstruction(
1335 subtreeRoot->leftChild()->getValue()->getType())));
1336 SetOperandsForMemInstr(mvec, mvec.end()-1, subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001337 break;
Chris Lattner20b1ea02001-09-14 03:47:57 +00001338
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001339 case 5: // stmt: BrUncond
Vikram S. Adve74825322002-03-18 03:15:35 +00001340 M = new MachineInstr(BA);
Vikram S. Adve6418eac2002-07-08 23:30:14 +00001341 M->SetMachineOperandVal(0, MachineOperand::MO_PCRelativeDisp,
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001342 cast<BranchInst>(subtreeRoot->getInstruction())->getSuccessor(0));
Vikram S. Adve74825322002-03-18 03:15:35 +00001343 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001344
1345 // delay slot
Vikram S. Adve74825322002-03-18 03:15:35 +00001346 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001347 break;
Chris Lattner20b1ea02001-09-14 03:47:57 +00001348
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001349 case 206: // stmt: BrCond(setCCconst)
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001350 { // setCCconst => boolean was computed with `%b = setCC type reg1 const'
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001351 // If the constant is ZERO, we can use the branch-on-integer-register
1352 // instructions and avoid the SUBcc instruction entirely.
1353 // Otherwise this is just the same as case 5, so just fall through.
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001354 //
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001355 InstrTreeNode* constNode = subtreeRoot->leftChild()->rightChild();
1356 assert(constNode &&
1357 constNode->getNodeType() ==InstrTreeNode::NTConstNode);
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001358 Constant *constVal = cast<Constant>(constNode->getValue());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001359 bool isValidConst;
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001360
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001361 if ((constVal->getType()->isIntegral()
Chris Lattner9b625032002-05-06 16:15:30 +00001362 || isa<PointerType>(constVal->getType()))
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001363 && GetConstantValueAsSignedInt(constVal, isValidConst) == 0
1364 && isValidConst)
1365 {
1366 // That constant is a zero after all...
1367 // Use the left child of setCC as the first argument!
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001368 // Mark the setCC node so that no code is generated for it.
1369 InstructionNode* setCCNode = (InstructionNode*)
1370 subtreeRoot->leftChild();
1371 assert(setCCNode->getOpLabel() == SetCCOp);
1372 setCCNode->markFoldedIntoParent();
1373
1374 BranchInst* brInst=cast<BranchInst>(subtreeRoot->getInstruction());
1375
Vikram S. Adve74825322002-03-18 03:15:35 +00001376 M = new MachineInstr(ChooseBprInstruction(subtreeRoot));
1377 M->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister,
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001378 setCCNode->leftChild()->getValue());
Vikram S. Adve74825322002-03-18 03:15:35 +00001379 M->SetMachineOperandVal(1, MachineOperand::MO_PCRelativeDisp,
1380 brInst->getSuccessor(0));
1381 mvec.push_back(M);
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001382
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001383 // delay slot
Vikram S. Adve74825322002-03-18 03:15:35 +00001384 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001385
1386 // false branch
Vikram S. Adve74825322002-03-18 03:15:35 +00001387 M = new MachineInstr(BA);
Vikram S. Adve6418eac2002-07-08 23:30:14 +00001388 M->SetMachineOperandVal(0, MachineOperand::MO_PCRelativeDisp,
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001389 brInst->getSuccessor(1));
Vikram S. Adve74825322002-03-18 03:15:35 +00001390 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001391
1392 // delay slot
Vikram S. Adve74825322002-03-18 03:15:35 +00001393 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001394
1395 break;
1396 }
1397 // ELSE FALL THROUGH
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001398 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001399
Vikram S. Adve30a6f492002-08-22 02:56:10 +00001400 case 6: // stmt: BrCond(setCC)
1401 { // bool => boolean was computed with SetCC.
1402 // The branch to use depends on whether it is FP, signed, or unsigned.
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001403 // If it is an integer CC, we also need to find the unique
1404 // TmpInstruction representing that CC.
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001405 //
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001406 BranchInst* brInst = cast<BranchInst>(subtreeRoot->getInstruction());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001407 bool isFPBranch;
Vikram S. Adve74825322002-03-18 03:15:35 +00001408 M = new MachineInstr(ChooseBccInstruction(subtreeRoot, isFPBranch));
Vikram S. Adve30a6f492002-08-22 02:56:10 +00001409
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001410 Value* ccValue = GetTmpForCC(subtreeRoot->leftChild()->getValue(),
1411 brInst->getParent()->getParent(),
1412 isFPBranch? Type::FloatTy : Type::IntTy);
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001413
Vikram S. Adve74825322002-03-18 03:15:35 +00001414 M->SetMachineOperandVal(0, MachineOperand::MO_CCRegister, ccValue);
1415 M->SetMachineOperandVal(1, MachineOperand::MO_PCRelativeDisp,
1416 brInst->getSuccessor(0));
1417 mvec.push_back(M);
Vikram S. Adve30a6f492002-08-22 02:56:10 +00001418
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001419 // delay slot
Vikram S. Adve74825322002-03-18 03:15:35 +00001420 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adve30a6f492002-08-22 02:56:10 +00001421
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001422 // false branch
Vikram S. Adve74825322002-03-18 03:15:35 +00001423 M = new MachineInstr(BA);
Vikram S. Adve6418eac2002-07-08 23:30:14 +00001424 M->SetMachineOperandVal(0, MachineOperand::MO_PCRelativeDisp,
Vikram S. Adve74825322002-03-18 03:15:35 +00001425 brInst->getSuccessor(1));
1426 mvec.push_back(M);
Vikram S. Adve30a6f492002-08-22 02:56:10 +00001427
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001428 // delay slot
Vikram S. Adve74825322002-03-18 03:15:35 +00001429 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001430 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001431 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001432
1433 case 208: // stmt: BrCond(boolconst)
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001434 {
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001435 // boolconst => boolean is a constant; use BA to first or second label
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001436 Constant* constVal =
1437 cast<Constant>(subtreeRoot->leftChild()->getValue());
1438 unsigned dest = cast<ConstantBool>(constVal)->getValue()? 0 : 1;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001439
Vikram S. Adve74825322002-03-18 03:15:35 +00001440 M = new MachineInstr(BA);
Vikram S. Adve6418eac2002-07-08 23:30:14 +00001441 M->SetMachineOperandVal(0, MachineOperand::MO_PCRelativeDisp,
Chris Lattner35504202002-04-27 03:14:39 +00001442 cast<BranchInst>(subtreeRoot->getInstruction())->getSuccessor(dest));
Vikram S. Adve74825322002-03-18 03:15:35 +00001443 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001444
1445 // delay slot
Vikram S. Adve74825322002-03-18 03:15:35 +00001446 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001447 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001448 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001449
1450 case 8: // stmt: BrCond(boolreg)
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001451 { // boolreg => boolean is stored in an existing register.
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001452 // Just use the branch-on-integer-register instruction!
1453 //
Vikram S. Adve74825322002-03-18 03:15:35 +00001454 M = new MachineInstr(BRNZ);
1455 M->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001456 subtreeRoot->leftChild()->getValue());
Vikram S. Adve74825322002-03-18 03:15:35 +00001457 M->SetMachineOperandVal(1, MachineOperand::MO_PCRelativeDisp,
Chris Lattner35504202002-04-27 03:14:39 +00001458 cast<BranchInst>(subtreeRoot->getInstruction())->getSuccessor(0));
Vikram S. Adve74825322002-03-18 03:15:35 +00001459 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001460
1461 // delay slot
Vikram S. Adve74825322002-03-18 03:15:35 +00001462 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001463
1464 // false branch
Vikram S. Adve74825322002-03-18 03:15:35 +00001465 M = new MachineInstr(BA);
Vikram S. Adve6418eac2002-07-08 23:30:14 +00001466 M->SetMachineOperandVal(0, MachineOperand::MO_PCRelativeDisp,
Chris Lattner35504202002-04-27 03:14:39 +00001467 cast<BranchInst>(subtreeRoot->getInstruction())->getSuccessor(1));
Vikram S. Adve74825322002-03-18 03:15:35 +00001468 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001469
1470 // delay slot
Vikram S. Adve74825322002-03-18 03:15:35 +00001471 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001472 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001473 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00001474
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001475 case 9: // stmt: Switch(reg)
1476 assert(0 && "*** SWITCH instruction is not implemented yet.");
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001477 break;
Chris Lattner20b1ea02001-09-14 03:47:57 +00001478
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001479 case 10: // reg: VRegList(reg, reg)
1480 assert(0 && "VRegList should never be the topmost non-chain rule");
1481 break;
1482
Vikram S. Advece08e1d2002-08-15 14:17:37 +00001483 case 21: // bool: Not(bool,reg): Both these are implemented as:
1484 case 421: // reg: BNot(reg,reg): reg = reg XOR-NOT 0
1485 { // First find the unary operand. It may be left or right, usually right.
1486 Value* notArg = BinaryOperator::getNotArgument(
1487 cast<BinaryOperator>(subtreeRoot->getInstruction()));
1488 mvec.push_back(Create3OperandInstr_Reg(XNOR, notArg,
1489 target.getRegInfo().getZeroRegNum(),
1490 subtreeRoot->getValue()));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001491 break;
Vikram S. Advece08e1d2002-08-15 14:17:37 +00001492 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001493
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001494 case 22: // reg: ToBoolTy(reg):
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001495 {
1496 const Type* opType = subtreeRoot->leftChild()->getValue()->getType();
Chris Lattner9b625032002-05-06 16:15:30 +00001497 assert(opType->isIntegral() || isa<PointerType>(opType)
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001498 || opType == Type::BoolTy);
Vikram S. Adve74825322002-03-18 03:15:35 +00001499 forwardOperandNum = 0; // forward first operand to user
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001500 break;
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001501 }
1502
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001503 case 23: // reg: ToUByteTy(reg)
1504 case 25: // reg: ToUShortTy(reg)
1505 case 27: // reg: ToUIntTy(reg)
1506 case 29: // reg: ToULongTy(reg)
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001507 {
Vikram S. Adve242a8082002-05-19 15:25:51 +00001508 Instruction* destI = subtreeRoot->getInstruction();
1509 Value* opVal = subtreeRoot->leftChild()->getValue();
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001510 const Type* opType = subtreeRoot->leftChild()->getValue()->getType();
Vikram S. Adve1e606692002-07-31 21:01:34 +00001511 if (opType->isIntegral()
1512 || isa<PointerType>(opType)
1513 || opType == Type::BoolTy)
1514 {
1515 unsigned opSize = target.DataLayout.getTypeSize(opType);
1516 unsigned destSize = target.DataLayout.getTypeSize(destI->getType());
1517 if (opSize > destSize ||
1518 (opType->isSigned()
1519 && destSize < target.DataLayout.getIntegerRegize()))
1520 { // operand is larger than dest,
1521 // OR both are equal but smaller than the full register size
1522 // AND operand is signed, so it may have extra sign bits:
1523 // mask high bits using AND
1524 M = Create3OperandInstr(AND, opVal,
1525 ConstantUInt::get(Type::ULongTy,
1526 ((uint64_t) 1 << 8*destSize) - 1),
1527 destI);
1528 mvec.push_back(M);
1529 }
1530 else
1531 forwardOperandNum = 0; // forward first operand to user
Vikram S. Adve242a8082002-05-19 15:25:51 +00001532 }
Vikram S. Adve1e606692002-07-31 21:01:34 +00001533 else if (opType->isFloatingPoint())
1534 CreateCodeToConvertIntToFloat(target, opVal, destI, mvec,
1535 MachineCodeForInstruction::get(destI));
Vikram S. Adve242a8082002-05-19 15:25:51 +00001536 else
Vikram S. Adve1e606692002-07-31 21:01:34 +00001537 assert(0 && "Unrecognized operand type for convert-to-unsigned");
1538
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001539 break;
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001540 }
1541
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001542 case 24: // reg: ToSByteTy(reg)
1543 case 26: // reg: ToShortTy(reg)
1544 case 28: // reg: ToIntTy(reg)
1545 case 30: // reg: ToLongTy(reg)
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001546 {
Vikram S. Adve242a8082002-05-19 15:25:51 +00001547 Instruction* destI = subtreeRoot->getInstruction();
1548 Value* opVal = subtreeRoot->leftChild()->getValue();
1549 MachineCodeForInstruction& mcfi =MachineCodeForInstruction::get(destI);
Vikram S. Adve1e606692002-07-31 21:01:34 +00001550
Vikram S. Adve242a8082002-05-19 15:25:51 +00001551 const Type* opType = opVal->getType();
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001552 if (opType->isIntegral()
Chris Lattner9b625032002-05-06 16:15:30 +00001553 || isa<PointerType>(opType)
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001554 || opType == Type::BoolTy)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001555 {
Vikram S. Adve242a8082002-05-19 15:25:51 +00001556 // These operand types have the same format as the destination,
1557 // but may have different size: add sign bits or mask as needed.
1558 //
1559 const Type* destType = destI->getType();
1560 unsigned opSize = target.DataLayout.getTypeSize(opType);
1561 unsigned destSize = target.DataLayout.getTypeSize(destType);
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001562
1563 if (opSize < destSize ||
1564 (opSize == destSize &&
1565 opSize == target.DataLayout.getIntegerRegize()))
1566 { // operand is smaller or both operand and result fill register
1567 forwardOperandNum = 0; // forward first operand to user
Vikram S. Adve242a8082002-05-19 15:25:51 +00001568 }
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001569 else
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001570 { // need to mask (possibly) and then sign-extend (definitely)
1571 Value* srcForSignExt = opVal;
1572 unsigned srcSizeForSignExt = 8 * opSize;
1573 if (opSize > destSize)
1574 { // operand is larger than dest: mask high bits
1575 TmpInstruction *tmpI = new TmpInstruction(destType, opVal,
1576 destI, "maskHi");
1577 mcfi.addTemp(tmpI);
1578 M = Create3OperandInstr(AND, opVal,
1579 ConstantUInt::get(Type::ULongTy,
1580 ((uint64_t) 1 << 8*destSize)-1),
1581 tmpI);
1582 mvec.push_back(M);
1583 srcForSignExt = tmpI;
1584 srcSizeForSignExt = 8 * destSize;
1585 }
1586
1587 // sign-extend
1588 target.getInstrInfo().CreateSignExtensionInstructions(target, destI->getParent()->getParent(), srcForSignExt, srcSizeForSignExt, destI, mvec, mcfi);
1589 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001590 }
Vikram S. Adve1e606692002-07-31 21:01:34 +00001591 else if (opType->isFloatingPoint())
1592 CreateCodeToConvertIntToFloat(target, opVal, destI, mvec, mcfi);
1593 else
1594 assert(0 && "Unrecognized operand type for convert-to-signed");
1595
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001596 break;
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001597 }
1598
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001599 case 31: // reg: ToFloatTy(reg):
1600 case 32: // reg: ToDoubleTy(reg):
1601 case 232: // reg: ToDoubleTy(Constant):
1602
1603 // If this instruction has a parent (a user) in the tree
1604 // and the user is translated as an FsMULd instruction,
1605 // then the cast is unnecessary. So check that first.
1606 // In the future, we'll want to do the same for the FdMULq instruction,
1607 // so do the check here instead of only for ToFloatTy(reg).
1608 //
1609 if (subtreeRoot->parent() != NULL &&
Chris Lattner9c461082002-02-03 07:50:56 +00001610 MachineCodeForInstruction::get(((InstructionNode*)subtreeRoot->parent())->getInstruction())[0]->getOpCode() == FSMULD)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001611 {
Vikram S. Adve74825322002-03-18 03:15:35 +00001612 forwardOperandNum = 0; // forward first operand to user
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001613 }
1614 else
1615 {
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001616 Value* leftVal = subtreeRoot->leftChild()->getValue();
1617 const Type* opType = leftVal->getType();
Vikram S. Advedbc4fad2002-04-25 04:37:51 +00001618 MachineOpCode opCode=ChooseConvertToFloatInstr(
1619 subtreeRoot->getOpLabel(), opType);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001620 if (opCode == INVALID_OPCODE) // no conversion needed
1621 {
Vikram S. Adve74825322002-03-18 03:15:35 +00001622 forwardOperandNum = 0; // forward first operand to user
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001623 }
1624 else
1625 {
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001626 // If the source operand is a non-FP type it must be
1627 // first copied from int to float register via memory!
1628 Instruction *dest = subtreeRoot->getInstruction();
1629 Value* srcForCast;
1630 int n = 0;
Vikram S. Adve242a8082002-05-19 15:25:51 +00001631 if (! opType->isFloatingPoint())
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001632 {
1633 // Create a temporary to represent the FP register
1634 // into which the integer will be copied via memory.
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001635 // The type of this temporary will determine the FP
1636 // register used: single-prec for a 32-bit int or smaller,
1637 // double-prec for a 64-bit int.
1638 //
1639 const Type* srcTypeToUse =
1640 (leftVal->getType() == Type::LongTy)? Type::DoubleTy
1641 : Type::FloatTy;
1642
Chris Lattner9c461082002-02-03 07:50:56 +00001643 srcForCast = new TmpInstruction(srcTypeToUse, dest);
Vikram S. Advedbc4fad2002-04-25 04:37:51 +00001644 MachineCodeForInstruction &destMCFI =
Chris Lattner9c461082002-02-03 07:50:56 +00001645 MachineCodeForInstruction::get(dest);
Vikram S. Advedbc4fad2002-04-25 04:37:51 +00001646 destMCFI.addTemp(srcForCast);
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001647
Vikram S. Adve242a8082002-05-19 15:25:51 +00001648 target.getInstrInfo().CreateCodeToCopyIntToFloat(target,
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001649 dest->getParent()->getParent(),
1650 leftVal, (TmpInstruction*) srcForCast,
Vikram S. Adve242a8082002-05-19 15:25:51 +00001651 mvec, destMCFI);
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001652 }
1653 else
1654 srcForCast = leftVal;
1655
Vikram S. Adve74825322002-03-18 03:15:35 +00001656 M = new MachineInstr(opCode);
1657 M->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister,
1658 srcForCast);
1659 M->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister,
1660 dest);
1661 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001662 }
1663 }
1664 break;
1665
1666 case 19: // reg: ToArrayTy(reg):
1667 case 20: // reg: ToPointerTy(reg):
Vikram S. Adve74825322002-03-18 03:15:35 +00001668 forwardOperandNum = 0; // forward first operand to user
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001669 break;
1670
1671 case 233: // reg: Add(reg, Constant)
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001672 maskUnsignedResult = true;
Vikram S. Adve74825322002-03-18 03:15:35 +00001673 M = CreateAddConstInstruction(subtreeRoot);
1674 if (M != NULL)
1675 {
1676 mvec.push_back(M);
1677 break;
1678 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001679 // ELSE FALL THROUGH
Vikram S. Adve74825322002-03-18 03:15:35 +00001680
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001681 case 33: // reg: Add(reg, reg)
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001682 maskUnsignedResult = true;
Vikram S. Adve74825322002-03-18 03:15:35 +00001683 mvec.push_back(new MachineInstr(ChooseAddInstruction(subtreeRoot)));
1684 Set3OperandsFromInstr(mvec.back(), subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001685 break;
1686
1687 case 234: // reg: Sub(reg, Constant)
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001688 maskUnsignedResult = true;
Vikram S. Adve74825322002-03-18 03:15:35 +00001689 M = CreateSubConstInstruction(subtreeRoot);
1690 if (M != NULL)
1691 {
1692 mvec.push_back(M);
1693 break;
1694 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001695 // ELSE FALL THROUGH
Vikram S. Adve74825322002-03-18 03:15:35 +00001696
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001697 case 34: // reg: Sub(reg, reg)
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001698 maskUnsignedResult = true;
Vikram S. Adve74825322002-03-18 03:15:35 +00001699 mvec.push_back(new MachineInstr(ChooseSubInstructionByType(
1700 subtreeRoot->getInstruction()->getType())));
1701 Set3OperandsFromInstr(mvec.back(), subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001702 break;
1703
1704 case 135: // reg: Mul(todouble, todouble)
1705 checkCast = true;
1706 // FALL THROUGH
1707
1708 case 35: // reg: Mul(reg, reg)
Vikram S. Adve74825322002-03-18 03:15:35 +00001709 {
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001710 maskUnsignedResult = true;
Vikram S. Adve74825322002-03-18 03:15:35 +00001711 MachineOpCode forceOp = ((checkCast && BothFloatToDouble(subtreeRoot))
1712 ? FSMULD
1713 : INVALID_MACHINE_OPCODE);
Vikram S. Adve242a8082002-05-19 15:25:51 +00001714 Instruction* mulInstr = subtreeRoot->getInstruction();
1715 CreateMulInstruction(target, mulInstr->getParent()->getParent(),
Vikram S. Adve74825322002-03-18 03:15:35 +00001716 subtreeRoot->leftChild()->getValue(),
1717 subtreeRoot->rightChild()->getValue(),
Vikram S. Adve242a8082002-05-19 15:25:51 +00001718 mulInstr, mvec,
1719 MachineCodeForInstruction::get(mulInstr),forceOp);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001720 break;
Vikram S. Adve74825322002-03-18 03:15:35 +00001721 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001722 case 335: // reg: Mul(todouble, todoubleConst)
1723 checkCast = true;
1724 // FALL THROUGH
1725
1726 case 235: // reg: Mul(reg, Constant)
Vikram S. Adve74825322002-03-18 03:15:35 +00001727 {
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001728 maskUnsignedResult = true;
Vikram S. Adve74825322002-03-18 03:15:35 +00001729 MachineOpCode forceOp = ((checkCast && BothFloatToDouble(subtreeRoot))
1730 ? FSMULD
1731 : INVALID_MACHINE_OPCODE);
Vikram S. Adve242a8082002-05-19 15:25:51 +00001732 Instruction* mulInstr = subtreeRoot->getInstruction();
1733 CreateMulInstruction(target, mulInstr->getParent()->getParent(),
Vikram S. Adve74825322002-03-18 03:15:35 +00001734 subtreeRoot->leftChild()->getValue(),
1735 subtreeRoot->rightChild()->getValue(),
Vikram S. Adve242a8082002-05-19 15:25:51 +00001736 mulInstr, mvec,
1737 MachineCodeForInstruction::get(mulInstr),
1738 forceOp);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001739 break;
Vikram S. Adve74825322002-03-18 03:15:35 +00001740 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001741 case 236: // reg: Div(reg, Constant)
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001742 maskUnsignedResult = true;
Vikram S. Adve74825322002-03-18 03:15:35 +00001743 L = mvec.size();
1744 CreateDivConstInstruction(target, subtreeRoot, mvec);
1745 if (mvec.size() > L)
1746 break;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001747 // ELSE FALL THROUGH
Vikram S. Adve74825322002-03-18 03:15:35 +00001748
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001749 case 36: // reg: Div(reg, reg)
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001750 maskUnsignedResult = true;
Vikram S. Adve74825322002-03-18 03:15:35 +00001751 mvec.push_back(new MachineInstr(ChooseDivInstruction(target, subtreeRoot)));
1752 Set3OperandsFromInstr(mvec.back(), subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001753 break;
1754
1755 case 37: // reg: Rem(reg, reg)
1756 case 237: // reg: Rem(reg, Constant)
Vikram S. Adve510eec72001-11-04 21:59:14 +00001757 {
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001758 maskUnsignedResult = true;
Vikram S. Adve510eec72001-11-04 21:59:14 +00001759 Instruction* remInstr = subtreeRoot->getInstruction();
1760
Chris Lattner9c461082002-02-03 07:50:56 +00001761 TmpInstruction* quot = new TmpInstruction(
Vikram S. Adve510eec72001-11-04 21:59:14 +00001762 subtreeRoot->leftChild()->getValue(),
1763 subtreeRoot->rightChild()->getValue());
Chris Lattner9c461082002-02-03 07:50:56 +00001764 TmpInstruction* prod = new TmpInstruction(
Vikram S. Adve510eec72001-11-04 21:59:14 +00001765 quot,
1766 subtreeRoot->rightChild()->getValue());
Chris Lattner9c461082002-02-03 07:50:56 +00001767 MachineCodeForInstruction::get(remInstr).addTemp(quot).addTemp(prod);
Vikram S. Adve510eec72001-11-04 21:59:14 +00001768
Vikram S. Adve74825322002-03-18 03:15:35 +00001769 M = new MachineInstr(ChooseDivInstruction(target, subtreeRoot));
1770 Set3OperandsFromInstr(M, subtreeRoot, target);
1771 M->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister,quot);
1772 mvec.push_back(M);
Vikram S. Adve510eec72001-11-04 21:59:14 +00001773
Vikram S. Advece08e1d2002-08-15 14:17:37 +00001774 M = Create3OperandInstr(ChooseMulInstructionByType(
1775 subtreeRoot->getInstruction()->getType()),
1776 quot, subtreeRoot->rightChild()->getValue(),
1777 prod);
Vikram S. Adve74825322002-03-18 03:15:35 +00001778 mvec.push_back(M);
Vikram S. Adve510eec72001-11-04 21:59:14 +00001779
Vikram S. Adve74825322002-03-18 03:15:35 +00001780 M = new MachineInstr(ChooseSubInstructionByType(
Vikram S. Adve510eec72001-11-04 21:59:14 +00001781 subtreeRoot->getInstruction()->getType()));
Vikram S. Adve74825322002-03-18 03:15:35 +00001782 Set3OperandsFromInstr(M, subtreeRoot, target);
1783 M->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister,prod);
1784 mvec.push_back(M);
Vikram S. Adve510eec72001-11-04 21:59:14 +00001785
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001786 break;
Vikram S. Adve510eec72001-11-04 21:59:14 +00001787 }
1788
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001789 case 38: // bool: And(bool, bool)
1790 case 238: // bool: And(bool, boolconst)
1791 case 338: // reg : BAnd(reg, reg)
1792 case 538: // reg : BAnd(reg, Constant)
Vikram S. Adve74825322002-03-18 03:15:35 +00001793 mvec.push_back(new MachineInstr(AND));
1794 Set3OperandsFromInstr(mvec.back(), subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001795 break;
1796
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001797 case 138: // bool: And(bool, not)
Vikram S. Advece08e1d2002-08-15 14:17:37 +00001798 case 438: // bool: BAnd(bool, bnot)
1799 { // Use the argument of NOT as the second argument!
1800 // Mark the NOT node so that no code is generated for it.
1801 InstructionNode* notNode = (InstructionNode*) subtreeRoot->rightChild();
1802 Value* notArg = BinaryOperator::getNotArgument(
1803 cast<BinaryOperator>(notNode->getInstruction()));
1804 notNode->markFoldedIntoParent();
1805 mvec.push_back(Create3OperandInstr(ANDN,
1806 subtreeRoot->leftChild()->getValue(),
1807 notArg, subtreeRoot->getValue()));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001808 break;
Vikram S. Advece08e1d2002-08-15 14:17:37 +00001809 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001810
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001811 case 39: // bool: Or(bool, bool)
1812 case 239: // bool: Or(bool, boolconst)
1813 case 339: // reg : BOr(reg, reg)
1814 case 539: // reg : BOr(reg, Constant)
Vikram S. Adve242a8082002-05-19 15:25:51 +00001815 mvec.push_back(new MachineInstr(OR));
Vikram S. Adve74825322002-03-18 03:15:35 +00001816 Set3OperandsFromInstr(mvec.back(), subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001817 break;
1818
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001819 case 139: // bool: Or(bool, not)
Vikram S. Advece08e1d2002-08-15 14:17:37 +00001820 case 439: // bool: BOr(bool, bnot)
1821 { // Use the argument of NOT as the second argument!
1822 // Mark the NOT node so that no code is generated for it.
1823 InstructionNode* notNode = (InstructionNode*) subtreeRoot->rightChild();
1824 Value* notArg = BinaryOperator::getNotArgument(
1825 cast<BinaryOperator>(notNode->getInstruction()));
1826 notNode->markFoldedIntoParent();
1827 mvec.push_back(Create3OperandInstr(ORN,
1828 subtreeRoot->leftChild()->getValue(),
1829 notArg, subtreeRoot->getValue()));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001830 break;
Vikram S. Advece08e1d2002-08-15 14:17:37 +00001831 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001832
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001833 case 40: // bool: Xor(bool, bool)
1834 case 240: // bool: Xor(bool, boolconst)
1835 case 340: // reg : BXor(reg, reg)
1836 case 540: // reg : BXor(reg, Constant)
Vikram S. Adve74825322002-03-18 03:15:35 +00001837 mvec.push_back(new MachineInstr(XOR));
1838 Set3OperandsFromInstr(mvec.back(), subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001839 break;
1840
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001841 case 140: // bool: Xor(bool, not)
Vikram S. Advece08e1d2002-08-15 14:17:37 +00001842 case 440: // bool: BXor(bool, bnot)
1843 { // Use the argument of NOT as the second argument!
1844 // Mark the NOT node so that no code is generated for it.
1845 InstructionNode* notNode = (InstructionNode*) subtreeRoot->rightChild();
1846 Value* notArg = BinaryOperator::getNotArgument(
1847 cast<BinaryOperator>(notNode->getInstruction()));
1848 notNode->markFoldedIntoParent();
1849 mvec.push_back(Create3OperandInstr(XNOR,
1850 subtreeRoot->leftChild()->getValue(),
1851 notArg, subtreeRoot->getValue()));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001852 break;
Vikram S. Advece08e1d2002-08-15 14:17:37 +00001853 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001854
1855 case 41: // boolconst: SetCC(reg, Constant)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001856 //
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001857 // If the SetCC was folded into the user (parent), it will be
1858 // caught above. All other cases are the same as case 42,
1859 // so just fall through.
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001860 //
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001861 case 42: // bool: SetCC(reg, reg):
1862 {
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001863 // This generates a SUBCC instruction, putting the difference in
1864 // a result register, and setting a condition code.
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001865 //
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001866 // If the boolean result of the SetCC is used by anything other
Vikram S. Adve6418eac2002-07-08 23:30:14 +00001867 // than a branch instruction, or if it is used outside the current
1868 // basic block, the boolean must be
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001869 // computed and stored in the result register. Otherwise, discard
1870 // the difference (by using %g0) and keep only the condition code.
1871 //
1872 // To compute the boolean result in a register we use a conditional
1873 // move, unless the result of the SUBCC instruction can be used as
1874 // the bool! This assumes that zero is FALSE and any non-zero
1875 // integer is TRUE.
1876 //
1877 InstructionNode* parentNode = (InstructionNode*) subtreeRoot->parent();
1878 Instruction* setCCInstr = subtreeRoot->getInstruction();
Vikram S. Adve242a8082002-05-19 15:25:51 +00001879
Vikram S. Adve6418eac2002-07-08 23:30:14 +00001880 bool keepBoolVal = parentNode == NULL ||
1881 ! AllUsesAreBranches(setCCInstr);
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001882 bool subValIsBoolVal = setCCInstr->getOpcode() == Instruction::SetNE;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001883 bool keepSubVal = keepBoolVal && subValIsBoolVal;
1884 bool computeBoolVal = keepBoolVal && ! subValIsBoolVal;
1885
1886 bool mustClearReg;
1887 int valueToMove;
Chris Lattner8e5c0b42001-11-07 14:01:59 +00001888 MachineOpCode movOpCode = 0;
Vikram S. Adve6418eac2002-07-08 23:30:14 +00001889
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001890 // Mark the 4th operand as being a CC register, and as a def
1891 // A TmpInstruction is created to represent the CC "result".
1892 // Unlike other instances of TmpInstruction, this one is used
1893 // by machine code of multiple LLVM instructions, viz.,
1894 // the SetCC and the branch. Make sure to get the same one!
1895 // Note that we do this even for FP CC registers even though they
1896 // are explicit operands, because the type of the operand
1897 // needs to be a floating point condition code, not an integer
1898 // condition code. Think of this as casting the bool result to
1899 // a FP condition code register.
1900 //
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001901 Value* leftVal = subtreeRoot->leftChild()->getValue();
Chris Lattner9b625032002-05-06 16:15:30 +00001902 bool isFPCompare = leftVal->getType()->isFloatingPoint();
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001903
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001904 TmpInstruction* tmpForCC = GetTmpForCC(setCCInstr,
1905 setCCInstr->getParent()->getParent(),
Chris Lattner9b625032002-05-06 16:15:30 +00001906 isFPCompare ? Type::FloatTy : Type::IntTy);
Chris Lattner9c461082002-02-03 07:50:56 +00001907 MachineCodeForInstruction::get(setCCInstr).addTemp(tmpForCC);
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001908
1909 if (! isFPCompare)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001910 {
1911 // Integer condition: dest. should be %g0 or an integer register.
1912 // If result must be saved but condition is not SetEQ then we need
1913 // a separate instruction to compute the bool result, so discard
1914 // result of SUBcc instruction anyway.
1915 //
Vikram S. Adve74825322002-03-18 03:15:35 +00001916 M = new MachineInstr(SUBcc);
1917 Set3OperandsFromInstr(M, subtreeRoot, target, ! keepSubVal);
1918 M->SetMachineOperandVal(3, MachineOperand::MO_CCRegister,
1919 tmpForCC, /*def*/true);
1920 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001921
1922 if (computeBoolVal)
1923 { // recompute bool using the integer condition codes
1924 movOpCode =
1925 ChooseMovpccAfterSub(subtreeRoot,mustClearReg,valueToMove);
1926 }
1927 }
1928 else
1929 {
1930 // FP condition: dest of FCMP should be some FCCn register
Vikram S. Adve74825322002-03-18 03:15:35 +00001931 M = new MachineInstr(ChooseFcmpInstruction(subtreeRoot));
1932 M->SetMachineOperandVal(0, MachineOperand::MO_CCRegister,
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001933 tmpForCC);
Vikram S. Adve74825322002-03-18 03:15:35 +00001934 M->SetMachineOperandVal(1,MachineOperand::MO_VirtualRegister,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001935 subtreeRoot->leftChild()->getValue());
Vikram S. Adve74825322002-03-18 03:15:35 +00001936 M->SetMachineOperandVal(2,MachineOperand::MO_VirtualRegister,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001937 subtreeRoot->rightChild()->getValue());
Vikram S. Adve74825322002-03-18 03:15:35 +00001938 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001939
1940 if (computeBoolVal)
1941 {// recompute bool using the FP condition codes
1942 mustClearReg = true;
1943 valueToMove = 1;
1944 movOpCode = ChooseMovFpccInstruction(subtreeRoot);
1945 }
1946 }
1947
1948 if (computeBoolVal)
1949 {
1950 if (mustClearReg)
1951 {// Unconditionally set register to 0
Vikram S. Adve74825322002-03-18 03:15:35 +00001952 M = new MachineInstr(SETHI);
1953 M->SetMachineOperandConst(0,MachineOperand::MO_UnextendedImmed,
1954 (int64_t)0);
1955 M->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister,
1956 setCCInstr);
1957 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001958 }
1959
1960 // Now conditionally move `valueToMove' (0 or 1) into the register
Vikram S. Adve6418eac2002-07-08 23:30:14 +00001961 // Mark the register as a use (as well as a def) because the old
1962 // value should be retained if the condition is false.
Vikram S. Adve74825322002-03-18 03:15:35 +00001963 M = new MachineInstr(movOpCode);
1964 M->SetMachineOperandVal(0, MachineOperand::MO_CCRegister,
1965 tmpForCC);
1966 M->SetMachineOperandConst(1, MachineOperand::MO_UnextendedImmed,
1967 valueToMove);
1968 M->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister,
Vikram S. Adve6418eac2002-07-08 23:30:14 +00001969 setCCInstr, /*isDef*/ true,
1970 /*isDefAndUse*/ true);
Vikram S. Adve74825322002-03-18 03:15:35 +00001971 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001972 }
1973 break;
1974 }
1975
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001976 case 51: // reg: Load(reg)
1977 case 52: // reg: Load(ptrreg)
1978 case 53: // reg: LoadIdx(reg,reg)
1979 case 54: // reg: LoadIdx(ptrreg,reg)
Vikram S. Adve74825322002-03-18 03:15:35 +00001980 mvec.push_back(new MachineInstr(ChooseLoadInstruction(
1981 subtreeRoot->getValue()->getType())));
1982 SetOperandsForMemInstr(mvec, mvec.end()-1, subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001983 break;
1984
1985 case 55: // reg: GetElemPtr(reg)
1986 case 56: // reg: GetElemPtrIdx(reg,reg)
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001987 // If the GetElemPtr was folded into the user (parent), it will be
1988 // caught above. For other cases, we have to compute the address.
Vikram S. Adve74825322002-03-18 03:15:35 +00001989 mvec.push_back(new MachineInstr(ADD));
1990 SetOperandsForMemInstr(mvec, mvec.end()-1, subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001991 break;
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001992
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001993 case 57: // reg: Alloca: Implement as 1 instruction:
1994 { // add %fp, offsetFromFP -> result
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001995 AllocationInst* instr =
1996 cast<AllocationInst>(subtreeRoot->getInstruction());
1997 unsigned int tsize =
1998 target.findOptimalStorageSize(instr->getAllocatedType());
Vikram S. Adve74825322002-03-18 03:15:35 +00001999 assert(tsize != 0);
2000 CreateCodeForFixedSizeAlloca(target, instr, tsize, 1, mvec);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002001 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00002002 }
Vikram S. Adve74825322002-03-18 03:15:35 +00002003
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002004 case 58: // reg: Alloca(reg): Implement as 3 instructions:
2005 // mul num, typeSz -> tmp
2006 // sub %sp, tmp -> %sp
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00002007 { // add %sp, frameSizeBelowDynamicArea -> result
Vikram S. Advefd3900a2002-03-24 03:33:02 +00002008 AllocationInst* instr =
2009 cast<AllocationInst>(subtreeRoot->getInstruction());
Vikram S. Adve74825322002-03-18 03:15:35 +00002010 const Type* eltType = instr->getAllocatedType();
2011
Vikram S. Advefd3900a2002-03-24 03:33:02 +00002012 // If #elements is constant, use simpler code for fixed-size allocas
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002013 int tsize = (int) target.findOptimalStorageSize(eltType);
Vikram S. Advefd3900a2002-03-24 03:33:02 +00002014 Value* numElementsVal = NULL;
2015 bool isArray = instr->isArrayAllocation();
2016
2017 if (!isArray ||
2018 isa<Constant>(numElementsVal = instr->getArraySize()))
2019 { // total size is constant: generate code for fixed-size alloca
2020 unsigned int numElements = isArray?
2021 cast<ConstantUInt>(numElementsVal)->getValue() : 1;
2022 CreateCodeForFixedSizeAlloca(target, instr, tsize,
2023 numElements, mvec);
2024 }
Vikram S. Adve74825322002-03-18 03:15:35 +00002025 else // total size is not constant.
2026 CreateCodeForVariableSizeAlloca(target, instr, tsize,
Vikram S. Advefd3900a2002-03-24 03:33:02 +00002027 numElementsVal, mvec);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002028 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00002029 }
Vikram S. Adve74825322002-03-18 03:15:35 +00002030
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002031 case 61: // reg: Call
Vikram S. Advedbc4fad2002-04-25 04:37:51 +00002032 { // Generate a direct (CALL) or indirect (JMPL). depending
2033 // Mark the return-address register and the indirection
2034 // register (if any) as hidden virtual registers.
Vikram S. Advea995e602001-10-11 04:23:19 +00002035 // Also, mark the operands of the Call and return value (if
2036 // any) as implicit operands of the CALL machine instruction.
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00002037 //
Vikram S. Advedbc4fad2002-04-25 04:37:51 +00002038 // If this is a varargs function, floating point arguments
2039 // have to passed in integer registers so insert
2040 // copy-float-to-int instructions for each float operand.
2041 //
Chris Lattnerb00c5822001-10-02 03:41:24 +00002042 CallInst *callInstr = cast<CallInst>(subtreeRoot->getInstruction());
Chris Lattner749655f2001-10-13 06:54:30 +00002043 Value *callee = callInstr->getCalledValue();
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002044
Vikram S. Advea10d1a72002-03-31 19:07:35 +00002045 // Create hidden virtual register for return address, with type void*.
Vikram S. Adve242a8082002-05-19 15:25:51 +00002046 TmpInstruction* retAddrReg =
Vikram S. Advea10d1a72002-03-31 19:07:35 +00002047 new TmpInstruction(PointerType::get(Type::VoidTy), callInstr);
Chris Lattner9c461082002-02-03 07:50:56 +00002048 MachineCodeForInstruction::get(callInstr).addTemp(retAddrReg);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002049
Vikram S. Adveea21a6c2001-10-20 20:57:06 +00002050 // Generate the machine instruction and its operands.
2051 // Use CALL for direct function calls; this optimistically assumes
2052 // the PC-relative address fits in the CALL address field (22 bits).
2053 // Use JMPL for indirect calls.
2054 //
Chris Lattnerb0d04722002-03-26 17:58:12 +00002055 if (isa<Function>(callee))
Vikram S. Adveea21a6c2001-10-20 20:57:06 +00002056 { // direct function call
Vikram S. Adve74825322002-03-18 03:15:35 +00002057 M = new MachineInstr(CALL);
2058 M->SetMachineOperandVal(0, MachineOperand::MO_PCRelativeDisp,
2059 callee);
Vikram S. Adveea21a6c2001-10-20 20:57:06 +00002060 }
2061 else
2062 { // indirect function call
Vikram S. Adve74825322002-03-18 03:15:35 +00002063 M = new MachineInstr(JMPLCALL);
2064 M->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister,
2065 callee);
2066 M->SetMachineOperandConst(1, MachineOperand::MO_SignExtendedImmed,
2067 (int64_t) 0);
2068 M->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister,
2069 retAddrReg);
Vikram S. Adveea21a6c2001-10-20 20:57:06 +00002070 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002071
Vikram S. Adve74825322002-03-18 03:15:35 +00002072 mvec.push_back(M);
Vikram S. Advea10d1a72002-03-31 19:07:35 +00002073
Vikram S. Adve242a8082002-05-19 15:25:51 +00002074 const FunctionType* funcType =
2075 cast<FunctionType>(cast<PointerType>(callee->getType())
2076 ->getElementType());
2077 bool isVarArgs = funcType->isVarArg();
2078 bool noPrototype = isVarArgs && funcType->getNumParams() == 0;
Vikram S. Advedbc4fad2002-04-25 04:37:51 +00002079
Vikram S. Adve242a8082002-05-19 15:25:51 +00002080 // Use an annotation to pass information about call arguments
2081 // to the register allocator.
2082 CallArgsDescriptor* argDesc = new CallArgsDescriptor(callInstr,
2083 retAddrReg, isVarArgs, noPrototype);
2084 M->addAnnotation(argDesc);
Vikram S. Advea995e602001-10-11 04:23:19 +00002085
Vikram S. Adve242a8082002-05-19 15:25:51 +00002086 assert(callInstr->getOperand(0) == callee
2087 && "This is assumed in the loop below!");
2088
2089 for (unsigned i=1, N=callInstr->getNumOperands(); i < N; ++i)
2090 {
2091 Value* argVal = callInstr->getOperand(i);
2092 Instruction* intArgReg = NULL;
2093
2094 // Check for FP arguments to varargs functions.
2095 // Any such argument in the first $K$ args must be passed in an
2096 // integer register, where K = #integer argument registers.
2097 if (isVarArgs && argVal->getType()->isFloatingPoint())
2098 {
2099 // If it is a function with no prototype, pass value
2100 // as an FP value as well as a varargs value
2101 if (noPrototype)
2102 argDesc->getArgInfo(i-1).setUseFPArgReg();
2103
2104 // If this arg. is in the first $K$ regs, add a copy
2105 // float-to-int instruction to pass the value as an integer.
2106 if (i < target.getRegInfo().GetNumOfIntArgRegs())
2107 {
2108 MachineCodeForInstruction &destMCFI =
2109 MachineCodeForInstruction::get(callInstr);
2110 intArgReg = new TmpInstruction(Type::IntTy, argVal);
2111 destMCFI.addTemp(intArgReg);
2112
2113 vector<MachineInstr*> copyMvec;
2114 target.getInstrInfo().CreateCodeToCopyFloatToInt(target,
2115 callInstr->getParent()->getParent(),
2116 argVal, (TmpInstruction*) intArgReg,
2117 copyMvec, destMCFI);
2118 mvec.insert(mvec.begin(),copyMvec.begin(),copyMvec.end());
2119
2120 argDesc->getArgInfo(i-1).setUseIntArgReg();
2121 argDesc->getArgInfo(i-1).setArgCopy(intArgReg);
2122 }
2123 else
2124 // Cannot fit in first $K$ regs so pass the arg on the stack
2125 argDesc->getArgInfo(i-1).setUseStackSlot();
2126 }
2127
2128 if (intArgReg)
2129 mvec.back()->addImplicitRef(intArgReg);
2130
2131 mvec.back()->addImplicitRef(argVal);
2132 }
2133
2134 // Add the return value as an implicit ref. The call operands
2135 // were added above.
Vikram S. Adveea21a6c2001-10-20 20:57:06 +00002136 if (callInstr->getType() != Type::VoidTy)
Vikram S. Adve74825322002-03-18 03:15:35 +00002137 mvec.back()->addImplicitRef(callInstr, /*isDef*/ true);
Vikram S. Advea995e602001-10-11 04:23:19 +00002138
Vikram S. Adveea21a6c2001-10-20 20:57:06 +00002139 // For the CALL instruction, the ret. addr. reg. is also implicit
Chris Lattnerb0d04722002-03-26 17:58:12 +00002140 if (isa<Function>(callee))
Vikram S. Adve74825322002-03-18 03:15:35 +00002141 mvec.back()->addImplicitRef(retAddrReg, /*isDef*/ true);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002142
Vikram S. Adve74825322002-03-18 03:15:35 +00002143 // delay slot
2144 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002145 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00002146 }
Vikram S. Adve242a8082002-05-19 15:25:51 +00002147
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002148 case 62: // reg: Shl(reg, reg)
Vikram S. Adve242a8082002-05-19 15:25:51 +00002149 {
2150 Value* argVal1 = subtreeRoot->leftChild()->getValue();
2151 Value* argVal2 = subtreeRoot->rightChild()->getValue();
2152 Instruction* shlInstr = subtreeRoot->getInstruction();
2153
2154 const Type* opType = argVal1->getType();
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002155 assert(opType->isIntegral()
Vikram S. Adve242a8082002-05-19 15:25:51 +00002156 || opType == Type::BoolTy
2157 || isa<PointerType>(opType)&&"Shl unsupported for other types");
2158
2159 CreateShiftInstructions(target, shlInstr->getParent()->getParent(),
2160 (opType == Type::LongTy)? SLLX : SLL,
2161 argVal1, argVal2, 0, shlInstr, mvec,
2162 MachineCodeForInstruction::get(shlInstr));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002163 break;
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00002164 }
2165
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002166 case 63: // reg: Shr(reg, reg)
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00002167 { const Type* opType = subtreeRoot->leftChild()->getValue()->getType();
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002168 assert(opType->isIntegral()
Vikram S. Adve242a8082002-05-19 15:25:51 +00002169 || isa<PointerType>(opType)&&"Shr unsupported for other types");
Vikram S. Adve74825322002-03-18 03:15:35 +00002170 mvec.push_back(new MachineInstr((opType->isSigned()
2171 ? ((opType == Type::LongTy)? SRAX : SRA)
2172 : ((opType == Type::LongTy)? SRLX : SRL))));
2173 Set3OperandsFromInstr(mvec.back(), subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002174 break;
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00002175 }
2176
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002177 case 64: // reg: Phi(reg,reg)
Vikram S. Adve74825322002-03-18 03:15:35 +00002178 break; // don't forward the value
2179
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002180 case 71: // reg: VReg
2181 case 72: // reg: Constant
Vikram S. Adve74825322002-03-18 03:15:35 +00002182 break; // don't forward the value
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002183
2184 default:
2185 assert(0 && "Unrecognized BURG rule");
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002186 break;
2187 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00002188 }
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00002189
Chris Lattner20b1ea02001-09-14 03:47:57 +00002190 if (forwardOperandNum >= 0)
2191 { // We did not generate a machine instruction but need to use operand.
2192 // If user is in the same tree, replace Value in its machine operand.
2193 // If not, insert a copy instruction which should get coalesced away
2194 // by register allocation.
2195 if (subtreeRoot->parent() != NULL)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002196 ForwardOperand(subtreeRoot, subtreeRoot->parent(), forwardOperandNum);
Chris Lattner20b1ea02001-09-14 03:47:57 +00002197 else
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002198 {
Vikram S. Adve7fe27872001-10-18 00:26:20 +00002199 vector<MachineInstr*> minstrVec;
Vikram S. Adve242a8082002-05-19 15:25:51 +00002200 Instruction* instr = subtreeRoot->getInstruction();
2201 target.getInstrInfo().
2202 CreateCopyInstructionsByType(target,
2203 instr->getParent()->getParent(),
2204 instr->getOperand(forwardOperandNum),
2205 instr, minstrVec,
2206 MachineCodeForInstruction::get(instr));
Vikram S. Adve7fe27872001-10-18 00:26:20 +00002207 assert(minstrVec.size() > 0);
Vikram S. Adve74825322002-03-18 03:15:35 +00002208 mvec.insert(mvec.end(), minstrVec.begin(), minstrVec.end());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002209 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00002210 }
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00002211
2212 if (maskUnsignedResult)
2213 { // If result is unsigned and smaller than int reg size,
2214 // we need to clear high bits of result value.
2215 assert(forwardOperandNum < 0 && "Need mask but no instruction generated");
2216 Instruction* dest = subtreeRoot->getInstruction();
2217 if (! dest->getType()->isSigned())
2218 {
2219 unsigned destSize = target.DataLayout.getTypeSize(dest->getType());
2220 if (destSize < target.DataLayout.getIntegerRegize())
2221 { // Mask high bits. Use a TmpInstruction to represent the
2222 // intermediate result before masking. Since those instructions
2223 // have already been generated, go back and substitute tmpI
2224 // for dest in the result position of each one of them.
2225 TmpInstruction *tmpI = new TmpInstruction(dest->getType(), dest,
2226 NULL, "maskHi");
2227 MachineCodeForInstruction::get(dest).addTemp(tmpI);
2228
2229 for (unsigned i=0, N=mvec.size(); i < N; ++i)
2230 mvec[i]->substituteValue(dest, tmpI);
2231
2232 M = Create3OperandInstr(AND, tmpI,
2233 ConstantUInt::get(Type::ULongTy,
2234 ((uint64_t) 1 << 8*destSize) - 1),
2235 dest);
2236 mvec.push_back(M);
2237 }
2238 }
2239 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00002240}