blob: ea1ddea881f8001dc00079789298651233ee7a21 [file] [log] [blame]
Vikram S. Adve243dd452001-09-18 13:03:13 +00001// $Id$
Chris Lattner20b1ea02001-09-14 03:47:57 +00002//***************************************************************************
3// File:
4// SparcInstrSelection.cpp
5//
6// Purpose:
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00007// BURS instruction selection for SPARC V9 architecture.
Chris Lattner20b1ea02001-09-14 03:47:57 +00008//
9// History:
10// 7/02/01 - Vikram Adve - Created
11//**************************************************************************/
12
13#include "SparcInternals.h"
Vikram S. Adve7fe27872001-10-18 00:26:20 +000014#include "SparcInstrSelectionSupport.h"
Vikram S. Adve74825322002-03-18 03:15:35 +000015#include "SparcRegClassInfo.h"
Vikram S. Adve8557b222001-10-10 20:56:33 +000016#include "llvm/CodeGen/InstrSelectionSupport.h"
Chris Lattner20b1ea02001-09-14 03:47:57 +000017#include "llvm/CodeGen/MachineInstr.h"
Vikram S. Adve242a8082002-05-19 15:25:51 +000018#include "llvm/CodeGen/MachineInstrAnnot.h"
Chris Lattner20b1ea02001-09-14 03:47:57 +000019#include "llvm/CodeGen/InstrForest.h"
20#include "llvm/CodeGen/InstrSelection.h"
Chris Lattner9c461082002-02-03 07:50:56 +000021#include "llvm/CodeGen/MachineCodeForMethod.h"
22#include "llvm/CodeGen/MachineCodeForInstruction.h"
Chris Lattner20b1ea02001-09-14 03:47:57 +000023#include "llvm/DerivedTypes.h"
24#include "llvm/iTerminators.h"
25#include "llvm/iMemory.h"
26#include "llvm/iOther.h"
27#include "llvm/BasicBlock.h"
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000028#include "llvm/Function.h"
Chris Lattner31bcdb82002-04-28 19:55:58 +000029#include "llvm/Constants.h"
Chris Lattnercee8f9a2001-11-27 00:03:19 +000030#include "Support/MathExtras.h"
Chris Lattner749655f2001-10-13 06:54:30 +000031#include <math.h>
Chris Lattner697954c2002-01-20 22:54:45 +000032using std::vector;
Chris Lattner20b1ea02001-09-14 03:47:57 +000033
34//************************* Forward Declarations ***************************/
35
36
Vikram S. Adve74825322002-03-18 03:15:35 +000037static void SetMemOperands_Internal (vector<MachineInstr*>& mvec,
38 vector<MachineInstr*>::iterator mvecI,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +000039 const InstructionNode* vmInstrNode,
40 Value* ptrVal,
Vikram S. Advefd3900a2002-03-24 03:33:02 +000041 std::vector<Value*>& idxVec,
Vikram S. Adve242a8082002-05-19 15:25:51 +000042 bool allConstantIndices,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +000043 const TargetMachine& target);
Chris Lattner20b1ea02001-09-14 03:47:57 +000044
45
46//************************ Internal Functions ******************************/
47
Chris Lattner20b1ea02001-09-14 03:47:57 +000048
Chris Lattner20b1ea02001-09-14 03:47:57 +000049static inline MachineOpCode
50ChooseBprInstruction(const InstructionNode* instrNode)
51{
52 MachineOpCode opCode;
53
54 Instruction* setCCInstr =
55 ((InstructionNode*) instrNode->leftChild())->getInstruction();
56
57 switch(setCCInstr->getOpcode())
58 {
59 case Instruction::SetEQ: opCode = BRZ; break;
60 case Instruction::SetNE: opCode = BRNZ; break;
61 case Instruction::SetLE: opCode = BRLEZ; break;
62 case Instruction::SetGE: opCode = BRGEZ; break;
63 case Instruction::SetLT: opCode = BRLZ; break;
64 case Instruction::SetGT: opCode = BRGZ; break;
65 default:
66 assert(0 && "Unrecognized VM instruction!");
67 opCode = INVALID_OPCODE;
68 break;
69 }
70
71 return opCode;
72}
73
74
75static inline MachineOpCode
Chris Lattner20b1ea02001-09-14 03:47:57 +000076ChooseBpccInstruction(const InstructionNode* instrNode,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +000077 const BinaryOperator* setCCInstr)
Chris Lattner20b1ea02001-09-14 03:47:57 +000078{
79 MachineOpCode opCode = INVALID_OPCODE;
80
81 bool isSigned = setCCInstr->getOperand(0)->getType()->isSigned();
82
83 if (isSigned)
84 {
85 switch(setCCInstr->getOpcode())
Vikram S. Adve4cecdd22001-10-01 00:12:53 +000086 {
87 case Instruction::SetEQ: opCode = BE; break;
88 case Instruction::SetNE: opCode = BNE; break;
89 case Instruction::SetLE: opCode = BLE; break;
90 case Instruction::SetGE: opCode = BGE; break;
91 case Instruction::SetLT: opCode = BL; break;
92 case Instruction::SetGT: opCode = BG; break;
93 default:
94 assert(0 && "Unrecognized VM instruction!");
95 break;
96 }
Chris Lattner20b1ea02001-09-14 03:47:57 +000097 }
98 else
99 {
100 switch(setCCInstr->getOpcode())
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000101 {
102 case Instruction::SetEQ: opCode = BE; break;
103 case Instruction::SetNE: opCode = BNE; break;
104 case Instruction::SetLE: opCode = BLEU; break;
105 case Instruction::SetGE: opCode = BCC; break;
106 case Instruction::SetLT: opCode = BCS; break;
107 case Instruction::SetGT: opCode = BGU; break;
108 default:
109 assert(0 && "Unrecognized VM instruction!");
110 break;
111 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000112 }
113
114 return opCode;
115}
116
117static inline MachineOpCode
118ChooseBFpccInstruction(const InstructionNode* instrNode,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000119 const BinaryOperator* setCCInstr)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000120{
121 MachineOpCode opCode = INVALID_OPCODE;
122
123 switch(setCCInstr->getOpcode())
124 {
125 case Instruction::SetEQ: opCode = FBE; break;
126 case Instruction::SetNE: opCode = FBNE; break;
127 case Instruction::SetLE: opCode = FBLE; break;
128 case Instruction::SetGE: opCode = FBGE; break;
129 case Instruction::SetLT: opCode = FBL; break;
130 case Instruction::SetGT: opCode = FBG; break;
131 default:
132 assert(0 && "Unrecognized VM instruction!");
133 break;
134 }
135
136 return opCode;
137}
138
139
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000140// Create a unique TmpInstruction for a boolean value,
141// representing the CC register used by a branch on that value.
142// For now, hack this using a little static cache of TmpInstructions.
143// Eventually the entire BURG instruction selection should be put
144// into a separate class that can hold such information.
Vikram S. Adveff5a09e2001-11-08 05:04:09 +0000145// The static cache is not too bad because the memory for these
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000146// TmpInstructions will be freed along with the rest of the Function anyway.
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000147//
148static TmpInstruction*
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000149GetTmpForCC(Value* boolVal, const Function *F, const Type* ccType)
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000150{
Chris Lattner697954c2002-01-20 22:54:45 +0000151 typedef std::hash_map<const Value*, TmpInstruction*> BoolTmpCache;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000152 static BoolTmpCache boolToTmpCache; // Map boolVal -> TmpInstruction*
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000153 static const Function *lastFunction = 0;// Use to flush cache between funcs
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000154
155 assert(boolVal->getType() == Type::BoolTy && "Weird but ok! Delete assert");
156
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000157 if (lastFunction != F)
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000158 {
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000159 lastFunction = F;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000160 boolToTmpCache.clear();
161 }
162
Vikram S. Adveff5a09e2001-11-08 05:04:09 +0000163 // Look for tmpI and create a new one otherwise. The new value is
164 // directly written to map using the ref returned by operator[].
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000165 TmpInstruction*& tmpI = boolToTmpCache[boolVal];
166 if (tmpI == NULL)
Chris Lattner9c461082002-02-03 07:50:56 +0000167 tmpI = new TmpInstruction(ccType, boolVal);
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000168
169 return tmpI;
170}
171
172
Chris Lattner20b1ea02001-09-14 03:47:57 +0000173static inline MachineOpCode
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000174ChooseBccInstruction(const InstructionNode* instrNode,
175 bool& isFPBranch)
176{
177 InstructionNode* setCCNode = (InstructionNode*) instrNode->leftChild();
178 BinaryOperator* setCCInstr = (BinaryOperator*) setCCNode->getInstruction();
179 const Type* setCCType = setCCInstr->getOperand(0)->getType();
180
Vikram S. Adve242a8082002-05-19 15:25:51 +0000181 isFPBranch = setCCType->isFloatingPoint(); // Return value: don't delete!
182
183 if (isFPBranch)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000184 return ChooseBFpccInstruction(instrNode, setCCInstr);
185 else
186 return ChooseBpccInstruction(instrNode, setCCInstr);
187}
188
189
190static inline MachineOpCode
Chris Lattner20b1ea02001-09-14 03:47:57 +0000191ChooseMovFpccInstruction(const InstructionNode* instrNode)
192{
193 MachineOpCode opCode = INVALID_OPCODE;
194
195 switch(instrNode->getInstruction()->getOpcode())
196 {
197 case Instruction::SetEQ: opCode = MOVFE; break;
198 case Instruction::SetNE: opCode = MOVFNE; break;
199 case Instruction::SetLE: opCode = MOVFLE; break;
200 case Instruction::SetGE: opCode = MOVFGE; break;
201 case Instruction::SetLT: opCode = MOVFL; break;
202 case Instruction::SetGT: opCode = MOVFG; break;
203 default:
204 assert(0 && "Unrecognized VM instruction!");
205 break;
206 }
207
208 return opCode;
209}
210
211
212// Assumes that SUBcc v1, v2 -> v3 has been executed.
213// In most cases, we want to clear v3 and then follow it by instruction
214// MOVcc 1 -> v3.
215// Set mustClearReg=false if v3 need not be cleared before conditional move.
216// Set valueToMove=0 if we want to conditionally move 0 instead of 1
217// (i.e., we want to test inverse of a condition)
Vikram S. Adve243dd452001-09-18 13:03:13 +0000218// (The latter two cases do not seem to arise because SetNE needs nothing.)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000219//
220static MachineOpCode
221ChooseMovpccAfterSub(const InstructionNode* instrNode,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000222 bool& mustClearReg,
223 int& valueToMove)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000224{
225 MachineOpCode opCode = INVALID_OPCODE;
226 mustClearReg = true;
227 valueToMove = 1;
228
229 switch(instrNode->getInstruction()->getOpcode())
230 {
Vikram S. Adve243dd452001-09-18 13:03:13 +0000231 case Instruction::SetEQ: opCode = MOVE; break;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000232 case Instruction::SetLE: opCode = MOVLE; break;
233 case Instruction::SetGE: opCode = MOVGE; break;
234 case Instruction::SetLT: opCode = MOVL; break;
235 case Instruction::SetGT: opCode = MOVG; break;
Vikram S. Adve243dd452001-09-18 13:03:13 +0000236 case Instruction::SetNE: assert(0 && "No move required!"); break;
237 default: assert(0 && "Unrecognized VM instr!"); break;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000238 }
239
240 return opCode;
241}
242
Chris Lattner20b1ea02001-09-14 03:47:57 +0000243static inline MachineOpCode
Vikram S. Advedbc4fad2002-04-25 04:37:51 +0000244ChooseConvertToFloatInstr(OpLabel vopCode, const Type* opType)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000245{
246 MachineOpCode opCode = INVALID_OPCODE;
247
Vikram S. Advedbc4fad2002-04-25 04:37:51 +0000248 switch(vopCode)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000249 {
250 case ToFloatTy:
251 if (opType == Type::SByteTy || opType == Type::ShortTy || opType == Type::IntTy)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000252 opCode = FITOS;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000253 else if (opType == Type::LongTy)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000254 opCode = FXTOS;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000255 else if (opType == Type::DoubleTy)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000256 opCode = FDTOS;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000257 else if (opType == Type::FloatTy)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000258 ;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000259 else
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000260 assert(0 && "Cannot convert this type to FLOAT on SPARC");
Chris Lattner20b1ea02001-09-14 03:47:57 +0000261 break;
262
263 case ToDoubleTy:
Vikram S. Adve74825322002-03-18 03:15:35 +0000264 // This is usually used in conjunction with CreateCodeToCopyIntToFloat().
265 // Both functions should treat the integer as a 32-bit value for types
266 // of 4 bytes or less, and as a 64-bit value otherwise.
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000267 if (opType == Type::SByteTy || opType == Type::UByteTy ||
268 opType == Type::ShortTy || opType == Type::UShortTy ||
269 opType == Type::IntTy || opType == Type::UIntTy)
Vikram S. Adve74825322002-03-18 03:15:35 +0000270 opCode = FITOD;
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000271 else if (opType == Type::LongTy || opType == Type::ULongTy)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000272 opCode = FXTOD;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000273 else if (opType == Type::FloatTy)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000274 opCode = FSTOD;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000275 else if (opType == Type::DoubleTy)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000276 ;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000277 else
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000278 assert(0 && "Cannot convert this type to DOUBLE on SPARC");
Chris Lattner20b1ea02001-09-14 03:47:57 +0000279 break;
280
281 default:
282 break;
283 }
284
285 return opCode;
286}
287
288static inline MachineOpCode
Vikram S. Advedbc4fad2002-04-25 04:37:51 +0000289ChooseConvertToIntInstr(OpLabel vopCode, const Type* opType)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000290{
291 MachineOpCode opCode = INVALID_OPCODE;;
292
Vikram S. Advedbc4fad2002-04-25 04:37:51 +0000293 if (vopCode == ToSByteTy || vopCode == ToShortTy || vopCode == ToIntTy)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000294 {
295 switch (opType->getPrimitiveID())
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000296 {
Chris Lattner20b1ea02001-09-14 03:47:57 +0000297 case Type::FloatTyID: opCode = FSTOI; break;
298 case Type::DoubleTyID: opCode = FDTOI; break;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000299 default:
300 assert(0 && "Non-numeric non-bool type cannot be converted to Int");
301 break;
302 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000303 }
Vikram S. Advedbc4fad2002-04-25 04:37:51 +0000304 else if (vopCode == ToLongTy)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000305 {
306 switch (opType->getPrimitiveID())
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000307 {
Chris Lattner20b1ea02001-09-14 03:47:57 +0000308 case Type::FloatTyID: opCode = FSTOX; break;
309 case Type::DoubleTyID: opCode = FDTOX; break;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000310 default:
311 assert(0 && "Non-numeric non-bool type cannot be converted to Long");
312 break;
313 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000314 }
315 else
316 assert(0 && "Should not get here, Mo!");
317
318 return opCode;
319}
320
Vikram S. Advedbc4fad2002-04-25 04:37:51 +0000321MachineInstr*
322CreateConvertToIntInstr(OpLabel vopCode, Value* srcVal, Value* destVal)
323{
324 MachineOpCode opCode = ChooseConvertToIntInstr(vopCode, srcVal->getType());
325 assert(opCode != INVALID_OPCODE && "Expected to need conversion!");
326
327 MachineInstr* M = new MachineInstr(opCode);
328 M->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister, srcVal);
329 M->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister, destVal);
330 return M;
331}
Chris Lattner20b1ea02001-09-14 03:47:57 +0000332
333static inline MachineOpCode
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000334ChooseAddInstruction(const InstructionNode* instrNode)
335{
336 return ChooseAddInstructionByType(instrNode->getInstruction()->getType());
337}
338
339
Chris Lattner20b1ea02001-09-14 03:47:57 +0000340static inline MachineInstr*
341CreateMovFloatInstruction(const InstructionNode* instrNode,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000342 const Type* resultType)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000343{
344 MachineInstr* minstr = new MachineInstr((resultType == Type::FloatTy)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000345 ? FMOVS : FMOVD);
Vikram S. Adve74825322002-03-18 03:15:35 +0000346 minstr->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister,
347 instrNode->leftChild()->getValue());
348 minstr->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister,
349 instrNode->getValue());
Chris Lattner20b1ea02001-09-14 03:47:57 +0000350 return minstr;
351}
352
353static inline MachineInstr*
354CreateAddConstInstruction(const InstructionNode* instrNode)
355{
356 MachineInstr* minstr = NULL;
357
358 Value* constOp = ((InstrTreeNode*) instrNode->rightChild())->getValue();
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000359 assert(isa<Constant>(constOp));
Chris Lattner20b1ea02001-09-14 03:47:57 +0000360
361 // Cases worth optimizing are:
362 // (1) Add with 0 for float or double: use an FMOV of appropriate type,
363 // instead of an FADD (1 vs 3 cycles). There is no integer MOV.
364 //
Chris Lattner9b625032002-05-06 16:15:30 +0000365 if (ConstantFP *FPC = dyn_cast<ConstantFP>(constOp)) {
366 double dval = FPC->getValue();
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000367 if (dval == 0.0)
Vikram S. Adve242a8082002-05-19 15:25:51 +0000368 minstr = CreateMovFloatInstruction(instrNode,
369 instrNode->getInstruction()->getType());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000370 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000371
372 return minstr;
373}
374
375
376static inline MachineOpCode
Vikram S. Adve510eec72001-11-04 21:59:14 +0000377ChooseSubInstructionByType(const Type* resultType)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000378{
379 MachineOpCode opCode = INVALID_OPCODE;
380
Chris Lattner9b625032002-05-06 16:15:30 +0000381 if (resultType->isIntegral() || isa<PointerType>(resultType))
Chris Lattner20b1ea02001-09-14 03:47:57 +0000382 {
383 opCode = SUB;
384 }
385 else
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000386 switch(resultType->getPrimitiveID())
387 {
388 case Type::FloatTyID: opCode = FSUBS; break;
389 case Type::DoubleTyID: opCode = FSUBD; break;
390 default: assert(0 && "Invalid type for SUB instruction"); break;
391 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000392
393 return opCode;
394}
395
396
397static inline MachineInstr*
398CreateSubConstInstruction(const InstructionNode* instrNode)
399{
400 MachineInstr* minstr = NULL;
401
402 Value* constOp = ((InstrTreeNode*) instrNode->rightChild())->getValue();
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000403 assert(isa<Constant>(constOp));
Chris Lattner20b1ea02001-09-14 03:47:57 +0000404
405 // Cases worth optimizing are:
406 // (1) Sub with 0 for float or double: use an FMOV of appropriate type,
407 // instead of an FSUB (1 vs 3 cycles). There is no integer MOV.
408 //
Chris Lattner9b625032002-05-06 16:15:30 +0000409 if (ConstantFP *FPC = dyn_cast<ConstantFP>(constOp)) {
410 double dval = FPC->getValue();
411 if (dval == 0.0)
Vikram S. Adve242a8082002-05-19 15:25:51 +0000412 minstr = CreateMovFloatInstruction(instrNode,
413 instrNode->getInstruction()->getType());
Chris Lattner9b625032002-05-06 16:15:30 +0000414 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000415
416 return minstr;
417}
418
419
420static inline MachineOpCode
421ChooseFcmpInstruction(const InstructionNode* instrNode)
422{
423 MachineOpCode opCode = INVALID_OPCODE;
424
425 Value* operand = ((InstrTreeNode*) instrNode->leftChild())->getValue();
426 switch(operand->getType()->getPrimitiveID()) {
427 case Type::FloatTyID: opCode = FCMPS; break;
428 case Type::DoubleTyID: opCode = FCMPD; break;
429 default: assert(0 && "Invalid type for FCMP instruction"); break;
430 }
431
432 return opCode;
433}
434
435
436// Assumes that leftArg and rightArg are both cast instructions.
437//
438static inline bool
439BothFloatToDouble(const InstructionNode* instrNode)
440{
441 InstrTreeNode* leftArg = instrNode->leftChild();
442 InstrTreeNode* rightArg = instrNode->rightChild();
443 InstrTreeNode* leftArgArg = leftArg->leftChild();
444 InstrTreeNode* rightArgArg = rightArg->leftChild();
445 assert(leftArg->getValue()->getType() == rightArg->getValue()->getType());
446
447 // Check if both arguments are floats cast to double
448 return (leftArg->getValue()->getType() == Type::DoubleTy &&
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000449 leftArgArg->getValue()->getType() == Type::FloatTy &&
450 rightArgArg->getValue()->getType() == Type::FloatTy);
Chris Lattner20b1ea02001-09-14 03:47:57 +0000451}
452
453
454static inline MachineOpCode
Vikram S. Adve510eec72001-11-04 21:59:14 +0000455ChooseMulInstructionByType(const Type* resultType)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000456{
457 MachineOpCode opCode = INVALID_OPCODE;
458
Chris Lattner20b1ea02001-09-14 03:47:57 +0000459 if (resultType->isIntegral())
Vikram S. Adve510eec72001-11-04 21:59:14 +0000460 opCode = MULX;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000461 else
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000462 switch(resultType->getPrimitiveID())
463 {
464 case Type::FloatTyID: opCode = FMULS; break;
465 case Type::DoubleTyID: opCode = FMULD; break;
466 default: assert(0 && "Invalid type for MUL instruction"); break;
467 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000468
469 return opCode;
470}
471
472
Vikram S. Adve510eec72001-11-04 21:59:14 +0000473
Chris Lattner20b1ea02001-09-14 03:47:57 +0000474static inline MachineInstr*
Vikram S. Adve74825322002-03-18 03:15:35 +0000475CreateIntNegInstruction(const TargetMachine& target,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000476 Value* vreg)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000477{
478 MachineInstr* minstr = new MachineInstr(SUB);
Vikram S. Adve74825322002-03-18 03:15:35 +0000479 minstr->SetMachineOperandReg(0, target.getRegInfo().getZeroRegNum());
480 minstr->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister, vreg);
481 minstr->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister, vreg);
Chris Lattner20b1ea02001-09-14 03:47:57 +0000482 return minstr;
483}
484
485
Vikram S. Adve242a8082002-05-19 15:25:51 +0000486// Create instruction sequence for any shift operation.
487// SLL or SLLX on an operand smaller than the integer reg. size (64bits)
488// requires a second instruction for explicit sign-extension.
489// Note that we only have to worry about a sign-bit appearing in the
490// most significant bit of the operand after shifting (e.g., bit 32 of
491// Int or bit 16 of Short), so we do not have to worry about results
492// that are as large as a normal integer register.
493//
494static inline void
495CreateShiftInstructions(const TargetMachine& target,
496 Function* F,
497 MachineOpCode shiftOpCode,
498 Value* argVal1,
499 Value* optArgVal2, /* Use optArgVal2 if not NULL */
500 unsigned int optShiftNum, /* else use optShiftNum */
501 Instruction* destVal,
502 vector<MachineInstr*>& mvec,
503 MachineCodeForInstruction& mcfi)
504{
505 assert((optArgVal2 != NULL || optShiftNum <= 64) &&
506 "Large shift sizes unexpected, but can be handled below: "
507 "You need to check whether or not it fits in immed field below");
508
509 // If this is a logical left shift of a type smaller than the standard
510 // integer reg. size, we have to extend the sign-bit into upper bits
511 // of dest, so we need to put the result of the SLL into a temporary.
512 //
513 Value* shiftDest = destVal;
514 const Type* opType = argVal1->getType();
515 unsigned opSize = target.DataLayout.getTypeSize(argVal1->getType());
516 if ((shiftOpCode == SLL || shiftOpCode == SLLX)
517 && opSize < target.DataLayout.getIntegerRegize())
518 { // put SLL result into a temporary
519 shiftDest = new TmpInstruction(argVal1, optArgVal2, "sllTmp");
520 mcfi.addTemp(shiftDest);
521 }
522
523 MachineInstr* M = (optArgVal2 != NULL)
524 ? Create3OperandInstr(shiftOpCode, argVal1, optArgVal2, shiftDest)
525 : Create3OperandInstr_UImmed(shiftOpCode, argVal1, optShiftNum, shiftDest);
526 mvec.push_back(M);
527
528 if (shiftDest != destVal)
529 { // extend the sign-bit of the result into all upper bits of dest
530 assert(8*opSize <= 32 && "Unexpected type size > 4 and < IntRegSize?");
531 target.getInstrInfo().
532 CreateSignExtensionInstructions(target, F, shiftDest, 8*opSize,
533 destVal, mvec, mcfi);
534 }
535}
536
537
Vikram S. Adve74825322002-03-18 03:15:35 +0000538// Does not create any instructions if we cannot exploit constant to
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000539// create a cheaper instruction.
540// This returns the approximate cost of the instructions generated,
541// which is used to pick the cheapest when both operands are constant.
542static inline unsigned int
Vikram S. Adve242a8082002-05-19 15:25:51 +0000543CreateMulConstInstruction(const TargetMachine &target, Function* F,
544 Value* lval, Value* rval, Instruction* destVal,
545 vector<MachineInstr*>& mvec,
546 MachineCodeForInstruction& mcfi)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000547{
Vikram S. Adve242a8082002-05-19 15:25:51 +0000548 /* Use max. multiply cost, viz., cost of MULX */
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000549 unsigned int cost = target.getInstrInfo().minLatency(MULX);
Vikram S. Adve242a8082002-05-19 15:25:51 +0000550 unsigned int firstNewInstr = mvec.size();
Vikram S. Adve74825322002-03-18 03:15:35 +0000551
552 Value* constOp = rval;
553 if (! isa<Constant>(constOp))
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000554 return cost;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000555
556 // Cases worth optimizing are:
557 // (1) Multiply by 0 or 1 for any type: replace with copy (ADD or FMOV)
558 // (2) Multiply by 2^x for integer types: replace with Shift
559 //
Vikram S. Adve74825322002-03-18 03:15:35 +0000560 const Type* resultType = destVal->getType();
Chris Lattner20b1ea02001-09-14 03:47:57 +0000561
Chris Lattner9b625032002-05-06 16:15:30 +0000562 if (resultType->isIntegral() || isa<PointerType>(resultType))
Chris Lattner20b1ea02001-09-14 03:47:57 +0000563 {
Chris Lattner20b1ea02001-09-14 03:47:57 +0000564 bool isValidConst;
565 int64_t C = GetConstantValueAsSignedInt(constOp, isValidConst);
566 if (isValidConst)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000567 {
Vikram S. Adve242a8082002-05-19 15:25:51 +0000568 unsigned pow;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000569 bool needNeg = false;
570 if (C < 0)
571 {
572 needNeg = true;
573 C = -C;
574 }
575
576 if (C == 0 || C == 1)
577 {
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000578 cost = target.getInstrInfo().minLatency(ADD);
Vikram S. Adve242a8082002-05-19 15:25:51 +0000579 MachineInstr* M = (C == 0)
580 ? Create3OperandInstr_Reg(ADD,
581 target.getRegInfo().getZeroRegNum(),
582 target.getRegInfo().getZeroRegNum(),
583 destVal)
584 : Create3OperandInstr_Reg(ADD, lval,
585 target.getRegInfo().getZeroRegNum(),
586 destVal);
587 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000588 }
Chris Lattner36346c72002-05-19 21:20:19 +0000589 else if (isPowerOf2(C, pow))
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000590 {
Vikram S. Adve242a8082002-05-19 15:25:51 +0000591 unsigned int opSize = target.DataLayout.getTypeSize(resultType);
592 MachineOpCode opCode = (opSize <= 32)? SLL : SLLX;
593 CreateShiftInstructions(target, F, opCode, lval, NULL, pow,
594 destVal, mvec, mcfi);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000595 }
596
Vikram S. Adve242a8082002-05-19 15:25:51 +0000597 if (mvec.size() > 0 && needNeg)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000598 { // insert <reg = SUB 0, reg> after the instr to flip the sign
Vikram S. Adve242a8082002-05-19 15:25:51 +0000599 MachineInstr* M = CreateIntNegInstruction(target, destVal);
600 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000601 }
602 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000603 }
604 else
605 {
Chris Lattner9b625032002-05-06 16:15:30 +0000606 if (ConstantFP *FPC = dyn_cast<ConstantFP>(constOp))
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000607 {
Chris Lattner9b625032002-05-06 16:15:30 +0000608 double dval = FPC->getValue();
Vikram S. Adve6ad7c552001-11-09 02:18:16 +0000609 if (fabs(dval) == 1)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000610 {
Vikram S. Adve242a8082002-05-19 15:25:51 +0000611 MachineOpCode opCode = (dval < 0)
Vikram S. Adve6ad7c552001-11-09 02:18:16 +0000612 ? (resultType == Type::FloatTy? FNEGS : FNEGD)
613 : (resultType == Type::FloatTy? FMOVS : FMOVD);
Vikram S. Adve242a8082002-05-19 15:25:51 +0000614 MachineInstr* M = Create2OperandInstr(opCode, lval, destVal);
615 mvec.push_back(M);
Vikram S. Adve6ad7c552001-11-09 02:18:16 +0000616 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000617 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000618 }
619
Vikram S. Adve242a8082002-05-19 15:25:51 +0000620 if (firstNewInstr < mvec.size())
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000621 {
Vikram S. Adve242a8082002-05-19 15:25:51 +0000622 cost = 0;
623 for (unsigned int i=firstNewInstr; i < mvec.size(); ++i)
624 cost += target.getInstrInfo().minLatency(mvec[i]->getOpCode());
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000625 }
626
627 return cost;
Vikram S. Adve74825322002-03-18 03:15:35 +0000628}
629
630
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000631// Does not create any instructions if we cannot exploit constant to
632// create a cheaper instruction.
633//
634static inline void
635CreateCheapestMulConstInstruction(const TargetMachine &target,
Vikram S. Adve242a8082002-05-19 15:25:51 +0000636 Function* F,
637 Value* lval, Value* rval,
638 Instruction* destVal,
639 vector<MachineInstr*>& mvec,
640 MachineCodeForInstruction& mcfi)
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000641{
642 Value* constOp;
643 if (isa<Constant>(lval) && isa<Constant>(rval))
644 { // both operands are constant: try both orders!
645 vector<MachineInstr*> mvec1, mvec2;
Vikram S. Adve242a8082002-05-19 15:25:51 +0000646 unsigned int lcost = CreateMulConstInstruction(target, F, lval, rval,
647 destVal, mvec1, mcfi);
648 unsigned int rcost = CreateMulConstInstruction(target, F, rval, lval,
649 destVal, mvec2, mcfi);
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000650 vector<MachineInstr*>& mincostMvec = (lcost <= rcost)? mvec1 : mvec2;
651 vector<MachineInstr*>& maxcostMvec = (lcost <= rcost)? mvec2 : mvec1;
652 mvec.insert(mvec.end(), mincostMvec.begin(), mincostMvec.end());
653
654 for (unsigned int i=0; i < maxcostMvec.size(); ++i)
655 delete maxcostMvec[i];
656 }
657 else if (isa<Constant>(rval)) // rval is constant, but not lval
Vikram S. Adve242a8082002-05-19 15:25:51 +0000658 CreateMulConstInstruction(target, F, lval, rval, destVal, mvec, mcfi);
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000659 else if (isa<Constant>(lval)) // lval is constant, but not rval
Vikram S. Adve242a8082002-05-19 15:25:51 +0000660 CreateMulConstInstruction(target, F, lval, rval, destVal, mvec, mcfi);
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000661
662 // else neither is constant
663 return;
664}
665
Vikram S. Adve74825322002-03-18 03:15:35 +0000666// Return NULL if we cannot exploit constant to create a cheaper instruction
667static inline void
Vikram S. Adve242a8082002-05-19 15:25:51 +0000668CreateMulInstruction(const TargetMachine &target, Function* F,
669 Value* lval, Value* rval, Instruction* destVal,
Vikram S. Adve74825322002-03-18 03:15:35 +0000670 vector<MachineInstr*>& mvec,
Vikram S. Adve242a8082002-05-19 15:25:51 +0000671 MachineCodeForInstruction& mcfi,
Vikram S. Adve74825322002-03-18 03:15:35 +0000672 MachineOpCode forceMulOp = INVALID_MACHINE_OPCODE)
673{
674 unsigned int L = mvec.size();
Vikram S. Adve242a8082002-05-19 15:25:51 +0000675 CreateCheapestMulConstInstruction(target,F, lval, rval, destVal, mvec, mcfi);
Vikram S. Adve74825322002-03-18 03:15:35 +0000676 if (mvec.size() == L)
677 { // no instructions were added so create MUL reg, reg, reg.
678 // Use FSMULD if both operands are actually floats cast to doubles.
679 // Otherwise, use the default opcode for the appropriate type.
680 MachineOpCode mulOp = ((forceMulOp != INVALID_MACHINE_OPCODE)
681 ? forceMulOp
682 : ChooseMulInstructionByType(destVal->getType()));
683 MachineInstr* M = new MachineInstr(mulOp);
684 M->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister, lval);
685 M->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister, rval);
686 M->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister, destVal);
687 mvec.push_back(M);
688 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000689}
690
691
Vikram S. Adve510eec72001-11-04 21:59:14 +0000692// Generate a divide instruction for Div or Rem.
693// For Rem, this assumes that the operand type will be signed if the result
694// type is signed. This is correct because they must have the same sign.
695//
Chris Lattner20b1ea02001-09-14 03:47:57 +0000696static inline MachineOpCode
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000697ChooseDivInstruction(TargetMachine &target,
698 const InstructionNode* instrNode)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000699{
700 MachineOpCode opCode = INVALID_OPCODE;
701
702 const Type* resultType = instrNode->getInstruction()->getType();
703
704 if (resultType->isIntegral())
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000705 opCode = resultType->isSigned()? SDIVX : UDIVX;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000706 else
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000707 switch(resultType->getPrimitiveID())
708 {
709 case Type::FloatTyID: opCode = FDIVS; break;
710 case Type::DoubleTyID: opCode = FDIVD; break;
711 default: assert(0 && "Invalid type for DIV instruction"); break;
712 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000713
714 return opCode;
715}
716
717
Vikram S. Adve74825322002-03-18 03:15:35 +0000718// Return NULL if we cannot exploit constant to create a cheaper instruction
719static inline void
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000720CreateDivConstInstruction(TargetMachine &target,
721 const InstructionNode* instrNode,
Vikram S. Adve74825322002-03-18 03:15:35 +0000722 vector<MachineInstr*>& mvec)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000723{
Vikram S. Adve74825322002-03-18 03:15:35 +0000724 MachineInstr* minstr1 = NULL;
725 MachineInstr* minstr2 = NULL;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000726
727 Value* constOp = ((InstrTreeNode*) instrNode->rightChild())->getValue();
Vikram S. Adve74825322002-03-18 03:15:35 +0000728 if (! isa<Constant>(constOp))
729 return;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000730
731 // Cases worth optimizing are:
732 // (1) Divide by 1 for any type: replace with copy (ADD or FMOV)
733 // (2) Divide by 2^x for integer types: replace with SR[L or A]{X}
734 //
735 const Type* resultType = instrNode->getInstruction()->getType();
736
737 if (resultType->isIntegral())
738 {
739 unsigned pow;
740 bool isValidConst;
741 int64_t C = GetConstantValueAsSignedInt(constOp, isValidConst);
742 if (isValidConst)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000743 {
744 bool needNeg = false;
745 if (C < 0)
746 {
747 needNeg = true;
748 C = -C;
749 }
750
751 if (C == 1)
752 {
Vikram S. Adve74825322002-03-18 03:15:35 +0000753 minstr1 = new MachineInstr(ADD);
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000754 minstr1->SetMachineOperandVal(0,
755 MachineOperand::MO_VirtualRegister,
756 instrNode->leftChild()->getValue());
757 minstr1->SetMachineOperandReg(1,
758 target.getRegInfo().getZeroRegNum());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000759 }
Chris Lattner36346c72002-05-19 21:20:19 +0000760 else if (isPowerOf2(C, pow))
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000761 {
762 MachineOpCode opCode= ((resultType->isSigned())
763 ? (resultType==Type::LongTy)? SRAX : SRA
764 : (resultType==Type::LongTy)? SRLX : SRL);
Vikram S. Adve74825322002-03-18 03:15:35 +0000765 minstr1 = new MachineInstr(opCode);
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000766 minstr1->SetMachineOperandVal(0,
767 MachineOperand::MO_VirtualRegister,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000768 instrNode->leftChild()->getValue());
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000769 minstr1->SetMachineOperandConst(1,
770 MachineOperand::MO_UnextendedImmed,
771 pow);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000772 }
773
Vikram S. Adve74825322002-03-18 03:15:35 +0000774 if (minstr1 && needNeg)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000775 { // insert <reg = SUB 0, reg> after the instr to flip the sign
Vikram S. Adve74825322002-03-18 03:15:35 +0000776 minstr2 = CreateIntNegInstruction(target,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000777 instrNode->getValue());
778 }
779 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000780 }
781 else
782 {
Chris Lattner9b625032002-05-06 16:15:30 +0000783 if (ConstantFP *FPC = dyn_cast<ConstantFP>(constOp))
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000784 {
Chris Lattner9b625032002-05-06 16:15:30 +0000785 double dval = FPC->getValue();
Vikram S. Adve6ad7c552001-11-09 02:18:16 +0000786 if (fabs(dval) == 1)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000787 {
788 bool needNeg = (dval < 0);
789
790 MachineOpCode opCode = needNeg
791 ? (resultType == Type::FloatTy? FNEGS : FNEGD)
792 : (resultType == Type::FloatTy? FMOVS : FMOVD);
793
Vikram S. Adve74825322002-03-18 03:15:35 +0000794 minstr1 = new MachineInstr(opCode);
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000795 minstr1->SetMachineOperandVal(0,
796 MachineOperand::MO_VirtualRegister,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000797 instrNode->leftChild()->getValue());
798 }
799 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000800 }
801
Vikram S. Adve74825322002-03-18 03:15:35 +0000802 if (minstr1 != NULL)
803 minstr1->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister,
804 instrNode->getValue());
Chris Lattner20b1ea02001-09-14 03:47:57 +0000805
Vikram S. Adve74825322002-03-18 03:15:35 +0000806 if (minstr1)
807 mvec.push_back(minstr1);
808 if (minstr2)
809 mvec.push_back(minstr2);
Chris Lattner20b1ea02001-09-14 03:47:57 +0000810}
811
812
Vikram S. Adve74825322002-03-18 03:15:35 +0000813static void
814CreateCodeForVariableSizeAlloca(const TargetMachine& target,
815 Instruction* result,
816 unsigned int tsize,
817 Value* numElementsVal,
818 vector<MachineInstr*>& getMvec)
819{
820 MachineInstr* M;
821
822 // Create a Value to hold the (constant) element size
823 Value* tsizeVal = ConstantSInt::get(Type::IntTy, tsize);
824
825 // Get the constant offset from SP for dynamically allocated storage
826 // and create a temporary Value to hold it.
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000827 assert(result && result->getParent() && "Result value is not part of a fn?");
828 Function *F = result->getParent()->getParent();
829 MachineCodeForMethod& mcInfo = MachineCodeForMethod::get(F);
Vikram S. Adve74825322002-03-18 03:15:35 +0000830 bool growUp;
831 ConstantSInt* dynamicAreaOffset =
832 ConstantSInt::get(Type::IntTy,
833 target.getFrameInfo().getDynamicAreaOffset(mcInfo,growUp));
834 assert(! growUp && "Has SPARC v9 stack frame convention changed?");
835
836 // Create a temporary value to hold the result of MUL
837 TmpInstruction* tmpProd = new TmpInstruction(numElementsVal, tsizeVal);
838 MachineCodeForInstruction::get(result).addTemp(tmpProd);
839
840 // Instruction 1: mul numElements, typeSize -> tmpProd
841 M = new MachineInstr(MULX);
842 M->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister, numElementsVal);
843 M->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister, tsizeVal);
844 M->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister, tmpProd);
845 getMvec.push_back(M);
846
847 // Instruction 2: sub %sp, tmpProd -> %sp
848 M = new MachineInstr(SUB);
849 M->SetMachineOperandReg(0, target.getRegInfo().getStackPointer());
850 M->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister, tmpProd);
851 M->SetMachineOperandReg(2, target.getRegInfo().getStackPointer());
852 getMvec.push_back(M);
853
854 // Instruction 3: add %sp, frameSizeBelowDynamicArea -> result
855 M = new MachineInstr(ADD);
856 M->SetMachineOperandReg(0, target.getRegInfo().getStackPointer());
857 M->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister, dynamicAreaOffset);
858 M->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister, result);
859 getMvec.push_back(M);
860}
861
862
863static void
864CreateCodeForFixedSizeAlloca(const TargetMachine& target,
865 Instruction* result,
866 unsigned int tsize,
867 unsigned int numElements,
868 vector<MachineInstr*>& getMvec)
869{
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000870 assert(result && result->getParent() &&
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000871 "Result value is not part of a function?");
872 Function *F = result->getParent()->getParent();
873 MachineCodeForMethod &mcInfo = MachineCodeForMethod::get(F);
Vikram S. Adve74825322002-03-18 03:15:35 +0000874
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000875 // Check if the offset would small enough to use as an immediate in
876 // load/stores (check LDX because all load/stores have the same-size immediate
877 // field). If not, put the variable in the dynamically sized area of the
878 // frame.
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000879 unsigned int paddedSizeIgnored;
Vikram S. Adve74825322002-03-18 03:15:35 +0000880 int offsetFromFP = mcInfo.computeOffsetforLocalVar(target, result,
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000881 paddedSizeIgnored,
Vikram S. Adve74825322002-03-18 03:15:35 +0000882 tsize * numElements);
883 if (! target.getInstrInfo().constantFitsInImmedField(LDX, offsetFromFP))
884 {
885 CreateCodeForVariableSizeAlloca(target, result, tsize,
886 ConstantSInt::get(Type::IntTy,numElements),
887 getMvec);
888 return;
889 }
890
891 // else offset fits in immediate field so go ahead and allocate it.
892 offsetFromFP = mcInfo.allocateLocalVar(target, result, tsize * numElements);
893
894 // Create a temporary Value to hold the constant offset.
895 // This is needed because it may not fit in the immediate field.
896 ConstantSInt* offsetVal = ConstantSInt::get(Type::IntTy, offsetFromFP);
897
898 // Instruction 1: add %fp, offsetFromFP -> result
899 MachineInstr* M = new MachineInstr(ADD);
900 M->SetMachineOperandReg(0, target.getRegInfo().getFramePointer());
901 M->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister, offsetVal);
902 M->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister, result);
903
904 getMvec.push_back(M);
905}
906
907
908
Vikram S. Adve242a8082002-05-19 15:25:51 +0000909
910
Chris Lattner20b1ea02001-09-14 03:47:57 +0000911//------------------------------------------------------------------------
912// Function SetOperandsForMemInstr
913//
914// Choose addressing mode for the given load or store instruction.
915// Use [reg+reg] if it is an indexed reference, and the index offset is
916// not a constant or if it cannot fit in the offset field.
917// Use [reg+offset] in all other cases.
918//
919// This assumes that all array refs are "lowered" to one of these forms:
920// %x = load (subarray*) ptr, constant ; single constant offset
921// %x = load (subarray*) ptr, offsetVal ; single non-constant offset
922// Generally, this should happen via strength reduction + LICM.
923// Also, strength reduction should take care of using the same register for
924// the loop index variable and an array index, when that is profitable.
925//------------------------------------------------------------------------
926
927static void
Vikram S. Adve74825322002-03-18 03:15:35 +0000928SetOperandsForMemInstr(vector<MachineInstr*>& mvec,
929 vector<MachineInstr*>::iterator mvecI,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000930 const InstructionNode* vmInstrNode,
931 const TargetMachine& target)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000932{
933 MemAccessInst* memInst = (MemAccessInst*) vmInstrNode->getInstruction();
934
Vikram S. Adve242a8082002-05-19 15:25:51 +0000935 // Variables to hold the index vector and ptr value.
Chris Lattner20b1ea02001-09-14 03:47:57 +0000936 // The major work here is to extract these for all 3 instruction types
Vikram S. Adve242a8082002-05-19 15:25:51 +0000937 // and to try to fold chains of constant indices into a single offset.
938 // After that, we call SetMemOperands_Internal(), which creates the
939 // appropriate operands for the machine instruction.
Vikram S. Advea10d1a72002-03-31 19:07:35 +0000940 vector<Value*> idxVec;
Vikram S. Adve242a8082002-05-19 15:25:51 +0000941 bool allConstantIndices = true;
942 Value* ptrVal = memInst->getPointerOperand();
Vikram S. Advea10d1a72002-03-31 19:07:35 +0000943
944 // If there is a GetElemPtr instruction to fold in to this instr,
945 // it must be in the left child for Load and GetElemPtr, and in the
946 // right child for Store instructions.
Chris Lattner20b1ea02001-09-14 03:47:57 +0000947 InstrTreeNode* ptrChild = (vmInstrNode->getOpLabel() == Instruction::Store
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000948 ? vmInstrNode->rightChild()
949 : vmInstrNode->leftChild());
Chris Lattner20b1ea02001-09-14 03:47:57 +0000950
Vikram S. Adve242a8082002-05-19 15:25:51 +0000951 // Check if all indices are constant for this instruction
952 for (MemAccessInst::op_iterator OI=memInst->idx_begin();
953 OI != memInst->idx_end(); ++OI)
954 if (! isa<ConstantUInt>(*OI))
955 {
956 allConstantIndices = false;
957 break;
958 }
959
960 // If we have only constant indices, fold chains of constant indices
961 // in this and any preceding GetElemPtr instructions.
962 if (allConstantIndices &&
963 ptrChild->getOpLabel() == Instruction::GetElementPtr ||
964 ptrChild->getOpLabel() == GetElemPtrIdx)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000965 {
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000966 Value* newPtr = FoldGetElemChain((InstructionNode*) ptrChild, idxVec);
967 if (newPtr)
968 ptrVal = newPtr;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000969 }
970
Vikram S. Adve242a8082002-05-19 15:25:51 +0000971 // Append the index vector of the current instruction, if any.
972 // Discard any leading [0] index.
973 if (memInst->idx_begin() != memInst->idx_end())
974 {
Chris Lattnerdb241dc2002-06-05 18:11:37 +0000975 const ConstantUInt* CV = dyn_cast<ConstantUInt>(memInst->idx_begin()->get());
Vikram S. Adve242a8082002-05-19 15:25:51 +0000976 unsigned zeroOrIOne = (CV && CV->getType() == Type::UIntTy &&
977 (CV->getValue() == 0))? 1 : 0;
978 idxVec.insert(idxVec.end(),
979 memInst->idx_begin()+zeroOrIOne, memInst->idx_end());
980 }
981
982 // Now create the appropriate operands for the machine instruction
983 SetMemOperands_Internal(mvec, mvecI, vmInstrNode,
984 ptrVal, idxVec, allConstantIndices, target);
Chris Lattner20b1ea02001-09-14 03:47:57 +0000985}
986
987
Vikram S. Adve74825322002-03-18 03:15:35 +0000988// Generate the correct operands (and additional instructions if needed)
989// for the given pointer and given index vector.
990//
Chris Lattner20b1ea02001-09-14 03:47:57 +0000991static void
Vikram S. Adve74825322002-03-18 03:15:35 +0000992SetMemOperands_Internal(vector<MachineInstr*>& mvec,
993 vector<MachineInstr*>::iterator mvecI,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000994 const InstructionNode* vmInstrNode,
995 Value* ptrVal,
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000996 vector<Value*>& idxVec,
Vikram S. Adve242a8082002-05-19 15:25:51 +0000997 bool allConstantIndices,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000998 const TargetMachine& target)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000999{
1000 MemAccessInst* memInst = (MemAccessInst*) vmInstrNode->getInstruction();
1001
1002 // Initialize so we default to storing the offset in a register.
Chris Lattner8e5c0b42001-11-07 14:01:59 +00001003 int64_t smallConstOffset = 0;
Chris Lattner20b1ea02001-09-14 03:47:57 +00001004 Value* valueForRegOffset = NULL;
1005 MachineOperand::MachineOperandType offsetOpType =MachineOperand::MO_VirtualRegister;
1006
Vikram S. Adve74825322002-03-18 03:15:35 +00001007 // Check if there is an index vector and if so, compute the
1008 // right offset for structures and for arrays
Chris Lattner20b1ea02001-09-14 03:47:57 +00001009 //
1010 if (idxVec.size() > 0)
1011 {
Vikram S. Adve74825322002-03-18 03:15:35 +00001012 const PointerType* ptrType = cast<PointerType>(ptrVal->getType());
Chris Lattner20b1ea02001-09-14 03:47:57 +00001013
Vikram S. Adve242a8082002-05-19 15:25:51 +00001014 // If all indices are constant, compute the combined offset directly.
1015 if (allConstantIndices)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001016 {
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001017 // Compute the offset value using the index vector. Create a
1018 // virtual reg. for it since it may not fit in the immed field.
Vikram S. Adve242a8082002-05-19 15:25:51 +00001019 uint64_t offset = target.DataLayout.getIndexedOffset(ptrType,idxVec);
1020 valueForRegOffset = ConstantSInt::get(Type::LongTy, offset);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001021 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00001022 else
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001023 {
Vikram S. Adve242a8082002-05-19 15:25:51 +00001024 // There is at least one non-constant offset. Therefore, this must
1025 // be an array ref, and must have been lowered to a single offset.
Vikram S. Adve74825322002-03-18 03:15:35 +00001026 assert((memInst->getNumOperands()
1027 == (unsigned) 1 + memInst->getFirstIndexOperandNumber())
1028 && "Array refs must be lowered before Instruction Selection");
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001029
Vikram S. Adve74825322002-03-18 03:15:35 +00001030 Value* arrayOffsetVal = * memInst->idx_begin();
1031
Vikram S. Adve242a8082002-05-19 15:25:51 +00001032 // Handle special common case of leading [0] index.
1033 ConstantUInt* CV = dyn_cast<ConstantUInt>(idxVec.front());
1034 bool firstIndexIsZero = bool(CV && CV->getType() == Type::UIntTy &&
1035 (CV->getValue() == 0));
1036
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001037 // If index is 0, the offset value is just 0. Otherwise,
1038 // generate a MUL instruction to compute address from index.
1039 // The call to getTypeSize() will fail if size is not constant.
1040 // CreateMulInstruction() folds constants intelligently enough.
1041 //
1042 if (firstIndexIsZero)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001043 {
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001044 offsetOpType = MachineOperand::MO_SignExtendedImmed;
1045 smallConstOffset = 0;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001046 }
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001047 else
1048 {
1049 vector<MachineInstr*> mulVec;
1050 Instruction* addr = new TmpInstruction(Type::UIntTy, memInst);
1051 MachineCodeForInstruction::get(memInst).addTemp(addr);
1052
1053 unsigned int eltSize =
1054 target.DataLayout.getTypeSize(ptrType->getElementType());
1055 assert(eltSize > 0 && "Invalid or non-const array element size");
1056 ConstantUInt* eltVal = ConstantUInt::get(Type::UIntTy, eltSize);
1057
1058 CreateMulInstruction(target,
Vikram S. Adve242a8082002-05-19 15:25:51 +00001059 memInst->getParent()->getParent(),
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001060 arrayOffsetVal, /* lval, not likely const */
1061 eltVal, /* rval, likely constant */
1062 addr, /* result*/
Vikram S. Adve242a8082002-05-19 15:25:51 +00001063 mulVec,
1064 MachineCodeForInstruction::get(memInst),
1065 INVALID_MACHINE_OPCODE);
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001066 assert(mulVec.size() > 0 && "No multiply instruction created?");
1067 for (vector<MachineInstr*>::const_iterator I = mulVec.begin();
1068 I != mulVec.end(); ++I)
1069 {
1070 mvecI = mvec.insert(mvecI, *I); // ptr to inserted value
1071 ++mvecI; // ptr to mem. instr.
1072 }
1073
1074 valueForRegOffset = addr;
1075 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001076 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00001077 }
1078 else
1079 {
1080 offsetOpType = MachineOperand::MO_SignExtendedImmed;
1081 smallConstOffset = 0;
1082 }
1083
Vikram S. Advea10d1a72002-03-31 19:07:35 +00001084 // For STORE:
1085 // Operand 0 is value, operand 1 is ptr, operand 2 is offset
1086 // For LOAD or GET_ELEMENT_PTR,
1087 // Operand 0 is ptr, operand 1 is offset, operand 2 is result.
1088 //
1089 unsigned offsetOpNum, ptrOpNum;
1090 if (memInst->getOpcode() == Instruction::Store)
1091 {
1092 (*mvecI)->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister,
1093 vmInstrNode->leftChild()->getValue());
1094 ptrOpNum = 1;
1095 offsetOpNum = 2;
1096 }
1097 else
1098 {
1099 ptrOpNum = 0;
1100 offsetOpNum = 1;
1101 (*mvecI)->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister,
1102 memInst);
1103 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00001104
Vikram S. Advea10d1a72002-03-31 19:07:35 +00001105 (*mvecI)->SetMachineOperandVal(ptrOpNum, MachineOperand::MO_VirtualRegister,
1106 ptrVal);
1107
Chris Lattner20b1ea02001-09-14 03:47:57 +00001108 if (offsetOpType == MachineOperand::MO_VirtualRegister)
1109 {
1110 assert(valueForRegOffset != NULL);
Vikram S. Adve74825322002-03-18 03:15:35 +00001111 (*mvecI)->SetMachineOperandVal(offsetOpNum, offsetOpType,
1112 valueForRegOffset);
Chris Lattner20b1ea02001-09-14 03:47:57 +00001113 }
1114 else
Vikram S. Adve74825322002-03-18 03:15:35 +00001115 (*mvecI)->SetMachineOperandConst(offsetOpNum, offsetOpType,
1116 smallConstOffset);
Chris Lattner20b1ea02001-09-14 03:47:57 +00001117}
1118
1119
Chris Lattner20b1ea02001-09-14 03:47:57 +00001120//
1121// Substitute operand `operandNum' of the instruction in node `treeNode'
Vikram S. Advec025fc12001-10-14 23:28:43 +00001122// in place of the use(s) of that instruction in node `parent'.
1123// Check both explicit and implicit operands!
Vikram S. Adve74825322002-03-18 03:15:35 +00001124// Also make sure to skip over a parent who:
1125// (1) is a list node in the Burg tree, or
1126// (2) itself had its results forwarded to its parent
Chris Lattner20b1ea02001-09-14 03:47:57 +00001127//
1128static void
1129ForwardOperand(InstructionNode* treeNode,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001130 InstrTreeNode* parent,
1131 int operandNum)
Chris Lattner20b1ea02001-09-14 03:47:57 +00001132{
Vikram S. Adve243dd452001-09-18 13:03:13 +00001133 assert(treeNode && parent && "Invalid invocation of ForwardOperand");
1134
Chris Lattner20b1ea02001-09-14 03:47:57 +00001135 Instruction* unusedOp = treeNode->getInstruction();
1136 Value* fwdOp = unusedOp->getOperand(operandNum);
Vikram S. Adve243dd452001-09-18 13:03:13 +00001137
1138 // The parent itself may be a list node, so find the real parent instruction
1139 while (parent->getNodeType() != InstrTreeNode::NTInstructionNode)
1140 {
1141 parent = parent->parent();
1142 assert(parent && "ERROR: Non-instruction node has no parent in tree.");
1143 }
1144 InstructionNode* parentInstrNode = (InstructionNode*) parent;
1145
1146 Instruction* userInstr = parentInstrNode->getInstruction();
Chris Lattner9c461082002-02-03 07:50:56 +00001147 MachineCodeForInstruction &mvec = MachineCodeForInstruction::get(userInstr);
Vikram S. Adve74825322002-03-18 03:15:35 +00001148
1149 // The parent's mvec would be empty if it was itself forwarded.
1150 // Recursively call ForwardOperand in that case...
1151 //
1152 if (mvec.size() == 0)
Chris Lattner20b1ea02001-09-14 03:47:57 +00001153 {
Vikram S. Adve74825322002-03-18 03:15:35 +00001154 assert(parent->parent() != NULL &&
1155 "Parent could not have been forwarded, yet has no instructions?");
1156 ForwardOperand(treeNode, parent->parent(), operandNum);
1157 }
1158 else
1159 {
Vikram S. Adve74825322002-03-18 03:15:35 +00001160 for (unsigned i=0, N=mvec.size(); i < N; i++)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001161 {
Vikram S. Adve74825322002-03-18 03:15:35 +00001162 MachineInstr* minstr = mvec[i];
1163 for (unsigned i=0, numOps=minstr->getNumOperands(); i < numOps; ++i)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001164 {
Vikram S. Adve74825322002-03-18 03:15:35 +00001165 const MachineOperand& mop = minstr->getOperand(i);
1166 if (mop.getOperandType() == MachineOperand::MO_VirtualRegister &&
1167 mop.getVRegValue() == unusedOp)
Vikram S. Adve242a8082002-05-19 15:25:51 +00001168 minstr->SetMachineOperandVal(i,
Vikram S. Adve74825322002-03-18 03:15:35 +00001169 MachineOperand::MO_VirtualRegister, fwdOp);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001170 }
Vikram S. Adve74825322002-03-18 03:15:35 +00001171
1172 for (unsigned i=0,numOps=minstr->getNumImplicitRefs(); i<numOps; ++i)
1173 if (minstr->getImplicitRef(i) == unusedOp)
Vikram S. Adve242a8082002-05-19 15:25:51 +00001174 minstr->setImplicitRef(i, fwdOp,
Vikram S. Adve6418eac2002-07-08 23:30:14 +00001175 minstr->implicitRefIsDefined(i),
1176 minstr->implicitRefIsDefinedAndUsed(i));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001177 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00001178 }
1179}
1180
1181
Vikram S. Adve242a8082002-05-19 15:25:51 +00001182inline bool
1183AllUsesAreBranches(const Instruction* setccI)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001184{
Vikram S. Adve242a8082002-05-19 15:25:51 +00001185 for (Value::use_const_iterator UI=setccI->use_begin(), UE=setccI->use_end();
1186 UI != UE; ++UI)
1187 if (! isa<TmpInstruction>(*UI) // ignore tmp instructions here
1188 && cast<Instruction>(*UI)->getOpcode() != Instruction::Br)
1189 return false;
1190 return true;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001191}
1192
Vikram S. Advefb361122001-10-22 13:36:31 +00001193//******************* Externally Visible Functions *************************/
1194
Vikram S. Advefb361122001-10-22 13:36:31 +00001195//------------------------------------------------------------------------
1196// External Function: ThisIsAChainRule
1197//
1198// Purpose:
1199// Check if a given BURG rule is a chain rule.
1200//------------------------------------------------------------------------
1201
1202extern bool
1203ThisIsAChainRule(int eruleno)
1204{
1205 switch(eruleno)
1206 {
1207 case 111: // stmt: reg
1208 case 113: // stmt: bool
1209 case 123:
1210 case 124:
1211 case 125:
1212 case 126:
1213 case 127:
1214 case 128:
1215 case 129:
1216 case 130:
1217 case 131:
1218 case 132:
1219 case 133:
1220 case 155:
1221 case 221:
1222 case 222:
1223 case 241:
1224 case 242:
1225 case 243:
1226 case 244:
Vikram S. Adve85e1e9c2002-04-01 20:28:48 +00001227 case 321:
Vikram S. Advefb361122001-10-22 13:36:31 +00001228 return true; break;
1229
1230 default:
1231 return false; break;
1232 }
1233}
Chris Lattner20b1ea02001-09-14 03:47:57 +00001234
1235
1236//------------------------------------------------------------------------
1237// External Function: GetInstructionsByRule
1238//
1239// Purpose:
1240// Choose machine instructions for the SPARC according to the
1241// patterns chosen by the BURG-generated parser.
1242//------------------------------------------------------------------------
1243
Vikram S. Adve74825322002-03-18 03:15:35 +00001244void
Chris Lattner20b1ea02001-09-14 03:47:57 +00001245GetInstructionsByRule(InstructionNode* subtreeRoot,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001246 int ruleForNode,
1247 short* nts,
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001248 TargetMachine &target,
Vikram S. Adve74825322002-03-18 03:15:35 +00001249 vector<MachineInstr*>& mvec)
Chris Lattner20b1ea02001-09-14 03:47:57 +00001250{
Chris Lattner20b1ea02001-09-14 03:47:57 +00001251 bool checkCast = false; // initialize here to use fall-through
Chris Lattner20b1ea02001-09-14 03:47:57 +00001252 int nextRule;
1253 int forwardOperandNum = -1;
Vikram S. Adve74825322002-03-18 03:15:35 +00001254 unsigned int allocaSize = 0;
1255 MachineInstr* M, *M2;
1256 unsigned int L;
1257
1258 mvec.clear();
Chris Lattner20b1ea02001-09-14 03:47:57 +00001259
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001260 // If the code for this instruction was folded into the parent (user),
1261 // then do nothing!
1262 if (subtreeRoot->isFoldedIntoParent())
1263 return;
1264
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001265 //
1266 // Let's check for chain rules outside the switch so that we don't have
1267 // to duplicate the list of chain rule production numbers here again
1268 //
1269 if (ThisIsAChainRule(ruleForNode))
Chris Lattner20b1ea02001-09-14 03:47:57 +00001270 {
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001271 // Chain rules have a single nonterminal on the RHS.
1272 // Get the rule that matches the RHS non-terminal and use that instead.
1273 //
1274 assert(nts[0] && ! nts[1]
1275 && "A chain rule should have only one RHS non-terminal!");
1276 nextRule = burm_rule(subtreeRoot->state, nts[0]);
1277 nts = burm_nts[nextRule];
Vikram S. Adve74825322002-03-18 03:15:35 +00001278 GetInstructionsByRule(subtreeRoot, nextRule, nts, target, mvec);
Chris Lattner20b1ea02001-09-14 03:47:57 +00001279 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001280 else
Chris Lattner20b1ea02001-09-14 03:47:57 +00001281 {
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001282 switch(ruleForNode) {
1283 case 1: // stmt: Ret
1284 case 2: // stmt: RetValue(reg)
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001285 { // NOTE: Prepass of register allocation is responsible
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001286 // for moving return value to appropriate register.
1287 // Mark the return-address register as a hidden virtual reg.
Vikram S. Advea995e602001-10-11 04:23:19 +00001288 // Mark the return value register as an implicit ref of
1289 // the machine instruction.
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001290 // Finally put a NOP in the delay slot.
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001291 ReturnInst *returnInstr =
1292 cast<ReturnInst>(subtreeRoot->getInstruction());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001293 assert(returnInstr->getOpcode() == Instruction::Ret);
1294
Chris Lattner9c461082002-02-03 07:50:56 +00001295 Instruction* returnReg = new TmpInstruction(returnInstr);
1296 MachineCodeForInstruction::get(returnInstr).addTemp(returnReg);
Vikram S. Advefb361122001-10-22 13:36:31 +00001297
Vikram S. Adve74825322002-03-18 03:15:35 +00001298 M = new MachineInstr(JMPLRET);
1299 M->SetMachineOperandReg(0, MachineOperand::MO_VirtualRegister,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001300 returnReg);
Vikram S. Adve74825322002-03-18 03:15:35 +00001301 M->SetMachineOperandConst(1,MachineOperand::MO_SignExtendedImmed,
Chris Lattner697954c2002-01-20 22:54:45 +00001302 (int64_t)8);
Vikram S. Adve74825322002-03-18 03:15:35 +00001303 M->SetMachineOperandReg(2, target.getRegInfo().getZeroRegNum());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001304
Vikram S. Advea995e602001-10-11 04:23:19 +00001305 if (returnInstr->getReturnValue() != NULL)
Vikram S. Adve74825322002-03-18 03:15:35 +00001306 M->addImplicitRef(returnInstr->getReturnValue());
Vikram S. Advea995e602001-10-11 04:23:19 +00001307
Vikram S. Adve74825322002-03-18 03:15:35 +00001308 mvec.push_back(M);
1309 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001310
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001311 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001312 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001313
1314 case 3: // stmt: Store(reg,reg)
1315 case 4: // stmt: Store(reg,ptrreg)
Vikram S. Adve74825322002-03-18 03:15:35 +00001316 mvec.push_back(new MachineInstr(
1317 ChooseStoreInstruction(
1318 subtreeRoot->leftChild()->getValue()->getType())));
1319 SetOperandsForMemInstr(mvec, mvec.end()-1, subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001320 break;
Chris Lattner20b1ea02001-09-14 03:47:57 +00001321
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001322 case 5: // stmt: BrUncond
Vikram S. Adve74825322002-03-18 03:15:35 +00001323 M = new MachineInstr(BA);
Vikram S. Adve6418eac2002-07-08 23:30:14 +00001324 M->SetMachineOperandVal(0, MachineOperand::MO_PCRelativeDisp,
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001325 cast<BranchInst>(subtreeRoot->getInstruction())->getSuccessor(0));
Vikram S. Adve74825322002-03-18 03:15:35 +00001326 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001327
1328 // delay slot
Vikram S. Adve74825322002-03-18 03:15:35 +00001329 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001330 break;
Chris Lattner20b1ea02001-09-14 03:47:57 +00001331
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001332 case 206: // stmt: BrCond(setCCconst)
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001333 { // setCCconst => boolean was computed with `%b = setCC type reg1 const'
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001334 // If the constant is ZERO, we can use the branch-on-integer-register
1335 // instructions and avoid the SUBcc instruction entirely.
1336 // Otherwise this is just the same as case 5, so just fall through.
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001337 //
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001338 InstrTreeNode* constNode = subtreeRoot->leftChild()->rightChild();
1339 assert(constNode &&
1340 constNode->getNodeType() ==InstrTreeNode::NTConstNode);
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001341 Constant *constVal = cast<Constant>(constNode->getValue());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001342 bool isValidConst;
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001343
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001344 if ((constVal->getType()->isIntegral()
Chris Lattner9b625032002-05-06 16:15:30 +00001345 || isa<PointerType>(constVal->getType()))
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001346 && GetConstantValueAsSignedInt(constVal, isValidConst) == 0
1347 && isValidConst)
1348 {
1349 // That constant is a zero after all...
1350 // Use the left child of setCC as the first argument!
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001351 // Mark the setCC node so that no code is generated for it.
1352 InstructionNode* setCCNode = (InstructionNode*)
1353 subtreeRoot->leftChild();
1354 assert(setCCNode->getOpLabel() == SetCCOp);
1355 setCCNode->markFoldedIntoParent();
1356
1357 BranchInst* brInst=cast<BranchInst>(subtreeRoot->getInstruction());
1358
Vikram S. Adve74825322002-03-18 03:15:35 +00001359 M = new MachineInstr(ChooseBprInstruction(subtreeRoot));
1360 M->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister,
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001361 setCCNode->leftChild()->getValue());
Vikram S. Adve74825322002-03-18 03:15:35 +00001362 M->SetMachineOperandVal(1, MachineOperand::MO_PCRelativeDisp,
1363 brInst->getSuccessor(0));
1364 mvec.push_back(M);
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001365
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001366 // delay slot
Vikram S. Adve74825322002-03-18 03:15:35 +00001367 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001368
1369 // false branch
Vikram S. Adve74825322002-03-18 03:15:35 +00001370 M = new MachineInstr(BA);
Vikram S. Adve6418eac2002-07-08 23:30:14 +00001371 M->SetMachineOperandVal(0, MachineOperand::MO_PCRelativeDisp,
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001372 brInst->getSuccessor(1));
Vikram S. Adve74825322002-03-18 03:15:35 +00001373 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001374
1375 // delay slot
Vikram S. Adve74825322002-03-18 03:15:35 +00001376 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001377
1378 break;
1379 }
1380 // ELSE FALL THROUGH
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001381 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001382
1383 case 6: // stmt: BrCond(bool)
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001384 { // bool => boolean was computed with some boolean operator
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001385 // (SetCC, Not, ...). We need to check whether the type was a FP,
1386 // signed int or unsigned int, and check the branching condition in
1387 // order to choose the branch to use.
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001388 // If it is an integer CC, we also need to find the unique
1389 // TmpInstruction representing that CC.
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001390 //
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001391 BranchInst* brInst = cast<BranchInst>(subtreeRoot->getInstruction());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001392 bool isFPBranch;
Vikram S. Adve74825322002-03-18 03:15:35 +00001393 M = new MachineInstr(ChooseBccInstruction(subtreeRoot, isFPBranch));
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001394
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001395 Value* ccValue = GetTmpForCC(subtreeRoot->leftChild()->getValue(),
1396 brInst->getParent()->getParent(),
1397 isFPBranch? Type::FloatTy : Type::IntTy);
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001398
Vikram S. Adve74825322002-03-18 03:15:35 +00001399 M->SetMachineOperandVal(0, MachineOperand::MO_CCRegister, ccValue);
1400 M->SetMachineOperandVal(1, MachineOperand::MO_PCRelativeDisp,
1401 brInst->getSuccessor(0));
1402 mvec.push_back(M);
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001403
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001404 // delay slot
Vikram S. Adve74825322002-03-18 03:15:35 +00001405 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001406
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001407 // false branch
Vikram S. Adve74825322002-03-18 03:15:35 +00001408 M = new MachineInstr(BA);
Vikram S. Adve6418eac2002-07-08 23:30:14 +00001409 M->SetMachineOperandVal(0, MachineOperand::MO_PCRelativeDisp,
Vikram S. Adve74825322002-03-18 03:15:35 +00001410 brInst->getSuccessor(1));
1411 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001412
1413 // delay slot
Vikram S. Adve74825322002-03-18 03:15:35 +00001414 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001415 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001416 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001417
1418 case 208: // stmt: BrCond(boolconst)
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001419 {
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001420 // boolconst => boolean is a constant; use BA to first or second label
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001421 Constant* constVal =
1422 cast<Constant>(subtreeRoot->leftChild()->getValue());
1423 unsigned dest = cast<ConstantBool>(constVal)->getValue()? 0 : 1;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001424
Vikram S. Adve74825322002-03-18 03:15:35 +00001425 M = new MachineInstr(BA);
Vikram S. Adve6418eac2002-07-08 23:30:14 +00001426 M->SetMachineOperandVal(0, MachineOperand::MO_PCRelativeDisp,
Chris Lattner35504202002-04-27 03:14:39 +00001427 cast<BranchInst>(subtreeRoot->getInstruction())->getSuccessor(dest));
Vikram S. Adve74825322002-03-18 03:15:35 +00001428 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001429
1430 // delay slot
Vikram S. Adve74825322002-03-18 03:15:35 +00001431 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001432 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001433 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001434
1435 case 8: // stmt: BrCond(boolreg)
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001436 { // boolreg => boolean is stored in an existing register.
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001437 // Just use the branch-on-integer-register instruction!
1438 //
Vikram S. Adve74825322002-03-18 03:15:35 +00001439 M = new MachineInstr(BRNZ);
1440 M->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001441 subtreeRoot->leftChild()->getValue());
Vikram S. Adve74825322002-03-18 03:15:35 +00001442 M->SetMachineOperandVal(1, MachineOperand::MO_PCRelativeDisp,
Chris Lattner35504202002-04-27 03:14:39 +00001443 cast<BranchInst>(subtreeRoot->getInstruction())->getSuccessor(0));
Vikram S. Adve74825322002-03-18 03:15:35 +00001444 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001445
1446 // delay slot
Vikram S. Adve74825322002-03-18 03:15:35 +00001447 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001448
1449 // false branch
Vikram S. Adve74825322002-03-18 03:15:35 +00001450 M = new MachineInstr(BA);
Vikram S. Adve6418eac2002-07-08 23:30:14 +00001451 M->SetMachineOperandVal(0, MachineOperand::MO_PCRelativeDisp,
Chris Lattner35504202002-04-27 03:14:39 +00001452 cast<BranchInst>(subtreeRoot->getInstruction())->getSuccessor(1));
Vikram S. Adve74825322002-03-18 03:15:35 +00001453 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001454
1455 // delay slot
Vikram S. Adve74825322002-03-18 03:15:35 +00001456 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001457 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001458 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00001459
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001460 case 9: // stmt: Switch(reg)
1461 assert(0 && "*** SWITCH instruction is not implemented yet.");
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001462 break;
Chris Lattner20b1ea02001-09-14 03:47:57 +00001463
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001464 case 10: // reg: VRegList(reg, reg)
1465 assert(0 && "VRegList should never be the topmost non-chain rule");
1466 break;
1467
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001468 case 21: // bool: Not(bool): Both these are implemented as:
Vikram S. Adve85e1e9c2002-04-01 20:28:48 +00001469 case 421: // reg: BNot(reg) : reg = reg XOR-NOT 0
Vikram S. Adve74825322002-03-18 03:15:35 +00001470 M = new MachineInstr(XNOR);
1471 M->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister,
1472 subtreeRoot->leftChild()->getValue());
1473 M->SetMachineOperandReg(1, target.getRegInfo().getZeroRegNum());
1474 M->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister,
1475 subtreeRoot->getValue());
1476 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001477 break;
1478
1479 case 322: // reg: ToBoolTy(bool):
1480 case 22: // reg: ToBoolTy(reg):
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001481 {
1482 const Type* opType = subtreeRoot->leftChild()->getValue()->getType();
Chris Lattner9b625032002-05-06 16:15:30 +00001483 assert(opType->isIntegral() || isa<PointerType>(opType)
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001484 || opType == Type::BoolTy);
Vikram S. Adve74825322002-03-18 03:15:35 +00001485 forwardOperandNum = 0; // forward first operand to user
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001486 break;
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001487 }
1488
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001489 case 23: // reg: ToUByteTy(reg)
1490 case 25: // reg: ToUShortTy(reg)
1491 case 27: // reg: ToUIntTy(reg)
1492 case 29: // reg: ToULongTy(reg)
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001493 {
Vikram S. Adve242a8082002-05-19 15:25:51 +00001494 Instruction* destI = subtreeRoot->getInstruction();
1495 Value* opVal = subtreeRoot->leftChild()->getValue();
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001496 const Type* opType = subtreeRoot->leftChild()->getValue()->getType();
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001497 assert(opType->isIntegral() ||
Chris Lattner9b625032002-05-06 16:15:30 +00001498 isa<PointerType>(opType) ||
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001499 opType == Type::BoolTy && "Cast is illegal for other types");
Vikram S. Adve242a8082002-05-19 15:25:51 +00001500
1501 unsigned opSize = target.DataLayout.getTypeSize(opType);
1502 unsigned destSize = target.DataLayout.getTypeSize(destI->getType());
1503
1504 if (opSize > destSize ||
1505 (opType->isSigned()
1506 && destSize < target.DataLayout.getIntegerRegize()))
1507 { // operand is larger than dest,
1508 // OR both are equal but smaller than the full register size
1509 // AND operand is signed, so it may have extra sign bits:
1510 // mask high bits using AND
1511 //
1512 M = Create3OperandInstr(AND, opVal,
1513 ConstantUInt::get(Type::ULongTy,
1514 ((uint64_t) 1 << 8*destSize) - 1),
1515 destI);
1516 mvec.push_back(M);
1517 }
1518 else
1519 forwardOperandNum = 0; // forward first operand to user
1520
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001521 break;
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001522 }
1523
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001524 case 24: // reg: ToSByteTy(reg)
1525 case 26: // reg: ToShortTy(reg)
1526 case 28: // reg: ToIntTy(reg)
1527 case 30: // reg: ToLongTy(reg)
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001528 {
Vikram S. Adve242a8082002-05-19 15:25:51 +00001529 unsigned int oldMvecSize = mvec.size(); // to check if it grew
1530 Instruction* destI = subtreeRoot->getInstruction();
1531 Value* opVal = subtreeRoot->leftChild()->getValue();
1532 MachineCodeForInstruction& mcfi =MachineCodeForInstruction::get(destI);
1533
1534 const Type* opType = opVal->getType();
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001535 if (opType->isIntegral()
Chris Lattner9b625032002-05-06 16:15:30 +00001536 || isa<PointerType>(opType)
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001537 || opType == Type::BoolTy)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001538 {
Vikram S. Adve242a8082002-05-19 15:25:51 +00001539 // These operand types have the same format as the destination,
1540 // but may have different size: add sign bits or mask as needed.
1541 //
1542 const Type* destType = destI->getType();
1543 unsigned opSize = target.DataLayout.getTypeSize(opType);
1544 unsigned destSize = target.DataLayout.getTypeSize(destType);
1545 if (opSize <= destSize && !opType->isSigned())
1546 { // operand is smaller than or same size as dest:
1547 // -- if operand is signed (checked above), nothing to do
1548 // -- if operand is unsigned, sign-extend the value:
1549 //
1550 target.getInstrInfo().CreateSignExtensionInstructions(target, destI->getParent()->getParent(), opVal, 8*opSize, destI, mvec, mcfi);
1551 }
1552 else if (opSize > destSize)
1553 { // operand is larger than dest: mask high bits using AND
1554 // and then sign-extend using SRA by 0!
1555 //
1556 TmpInstruction *tmpI = new TmpInstruction(destType, opVal,
1557 destI, "maskHi");
1558 mcfi.addTemp(tmpI);
1559 M = Create3OperandInstr(AND, opVal,
1560 ConstantUInt::get(Type::UIntTy,
1561 ((uint64_t) 1 << 8*destSize)-1),
1562 tmpI);
1563 mvec.push_back(M);
1564
1565 target.getInstrInfo().CreateSignExtensionInstructions(target, destI->getParent()->getParent(), tmpI, 8*destSize, destI, mvec, mcfi);
1566 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001567 }
1568 else
1569 {
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001570 // If the source operand is an FP type, the int result must be
1571 // copied from float to int register via memory!
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001572 Value* leftVal = subtreeRoot->leftChild()->getValue();
1573 Value* destForCast;
1574 vector<MachineInstr*> minstrVec;
1575
Chris Lattner9b625032002-05-06 16:15:30 +00001576 if (opType->isFloatingPoint())
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001577 {
1578 // Create a temporary to represent the INT register
1579 // into which the FP value will be copied via memory.
1580 // The type of this temporary will determine the FP
1581 // register used: single-prec for a 32-bit int or smaller,
1582 // double-prec for a 64-bit int.
1583 //
1584 const Type* destTypeToUse =
Vikram S. Adve242a8082002-05-19 15:25:51 +00001585 (destI->getType() == Type::LongTy)? Type::DoubleTy
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001586 : Type::FloatTy;
Chris Lattner9c461082002-02-03 07:50:56 +00001587 destForCast = new TmpInstruction(destTypeToUse, leftVal);
Vikram S. Advedbc4fad2002-04-25 04:37:51 +00001588 MachineCodeForInstruction &destMCFI =
Vikram S. Adve242a8082002-05-19 15:25:51 +00001589 MachineCodeForInstruction::get(destI);
Vikram S. Advedbc4fad2002-04-25 04:37:51 +00001590 destMCFI.addTemp(destForCast);
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001591
Vikram S. Adve242a8082002-05-19 15:25:51 +00001592 target.getInstrInfo().
1593 CreateCodeToCopyFloatToInt(target,
1594 destI->getParent()->getParent(),
1595 (TmpInstruction*) destForCast,
1596 destI, minstrVec, destMCFI);
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001597 }
1598 else
1599 destForCast = leftVal;
1600
Vikram S. Advedbc4fad2002-04-25 04:37:51 +00001601 M = CreateConvertToIntInstr(subtreeRoot->getOpLabel(),
1602 leftVal, destForCast);
Vikram S. Adve74825322002-03-18 03:15:35 +00001603 mvec.push_back(M);
Vikram S. Advedbc4fad2002-04-25 04:37:51 +00001604
Vikram S. Adve74825322002-03-18 03:15:35 +00001605 // Append the copy code, if any, after the conversion instr.
1606 mvec.insert(mvec.end(), minstrVec.begin(), minstrVec.end());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001607 }
Vikram S. Adve242a8082002-05-19 15:25:51 +00001608
1609 if (oldMvecSize == mvec.size()) // no instruction was generated
1610 forwardOperandNum = 0; // forward first operand to user
1611
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001612 break;
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001613 }
1614
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001615 case 31: // reg: ToFloatTy(reg):
1616 case 32: // reg: ToDoubleTy(reg):
1617 case 232: // reg: ToDoubleTy(Constant):
1618
1619 // If this instruction has a parent (a user) in the tree
1620 // and the user is translated as an FsMULd instruction,
1621 // then the cast is unnecessary. So check that first.
1622 // In the future, we'll want to do the same for the FdMULq instruction,
1623 // so do the check here instead of only for ToFloatTy(reg).
1624 //
1625 if (subtreeRoot->parent() != NULL &&
Chris Lattner9c461082002-02-03 07:50:56 +00001626 MachineCodeForInstruction::get(((InstructionNode*)subtreeRoot->parent())->getInstruction())[0]->getOpCode() == FSMULD)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001627 {
Vikram S. Adve74825322002-03-18 03:15:35 +00001628 forwardOperandNum = 0; // forward first operand to user
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001629 }
1630 else
1631 {
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001632 Value* leftVal = subtreeRoot->leftChild()->getValue();
1633 const Type* opType = leftVal->getType();
Vikram S. Advedbc4fad2002-04-25 04:37:51 +00001634 MachineOpCode opCode=ChooseConvertToFloatInstr(
1635 subtreeRoot->getOpLabel(), opType);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001636 if (opCode == INVALID_OPCODE) // no conversion needed
1637 {
Vikram S. Adve74825322002-03-18 03:15:35 +00001638 forwardOperandNum = 0; // forward first operand to user
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001639 }
1640 else
1641 {
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001642 // If the source operand is a non-FP type it must be
1643 // first copied from int to float register via memory!
1644 Instruction *dest = subtreeRoot->getInstruction();
1645 Value* srcForCast;
1646 int n = 0;
Vikram S. Adve242a8082002-05-19 15:25:51 +00001647 if (! opType->isFloatingPoint())
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001648 {
1649 // Create a temporary to represent the FP register
1650 // into which the integer will be copied via memory.
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001651 // The type of this temporary will determine the FP
1652 // register used: single-prec for a 32-bit int or smaller,
1653 // double-prec for a 64-bit int.
1654 //
1655 const Type* srcTypeToUse =
1656 (leftVal->getType() == Type::LongTy)? Type::DoubleTy
1657 : Type::FloatTy;
1658
Chris Lattner9c461082002-02-03 07:50:56 +00001659 srcForCast = new TmpInstruction(srcTypeToUse, dest);
Vikram S. Advedbc4fad2002-04-25 04:37:51 +00001660 MachineCodeForInstruction &destMCFI =
Chris Lattner9c461082002-02-03 07:50:56 +00001661 MachineCodeForInstruction::get(dest);
Vikram S. Advedbc4fad2002-04-25 04:37:51 +00001662 destMCFI.addTemp(srcForCast);
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001663
Vikram S. Adve242a8082002-05-19 15:25:51 +00001664 target.getInstrInfo().CreateCodeToCopyIntToFloat(target,
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001665 dest->getParent()->getParent(),
1666 leftVal, (TmpInstruction*) srcForCast,
Vikram S. Adve242a8082002-05-19 15:25:51 +00001667 mvec, destMCFI);
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001668 }
1669 else
1670 srcForCast = leftVal;
1671
Vikram S. Adve74825322002-03-18 03:15:35 +00001672 M = new MachineInstr(opCode);
1673 M->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister,
1674 srcForCast);
1675 M->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister,
1676 dest);
1677 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001678 }
1679 }
1680 break;
1681
1682 case 19: // reg: ToArrayTy(reg):
1683 case 20: // reg: ToPointerTy(reg):
Vikram S. Adve74825322002-03-18 03:15:35 +00001684 forwardOperandNum = 0; // forward first operand to user
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001685 break;
1686
1687 case 233: // reg: Add(reg, Constant)
Vikram S. Adve74825322002-03-18 03:15:35 +00001688 M = CreateAddConstInstruction(subtreeRoot);
1689 if (M != NULL)
1690 {
1691 mvec.push_back(M);
1692 break;
1693 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001694 // ELSE FALL THROUGH
Vikram S. Adve74825322002-03-18 03:15:35 +00001695
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001696 case 33: // reg: Add(reg, reg)
Vikram S. Adve74825322002-03-18 03:15:35 +00001697 mvec.push_back(new MachineInstr(ChooseAddInstruction(subtreeRoot)));
1698 Set3OperandsFromInstr(mvec.back(), subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001699 break;
1700
1701 case 234: // reg: Sub(reg, Constant)
Vikram S. Adve74825322002-03-18 03:15:35 +00001702 M = CreateSubConstInstruction(subtreeRoot);
1703 if (M != NULL)
1704 {
1705 mvec.push_back(M);
1706 break;
1707 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001708 // ELSE FALL THROUGH
Vikram S. Adve74825322002-03-18 03:15:35 +00001709
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001710 case 34: // reg: Sub(reg, reg)
Vikram S. Adve74825322002-03-18 03:15:35 +00001711 mvec.push_back(new MachineInstr(ChooseSubInstructionByType(
1712 subtreeRoot->getInstruction()->getType())));
1713 Set3OperandsFromInstr(mvec.back(), subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001714 break;
1715
1716 case 135: // reg: Mul(todouble, todouble)
1717 checkCast = true;
1718 // FALL THROUGH
1719
1720 case 35: // reg: Mul(reg, reg)
Vikram S. Adve74825322002-03-18 03:15:35 +00001721 {
1722 MachineOpCode forceOp = ((checkCast && BothFloatToDouble(subtreeRoot))
1723 ? FSMULD
1724 : INVALID_MACHINE_OPCODE);
Vikram S. Adve242a8082002-05-19 15:25:51 +00001725 Instruction* mulInstr = subtreeRoot->getInstruction();
1726 CreateMulInstruction(target, mulInstr->getParent()->getParent(),
Vikram S. Adve74825322002-03-18 03:15:35 +00001727 subtreeRoot->leftChild()->getValue(),
1728 subtreeRoot->rightChild()->getValue(),
Vikram S. Adve242a8082002-05-19 15:25:51 +00001729 mulInstr, mvec,
1730 MachineCodeForInstruction::get(mulInstr),forceOp);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001731 break;
Vikram S. Adve74825322002-03-18 03:15:35 +00001732 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001733 case 335: // reg: Mul(todouble, todoubleConst)
1734 checkCast = true;
1735 // FALL THROUGH
1736
1737 case 235: // reg: Mul(reg, Constant)
Vikram S. Adve74825322002-03-18 03:15:35 +00001738 {
1739 MachineOpCode forceOp = ((checkCast && BothFloatToDouble(subtreeRoot))
1740 ? FSMULD
1741 : INVALID_MACHINE_OPCODE);
Vikram S. Adve242a8082002-05-19 15:25:51 +00001742 Instruction* mulInstr = subtreeRoot->getInstruction();
1743 CreateMulInstruction(target, mulInstr->getParent()->getParent(),
Vikram S. Adve74825322002-03-18 03:15:35 +00001744 subtreeRoot->leftChild()->getValue(),
1745 subtreeRoot->rightChild()->getValue(),
Vikram S. Adve242a8082002-05-19 15:25:51 +00001746 mulInstr, mvec,
1747 MachineCodeForInstruction::get(mulInstr),
1748 forceOp);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001749 break;
Vikram S. Adve74825322002-03-18 03:15:35 +00001750 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001751 case 236: // reg: Div(reg, Constant)
Vikram S. Adve74825322002-03-18 03:15:35 +00001752 L = mvec.size();
1753 CreateDivConstInstruction(target, subtreeRoot, mvec);
1754 if (mvec.size() > L)
1755 break;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001756 // ELSE FALL THROUGH
Vikram S. Adve74825322002-03-18 03:15:35 +00001757
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001758 case 36: // reg: Div(reg, reg)
Vikram S. Adve74825322002-03-18 03:15:35 +00001759 mvec.push_back(new MachineInstr(ChooseDivInstruction(target, subtreeRoot)));
1760 Set3OperandsFromInstr(mvec.back(), subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001761 break;
1762
1763 case 37: // reg: Rem(reg, reg)
1764 case 237: // reg: Rem(reg, Constant)
Vikram S. Adve510eec72001-11-04 21:59:14 +00001765 {
1766 Instruction* remInstr = subtreeRoot->getInstruction();
1767
Chris Lattner9c461082002-02-03 07:50:56 +00001768 TmpInstruction* quot = new TmpInstruction(
Vikram S. Adve510eec72001-11-04 21:59:14 +00001769 subtreeRoot->leftChild()->getValue(),
1770 subtreeRoot->rightChild()->getValue());
Chris Lattner9c461082002-02-03 07:50:56 +00001771 TmpInstruction* prod = new TmpInstruction(
Vikram S. Adve510eec72001-11-04 21:59:14 +00001772 quot,
1773 subtreeRoot->rightChild()->getValue());
Chris Lattner9c461082002-02-03 07:50:56 +00001774 MachineCodeForInstruction::get(remInstr).addTemp(quot).addTemp(prod);
Vikram S. Adve510eec72001-11-04 21:59:14 +00001775
Vikram S. Adve74825322002-03-18 03:15:35 +00001776 M = new MachineInstr(ChooseDivInstruction(target, subtreeRoot));
1777 Set3OperandsFromInstr(M, subtreeRoot, target);
1778 M->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister,quot);
1779 mvec.push_back(M);
Vikram S. Adve510eec72001-11-04 21:59:14 +00001780
Vikram S. Adve74825322002-03-18 03:15:35 +00001781 M = new MachineInstr(ChooseMulInstructionByType(
Vikram S. Adve510eec72001-11-04 21:59:14 +00001782 subtreeRoot->getInstruction()->getType()));
Vikram S. Adve74825322002-03-18 03:15:35 +00001783 M->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister,quot);
1784 M->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister,
Vikram S. Adve510eec72001-11-04 21:59:14 +00001785 subtreeRoot->rightChild()->getValue());
Vikram S. Adve74825322002-03-18 03:15:35 +00001786 M->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister,prod);
1787 mvec.push_back(M);
Vikram S. Adve510eec72001-11-04 21:59:14 +00001788
Vikram S. Adve74825322002-03-18 03:15:35 +00001789 M = new MachineInstr(ChooseSubInstructionByType(
Vikram S. Adve510eec72001-11-04 21:59:14 +00001790 subtreeRoot->getInstruction()->getType()));
Vikram S. Adve74825322002-03-18 03:15:35 +00001791 Set3OperandsFromInstr(M, subtreeRoot, target);
1792 M->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister,prod);
1793 mvec.push_back(M);
Vikram S. Adve510eec72001-11-04 21:59:14 +00001794
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001795 break;
Vikram S. Adve510eec72001-11-04 21:59:14 +00001796 }
1797
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001798 case 38: // bool: And(bool, bool)
1799 case 238: // bool: And(bool, boolconst)
1800 case 338: // reg : BAnd(reg, reg)
1801 case 538: // reg : BAnd(reg, Constant)
Vikram S. Adve74825322002-03-18 03:15:35 +00001802 mvec.push_back(new MachineInstr(AND));
1803 Set3OperandsFromInstr(mvec.back(), subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001804 break;
1805
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001806 case 138: // bool: And(bool, not)
1807 case 438: // bool: BAnd(bool, not)
Vikram S. Adve74825322002-03-18 03:15:35 +00001808 mvec.push_back(new MachineInstr(ANDN));
1809 Set3OperandsFromInstr(mvec.back(), subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001810 break;
1811
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001812 case 39: // bool: Or(bool, bool)
1813 case 239: // bool: Or(bool, boolconst)
1814 case 339: // reg : BOr(reg, reg)
1815 case 539: // reg : BOr(reg, Constant)
Vikram S. Adve242a8082002-05-19 15:25:51 +00001816 mvec.push_back(new MachineInstr(OR));
Vikram S. Adve74825322002-03-18 03:15:35 +00001817 Set3OperandsFromInstr(mvec.back(), subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001818 break;
1819
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001820 case 139: // bool: Or(bool, not)
1821 case 439: // bool: BOr(bool, not)
Vikram S. Adve74825322002-03-18 03:15:35 +00001822 mvec.push_back(new MachineInstr(ORN));
1823 Set3OperandsFromInstr(mvec.back(), subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001824 break;
1825
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001826 case 40: // bool: Xor(bool, bool)
1827 case 240: // bool: Xor(bool, boolconst)
1828 case 340: // reg : BXor(reg, reg)
1829 case 540: // reg : BXor(reg, Constant)
Vikram S. Adve74825322002-03-18 03:15:35 +00001830 mvec.push_back(new MachineInstr(XOR));
1831 Set3OperandsFromInstr(mvec.back(), subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001832 break;
1833
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001834 case 140: // bool: Xor(bool, not)
1835 case 440: // bool: BXor(bool, not)
Vikram S. Adve74825322002-03-18 03:15:35 +00001836 mvec.push_back(new MachineInstr(XNOR));
1837 Set3OperandsFromInstr(mvec.back(), subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001838 break;
1839
1840 case 41: // boolconst: SetCC(reg, Constant)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001841 //
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001842 // If the SetCC was folded into the user (parent), it will be
1843 // caught above. All other cases are the same as case 42,
1844 // so just fall through.
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001845 //
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001846 case 42: // bool: SetCC(reg, reg):
1847 {
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001848 // This generates a SUBCC instruction, putting the difference in
1849 // a result register, and setting a condition code.
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001850 //
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001851 // If the boolean result of the SetCC is used by anything other
Vikram S. Adve6418eac2002-07-08 23:30:14 +00001852 // than a branch instruction, or if it is used outside the current
1853 // basic block, the boolean must be
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001854 // computed and stored in the result register. Otherwise, discard
1855 // the difference (by using %g0) and keep only the condition code.
1856 //
1857 // To compute the boolean result in a register we use a conditional
1858 // move, unless the result of the SUBCC instruction can be used as
1859 // the bool! This assumes that zero is FALSE and any non-zero
1860 // integer is TRUE.
1861 //
1862 InstructionNode* parentNode = (InstructionNode*) subtreeRoot->parent();
1863 Instruction* setCCInstr = subtreeRoot->getInstruction();
Vikram S. Adve242a8082002-05-19 15:25:51 +00001864
Vikram S. Adve6418eac2002-07-08 23:30:14 +00001865 bool keepBoolVal = parentNode == NULL ||
1866 ! AllUsesAreBranches(setCCInstr);
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001867 bool subValIsBoolVal = setCCInstr->getOpcode() == Instruction::SetNE;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001868 bool keepSubVal = keepBoolVal && subValIsBoolVal;
1869 bool computeBoolVal = keepBoolVal && ! subValIsBoolVal;
1870
1871 bool mustClearReg;
1872 int valueToMove;
Chris Lattner8e5c0b42001-11-07 14:01:59 +00001873 MachineOpCode movOpCode = 0;
Vikram S. Adve6418eac2002-07-08 23:30:14 +00001874
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001875 // Mark the 4th operand as being a CC register, and as a def
1876 // A TmpInstruction is created to represent the CC "result".
1877 // Unlike other instances of TmpInstruction, this one is used
1878 // by machine code of multiple LLVM instructions, viz.,
1879 // the SetCC and the branch. Make sure to get the same one!
1880 // Note that we do this even for FP CC registers even though they
1881 // are explicit operands, because the type of the operand
1882 // needs to be a floating point condition code, not an integer
1883 // condition code. Think of this as casting the bool result to
1884 // a FP condition code register.
1885 //
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001886 Value* leftVal = subtreeRoot->leftChild()->getValue();
Chris Lattner9b625032002-05-06 16:15:30 +00001887 bool isFPCompare = leftVal->getType()->isFloatingPoint();
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001888
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001889 TmpInstruction* tmpForCC = GetTmpForCC(setCCInstr,
1890 setCCInstr->getParent()->getParent(),
Chris Lattner9b625032002-05-06 16:15:30 +00001891 isFPCompare ? Type::FloatTy : Type::IntTy);
Chris Lattner9c461082002-02-03 07:50:56 +00001892 MachineCodeForInstruction::get(setCCInstr).addTemp(tmpForCC);
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001893
1894 if (! isFPCompare)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001895 {
1896 // Integer condition: dest. should be %g0 or an integer register.
1897 // If result must be saved but condition is not SetEQ then we need
1898 // a separate instruction to compute the bool result, so discard
1899 // result of SUBcc instruction anyway.
1900 //
Vikram S. Adve74825322002-03-18 03:15:35 +00001901 M = new MachineInstr(SUBcc);
1902 Set3OperandsFromInstr(M, subtreeRoot, target, ! keepSubVal);
1903 M->SetMachineOperandVal(3, MachineOperand::MO_CCRegister,
1904 tmpForCC, /*def*/true);
1905 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001906
1907 if (computeBoolVal)
1908 { // recompute bool using the integer condition codes
1909 movOpCode =
1910 ChooseMovpccAfterSub(subtreeRoot,mustClearReg,valueToMove);
1911 }
1912 }
1913 else
1914 {
1915 // FP condition: dest of FCMP should be some FCCn register
Vikram S. Adve74825322002-03-18 03:15:35 +00001916 M = new MachineInstr(ChooseFcmpInstruction(subtreeRoot));
1917 M->SetMachineOperandVal(0, MachineOperand::MO_CCRegister,
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001918 tmpForCC);
Vikram S. Adve74825322002-03-18 03:15:35 +00001919 M->SetMachineOperandVal(1,MachineOperand::MO_VirtualRegister,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001920 subtreeRoot->leftChild()->getValue());
Vikram S. Adve74825322002-03-18 03:15:35 +00001921 M->SetMachineOperandVal(2,MachineOperand::MO_VirtualRegister,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001922 subtreeRoot->rightChild()->getValue());
Vikram S. Adve74825322002-03-18 03:15:35 +00001923 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001924
1925 if (computeBoolVal)
1926 {// recompute bool using the FP condition codes
1927 mustClearReg = true;
1928 valueToMove = 1;
1929 movOpCode = ChooseMovFpccInstruction(subtreeRoot);
1930 }
1931 }
1932
1933 if (computeBoolVal)
1934 {
1935 if (mustClearReg)
1936 {// Unconditionally set register to 0
Vikram S. Adve74825322002-03-18 03:15:35 +00001937 M = new MachineInstr(SETHI);
1938 M->SetMachineOperandConst(0,MachineOperand::MO_UnextendedImmed,
1939 (int64_t)0);
1940 M->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister,
1941 setCCInstr);
1942 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001943 }
1944
1945 // Now conditionally move `valueToMove' (0 or 1) into the register
Vikram S. Adve6418eac2002-07-08 23:30:14 +00001946 // Mark the register as a use (as well as a def) because the old
1947 // value should be retained if the condition is false.
Vikram S. Adve74825322002-03-18 03:15:35 +00001948 M = new MachineInstr(movOpCode);
1949 M->SetMachineOperandVal(0, MachineOperand::MO_CCRegister,
1950 tmpForCC);
1951 M->SetMachineOperandConst(1, MachineOperand::MO_UnextendedImmed,
1952 valueToMove);
1953 M->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister,
Vikram S. Adve6418eac2002-07-08 23:30:14 +00001954 setCCInstr, /*isDef*/ true,
1955 /*isDefAndUse*/ true);
Vikram S. Adve74825322002-03-18 03:15:35 +00001956 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001957 }
1958 break;
1959 }
1960
1961 case 43: // boolreg: VReg
1962 case 44: // boolreg: Constant
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001963 break;
1964
1965 case 51: // reg: Load(reg)
1966 case 52: // reg: Load(ptrreg)
1967 case 53: // reg: LoadIdx(reg,reg)
1968 case 54: // reg: LoadIdx(ptrreg,reg)
Vikram S. Adve74825322002-03-18 03:15:35 +00001969 mvec.push_back(new MachineInstr(ChooseLoadInstruction(
1970 subtreeRoot->getValue()->getType())));
1971 SetOperandsForMemInstr(mvec, mvec.end()-1, subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001972 break;
1973
1974 case 55: // reg: GetElemPtr(reg)
1975 case 56: // reg: GetElemPtrIdx(reg,reg)
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001976 // If the GetElemPtr was folded into the user (parent), it will be
1977 // caught above. For other cases, we have to compute the address.
Vikram S. Adve74825322002-03-18 03:15:35 +00001978 mvec.push_back(new MachineInstr(ADD));
1979 SetOperandsForMemInstr(mvec, mvec.end()-1, subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001980 break;
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001981
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001982 case 57: // reg: Alloca: Implement as 1 instruction:
1983 { // add %fp, offsetFromFP -> result
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001984 AllocationInst* instr =
1985 cast<AllocationInst>(subtreeRoot->getInstruction());
1986 unsigned int tsize =
1987 target.findOptimalStorageSize(instr->getAllocatedType());
Vikram S. Adve74825322002-03-18 03:15:35 +00001988 assert(tsize != 0);
1989 CreateCodeForFixedSizeAlloca(target, instr, tsize, 1, mvec);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001990 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001991 }
Vikram S. Adve74825322002-03-18 03:15:35 +00001992
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001993 case 58: // reg: Alloca(reg): Implement as 3 instructions:
1994 // mul num, typeSz -> tmp
1995 // sub %sp, tmp -> %sp
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001996 { // add %sp, frameSizeBelowDynamicArea -> result
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001997 AllocationInst* instr =
1998 cast<AllocationInst>(subtreeRoot->getInstruction());
Vikram S. Adve74825322002-03-18 03:15:35 +00001999 const Type* eltType = instr->getAllocatedType();
2000
Vikram S. Advefd3900a2002-03-24 03:33:02 +00002001 // If #elements is constant, use simpler code for fixed-size allocas
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002002 int tsize = (int) target.findOptimalStorageSize(eltType);
Vikram S. Advefd3900a2002-03-24 03:33:02 +00002003 Value* numElementsVal = NULL;
2004 bool isArray = instr->isArrayAllocation();
2005
2006 if (!isArray ||
2007 isa<Constant>(numElementsVal = instr->getArraySize()))
2008 { // total size is constant: generate code for fixed-size alloca
2009 unsigned int numElements = isArray?
2010 cast<ConstantUInt>(numElementsVal)->getValue() : 1;
2011 CreateCodeForFixedSizeAlloca(target, instr, tsize,
2012 numElements, mvec);
2013 }
Vikram S. Adve74825322002-03-18 03:15:35 +00002014 else // total size is not constant.
2015 CreateCodeForVariableSizeAlloca(target, instr, tsize,
Vikram S. Advefd3900a2002-03-24 03:33:02 +00002016 numElementsVal, mvec);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002017 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00002018 }
Vikram S. Adve74825322002-03-18 03:15:35 +00002019
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002020 case 61: // reg: Call
Vikram S. Advedbc4fad2002-04-25 04:37:51 +00002021 { // Generate a direct (CALL) or indirect (JMPL). depending
2022 // Mark the return-address register and the indirection
2023 // register (if any) as hidden virtual registers.
Vikram S. Advea995e602001-10-11 04:23:19 +00002024 // Also, mark the operands of the Call and return value (if
2025 // any) as implicit operands of the CALL machine instruction.
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00002026 //
Vikram S. Advedbc4fad2002-04-25 04:37:51 +00002027 // If this is a varargs function, floating point arguments
2028 // have to passed in integer registers so insert
2029 // copy-float-to-int instructions for each float operand.
2030 //
Chris Lattnerb00c5822001-10-02 03:41:24 +00002031 CallInst *callInstr = cast<CallInst>(subtreeRoot->getInstruction());
Chris Lattner749655f2001-10-13 06:54:30 +00002032 Value *callee = callInstr->getCalledValue();
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002033
Vikram S. Advea10d1a72002-03-31 19:07:35 +00002034 // Create hidden virtual register for return address, with type void*.
Vikram S. Adve242a8082002-05-19 15:25:51 +00002035 TmpInstruction* retAddrReg =
Vikram S. Advea10d1a72002-03-31 19:07:35 +00002036 new TmpInstruction(PointerType::get(Type::VoidTy), callInstr);
Chris Lattner9c461082002-02-03 07:50:56 +00002037 MachineCodeForInstruction::get(callInstr).addTemp(retAddrReg);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002038
Vikram S. Adveea21a6c2001-10-20 20:57:06 +00002039 // Generate the machine instruction and its operands.
2040 // Use CALL for direct function calls; this optimistically assumes
2041 // the PC-relative address fits in the CALL address field (22 bits).
2042 // Use JMPL for indirect calls.
2043 //
Chris Lattnerb0d04722002-03-26 17:58:12 +00002044 if (isa<Function>(callee))
Vikram S. Adveea21a6c2001-10-20 20:57:06 +00002045 { // direct function call
Vikram S. Adve74825322002-03-18 03:15:35 +00002046 M = new MachineInstr(CALL);
2047 M->SetMachineOperandVal(0, MachineOperand::MO_PCRelativeDisp,
2048 callee);
Vikram S. Adveea21a6c2001-10-20 20:57:06 +00002049 }
2050 else
2051 { // indirect function call
Vikram S. Adve74825322002-03-18 03:15:35 +00002052 M = new MachineInstr(JMPLCALL);
2053 M->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister,
2054 callee);
2055 M->SetMachineOperandConst(1, MachineOperand::MO_SignExtendedImmed,
2056 (int64_t) 0);
2057 M->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister,
2058 retAddrReg);
Vikram S. Adveea21a6c2001-10-20 20:57:06 +00002059 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002060
Vikram S. Adve74825322002-03-18 03:15:35 +00002061 mvec.push_back(M);
Vikram S. Advea10d1a72002-03-31 19:07:35 +00002062
Vikram S. Adve242a8082002-05-19 15:25:51 +00002063 const FunctionType* funcType =
2064 cast<FunctionType>(cast<PointerType>(callee->getType())
2065 ->getElementType());
2066 bool isVarArgs = funcType->isVarArg();
2067 bool noPrototype = isVarArgs && funcType->getNumParams() == 0;
Vikram S. Advedbc4fad2002-04-25 04:37:51 +00002068
Vikram S. Adve242a8082002-05-19 15:25:51 +00002069 // Use an annotation to pass information about call arguments
2070 // to the register allocator.
2071 CallArgsDescriptor* argDesc = new CallArgsDescriptor(callInstr,
2072 retAddrReg, isVarArgs, noPrototype);
2073 M->addAnnotation(argDesc);
Vikram S. Advea995e602001-10-11 04:23:19 +00002074
Vikram S. Adve242a8082002-05-19 15:25:51 +00002075 assert(callInstr->getOperand(0) == callee
2076 && "This is assumed in the loop below!");
2077
2078 for (unsigned i=1, N=callInstr->getNumOperands(); i < N; ++i)
2079 {
2080 Value* argVal = callInstr->getOperand(i);
2081 Instruction* intArgReg = NULL;
2082
2083 // Check for FP arguments to varargs functions.
2084 // Any such argument in the first $K$ args must be passed in an
2085 // integer register, where K = #integer argument registers.
2086 if (isVarArgs && argVal->getType()->isFloatingPoint())
2087 {
2088 // If it is a function with no prototype, pass value
2089 // as an FP value as well as a varargs value
2090 if (noPrototype)
2091 argDesc->getArgInfo(i-1).setUseFPArgReg();
2092
2093 // If this arg. is in the first $K$ regs, add a copy
2094 // float-to-int instruction to pass the value as an integer.
2095 if (i < target.getRegInfo().GetNumOfIntArgRegs())
2096 {
2097 MachineCodeForInstruction &destMCFI =
2098 MachineCodeForInstruction::get(callInstr);
2099 intArgReg = new TmpInstruction(Type::IntTy, argVal);
2100 destMCFI.addTemp(intArgReg);
2101
2102 vector<MachineInstr*> copyMvec;
2103 target.getInstrInfo().CreateCodeToCopyFloatToInt(target,
2104 callInstr->getParent()->getParent(),
2105 argVal, (TmpInstruction*) intArgReg,
2106 copyMvec, destMCFI);
2107 mvec.insert(mvec.begin(),copyMvec.begin(),copyMvec.end());
2108
2109 argDesc->getArgInfo(i-1).setUseIntArgReg();
2110 argDesc->getArgInfo(i-1).setArgCopy(intArgReg);
2111 }
2112 else
2113 // Cannot fit in first $K$ regs so pass the arg on the stack
2114 argDesc->getArgInfo(i-1).setUseStackSlot();
2115 }
2116
2117 if (intArgReg)
2118 mvec.back()->addImplicitRef(intArgReg);
2119
2120 mvec.back()->addImplicitRef(argVal);
2121 }
2122
2123 // Add the return value as an implicit ref. The call operands
2124 // were added above.
Vikram S. Adveea21a6c2001-10-20 20:57:06 +00002125 if (callInstr->getType() != Type::VoidTy)
Vikram S. Adve74825322002-03-18 03:15:35 +00002126 mvec.back()->addImplicitRef(callInstr, /*isDef*/ true);
Vikram S. Advea995e602001-10-11 04:23:19 +00002127
Vikram S. Adveea21a6c2001-10-20 20:57:06 +00002128 // For the CALL instruction, the ret. addr. reg. is also implicit
Chris Lattnerb0d04722002-03-26 17:58:12 +00002129 if (isa<Function>(callee))
Vikram S. Adve74825322002-03-18 03:15:35 +00002130 mvec.back()->addImplicitRef(retAddrReg, /*isDef*/ true);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002131
Vikram S. Adve74825322002-03-18 03:15:35 +00002132 // delay slot
2133 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002134 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00002135 }
Vikram S. Adve242a8082002-05-19 15:25:51 +00002136
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002137 case 62: // reg: Shl(reg, reg)
Vikram S. Adve242a8082002-05-19 15:25:51 +00002138 {
2139 Value* argVal1 = subtreeRoot->leftChild()->getValue();
2140 Value* argVal2 = subtreeRoot->rightChild()->getValue();
2141 Instruction* shlInstr = subtreeRoot->getInstruction();
2142
2143 const Type* opType = argVal1->getType();
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002144 assert(opType->isIntegral()
Vikram S. Adve242a8082002-05-19 15:25:51 +00002145 || opType == Type::BoolTy
2146 || isa<PointerType>(opType)&&"Shl unsupported for other types");
2147
2148 CreateShiftInstructions(target, shlInstr->getParent()->getParent(),
2149 (opType == Type::LongTy)? SLLX : SLL,
2150 argVal1, argVal2, 0, shlInstr, mvec,
2151 MachineCodeForInstruction::get(shlInstr));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002152 break;
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00002153 }
2154
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002155 case 63: // reg: Shr(reg, reg)
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00002156 { const Type* opType = subtreeRoot->leftChild()->getValue()->getType();
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002157 assert(opType->isIntegral()
Vikram S. Adve242a8082002-05-19 15:25:51 +00002158 || isa<PointerType>(opType)&&"Shr unsupported for other types");
Vikram S. Adve74825322002-03-18 03:15:35 +00002159 mvec.push_back(new MachineInstr((opType->isSigned()
2160 ? ((opType == Type::LongTy)? SRAX : SRA)
2161 : ((opType == Type::LongTy)? SRLX : SRL))));
2162 Set3OperandsFromInstr(mvec.back(), subtreeRoot, target);
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 64: // reg: Phi(reg,reg)
Vikram S. Adve74825322002-03-18 03:15:35 +00002167 break; // don't forward the value
2168
Vikram S. Adve3438b212001-11-12 18:54:11 +00002169#undef NEED_PHI_MACHINE_INSTRS
2170#ifdef NEED_PHI_MACHINE_INSTRS
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002171 { // This instruction has variable #operands, so resultPos is 0.
2172 Instruction* phi = subtreeRoot->getInstruction();
Vikram S. Adve74825322002-03-18 03:15:35 +00002173 M = new MachineInstr(PHI, 1 + phi->getNumOperands());
2174 M->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002175 subtreeRoot->getValue());
2176 for (unsigned i=0, N=phi->getNumOperands(); i < N; i++)
Vikram S. Adve74825322002-03-18 03:15:35 +00002177 M->SetMachineOperandVal(i+1, MachineOperand::MO_VirtualRegister,
2178 phi->getOperand(i));
2179 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002180 break;
2181 }
Chris Lattner697954c2002-01-20 22:54:45 +00002182#endif // NEED_PHI_MACHINE_INSTRS
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00002183
Vikram S. Adve74825322002-03-18 03:15:35 +00002184
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002185 case 71: // reg: VReg
2186 case 72: // reg: Constant
Vikram S. Adve74825322002-03-18 03:15:35 +00002187 break; // don't forward the value
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002188
2189 default:
2190 assert(0 && "Unrecognized BURG rule");
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002191 break;
2192 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00002193 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00002194
2195 if (forwardOperandNum >= 0)
2196 { // We did not generate a machine instruction but need to use operand.
2197 // If user is in the same tree, replace Value in its machine operand.
2198 // If not, insert a copy instruction which should get coalesced away
2199 // by register allocation.
2200 if (subtreeRoot->parent() != NULL)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002201 ForwardOperand(subtreeRoot, subtreeRoot->parent(), forwardOperandNum);
Chris Lattner20b1ea02001-09-14 03:47:57 +00002202 else
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002203 {
Vikram S. Adve7fe27872001-10-18 00:26:20 +00002204 vector<MachineInstr*> minstrVec;
Vikram S. Adve242a8082002-05-19 15:25:51 +00002205 Instruction* instr = subtreeRoot->getInstruction();
2206 target.getInstrInfo().
2207 CreateCopyInstructionsByType(target,
2208 instr->getParent()->getParent(),
2209 instr->getOperand(forwardOperandNum),
2210 instr, minstrVec,
2211 MachineCodeForInstruction::get(instr));
Vikram S. Adve7fe27872001-10-18 00:26:20 +00002212 assert(minstrVec.size() > 0);
Vikram S. Adve74825322002-03-18 03:15:35 +00002213 mvec.insert(mvec.end(), minstrVec.begin(), minstrVec.end());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002214 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00002215 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00002216}