llvm.org GIT mirror llvm / fc36a56
reimplement multiple return value handling in IPSCCP, making it more aggressive an correct. This survives building llvm in 64-bit mode with optimizations and the built llvm passes make check. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@85973 91177308-0d34-0410-b5e6-96231b3b80d8 Chris Lattner 9 years ago
2 changed file(s) with 207 addition(s) and 128 deletion(s). Raw diff Collapse all Expand all
158158 SmallPtrSet BBExecutable;// The BBs that are executable.
159159 DenseMap ValueState; // The state each value is in.
160160
161 /// StructValueState - This maintains ValueState for values that have
162 /// StructType, for example for formal arguments, calls, insertelement, etc.
163 ///
164 DenseMap, LatticeVal> StructValueState;
165
161166 /// GlobalValue - If we are tracking any values for the contents of a global
162167 /// variable, we keep a mapping from the constant accessor to the element of
163168 /// the global, to the currently known value. If the value becomes
172177 /// TrackedMultipleRetVals - Same as TrackedRetVals, but used for functions
173178 /// that return multiple values.
174179 DenseMap, LatticeVal> TrackedMultipleRetVals;
180
181 /// MRVFunctionsTracked - Each function in TrackedMultipleRetVals is
182 /// represented here for efficient lookup.
183 SmallPtrSet MRVFunctionsTracked;
175184
176185 /// TrackingIncomingArguments - This is the set of functions for whose
177186 /// arguments we make optimistic assumptions about and try to prove as
218227 /// specified global variable if it can. This is only legal to call if
219228 /// performing Interprocedural SCCP.
220229 void TrackValueOfGlobalVariable(GlobalVariable *GV) {
221 const Type *ElTy = GV->getType()->getElementType();
222 if (ElTy->isFirstClassType()) {
230 // We only track the contents of scalar globals.
231 if (GV->getType()->getElementType()->isSingleValueType()) {
223232 LatticeVal &IV = TrackedGlobals[GV];
224233 if (!isa(GV->getInitializer()))
225234 IV.markConstant(GV->getInitializer());
232241 void AddTrackedFunction(Function *F) {
233242 // Add an entry, F -> undef.
234243 if (const StructType *STy = dyn_cast(F->getReturnType())) {
244 MRVFunctionsTracked.insert(F);
235245 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i)
236246 TrackedMultipleRetVals.insert(std::make_pair(std::make_pair(F, i),
237247 LatticeVal()));
263273 assert(I != ValueState.end() && "V is not in valuemap!");
264274 return I->second;
265275 }
276
277 LatticeVal getStructLatticeValueFor(Value *V, unsigned i) const {
278 DenseMap, LatticeVal>::const_iterator I =
279 StructValueState.find(std::make_pair(V, i));
280 assert(I != StructValueState.end() && "V is not in valuemap!");
281 return I->second;
282 }
266283
267284 /// getTrackedRetVals - Get the inferred return value map.
268285 ///
277294 }
278295
279296 void markOverdefined(Value *V) {
297 assert(!isa(V->getType()) && "Should use other method");
280298 markOverdefined(ValueState[V], V);
281299 }
282300
301 /// markAnythingOverdefined - Mark the specified value overdefined. This
302 /// works with both scalars and structs.
303 void markAnythingOverdefined(Value *V) {
304 if (const StructType *STy = dyn_cast(V->getType()))
305 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i)
306 markOverdefined(getStructValueState(V, i), V);
307 else
308 markOverdefined(V);
309 }
310
283311 private:
284312 // markConstant - Make a value be marked as "constant". If the value
285313 // is not already a constant, add it to the instruction work list so that
292320 }
293321
294322 void markConstant(Value *V, Constant *C) {
323 assert(!isa(V->getType()) && "Should use other method");
295324 markConstant(ValueState[V], V, C);
296325 }
297326
298327 void markForcedConstant(Value *V, Constant *C) {
328 assert(!isa(V->getType()) && "Should use other method");
299329 ValueState[V].markForcedConstant(C);
300330 DEBUG(errs() << "markForcedConstant: " << *C << ": " << *V << '\n');
301331 InstWorkList.push_back(V);
329359 }
330360
331361 void mergeInValue(Value *V, LatticeVal MergeWithV) {
362 assert(!isa(V->getType()) && "Should use other method");
332363 mergeInValue(ValueState[V], V, MergeWithV);
333364 }
334365
337368 /// value. This function handles the case when the value hasn't been seen yet
338369 /// by properly seeding constants etc.
339370 LatticeVal &getValueState(Value *V) {
371 assert(!isa(V->getType()) && "Should use getStructValueState");
372
373 // TODO: Change to do insert+find in one operation.
340374 DenseMap::iterator I = ValueState.find(V);
341 if (I != ValueState.end()) return I->second; // Common case, in the map
375 if (I != ValueState.end())
376 return I->second; // Common case, already in the map.
342377
343378 LatticeVal &LV = ValueState[V];
344379
351386 // All others are underdefined by default.
352387 return LV;
353388 }
389
390 /// getStructValueState - Return the LatticeVal object that corresponds to the
391 /// value/field pair. This function handles the case when the value hasn't
392 /// been seen yet by properly seeding constants etc.
393 LatticeVal &getStructValueState(Value *V, unsigned i) {
394 assert(isa(V->getType()) && "Should use getValueState");
395 assert(i < cast(V->getType())->getNumElements() &&
396 "Invalid element #");
397
398 // TODO: Change to do insert+find in one operation.
399 DenseMap, LatticeVal>::iterator
400 I = StructValueState.find(std::make_pair(V, i));
401 if (I != StructValueState.end())
402 return I->second; // Common case, already in the map.
403
404 LatticeVal &LV = StructValueState[std::make_pair(V, i)];
405
406 if (Constant *C = dyn_cast(V)) {
407 if (isa(C))
408 ; // Undef values remain undefined.
409 else if (ConstantStruct *CS = dyn_cast(C))
410 LV.markConstant(CS->getOperand(i)); // Constants are constant.
411 else if (isa(C)) {
412 const Type *FieldTy = cast(V->getType())->getElementType(i);
413 LV.markConstant(Constant::getNullValue(FieldTy));
414 } else
415 LV.markOverdefined(); // Unknown sort of constant.
416 }
417
418 // All others are underdefined by default.
419 return LV;
420 }
421
354422
355423 /// markEdgeExecutable - Mark a basic block as executable, adding it to the BB
356424 /// work list if it is not already executable.
443511 void visitUnreachableInst(TerminatorInst &I) { /*returns void*/ }
444512 void visitAllocaInst (Instruction &I) { markOverdefined(&I); }
445513 void visitVANextInst (Instruction &I) { markOverdefined(&I); }
446 void visitVAArgInst (Instruction &I) { markOverdefined(&I); }
514 void visitVAArgInst (Instruction &I) { markAnythingOverdefined(&I); }
447515
448516 void visitInstruction(Instruction &I) {
449517 // If a new instruction is added to LLVM that we don't handle.
450518 errs() << "SCCP: Don't know how to handle: " << I;
451 markOverdefined(&I); // Just in case
519 markAnythingOverdefined(&I); // Just in case
452520 }
453521 };
454522
595663 // successors executable.
596664 //
597665 void SCCPSolver::visitPHINode(PHINode &PN) {
666 // If this PN returns a struct, just mark the result overdefined.
667 // TODO: We could do a lot better than this if code actually uses this.
668 if (isa(PN.getType()))
669 return markAnythingOverdefined(&PN);
670
598671 if (getValueState(&PN).isOverdefined()) {
599672 // There may be instructions using this PHI node that are not overdefined
600673 // themselves. If so, make sure that they know that the PHI node operand
616689 // and slow us down a lot. Just mark them overdefined.
617690 if (PN.getNumIncomingValues() > 64)
618691 return markOverdefined(&PN);
619
692
620693 // Look at all of the executable operands of the PHI node. If any of them
621694 // are overdefined, the PHI becomes overdefined as well. If they are all
622695 // constant, and they agree with each other, the PHI becomes the identical
665738 if (I.getNumOperands() == 0) return; // ret void
666739
667740 Function *F = I.getParent()->getParent();
741 Value *ResultOp = I.getOperand(0);
668742
669743 // If we are tracking the return value of this function, merge it in.
670 if (!TrackedRetVals.empty()) {
744 if (!TrackedRetVals.empty() && !isa(ResultOp->getType())) {
671745 DenseMap::iterator TFRVI =
672746 TrackedRetVals.find(F);
673747 if (TFRVI != TrackedRetVals.end()) {
674 mergeInValue(TFRVI->second, F, getValueState(I.getOperand(0)));
748 mergeInValue(TFRVI->second, F, getValueState(ResultOp));
675749 return;
676750 }
677751 }
678752
679753 // Handle functions that return multiple values.
680 if (!TrackedMultipleRetVals.empty() &&
681 isa(I.getOperand(0)->getType())) {
682 for (unsigned i = 0, e = I.getOperand(0)->getType()->getNumContainedTypes();
683 i != e; ++i) {
684 DenseMap, LatticeVal>::iterator
685 It = TrackedMultipleRetVals.find(std::make_pair(F, i));
686 if (It == TrackedMultipleRetVals.end()) break;
687 if (Value *Val = FindInsertedValue(I.getOperand(0), i, I.getContext()))
688 mergeInValue(It->second, F, getValueState(Val));
689 }
754 if (!TrackedMultipleRetVals.empty()) {
755 if (const StructType *STy = dyn_cast(ResultOp->getType()))
756 if (MRVFunctionsTracked.count(F))
757 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i)
758 mergeInValue(TrackedMultipleRetVals[std::make_pair(F, i)], F,
759 getStructValueState(ResultOp, i));
760
690761 }
691762 }
692763
711782 OpSt.getConstant(), I.getType()));
712783 }
713784
785
714786 void SCCPSolver::visitExtractValueInst(ExtractValueInst &EVI) {
715 Value *Aggr = EVI.getAggregateOperand();
716
717 // If the operand to the extractvalue is an undef, the result is undef.
718 if (isa(Aggr))
719 return;
720
721 // Currently only handle single-index extractvalues.
787 // If this returns a struct, mark all elements over defined, we don't track
788 // structs in structs.
789 if (isa(EVI.getType()))
790 return markAnythingOverdefined(&EVI);
791
792 // If this is extracting from more than one level of struct, we don't know.
722793 if (EVI.getNumIndices() != 1)
723794 return markOverdefined(&EVI);
724
725 Function *F = 0;
726 if (CallInst *CI = dyn_cast(Aggr))
727 F = CI->getCalledFunction();
728 else if (InvokeInst *II = dyn_cast(Aggr))
729 F = II->getCalledFunction();
730
731 // TODO: If IPSCCP resolves the callee of this function, we could propagate a
732 // result back!
733 if (F == 0 || TrackedMultipleRetVals.empty())
734 return markOverdefined(&EVI);
735
736 // See if we are tracking the result of the callee. If not tracking this
737 // function (for example, it is a declaration) just move to overdefined.
738 if (!TrackedMultipleRetVals.count(std::make_pair(F, *EVI.idx_begin())))
739 return markOverdefined(&EVI);
740
741 // Otherwise, the value will be merged in here as a result of CallSite
742 // handling.
795
796 Value *AggVal = EVI.getAggregateOperand();
797 unsigned i = *EVI.idx_begin();
798 LatticeVal EltVal = getStructValueState(AggVal, i);
799 mergeInValue(getValueState(&EVI), &EVI, EltVal);
743800 }
744801
745802 void SCCPSolver::visitInsertValueInst(InsertValueInst &IVI) {
803 const StructType *STy = dyn_cast(IVI.getType());
804 if (STy == 0)
805 return markOverdefined(&IVI);
806
807 // If this has more than one index, we can't handle it, drive all results to
808 // undef.
809 if (IVI.getNumIndices() != 1)
810 return markAnythingOverdefined(&IVI);
811
746812 Value *Aggr = IVI.getAggregateOperand();
747 Value *Val = IVI.getInsertedValueOperand();
748
749 // If the operands to the insertvalue are undef, the result is undef.
750 if (isa(Aggr) && isa(Val))
751 return;
752
753 // Currently only handle single-index insertvalues.
754 if (IVI.getNumIndices() != 1)
755 return markOverdefined(&IVI);
756
757 // Currently only handle insertvalue instructions that are in a single-use
758 // chain that builds up a return value.
759 for (const InsertValueInst *TmpIVI = &IVI; ; ) {
760 if (!TmpIVI->hasOneUse())
761 return markOverdefined(&IVI);
762
763 const Value *V = *TmpIVI->use_begin();
764 if (isa(V))
765 break;
766 TmpIVI = dyn_cast(V);
767 if (!TmpIVI)
768 return markOverdefined(&IVI);
769 }
770
771 // See if we are tracking the result of the callee.
772 Function *F = IVI.getParent()->getParent();
773 DenseMap, LatticeVal>::iterator
774 It = TrackedMultipleRetVals.find(std::make_pair(F, *IVI.idx_begin()));
775
776 // Merge in the inserted member value.
777 if (It != TrackedMultipleRetVals.end())
778 mergeInValue(It->second, F, getValueState(Val));
779
780 // Mark the aggregate result of the IVI overdefined; any tracking that we do
781 // will be done on the individual member values.
782 markOverdefined(&IVI);
813 unsigned Idx = *IVI.idx_begin();
814
815 // Compute the result based on what we're inserting.
816 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
817 // This passes through all values that aren't the inserted element.
818 if (i != Idx) {
819 LatticeVal EltVal = getStructValueState(Aggr, i);
820 mergeInValue(getStructValueState(&IVI, i), &IVI, EltVal);
821 continue;
822 }
823
824 Value *Val = IVI.getInsertedValueOperand();
825 if (isa(Val->getType()))
826 // We don't track structs in structs.
827 markOverdefined(getStructValueState(&IVI, i), &IVI);
828 else {
829 LatticeVal InVal = getValueState(Val);
830 mergeInValue(getStructValueState(&IVI, i), &IVI, InVal);
831 }
832 }
783833 }
784834
785835 void SCCPSolver::visitSelectInst(SelectInst &I) {
836 // If this select returns a struct, just mark the result overdefined.
837 // TODO: We could do a lot better than this if code actually uses this.
838 if (isa(I.getType()))
839 return markAnythingOverdefined(&I);
840
786841 LatticeVal CondValue = getValueState(I.getCondition());
787842 if (CondValue.isUndefined())
788843 return;
10101065 }
10111066
10121067 void SCCPSolver::visitExtractElementInst(ExtractElementInst &I) {
1013 // FIXME : SCCP does not handle vectors properly.
1068 // TODO : SCCP does not handle vectors properly.
10141069 return markOverdefined(&I);
10151070
10161071 #if 0
10261081 }
10271082
10281083 void SCCPSolver::visitInsertElementInst(InsertElementInst &I) {
1029 // FIXME : SCCP does not handle vectors properly.
1084 // TODO : SCCP does not handle vectors properly.
10301085 return markOverdefined(&I);
10311086 #if 0
10321087 LatticeVal &ValState = getValueState(I.getOperand(0));
10501105 }
10511106
10521107 void SCCPSolver::visitShuffleVectorInst(ShuffleVectorInst &I) {
1053 // FIXME : SCCP does not handle vectors properly.
1108 // TODO : SCCP does not handle vectors properly.
10541109 return markOverdefined(&I);
10551110 #if 0
10561111 LatticeVal &V1State = getValueState(I.getOperand(0));
11041159 }
11051160
11061161 void SCCPSolver::visitStoreInst(StoreInst &SI) {
1162 // If this store is of a struct, ignore it.
1163 if (isa(SI.getOperand(0)->getType()))
1164 return;
1165
11071166 if (TrackedGlobals.empty() || !isa(SI.getOperand(1)))
11081167 return;
11091168
11211180 // Handle load instructions. If the operand is a constant pointer to a constant
11221181 // global, we can replace the load with the loaded constant value!
11231182 void SCCPSolver::visitLoadInst(LoadInst &I) {
1183 // If this load is of a struct, just mark the result overdefined.
1184 if (isa(I.getType()))
1185 return markAnythingOverdefined(&I);
1186
11241187 LatticeVal PtrVal = getValueState(I.getOperand(0));
11251188 if (PtrVal.isUndefined()) return; // The pointer is not resolved yet!
11261189
11951258 }
11961259
11971260 // Otherwise, we don't know anything about this call, mark it overdefined.
1198 return markOverdefined(I);
1261 return markAnythingOverdefined(I);
11991262 }
12001263
12011264 // If this is a local function that doesn't have its address taken, mark its
12151278 continue;
12161279 }
12171280
1218 mergeInValue(AI, getValueState(*CAI));
1281 if (const StructType *STy = dyn_cast(AI->getType())) {
1282 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i)
1283 mergeInValue(getStructValueState(AI, i), AI,
1284 getStructValueState(*CAI, i));
1285 } else {
1286 mergeInValue(AI, getValueState(*CAI));
1287 }
12191288 }
12201289 }
12211290
12221291 // If this is a single/zero retval case, see if we're tracking the function.
1223 DenseMap::iterator TFRVI = TrackedRetVals.find(F);
1224 if (TFRVI != TrackedRetVals.end()) {
1292 if (const StructType *STy = dyn_cast(F->getReturnType())) {
1293 if (!MRVFunctionsTracked.count(F))
1294 goto CallOverdefined; // Not tracking this callee.
1295
1296 // If we are tracking this callee, propagate the result of the function
1297 // into this call site.
1298 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i)
1299 mergeInValue(getStructValueState(I, i), I,
1300 TrackedMultipleRetVals[std::make_pair(F, i)]);
1301 } else {
1302 DenseMap::iterator TFRVI = TrackedRetVals.find(F);
1303 if (TFRVI == TrackedRetVals.end())
1304 goto CallOverdefined; // Not tracking this callee.
1305
12251306 // If so, propagate the return value of the callee into this call result.
12261307 mergeInValue(I, TFRVI->second);
1227 } else if (isa(I->getType())) {
1228 // Check to see if we're tracking this callee, if not, handle it in the
1229 // common path above.
1230 DenseMap, LatticeVal>::iterator
1231 TMRVI = TrackedMultipleRetVals.find(std::make_pair(F, 0));
1232 if (TMRVI == TrackedMultipleRetVals.end())
1233 goto CallOverdefined;
1234
1235 // Need to mark as overdefined, otherwise it stays undefined which
1236 // creates extractvalue undef,
1237 markOverdefined(I);
1238
1239 // If we are tracking this callee, propagate the return values of the call
1240 // into this call site. We do this by walking all the uses. Single-index
1241 // ExtractValueInst uses can be tracked; anything more complicated is
1242 // currently handled conservatively.
1243 for (Value::use_iterator UI = I->use_begin(), E = I->use_end();
1244 UI != E; ++UI) {
1245 if (ExtractValueInst *EVI = dyn_cast(*UI)) {
1246 if (EVI->getNumIndices() == 1) {
1247 mergeInValue(EVI,
1248 TrackedMultipleRetVals[std::make_pair(F, *EVI->idx_begin())]);
1249 continue;
1250 }
1251 }
1252 // The aggregate value is used in a way not handled here. Assume nothing.
1253 markOverdefined(*UI);
1254 }
1255 } else {
1256 // Otherwise we're not tracking this callee, so handle it in the
1257 // common path above.
1258 goto CallOverdefined;
12591308 }
12601309 }
12611310
12961345 // since all of its users will have already been marked as overdefined.
12971346 // Update all of the users of this instruction's value.
12981347 //
1299 if (!getValueState(I).isOverdefined())
1348 if (isa(I->getType()) || !getValueState(I).isOverdefined())
13001349 for (Value::use_iterator UI = I->use_begin(), E = I->use_end();
13011350 UI != E; ++UI)
13021351 if (Instruction *I = dyn_cast(*UI))
13441393 // Look for instructions which produce undef values.
13451394 if (I->getType()->isVoidTy()) continue;
13461395
1396 if (const StructType *STy = dyn_cast(I->getType())) {
1397 // Only a few things that can be structs matter for undef. Just send
1398 // all their results to overdefined. We could be more precise than this
1399 // but it isn't worth bothering.
1400 if (isa(I) || isa(I)) {
1401 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
1402 LatticeVal &LV = getStructValueState(I, i);
1403 if (LV.isUndefined())
1404 markOverdefined(LV, I);
1405 }
1406 }
1407 continue;
1408 }
1409
13471410 LatticeVal &LV = getValueState(I);
13481411 if (!LV.isUndefined()) continue;
1412
1413 // No instructions using structs need disambiguation.
1414 if (isa(I->getOperand(0)->getType()))
1415 continue;
13491416
13501417 // Get the lattice values of the first two operands for use below.
13511418 LatticeVal Op0LV = getValueState(I->getOperand(0));
13521419 LatticeVal Op1LV;
13531420 if (I->getNumOperands() == 2) {
1421 // No instructions using structs need disambiguation.
1422 if (isa(I->getOperand(1)->getType()))
1423 continue;
1424
13541425 // If this is a two-operand instruction, and if both operands are
13551426 // undefs, the result stays undef.
13561427 Op1LV = getValueState(I->getOperand(1));
15461617
15471618 // Mark all arguments to the function as being overdefined.
15481619 for (Function::arg_iterator AI = F.arg_begin(), E = F.arg_end(); AI != E;++AI)
1549 Solver.markOverdefined(AI);
1620 Solver.markAnythingOverdefined(AI);
15501621
15511622 // Solve for constants.
15521623 bool ResolvedUndefs = true;
15751646 for (BasicBlock::iterator BI = BB->begin(), E = BB->end(); BI != E; ) {
15761647 Instruction *Inst = BI++;
15771648 if (Inst->getType()->isVoidTy() || isa(Inst))
1649 continue;
1650
1651 // TODO: Reconstruct structs from their elements.
1652 if (isa(Inst->getType()))
15781653 continue;
15791654
15801655 LatticeVal IV = Solver.getLatticeValueFor(Inst);
16601735
16611736 // If this is a strong or ODR definition of this function, then we can
16621737 // propagate information about its result into callsites of it.
1663 if (!F->mayBeOverridden() &&
1664 !isa(F->getReturnType()))
1738 if (!F->mayBeOverridden())
16651739 Solver.AddTrackedFunction(F);
16661740
16671741 // If this function only has direct calls that we can see, we can track its
16781752 // Assume nothing about the incoming arguments.
16791753 for (Function::arg_iterator AI = F->arg_begin(), E = F->arg_end();
16801754 AI != E; ++AI)
1681 Solver.markOverdefined(AI);
1755 Solver.markAnythingOverdefined(AI);
16821756 }
16831757
16841758 // Loop over global variables. We inform the solver about any internal global
17111785 if (Solver.isBlockExecutable(F->begin())) {
17121786 for (Function::arg_iterator AI = F->arg_begin(), E = F->arg_end();
17131787 AI != E; ++AI) {
1714 if (AI->use_empty()) continue;
1788 if (AI->use_empty() || isa(AI->getType())) continue;
17151789
1790 // TODO: Could use getStructLatticeValueFor to find out if the entire
1791 // result is a constant and replace it entirely if so.
1792
17161793 LatticeVal IV = Solver.getLatticeValueFor(AI);
17171794 if (IV.isOverdefined()) continue;
17181795
17511828
17521829 for (BasicBlock::iterator BI = BB->begin(), E = BB->end(); BI != E; ) {
17531830 Instruction *Inst = BI++;
1754 if (Inst->getType()->isVoidTy())
1831 if (Inst->getType()->isVoidTy() || isa(Inst->getType()))
17551832 continue;
1833
1834 // TODO: Could use getStructLatticeValueFor to find out if the entire
1835 // result is a constant and replace it entirely if so.
17561836
17571837 LatticeVal IV = Solver.getLatticeValueFor(Inst);
17581838 if (IV.isOverdefined())
0 ; RUN: opt < %s -ipsccp -S | FileCheck %s
1 ; XFAIL: *
21
32 ;;======================== test1
43
127126 ; CHECK: define i64 @test5b()
128127 ; CHECK: A:
129128 ; CHECK-NEXT: %c = call i64 @test5c(%0 %a)
130 ; CHECK-NEXT: ret i64 %c
129 ; CHECK-NEXT: ret i64 5
131130
132131 define internal i64 @test5c({i64,i64} %a) {
133132 %b = extractvalue {i64,i64} %a, 0