blob: 5afdea505cce786b57d03a586d166ac51cbfa00e [file] [log] [blame]
Chris Lattner035dfbe2002-08-09 20:08:06 +00001//===-- SparcInstrSelection.cpp -------------------------------------------===//
2//
3// BURS instruction selection for SPARC V9 architecture.
4//
5//===----------------------------------------------------------------------===//
Chris Lattner20b1ea02001-09-14 03:47:57 +00006
7#include "SparcInternals.h"
Vikram S. Adve7fe27872001-10-18 00:26:20 +00008#include "SparcInstrSelectionSupport.h"
Vikram S. Adve74825322002-03-18 03:15:35 +00009#include "SparcRegClassInfo.h"
Vikram S. Adve8557b222001-10-10 20:56:33 +000010#include "llvm/CodeGen/InstrSelectionSupport.h"
Chris Lattner20b1ea02001-09-14 03:47:57 +000011#include "llvm/CodeGen/MachineInstr.h"
Vikram S. Adve242a8082002-05-19 15:25:51 +000012#include "llvm/CodeGen/MachineInstrAnnot.h"
Chris Lattner20b1ea02001-09-14 03:47:57 +000013#include "llvm/CodeGen/InstrForest.h"
14#include "llvm/CodeGen/InstrSelection.h"
Misha Brukmanfce11432002-10-28 00:28:31 +000015#include "llvm/CodeGen/MachineFunction.h"
Chris Lattner9c461082002-02-03 07:50:56 +000016#include "llvm/CodeGen/MachineCodeForInstruction.h"
Chris Lattner20b1ea02001-09-14 03:47:57 +000017#include "llvm/DerivedTypes.h"
18#include "llvm/iTerminators.h"
19#include "llvm/iMemory.h"
20#include "llvm/iOther.h"
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000021#include "llvm/Function.h"
Chris Lattner31bcdb82002-04-28 19:55:58 +000022#include "llvm/Constants.h"
Vikram S. Adved3e26482002-10-13 00:18:57 +000023#include "llvm/ConstantHandling.h"
Chris Lattnercee8f9a2001-11-27 00:03:19 +000024#include "Support/MathExtras.h"
Chris Lattner749655f2001-10-13 06:54:30 +000025#include <math.h>
Chris Lattner697954c2002-01-20 22:54:45 +000026using std::vector;
Chris Lattner20b1ea02001-09-14 03:47:57 +000027
Chris Lattner20b1ea02001-09-14 03:47:57 +000028//************************ Internal Functions ******************************/
29
Chris Lattner20b1ea02001-09-14 03:47:57 +000030
Chris Lattner20b1ea02001-09-14 03:47:57 +000031static inline MachineOpCode
32ChooseBprInstruction(const InstructionNode* instrNode)
33{
34 MachineOpCode opCode;
35
36 Instruction* setCCInstr =
37 ((InstructionNode*) instrNode->leftChild())->getInstruction();
38
39 switch(setCCInstr->getOpcode())
40 {
41 case Instruction::SetEQ: opCode = BRZ; break;
42 case Instruction::SetNE: opCode = BRNZ; break;
43 case Instruction::SetLE: opCode = BRLEZ; break;
44 case Instruction::SetGE: opCode = BRGEZ; break;
45 case Instruction::SetLT: opCode = BRLZ; break;
46 case Instruction::SetGT: opCode = BRGZ; break;
47 default:
48 assert(0 && "Unrecognized VM instruction!");
49 opCode = INVALID_OPCODE;
50 break;
51 }
52
53 return opCode;
54}
55
56
57static inline MachineOpCode
Chris Lattner20b1ea02001-09-14 03:47:57 +000058ChooseBpccInstruction(const InstructionNode* instrNode,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +000059 const BinaryOperator* setCCInstr)
Chris Lattner20b1ea02001-09-14 03:47:57 +000060{
61 MachineOpCode opCode = INVALID_OPCODE;
62
63 bool isSigned = setCCInstr->getOperand(0)->getType()->isSigned();
64
65 if (isSigned)
66 {
67 switch(setCCInstr->getOpcode())
Vikram S. Adve4cecdd22001-10-01 00:12:53 +000068 {
69 case Instruction::SetEQ: opCode = BE; break;
70 case Instruction::SetNE: opCode = BNE; break;
71 case Instruction::SetLE: opCode = BLE; break;
72 case Instruction::SetGE: opCode = BGE; break;
73 case Instruction::SetLT: opCode = BL; break;
74 case Instruction::SetGT: opCode = BG; break;
75 default:
76 assert(0 && "Unrecognized VM instruction!");
77 break;
78 }
Chris Lattner20b1ea02001-09-14 03:47:57 +000079 }
80 else
81 {
82 switch(setCCInstr->getOpcode())
Vikram S. Adve4cecdd22001-10-01 00:12:53 +000083 {
84 case Instruction::SetEQ: opCode = BE; break;
85 case Instruction::SetNE: opCode = BNE; break;
86 case Instruction::SetLE: opCode = BLEU; break;
87 case Instruction::SetGE: opCode = BCC; break;
88 case Instruction::SetLT: opCode = BCS; break;
89 case Instruction::SetGT: opCode = BGU; break;
90 default:
91 assert(0 && "Unrecognized VM instruction!");
92 break;
93 }
Chris Lattner20b1ea02001-09-14 03:47:57 +000094 }
95
96 return opCode;
97}
98
99static inline MachineOpCode
100ChooseBFpccInstruction(const InstructionNode* instrNode,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000101 const BinaryOperator* setCCInstr)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000102{
103 MachineOpCode opCode = INVALID_OPCODE;
104
105 switch(setCCInstr->getOpcode())
106 {
107 case Instruction::SetEQ: opCode = FBE; break;
108 case Instruction::SetNE: opCode = FBNE; break;
109 case Instruction::SetLE: opCode = FBLE; break;
110 case Instruction::SetGE: opCode = FBGE; break;
111 case Instruction::SetLT: opCode = FBL; break;
112 case Instruction::SetGT: opCode = FBG; break;
113 default:
114 assert(0 && "Unrecognized VM instruction!");
115 break;
116 }
117
118 return opCode;
119}
120
121
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000122// Create a unique TmpInstruction for a boolean value,
123// representing the CC register used by a branch on that value.
124// For now, hack this using a little static cache of TmpInstructions.
125// Eventually the entire BURG instruction selection should be put
126// into a separate class that can hold such information.
Vikram S. Adveff5a09e2001-11-08 05:04:09 +0000127// The static cache is not too bad because the memory for these
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000128// TmpInstructions will be freed along with the rest of the Function anyway.
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000129//
130static TmpInstruction*
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000131GetTmpForCC(Value* boolVal, const Function *F, const Type* ccType)
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000132{
Chris Lattner09ff1122002-07-24 21:21:32 +0000133 typedef hash_map<const Value*, TmpInstruction*> BoolTmpCache;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000134 static BoolTmpCache boolToTmpCache; // Map boolVal -> TmpInstruction*
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000135 static const Function *lastFunction = 0;// Use to flush cache between funcs
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000136
137 assert(boolVal->getType() == Type::BoolTy && "Weird but ok! Delete assert");
138
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000139 if (lastFunction != F)
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000140 {
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000141 lastFunction = F;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000142 boolToTmpCache.clear();
143 }
144
Vikram S. Adveff5a09e2001-11-08 05:04:09 +0000145 // Look for tmpI and create a new one otherwise. The new value is
146 // directly written to map using the ref returned by operator[].
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000147 TmpInstruction*& tmpI = boolToTmpCache[boolVal];
148 if (tmpI == NULL)
Chris Lattner9c461082002-02-03 07:50:56 +0000149 tmpI = new TmpInstruction(ccType, boolVal);
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000150
151 return tmpI;
152}
153
154
Chris Lattner20b1ea02001-09-14 03:47:57 +0000155static inline MachineOpCode
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000156ChooseBccInstruction(const InstructionNode* instrNode,
157 bool& isFPBranch)
158{
159 InstructionNode* setCCNode = (InstructionNode*) instrNode->leftChild();
Vikram S. Adve30a6f492002-08-22 02:56:10 +0000160 assert(setCCNode->getOpLabel() == SetCCOp);
161 BinaryOperator* setCCInstr =cast<BinaryOperator>(setCCNode->getInstruction());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000162 const Type* setCCType = setCCInstr->getOperand(0)->getType();
163
Vikram S. Adve242a8082002-05-19 15:25:51 +0000164 isFPBranch = setCCType->isFloatingPoint(); // Return value: don't delete!
165
166 if (isFPBranch)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000167 return ChooseBFpccInstruction(instrNode, setCCInstr);
168 else
169 return ChooseBpccInstruction(instrNode, setCCInstr);
170}
171
172
173static inline MachineOpCode
Chris Lattner20b1ea02001-09-14 03:47:57 +0000174ChooseMovFpccInstruction(const InstructionNode* instrNode)
175{
176 MachineOpCode opCode = INVALID_OPCODE;
177
178 switch(instrNode->getInstruction()->getOpcode())
179 {
180 case Instruction::SetEQ: opCode = MOVFE; break;
181 case Instruction::SetNE: opCode = MOVFNE; break;
182 case Instruction::SetLE: opCode = MOVFLE; break;
183 case Instruction::SetGE: opCode = MOVFGE; break;
184 case Instruction::SetLT: opCode = MOVFL; break;
185 case Instruction::SetGT: opCode = MOVFG; break;
186 default:
187 assert(0 && "Unrecognized VM instruction!");
188 break;
189 }
190
191 return opCode;
192}
193
194
195// Assumes that SUBcc v1, v2 -> v3 has been executed.
196// In most cases, we want to clear v3 and then follow it by instruction
197// MOVcc 1 -> v3.
198// Set mustClearReg=false if v3 need not be cleared before conditional move.
199// Set valueToMove=0 if we want to conditionally move 0 instead of 1
200// (i.e., we want to test inverse of a condition)
Vikram S. Adve243dd452001-09-18 13:03:13 +0000201// (The latter two cases do not seem to arise because SetNE needs nothing.)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000202//
203static MachineOpCode
204ChooseMovpccAfterSub(const InstructionNode* instrNode,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000205 bool& mustClearReg,
206 int& valueToMove)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000207{
208 MachineOpCode opCode = INVALID_OPCODE;
209 mustClearReg = true;
210 valueToMove = 1;
211
212 switch(instrNode->getInstruction()->getOpcode())
213 {
Vikram S. Adve243dd452001-09-18 13:03:13 +0000214 case Instruction::SetEQ: opCode = MOVE; break;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000215 case Instruction::SetLE: opCode = MOVLE; break;
216 case Instruction::SetGE: opCode = MOVGE; break;
217 case Instruction::SetLT: opCode = MOVL; break;
218 case Instruction::SetGT: opCode = MOVG; break;
Vikram S. Adve243dd452001-09-18 13:03:13 +0000219 case Instruction::SetNE: assert(0 && "No move required!"); break;
220 default: assert(0 && "Unrecognized VM instr!"); break;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000221 }
222
223 return opCode;
224}
225
Chris Lattner20b1ea02001-09-14 03:47:57 +0000226static inline MachineOpCode
Vikram S. Advedbc4fad2002-04-25 04:37:51 +0000227ChooseConvertToFloatInstr(OpLabel vopCode, const Type* opType)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000228{
229 MachineOpCode opCode = INVALID_OPCODE;
230
Vikram S. Advedbc4fad2002-04-25 04:37:51 +0000231 switch(vopCode)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000232 {
233 case ToFloatTy:
234 if (opType == Type::SByteTy || opType == Type::ShortTy || opType == Type::IntTy)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000235 opCode = FITOS;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000236 else if (opType == Type::LongTy)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000237 opCode = FXTOS;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000238 else if (opType == Type::DoubleTy)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000239 opCode = FDTOS;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000240 else if (opType == Type::FloatTy)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000241 ;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000242 else
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000243 assert(0 && "Cannot convert this type to FLOAT on SPARC");
Chris Lattner20b1ea02001-09-14 03:47:57 +0000244 break;
245
246 case ToDoubleTy:
Vikram S. Adve74825322002-03-18 03:15:35 +0000247 // This is usually used in conjunction with CreateCodeToCopyIntToFloat().
248 // Both functions should treat the integer as a 32-bit value for types
249 // of 4 bytes or less, and as a 64-bit value otherwise.
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000250 if (opType == Type::SByteTy || opType == Type::UByteTy ||
251 opType == Type::ShortTy || opType == Type::UShortTy ||
252 opType == Type::IntTy || opType == Type::UIntTy)
Vikram S. Adve74825322002-03-18 03:15:35 +0000253 opCode = FITOD;
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000254 else if (opType == Type::LongTy || opType == Type::ULongTy)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000255 opCode = FXTOD;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000256 else if (opType == Type::FloatTy)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000257 opCode = FSTOD;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000258 else if (opType == Type::DoubleTy)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000259 ;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000260 else
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000261 assert(0 && "Cannot convert this type to DOUBLE on SPARC");
Chris Lattner20b1ea02001-09-14 03:47:57 +0000262 break;
263
264 default:
265 break;
266 }
267
268 return opCode;
269}
270
271static inline MachineOpCode
Vikram S. Adve94c40812002-09-27 14:33:08 +0000272ChooseConvertFPToIntInstr(Type::PrimitiveID tid, const Type* opType)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000273{
274 MachineOpCode opCode = INVALID_OPCODE;;
Vikram S. Adve94c40812002-09-27 14:33:08 +0000275
276 assert((opType == Type::FloatTy || opType == Type::DoubleTy)
277 && "This function should only be called for FLOAT or DOUBLE");
278
279 if (tid==Type::UIntTyID)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000280 {
Vikram S. Adve94c40812002-09-27 14:33:08 +0000281 assert(tid != Type::UIntTyID && "FP-to-uint conversions must be expanded"
282 " into FP->long->uint for SPARC v9: SO RUN PRESELECTION PASS!");
283 }
284 else if (tid==Type::SByteTyID || tid==Type::ShortTyID || tid==Type::IntTyID ||
285 tid==Type::UByteTyID || tid==Type::UShortTyID)
286 {
287 opCode = (opType == Type::FloatTy)? FSTOI : FDTOI;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000288 }
Vikram S. Adve1e606692002-07-31 21:01:34 +0000289 else if (tid==Type::LongTyID || tid==Type::ULongTyID)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000290 {
Vikram S. Adve94c40812002-09-27 14:33:08 +0000291 opCode = (opType == Type::FloatTy)? FSTOX : FDTOX;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000292 }
293 else
294 assert(0 && "Should not get here, Mo!");
Vikram S. Adve94c40812002-09-27 14:33:08 +0000295
Chris Lattner20b1ea02001-09-14 03:47:57 +0000296 return opCode;
297}
298
Vikram S. Advedbc4fad2002-04-25 04:37:51 +0000299MachineInstr*
Vikram S. Adve94c40812002-09-27 14:33:08 +0000300CreateConvertFPToIntInstr(Type::PrimitiveID destTID,
301 Value* srcVal, Value* destVal)
Vikram S. Advedbc4fad2002-04-25 04:37:51 +0000302{
Vikram S. Adve94c40812002-09-27 14:33:08 +0000303 MachineOpCode opCode = ChooseConvertFPToIntInstr(destTID, srcVal->getType());
Vikram S. Advedbc4fad2002-04-25 04:37:51 +0000304 assert(opCode != INVALID_OPCODE && "Expected to need conversion!");
305
306 MachineInstr* M = new MachineInstr(opCode);
307 M->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister, srcVal);
308 M->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister, destVal);
309 return M;
310}
Chris Lattner20b1ea02001-09-14 03:47:57 +0000311
Vikram S. Adve8cfffd32002-08-24 20:56:53 +0000312// CreateCodeToConvertFloatToInt: Convert FP value to signed or unsigned integer
Vikram S. Adve1e606692002-07-31 21:01:34 +0000313// The FP value must be converted to the dest type in an FP register,
314// and the result is then copied from FP to int register via memory.
Vikram S. Advebabc0fa2002-09-05 18:32:13 +0000315//
316// Since fdtoi converts to signed integers, any FP value V between MAXINT+1
317// and MAXUNSIGNED (i.e., 2^31 <= V <= 2^32-1) would be converted incorrectly
318// *only* when converting to an unsigned int. (Unsigned byte, short or long
319// don't have this problem.)
320// For unsigned int, we therefore have to generate the code sequence:
321//
322// if (V > (float) MAXINT) {
323// unsigned result = (unsigned) (V - (float) MAXINT);
324// result = result + (unsigned) MAXINT;
325// }
326// else
327// result = (unsigned int) V;
328//
Vikram S. Adve1e606692002-07-31 21:01:34 +0000329static void
Vikram S. Adve8cfffd32002-08-24 20:56:53 +0000330CreateCodeToConvertFloatToInt(const TargetMachine& target,
331 Value* opVal,
332 Instruction* destI,
333 std::vector<MachineInstr*>& mvec,
334 MachineCodeForInstruction& mcfi)
Vikram S. Adve1e606692002-07-31 21:01:34 +0000335{
336 // Create a temporary to represent the FP register into which the
337 // int value will placed after conversion. The type of this temporary
338 // depends on the type of FP register to use: single-prec for a 32-bit
339 // int or smaller; double-prec for a 64-bit int.
340 //
Vikram S. Advebabc0fa2002-09-05 18:32:13 +0000341 size_t destSize = target.DataLayout.getTypeSize(destI->getType());
342 const Type* destTypeToUse = (destSize > 4)? Type::DoubleTy : Type::FloatTy;
343 TmpInstruction* destForCast = new TmpInstruction(destTypeToUse, opVal);
Vikram S. Adve1e606692002-07-31 21:01:34 +0000344 mcfi.addTemp(destForCast);
345
346 // Create the fp-to-int conversion code
Vikram S. Adve94c40812002-09-27 14:33:08 +0000347 MachineInstr* M =CreateConvertFPToIntInstr(destI->getType()->getPrimitiveID(),
348 opVal, destForCast);
Vikram S. Adve1e606692002-07-31 21:01:34 +0000349 mvec.push_back(M);
350
351 // Create the fpreg-to-intreg copy code
352 target.getInstrInfo().
353 CreateCodeToCopyFloatToInt(target, destI->getParent()->getParent(),
Vikram S. Advebabc0fa2002-09-05 18:32:13 +0000354 destForCast, destI, mvec, mcfi);
Vikram S. Adve1e606692002-07-31 21:01:34 +0000355}
356
357
Chris Lattner20b1ea02001-09-14 03:47:57 +0000358static inline MachineOpCode
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000359ChooseAddInstruction(const InstructionNode* instrNode)
360{
361 return ChooseAddInstructionByType(instrNode->getInstruction()->getType());
362}
363
364
Chris Lattner20b1ea02001-09-14 03:47:57 +0000365static inline MachineInstr*
366CreateMovFloatInstruction(const InstructionNode* instrNode,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000367 const Type* resultType)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000368{
369 MachineInstr* minstr = new MachineInstr((resultType == Type::FloatTy)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000370 ? FMOVS : FMOVD);
Vikram S. Adve74825322002-03-18 03:15:35 +0000371 minstr->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister,
372 instrNode->leftChild()->getValue());
373 minstr->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister,
374 instrNode->getValue());
Chris Lattner20b1ea02001-09-14 03:47:57 +0000375 return minstr;
376}
377
378static inline MachineInstr*
379CreateAddConstInstruction(const InstructionNode* instrNode)
380{
381 MachineInstr* minstr = NULL;
382
383 Value* constOp = ((InstrTreeNode*) instrNode->rightChild())->getValue();
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000384 assert(isa<Constant>(constOp));
Chris Lattner20b1ea02001-09-14 03:47:57 +0000385
386 // Cases worth optimizing are:
387 // (1) Add with 0 for float or double: use an FMOV of appropriate type,
388 // instead of an FADD (1 vs 3 cycles). There is no integer MOV.
389 //
Chris Lattner9b625032002-05-06 16:15:30 +0000390 if (ConstantFP *FPC = dyn_cast<ConstantFP>(constOp)) {
391 double dval = FPC->getValue();
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000392 if (dval == 0.0)
Vikram S. Adve242a8082002-05-19 15:25:51 +0000393 minstr = CreateMovFloatInstruction(instrNode,
394 instrNode->getInstruction()->getType());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000395 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000396
397 return minstr;
398}
399
400
401static inline MachineOpCode
Vikram S. Adve510eec72001-11-04 21:59:14 +0000402ChooseSubInstructionByType(const Type* resultType)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000403{
404 MachineOpCode opCode = INVALID_OPCODE;
405
Chris Lattner0c4e8862002-09-03 01:08:28 +0000406 if (resultType->isInteger() || isa<PointerType>(resultType))
Chris Lattner20b1ea02001-09-14 03:47:57 +0000407 {
408 opCode = SUB;
409 }
410 else
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000411 switch(resultType->getPrimitiveID())
412 {
413 case Type::FloatTyID: opCode = FSUBS; break;
414 case Type::DoubleTyID: opCode = FSUBD; break;
415 default: assert(0 && "Invalid type for SUB instruction"); break;
416 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000417
418 return opCode;
419}
420
421
422static inline MachineInstr*
423CreateSubConstInstruction(const InstructionNode* instrNode)
424{
425 MachineInstr* minstr = NULL;
426
427 Value* constOp = ((InstrTreeNode*) instrNode->rightChild())->getValue();
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000428 assert(isa<Constant>(constOp));
Chris Lattner20b1ea02001-09-14 03:47:57 +0000429
430 // Cases worth optimizing are:
431 // (1) Sub with 0 for float or double: use an FMOV of appropriate type,
432 // instead of an FSUB (1 vs 3 cycles). There is no integer MOV.
433 //
Chris Lattner9b625032002-05-06 16:15:30 +0000434 if (ConstantFP *FPC = dyn_cast<ConstantFP>(constOp)) {
435 double dval = FPC->getValue();
436 if (dval == 0.0)
Vikram S. Adve242a8082002-05-19 15:25:51 +0000437 minstr = CreateMovFloatInstruction(instrNode,
438 instrNode->getInstruction()->getType());
Chris Lattner9b625032002-05-06 16:15:30 +0000439 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000440
441 return minstr;
442}
443
444
445static inline MachineOpCode
446ChooseFcmpInstruction(const InstructionNode* instrNode)
447{
448 MachineOpCode opCode = INVALID_OPCODE;
449
450 Value* operand = ((InstrTreeNode*) instrNode->leftChild())->getValue();
451 switch(operand->getType()->getPrimitiveID()) {
452 case Type::FloatTyID: opCode = FCMPS; break;
453 case Type::DoubleTyID: opCode = FCMPD; break;
454 default: assert(0 && "Invalid type for FCMP instruction"); break;
455 }
456
457 return opCode;
458}
459
460
461// Assumes that leftArg and rightArg are both cast instructions.
462//
463static inline bool
464BothFloatToDouble(const InstructionNode* instrNode)
465{
466 InstrTreeNode* leftArg = instrNode->leftChild();
467 InstrTreeNode* rightArg = instrNode->rightChild();
468 InstrTreeNode* leftArgArg = leftArg->leftChild();
469 InstrTreeNode* rightArgArg = rightArg->leftChild();
470 assert(leftArg->getValue()->getType() == rightArg->getValue()->getType());
471
472 // Check if both arguments are floats cast to double
473 return (leftArg->getValue()->getType() == Type::DoubleTy &&
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000474 leftArgArg->getValue()->getType() == Type::FloatTy &&
475 rightArgArg->getValue()->getType() == Type::FloatTy);
Chris Lattner20b1ea02001-09-14 03:47:57 +0000476}
477
478
479static inline MachineOpCode
Vikram S. Adve510eec72001-11-04 21:59:14 +0000480ChooseMulInstructionByType(const Type* resultType)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000481{
482 MachineOpCode opCode = INVALID_OPCODE;
483
Chris Lattner0c4e8862002-09-03 01:08:28 +0000484 if (resultType->isInteger())
Vikram S. Adve510eec72001-11-04 21:59:14 +0000485 opCode = MULX;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000486 else
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000487 switch(resultType->getPrimitiveID())
488 {
489 case Type::FloatTyID: opCode = FMULS; break;
490 case Type::DoubleTyID: opCode = FMULD; break;
491 default: assert(0 && "Invalid type for MUL instruction"); break;
492 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000493
494 return opCode;
495}
496
497
Vikram S. Adve510eec72001-11-04 21:59:14 +0000498
Chris Lattner20b1ea02001-09-14 03:47:57 +0000499static inline MachineInstr*
Vikram S. Adve74825322002-03-18 03:15:35 +0000500CreateIntNegInstruction(const TargetMachine& target,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000501 Value* vreg)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000502{
503 MachineInstr* minstr = new MachineInstr(SUB);
Vikram S. Adve74825322002-03-18 03:15:35 +0000504 minstr->SetMachineOperandReg(0, target.getRegInfo().getZeroRegNum());
505 minstr->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister, vreg);
506 minstr->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister, vreg);
Chris Lattner20b1ea02001-09-14 03:47:57 +0000507 return minstr;
508}
509
510
Vikram S. Adve242a8082002-05-19 15:25:51 +0000511// Create instruction sequence for any shift operation.
512// SLL or SLLX on an operand smaller than the integer reg. size (64bits)
513// requires a second instruction for explicit sign-extension.
514// Note that we only have to worry about a sign-bit appearing in the
515// most significant bit of the operand after shifting (e.g., bit 32 of
516// Int or bit 16 of Short), so we do not have to worry about results
517// that are as large as a normal integer register.
518//
519static inline void
520CreateShiftInstructions(const TargetMachine& target,
521 Function* F,
522 MachineOpCode shiftOpCode,
523 Value* argVal1,
524 Value* optArgVal2, /* Use optArgVal2 if not NULL */
525 unsigned int optShiftNum, /* else use optShiftNum */
526 Instruction* destVal,
527 vector<MachineInstr*>& mvec,
528 MachineCodeForInstruction& mcfi)
529{
530 assert((optArgVal2 != NULL || optShiftNum <= 64) &&
531 "Large shift sizes unexpected, but can be handled below: "
532 "You need to check whether or not it fits in immed field below");
533
534 // If this is a logical left shift of a type smaller than the standard
535 // integer reg. size, we have to extend the sign-bit into upper bits
536 // of dest, so we need to put the result of the SLL into a temporary.
537 //
538 Value* shiftDest = destVal;
Vikram S. Adve242a8082002-05-19 15:25:51 +0000539 unsigned opSize = target.DataLayout.getTypeSize(argVal1->getType());
540 if ((shiftOpCode == SLL || shiftOpCode == SLLX)
541 && opSize < target.DataLayout.getIntegerRegize())
542 { // put SLL result into a temporary
543 shiftDest = new TmpInstruction(argVal1, optArgVal2, "sllTmp");
544 mcfi.addTemp(shiftDest);
545 }
546
547 MachineInstr* M = (optArgVal2 != NULL)
548 ? Create3OperandInstr(shiftOpCode, argVal1, optArgVal2, shiftDest)
549 : Create3OperandInstr_UImmed(shiftOpCode, argVal1, optShiftNum, shiftDest);
550 mvec.push_back(M);
551
552 if (shiftDest != destVal)
553 { // extend the sign-bit of the result into all upper bits of dest
554 assert(8*opSize <= 32 && "Unexpected type size > 4 and < IntRegSize?");
555 target.getInstrInfo().
Vikram S. Adve94c40812002-09-27 14:33:08 +0000556 CreateSignExtensionInstructions(target, F, shiftDest, destVal,
557 8*opSize, mvec, mcfi);
Vikram S. Adve242a8082002-05-19 15:25:51 +0000558 }
559}
560
561
Vikram S. Adve74825322002-03-18 03:15:35 +0000562// Does not create any instructions if we cannot exploit constant to
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000563// create a cheaper instruction.
564// This returns the approximate cost of the instructions generated,
565// which is used to pick the cheapest when both operands are constant.
566static inline unsigned int
Vikram S. Adve242a8082002-05-19 15:25:51 +0000567CreateMulConstInstruction(const TargetMachine &target, Function* F,
568 Value* lval, Value* rval, Instruction* destVal,
569 vector<MachineInstr*>& mvec,
570 MachineCodeForInstruction& mcfi)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000571{
Vikram S. Adve242a8082002-05-19 15:25:51 +0000572 /* Use max. multiply cost, viz., cost of MULX */
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000573 unsigned int cost = target.getInstrInfo().minLatency(MULX);
Vikram S. Adve242a8082002-05-19 15:25:51 +0000574 unsigned int firstNewInstr = mvec.size();
Vikram S. Adve74825322002-03-18 03:15:35 +0000575
576 Value* constOp = rval;
577 if (! isa<Constant>(constOp))
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000578 return cost;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000579
580 // Cases worth optimizing are:
581 // (1) Multiply by 0 or 1 for any type: replace with copy (ADD or FMOV)
582 // (2) Multiply by 2^x for integer types: replace with Shift
583 //
Vikram S. Adve74825322002-03-18 03:15:35 +0000584 const Type* resultType = destVal->getType();
Chris Lattner20b1ea02001-09-14 03:47:57 +0000585
Chris Lattner0c4e8862002-09-03 01:08:28 +0000586 if (resultType->isInteger() || isa<PointerType>(resultType))
Chris Lattner20b1ea02001-09-14 03:47:57 +0000587 {
Chris Lattner20b1ea02001-09-14 03:47:57 +0000588 bool isValidConst;
589 int64_t C = GetConstantValueAsSignedInt(constOp, isValidConst);
590 if (isValidConst)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000591 {
Vikram S. Adve242a8082002-05-19 15:25:51 +0000592 unsigned pow;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000593 bool needNeg = false;
594 if (C < 0)
595 {
596 needNeg = true;
597 C = -C;
598 }
599
600 if (C == 0 || C == 1)
601 {
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000602 cost = target.getInstrInfo().minLatency(ADD);
Vikram S. Adve242a8082002-05-19 15:25:51 +0000603 MachineInstr* M = (C == 0)
604 ? Create3OperandInstr_Reg(ADD,
605 target.getRegInfo().getZeroRegNum(),
606 target.getRegInfo().getZeroRegNum(),
607 destVal)
608 : Create3OperandInstr_Reg(ADD, lval,
609 target.getRegInfo().getZeroRegNum(),
610 destVal);
611 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000612 }
Chris Lattner36346c72002-05-19 21:20:19 +0000613 else if (isPowerOf2(C, pow))
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000614 {
Vikram S. Adve242a8082002-05-19 15:25:51 +0000615 unsigned int opSize = target.DataLayout.getTypeSize(resultType);
616 MachineOpCode opCode = (opSize <= 32)? SLL : SLLX;
617 CreateShiftInstructions(target, F, opCode, lval, NULL, pow,
618 destVal, mvec, mcfi);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000619 }
620
Vikram S. Adve242a8082002-05-19 15:25:51 +0000621 if (mvec.size() > 0 && needNeg)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000622 { // insert <reg = SUB 0, reg> after the instr to flip the sign
Vikram S. Adve242a8082002-05-19 15:25:51 +0000623 MachineInstr* M = CreateIntNegInstruction(target, destVal);
624 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000625 }
626 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000627 }
628 else
629 {
Chris Lattner9b625032002-05-06 16:15:30 +0000630 if (ConstantFP *FPC = dyn_cast<ConstantFP>(constOp))
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000631 {
Chris Lattner9b625032002-05-06 16:15:30 +0000632 double dval = FPC->getValue();
Vikram S. Adve6ad7c552001-11-09 02:18:16 +0000633 if (fabs(dval) == 1)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000634 {
Vikram S. Adve242a8082002-05-19 15:25:51 +0000635 MachineOpCode opCode = (dval < 0)
Vikram S. Adve6ad7c552001-11-09 02:18:16 +0000636 ? (resultType == Type::FloatTy? FNEGS : FNEGD)
637 : (resultType == Type::FloatTy? FMOVS : FMOVD);
Vikram S. Adve242a8082002-05-19 15:25:51 +0000638 MachineInstr* M = Create2OperandInstr(opCode, lval, destVal);
639 mvec.push_back(M);
Vikram S. Adve6ad7c552001-11-09 02:18:16 +0000640 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000641 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000642 }
643
Vikram S. Adve242a8082002-05-19 15:25:51 +0000644 if (firstNewInstr < mvec.size())
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000645 {
Vikram S. Adve242a8082002-05-19 15:25:51 +0000646 cost = 0;
647 for (unsigned int i=firstNewInstr; i < mvec.size(); ++i)
648 cost += target.getInstrInfo().minLatency(mvec[i]->getOpCode());
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000649 }
650
651 return cost;
Vikram S. Adve74825322002-03-18 03:15:35 +0000652}
653
654
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000655// Does not create any instructions if we cannot exploit constant to
656// create a cheaper instruction.
657//
658static inline void
659CreateCheapestMulConstInstruction(const TargetMachine &target,
Vikram S. Adve242a8082002-05-19 15:25:51 +0000660 Function* F,
661 Value* lval, Value* rval,
662 Instruction* destVal,
663 vector<MachineInstr*>& mvec,
664 MachineCodeForInstruction& mcfi)
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000665{
666 Value* constOp;
667 if (isa<Constant>(lval) && isa<Constant>(rval))
Vikram S. Adved3e26482002-10-13 00:18:57 +0000668 { // both operands are constant: evaluate and "set" in dest
669 Constant* P = ConstantFoldBinaryInstruction(Instruction::Mul,
670 cast<Constant>(lval), cast<Constant>(rval));
671 target.getInstrInfo().CreateCodeToLoadConst(target,F,P,destVal,mvec,mcfi);
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000672 }
673 else if (isa<Constant>(rval)) // rval is constant, but not lval
Vikram S. Adve242a8082002-05-19 15:25:51 +0000674 CreateMulConstInstruction(target, F, lval, rval, destVal, mvec, mcfi);
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000675 else if (isa<Constant>(lval)) // lval is constant, but not rval
Vikram S. Adve242a8082002-05-19 15:25:51 +0000676 CreateMulConstInstruction(target, F, lval, rval, destVal, mvec, mcfi);
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000677
678 // else neither is constant
679 return;
680}
681
Vikram S. Adve74825322002-03-18 03:15:35 +0000682// Return NULL if we cannot exploit constant to create a cheaper instruction
683static inline void
Vikram S. Adve242a8082002-05-19 15:25:51 +0000684CreateMulInstruction(const TargetMachine &target, Function* F,
685 Value* lval, Value* rval, Instruction* destVal,
Vikram S. Adve74825322002-03-18 03:15:35 +0000686 vector<MachineInstr*>& mvec,
Vikram S. Adve242a8082002-05-19 15:25:51 +0000687 MachineCodeForInstruction& mcfi,
Vikram S. Adve74825322002-03-18 03:15:35 +0000688 MachineOpCode forceMulOp = INVALID_MACHINE_OPCODE)
689{
690 unsigned int L = mvec.size();
Vikram S. Adve242a8082002-05-19 15:25:51 +0000691 CreateCheapestMulConstInstruction(target,F, lval, rval, destVal, mvec, mcfi);
Vikram S. Adve74825322002-03-18 03:15:35 +0000692 if (mvec.size() == L)
693 { // no instructions were added so create MUL reg, reg, reg.
694 // Use FSMULD if both operands are actually floats cast to doubles.
695 // Otherwise, use the default opcode for the appropriate type.
696 MachineOpCode mulOp = ((forceMulOp != INVALID_MACHINE_OPCODE)
697 ? forceMulOp
698 : ChooseMulInstructionByType(destVal->getType()));
699 MachineInstr* M = new MachineInstr(mulOp);
700 M->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister, lval);
701 M->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister, rval);
702 M->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister, destVal);
703 mvec.push_back(M);
704 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000705}
706
707
Vikram S. Adve510eec72001-11-04 21:59:14 +0000708// Generate a divide instruction for Div or Rem.
709// For Rem, this assumes that the operand type will be signed if the result
710// type is signed. This is correct because they must have the same sign.
711//
Chris Lattner20b1ea02001-09-14 03:47:57 +0000712static inline MachineOpCode
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000713ChooseDivInstruction(TargetMachine &target,
714 const InstructionNode* instrNode)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000715{
716 MachineOpCode opCode = INVALID_OPCODE;
717
718 const Type* resultType = instrNode->getInstruction()->getType();
719
Chris Lattner0c4e8862002-09-03 01:08:28 +0000720 if (resultType->isInteger())
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000721 opCode = resultType->isSigned()? SDIVX : UDIVX;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000722 else
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000723 switch(resultType->getPrimitiveID())
724 {
725 case Type::FloatTyID: opCode = FDIVS; break;
726 case Type::DoubleTyID: opCode = FDIVD; break;
727 default: assert(0 && "Invalid type for DIV instruction"); break;
728 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000729
730 return opCode;
731}
732
733
Vikram S. Adve74825322002-03-18 03:15:35 +0000734// Return NULL if we cannot exploit constant to create a cheaper instruction
735static inline void
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000736CreateDivConstInstruction(TargetMachine &target,
737 const InstructionNode* instrNode,
Vikram S. Adve74825322002-03-18 03:15:35 +0000738 vector<MachineInstr*>& mvec)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000739{
Vikram S. Adve74825322002-03-18 03:15:35 +0000740 MachineInstr* minstr1 = NULL;
741 MachineInstr* minstr2 = NULL;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000742
743 Value* constOp = ((InstrTreeNode*) instrNode->rightChild())->getValue();
Vikram S. Adve74825322002-03-18 03:15:35 +0000744 if (! isa<Constant>(constOp))
745 return;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000746
747 // Cases worth optimizing are:
748 // (1) Divide by 1 for any type: replace with copy (ADD or FMOV)
749 // (2) Divide by 2^x for integer types: replace with SR[L or A]{X}
750 //
751 const Type* resultType = instrNode->getInstruction()->getType();
752
Chris Lattner0c4e8862002-09-03 01:08:28 +0000753 if (resultType->isInteger())
Chris Lattner20b1ea02001-09-14 03:47:57 +0000754 {
755 unsigned pow;
756 bool isValidConst;
757 int64_t C = GetConstantValueAsSignedInt(constOp, isValidConst);
758 if (isValidConst)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000759 {
760 bool needNeg = false;
761 if (C < 0)
762 {
763 needNeg = true;
764 C = -C;
765 }
766
767 if (C == 1)
768 {
Vikram S. Adve74825322002-03-18 03:15:35 +0000769 minstr1 = new MachineInstr(ADD);
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000770 minstr1->SetMachineOperandVal(0,
771 MachineOperand::MO_VirtualRegister,
772 instrNode->leftChild()->getValue());
773 minstr1->SetMachineOperandReg(1,
774 target.getRegInfo().getZeroRegNum());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000775 }
Chris Lattner36346c72002-05-19 21:20:19 +0000776 else if (isPowerOf2(C, pow))
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000777 {
778 MachineOpCode opCode= ((resultType->isSigned())
779 ? (resultType==Type::LongTy)? SRAX : SRA
780 : (resultType==Type::LongTy)? SRLX : SRL);
Vikram S. Adve74825322002-03-18 03:15:35 +0000781 minstr1 = new MachineInstr(opCode);
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000782 minstr1->SetMachineOperandVal(0,
783 MachineOperand::MO_VirtualRegister,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000784 instrNode->leftChild()->getValue());
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000785 minstr1->SetMachineOperandConst(1,
786 MachineOperand::MO_UnextendedImmed,
787 pow);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000788 }
789
Vikram S. Adve74825322002-03-18 03:15:35 +0000790 if (minstr1 && needNeg)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000791 { // insert <reg = SUB 0, reg> after the instr to flip the sign
Vikram S. Adve74825322002-03-18 03:15:35 +0000792 minstr2 = CreateIntNegInstruction(target,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000793 instrNode->getValue());
794 }
795 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000796 }
797 else
798 {
Chris Lattner9b625032002-05-06 16:15:30 +0000799 if (ConstantFP *FPC = dyn_cast<ConstantFP>(constOp))
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000800 {
Chris Lattner9b625032002-05-06 16:15:30 +0000801 double dval = FPC->getValue();
Vikram S. Adve6ad7c552001-11-09 02:18:16 +0000802 if (fabs(dval) == 1)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000803 {
804 bool needNeg = (dval < 0);
805
806 MachineOpCode opCode = needNeg
807 ? (resultType == Type::FloatTy? FNEGS : FNEGD)
808 : (resultType == Type::FloatTy? FMOVS : FMOVD);
809
Vikram S. Adve74825322002-03-18 03:15:35 +0000810 minstr1 = new MachineInstr(opCode);
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000811 minstr1->SetMachineOperandVal(0,
812 MachineOperand::MO_VirtualRegister,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000813 instrNode->leftChild()->getValue());
814 }
815 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000816 }
817
Vikram S. Adve74825322002-03-18 03:15:35 +0000818 if (minstr1 != NULL)
819 minstr1->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister,
820 instrNode->getValue());
Chris Lattner20b1ea02001-09-14 03:47:57 +0000821
Vikram S. Adve74825322002-03-18 03:15:35 +0000822 if (minstr1)
823 mvec.push_back(minstr1);
824 if (minstr2)
825 mvec.push_back(minstr2);
Chris Lattner20b1ea02001-09-14 03:47:57 +0000826}
827
828
Vikram S. Adve74825322002-03-18 03:15:35 +0000829static void
830CreateCodeForVariableSizeAlloca(const TargetMachine& target,
831 Instruction* result,
832 unsigned int tsize,
833 Value* numElementsVal,
834 vector<MachineInstr*>& getMvec)
835{
836 MachineInstr* M;
Vikram S. Adved3e26482002-10-13 00:18:57 +0000837 MachineCodeForInstruction& mcfi = MachineCodeForInstruction::get(result);
838
Vikram S. Adve74825322002-03-18 03:15:35 +0000839 // Create a Value to hold the (constant) element size
840 Value* tsizeVal = ConstantSInt::get(Type::IntTy, tsize);
841
842 // Get the constant offset from SP for dynamically allocated storage
843 // and create a temporary Value to hold it.
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000844 assert(result && result->getParent() && "Result value is not part of a fn?");
845 Function *F = result->getParent()->getParent();
Misha Brukmanfce11432002-10-28 00:28:31 +0000846 MachineFunction& mcInfo = MachineFunction::get(F);
Vikram S. Adve74825322002-03-18 03:15:35 +0000847 bool growUp;
848 ConstantSInt* dynamicAreaOffset =
849 ConstantSInt::get(Type::IntTy,
850 target.getFrameInfo().getDynamicAreaOffset(mcInfo,growUp));
851 assert(! growUp && "Has SPARC v9 stack frame convention changed?");
852
853 // Create a temporary value to hold the result of MUL
854 TmpInstruction* tmpProd = new TmpInstruction(numElementsVal, tsizeVal);
Vikram S. Adved3e26482002-10-13 00:18:57 +0000855 mcfi.addTemp(tmpProd);
Vikram S. Adve74825322002-03-18 03:15:35 +0000856
857 // Instruction 1: mul numElements, typeSize -> tmpProd
Vikram S. Adved3e26482002-10-13 00:18:57 +0000858 CreateMulInstruction(target, F, numElementsVal, tsizeVal, tmpProd, getMvec,
859 mcfi, INVALID_MACHINE_OPCODE);
Vikram S. Adve74825322002-03-18 03:15:35 +0000860
861 // Instruction 2: sub %sp, tmpProd -> %sp
862 M = new MachineInstr(SUB);
863 M->SetMachineOperandReg(0, target.getRegInfo().getStackPointer());
864 M->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister, tmpProd);
865 M->SetMachineOperandReg(2, target.getRegInfo().getStackPointer());
866 getMvec.push_back(M);
867
868 // Instruction 3: add %sp, frameSizeBelowDynamicArea -> result
869 M = new MachineInstr(ADD);
870 M->SetMachineOperandReg(0, target.getRegInfo().getStackPointer());
871 M->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister, dynamicAreaOffset);
872 M->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister, result);
873 getMvec.push_back(M);
874}
875
876
877static void
878CreateCodeForFixedSizeAlloca(const TargetMachine& target,
879 Instruction* result,
880 unsigned int tsize,
881 unsigned int numElements,
882 vector<MachineInstr*>& getMvec)
883{
Vikram S. Adved3e26482002-10-13 00:18:57 +0000884 assert(tsize > 0 && "Illegal (zero) type size for alloca");
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000885 assert(result && result->getParent() &&
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000886 "Result value is not part of a function?");
887 Function *F = result->getParent()->getParent();
Misha Brukmanfce11432002-10-28 00:28:31 +0000888 MachineFunction &mcInfo = MachineFunction::get(F);
Vikram S. Adve74825322002-03-18 03:15:35 +0000889
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000890 // Check if the offset would small enough to use as an immediate in
891 // load/stores (check LDX because all load/stores have the same-size immediate
892 // field). If not, put the variable in the dynamically sized area of the
893 // frame.
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000894 unsigned int paddedSizeIgnored;
Vikram S. Adve74825322002-03-18 03:15:35 +0000895 int offsetFromFP = mcInfo.computeOffsetforLocalVar(target, result,
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000896 paddedSizeIgnored,
Vikram S. Adve74825322002-03-18 03:15:35 +0000897 tsize * numElements);
898 if (! target.getInstrInfo().constantFitsInImmedField(LDX, offsetFromFP))
899 {
900 CreateCodeForVariableSizeAlloca(target, result, tsize,
901 ConstantSInt::get(Type::IntTy,numElements),
902 getMvec);
903 return;
904 }
905
906 // else offset fits in immediate field so go ahead and allocate it.
907 offsetFromFP = mcInfo.allocateLocalVar(target, result, tsize * numElements);
908
909 // Create a temporary Value to hold the constant offset.
910 // This is needed because it may not fit in the immediate field.
911 ConstantSInt* offsetVal = ConstantSInt::get(Type::IntTy, offsetFromFP);
912
913 // Instruction 1: add %fp, offsetFromFP -> result
914 MachineInstr* M = new MachineInstr(ADD);
915 M->SetMachineOperandReg(0, target.getRegInfo().getFramePointer());
916 M->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister, offsetVal);
917 M->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister, result);
918
919 getMvec.push_back(M);
920}
921
922
Chris Lattner20b1ea02001-09-14 03:47:57 +0000923//------------------------------------------------------------------------
924// Function SetOperandsForMemInstr
925//
926// Choose addressing mode for the given load or store instruction.
927// Use [reg+reg] if it is an indexed reference, and the index offset is
928// not a constant or if it cannot fit in the offset field.
929// Use [reg+offset] in all other cases.
930//
931// This assumes that all array refs are "lowered" to one of these forms:
932// %x = load (subarray*) ptr, constant ; single constant offset
933// %x = load (subarray*) ptr, offsetVal ; single non-constant offset
934// Generally, this should happen via strength reduction + LICM.
935// Also, strength reduction should take care of using the same register for
936// the loop index variable and an array index, when that is profitable.
937//------------------------------------------------------------------------
938
939static void
Vikram S. Adve74825322002-03-18 03:15:35 +0000940SetOperandsForMemInstr(vector<MachineInstr*>& mvec,
Vikram S. Adveefc94332002-10-14 16:32:24 +0000941 InstructionNode* vmInstrNode,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000942 const TargetMachine& target)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000943{
Vikram S. Adve8cfffd32002-08-24 20:56:53 +0000944 Instruction* memInst = vmInstrNode->getInstruction();
945 vector<MachineInstr*>::iterator mvecI = mvec.end() - 1;
946
947 // Index vector, ptr value, and flag if all indices are const.
Vikram S. Advea10d1a72002-03-31 19:07:35 +0000948 vector<Value*> idxVec;
Vikram S. Adve8cfffd32002-08-24 20:56:53 +0000949 bool allConstantIndices;
950 Value* ptrVal = GetMemInstArgs(vmInstrNode, idxVec, allConstantIndices);
Vikram S. Adveed3fefb2002-08-03 13:48:21 +0000951
Vikram S. Adve8cfffd32002-08-24 20:56:53 +0000952 // Now create the appropriate operands for the machine instruction.
953 // First, initialize so we default to storing the offset in a register.
Chris Lattner8e5c0b42001-11-07 14:01:59 +0000954 int64_t smallConstOffset = 0;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000955 Value* valueForRegOffset = NULL;
Vikram S. Adveed3fefb2002-08-03 13:48:21 +0000956 MachineOperand::MachineOperandType offsetOpType =
957 MachineOperand::MO_VirtualRegister;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000958
Vikram S. Adve74825322002-03-18 03:15:35 +0000959 // Check if there is an index vector and if so, compute the
960 // right offset for structures and for arrays
Chris Lattner20b1ea02001-09-14 03:47:57 +0000961 //
Chris Lattner3bb8ad22002-08-22 23:37:24 +0000962 if (!idxVec.empty())
Chris Lattner20b1ea02001-09-14 03:47:57 +0000963 {
Vikram S. Adve74825322002-03-18 03:15:35 +0000964 const PointerType* ptrType = cast<PointerType>(ptrVal->getType());
Chris Lattner20b1ea02001-09-14 03:47:57 +0000965
Vikram S. Adve242a8082002-05-19 15:25:51 +0000966 // If all indices are constant, compute the combined offset directly.
967 if (allConstantIndices)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000968 {
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000969 // Compute the offset value using the index vector. Create a
970 // virtual reg. for it since it may not fit in the immed field.
Vikram S. Adve242a8082002-05-19 15:25:51 +0000971 uint64_t offset = target.DataLayout.getIndexedOffset(ptrType,idxVec);
972 valueForRegOffset = ConstantSInt::get(Type::LongTy, offset);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000973 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000974 else
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000975 {
Vikram S. Adve242a8082002-05-19 15:25:51 +0000976 // There is at least one non-constant offset. Therefore, this must
Vikram S. Adveed3fefb2002-08-03 13:48:21 +0000977 // be an array ref, and must have been lowered to a single non-zero
978 // offset. (An extra leading zero offset, if any, can be ignored.)
979 // Generate code sequence to compute address from index.
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000980 //
Chris Lattner0374b8d2002-09-11 01:21:35 +0000981 bool firstIdxIsZero =
982 (idxVec[0] == Constant::getNullValue(idxVec[0]->getType()));
Vikram S. Advebabc0fa2002-09-05 18:32:13 +0000983 assert(idxVec.size() == 1U + firstIdxIsZero
Vikram S. Adveed3fefb2002-08-03 13:48:21 +0000984 && "Array refs must be lowered before Instruction Selection");
985
Vikram S. Advebabc0fa2002-09-05 18:32:13 +0000986 Value* idxVal = idxVec[firstIdxIsZero];
Vikram S. Adveed3fefb2002-08-03 13:48:21 +0000987
988 vector<MachineInstr*> mulVec;
Vikram S. Adve94c40812002-09-27 14:33:08 +0000989 Instruction* addr = new TmpInstruction(Type::ULongTy, memInst);
Vikram S. Adveed3fefb2002-08-03 13:48:21 +0000990 MachineCodeForInstruction::get(memInst).addTemp(addr);
991
Vikram S. Advebabc0fa2002-09-05 18:32:13 +0000992 // Get the array type indexed by idxVal, and compute its element size.
Vikram S. Adveed3fefb2002-08-03 13:48:21 +0000993 // The call to getTypeSize() will fail if size is not constant.
Vikram S. Advebabc0fa2002-09-05 18:32:13 +0000994 const Type* vecType = (firstIdxIsZero
995 ? GetElementPtrInst::getIndexedType(ptrType,
996 std::vector<Value*>(1U, idxVec[0]),
997 /*AllowCompositeLeaf*/ true)
998 : ptrType);
999 const Type* eltType = cast<SequentialType>(vecType)->getElementType();
Vikram S. Advee102a642002-09-16 15:56:45 +00001000 ConstantUInt* eltSizeVal = ConstantUInt::get(Type::ULongTy,
Vikram S. Advebabc0fa2002-09-05 18:32:13 +00001001 target.DataLayout.getTypeSize(eltType));
Vikram S. Adveed3fefb2002-08-03 13:48:21 +00001002
1003 // CreateMulInstruction() folds constants intelligently enough.
Vikram S. Adved3e26482002-10-13 00:18:57 +00001004 CreateMulInstruction(target, memInst->getParent()->getParent(),
Vikram S. Advebabc0fa2002-09-05 18:32:13 +00001005 idxVal, /* lval, not likely to be const*/
1006 eltSizeVal, /* rval, likely to be constant */
1007 addr, /* result */
Vikram S. Adved3e26482002-10-13 00:18:57 +00001008 mulVec, MachineCodeForInstruction::get(memInst),
Vikram S. Adveed3fefb2002-08-03 13:48:21 +00001009 INVALID_MACHINE_OPCODE);
1010
1011 // Insert mulVec[] before *mvecI in mvec[] and update mvecI
1012 // to point to the same instruction it pointed to before.
1013 assert(mulVec.size() > 0 && "No multiply code created?");
1014 vector<MachineInstr*>::iterator oldMvecI = mvecI;
1015 for (unsigned i=0, N=mulVec.size(); i < N; ++i)
1016 mvecI = mvec.insert(mvecI, mulVec[i]) + 1; // pts to mem instr
1017
1018 valueForRegOffset = addr;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001019 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00001020 }
1021 else
1022 {
1023 offsetOpType = MachineOperand::MO_SignExtendedImmed;
1024 smallConstOffset = 0;
1025 }
Vikram S. Adveed3fefb2002-08-03 13:48:21 +00001026
Vikram S. Advea10d1a72002-03-31 19:07:35 +00001027 // For STORE:
1028 // Operand 0 is value, operand 1 is ptr, operand 2 is offset
1029 // For LOAD or GET_ELEMENT_PTR,
1030 // Operand 0 is ptr, operand 1 is offset, operand 2 is result.
1031 //
1032 unsigned offsetOpNum, ptrOpNum;
1033 if (memInst->getOpcode() == Instruction::Store)
1034 {
1035 (*mvecI)->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister,
1036 vmInstrNode->leftChild()->getValue());
1037 ptrOpNum = 1;
1038 offsetOpNum = 2;
1039 }
1040 else
1041 {
1042 ptrOpNum = 0;
1043 offsetOpNum = 1;
1044 (*mvecI)->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister,
1045 memInst);
1046 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00001047
Vikram S. Advea10d1a72002-03-31 19:07:35 +00001048 (*mvecI)->SetMachineOperandVal(ptrOpNum, MachineOperand::MO_VirtualRegister,
1049 ptrVal);
1050
Chris Lattner20b1ea02001-09-14 03:47:57 +00001051 if (offsetOpType == MachineOperand::MO_VirtualRegister)
1052 {
1053 assert(valueForRegOffset != NULL);
Vikram S. Adve74825322002-03-18 03:15:35 +00001054 (*mvecI)->SetMachineOperandVal(offsetOpNum, offsetOpType,
1055 valueForRegOffset);
Chris Lattner20b1ea02001-09-14 03:47:57 +00001056 }
1057 else
Vikram S. Adve74825322002-03-18 03:15:35 +00001058 (*mvecI)->SetMachineOperandConst(offsetOpNum, offsetOpType,
1059 smallConstOffset);
Chris Lattner20b1ea02001-09-14 03:47:57 +00001060}
1061
1062
Chris Lattner20b1ea02001-09-14 03:47:57 +00001063//
1064// Substitute operand `operandNum' of the instruction in node `treeNode'
Vikram S. Advec025fc12001-10-14 23:28:43 +00001065// in place of the use(s) of that instruction in node `parent'.
1066// Check both explicit and implicit operands!
Vikram S. Adve74825322002-03-18 03:15:35 +00001067// Also make sure to skip over a parent who:
1068// (1) is a list node in the Burg tree, or
1069// (2) itself had its results forwarded to its parent
Chris Lattner20b1ea02001-09-14 03:47:57 +00001070//
1071static void
1072ForwardOperand(InstructionNode* treeNode,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001073 InstrTreeNode* parent,
1074 int operandNum)
Chris Lattner20b1ea02001-09-14 03:47:57 +00001075{
Vikram S. Adve243dd452001-09-18 13:03:13 +00001076 assert(treeNode && parent && "Invalid invocation of ForwardOperand");
1077
Chris Lattner20b1ea02001-09-14 03:47:57 +00001078 Instruction* unusedOp = treeNode->getInstruction();
1079 Value* fwdOp = unusedOp->getOperand(operandNum);
Vikram S. Adve243dd452001-09-18 13:03:13 +00001080
1081 // The parent itself may be a list node, so find the real parent instruction
1082 while (parent->getNodeType() != InstrTreeNode::NTInstructionNode)
1083 {
1084 parent = parent->parent();
1085 assert(parent && "ERROR: Non-instruction node has no parent in tree.");
1086 }
1087 InstructionNode* parentInstrNode = (InstructionNode*) parent;
1088
1089 Instruction* userInstr = parentInstrNode->getInstruction();
Chris Lattner9c461082002-02-03 07:50:56 +00001090 MachineCodeForInstruction &mvec = MachineCodeForInstruction::get(userInstr);
Vikram S. Adve74825322002-03-18 03:15:35 +00001091
1092 // The parent's mvec would be empty if it was itself forwarded.
1093 // Recursively call ForwardOperand in that case...
1094 //
1095 if (mvec.size() == 0)
Chris Lattner20b1ea02001-09-14 03:47:57 +00001096 {
Vikram S. Adve74825322002-03-18 03:15:35 +00001097 assert(parent->parent() != NULL &&
1098 "Parent could not have been forwarded, yet has no instructions?");
1099 ForwardOperand(treeNode, parent->parent(), operandNum);
1100 }
1101 else
1102 {
Vikram S. Adve74825322002-03-18 03:15:35 +00001103 for (unsigned i=0, N=mvec.size(); i < N; i++)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001104 {
Vikram S. Adve74825322002-03-18 03:15:35 +00001105 MachineInstr* minstr = mvec[i];
1106 for (unsigned i=0, numOps=minstr->getNumOperands(); i < numOps; ++i)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001107 {
Vikram S. Adve74825322002-03-18 03:15:35 +00001108 const MachineOperand& mop = minstr->getOperand(i);
Chris Lattner133f0792002-10-28 04:45:29 +00001109 if (mop.getType() == MachineOperand::MO_VirtualRegister &&
Vikram S. Adve74825322002-03-18 03:15:35 +00001110 mop.getVRegValue() == unusedOp)
Vikram S. Adve242a8082002-05-19 15:25:51 +00001111 minstr->SetMachineOperandVal(i,
Vikram S. Adve74825322002-03-18 03:15:35 +00001112 MachineOperand::MO_VirtualRegister, fwdOp);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001113 }
Vikram S. Adve74825322002-03-18 03:15:35 +00001114
1115 for (unsigned i=0,numOps=minstr->getNumImplicitRefs(); i<numOps; ++i)
1116 if (minstr->getImplicitRef(i) == unusedOp)
Vikram S. Adve242a8082002-05-19 15:25:51 +00001117 minstr->setImplicitRef(i, fwdOp,
Vikram S. Adve6418eac2002-07-08 23:30:14 +00001118 minstr->implicitRefIsDefined(i),
1119 minstr->implicitRefIsDefinedAndUsed(i));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001120 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00001121 }
1122}
1123
1124
Vikram S. Adve242a8082002-05-19 15:25:51 +00001125inline bool
1126AllUsesAreBranches(const Instruction* setccI)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001127{
Vikram S. Adve242a8082002-05-19 15:25:51 +00001128 for (Value::use_const_iterator UI=setccI->use_begin(), UE=setccI->use_end();
1129 UI != UE; ++UI)
1130 if (! isa<TmpInstruction>(*UI) // ignore tmp instructions here
1131 && cast<Instruction>(*UI)->getOpcode() != Instruction::Br)
1132 return false;
1133 return true;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001134}
1135
Vikram S. Advefb361122001-10-22 13:36:31 +00001136//******************* Externally Visible Functions *************************/
1137
Vikram S. Advefb361122001-10-22 13:36:31 +00001138//------------------------------------------------------------------------
1139// External Function: ThisIsAChainRule
1140//
1141// Purpose:
1142// Check if a given BURG rule is a chain rule.
1143//------------------------------------------------------------------------
1144
1145extern bool
1146ThisIsAChainRule(int eruleno)
1147{
1148 switch(eruleno)
1149 {
1150 case 111: // stmt: reg
Vikram S. Advefb361122001-10-22 13:36:31 +00001151 case 123:
1152 case 124:
1153 case 125:
1154 case 126:
1155 case 127:
1156 case 128:
1157 case 129:
1158 case 130:
1159 case 131:
1160 case 132:
1161 case 133:
1162 case 155:
1163 case 221:
1164 case 222:
1165 case 241:
1166 case 242:
1167 case 243:
1168 case 244:
Vikram S. Adve30a6f492002-08-22 02:56:10 +00001169 case 245:
Vikram S. Adve85e1e9c2002-04-01 20:28:48 +00001170 case 321:
Vikram S. Advefb361122001-10-22 13:36:31 +00001171 return true; break;
Vikram S. Adve30a6f492002-08-22 02:56:10 +00001172
Vikram S. Advefb361122001-10-22 13:36:31 +00001173 default:
1174 return false; break;
1175 }
1176}
Chris Lattner20b1ea02001-09-14 03:47:57 +00001177
1178
1179//------------------------------------------------------------------------
1180// External Function: GetInstructionsByRule
1181//
1182// Purpose:
1183// Choose machine instructions for the SPARC according to the
1184// patterns chosen by the BURG-generated parser.
1185//------------------------------------------------------------------------
1186
Vikram S. Adve74825322002-03-18 03:15:35 +00001187void
Chris Lattner20b1ea02001-09-14 03:47:57 +00001188GetInstructionsByRule(InstructionNode* subtreeRoot,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001189 int ruleForNode,
1190 short* nts,
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001191 TargetMachine &target,
Vikram S. Adve74825322002-03-18 03:15:35 +00001192 vector<MachineInstr*>& mvec)
Chris Lattner20b1ea02001-09-14 03:47:57 +00001193{
Chris Lattner20b1ea02001-09-14 03:47:57 +00001194 bool checkCast = false; // initialize here to use fall-through
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001195 bool maskUnsignedResult = false;
Chris Lattner20b1ea02001-09-14 03:47:57 +00001196 int nextRule;
1197 int forwardOperandNum = -1;
Vikram S. Adve74825322002-03-18 03:15:35 +00001198 unsigned int allocaSize = 0;
1199 MachineInstr* M, *M2;
1200 unsigned int L;
1201
1202 mvec.clear();
Chris Lattner20b1ea02001-09-14 03:47:57 +00001203
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001204 // If the code for this instruction was folded into the parent (user),
1205 // then do nothing!
1206 if (subtreeRoot->isFoldedIntoParent())
1207 return;
1208
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001209 //
1210 // Let's check for chain rules outside the switch so that we don't have
1211 // to duplicate the list of chain rule production numbers here again
1212 //
1213 if (ThisIsAChainRule(ruleForNode))
Chris Lattner20b1ea02001-09-14 03:47:57 +00001214 {
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001215 // Chain rules have a single nonterminal on the RHS.
1216 // Get the rule that matches the RHS non-terminal and use that instead.
1217 //
1218 assert(nts[0] && ! nts[1]
1219 && "A chain rule should have only one RHS non-terminal!");
1220 nextRule = burm_rule(subtreeRoot->state, nts[0]);
1221 nts = burm_nts[nextRule];
Vikram S. Adve74825322002-03-18 03:15:35 +00001222 GetInstructionsByRule(subtreeRoot, nextRule, nts, target, mvec);
Chris Lattner20b1ea02001-09-14 03:47:57 +00001223 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001224 else
Chris Lattner20b1ea02001-09-14 03:47:57 +00001225 {
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001226 switch(ruleForNode) {
1227 case 1: // stmt: Ret
1228 case 2: // stmt: RetValue(reg)
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001229 { // NOTE: Prepass of register allocation is responsible
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001230 // for moving return value to appropriate register.
1231 // Mark the return-address register as a hidden virtual reg.
Vikram S. Advea995e602001-10-11 04:23:19 +00001232 // Mark the return value register as an implicit ref of
1233 // the machine instruction.
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001234 // Finally put a NOP in the delay slot.
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001235 ReturnInst *returnInstr =
1236 cast<ReturnInst>(subtreeRoot->getInstruction());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001237 assert(returnInstr->getOpcode() == Instruction::Ret);
1238
Chris Lattner9c461082002-02-03 07:50:56 +00001239 Instruction* returnReg = new TmpInstruction(returnInstr);
1240 MachineCodeForInstruction::get(returnInstr).addTemp(returnReg);
Vikram S. Advefb361122001-10-22 13:36:31 +00001241
Vikram S. Adve74825322002-03-18 03:15:35 +00001242 M = new MachineInstr(JMPLRET);
Chris Lattner1c7907e2002-10-28 20:11:17 +00001243 M->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister,
1244 returnReg);
Vikram S. Adve74825322002-03-18 03:15:35 +00001245 M->SetMachineOperandConst(1,MachineOperand::MO_SignExtendedImmed,
Chris Lattner1c7907e2002-10-28 20:11:17 +00001246 (int64_t)8);
Vikram S. Adve74825322002-03-18 03:15:35 +00001247 M->SetMachineOperandReg(2, target.getRegInfo().getZeroRegNum());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001248
Vikram S. Advea995e602001-10-11 04:23:19 +00001249 if (returnInstr->getReturnValue() != NULL)
Vikram S. Adve74825322002-03-18 03:15:35 +00001250 M->addImplicitRef(returnInstr->getReturnValue());
Vikram S. Advea995e602001-10-11 04:23:19 +00001251
Vikram S. Adve74825322002-03-18 03:15:35 +00001252 mvec.push_back(M);
1253 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001254
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001255 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001256 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001257
1258 case 3: // stmt: Store(reg,reg)
1259 case 4: // stmt: Store(reg,ptrreg)
Vikram S. Adve74825322002-03-18 03:15:35 +00001260 mvec.push_back(new MachineInstr(
1261 ChooseStoreInstruction(
1262 subtreeRoot->leftChild()->getValue()->getType())));
Vikram S. Adve8cfffd32002-08-24 20:56:53 +00001263 SetOperandsForMemInstr(mvec, subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001264 break;
Chris Lattner20b1ea02001-09-14 03:47:57 +00001265
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001266 case 5: // stmt: BrUncond
Vikram S. Adve74825322002-03-18 03:15:35 +00001267 M = new MachineInstr(BA);
Vikram S. Adve6418eac2002-07-08 23:30:14 +00001268 M->SetMachineOperandVal(0, MachineOperand::MO_PCRelativeDisp,
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001269 cast<BranchInst>(subtreeRoot->getInstruction())->getSuccessor(0));
Vikram S. Adve74825322002-03-18 03:15:35 +00001270 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001271
1272 // delay slot
Vikram S. Adve74825322002-03-18 03:15:35 +00001273 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001274 break;
Chris Lattner20b1ea02001-09-14 03:47:57 +00001275
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001276 case 206: // stmt: BrCond(setCCconst)
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001277 { // setCCconst => boolean was computed with `%b = setCC type reg1 const'
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001278 // If the constant is ZERO, we can use the branch-on-integer-register
1279 // instructions and avoid the SUBcc instruction entirely.
1280 // Otherwise this is just the same as case 5, so just fall through.
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001281 //
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001282 InstrTreeNode* constNode = subtreeRoot->leftChild()->rightChild();
1283 assert(constNode &&
1284 constNode->getNodeType() ==InstrTreeNode::NTConstNode);
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001285 Constant *constVal = cast<Constant>(constNode->getValue());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001286 bool isValidConst;
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001287
Chris Lattner0c4e8862002-09-03 01:08:28 +00001288 if ((constVal->getType()->isInteger()
Chris Lattner9b625032002-05-06 16:15:30 +00001289 || isa<PointerType>(constVal->getType()))
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001290 && GetConstantValueAsSignedInt(constVal, isValidConst) == 0
1291 && isValidConst)
1292 {
1293 // That constant is a zero after all...
1294 // Use the left child of setCC as the first argument!
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001295 // Mark the setCC node so that no code is generated for it.
1296 InstructionNode* setCCNode = (InstructionNode*)
1297 subtreeRoot->leftChild();
1298 assert(setCCNode->getOpLabel() == SetCCOp);
1299 setCCNode->markFoldedIntoParent();
1300
1301 BranchInst* brInst=cast<BranchInst>(subtreeRoot->getInstruction());
1302
Vikram S. Adve74825322002-03-18 03:15:35 +00001303 M = new MachineInstr(ChooseBprInstruction(subtreeRoot));
1304 M->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister,
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001305 setCCNode->leftChild()->getValue());
Vikram S. Adve74825322002-03-18 03:15:35 +00001306 M->SetMachineOperandVal(1, MachineOperand::MO_PCRelativeDisp,
1307 brInst->getSuccessor(0));
1308 mvec.push_back(M);
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001309
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001310 // delay slot
Vikram S. Adve74825322002-03-18 03:15:35 +00001311 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001312
1313 // false branch
Vikram S. Adve74825322002-03-18 03:15:35 +00001314 M = new MachineInstr(BA);
Vikram S. Adve6418eac2002-07-08 23:30:14 +00001315 M->SetMachineOperandVal(0, MachineOperand::MO_PCRelativeDisp,
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001316 brInst->getSuccessor(1));
Vikram S. Adve74825322002-03-18 03:15:35 +00001317 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001318
1319 // delay slot
Vikram S. Adve74825322002-03-18 03:15:35 +00001320 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001321
1322 break;
1323 }
1324 // ELSE FALL THROUGH
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001325 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001326
Vikram S. Adve30a6f492002-08-22 02:56:10 +00001327 case 6: // stmt: BrCond(setCC)
1328 { // bool => boolean was computed with SetCC.
1329 // The branch to use depends on whether it is FP, signed, or unsigned.
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001330 // If it is an integer CC, we also need to find the unique
1331 // TmpInstruction representing that CC.
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001332 //
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001333 BranchInst* brInst = cast<BranchInst>(subtreeRoot->getInstruction());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001334 bool isFPBranch;
Vikram S. Adve74825322002-03-18 03:15:35 +00001335 M = new MachineInstr(ChooseBccInstruction(subtreeRoot, isFPBranch));
Vikram S. Adve30a6f492002-08-22 02:56:10 +00001336
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001337 Value* ccValue = GetTmpForCC(subtreeRoot->leftChild()->getValue(),
1338 brInst->getParent()->getParent(),
1339 isFPBranch? Type::FloatTy : Type::IntTy);
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001340
Vikram S. Adve74825322002-03-18 03:15:35 +00001341 M->SetMachineOperandVal(0, MachineOperand::MO_CCRegister, ccValue);
1342 M->SetMachineOperandVal(1, MachineOperand::MO_PCRelativeDisp,
1343 brInst->getSuccessor(0));
1344 mvec.push_back(M);
Vikram S. Adve30a6f492002-08-22 02:56:10 +00001345
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001346 // delay slot
Vikram S. Adve74825322002-03-18 03:15:35 +00001347 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adve30a6f492002-08-22 02:56:10 +00001348
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001349 // false branch
Vikram S. Adve74825322002-03-18 03:15:35 +00001350 M = new MachineInstr(BA);
Vikram S. Adve6418eac2002-07-08 23:30:14 +00001351 M->SetMachineOperandVal(0, MachineOperand::MO_PCRelativeDisp,
Vikram S. Adve74825322002-03-18 03:15:35 +00001352 brInst->getSuccessor(1));
1353 mvec.push_back(M);
Vikram S. Adve30a6f492002-08-22 02:56:10 +00001354
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001355 // delay slot
Vikram S. Adve74825322002-03-18 03:15:35 +00001356 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001357 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001358 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001359
1360 case 208: // stmt: BrCond(boolconst)
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001361 {
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001362 // boolconst => boolean is a constant; use BA to first or second label
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001363 Constant* constVal =
1364 cast<Constant>(subtreeRoot->leftChild()->getValue());
1365 unsigned dest = cast<ConstantBool>(constVal)->getValue()? 0 : 1;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001366
Vikram S. Adve74825322002-03-18 03:15:35 +00001367 M = new MachineInstr(BA);
Vikram S. Adve6418eac2002-07-08 23:30:14 +00001368 M->SetMachineOperandVal(0, MachineOperand::MO_PCRelativeDisp,
Chris Lattner35504202002-04-27 03:14:39 +00001369 cast<BranchInst>(subtreeRoot->getInstruction())->getSuccessor(dest));
Vikram S. Adve74825322002-03-18 03:15:35 +00001370 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001371
1372 // delay slot
Vikram S. Adve74825322002-03-18 03:15:35 +00001373 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001374 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001375 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001376
1377 case 8: // stmt: BrCond(boolreg)
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001378 { // boolreg => boolean is stored in an existing register.
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001379 // Just use the branch-on-integer-register instruction!
1380 //
Vikram S. Adve74825322002-03-18 03:15:35 +00001381 M = new MachineInstr(BRNZ);
1382 M->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001383 subtreeRoot->leftChild()->getValue());
Vikram S. Adve74825322002-03-18 03:15:35 +00001384 M->SetMachineOperandVal(1, MachineOperand::MO_PCRelativeDisp,
Chris Lattner35504202002-04-27 03:14:39 +00001385 cast<BranchInst>(subtreeRoot->getInstruction())->getSuccessor(0));
Vikram S. Adve74825322002-03-18 03:15:35 +00001386 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001387
1388 // delay slot
Vikram S. Adve74825322002-03-18 03:15:35 +00001389 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001390
1391 // false branch
Vikram S. Adve74825322002-03-18 03:15:35 +00001392 M = new MachineInstr(BA);
Vikram S. Adve6418eac2002-07-08 23:30:14 +00001393 M->SetMachineOperandVal(0, MachineOperand::MO_PCRelativeDisp,
Chris Lattner35504202002-04-27 03:14:39 +00001394 cast<BranchInst>(subtreeRoot->getInstruction())->getSuccessor(1));
Vikram S. Adve74825322002-03-18 03:15:35 +00001395 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001396
1397 // delay slot
Vikram S. Adve74825322002-03-18 03:15:35 +00001398 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001399 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001400 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00001401
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001402 case 9: // stmt: Switch(reg)
1403 assert(0 && "*** SWITCH instruction is not implemented yet.");
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001404 break;
Chris Lattner20b1ea02001-09-14 03:47:57 +00001405
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001406 case 10: // reg: VRegList(reg, reg)
1407 assert(0 && "VRegList should never be the topmost non-chain rule");
1408 break;
1409
Vikram S. Advece08e1d2002-08-15 14:17:37 +00001410 case 21: // bool: Not(bool,reg): Both these are implemented as:
1411 case 421: // reg: BNot(reg,reg): reg = reg XOR-NOT 0
1412 { // First find the unary operand. It may be left or right, usually right.
1413 Value* notArg = BinaryOperator::getNotArgument(
1414 cast<BinaryOperator>(subtreeRoot->getInstruction()));
1415 mvec.push_back(Create3OperandInstr_Reg(XNOR, notArg,
1416 target.getRegInfo().getZeroRegNum(),
1417 subtreeRoot->getValue()));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001418 break;
Vikram S. Advece08e1d2002-08-15 14:17:37 +00001419 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001420
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001421 case 22: // reg: ToBoolTy(reg):
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001422 {
1423 const Type* opType = subtreeRoot->leftChild()->getValue()->getType();
Chris Lattner0c4e8862002-09-03 01:08:28 +00001424 assert(opType->isIntegral() || isa<PointerType>(opType));
Vikram S. Adve74825322002-03-18 03:15:35 +00001425 forwardOperandNum = 0; // forward first operand to user
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001426 break;
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001427 }
1428
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001429 case 23: // reg: ToUByteTy(reg)
Vikram S. Adve94c40812002-09-27 14:33:08 +00001430 case 24: // reg: ToSByteTy(reg)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001431 case 25: // reg: ToUShortTy(reg)
Vikram S. Adve94c40812002-09-27 14:33:08 +00001432 case 26: // reg: ToShortTy(reg)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001433 case 27: // reg: ToUIntTy(reg)
Vikram S. Adve94c40812002-09-27 14:33:08 +00001434 case 28: // reg: ToIntTy(reg)
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001435 {
Vikram S. Adve94c40812002-09-27 14:33:08 +00001436 //======================================================================
1437 // Rules for integer conversions:
1438 //
1439 //--------
1440 // From ISO 1998 C++ Standard, Sec. 4.7:
1441 //
1442 // 2. If the destination type is unsigned, the resulting value is
1443 // the least unsigned integer congruent to the source integer
1444 // (modulo 2n where n is the number of bits used to represent the
1445 // unsigned type). [Note: In a two s complement representation,
1446 // this conversion is conceptual and there is no change in the
1447 // bit pattern (if there is no truncation). ]
1448 //
1449 // 3. If the destination type is signed, the value is unchanged if
1450 // it can be represented in the destination type (and bitfield width);
1451 // otherwise, the value is implementation-defined.
1452 //--------
1453 //
1454 // Since we assume 2s complement representations, this implies:
1455 //
1456 // -- if operand is smaller than destination, zero-extend or sign-extend
1457 // according to the signedness of the *operand*: source decides.
1458 // ==> we have to do nothing here!
1459 //
1460 // -- if operand is same size as or larger than destination, and the
1461 // destination is *unsigned*, zero-extend the operand: dest. decides
1462 //
1463 // -- if operand is same size as or larger than destination, and the
1464 // destination is *signed*, the choice is implementation defined:
1465 // we sign-extend the operand: i.e., again dest. decides.
1466 // Note: this matches both Sun's cc and gcc3.2.
1467 //======================================================================
1468
Vikram S. Adve242a8082002-05-19 15:25:51 +00001469 Instruction* destI = subtreeRoot->getInstruction();
1470 Value* opVal = subtreeRoot->leftChild()->getValue();
Vikram S. Adve94c40812002-09-27 14:33:08 +00001471 const Type* opType = opVal->getType();
Chris Lattner0c4e8862002-09-03 01:08:28 +00001472 if (opType->isIntegral() || isa<PointerType>(opType))
Vikram S. Adve1e606692002-07-31 21:01:34 +00001473 {
1474 unsigned opSize = target.DataLayout.getTypeSize(opType);
1475 unsigned destSize = target.DataLayout.getTypeSize(destI->getType());
Vikram S. Adve94c40812002-09-27 14:33:08 +00001476 if (opSize >= destSize)
1477 { // Operand is same size as or larger than dest:
1478 // zero- or sign-extend, according to the signeddness of
1479 // the destination (see above).
1480 if (destI->getType()->isSigned())
1481 target.getInstrInfo().CreateSignExtensionInstructions(target,
1482 destI->getParent()->getParent(), opVal, destI, 8*destSize,
1483 mvec, MachineCodeForInstruction::get(destI));
1484 else
1485 target.getInstrInfo().CreateZeroExtensionInstructions(target,
1486 destI->getParent()->getParent(), opVal, destI, 8*destSize,
1487 mvec, MachineCodeForInstruction::get(destI));
Vikram S. Adve1e606692002-07-31 21:01:34 +00001488 }
1489 else
1490 forwardOperandNum = 0; // forward first operand to user
Vikram S. Adve242a8082002-05-19 15:25:51 +00001491 }
Vikram S. Adve1e606692002-07-31 21:01:34 +00001492 else if (opType->isFloatingPoint())
Vikram S. Adve8cfffd32002-08-24 20:56:53 +00001493 {
1494 CreateCodeToConvertFloatToInt(target, opVal, destI, mvec,
1495 MachineCodeForInstruction::get(destI));
Vikram S. Adve94c40812002-09-27 14:33:08 +00001496 if (destI->getType()->isUnsigned())
1497 maskUnsignedResult = true; // not handled by fp->int code
Vikram S. Adve8cfffd32002-08-24 20:56:53 +00001498 }
Vikram S. Adve242a8082002-05-19 15:25:51 +00001499 else
Vikram S. Adve1e606692002-07-31 21:01:34 +00001500 assert(0 && "Unrecognized operand type for convert-to-unsigned");
1501
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001502 break;
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001503 }
Vikram S. Adve94c40812002-09-27 14:33:08 +00001504
1505 case 29: // reg: ToULongTy(reg)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001506 case 30: // reg: ToLongTy(reg)
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001507 {
Vikram S. Adve242a8082002-05-19 15:25:51 +00001508 Value* opVal = subtreeRoot->leftChild()->getValue();
Vikram S. Adve242a8082002-05-19 15:25:51 +00001509 const Type* opType = opVal->getType();
Chris Lattner0c4e8862002-09-03 01:08:28 +00001510 if (opType->isIntegral() || isa<PointerType>(opType))
Vikram S. Adve94c40812002-09-27 14:33:08 +00001511 forwardOperandNum = 0; // forward first operand to user
Vikram S. Adve1e606692002-07-31 21:01:34 +00001512 else if (opType->isFloatingPoint())
Vikram S. Adve94c40812002-09-27 14:33:08 +00001513 {
1514 Instruction* destI = subtreeRoot->getInstruction();
1515 CreateCodeToConvertFloatToInt(target, opVal, destI, mvec,
1516 MachineCodeForInstruction::get(destI));
1517 }
Vikram S. Adve1e606692002-07-31 21:01:34 +00001518 else
1519 assert(0 && "Unrecognized operand type for convert-to-signed");
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001520 break;
Vikram S. Adve94c40812002-09-27 14:33:08 +00001521 }
1522
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001523 case 31: // reg: ToFloatTy(reg):
1524 case 32: // reg: ToDoubleTy(reg):
1525 case 232: // reg: ToDoubleTy(Constant):
Vikram S. Adveec7f4822002-09-09 14:54:21 +00001526
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001527 // If this instruction has a parent (a user) in the tree
1528 // and the user is translated as an FsMULd instruction,
1529 // then the cast is unnecessary. So check that first.
1530 // In the future, we'll want to do the same for the FdMULq instruction,
1531 // so do the check here instead of only for ToFloatTy(reg).
1532 //
Vikram S. Adveec7f4822002-09-09 14:54:21 +00001533 if (subtreeRoot->parent() != NULL)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001534 {
Vikram S. Adveec7f4822002-09-09 14:54:21 +00001535 const MachineCodeForInstruction& mcfi =
1536 MachineCodeForInstruction::get(
1537 cast<InstructionNode>(subtreeRoot->parent())->getInstruction());
1538 if (mcfi.size() == 0 || mcfi.front()->getOpCode() == FSMULD)
1539 forwardOperandNum = 0; // forward first operand to user
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001540 }
Vikram S. Adveec7f4822002-09-09 14:54:21 +00001541
1542 if (forwardOperandNum != 0) // we do need the cast
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001543 {
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001544 Value* leftVal = subtreeRoot->leftChild()->getValue();
1545 const Type* opType = leftVal->getType();
Vikram S. Advedbc4fad2002-04-25 04:37:51 +00001546 MachineOpCode opCode=ChooseConvertToFloatInstr(
1547 subtreeRoot->getOpLabel(), opType);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001548 if (opCode == INVALID_OPCODE) // no conversion needed
1549 {
Vikram S. Adve74825322002-03-18 03:15:35 +00001550 forwardOperandNum = 0; // forward first operand to user
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001551 }
1552 else
1553 {
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001554 // If the source operand is a non-FP type it must be
1555 // first copied from int to float register via memory!
1556 Instruction *dest = subtreeRoot->getInstruction();
1557 Value* srcForCast;
1558 int n = 0;
Vikram S. Adve242a8082002-05-19 15:25:51 +00001559 if (! opType->isFloatingPoint())
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001560 {
1561 // Create a temporary to represent the FP register
1562 // into which the integer will be copied via memory.
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001563 // The type of this temporary will determine the FP
1564 // register used: single-prec for a 32-bit int or smaller,
1565 // double-prec for a 64-bit int.
1566 //
Vikram S. Advebabc0fa2002-09-05 18:32:13 +00001567 uint64_t srcSize =
1568 target.DataLayout.getTypeSize(leftVal->getType());
1569 Type* tmpTypeToUse =
1570 (srcSize <= 4)? Type::FloatTy : Type::DoubleTy;
1571 srcForCast = new TmpInstruction(tmpTypeToUse, dest);
Vikram S. Advedbc4fad2002-04-25 04:37:51 +00001572 MachineCodeForInstruction &destMCFI =
Chris Lattner9c461082002-02-03 07:50:56 +00001573 MachineCodeForInstruction::get(dest);
Vikram S. Advedbc4fad2002-04-25 04:37:51 +00001574 destMCFI.addTemp(srcForCast);
Vikram S. Advebabc0fa2002-09-05 18:32:13 +00001575
Vikram S. Adve242a8082002-05-19 15:25:51 +00001576 target.getInstrInfo().CreateCodeToCopyIntToFloat(target,
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001577 dest->getParent()->getParent(),
Vikram S. Advebabc0fa2002-09-05 18:32:13 +00001578 leftVal, cast<Instruction>(srcForCast),
Vikram S. Adve242a8082002-05-19 15:25:51 +00001579 mvec, destMCFI);
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001580 }
1581 else
1582 srcForCast = leftVal;
Vikram S. Adve94c40812002-09-27 14:33:08 +00001583
Vikram S. Adve74825322002-03-18 03:15:35 +00001584 M = new MachineInstr(opCode);
1585 M->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister,
1586 srcForCast);
1587 M->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister,
1588 dest);
1589 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001590 }
1591 }
1592 break;
1593
1594 case 19: // reg: ToArrayTy(reg):
1595 case 20: // reg: ToPointerTy(reg):
Vikram S. Adve74825322002-03-18 03:15:35 +00001596 forwardOperandNum = 0; // forward first operand to user
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001597 break;
1598
1599 case 233: // reg: Add(reg, Constant)
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001600 maskUnsignedResult = true;
Vikram S. Adve74825322002-03-18 03:15:35 +00001601 M = CreateAddConstInstruction(subtreeRoot);
1602 if (M != NULL)
1603 {
1604 mvec.push_back(M);
1605 break;
1606 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001607 // ELSE FALL THROUGH
Vikram S. Adve74825322002-03-18 03:15:35 +00001608
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001609 case 33: // reg: Add(reg, reg)
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001610 maskUnsignedResult = true;
Vikram S. Adve74825322002-03-18 03:15:35 +00001611 mvec.push_back(new MachineInstr(ChooseAddInstruction(subtreeRoot)));
1612 Set3OperandsFromInstr(mvec.back(), subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001613 break;
1614
1615 case 234: // reg: Sub(reg, Constant)
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001616 maskUnsignedResult = true;
Vikram S. Adve74825322002-03-18 03:15:35 +00001617 M = CreateSubConstInstruction(subtreeRoot);
1618 if (M != NULL)
1619 {
1620 mvec.push_back(M);
1621 break;
1622 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001623 // ELSE FALL THROUGH
Vikram S. Adve74825322002-03-18 03:15:35 +00001624
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001625 case 34: // reg: Sub(reg, reg)
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001626 maskUnsignedResult = true;
Vikram S. Adve74825322002-03-18 03:15:35 +00001627 mvec.push_back(new MachineInstr(ChooseSubInstructionByType(
1628 subtreeRoot->getInstruction()->getType())));
1629 Set3OperandsFromInstr(mvec.back(), subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001630 break;
1631
1632 case 135: // reg: Mul(todouble, todouble)
1633 checkCast = true;
1634 // FALL THROUGH
1635
1636 case 35: // reg: Mul(reg, reg)
Vikram S. Adve74825322002-03-18 03:15:35 +00001637 {
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001638 maskUnsignedResult = true;
Vikram S. Adve74825322002-03-18 03:15:35 +00001639 MachineOpCode forceOp = ((checkCast && BothFloatToDouble(subtreeRoot))
1640 ? FSMULD
1641 : INVALID_MACHINE_OPCODE);
Vikram S. Adve242a8082002-05-19 15:25:51 +00001642 Instruction* mulInstr = subtreeRoot->getInstruction();
1643 CreateMulInstruction(target, mulInstr->getParent()->getParent(),
Vikram S. Adve74825322002-03-18 03:15:35 +00001644 subtreeRoot->leftChild()->getValue(),
1645 subtreeRoot->rightChild()->getValue(),
Vikram S. Adve242a8082002-05-19 15:25:51 +00001646 mulInstr, mvec,
1647 MachineCodeForInstruction::get(mulInstr),forceOp);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001648 break;
Vikram S. Adve74825322002-03-18 03:15:35 +00001649 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001650 case 335: // reg: Mul(todouble, todoubleConst)
1651 checkCast = true;
1652 // FALL THROUGH
1653
1654 case 235: // reg: Mul(reg, Constant)
Vikram S. Adve74825322002-03-18 03:15:35 +00001655 {
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001656 maskUnsignedResult = true;
Vikram S. Adve74825322002-03-18 03:15:35 +00001657 MachineOpCode forceOp = ((checkCast && BothFloatToDouble(subtreeRoot))
1658 ? FSMULD
1659 : INVALID_MACHINE_OPCODE);
Vikram S. Adve242a8082002-05-19 15:25:51 +00001660 Instruction* mulInstr = subtreeRoot->getInstruction();
1661 CreateMulInstruction(target, mulInstr->getParent()->getParent(),
Vikram S. Adve74825322002-03-18 03:15:35 +00001662 subtreeRoot->leftChild()->getValue(),
1663 subtreeRoot->rightChild()->getValue(),
Vikram S. Adve242a8082002-05-19 15:25:51 +00001664 mulInstr, mvec,
1665 MachineCodeForInstruction::get(mulInstr),
1666 forceOp);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001667 break;
Vikram S. Adve74825322002-03-18 03:15:35 +00001668 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001669 case 236: // reg: Div(reg, Constant)
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001670 maskUnsignedResult = true;
Vikram S. Adve74825322002-03-18 03:15:35 +00001671 L = mvec.size();
1672 CreateDivConstInstruction(target, subtreeRoot, mvec);
1673 if (mvec.size() > L)
1674 break;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001675 // ELSE FALL THROUGH
Vikram S. Adve74825322002-03-18 03:15:35 +00001676
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001677 case 36: // reg: Div(reg, reg)
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001678 maskUnsignedResult = true;
Vikram S. Adve74825322002-03-18 03:15:35 +00001679 mvec.push_back(new MachineInstr(ChooseDivInstruction(target, subtreeRoot)));
1680 Set3OperandsFromInstr(mvec.back(), subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001681 break;
1682
1683 case 37: // reg: Rem(reg, reg)
1684 case 237: // reg: Rem(reg, Constant)
Vikram S. Adve510eec72001-11-04 21:59:14 +00001685 {
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001686 maskUnsignedResult = true;
Vikram S. Adve510eec72001-11-04 21:59:14 +00001687 Instruction* remInstr = subtreeRoot->getInstruction();
1688
Chris Lattner9c461082002-02-03 07:50:56 +00001689 TmpInstruction* quot = new TmpInstruction(
Vikram S. Adve510eec72001-11-04 21:59:14 +00001690 subtreeRoot->leftChild()->getValue(),
1691 subtreeRoot->rightChild()->getValue());
Chris Lattner9c461082002-02-03 07:50:56 +00001692 TmpInstruction* prod = new TmpInstruction(
Vikram S. Adve510eec72001-11-04 21:59:14 +00001693 quot,
1694 subtreeRoot->rightChild()->getValue());
Chris Lattner9c461082002-02-03 07:50:56 +00001695 MachineCodeForInstruction::get(remInstr).addTemp(quot).addTemp(prod);
Vikram S. Adve510eec72001-11-04 21:59:14 +00001696
Vikram S. Adve74825322002-03-18 03:15:35 +00001697 M = new MachineInstr(ChooseDivInstruction(target, subtreeRoot));
1698 Set3OperandsFromInstr(M, subtreeRoot, target);
1699 M->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister,quot);
1700 mvec.push_back(M);
Vikram S. Adve510eec72001-11-04 21:59:14 +00001701
Vikram S. Advece08e1d2002-08-15 14:17:37 +00001702 M = Create3OperandInstr(ChooseMulInstructionByType(
1703 subtreeRoot->getInstruction()->getType()),
1704 quot, subtreeRoot->rightChild()->getValue(),
1705 prod);
Vikram S. Adve74825322002-03-18 03:15:35 +00001706 mvec.push_back(M);
Vikram S. Adve510eec72001-11-04 21:59:14 +00001707
Vikram S. Adve74825322002-03-18 03:15:35 +00001708 M = new MachineInstr(ChooseSubInstructionByType(
Vikram S. Adve510eec72001-11-04 21:59:14 +00001709 subtreeRoot->getInstruction()->getType()));
Vikram S. Adve74825322002-03-18 03:15:35 +00001710 Set3OperandsFromInstr(M, subtreeRoot, target);
1711 M->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister,prod);
1712 mvec.push_back(M);
Vikram S. Adve510eec72001-11-04 21:59:14 +00001713
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001714 break;
Vikram S. Adve510eec72001-11-04 21:59:14 +00001715 }
1716
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001717 case 38: // bool: And(bool, bool)
1718 case 238: // bool: And(bool, boolconst)
1719 case 338: // reg : BAnd(reg, reg)
1720 case 538: // reg : BAnd(reg, Constant)
Vikram S. Adve74825322002-03-18 03:15:35 +00001721 mvec.push_back(new MachineInstr(AND));
1722 Set3OperandsFromInstr(mvec.back(), subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001723 break;
1724
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001725 case 138: // bool: And(bool, not)
Vikram S. Advece08e1d2002-08-15 14:17:37 +00001726 case 438: // bool: BAnd(bool, bnot)
1727 { // Use the argument of NOT as the second argument!
1728 // Mark the NOT node so that no code is generated for it.
1729 InstructionNode* notNode = (InstructionNode*) subtreeRoot->rightChild();
1730 Value* notArg = BinaryOperator::getNotArgument(
1731 cast<BinaryOperator>(notNode->getInstruction()));
1732 notNode->markFoldedIntoParent();
1733 mvec.push_back(Create3OperandInstr(ANDN,
1734 subtreeRoot->leftChild()->getValue(),
1735 notArg, subtreeRoot->getValue()));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001736 break;
Vikram S. Advece08e1d2002-08-15 14:17:37 +00001737 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001738
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001739 case 39: // bool: Or(bool, bool)
1740 case 239: // bool: Or(bool, boolconst)
1741 case 339: // reg : BOr(reg, reg)
1742 case 539: // reg : BOr(reg, Constant)
Vikram S. Adve242a8082002-05-19 15:25:51 +00001743 mvec.push_back(new MachineInstr(OR));
Vikram S. Adve74825322002-03-18 03:15:35 +00001744 Set3OperandsFromInstr(mvec.back(), subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001745 break;
1746
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001747 case 139: // bool: Or(bool, not)
Vikram S. Advece08e1d2002-08-15 14:17:37 +00001748 case 439: // bool: BOr(bool, bnot)
1749 { // Use the argument of NOT as the second argument!
1750 // Mark the NOT node so that no code is generated for it.
1751 InstructionNode* notNode = (InstructionNode*) subtreeRoot->rightChild();
1752 Value* notArg = BinaryOperator::getNotArgument(
1753 cast<BinaryOperator>(notNode->getInstruction()));
1754 notNode->markFoldedIntoParent();
1755 mvec.push_back(Create3OperandInstr(ORN,
1756 subtreeRoot->leftChild()->getValue(),
1757 notArg, subtreeRoot->getValue()));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001758 break;
Vikram S. Advece08e1d2002-08-15 14:17:37 +00001759 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001760
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001761 case 40: // bool: Xor(bool, bool)
1762 case 240: // bool: Xor(bool, boolconst)
1763 case 340: // reg : BXor(reg, reg)
1764 case 540: // reg : BXor(reg, Constant)
Vikram S. Adve74825322002-03-18 03:15:35 +00001765 mvec.push_back(new MachineInstr(XOR));
1766 Set3OperandsFromInstr(mvec.back(), subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001767 break;
1768
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001769 case 140: // bool: Xor(bool, not)
Vikram S. Advece08e1d2002-08-15 14:17:37 +00001770 case 440: // bool: BXor(bool, bnot)
1771 { // Use the argument of NOT as the second argument!
1772 // Mark the NOT node so that no code is generated for it.
1773 InstructionNode* notNode = (InstructionNode*) subtreeRoot->rightChild();
1774 Value* notArg = BinaryOperator::getNotArgument(
1775 cast<BinaryOperator>(notNode->getInstruction()));
1776 notNode->markFoldedIntoParent();
1777 mvec.push_back(Create3OperandInstr(XNOR,
1778 subtreeRoot->leftChild()->getValue(),
1779 notArg, subtreeRoot->getValue()));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001780 break;
Vikram S. Advece08e1d2002-08-15 14:17:37 +00001781 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001782
1783 case 41: // boolconst: SetCC(reg, Constant)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001784 //
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001785 // If the SetCC was folded into the user (parent), it will be
1786 // caught above. All other cases are the same as case 42,
1787 // so just fall through.
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001788 //
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001789 case 42: // bool: SetCC(reg, reg):
1790 {
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001791 // This generates a SUBCC instruction, putting the difference in
1792 // a result register, and setting a condition code.
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001793 //
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001794 // If the boolean result of the SetCC is used by anything other
Vikram S. Adve6418eac2002-07-08 23:30:14 +00001795 // than a branch instruction, or if it is used outside the current
1796 // basic block, the boolean must be
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001797 // computed and stored in the result register. Otherwise, discard
1798 // the difference (by using %g0) and keep only the condition code.
1799 //
1800 // To compute the boolean result in a register we use a conditional
1801 // move, unless the result of the SUBCC instruction can be used as
1802 // the bool! This assumes that zero is FALSE and any non-zero
1803 // integer is TRUE.
1804 //
1805 InstructionNode* parentNode = (InstructionNode*) subtreeRoot->parent();
1806 Instruction* setCCInstr = subtreeRoot->getInstruction();
Vikram S. Adve242a8082002-05-19 15:25:51 +00001807
Vikram S. Adve6418eac2002-07-08 23:30:14 +00001808 bool keepBoolVal = parentNode == NULL ||
1809 ! AllUsesAreBranches(setCCInstr);
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001810 bool subValIsBoolVal = setCCInstr->getOpcode() == Instruction::SetNE;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001811 bool keepSubVal = keepBoolVal && subValIsBoolVal;
1812 bool computeBoolVal = keepBoolVal && ! subValIsBoolVal;
1813
1814 bool mustClearReg;
1815 int valueToMove;
Chris Lattner8e5c0b42001-11-07 14:01:59 +00001816 MachineOpCode movOpCode = 0;
Vikram S. Adve6418eac2002-07-08 23:30:14 +00001817
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001818 // Mark the 4th operand as being a CC register, and as a def
1819 // A TmpInstruction is created to represent the CC "result".
1820 // Unlike other instances of TmpInstruction, this one is used
1821 // by machine code of multiple LLVM instructions, viz.,
1822 // the SetCC and the branch. Make sure to get the same one!
1823 // Note that we do this even for FP CC registers even though they
1824 // are explicit operands, because the type of the operand
1825 // needs to be a floating point condition code, not an integer
1826 // condition code. Think of this as casting the bool result to
1827 // a FP condition code register.
1828 //
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001829 Value* leftVal = subtreeRoot->leftChild()->getValue();
Chris Lattner9b625032002-05-06 16:15:30 +00001830 bool isFPCompare = leftVal->getType()->isFloatingPoint();
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001831
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001832 TmpInstruction* tmpForCC = GetTmpForCC(setCCInstr,
1833 setCCInstr->getParent()->getParent(),
Chris Lattner9b625032002-05-06 16:15:30 +00001834 isFPCompare ? Type::FloatTy : Type::IntTy);
Chris Lattner9c461082002-02-03 07:50:56 +00001835 MachineCodeForInstruction::get(setCCInstr).addTemp(tmpForCC);
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001836
1837 if (! isFPCompare)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001838 {
1839 // Integer condition: dest. should be %g0 or an integer register.
1840 // If result must be saved but condition is not SetEQ then we need
1841 // a separate instruction to compute the bool result, so discard
1842 // result of SUBcc instruction anyway.
1843 //
Vikram S. Adve74825322002-03-18 03:15:35 +00001844 M = new MachineInstr(SUBcc);
1845 Set3OperandsFromInstr(M, subtreeRoot, target, ! keepSubVal);
1846 M->SetMachineOperandVal(3, MachineOperand::MO_CCRegister,
1847 tmpForCC, /*def*/true);
1848 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001849
1850 if (computeBoolVal)
1851 { // recompute bool using the integer condition codes
1852 movOpCode =
1853 ChooseMovpccAfterSub(subtreeRoot,mustClearReg,valueToMove);
1854 }
1855 }
1856 else
1857 {
1858 // FP condition: dest of FCMP should be some FCCn register
Vikram S. Adve74825322002-03-18 03:15:35 +00001859 M = new MachineInstr(ChooseFcmpInstruction(subtreeRoot));
1860 M->SetMachineOperandVal(0, MachineOperand::MO_CCRegister,
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001861 tmpForCC);
Vikram S. Adve74825322002-03-18 03:15:35 +00001862 M->SetMachineOperandVal(1,MachineOperand::MO_VirtualRegister,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001863 subtreeRoot->leftChild()->getValue());
Vikram S. Adve74825322002-03-18 03:15:35 +00001864 M->SetMachineOperandVal(2,MachineOperand::MO_VirtualRegister,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001865 subtreeRoot->rightChild()->getValue());
Vikram S. Adve74825322002-03-18 03:15:35 +00001866 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001867
1868 if (computeBoolVal)
1869 {// recompute bool using the FP condition codes
1870 mustClearReg = true;
1871 valueToMove = 1;
1872 movOpCode = ChooseMovFpccInstruction(subtreeRoot);
1873 }
1874 }
1875
1876 if (computeBoolVal)
1877 {
1878 if (mustClearReg)
1879 {// Unconditionally set register to 0
Vikram S. Adve74825322002-03-18 03:15:35 +00001880 M = new MachineInstr(SETHI);
1881 M->SetMachineOperandConst(0,MachineOperand::MO_UnextendedImmed,
1882 (int64_t)0);
1883 M->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister,
1884 setCCInstr);
1885 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001886 }
1887
1888 // Now conditionally move `valueToMove' (0 or 1) into the register
Vikram S. Adve6418eac2002-07-08 23:30:14 +00001889 // Mark the register as a use (as well as a def) because the old
1890 // value should be retained if the condition is false.
Vikram S. Adve74825322002-03-18 03:15:35 +00001891 M = new MachineInstr(movOpCode);
1892 M->SetMachineOperandVal(0, MachineOperand::MO_CCRegister,
1893 tmpForCC);
1894 M->SetMachineOperandConst(1, MachineOperand::MO_UnextendedImmed,
1895 valueToMove);
1896 M->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister,
Vikram S. Adve6418eac2002-07-08 23:30:14 +00001897 setCCInstr, /*isDef*/ true,
1898 /*isDefAndUse*/ true);
Vikram S. Adve74825322002-03-18 03:15:35 +00001899 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001900 }
1901 break;
1902 }
1903
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001904 case 51: // reg: Load(reg)
1905 case 52: // reg: Load(ptrreg)
Vikram S. Adve74825322002-03-18 03:15:35 +00001906 mvec.push_back(new MachineInstr(ChooseLoadInstruction(
1907 subtreeRoot->getValue()->getType())));
Vikram S. Adve8cfffd32002-08-24 20:56:53 +00001908 SetOperandsForMemInstr(mvec, subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001909 break;
1910
1911 case 55: // reg: GetElemPtr(reg)
1912 case 56: // reg: GetElemPtrIdx(reg,reg)
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001913 // If the GetElemPtr was folded into the user (parent), it will be
1914 // caught above. For other cases, we have to compute the address.
Vikram S. Adve74825322002-03-18 03:15:35 +00001915 mvec.push_back(new MachineInstr(ADD));
Vikram S. Adve8cfffd32002-08-24 20:56:53 +00001916 SetOperandsForMemInstr(mvec, subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001917 break;
Vikram S. Adved3e26482002-10-13 00:18:57 +00001918
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001919 case 57: // reg: Alloca: Implement as 1 instruction:
1920 { // add %fp, offsetFromFP -> result
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001921 AllocationInst* instr =
1922 cast<AllocationInst>(subtreeRoot->getInstruction());
1923 unsigned int tsize =
Vikram S. Adved3e26482002-10-13 00:18:57 +00001924 target.DataLayout.getTypeSize(instr->getAllocatedType());
Vikram S. Adve74825322002-03-18 03:15:35 +00001925 assert(tsize != 0);
1926 CreateCodeForFixedSizeAlloca(target, instr, tsize, 1, mvec);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001927 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001928 }
Vikram S. Adved3e26482002-10-13 00:18:57 +00001929
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001930 case 58: // reg: Alloca(reg): Implement as 3 instructions:
1931 // mul num, typeSz -> tmp
1932 // sub %sp, tmp -> %sp
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001933 { // add %sp, frameSizeBelowDynamicArea -> result
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001934 AllocationInst* instr =
1935 cast<AllocationInst>(subtreeRoot->getInstruction());
Vikram S. Adve74825322002-03-18 03:15:35 +00001936 const Type* eltType = instr->getAllocatedType();
1937
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001938 // If #elements is constant, use simpler code for fixed-size allocas
Vikram S. Adved3e26482002-10-13 00:18:57 +00001939 int tsize = (int) target.DataLayout.getTypeSize(eltType);
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001940 Value* numElementsVal = NULL;
1941 bool isArray = instr->isArrayAllocation();
1942
1943 if (!isArray ||
1944 isa<Constant>(numElementsVal = instr->getArraySize()))
1945 { // total size is constant: generate code for fixed-size alloca
1946 unsigned int numElements = isArray?
1947 cast<ConstantUInt>(numElementsVal)->getValue() : 1;
1948 CreateCodeForFixedSizeAlloca(target, instr, tsize,
1949 numElements, mvec);
1950 }
Vikram S. Adve74825322002-03-18 03:15:35 +00001951 else // total size is not constant.
1952 CreateCodeForVariableSizeAlloca(target, instr, tsize,
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001953 numElementsVal, mvec);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001954 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001955 }
Vikram S. Adved3e26482002-10-13 00:18:57 +00001956
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001957 case 61: // reg: Call
Vikram S. Adve4a8bb2b2002-09-28 16:55:41 +00001958 { // Generate a direct (CALL) or indirect (JMPL) call.
1959 // Mark the return-address register, the indirection
1960 // register (for indirect calls), the operands of the Call,
1961 // and the return value (if any) as implicit operands
1962 // of the machine instruction.
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001963 //
Vikram S. Advedbc4fad2002-04-25 04:37:51 +00001964 // If this is a varargs function, floating point arguments
1965 // have to passed in integer registers so insert
1966 // copy-float-to-int instructions for each float operand.
1967 //
Chris Lattnerb00c5822001-10-02 03:41:24 +00001968 CallInst *callInstr = cast<CallInst>(subtreeRoot->getInstruction());
Chris Lattner749655f2001-10-13 06:54:30 +00001969 Value *callee = callInstr->getCalledValue();
Vikram S. Adve4a8bb2b2002-09-28 16:55:41 +00001970
1971 // Create hidden virtual register for return address with type void*
Vikram S. Adve242a8082002-05-19 15:25:51 +00001972 TmpInstruction* retAddrReg =
Vikram S. Advea10d1a72002-03-31 19:07:35 +00001973 new TmpInstruction(PointerType::get(Type::VoidTy), callInstr);
Chris Lattner9c461082002-02-03 07:50:56 +00001974 MachineCodeForInstruction::get(callInstr).addTemp(retAddrReg);
Vikram S. Adve4a8bb2b2002-09-28 16:55:41 +00001975
Vikram S. Adveea21a6c2001-10-20 20:57:06 +00001976 // Generate the machine instruction and its operands.
1977 // Use CALL for direct function calls; this optimistically assumes
1978 // the PC-relative address fits in the CALL address field (22 bits).
1979 // Use JMPL for indirect calls.
1980 //
Vikram S. Adve4a8bb2b2002-09-28 16:55:41 +00001981 if (isa<Function>(callee)) // direct function call
1982 M = Create1OperandInstr_Addr(CALL, callee);
1983 else // indirect function call
1984 M = Create3OperandInstr_SImmed(JMPLCALL, callee,
1985 (int64_t) 0, retAddrReg);
Vikram S. Adve74825322002-03-18 03:15:35 +00001986 mvec.push_back(M);
Vikram S. Advea10d1a72002-03-31 19:07:35 +00001987
Vikram S. Adve242a8082002-05-19 15:25:51 +00001988 const FunctionType* funcType =
1989 cast<FunctionType>(cast<PointerType>(callee->getType())
1990 ->getElementType());
1991 bool isVarArgs = funcType->isVarArg();
1992 bool noPrototype = isVarArgs && funcType->getNumParams() == 0;
Vikram S. Advedbc4fad2002-04-25 04:37:51 +00001993
Vikram S. Adve242a8082002-05-19 15:25:51 +00001994 // Use an annotation to pass information about call arguments
1995 // to the register allocator.
1996 CallArgsDescriptor* argDesc = new CallArgsDescriptor(callInstr,
1997 retAddrReg, isVarArgs, noPrototype);
1998 M->addAnnotation(argDesc);
Vikram S. Advea995e602001-10-11 04:23:19 +00001999
Vikram S. Adve242a8082002-05-19 15:25:51 +00002000 assert(callInstr->getOperand(0) == callee
2001 && "This is assumed in the loop below!");
2002
2003 for (unsigned i=1, N=callInstr->getNumOperands(); i < N; ++i)
2004 {
2005 Value* argVal = callInstr->getOperand(i);
2006 Instruction* intArgReg = NULL;
2007
2008 // Check for FP arguments to varargs functions.
2009 // Any such argument in the first $K$ args must be passed in an
2010 // integer register, where K = #integer argument registers.
2011 if (isVarArgs && argVal->getType()->isFloatingPoint())
2012 {
2013 // If it is a function with no prototype, pass value
2014 // as an FP value as well as a varargs value
2015 if (noPrototype)
2016 argDesc->getArgInfo(i-1).setUseFPArgReg();
2017
2018 // If this arg. is in the first $K$ regs, add a copy
2019 // float-to-int instruction to pass the value as an integer.
Vikram S. Adved3e26482002-10-13 00:18:57 +00002020 if (i <= target.getRegInfo().GetNumOfIntArgRegs())
Vikram S. Adve242a8082002-05-19 15:25:51 +00002021 {
2022 MachineCodeForInstruction &destMCFI =
2023 MachineCodeForInstruction::get(callInstr);
2024 intArgReg = new TmpInstruction(Type::IntTy, argVal);
2025 destMCFI.addTemp(intArgReg);
2026
2027 vector<MachineInstr*> copyMvec;
2028 target.getInstrInfo().CreateCodeToCopyFloatToInt(target,
2029 callInstr->getParent()->getParent(),
2030 argVal, (TmpInstruction*) intArgReg,
2031 copyMvec, destMCFI);
2032 mvec.insert(mvec.begin(),copyMvec.begin(),copyMvec.end());
2033
2034 argDesc->getArgInfo(i-1).setUseIntArgReg();
2035 argDesc->getArgInfo(i-1).setArgCopy(intArgReg);
2036 }
2037 else
2038 // Cannot fit in first $K$ regs so pass the arg on the stack
2039 argDesc->getArgInfo(i-1).setUseStackSlot();
2040 }
2041
2042 if (intArgReg)
2043 mvec.back()->addImplicitRef(intArgReg);
2044
2045 mvec.back()->addImplicitRef(argVal);
2046 }
2047
2048 // Add the return value as an implicit ref. The call operands
2049 // were added above.
Vikram S. Adveea21a6c2001-10-20 20:57:06 +00002050 if (callInstr->getType() != Type::VoidTy)
Vikram S. Adve74825322002-03-18 03:15:35 +00002051 mvec.back()->addImplicitRef(callInstr, /*isDef*/ true);
Vikram S. Advea995e602001-10-11 04:23:19 +00002052
Vikram S. Adveea21a6c2001-10-20 20:57:06 +00002053 // For the CALL instruction, the ret. addr. reg. is also implicit
Chris Lattnerb0d04722002-03-26 17:58:12 +00002054 if (isa<Function>(callee))
Vikram S. Adve74825322002-03-18 03:15:35 +00002055 mvec.back()->addImplicitRef(retAddrReg, /*isDef*/ true);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002056
Vikram S. Adve74825322002-03-18 03:15:35 +00002057 // delay slot
2058 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002059 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00002060 }
Vikram S. Adve242a8082002-05-19 15:25:51 +00002061
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002062 case 62: // reg: Shl(reg, reg)
Vikram S. Adve242a8082002-05-19 15:25:51 +00002063 {
2064 Value* argVal1 = subtreeRoot->leftChild()->getValue();
2065 Value* argVal2 = subtreeRoot->rightChild()->getValue();
2066 Instruction* shlInstr = subtreeRoot->getInstruction();
2067
2068 const Type* opType = argVal1->getType();
Chris Lattner0c4e8862002-09-03 01:08:28 +00002069 assert((opType->isInteger() || isa<PointerType>(opType)) &&
2070 "Shl unsupported for other types");
Vikram S. Adve242a8082002-05-19 15:25:51 +00002071
2072 CreateShiftInstructions(target, shlInstr->getParent()->getParent(),
2073 (opType == Type::LongTy)? SLLX : SLL,
2074 argVal1, argVal2, 0, shlInstr, mvec,
2075 MachineCodeForInstruction::get(shlInstr));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002076 break;
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00002077 }
2078
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002079 case 63: // reg: Shr(reg, reg)
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00002080 { const Type* opType = subtreeRoot->leftChild()->getValue()->getType();
Chris Lattner0c4e8862002-09-03 01:08:28 +00002081 assert((opType->isInteger() || isa<PointerType>(opType)) &&
2082 "Shr unsupported for other types");
Vikram S. Adve74825322002-03-18 03:15:35 +00002083 mvec.push_back(new MachineInstr((opType->isSigned()
2084 ? ((opType == Type::LongTy)? SRAX : SRA)
2085 : ((opType == Type::LongTy)? SRLX : SRL))));
2086 Set3OperandsFromInstr(mvec.back(), subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002087 break;
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00002088 }
2089
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002090 case 64: // reg: Phi(reg,reg)
Vikram S. Adve74825322002-03-18 03:15:35 +00002091 break; // don't forward the value
2092
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002093 case 71: // reg: VReg
2094 case 72: // reg: Constant
Vikram S. Adve74825322002-03-18 03:15:35 +00002095 break; // don't forward the value
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002096
2097 default:
2098 assert(0 && "Unrecognized BURG rule");
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002099 break;
2100 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00002101 }
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00002102
Chris Lattner20b1ea02001-09-14 03:47:57 +00002103 if (forwardOperandNum >= 0)
2104 { // We did not generate a machine instruction but need to use operand.
2105 // If user is in the same tree, replace Value in its machine operand.
2106 // If not, insert a copy instruction which should get coalesced away
2107 // by register allocation.
2108 if (subtreeRoot->parent() != NULL)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002109 ForwardOperand(subtreeRoot, subtreeRoot->parent(), forwardOperandNum);
Chris Lattner20b1ea02001-09-14 03:47:57 +00002110 else
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002111 {
Vikram S. Adve7fe27872001-10-18 00:26:20 +00002112 vector<MachineInstr*> minstrVec;
Vikram S. Adve242a8082002-05-19 15:25:51 +00002113 Instruction* instr = subtreeRoot->getInstruction();
2114 target.getInstrInfo().
2115 CreateCopyInstructionsByType(target,
2116 instr->getParent()->getParent(),
2117 instr->getOperand(forwardOperandNum),
2118 instr, minstrVec,
2119 MachineCodeForInstruction::get(instr));
Vikram S. Adve7fe27872001-10-18 00:26:20 +00002120 assert(minstrVec.size() > 0);
Vikram S. Adve74825322002-03-18 03:15:35 +00002121 mvec.insert(mvec.end(), minstrVec.begin(), minstrVec.end());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002122 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00002123 }
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00002124
2125 if (maskUnsignedResult)
2126 { // If result is unsigned and smaller than int reg size,
2127 // we need to clear high bits of result value.
2128 assert(forwardOperandNum < 0 && "Need mask but no instruction generated");
2129 Instruction* dest = subtreeRoot->getInstruction();
Vikram S. Adve8cfffd32002-08-24 20:56:53 +00002130 if (dest->getType()->isUnsigned())
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00002131 {
2132 unsigned destSize = target.DataLayout.getTypeSize(dest->getType());
Vikram S. Advebabc0fa2002-09-05 18:32:13 +00002133 if (destSize <= 4)
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00002134 { // Mask high bits. Use a TmpInstruction to represent the
2135 // intermediate result before masking. Since those instructions
2136 // have already been generated, go back and substitute tmpI
2137 // for dest in the result position of each one of them.
2138 TmpInstruction *tmpI = new TmpInstruction(dest->getType(), dest,
2139 NULL, "maskHi");
2140 MachineCodeForInstruction::get(dest).addTemp(tmpI);
2141
2142 for (unsigned i=0, N=mvec.size(); i < N; ++i)
2143 mvec[i]->substituteValue(dest, tmpI);
2144
Vikram S. Adve94c40812002-09-27 14:33:08 +00002145 M = Create3OperandInstr_UImmed(SRL, tmpI, 8*(4-destSize), dest);
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00002146 mvec.push_back(M);
2147 }
Vikram S. Advebabc0fa2002-09-05 18:32:13 +00002148 else if (destSize < target.DataLayout.getIntegerRegize())
2149 assert(0 && "Unsupported type size: 32 < size < 64 bits");
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00002150 }
2151 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00002152}