blob: 93f7618641551a1595bb12ae796efbb00eb33c3c [file] [log] [blame]
Chris Lattner035dfbe2002-08-09 20:08:06 +00001//===-- InstrSelectionSupport.cpp -----------------------------------------===//
John Criswellb576c942003-10-20 19:43:21 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
Chris Lattner035dfbe2002-08-09 20:08:06 +00009//
10// Target-independent instruction selection code. See SparcInstrSelection.cpp
11// for usage.
Vikram S. Advea1d14f32001-10-10 20:50:43 +000012//
Chris Lattner035dfbe2002-08-09 20:08:06 +000013//===----------------------------------------------------------------------===//
Vikram S. Advea1d14f32001-10-10 20:50:43 +000014
15#include "llvm/CodeGen/InstrSelectionSupport.h"
16#include "llvm/CodeGen/InstrSelection.h"
Vikram S. Adve36f0a9e2002-05-19 15:34:29 +000017#include "llvm/CodeGen/MachineInstrAnnot.h"
Chris Lattnerfb3b1ec2002-02-03 07:39:06 +000018#include "llvm/CodeGen/MachineCodeForInstruction.h"
Chris Lattnerfb3b1ec2002-02-03 07:39:06 +000019#include "llvm/CodeGen/InstrForest.h"
Vikram S. Advea1d14f32001-10-10 20:50:43 +000020#include "llvm/Target/TargetMachine.h"
Chris Lattnerd0f166a2002-12-29 03:13:05 +000021#include "llvm/Target/TargetRegInfo.h"
Chris Lattner3501fea2003-01-14 22:00:31 +000022#include "llvm/Target/TargetInstrInfo.h"
Chris Lattner31bcdb82002-04-28 19:55:58 +000023#include "llvm/Constants.h"
Chris Lattner795ba6c2003-01-15 21:36:50 +000024#include "llvm/BasicBlock.h"
Chris Lattnerc5b8b1a2002-10-28 23:54:47 +000025#include "llvm/DerivedTypes.h"
Chris Lattner18153832003-07-23 14:55:59 +000026#include "../../Target/Sparc/SparcInstrSelectionSupport.h" // FIXME!
Vikram S. Advea1d14f32001-10-10 20:50:43 +000027
Vikram S. Advea1d14f32001-10-10 20:50:43 +000028
Vikram S. Adve36f0a9e2002-05-19 15:34:29 +000029// Generate code to load the constant into a TmpInstruction (virtual reg) and
30// returns the virtual register.
31//
Vikram S. Adve6d353262001-10-17 23:57:50 +000032static TmpInstruction*
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000033InsertCodeToLoadConstant(Function *F,
Vikram S. Adve42f63202002-03-18 03:33:43 +000034 Value* opValue,
Vikram S. Adve6d353262001-10-17 23:57:50 +000035 Instruction* vmInstr,
Chris Lattner18153832003-07-23 14:55:59 +000036 std::vector<MachineInstr*>& loadConstVec,
Vikram S. Adve6d353262001-10-17 23:57:50 +000037 TargetMachine& target)
Vikram S. Advea1d14f32001-10-10 20:50:43 +000038{
Vikram S. Adve6d353262001-10-17 23:57:50 +000039 // Create a tmp virtual register to hold the constant.
Vikram S. Adve36f0a9e2002-05-19 15:34:29 +000040 MachineCodeForInstruction &mcfi = MachineCodeForInstruction::get(vmInstr);
Vikram S. Advef3d3ca12003-05-31 07:41:24 +000041 TmpInstruction* tmpReg = new TmpInstruction(mcfi, opValue);
Vikram S. Advea1d14f32001-10-10 20:50:43 +000042
Vikram S. Adve36f0a9e2002-05-19 15:34:29 +000043 target.getInstrInfo().CreateCodeToLoadConst(target, F, opValue, tmpReg,
44 loadConstVec, mcfi);
Vikram S. Adve6d353262001-10-17 23:57:50 +000045
46 // Record the mapping from the tmp VM instruction to machine instruction.
47 // Do this for all machine instructions that were not mapped to any
48 // other temp values created by
49 // tmpReg->addMachineInstruction(loadConstVec.back());
50
51 return tmpReg;
Vikram S. Advea1d14f32001-10-10 20:50:43 +000052}
53
54
Vikram S. Advea1d14f32001-10-10 20:50:43 +000055MachineOperand::MachineOperandType
Vikram S. Advefd0ec802002-09-16 15:15:57 +000056ChooseRegOrImmed(int64_t intValue,
57 bool isSigned,
58 MachineOpCode opCode,
59 const TargetMachine& target,
60 bool canUseImmed,
61 unsigned int& getMachineRegNum,
62 int64_t& getImmedValue)
63{
64 MachineOperand::MachineOperandType opType=MachineOperand::MO_VirtualRegister;
65 getMachineRegNum = 0;
66 getImmedValue = 0;
67
68 if (canUseImmed &&
Misha Brukman5e152592003-10-23 17:39:37 +000069 target.getInstrInfo().constantFitsInImmedField(opCode, intValue)) {
Vikram S. Advefd0ec802002-09-16 15:15:57 +000070 opType = isSigned? MachineOperand::MO_SignExtendedImmed
71 : MachineOperand::MO_UnextendedImmed;
72 getImmedValue = intValue;
Misha Brukman5e152592003-10-23 17:39:37 +000073 } else if (intValue == 0 && target.getRegInfo().getZeroRegNum() >= 0) {
74 opType = MachineOperand::MO_MachineRegister;
75 getMachineRegNum = target.getRegInfo().getZeroRegNum();
76 }
Vikram S. Advefd0ec802002-09-16 15:15:57 +000077
78 return opType;
79}
80
81
82MachineOperand::MachineOperandType
Vikram S. Advea1d14f32001-10-10 20:50:43 +000083ChooseRegOrImmed(Value* val,
84 MachineOpCode opCode,
85 const TargetMachine& target,
86 bool canUseImmed,
87 unsigned int& getMachineRegNum,
88 int64_t& getImmedValue)
89{
Vikram S. Advea1d14f32001-10-10 20:50:43 +000090 getMachineRegNum = 0;
91 getImmedValue = 0;
Vikram S. Advefd0ec802002-09-16 15:15:57 +000092
93 // To use reg or immed, constant needs to be integer, bool, or a NULL pointer
Vikram S. Adveb5161b62003-07-29 19:50:12 +000094 // TargetInstrInfo::ConvertConstantToIntType() does the right conversions:
95 bool isValidConstant;
96 uint64_t valueToUse =
97 target.getInstrInfo().ConvertConstantToIntType(target, val, val->getType(),
98 isValidConstant);
99 if (! isValidConstant)
Vikram S. Advefd0ec802002-09-16 15:15:57 +0000100 return MachineOperand::MO_VirtualRegister;
Vikram S. Advea1d14f32001-10-10 20:50:43 +0000101
Vikram S. Adveb5161b62003-07-29 19:50:12 +0000102 // Now check if the constant value fits in the IMMED field.
Vikram S. Advea1d14f32001-10-10 20:50:43 +0000103 //
Vikram S. Adveb5161b62003-07-29 19:50:12 +0000104 return ChooseRegOrImmed((int64_t) valueToUse, val->getType()->isSigned(),
Vikram S. Advefd0ec802002-09-16 15:15:57 +0000105 opCode, target, canUseImmed,
106 getMachineRegNum, getImmedValue);
Vikram S. Advea1d14f32001-10-10 20:50:43 +0000107}
108
Vikram S. Adve6d353262001-10-17 23:57:50 +0000109//---------------------------------------------------------------------------
110// Function: FixConstantOperandsForInstr
111//
112// Purpose:
113// Special handling for constant operands of a machine instruction
114// -- if the constant is 0, use the hardwired 0 register, if any;
115// -- if the constant fits in the IMMEDIATE field, use that field;
116// -- else create instructions to put the constant into a register, either
117// directly or by loading explicitly from the constant pool.
118//
119// In the first 2 cases, the operand of `minstr' is modified in place.
120// Returns a vector of machine instructions generated for operands that
121// fall under case 3; these must be inserted before `minstr'.
122//---------------------------------------------------------------------------
123
Chris Lattner18153832003-07-23 14:55:59 +0000124std::vector<MachineInstr*>
Vikram S. Adve6d353262001-10-17 23:57:50 +0000125FixConstantOperandsForInstr(Instruction* vmInstr,
126 MachineInstr* minstr,
127 TargetMachine& target)
128{
Chris Lattner18153832003-07-23 14:55:59 +0000129 std::vector<MachineInstr*> MVec;
Vikram S. Adve6d353262001-10-17 23:57:50 +0000130
Vikram S. Advefd0ec802002-09-16 15:15:57 +0000131 MachineOpCode opCode = minstr->getOpCode();
Chris Lattner3501fea2003-01-14 22:00:31 +0000132 const TargetInstrInfo& instrInfo = target.getInstrInfo();
Chris Lattner8f780272002-10-29 17:25:41 +0000133 int resultPos = instrInfo.getResultPos(opCode);
Vikram S. Advefd0ec802002-09-16 15:15:57 +0000134 int immedPos = instrInfo.getImmedConstantPos(opCode);
135
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000136 Function *F = vmInstr->getParent()->getParent();
Vikram S. Advefd0ec802002-09-16 15:15:57 +0000137
Vikram S. Adve6d353262001-10-17 23:57:50 +0000138 for (unsigned op=0; op < minstr->getNumOperands(); op++)
139 {
140 const MachineOperand& mop = minstr->getOperand(op);
141
Vikram S. Advefd0ec802002-09-16 15:15:57 +0000142 // Skip the result position, preallocated machine registers, or operands
143 // that cannot be constants (CC regs or PC-relative displacements)
Chris Lattner8f780272002-10-29 17:25:41 +0000144 if (resultPos == (int)op ||
Chris Lattner133f0792002-10-28 04:45:29 +0000145 mop.getType() == MachineOperand::MO_MachineRegister ||
146 mop.getType() == MachineOperand::MO_CCRegister ||
147 mop.getType() == MachineOperand::MO_PCRelativeDisp)
Vikram S. Advefd0ec802002-09-16 15:15:57 +0000148 continue;
149
Vikram S. Adve6d353262001-10-17 23:57:50 +0000150 bool constantThatMustBeLoaded = false;
Vikram S. Advefd0ec802002-09-16 15:15:57 +0000151 unsigned int machineRegNum = 0;
152 int64_t immedValue = 0;
153 Value* opValue = NULL;
154 MachineOperand::MachineOperandType opType =
155 MachineOperand::MO_VirtualRegister;
156
157 // Operand may be a virtual register or a compile-time constant
Misha Brukman5e152592003-10-23 17:39:37 +0000158 if (mop.getType() == MachineOperand::MO_VirtualRegister) {
159 assert(mop.getVRegValue() != NULL);
160 opValue = mop.getVRegValue();
161 if (Constant *opConst = dyn_cast<Constant>(opValue)) {
162 opType = ChooseRegOrImmed(opConst, opCode, target,
163 (immedPos == (int)op), machineRegNum,
164 immedValue);
Vikram S. Advefd0ec802002-09-16 15:15:57 +0000165 if (opType == MachineOperand::MO_VirtualRegister)
Misha Brukman5e152592003-10-23 17:39:37 +0000166 constantThatMustBeLoaded = true;
Vikram S. Advefd0ec802002-09-16 15:15:57 +0000167 }
Misha Brukman5e152592003-10-23 17:39:37 +0000168 } else {
169 assert(mop.isImmediate());
170 bool isSigned = mop.getType() == MachineOperand::MO_SignExtendedImmed;
171
172 // Bit-selection flags indicate an instruction that is extracting
173 // bits from its operand so ignore this even if it is a big constant.
174 if (mop.opHiBits32() || mop.opLoBits32() ||
175 mop.opHiBits64() || mop.opLoBits64())
176 continue;
177
178 opType = ChooseRegOrImmed(mop.getImmedValue(), isSigned,
179 opCode, target, (immedPos == (int)op),
180 machineRegNum, immedValue);
181
182 if (opType == MachineOperand::MO_SignExtendedImmed ||
183 opType == MachineOperand::MO_UnextendedImmed) {
184 // The optype is an immediate value
185 // This means we need to change the opcode, e.g. ADDr -> ADDi
186 unsigned newOpcode = convertOpcodeFromRegToImm(opCode);
187 minstr->setOpcode(newOpcode);
188 }
189
190 if (opType == mop.getType())
191 continue; // no change: this is the most common case
192
193 if (opType == MachineOperand::MO_VirtualRegister) {
194 constantThatMustBeLoaded = true;
195 opValue = isSigned
196 ? (Value*)ConstantSInt::get(Type::LongTy, immedValue)
197 : (Value*)ConstantUInt::get(Type::ULongTy,(uint64_t)immedValue);
198 }
199 }
Vikram S. Advefd0ec802002-09-16 15:15:57 +0000200
201 if (opType == MachineOperand::MO_MachineRegister)
202 minstr->SetMachineOperandReg(op, machineRegNum);
203 else if (opType == MachineOperand::MO_SignExtendedImmed ||
Misha Brukmanc740aae2003-06-03 03:18:20 +0000204 opType == MachineOperand::MO_UnextendedImmed) {
Vikram S. Advefd0ec802002-09-16 15:15:57 +0000205 minstr->SetMachineOperandConst(op, opType, immedValue);
Misha Brukman6fe69052003-06-07 02:34:43 +0000206 // The optype is or has become an immediate
207 // This means we need to change the opcode, e.g. ADDr -> ADDi
208 unsigned newOpcode = convertOpcodeFromRegToImm(opCode);
209 minstr->setOpcode(newOpcode);
Misha Brukmanc740aae2003-06-03 03:18:20 +0000210 } else if (constantThatMustBeLoaded ||
Vikram S. Advefd0ec802002-09-16 15:15:57 +0000211 (opValue && isa<GlobalValue>(opValue)))
212 { // opValue is a constant that must be explicitly loaded into a reg
213 assert(opValue);
214 TmpInstruction* tmpReg = InsertCodeToLoadConstant(F, opValue, vmInstr,
Chris Lattner04120772003-01-15 19:47:53 +0000215 MVec, target);
Vikram S. Adve42f63202002-03-18 03:33:43 +0000216 minstr->SetMachineOperandVal(op, MachineOperand::MO_VirtualRegister,
217 tmpReg);
Vikram S. Adve6d353262001-10-17 23:57:50 +0000218 }
219 }
220
Vikram S. Adve36f0a9e2002-05-19 15:34:29 +0000221 // Also, check for implicit operands used by the machine instruction
222 // (no need to check those defined since they cannot be constants).
223 // These include:
Vikram S. Adve6d353262001-10-17 23:57:50 +0000224 // -- arguments to a Call
225 // -- return value of a Return
226 // Any such operand that is a constant value needs to be fixed also.
227 // The current instructions with implicit refs (viz., Call and Return)
228 // have no immediate fields, so the constant always needs to be loaded
229 // into a register.
230 //
Vikram S. Advefd0ec802002-09-16 15:15:57 +0000231 bool isCall = instrInfo.isCall(opCode);
Vikram S. Adve36f0a9e2002-05-19 15:34:29 +0000232 unsigned lastCallArgNum = 0; // unused if not a call
233 CallArgsDescriptor* argDesc = NULL; // unused if not a call
234 if (isCall)
235 argDesc = CallArgsDescriptor::get(minstr);
236
Vikram S. Adve6d353262001-10-17 23:57:50 +0000237 for (unsigned i=0, N=minstr->getNumImplicitRefs(); i < N; ++i)
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000238 if (isa<Constant>(minstr->getImplicitRef(i)) ||
Vikram S. Adve6d353262001-10-17 23:57:50 +0000239 isa<GlobalValue>(minstr->getImplicitRef(i)))
240 {
Vikram S. Adve94e40ef2001-10-28 21:46:23 +0000241 Value* oldVal = minstr->getImplicitRef(i);
Vikram S. Adve6d353262001-10-17 23:57:50 +0000242 TmpInstruction* tmpReg =
Chris Lattner04120772003-01-15 19:47:53 +0000243 InsertCodeToLoadConstant(F, oldVal, vmInstr, MVec, target);
Vikram S. Adve6d353262001-10-17 23:57:50 +0000244 minstr->setImplicitRef(i, tmpReg);
Vikram S. Adve36f0a9e2002-05-19 15:34:29 +0000245
Misha Brukman5e152592003-10-23 17:39:37 +0000246 if (isCall) {
247 // find and replace the argument in the CallArgsDescriptor
248 unsigned i=lastCallArgNum;
249 while (argDesc->getArgInfo(i).getArgVal() != oldVal)
250 ++i;
251 assert(i < argDesc->getNumArgs() &&
252 "Constant operands to a call *must* be in the arg list");
253 lastCallArgNum = i;
254 argDesc->getArgInfo(i).replaceArgVal(tmpReg);
255 }
Vikram S. Adve6d353262001-10-17 23:57:50 +0000256 }
257
Chris Lattner04120772003-01-15 19:47:53 +0000258 return MVec;
Vikram S. Adve6d353262001-10-17 23:57:50 +0000259}