llvm.org GIT mirror llvm / 5eee427
Fix some bugs in SimplifyNodeWithTwoResults where it would call deletenode to delete a node even if it was not dead in some cases. Instead, just add it to the worklist. Also, make sure to use the CombineTo methods, as it was doing things that were unsafe: the top level combine loop could touch dangling memory. This fixes CodeGen/Generic/2008-01-25-dag-combine-mul.ll git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@46384 91177308-0d34-0410-b5e6-96231b3b80d8 Chris Lattner 12 years ago
2 changed file(s) with 63 addition(s) and 42 deletion(s). Raw diff Collapse all Expand all
276276 bool NotExtCompare = false);
277277 SDOperand SimplifySetCC(MVT::ValueType VT, SDOperand N0, SDOperand N1,
278278 ISD::CondCode Cond, bool foldBooleans = true);
279 bool SimplifyNodeWithTwoResults(SDNode *N, unsigned LoOp, unsigned HiOp);
279 SDOperand SimplifyNodeWithTwoResults(SDNode *N, unsigned LoOp,
280 unsigned HiOp);
280281 SDOperand ConstantFoldBIT_CONVERTofBUILD_VECTOR(SDNode *, MVT::ValueType);
281282 SDOperand BuildSDIV(SDNode *N);
282283 SDOperand BuildUDIV(SDNode *N);
585586 continue;
586587
587588 ++NodesCombined;
589
588590 // If we get back the same node we passed in, rather than a new node or
589591 // zero, we know that the node must have defined multiple values and
590592 // CombineTo was used. Since CombineTo takes care of the worklist
603605 if (N->getNumValues() == RV.Val->getNumValues())
604606 DAG.ReplaceAllUsesWith(N, RV.Val, &NowDead);
605607 else {
606 assert(N->getValueType(0) == RV.getValueType() && "Type mismatch");
608 assert(N->getValueType(0) == RV.getValueType() &&
609 N->getNumValues() == 1 && "Type mismatch");
607610 SDOperand OpV = RV;
608611 DAG.ReplaceAllUsesWith(N, &OpV, &NowDead);
609612 }
13101313 // X%C to the equivalent of X-X/C*C.
13111314 if (N1C && !N1C->isNullValue()) {
13121315 SDOperand Div = DAG.getNode(ISD::SDIV, VT, N0, N1);
1316 AddToWorkList(Div.Val);
13131317 SDOperand OptimizedDiv = combine(Div.Val);
13141318 if (OptimizedDiv.Val && OptimizedDiv.Val != Div.Val) {
13151319 SDOperand Mul = DAG.getNode(ISD::MUL, VT, OptimizedDiv, N1);
14201424 /// compute two values. LoOp and HiOp give the opcodes for the two computations
14211425 /// that are being performed. Return true if a simplification was made.
14221426 ///
1423 bool DAGCombiner::SimplifyNodeWithTwoResults(SDNode *N,
1424 unsigned LoOp, unsigned HiOp) {
1427 SDOperand DAGCombiner::SimplifyNodeWithTwoResults(SDNode *N, unsigned LoOp,
1428 unsigned HiOp) {
14251429 // If the high half is not needed, just compute the low half.
14261430 bool HiExists = N->hasAnyUseOfValue(1);
14271431 if (!HiExists &&
14281432 (!AfterLegalize ||
14291433 TLI.isOperationLegal(LoOp, N->getValueType(0)))) {
1430 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 0),
1431 DAG.getNode(LoOp, N->getValueType(0),
1432 N->op_begin(),
1433 N->getNumOperands()));
1434 return true;
1434 SDOperand Res = DAG.getNode(LoOp, N->getValueType(0), N->op_begin(),
1435 N->getNumOperands());
1436 return CombineTo(N, Res, Res);
14351437 }
14361438
14371439 // If the low half is not needed, just compute the high half.
14391441 if (!LoExists &&
14401442 (!AfterLegalize ||
14411443 TLI.isOperationLegal(HiOp, N->getValueType(1)))) {
1442 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 1),
1443 DAG.getNode(HiOp, N->getValueType(1),
1444 N->op_begin(),
1445 N->getNumOperands()));
1446 return true;
1444 SDOperand Res = DAG.getNode(HiOp, N->getValueType(1), N->op_begin(),
1445 N->getNumOperands());
1446 return CombineTo(N, Res, Res);
14471447 }
14481448
14491449 // If both halves are used, return as it is.
14501450 if (LoExists && HiExists)
1451 return false;
1451 return SDOperand();
14521452
14531453 // If the two computed results can be simplified separately, separate them.
1454 bool RetVal = false;
14551454 if (LoExists) {
14561455 SDOperand Lo = DAG.getNode(LoOp, N->getValueType(0),
14571456 N->op_begin(), N->getNumOperands());
1457 AddToWorkList(Lo.Val);
14581458 SDOperand LoOpt = combine(Lo.Val);
1459 if (LoOpt.Val && LoOpt != Lo &&
1460 TLI.isOperationLegal(LoOpt.getOpcode(), LoOpt.getValueType())) {
1461 RetVal = true;
1462 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 0), LoOpt);
1463 } else
1464 DAG.DeleteNode(Lo.Val);
1459 if (LoOpt.Val && LoOpt.Val != Lo.Val &&
1460 TLI.isOperationLegal(LoOpt.getOpcode(), LoOpt.getValueType()))
1461 return CombineTo(N, LoOpt, LoOpt);
14651462 }
14661463
14671464 if (HiExists) {
14681465 SDOperand Hi = DAG.getNode(HiOp, N->getValueType(1),
14691466 N->op_begin(), N->getNumOperands());
1467 AddToWorkList(Hi.Val);
14701468 SDOperand HiOpt = combine(Hi.Val);
14711469 if (HiOpt.Val && HiOpt != Hi &&
1472 TLI.isOperationLegal(HiOpt.getOpcode(), HiOpt.getValueType())) {
1473 RetVal = true;
1474 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 1), HiOpt);
1475 } else
1476 DAG.DeleteNode(Hi.Val);
1477 }
1478
1479 return RetVal;
1470 TLI.isOperationLegal(HiOpt.getOpcode(), HiOpt.getValueType()))
1471 return CombineTo(N, HiOpt, HiOpt);
1472 }
1473 return SDOperand();
14801474 }
14811475
14821476 SDOperand DAGCombiner::visitSMUL_LOHI(SDNode *N) {
1483
1484 if (SimplifyNodeWithTwoResults(N, ISD::MUL, ISD::MULHS))
1485 return SDOperand();
1477 SDOperand Res = SimplifyNodeWithTwoResults(N, ISD::MUL, ISD::MULHS);
1478 if (Res.Val) return Res;
14861479
14871480 return SDOperand();
14881481 }
14891482
14901483 SDOperand DAGCombiner::visitUMUL_LOHI(SDNode *N) {
1491
1492 if (SimplifyNodeWithTwoResults(N, ISD::MUL, ISD::MULHU))
1493 return SDOperand();
1484 SDOperand Res = SimplifyNodeWithTwoResults(N, ISD::MUL, ISD::MULHU);
1485 if (Res.Val) return Res;
14941486
14951487 return SDOperand();
14961488 }
14971489
14981490 SDOperand DAGCombiner::visitSDIVREM(SDNode *N) {
1499
1500 if (SimplifyNodeWithTwoResults(N, ISD::SDIV, ISD::SREM))
1501 return SDOperand();
1491 SDOperand Res = SimplifyNodeWithTwoResults(N, ISD::SDIV, ISD::SREM);
1492 if (Res.Val) return Res;
15021493
15031494 return SDOperand();
15041495 }
15051496
15061497 SDOperand DAGCombiner::visitUDIVREM(SDNode *N) {
1507
1508 if (SimplifyNodeWithTwoResults(N, ISD::UDIV, ISD::UREM))
1509 return SDOperand();
1498 SDOperand Res = SimplifyNodeWithTwoResults(N, ISD::UDIV, ISD::UREM);
1499 if (Res.Val) return Res;
15101500
15111501 return SDOperand();
15121502 }
0 ; RUN: llvm-as < %s | llc
1 ; rdar://5707064
2
3 define i32 @f(i16* %pc) {
4 entry:
5 %acc = alloca i64, align 8 ; [#uses=4]
6 %tmp97 = load i64* %acc, align 8 ; [#uses=1]
7 %tmp98 = and i64 %tmp97, 4294967295 ; [#uses=1]
8 %tmp99 = load i64* null, align 8 ; [#uses=1]
9 %tmp100 = and i64 %tmp99, 4294967295 ; [#uses=1]
10 %tmp101 = mul i64 %tmp98, %tmp100 ; [#uses=1]
11 %tmp103 = lshr i64 %tmp101, 0 ; [#uses=1]
12 %tmp104 = load i64* %acc, align 8 ; [#uses=1]
13 %.cast105 = zext i32 32 to i64 ; [#uses=1]
14 %tmp106 = lshr i64 %tmp104, %.cast105 ; [#uses=1]
15 %tmp107 = load i64* null, align 8 ; [#uses=1]
16 %tmp108 = and i64 %tmp107, 4294967295 ; [#uses=1]
17 %tmp109 = mul i64 %tmp106, %tmp108 ; [#uses=1]
18 %tmp112 = add i64 %tmp109, 0 ; [#uses=1]
19 %tmp116 = add i64 %tmp112, 0 ; [#uses=1]
20 %tmp117 = add i64 %tmp103, %tmp116 ; [#uses=1]
21 %tmp118 = load i64* %acc, align 8 ; [#uses=1]
22 %tmp120 = lshr i64 %tmp118, 0 ; [#uses=1]
23 %tmp121 = load i64* null, align 8 ; [#uses=1]
24 %tmp123 = lshr i64 %tmp121, 0 ; [#uses=1]
25 %tmp124 = mul i64 %tmp120, %tmp123 ; [#uses=1]
26 %tmp126 = shl i64 %tmp124, 0 ; [#uses=1]
27 %tmp127 = add i64 %tmp117, %tmp126 ; [#uses=1]
28 store i64 %tmp127, i64* %acc, align 8
29 ret i32 0
30 }