1 //===--- AMDILLiteralManager.cpp - AMDIL Literal Manager Pass --*- C++ -*--===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //==-----------------------------------------------------------------------===//
10 #define DEBUG_TYPE "literal_manager"
14 #include "AMDILAlgorithms.tpp"
15 #include "AMDILMachineFunctionInfo.h"
16 #include "AMDILSubtarget.h"
17 #include "AMDILTargetMachine.h"
18 #include "llvm/CodeGen/MachineFunctionPass.h"
19 #include "llvm/CodeGen/Passes.h"
20 #include "llvm/Support/Debug.h"
21 #include "llvm/Target/TargetMachine.h"
26 // AMDIL Literal Manager traverses through all of the LOADCONST instructions and
27 // converts them from an immediate value to the literal index. The literal index
28 // is valid IL, but the immediate values are not. The Immediate values must be
29 // aggregated and declared for clarity and to reduce the number of literals that
30 // are used. It is also illegal to declare the same literal twice, so this keeps
31 // that from occuring.
34 class AMDILLiteralManager
: public MachineFunctionPass
{
37 AMDILLiteralManager(TargetMachine
&tm AMDIL_OPT_LEVEL_DECL
);
38 virtual const char *getPassName() const;
40 bool runOnMachineFunction(MachineFunction
&MF
);
42 bool trackLiterals(MachineBasicBlock::iterator
*bbb
);
44 const AMDILSubtarget
*mSTM
;
45 AMDILMachineFunctionInfo
*mMFI
;
49 char AMDILLiteralManager::ID
= 0;
54 createAMDILLiteralManager(TargetMachine
&tm AMDIL_OPT_LEVEL_DECL
) {
55 return new AMDILLiteralManager(tm AMDIL_OPT_LEVEL_VAR
);
60 AMDILLiteralManager::AMDILLiteralManager(TargetMachine
&tm
62 : MachineFunctionPass(ID
),
66 bool AMDILLiteralManager::runOnMachineFunction(MachineFunction
&MF
) {
68 mMFI
= MF
.getInfo
<AMDILMachineFunctionInfo
>();
69 const AMDILTargetMachine
*amdtm
=
70 reinterpret_cast<const AMDILTargetMachine
*>(&TM
);
71 mSTM
= dynamic_cast<const AMDILSubtarget
*>(amdtm
->getSubtargetImpl());
72 safeNestedForEach(MF
.begin(), MF
.end(), MF
.begin()->begin(),
73 std::bind1st(std::mem_fun(&AMDILLiteralManager::trackLiterals
), this));
77 bool AMDILLiteralManager::trackLiterals(MachineBasicBlock::iterator
*bbb
) {
78 MachineInstr
*MI
= *bbb
;
79 uint32_t Opcode
= MI
->getOpcode();
83 case AMDIL::LOADCONST_i8
:
84 case AMDIL::LOADCONST_i16
:
85 case AMDIL::LOADCONST_i32
:
86 case AMDIL::LOADCONST_i64
:
87 case AMDIL::LOADCONST_f32
:
88 case AMDIL::LOADCONST_f64
:
91 MachineOperand
&dstOp
= MI
->getOperand(0);
92 MachineOperand
&litOp
= MI
->getOperand(1);
93 if (!litOp
.isImm() && !litOp
.isFPImm()) {
99 // Change the literal to the correct index for each literal that is found.
101 int64_t immVal
= litOp
.getImm();
102 uint32_t idx
= MI
->getOpcode() == AMDIL::LOADCONST_i64
103 ? mMFI
->addi64Literal(immVal
)
104 : mMFI
->addi32Literal(static_cast<int>(immVal
), Opcode
);
105 litOp
.ChangeToImmediate(idx
);
109 if (litOp
.isFPImm()) {
110 const ConstantFP
*fpVal
= litOp
.getFPImm();
111 uint32_t idx
= MI
->getOpcode() == AMDIL::LOADCONST_f64
112 ? mMFI
->addf64Literal(fpVal
)
113 : mMFI
->addf32Literal(fpVal
);
114 litOp
.ChangeToImmediate(idx
);
121 const char* AMDILLiteralManager::getPassName() const {
122 return "AMDIL Constant Propagation";