llvm.org GIT mirror llvm / 3523993
reuse negates where possible instead of always creating them from scratch. This allows us to optimize test12 into: define i32 @test12(i32 %X) { %factor = mul i32 %X, -3 ; <i32> [#uses=1] %Z = add i32 %factor, 6 ; <i32> [#uses=1] ret i32 %Z } instead of: define i32 @test12(i32 %X) { %Y = sub i32 6, %X ; <i32> [#uses=1] %C = sub i32 %Y, %X ; <i32> [#uses=1] %Z = sub i32 %C, %X ; <i32> [#uses=1] ret i32 %Z } git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@92373 91177308-0d34-0410-b5e6-96231b3b80d8 Chris Lattner 9 years ago
2 changed file(s) with 43 addition(s) and 1 deletion(s). Raw diff Collapse all Expand all
375375 // that should be processed next by the reassociation pass.
376376 //
377377 static Value *NegateValue(Value *V, Instruction *BI) {
378 if (Constant *C = dyn_cast(V))
379 return ConstantExpr::getNeg(C);
380
378381 // We are trying to expose opportunity for reassociation. One of the things
379382 // that we want to do to achieve this is to push a negation as deep into an
380383 // expression chain as possible, to expose the add instructions. In practice,
399402 I->setName(I->getName()+".neg");
400403 return I;
401404 }
405
406 // Okay, we need to materialize a negated version of V with an instruction.
407 // Scan the use lists of V to see if we have one already.
408 for (Value::use_iterator UI = V->use_begin(), E = V->use_end(); UI != E;++UI){
409 if (!BinaryOperator::isNeg(*UI)) continue;
410
411 // We found one! Now we have to make sure that the definition dominates
412 // this use. We do this by moving it to the entry block (if it is a
413 // non-instruction value) or right after the definition. These negates will
414 // be zapped by reassociate later, so we don't need much finesse here.
415 BinaryOperator *TheNeg = cast(*UI);
416
417 BasicBlock::iterator InsertPt;
418 if (Instruction *InstInput = dyn_cast(V)) {
419 if (InvokeInst *II = dyn_cast(InstInput)) {
420 InsertPt = II->getNormalDest()->begin();
421 } else {
422 InsertPt = InstInput;
423 ++InsertPt;
424 }
425 while (isa(InsertPt)) ++InsertPt;
426 } else {
427 InsertPt = TheNeg->getParent()->getParent()->getEntryBlock().begin();
428 }
429 TheNeg->moveBefore(InsertPt);
430 return TheNeg;
431 }
402432
403433 // Insert a 'neg' instruction that subtracts the value from zero to get the
404434 // negation.
405 //
406435 return BinaryOperator::CreateNeg(V, V->getName() + ".neg", BI);
407436 }
408437
164164 ; CHECK-NEXT: ret i32
165165 }
166166
167 define i32 @test12(i32 %X) {
168 %A = sub i32 1, %X
169 %B = sub i32 2, %X
170 %C = sub i32 3, %X
167171
172 %Y = add i32 %A ,%B
173 %Z = add i32 %Y, %C
174 ret i32 %Z
175 ; CHECK: @test12
176 ; CHECK-NEXT: mul i32 %X, -3
177 ; CHECK-NEXT: add i32{{.*}}, 6
178 ; CHECK-NEXT: ret i32
179 }
180