blob: dbcce3fdf4a7c0b4e3df7a869712c46546619750 [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 Lattnere5b1ed92003-01-15 00:03:28 +000011#include "llvm/CodeGen/MachineInstrBuilder.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 Lattnerea45d7b2002-12-28 20:19:44 +000016#include "llvm/CodeGen/MachineFunctionInfo.h"
Chris Lattner9c461082002-02-03 07:50:56 +000017#include "llvm/CodeGen/MachineCodeForInstruction.h"
Chris Lattner20b1ea02001-09-14 03:47:57 +000018#include "llvm/DerivedTypes.h"
19#include "llvm/iTerminators.h"
20#include "llvm/iMemory.h"
21#include "llvm/iOther.h"
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000022#include "llvm/Function.h"
Chris Lattner31bcdb82002-04-28 19:55:58 +000023#include "llvm/Constants.h"
Vikram S. Adved3e26482002-10-13 00:18:57 +000024#include "llvm/ConstantHandling.h"
Chris Lattnercee8f9a2001-11-27 00:03:19 +000025#include "Support/MathExtras.h"
Chris Lattner749655f2001-10-13 06:54:30 +000026#include <math.h>
Chris Lattner697954c2002-01-20 22:54:45 +000027using std::vector;
Chris Lattner20b1ea02001-09-14 03:47:57 +000028
Chris Lattner20b1ea02001-09-14 03:47:57 +000029//************************ Internal Functions ******************************/
30
Chris Lattner20b1ea02001-09-14 03:47:57 +000031
Chris Lattner20b1ea02001-09-14 03:47:57 +000032static inline MachineOpCode
33ChooseBprInstruction(const InstructionNode* instrNode)
34{
35 MachineOpCode opCode;
36
37 Instruction* setCCInstr =
38 ((InstructionNode*) instrNode->leftChild())->getInstruction();
39
40 switch(setCCInstr->getOpcode())
41 {
42 case Instruction::SetEQ: opCode = BRZ; break;
43 case Instruction::SetNE: opCode = BRNZ; break;
44 case Instruction::SetLE: opCode = BRLEZ; break;
45 case Instruction::SetGE: opCode = BRGEZ; break;
46 case Instruction::SetLT: opCode = BRLZ; break;
47 case Instruction::SetGT: opCode = BRGZ; break;
48 default:
49 assert(0 && "Unrecognized VM instruction!");
50 opCode = INVALID_OPCODE;
51 break;
52 }
53
54 return opCode;
55}
56
57
58static inline MachineOpCode
Chris Lattner20b1ea02001-09-14 03:47:57 +000059ChooseBpccInstruction(const InstructionNode* instrNode,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +000060 const BinaryOperator* setCCInstr)
Chris Lattner20b1ea02001-09-14 03:47:57 +000061{
62 MachineOpCode opCode = INVALID_OPCODE;
63
64 bool isSigned = setCCInstr->getOperand(0)->getType()->isSigned();
65
66 if (isSigned)
67 {
68 switch(setCCInstr->getOpcode())
Vikram S. Adve4cecdd22001-10-01 00:12:53 +000069 {
70 case Instruction::SetEQ: opCode = BE; break;
71 case Instruction::SetNE: opCode = BNE; break;
72 case Instruction::SetLE: opCode = BLE; break;
73 case Instruction::SetGE: opCode = BGE; break;
74 case Instruction::SetLT: opCode = BL; break;
75 case Instruction::SetGT: opCode = BG; break;
76 default:
77 assert(0 && "Unrecognized VM instruction!");
78 break;
79 }
Chris Lattner20b1ea02001-09-14 03:47:57 +000080 }
81 else
82 {
83 switch(setCCInstr->getOpcode())
Vikram S. Adve4cecdd22001-10-01 00:12:53 +000084 {
85 case Instruction::SetEQ: opCode = BE; break;
86 case Instruction::SetNE: opCode = BNE; break;
87 case Instruction::SetLE: opCode = BLEU; break;
88 case Instruction::SetGE: opCode = BCC; break;
89 case Instruction::SetLT: opCode = BCS; break;
90 case Instruction::SetGT: opCode = BGU; break;
91 default:
92 assert(0 && "Unrecognized VM instruction!");
93 break;
94 }
Chris Lattner20b1ea02001-09-14 03:47:57 +000095 }
96
97 return opCode;
98}
99
100static inline MachineOpCode
101ChooseBFpccInstruction(const InstructionNode* instrNode,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000102 const BinaryOperator* setCCInstr)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000103{
104 MachineOpCode opCode = INVALID_OPCODE;
105
106 switch(setCCInstr->getOpcode())
107 {
108 case Instruction::SetEQ: opCode = FBE; break;
109 case Instruction::SetNE: opCode = FBNE; break;
110 case Instruction::SetLE: opCode = FBLE; break;
111 case Instruction::SetGE: opCode = FBGE; break;
112 case Instruction::SetLT: opCode = FBL; break;
113 case Instruction::SetGT: opCode = FBG; break;
114 default:
115 assert(0 && "Unrecognized VM instruction!");
116 break;
117 }
118
119 return opCode;
120}
121
122
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000123// Create a unique TmpInstruction for a boolean value,
124// representing the CC register used by a branch on that value.
125// For now, hack this using a little static cache of TmpInstructions.
126// Eventually the entire BURG instruction selection should be put
127// into a separate class that can hold such information.
Vikram S. Adveff5a09e2001-11-08 05:04:09 +0000128// The static cache is not too bad because the memory for these
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000129// TmpInstructions will be freed along with the rest of the Function anyway.
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000130//
131static TmpInstruction*
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000132GetTmpForCC(Value* boolVal, const Function *F, const Type* ccType)
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000133{
Chris Lattner09ff1122002-07-24 21:21:32 +0000134 typedef hash_map<const Value*, TmpInstruction*> BoolTmpCache;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000135 static BoolTmpCache boolToTmpCache; // Map boolVal -> TmpInstruction*
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000136 static const Function *lastFunction = 0;// Use to flush cache between funcs
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000137
138 assert(boolVal->getType() == Type::BoolTy && "Weird but ok! Delete assert");
139
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000140 if (lastFunction != F)
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000141 {
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000142 lastFunction = F;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000143 boolToTmpCache.clear();
144 }
145
Vikram S. Adveff5a09e2001-11-08 05:04:09 +0000146 // Look for tmpI and create a new one otherwise. The new value is
147 // directly written to map using the ref returned by operator[].
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000148 TmpInstruction*& tmpI = boolToTmpCache[boolVal];
149 if (tmpI == NULL)
Chris Lattner9c461082002-02-03 07:50:56 +0000150 tmpI = new TmpInstruction(ccType, boolVal);
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000151
152 return tmpI;
153}
154
155
Chris Lattner20b1ea02001-09-14 03:47:57 +0000156static inline MachineOpCode
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000157ChooseBccInstruction(const InstructionNode* instrNode,
158 bool& isFPBranch)
159{
160 InstructionNode* setCCNode = (InstructionNode*) instrNode->leftChild();
Vikram S. Adve30a6f492002-08-22 02:56:10 +0000161 assert(setCCNode->getOpLabel() == SetCCOp);
162 BinaryOperator* setCCInstr =cast<BinaryOperator>(setCCNode->getInstruction());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000163 const Type* setCCType = setCCInstr->getOperand(0)->getType();
164
Vikram S. Adve242a8082002-05-19 15:25:51 +0000165 isFPBranch = setCCType->isFloatingPoint(); // Return value: don't delete!
166
167 if (isFPBranch)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000168 return ChooseBFpccInstruction(instrNode, setCCInstr);
169 else
170 return ChooseBpccInstruction(instrNode, setCCInstr);
171}
172
173
174static inline MachineOpCode
Chris Lattner20b1ea02001-09-14 03:47:57 +0000175ChooseMovFpccInstruction(const InstructionNode* instrNode)
176{
177 MachineOpCode opCode = INVALID_OPCODE;
178
179 switch(instrNode->getInstruction()->getOpcode())
180 {
181 case Instruction::SetEQ: opCode = MOVFE; break;
182 case Instruction::SetNE: opCode = MOVFNE; break;
183 case Instruction::SetLE: opCode = MOVFLE; break;
184 case Instruction::SetGE: opCode = MOVFGE; break;
185 case Instruction::SetLT: opCode = MOVFL; break;
186 case Instruction::SetGT: opCode = MOVFG; break;
187 default:
188 assert(0 && "Unrecognized VM instruction!");
189 break;
190 }
191
192 return opCode;
193}
194
195
196// Assumes that SUBcc v1, v2 -> v3 has been executed.
197// In most cases, we want to clear v3 and then follow it by instruction
198// MOVcc 1 -> v3.
199// Set mustClearReg=false if v3 need not be cleared before conditional move.
200// Set valueToMove=0 if we want to conditionally move 0 instead of 1
201// (i.e., we want to test inverse of a condition)
Vikram S. Adve243dd452001-09-18 13:03:13 +0000202// (The latter two cases do not seem to arise because SetNE needs nothing.)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000203//
204static MachineOpCode
205ChooseMovpccAfterSub(const InstructionNode* instrNode,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000206 bool& mustClearReg,
207 int& valueToMove)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000208{
209 MachineOpCode opCode = INVALID_OPCODE;
210 mustClearReg = true;
211 valueToMove = 1;
212
213 switch(instrNode->getInstruction()->getOpcode())
214 {
Vikram S. Adve243dd452001-09-18 13:03:13 +0000215 case Instruction::SetEQ: opCode = MOVE; break;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000216 case Instruction::SetLE: opCode = MOVLE; break;
217 case Instruction::SetGE: opCode = MOVGE; break;
218 case Instruction::SetLT: opCode = MOVL; break;
219 case Instruction::SetGT: opCode = MOVG; break;
Vikram S. Adve243dd452001-09-18 13:03:13 +0000220 case Instruction::SetNE: assert(0 && "No move required!"); break;
221 default: assert(0 && "Unrecognized VM instr!"); break;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000222 }
223
224 return opCode;
225}
226
Chris Lattner20b1ea02001-09-14 03:47:57 +0000227static inline MachineOpCode
Vikram S. Advedbc4fad2002-04-25 04:37:51 +0000228ChooseConvertToFloatInstr(OpLabel vopCode, const Type* opType)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000229{
230 MachineOpCode opCode = INVALID_OPCODE;
231
Vikram S. Advedbc4fad2002-04-25 04:37:51 +0000232 switch(vopCode)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000233 {
234 case ToFloatTy:
235 if (opType == Type::SByteTy || opType == Type::ShortTy || opType == Type::IntTy)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000236 opCode = FITOS;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000237 else if (opType == Type::LongTy)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000238 opCode = FXTOS;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000239 else if (opType == Type::DoubleTy)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000240 opCode = FDTOS;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000241 else if (opType == Type::FloatTy)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000242 ;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000243 else
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000244 assert(0 && "Cannot convert this type to FLOAT on SPARC");
Chris Lattner20b1ea02001-09-14 03:47:57 +0000245 break;
246
247 case ToDoubleTy:
Vikram S. Adve74825322002-03-18 03:15:35 +0000248 // This is usually used in conjunction with CreateCodeToCopyIntToFloat().
249 // Both functions should treat the integer as a 32-bit value for types
250 // of 4 bytes or less, and as a 64-bit value otherwise.
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000251 if (opType == Type::SByteTy || opType == Type::UByteTy ||
252 opType == Type::ShortTy || opType == Type::UShortTy ||
253 opType == Type::IntTy || opType == Type::UIntTy)
Vikram S. Adve74825322002-03-18 03:15:35 +0000254 opCode = FITOD;
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000255 else if (opType == Type::LongTy || opType == Type::ULongTy)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000256 opCode = FXTOD;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000257 else if (opType == Type::FloatTy)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000258 opCode = FSTOD;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000259 else if (opType == Type::DoubleTy)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000260 ;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000261 else
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000262 assert(0 && "Cannot convert this type to DOUBLE on SPARC");
Chris Lattner20b1ea02001-09-14 03:47:57 +0000263 break;
264
265 default:
266 break;
267 }
268
269 return opCode;
270}
271
272static inline MachineOpCode
Vikram S. Adve94c40812002-09-27 14:33:08 +0000273ChooseConvertFPToIntInstr(Type::PrimitiveID tid, const Type* opType)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000274{
275 MachineOpCode opCode = INVALID_OPCODE;;
Vikram S. Adve94c40812002-09-27 14:33:08 +0000276
277 assert((opType == Type::FloatTy || opType == Type::DoubleTy)
278 && "This function should only be called for FLOAT or DOUBLE");
279
280 if (tid==Type::UIntTyID)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000281 {
Vikram S. Adve94c40812002-09-27 14:33:08 +0000282 assert(tid != Type::UIntTyID && "FP-to-uint conversions must be expanded"
283 " into FP->long->uint for SPARC v9: SO RUN PRESELECTION PASS!");
284 }
285 else if (tid==Type::SByteTyID || tid==Type::ShortTyID || tid==Type::IntTyID ||
286 tid==Type::UByteTyID || tid==Type::UShortTyID)
287 {
288 opCode = (opType == Type::FloatTy)? FSTOI : FDTOI;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000289 }
Vikram S. Adve1e606692002-07-31 21:01:34 +0000290 else if (tid==Type::LongTyID || tid==Type::ULongTyID)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000291 {
Vikram S. Adve94c40812002-09-27 14:33:08 +0000292 opCode = (opType == Type::FloatTy)? FSTOX : FDTOX;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000293 }
294 else
295 assert(0 && "Should not get here, Mo!");
Vikram S. Adve94c40812002-09-27 14:33:08 +0000296
Chris Lattner20b1ea02001-09-14 03:47:57 +0000297 return opCode;
298}
299
Vikram S. Advedbc4fad2002-04-25 04:37:51 +0000300MachineInstr*
Vikram S. Adve94c40812002-09-27 14:33:08 +0000301CreateConvertFPToIntInstr(Type::PrimitiveID destTID,
302 Value* srcVal, Value* destVal)
Vikram S. Advedbc4fad2002-04-25 04:37:51 +0000303{
Vikram S. Adve94c40812002-09-27 14:33:08 +0000304 MachineOpCode opCode = ChooseConvertFPToIntInstr(destTID, srcVal->getType());
Vikram S. Advedbc4fad2002-04-25 04:37:51 +0000305 assert(opCode != INVALID_OPCODE && "Expected to need conversion!");
306
307 MachineInstr* M = new MachineInstr(opCode);
308 M->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister, srcVal);
309 M->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister, destVal);
310 return M;
311}
Chris Lattner20b1ea02001-09-14 03:47:57 +0000312
Vikram S. Adve8cfffd32002-08-24 20:56:53 +0000313// CreateCodeToConvertFloatToInt: Convert FP value to signed or unsigned integer
Vikram S. Adve1e606692002-07-31 21:01:34 +0000314// The FP value must be converted to the dest type in an FP register,
315// and the result is then copied from FP to int register via memory.
Vikram S. Advebabc0fa2002-09-05 18:32:13 +0000316//
317// Since fdtoi converts to signed integers, any FP value V between MAXINT+1
318// and MAXUNSIGNED (i.e., 2^31 <= V <= 2^32-1) would be converted incorrectly
Chris Lattnerea45d7b2002-12-28 20:19:44 +0000319// *only* when converting to an unsigned. (Unsigned byte, short or long
Vikram S. Advebabc0fa2002-09-05 18:32:13 +0000320// don't have this problem.)
321// For unsigned int, we therefore have to generate the code sequence:
322//
323// if (V > (float) MAXINT) {
324// unsigned result = (unsigned) (V - (float) MAXINT);
325// result = result + (unsigned) MAXINT;
326// }
327// else
Chris Lattnerea45d7b2002-12-28 20:19:44 +0000328// result = (unsigned) V;
Vikram S. Advebabc0fa2002-09-05 18:32:13 +0000329//
Vikram S. Adve1e606692002-07-31 21:01:34 +0000330static void
Vikram S. Adve8cfffd32002-08-24 20:56:53 +0000331CreateCodeToConvertFloatToInt(const TargetMachine& target,
332 Value* opVal,
333 Instruction* destI,
334 std::vector<MachineInstr*>& mvec,
335 MachineCodeForInstruction& mcfi)
Vikram S. Adve1e606692002-07-31 21:01:34 +0000336{
337 // Create a temporary to represent the FP register into which the
338 // int value will placed after conversion. The type of this temporary
339 // depends on the type of FP register to use: single-prec for a 32-bit
340 // int or smaller; double-prec for a 64-bit int.
341 //
Chris Lattnerea45d7b2002-12-28 20:19:44 +0000342 size_t destSize = target.getTargetData().getTypeSize(destI->getType());
Vikram S. Advebabc0fa2002-09-05 18:32:13 +0000343 const Type* destTypeToUse = (destSize > 4)? Type::DoubleTy : Type::FloatTy;
344 TmpInstruction* destForCast = new TmpInstruction(destTypeToUse, opVal);
Vikram S. Adve1e606692002-07-31 21:01:34 +0000345 mcfi.addTemp(destForCast);
346
347 // Create the fp-to-int conversion code
Vikram S. Adve94c40812002-09-27 14:33:08 +0000348 MachineInstr* M =CreateConvertFPToIntInstr(destI->getType()->getPrimitiveID(),
349 opVal, destForCast);
Vikram S. Adve1e606692002-07-31 21:01:34 +0000350 mvec.push_back(M);
351
352 // Create the fpreg-to-intreg copy code
353 target.getInstrInfo().
354 CreateCodeToCopyFloatToInt(target, destI->getParent()->getParent(),
Vikram S. Advebabc0fa2002-09-05 18:32:13 +0000355 destForCast, destI, mvec, mcfi);
Vikram S. Adve1e606692002-07-31 21:01:34 +0000356}
357
358
Chris Lattner20b1ea02001-09-14 03:47:57 +0000359static inline MachineOpCode
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000360ChooseAddInstruction(const InstructionNode* instrNode)
361{
362 return ChooseAddInstructionByType(instrNode->getInstruction()->getType());
363}
364
365
Chris Lattner20b1ea02001-09-14 03:47:57 +0000366static inline MachineInstr*
367CreateMovFloatInstruction(const InstructionNode* instrNode,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000368 const Type* resultType)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000369{
370 MachineInstr* minstr = new MachineInstr((resultType == Type::FloatTy)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000371 ? FMOVS : FMOVD);
Vikram S. Adve74825322002-03-18 03:15:35 +0000372 minstr->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister,
373 instrNode->leftChild()->getValue());
374 minstr->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister,
375 instrNode->getValue());
Chris Lattner20b1ea02001-09-14 03:47:57 +0000376 return minstr;
377}
378
379static inline MachineInstr*
380CreateAddConstInstruction(const InstructionNode* instrNode)
381{
382 MachineInstr* minstr = NULL;
383
384 Value* constOp = ((InstrTreeNode*) instrNode->rightChild())->getValue();
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000385 assert(isa<Constant>(constOp));
Chris Lattner20b1ea02001-09-14 03:47:57 +0000386
387 // Cases worth optimizing are:
388 // (1) Add with 0 for float or double: use an FMOV of appropriate type,
389 // instead of an FADD (1 vs 3 cycles). There is no integer MOV.
390 //
Chris Lattner9b625032002-05-06 16:15:30 +0000391 if (ConstantFP *FPC = dyn_cast<ConstantFP>(constOp)) {
392 double dval = FPC->getValue();
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000393 if (dval == 0.0)
Vikram S. Adve242a8082002-05-19 15:25:51 +0000394 minstr = CreateMovFloatInstruction(instrNode,
395 instrNode->getInstruction()->getType());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000396 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000397
398 return minstr;
399}
400
401
402static inline MachineOpCode
Vikram S. Adve510eec72001-11-04 21:59:14 +0000403ChooseSubInstructionByType(const Type* resultType)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000404{
405 MachineOpCode opCode = INVALID_OPCODE;
406
Chris Lattner0c4e8862002-09-03 01:08:28 +0000407 if (resultType->isInteger() || isa<PointerType>(resultType))
Chris Lattner20b1ea02001-09-14 03:47:57 +0000408 {
409 opCode = SUB;
410 }
411 else
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000412 switch(resultType->getPrimitiveID())
413 {
414 case Type::FloatTyID: opCode = FSUBS; break;
415 case Type::DoubleTyID: opCode = FSUBD; break;
416 default: assert(0 && "Invalid type for SUB instruction"); break;
417 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000418
419 return opCode;
420}
421
422
423static inline MachineInstr*
424CreateSubConstInstruction(const InstructionNode* instrNode)
425{
426 MachineInstr* minstr = NULL;
427
428 Value* constOp = ((InstrTreeNode*) instrNode->rightChild())->getValue();
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000429 assert(isa<Constant>(constOp));
Chris Lattner20b1ea02001-09-14 03:47:57 +0000430
431 // Cases worth optimizing are:
432 // (1) Sub with 0 for float or double: use an FMOV of appropriate type,
433 // instead of an FSUB (1 vs 3 cycles). There is no integer MOV.
434 //
Chris Lattner9b625032002-05-06 16:15:30 +0000435 if (ConstantFP *FPC = dyn_cast<ConstantFP>(constOp)) {
436 double dval = FPC->getValue();
437 if (dval == 0.0)
Vikram S. Adve242a8082002-05-19 15:25:51 +0000438 minstr = CreateMovFloatInstruction(instrNode,
439 instrNode->getInstruction()->getType());
Chris Lattner9b625032002-05-06 16:15:30 +0000440 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000441
442 return minstr;
443}
444
445
446static inline MachineOpCode
447ChooseFcmpInstruction(const InstructionNode* instrNode)
448{
449 MachineOpCode opCode = INVALID_OPCODE;
450
451 Value* operand = ((InstrTreeNode*) instrNode->leftChild())->getValue();
452 switch(operand->getType()->getPrimitiveID()) {
453 case Type::FloatTyID: opCode = FCMPS; break;
454 case Type::DoubleTyID: opCode = FCMPD; break;
455 default: assert(0 && "Invalid type for FCMP instruction"); break;
456 }
457
458 return opCode;
459}
460
461
462// Assumes that leftArg and rightArg are both cast instructions.
463//
464static inline bool
465BothFloatToDouble(const InstructionNode* instrNode)
466{
467 InstrTreeNode* leftArg = instrNode->leftChild();
468 InstrTreeNode* rightArg = instrNode->rightChild();
469 InstrTreeNode* leftArgArg = leftArg->leftChild();
470 InstrTreeNode* rightArgArg = rightArg->leftChild();
471 assert(leftArg->getValue()->getType() == rightArg->getValue()->getType());
472
473 // Check if both arguments are floats cast to double
474 return (leftArg->getValue()->getType() == Type::DoubleTy &&
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000475 leftArgArg->getValue()->getType() == Type::FloatTy &&
476 rightArgArg->getValue()->getType() == Type::FloatTy);
Chris Lattner20b1ea02001-09-14 03:47:57 +0000477}
478
479
480static inline MachineOpCode
Vikram S. Adve510eec72001-11-04 21:59:14 +0000481ChooseMulInstructionByType(const Type* resultType)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000482{
483 MachineOpCode opCode = INVALID_OPCODE;
484
Chris Lattner0c4e8862002-09-03 01:08:28 +0000485 if (resultType->isInteger())
Vikram S. Adve510eec72001-11-04 21:59:14 +0000486 opCode = MULX;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000487 else
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000488 switch(resultType->getPrimitiveID())
489 {
490 case Type::FloatTyID: opCode = FMULS; break;
491 case Type::DoubleTyID: opCode = FMULD; break;
492 default: assert(0 && "Invalid type for MUL instruction"); break;
493 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000494
495 return opCode;
496}
497
498
Vikram S. Adve510eec72001-11-04 21:59:14 +0000499
Chris Lattner20b1ea02001-09-14 03:47:57 +0000500static inline MachineInstr*
Vikram S. Adve74825322002-03-18 03:15:35 +0000501CreateIntNegInstruction(const TargetMachine& target,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000502 Value* vreg)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000503{
504 MachineInstr* minstr = new MachineInstr(SUB);
Vikram S. Adve74825322002-03-18 03:15:35 +0000505 minstr->SetMachineOperandReg(0, target.getRegInfo().getZeroRegNum());
506 minstr->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister, vreg);
507 minstr->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister, vreg);
Chris Lattner20b1ea02001-09-14 03:47:57 +0000508 return minstr;
509}
510
511
Vikram S. Adve242a8082002-05-19 15:25:51 +0000512// Create instruction sequence for any shift operation.
513// SLL or SLLX on an operand smaller than the integer reg. size (64bits)
514// requires a second instruction for explicit sign-extension.
515// Note that we only have to worry about a sign-bit appearing in the
516// most significant bit of the operand after shifting (e.g., bit 32 of
517// Int or bit 16 of Short), so we do not have to worry about results
518// that are as large as a normal integer register.
519//
520static inline void
521CreateShiftInstructions(const TargetMachine& target,
522 Function* F,
523 MachineOpCode shiftOpCode,
524 Value* argVal1,
525 Value* optArgVal2, /* Use optArgVal2 if not NULL */
Chris Lattnerea45d7b2002-12-28 20:19:44 +0000526 unsigned optShiftNum, /* else use optShiftNum */
Vikram S. Adve242a8082002-05-19 15:25:51 +0000527 Instruction* destVal,
528 vector<MachineInstr*>& mvec,
529 MachineCodeForInstruction& mcfi)
530{
531 assert((optArgVal2 != NULL || optShiftNum <= 64) &&
532 "Large shift sizes unexpected, but can be handled below: "
533 "You need to check whether or not it fits in immed field below");
534
535 // If this is a logical left shift of a type smaller than the standard
536 // integer reg. size, we have to extend the sign-bit into upper bits
537 // of dest, so we need to put the result of the SLL into a temporary.
538 //
539 Value* shiftDest = destVal;
Chris Lattnerea45d7b2002-12-28 20:19:44 +0000540 unsigned opSize = target.getTargetData().getTypeSize(argVal1->getType());
Vikram S. Adve242a8082002-05-19 15:25:51 +0000541 if ((shiftOpCode == SLL || shiftOpCode == SLLX)
Chris Lattnerea45d7b2002-12-28 20:19:44 +0000542 && opSize < target.getTargetData().getIntegerRegize())
Vikram S. Adve242a8082002-05-19 15:25:51 +0000543 { // put SLL result into a temporary
544 shiftDest = new TmpInstruction(argVal1, optArgVal2, "sllTmp");
545 mcfi.addTemp(shiftDest);
546 }
547
548 MachineInstr* M = (optArgVal2 != NULL)
Chris Lattnere5b1ed92003-01-15 00:03:28 +0000549 ? BuildMI(shiftOpCode, 3).addReg(argVal1).addReg(optArgVal2)
550 .addReg(shiftDest, MOTy::Def)
551 : BuildMI(shiftOpCode, 3).addReg(argVal1).addZImm(optShiftNum)
552 .addReg(shiftDest, MOTy::Def);
Vikram S. Adve242a8082002-05-19 15:25:51 +0000553 mvec.push_back(M);
554
555 if (shiftDest != destVal)
556 { // extend the sign-bit of the result into all upper bits of dest
557 assert(8*opSize <= 32 && "Unexpected type size > 4 and < IntRegSize?");
558 target.getInstrInfo().
Vikram S. Adve94c40812002-09-27 14:33:08 +0000559 CreateSignExtensionInstructions(target, F, shiftDest, destVal,
560 8*opSize, mvec, mcfi);
Vikram S. Adve242a8082002-05-19 15:25:51 +0000561 }
562}
563
564
Vikram S. Adve74825322002-03-18 03:15:35 +0000565// Does not create any instructions if we cannot exploit constant to
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000566// create a cheaper instruction.
567// This returns the approximate cost of the instructions generated,
568// which is used to pick the cheapest when both operands are constant.
Chris Lattnerea45d7b2002-12-28 20:19:44 +0000569static inline unsigned
Vikram S. Adve242a8082002-05-19 15:25:51 +0000570CreateMulConstInstruction(const TargetMachine &target, Function* F,
571 Value* lval, Value* rval, Instruction* destVal,
572 vector<MachineInstr*>& mvec,
573 MachineCodeForInstruction& mcfi)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000574{
Vikram S. Adve242a8082002-05-19 15:25:51 +0000575 /* Use max. multiply cost, viz., cost of MULX */
Chris Lattnerea45d7b2002-12-28 20:19:44 +0000576 unsigned cost = target.getInstrInfo().minLatency(MULX);
577 unsigned firstNewInstr = mvec.size();
Vikram S. Adve74825322002-03-18 03:15:35 +0000578
579 Value* constOp = rval;
580 if (! isa<Constant>(constOp))
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000581 return cost;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000582
583 // Cases worth optimizing are:
584 // (1) Multiply by 0 or 1 for any type: replace with copy (ADD or FMOV)
585 // (2) Multiply by 2^x for integer types: replace with Shift
586 //
Vikram S. Adve74825322002-03-18 03:15:35 +0000587 const Type* resultType = destVal->getType();
Chris Lattner20b1ea02001-09-14 03:47:57 +0000588
Chris Lattner0c4e8862002-09-03 01:08:28 +0000589 if (resultType->isInteger() || isa<PointerType>(resultType))
Chris Lattner20b1ea02001-09-14 03:47:57 +0000590 {
Chris Lattner20b1ea02001-09-14 03:47:57 +0000591 bool isValidConst;
592 int64_t C = GetConstantValueAsSignedInt(constOp, isValidConst);
593 if (isValidConst)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000594 {
Vikram S. Adve242a8082002-05-19 15:25:51 +0000595 unsigned pow;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000596 bool needNeg = false;
597 if (C < 0)
598 {
599 needNeg = true;
600 C = -C;
601 }
602
603 if (C == 0 || C == 1)
604 {
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000605 cost = target.getInstrInfo().minLatency(ADD);
Chris Lattnere5b1ed92003-01-15 00:03:28 +0000606 unsigned ZeroReg = target.getRegInfo().getZeroRegNum();
Vikram S. Adve242a8082002-05-19 15:25:51 +0000607 MachineInstr* M = (C == 0)
Chris Lattnere5b1ed92003-01-15 00:03:28 +0000608 ? Create3OperandInstr_Reg(ADD, ZeroReg, ZeroReg, destVal)
609 : Create3OperandInstr_Reg(ADD, lval, ZeroReg, destVal);
Vikram S. Adve242a8082002-05-19 15:25:51 +0000610 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000611 }
Chris Lattner36346c72002-05-19 21:20:19 +0000612 else if (isPowerOf2(C, pow))
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000613 {
Chris Lattnerea45d7b2002-12-28 20:19:44 +0000614 unsigned opSize = target.getTargetData().getTypeSize(resultType);
Vikram S. Adve242a8082002-05-19 15:25:51 +0000615 MachineOpCode opCode = (opSize <= 32)? SLL : SLLX;
616 CreateShiftInstructions(target, F, opCode, lval, NULL, pow,
617 destVal, mvec, mcfi);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000618 }
619
Vikram S. Adve242a8082002-05-19 15:25:51 +0000620 if (mvec.size() > 0 && needNeg)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000621 { // insert <reg = SUB 0, reg> after the instr to flip the sign
Vikram S. Adve242a8082002-05-19 15:25:51 +0000622 MachineInstr* M = CreateIntNegInstruction(target, destVal);
623 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000624 }
625 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000626 }
627 else
628 {
Chris Lattner9b625032002-05-06 16:15:30 +0000629 if (ConstantFP *FPC = dyn_cast<ConstantFP>(constOp))
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000630 {
Chris Lattner9b625032002-05-06 16:15:30 +0000631 double dval = FPC->getValue();
Vikram S. Adve6ad7c552001-11-09 02:18:16 +0000632 if (fabs(dval) == 1)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000633 {
Vikram S. Adve242a8082002-05-19 15:25:51 +0000634 MachineOpCode opCode = (dval < 0)
Vikram S. Adve6ad7c552001-11-09 02:18:16 +0000635 ? (resultType == Type::FloatTy? FNEGS : FNEGD)
636 : (resultType == Type::FloatTy? FMOVS : FMOVD);
Vikram S. Adve242a8082002-05-19 15:25:51 +0000637 MachineInstr* M = Create2OperandInstr(opCode, lval, destVal);
638 mvec.push_back(M);
Vikram S. Adve6ad7c552001-11-09 02:18:16 +0000639 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000640 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000641 }
642
Vikram S. Adve242a8082002-05-19 15:25:51 +0000643 if (firstNewInstr < mvec.size())
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000644 {
Vikram S. Adve242a8082002-05-19 15:25:51 +0000645 cost = 0;
Chris Lattnerea45d7b2002-12-28 20:19:44 +0000646 for (unsigned i=firstNewInstr; i < mvec.size(); ++i)
Vikram S. Adve242a8082002-05-19 15:25:51 +0000647 cost += target.getInstrInfo().minLatency(mvec[i]->getOpCode());
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000648 }
649
650 return cost;
Vikram S. Adve74825322002-03-18 03:15:35 +0000651}
652
653
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000654// Does not create any instructions if we cannot exploit constant to
655// create a cheaper instruction.
656//
657static inline void
658CreateCheapestMulConstInstruction(const TargetMachine &target,
Vikram S. Adve242a8082002-05-19 15:25:51 +0000659 Function* F,
660 Value* lval, Value* rval,
661 Instruction* destVal,
662 vector<MachineInstr*>& mvec,
663 MachineCodeForInstruction& mcfi)
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000664{
665 Value* constOp;
666 if (isa<Constant>(lval) && isa<Constant>(rval))
Vikram S. Adved3e26482002-10-13 00:18:57 +0000667 { // both operands are constant: evaluate and "set" in dest
668 Constant* P = ConstantFoldBinaryInstruction(Instruction::Mul,
669 cast<Constant>(lval), cast<Constant>(rval));
670 target.getInstrInfo().CreateCodeToLoadConst(target,F,P,destVal,mvec,mcfi);
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000671 }
672 else if (isa<Constant>(rval)) // rval is constant, but not lval
Vikram S. Adve242a8082002-05-19 15:25:51 +0000673 CreateMulConstInstruction(target, F, lval, rval, destVal, mvec, mcfi);
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000674 else if (isa<Constant>(lval)) // lval is constant, but not rval
Vikram S. Adve242a8082002-05-19 15:25:51 +0000675 CreateMulConstInstruction(target, F, lval, rval, destVal, mvec, mcfi);
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000676
677 // else neither is constant
678 return;
679}
680
Vikram S. Adve74825322002-03-18 03:15:35 +0000681// Return NULL if we cannot exploit constant to create a cheaper instruction
682static inline void
Vikram S. Adve242a8082002-05-19 15:25:51 +0000683CreateMulInstruction(const TargetMachine &target, Function* F,
684 Value* lval, Value* rval, Instruction* destVal,
Vikram S. Adve74825322002-03-18 03:15:35 +0000685 vector<MachineInstr*>& mvec,
Vikram S. Adve242a8082002-05-19 15:25:51 +0000686 MachineCodeForInstruction& mcfi,
Vikram S. Adve74825322002-03-18 03:15:35 +0000687 MachineOpCode forceMulOp = INVALID_MACHINE_OPCODE)
688{
Chris Lattnerea45d7b2002-12-28 20:19:44 +0000689 unsigned L = mvec.size();
Vikram S. Adve242a8082002-05-19 15:25:51 +0000690 CreateCheapestMulConstInstruction(target,F, lval, rval, destVal, mvec, mcfi);
Vikram S. Adve74825322002-03-18 03:15:35 +0000691 if (mvec.size() == L)
692 { // no instructions were added so create MUL reg, reg, reg.
693 // Use FSMULD if both operands are actually floats cast to doubles.
694 // Otherwise, use the default opcode for the appropriate type.
695 MachineOpCode mulOp = ((forceMulOp != INVALID_MACHINE_OPCODE)
696 ? forceMulOp
697 : ChooseMulInstructionByType(destVal->getType()));
698 MachineInstr* M = new MachineInstr(mulOp);
699 M->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister, lval);
700 M->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister, rval);
701 M->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister, destVal);
702 mvec.push_back(M);
703 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000704}
705
706
Vikram S. Adve510eec72001-11-04 21:59:14 +0000707// Generate a divide instruction for Div or Rem.
708// For Rem, this assumes that the operand type will be signed if the result
709// type is signed. This is correct because they must have the same sign.
710//
Chris Lattner20b1ea02001-09-14 03:47:57 +0000711static inline MachineOpCode
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000712ChooseDivInstruction(TargetMachine &target,
713 const InstructionNode* instrNode)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000714{
715 MachineOpCode opCode = INVALID_OPCODE;
716
717 const Type* resultType = instrNode->getInstruction()->getType();
718
Chris Lattner0c4e8862002-09-03 01:08:28 +0000719 if (resultType->isInteger())
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000720 opCode = resultType->isSigned()? SDIVX : UDIVX;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000721 else
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000722 switch(resultType->getPrimitiveID())
723 {
724 case Type::FloatTyID: opCode = FDIVS; break;
725 case Type::DoubleTyID: opCode = FDIVD; break;
726 default: assert(0 && "Invalid type for DIV instruction"); break;
727 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000728
729 return opCode;
730}
731
732
Vikram S. Adve74825322002-03-18 03:15:35 +0000733// Return NULL if we cannot exploit constant to create a cheaper instruction
734static inline void
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000735CreateDivConstInstruction(TargetMachine &target,
736 const InstructionNode* instrNode,
Vikram S. Adve74825322002-03-18 03:15:35 +0000737 vector<MachineInstr*>& mvec)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000738{
Vikram S. Adve74825322002-03-18 03:15:35 +0000739 MachineInstr* minstr1 = NULL;
740 MachineInstr* minstr2 = NULL;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000741
742 Value* constOp = ((InstrTreeNode*) instrNode->rightChild())->getValue();
Vikram S. Adve74825322002-03-18 03:15:35 +0000743 if (! isa<Constant>(constOp))
744 return;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000745
746 // Cases worth optimizing are:
747 // (1) Divide by 1 for any type: replace with copy (ADD or FMOV)
748 // (2) Divide by 2^x for integer types: replace with SR[L or A]{X}
749 //
750 const Type* resultType = instrNode->getInstruction()->getType();
751
Chris Lattner0c4e8862002-09-03 01:08:28 +0000752 if (resultType->isInteger())
Chris Lattner20b1ea02001-09-14 03:47:57 +0000753 {
754 unsigned pow;
755 bool isValidConst;
756 int64_t C = GetConstantValueAsSignedInt(constOp, isValidConst);
757 if (isValidConst)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000758 {
759 bool needNeg = false;
760 if (C < 0)
761 {
762 needNeg = true;
763 C = -C;
764 }
765
766 if (C == 1)
767 {
Vikram S. Adve74825322002-03-18 03:15:35 +0000768 minstr1 = new MachineInstr(ADD);
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000769 minstr1->SetMachineOperandVal(0,
770 MachineOperand::MO_VirtualRegister,
771 instrNode->leftChild()->getValue());
772 minstr1->SetMachineOperandReg(1,
773 target.getRegInfo().getZeroRegNum());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000774 }
Chris Lattner36346c72002-05-19 21:20:19 +0000775 else if (isPowerOf2(C, pow))
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000776 {
777 MachineOpCode opCode= ((resultType->isSigned())
778 ? (resultType==Type::LongTy)? SRAX : SRA
779 : (resultType==Type::LongTy)? SRLX : SRL);
Vikram S. Adve74825322002-03-18 03:15:35 +0000780 minstr1 = new MachineInstr(opCode);
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000781 minstr1->SetMachineOperandVal(0,
782 MachineOperand::MO_VirtualRegister,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000783 instrNode->leftChild()->getValue());
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000784 minstr1->SetMachineOperandConst(1,
785 MachineOperand::MO_UnextendedImmed,
786 pow);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000787 }
788
Vikram S. Adve74825322002-03-18 03:15:35 +0000789 if (minstr1 && needNeg)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000790 { // insert <reg = SUB 0, reg> after the instr to flip the sign
Vikram S. Adve74825322002-03-18 03:15:35 +0000791 minstr2 = CreateIntNegInstruction(target,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000792 instrNode->getValue());
793 }
794 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000795 }
796 else
797 {
Chris Lattner9b625032002-05-06 16:15:30 +0000798 if (ConstantFP *FPC = dyn_cast<ConstantFP>(constOp))
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000799 {
Chris Lattner9b625032002-05-06 16:15:30 +0000800 double dval = FPC->getValue();
Vikram S. Adve6ad7c552001-11-09 02:18:16 +0000801 if (fabs(dval) == 1)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000802 {
803 bool needNeg = (dval < 0);
804
805 MachineOpCode opCode = needNeg
806 ? (resultType == Type::FloatTy? FNEGS : FNEGD)
807 : (resultType == Type::FloatTy? FMOVS : FMOVD);
808
Vikram S. Adve74825322002-03-18 03:15:35 +0000809 minstr1 = new MachineInstr(opCode);
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000810 minstr1->SetMachineOperandVal(0,
811 MachineOperand::MO_VirtualRegister,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000812 instrNode->leftChild()->getValue());
813 }
814 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000815 }
816
Vikram S. Adve74825322002-03-18 03:15:35 +0000817 if (minstr1 != NULL)
818 minstr1->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister,
819 instrNode->getValue());
Chris Lattner20b1ea02001-09-14 03:47:57 +0000820
Vikram S. Adve74825322002-03-18 03:15:35 +0000821 if (minstr1)
822 mvec.push_back(minstr1);
823 if (minstr2)
824 mvec.push_back(minstr2);
Chris Lattner20b1ea02001-09-14 03:47:57 +0000825}
826
827
Vikram S. Adve74825322002-03-18 03:15:35 +0000828static void
829CreateCodeForVariableSizeAlloca(const TargetMachine& target,
830 Instruction* result,
Chris Lattnerea45d7b2002-12-28 20:19:44 +0000831 unsigned tsize,
Vikram S. Adve74825322002-03-18 03:15:35 +0000832 Value* numElementsVal,
833 vector<MachineInstr*>& getMvec)
834{
Vikram S. Adveaabb5952002-10-29 19:37:31 +0000835 Value* totalSizeVal;
Vikram S. Adve74825322002-03-18 03:15:35 +0000836 MachineInstr* M;
Vikram S. Adved3e26482002-10-13 00:18:57 +0000837 MachineCodeForInstruction& mcfi = MachineCodeForInstruction::get(result);
Vikram S. Adveaabb5952002-10-29 19:37:31 +0000838 Function *F = result->getParent()->getParent();
Vikram S. Adved3e26482002-10-13 00:18:57 +0000839
Vikram S. Adveaabb5952002-10-29 19:37:31 +0000840 // Enforce the alignment constraints on the stack pointer at
841 // compile time if the total size is a known constant.
842 if (isa<Constant>(numElementsVal))
843 {
844 bool isValid;
845 int64_t numElem = GetConstantValueAsSignedInt(numElementsVal, isValid);
846 assert(isValid && "Unexpectedly large array dimension in alloca!");
847 int64_t total = numElem * tsize;
848 if (int extra= total % target.getFrameInfo().getStackFrameSizeAlignment())
849 total += target.getFrameInfo().getStackFrameSizeAlignment() - extra;
850 totalSizeVal = ConstantSInt::get(Type::IntTy, total);
851 }
852 else
853 {
854 // The size is not a constant. Generate code to compute it and
855 // code to pad the size for stack alignment.
856 // Create a Value to hold the (constant) element size
857 Value* tsizeVal = ConstantSInt::get(Type::IntTy, tsize);
858
859 // Create temporary values to hold the result of MUL, SLL, SRL
860 // THIS CASE IS INCOMPLETE AND WILL BE FIXED SHORTLY.
861 TmpInstruction* tmpProd = new TmpInstruction(numElementsVal, tsizeVal);
862 TmpInstruction* tmpSLL = new TmpInstruction(numElementsVal, tmpProd);
863 TmpInstruction* tmpSRL = new TmpInstruction(numElementsVal, tmpSLL);
864 mcfi.addTemp(tmpProd);
865 mcfi.addTemp(tmpSLL);
866 mcfi.addTemp(tmpSRL);
867
868 // Instruction 1: mul numElements, typeSize -> tmpProd
869 // This will optimize the MUL as far as possible.
870 CreateMulInstruction(target, F, numElementsVal, tsizeVal, tmpProd,getMvec,
871 mcfi, INVALID_MACHINE_OPCODE);
872
873 assert(0 && "Need to insert padding instructions here!");
874
875 totalSizeVal = tmpProd;
876 }
Vikram S. Adve74825322002-03-18 03:15:35 +0000877
878 // Get the constant offset from SP for dynamically allocated storage
879 // and create a temporary Value to hold it.
Misha Brukmanfce11432002-10-28 00:28:31 +0000880 MachineFunction& mcInfo = MachineFunction::get(F);
Vikram S. Adve74825322002-03-18 03:15:35 +0000881 bool growUp;
882 ConstantSInt* dynamicAreaOffset =
883 ConstantSInt::get(Type::IntTy,
Vikram S. Adveaabb5952002-10-29 19:37:31 +0000884 target.getFrameInfo().getDynamicAreaOffset(mcInfo,growUp));
Vikram S. Adve74825322002-03-18 03:15:35 +0000885 assert(! growUp && "Has SPARC v9 stack frame convention changed?");
886
Vikram S. Adveaabb5952002-10-29 19:37:31 +0000887 // Instruction 2: sub %sp, totalSizeVal -> %sp
Vikram S. Adve74825322002-03-18 03:15:35 +0000888 M = new MachineInstr(SUB);
889 M->SetMachineOperandReg(0, target.getRegInfo().getStackPointer());
Vikram S. Adveaabb5952002-10-29 19:37:31 +0000890 M->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister, totalSizeVal);
Vikram S. Adve74825322002-03-18 03:15:35 +0000891 M->SetMachineOperandReg(2, target.getRegInfo().getStackPointer());
892 getMvec.push_back(M);
Vikram S. Adveaabb5952002-10-29 19:37:31 +0000893
Vikram S. Adve74825322002-03-18 03:15:35 +0000894 // Instruction 3: add %sp, frameSizeBelowDynamicArea -> result
895 M = new MachineInstr(ADD);
896 M->SetMachineOperandReg(0, target.getRegInfo().getStackPointer());
Chris Lattnerea45d7b2002-12-28 20:19:44 +0000897 M->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister,
898 dynamicAreaOffset);
Vikram S. Adve74825322002-03-18 03:15:35 +0000899 M->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister, result);
900 getMvec.push_back(M);
901}
902
903
904static void
905CreateCodeForFixedSizeAlloca(const TargetMachine& target,
906 Instruction* result,
Chris Lattnerea45d7b2002-12-28 20:19:44 +0000907 unsigned tsize,
908 unsigned numElements,
Vikram S. Adve74825322002-03-18 03:15:35 +0000909 vector<MachineInstr*>& getMvec)
910{
Vikram S. Adved3e26482002-10-13 00:18:57 +0000911 assert(tsize > 0 && "Illegal (zero) type size for alloca");
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000912 assert(result && result->getParent() &&
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000913 "Result value is not part of a function?");
914 Function *F = result->getParent()->getParent();
Misha Brukmanfce11432002-10-28 00:28:31 +0000915 MachineFunction &mcInfo = MachineFunction::get(F);
Vikram S. Adve74825322002-03-18 03:15:35 +0000916
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000917 // Check if the offset would small enough to use as an immediate in
918 // load/stores (check LDX because all load/stores have the same-size immediate
919 // field). If not, put the variable in the dynamically sized area of the
920 // frame.
Chris Lattnerea45d7b2002-12-28 20:19:44 +0000921 unsigned paddedSizeIgnored;
922 int offsetFromFP = mcInfo.getInfo()->computeOffsetforLocalVar(result,
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000923 paddedSizeIgnored,
Vikram S. Adve74825322002-03-18 03:15:35 +0000924 tsize * numElements);
Chris Lattnerea45d7b2002-12-28 20:19:44 +0000925 if (! target.getInstrInfo().constantFitsInImmedField(LDX, offsetFromFP)) {
926 CreateCodeForVariableSizeAlloca(target, result, tsize,
927 ConstantSInt::get(Type::IntTy,numElements),
928 getMvec);
929 return;
930 }
Vikram S. Adve74825322002-03-18 03:15:35 +0000931
932 // else offset fits in immediate field so go ahead and allocate it.
Chris Lattnerea45d7b2002-12-28 20:19:44 +0000933 offsetFromFP = mcInfo.getInfo()->allocateLocalVar(result, tsize *numElements);
Vikram S. Adve74825322002-03-18 03:15:35 +0000934
935 // Create a temporary Value to hold the constant offset.
936 // This is needed because it may not fit in the immediate field.
937 ConstantSInt* offsetVal = ConstantSInt::get(Type::IntTy, offsetFromFP);
938
939 // Instruction 1: add %fp, offsetFromFP -> result
940 MachineInstr* M = new MachineInstr(ADD);
941 M->SetMachineOperandReg(0, target.getRegInfo().getFramePointer());
942 M->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister, offsetVal);
943 M->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister, result);
944
945 getMvec.push_back(M);
946}
947
948
Chris Lattner20b1ea02001-09-14 03:47:57 +0000949//------------------------------------------------------------------------
950// Function SetOperandsForMemInstr
951//
952// Choose addressing mode for the given load or store instruction.
953// Use [reg+reg] if it is an indexed reference, and the index offset is
954// not a constant or if it cannot fit in the offset field.
955// Use [reg+offset] in all other cases.
956//
957// This assumes that all array refs are "lowered" to one of these forms:
958// %x = load (subarray*) ptr, constant ; single constant offset
959// %x = load (subarray*) ptr, offsetVal ; single non-constant offset
960// Generally, this should happen via strength reduction + LICM.
961// Also, strength reduction should take care of using the same register for
962// the loop index variable and an array index, when that is profitable.
963//------------------------------------------------------------------------
964
965static void
Vikram S. Adve74825322002-03-18 03:15:35 +0000966SetOperandsForMemInstr(vector<MachineInstr*>& mvec,
Vikram S. Adveefc94332002-10-14 16:32:24 +0000967 InstructionNode* vmInstrNode,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000968 const TargetMachine& target)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000969{
Vikram S. Adve8cfffd32002-08-24 20:56:53 +0000970 Instruction* memInst = vmInstrNode->getInstruction();
971 vector<MachineInstr*>::iterator mvecI = mvec.end() - 1;
972
973 // Index vector, ptr value, and flag if all indices are const.
Vikram S. Advea10d1a72002-03-31 19:07:35 +0000974 vector<Value*> idxVec;
Vikram S. Adve8cfffd32002-08-24 20:56:53 +0000975 bool allConstantIndices;
976 Value* ptrVal = GetMemInstArgs(vmInstrNode, idxVec, allConstantIndices);
Vikram S. Adveed3fefb2002-08-03 13:48:21 +0000977
Vikram S. Adve8cfffd32002-08-24 20:56:53 +0000978 // Now create the appropriate operands for the machine instruction.
979 // First, initialize so we default to storing the offset in a register.
Chris Lattner8e5c0b42001-11-07 14:01:59 +0000980 int64_t smallConstOffset = 0;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000981 Value* valueForRegOffset = NULL;
Vikram S. Adveed3fefb2002-08-03 13:48:21 +0000982 MachineOperand::MachineOperandType offsetOpType =
983 MachineOperand::MO_VirtualRegister;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000984
Vikram S. Adve74825322002-03-18 03:15:35 +0000985 // Check if there is an index vector and if so, compute the
986 // right offset for structures and for arrays
Chris Lattner20b1ea02001-09-14 03:47:57 +0000987 //
Chris Lattner3bb8ad22002-08-22 23:37:24 +0000988 if (!idxVec.empty())
Chris Lattner20b1ea02001-09-14 03:47:57 +0000989 {
Vikram S. Adve74825322002-03-18 03:15:35 +0000990 const PointerType* ptrType = cast<PointerType>(ptrVal->getType());
Chris Lattner20b1ea02001-09-14 03:47:57 +0000991
Vikram S. Adve242a8082002-05-19 15:25:51 +0000992 // If all indices are constant, compute the combined offset directly.
993 if (allConstantIndices)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000994 {
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000995 // Compute the offset value using the index vector. Create a
996 // virtual reg. for it since it may not fit in the immed field.
Chris Lattnerea45d7b2002-12-28 20:19:44 +0000997 uint64_t offset = target.getTargetData().getIndexedOffset(ptrType,idxVec);
Vikram S. Adve242a8082002-05-19 15:25:51 +0000998 valueForRegOffset = ConstantSInt::get(Type::LongTy, offset);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000999 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00001000 else
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001001 {
Vikram S. Adve242a8082002-05-19 15:25:51 +00001002 // There is at least one non-constant offset. Therefore, this must
Vikram S. Adveed3fefb2002-08-03 13:48:21 +00001003 // be an array ref, and must have been lowered to a single non-zero
1004 // offset. (An extra leading zero offset, if any, can be ignored.)
1005 // Generate code sequence to compute address from index.
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001006 //
Chris Lattner0374b8d2002-09-11 01:21:35 +00001007 bool firstIdxIsZero =
1008 (idxVec[0] == Constant::getNullValue(idxVec[0]->getType()));
Vikram S. Advebabc0fa2002-09-05 18:32:13 +00001009 assert(idxVec.size() == 1U + firstIdxIsZero
Vikram S. Adveed3fefb2002-08-03 13:48:21 +00001010 && "Array refs must be lowered before Instruction Selection");
1011
Vikram S. Advebabc0fa2002-09-05 18:32:13 +00001012 Value* idxVal = idxVec[firstIdxIsZero];
Vikram S. Adveed3fefb2002-08-03 13:48:21 +00001013
1014 vector<MachineInstr*> mulVec;
Vikram S. Adve94c40812002-09-27 14:33:08 +00001015 Instruction* addr = new TmpInstruction(Type::ULongTy, memInst);
Vikram S. Adveed3fefb2002-08-03 13:48:21 +00001016 MachineCodeForInstruction::get(memInst).addTemp(addr);
1017
Vikram S. Advebabc0fa2002-09-05 18:32:13 +00001018 // Get the array type indexed by idxVal, and compute its element size.
Vikram S. Adveed3fefb2002-08-03 13:48:21 +00001019 // The call to getTypeSize() will fail if size is not constant.
Vikram S. Advebabc0fa2002-09-05 18:32:13 +00001020 const Type* vecType = (firstIdxIsZero
1021 ? GetElementPtrInst::getIndexedType(ptrType,
1022 std::vector<Value*>(1U, idxVec[0]),
1023 /*AllowCompositeLeaf*/ true)
1024 : ptrType);
1025 const Type* eltType = cast<SequentialType>(vecType)->getElementType();
Vikram S. Advee102a642002-09-16 15:56:45 +00001026 ConstantUInt* eltSizeVal = ConstantUInt::get(Type::ULongTy,
Chris Lattnerea45d7b2002-12-28 20:19:44 +00001027 target.getTargetData().getTypeSize(eltType));
Vikram S. Adveed3fefb2002-08-03 13:48:21 +00001028
1029 // CreateMulInstruction() folds constants intelligently enough.
Vikram S. Adved3e26482002-10-13 00:18:57 +00001030 CreateMulInstruction(target, memInst->getParent()->getParent(),
Vikram S. Advebabc0fa2002-09-05 18:32:13 +00001031 idxVal, /* lval, not likely to be const*/
1032 eltSizeVal, /* rval, likely to be constant */
1033 addr, /* result */
Vikram S. Adved3e26482002-10-13 00:18:57 +00001034 mulVec, MachineCodeForInstruction::get(memInst),
Vikram S. Adveed3fefb2002-08-03 13:48:21 +00001035 INVALID_MACHINE_OPCODE);
1036
1037 // Insert mulVec[] before *mvecI in mvec[] and update mvecI
1038 // to point to the same instruction it pointed to before.
1039 assert(mulVec.size() > 0 && "No multiply code created?");
1040 vector<MachineInstr*>::iterator oldMvecI = mvecI;
1041 for (unsigned i=0, N=mulVec.size(); i < N; ++i)
1042 mvecI = mvec.insert(mvecI, mulVec[i]) + 1; // pts to mem instr
1043
1044 valueForRegOffset = addr;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001045 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00001046 }
1047 else
1048 {
1049 offsetOpType = MachineOperand::MO_SignExtendedImmed;
1050 smallConstOffset = 0;
1051 }
Vikram S. Adveed3fefb2002-08-03 13:48:21 +00001052
Vikram S. Advea10d1a72002-03-31 19:07:35 +00001053 // For STORE:
1054 // Operand 0 is value, operand 1 is ptr, operand 2 is offset
1055 // For LOAD or GET_ELEMENT_PTR,
1056 // Operand 0 is ptr, operand 1 is offset, operand 2 is result.
1057 //
1058 unsigned offsetOpNum, ptrOpNum;
1059 if (memInst->getOpcode() == Instruction::Store)
1060 {
1061 (*mvecI)->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister,
1062 vmInstrNode->leftChild()->getValue());
1063 ptrOpNum = 1;
1064 offsetOpNum = 2;
1065 }
1066 else
1067 {
1068 ptrOpNum = 0;
1069 offsetOpNum = 1;
1070 (*mvecI)->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister,
1071 memInst);
1072 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00001073
Vikram S. Advea10d1a72002-03-31 19:07:35 +00001074 (*mvecI)->SetMachineOperandVal(ptrOpNum, MachineOperand::MO_VirtualRegister,
1075 ptrVal);
1076
Chris Lattner20b1ea02001-09-14 03:47:57 +00001077 if (offsetOpType == MachineOperand::MO_VirtualRegister)
1078 {
1079 assert(valueForRegOffset != NULL);
Vikram S. Adve74825322002-03-18 03:15:35 +00001080 (*mvecI)->SetMachineOperandVal(offsetOpNum, offsetOpType,
1081 valueForRegOffset);
Chris Lattner20b1ea02001-09-14 03:47:57 +00001082 }
1083 else
Vikram S. Adve74825322002-03-18 03:15:35 +00001084 (*mvecI)->SetMachineOperandConst(offsetOpNum, offsetOpType,
1085 smallConstOffset);
Chris Lattner20b1ea02001-09-14 03:47:57 +00001086}
1087
1088
Chris Lattner20b1ea02001-09-14 03:47:57 +00001089//
1090// Substitute operand `operandNum' of the instruction in node `treeNode'
Vikram S. Advec025fc12001-10-14 23:28:43 +00001091// in place of the use(s) of that instruction in node `parent'.
1092// Check both explicit and implicit operands!
Vikram S. Adve74825322002-03-18 03:15:35 +00001093// Also make sure to skip over a parent who:
1094// (1) is a list node in the Burg tree, or
1095// (2) itself had its results forwarded to its parent
Chris Lattner20b1ea02001-09-14 03:47:57 +00001096//
1097static void
1098ForwardOperand(InstructionNode* treeNode,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001099 InstrTreeNode* parent,
1100 int operandNum)
Chris Lattner20b1ea02001-09-14 03:47:57 +00001101{
Vikram S. Adve243dd452001-09-18 13:03:13 +00001102 assert(treeNode && parent && "Invalid invocation of ForwardOperand");
1103
Chris Lattner20b1ea02001-09-14 03:47:57 +00001104 Instruction* unusedOp = treeNode->getInstruction();
1105 Value* fwdOp = unusedOp->getOperand(operandNum);
Vikram S. Adve243dd452001-09-18 13:03:13 +00001106
1107 // The parent itself may be a list node, so find the real parent instruction
1108 while (parent->getNodeType() != InstrTreeNode::NTInstructionNode)
1109 {
1110 parent = parent->parent();
1111 assert(parent && "ERROR: Non-instruction node has no parent in tree.");
1112 }
1113 InstructionNode* parentInstrNode = (InstructionNode*) parent;
1114
1115 Instruction* userInstr = parentInstrNode->getInstruction();
Chris Lattner9c461082002-02-03 07:50:56 +00001116 MachineCodeForInstruction &mvec = MachineCodeForInstruction::get(userInstr);
Vikram S. Adve74825322002-03-18 03:15:35 +00001117
1118 // The parent's mvec would be empty if it was itself forwarded.
1119 // Recursively call ForwardOperand in that case...
1120 //
1121 if (mvec.size() == 0)
Chris Lattner20b1ea02001-09-14 03:47:57 +00001122 {
Vikram S. Adve74825322002-03-18 03:15:35 +00001123 assert(parent->parent() != NULL &&
1124 "Parent could not have been forwarded, yet has no instructions?");
1125 ForwardOperand(treeNode, parent->parent(), operandNum);
1126 }
1127 else
1128 {
Vikram S. Adve74825322002-03-18 03:15:35 +00001129 for (unsigned i=0, N=mvec.size(); i < N; i++)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001130 {
Vikram S. Adve74825322002-03-18 03:15:35 +00001131 MachineInstr* minstr = mvec[i];
1132 for (unsigned i=0, numOps=minstr->getNumOperands(); i < numOps; ++i)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001133 {
Vikram S. Adve74825322002-03-18 03:15:35 +00001134 const MachineOperand& mop = minstr->getOperand(i);
Chris Lattner133f0792002-10-28 04:45:29 +00001135 if (mop.getType() == MachineOperand::MO_VirtualRegister &&
Vikram S. Adve74825322002-03-18 03:15:35 +00001136 mop.getVRegValue() == unusedOp)
Vikram S. Adve242a8082002-05-19 15:25:51 +00001137 minstr->SetMachineOperandVal(i,
Vikram S. Adve74825322002-03-18 03:15:35 +00001138 MachineOperand::MO_VirtualRegister, fwdOp);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001139 }
Vikram S. Adve74825322002-03-18 03:15:35 +00001140
1141 for (unsigned i=0,numOps=minstr->getNumImplicitRefs(); i<numOps; ++i)
1142 if (minstr->getImplicitRef(i) == unusedOp)
Vikram S. Adve242a8082002-05-19 15:25:51 +00001143 minstr->setImplicitRef(i, fwdOp,
Vikram S. Adve6418eac2002-07-08 23:30:14 +00001144 minstr->implicitRefIsDefined(i),
1145 minstr->implicitRefIsDefinedAndUsed(i));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001146 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00001147 }
1148}
1149
1150
Vikram S. Adve242a8082002-05-19 15:25:51 +00001151inline bool
1152AllUsesAreBranches(const Instruction* setccI)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001153{
Vikram S. Adve242a8082002-05-19 15:25:51 +00001154 for (Value::use_const_iterator UI=setccI->use_begin(), UE=setccI->use_end();
1155 UI != UE; ++UI)
1156 if (! isa<TmpInstruction>(*UI) // ignore tmp instructions here
1157 && cast<Instruction>(*UI)->getOpcode() != Instruction::Br)
1158 return false;
1159 return true;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001160}
1161
Vikram S. Advefb361122001-10-22 13:36:31 +00001162//******************* Externally Visible Functions *************************/
1163
Vikram S. Advefb361122001-10-22 13:36:31 +00001164//------------------------------------------------------------------------
1165// External Function: ThisIsAChainRule
1166//
1167// Purpose:
1168// Check if a given BURG rule is a chain rule.
1169//------------------------------------------------------------------------
1170
1171extern bool
1172ThisIsAChainRule(int eruleno)
1173{
1174 switch(eruleno)
1175 {
1176 case 111: // stmt: reg
Vikram S. Advefb361122001-10-22 13:36:31 +00001177 case 123:
1178 case 124:
1179 case 125:
1180 case 126:
1181 case 127:
1182 case 128:
1183 case 129:
1184 case 130:
1185 case 131:
1186 case 132:
1187 case 133:
1188 case 155:
1189 case 221:
1190 case 222:
1191 case 241:
1192 case 242:
1193 case 243:
1194 case 244:
Vikram S. Adve30a6f492002-08-22 02:56:10 +00001195 case 245:
Vikram S. Adve85e1e9c2002-04-01 20:28:48 +00001196 case 321:
Vikram S. Advefb361122001-10-22 13:36:31 +00001197 return true; break;
Vikram S. Adve30a6f492002-08-22 02:56:10 +00001198
Vikram S. Advefb361122001-10-22 13:36:31 +00001199 default:
1200 return false; break;
1201 }
1202}
Chris Lattner20b1ea02001-09-14 03:47:57 +00001203
1204
1205//------------------------------------------------------------------------
1206// External Function: GetInstructionsByRule
1207//
1208// Purpose:
1209// Choose machine instructions for the SPARC according to the
1210// patterns chosen by the BURG-generated parser.
1211//------------------------------------------------------------------------
1212
Vikram S. Adve74825322002-03-18 03:15:35 +00001213void
Chris Lattner20b1ea02001-09-14 03:47:57 +00001214GetInstructionsByRule(InstructionNode* subtreeRoot,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001215 int ruleForNode,
1216 short* nts,
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001217 TargetMachine &target,
Vikram S. Adve74825322002-03-18 03:15:35 +00001218 vector<MachineInstr*>& mvec)
Chris Lattner20b1ea02001-09-14 03:47:57 +00001219{
Chris Lattner20b1ea02001-09-14 03:47:57 +00001220 bool checkCast = false; // initialize here to use fall-through
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001221 bool maskUnsignedResult = false;
Chris Lattner20b1ea02001-09-14 03:47:57 +00001222 int nextRule;
1223 int forwardOperandNum = -1;
Chris Lattnerea45d7b2002-12-28 20:19:44 +00001224 unsigned allocaSize = 0;
Vikram S. Adve74825322002-03-18 03:15:35 +00001225 MachineInstr* M, *M2;
Chris Lattnerea45d7b2002-12-28 20:19:44 +00001226 unsigned L;
Vikram S. Adve74825322002-03-18 03:15:35 +00001227
1228 mvec.clear();
Chris Lattner20b1ea02001-09-14 03:47:57 +00001229
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001230 // If the code for this instruction was folded into the parent (user),
1231 // then do nothing!
1232 if (subtreeRoot->isFoldedIntoParent())
1233 return;
1234
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001235 //
1236 // Let's check for chain rules outside the switch so that we don't have
1237 // to duplicate the list of chain rule production numbers here again
1238 //
1239 if (ThisIsAChainRule(ruleForNode))
Chris Lattner20b1ea02001-09-14 03:47:57 +00001240 {
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001241 // Chain rules have a single nonterminal on the RHS.
1242 // Get the rule that matches the RHS non-terminal and use that instead.
1243 //
1244 assert(nts[0] && ! nts[1]
1245 && "A chain rule should have only one RHS non-terminal!");
1246 nextRule = burm_rule(subtreeRoot->state, nts[0]);
1247 nts = burm_nts[nextRule];
Vikram S. Adve74825322002-03-18 03:15:35 +00001248 GetInstructionsByRule(subtreeRoot, nextRule, nts, target, mvec);
Chris Lattner20b1ea02001-09-14 03:47:57 +00001249 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001250 else
Chris Lattner20b1ea02001-09-14 03:47:57 +00001251 {
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001252 switch(ruleForNode) {
1253 case 1: // stmt: Ret
1254 case 2: // stmt: RetValue(reg)
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001255 { // NOTE: Prepass of register allocation is responsible
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001256 // for moving return value to appropriate register.
1257 // Mark the return-address register as a hidden virtual reg.
Vikram S. Advea995e602001-10-11 04:23:19 +00001258 // Mark the return value register as an implicit ref of
1259 // the machine instruction.
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001260 // Finally put a NOP in the delay slot.
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001261 ReturnInst *returnInstr =
1262 cast<ReturnInst>(subtreeRoot->getInstruction());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001263 assert(returnInstr->getOpcode() == Instruction::Ret);
1264
Chris Lattner9c461082002-02-03 07:50:56 +00001265 Instruction* returnReg = new TmpInstruction(returnInstr);
1266 MachineCodeForInstruction::get(returnInstr).addTemp(returnReg);
Vikram S. Advefb361122001-10-22 13:36:31 +00001267
Vikram S. Adve74825322002-03-18 03:15:35 +00001268 M = new MachineInstr(JMPLRET);
Chris Lattner1c7907e2002-10-28 20:11:17 +00001269 M->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister,
1270 returnReg);
Vikram S. Adve74825322002-03-18 03:15:35 +00001271 M->SetMachineOperandConst(1,MachineOperand::MO_SignExtendedImmed,
Chris Lattner1c7907e2002-10-28 20:11:17 +00001272 (int64_t)8);
Vikram S. Adve74825322002-03-18 03:15:35 +00001273 M->SetMachineOperandReg(2, target.getRegInfo().getZeroRegNum());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001274
Vikram S. Advea995e602001-10-11 04:23:19 +00001275 if (returnInstr->getReturnValue() != NULL)
Vikram S. Adve74825322002-03-18 03:15:35 +00001276 M->addImplicitRef(returnInstr->getReturnValue());
Vikram S. Advea995e602001-10-11 04:23:19 +00001277
Vikram S. Adve74825322002-03-18 03:15:35 +00001278 mvec.push_back(M);
1279 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001280
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001281 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001282 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001283
1284 case 3: // stmt: Store(reg,reg)
1285 case 4: // stmt: Store(reg,ptrreg)
Vikram S. Adve74825322002-03-18 03:15:35 +00001286 mvec.push_back(new MachineInstr(
1287 ChooseStoreInstruction(
1288 subtreeRoot->leftChild()->getValue()->getType())));
Vikram S. Adve8cfffd32002-08-24 20:56:53 +00001289 SetOperandsForMemInstr(mvec, subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001290 break;
Chris Lattner20b1ea02001-09-14 03:47:57 +00001291
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001292 case 5: // stmt: BrUncond
Vikram S. Adve74825322002-03-18 03:15:35 +00001293 M = new MachineInstr(BA);
Vikram S. Adve6418eac2002-07-08 23:30:14 +00001294 M->SetMachineOperandVal(0, MachineOperand::MO_PCRelativeDisp,
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001295 cast<BranchInst>(subtreeRoot->getInstruction())->getSuccessor(0));
Vikram S. Adve74825322002-03-18 03:15:35 +00001296 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001297
1298 // delay slot
Vikram S. Adve74825322002-03-18 03:15:35 +00001299 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001300 break;
Chris Lattner20b1ea02001-09-14 03:47:57 +00001301
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001302 case 206: // stmt: BrCond(setCCconst)
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001303 { // setCCconst => boolean was computed with `%b = setCC type reg1 const'
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001304 // If the constant is ZERO, we can use the branch-on-integer-register
1305 // instructions and avoid the SUBcc instruction entirely.
1306 // Otherwise this is just the same as case 5, so just fall through.
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001307 //
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001308 InstrTreeNode* constNode = subtreeRoot->leftChild()->rightChild();
1309 assert(constNode &&
1310 constNode->getNodeType() ==InstrTreeNode::NTConstNode);
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001311 Constant *constVal = cast<Constant>(constNode->getValue());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001312 bool isValidConst;
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001313
Chris Lattner0c4e8862002-09-03 01:08:28 +00001314 if ((constVal->getType()->isInteger()
Chris Lattner9b625032002-05-06 16:15:30 +00001315 || isa<PointerType>(constVal->getType()))
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001316 && GetConstantValueAsSignedInt(constVal, isValidConst) == 0
1317 && isValidConst)
1318 {
1319 // That constant is a zero after all...
1320 // Use the left child of setCC as the first argument!
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001321 // Mark the setCC node so that no code is generated for it.
1322 InstructionNode* setCCNode = (InstructionNode*)
1323 subtreeRoot->leftChild();
1324 assert(setCCNode->getOpLabel() == SetCCOp);
1325 setCCNode->markFoldedIntoParent();
1326
1327 BranchInst* brInst=cast<BranchInst>(subtreeRoot->getInstruction());
1328
Vikram S. Adve74825322002-03-18 03:15:35 +00001329 M = new MachineInstr(ChooseBprInstruction(subtreeRoot));
1330 M->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister,
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001331 setCCNode->leftChild()->getValue());
Vikram S. Adve74825322002-03-18 03:15:35 +00001332 M->SetMachineOperandVal(1, MachineOperand::MO_PCRelativeDisp,
1333 brInst->getSuccessor(0));
1334 mvec.push_back(M);
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001335
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001336 // delay slot
Vikram S. Adve74825322002-03-18 03:15:35 +00001337 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001338
1339 // false branch
Vikram S. Adve74825322002-03-18 03:15:35 +00001340 M = new MachineInstr(BA);
Vikram S. Adve6418eac2002-07-08 23:30:14 +00001341 M->SetMachineOperandVal(0, MachineOperand::MO_PCRelativeDisp,
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001342 brInst->getSuccessor(1));
Vikram S. Adve74825322002-03-18 03:15:35 +00001343 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001344
1345 // delay slot
Vikram S. Adve74825322002-03-18 03:15:35 +00001346 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001347
1348 break;
1349 }
1350 // ELSE FALL THROUGH
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001351 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001352
Vikram S. Adve30a6f492002-08-22 02:56:10 +00001353 case 6: // stmt: BrCond(setCC)
1354 { // bool => boolean was computed with SetCC.
1355 // The branch to use depends on whether it is FP, signed, or unsigned.
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001356 // If it is an integer CC, we also need to find the unique
1357 // TmpInstruction representing that CC.
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001358 //
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001359 BranchInst* brInst = cast<BranchInst>(subtreeRoot->getInstruction());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001360 bool isFPBranch;
Vikram S. Adve74825322002-03-18 03:15:35 +00001361 M = new MachineInstr(ChooseBccInstruction(subtreeRoot, isFPBranch));
Vikram S. Adve30a6f492002-08-22 02:56:10 +00001362
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001363 Value* ccValue = GetTmpForCC(subtreeRoot->leftChild()->getValue(),
1364 brInst->getParent()->getParent(),
1365 isFPBranch? Type::FloatTy : Type::IntTy);
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001366
Vikram S. Adve74825322002-03-18 03:15:35 +00001367 M->SetMachineOperandVal(0, MachineOperand::MO_CCRegister, ccValue);
1368 M->SetMachineOperandVal(1, MachineOperand::MO_PCRelativeDisp,
1369 brInst->getSuccessor(0));
1370 mvec.push_back(M);
Vikram S. Adve30a6f492002-08-22 02:56:10 +00001371
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001372 // delay slot
Vikram S. Adve74825322002-03-18 03:15:35 +00001373 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adve30a6f492002-08-22 02:56:10 +00001374
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001375 // false branch
Vikram S. Adve74825322002-03-18 03:15:35 +00001376 M = new MachineInstr(BA);
Vikram S. Adve6418eac2002-07-08 23:30:14 +00001377 M->SetMachineOperandVal(0, MachineOperand::MO_PCRelativeDisp,
Vikram S. Adve74825322002-03-18 03:15:35 +00001378 brInst->getSuccessor(1));
1379 mvec.push_back(M);
Vikram S. Adve30a6f492002-08-22 02:56:10 +00001380
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001381 // delay slot
Vikram S. Adve74825322002-03-18 03:15:35 +00001382 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001383 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001384 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001385
1386 case 208: // stmt: BrCond(boolconst)
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001387 {
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001388 // boolconst => boolean is a constant; use BA to first or second label
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001389 Constant* constVal =
1390 cast<Constant>(subtreeRoot->leftChild()->getValue());
1391 unsigned dest = cast<ConstantBool>(constVal)->getValue()? 0 : 1;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001392
Vikram S. Adve74825322002-03-18 03:15:35 +00001393 M = new MachineInstr(BA);
Vikram S. Adve6418eac2002-07-08 23:30:14 +00001394 M->SetMachineOperandVal(0, MachineOperand::MO_PCRelativeDisp,
Chris Lattner35504202002-04-27 03:14:39 +00001395 cast<BranchInst>(subtreeRoot->getInstruction())->getSuccessor(dest));
Vikram S. Adve74825322002-03-18 03:15:35 +00001396 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001397
1398 // delay slot
Vikram S. Adve74825322002-03-18 03:15:35 +00001399 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001400 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001401 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001402
1403 case 8: // stmt: BrCond(boolreg)
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001404 { // boolreg => boolean is stored in an existing register.
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001405 // Just use the branch-on-integer-register instruction!
1406 //
Vikram S. Adve74825322002-03-18 03:15:35 +00001407 M = new MachineInstr(BRNZ);
1408 M->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001409 subtreeRoot->leftChild()->getValue());
Vikram S. Adve74825322002-03-18 03:15:35 +00001410 M->SetMachineOperandVal(1, MachineOperand::MO_PCRelativeDisp,
Chris Lattner35504202002-04-27 03:14:39 +00001411 cast<BranchInst>(subtreeRoot->getInstruction())->getSuccessor(0));
Vikram S. Adve74825322002-03-18 03:15:35 +00001412 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001413
1414 // delay slot
Vikram S. Adve74825322002-03-18 03:15:35 +00001415 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001416
1417 // false branch
Vikram S. Adve74825322002-03-18 03:15:35 +00001418 M = new MachineInstr(BA);
Vikram S. Adve6418eac2002-07-08 23:30:14 +00001419 M->SetMachineOperandVal(0, MachineOperand::MO_PCRelativeDisp,
Chris Lattner35504202002-04-27 03:14:39 +00001420 cast<BranchInst>(subtreeRoot->getInstruction())->getSuccessor(1));
Vikram S. Adve74825322002-03-18 03:15:35 +00001421 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001422
1423 // delay slot
Vikram S. Adve74825322002-03-18 03:15:35 +00001424 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001425 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001426 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00001427
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001428 case 9: // stmt: Switch(reg)
1429 assert(0 && "*** SWITCH instruction is not implemented yet.");
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001430 break;
Chris Lattner20b1ea02001-09-14 03:47:57 +00001431
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001432 case 10: // reg: VRegList(reg, reg)
1433 assert(0 && "VRegList should never be the topmost non-chain rule");
1434 break;
1435
Vikram S. Advece08e1d2002-08-15 14:17:37 +00001436 case 21: // bool: Not(bool,reg): Both these are implemented as:
1437 case 421: // reg: BNot(reg,reg): reg = reg XOR-NOT 0
1438 { // First find the unary operand. It may be left or right, usually right.
1439 Value* notArg = BinaryOperator::getNotArgument(
1440 cast<BinaryOperator>(subtreeRoot->getInstruction()));
1441 mvec.push_back(Create3OperandInstr_Reg(XNOR, notArg,
1442 target.getRegInfo().getZeroRegNum(),
1443 subtreeRoot->getValue()));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001444 break;
Vikram S. Advece08e1d2002-08-15 14:17:37 +00001445 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001446
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001447 case 22: // reg: ToBoolTy(reg):
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001448 {
1449 const Type* opType = subtreeRoot->leftChild()->getValue()->getType();
Chris Lattner0c4e8862002-09-03 01:08:28 +00001450 assert(opType->isIntegral() || isa<PointerType>(opType));
Vikram S. Adve74825322002-03-18 03:15:35 +00001451 forwardOperandNum = 0; // forward first operand to user
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001452 break;
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001453 }
1454
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001455 case 23: // reg: ToUByteTy(reg)
Vikram S. Adve94c40812002-09-27 14:33:08 +00001456 case 24: // reg: ToSByteTy(reg)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001457 case 25: // reg: ToUShortTy(reg)
Vikram S. Adve94c40812002-09-27 14:33:08 +00001458 case 26: // reg: ToShortTy(reg)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001459 case 27: // reg: ToUIntTy(reg)
Vikram S. Adve94c40812002-09-27 14:33:08 +00001460 case 28: // reg: ToIntTy(reg)
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001461 {
Vikram S. Adve94c40812002-09-27 14:33:08 +00001462 //======================================================================
1463 // Rules for integer conversions:
1464 //
1465 //--------
1466 // From ISO 1998 C++ Standard, Sec. 4.7:
1467 //
1468 // 2. If the destination type is unsigned, the resulting value is
1469 // the least unsigned integer congruent to the source integer
1470 // (modulo 2n where n is the number of bits used to represent the
1471 // unsigned type). [Note: In a two s complement representation,
1472 // this conversion is conceptual and there is no change in the
1473 // bit pattern (if there is no truncation). ]
1474 //
1475 // 3. If the destination type is signed, the value is unchanged if
1476 // it can be represented in the destination type (and bitfield width);
1477 // otherwise, the value is implementation-defined.
1478 //--------
1479 //
1480 // Since we assume 2s complement representations, this implies:
1481 //
1482 // -- if operand is smaller than destination, zero-extend or sign-extend
1483 // according to the signedness of the *operand*: source decides.
1484 // ==> we have to do nothing here!
1485 //
1486 // -- if operand is same size as or larger than destination, and the
1487 // destination is *unsigned*, zero-extend the operand: dest. decides
1488 //
1489 // -- if operand is same size as or larger than destination, and the
1490 // destination is *signed*, the choice is implementation defined:
1491 // we sign-extend the operand: i.e., again dest. decides.
1492 // Note: this matches both Sun's cc and gcc3.2.
1493 //======================================================================
1494
Vikram S. Adve242a8082002-05-19 15:25:51 +00001495 Instruction* destI = subtreeRoot->getInstruction();
1496 Value* opVal = subtreeRoot->leftChild()->getValue();
Vikram S. Adve94c40812002-09-27 14:33:08 +00001497 const Type* opType = opVal->getType();
Chris Lattner0c4e8862002-09-03 01:08:28 +00001498 if (opType->isIntegral() || isa<PointerType>(opType))
Vikram S. Adve1e606692002-07-31 21:01:34 +00001499 {
Chris Lattnerea45d7b2002-12-28 20:19:44 +00001500 unsigned opSize = target.getTargetData().getTypeSize(opType);
1501 unsigned destSize = target.getTargetData().getTypeSize(destI->getType());
Vikram S. Adve94c40812002-09-27 14:33:08 +00001502 if (opSize >= destSize)
1503 { // Operand is same size as or larger than dest:
1504 // zero- or sign-extend, according to the signeddness of
1505 // the destination (see above).
1506 if (destI->getType()->isSigned())
1507 target.getInstrInfo().CreateSignExtensionInstructions(target,
1508 destI->getParent()->getParent(), opVal, destI, 8*destSize,
1509 mvec, MachineCodeForInstruction::get(destI));
1510 else
1511 target.getInstrInfo().CreateZeroExtensionInstructions(target,
1512 destI->getParent()->getParent(), opVal, destI, 8*destSize,
1513 mvec, MachineCodeForInstruction::get(destI));
Vikram S. Adve1e606692002-07-31 21:01:34 +00001514 }
1515 else
1516 forwardOperandNum = 0; // forward first operand to user
Vikram S. Adve242a8082002-05-19 15:25:51 +00001517 }
Vikram S. Adve1e606692002-07-31 21:01:34 +00001518 else if (opType->isFloatingPoint())
Vikram S. Adve8cfffd32002-08-24 20:56:53 +00001519 {
1520 CreateCodeToConvertFloatToInt(target, opVal, destI, mvec,
1521 MachineCodeForInstruction::get(destI));
Vikram S. Adve94c40812002-09-27 14:33:08 +00001522 if (destI->getType()->isUnsigned())
1523 maskUnsignedResult = true; // not handled by fp->int code
Vikram S. Adve8cfffd32002-08-24 20:56:53 +00001524 }
Vikram S. Adve242a8082002-05-19 15:25:51 +00001525 else
Vikram S. Adve1e606692002-07-31 21:01:34 +00001526 assert(0 && "Unrecognized operand type for convert-to-unsigned");
1527
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001528 break;
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001529 }
Vikram S. Adve94c40812002-09-27 14:33:08 +00001530
1531 case 29: // reg: ToULongTy(reg)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001532 case 30: // reg: ToLongTy(reg)
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001533 {
Vikram S. Adve242a8082002-05-19 15:25:51 +00001534 Value* opVal = subtreeRoot->leftChild()->getValue();
Vikram S. Adve242a8082002-05-19 15:25:51 +00001535 const Type* opType = opVal->getType();
Chris Lattner0c4e8862002-09-03 01:08:28 +00001536 if (opType->isIntegral() || isa<PointerType>(opType))
Vikram S. Adve94c40812002-09-27 14:33:08 +00001537 forwardOperandNum = 0; // forward first operand to user
Vikram S. Adve1e606692002-07-31 21:01:34 +00001538 else if (opType->isFloatingPoint())
Vikram S. Adve94c40812002-09-27 14:33:08 +00001539 {
1540 Instruction* destI = subtreeRoot->getInstruction();
1541 CreateCodeToConvertFloatToInt(target, opVal, destI, mvec,
1542 MachineCodeForInstruction::get(destI));
1543 }
Vikram S. Adve1e606692002-07-31 21:01:34 +00001544 else
1545 assert(0 && "Unrecognized operand type for convert-to-signed");
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001546 break;
Vikram S. Adve94c40812002-09-27 14:33:08 +00001547 }
1548
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001549 case 31: // reg: ToFloatTy(reg):
1550 case 32: // reg: ToDoubleTy(reg):
1551 case 232: // reg: ToDoubleTy(Constant):
Vikram S. Adveec7f4822002-09-09 14:54:21 +00001552
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001553 // If this instruction has a parent (a user) in the tree
1554 // and the user is translated as an FsMULd instruction,
1555 // then the cast is unnecessary. So check that first.
1556 // In the future, we'll want to do the same for the FdMULq instruction,
1557 // so do the check here instead of only for ToFloatTy(reg).
1558 //
Vikram S. Adveec7f4822002-09-09 14:54:21 +00001559 if (subtreeRoot->parent() != NULL)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001560 {
Vikram S. Adveec7f4822002-09-09 14:54:21 +00001561 const MachineCodeForInstruction& mcfi =
1562 MachineCodeForInstruction::get(
1563 cast<InstructionNode>(subtreeRoot->parent())->getInstruction());
1564 if (mcfi.size() == 0 || mcfi.front()->getOpCode() == FSMULD)
1565 forwardOperandNum = 0; // forward first operand to user
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001566 }
Vikram S. Adveec7f4822002-09-09 14:54:21 +00001567
1568 if (forwardOperandNum != 0) // we do need the cast
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001569 {
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001570 Value* leftVal = subtreeRoot->leftChild()->getValue();
1571 const Type* opType = leftVal->getType();
Vikram S. Advedbc4fad2002-04-25 04:37:51 +00001572 MachineOpCode opCode=ChooseConvertToFloatInstr(
1573 subtreeRoot->getOpLabel(), opType);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001574 if (opCode == INVALID_OPCODE) // no conversion needed
1575 {
Vikram S. Adve74825322002-03-18 03:15:35 +00001576 forwardOperandNum = 0; // forward first operand to user
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001577 }
1578 else
1579 {
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001580 // If the source operand is a non-FP type it must be
1581 // first copied from int to float register via memory!
1582 Instruction *dest = subtreeRoot->getInstruction();
1583 Value* srcForCast;
1584 int n = 0;
Vikram S. Adve242a8082002-05-19 15:25:51 +00001585 if (! opType->isFloatingPoint())
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001586 {
1587 // Create a temporary to represent the FP register
1588 // into which the integer will be copied via memory.
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001589 // The type of this temporary will determine the FP
1590 // register used: single-prec for a 32-bit int or smaller,
1591 // double-prec for a 64-bit int.
1592 //
Vikram S. Advebabc0fa2002-09-05 18:32:13 +00001593 uint64_t srcSize =
Chris Lattnerea45d7b2002-12-28 20:19:44 +00001594 target.getTargetData().getTypeSize(leftVal->getType());
Vikram S. Advebabc0fa2002-09-05 18:32:13 +00001595 Type* tmpTypeToUse =
1596 (srcSize <= 4)? Type::FloatTy : Type::DoubleTy;
1597 srcForCast = new TmpInstruction(tmpTypeToUse, dest);
Vikram S. Advedbc4fad2002-04-25 04:37:51 +00001598 MachineCodeForInstruction &destMCFI =
Chris Lattner9c461082002-02-03 07:50:56 +00001599 MachineCodeForInstruction::get(dest);
Vikram S. Advedbc4fad2002-04-25 04:37:51 +00001600 destMCFI.addTemp(srcForCast);
Vikram S. Advebabc0fa2002-09-05 18:32:13 +00001601
Vikram S. Adve242a8082002-05-19 15:25:51 +00001602 target.getInstrInfo().CreateCodeToCopyIntToFloat(target,
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001603 dest->getParent()->getParent(),
Vikram S. Advebabc0fa2002-09-05 18:32:13 +00001604 leftVal, cast<Instruction>(srcForCast),
Vikram S. Adve242a8082002-05-19 15:25:51 +00001605 mvec, destMCFI);
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001606 }
1607 else
1608 srcForCast = leftVal;
Vikram S. Adve94c40812002-09-27 14:33:08 +00001609
Vikram S. Adve74825322002-03-18 03:15:35 +00001610 M = new MachineInstr(opCode);
1611 M->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister,
1612 srcForCast);
1613 M->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister,
1614 dest);
1615 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001616 }
1617 }
1618 break;
1619
1620 case 19: // reg: ToArrayTy(reg):
1621 case 20: // reg: ToPointerTy(reg):
Vikram S. Adve74825322002-03-18 03:15:35 +00001622 forwardOperandNum = 0; // forward first operand to user
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001623 break;
1624
1625 case 233: // reg: Add(reg, Constant)
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001626 maskUnsignedResult = true;
Vikram S. Adve74825322002-03-18 03:15:35 +00001627 M = CreateAddConstInstruction(subtreeRoot);
1628 if (M != NULL)
1629 {
1630 mvec.push_back(M);
1631 break;
1632 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001633 // ELSE FALL THROUGH
Vikram S. Adve74825322002-03-18 03:15:35 +00001634
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001635 case 33: // reg: Add(reg, reg)
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001636 maskUnsignedResult = true;
Vikram S. Adve74825322002-03-18 03:15:35 +00001637 mvec.push_back(new MachineInstr(ChooseAddInstruction(subtreeRoot)));
1638 Set3OperandsFromInstr(mvec.back(), subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001639 break;
1640
1641 case 234: // reg: Sub(reg, Constant)
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001642 maskUnsignedResult = true;
Vikram S. Adve74825322002-03-18 03:15:35 +00001643 M = CreateSubConstInstruction(subtreeRoot);
1644 if (M != NULL)
1645 {
1646 mvec.push_back(M);
1647 break;
1648 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001649 // ELSE FALL THROUGH
Vikram S. Adve74825322002-03-18 03:15:35 +00001650
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001651 case 34: // reg: Sub(reg, reg)
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001652 maskUnsignedResult = true;
Vikram S. Adve74825322002-03-18 03:15:35 +00001653 mvec.push_back(new MachineInstr(ChooseSubInstructionByType(
1654 subtreeRoot->getInstruction()->getType())));
1655 Set3OperandsFromInstr(mvec.back(), subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001656 break;
1657
1658 case 135: // reg: Mul(todouble, todouble)
1659 checkCast = true;
1660 // FALL THROUGH
1661
1662 case 35: // reg: Mul(reg, reg)
Vikram S. Adve74825322002-03-18 03:15:35 +00001663 {
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001664 maskUnsignedResult = true;
Vikram S. Adve74825322002-03-18 03:15:35 +00001665 MachineOpCode forceOp = ((checkCast && BothFloatToDouble(subtreeRoot))
1666 ? FSMULD
1667 : INVALID_MACHINE_OPCODE);
Vikram S. Adve242a8082002-05-19 15:25:51 +00001668 Instruction* mulInstr = subtreeRoot->getInstruction();
1669 CreateMulInstruction(target, mulInstr->getParent()->getParent(),
Vikram S. Adve74825322002-03-18 03:15:35 +00001670 subtreeRoot->leftChild()->getValue(),
1671 subtreeRoot->rightChild()->getValue(),
Vikram S. Adve242a8082002-05-19 15:25:51 +00001672 mulInstr, mvec,
1673 MachineCodeForInstruction::get(mulInstr),forceOp);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001674 break;
Vikram S. Adve74825322002-03-18 03:15:35 +00001675 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001676 case 335: // reg: Mul(todouble, todoubleConst)
1677 checkCast = true;
1678 // FALL THROUGH
1679
1680 case 235: // reg: Mul(reg, Constant)
Vikram S. Adve74825322002-03-18 03:15:35 +00001681 {
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001682 maskUnsignedResult = true;
Vikram S. Adve74825322002-03-18 03:15:35 +00001683 MachineOpCode forceOp = ((checkCast && BothFloatToDouble(subtreeRoot))
1684 ? FSMULD
1685 : INVALID_MACHINE_OPCODE);
Vikram S. Adve242a8082002-05-19 15:25:51 +00001686 Instruction* mulInstr = subtreeRoot->getInstruction();
1687 CreateMulInstruction(target, mulInstr->getParent()->getParent(),
Vikram S. Adve74825322002-03-18 03:15:35 +00001688 subtreeRoot->leftChild()->getValue(),
1689 subtreeRoot->rightChild()->getValue(),
Vikram S. Adve242a8082002-05-19 15:25:51 +00001690 mulInstr, mvec,
1691 MachineCodeForInstruction::get(mulInstr),
1692 forceOp);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001693 break;
Vikram S. Adve74825322002-03-18 03:15:35 +00001694 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001695 case 236: // reg: Div(reg, Constant)
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001696 maskUnsignedResult = true;
Vikram S. Adve74825322002-03-18 03:15:35 +00001697 L = mvec.size();
1698 CreateDivConstInstruction(target, subtreeRoot, mvec);
1699 if (mvec.size() > L)
1700 break;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001701 // ELSE FALL THROUGH
Vikram S. Adve74825322002-03-18 03:15:35 +00001702
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001703 case 36: // reg: Div(reg, reg)
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001704 maskUnsignedResult = true;
Chris Lattnerea45d7b2002-12-28 20:19:44 +00001705 mvec.push_back(new MachineInstr(ChooseDivInstruction(target,
1706 subtreeRoot)));
Vikram S. Adve74825322002-03-18 03:15:35 +00001707 Set3OperandsFromInstr(mvec.back(), subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001708 break;
1709
1710 case 37: // reg: Rem(reg, reg)
1711 case 237: // reg: Rem(reg, Constant)
Vikram S. Adve510eec72001-11-04 21:59:14 +00001712 {
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001713 maskUnsignedResult = true;
Vikram S. Adve510eec72001-11-04 21:59:14 +00001714 Instruction* remInstr = subtreeRoot->getInstruction();
1715
Chris Lattner9c461082002-02-03 07:50:56 +00001716 TmpInstruction* quot = new TmpInstruction(
Vikram S. Adve510eec72001-11-04 21:59:14 +00001717 subtreeRoot->leftChild()->getValue(),
1718 subtreeRoot->rightChild()->getValue());
Chris Lattner9c461082002-02-03 07:50:56 +00001719 TmpInstruction* prod = new TmpInstruction(
Vikram S. Adve510eec72001-11-04 21:59:14 +00001720 quot,
1721 subtreeRoot->rightChild()->getValue());
Chris Lattner9c461082002-02-03 07:50:56 +00001722 MachineCodeForInstruction::get(remInstr).addTemp(quot).addTemp(prod);
Vikram S. Adve510eec72001-11-04 21:59:14 +00001723
Vikram S. Adve74825322002-03-18 03:15:35 +00001724 M = new MachineInstr(ChooseDivInstruction(target, subtreeRoot));
1725 Set3OperandsFromInstr(M, subtreeRoot, target);
1726 M->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister,quot);
1727 mvec.push_back(M);
Vikram S. Adve510eec72001-11-04 21:59:14 +00001728
Chris Lattnere5b1ed92003-01-15 00:03:28 +00001729 unsigned MulOpcode =
1730 ChooseMulInstructionByType(subtreeRoot->getInstruction()->getType());
1731 Value *MulRHS = subtreeRoot->rightChild()->getValue();
1732 M = BuildMI(MulOpcode, 3).addReg(quot).addReg(MulRHS).addReg(prod,
1733 MOTy::Def);
Vikram S. Adve74825322002-03-18 03:15:35 +00001734 mvec.push_back(M);
Vikram S. Adve510eec72001-11-04 21:59:14 +00001735
Vikram S. Adve74825322002-03-18 03:15:35 +00001736 M = new MachineInstr(ChooseSubInstructionByType(
Vikram S. Adve510eec72001-11-04 21:59:14 +00001737 subtreeRoot->getInstruction()->getType()));
Vikram S. Adve74825322002-03-18 03:15:35 +00001738 Set3OperandsFromInstr(M, subtreeRoot, target);
1739 M->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister,prod);
1740 mvec.push_back(M);
Vikram S. Adve510eec72001-11-04 21:59:14 +00001741
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001742 break;
Vikram S. Adve510eec72001-11-04 21:59:14 +00001743 }
1744
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001745 case 38: // bool: And(bool, bool)
1746 case 238: // bool: And(bool, boolconst)
1747 case 338: // reg : BAnd(reg, reg)
1748 case 538: // reg : BAnd(reg, Constant)
Vikram S. Adve74825322002-03-18 03:15:35 +00001749 mvec.push_back(new MachineInstr(AND));
1750 Set3OperandsFromInstr(mvec.back(), subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001751 break;
1752
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001753 case 138: // bool: And(bool, not)
Vikram S. Advece08e1d2002-08-15 14:17:37 +00001754 case 438: // bool: BAnd(bool, bnot)
1755 { // Use the argument of NOT as the second argument!
1756 // Mark the NOT node so that no code is generated for it.
1757 InstructionNode* notNode = (InstructionNode*) subtreeRoot->rightChild();
1758 Value* notArg = BinaryOperator::getNotArgument(
1759 cast<BinaryOperator>(notNode->getInstruction()));
1760 notNode->markFoldedIntoParent();
Chris Lattnere5b1ed92003-01-15 00:03:28 +00001761 Value *LHS = subtreeRoot->leftChild()->getValue();
1762 Value *Dest = subtreeRoot->getValue();
1763 mvec.push_back(BuildMI(ANDN, 3).addReg(LHS).addReg(notArg)
1764 .addReg(Dest, MOTy::Def));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001765 break;
Vikram S. Advece08e1d2002-08-15 14:17:37 +00001766 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001767
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001768 case 39: // bool: Or(bool, bool)
1769 case 239: // bool: Or(bool, boolconst)
1770 case 339: // reg : BOr(reg, reg)
1771 case 539: // reg : BOr(reg, Constant)
Vikram S. Adve242a8082002-05-19 15:25:51 +00001772 mvec.push_back(new MachineInstr(OR));
Vikram S. Adve74825322002-03-18 03:15:35 +00001773 Set3OperandsFromInstr(mvec.back(), subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001774 break;
1775
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001776 case 139: // bool: Or(bool, not)
Vikram S. Advece08e1d2002-08-15 14:17:37 +00001777 case 439: // bool: BOr(bool, bnot)
1778 { // Use the argument of NOT as the second argument!
1779 // Mark the NOT node so that no code is generated for it.
1780 InstructionNode* notNode = (InstructionNode*) subtreeRoot->rightChild();
1781 Value* notArg = BinaryOperator::getNotArgument(
1782 cast<BinaryOperator>(notNode->getInstruction()));
1783 notNode->markFoldedIntoParent();
Chris Lattnere5b1ed92003-01-15 00:03:28 +00001784 Value *LHS = subtreeRoot->leftChild()->getValue();
1785 Value *Dest = subtreeRoot->getValue();
1786 mvec.push_back(BuildMI(ORN, 3).addReg(LHS).addReg(notArg)
1787 .addReg(Dest, MOTy::Def));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001788 break;
Vikram S. Advece08e1d2002-08-15 14:17:37 +00001789 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001790
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001791 case 40: // bool: Xor(bool, bool)
1792 case 240: // bool: Xor(bool, boolconst)
1793 case 340: // reg : BXor(reg, reg)
1794 case 540: // reg : BXor(reg, Constant)
Vikram S. Adve74825322002-03-18 03:15:35 +00001795 mvec.push_back(new MachineInstr(XOR));
1796 Set3OperandsFromInstr(mvec.back(), subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001797 break;
1798
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001799 case 140: // bool: Xor(bool, not)
Vikram S. Advece08e1d2002-08-15 14:17:37 +00001800 case 440: // bool: BXor(bool, bnot)
1801 { // Use the argument of NOT as the second argument!
1802 // Mark the NOT node so that no code is generated for it.
1803 InstructionNode* notNode = (InstructionNode*) subtreeRoot->rightChild();
1804 Value* notArg = BinaryOperator::getNotArgument(
1805 cast<BinaryOperator>(notNode->getInstruction()));
1806 notNode->markFoldedIntoParent();
Chris Lattnere5b1ed92003-01-15 00:03:28 +00001807 Value *LHS = subtreeRoot->leftChild()->getValue();
1808 Value *Dest = subtreeRoot->getValue();
1809 mvec.push_back(BuildMI(XNOR, 3).addReg(LHS).addReg(notArg)
1810 .addReg(Dest, MOTy::Def));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001811 break;
Vikram S. Advece08e1d2002-08-15 14:17:37 +00001812 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001813
1814 case 41: // boolconst: SetCC(reg, Constant)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001815 //
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001816 // If the SetCC was folded into the user (parent), it will be
1817 // caught above. All other cases are the same as case 42,
1818 // so just fall through.
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001819 //
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001820 case 42: // bool: SetCC(reg, reg):
1821 {
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001822 // This generates a SUBCC instruction, putting the difference in
1823 // a result register, and setting a condition code.
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001824 //
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001825 // If the boolean result of the SetCC is used by anything other
Vikram S. Adve6418eac2002-07-08 23:30:14 +00001826 // than a branch instruction, or if it is used outside the current
1827 // basic block, the boolean must be
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001828 // computed and stored in the result register. Otherwise, discard
1829 // the difference (by using %g0) and keep only the condition code.
1830 //
1831 // To compute the boolean result in a register we use a conditional
1832 // move, unless the result of the SUBCC instruction can be used as
1833 // the bool! This assumes that zero is FALSE and any non-zero
1834 // integer is TRUE.
1835 //
1836 InstructionNode* parentNode = (InstructionNode*) subtreeRoot->parent();
1837 Instruction* setCCInstr = subtreeRoot->getInstruction();
Vikram S. Adve242a8082002-05-19 15:25:51 +00001838
Vikram S. Adve6418eac2002-07-08 23:30:14 +00001839 bool keepBoolVal = parentNode == NULL ||
1840 ! AllUsesAreBranches(setCCInstr);
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001841 bool subValIsBoolVal = setCCInstr->getOpcode() == Instruction::SetNE;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001842 bool keepSubVal = keepBoolVal && subValIsBoolVal;
1843 bool computeBoolVal = keepBoolVal && ! subValIsBoolVal;
1844
1845 bool mustClearReg;
1846 int valueToMove;
Chris Lattner8e5c0b42001-11-07 14:01:59 +00001847 MachineOpCode movOpCode = 0;
Vikram S. Adve6418eac2002-07-08 23:30:14 +00001848
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001849 // Mark the 4th operand as being a CC register, and as a def
1850 // A TmpInstruction is created to represent the CC "result".
1851 // Unlike other instances of TmpInstruction, this one is used
1852 // by machine code of multiple LLVM instructions, viz.,
1853 // the SetCC and the branch. Make sure to get the same one!
1854 // Note that we do this even for FP CC registers even though they
1855 // are explicit operands, because the type of the operand
1856 // needs to be a floating point condition code, not an integer
1857 // condition code. Think of this as casting the bool result to
1858 // a FP condition code register.
1859 //
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001860 Value* leftVal = subtreeRoot->leftChild()->getValue();
Chris Lattner9b625032002-05-06 16:15:30 +00001861 bool isFPCompare = leftVal->getType()->isFloatingPoint();
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001862
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001863 TmpInstruction* tmpForCC = GetTmpForCC(setCCInstr,
1864 setCCInstr->getParent()->getParent(),
Chris Lattner9b625032002-05-06 16:15:30 +00001865 isFPCompare ? Type::FloatTy : Type::IntTy);
Chris Lattner9c461082002-02-03 07:50:56 +00001866 MachineCodeForInstruction::get(setCCInstr).addTemp(tmpForCC);
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001867
1868 if (! isFPCompare)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001869 {
1870 // Integer condition: dest. should be %g0 or an integer register.
1871 // If result must be saved but condition is not SetEQ then we need
1872 // a separate instruction to compute the bool result, so discard
1873 // result of SUBcc instruction anyway.
1874 //
Vikram S. Adve74825322002-03-18 03:15:35 +00001875 M = new MachineInstr(SUBcc);
1876 Set3OperandsFromInstr(M, subtreeRoot, target, ! keepSubVal);
1877 M->SetMachineOperandVal(3, MachineOperand::MO_CCRegister,
1878 tmpForCC, /*def*/true);
1879 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001880
1881 if (computeBoolVal)
1882 { // recompute bool using the integer condition codes
1883 movOpCode =
1884 ChooseMovpccAfterSub(subtreeRoot,mustClearReg,valueToMove);
1885 }
1886 }
1887 else
1888 {
1889 // FP condition: dest of FCMP should be some FCCn register
Vikram S. Adve74825322002-03-18 03:15:35 +00001890 M = new MachineInstr(ChooseFcmpInstruction(subtreeRoot));
1891 M->SetMachineOperandVal(0, MachineOperand::MO_CCRegister,
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001892 tmpForCC);
Vikram S. Adve74825322002-03-18 03:15:35 +00001893 M->SetMachineOperandVal(1,MachineOperand::MO_VirtualRegister,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001894 subtreeRoot->leftChild()->getValue());
Vikram S. Adve74825322002-03-18 03:15:35 +00001895 M->SetMachineOperandVal(2,MachineOperand::MO_VirtualRegister,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001896 subtreeRoot->rightChild()->getValue());
Vikram S. Adve74825322002-03-18 03:15:35 +00001897 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001898
1899 if (computeBoolVal)
1900 {// recompute bool using the FP condition codes
1901 mustClearReg = true;
1902 valueToMove = 1;
1903 movOpCode = ChooseMovFpccInstruction(subtreeRoot);
1904 }
1905 }
1906
1907 if (computeBoolVal)
1908 {
1909 if (mustClearReg)
1910 {// Unconditionally set register to 0
Vikram S. Adve74825322002-03-18 03:15:35 +00001911 M = new MachineInstr(SETHI);
1912 M->SetMachineOperandConst(0,MachineOperand::MO_UnextendedImmed,
1913 (int64_t)0);
1914 M->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister,
1915 setCCInstr);
1916 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001917 }
1918
1919 // Now conditionally move `valueToMove' (0 or 1) into the register
Vikram S. Adve6418eac2002-07-08 23:30:14 +00001920 // Mark the register as a use (as well as a def) because the old
1921 // value should be retained if the condition is false.
Vikram S. Adve74825322002-03-18 03:15:35 +00001922 M = new MachineInstr(movOpCode);
1923 M->SetMachineOperandVal(0, MachineOperand::MO_CCRegister,
1924 tmpForCC);
1925 M->SetMachineOperandConst(1, MachineOperand::MO_UnextendedImmed,
1926 valueToMove);
1927 M->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister,
Vikram S. Adve6418eac2002-07-08 23:30:14 +00001928 setCCInstr, /*isDef*/ true,
1929 /*isDefAndUse*/ true);
Vikram S. Adve74825322002-03-18 03:15:35 +00001930 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001931 }
1932 break;
1933 }
1934
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001935 case 51: // reg: Load(reg)
1936 case 52: // reg: Load(ptrreg)
Vikram S. Adve74825322002-03-18 03:15:35 +00001937 mvec.push_back(new MachineInstr(ChooseLoadInstruction(
1938 subtreeRoot->getValue()->getType())));
Vikram S. Adve8cfffd32002-08-24 20:56:53 +00001939 SetOperandsForMemInstr(mvec, subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001940 break;
1941
1942 case 55: // reg: GetElemPtr(reg)
1943 case 56: // reg: GetElemPtrIdx(reg,reg)
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001944 // If the GetElemPtr was folded into the user (parent), it will be
1945 // caught above. For other cases, we have to compute the address.
Vikram S. Adve74825322002-03-18 03:15:35 +00001946 mvec.push_back(new MachineInstr(ADD));
Vikram S. Adve8cfffd32002-08-24 20:56:53 +00001947 SetOperandsForMemInstr(mvec, subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001948 break;
Vikram S. Adved3e26482002-10-13 00:18:57 +00001949
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001950 case 57: // reg: Alloca: Implement as 1 instruction:
1951 { // add %fp, offsetFromFP -> result
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001952 AllocationInst* instr =
1953 cast<AllocationInst>(subtreeRoot->getInstruction());
Chris Lattnerea45d7b2002-12-28 20:19:44 +00001954 unsigned tsize =
1955 target.getTargetData().getTypeSize(instr->getAllocatedType());
Vikram S. Adve74825322002-03-18 03:15:35 +00001956 assert(tsize != 0);
1957 CreateCodeForFixedSizeAlloca(target, instr, tsize, 1, mvec);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001958 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001959 }
Vikram S. Adved3e26482002-10-13 00:18:57 +00001960
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001961 case 58: // reg: Alloca(reg): Implement as 3 instructions:
1962 // mul num, typeSz -> tmp
1963 // sub %sp, tmp -> %sp
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001964 { // add %sp, frameSizeBelowDynamicArea -> result
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001965 AllocationInst* instr =
1966 cast<AllocationInst>(subtreeRoot->getInstruction());
Vikram S. Adve74825322002-03-18 03:15:35 +00001967 const Type* eltType = instr->getAllocatedType();
1968
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001969 // If #elements is constant, use simpler code for fixed-size allocas
Chris Lattnerea45d7b2002-12-28 20:19:44 +00001970 int tsize = (int) target.getTargetData().getTypeSize(eltType);
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001971 Value* numElementsVal = NULL;
1972 bool isArray = instr->isArrayAllocation();
1973
1974 if (!isArray ||
1975 isa<Constant>(numElementsVal = instr->getArraySize()))
1976 { // total size is constant: generate code for fixed-size alloca
Chris Lattnerea45d7b2002-12-28 20:19:44 +00001977 unsigned numElements = isArray?
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001978 cast<ConstantUInt>(numElementsVal)->getValue() : 1;
1979 CreateCodeForFixedSizeAlloca(target, instr, tsize,
1980 numElements, mvec);
1981 }
Vikram S. Adve74825322002-03-18 03:15:35 +00001982 else // total size is not constant.
1983 CreateCodeForVariableSizeAlloca(target, instr, tsize,
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001984 numElementsVal, mvec);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001985 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001986 }
Vikram S. Adved3e26482002-10-13 00:18:57 +00001987
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001988 case 61: // reg: Call
Vikram S. Adve4a8bb2b2002-09-28 16:55:41 +00001989 { // Generate a direct (CALL) or indirect (JMPL) call.
1990 // Mark the return-address register, the indirection
1991 // register (for indirect calls), the operands of the Call,
1992 // and the return value (if any) as implicit operands
1993 // of the machine instruction.
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001994 //
Vikram S. Advedbc4fad2002-04-25 04:37:51 +00001995 // If this is a varargs function, floating point arguments
1996 // have to passed in integer registers so insert
1997 // copy-float-to-int instructions for each float operand.
1998 //
Chris Lattnerb00c5822001-10-02 03:41:24 +00001999 CallInst *callInstr = cast<CallInst>(subtreeRoot->getInstruction());
Chris Lattner749655f2001-10-13 06:54:30 +00002000 Value *callee = callInstr->getCalledValue();
Vikram S. Adve4a8bb2b2002-09-28 16:55:41 +00002001
2002 // Create hidden virtual register for return address with type void*
Vikram S. Adve242a8082002-05-19 15:25:51 +00002003 TmpInstruction* retAddrReg =
Vikram S. Advea10d1a72002-03-31 19:07:35 +00002004 new TmpInstruction(PointerType::get(Type::VoidTy), callInstr);
Chris Lattner9c461082002-02-03 07:50:56 +00002005 MachineCodeForInstruction::get(callInstr).addTemp(retAddrReg);
Vikram S. Adve4a8bb2b2002-09-28 16:55:41 +00002006
Vikram S. Adveea21a6c2001-10-20 20:57:06 +00002007 // Generate the machine instruction and its operands.
2008 // Use CALL for direct function calls; this optimistically assumes
2009 // the PC-relative address fits in the CALL address field (22 bits).
2010 // Use JMPL for indirect calls.
2011 //
Vikram S. Adve4a8bb2b2002-09-28 16:55:41 +00002012 if (isa<Function>(callee)) // direct function call
2013 M = Create1OperandInstr_Addr(CALL, callee);
2014 else // indirect function call
Chris Lattnere5b1ed92003-01-15 00:03:28 +00002015 M = BuildMI(JMPLCALL,
2016 3).addReg(callee).addSImm((int64_t)0).addReg(retAddrReg);
Vikram S. Adve74825322002-03-18 03:15:35 +00002017 mvec.push_back(M);
Vikram S. Advea10d1a72002-03-31 19:07:35 +00002018
Vikram S. Adve242a8082002-05-19 15:25:51 +00002019 const FunctionType* funcType =
2020 cast<FunctionType>(cast<PointerType>(callee->getType())
2021 ->getElementType());
2022 bool isVarArgs = funcType->isVarArg();
2023 bool noPrototype = isVarArgs && funcType->getNumParams() == 0;
Vikram S. Advedbc4fad2002-04-25 04:37:51 +00002024
Vikram S. Adveaabb5952002-10-29 19:37:31 +00002025 // Use a descriptor to pass information about call arguments
2026 // to the register allocator. This descriptor will be "owned"
2027 // and freed automatically when the MachineCodeForInstruction
2028 // object for the callInstr goes away.
Vikram S. Adve242a8082002-05-19 15:25:51 +00002029 CallArgsDescriptor* argDesc = new CallArgsDescriptor(callInstr,
2030 retAddrReg, isVarArgs, noPrototype);
Vikram S. Advea995e602001-10-11 04:23:19 +00002031
Vikram S. Adve242a8082002-05-19 15:25:51 +00002032 assert(callInstr->getOperand(0) == callee
2033 && "This is assumed in the loop below!");
2034
2035 for (unsigned i=1, N=callInstr->getNumOperands(); i < N; ++i)
2036 {
2037 Value* argVal = callInstr->getOperand(i);
2038 Instruction* intArgReg = NULL;
2039
2040 // Check for FP arguments to varargs functions.
2041 // Any such argument in the first $K$ args must be passed in an
2042 // integer register, where K = #integer argument registers.
2043 if (isVarArgs && argVal->getType()->isFloatingPoint())
2044 {
2045 // If it is a function with no prototype, pass value
2046 // as an FP value as well as a varargs value
2047 if (noPrototype)
2048 argDesc->getArgInfo(i-1).setUseFPArgReg();
2049
2050 // If this arg. is in the first $K$ regs, add a copy
2051 // float-to-int instruction to pass the value as an integer.
Vikram S. Adved3e26482002-10-13 00:18:57 +00002052 if (i <= target.getRegInfo().GetNumOfIntArgRegs())
Vikram S. Adve242a8082002-05-19 15:25:51 +00002053 {
2054 MachineCodeForInstruction &destMCFI =
2055 MachineCodeForInstruction::get(callInstr);
2056 intArgReg = new TmpInstruction(Type::IntTy, argVal);
2057 destMCFI.addTemp(intArgReg);
2058
2059 vector<MachineInstr*> copyMvec;
2060 target.getInstrInfo().CreateCodeToCopyFloatToInt(target,
2061 callInstr->getParent()->getParent(),
2062 argVal, (TmpInstruction*) intArgReg,
2063 copyMvec, destMCFI);
2064 mvec.insert(mvec.begin(),copyMvec.begin(),copyMvec.end());
2065
2066 argDesc->getArgInfo(i-1).setUseIntArgReg();
2067 argDesc->getArgInfo(i-1).setArgCopy(intArgReg);
2068 }
2069 else
2070 // Cannot fit in first $K$ regs so pass the arg on the stack
2071 argDesc->getArgInfo(i-1).setUseStackSlot();
2072 }
2073
2074 if (intArgReg)
2075 mvec.back()->addImplicitRef(intArgReg);
2076
2077 mvec.back()->addImplicitRef(argVal);
2078 }
2079
2080 // Add the return value as an implicit ref. The call operands
2081 // were added above.
Vikram S. Adveea21a6c2001-10-20 20:57:06 +00002082 if (callInstr->getType() != Type::VoidTy)
Vikram S. Adve74825322002-03-18 03:15:35 +00002083 mvec.back()->addImplicitRef(callInstr, /*isDef*/ true);
Vikram S. Advea995e602001-10-11 04:23:19 +00002084
Vikram S. Adveea21a6c2001-10-20 20:57:06 +00002085 // For the CALL instruction, the ret. addr. reg. is also implicit
Chris Lattnerb0d04722002-03-26 17:58:12 +00002086 if (isa<Function>(callee))
Vikram S. Adve74825322002-03-18 03:15:35 +00002087 mvec.back()->addImplicitRef(retAddrReg, /*isDef*/ true);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002088
Vikram S. Adve74825322002-03-18 03:15:35 +00002089 // delay slot
2090 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002091 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00002092 }
Vikram S. Adve242a8082002-05-19 15:25:51 +00002093
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002094 case 62: // reg: Shl(reg, reg)
Vikram S. Adve242a8082002-05-19 15:25:51 +00002095 {
2096 Value* argVal1 = subtreeRoot->leftChild()->getValue();
2097 Value* argVal2 = subtreeRoot->rightChild()->getValue();
2098 Instruction* shlInstr = subtreeRoot->getInstruction();
2099
2100 const Type* opType = argVal1->getType();
Chris Lattner0c4e8862002-09-03 01:08:28 +00002101 assert((opType->isInteger() || isa<PointerType>(opType)) &&
2102 "Shl unsupported for other types");
Vikram S. Adve242a8082002-05-19 15:25:51 +00002103
2104 CreateShiftInstructions(target, shlInstr->getParent()->getParent(),
2105 (opType == Type::LongTy)? SLLX : SLL,
2106 argVal1, argVal2, 0, shlInstr, mvec,
2107 MachineCodeForInstruction::get(shlInstr));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002108 break;
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00002109 }
2110
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002111 case 63: // reg: Shr(reg, reg)
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00002112 { const Type* opType = subtreeRoot->leftChild()->getValue()->getType();
Chris Lattner0c4e8862002-09-03 01:08:28 +00002113 assert((opType->isInteger() || isa<PointerType>(opType)) &&
2114 "Shr unsupported for other types");
Vikram S. Adve74825322002-03-18 03:15:35 +00002115 mvec.push_back(new MachineInstr((opType->isSigned()
2116 ? ((opType == Type::LongTy)? SRAX : SRA)
2117 : ((opType == Type::LongTy)? SRLX : SRL))));
2118 Set3OperandsFromInstr(mvec.back(), subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002119 break;
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00002120 }
2121
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002122 case 64: // reg: Phi(reg,reg)
Vikram S. Adve74825322002-03-18 03:15:35 +00002123 break; // don't forward the value
2124
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002125 case 71: // reg: VReg
2126 case 72: // reg: Constant
Vikram S. Adve74825322002-03-18 03:15:35 +00002127 break; // don't forward the value
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002128
2129 default:
2130 assert(0 && "Unrecognized BURG rule");
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002131 break;
2132 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00002133 }
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00002134
Chris Lattner20b1ea02001-09-14 03:47:57 +00002135 if (forwardOperandNum >= 0)
2136 { // We did not generate a machine instruction but need to use operand.
2137 // If user is in the same tree, replace Value in its machine operand.
2138 // If not, insert a copy instruction which should get coalesced away
2139 // by register allocation.
2140 if (subtreeRoot->parent() != NULL)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002141 ForwardOperand(subtreeRoot, subtreeRoot->parent(), forwardOperandNum);
Chris Lattner20b1ea02001-09-14 03:47:57 +00002142 else
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002143 {
Vikram S. Adve7fe27872001-10-18 00:26:20 +00002144 vector<MachineInstr*> minstrVec;
Vikram S. Adve242a8082002-05-19 15:25:51 +00002145 Instruction* instr = subtreeRoot->getInstruction();
2146 target.getInstrInfo().
2147 CreateCopyInstructionsByType(target,
2148 instr->getParent()->getParent(),
2149 instr->getOperand(forwardOperandNum),
2150 instr, minstrVec,
2151 MachineCodeForInstruction::get(instr));
Vikram S. Adve7fe27872001-10-18 00:26:20 +00002152 assert(minstrVec.size() > 0);
Vikram S. Adve74825322002-03-18 03:15:35 +00002153 mvec.insert(mvec.end(), minstrVec.begin(), minstrVec.end());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002154 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00002155 }
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00002156
2157 if (maskUnsignedResult)
2158 { // If result is unsigned and smaller than int reg size,
2159 // we need to clear high bits of result value.
2160 assert(forwardOperandNum < 0 && "Need mask but no instruction generated");
2161 Instruction* dest = subtreeRoot->getInstruction();
Vikram S. Adve8cfffd32002-08-24 20:56:53 +00002162 if (dest->getType()->isUnsigned())
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00002163 {
Chris Lattnerea45d7b2002-12-28 20:19:44 +00002164 unsigned destSize=target.getTargetData().getTypeSize(dest->getType());
Vikram S. Advebabc0fa2002-09-05 18:32:13 +00002165 if (destSize <= 4)
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00002166 { // Mask high bits. Use a TmpInstruction to represent the
2167 // intermediate result before masking. Since those instructions
2168 // have already been generated, go back and substitute tmpI
2169 // for dest in the result position of each one of them.
2170 TmpInstruction *tmpI = new TmpInstruction(dest->getType(), dest,
2171 NULL, "maskHi");
2172 MachineCodeForInstruction::get(dest).addTemp(tmpI);
2173
2174 for (unsigned i=0, N=mvec.size(); i < N; ++i)
2175 mvec[i]->substituteValue(dest, tmpI);
2176
Chris Lattnere5b1ed92003-01-15 00:03:28 +00002177 M = BuildMI(SRL, 3).addReg(tmpI).addZImm(8*(4-destSize))
2178 .addReg(dest, MOTy::Def);
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00002179 mvec.push_back(M);
2180 }
Chris Lattnerea45d7b2002-12-28 20:19:44 +00002181 else if (destSize < target.getTargetData().getIntegerRegize())
Vikram S. Advebabc0fa2002-09-05 18:32:13 +00002182 assert(0 && "Unsupported type size: 32 < size < 64 bits");
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00002183 }
2184 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00002185}