@@ -72,14 +72,6 @@ namespace llvm {
72
72
// / anywhere in the function.
73
73
std::map<const AllocaInst*, int > StaticAllocaMap;
74
74
75
- // / BlockLocalArguments - If any arguments are only used in a single basic
76
- // / block, and if the target can access the arguments without side-effects,
77
- // / avoid emitting CopyToReg nodes for those arguments. This map keeps
78
- // / track of which arguments are local to each BB.
79
- std::multimap<BasicBlock*, std::pair<Argument*,
80
- unsigned > > BlockLocalArguments;
81
-
82
-
83
75
unsigned MakeReg (MVT::ValueType VT) {
84
76
return RegMap->createVirtualRegister (TLI.getRegClassFor (VT));
85
77
}
@@ -125,17 +117,30 @@ static bool isUsedOutsideOfDefiningBlock(Instruction *I) {
125
117
return false ;
126
118
}
127
119
120
+ // / isOnlyUsedInEntryBlock - If the specified argument is only used in the
121
+ // / entry block, return true.
122
+ static bool isOnlyUsedInEntryBlock (Argument *A) {
123
+ BasicBlock *Entry = A->getParent ()->begin ();
124
+ for (Value::use_iterator UI = A->use_begin (), E = A->use_end (); UI != E; ++UI)
125
+ if (cast<Instruction>(*UI)->getParent () != Entry)
126
+ return false ; // Use not in entry block.
127
+ return true ;
128
+ }
129
+
128
130
FunctionLoweringInfo::FunctionLoweringInfo (TargetLowering &tli,
129
131
Function &fn, MachineFunction &mf)
130
132
: TLI(tli), Fn(fn), MF(mf), RegMap(MF.getSSARegMap()) {
131
133
132
- // Initialize the mapping of values to registers. This is only set up for
133
- // instruction values that are used outside of the block that defines
134
- // them.
134
+ // Create a vreg for each argument register that is not dead and is used
135
+ // outside of the entry block for the function.
135
136
for (Function::arg_iterator AI = Fn.arg_begin (), E = Fn.arg_end ();
136
137
AI != E; ++AI)
137
- InitializeRegForValue (AI);
138
+ if (!isOnlyUsedInEntryBlock (AI))
139
+ InitializeRegForValue (AI);
138
140
141
+ // Initialize the mapping of values to registers. This is only set up for
142
+ // instruction values that are used outside of the block that defines
143
+ // them.
139
144
Function::iterator BB = Fn.begin (), EB = Fn.end ();
140
145
for (BasicBlock::iterator I = BB->begin (), E = BB->end (); I != E; ++I)
141
146
if (AllocaInst *AI = dyn_cast<AllocaInst>(I))
@@ -1072,104 +1077,45 @@ CopyValueToVirtualRegister(SelectionDAGLowering &SDL, Value *V, unsigned Reg) {
1072
1077
}
1073
1078
}
1074
1079
1075
- // / IsOnlyUsedInOneBasicBlock - If the specified argument is only used in a
1076
- // / single basic block, return that block. Otherwise, return a null pointer.
1077
- static BasicBlock *IsOnlyUsedInOneBasicBlock (Argument *A) {
1078
- if (A->use_empty ()) return 0 ;
1079
- BasicBlock *BB = cast<Instruction>(A->use_back ())->getParent ();
1080
- for (Argument::use_iterator UI = A->use_begin (), E = A->use_end (); UI != E;
1081
- ++UI)
1082
- if (isa<PHINode>(*UI) || cast<Instruction>(*UI)->getParent () != BB)
1083
- return 0 ; // Disagreement among the users?
1084
-
1085
- // Okay, there is a single BB user. Only permit this optimization if this is
1086
- // the entry block, otherwise, we might sink argument loads into loops and
1087
- // stuff. Later, when we have global instruction selection, this won't be an
1088
- // issue clearly.
1089
- if (BB == BB->getParent ()->begin ())
1090
- return BB;
1091
- return 0 ;
1092
- }
1093
-
1094
1080
void SelectionDAGISel::
1095
1081
LowerArguments (BasicBlock *BB, SelectionDAGLowering &SDL,
1096
1082
std::vector<SDOperand> &UnorderedChains) {
1097
1083
// If this is the entry block, emit arguments.
1098
1084
Function &F = *BB->getParent ();
1099
1085
FunctionLoweringInfo &FuncInfo = SDL.FuncInfo ;
1100
-
1101
- if (BB == &F.front ()) {
1102
- SDOperand OldRoot = SDL.DAG .getRoot ();
1103
-
1104
- std::vector<SDOperand> Args = TLI.LowerArguments (F, SDL.DAG );
1105
-
1106
- // If there were side effects accessing the argument list, do not do
1107
- // anything special.
1108
- if (OldRoot != SDL.DAG .getRoot ()) {
1109
- unsigned a = 0 ;
1110
- for (Function::arg_iterator AI = F.arg_begin (), E = F.arg_end ();
1111
- AI != E; ++AI,++a)
1112
- if (!AI->use_empty ()) {
1113
- SDL.setValue (AI, Args[a]);
1114
-
1115
- SDOperand Copy =
1116
- CopyValueToVirtualRegister (SDL, AI, FuncInfo.ValueMap [AI]);
1117
- UnorderedChains.push_back (Copy);
1118
- }
1119
- } else {
1120
- // Otherwise, if any argument is only accessed in a single basic block,
1121
- // emit that argument only to that basic block.
1122
- unsigned a = 0 ;
1123
- for (Function::arg_iterator AI = F.arg_begin (), E = F.arg_end ();
1124
- AI != E; ++AI,++a)
1125
- if (!AI->use_empty ()) {
1126
- if (BasicBlock *BBU = IsOnlyUsedInOneBasicBlock (AI)) {
1127
- FuncInfo.BlockLocalArguments .insert (std::make_pair (BBU,
1128
- std::make_pair (AI, a)));
1129
- } else {
1130
- SDL.setValue (AI, Args[a]);
1131
- SDOperand Copy =
1132
- CopyValueToVirtualRegister (SDL, AI, FuncInfo.ValueMap [AI]);
1133
- UnorderedChains.push_back (Copy);
1134
- }
1135
- }
1136
- }
1137
-
1138
- // Next, if the function has live ins that need to be copied into vregs,
1139
- // emit the copies now, into the top of the block.
1140
- MachineFunction &MF = SDL.DAG .getMachineFunction ();
1141
- if (MF.livein_begin () != MF.livein_end ()) {
1142
- SSARegMap *RegMap = MF.getSSARegMap ();
1143
- const MRegisterInfo &MRI = *MF.getTarget ().getRegisterInfo ();
1144
- for (MachineFunction::livein_iterator LI = MF.livein_begin (),
1145
- E = MF.livein_end (); LI != E; ++LI)
1146
- if (LI->second )
1147
- MRI.copyRegToReg (*MF.begin (), MF.begin ()->end (), LI->second ,
1148
- LI->first , RegMap->getRegClass (LI->second ));
1149
- }
1086
+ SDOperand OldRoot = SDL.DAG .getRoot ();
1087
+ std::vector<SDOperand> Args = TLI.LowerArguments (F, SDL.DAG );
1088
+
1089
+ unsigned a = 0 ;
1090
+ for (Function::arg_iterator AI = F.arg_begin (), E = F.arg_end ();
1091
+ AI != E; ++AI, ++a)
1092
+ if (!AI->use_empty ()) {
1093
+ SDL.setValue (AI, Args[a]);
1150
1094
1151
- // Finally, if the target has anything special to do, allow it to do so.
1152
- EmitFunctionEntryCode (F, SDL.DAG .getMachineFunction ());
1153
- }
1154
-
1155
- // See if there are any block-local arguments that need to be emitted in this
1156
- // block.
1157
-
1158
- if (!FuncInfo.BlockLocalArguments .empty ()) {
1159
- std::multimap<BasicBlock*, std::pair<Argument*, unsigned > >::iterator BLAI =
1160
- FuncInfo.BlockLocalArguments .lower_bound (BB);
1161
- if (BLAI != FuncInfo.BlockLocalArguments .end () && BLAI->first == BB) {
1162
- // Lower the arguments into this block.
1163
- std::vector<SDOperand> Args = TLI.LowerArguments (F, SDL.DAG );
1164
-
1165
- // Set up the value mapping for the local arguments.
1166
- for (; BLAI != FuncInfo.BlockLocalArguments .end () && BLAI->first == BB;
1167
- ++BLAI)
1168
- SDL.setValue (BLAI->second .first , Args[BLAI->second .second ]);
1169
-
1170
- // Any dead arguments will just be ignored here.
1095
+ // If this argument is live outside of the entry block, insert a copy from
1096
+ // whereever we got it to the vreg that other BB's will reference it as.
1097
+ if (FuncInfo.ValueMap .count (AI)) {
1098
+ SDOperand Copy =
1099
+ CopyValueToVirtualRegister (SDL, AI, FuncInfo.ValueMap [AI]);
1100
+ UnorderedChains.push_back (Copy);
1101
+ }
1171
1102
}
1103
+
1104
+ // Next, if the function has live ins that need to be copied into vregs,
1105
+ // emit the copies now, into the top of the block.
1106
+ MachineFunction &MF = SDL.DAG .getMachineFunction ();
1107
+ if (MF.livein_begin () != MF.livein_end ()) {
1108
+ SSARegMap *RegMap = MF.getSSARegMap ();
1109
+ const MRegisterInfo &MRI = *MF.getTarget ().getRegisterInfo ();
1110
+ for (MachineFunction::livein_iterator LI = MF.livein_begin (),
1111
+ E = MF.livein_end (); LI != E; ++LI)
1112
+ if (LI->second )
1113
+ MRI.copyRegToReg (*MF.begin (), MF.begin ()->end (), LI->second ,
1114
+ LI->first , RegMap->getRegClass (LI->second ));
1172
1115
}
1116
+
1117
+ // Finally, if the target has anything special to do, allow it to do so.
1118
+ EmitFunctionEntryCode (F, SDL.DAG .getMachineFunction ());
1173
1119
}
1174
1120
1175
1121
@@ -1180,8 +1126,9 @@ void SelectionDAGISel::BuildSelectionDAG(SelectionDAG &DAG, BasicBlock *LLVMBB,
1180
1126
1181
1127
std::vector<SDOperand> UnorderedChains;
1182
1128
1183
- // Lower any arguments needed in this block.
1184
- LowerArguments (LLVMBB, SDL, UnorderedChains);
1129
+ // Lower any arguments needed in this block if this is the entry block.
1130
+ if (LLVMBB == &LLVMBB->getParent ()->front ())
1131
+ LowerArguments (LLVMBB, SDL, UnorderedChains);
1185
1132
1186
1133
BB = FuncInfo.MBBMap [LLVMBB];
1187
1134
SDL.setCurrentBasicBlock (BB);
0 commit comments