blob: 2d7664330a386e02e7ac2ec017c98c3d6f6b4c9c [file] [log] [blame]
Chris Lattner035dfbe2002-08-09 20:08:06 +00001//===-- SparcInstrSelection.cpp -------------------------------------------===//
2//
3// BURS instruction selection for SPARC V9 architecture.
4//
5//===----------------------------------------------------------------------===//
Chris Lattner20b1ea02001-09-14 03:47:57 +00006
7#include "SparcInternals.h"
Vikram S. Adve7fe27872001-10-18 00:26:20 +00008#include "SparcInstrSelectionSupport.h"
Vikram S. Adve74825322002-03-18 03:15:35 +00009#include "SparcRegClassInfo.h"
Vikram S. Adve8557b222001-10-10 20:56:33 +000010#include "llvm/CodeGen/InstrSelectionSupport.h"
Chris Lattner20b1ea02001-09-14 03:47:57 +000011#include "llvm/CodeGen/MachineInstr.h"
Vikram S. Adve242a8082002-05-19 15:25:51 +000012#include "llvm/CodeGen/MachineInstrAnnot.h"
Chris Lattner20b1ea02001-09-14 03:47:57 +000013#include "llvm/CodeGen/InstrForest.h"
14#include "llvm/CodeGen/InstrSelection.h"
Misha Brukmanfce11432002-10-28 00:28:31 +000015#include "llvm/CodeGen/MachineFunction.h"
Chris 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)
549 ? Create3OperandInstr(shiftOpCode, argVal1, optArgVal2, shiftDest)
550 : Create3OperandInstr_UImmed(shiftOpCode, argVal1, optShiftNum, shiftDest);
551 mvec.push_back(M);
552
553 if (shiftDest != destVal)
554 { // extend the sign-bit of the result into all upper bits of dest
555 assert(8*opSize <= 32 && "Unexpected type size > 4 and < IntRegSize?");
556 target.getInstrInfo().
Vikram S. Adve94c40812002-09-27 14:33:08 +0000557 CreateSignExtensionInstructions(target, F, shiftDest, destVal,
558 8*opSize, mvec, mcfi);
Vikram S. Adve242a8082002-05-19 15:25:51 +0000559 }
560}
561
562
Vikram S. Adve74825322002-03-18 03:15:35 +0000563// Does not create any instructions if we cannot exploit constant to
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000564// create a cheaper instruction.
565// This returns the approximate cost of the instructions generated,
566// which is used to pick the cheapest when both operands are constant.
Chris Lattnerea45d7b2002-12-28 20:19:44 +0000567static inline unsigned
Vikram S. Adve242a8082002-05-19 15:25:51 +0000568CreateMulConstInstruction(const TargetMachine &target, Function* F,
569 Value* lval, Value* rval, Instruction* destVal,
570 vector<MachineInstr*>& mvec,
571 MachineCodeForInstruction& mcfi)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000572{
Vikram S. Adve242a8082002-05-19 15:25:51 +0000573 /* Use max. multiply cost, viz., cost of MULX */
Chris Lattnerea45d7b2002-12-28 20:19:44 +0000574 unsigned cost = target.getInstrInfo().minLatency(MULX);
575 unsigned firstNewInstr = mvec.size();
Vikram S. Adve74825322002-03-18 03:15:35 +0000576
577 Value* constOp = rval;
578 if (! isa<Constant>(constOp))
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000579 return cost;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000580
581 // Cases worth optimizing are:
582 // (1) Multiply by 0 or 1 for any type: replace with copy (ADD or FMOV)
583 // (2) Multiply by 2^x for integer types: replace with Shift
584 //
Vikram S. Adve74825322002-03-18 03:15:35 +0000585 const Type* resultType = destVal->getType();
Chris Lattner20b1ea02001-09-14 03:47:57 +0000586
Chris Lattner0c4e8862002-09-03 01:08:28 +0000587 if (resultType->isInteger() || isa<PointerType>(resultType))
Chris Lattner20b1ea02001-09-14 03:47:57 +0000588 {
Chris Lattner20b1ea02001-09-14 03:47:57 +0000589 bool isValidConst;
590 int64_t C = GetConstantValueAsSignedInt(constOp, isValidConst);
591 if (isValidConst)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000592 {
Vikram S. Adve242a8082002-05-19 15:25:51 +0000593 unsigned pow;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000594 bool needNeg = false;
595 if (C < 0)
596 {
597 needNeg = true;
598 C = -C;
599 }
600
601 if (C == 0 || C == 1)
602 {
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000603 cost = target.getInstrInfo().minLatency(ADD);
Vikram S. Adve242a8082002-05-19 15:25:51 +0000604 MachineInstr* M = (C == 0)
605 ? Create3OperandInstr_Reg(ADD,
606 target.getRegInfo().getZeroRegNum(),
607 target.getRegInfo().getZeroRegNum(),
608 destVal)
609 : Create3OperandInstr_Reg(ADD, lval,
610 target.getRegInfo().getZeroRegNum(),
611 destVal);
612 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000613 }
Chris Lattner36346c72002-05-19 21:20:19 +0000614 else if (isPowerOf2(C, pow))
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000615 {
Chris Lattnerea45d7b2002-12-28 20:19:44 +0000616 unsigned opSize = target.getTargetData().getTypeSize(resultType);
Vikram S. Adve242a8082002-05-19 15:25:51 +0000617 MachineOpCode opCode = (opSize <= 32)? SLL : SLLX;
618 CreateShiftInstructions(target, F, opCode, lval, NULL, pow,
619 destVal, mvec, mcfi);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000620 }
621
Vikram S. Adve242a8082002-05-19 15:25:51 +0000622 if (mvec.size() > 0 && needNeg)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000623 { // insert <reg = SUB 0, reg> after the instr to flip the sign
Vikram S. Adve242a8082002-05-19 15:25:51 +0000624 MachineInstr* M = CreateIntNegInstruction(target, destVal);
625 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000626 }
627 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000628 }
629 else
630 {
Chris Lattner9b625032002-05-06 16:15:30 +0000631 if (ConstantFP *FPC = dyn_cast<ConstantFP>(constOp))
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000632 {
Chris Lattner9b625032002-05-06 16:15:30 +0000633 double dval = FPC->getValue();
Vikram S. Adve6ad7c552001-11-09 02:18:16 +0000634 if (fabs(dval) == 1)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000635 {
Vikram S. Adve242a8082002-05-19 15:25:51 +0000636 MachineOpCode opCode = (dval < 0)
Vikram S. Adve6ad7c552001-11-09 02:18:16 +0000637 ? (resultType == Type::FloatTy? FNEGS : FNEGD)
638 : (resultType == Type::FloatTy? FMOVS : FMOVD);
Vikram S. Adve242a8082002-05-19 15:25:51 +0000639 MachineInstr* M = Create2OperandInstr(opCode, lval, destVal);
640 mvec.push_back(M);
Vikram S. Adve6ad7c552001-11-09 02:18:16 +0000641 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000642 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000643 }
644
Vikram S. Adve242a8082002-05-19 15:25:51 +0000645 if (firstNewInstr < mvec.size())
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000646 {
Vikram S. Adve242a8082002-05-19 15:25:51 +0000647 cost = 0;
Chris Lattnerea45d7b2002-12-28 20:19:44 +0000648 for (unsigned i=firstNewInstr; i < mvec.size(); ++i)
Vikram S. Adve242a8082002-05-19 15:25:51 +0000649 cost += target.getInstrInfo().minLatency(mvec[i]->getOpCode());
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000650 }
651
652 return cost;
Vikram S. Adve74825322002-03-18 03:15:35 +0000653}
654
655
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000656// Does not create any instructions if we cannot exploit constant to
657// create a cheaper instruction.
658//
659static inline void
660CreateCheapestMulConstInstruction(const TargetMachine &target,
Vikram S. Adve242a8082002-05-19 15:25:51 +0000661 Function* F,
662 Value* lval, Value* rval,
663 Instruction* destVal,
664 vector<MachineInstr*>& mvec,
665 MachineCodeForInstruction& mcfi)
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000666{
667 Value* constOp;
668 if (isa<Constant>(lval) && isa<Constant>(rval))
Vikram S. Adved3e26482002-10-13 00:18:57 +0000669 { // both operands are constant: evaluate and "set" in dest
670 Constant* P = ConstantFoldBinaryInstruction(Instruction::Mul,
671 cast<Constant>(lval), cast<Constant>(rval));
672 target.getInstrInfo().CreateCodeToLoadConst(target,F,P,destVal,mvec,mcfi);
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000673 }
674 else if (isa<Constant>(rval)) // rval is constant, but not lval
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 else if (isa<Constant>(lval)) // lval is constant, but not rval
Vikram S. Adve242a8082002-05-19 15:25:51 +0000677 CreateMulConstInstruction(target, F, lval, rval, destVal, mvec, mcfi);
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000678
679 // else neither is constant
680 return;
681}
682
Vikram S. Adve74825322002-03-18 03:15:35 +0000683// Return NULL if we cannot exploit constant to create a cheaper instruction
684static inline void
Vikram S. Adve242a8082002-05-19 15:25:51 +0000685CreateMulInstruction(const TargetMachine &target, Function* F,
686 Value* lval, Value* rval, Instruction* destVal,
Vikram S. Adve74825322002-03-18 03:15:35 +0000687 vector<MachineInstr*>& mvec,
Vikram S. Adve242a8082002-05-19 15:25:51 +0000688 MachineCodeForInstruction& mcfi,
Vikram S. Adve74825322002-03-18 03:15:35 +0000689 MachineOpCode forceMulOp = INVALID_MACHINE_OPCODE)
690{
Chris Lattnerea45d7b2002-12-28 20:19:44 +0000691 unsigned L = mvec.size();
Vikram S. Adve242a8082002-05-19 15:25:51 +0000692 CreateCheapestMulConstInstruction(target,F, lval, rval, destVal, mvec, mcfi);
Vikram S. Adve74825322002-03-18 03:15:35 +0000693 if (mvec.size() == L)
694 { // no instructions were added so create MUL reg, reg, reg.
695 // Use FSMULD if both operands are actually floats cast to doubles.
696 // Otherwise, use the default opcode for the appropriate type.
697 MachineOpCode mulOp = ((forceMulOp != INVALID_MACHINE_OPCODE)
698 ? forceMulOp
699 : ChooseMulInstructionByType(destVal->getType()));
700 MachineInstr* M = new MachineInstr(mulOp);
701 M->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister, lval);
702 M->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister, rval);
703 M->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister, destVal);
704 mvec.push_back(M);
705 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000706}
707
708
Vikram S. Adve510eec72001-11-04 21:59:14 +0000709// Generate a divide instruction for Div or Rem.
710// For Rem, this assumes that the operand type will be signed if the result
711// type is signed. This is correct because they must have the same sign.
712//
Chris Lattner20b1ea02001-09-14 03:47:57 +0000713static inline MachineOpCode
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000714ChooseDivInstruction(TargetMachine &target,
715 const InstructionNode* instrNode)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000716{
717 MachineOpCode opCode = INVALID_OPCODE;
718
719 const Type* resultType = instrNode->getInstruction()->getType();
720
Chris Lattner0c4e8862002-09-03 01:08:28 +0000721 if (resultType->isInteger())
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000722 opCode = resultType->isSigned()? SDIVX : UDIVX;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000723 else
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000724 switch(resultType->getPrimitiveID())
725 {
726 case Type::FloatTyID: opCode = FDIVS; break;
727 case Type::DoubleTyID: opCode = FDIVD; break;
728 default: assert(0 && "Invalid type for DIV instruction"); break;
729 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000730
731 return opCode;
732}
733
734
Vikram S. Adve74825322002-03-18 03:15:35 +0000735// Return NULL if we cannot exploit constant to create a cheaper instruction
736static inline void
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000737CreateDivConstInstruction(TargetMachine &target,
738 const InstructionNode* instrNode,
Vikram S. Adve74825322002-03-18 03:15:35 +0000739 vector<MachineInstr*>& mvec)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000740{
Vikram S. Adve74825322002-03-18 03:15:35 +0000741 MachineInstr* minstr1 = NULL;
742 MachineInstr* minstr2 = NULL;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000743
744 Value* constOp = ((InstrTreeNode*) instrNode->rightChild())->getValue();
Vikram S. Adve74825322002-03-18 03:15:35 +0000745 if (! isa<Constant>(constOp))
746 return;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000747
748 // Cases worth optimizing are:
749 // (1) Divide by 1 for any type: replace with copy (ADD or FMOV)
750 // (2) Divide by 2^x for integer types: replace with SR[L or A]{X}
751 //
752 const Type* resultType = instrNode->getInstruction()->getType();
753
Chris Lattner0c4e8862002-09-03 01:08:28 +0000754 if (resultType->isInteger())
Chris Lattner20b1ea02001-09-14 03:47:57 +0000755 {
756 unsigned pow;
757 bool isValidConst;
758 int64_t C = GetConstantValueAsSignedInt(constOp, isValidConst);
759 if (isValidConst)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000760 {
761 bool needNeg = false;
762 if (C < 0)
763 {
764 needNeg = true;
765 C = -C;
766 }
767
768 if (C == 1)
769 {
Vikram S. Adve74825322002-03-18 03:15:35 +0000770 minstr1 = new MachineInstr(ADD);
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000771 minstr1->SetMachineOperandVal(0,
772 MachineOperand::MO_VirtualRegister,
773 instrNode->leftChild()->getValue());
774 minstr1->SetMachineOperandReg(1,
775 target.getRegInfo().getZeroRegNum());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000776 }
Chris Lattner36346c72002-05-19 21:20:19 +0000777 else if (isPowerOf2(C, pow))
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000778 {
779 MachineOpCode opCode= ((resultType->isSigned())
780 ? (resultType==Type::LongTy)? SRAX : SRA
781 : (resultType==Type::LongTy)? SRLX : SRL);
Vikram S. Adve74825322002-03-18 03:15:35 +0000782 minstr1 = new MachineInstr(opCode);
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000783 minstr1->SetMachineOperandVal(0,
784 MachineOperand::MO_VirtualRegister,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000785 instrNode->leftChild()->getValue());
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000786 minstr1->SetMachineOperandConst(1,
787 MachineOperand::MO_UnextendedImmed,
788 pow);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000789 }
790
Vikram S. Adve74825322002-03-18 03:15:35 +0000791 if (minstr1 && needNeg)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000792 { // insert <reg = SUB 0, reg> after the instr to flip the sign
Vikram S. Adve74825322002-03-18 03:15:35 +0000793 minstr2 = CreateIntNegInstruction(target,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000794 instrNode->getValue());
795 }
796 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000797 }
798 else
799 {
Chris Lattner9b625032002-05-06 16:15:30 +0000800 if (ConstantFP *FPC = dyn_cast<ConstantFP>(constOp))
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000801 {
Chris Lattner9b625032002-05-06 16:15:30 +0000802 double dval = FPC->getValue();
Vikram S. Adve6ad7c552001-11-09 02:18:16 +0000803 if (fabs(dval) == 1)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000804 {
805 bool needNeg = (dval < 0);
806
807 MachineOpCode opCode = needNeg
808 ? (resultType == Type::FloatTy? FNEGS : FNEGD)
809 : (resultType == Type::FloatTy? FMOVS : FMOVD);
810
Vikram S. Adve74825322002-03-18 03:15:35 +0000811 minstr1 = new MachineInstr(opCode);
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000812 minstr1->SetMachineOperandVal(0,
813 MachineOperand::MO_VirtualRegister,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000814 instrNode->leftChild()->getValue());
815 }
816 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000817 }
818
Vikram S. Adve74825322002-03-18 03:15:35 +0000819 if (minstr1 != NULL)
820 minstr1->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister,
821 instrNode->getValue());
Chris Lattner20b1ea02001-09-14 03:47:57 +0000822
Vikram S. Adve74825322002-03-18 03:15:35 +0000823 if (minstr1)
824 mvec.push_back(minstr1);
825 if (minstr2)
826 mvec.push_back(minstr2);
Chris Lattner20b1ea02001-09-14 03:47:57 +0000827}
828
829
Vikram S. Adve74825322002-03-18 03:15:35 +0000830static void
831CreateCodeForVariableSizeAlloca(const TargetMachine& target,
832 Instruction* result,
Chris Lattnerea45d7b2002-12-28 20:19:44 +0000833 unsigned tsize,
Vikram S. Adve74825322002-03-18 03:15:35 +0000834 Value* numElementsVal,
835 vector<MachineInstr*>& getMvec)
836{
Vikram S. Adveaabb5952002-10-29 19:37:31 +0000837 Value* totalSizeVal;
Vikram S. Adve74825322002-03-18 03:15:35 +0000838 MachineInstr* M;
Vikram S. Adved3e26482002-10-13 00:18:57 +0000839 MachineCodeForInstruction& mcfi = MachineCodeForInstruction::get(result);
Vikram S. Adveaabb5952002-10-29 19:37:31 +0000840 Function *F = result->getParent()->getParent();
Vikram S. Adved3e26482002-10-13 00:18:57 +0000841
Vikram S. Adveaabb5952002-10-29 19:37:31 +0000842 // Enforce the alignment constraints on the stack pointer at
843 // compile time if the total size is a known constant.
844 if (isa<Constant>(numElementsVal))
845 {
846 bool isValid;
847 int64_t numElem = GetConstantValueAsSignedInt(numElementsVal, isValid);
848 assert(isValid && "Unexpectedly large array dimension in alloca!");
849 int64_t total = numElem * tsize;
850 if (int extra= total % target.getFrameInfo().getStackFrameSizeAlignment())
851 total += target.getFrameInfo().getStackFrameSizeAlignment() - extra;
852 totalSizeVal = ConstantSInt::get(Type::IntTy, total);
853 }
854 else
855 {
856 // The size is not a constant. Generate code to compute it and
857 // code to pad the size for stack alignment.
858 // Create a Value to hold the (constant) element size
859 Value* tsizeVal = ConstantSInt::get(Type::IntTy, tsize);
860
861 // Create temporary values to hold the result of MUL, SLL, SRL
862 // THIS CASE IS INCOMPLETE AND WILL BE FIXED SHORTLY.
863 TmpInstruction* tmpProd = new TmpInstruction(numElementsVal, tsizeVal);
864 TmpInstruction* tmpSLL = new TmpInstruction(numElementsVal, tmpProd);
865 TmpInstruction* tmpSRL = new TmpInstruction(numElementsVal, tmpSLL);
866 mcfi.addTemp(tmpProd);
867 mcfi.addTemp(tmpSLL);
868 mcfi.addTemp(tmpSRL);
869
870 // Instruction 1: mul numElements, typeSize -> tmpProd
871 // This will optimize the MUL as far as possible.
872 CreateMulInstruction(target, F, numElementsVal, tsizeVal, tmpProd,getMvec,
873 mcfi, INVALID_MACHINE_OPCODE);
874
875 assert(0 && "Need to insert padding instructions here!");
876
877 totalSizeVal = tmpProd;
878 }
Vikram S. Adve74825322002-03-18 03:15:35 +0000879
880 // Get the constant offset from SP for dynamically allocated storage
881 // and create a temporary Value to hold it.
Misha Brukmanfce11432002-10-28 00:28:31 +0000882 MachineFunction& mcInfo = MachineFunction::get(F);
Vikram S. Adve74825322002-03-18 03:15:35 +0000883 bool growUp;
884 ConstantSInt* dynamicAreaOffset =
885 ConstantSInt::get(Type::IntTy,
Vikram S. Adveaabb5952002-10-29 19:37:31 +0000886 target.getFrameInfo().getDynamicAreaOffset(mcInfo,growUp));
Vikram S. Adve74825322002-03-18 03:15:35 +0000887 assert(! growUp && "Has SPARC v9 stack frame convention changed?");
888
Vikram S. Adveaabb5952002-10-29 19:37:31 +0000889 // Instruction 2: sub %sp, totalSizeVal -> %sp
Vikram S. Adve74825322002-03-18 03:15:35 +0000890 M = new MachineInstr(SUB);
891 M->SetMachineOperandReg(0, target.getRegInfo().getStackPointer());
Vikram S. Adveaabb5952002-10-29 19:37:31 +0000892 M->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister, totalSizeVal);
Vikram S. Adve74825322002-03-18 03:15:35 +0000893 M->SetMachineOperandReg(2, target.getRegInfo().getStackPointer());
894 getMvec.push_back(M);
Vikram S. Adveaabb5952002-10-29 19:37:31 +0000895
Vikram S. Adve74825322002-03-18 03:15:35 +0000896 // Instruction 3: add %sp, frameSizeBelowDynamicArea -> result
897 M = new MachineInstr(ADD);
898 M->SetMachineOperandReg(0, target.getRegInfo().getStackPointer());
Chris Lattnerea45d7b2002-12-28 20:19:44 +0000899 M->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister,
900 dynamicAreaOffset);
Vikram S. Adve74825322002-03-18 03:15:35 +0000901 M->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister, result);
902 getMvec.push_back(M);
903}
904
905
906static void
907CreateCodeForFixedSizeAlloca(const TargetMachine& target,
908 Instruction* result,
Chris Lattnerea45d7b2002-12-28 20:19:44 +0000909 unsigned tsize,
910 unsigned numElements,
Vikram S. Adve74825322002-03-18 03:15:35 +0000911 vector<MachineInstr*>& getMvec)
912{
Vikram S. Adved3e26482002-10-13 00:18:57 +0000913 assert(tsize > 0 && "Illegal (zero) type size for alloca");
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000914 assert(result && result->getParent() &&
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000915 "Result value is not part of a function?");
916 Function *F = result->getParent()->getParent();
Misha Brukmanfce11432002-10-28 00:28:31 +0000917 MachineFunction &mcInfo = MachineFunction::get(F);
Vikram S. Adve74825322002-03-18 03:15:35 +0000918
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000919 // Check if the offset would small enough to use as an immediate in
920 // load/stores (check LDX because all load/stores have the same-size immediate
921 // field). If not, put the variable in the dynamically sized area of the
922 // frame.
Chris Lattnerea45d7b2002-12-28 20:19:44 +0000923 unsigned paddedSizeIgnored;
924 int offsetFromFP = mcInfo.getInfo()->computeOffsetforLocalVar(result,
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000925 paddedSizeIgnored,
Vikram S. Adve74825322002-03-18 03:15:35 +0000926 tsize * numElements);
Chris Lattnerea45d7b2002-12-28 20:19:44 +0000927 if (! target.getInstrInfo().constantFitsInImmedField(LDX, offsetFromFP)) {
928 CreateCodeForVariableSizeAlloca(target, result, tsize,
929 ConstantSInt::get(Type::IntTy,numElements),
930 getMvec);
931 return;
932 }
Vikram S. Adve74825322002-03-18 03:15:35 +0000933
934 // else offset fits in immediate field so go ahead and allocate it.
Chris Lattnerea45d7b2002-12-28 20:19:44 +0000935 offsetFromFP = mcInfo.getInfo()->allocateLocalVar(result, tsize *numElements);
Vikram S. Adve74825322002-03-18 03:15:35 +0000936
937 // Create a temporary Value to hold the constant offset.
938 // This is needed because it may not fit in the immediate field.
939 ConstantSInt* offsetVal = ConstantSInt::get(Type::IntTy, offsetFromFP);
940
941 // Instruction 1: add %fp, offsetFromFP -> result
942 MachineInstr* M = new MachineInstr(ADD);
943 M->SetMachineOperandReg(0, target.getRegInfo().getFramePointer());
944 M->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister, offsetVal);
945 M->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister, result);
946
947 getMvec.push_back(M);
948}
949
950
Chris Lattner20b1ea02001-09-14 03:47:57 +0000951//------------------------------------------------------------------------
952// Function SetOperandsForMemInstr
953//
954// Choose addressing mode for the given load or store instruction.
955// Use [reg+reg] if it is an indexed reference, and the index offset is
956// not a constant or if it cannot fit in the offset field.
957// Use [reg+offset] in all other cases.
958//
959// This assumes that all array refs are "lowered" to one of these forms:
960// %x = load (subarray*) ptr, constant ; single constant offset
961// %x = load (subarray*) ptr, offsetVal ; single non-constant offset
962// Generally, this should happen via strength reduction + LICM.
963// Also, strength reduction should take care of using the same register for
964// the loop index variable and an array index, when that is profitable.
965//------------------------------------------------------------------------
966
967static void
Vikram S. Adve74825322002-03-18 03:15:35 +0000968SetOperandsForMemInstr(vector<MachineInstr*>& mvec,
Vikram S. Adveefc94332002-10-14 16:32:24 +0000969 InstructionNode* vmInstrNode,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000970 const TargetMachine& target)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000971{
Vikram S. Adve8cfffd32002-08-24 20:56:53 +0000972 Instruction* memInst = vmInstrNode->getInstruction();
973 vector<MachineInstr*>::iterator mvecI = mvec.end() - 1;
974
975 // Index vector, ptr value, and flag if all indices are const.
Vikram S. Advea10d1a72002-03-31 19:07:35 +0000976 vector<Value*> idxVec;
Vikram S. Adve8cfffd32002-08-24 20:56:53 +0000977 bool allConstantIndices;
978 Value* ptrVal = GetMemInstArgs(vmInstrNode, idxVec, allConstantIndices);
Vikram S. Adveed3fefb2002-08-03 13:48:21 +0000979
Vikram S. Adve8cfffd32002-08-24 20:56:53 +0000980 // Now create the appropriate operands for the machine instruction.
981 // First, initialize so we default to storing the offset in a register.
Chris Lattner8e5c0b42001-11-07 14:01:59 +0000982 int64_t smallConstOffset = 0;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000983 Value* valueForRegOffset = NULL;
Vikram S. Adveed3fefb2002-08-03 13:48:21 +0000984 MachineOperand::MachineOperandType offsetOpType =
985 MachineOperand::MO_VirtualRegister;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000986
Vikram S. Adve74825322002-03-18 03:15:35 +0000987 // Check if there is an index vector and if so, compute the
988 // right offset for structures and for arrays
Chris Lattner20b1ea02001-09-14 03:47:57 +0000989 //
Chris Lattner3bb8ad22002-08-22 23:37:24 +0000990 if (!idxVec.empty())
Chris Lattner20b1ea02001-09-14 03:47:57 +0000991 {
Vikram S. Adve74825322002-03-18 03:15:35 +0000992 const PointerType* ptrType = cast<PointerType>(ptrVal->getType());
Chris Lattner20b1ea02001-09-14 03:47:57 +0000993
Vikram S. Adve242a8082002-05-19 15:25:51 +0000994 // If all indices are constant, compute the combined offset directly.
995 if (allConstantIndices)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000996 {
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000997 // Compute the offset value using the index vector. Create a
998 // virtual reg. for it since it may not fit in the immed field.
Chris Lattnerea45d7b2002-12-28 20:19:44 +0000999 uint64_t offset = target.getTargetData().getIndexedOffset(ptrType,idxVec);
Vikram S. Adve242a8082002-05-19 15:25:51 +00001000 valueForRegOffset = ConstantSInt::get(Type::LongTy, offset);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001001 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00001002 else
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001003 {
Vikram S. Adve242a8082002-05-19 15:25:51 +00001004 // There is at least one non-constant offset. Therefore, this must
Vikram S. Adveed3fefb2002-08-03 13:48:21 +00001005 // be an array ref, and must have been lowered to a single non-zero
1006 // offset. (An extra leading zero offset, if any, can be ignored.)
1007 // Generate code sequence to compute address from index.
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001008 //
Chris Lattner0374b8d2002-09-11 01:21:35 +00001009 bool firstIdxIsZero =
1010 (idxVec[0] == Constant::getNullValue(idxVec[0]->getType()));
Vikram S. Advebabc0fa2002-09-05 18:32:13 +00001011 assert(idxVec.size() == 1U + firstIdxIsZero
Vikram S. Adveed3fefb2002-08-03 13:48:21 +00001012 && "Array refs must be lowered before Instruction Selection");
1013
Vikram S. Advebabc0fa2002-09-05 18:32:13 +00001014 Value* idxVal = idxVec[firstIdxIsZero];
Vikram S. Adveed3fefb2002-08-03 13:48:21 +00001015
1016 vector<MachineInstr*> mulVec;
Vikram S. Adve94c40812002-09-27 14:33:08 +00001017 Instruction* addr = new TmpInstruction(Type::ULongTy, memInst);
Vikram S. Adveed3fefb2002-08-03 13:48:21 +00001018 MachineCodeForInstruction::get(memInst).addTemp(addr);
1019
Vikram S. Advebabc0fa2002-09-05 18:32:13 +00001020 // Get the array type indexed by idxVal, and compute its element size.
Vikram S. Adveed3fefb2002-08-03 13:48:21 +00001021 // The call to getTypeSize() will fail if size is not constant.
Vikram S. Advebabc0fa2002-09-05 18:32:13 +00001022 const Type* vecType = (firstIdxIsZero
1023 ? GetElementPtrInst::getIndexedType(ptrType,
1024 std::vector<Value*>(1U, idxVec[0]),
1025 /*AllowCompositeLeaf*/ true)
1026 : ptrType);
1027 const Type* eltType = cast<SequentialType>(vecType)->getElementType();
Vikram S. Advee102a642002-09-16 15:56:45 +00001028 ConstantUInt* eltSizeVal = ConstantUInt::get(Type::ULongTy,
Chris Lattnerea45d7b2002-12-28 20:19:44 +00001029 target.getTargetData().getTypeSize(eltType));
Vikram S. Adveed3fefb2002-08-03 13:48:21 +00001030
1031 // CreateMulInstruction() folds constants intelligently enough.
Vikram S. Adved3e26482002-10-13 00:18:57 +00001032 CreateMulInstruction(target, memInst->getParent()->getParent(),
Vikram S. Advebabc0fa2002-09-05 18:32:13 +00001033 idxVal, /* lval, not likely to be const*/
1034 eltSizeVal, /* rval, likely to be constant */
1035 addr, /* result */
Vikram S. Adved3e26482002-10-13 00:18:57 +00001036 mulVec, MachineCodeForInstruction::get(memInst),
Vikram S. Adveed3fefb2002-08-03 13:48:21 +00001037 INVALID_MACHINE_OPCODE);
1038
1039 // Insert mulVec[] before *mvecI in mvec[] and update mvecI
1040 // to point to the same instruction it pointed to before.
1041 assert(mulVec.size() > 0 && "No multiply code created?");
1042 vector<MachineInstr*>::iterator oldMvecI = mvecI;
1043 for (unsigned i=0, N=mulVec.size(); i < N; ++i)
1044 mvecI = mvec.insert(mvecI, mulVec[i]) + 1; // pts to mem instr
1045
1046 valueForRegOffset = addr;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001047 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00001048 }
1049 else
1050 {
1051 offsetOpType = MachineOperand::MO_SignExtendedImmed;
1052 smallConstOffset = 0;
1053 }
Vikram S. Adveed3fefb2002-08-03 13:48:21 +00001054
Vikram S. Advea10d1a72002-03-31 19:07:35 +00001055 // For STORE:
1056 // Operand 0 is value, operand 1 is ptr, operand 2 is offset
1057 // For LOAD or GET_ELEMENT_PTR,
1058 // Operand 0 is ptr, operand 1 is offset, operand 2 is result.
1059 //
1060 unsigned offsetOpNum, ptrOpNum;
1061 if (memInst->getOpcode() == Instruction::Store)
1062 {
1063 (*mvecI)->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister,
1064 vmInstrNode->leftChild()->getValue());
1065 ptrOpNum = 1;
1066 offsetOpNum = 2;
1067 }
1068 else
1069 {
1070 ptrOpNum = 0;
1071 offsetOpNum = 1;
1072 (*mvecI)->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister,
1073 memInst);
1074 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00001075
Vikram S. Advea10d1a72002-03-31 19:07:35 +00001076 (*mvecI)->SetMachineOperandVal(ptrOpNum, MachineOperand::MO_VirtualRegister,
1077 ptrVal);
1078
Chris Lattner20b1ea02001-09-14 03:47:57 +00001079 if (offsetOpType == MachineOperand::MO_VirtualRegister)
1080 {
1081 assert(valueForRegOffset != NULL);
Vikram S. Adve74825322002-03-18 03:15:35 +00001082 (*mvecI)->SetMachineOperandVal(offsetOpNum, offsetOpType,
1083 valueForRegOffset);
Chris Lattner20b1ea02001-09-14 03:47:57 +00001084 }
1085 else
Vikram S. Adve74825322002-03-18 03:15:35 +00001086 (*mvecI)->SetMachineOperandConst(offsetOpNum, offsetOpType,
1087 smallConstOffset);
Chris Lattner20b1ea02001-09-14 03:47:57 +00001088}
1089
1090
Chris Lattner20b1ea02001-09-14 03:47:57 +00001091//
1092// Substitute operand `operandNum' of the instruction in node `treeNode'
Vikram S. Advec025fc12001-10-14 23:28:43 +00001093// in place of the use(s) of that instruction in node `parent'.
1094// Check both explicit and implicit operands!
Vikram S. Adve74825322002-03-18 03:15:35 +00001095// Also make sure to skip over a parent who:
1096// (1) is a list node in the Burg tree, or
1097// (2) itself had its results forwarded to its parent
Chris Lattner20b1ea02001-09-14 03:47:57 +00001098//
1099static void
1100ForwardOperand(InstructionNode* treeNode,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001101 InstrTreeNode* parent,
1102 int operandNum)
Chris Lattner20b1ea02001-09-14 03:47:57 +00001103{
Vikram S. Adve243dd452001-09-18 13:03:13 +00001104 assert(treeNode && parent && "Invalid invocation of ForwardOperand");
1105
Chris Lattner20b1ea02001-09-14 03:47:57 +00001106 Instruction* unusedOp = treeNode->getInstruction();
1107 Value* fwdOp = unusedOp->getOperand(operandNum);
Vikram S. Adve243dd452001-09-18 13:03:13 +00001108
1109 // The parent itself may be a list node, so find the real parent instruction
1110 while (parent->getNodeType() != InstrTreeNode::NTInstructionNode)
1111 {
1112 parent = parent->parent();
1113 assert(parent && "ERROR: Non-instruction node has no parent in tree.");
1114 }
1115 InstructionNode* parentInstrNode = (InstructionNode*) parent;
1116
1117 Instruction* userInstr = parentInstrNode->getInstruction();
Chris Lattner9c461082002-02-03 07:50:56 +00001118 MachineCodeForInstruction &mvec = MachineCodeForInstruction::get(userInstr);
Vikram S. Adve74825322002-03-18 03:15:35 +00001119
1120 // The parent's mvec would be empty if it was itself forwarded.
1121 // Recursively call ForwardOperand in that case...
1122 //
1123 if (mvec.size() == 0)
Chris Lattner20b1ea02001-09-14 03:47:57 +00001124 {
Vikram S. Adve74825322002-03-18 03:15:35 +00001125 assert(parent->parent() != NULL &&
1126 "Parent could not have been forwarded, yet has no instructions?");
1127 ForwardOperand(treeNode, parent->parent(), operandNum);
1128 }
1129 else
1130 {
Vikram S. Adve74825322002-03-18 03:15:35 +00001131 for (unsigned i=0, N=mvec.size(); i < N; i++)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001132 {
Vikram S. Adve74825322002-03-18 03:15:35 +00001133 MachineInstr* minstr = mvec[i];
1134 for (unsigned i=0, numOps=minstr->getNumOperands(); i < numOps; ++i)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001135 {
Vikram S. Adve74825322002-03-18 03:15:35 +00001136 const MachineOperand& mop = minstr->getOperand(i);
Chris Lattner133f0792002-10-28 04:45:29 +00001137 if (mop.getType() == MachineOperand::MO_VirtualRegister &&
Vikram S. Adve74825322002-03-18 03:15:35 +00001138 mop.getVRegValue() == unusedOp)
Vikram S. Adve242a8082002-05-19 15:25:51 +00001139 minstr->SetMachineOperandVal(i,
Vikram S. Adve74825322002-03-18 03:15:35 +00001140 MachineOperand::MO_VirtualRegister, fwdOp);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001141 }
Vikram S. Adve74825322002-03-18 03:15:35 +00001142
1143 for (unsigned i=0,numOps=minstr->getNumImplicitRefs(); i<numOps; ++i)
1144 if (minstr->getImplicitRef(i) == unusedOp)
Vikram S. Adve242a8082002-05-19 15:25:51 +00001145 minstr->setImplicitRef(i, fwdOp,
Vikram S. Adve6418eac2002-07-08 23:30:14 +00001146 minstr->implicitRefIsDefined(i),
1147 minstr->implicitRefIsDefinedAndUsed(i));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001148 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00001149 }
1150}
1151
1152
Vikram S. Adve242a8082002-05-19 15:25:51 +00001153inline bool
1154AllUsesAreBranches(const Instruction* setccI)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001155{
Vikram S. Adve242a8082002-05-19 15:25:51 +00001156 for (Value::use_const_iterator UI=setccI->use_begin(), UE=setccI->use_end();
1157 UI != UE; ++UI)
1158 if (! isa<TmpInstruction>(*UI) // ignore tmp instructions here
1159 && cast<Instruction>(*UI)->getOpcode() != Instruction::Br)
1160 return false;
1161 return true;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001162}
1163
Vikram S. Advefb361122001-10-22 13:36:31 +00001164//******************* Externally Visible Functions *************************/
1165
Vikram S. Advefb361122001-10-22 13:36:31 +00001166//------------------------------------------------------------------------
1167// External Function: ThisIsAChainRule
1168//
1169// Purpose:
1170// Check if a given BURG rule is a chain rule.
1171//------------------------------------------------------------------------
1172
1173extern bool
1174ThisIsAChainRule(int eruleno)
1175{
1176 switch(eruleno)
1177 {
1178 case 111: // stmt: reg
Vikram S. Advefb361122001-10-22 13:36:31 +00001179 case 123:
1180 case 124:
1181 case 125:
1182 case 126:
1183 case 127:
1184 case 128:
1185 case 129:
1186 case 130:
1187 case 131:
1188 case 132:
1189 case 133:
1190 case 155:
1191 case 221:
1192 case 222:
1193 case 241:
1194 case 242:
1195 case 243:
1196 case 244:
Vikram S. Adve30a6f492002-08-22 02:56:10 +00001197 case 245:
Vikram S. Adve85e1e9c2002-04-01 20:28:48 +00001198 case 321:
Vikram S. Advefb361122001-10-22 13:36:31 +00001199 return true; break;
Vikram S. Adve30a6f492002-08-22 02:56:10 +00001200
Vikram S. Advefb361122001-10-22 13:36:31 +00001201 default:
1202 return false; break;
1203 }
1204}
Chris Lattner20b1ea02001-09-14 03:47:57 +00001205
1206
1207//------------------------------------------------------------------------
1208// External Function: GetInstructionsByRule
1209//
1210// Purpose:
1211// Choose machine instructions for the SPARC according to the
1212// patterns chosen by the BURG-generated parser.
1213//------------------------------------------------------------------------
1214
Vikram S. Adve74825322002-03-18 03:15:35 +00001215void
Chris Lattner20b1ea02001-09-14 03:47:57 +00001216GetInstructionsByRule(InstructionNode* subtreeRoot,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001217 int ruleForNode,
1218 short* nts,
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001219 TargetMachine &target,
Vikram S. Adve74825322002-03-18 03:15:35 +00001220 vector<MachineInstr*>& mvec)
Chris Lattner20b1ea02001-09-14 03:47:57 +00001221{
Chris Lattner20b1ea02001-09-14 03:47:57 +00001222 bool checkCast = false; // initialize here to use fall-through
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001223 bool maskUnsignedResult = false;
Chris Lattner20b1ea02001-09-14 03:47:57 +00001224 int nextRule;
1225 int forwardOperandNum = -1;
Chris Lattnerea45d7b2002-12-28 20:19:44 +00001226 unsigned allocaSize = 0;
Vikram S. Adve74825322002-03-18 03:15:35 +00001227 MachineInstr* M, *M2;
Chris Lattnerea45d7b2002-12-28 20:19:44 +00001228 unsigned L;
Vikram S. Adve74825322002-03-18 03:15:35 +00001229
1230 mvec.clear();
Chris Lattner20b1ea02001-09-14 03:47:57 +00001231
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001232 // If the code for this instruction was folded into the parent (user),
1233 // then do nothing!
1234 if (subtreeRoot->isFoldedIntoParent())
1235 return;
1236
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001237 //
1238 // Let's check for chain rules outside the switch so that we don't have
1239 // to duplicate the list of chain rule production numbers here again
1240 //
1241 if (ThisIsAChainRule(ruleForNode))
Chris Lattner20b1ea02001-09-14 03:47:57 +00001242 {
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001243 // Chain rules have a single nonterminal on the RHS.
1244 // Get the rule that matches the RHS non-terminal and use that instead.
1245 //
1246 assert(nts[0] && ! nts[1]
1247 && "A chain rule should have only one RHS non-terminal!");
1248 nextRule = burm_rule(subtreeRoot->state, nts[0]);
1249 nts = burm_nts[nextRule];
Vikram S. Adve74825322002-03-18 03:15:35 +00001250 GetInstructionsByRule(subtreeRoot, nextRule, nts, target, mvec);
Chris Lattner20b1ea02001-09-14 03:47:57 +00001251 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001252 else
Chris Lattner20b1ea02001-09-14 03:47:57 +00001253 {
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001254 switch(ruleForNode) {
1255 case 1: // stmt: Ret
1256 case 2: // stmt: RetValue(reg)
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001257 { // NOTE: Prepass of register allocation is responsible
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001258 // for moving return value to appropriate register.
1259 // Mark the return-address register as a hidden virtual reg.
Vikram S. Advea995e602001-10-11 04:23:19 +00001260 // Mark the return value register as an implicit ref of
1261 // the machine instruction.
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001262 // Finally put a NOP in the delay slot.
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001263 ReturnInst *returnInstr =
1264 cast<ReturnInst>(subtreeRoot->getInstruction());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001265 assert(returnInstr->getOpcode() == Instruction::Ret);
1266
Chris Lattner9c461082002-02-03 07:50:56 +00001267 Instruction* returnReg = new TmpInstruction(returnInstr);
1268 MachineCodeForInstruction::get(returnInstr).addTemp(returnReg);
Vikram S. Advefb361122001-10-22 13:36:31 +00001269
Vikram S. Adve74825322002-03-18 03:15:35 +00001270 M = new MachineInstr(JMPLRET);
Chris Lattner1c7907e2002-10-28 20:11:17 +00001271 M->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister,
1272 returnReg);
Vikram S. Adve74825322002-03-18 03:15:35 +00001273 M->SetMachineOperandConst(1,MachineOperand::MO_SignExtendedImmed,
Chris Lattner1c7907e2002-10-28 20:11:17 +00001274 (int64_t)8);
Vikram S. Adve74825322002-03-18 03:15:35 +00001275 M->SetMachineOperandReg(2, target.getRegInfo().getZeroRegNum());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001276
Vikram S. Advea995e602001-10-11 04:23:19 +00001277 if (returnInstr->getReturnValue() != NULL)
Vikram S. Adve74825322002-03-18 03:15:35 +00001278 M->addImplicitRef(returnInstr->getReturnValue());
Vikram S. Advea995e602001-10-11 04:23:19 +00001279
Vikram S. Adve74825322002-03-18 03:15:35 +00001280 mvec.push_back(M);
1281 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001282
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001283 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001284 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001285
1286 case 3: // stmt: Store(reg,reg)
1287 case 4: // stmt: Store(reg,ptrreg)
Vikram S. Adve74825322002-03-18 03:15:35 +00001288 mvec.push_back(new MachineInstr(
1289 ChooseStoreInstruction(
1290 subtreeRoot->leftChild()->getValue()->getType())));
Vikram S. Adve8cfffd32002-08-24 20:56:53 +00001291 SetOperandsForMemInstr(mvec, subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001292 break;
Chris Lattner20b1ea02001-09-14 03:47:57 +00001293
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001294 case 5: // stmt: BrUncond
Vikram S. Adve74825322002-03-18 03:15:35 +00001295 M = new MachineInstr(BA);
Vikram S. Adve6418eac2002-07-08 23:30:14 +00001296 M->SetMachineOperandVal(0, MachineOperand::MO_PCRelativeDisp,
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001297 cast<BranchInst>(subtreeRoot->getInstruction())->getSuccessor(0));
Vikram S. Adve74825322002-03-18 03:15:35 +00001298 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001299
1300 // delay slot
Vikram S. Adve74825322002-03-18 03:15:35 +00001301 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001302 break;
Chris Lattner20b1ea02001-09-14 03:47:57 +00001303
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001304 case 206: // stmt: BrCond(setCCconst)
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001305 { // setCCconst => boolean was computed with `%b = setCC type reg1 const'
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001306 // If the constant is ZERO, we can use the branch-on-integer-register
1307 // instructions and avoid the SUBcc instruction entirely.
1308 // Otherwise this is just the same as case 5, so just fall through.
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001309 //
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001310 InstrTreeNode* constNode = subtreeRoot->leftChild()->rightChild();
1311 assert(constNode &&
1312 constNode->getNodeType() ==InstrTreeNode::NTConstNode);
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001313 Constant *constVal = cast<Constant>(constNode->getValue());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001314 bool isValidConst;
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001315
Chris Lattner0c4e8862002-09-03 01:08:28 +00001316 if ((constVal->getType()->isInteger()
Chris Lattner9b625032002-05-06 16:15:30 +00001317 || isa<PointerType>(constVal->getType()))
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001318 && GetConstantValueAsSignedInt(constVal, isValidConst) == 0
1319 && isValidConst)
1320 {
1321 // That constant is a zero after all...
1322 // Use the left child of setCC as the first argument!
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001323 // Mark the setCC node so that no code is generated for it.
1324 InstructionNode* setCCNode = (InstructionNode*)
1325 subtreeRoot->leftChild();
1326 assert(setCCNode->getOpLabel() == SetCCOp);
1327 setCCNode->markFoldedIntoParent();
1328
1329 BranchInst* brInst=cast<BranchInst>(subtreeRoot->getInstruction());
1330
Vikram S. Adve74825322002-03-18 03:15:35 +00001331 M = new MachineInstr(ChooseBprInstruction(subtreeRoot));
1332 M->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister,
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001333 setCCNode->leftChild()->getValue());
Vikram S. Adve74825322002-03-18 03:15:35 +00001334 M->SetMachineOperandVal(1, MachineOperand::MO_PCRelativeDisp,
1335 brInst->getSuccessor(0));
1336 mvec.push_back(M);
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001337
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001338 // delay slot
Vikram S. Adve74825322002-03-18 03:15:35 +00001339 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001340
1341 // false branch
Vikram S. Adve74825322002-03-18 03:15:35 +00001342 M = new MachineInstr(BA);
Vikram S. Adve6418eac2002-07-08 23:30:14 +00001343 M->SetMachineOperandVal(0, MachineOperand::MO_PCRelativeDisp,
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001344 brInst->getSuccessor(1));
Vikram S. Adve74825322002-03-18 03:15:35 +00001345 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001346
1347 // delay slot
Vikram S. Adve74825322002-03-18 03:15:35 +00001348 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001349
1350 break;
1351 }
1352 // ELSE FALL THROUGH
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001353 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001354
Vikram S. Adve30a6f492002-08-22 02:56:10 +00001355 case 6: // stmt: BrCond(setCC)
1356 { // bool => boolean was computed with SetCC.
1357 // The branch to use depends on whether it is FP, signed, or unsigned.
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001358 // If it is an integer CC, we also need to find the unique
1359 // TmpInstruction representing that CC.
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001360 //
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001361 BranchInst* brInst = cast<BranchInst>(subtreeRoot->getInstruction());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001362 bool isFPBranch;
Vikram S. Adve74825322002-03-18 03:15:35 +00001363 M = new MachineInstr(ChooseBccInstruction(subtreeRoot, isFPBranch));
Vikram S. Adve30a6f492002-08-22 02:56:10 +00001364
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001365 Value* ccValue = GetTmpForCC(subtreeRoot->leftChild()->getValue(),
1366 brInst->getParent()->getParent(),
1367 isFPBranch? Type::FloatTy : Type::IntTy);
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001368
Vikram S. Adve74825322002-03-18 03:15:35 +00001369 M->SetMachineOperandVal(0, MachineOperand::MO_CCRegister, ccValue);
1370 M->SetMachineOperandVal(1, MachineOperand::MO_PCRelativeDisp,
1371 brInst->getSuccessor(0));
1372 mvec.push_back(M);
Vikram S. Adve30a6f492002-08-22 02:56:10 +00001373
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001374 // delay slot
Vikram S. Adve74825322002-03-18 03:15:35 +00001375 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adve30a6f492002-08-22 02:56:10 +00001376
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001377 // false branch
Vikram S. Adve74825322002-03-18 03:15:35 +00001378 M = new MachineInstr(BA);
Vikram S. Adve6418eac2002-07-08 23:30:14 +00001379 M->SetMachineOperandVal(0, MachineOperand::MO_PCRelativeDisp,
Vikram S. Adve74825322002-03-18 03:15:35 +00001380 brInst->getSuccessor(1));
1381 mvec.push_back(M);
Vikram S. Adve30a6f492002-08-22 02:56:10 +00001382
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001383 // delay slot
Vikram S. Adve74825322002-03-18 03:15:35 +00001384 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001385 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001386 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001387
1388 case 208: // stmt: BrCond(boolconst)
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001389 {
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001390 // boolconst => boolean is a constant; use BA to first or second label
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001391 Constant* constVal =
1392 cast<Constant>(subtreeRoot->leftChild()->getValue());
1393 unsigned dest = cast<ConstantBool>(constVal)->getValue()? 0 : 1;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001394
Vikram S. Adve74825322002-03-18 03:15:35 +00001395 M = new MachineInstr(BA);
Vikram S. Adve6418eac2002-07-08 23:30:14 +00001396 M->SetMachineOperandVal(0, MachineOperand::MO_PCRelativeDisp,
Chris Lattner35504202002-04-27 03:14:39 +00001397 cast<BranchInst>(subtreeRoot->getInstruction())->getSuccessor(dest));
Vikram S. Adve74825322002-03-18 03:15:35 +00001398 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001399
1400 // delay slot
Vikram S. Adve74825322002-03-18 03:15:35 +00001401 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001402 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001403 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001404
1405 case 8: // stmt: BrCond(boolreg)
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001406 { // boolreg => boolean is stored in an existing register.
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001407 // Just use the branch-on-integer-register instruction!
1408 //
Vikram S. Adve74825322002-03-18 03:15:35 +00001409 M = new MachineInstr(BRNZ);
1410 M->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001411 subtreeRoot->leftChild()->getValue());
Vikram S. Adve74825322002-03-18 03:15:35 +00001412 M->SetMachineOperandVal(1, MachineOperand::MO_PCRelativeDisp,
Chris Lattner35504202002-04-27 03:14:39 +00001413 cast<BranchInst>(subtreeRoot->getInstruction())->getSuccessor(0));
Vikram S. Adve74825322002-03-18 03:15:35 +00001414 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001415
1416 // delay slot
Vikram S. Adve74825322002-03-18 03:15:35 +00001417 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001418
1419 // false branch
Vikram S. Adve74825322002-03-18 03:15:35 +00001420 M = new MachineInstr(BA);
Vikram S. Adve6418eac2002-07-08 23:30:14 +00001421 M->SetMachineOperandVal(0, MachineOperand::MO_PCRelativeDisp,
Chris Lattner35504202002-04-27 03:14:39 +00001422 cast<BranchInst>(subtreeRoot->getInstruction())->getSuccessor(1));
Vikram S. Adve74825322002-03-18 03:15:35 +00001423 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001424
1425 // delay slot
Vikram S. Adve74825322002-03-18 03:15:35 +00001426 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001427 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001428 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00001429
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001430 case 9: // stmt: Switch(reg)
1431 assert(0 && "*** SWITCH instruction is not implemented yet.");
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001432 break;
Chris Lattner20b1ea02001-09-14 03:47:57 +00001433
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001434 case 10: // reg: VRegList(reg, reg)
1435 assert(0 && "VRegList should never be the topmost non-chain rule");
1436 break;
1437
Vikram S. Advece08e1d2002-08-15 14:17:37 +00001438 case 21: // bool: Not(bool,reg): Both these are implemented as:
1439 case 421: // reg: BNot(reg,reg): reg = reg XOR-NOT 0
1440 { // First find the unary operand. It may be left or right, usually right.
1441 Value* notArg = BinaryOperator::getNotArgument(
1442 cast<BinaryOperator>(subtreeRoot->getInstruction()));
1443 mvec.push_back(Create3OperandInstr_Reg(XNOR, notArg,
1444 target.getRegInfo().getZeroRegNum(),
1445 subtreeRoot->getValue()));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001446 break;
Vikram S. Advece08e1d2002-08-15 14:17:37 +00001447 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001448
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001449 case 22: // reg: ToBoolTy(reg):
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001450 {
1451 const Type* opType = subtreeRoot->leftChild()->getValue()->getType();
Chris Lattner0c4e8862002-09-03 01:08:28 +00001452 assert(opType->isIntegral() || isa<PointerType>(opType));
Vikram S. Adve74825322002-03-18 03:15:35 +00001453 forwardOperandNum = 0; // forward first operand to user
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001454 break;
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001455 }
1456
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001457 case 23: // reg: ToUByteTy(reg)
Vikram S. Adve94c40812002-09-27 14:33:08 +00001458 case 24: // reg: ToSByteTy(reg)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001459 case 25: // reg: ToUShortTy(reg)
Vikram S. Adve94c40812002-09-27 14:33:08 +00001460 case 26: // reg: ToShortTy(reg)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001461 case 27: // reg: ToUIntTy(reg)
Vikram S. Adve94c40812002-09-27 14:33:08 +00001462 case 28: // reg: ToIntTy(reg)
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001463 {
Vikram S. Adve94c40812002-09-27 14:33:08 +00001464 //======================================================================
1465 // Rules for integer conversions:
1466 //
1467 //--------
1468 // From ISO 1998 C++ Standard, Sec. 4.7:
1469 //
1470 // 2. If the destination type is unsigned, the resulting value is
1471 // the least unsigned integer congruent to the source integer
1472 // (modulo 2n where n is the number of bits used to represent the
1473 // unsigned type). [Note: In a two s complement representation,
1474 // this conversion is conceptual and there is no change in the
1475 // bit pattern (if there is no truncation). ]
1476 //
1477 // 3. If the destination type is signed, the value is unchanged if
1478 // it can be represented in the destination type (and bitfield width);
1479 // otherwise, the value is implementation-defined.
1480 //--------
1481 //
1482 // Since we assume 2s complement representations, this implies:
1483 //
1484 // -- if operand is smaller than destination, zero-extend or sign-extend
1485 // according to the signedness of the *operand*: source decides.
1486 // ==> we have to do nothing here!
1487 //
1488 // -- if operand is same size as or larger than destination, and the
1489 // destination is *unsigned*, zero-extend the operand: dest. decides
1490 //
1491 // -- if operand is same size as or larger than destination, and the
1492 // destination is *signed*, the choice is implementation defined:
1493 // we sign-extend the operand: i.e., again dest. decides.
1494 // Note: this matches both Sun's cc and gcc3.2.
1495 //======================================================================
1496
Vikram S. Adve242a8082002-05-19 15:25:51 +00001497 Instruction* destI = subtreeRoot->getInstruction();
1498 Value* opVal = subtreeRoot->leftChild()->getValue();
Vikram S. Adve94c40812002-09-27 14:33:08 +00001499 const Type* opType = opVal->getType();
Chris Lattner0c4e8862002-09-03 01:08:28 +00001500 if (opType->isIntegral() || isa<PointerType>(opType))
Vikram S. Adve1e606692002-07-31 21:01:34 +00001501 {
Chris Lattnerea45d7b2002-12-28 20:19:44 +00001502 unsigned opSize = target.getTargetData().getTypeSize(opType);
1503 unsigned destSize = target.getTargetData().getTypeSize(destI->getType());
Vikram S. Adve94c40812002-09-27 14:33:08 +00001504 if (opSize >= destSize)
1505 { // Operand is same size as or larger than dest:
1506 // zero- or sign-extend, according to the signeddness of
1507 // the destination (see above).
1508 if (destI->getType()->isSigned())
1509 target.getInstrInfo().CreateSignExtensionInstructions(target,
1510 destI->getParent()->getParent(), opVal, destI, 8*destSize,
1511 mvec, MachineCodeForInstruction::get(destI));
1512 else
1513 target.getInstrInfo().CreateZeroExtensionInstructions(target,
1514 destI->getParent()->getParent(), opVal, destI, 8*destSize,
1515 mvec, MachineCodeForInstruction::get(destI));
Vikram S. Adve1e606692002-07-31 21:01:34 +00001516 }
1517 else
1518 forwardOperandNum = 0; // forward first operand to user
Vikram S. Adve242a8082002-05-19 15:25:51 +00001519 }
Vikram S. Adve1e606692002-07-31 21:01:34 +00001520 else if (opType->isFloatingPoint())
Vikram S. Adve8cfffd32002-08-24 20:56:53 +00001521 {
1522 CreateCodeToConvertFloatToInt(target, opVal, destI, mvec,
1523 MachineCodeForInstruction::get(destI));
Vikram S. Adve94c40812002-09-27 14:33:08 +00001524 if (destI->getType()->isUnsigned())
1525 maskUnsignedResult = true; // not handled by fp->int code
Vikram S. Adve8cfffd32002-08-24 20:56:53 +00001526 }
Vikram S. Adve242a8082002-05-19 15:25:51 +00001527 else
Vikram S. Adve1e606692002-07-31 21:01:34 +00001528 assert(0 && "Unrecognized operand type for convert-to-unsigned");
1529
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001530 break;
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001531 }
Vikram S. Adve94c40812002-09-27 14:33:08 +00001532
1533 case 29: // reg: ToULongTy(reg)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001534 case 30: // reg: ToLongTy(reg)
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001535 {
Vikram S. Adve242a8082002-05-19 15:25:51 +00001536 Value* opVal = subtreeRoot->leftChild()->getValue();
Vikram S. Adve242a8082002-05-19 15:25:51 +00001537 const Type* opType = opVal->getType();
Chris Lattner0c4e8862002-09-03 01:08:28 +00001538 if (opType->isIntegral() || isa<PointerType>(opType))
Vikram S. Adve94c40812002-09-27 14:33:08 +00001539 forwardOperandNum = 0; // forward first operand to user
Vikram S. Adve1e606692002-07-31 21:01:34 +00001540 else if (opType->isFloatingPoint())
Vikram S. Adve94c40812002-09-27 14:33:08 +00001541 {
1542 Instruction* destI = subtreeRoot->getInstruction();
1543 CreateCodeToConvertFloatToInt(target, opVal, destI, mvec,
1544 MachineCodeForInstruction::get(destI));
1545 }
Vikram S. Adve1e606692002-07-31 21:01:34 +00001546 else
1547 assert(0 && "Unrecognized operand type for convert-to-signed");
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001548 break;
Vikram S. Adve94c40812002-09-27 14:33:08 +00001549 }
1550
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001551 case 31: // reg: ToFloatTy(reg):
1552 case 32: // reg: ToDoubleTy(reg):
1553 case 232: // reg: ToDoubleTy(Constant):
Vikram S. Adveec7f4822002-09-09 14:54:21 +00001554
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001555 // If this instruction has a parent (a user) in the tree
1556 // and the user is translated as an FsMULd instruction,
1557 // then the cast is unnecessary. So check that first.
1558 // In the future, we'll want to do the same for the FdMULq instruction,
1559 // so do the check here instead of only for ToFloatTy(reg).
1560 //
Vikram S. Adveec7f4822002-09-09 14:54:21 +00001561 if (subtreeRoot->parent() != NULL)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001562 {
Vikram S. Adveec7f4822002-09-09 14:54:21 +00001563 const MachineCodeForInstruction& mcfi =
1564 MachineCodeForInstruction::get(
1565 cast<InstructionNode>(subtreeRoot->parent())->getInstruction());
1566 if (mcfi.size() == 0 || mcfi.front()->getOpCode() == FSMULD)
1567 forwardOperandNum = 0; // forward first operand to user
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001568 }
Vikram S. Adveec7f4822002-09-09 14:54:21 +00001569
1570 if (forwardOperandNum != 0) // we do need the cast
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001571 {
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001572 Value* leftVal = subtreeRoot->leftChild()->getValue();
1573 const Type* opType = leftVal->getType();
Vikram S. Advedbc4fad2002-04-25 04:37:51 +00001574 MachineOpCode opCode=ChooseConvertToFloatInstr(
1575 subtreeRoot->getOpLabel(), opType);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001576 if (opCode == INVALID_OPCODE) // no conversion needed
1577 {
Vikram S. Adve74825322002-03-18 03:15:35 +00001578 forwardOperandNum = 0; // forward first operand to user
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001579 }
1580 else
1581 {
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001582 // If the source operand is a non-FP type it must be
1583 // first copied from int to float register via memory!
1584 Instruction *dest = subtreeRoot->getInstruction();
1585 Value* srcForCast;
1586 int n = 0;
Vikram S. Adve242a8082002-05-19 15:25:51 +00001587 if (! opType->isFloatingPoint())
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001588 {
1589 // Create a temporary to represent the FP register
1590 // into which the integer will be copied via memory.
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001591 // The type of this temporary will determine the FP
1592 // register used: single-prec for a 32-bit int or smaller,
1593 // double-prec for a 64-bit int.
1594 //
Vikram S. Advebabc0fa2002-09-05 18:32:13 +00001595 uint64_t srcSize =
Chris Lattnerea45d7b2002-12-28 20:19:44 +00001596 target.getTargetData().getTypeSize(leftVal->getType());
Vikram S. Advebabc0fa2002-09-05 18:32:13 +00001597 Type* tmpTypeToUse =
1598 (srcSize <= 4)? Type::FloatTy : Type::DoubleTy;
1599 srcForCast = new TmpInstruction(tmpTypeToUse, dest);
Vikram S. Advedbc4fad2002-04-25 04:37:51 +00001600 MachineCodeForInstruction &destMCFI =
Chris Lattner9c461082002-02-03 07:50:56 +00001601 MachineCodeForInstruction::get(dest);
Vikram S. Advedbc4fad2002-04-25 04:37:51 +00001602 destMCFI.addTemp(srcForCast);
Vikram S. Advebabc0fa2002-09-05 18:32:13 +00001603
Vikram S. Adve242a8082002-05-19 15:25:51 +00001604 target.getInstrInfo().CreateCodeToCopyIntToFloat(target,
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001605 dest->getParent()->getParent(),
Vikram S. Advebabc0fa2002-09-05 18:32:13 +00001606 leftVal, cast<Instruction>(srcForCast),
Vikram S. Adve242a8082002-05-19 15:25:51 +00001607 mvec, destMCFI);
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001608 }
1609 else
1610 srcForCast = leftVal;
Vikram S. Adve94c40812002-09-27 14:33:08 +00001611
Vikram S. Adve74825322002-03-18 03:15:35 +00001612 M = new MachineInstr(opCode);
1613 M->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister,
1614 srcForCast);
1615 M->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister,
1616 dest);
1617 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001618 }
1619 }
1620 break;
1621
1622 case 19: // reg: ToArrayTy(reg):
1623 case 20: // reg: ToPointerTy(reg):
Vikram S. Adve74825322002-03-18 03:15:35 +00001624 forwardOperandNum = 0; // forward first operand to user
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001625 break;
1626
1627 case 233: // reg: Add(reg, Constant)
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001628 maskUnsignedResult = true;
Vikram S. Adve74825322002-03-18 03:15:35 +00001629 M = CreateAddConstInstruction(subtreeRoot);
1630 if (M != NULL)
1631 {
1632 mvec.push_back(M);
1633 break;
1634 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001635 // ELSE FALL THROUGH
Vikram S. Adve74825322002-03-18 03:15:35 +00001636
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001637 case 33: // reg: Add(reg, reg)
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001638 maskUnsignedResult = true;
Vikram S. Adve74825322002-03-18 03:15:35 +00001639 mvec.push_back(new MachineInstr(ChooseAddInstruction(subtreeRoot)));
1640 Set3OperandsFromInstr(mvec.back(), subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001641 break;
1642
1643 case 234: // reg: Sub(reg, Constant)
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001644 maskUnsignedResult = true;
Vikram S. Adve74825322002-03-18 03:15:35 +00001645 M = CreateSubConstInstruction(subtreeRoot);
1646 if (M != NULL)
1647 {
1648 mvec.push_back(M);
1649 break;
1650 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001651 // ELSE FALL THROUGH
Vikram S. Adve74825322002-03-18 03:15:35 +00001652
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001653 case 34: // reg: Sub(reg, reg)
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001654 maskUnsignedResult = true;
Vikram S. Adve74825322002-03-18 03:15:35 +00001655 mvec.push_back(new MachineInstr(ChooseSubInstructionByType(
1656 subtreeRoot->getInstruction()->getType())));
1657 Set3OperandsFromInstr(mvec.back(), subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001658 break;
1659
1660 case 135: // reg: Mul(todouble, todouble)
1661 checkCast = true;
1662 // FALL THROUGH
1663
1664 case 35: // reg: Mul(reg, reg)
Vikram S. Adve74825322002-03-18 03:15:35 +00001665 {
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001666 maskUnsignedResult = true;
Vikram S. Adve74825322002-03-18 03:15:35 +00001667 MachineOpCode forceOp = ((checkCast && BothFloatToDouble(subtreeRoot))
1668 ? FSMULD
1669 : INVALID_MACHINE_OPCODE);
Vikram S. Adve242a8082002-05-19 15:25:51 +00001670 Instruction* mulInstr = subtreeRoot->getInstruction();
1671 CreateMulInstruction(target, mulInstr->getParent()->getParent(),
Vikram S. Adve74825322002-03-18 03:15:35 +00001672 subtreeRoot->leftChild()->getValue(),
1673 subtreeRoot->rightChild()->getValue(),
Vikram S. Adve242a8082002-05-19 15:25:51 +00001674 mulInstr, mvec,
1675 MachineCodeForInstruction::get(mulInstr),forceOp);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001676 break;
Vikram S. Adve74825322002-03-18 03:15:35 +00001677 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001678 case 335: // reg: Mul(todouble, todoubleConst)
1679 checkCast = true;
1680 // FALL THROUGH
1681
1682 case 235: // reg: Mul(reg, Constant)
Vikram S. Adve74825322002-03-18 03:15:35 +00001683 {
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001684 maskUnsignedResult = true;
Vikram S. Adve74825322002-03-18 03:15:35 +00001685 MachineOpCode forceOp = ((checkCast && BothFloatToDouble(subtreeRoot))
1686 ? FSMULD
1687 : INVALID_MACHINE_OPCODE);
Vikram S. Adve242a8082002-05-19 15:25:51 +00001688 Instruction* mulInstr = subtreeRoot->getInstruction();
1689 CreateMulInstruction(target, mulInstr->getParent()->getParent(),
Vikram S. Adve74825322002-03-18 03:15:35 +00001690 subtreeRoot->leftChild()->getValue(),
1691 subtreeRoot->rightChild()->getValue(),
Vikram S. Adve242a8082002-05-19 15:25:51 +00001692 mulInstr, mvec,
1693 MachineCodeForInstruction::get(mulInstr),
1694 forceOp);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001695 break;
Vikram S. Adve74825322002-03-18 03:15:35 +00001696 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001697 case 236: // reg: Div(reg, Constant)
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001698 maskUnsignedResult = true;
Vikram S. Adve74825322002-03-18 03:15:35 +00001699 L = mvec.size();
1700 CreateDivConstInstruction(target, subtreeRoot, mvec);
1701 if (mvec.size() > L)
1702 break;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001703 // ELSE FALL THROUGH
Vikram S. Adve74825322002-03-18 03:15:35 +00001704
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001705 case 36: // reg: Div(reg, reg)
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001706 maskUnsignedResult = true;
Chris Lattnerea45d7b2002-12-28 20:19:44 +00001707 mvec.push_back(new MachineInstr(ChooseDivInstruction(target,
1708 subtreeRoot)));
Vikram S. Adve74825322002-03-18 03:15:35 +00001709 Set3OperandsFromInstr(mvec.back(), subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001710 break;
1711
1712 case 37: // reg: Rem(reg, reg)
1713 case 237: // reg: Rem(reg, Constant)
Vikram S. Adve510eec72001-11-04 21:59:14 +00001714 {
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001715 maskUnsignedResult = true;
Vikram S. Adve510eec72001-11-04 21:59:14 +00001716 Instruction* remInstr = subtreeRoot->getInstruction();
1717
Chris Lattner9c461082002-02-03 07:50:56 +00001718 TmpInstruction* quot = new TmpInstruction(
Vikram S. Adve510eec72001-11-04 21:59:14 +00001719 subtreeRoot->leftChild()->getValue(),
1720 subtreeRoot->rightChild()->getValue());
Chris Lattner9c461082002-02-03 07:50:56 +00001721 TmpInstruction* prod = new TmpInstruction(
Vikram S. Adve510eec72001-11-04 21:59:14 +00001722 quot,
1723 subtreeRoot->rightChild()->getValue());
Chris Lattner9c461082002-02-03 07:50:56 +00001724 MachineCodeForInstruction::get(remInstr).addTemp(quot).addTemp(prod);
Vikram S. Adve510eec72001-11-04 21:59:14 +00001725
Vikram S. Adve74825322002-03-18 03:15:35 +00001726 M = new MachineInstr(ChooseDivInstruction(target, subtreeRoot));
1727 Set3OperandsFromInstr(M, subtreeRoot, target);
1728 M->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister,quot);
1729 mvec.push_back(M);
Vikram S. Adve510eec72001-11-04 21:59:14 +00001730
Vikram S. Advece08e1d2002-08-15 14:17:37 +00001731 M = Create3OperandInstr(ChooseMulInstructionByType(
1732 subtreeRoot->getInstruction()->getType()),
1733 quot, subtreeRoot->rightChild()->getValue(),
1734 prod);
Vikram S. Adve74825322002-03-18 03:15:35 +00001735 mvec.push_back(M);
Vikram S. Adve510eec72001-11-04 21:59:14 +00001736
Vikram S. Adve74825322002-03-18 03:15:35 +00001737 M = new MachineInstr(ChooseSubInstructionByType(
Vikram S. Adve510eec72001-11-04 21:59:14 +00001738 subtreeRoot->getInstruction()->getType()));
Vikram S. Adve74825322002-03-18 03:15:35 +00001739 Set3OperandsFromInstr(M, subtreeRoot, target);
1740 M->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister,prod);
1741 mvec.push_back(M);
Vikram S. Adve510eec72001-11-04 21:59:14 +00001742
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001743 break;
Vikram S. Adve510eec72001-11-04 21:59:14 +00001744 }
1745
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001746 case 38: // bool: And(bool, bool)
1747 case 238: // bool: And(bool, boolconst)
1748 case 338: // reg : BAnd(reg, reg)
1749 case 538: // reg : BAnd(reg, Constant)
Vikram S. Adve74825322002-03-18 03:15:35 +00001750 mvec.push_back(new MachineInstr(AND));
1751 Set3OperandsFromInstr(mvec.back(), subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001752 break;
1753
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001754 case 138: // bool: And(bool, not)
Vikram S. Advece08e1d2002-08-15 14:17:37 +00001755 case 438: // bool: BAnd(bool, bnot)
1756 { // Use the argument of NOT as the second argument!
1757 // Mark the NOT node so that no code is generated for it.
1758 InstructionNode* notNode = (InstructionNode*) subtreeRoot->rightChild();
1759 Value* notArg = BinaryOperator::getNotArgument(
1760 cast<BinaryOperator>(notNode->getInstruction()));
1761 notNode->markFoldedIntoParent();
1762 mvec.push_back(Create3OperandInstr(ANDN,
1763 subtreeRoot->leftChild()->getValue(),
1764 notArg, subtreeRoot->getValue()));
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();
1784 mvec.push_back(Create3OperandInstr(ORN,
1785 subtreeRoot->leftChild()->getValue(),
1786 notArg, subtreeRoot->getValue()));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001787 break;
Vikram S. Advece08e1d2002-08-15 14:17:37 +00001788 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001789
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001790 case 40: // bool: Xor(bool, bool)
1791 case 240: // bool: Xor(bool, boolconst)
1792 case 340: // reg : BXor(reg, reg)
1793 case 540: // reg : BXor(reg, Constant)
Vikram S. Adve74825322002-03-18 03:15:35 +00001794 mvec.push_back(new MachineInstr(XOR));
1795 Set3OperandsFromInstr(mvec.back(), subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001796 break;
1797
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001798 case 140: // bool: Xor(bool, not)
Vikram S. Advece08e1d2002-08-15 14:17:37 +00001799 case 440: // bool: BXor(bool, bnot)
1800 { // Use the argument of NOT as the second argument!
1801 // Mark the NOT node so that no code is generated for it.
1802 InstructionNode* notNode = (InstructionNode*) subtreeRoot->rightChild();
1803 Value* notArg = BinaryOperator::getNotArgument(
1804 cast<BinaryOperator>(notNode->getInstruction()));
1805 notNode->markFoldedIntoParent();
1806 mvec.push_back(Create3OperandInstr(XNOR,
1807 subtreeRoot->leftChild()->getValue(),
1808 notArg, subtreeRoot->getValue()));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001809 break;
Vikram S. Advece08e1d2002-08-15 14:17:37 +00001810 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001811
1812 case 41: // boolconst: SetCC(reg, Constant)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001813 //
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001814 // If the SetCC was folded into the user (parent), it will be
1815 // caught above. All other cases are the same as case 42,
1816 // so just fall through.
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001817 //
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001818 case 42: // bool: SetCC(reg, reg):
1819 {
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001820 // This generates a SUBCC instruction, putting the difference in
1821 // a result register, and setting a condition code.
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001822 //
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001823 // If the boolean result of the SetCC is used by anything other
Vikram S. Adve6418eac2002-07-08 23:30:14 +00001824 // than a branch instruction, or if it is used outside the current
1825 // basic block, the boolean must be
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001826 // computed and stored in the result register. Otherwise, discard
1827 // the difference (by using %g0) and keep only the condition code.
1828 //
1829 // To compute the boolean result in a register we use a conditional
1830 // move, unless the result of the SUBCC instruction can be used as
1831 // the bool! This assumes that zero is FALSE and any non-zero
1832 // integer is TRUE.
1833 //
1834 InstructionNode* parentNode = (InstructionNode*) subtreeRoot->parent();
1835 Instruction* setCCInstr = subtreeRoot->getInstruction();
Vikram S. Adve242a8082002-05-19 15:25:51 +00001836
Vikram S. Adve6418eac2002-07-08 23:30:14 +00001837 bool keepBoolVal = parentNode == NULL ||
1838 ! AllUsesAreBranches(setCCInstr);
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001839 bool subValIsBoolVal = setCCInstr->getOpcode() == Instruction::SetNE;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001840 bool keepSubVal = keepBoolVal && subValIsBoolVal;
1841 bool computeBoolVal = keepBoolVal && ! subValIsBoolVal;
1842
1843 bool mustClearReg;
1844 int valueToMove;
Chris Lattner8e5c0b42001-11-07 14:01:59 +00001845 MachineOpCode movOpCode = 0;
Vikram S. Adve6418eac2002-07-08 23:30:14 +00001846
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001847 // Mark the 4th operand as being a CC register, and as a def
1848 // A TmpInstruction is created to represent the CC "result".
1849 // Unlike other instances of TmpInstruction, this one is used
1850 // by machine code of multiple LLVM instructions, viz.,
1851 // the SetCC and the branch. Make sure to get the same one!
1852 // Note that we do this even for FP CC registers even though they
1853 // are explicit operands, because the type of the operand
1854 // needs to be a floating point condition code, not an integer
1855 // condition code. Think of this as casting the bool result to
1856 // a FP condition code register.
1857 //
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001858 Value* leftVal = subtreeRoot->leftChild()->getValue();
Chris Lattner9b625032002-05-06 16:15:30 +00001859 bool isFPCompare = leftVal->getType()->isFloatingPoint();
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001860
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001861 TmpInstruction* tmpForCC = GetTmpForCC(setCCInstr,
1862 setCCInstr->getParent()->getParent(),
Chris Lattner9b625032002-05-06 16:15:30 +00001863 isFPCompare ? Type::FloatTy : Type::IntTy);
Chris Lattner9c461082002-02-03 07:50:56 +00001864 MachineCodeForInstruction::get(setCCInstr).addTemp(tmpForCC);
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001865
1866 if (! isFPCompare)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001867 {
1868 // Integer condition: dest. should be %g0 or an integer register.
1869 // If result must be saved but condition is not SetEQ then we need
1870 // a separate instruction to compute the bool result, so discard
1871 // result of SUBcc instruction anyway.
1872 //
Vikram S. Adve74825322002-03-18 03:15:35 +00001873 M = new MachineInstr(SUBcc);
1874 Set3OperandsFromInstr(M, subtreeRoot, target, ! keepSubVal);
1875 M->SetMachineOperandVal(3, MachineOperand::MO_CCRegister,
1876 tmpForCC, /*def*/true);
1877 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001878
1879 if (computeBoolVal)
1880 { // recompute bool using the integer condition codes
1881 movOpCode =
1882 ChooseMovpccAfterSub(subtreeRoot,mustClearReg,valueToMove);
1883 }
1884 }
1885 else
1886 {
1887 // FP condition: dest of FCMP should be some FCCn register
Vikram S. Adve74825322002-03-18 03:15:35 +00001888 M = new MachineInstr(ChooseFcmpInstruction(subtreeRoot));
1889 M->SetMachineOperandVal(0, MachineOperand::MO_CCRegister,
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001890 tmpForCC);
Vikram S. Adve74825322002-03-18 03:15:35 +00001891 M->SetMachineOperandVal(1,MachineOperand::MO_VirtualRegister,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001892 subtreeRoot->leftChild()->getValue());
Vikram S. Adve74825322002-03-18 03:15:35 +00001893 M->SetMachineOperandVal(2,MachineOperand::MO_VirtualRegister,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001894 subtreeRoot->rightChild()->getValue());
Vikram S. Adve74825322002-03-18 03:15:35 +00001895 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001896
1897 if (computeBoolVal)
1898 {// recompute bool using the FP condition codes
1899 mustClearReg = true;
1900 valueToMove = 1;
1901 movOpCode = ChooseMovFpccInstruction(subtreeRoot);
1902 }
1903 }
1904
1905 if (computeBoolVal)
1906 {
1907 if (mustClearReg)
1908 {// Unconditionally set register to 0
Vikram S. Adve74825322002-03-18 03:15:35 +00001909 M = new MachineInstr(SETHI);
1910 M->SetMachineOperandConst(0,MachineOperand::MO_UnextendedImmed,
1911 (int64_t)0);
1912 M->SetMachineOperandVal(1, MachineOperand::MO_VirtualRegister,
1913 setCCInstr);
1914 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001915 }
1916
1917 // Now conditionally move `valueToMove' (0 or 1) into the register
Vikram S. Adve6418eac2002-07-08 23:30:14 +00001918 // Mark the register as a use (as well as a def) because the old
1919 // value should be retained if the condition is false.
Vikram S. Adve74825322002-03-18 03:15:35 +00001920 M = new MachineInstr(movOpCode);
1921 M->SetMachineOperandVal(0, MachineOperand::MO_CCRegister,
1922 tmpForCC);
1923 M->SetMachineOperandConst(1, MachineOperand::MO_UnextendedImmed,
1924 valueToMove);
1925 M->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister,
Vikram S. Adve6418eac2002-07-08 23:30:14 +00001926 setCCInstr, /*isDef*/ true,
1927 /*isDefAndUse*/ true);
Vikram S. Adve74825322002-03-18 03:15:35 +00001928 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001929 }
1930 break;
1931 }
1932
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001933 case 51: // reg: Load(reg)
1934 case 52: // reg: Load(ptrreg)
Vikram S. Adve74825322002-03-18 03:15:35 +00001935 mvec.push_back(new MachineInstr(ChooseLoadInstruction(
1936 subtreeRoot->getValue()->getType())));
Vikram S. Adve8cfffd32002-08-24 20:56:53 +00001937 SetOperandsForMemInstr(mvec, subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001938 break;
1939
1940 case 55: // reg: GetElemPtr(reg)
1941 case 56: // reg: GetElemPtrIdx(reg,reg)
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001942 // If the GetElemPtr was folded into the user (parent), it will be
1943 // caught above. For other cases, we have to compute the address.
Vikram S. Adve74825322002-03-18 03:15:35 +00001944 mvec.push_back(new MachineInstr(ADD));
Vikram S. Adve8cfffd32002-08-24 20:56:53 +00001945 SetOperandsForMemInstr(mvec, subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001946 break;
Vikram S. Adved3e26482002-10-13 00:18:57 +00001947
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001948 case 57: // reg: Alloca: Implement as 1 instruction:
1949 { // add %fp, offsetFromFP -> result
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001950 AllocationInst* instr =
1951 cast<AllocationInst>(subtreeRoot->getInstruction());
Chris Lattnerea45d7b2002-12-28 20:19:44 +00001952 unsigned tsize =
1953 target.getTargetData().getTypeSize(instr->getAllocatedType());
Vikram S. Adve74825322002-03-18 03:15:35 +00001954 assert(tsize != 0);
1955 CreateCodeForFixedSizeAlloca(target, instr, tsize, 1, mvec);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001956 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001957 }
Vikram S. Adved3e26482002-10-13 00:18:57 +00001958
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001959 case 58: // reg: Alloca(reg): Implement as 3 instructions:
1960 // mul num, typeSz -> tmp
1961 // sub %sp, tmp -> %sp
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001962 { // add %sp, frameSizeBelowDynamicArea -> result
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001963 AllocationInst* instr =
1964 cast<AllocationInst>(subtreeRoot->getInstruction());
Vikram S. Adve74825322002-03-18 03:15:35 +00001965 const Type* eltType = instr->getAllocatedType();
1966
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001967 // If #elements is constant, use simpler code for fixed-size allocas
Chris Lattnerea45d7b2002-12-28 20:19:44 +00001968 int tsize = (int) target.getTargetData().getTypeSize(eltType);
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001969 Value* numElementsVal = NULL;
1970 bool isArray = instr->isArrayAllocation();
1971
1972 if (!isArray ||
1973 isa<Constant>(numElementsVal = instr->getArraySize()))
1974 { // total size is constant: generate code for fixed-size alloca
Chris Lattnerea45d7b2002-12-28 20:19:44 +00001975 unsigned numElements = isArray?
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001976 cast<ConstantUInt>(numElementsVal)->getValue() : 1;
1977 CreateCodeForFixedSizeAlloca(target, instr, tsize,
1978 numElements, mvec);
1979 }
Vikram S. Adve74825322002-03-18 03:15:35 +00001980 else // total size is not constant.
1981 CreateCodeForVariableSizeAlloca(target, instr, tsize,
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001982 numElementsVal, mvec);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001983 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001984 }
Vikram S. Adved3e26482002-10-13 00:18:57 +00001985
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001986 case 61: // reg: Call
Vikram S. Adve4a8bb2b2002-09-28 16:55:41 +00001987 { // Generate a direct (CALL) or indirect (JMPL) call.
1988 // Mark the return-address register, the indirection
1989 // register (for indirect calls), the operands of the Call,
1990 // and the return value (if any) as implicit operands
1991 // of the machine instruction.
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001992 //
Vikram S. Advedbc4fad2002-04-25 04:37:51 +00001993 // If this is a varargs function, floating point arguments
1994 // have to passed in integer registers so insert
1995 // copy-float-to-int instructions for each float operand.
1996 //
Chris Lattnerb00c5822001-10-02 03:41:24 +00001997 CallInst *callInstr = cast<CallInst>(subtreeRoot->getInstruction());
Chris Lattner749655f2001-10-13 06:54:30 +00001998 Value *callee = callInstr->getCalledValue();
Vikram S. Adve4a8bb2b2002-09-28 16:55:41 +00001999
2000 // Create hidden virtual register for return address with type void*
Vikram S. Adve242a8082002-05-19 15:25:51 +00002001 TmpInstruction* retAddrReg =
Vikram S. Advea10d1a72002-03-31 19:07:35 +00002002 new TmpInstruction(PointerType::get(Type::VoidTy), callInstr);
Chris Lattner9c461082002-02-03 07:50:56 +00002003 MachineCodeForInstruction::get(callInstr).addTemp(retAddrReg);
Vikram S. Adve4a8bb2b2002-09-28 16:55:41 +00002004
Vikram S. Adveea21a6c2001-10-20 20:57:06 +00002005 // Generate the machine instruction and its operands.
2006 // Use CALL for direct function calls; this optimistically assumes
2007 // the PC-relative address fits in the CALL address field (22 bits).
2008 // Use JMPL for indirect calls.
2009 //
Vikram S. Adve4a8bb2b2002-09-28 16:55:41 +00002010 if (isa<Function>(callee)) // direct function call
2011 M = Create1OperandInstr_Addr(CALL, callee);
2012 else // indirect function call
2013 M = Create3OperandInstr_SImmed(JMPLCALL, callee,
2014 (int64_t) 0, retAddrReg);
Vikram S. Adve74825322002-03-18 03:15:35 +00002015 mvec.push_back(M);
Vikram S. Advea10d1a72002-03-31 19:07:35 +00002016
Vikram S. Adve242a8082002-05-19 15:25:51 +00002017 const FunctionType* funcType =
2018 cast<FunctionType>(cast<PointerType>(callee->getType())
2019 ->getElementType());
2020 bool isVarArgs = funcType->isVarArg();
2021 bool noPrototype = isVarArgs && funcType->getNumParams() == 0;
Vikram S. Advedbc4fad2002-04-25 04:37:51 +00002022
Vikram S. Adveaabb5952002-10-29 19:37:31 +00002023 // Use a descriptor to pass information about call arguments
2024 // to the register allocator. This descriptor will be "owned"
2025 // and freed automatically when the MachineCodeForInstruction
2026 // object for the callInstr goes away.
Vikram S. Adve242a8082002-05-19 15:25:51 +00002027 CallArgsDescriptor* argDesc = new CallArgsDescriptor(callInstr,
2028 retAddrReg, isVarArgs, noPrototype);
Vikram S. Advea995e602001-10-11 04:23:19 +00002029
Vikram S. Adve242a8082002-05-19 15:25:51 +00002030 assert(callInstr->getOperand(0) == callee
2031 && "This is assumed in the loop below!");
2032
2033 for (unsigned i=1, N=callInstr->getNumOperands(); i < N; ++i)
2034 {
2035 Value* argVal = callInstr->getOperand(i);
2036 Instruction* intArgReg = NULL;
2037
2038 // Check for FP arguments to varargs functions.
2039 // Any such argument in the first $K$ args must be passed in an
2040 // integer register, where K = #integer argument registers.
2041 if (isVarArgs && argVal->getType()->isFloatingPoint())
2042 {
2043 // If it is a function with no prototype, pass value
2044 // as an FP value as well as a varargs value
2045 if (noPrototype)
2046 argDesc->getArgInfo(i-1).setUseFPArgReg();
2047
2048 // If this arg. is in the first $K$ regs, add a copy
2049 // float-to-int instruction to pass the value as an integer.
Vikram S. Adved3e26482002-10-13 00:18:57 +00002050 if (i <= target.getRegInfo().GetNumOfIntArgRegs())
Vikram S. Adve242a8082002-05-19 15:25:51 +00002051 {
2052 MachineCodeForInstruction &destMCFI =
2053 MachineCodeForInstruction::get(callInstr);
2054 intArgReg = new TmpInstruction(Type::IntTy, argVal);
2055 destMCFI.addTemp(intArgReg);
2056
2057 vector<MachineInstr*> copyMvec;
2058 target.getInstrInfo().CreateCodeToCopyFloatToInt(target,
2059 callInstr->getParent()->getParent(),
2060 argVal, (TmpInstruction*) intArgReg,
2061 copyMvec, destMCFI);
2062 mvec.insert(mvec.begin(),copyMvec.begin(),copyMvec.end());
2063
2064 argDesc->getArgInfo(i-1).setUseIntArgReg();
2065 argDesc->getArgInfo(i-1).setArgCopy(intArgReg);
2066 }
2067 else
2068 // Cannot fit in first $K$ regs so pass the arg on the stack
2069 argDesc->getArgInfo(i-1).setUseStackSlot();
2070 }
2071
2072 if (intArgReg)
2073 mvec.back()->addImplicitRef(intArgReg);
2074
2075 mvec.back()->addImplicitRef(argVal);
2076 }
2077
2078 // Add the return value as an implicit ref. The call operands
2079 // were added above.
Vikram S. Adveea21a6c2001-10-20 20:57:06 +00002080 if (callInstr->getType() != Type::VoidTy)
Vikram S. Adve74825322002-03-18 03:15:35 +00002081 mvec.back()->addImplicitRef(callInstr, /*isDef*/ true);
Vikram S. Advea995e602001-10-11 04:23:19 +00002082
Vikram S. Adveea21a6c2001-10-20 20:57:06 +00002083 // For the CALL instruction, the ret. addr. reg. is also implicit
Chris Lattnerb0d04722002-03-26 17:58:12 +00002084 if (isa<Function>(callee))
Vikram S. Adve74825322002-03-18 03:15:35 +00002085 mvec.back()->addImplicitRef(retAddrReg, /*isDef*/ true);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002086
Vikram S. Adve74825322002-03-18 03:15:35 +00002087 // delay slot
2088 mvec.push_back(new MachineInstr(NOP));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002089 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00002090 }
Vikram S. Adve242a8082002-05-19 15:25:51 +00002091
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002092 case 62: // reg: Shl(reg, reg)
Vikram S. Adve242a8082002-05-19 15:25:51 +00002093 {
2094 Value* argVal1 = subtreeRoot->leftChild()->getValue();
2095 Value* argVal2 = subtreeRoot->rightChild()->getValue();
2096 Instruction* shlInstr = subtreeRoot->getInstruction();
2097
2098 const Type* opType = argVal1->getType();
Chris Lattner0c4e8862002-09-03 01:08:28 +00002099 assert((opType->isInteger() || isa<PointerType>(opType)) &&
2100 "Shl unsupported for other types");
Vikram S. Adve242a8082002-05-19 15:25:51 +00002101
2102 CreateShiftInstructions(target, shlInstr->getParent()->getParent(),
2103 (opType == Type::LongTy)? SLLX : SLL,
2104 argVal1, argVal2, 0, shlInstr, mvec,
2105 MachineCodeForInstruction::get(shlInstr));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002106 break;
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00002107 }
2108
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002109 case 63: // reg: Shr(reg, reg)
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00002110 { const Type* opType = subtreeRoot->leftChild()->getValue()->getType();
Chris Lattner0c4e8862002-09-03 01:08:28 +00002111 assert((opType->isInteger() || isa<PointerType>(opType)) &&
2112 "Shr unsupported for other types");
Vikram S. Adve74825322002-03-18 03:15:35 +00002113 mvec.push_back(new MachineInstr((opType->isSigned()
2114 ? ((opType == Type::LongTy)? SRAX : SRA)
2115 : ((opType == Type::LongTy)? SRLX : SRL))));
2116 Set3OperandsFromInstr(mvec.back(), subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002117 break;
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00002118 }
2119
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002120 case 64: // reg: Phi(reg,reg)
Vikram S. Adve74825322002-03-18 03:15:35 +00002121 break; // don't forward the value
2122
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002123 case 71: // reg: VReg
2124 case 72: // reg: Constant
Vikram S. Adve74825322002-03-18 03:15:35 +00002125 break; // don't forward the value
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002126
2127 default:
2128 assert(0 && "Unrecognized BURG rule");
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002129 break;
2130 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00002131 }
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00002132
Chris Lattner20b1ea02001-09-14 03:47:57 +00002133 if (forwardOperandNum >= 0)
2134 { // We did not generate a machine instruction but need to use operand.
2135 // If user is in the same tree, replace Value in its machine operand.
2136 // If not, insert a copy instruction which should get coalesced away
2137 // by register allocation.
2138 if (subtreeRoot->parent() != NULL)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002139 ForwardOperand(subtreeRoot, subtreeRoot->parent(), forwardOperandNum);
Chris Lattner20b1ea02001-09-14 03:47:57 +00002140 else
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002141 {
Vikram S. Adve7fe27872001-10-18 00:26:20 +00002142 vector<MachineInstr*> minstrVec;
Vikram S. Adve242a8082002-05-19 15:25:51 +00002143 Instruction* instr = subtreeRoot->getInstruction();
2144 target.getInstrInfo().
2145 CreateCopyInstructionsByType(target,
2146 instr->getParent()->getParent(),
2147 instr->getOperand(forwardOperandNum),
2148 instr, minstrVec,
2149 MachineCodeForInstruction::get(instr));
Vikram S. Adve7fe27872001-10-18 00:26:20 +00002150 assert(minstrVec.size() > 0);
Vikram S. Adve74825322002-03-18 03:15:35 +00002151 mvec.insert(mvec.end(), minstrVec.begin(), minstrVec.end());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002152 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00002153 }
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00002154
2155 if (maskUnsignedResult)
2156 { // If result is unsigned and smaller than int reg size,
2157 // we need to clear high bits of result value.
2158 assert(forwardOperandNum < 0 && "Need mask but no instruction generated");
2159 Instruction* dest = subtreeRoot->getInstruction();
Vikram S. Adve8cfffd32002-08-24 20:56:53 +00002160 if (dest->getType()->isUnsigned())
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00002161 {
Chris Lattnerea45d7b2002-12-28 20:19:44 +00002162 unsigned destSize=target.getTargetData().getTypeSize(dest->getType());
Vikram S. Advebabc0fa2002-09-05 18:32:13 +00002163 if (destSize <= 4)
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00002164 { // Mask high bits. Use a TmpInstruction to represent the
2165 // intermediate result before masking. Since those instructions
2166 // have already been generated, go back and substitute tmpI
2167 // for dest in the result position of each one of them.
2168 TmpInstruction *tmpI = new TmpInstruction(dest->getType(), dest,
2169 NULL, "maskHi");
2170 MachineCodeForInstruction::get(dest).addTemp(tmpI);
2171
2172 for (unsigned i=0, N=mvec.size(); i < N; ++i)
2173 mvec[i]->substituteValue(dest, tmpI);
2174
Vikram S. Adve94c40812002-09-27 14:33:08 +00002175 M = Create3OperandInstr_UImmed(SRL, tmpI, 8*(4-destSize), dest);
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00002176 mvec.push_back(M);
2177 }
Chris Lattnerea45d7b2002-12-28 20:19:44 +00002178 else if (destSize < target.getTargetData().getIntegerRegize())
Vikram S. Advebabc0fa2002-09-05 18:32:13 +00002179 assert(0 && "Unsupported type size: 32 < size < 64 bits");
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00002180 }
2181 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00002182}