2 //***************************************************************************
9 // 10/15/01 - Vikram Adve - Created
10 //**************************************************************************/
13 #include "SparcInternals.h"
14 #include "SparcInstrSelectionSupport.h"
15 #include "llvm/Target/Sparc.h"
16 #include "llvm/CodeGen/InstrSelection.h"
17 #include "llvm/CodeGen/InstrSelectionSupport.h"
18 #include "llvm/CodeGen/MachineInstr.h"
19 #include "llvm/CodeGen/MachineCodeForMethod.h"
20 #include "llvm/Method.h"
21 #include "llvm/ConstantVals.h"
22 #include "llvm/DerivedTypes.h"
25 //************************ Internal Functions ******************************/
28 static inline MachineInstr*
29 CreateIntSetInstruction(int64_t C, Value* dest,
30 std::vector<TmpInstruction*>& tempVec)
33 uint64_t absC = (C >= 0)? C : -C;
34 if (absC > (unsigned int) ~0)
35 { // C does not fit in 32 bits
36 TmpInstruction* tmpReg = new TmpInstruction(Type::IntTy);
37 tempVec.push_back(tmpReg);
39 minstr = new MachineInstr(SETX);
40 minstr->SetMachineOperand(0, MachineOperand::MO_SignExtendedImmed, C);
41 minstr->SetMachineOperand(1, MachineOperand::MO_VirtualRegister, tmpReg,
43 minstr->SetMachineOperand(2, MachineOperand::MO_VirtualRegister,dest);
47 minstr = new MachineInstr(SETSW);
48 minstr->SetMachineOperand(0, MachineOperand::MO_SignExtendedImmed, C);
49 minstr->SetMachineOperand(1, MachineOperand::MO_VirtualRegister, dest);
55 static inline MachineInstr*
56 CreateUIntSetInstruction(uint64_t C, Value* dest,
57 std::vector<TmpInstruction*>& tempVec)
60 if (C > (unsigned int) ~0)
61 { // C does not fit in 32 bits
62 TmpInstruction *tmpReg = new TmpInstruction(Type::IntTy);
63 tempVec.push_back(tmpReg);
65 minstr = new MachineInstr(SETX);
66 minstr->SetMachineOperand(0, MachineOperand::MO_SignExtendedImmed, C);
67 minstr->SetMachineOperand(1, MachineOperand::MO_VirtualRegister, tmpReg,
69 minstr->SetMachineOperand(2, MachineOperand::MO_VirtualRegister,dest);
73 minstr = new MachineInstr(SETUW);
74 minstr->SetMachineOperand(0, MachineOperand::MO_UnextendedImmed, C);
75 minstr->SetMachineOperand(1, MachineOperand::MO_VirtualRegister, dest);
81 //************************* External Classes *******************************/
83 //---------------------------------------------------------------------------
84 // class UltraSparcInstrInfo
87 // Information about individual instructions.
88 // Most information is stored in the SparcMachineInstrDesc array above.
89 // Other information is computed on demand, and most such functions
90 // default to member functions in base class MachineInstrInfo.
91 //---------------------------------------------------------------------------
94 UltraSparcInstrInfo::UltraSparcInstrInfo(const TargetMachine& tgt)
95 : MachineInstrInfo(tgt, SparcMachineInstrDesc,
96 /*descSize = */ NUM_TOTAL_OPCODES,
97 /*numRealOpCodes = */ NUM_REAL_OPCODES)
102 // Create an instruction sequence to put the constant `val' into
103 // the virtual register `dest'. `val' may be a Constant or a
104 // GlobalValue, viz., the constant address of a global variable or function.
105 // The generated instructions are returned in `minstrVec'.
106 // Any temp. registers (TmpInstruction) created are returned in `tempVec'.
109 UltraSparcInstrInfo::CreateCodeToLoadConst(Value* val,
111 std::vector<MachineInstr*>& minstrVec,
112 std::vector<TmpInstruction*>& tempVec) const
114 MachineInstr* minstr;
116 assert(isa<Constant>(val) || isa<GlobalValue>(val) &&
117 "I only know about constant values and global addresses");
119 // Use a "set" instruction for known constants that can go in an integer reg.
120 // Use a "load" instruction for all other constants, in particular,
121 // floating point constants and addresses of globals.
123 const Type* valType = val->getType();
125 if (valType->isIntegral() || valType == Type::BoolTy)
127 if (ConstantUInt* uval = dyn_cast<ConstantUInt>(val))
129 uint64_t C = uval->getValue();
130 minstr = CreateUIntSetInstruction(C, dest, tempVec);
134 bool isValidConstant;
135 int64_t C = GetConstantValueAsSignedInt(val, isValidConstant);
136 assert(isValidConstant && "Unrecognized constant");
137 minstr = CreateIntSetInstruction(C, dest, tempVec);
139 minstrVec.push_back(minstr);
143 // Make an instruction sequence to load the constant, viz:
144 // SETX <addr-of-constant>, tmpReg, addrReg
145 // LOAD /*addr*/ addrReg, /*offset*/ 0, dest
146 // Only the SETX is needed if `val' is a GlobalValue, i.e,. it is
147 // itself a constant address. Otherwise, both are needed.
150 int64_t zeroOffset = 0; // to avoid ambiguity with (Value*) 0
152 TmpInstruction* tmpReg =
153 new TmpInstruction(PointerType::get(val->getType()), val);
154 tempVec.push_back(tmpReg);
156 if (isa<Constant>(val))
158 // Create another TmpInstruction for the hidden integer register
159 TmpInstruction* addrReg =
160 new TmpInstruction(PointerType::get(val->getType()), val);
161 tempVec.push_back(addrReg);
167 minstr = new MachineInstr(SETX);
168 minstr->SetMachineOperand(0, MachineOperand::MO_PCRelativeDisp, val);
169 minstr->SetMachineOperand(1, MachineOperand::MO_VirtualRegister, tmpReg,
171 minstr->SetMachineOperand(2, MachineOperand::MO_VirtualRegister,addrVal);
172 minstrVec.push_back(minstr);
174 if (isa<Constant>(val))
176 // addrVal->addMachineInstruction(minstr);
178 minstr = new MachineInstr(ChooseLoadInstruction(val->getType()));
179 minstr->SetMachineOperand(0, MachineOperand::MO_VirtualRegister,
181 minstr->SetMachineOperand(1, MachineOperand::MO_SignExtendedImmed,
183 minstr->SetMachineOperand(2, MachineOperand::MO_VirtualRegister,
185 minstrVec.push_back(minstr);
191 // Create an instruction sequence to copy an integer value `val'
192 // to a floating point value `dest' by copying to memory and back.
193 // val must be an integral type. dest must be a Float or Double.
194 // The generated instructions are returned in `minstrVec'.
195 // Any temp. registers (TmpInstruction) created are returned in `tempVec'.
198 UltraSparcInstrInfo::CreateCodeToCopyIntToFloat(Method* method,
201 std::vector<MachineInstr*>& minstrVec,
202 std::vector<TmpInstruction*>& tempVec,
203 TargetMachine& target) const
205 assert((val->getType()->isIntegral() || val->getType()->isPointerType())
206 && "Source type must be integral");
207 assert((dest->getType() ==Type::FloatTy || dest->getType() ==Type::DoubleTy)
208 && "Dest type must be float/double");
210 MachineCodeForMethod& mcinfo = MachineCodeForMethod::get(method);
211 int offset = mcinfo.allocateLocalVar(target, val);
213 // Store instruction stores `val' to [%fp+offset].
214 // The store and load opCodes are based on the value being copied, and
215 // they use integer and float types that accomodate the
216 // larger of the source type and the destination type:
217 // On SparcV9: int for float, long for double.
219 Type* tmpType = (dest->getType() == Type::FloatTy)? Type::IntTy
221 MachineInstr* store = new MachineInstr(ChooseStoreInstruction(tmpType));
222 store->SetMachineOperand(0, MachineOperand::MO_VirtualRegister, val);
223 store->SetMachineOperand(1, target.getRegInfo().getFramePointer());
224 store->SetMachineOperand(2, MachineOperand::MO_SignExtendedImmed, offset);
225 minstrVec.push_back(store);
227 // Load instruction loads [%fp+offset] to `dest'.
229 MachineInstr* load =new MachineInstr(ChooseLoadInstruction(dest->getType()));
230 load->SetMachineOperand(0, target.getRegInfo().getFramePointer());
231 load->SetMachineOperand(1, MachineOperand::MO_SignExtendedImmed, offset);
232 load->SetMachineOperand(2, MachineOperand::MO_VirtualRegister, dest);
233 minstrVec.push_back(load);
237 // Similarly, create an instruction sequence to copy an FP value
238 // `val' to an integer value `dest' by copying to memory and back.
239 // See the previous function for information about return values.
242 UltraSparcInstrInfo::CreateCodeToCopyFloatToInt(Method* method,
245 std::vector<MachineInstr*>& minstrVec,
246 std::vector<TmpInstruction*>& tempVec,
247 TargetMachine& target) const
249 assert((val->getType() ==Type::FloatTy || val->getType() ==Type::DoubleTy)
250 && "Source type must be float/double");
251 assert((dest->getType()->isIntegral() || dest->getType()->isPointerType())
252 && "Dest type must be integral");
254 MachineCodeForMethod& mcinfo = MachineCodeForMethod::get(method);
255 int offset = mcinfo.allocateLocalVar(target, val);
257 // Store instruction stores `val' to [%fp+offset].
258 // The store and load opCodes are based on the value being copied, and
259 // they use the integer type that matches the source type in size:
260 // On SparcV9: int for float, long for double.
262 Type* tmpType = (val->getType() == Type::FloatTy)? Type::IntTy
264 MachineInstr* store=new MachineInstr(ChooseStoreInstruction(val->getType()));
265 store->SetMachineOperand(0, MachineOperand::MO_VirtualRegister, val);
266 store->SetMachineOperand(1, target.getRegInfo().getFramePointer());
267 store->SetMachineOperand(2, MachineOperand::MO_SignExtendedImmed, offset);
268 minstrVec.push_back(store);
270 // Load instruction loads [%fp+offset] to `dest'.
272 MachineInstr* load = new MachineInstr(ChooseLoadInstruction(tmpType));
273 load->SetMachineOperand(0, target.getRegInfo().getFramePointer());
274 load->SetMachineOperand(1, MachineOperand::MO_SignExtendedImmed, offset);
275 load->SetMachineOperand(2, MachineOperand::MO_VirtualRegister, dest);
276 minstrVec.push_back(load);